1 #define cvar_base "g_turrets_unit_"
3 float turret_send(entity to, float sf)
5 dprint("Sending update\n");
6 WriteByte(MSG_ENTITY, ENT_CLIENT_TURRET);
7 WriteByte(MSG_ENTITY, sf);
10 WriteByte(MSG_ENTITY, self.turret_type);
12 WriteCoord(MSG_ENTITY, self.origin_x);
13 WriteCoord(MSG_ENTITY, self.origin_y);
14 WriteCoord(MSG_ENTITY, self.origin_z);
16 WriteAngle(MSG_ENTITY, self.angles_x);
17 WriteAngle(MSG_ENTITY, self.angles_y);
22 WriteShort(MSG_ENTITY, rint(self.tur_head.angles_x));
23 WriteShort(MSG_ENTITY, rint(self.tur_head.angles_y));
28 WriteShort(MSG_ENTITY, rint(self.tur_head.avelocity_x));
29 WriteShort(MSG_ENTITY, rint(self.tur_head.avelocity_y));
34 WriteShort(MSG_ENTITY, rint(self.origin_x));
35 WriteShort(MSG_ENTITY, rint(self.origin_y));
36 WriteShort(MSG_ENTITY, rint(self.origin_z));
38 WriteShort(MSG_ENTITY, rint(self.velocity_x));
39 WriteShort(MSG_ENTITY, rint(self.velocity_y));
40 WriteShort(MSG_ENTITY, rint(self.velocity_z));
42 WriteShort(MSG_ENTITY, rint(self.angles_y));
47 WriteCoord(MSG_ENTITY, self.anim_start_time);
48 WriteByte(MSG_ENTITY, self.frame);
53 WriteByte(MSG_ENTITY, self.team);
55 WriteByte(MSG_ENTITY, 0);
57 WriteByte(MSG_ENTITY, rint((self.health / self.tur_health) * 255)); // Send health as 0-255 insted of real value, where 255 = 100%
63 void load_unit_settings(entity ent, string unitname, float is_reload)
70 if not (ent.turret_scale_damage) ent.turret_scale_damage = 1;
71 if not (ent.turret_scale_range) ent.turret_scale_range = 1;
72 if not (ent.turret_scale_refire) ent.turret_scale_refire = 1;
73 if not (ent.turret_scale_ammo) ent.turret_scale_ammo = 1;
74 if not (ent.turret_scale_aim) ent.turret_scale_aim = 1;
75 if not (ent.turret_scale_health) ent.turret_scale_health = 1;
76 if not (ent.turret_scale_respawn) ent.turret_scale_respawn = 1;
78 sbase = strcat(cvar_base,unitname);
82 ent.tur_head.avelocity = '0 0 0';
84 ent.tur_head.angles = '0 0 0';
87 ent.health = cvar(strcat(sbase,"_health")) * ent.turret_scale_health;
88 ent.respawntime = cvar(strcat(sbase,"_respawntime")) * ent.turret_scale_respawn;
90 ent.shot_dmg = cvar(strcat(sbase,"_shot_dmg")) * ent.turret_scale_damage;
91 ent.shot_refire = cvar(strcat(sbase,"_shot_refire")) * ent.turret_scale_refire;
92 ent.shot_radius = cvar(strcat(sbase,"_shot_radius")) * ent.turret_scale_damage;
93 ent.shot_speed = cvar(strcat(sbase,"_shot_speed"));
94 ent.shot_spread = cvar(strcat(sbase,"_shot_spread"));
95 ent.shot_force = cvar(strcat(sbase,"_shot_force")) * ent.turret_scale_damage;
96 ent.shot_volly = cvar(strcat(sbase,"_shot_volly"));
97 ent.shot_volly_refire = cvar(strcat(sbase,"_shot_volly_refire")) * ent.turret_scale_refire;
99 ent.target_range = cvar(strcat(sbase,"_target_range")) * ent.turret_scale_range;
100 ent.target_range_min = cvar(strcat(sbase,"_target_range_min")) * ent.turret_scale_range;
101 ent.target_range_optimal = cvar(strcat(sbase,"_target_range_optimal")) * ent.turret_scale_range;
102 //ent.target_range_fire = cvar(strcat(sbase,"_target_range_fire")) * ent.turret_scale_range;
104 ent.target_select_rangebias = cvar(strcat(sbase,"_target_select_rangebias"));
105 ent.target_select_samebias = cvar(strcat(sbase,"_target_select_samebias"));
106 ent.target_select_anglebias = cvar(strcat(sbase,"_target_select_anglebias"));
107 ent.target_select_playerbias = cvar(strcat(sbase,"_target_select_playerbias"));
108 //ent.target_select_fov = cvar(cvar_gets(sbase,"_target_select_fov"));
110 ent.ammo_max = cvar(strcat(sbase,"_ammo_max")) * ent.turret_scale_ammo;
111 ent.ammo_recharge = cvar(strcat(sbase,"_ammo_recharge")) * ent.turret_scale_ammo;
113 ent.aim_firetolerance_dist = cvar(strcat(sbase,"_aim_firetolerance_dist"));
114 ent.aim_speed = cvar(strcat(sbase,"_aim_speed")) * ent.turret_scale_aim;
115 ent.aim_maxrot = cvar(strcat(sbase,"_aim_maxrot"));
116 ent.aim_maxpitch = cvar(strcat(sbase,"_aim_maxpitch"));
118 ent.track_type = cvar(strcat(sbase,"_track_type"));
119 ent.track_accel_pitch = cvar(strcat(sbase,"_track_accel_pitch"));
120 ent.track_accel_rot = cvar(strcat(sbase,"_track_accel_rot"));
121 ent.track_blendrate = cvar(strcat(sbase,"_track_blendrate"));
124 if(ent.turret_respawnhook)
125 ent.turret_respawnhook();
130 ** updates enemy distances, predicted impact point/time
131 ** and updated aim<->predict impact distance.
133 void turret_do_updates(entity t_turret)
135 vector enemy_pos, oldpos;
141 enemy_pos = real_origin(self.enemy);
143 turret_tag_fire_update();
145 self.tur_shotdir_updated = v_forward;
146 self.tur_dist_enemy = vlen(self.tur_shotorg - enemy_pos);
147 self.tur_dist_aimpos = vlen(self.tur_shotorg - self.tur_aimpos);
149 if((self.firecheck_flags & TFL_FIRECHECK_VERIFIED) && (self.enemy))
151 oldpos = self.enemy.origin;
152 setorigin(self.enemy,self.tur_aimpos);
153 tracebox(self.tur_shotorg, '-1 -1 -1','1 1 1',self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos),MOVE_NORMAL,self);
154 setorigin(self.enemy,oldpos);
156 if(trace_ent == self.enemy)
157 self.tur_dist_impact_to_aimpos = 0;
159 self.tur_dist_impact_to_aimpos = vlen(trace_endpos - self.tur_aimpos);
162 tracebox(self.tur_shotorg, '-1 -1 -1','1 1 1', self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos),MOVE_NORMAL,self);
164 self.tur_dist_impact_to_aimpos = vlen(trace_endpos - self.tur_aimpos) - (vlen(self.enemy.maxs - self.enemy.mins) * 0.5);
165 self.tur_impactent = trace_ent;
166 self.tur_impacttime = vlen(self.tur_shotorg - trace_endpos) / self.shot_speed;
172 vector turret_fovsearch_pingpong()
175 if(self.phase < time)
177 if( self.tur_head.phase )
178 self.tur_head.phase = 0;
180 self.tur_head.phase = 1;
181 self.phase = time + 5;
184 if( self.tur_head.phase)
185 wish_angle = self.idle_aim + '0 1 0' * (self.aim_maxrot * (self.target_select_fov / 360));
187 wish_angle = self.idle_aim - '0 1 0' * (self.aim_maxrot * (self.target_select_fov / 360));
192 vector turret_fovsearch_steprot()
197 wish_angle = self.tur_head.angles;
198 wish_angle_x = self.idle_aim_x;
200 if (self.phase < time)
202 //rot_add = self.aim_maxrot / self.target_select_fov;
203 wish_angle_y += (self.target_select_fov * 2);
205 if(wish_angle_y > 360)
206 wish_angle_y = wish_angle_y - 360;
208 self.phase = time + 1.5;
214 vector turret_fovsearch_random()
218 if (self.phase < time)
220 wish_angle_y = random() * self.aim_maxrot;
224 wish_angle_x = random() * self.aim_maxpitch;
228 self.phase = time + 5;
230 self.tur_aimpos = wish_angle;
233 return self.idle_aim + self.tur_aimpos;
238 ** Handles head rotation according to
239 ** the units .track_type and .track_flags
241 .float turret_framecounter;
242 void turret_stdproc_track()
244 vector target_angle; // This is where we want to aim
245 vector move_angle; // This is where we can aim
248 v1 = self.tur_head.angles;
249 v2 = self.tur_head.avelocity;
251 if (self.track_flags == TFL_TRACK_NO)
254 if not (self.tur_active)
255 target_angle = self.idle_aim - ('1 0 0' * self.aim_maxpitch);
256 else if (self.enemy == world)
259 target_angle = self.idle_aim + self.angles;
261 target_angle = vectoangles(normalize(self.tur_aimpos - self.tur_shotorg));
265 target_angle = vectoangles(normalize(self.tur_aimpos - self.tur_shotorg));
268 self.tur_head.angles_x = anglemods(self.tur_head.angles_x);
269 self.tur_head.angles_y = anglemods(self.tur_head.angles_y);
271 // Find the diffrence between where we currently aim and where we want to aim
272 move_angle = target_angle - (self.angles + self.tur_head.angles);
273 move_angle = shortangle_vxy(move_angle,(self.angles + self.tur_head.angles));
275 switch(self.track_type)
277 case TFL_TRACKTYPE_STEPMOTOR:
278 f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
279 if (self.track_flags & TFL_TRACK_PITCH)
281 self.tur_head.angles_x += bound(-f_tmp,move_angle_x, f_tmp);
282 if(self.tur_head.angles_x > self.aim_maxpitch)
283 self.tur_head.angles_x = self.aim_maxpitch;
285 if(self.tur_head.angles_x < -self.aim_maxpitch)
286 self.tur_head.angles_x = self.aim_maxpitch;
289 if (self.track_flags & TFL_TRACK_ROT)
291 self.tur_head.angles_y += bound(-f_tmp, move_angle_y, f_tmp);
292 if(self.tur_head.angles_y > self.aim_maxrot)
293 self.tur_head.angles_y = self.aim_maxrot;
295 if(self.tur_head.angles_y < -self.aim_maxrot)
296 self.tur_head.angles_y = self.aim_maxrot;
300 self.SendFlags = TNSF_ANG;
304 case TFL_TRACKTYPE_FLUIDINERTIA:
305 f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
306 move_angle_x = bound(-self.aim_speed, move_angle_x * self.track_accel_pitch * f_tmp, self.aim_speed);
307 move_angle_y = bound(-self.aim_speed, move_angle_y * self.track_accel_rot * f_tmp, self.aim_speed);
308 move_angle = (self.tur_head.avelocity * self.track_blendrate) + (move_angle * (1 - self.track_blendrate));
311 case TFL_TRACKTYPE_FLUIDPRECISE:
313 move_angle_y = bound(-self.aim_speed, move_angle_y, self.aim_speed);
314 move_angle_x = bound(-self.aim_speed, move_angle_x, self.aim_speed);
320 if (self.track_flags & TFL_TRACK_PITCH)
322 self.tur_head.avelocity_x = move_angle_x;
323 if((self.tur_head.angles_x + self.tur_head.avelocity_x * self.ticrate) > self.aim_maxpitch)
325 self.tur_head.avelocity_x = 0;
326 self.tur_head.angles_x = self.aim_maxpitch;
328 self.SendFlags |= TNSF_ANG;
331 if((self.tur_head.angles_x + self.tur_head.avelocity_x * self.ticrate) < -self.aim_maxpitch)
333 self.tur_head.avelocity_x = 0;
334 self.tur_head.angles_x = -self.aim_maxpitch;
336 self.SendFlags |= TNSF_ANG;
341 if (self.track_flags & TFL_TRACK_ROT)
343 self.tur_head.avelocity_y = move_angle_y;
345 if((self.tur_head.angles_y + self.tur_head.avelocity_y * self.ticrate) > self.aim_maxrot)
347 self.tur_head.avelocity_y = 0;
348 self.tur_head.angles_y = self.aim_maxrot;
350 self.SendFlags |= TNSF_ANG;
353 if((self.tur_head.angles_y + self.tur_head.avelocity_y * self.ticrate) < -self.aim_maxrot)
355 self.tur_head.avelocity_y = 0;
356 self.tur_head.angles_y = -self.aim_maxrot;
358 self.SendFlags |= TNSF_ANG;
362 self.SendFlags |= TNSF_AVEL;
364 // Force a angle update every 10'th frame
365 self.turret_framecounter += 1;
366 if(self.turret_framecounter >= 10)
368 self.SendFlags |= TNSF_ANG;
369 self.turret_framecounter = 0;
379 + TFL_FIRECHECK_WORLD
381 + TFL_FIRECHECK_DISTANCES
383 + TFL_FIRECHECK_AIMDIST
384 + TFL_FIRECHECK_REALDIST
385 - TFL_FIRECHECK_ANGLEDIST
386 - TFL_FIRECHECK_TEAMCECK
388 + TFL_FIRECHECK_OWM_AMMO
389 + TFL_FIRECHECK_OTHER_AMMO
390 + TFL_FIRECHECK_REFIRE
394 ** Preforms pre-fire checks based on the uints firecheck_flags
396 float turret_stdproc_firecheck()
398 // This one just dont care =)
399 if (self.firecheck_flags & TFL_FIRECHECK_NO) return 1;
402 if (self.firecheck_flags & TFL_FIRECHECK_REFIRE)
403 if (self.attack_finished_single > time) return 0;
405 // Special case: volly fire turret that has to fire a full volly if a shot was fired.
406 if (self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
407 if (self.volly_counter != self.shot_volly)
408 if(self.ammo >= self.shot_dmg)
411 // Lack of zombies makes shooting dead things unnecessary :P
412 if (self.firecheck_flags & TFL_FIRECHECK_DEAD)
413 if (self.enemy.deadflag != DEAD_NO)
416 // Plz stop killing the world!
417 if (self.firecheck_flags & TFL_FIRECHECK_WORLD)
418 if (self.enemy == world)
422 if (self.firecheck_flags & TFL_FIRECHECK_OWM_AMMO)
423 if (self.ammo < self.shot_dmg)
426 // Other's ammo? (support-supply units)
427 if (self.firecheck_flags & TFL_FIRECHECK_OTHER_AMMO)
428 if (self.enemy.ammo >= self.enemy.ammo_max)
431 // Target of opertunity?
432 if(turret_validate_target(self, self.tur_impactent, self.target_validate_flags) > 0)
434 self.enemy = self.tur_impactent;
438 if (self.firecheck_flags & TFL_FIRECHECK_DISTANCES)
441 if (self.tur_dist_aimpos < self.target_range_min)
442 if(turret_validate_target(self, self.tur_impactent, self.target_validate_flags) > 0)
443 return 1; // Target of opertunity?
449 if (self.firecheck_flags & TFL_FIRECHECK_AFF)
450 if (self.tur_impactent.team == self.team)
453 // aim<->predicted impact
454 if (self.firecheck_flags & TFL_FIRECHECK_AIMDIST)
455 if (self.tur_dist_impact_to_aimpos > self.aim_firetolerance_dist)
459 if (self.shot_volly > 1)
460 if (self.volly_counter == self.shot_volly)
461 if (self.ammo < (self.shot_dmg * self.shot_volly))
464 if(self.firecheck_flags & TFL_FIRECHECK_VERIFIED)
465 if(self.tur_impactent != self.enemy)
472 + TFL_TARGETSELECT_NO
473 + TFL_TARGETSELECT_LOS
474 + TFL_TARGETSELECT_PLAYERS
475 + TFL_TARGETSELECT_MISSILES
476 - TFL_TARGETSELECT_TRIGGERTARGET
477 + TFL_TARGETSELECT_ANGLELIMITS
478 + TFL_TARGETSELECT_RANGELIMTS
479 + TFL_TARGETSELECT_TEAMCHECK
480 - TFL_TARGETSELECT_NOBUILTIN
481 + TFL_TARGETSELECT_OWNTEAM
485 ** Evaluate a entity for target valitity based on validate_flags
486 ** NOTE: the caller must check takedamage before calling this, to inline this check.
488 float turret_validate_target(entity e_turret, entity e_target, float validate_flags)
492 //if(!validate_flags & TFL_TARGETSELECT_NOBUILTIN)
495 if(e_target.owner == e_turret)
498 if not(checkpvs(e_target.origin, e_turret))
505 if (substring(e_target.classname, 0, 10) == "onslaught_") // don't attack onslaught targets, that's the player's job!
508 if (validate_flags & TFL_TARGETSELECT_NO)
511 // If only this was used more..
512 if (e_target.flags & FL_NOTARGET)
516 if (e_target.health < 0)
520 if (e_target.flags & FL_CLIENT)
522 if not (validate_flags & TFL_TARGETSELECT_PLAYERS)
525 if (e_target.deadflag != DEAD_NO)
530 if (validate_flags & TFL_TARGETSELECT_NOTURRETS)
531 if (e_target.turret_firefunc || e_target.owner.tur_head == e_target)
532 if(e_target.team != e_turret.team) // Dont break support units.
536 if (e_target.flags & FL_PROJECTILE)
537 if not (validate_flags & TFL_TARGETSELECT_MISSILES)
540 if (validate_flags & TFL_TARGETSELECT_MISSILESONLY)
541 if not (e_target.flags & FL_PROJECTILE)
545 if (validate_flags & TFL_TARGETSELECT_TEAMCHECK)
547 if (validate_flags & TFL_TARGETSELECT_OWNTEAM)
549 if (e_target.team != e_turret.team)
552 if (e_turret.team != e_target.owner.team)
557 if (e_target.team == e_turret.team)
560 if (e_turret.team == e_target.owner.team)
566 tvt_dist = vlen(e_turret.origin - real_origin(e_target));
567 if (validate_flags & TFL_TARGETSELECT_RANGELIMTS)
569 if (tvt_dist < e_turret.target_range_min)
572 if (tvt_dist > e_turret.target_range)
576 // Can we even aim this thing?
577 tvt_thadv = angleofs3(e_turret.tur_head.origin, e_turret.angles + e_turret.tur_head.angles, e_target);
578 tvt_tadv = shortangle_vxy(angleofs(e_turret, e_target), e_turret.angles);
579 tvt_thadf = vlen(tvt_thadv);
580 tvt_tadf = vlen(tvt_tadv);
583 if(validate_flags & TFL_TARGETSELECT_FOV)
585 if(e_turret.target_select_fov < tvt_thadf)
590 if (validate_flags & TFL_TARGETSELECT_ANGLELIMITS)
592 if (fabs(tvt_tadv_x) > e_turret.aim_maxpitch)
595 if (fabs(tvt_tadv_y) > e_turret.aim_maxrot)
600 if (validate_flags & TFL_TARGETSELECT_LOS)
602 v_tmp = real_origin(e_target) + ((e_target.mins + e_target.maxs) * 0.5);
604 traceline(e_turret.tur_shotorg, v_tmp, 0, e_turret);
606 if (e_turret.aim_firetolerance_dist < vlen(v_tmp - trace_endpos))
610 if (e_target.classname == "grapplinghook")
614 if (e_target.classname == "func_button")
618 #ifdef TURRET_DEBUG_TARGETSELECT
619 dprint("Target:",e_target.netname," is a valid target for ",e_turret.netname,"\n");
625 entity turret_select_target()
627 entity e; // target looper entity
628 float score; // target looper entity score
629 entity e_enemy; // currently best scoreing target
630 float m_score; // currently best scoreing target's score
634 if(self.enemy.takedamage)
635 if(turret_validate_target(self,self.enemy,self.target_validate_flags) > 0)
637 e_enemy = self.enemy;
638 m_score = self.turret_score_target(self,e_enemy) * self.target_select_samebias;
643 e = findradius(self.origin, self.target_range);
645 // Nothing to aim at?
653 if (turret_validate_target(self, e, self.target_select_flags) > 0)
655 score = self.turret_score_target(self,e);
656 if ((score > m_score) && (score > 0))
673 self.nextthink = time + self.ticrate;
674 //self.SendFlags = TNSF_UPDATE | TNSF_STATUS | TNSF_ANG | TNSF_AVEL;
676 // ONS uses somewhat backwards linking.
682 e = find(world, targetname,self.target);
687 if (self.team != self.tur_head.team)
688 turret_stdproc_respawn();
692 if (self.tur_dbg_tmr1 < time)
694 if (self.enemy) paint_target (self.enemy,128,self.tur_dbg_rvec,0.9);
695 paint_target(self,256,self.tur_dbg_rvec,0.9);
696 self.tur_dbg_tmr1 = time + 1;
701 if not (self.spawnflags & TSF_NO_AMMO_REGEN)
702 if (self.ammo < self.ammo_max)
703 self.ammo = min(self.ammo + self.ammo_recharge, self.ammo_max);
705 // Inactive turrets needs to run the think loop,
706 // So they can handle animation and wake up if need be.
707 if not (self.tur_active)
709 turret_stdproc_track();
713 // This is typicaly used for zaping every target in range
714 // turret_fusionreactor uses this to recharge friendlys.
715 if (self.shoot_flags & TFL_SHOOT_HITALLVALID)
717 // Do a self.turret_fire for every valid target.
718 e = findradius(self.origin,self.target_range);
723 if (turret_validate_target(self,e,self.target_validate_flags))
727 turret_do_updates(self);
729 if (self.turret_firecheckfunc())
738 else if(self.shoot_flags & TFL_SHOOT_CUSTOM)
740 // This one is doing something.. oddball. assume its handles what needs to be handled.
743 if not(self.aim_flags & TFL_AIM_NO)
744 self.tur_aimpos = turret_stdproc_aim_generic();
747 if not(self.track_flags & TFL_TRACK_NO)
748 turret_stdproc_track();
750 turret_do_updates(self);
753 if (self.turret_firecheckfunc())
758 // Special case for volly always. if it fired once it must compleate the volly.
759 if(self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
760 if(self.volly_counter != self.shot_volly)
762 // Predict or whatnot
763 if not(self.aim_flags & TFL_AIM_NO)
764 self.tur_aimpos = turret_stdproc_aim_generic();
767 if not(self.track_flags & TFL_TRACK_NO)
768 turret_stdproc_track();
770 turret_do_updates(self);
773 if (self.turret_firecheckfunc() != 0)
776 if(self.turret_postthink)
777 self.turret_postthink();
782 // Check if we have a vailid enemy, and try to find one if we dont.
784 // g_turrets_targetscan_maxdelay forces a target re-scan at least this often
785 float do_target_scan;
786 if((self.target_select_time + autocvar_g_turrets_targetscan_maxdelay) < time)
789 // Old target (if any) invalid?
790 if (turret_validate_target(self, self.enemy, self.target_validate_flags) <= 0)
796 // But never more often then g_turrets_targetscan_mindelay!
797 if (self.target_select_time + autocvar_g_turrets_targetscan_mindelay > time)
802 self.enemy = turret_select_target();
803 self.target_select_time = time;
806 // No target, just go to idle, do any custom stuff and bail.
807 if (self.enemy == world)
810 if not(self.track_flags & TFL_TRACK_NO)
811 turret_stdproc_track();
813 // do any per-turret stuff
814 if(self.turret_postthink)
815 self.turret_postthink();
821 self.lip = time + autocvar_g_turrets_aimidle_delay; // Keep track of the last time we had a target.
824 if not(self.aim_flags & TFL_AIM_NO)
825 self.tur_aimpos = turret_stdproc_aim_generic();
828 if not(self.track_flags & TFL_TRACK_NO)
829 turret_stdproc_track();
831 turret_do_updates(self);
834 if (self.turret_firecheckfunc())
838 // do any custom per-turret stuff
839 if(self.turret_postthink)
840 self.turret_postthink();
845 if (autocvar_g_turrets_nofire != 0)
848 self.turret_firefunc();
850 self.attack_finished_single = time + self.shot_refire;
851 self.ammo -= self.shot_dmg;
852 self.volly_counter = self.volly_counter - 1;
854 if (self.volly_counter <= 0)
856 self.volly_counter = self.shot_volly;
858 if (self.shoot_flags & TFL_SHOOT_CLEARTARGET)
861 if (self.shot_volly > 1)
862 self.attack_finished_single = time + self.shot_volly_refire;
866 if (self.enemy) paint_target3(self.tur_aimpos, 64, self.tur_dbg_rvec, self.tur_impacttime + 0.25);
870 void turret_stdproc_fire()
872 dprint("^1Bang, ^3your dead^7 ",self.enemy.netname,"! ^1(turret with no real firefunc)\n");
876 When .used a turret switch team to activator.team.
877 If activator is world, the turret go inactive.
879 void turret_stdproc_use()
881 dprint("Turret ",self.netname, " used by ", activator.classname, "\n");
883 self.team = activator.team;
894 Net_LinkEntity(self, TRUE, 0, turret_send);
895 self.think = turret_think;
896 self.nextthink = time;
897 self.tur_head.effects = EF_NODRAW;
900 void turrets_manager_think()
902 self.nextthink = time + 1;
905 if (autocvar_g_turrets_reloadcvars == 1)
910 if (e.turrcaps_flags & TFL_TURRCAPS_ISTURRET)
912 load_unit_settings(e,e.cvar_basename,1);
913 if(e.turret_postthink)
914 e.turret_postthink();
919 cvar_set("g_turrets_reloadcvars","0");
924 * Standard turret initialization. use this!
925 * (unless you have a very good reason not to)
926 * if the return value is 0, the turret should be removed.
928 float turret_stdproc_init (string cvar_base_name, string base, string head, float _turret_type)
932 // Are turrets allowed?
933 if (autocvar_g_turrets == 0)
936 if(_turret_type < 1 || _turret_type > TID_LAST)
938 dprint("Invalid / Unkown turret type\"", ftos(_turret_type), "\", aborting!\n");
941 self.turret_type = _turret_type;
943 e = find(world, classname, "turret_manager");
949 setorigin(e,'0 0 0');
950 setmodel(e,"models/turrets/plasma.md3");
952 v = gettaginfo(e,gettagindex(e,"tag_fire"));
955 //objerror("^1ERROR: Engine is borken! Turrets will NOT work. force g_turrets to 0 to run maps with turrets anyway.");
961 e.classname = "turret_manager";
962 e.think = turrets_manager_think;
963 e.nextthink = time + 2;
969 dprint("WARNING: turret requested csqc_shared but this is not implemented. Expect strange things to happen.\n");
974 if not (self.spawnflags & TSF_SUSPENDED)
975 droptofloor_builtin();
977 // Terrainbase spawnflag. This puts a enlongated model
978 // under the turret, so it looks ok on uneaven surfaces.
979 /* TODO: Handle this with CSQC
980 if (self.spawnflags & TSF_TERRAINBASE)
984 setmodel(tb,"models/turrets/terrainbase.md3");
985 setorigin(tb,self.origin);
986 tb.solid = SOLID_BBOX;
990 self.cvar_basename = cvar_base_name;
991 load_unit_settings(self, self.cvar_basename, 0);
993 // Handle turret teams.
994 if (autocvar_g_assault != 0)
997 self.team = 14; // Assume turrets are on the defending side if not explicitly set otehrwize
999 else if not (teamplay)
1000 self.team = MAX_SHOT_DISTANCE; // Group all turrets into the same team, so they dont kill eachother.
1001 else if(g_onslaught && self.targetname)
1003 e = find(world,target,self.targetname);
1011 self.team = MAX_SHOT_DISTANCE; // Group all turrets into the same team, so they dont kill eachother.
1014 * Try to guess some reasonaly defaults
1015 * for missing params and do sanety checks
1016 * thise checks could produce some "interesting" results
1017 * if it hits a glitch in my logic :P so try to set as mutch
1018 * as possible beforehand.
1020 if (self.turrcaps_flags & TFL_TURRCAPS_SUPPORT)
1021 self.ticrate = 0.2; // Support units generaly dont need to have a high speed ai-loop
1023 self.ticrate = 0.1; // 10 fps for normal turrets
1025 self.ticrate = bound(sys_frametime, self.ticrate, 60); // keep it sane
1028 if (self.netname == "")
1029 self.netname = self.classname;
1031 if not (self.respawntime)
1032 self.respawntime = 60;
1033 self.respawntime = max(-1, self.respawntime);
1035 if not (self.health)
1037 self.tur_health = max(1, self.health);
1039 if not (self.turrcaps_flags)
1040 self.turrcaps_flags = TFL_TURRCAPS_RADIUSDMG | TFL_TURRCAPS_MEDPROJ | TFL_TURRCAPS_PLAYERKILL;
1042 if not (self.damage_flags)
1043 self.damage_flags = TFL_DMG_YES | TFL_DMG_RETALIATE | TFL_DMG_AIMSHAKE;
1046 if not (self.shot_refire)
1047 self.shot_refire = 1;
1048 self.shot_refire = bound(0.01, self.shot_refire, 9999);
1050 if not (self.shot_dmg)
1051 self.shot_dmg = self.shot_refire * 50;
1052 self.shot_dmg = max(1, self.shot_dmg);
1054 if not (self.shot_radius)
1055 self.shot_radius = self.shot_dmg * 0.5;
1056 self.shot_radius = max(1, self.shot_radius);
1058 if not (self.shot_speed)
1059 self.shot_speed = 2500;
1060 self.shot_speed = max(1, self.shot_speed);
1062 if not (self.shot_spread)
1063 self.shot_spread = 0.0125;
1064 self.shot_spread = bound(0.0001, self.shot_spread, 500);
1066 if not (self.shot_force)
1067 self.shot_force = self.shot_dmg * 0.5 + self.shot_radius * 0.5;
1068 self.shot_force = bound(0.001, self.shot_force, 5000);
1070 if not (self.shot_volly)
1071 self.shot_volly = 1;
1072 self.shot_volly = bound(1, self.shot_volly, floor(self.ammo_max / self.shot_dmg));
1074 if not (self.shot_volly_refire)
1075 self.shot_volly_refire = self.shot_refire * self.shot_volly;
1076 self.shot_volly_refire = bound(self.shot_refire, self.shot_volly_refire, 60);
1078 if not (self.firecheck_flags)
1079 self.firecheck_flags = TFL_FIRECHECK_WORLD | TFL_FIRECHECK_DEAD | TFL_FIRECHECK_DISTANCES |
1080 TFL_FIRECHECK_LOS | TFL_FIRECHECK_AIMDIST | TFL_FIRECHECK_TEAMCECK |
1081 TFL_FIRECHECK_OWM_AMMO | TFL_FIRECHECK_REFIRE | TFL_FIRECHECK_WORLD;
1084 if not (self.target_range)
1085 self.target_range = self.shot_speed * 0.5;
1086 self.target_range = bound(0, self.target_range, MAX_SHOT_DISTANCE);
1088 if not (self.target_range_min)
1089 self.target_range_min = self.shot_radius * 2;
1090 self.target_range_min = bound(0, self.target_range_min, MAX_SHOT_DISTANCE);
1092 if not (self.target_range_optimal)
1093 self.target_range_optimal = self.target_range * 0.5;
1094 self.target_range_optimal = bound(0, self.target_range_optimal, MAX_SHOT_DISTANCE);
1098 if not (self.aim_maxrot)
1099 self.aim_maxrot = 90;
1100 self.aim_maxrot = bound(0, self.aim_maxrot, 360);
1102 if not (self.aim_maxpitch)
1103 self.aim_maxpitch = 20;
1104 self.aim_maxpitch = bound(0, self.aim_maxpitch, 90);
1106 if not (self.aim_speed)
1107 self.aim_speed = 36;
1108 self.aim_speed = bound(0.1, self.aim_speed, 1000);
1110 if not (self.aim_firetolerance_dist)
1111 self.aim_firetolerance_dist = 5 + (self.shot_radius * 2);
1112 self.aim_firetolerance_dist = bound(0.1, self.aim_firetolerance_dist, MAX_SHOT_DISTANCE);
1114 if not (self.aim_flags)
1116 self.aim_flags = TFL_AIM_LEAD | TFL_AIM_SHOTTIMECOMPENSATE;
1117 if(self.turrcaps_flags & TFL_TURRCAPS_RADIUSDMG)
1118 self.aim_flags |= TFL_AIM_GROUND2;
1121 if not (self.track_type)
1122 self.track_type = TFL_TRACKTYPE_STEPMOTOR;
1124 if (self.track_type != TFL_TRACKTYPE_STEPMOTOR)
1126 // Fluid / Ineria mode. Looks mutch nicer.
1127 // Can reduce aim preformance alot, needs a bit diffrent aimspeed
1129 if not (self.aim_speed)
1130 self.aim_speed = 180;
1131 self.aim_speed = bound(0.1, self.aim_speed, 1000);
1133 if not (self.track_accel_pitch)
1134 self.track_accel_pitch = 0.5;
1136 if not (self.track_accel_rot)
1137 self.track_accel_rot = 0.5;
1139 if not (self.track_blendrate)
1140 self.track_blendrate = 0.35;
1143 if (!self.track_flags)
1144 self.track_flags = TFL_TRACK_PITCH | TFL_TRACK_ROT;
1147 // Target selection stuff.
1148 if not (self.target_select_rangebias)
1149 self.target_select_rangebias = 1;
1150 self.target_select_rangebias = bound(-10, self.target_select_rangebias, 10);
1152 if not (self.target_select_samebias)
1153 self.target_select_samebias = 1;
1154 self.target_select_samebias = bound(-10, self.target_select_samebias, 10);
1156 if not (self.target_select_anglebias)
1157 self.target_select_anglebias = 1;
1158 self.target_select_anglebias = bound(-10, self.target_select_anglebias, 10);
1160 if not (self.target_select_missilebias)
1161 self.target_select_missilebias = -10;
1163 self.target_select_missilebias = bound(-10, self.target_select_missilebias, 10);
1164 self.target_select_playerbias = bound(-10, self.target_select_playerbias, 10);
1166 if not (self.target_select_flags)
1168 self.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_TEAMCHECK
1169 | TFL_TARGETSELECT_RANGELIMTS | TFL_TARGETSELECT_ANGLELIMITS;
1171 if (self.turrcaps_flags & TFL_TURRCAPS_MISSILEKILL)
1172 self.target_select_flags |= TFL_TARGETSELECT_MISSILES;
1174 if (self.turrcaps_flags & TFL_TURRCAPS_PLAYERKILL)
1175 self.target_select_flags |= TFL_TARGETSELECT_PLAYERS;
1177 // self.target_select_flags = TFL_TARGETSELECT_NO;
1180 self.target_validate_flags = self.target_select_flags;
1183 if not (self.ammo_max)
1184 self.ammo_max = self.shot_dmg * 10;
1185 self.ammo_max = max(self.shot_dmg, self.ammo_max);
1188 self.ammo = self.shot_dmg * 5;
1189 self.ammo = bound(0,self.ammo, self.ammo_max);
1191 if not (self.ammo_recharge)
1192 self.ammo_recharge = self.shot_dmg * 0.5;
1193 self.ammo_recharge = max(0 ,self.ammo_recharge);
1195 // Convert the recharge from X per sec to X per ticrate
1196 self.ammo_recharge = self.ammo_recharge * self.ticrate;
1198 if not (self.ammo_flags)
1199 self.ammo_flags = TFL_AMMO_ENERGY | TFL_AMMO_RECHARGE;
1202 if(self.spawnflags & TSL_NO_RESPAWN)
1203 if not (self.damage_flags & TFL_DMG_DEATH_NORESPAWN)
1204 self.damage_flags |= TFL_DMG_DEATH_NORESPAWN;
1206 // Offsets & origins
1207 if (!self.tur_shotorg) self.tur_shotorg = '50 0 50';
1213 if(MUTATOR_CALLHOOK(TurretSpawn))
1216 // End of default & sanety checks, start building the turret.
1219 self.tur_head = spawn();
1220 self.tur_head.netname = self.tur_head.classname = "turret_head";
1221 self.tur_head.team = self.team;
1222 self.tur_head.owner = self;
1224 setmodel(self, base);
1225 setmodel(self.tur_head, head);
1227 setsize(self, '-32 -32 0', '32 32 64');
1228 setsize(self.tur_head, '0 0 0', '0 0 0');
1230 setorigin(self.tur_head, '0 0 0');
1231 setattachment(self.tur_head, self, "tag_head");
1233 self.tur_health = self.health;
1234 self.solid = SOLID_BBOX;
1235 self.tur_head.solid = SOLID_NOT;
1236 self.takedamage = DAMAGE_AIM;
1237 self.tur_head.takedamage = DAMAGE_NO;
1238 self.movetype = MOVETYPE_NOCLIP;
1239 self.tur_head.movetype = MOVETYPE_NOCLIP;
1242 if not (self.tur_defend)
1243 if (self.target != "")
1245 self.tur_defend = find(world, targetname, self.target);
1246 if (self.tur_defend == world)
1249 dprint("Turret has invalid defendpoint!\n");
1253 // In target defend mode, aim on the spot to defend when idle.
1254 if (self.tur_defend)
1255 self.idle_aim = self.tur_head.angles + angleofs(self.tur_head, self.tur_defend);
1257 self.idle_aim = '0 0 0';
1259 // Attach stdprocs. override when and what needed
1260 self.turret_firecheckfunc = turret_stdproc_firecheck;
1261 self.turret_firefunc = turret_stdproc_fire;
1262 self.event_damage = turret_stdproc_damage;
1264 if (self.turrcaps_flags & TFL_TURRCAPS_SUPPORT)
1265 self.turret_score_target = turret_stdproc_targetscore_support;
1267 self.turret_score_target = turret_stdproc_targetscore_generic;
1269 self.use = turret_stdproc_use;
1270 self.bot_attack = TRUE;
1273 self.nextthink = time + 1;
1274 self.nextthink += turret_count * sys_frametime;
1276 self.tur_head.team = self.team;
1277 self.view_ofs = '0 0 0';
1280 self.tur_dbg_start = self.nextthink;
1281 while (vlen(self.tur_dbg_rvec) < 2)
1282 self.tur_dbg_rvec = randomvec() * 4;
1284 self.tur_dbg_rvec_x = fabs(self.tur_dbg_rvec_x);
1285 self.tur_dbg_rvec_y = fabs(self.tur_dbg_rvec_y);
1286 self.tur_dbg_rvec_z = fabs(self.tur_dbg_rvec_z);
1290 self.turrcaps_flags |= TFL_TURRCAPS_ISTURRET;
1292 self.classname = "turret_main";
1294 self.tur_active = 1;
1296 // In ONS mode, and linked to a ONS ent. need to call the use to set team.
1297 if (g_onslaught && ee)
1303 turret_stdproc_respawn();
1305 if (!turret_tag_fire_update())
1306 dprint("Warning: Turret ",self.classname, " faild to initialize md3 tags\n");