1 #define cvar_base "g_turrets_unit_"
4 float turret_customizeentityforclient()
8 float Turret_SendEntity(entity to, float sf)
11 WriteByte(MSG_ENTITY, ENT_CLIENT_TURRET);
12 WriteCoord(MSG_ENTITY, self.tur_head.angles_x);
13 WriteCoord(MSG_ENTITY, self.tur_head.angles_y);
14 WriteByte(MSG_ENTITY, self.tur_head.frame);
16 //WriteCoord(MSG_ENTITY, self.tur_head.angles_z);
22 void load_unit_settings(entity ent,string unitname,float is_reload)
26 // dprint("Reloading turret ",e_turret.netname,"\n");
31 if not (ent.turret_scale_damage) ent.turret_scale_damage = 1;
32 if not (ent.turret_scale_range) ent.turret_scale_range = 1;
33 if not (ent.turret_scale_refire) ent.turret_scale_refire = 1;
34 if not (ent.turret_scale_ammo) ent.turret_scale_ammo = 1;
35 if not (ent.turret_scale_aim) ent.turret_scale_aim = 1;
36 if not (ent.turret_scale_health) ent.turret_scale_health = 1;
37 if not (ent.turret_scale_respawn) ent.turret_scale_respawn = 1;
39 sbase = strcat(cvar_base,unitname);
43 ent.tur_head.avelocity = '0 0 0';
45 ent.tur_head.angles = '0 0 0';
48 ent.health = cvar(strcat(sbase,"_health")) * ent.turret_scale_health;
49 ent.respawntime = cvar(strcat(sbase,"_respawntime")) * ent.turret_scale_respawn;
51 ent.shot_dmg = cvar(strcat(sbase,"_shot_dmg")) * ent.turret_scale_damage;
52 ent.shot_refire = cvar(strcat(sbase,"_shot_refire")) * ent.turret_scale_refire;
53 ent.shot_radius = cvar(strcat(sbase,"_shot_radius")) * ent.turret_scale_damage;
54 ent.shot_speed = cvar(strcat(sbase,"_shot_speed"));
55 ent.shot_spread = cvar(strcat(sbase,"_shot_spread"));
56 ent.shot_force = cvar(strcat(sbase,"_shot_force")) * ent.turret_scale_damage;
57 ent.shot_volly = cvar(strcat(sbase,"_shot_volly"));
58 ent.shot_volly_refire = cvar(strcat(sbase,"_shot_volly_refire")) * ent.turret_scale_refire;
60 ent.target_range = cvar(strcat(sbase,"_target_range")) * ent.turret_scale_range;
61 ent.target_range_min = cvar(strcat(sbase,"_target_range_min")) * ent.turret_scale_range;
62 //ent.target_range_fire = cvar(strcat(sbase,"_target_range_fire")) * ent.turret_scale_range;
63 ent.target_range_optimal = cvar(strcat(sbase,"_target_range_optimal")) * ent.turret_scale_range;
65 ent.target_select_rangebias = cvar(strcat(sbase,"_target_select_rangebias"));
66 ent.target_select_samebias = cvar(strcat(sbase,"_target_select_samebias"));
67 ent.target_select_anglebias = cvar(strcat(sbase,"_target_select_anglebias"));
68 ent.target_select_playerbias = cvar(strcat(sbase,"_target_select_playerbias"));
69 //ent.target_select_fov = cvar(cvar_gets(sbase,"_target_select_fov"));
71 ent.ammo_max = cvar(strcat(sbase,"_ammo_max")) * ent.turret_scale_ammo;
72 ent.ammo_recharge = cvar(strcat(sbase,"_ammo_recharge")) * ent.turret_scale_ammo;
74 ent.aim_firetolerance_dist = cvar(strcat(sbase,"_aim_firetolerance_dist"));
75 ent.aim_speed = cvar(strcat(sbase,"_aim_speed")) * ent.turret_scale_aim;
76 ent.aim_maxrot = cvar(strcat(sbase,"_aim_maxrot"));
77 ent.aim_maxpitch = cvar(strcat(sbase,"_aim_maxpitch"));
79 ent.track_type = cvar(strcat(sbase,"_track_type"));
80 ent.track_accel_pitch = cvar(strcat(sbase,"_track_accel_pitch"));
81 ent.track_accel_rot = cvar(strcat(sbase,"_track_accel_rot"));
82 ent.track_blendrate = cvar(strcat(sbase,"_track_blendrate"));
85 if(ent.turret_respawnhook)
86 ent.turret_respawnhook();
91 float turret_stdproc_true()
96 float turret_stdproc_false()
102 void turret_stdproc_nothing()
109 ** updates enemy distances, predicted impact point/time
110 ** and updated aim<->predict impact distance.
112 void turret_do_updates(entity t_turret)
114 vector enemy_pos,oldpos;
120 enemy_pos = real_origin(self.enemy);
122 turret_tag_fire_update();
124 self.tur_shotdir_updated = normalize(v_forward);
126 self.tur_dist_enemy = vlen(self.tur_shotorg - enemy_pos);
127 self.tur_dist_aimpos = vlen(self.tur_shotorg - self.tur_aimpos);
129 if((self.firecheck_flags & TFL_FIRECHECK_VERIFIED) && (self.enemy))
131 oldpos = self.enemy.origin;
132 setorigin(self.enemy,self.tur_aimpos);
133 tracebox(self.tur_shotorg, '-1 -1 -1','1 1 1',self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos),MOVE_NORMAL,self);
134 setorigin(self.enemy,oldpos);
136 if(trace_ent == self.enemy)
137 self.tur_dist_impact_to_aimpos = 0;
139 self.tur_dist_impact_to_aimpos = vlen(trace_endpos - self.tur_aimpos);// - (vlen(self.enemy.maxs - self.enemy.mins)*0.5);
141 self.tur_impactent = trace_ent;
142 self.tur_impacttime = vlen(self.tur_shotorg - trace_endpos) / self.shot_speed;
146 tracebox(self.tur_shotorg, '-1 -1 -1','1 1 1',self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos),MOVE_NORMAL,self);
147 //traceline(self.tur_shotorg, self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos),MOVE_NORMAL,self);
149 self.tur_dist_impact_to_aimpos = vlen(trace_endpos - self.tur_aimpos) - (vlen(self.enemy.maxs - self.enemy.mins)*0.5);
150 self.tur_impactent = trace_ent;
151 self.tur_impacttime = vlen(self.tur_shotorg - trace_endpos) / self.shot_speed;
158 vector turret_fovsearch_pingpong()
161 if(self.phase < time)
163 if( self.tur_head.phase )
164 self.tur_head.phase = 0;
166 self.tur_head.phase = 1;
167 self.phase = time + 5;
170 if( self.tur_head.phase)
171 wish_angle = self.idle_aim + '0 1 0' * (self.aim_maxrot * (self.target_select_fov / 360));
173 wish_angle = self.idle_aim - '0 1 0' * (self.aim_maxrot * (self.target_select_fov / 360));
178 vector turret_fovsearch_steprot()
183 wish_angle = self.tur_head.angles;
184 wish_angle_x = self.idle_aim_x;
186 if (self.phase < time)
188 //rot_add = self.aim_maxrot / self.target_select_fov;
189 wish_angle_y += (self.target_select_fov * 2);
191 if(wish_angle_y > 360)
192 wish_angle_y = wish_angle_y - 360;
194 self.phase = time + 1.5;
200 vector turret_fovsearch_random()
204 if (self.phase < time)
206 wish_angle_y = random() * self.aim_maxrot;
210 wish_angle_x = random() * self.aim_maxpitch;
214 self.phase = time + 5;
216 self.tur_aimpos = wish_angle;
219 return self.idle_aim + self.tur_aimpos;
224 ** Handles head rotation according to
225 ** the units .track_type and .track_flags
228 void turret_stdproc_track()
230 vector target_angle; // This is where we want to aim
231 vector move_angle; // This is where we can aim
234 if (self.track_flags == TFL_TRACK_NO)
237 if not (self.tur_active)
238 target_angle = self.idle_aim - ('1 0 0' * self.aim_maxpitch);
239 else if (self.enemy == world)
242 target_angle = self.idle_aim + self.angles;
244 target_angle = vectoangles(normalize(self.tur_aimpos - self.tur_shotorg));
248 // Find the direction
249 target_angle = normalize(self.tur_aimpos - self.tur_shotorg);
250 target_angle = vectoangles(target_angle); // And make a angle
253 self.tur_head.angles_x = safeangle(self.tur_head.angles_x);
254 self.tur_head.angles_y = safeangle(self.tur_head.angles_y);
256 // Find the diffrence between where we currently aim and where we want to aim
257 move_angle = target_angle - (self.angles + self.tur_head.angles);
258 move_angle = shortangle_vxy(move_angle,(self.angles + self.tur_head.angles));
263 switch(self.track_type)
265 case TFL_TRACKTYPE_STEPMOTOR:
266 f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
267 if (self.track_flags & TFL_TRACK_PITCH)
269 self.tur_head.angles_x += bound(-f_tmp,move_angle_x, f_tmp);
270 if(self.tur_head.angles_x > self.aim_maxpitch)
271 self.tur_head.angles_x = self.aim_maxpitch;
273 if(self.tur_head.angles_x < -self.aim_maxpitch)
274 self.tur_head.angles_x = self.aim_maxpitch;
277 if (self.track_flags & TFL_TRACK_ROT)
279 self.tur_head.angles_y += bound(-f_tmp, move_angle_y, f_tmp);
280 if(self.tur_head.angles_y > self.aim_maxrot)
281 self.tur_head.angles_y = self.aim_maxrot;
283 if(self.tur_head.angles_y < -self.aim_maxrot)
284 self.tur_head.angles_y = self.aim_maxrot;
289 case TFL_TRACKTYPE_FLUIDINERTIA:
290 f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
291 move_angle_x = bound(-self.aim_speed, move_angle_x * self.track_accel_pitch * f_tmp,self.aim_speed);
292 move_angle_y = bound(-self.aim_speed, move_angle_y * self.track_accel_rot * f_tmp,self.aim_speed);
293 move_angle = (self.tur_head.avelocity * self.track_blendrate) + (move_angle * (1 - self.track_blendrate));
296 case TFL_TRACKTYPE_FLUIDPRECISE:
298 move_angle_y = bound(-self.aim_speed, move_angle_y, self.aim_speed);
299 move_angle_x = bound(-self.aim_speed, move_angle_x, self.aim_speed);
305 if (self.track_flags & TFL_TRACK_PITCH)
307 self.tur_head.avelocity_x = move_angle_x;
308 if((self.tur_head.angles_x + self.tur_head.avelocity_x * self.ticrate) > self.aim_maxpitch)
310 self.tur_head.avelocity_x = 0;
311 self.tur_head.angles_x = self.aim_maxpitch;
313 if((self.tur_head.angles_x + self.tur_head.avelocity_x * self.ticrate) < -self.aim_maxpitch)
315 self.tur_head.avelocity_x = 0;
316 self.tur_head.angles_x = self.aim_maxpitch;
322 if (self.track_flags & TFL_TRACK_ROT)
324 self.tur_head.avelocity_y = move_angle_y;
326 if((self.tur_head.angles_y + self.tur_head.avelocity_y * self.ticrate) > self.aim_maxrot)
328 self.tur_head.avelocity_y = 0;
329 self.tur_head.angles_y = self.aim_maxrot;
332 if((self.tur_head.angles_y + self.tur_head.avelocity_y * self.ticrate) < -self.aim_maxrot)
334 self.tur_head.avelocity_y = 0;
335 self.tur_head.angles_y = self.aim_maxrot;
348 + TFL_FIRECHECK_WORLD
350 + TFL_FIRECHECK_DISTANCES
352 + TFL_FIRECHECK_AIMDIST
353 + TFL_FIRECHECK_REALDIST
354 - TFL_FIRECHECK_ANGLEDIST
355 - TFL_FIRECHECK_TEAMCECK
357 + TFL_FIRECHECK_OWM_AMMO
358 + TFL_FIRECHECK_OTHER_AMMO
359 + TFL_FIRECHECK_REFIRE
363 ** Preforms pre-fire checks based on the uints firecheck_flags
365 float turret_stdproc_firecheck()
367 // This one just dont care =)
368 if (self.firecheck_flags & TFL_FIRECHECK_NO) return 1;
371 if (self.firecheck_flags & TFL_FIRECHECK_REFIRE)
372 if (self.attack_finished_single >= time) return 0;
374 // Special case: volly fire turret that has to fire a full volly if a shot was fired.
375 if (self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
376 if not (self.volly_counter == self.shot_volly)
379 // Lack of zombies makes shooting dead things unnecessary :P
380 if (self.firecheck_flags & TFL_FIRECHECK_DEAD)
381 if (self.enemy.deadflag != DEAD_NO)
384 // Plz stop killing the world!
385 if (self.firecheck_flags & TFL_FIRECHECK_WORLD)
386 if (self.enemy == world)
390 if (self.firecheck_flags & TFL_FIRECHECK_OWM_AMMO)
391 if (self.ammo < self.shot_dmg)
394 // Other's ammo? (support-supply units)
395 if (self.firecheck_flags & TFL_FIRECHECK_OTHER_AMMO)
396 if (self.enemy.ammo >= self.enemy.ammo_max)
399 if (self.firecheck_flags & TFL_FIRECHECK_DISTANCES)
401 // Not close enougth?
402 //if (self.tur_dist_aimpos > self.target_range_fire) return 0;
405 if (self.tur_dist_aimpos < self.target_range_min)
410 if (self.firecheck_flags & TFL_FIRECHECK_AFF)
411 if (self.tur_impactent.team == self.team)
414 // aim<->predicted impact
415 if (self.firecheck_flags & TFL_FIRECHECK_AIMDIST)
416 if (self.tur_dist_impact_to_aimpos > self.aim_firetolerance_dist)
419 //if (self.tur_impactent != self.enemy)
422 if (self.shot_volly > 1)
423 if (self.volly_counter == self.shot_volly)
424 if (self.ammo < (self.shot_dmg * self.shot_volly))
427 if(self.firecheck_flags & TFL_FIRECHECK_VERIFIED)
428 if(self.tur_impactent != self.enemy)
435 + TFL_TARGETSELECT_NO
436 + TFL_TARGETSELECT_LOS
437 + TFL_TARGETSELECT_PLAYERS
438 + TFL_TARGETSELECT_MISSILES
439 - TFL_TARGETSELECT_TRIGGERTARGET
440 + TFL_TARGETSELECT_ANGLELIMITS
441 + TFL_TARGETSELECT_RANGELIMTS
442 + TFL_TARGETSELECT_TEAMCHECK
443 - TFL_TARGETSELECT_NOBUILTIN
444 + TFL_TARGETSELECT_OWNTEAM
448 ** Evaluate a entity for target valitity based on validate_flags
449 ** NOTE: the caller must check takedamage before calling this, to inline this check.
451 float turret_validate_target(entity e_turret,entity e_target,float validate_flags)
455 //if(!validate_flags & TFL_TARGETSELECT_NOBUILTIN)
458 if(e_target.owner == e_turret)
461 if not(checkpvs(e_target.origin, e_turret))
468 if (substring(e_target.classname, 0, 10) == "onslaught_") // don't attack onslaught targets, that's the player's job!
471 if (validate_flags & TFL_TARGETSELECT_NO)
474 // If only this was used more..
475 if (e_target.flags & FL_NOTARGET)
479 if (e_target.health < 0)
483 if (e_target.flags & FL_CLIENT)
485 if not (validate_flags & TFL_TARGETSELECT_PLAYERS)
488 if (e_target.deadflag != DEAD_NO)
493 if (validate_flags & TFL_TARGETSELECT_NOTURRETS)
494 if (e_target.turret_firefunc || e_target.owner.tur_head == e_target)
495 if(e_target.team != e_turret.team) // Dont break support units.
499 if (e_target.flags & FL_PROJECTILE)
500 if not (validate_flags & TFL_TARGETSELECT_MISSILES)
503 if (validate_flags & TFL_TARGETSELECT_MISSILESONLY)
504 if not (e_target.flags & FL_PROJECTILE)
508 if (validate_flags & TFL_TARGETSELECT_TEAMCHECK)
510 if (validate_flags & TFL_TARGETSELECT_OWNTEAM)
512 if (e_target.team != e_turret.team)
515 if (e_turret.team != e_target.owner.team)
520 if (e_target.team == e_turret.team)
523 if (e_turret.team == e_target.owner.team)
529 tvt_dist = vlen(e_turret.origin - real_origin(e_target));
530 if (validate_flags & TFL_TARGETSELECT_RANGELIMTS)
532 if (tvt_dist < e_turret.target_range_min)
535 if (tvt_dist > e_turret.target_range)
539 // Can we even aim this thing?
540 tvt_thadv = angleofs3(e_turret.tur_head.origin,e_turret.angles + e_turret.tur_head.angles ,e_target);
541 //tvt_thadv = angleofs(e_turret.angles,e_target);
545 tvt_tadv = shortangle_vxy(angleofs(e_turret,e_target),e_turret.angles);
546 tvt_thadf = vlen(tvt_thadv);
547 tvt_tadf = vlen(tvt_tadv);
550 if(validate_flags & TFL_TARGETSELECT_FOV)
552 if(e_turret.target_select_fov < tvt_thadf)
557 if (validate_flags & TFL_TARGETSELECT_ANGLELIMITS)
559 if (fabs(tvt_tadv_x) > e_turret.aim_maxpitch)
562 if (fabs(tvt_tadv_y) > e_turret.aim_maxrot)
567 if (validate_flags & TFL_TARGETSELECT_LOS)
569 v_tmp = real_origin(e_target) + ((e_target.mins + e_target.maxs) * 0.5);
570 traceline(e_turret.tur_shotorg,v_tmp,0,e_turret);
572 if (e_turret.aim_firetolerance_dist < vlen(v_tmp - trace_endpos))
576 if (e_target.classname == "grapplinghook")
580 if (e_target.classname == "func_button")
584 #ifdef TURRET_DEBUG_TARGETSELECT
585 dprint("Target:",e_target.netname," is a valid target for ",e_turret.netname,"\n");
591 entity turret_select_target()
593 entity e; // target looper entity
594 float score; // target looper entity score
595 entity e_enemy; // currently best scoreing target
596 float m_score; // currently best scoreing target's score
601 if(self.enemy.takedamage)
602 if(turret_validate_target(self,self.enemy,self.target_validate_flags) > 0)
604 e_enemy = self.enemy;
605 m_score = self.turret_score_target(self,e_enemy) * self.target_select_samebias;
610 e = findradius(self.origin,self.target_range);
612 // Nothing to aim at?
613 if (!e) return world;
619 f = turret_validate_target(self,e,self.target_select_flags);
622 score = self.turret_score_target(self,e);
623 if ((score > m_score) && (score > 0))
640 self.nextthink = time + self.ticrate;
642 // ONS uses somewhat backwards linking.
648 e = find(world,targetname,self.target);
653 if (self.team != self.tur_head.team)
654 turret_stdproc_respawn();
658 if (self.tur_dbg_tmr1 < time)
660 if (self.enemy) paint_target (self.enemy,128,self.tur_dbg_rvec,0.9);
661 paint_target(self,256,self.tur_dbg_rvec,0.9);
662 self.tur_dbg_tmr1 = time + 1;
667 if not (self.spawnflags & TSF_NO_AMMO_REGEN)
668 if (self.ammo < self.ammo_max)
669 self.ammo = min(self.ammo + self.ammo_recharge,self.ammo_max);
672 // Inactive turrets needs to run the think loop,
673 // So they can handle animation and wake up if need be.
674 if not (self.tur_active)
676 turret_stdproc_track();
680 //This is just wrong :| and unlikely to ever happen.
682 if(self.deadflag != DEAD_NO)
684 dprint("WARNING: dead turret running the think function!\n");
689 // This is typicaly used for zaping every target in range
690 // turret_fusionreactor uses this to recharge friendlys.
691 if (self.shoot_flags & TFL_SHOOT_HITALLVALID)
693 // Do a self.turret_fire for every valid target.
694 e = findradius(self.origin,self.target_range);
699 if (turret_validate_target(self,e,self.target_validate_flags))
703 turret_do_updates(self);
705 if (self.turret_firecheckfunc())
714 else if(self.shoot_flags & TFL_SHOOT_CUSTOM)
716 // This one is doing something.. oddball. assume its handles what needs to be handled.
719 if not(self.aim_flags & TFL_AIM_NO)
720 self.tur_aimpos = turret_stdproc_aim_generic();
723 if not(self.track_flags & TFL_TRACK_NO)
724 turret_stdproc_track();
726 turret_do_updates(self);
729 if (self.turret_firecheckfunc())
734 // Special case for volly always. if it fired once it must compleate the volly.
735 if(self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
736 if(self.volly_counter != self.shot_volly)
738 // Predict or whatnot
739 if not(self.aim_flags & TFL_AIM_NO)
740 self.tur_aimpos = turret_stdproc_aim_generic();
743 if not(self.track_flags & TFL_TRACK_NO)
744 turret_stdproc_track();
746 turret_do_updates(self);
749 if (self.turret_firecheckfunc() != 0)
752 if(self.turret_postthink)
753 self.turret_postthink();
758 // Check if we have a vailid enemy, and try to find one if we dont.
760 // g_turrets_targetscan_maxdelay forces a target re-scan this often
761 float do_target_scan;
762 if((self.target_select_time + cvar("g_turrets_targetscan_maxdelay")) < time)
765 // Old target (if any) invalid?
766 if (turret_validate_target(self,self.enemy,self.target_validate_flags) <= 0)
769 // But never more often then g_turrets_targetscan_mindelay!
770 if (self.target_select_time + cvar("g_turrets_targetscan_mindelay") > time)
775 self.enemy = turret_select_target();
776 self.target_select_time = time;
779 // No target, just go to idle, do any custom stuff and bail.
780 if (self.enemy == world)
783 if not(self.track_flags & TFL_TRACK_NO)
784 turret_stdproc_track();
786 // do any per-turret stuff
787 if(self.turret_postthink)
788 self.turret_postthink();
794 self.lip = time + cvar("g_turrets_aimidle_delay"); // Keep track of the last time we had a target.
797 if not(self.aim_flags & TFL_AIM_NO)
798 self.tur_aimpos = turret_stdproc_aim_generic();
801 if not(self.track_flags & TFL_TRACK_NO)
802 turret_stdproc_track();
804 turret_do_updates(self);
807 if (self.turret_firecheckfunc())
811 // do any per-turret stuff
812 if(self.turret_postthink)
813 self.turret_postthink();
818 if (cvar("g_turrets_nofire") != 0)
822 // unlikely to ever happen.
823 if (self.deadflag != DEAD_NO)
826 if not (self.tur_active)
830 self.turret_firefunc();
832 self.attack_finished_single = time + self.shot_refire;
833 self.ammo -= self.shot_dmg;
834 self.volly_counter = self.volly_counter - 1;
836 if (self.volly_counter <= 0)
838 self.volly_counter = self.shot_volly;
840 if (self.shoot_flags & TFL_SHOOT_CLEARTARGET)
843 if (self.shot_volly > 1)
844 self.attack_finished_single = time + self.shot_volly_refire;
848 if (self.enemy) paint_target3(self.tur_aimpos, 64, self.tur_dbg_rvec, self.tur_impacttime + 0.25);
852 void turret_stdproc_fire()
854 dprint("^1Bang, ^3your dead^7 ",self.enemy.netname,"! ^1(turret with no real firefunc)\n");
858 When .used a turret switch team to activator.team.
859 If activator is world, the turrets goes inactive.
861 void turret_stdproc_use()
863 dprint("Turret ",self.netname, " used by ",activator.classname,"\n");
865 self.team = activator.team;
876 //Net_LinkEntity(self, FALSE, 0, Turret_SendEntity);
877 self.think = turret_think;
878 self.nextthink = time;
881 void turrets_manager_think()
883 self.nextthink = time + 1;
886 if (cvar("g_turrets_reloadcvars") == 1)
891 if (e.turrcaps_flags & TFL_TURRCAPS_ISTURRET)
893 load_unit_settings(e,e.cvar_basename,1);
894 if(e.turret_postthink)
895 e.turret_postthink();
900 cvar_set("g_turrets_reloadcvars","0");
905 * Standard turret initialization. use this!
906 * (unless you have a very good reason not to)
907 * if the return value is 0, the turret should be removed.
909 float turret_stdproc_init (string cvar_base_name, float csqc_shared, string base, string head)
913 // Are turrets allowed atm?
914 if (cvar("g_turrets") == 0)
918 e = find(world, classname, "turret_manager");
923 setorigin(e,'0 0 0');
924 setmodel(e,"models/turrets/plasma.md3");
926 v = gettaginfo(e,gettagindex(e,"tag_fire"));
929 objerror("^1ERROR: Engine is borken! Turrets will NOT work. force g_turrets to 0 to run maps with turrets anyway.");
934 e.classname = "turret_manager";
935 e.think = turrets_manager_think;
936 e.nextthink = time + 2;
941 dprint("WARNING: turret requested csqc_shared but this is not implemented. Expect strange things to happen.\n");
945 // Terrainbase spawnflag. This puts a enlongated model
946 // under the turret, so it looks ok on uneaven surfaces.
947 if (self.spawnflags & TSF_TERRAINBASE)
951 setmodel(tb,"models/turrets/terrainbase.md3");
952 setorigin(tb,self.origin);
953 tb.solid = SOLID_BBOX;
956 self.cvar_basename = cvar_base_name;
957 load_unit_settings(self,self.cvar_basename, 0);
959 // Handle turret teams.
960 if (cvar("g_assault") != 0)
963 self.team = 14; // Assume turrets are on the defending side if not explicitly set otehrwize
965 else if not (teamplay)
966 self.team = MAX_SHOT_DISTANCE; // Group all turrets into the same team iso they dont kill eachother.
967 else if(g_onslaught && self.targetname)
969 e = find(world,target,self.targetname);
977 self.team = MAX_SHOT_DISTANCE; // Group all turrets into the same team iso they dont kill eachother.
980 * Try to guess some reasonaly defaults
981 * for missing params and do sanety checks
982 * thise checks could produce some "interesting" results
983 * if it hits a glitch in my logic :P so try to set as mutch
984 * as possible beforehand.
986 if (self.turrcaps_flags & TFL_TURRCAPS_SUPPORT)
987 if not (self.ticrate) self.ticrate = 0.2; // Support units generaly dont need to have a high speed ai-loop
989 if not (self.ticrate) self.ticrate = 0.1; // 10 fps for normal turrets
991 self.ticrate = bound(sys_frametime,self.ticrate,60); // keep it sane
994 if (self.netname == "")
995 self.netname = self.classname;
997 if not (self.respawntime)
998 self.respawntime = 60;
999 self.respawntime = max(-1,self.respawntime);
1001 if not (self.health)
1003 self.tur_health = max(1,self.health);
1005 if not (self.turrcaps_flags)
1006 self.turrcaps_flags = TFL_TURRCAPS_RADIUSDMG | TFL_TURRCAPS_MEDPROJ | TFL_TURRCAPS_PLAYERKILL;
1008 if (!self.damage_flags)
1009 self.damage_flags = TFL_DMG_YES | TFL_DMG_RETALIATE | TFL_DMG_AIMSHAKE;
1012 if not (self.shot_refire)
1013 self.shot_refire = 1;
1014 self.shot_refire = bound(0.01,self.shot_refire,9999);
1016 if not (self.shot_dmg)
1017 self.shot_dmg = self.shot_refire * 50;
1018 self.shot_dmg = max(1,self.shot_dmg);
1020 if not (self.shot_radius)
1021 self.shot_radius = self.shot_dmg * 0.5;
1022 self.shot_radius = max(1,self.shot_radius);
1024 if not (self.shot_speed)
1025 self.shot_speed = 2500;
1026 self.shot_speed = max(1,self.shot_speed);
1028 if not (self.shot_spread)
1029 self.shot_spread = 0.0125;
1030 self.shot_spread = bound(0.0001,self.shot_spread,500);
1032 if not (self.shot_force)
1033 self.shot_force = self.shot_dmg * 0.5 + self.shot_radius * 0.5;
1034 self.shot_force = bound(0.001,self.shot_force,MAX_SHOT_DISTANCE * 0.5);
1036 if not (self.shot_volly)
1037 self.shot_volly = 1;
1038 self.shot_volly = bound(1,self.shot_volly,floor(self.ammo_max / self.shot_dmg));
1040 if not (self.shot_volly_refire)
1041 self.shot_volly_refire = self.shot_refire * self.shot_volly;
1042 self.shot_volly_refire = bound(self.shot_refire,self.shot_volly_refire,60);
1044 if not (self.firecheck_flags)
1045 self.firecheck_flags = TFL_FIRECHECK_WORLD | TFL_FIRECHECK_DEAD | TFL_FIRECHECK_DISTANCES |
1046 TFL_FIRECHECK_LOS | TFL_FIRECHECK_AIMDIST | TFL_FIRECHECK_TEAMCECK |
1047 TFL_FIRECHECK_OWM_AMMO | TFL_FIRECHECK_REFIRE | TFL_FIRECHECK_WORLD;
1050 if not (self.target_range)
1051 self.target_range = self.shot_speed * 0.5;
1052 self.target_range = bound(0,self.target_range,MAX_SHOT_DISTANCE);
1054 if not (self.target_range_min)
1055 self.target_range_min = self.shot_radius * 2;
1056 self.target_range_min = bound(0,self.target_range_min,MAX_SHOT_DISTANCE);
1058 //if (!self.target_range_fire)
1059 // self.target_range_fire = self.target_range * 0.8;
1060 //self.target_range_fire = bound(0,self.target_range_fire,MAX_SHOT_DISTANCE);
1062 if not (self.target_range_optimal)
1063 self.target_range_optimal = self.target_range * 0.5;
1064 self.target_range_optimal = bound(0,self.target_range_optimal,MAX_SHOT_DISTANCE);
1068 if not (self.aim_maxrot)
1069 self.aim_maxrot = 90;
1070 self.aim_maxrot = bound(0,self.aim_maxrot,360);
1072 if not (self.aim_maxpitch)
1073 self.aim_maxpitch = 20;
1074 self.aim_maxpitch = bound(0,self.aim_maxpitch,90);
1076 if not (self.aim_speed)
1077 self.aim_speed = 36;
1078 self.aim_speed = bound(0.1,self.aim_speed, 1000);
1080 if not (self.aim_firetolerance_dist)
1081 self.aim_firetolerance_dist = 5 + (self.shot_radius * 2);
1082 self.aim_firetolerance_dist = bound(0.1,self.aim_firetolerance_dist,MAX_SHOT_DISTANCE);
1084 if not (self.aim_flags)
1086 self.aim_flags = TFL_AIM_LEAD | TFL_AIM_SHOTTIMECOMPENSATE;
1087 if(self.turrcaps_flags & TFL_TURRCAPS_RADIUSDMG)
1088 self.aim_flags |= TFL_AIM_GROUND2;
1091 // Sill the most tested (and aim-effective)
1092 if not (self.track_type)
1093 self.track_type = TFL_TRACKTYPE_STEPMOTOR;
1095 if (self.track_type != TFL_TRACKTYPE_STEPMOTOR)
1097 // Fluid / Ineria mode. Looks mutch nicer, bit experimental &
1098 // Can inmapt aim preformance alot.
1099 // needs a bit diffrent aimspeed
1101 if not (self.aim_speed)
1102 self.aim_speed = 180;
1103 self.aim_speed = bound(0.1,self.aim_speed, 1000);
1105 if not (self.track_accel_pitch)
1106 self.track_accel_pitch = 0.5;
1108 if not (self.track_accel_rot)
1109 self.track_accel_rot = 0.5;
1111 if not (self.track_blendrate)
1112 self.track_blendrate = 0.35;
1115 if (!self.track_flags)
1116 self.track_flags = TFL_TRACK_PITCH | TFL_TRACK_ROT;
1119 // Target selection stuff.
1120 if not (self.target_select_rangebias)
1121 self.target_select_rangebias = 1;
1122 self.target_select_rangebias = bound(-10,self.target_select_rangebias,10);
1124 if not (self.target_select_samebias)
1125 self.target_select_samebias = 1;
1126 self.target_select_samebias = bound(-10,self.target_select_samebias,10);
1128 if not (self.target_select_anglebias)
1129 self.target_select_anglebias = 1;
1130 self.target_select_anglebias = bound(-10,self.target_select_anglebias,10);
1132 if not (self.target_select_missilebias)
1133 self.target_select_missilebias = -10;
1135 self.target_select_missilebias = bound(-10,self.target_select_missilebias,10);
1136 self.target_select_playerbias = bound(-10,self.target_select_playerbias,10);
1138 if not (self.target_select_flags)
1140 self.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_TEAMCHECK
1141 | TFL_TARGETSELECT_RANGELIMTS | TFL_TARGETSELECT_ANGLELIMITS;
1143 if (self.turrcaps_flags & TFL_TURRCAPS_MISSILEKILL)
1144 self.target_select_flags |= TFL_TARGETSELECT_MISSILES;
1146 if (self.turrcaps_flags & TFL_TURRCAPS_PLAYERKILL)
1147 self.target_select_flags |= TFL_TARGETSELECT_PLAYERS;
1149 // self.target_select_flags = TFL_TARGETSELECT_NO;
1152 self.target_validate_flags = self.target_select_flags;
1156 if not (self.ammo_max)
1157 self.ammo_max = self.shot_dmg * 10;
1158 self.ammo_max = max(self.shot_dmg,self.ammo_max);
1161 self.ammo = self.shot_dmg * 5;
1162 self.ammo = bound(0,self.ammo,self.ammo_max);
1164 if not (self.ammo_recharge)
1165 self.ammo_recharge = self.shot_dmg * 0.5;
1166 self.ammo_recharge = max(0,self.ammo_recharge);
1168 // Convert the recharge from X per sec to X per ticrate
1169 self.ammo_recharge = self.ammo_recharge * self.ticrate;
1171 if not (self.ammo_flags)
1172 self.ammo_flags = TFL_AMMO_ENERGY | TFL_AMMO_RECHARGE;
1175 if(self.spawnflags & TSL_NO_RESPAWN)
1176 if not (self.damage_flags & TFL_DMG_DEATH_NORESPAWN)
1177 self.damage_flags |= TFL_DMG_DEATH_NORESPAWN;
1179 // Offsets & origins
1180 if (!self.tur_shotorg) self.tur_shotorg = '50 0 50';
1182 // End of default & sanety checks, start building the turret.
1185 self.tur_head = spawn();
1186 self.tur_head.netname = self.tur_head.classname = "turret_head";
1187 self.tur_head.team = self.team;
1188 self.tur_head.owner = self;
1190 setmodel(self,base);
1191 setmodel(self.tur_head,head);
1193 setsize(self,'-32 -32 0','32 32 64');
1194 setsize(self.tur_head,'0 0 0','0 0 0');
1196 setorigin(self.tur_head,'0 0 0');
1197 setattachment(self.tur_head, self, "tag_head");
1202 self.tur_health = self.health;
1203 self.solid = SOLID_BBOX;
1204 self.tur_head.solid = SOLID_NOT;
1205 self.takedamage = DAMAGE_AIM;
1206 self.tur_head.takedamage = DAMAGE_NO;
1207 self.movetype = MOVETYPE_NOCLIP;
1208 self.tur_head.movetype = MOVETYPE_NOCLIP;
1211 if not (self.tur_defend)
1212 if (self.target != "")
1214 self.tur_defend = find(world, targetname, self.target);
1215 if (self.tur_defend == world)
1218 dprint("Turret has invalid defendpoint!\n");
1222 // In target defend mode, aim on the spot to defend when idle.
1223 if (self.tur_defend)
1224 self.idle_aim = self.tur_head.angles + angleofs(self.tur_head,self.tur_defend);
1226 self.idle_aim = '0 0 0';
1229 if (self.team == COLOR_TEAM1) self.colormod = '1.4 0.8 0.8';
1230 if (self.team == COLOR_TEAM2) self.colormod = '0.8 0.8 1.4';
1232 // Attach stdprocs. override when and what needed
1233 if (self.turrcaps_flags & TFL_TURRCAPS_SUPPORT)
1235 self.turret_score_target = turret_stdproc_targetscore_support;
1236 self.turret_firecheckfunc = turret_stdproc_firecheck;
1237 self.turret_firefunc = turret_stdproc_fire;
1238 self.event_damage = turret_stdproc_damage;
1242 self.turret_score_target = turret_stdproc_targetscore_generic;
1243 self.turret_firecheckfunc = turret_stdproc_firecheck;
1244 self.turret_firefunc = turret_stdproc_fire;
1245 self.event_damage = turret_stdproc_damage;
1248 self.use = turret_stdproc_use;
1249 self.bot_attack = TRUE;
1251 // Initiate the main AI loop
1253 self.think = turret_link;
1255 self.think = turret_think;
1258 self.nextthink = time + 1;
1259 self.nextthink += turret_count * sys_frametime;
1261 self.tur_head.team = self.team;
1262 self.view_ofs = '0 0 0';
1265 self.tur_dbg_start = self.nextthink;
1266 while (vlen(self.tur_dbg_rvec) < 2)
1267 self.tur_dbg_rvec = randomvec() * 4;
1269 self.tur_dbg_rvec_x = fabs(self.tur_dbg_rvec_x);
1270 self.tur_dbg_rvec_y = fabs(self.tur_dbg_rvec_y);
1271 self.tur_dbg_rvec_z = fabs(self.tur_dbg_rvec_z);
1275 self.turrcaps_flags |= TFL_TURRCAPS_ISTURRET;
1277 self.classname = "turret_main";
1279 self.tur_active = 1;
1281 // In ONS mode, and linked to a ONS ent. need to call the use to set team.
1282 if (g_onslaught && ee)