1 #define cvar_base "g_turrets_unit_"
10 float TNSF_AVEL2 = 128;
12 float TNSF_FULL_UPDATE = 16777215;
13 float turret_send(entity to, float sf)
15 WriteByte(MSG_ENTITY, ENT_CLIENT_TURRET);
17 WriteByte(MSG_ENTITY, sf);
20 WriteByte(MSG_ENTITY, self.turret_type);
22 WriteCoord(MSG_ENTITY, self.origin_x);
23 WriteCoord(MSG_ENTITY, self.origin_y);
24 WriteCoord(MSG_ENTITY, self.origin_z);
26 WriteAngle(MSG_ENTITY, self.angles_x);
27 WriteAngle(MSG_ENTITY, self.angles_y);
32 WriteAngle(MSG_ENTITY, self.tur_head.angles_x);
33 WriteAngle(MSG_ENTITY, self.tur_head.angles_y);
34 WriteAngle(MSG_ENTITY, self.tur_head.avelocity_x);
35 WriteAngle(MSG_ENTITY, self.tur_head.avelocity_y);
40 WriteByte(MSG_ENTITY, self.team);
41 WriteByte(MSG_ENTITY, rint((self.health / self.tur_health) * 255));
47 void load_unit_settings(entity ent, string unitname, float is_reload)
54 if not (ent.turret_scale_damage) ent.turret_scale_damage = 1;
55 if not (ent.turret_scale_range) ent.turret_scale_range = 1;
56 if not (ent.turret_scale_refire) ent.turret_scale_refire = 1;
57 if not (ent.turret_scale_ammo) ent.turret_scale_ammo = 1;
58 if not (ent.turret_scale_aim) ent.turret_scale_aim = 1;
59 if not (ent.turret_scale_health) ent.turret_scale_health = 1;
60 if not (ent.turret_scale_respawn) ent.turret_scale_respawn = 1;
62 sbase = strcat(cvar_base,unitname);
66 ent.tur_head.avelocity = '0 0 0';
68 ent.tur_head.angles = '0 0 0';
71 ent.health = cvar(strcat(sbase,"_health")) * ent.turret_scale_health;
72 ent.respawntime = cvar(strcat(sbase,"_respawntime")) * ent.turret_scale_respawn;
74 ent.shot_dmg = cvar(strcat(sbase,"_shot_dmg")) * ent.turret_scale_damage;
75 ent.shot_refire = cvar(strcat(sbase,"_shot_refire")) * ent.turret_scale_refire;
76 ent.shot_radius = cvar(strcat(sbase,"_shot_radius")) * ent.turret_scale_damage;
77 ent.shot_speed = cvar(strcat(sbase,"_shot_speed"));
78 ent.shot_spread = cvar(strcat(sbase,"_shot_spread"));
79 ent.shot_force = cvar(strcat(sbase,"_shot_force")) * ent.turret_scale_damage;
80 ent.shot_volly = cvar(strcat(sbase,"_shot_volly"));
81 ent.shot_volly_refire = cvar(strcat(sbase,"_shot_volly_refire")) * ent.turret_scale_refire;
83 ent.target_range = cvar(strcat(sbase,"_target_range")) * ent.turret_scale_range;
84 ent.target_range_min = cvar(strcat(sbase,"_target_range_min")) * ent.turret_scale_range;
85 ent.target_range_optimal = cvar(strcat(sbase,"_target_range_optimal")) * ent.turret_scale_range;
86 //ent.target_range_fire = cvar(strcat(sbase,"_target_range_fire")) * ent.turret_scale_range;
88 ent.target_select_rangebias = cvar(strcat(sbase,"_target_select_rangebias"));
89 ent.target_select_samebias = cvar(strcat(sbase,"_target_select_samebias"));
90 ent.target_select_anglebias = cvar(strcat(sbase,"_target_select_anglebias"));
91 ent.target_select_playerbias = cvar(strcat(sbase,"_target_select_playerbias"));
92 //ent.target_select_fov = cvar(cvar_gets(sbase,"_target_select_fov"));
94 ent.ammo_max = cvar(strcat(sbase,"_ammo_max")) * ent.turret_scale_ammo;
95 ent.ammo_recharge = cvar(strcat(sbase,"_ammo_recharge")) * ent.turret_scale_ammo;
97 ent.aim_firetolerance_dist = cvar(strcat(sbase,"_aim_firetolerance_dist"));
98 ent.aim_speed = cvar(strcat(sbase,"_aim_speed")) * ent.turret_scale_aim;
99 ent.aim_maxrot = cvar(strcat(sbase,"_aim_maxrot"));
100 ent.aim_maxpitch = cvar(strcat(sbase,"_aim_maxpitch"));
102 ent.track_type = cvar(strcat(sbase,"_track_type"));
103 ent.track_accel_pitch = cvar(strcat(sbase,"_track_accel_pitch"));
104 ent.track_accel_rot = cvar(strcat(sbase,"_track_accel_rot"));
105 ent.track_blendrate = cvar(strcat(sbase,"_track_blendrate"));
108 if(ent.turret_respawnhook)
109 ent.turret_respawnhook();
114 ** updates enemy distances, predicted impact point/time
115 ** and updated aim<->predict impact distance.
117 void turret_do_updates(entity t_turret)
119 vector enemy_pos, oldpos;
125 enemy_pos = real_origin(self.enemy);
127 turret_tag_fire_update();
129 self.tur_shotdir_updated = v_forward;
130 self.tur_dist_enemy = vlen(self.tur_shotorg - enemy_pos);
131 self.tur_dist_aimpos = vlen(self.tur_shotorg - self.tur_aimpos);
133 if((self.firecheck_flags & TFL_FIRECHECK_VERIFIED) && (self.enemy))
135 oldpos = self.enemy.origin;
136 setorigin(self.enemy,self.tur_aimpos);
137 tracebox(self.tur_shotorg, '-1 -1 -1','1 1 1',self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos),MOVE_NORMAL,self);
138 setorigin(self.enemy,oldpos);
140 if(trace_ent == self.enemy)
141 self.tur_dist_impact_to_aimpos = 0;
143 self.tur_dist_impact_to_aimpos = vlen(trace_endpos - self.tur_aimpos);
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);
148 self.tur_dist_impact_to_aimpos = vlen(trace_endpos - self.tur_aimpos) - (vlen(self.enemy.maxs - self.enemy.mins) * 0.5);
149 self.tur_impactent = trace_ent;
150 self.tur_impacttime = vlen(self.tur_shotorg - trace_endpos) / self.shot_speed;
156 vector turret_fovsearch_pingpong()
159 if(self.phase < time)
161 if( self.tur_head.phase )
162 self.tur_head.phase = 0;
164 self.tur_head.phase = 1;
165 self.phase = time + 5;
168 if( self.tur_head.phase)
169 wish_angle = self.idle_aim + '0 1 0' * (self.aim_maxrot * (self.target_select_fov / 360));
171 wish_angle = self.idle_aim - '0 1 0' * (self.aim_maxrot * (self.target_select_fov / 360));
176 vector turret_fovsearch_steprot()
181 wish_angle = self.tur_head.angles;
182 wish_angle_x = self.idle_aim_x;
184 if (self.phase < time)
186 //rot_add = self.aim_maxrot / self.target_select_fov;
187 wish_angle_y += (self.target_select_fov * 2);
189 if(wish_angle_y > 360)
190 wish_angle_y = wish_angle_y - 360;
192 self.phase = time + 1.5;
198 vector turret_fovsearch_random()
202 if (self.phase < time)
204 wish_angle_y = random() * self.aim_maxrot;
208 wish_angle_x = random() * self.aim_maxpitch;
212 self.phase = time + 5;
214 self.tur_aimpos = wish_angle;
217 return self.idle_aim + self.tur_aimpos;
222 ** Handles head rotation according to
223 ** the units .track_type and .track_flags
225 void turret_stdproc_track()
227 vector target_angle; // This is where we want to aim
228 vector move_angle; // This is where we can aim
231 v1 = self.tur_head.angles;
232 v2 = self.tur_head.avelocity;
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 target_angle = vectoangles(normalize(self.tur_aimpos - self.tur_shotorg));
251 self.tur_head.angles_x = anglemods(self.tur_head.angles_x);
252 self.tur_head.angles_y = anglemods(self.tur_head.angles_y);
254 // Find the diffrence between where we currently aim and where we want to aim
255 move_angle = target_angle - (self.angles + self.tur_head.angles);
256 move_angle = shortangle_vxy(move_angle,(self.angles + self.tur_head.angles));
258 switch(self.track_type)
260 case TFL_TRACKTYPE_STEPMOTOR:
261 f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
262 if (self.track_flags & TFL_TRACK_PITCH)
264 self.tur_head.angles_x += bound(-f_tmp,move_angle_x, f_tmp);
265 if(self.tur_head.angles_x > self.aim_maxpitch)
266 self.tur_head.angles_x = self.aim_maxpitch;
268 if(self.tur_head.angles_x < -self.aim_maxpitch)
269 self.tur_head.angles_x = self.aim_maxpitch;
272 if (self.track_flags & TFL_TRACK_ROT)
274 self.tur_head.angles_y += bound(-f_tmp, move_angle_y, f_tmp);
275 if(self.tur_head.angles_y > self.aim_maxrot)
276 self.tur_head.angles_y = self.aim_maxrot;
278 if(self.tur_head.angles_y < -self.aim_maxrot)
279 self.tur_head.angles_y = self.aim_maxrot;
284 self.SendFlags = TNSF_STATUS;
287 case TFL_TRACKTYPE_FLUIDINERTIA:
288 f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
289 move_angle_x = bound(-self.aim_speed, move_angle_x * self.track_accel_pitch * f_tmp, self.aim_speed);
290 move_angle_y = bound(-self.aim_speed, move_angle_y * self.track_accel_rot * f_tmp, self.aim_speed);
291 move_angle = (self.tur_head.avelocity * self.track_blendrate) + (move_angle * (1 - self.track_blendrate));
294 case TFL_TRACKTYPE_FLUIDPRECISE:
296 move_angle_y = bound(-self.aim_speed, move_angle_y, self.aim_speed);
297 move_angle_x = bound(-self.aim_speed, move_angle_x, self.aim_speed);
303 if (self.track_flags & TFL_TRACK_PITCH)
305 self.tur_head.avelocity_x = move_angle_x;
306 if((self.tur_head.angles_x + self.tur_head.avelocity_x * self.ticrate) > self.aim_maxpitch)
308 self.tur_head.avelocity_x = 0;
309 self.tur_head.angles_x = self.aim_maxpitch;
312 if((self.tur_head.angles_x + self.tur_head.avelocity_x * self.ticrate) < -self.aim_maxpitch)
314 self.tur_head.avelocity_x = 0;
315 self.tur_head.angles_x = -self.aim_maxpitch;
320 if (self.track_flags & TFL_TRACK_ROT)
322 self.tur_head.avelocity_y = move_angle_y;
324 if((self.tur_head.angles_y + self.tur_head.avelocity_y * self.ticrate) > self.aim_maxrot)
326 self.tur_head.avelocity_y = 0;
327 self.tur_head.angles_y = self.aim_maxrot;
330 if((self.tur_head.angles_y + self.tur_head.avelocity_y * self.ticrate) < -self.aim_maxrot)
332 self.tur_head.avelocity_y = 0;
333 self.tur_head.angles_y = -self.aim_maxrot;
344 + TFL_FIRECHECK_WORLD
346 + TFL_FIRECHECK_DISTANCES
348 + TFL_FIRECHECK_AIMDIST
349 + TFL_FIRECHECK_REALDIST
350 - TFL_FIRECHECK_ANGLEDIST
351 - TFL_FIRECHECK_TEAMCECK
353 + TFL_FIRECHECK_OWM_AMMO
354 + TFL_FIRECHECK_OTHER_AMMO
355 + TFL_FIRECHECK_REFIRE
359 ** Preforms pre-fire checks based on the uints firecheck_flags
361 float turret_stdproc_firecheck()
363 // This one just dont care =)
364 if (self.firecheck_flags & TFL_FIRECHECK_NO) return 1;
367 if (self.firecheck_flags & TFL_FIRECHECK_REFIRE)
368 if (self.attack_finished_single > time) return 0;
370 // Special case: volly fire turret that has to fire a full volly if a shot was fired.
371 if (self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
372 if (self.volly_counter != self.shot_volly)
373 if(self.ammo >= self.shot_dmg)
376 // Lack of zombies makes shooting dead things unnecessary :P
377 if (self.firecheck_flags & TFL_FIRECHECK_DEAD)
378 if (self.enemy.deadflag != DEAD_NO)
381 // Plz stop killing the world!
382 if (self.firecheck_flags & TFL_FIRECHECK_WORLD)
383 if (self.enemy == world)
387 if (self.firecheck_flags & TFL_FIRECHECK_OWM_AMMO)
388 if (self.ammo < self.shot_dmg)
391 // Other's ammo? (support-supply units)
392 if (self.firecheck_flags & TFL_FIRECHECK_OTHER_AMMO)
393 if (self.enemy.ammo >= self.enemy.ammo_max)
396 // Target of opertunity?
397 if(turret_validate_target(self, self.tur_impactent, self.target_validate_flags) > 0)
399 self.enemy = self.tur_impactent;
403 if (self.firecheck_flags & TFL_FIRECHECK_DISTANCES)
406 if (self.tur_dist_aimpos < self.target_range_min)
407 if(turret_validate_target(self, self.tur_impactent, self.target_validate_flags) > 0)
408 return 1; // Target of opertunity?
414 if (self.firecheck_flags & TFL_FIRECHECK_AFF)
415 if (self.tur_impactent.team == self.team)
418 // aim<->predicted impact
419 if (self.firecheck_flags & TFL_FIRECHECK_AIMDIST)
420 if (self.tur_dist_impact_to_aimpos > self.aim_firetolerance_dist)
424 if (self.shot_volly > 1)
425 if (self.volly_counter == self.shot_volly)
426 if (self.ammo < (self.shot_dmg * self.shot_volly))
429 if(self.firecheck_flags & TFL_FIRECHECK_VERIFIED)
430 if(self.tur_impactent != self.enemy)
437 + TFL_TARGETSELECT_NO
438 + TFL_TARGETSELECT_LOS
439 + TFL_TARGETSELECT_PLAYERS
440 + TFL_TARGETSELECT_MISSILES
441 - TFL_TARGETSELECT_TRIGGERTARGET
442 + TFL_TARGETSELECT_ANGLELIMITS
443 + TFL_TARGETSELECT_RANGELIMTS
444 + TFL_TARGETSELECT_TEAMCHECK
445 - TFL_TARGETSELECT_NOBUILTIN
446 + TFL_TARGETSELECT_OWNTEAM
450 ** Evaluate a entity for target valitity based on validate_flags
451 ** NOTE: the caller must check takedamage before calling this, to inline this check.
453 float turret_validate_target(entity e_turret, entity e_target, float validate_flags)
457 //if(!validate_flags & TFL_TARGETSELECT_NOBUILTIN)
460 if(e_target.owner == e_turret)
463 if not(checkpvs(e_target.origin, e_turret))
470 if (substring(e_target.classname, 0, 10) == "onslaught_") // don't attack onslaught targets, that's the player's job!
473 if (validate_flags & TFL_TARGETSELECT_NO)
476 // If only this was used more..
477 if (e_target.flags & FL_NOTARGET)
481 if (e_target.health < 0)
485 if (e_target.flags & FL_CLIENT)
487 if not (validate_flags & TFL_TARGETSELECT_PLAYERS)
490 if (e_target.deadflag != DEAD_NO)
495 if (validate_flags & TFL_TARGETSELECT_NOTURRETS)
496 if (e_target.turret_firefunc || e_target.owner.tur_head == e_target)
497 if(e_target.team != e_turret.team) // Dont break support units.
501 if (e_target.flags & FL_PROJECTILE)
502 if not (validate_flags & TFL_TARGETSELECT_MISSILES)
505 if (validate_flags & TFL_TARGETSELECT_MISSILESONLY)
506 if not (e_target.flags & FL_PROJECTILE)
510 if (validate_flags & TFL_TARGETSELECT_TEAMCHECK)
512 if (validate_flags & TFL_TARGETSELECT_OWNTEAM)
514 if (e_target.team != e_turret.team)
517 if (e_turret.team != e_target.owner.team)
522 if (e_target.team == e_turret.team)
525 if (e_turret.team == e_target.owner.team)
531 tvt_dist = vlen(e_turret.origin - real_origin(e_target));
532 if (validate_flags & TFL_TARGETSELECT_RANGELIMTS)
534 if (tvt_dist < e_turret.target_range_min)
537 if (tvt_dist > e_turret.target_range)
541 // Can we even aim this thing?
542 tvt_thadv = angleofs3(e_turret.tur_head.origin, e_turret.angles + e_turret.tur_head.angles, e_target);
543 tvt_tadv = shortangle_vxy(angleofs(e_turret, e_target), e_turret.angles);
544 tvt_thadf = vlen(tvt_thadv);
545 tvt_tadf = vlen(tvt_tadv);
548 if(validate_flags & TFL_TARGETSELECT_FOV)
550 if(e_turret.target_select_fov < tvt_thadf)
555 if (validate_flags & TFL_TARGETSELECT_ANGLELIMITS)
557 if (fabs(tvt_tadv_x) > e_turret.aim_maxpitch)
560 if (fabs(tvt_tadv_y) > e_turret.aim_maxrot)
565 if (validate_flags & TFL_TARGETSELECT_LOS)
567 v_tmp = real_origin(e_target) + ((e_target.mins + e_target.maxs) * 0.5);
569 traceline(e_turret.tur_shotorg, v_tmp, 0, e_turret);
571 if (e_turret.aim_firetolerance_dist < vlen(v_tmp - trace_endpos))
575 if (e_target.classname == "grapplinghook")
579 if (e_target.classname == "func_button")
583 #ifdef TURRET_DEBUG_TARGETSELECT
584 dprint("Target:",e_target.netname," is a valid target for ",e_turret.netname,"\n");
590 entity turret_select_target()
592 entity e; // target looper entity
593 float score; // target looper entity score
594 entity e_enemy; // currently best scoreing target
595 float m_score; // currently best scoreing target's score
599 if(self.enemy.takedamage)
600 if(turret_validate_target(self,self.enemy,self.target_validate_flags) > 0)
602 e_enemy = self.enemy;
603 m_score = self.turret_score_target(self,e_enemy) * self.target_select_samebias;
608 e = findradius(self.origin, self.target_range);
610 // Nothing to aim at?
618 if (turret_validate_target(self, e, self.target_select_flags) > 0)
620 score = self.turret_score_target(self,e);
621 if ((score > m_score) && (score > 0))
638 self.nextthink = time + self.ticrate;
640 // ONS uses somewhat backwards linking.
646 e = find(world, targetname,self.target);
651 if (self.team != self.tur_head.team)
652 turret_stdproc_respawn();
656 if (self.tur_dbg_tmr1 < time)
658 if (self.enemy) paint_target (self.enemy,128,self.tur_dbg_rvec,0.9);
659 paint_target(self,256,self.tur_dbg_rvec,0.9);
660 self.tur_dbg_tmr1 = time + 1;
665 if not (self.spawnflags & TSF_NO_AMMO_REGEN)
666 if (self.ammo < self.ammo_max)
667 self.ammo = min(self.ammo + self.ammo_recharge, self.ammo_max);
669 if (self.health < (self.tur_health * 0.5))
671 te_spark(self.origin + '0 0 40', randomvec() * 256 + '0 0 256', 16);
673 // Inactive turrets needs to run the think loop,
674 // So they can handle animation and wake up if need be.
675 if not (self.tur_active)
677 turret_stdproc_track();
681 // This is typicaly used for zaping every target in range
682 // turret_fusionreactor uses this to recharge friendlys.
683 if (self.shoot_flags & TFL_SHOOT_HITALLVALID)
685 // Do a self.turret_fire for every valid target.
686 e = findradius(self.origin,self.target_range);
691 if (turret_validate_target(self,e,self.target_validate_flags))
695 turret_do_updates(self);
697 if (self.turret_firecheckfunc())
706 else if(self.shoot_flags & TFL_SHOOT_CUSTOM)
708 // This one is doing something.. oddball. assume its handles what needs to be handled.
711 if not(self.aim_flags & TFL_AIM_NO)
712 self.tur_aimpos = turret_stdproc_aim_generic();
715 if not(self.track_flags & TFL_TRACK_NO)
716 turret_stdproc_track();
718 turret_do_updates(self);
721 if (self.turret_firecheckfunc())
726 // Special case for volly always. if it fired once it must compleate the volly.
727 if(self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
728 if(self.volly_counter != self.shot_volly)
730 // Predict or whatnot
731 if not(self.aim_flags & TFL_AIM_NO)
732 self.tur_aimpos = turret_stdproc_aim_generic();
735 if not(self.track_flags & TFL_TRACK_NO)
736 turret_stdproc_track();
738 turret_do_updates(self);
741 if (self.turret_firecheckfunc() != 0)
744 if(self.turret_postthink)
745 self.turret_postthink();
750 // Check if we have a vailid enemy, and try to find one if we dont.
752 // g_turrets_targetscan_maxdelay forces a target re-scan at least this often
753 float do_target_scan;
754 if((self.target_select_time + autocvar_g_turrets_targetscan_maxdelay) < time)
757 // Old target (if any) invalid?
758 if (turret_validate_target(self, self.enemy, self.target_validate_flags) <= 0)
764 // But never more often then g_turrets_targetscan_mindelay!
765 if (self.target_select_time + autocvar_g_turrets_targetscan_mindelay > time)
770 self.enemy = turret_select_target();
771 self.target_select_time = time;
774 // No target, just go to idle, do any custom stuff and bail.
775 if (self.enemy == world)
778 if not(self.track_flags & TFL_TRACK_NO)
779 turret_stdproc_track();
781 // do any per-turret stuff
782 if(self.turret_postthink)
783 self.turret_postthink();
789 self.lip = time + autocvar_g_turrets_aimidle_delay; // Keep track of the last time we had a target.
792 if not(self.aim_flags & TFL_AIM_NO)
793 self.tur_aimpos = turret_stdproc_aim_generic();
796 if not(self.track_flags & TFL_TRACK_NO)
797 turret_stdproc_track();
799 turret_do_updates(self);
802 if (self.turret_firecheckfunc())
806 // do any custom per-turret stuff
807 if(self.turret_postthink)
808 self.turret_postthink();
813 if (autocvar_g_turrets_nofire != 0)
816 self.turret_firefunc();
818 self.attack_finished_single = time + self.shot_refire;
819 self.ammo -= self.shot_dmg;
820 self.volly_counter = self.volly_counter - 1;
822 if (self.volly_counter <= 0)
824 self.volly_counter = self.shot_volly;
826 if (self.shoot_flags & TFL_SHOOT_CLEARTARGET)
829 if (self.shot_volly > 1)
830 self.attack_finished_single = time + self.shot_volly_refire;
834 if (self.enemy) paint_target3(self.tur_aimpos, 64, self.tur_dbg_rvec, self.tur_impacttime + 0.25);
838 void turret_stdproc_fire()
840 dprint("^1Bang, ^3your dead^7 ",self.enemy.netname,"! ^1(turret with no real firefunc)\n");
844 When .used a turret switch team to activator.team.
845 If activator is world, the turret go inactive.
847 void turret_stdproc_use()
849 dprint("Turret ",self.netname, " used by ", activator.classname, "\n");
851 self.team = activator.team;
862 Net_LinkEntity(self, TRUE, 0, turret_send);
863 self.think = turret_think;
864 self.nextthink = time;
867 void turrets_manager_think()
869 self.nextthink = time + 1;
872 if (autocvar_g_turrets_reloadcvars == 1)
877 if (e.turrcaps_flags & TFL_TURRCAPS_ISTURRET)
879 load_unit_settings(e,e.cvar_basename,1);
880 if(e.turret_postthink)
881 e.turret_postthink();
886 cvar_set("g_turrets_reloadcvars","0");
891 * Standard turret initialization. use this!
892 * (unless you have a very good reason not to)
893 * if the return value is 0, the turret should be removed.
895 float turret_stdproc_init (string cvar_base_name, float csqc_shared, string base, string head, float _turret_type)
899 // Are turrets allowed?
900 if (autocvar_g_turrets == 0)
903 if(_turret_type < 1 || _turret_type > TID_LAST)
905 dprint("Invalid / Unkown turret type\"", ftos(_turret_type), "\", aborting!\n");
908 self.turret_type = _turret_type;
910 e = find(world, classname, "turret_manager");
916 setorigin(e,'0 0 0');
917 setmodel(e,"models/turrets/plasma.md3");
919 v = gettaginfo(e,gettagindex(e,"tag_fire"));
922 //objerror("^1ERROR: Engine is borken! Turrets will NOT work. force g_turrets to 0 to run maps with turrets anyway.");
928 e.classname = "turret_manager";
929 e.think = turrets_manager_think;
930 e.nextthink = time + 2;
936 dprint("WARNING: turret requested csqc_shared but this is not implemented. Expect strange things to happen.\n");
941 if not (self.spawnflags & TSF_SUSPENDED)
942 droptofloor_builtin();
944 // Terrainbase spawnflag. This puts a enlongated model
945 // under the turret, so it looks ok on uneaven surfaces.
946 /* TODO: Handle this with CSQC
947 if (self.spawnflags & TSF_TERRAINBASE)
951 setmodel(tb,"models/turrets/terrainbase.md3");
952 setorigin(tb,self.origin);
953 tb.solid = SOLID_BBOX;
957 self.cvar_basename = cvar_base_name;
958 load_unit_settings(self, self.cvar_basename, 0);
960 // Handle turret teams.
961 if (autocvar_g_assault != 0)
964 self.team = 14; // Assume turrets are on the defending side if not explicitly set otehrwize
966 else if not (teams_matter)
967 self.team = MAX_SHOT_DISTANCE; // Group all turrets into the same team, so they dont kill eachother.
968 else if(g_onslaught && self.targetname)
970 e = find(world,target,self.targetname);
978 self.team = MAX_SHOT_DISTANCE; // Group all turrets into the same team, so they dont kill eachother.
981 * Try to guess some reasonaly defaults
982 * for missing params and do sanety checks
983 * thise checks could produce some "interesting" results
984 * if it hits a glitch in my logic :P so try to set as mutch
985 * as possible beforehand.
987 if (self.turrcaps_flags & TFL_TURRCAPS_SUPPORT)
988 self.ticrate = 0.2; // Support units generaly dont need to have a high speed ai-loop
990 self.ticrate = 0.1; // 10 fps for normal turrets
992 self.ticrate = bound(sys_frametime, self.ticrate, 60); // keep it sane
995 if (self.netname == "")
996 self.netname = self.classname;
998 if not (self.respawntime)
999 self.respawntime = 60;
1000 self.respawntime = max(-1, self.respawntime);
1002 if not (self.health)
1004 self.tur_health = max(1, self.health);
1006 if not (self.turrcaps_flags)
1007 self.turrcaps_flags = TFL_TURRCAPS_RADIUSDMG | TFL_TURRCAPS_MEDPROJ | TFL_TURRCAPS_PLAYERKILL;
1009 if not (self.damage_flags)
1010 self.damage_flags = TFL_DMG_YES | TFL_DMG_RETALIATE | TFL_DMG_AIMSHAKE;
1013 if not (self.shot_refire)
1014 self.shot_refire = 1;
1015 self.shot_refire = bound(0.01, self.shot_refire, 9999);
1017 if not (self.shot_dmg)
1018 self.shot_dmg = self.shot_refire * 50;
1019 self.shot_dmg = max(1, self.shot_dmg);
1021 if not (self.shot_radius)
1022 self.shot_radius = self.shot_dmg * 0.5;
1023 self.shot_radius = max(1, self.shot_radius);
1025 if not (self.shot_speed)
1026 self.shot_speed = 2500;
1027 self.shot_speed = max(1, self.shot_speed);
1029 if not (self.shot_spread)
1030 self.shot_spread = 0.0125;
1031 self.shot_spread = bound(0.0001, self.shot_spread, 500);
1033 if not (self.shot_force)
1034 self.shot_force = self.shot_dmg * 0.5 + self.shot_radius * 0.5;
1035 self.shot_force = bound(0.001, self.shot_force, 5000);
1037 if not (self.shot_volly)
1038 self.shot_volly = 1;
1039 self.shot_volly = bound(1, self.shot_volly, floor(self.ammo_max / self.shot_dmg));
1041 if not (self.shot_volly_refire)
1042 self.shot_volly_refire = self.shot_refire * self.shot_volly;
1043 self.shot_volly_refire = bound(self.shot_refire, self.shot_volly_refire, 60);
1045 if not (self.firecheck_flags)
1046 self.firecheck_flags = TFL_FIRECHECK_WORLD | TFL_FIRECHECK_DEAD | TFL_FIRECHECK_DISTANCES |
1047 TFL_FIRECHECK_LOS | TFL_FIRECHECK_AIMDIST | TFL_FIRECHECK_TEAMCECK |
1048 TFL_FIRECHECK_OWM_AMMO | TFL_FIRECHECK_REFIRE | TFL_FIRECHECK_WORLD;
1051 if not (self.target_range)
1052 self.target_range = self.shot_speed * 0.5;
1053 self.target_range = bound(0, self.target_range, MAX_SHOT_DISTANCE);
1055 if not (self.target_range_min)
1056 self.target_range_min = self.shot_radius * 2;
1057 self.target_range_min = bound(0, self.target_range_min, MAX_SHOT_DISTANCE);
1059 if not (self.target_range_optimal)
1060 self.target_range_optimal = self.target_range * 0.5;
1061 self.target_range_optimal = bound(0, self.target_range_optimal, MAX_SHOT_DISTANCE);
1065 if not (self.aim_maxrot)
1066 self.aim_maxrot = 90;
1067 self.aim_maxrot = bound(0, self.aim_maxrot, 360);
1069 if not (self.aim_maxpitch)
1070 self.aim_maxpitch = 20;
1071 self.aim_maxpitch = bound(0, self.aim_maxpitch, 90);
1073 if not (self.aim_speed)
1074 self.aim_speed = 36;
1075 self.aim_speed = bound(0.1, self.aim_speed, 1000);
1077 if not (self.aim_firetolerance_dist)
1078 self.aim_firetolerance_dist = 5 + (self.shot_radius * 2);
1079 self.aim_firetolerance_dist = bound(0.1, self.aim_firetolerance_dist, MAX_SHOT_DISTANCE);
1081 if not (self.aim_flags)
1083 self.aim_flags = TFL_AIM_LEAD | TFL_AIM_SHOTTIMECOMPENSATE;
1084 if(self.turrcaps_flags & TFL_TURRCAPS_RADIUSDMG)
1085 self.aim_flags |= TFL_AIM_GROUND2;
1088 if not (self.track_type)
1089 self.track_type = TFL_TRACKTYPE_STEPMOTOR;
1091 if (self.track_type != TFL_TRACKTYPE_STEPMOTOR)
1093 // Fluid / Ineria mode. Looks mutch nicer.
1094 // Can reduce aim preformance alot, needs a bit diffrent aimspeed
1096 if not (self.aim_speed)
1097 self.aim_speed = 180;
1098 self.aim_speed = bound(0.1, self.aim_speed, 1000);
1100 if not (self.track_accel_pitch)
1101 self.track_accel_pitch = 0.5;
1103 if not (self.track_accel_rot)
1104 self.track_accel_rot = 0.5;
1106 if not (self.track_blendrate)
1107 self.track_blendrate = 0.35;
1110 if (!self.track_flags)
1111 self.track_flags = TFL_TRACK_PITCH | TFL_TRACK_ROT;
1114 // Target selection stuff.
1115 if not (self.target_select_rangebias)
1116 self.target_select_rangebias = 1;
1117 self.target_select_rangebias = bound(-10, self.target_select_rangebias, 10);
1119 if not (self.target_select_samebias)
1120 self.target_select_samebias = 1;
1121 self.target_select_samebias = bound(-10, self.target_select_samebias, 10);
1123 if not (self.target_select_anglebias)
1124 self.target_select_anglebias = 1;
1125 self.target_select_anglebias = bound(-10, self.target_select_anglebias, 10);
1127 if not (self.target_select_missilebias)
1128 self.target_select_missilebias = -10;
1130 self.target_select_missilebias = bound(-10, self.target_select_missilebias, 10);
1131 self.target_select_playerbias = bound(-10, self.target_select_playerbias, 10);
1133 if not (self.target_select_flags)
1135 self.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_TEAMCHECK
1136 | TFL_TARGETSELECT_RANGELIMTS | TFL_TARGETSELECT_ANGLELIMITS;
1138 if (self.turrcaps_flags & TFL_TURRCAPS_MISSILEKILL)
1139 self.target_select_flags |= TFL_TARGETSELECT_MISSILES;
1141 if (self.turrcaps_flags & TFL_TURRCAPS_PLAYERKILL)
1142 self.target_select_flags |= TFL_TARGETSELECT_PLAYERS;
1144 // self.target_select_flags = TFL_TARGETSELECT_NO;
1147 self.target_validate_flags = self.target_select_flags;
1151 if not (self.ammo_max)
1152 self.ammo_max = self.shot_dmg * 10;
1153 self.ammo_max = max(self.shot_dmg, self.ammo_max);
1156 self.ammo = self.shot_dmg * 5;
1157 self.ammo = bound(0,self.ammo, self.ammo_max);
1159 if not (self.ammo_recharge)
1160 self.ammo_recharge = self.shot_dmg * 0.5;
1161 self.ammo_recharge = max(0 ,self.ammo_recharge);
1163 // Convert the recharge from X per sec to X per ticrate
1164 self.ammo_recharge = self.ammo_recharge * self.ticrate;
1166 if not (self.ammo_flags)
1167 self.ammo_flags = TFL_AMMO_ENERGY | TFL_AMMO_RECHARGE;
1170 if(self.spawnflags & TSL_NO_RESPAWN)
1171 if not (self.damage_flags & TFL_DMG_DEATH_NORESPAWN)
1172 self.damage_flags |= TFL_DMG_DEATH_NORESPAWN;
1174 // Offsets & origins
1175 if (!self.tur_shotorg) self.tur_shotorg = '50 0 50';
1178 if(MUTATOR_CALLHOOK(TurretSpawn))
1181 // End of default & sanety checks, start building the turret.
1184 self.tur_head = spawn();
1185 self.tur_head.netname = self.tur_head.classname = "turret_head";
1186 self.tur_head.team = self.team;
1187 self.tur_head.owner = self;
1189 setmodel(self, base);
1190 setmodel(self.tur_head, head);
1192 setsize(self, '-32 -32 0', '32 32 64');
1193 setsize(self.tur_head, '0 0 0', '0 0 0');
1195 setorigin(self.tur_head, '0 0 0');
1196 setattachment(self.tur_head, self, "tag_head");
1201 self.tur_health = self.health;
1202 self.solid = SOLID_BBOX;
1203 self.tur_head.solid = SOLID_NOT;
1204 self.takedamage = DAMAGE_AIM;
1205 self.tur_head.takedamage = DAMAGE_NO;
1206 self.movetype = MOVETYPE_NOCLIP;
1207 self.tur_head.movetype = MOVETYPE_NOCLIP;
1210 if not (self.tur_defend)
1211 if (self.target != "")
1213 self.tur_defend = find(world, targetname, self.target);
1214 if (self.tur_defend == world)
1217 dprint("Turret has invalid defendpoint!\n");
1221 // In target defend mode, aim on the spot to defend when idle.
1222 if (self.tur_defend)
1223 self.idle_aim = self.tur_head.angles + angleofs(self.tur_head, self.tur_defend);
1225 self.idle_aim = '0 0 0';
1228 if (self.team == COLOR_TEAM1) self.colormod = '1.4 0.8 0.8';
1229 if (self.team == COLOR_TEAM2) self.colormod = '0.8 0.8 1.4';
1231 // Attach stdprocs. override when and what needed
1232 if (self.turrcaps_flags & TFL_TURRCAPS_SUPPORT)
1234 self.turret_score_target = turret_stdproc_targetscore_support;
1235 self.turret_firecheckfunc = turret_stdproc_firecheck;
1236 self.turret_firefunc = turret_stdproc_fire;
1237 self.event_damage = turret_stdproc_damage;
1241 self.turret_score_target = turret_stdproc_targetscore_generic;
1242 self.turret_firecheckfunc = turret_stdproc_firecheck;
1243 self.turret_firefunc = turret_stdproc_fire;
1244 self.event_damage = turret_stdproc_damage;
1247 self.use = turret_stdproc_use;
1248 self.bot_attack = TRUE;
1251 self.nextthink = time + 1;
1252 self.nextthink += turret_count * sys_frametime;
1254 self.tur_head.team = self.team;
1255 self.view_ofs = '0 0 0';
1258 self.tur_dbg_start = self.nextthink;
1259 while (vlen(self.tur_dbg_rvec) < 2)
1260 self.tur_dbg_rvec = randomvec() * 4;
1262 self.tur_dbg_rvec_x = fabs(self.tur_dbg_rvec_x);
1263 self.tur_dbg_rvec_y = fabs(self.tur_dbg_rvec_y);
1264 self.tur_dbg_rvec_z = fabs(self.tur_dbg_rvec_z);
1268 self.turrcaps_flags |= TFL_TURRCAPS_ISTURRET;
1270 self.classname = "turret_main";
1272 self.tur_active = 1;
1274 // In ONS mode, and linked to a ONS ent. need to call the use to set team.
1275 if (g_onslaught && ee)
1281 turret_stdproc_respawn();
1283 // Initiate the main AI loop
1285 self.think = turret_link;
1287 self.think = turret_think;