1 #define cvar_base "g_turrets_unit_"
3 void turrets_setframe(float _frame, float client_only)
5 if((client_only ? self.clientframe : self.frame ) != _frame)
7 self.SendFlags |= TNSF_ANIM;
8 self.anim_start_time = time;
12 self.clientframe = _frame;
18 float turret_send(entity to, float sf)
21 WriteByte(MSG_ENTITY, ENT_CLIENT_TURRET);
22 WriteByte(MSG_ENTITY, sf);
25 WriteByte(MSG_ENTITY, self.turret_type);
27 WriteCoord(MSG_ENTITY, self.origin_x);
28 WriteCoord(MSG_ENTITY, self.origin_y);
29 WriteCoord(MSG_ENTITY, self.origin_z);
31 WriteAngle(MSG_ENTITY, self.angles_x);
32 WriteAngle(MSG_ENTITY, self.angles_y);
37 WriteShort(MSG_ENTITY, rint(self.tur_head.angles_x));
38 WriteShort(MSG_ENTITY, rint(self.tur_head.angles_y));
43 WriteShort(MSG_ENTITY, rint(self.tur_head.avelocity_x));
44 WriteShort(MSG_ENTITY, rint(self.tur_head.avelocity_y));
49 WriteShort(MSG_ENTITY, rint(self.origin_x));
50 WriteShort(MSG_ENTITY, rint(self.origin_y));
51 WriteShort(MSG_ENTITY, rint(self.origin_z));
53 WriteShort(MSG_ENTITY, rint(self.velocity_x));
54 WriteShort(MSG_ENTITY, rint(self.velocity_y));
55 WriteShort(MSG_ENTITY, rint(self.velocity_z));
57 WriteShort(MSG_ENTITY, rint(self.angles_y));
62 WriteCoord(MSG_ENTITY, self.anim_start_time);
63 WriteByte(MSG_ENTITY, self.frame);
68 WriteByte(MSG_ENTITY, self.team);
70 WriteByte(MSG_ENTITY, 0);
72 WriteByte(MSG_ENTITY, rint((self.health / self.tur_health) * 255)); // Send health as 0-255 insted of real value, where 255 = 100%
78 void load_unit_settings(entity ent, string unitname, float is_reload)
85 if not (ent.turret_scale_damage) ent.turret_scale_damage = 1;
86 if not (ent.turret_scale_range) ent.turret_scale_range = 1;
87 if not (ent.turret_scale_refire) ent.turret_scale_refire = 1;
88 if not (ent.turret_scale_ammo) ent.turret_scale_ammo = 1;
89 if not (ent.turret_scale_aim) ent.turret_scale_aim = 1;
90 if not (ent.turret_scale_health) ent.turret_scale_health = 1;
91 if not (ent.turret_scale_respawn) ent.turret_scale_respawn = 1;
93 sbase = strcat(cvar_base,unitname);
97 ent.tur_head.avelocity = '0 0 0';
99 ent.tur_head.angles = '0 0 0';
102 ent.health = cvar(strcat(sbase,"_health")) * ent.turret_scale_health;
103 ent.respawntime = cvar(strcat(sbase,"_respawntime")) * ent.turret_scale_respawn;
105 ent.shot_dmg = cvar(strcat(sbase,"_shot_dmg")) * ent.turret_scale_damage;
106 ent.shot_refire = cvar(strcat(sbase,"_shot_refire")) * ent.turret_scale_refire;
107 ent.shot_radius = cvar(strcat(sbase,"_shot_radius")) * ent.turret_scale_damage;
108 ent.shot_speed = cvar(strcat(sbase,"_shot_speed"));
109 ent.shot_spread = cvar(strcat(sbase,"_shot_spread"));
110 ent.shot_force = cvar(strcat(sbase,"_shot_force")) * ent.turret_scale_damage;
111 ent.shot_volly = cvar(strcat(sbase,"_shot_volly"));
112 ent.shot_volly_refire = cvar(strcat(sbase,"_shot_volly_refire")) * ent.turret_scale_refire;
114 ent.target_range = cvar(strcat(sbase,"_target_range")) * ent.turret_scale_range;
115 ent.target_range_min = cvar(strcat(sbase,"_target_range_min")) * ent.turret_scale_range;
116 ent.target_range_optimal = cvar(strcat(sbase,"_target_range_optimal")) * ent.turret_scale_range;
117 //ent.target_range_fire = cvar(strcat(sbase,"_target_range_fire")) * ent.turret_scale_range;
119 ent.target_select_rangebias = cvar(strcat(sbase,"_target_select_rangebias"));
120 ent.target_select_samebias = cvar(strcat(sbase,"_target_select_samebias"));
121 ent.target_select_anglebias = cvar(strcat(sbase,"_target_select_anglebias"));
122 ent.target_select_playerbias = cvar(strcat(sbase,"_target_select_playerbias"));
123 //ent.target_select_fov = cvar(cvar_gets(sbase,"_target_select_fov"));
125 ent.ammo_max = cvar(strcat(sbase,"_ammo_max")) * ent.turret_scale_ammo;
126 ent.ammo_recharge = cvar(strcat(sbase,"_ammo_recharge")) * ent.turret_scale_ammo;
128 ent.aim_firetolerance_dist = cvar(strcat(sbase,"_aim_firetolerance_dist"));
129 ent.aim_speed = cvar(strcat(sbase,"_aim_speed")) * ent.turret_scale_aim;
130 ent.aim_maxrot = cvar(strcat(sbase,"_aim_maxrot"));
131 ent.aim_maxpitch = cvar(strcat(sbase,"_aim_maxpitch"));
133 ent.track_type = cvar(strcat(sbase,"_track_type"));
134 ent.track_accel_pitch = cvar(strcat(sbase,"_track_accel_pitch"));
135 ent.track_accel_rot = cvar(strcat(sbase,"_track_accel_rot"));
136 ent.track_blendrate = cvar(strcat(sbase,"_track_blendrate"));
139 if(ent.turret_respawnhook)
140 ent.turret_respawnhook();
145 ** updates enemy distances, predicted impact point/time
146 ** and updated aim<->predict impact distance.
148 void turret_do_updates(entity t_turret)
150 vector enemy_pos, oldpos;
156 enemy_pos = real_origin(self.enemy);
158 turret_tag_fire_update();
160 self.tur_shotdir_updated = v_forward;
161 self.tur_dist_enemy = vlen(self.tur_shotorg - enemy_pos);
162 self.tur_dist_aimpos = vlen(self.tur_shotorg - self.tur_aimpos);
164 if((self.firecheck_flags & TFL_FIRECHECK_VERIFIED) && (self.enemy))
166 oldpos = self.enemy.origin;
167 setorigin(self.enemy,self.tur_aimpos);
168 tracebox(self.tur_shotorg, '-1 -1 -1','1 1 1',self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos),MOVE_NORMAL,self);
169 setorigin(self.enemy,oldpos);
171 if(trace_ent == self.enemy)
172 self.tur_dist_impact_to_aimpos = 0;
174 self.tur_dist_impact_to_aimpos = vlen(trace_endpos - self.tur_aimpos);
177 tracebox(self.tur_shotorg, '-1 -1 -1','1 1 1', self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos),MOVE_NORMAL,self);
179 self.tur_dist_impact_to_aimpos = vlen(trace_endpos - self.tur_aimpos) - (vlen(self.enemy.maxs - self.enemy.mins) * 0.5);
180 self.tur_impactent = trace_ent;
181 self.tur_impacttime = vlen(self.tur_shotorg - trace_endpos) / self.shot_speed;
187 vector turret_fovsearch_pingpong()
190 if(self.phase < time)
192 if( self.tur_head.phase )
193 self.tur_head.phase = 0;
195 self.tur_head.phase = 1;
196 self.phase = time + 5;
199 if( self.tur_head.phase)
200 wish_angle = self.idle_aim + '0 1 0' * (self.aim_maxrot * (self.target_select_fov / 360));
202 wish_angle = self.idle_aim - '0 1 0' * (self.aim_maxrot * (self.target_select_fov / 360));
207 vector turret_fovsearch_steprot()
212 wish_angle = self.tur_head.angles;
213 wish_angle_x = self.idle_aim_x;
215 if (self.phase < time)
217 //rot_add = self.aim_maxrot / self.target_select_fov;
218 wish_angle_y += (self.target_select_fov * 2);
220 if(wish_angle_y > 360)
221 wish_angle_y = wish_angle_y - 360;
223 self.phase = time + 1.5;
229 vector turret_fovsearch_random()
233 if (self.phase < time)
235 wish_angle_y = random() * self.aim_maxrot;
239 wish_angle_x = random() * self.aim_maxpitch;
243 self.phase = time + 5;
245 self.tur_aimpos = wish_angle;
248 return self.idle_aim + self.tur_aimpos;
253 ** Handles head rotation according to
254 ** the units .track_type and .track_flags
256 .float turret_framecounter;
257 void turret_stdproc_track()
259 vector target_angle; // This is where we want to aim
260 vector move_angle; // This is where we can aim
263 v1 = self.tur_head.angles;
264 v2 = self.tur_head.avelocity;
266 if (self.track_flags == TFL_TRACK_NO)
269 if not (self.tur_active)
270 target_angle = self.idle_aim - ('1 0 0' * self.aim_maxpitch);
271 else if (self.enemy == world)
274 target_angle = self.idle_aim + self.angles;
276 target_angle = vectoangles(normalize(self.tur_aimpos - self.tur_shotorg));
280 target_angle = vectoangles(normalize(self.tur_aimpos - self.tur_shotorg));
283 self.tur_head.angles_x = anglemods(self.tur_head.angles_x);
284 self.tur_head.angles_y = anglemods(self.tur_head.angles_y);
286 // Find the diffrence between where we currently aim and where we want to aim
287 //move_angle = target_angle - (self.angles + self.tur_head.angles);
288 move_angle = AnglesTransform_ToAngles(AnglesTransform_LeftDivide(AnglesTransform_FromAngles(self.angles), AnglesTransform_FromAngles(target_angle))) - self.tur_head.angles;
290 move_angle = shortangle_vxy(move_angle,(self.angles + self.tur_head.angles));
292 switch(self.track_type)
294 case TFL_TRACKTYPE_STEPMOTOR:
295 f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
296 if (self.track_flags & TFL_TRACK_PITCH)
298 self.tur_head.angles_x += bound(-f_tmp,move_angle_x, f_tmp);
299 if(self.tur_head.angles_x > self.aim_maxpitch)
300 self.tur_head.angles_x = self.aim_maxpitch;
302 if(self.tur_head.angles_x < -self.aim_maxpitch)
303 self.tur_head.angles_x = self.aim_maxpitch;
306 if (self.track_flags & TFL_TRACK_ROT)
308 self.tur_head.angles_y += bound(-f_tmp, move_angle_y, f_tmp);
309 if(self.tur_head.angles_y > self.aim_maxrot)
310 self.tur_head.angles_y = self.aim_maxrot;
312 if(self.tur_head.angles_y < -self.aim_maxrot)
313 self.tur_head.angles_y = self.aim_maxrot;
317 self.SendFlags = TNSF_ANG;
321 case TFL_TRACKTYPE_FLUIDINERTIA:
322 f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
323 move_angle_x = bound(-self.aim_speed, move_angle_x * self.track_accel_pitch * f_tmp, self.aim_speed);
324 move_angle_y = bound(-self.aim_speed, move_angle_y * self.track_accel_rot * f_tmp, self.aim_speed);
325 move_angle = (self.tur_head.avelocity * self.track_blendrate) + (move_angle * (1 - self.track_blendrate));
328 case TFL_TRACKTYPE_FLUIDPRECISE:
330 move_angle_y = bound(-self.aim_speed, move_angle_y, self.aim_speed);
331 move_angle_x = bound(-self.aim_speed, move_angle_x, self.aim_speed);
337 if (self.track_flags & TFL_TRACK_PITCH)
339 self.tur_head.avelocity_x = move_angle_x;
340 if((self.tur_head.angles_x + self.tur_head.avelocity_x * self.ticrate) > self.aim_maxpitch)
342 self.tur_head.avelocity_x = 0;
343 self.tur_head.angles_x = self.aim_maxpitch;
345 self.SendFlags |= TNSF_ANG;
348 if((self.tur_head.angles_x + self.tur_head.avelocity_x * self.ticrate) < -self.aim_maxpitch)
350 self.tur_head.avelocity_x = 0;
351 self.tur_head.angles_x = -self.aim_maxpitch;
353 self.SendFlags |= TNSF_ANG;
358 if (self.track_flags & TFL_TRACK_ROT)
360 self.tur_head.avelocity_y = move_angle_y;
362 if((self.tur_head.angles_y + self.tur_head.avelocity_y * self.ticrate) > self.aim_maxrot)
364 self.tur_head.avelocity_y = 0;
365 self.tur_head.angles_y = self.aim_maxrot;
367 self.SendFlags |= TNSF_ANG;
370 if((self.tur_head.angles_y + self.tur_head.avelocity_y * self.ticrate) < -self.aim_maxrot)
372 self.tur_head.avelocity_y = 0;
373 self.tur_head.angles_y = -self.aim_maxrot;
375 self.SendFlags |= TNSF_ANG;
379 self.SendFlags |= TNSF_AVEL;
381 // Force a angle update every 10'th frame
382 self.turret_framecounter += 1;
383 if(self.turret_framecounter >= 10)
385 self.SendFlags |= TNSF_ANG;
386 self.turret_framecounter = 0;
396 + TFL_FIRECHECK_WORLD
398 + TFL_FIRECHECK_DISTANCES
400 + TFL_FIRECHECK_AIMDIST
401 + TFL_FIRECHECK_REALDIST
402 - TFL_FIRECHECK_ANGLEDIST
403 - TFL_FIRECHECK_TEAMCECK
405 + TFL_FIRECHECK_OWM_AMMO
406 + TFL_FIRECHECK_OTHER_AMMO
407 + TFL_FIRECHECK_REFIRE
411 ** Preforms pre-fire checks based on the uints firecheck_flags
413 float turret_stdproc_firecheck()
415 // This one just dont care =)
416 if (self.firecheck_flags & TFL_FIRECHECK_NO) return 1;
419 if (self.firecheck_flags & TFL_FIRECHECK_REFIRE)
420 if (self.attack_finished_single > time) return 0;
422 // Special case: volly fire turret that has to fire a full volly if a shot was fired.
423 if (self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
424 if (self.volly_counter != self.shot_volly)
425 if(self.ammo >= self.shot_dmg)
428 // Lack of zombies makes shooting dead things unnecessary :P
429 if (self.firecheck_flags & TFL_FIRECHECK_DEAD)
430 if (self.enemy.deadflag != DEAD_NO)
433 // Plz stop killing the world!
434 if (self.firecheck_flags & TFL_FIRECHECK_WORLD)
435 if (self.enemy == world)
439 if (self.firecheck_flags & TFL_FIRECHECK_OWM_AMMO)
440 if (self.ammo < self.shot_dmg)
443 // Other's ammo? (support-supply units)
444 if (self.firecheck_flags & TFL_FIRECHECK_OTHER_AMMO)
445 if (self.enemy.ammo >= self.enemy.ammo_max)
448 // Target of opertunity?
449 if(turret_validate_target(self, self.tur_impactent, self.target_validate_flags) > 0)
451 self.enemy = self.tur_impactent;
455 if (self.firecheck_flags & TFL_FIRECHECK_DISTANCES)
458 if (self.tur_dist_aimpos < self.target_range_min)
459 if(turret_validate_target(self, self.tur_impactent, self.target_validate_flags) > 0)
460 return 1; // Target of opertunity?
466 if (self.firecheck_flags & TFL_FIRECHECK_AFF)
467 if (self.tur_impactent.team == self.team)
470 // aim<->predicted impact
471 if (self.firecheck_flags & TFL_FIRECHECK_AIMDIST)
472 if (self.tur_dist_impact_to_aimpos > self.aim_firetolerance_dist)
476 if (self.shot_volly > 1)
477 if (self.volly_counter == self.shot_volly)
478 if (self.ammo < (self.shot_dmg * self.shot_volly))
481 if(self.firecheck_flags & TFL_FIRECHECK_VERIFIED)
482 if(self.tur_impactent != self.enemy)
489 + TFL_TARGETSELECT_NO
490 + TFL_TARGETSELECT_LOS
491 + TFL_TARGETSELECT_PLAYERS
492 + TFL_TARGETSELECT_MISSILES
493 - TFL_TARGETSELECT_TRIGGERTARGET
494 + TFL_TARGETSELECT_ANGLELIMITS
495 + TFL_TARGETSELECT_RANGELIMTS
496 + TFL_TARGETSELECT_TEAMCHECK
497 - TFL_TARGETSELECT_NOBUILTIN
498 + TFL_TARGETSELECT_OWNTEAM
502 ** Evaluate a entity for target valitity based on validate_flags
503 ** NOTE: the caller must check takedamage before calling this, to inline this check.
505 float turret_validate_target(entity e_turret, entity e_target, float validate_flags)
509 //if(!validate_flags & TFL_TARGETSELECT_NOBUILTIN)
512 if(e_target.owner == e_turret)
515 if not(checkpvs(e_target.origin, e_turret))
522 if (substring(e_target.classname, 0, 10) == "onslaught_") // don't attack onslaught targets, that's the player's job!
525 if (validate_flags & TFL_TARGETSELECT_NO)
528 // If only this was used more..
529 if (e_target.flags & FL_NOTARGET)
533 if (e_target.health < 0)
537 if (e_target.flags & FL_CLIENT)
539 if not (validate_flags & TFL_TARGETSELECT_PLAYERS)
542 if (e_target.deadflag != DEAD_NO)
547 if (validate_flags & TFL_TARGETSELECT_NOTURRETS)
548 if (e_target.turret_firefunc || e_target.owner.tur_head == e_target)
549 if(e_target.team != e_turret.team) // Dont break support units.
553 if (e_target.flags & FL_PROJECTILE)
554 if not (validate_flags & TFL_TARGETSELECT_MISSILES)
557 if (validate_flags & TFL_TARGETSELECT_MISSILESONLY)
558 if not (e_target.flags & FL_PROJECTILE)
562 if (validate_flags & TFL_TARGETSELECT_TEAMCHECK)
564 if (validate_flags & TFL_TARGETSELECT_OWNTEAM)
566 if (e_target.team != e_turret.team)
569 if (e_turret.team != e_target.owner.team)
574 if (e_target.team == e_turret.team)
577 if (e_turret.team == e_target.owner.team)
583 tvt_dist = vlen(e_turret.origin - real_origin(e_target));
584 if (validate_flags & TFL_TARGETSELECT_RANGELIMTS)
586 if (tvt_dist < e_turret.target_range_min)
589 if (tvt_dist > e_turret.target_range)
593 // Can we even aim this thing?
594 tvt_thadv = angleofs3(e_turret.tur_head.origin, e_turret.angles + e_turret.tur_head.angles, e_target);
595 tvt_tadv = shortangle_vxy(angleofs(e_turret, e_target), e_turret.angles);
596 tvt_thadf = vlen(tvt_thadv);
597 tvt_tadf = vlen(tvt_tadv);
600 if(validate_flags & TFL_TARGETSELECT_FOV)
602 if(e_turret.target_select_fov < tvt_thadf)
607 if (validate_flags & TFL_TARGETSELECT_ANGLELIMITS)
609 if (fabs(tvt_tadv_x) > e_turret.aim_maxpitch)
612 if (fabs(tvt_tadv_y) > e_turret.aim_maxrot)
617 if (validate_flags & TFL_TARGETSELECT_LOS)
619 v_tmp = real_origin(e_target) + ((e_target.mins + e_target.maxs) * 0.5);
621 traceline(e_turret.tur_shotorg, v_tmp, 0, e_turret);
623 if (e_turret.aim_firetolerance_dist < vlen(v_tmp - trace_endpos))
627 if (e_target.classname == "grapplinghook")
631 if (e_target.classname == "func_button")
635 #ifdef TURRET_DEBUG_TARGETSELECT
636 dprint("Target:",e_target.netname," is a valid target for ",e_turret.netname,"\n");
642 entity turret_select_target()
644 entity e; // target looper entity
645 float score; // target looper entity score
646 entity e_enemy; // currently best scoreing target
647 float m_score; // currently best scoreing target's score
651 if(self.enemy.takedamage)
652 if(turret_validate_target(self,self.enemy,self.target_validate_flags) > 0)
654 e_enemy = self.enemy;
655 m_score = self.turret_score_target(self,e_enemy) * self.target_select_samebias;
660 e = findradius(self.origin, self.target_range);
662 // Nothing to aim at?
670 if (turret_validate_target(self, e, self.target_select_flags) > 0)
672 score = self.turret_score_target(self,e);
673 if ((score > m_score) && (score > 0))
690 self.nextthink = time + self.ticrate;
691 //self.SendFlags = TNSF_UPDATE | TNSF_STATUS | TNSF_ANG | TNSF_AVEL;
693 // ONS uses somewhat backwards linking.
699 e = find(world, targetname,self.target);
704 if (self.team != self.tur_head.team)
705 turret_stdproc_respawn();
709 if (self.tur_dbg_tmr1 < time)
711 if (self.enemy) paint_target (self.enemy,128,self.tur_dbg_rvec,0.9);
712 paint_target(self,256,self.tur_dbg_rvec,0.9);
713 self.tur_dbg_tmr1 = time + 1;
718 if not (self.spawnflags & TSF_NO_AMMO_REGEN)
719 if (self.ammo < self.ammo_max)
720 self.ammo = min(self.ammo + self.ammo_recharge, self.ammo_max);
722 // Inactive turrets needs to run the think loop,
723 // So they can handle animation and wake up if need be.
724 if not (self.tur_active)
726 turret_stdproc_track();
730 // This is typicaly used for zaping every target in range
731 // turret_fusionreactor uses this to recharge friendlys.
732 if (self.shoot_flags & TFL_SHOOT_HITALLVALID)
734 // Do a self.turret_fire for every valid target.
735 e = findradius(self.origin,self.target_range);
740 if (turret_validate_target(self,e,self.target_validate_flags))
744 turret_do_updates(self);
746 if (self.turret_firecheckfunc())
755 else if(self.shoot_flags & TFL_SHOOT_CUSTOM)
757 // This one is doing something.. oddball. assume its handles what needs to be handled.
760 if not(self.aim_flags & TFL_AIM_NO)
761 self.tur_aimpos = turret_stdproc_aim_generic();
764 if not(self.track_flags & TFL_TRACK_NO)
765 turret_stdproc_track();
767 turret_do_updates(self);
770 if (self.turret_firecheckfunc())
775 // Special case for volly always. if it fired once it must compleate the volly.
776 if(self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
777 if(self.volly_counter != self.shot_volly)
779 // Predict or whatnot
780 if not(self.aim_flags & TFL_AIM_NO)
781 self.tur_aimpos = turret_stdproc_aim_generic();
784 if not(self.track_flags & TFL_TRACK_NO)
785 turret_stdproc_track();
787 turret_do_updates(self);
790 if (self.turret_firecheckfunc() != 0)
793 if(self.turret_postthink)
794 self.turret_postthink();
799 // Check if we have a vailid enemy, and try to find one if we dont.
801 // g_turrets_targetscan_maxdelay forces a target re-scan at least this often
802 float do_target_scan;
803 if((self.target_select_time + autocvar_g_turrets_targetscan_maxdelay) < time)
806 // Old target (if any) invalid?
807 if (turret_validate_target(self, self.enemy, self.target_validate_flags) <= 0)
813 // But never more often then g_turrets_targetscan_mindelay!
814 if (self.target_select_time + autocvar_g_turrets_targetscan_mindelay > time)
819 self.enemy = turret_select_target();
820 self.target_select_time = time;
823 // No target, just go to idle, do any custom stuff and bail.
824 if (self.enemy == world)
827 if not(self.track_flags & TFL_TRACK_NO)
828 turret_stdproc_track();
830 // do any per-turret stuff
831 if(self.turret_postthink)
832 self.turret_postthink();
838 self.lip = time + autocvar_g_turrets_aimidle_delay; // Keep track of the last time we had a target.
841 if not(self.aim_flags & TFL_AIM_NO)
842 self.tur_aimpos = turret_stdproc_aim_generic();
845 if not(self.track_flags & TFL_TRACK_NO)
846 turret_stdproc_track();
848 turret_do_updates(self);
851 if (self.turret_firecheckfunc())
855 // do any custom per-turret stuff
856 if(self.turret_postthink)
857 self.turret_postthink();
862 if (autocvar_g_turrets_nofire != 0)
865 self.turret_firefunc();
867 self.attack_finished_single = time + self.shot_refire;
868 self.ammo -= self.shot_dmg;
869 self.volly_counter = self.volly_counter - 1;
871 if (self.volly_counter <= 0)
873 self.volly_counter = self.shot_volly;
875 if (self.shoot_flags & TFL_SHOOT_CLEARTARGET)
878 if (self.shot_volly > 1)
879 self.attack_finished_single = time + self.shot_volly_refire;
883 if (self.enemy) paint_target3(self.tur_aimpos, 64, self.tur_dbg_rvec, self.tur_impacttime + 0.25);
887 void turret_stdproc_fire()
889 dprint("^1Bang, ^3your dead^7 ",self.enemy.netname,"! ^1(turret with no real firefunc)\n");
893 When .used a turret switch team to activator.team.
894 If activator is world, the turret go inactive.
896 void turret_stdproc_use()
898 dprint("Turret ",self.netname, " used by ", activator.classname, "\n");
900 self.team = activator.team;
911 Net_LinkEntity(self, TRUE, 0, turret_send);
912 self.think = turret_think;
913 self.nextthink = time;
914 self.tur_head.effects = EF_NODRAW;
917 void turrets_manager_think()
919 self.nextthink = time + 1;
922 if (autocvar_g_turrets_reloadcvars == 1)
927 if (e.turrcaps_flags & TFL_TURRCAPS_ISTURRET)
929 load_unit_settings(e,e.cvar_basename,1);
930 if(e.turret_postthink)
931 e.turret_postthink();
936 cvar_set("g_turrets_reloadcvars","0");
941 * Standard turret initialization. use this!
942 * (unless you have a very good reason not to)
943 * if the return value is 0, the turret should be removed.
945 float turret_stdproc_init (string cvar_base_name, string base, string head, float _turret_type)
949 // Are turrets allowed?
950 if (autocvar_g_turrets == 0)
953 if(_turret_type < 1 || _turret_type > TID_LAST)
955 dprint("Invalid / Unkown turret type\"", ftos(_turret_type), "\", aborting!\n");
958 self.turret_type = _turret_type;
960 e = find(world, classname, "turret_manager");
966 setorigin(e,'0 0 0');
967 setmodel(e,"models/turrets/plasma.md3");
969 v = gettaginfo(e,gettagindex(e,"tag_fire"));
972 //objerror("^1ERROR: Engine is borken! Turrets will NOT work. force g_turrets to 0 to run maps with turrets anyway.");
978 e.classname = "turret_manager";
979 e.think = turrets_manager_think;
980 e.nextthink = time + 2;
986 dprint("WARNING: turret requested csqc_shared but this is not implemented. Expect strange things to happen.\n");
991 if not (self.spawnflags & TSF_SUSPENDED)
992 droptofloor_builtin();
994 // Terrainbase spawnflag. This puts a enlongated model
995 // under the turret, so it looks ok on uneaven surfaces.
996 /* TODO: Handle this with CSQC
997 if (self.spawnflags & TSF_TERRAINBASE)
1001 setmodel(tb,"models/turrets/terrainbase.md3");
1002 setorigin(tb,self.origin);
1003 tb.solid = SOLID_BBOX;
1007 self.cvar_basename = cvar_base_name;
1008 load_unit_settings(self, self.cvar_basename, 0);
1010 self.effects = EF_NODRAW;
1012 // Handle turret teams.
1013 if (autocvar_g_assault != 0)
1016 self.team = 14; // Assume turrets are on the defending side if not explicitly set otehrwize
1018 else if not (teamplay)
1019 self.team = MAX_SHOT_DISTANCE; // Group all turrets into the same team, so they dont kill eachother.
1020 else if(g_onslaught && self.targetname)
1022 e = find(world,target,self.targetname);
1030 self.team = MAX_SHOT_DISTANCE; // Group all turrets into the same team, so they dont kill eachother.
1033 * Try to guess some reasonaly defaults
1034 * for missing params and do sanety checks
1035 * thise checks could produce some "interesting" results
1036 * if it hits a glitch in my logic :P so try to set as mutch
1037 * as possible beforehand.
1039 if (self.turrcaps_flags & TFL_TURRCAPS_SUPPORT)
1040 self.ticrate = 0.2; // Support units generaly dont need to have a high speed ai-loop
1042 self.ticrate = 0.1; // 10 fps for normal turrets
1044 self.ticrate = bound(sys_frametime, self.ticrate, 60); // keep it sane
1047 if (self.netname == "")
1048 self.netname = self.classname;
1050 if not (self.respawntime)
1051 self.respawntime = 60;
1052 self.respawntime = max(-1, self.respawntime);
1054 if not (self.health)
1056 self.tur_health = max(1, self.health);
1058 if not (self.turrcaps_flags)
1059 self.turrcaps_flags = TFL_TURRCAPS_RADIUSDMG | TFL_TURRCAPS_MEDPROJ | TFL_TURRCAPS_PLAYERKILL;
1061 if not (self.damage_flags)
1062 self.damage_flags = TFL_DMG_YES | TFL_DMG_RETALIATE | TFL_DMG_AIMSHAKE;
1065 if not (self.shot_refire)
1066 self.shot_refire = 1;
1067 self.shot_refire = bound(0.01, self.shot_refire, 9999);
1069 if not (self.shot_dmg)
1070 self.shot_dmg = self.shot_refire * 50;
1071 self.shot_dmg = max(1, self.shot_dmg);
1073 if not (self.shot_radius)
1074 self.shot_radius = self.shot_dmg * 0.5;
1075 self.shot_radius = max(1, self.shot_radius);
1077 if not (self.shot_speed)
1078 self.shot_speed = 2500;
1079 self.shot_speed = max(1, self.shot_speed);
1081 if not (self.shot_spread)
1082 self.shot_spread = 0.0125;
1083 self.shot_spread = bound(0.0001, self.shot_spread, 500);
1085 if not (self.shot_force)
1086 self.shot_force = self.shot_dmg * 0.5 + self.shot_radius * 0.5;
1087 self.shot_force = bound(0.001, self.shot_force, 5000);
1089 if not (self.shot_volly)
1090 self.shot_volly = 1;
1091 self.shot_volly = bound(1, self.shot_volly, floor(self.ammo_max / self.shot_dmg));
1093 if not (self.shot_volly_refire)
1094 self.shot_volly_refire = self.shot_refire * self.shot_volly;
1095 self.shot_volly_refire = bound(self.shot_refire, self.shot_volly_refire, 60);
1097 if not (self.firecheck_flags)
1098 self.firecheck_flags = TFL_FIRECHECK_WORLD | TFL_FIRECHECK_DEAD | TFL_FIRECHECK_DISTANCES |
1099 TFL_FIRECHECK_LOS | TFL_FIRECHECK_AIMDIST | TFL_FIRECHECK_TEAMCECK |
1100 TFL_FIRECHECK_OWM_AMMO | TFL_FIRECHECK_REFIRE | TFL_FIRECHECK_WORLD;
1103 if not (self.target_range)
1104 self.target_range = self.shot_speed * 0.5;
1105 self.target_range = bound(0, self.target_range, MAX_SHOT_DISTANCE);
1107 if not (self.target_range_min)
1108 self.target_range_min = self.shot_radius * 2;
1109 self.target_range_min = bound(0, self.target_range_min, MAX_SHOT_DISTANCE);
1111 if not (self.target_range_optimal)
1112 self.target_range_optimal = self.target_range * 0.5;
1113 self.target_range_optimal = bound(0, self.target_range_optimal, MAX_SHOT_DISTANCE);
1117 if not (self.aim_maxrot)
1118 self.aim_maxrot = 90;
1119 self.aim_maxrot = bound(0, self.aim_maxrot, 360);
1121 if not (self.aim_maxpitch)
1122 self.aim_maxpitch = 20;
1123 self.aim_maxpitch = bound(0, self.aim_maxpitch, 90);
1125 if not (self.aim_speed)
1126 self.aim_speed = 36;
1127 self.aim_speed = bound(0.1, self.aim_speed, 1000);
1129 if not (self.aim_firetolerance_dist)
1130 self.aim_firetolerance_dist = 5 + (self.shot_radius * 2);
1131 self.aim_firetolerance_dist = bound(0.1, self.aim_firetolerance_dist, MAX_SHOT_DISTANCE);
1133 if not (self.aim_flags)
1135 self.aim_flags = TFL_AIM_LEAD | TFL_AIM_SHOTTIMECOMPENSATE;
1136 if(self.turrcaps_flags & TFL_TURRCAPS_RADIUSDMG)
1137 self.aim_flags |= TFL_AIM_GROUND2;
1140 if not (self.track_type)
1141 self.track_type = TFL_TRACKTYPE_STEPMOTOR;
1143 if (self.track_type != TFL_TRACKTYPE_STEPMOTOR)
1145 // Fluid / Ineria mode. Looks mutch nicer.
1146 // Can reduce aim preformance alot, needs a bit diffrent aimspeed
1148 if not (self.aim_speed)
1149 self.aim_speed = 180;
1150 self.aim_speed = bound(0.1, self.aim_speed, 1000);
1152 if not (self.track_accel_pitch)
1153 self.track_accel_pitch = 0.5;
1155 if not (self.track_accel_rot)
1156 self.track_accel_rot = 0.5;
1158 if not (self.track_blendrate)
1159 self.track_blendrate = 0.35;
1162 if (!self.track_flags)
1163 self.track_flags = TFL_TRACK_PITCH | TFL_TRACK_ROT;
1166 // Target selection stuff.
1167 if not (self.target_select_rangebias)
1168 self.target_select_rangebias = 1;
1169 self.target_select_rangebias = bound(-10, self.target_select_rangebias, 10);
1171 if not (self.target_select_samebias)
1172 self.target_select_samebias = 1;
1173 self.target_select_samebias = bound(-10, self.target_select_samebias, 10);
1175 if not (self.target_select_anglebias)
1176 self.target_select_anglebias = 1;
1177 self.target_select_anglebias = bound(-10, self.target_select_anglebias, 10);
1179 if not (self.target_select_missilebias)
1180 self.target_select_missilebias = -10;
1182 self.target_select_missilebias = bound(-10, self.target_select_missilebias, 10);
1183 self.target_select_playerbias = bound(-10, self.target_select_playerbias, 10);
1185 if not (self.target_select_flags)
1187 self.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_TEAMCHECK
1188 | TFL_TARGETSELECT_RANGELIMTS | TFL_TARGETSELECT_ANGLELIMITS;
1190 if (self.turrcaps_flags & TFL_TURRCAPS_MISSILEKILL)
1191 self.target_select_flags |= TFL_TARGETSELECT_MISSILES;
1193 if (self.turrcaps_flags & TFL_TURRCAPS_PLAYERKILL)
1194 self.target_select_flags |= TFL_TARGETSELECT_PLAYERS;
1196 // self.target_select_flags = TFL_TARGETSELECT_NO;
1199 self.target_validate_flags = self.target_select_flags;
1202 if not (self.ammo_max)
1203 self.ammo_max = self.shot_dmg * 10;
1204 self.ammo_max = max(self.shot_dmg, self.ammo_max);
1207 self.ammo = self.shot_dmg * 5;
1208 self.ammo = bound(0,self.ammo, self.ammo_max);
1210 if not (self.ammo_recharge)
1211 self.ammo_recharge = self.shot_dmg * 0.5;
1212 self.ammo_recharge = max(0 ,self.ammo_recharge);
1214 // Convert the recharge from X per sec to X per ticrate
1215 self.ammo_recharge = self.ammo_recharge * self.ticrate;
1217 if not (self.ammo_flags)
1218 self.ammo_flags = TFL_AMMO_ENERGY | TFL_AMMO_RECHARGE;
1221 if(self.spawnflags & TSL_NO_RESPAWN)
1222 if not (self.damage_flags & TFL_DMG_DEATH_NORESPAWN)
1223 self.damage_flags |= TFL_DMG_DEATH_NORESPAWN;
1225 // Offsets & origins
1226 if (!self.tur_shotorg) self.tur_shotorg = '50 0 50';
1232 if(MUTATOR_CALLHOOK(TurretSpawn))
1235 // End of default & sanety checks, start building the turret.
1238 self.tur_head = spawn();
1239 self.tur_head.netname = self.tur_head.classname = "turret_head";
1240 self.tur_head.team = self.team;
1241 self.tur_head.owner = self;
1243 setmodel(self, base);
1244 setmodel(self.tur_head, head);
1246 setsize(self, '-32 -32 0', '32 32 64');
1247 setsize(self.tur_head, '0 0 0', '0 0 0');
1249 setorigin(self.tur_head, '0 0 0');
1250 setattachment(self.tur_head, self, "tag_head");
1252 self.tur_health = self.health;
1253 self.solid = SOLID_BBOX;
1254 self.tur_head.solid = SOLID_NOT;
1255 self.takedamage = DAMAGE_AIM;
1256 self.tur_head.takedamage = DAMAGE_NO;
1257 self.movetype = MOVETYPE_NOCLIP;
1258 self.tur_head.movetype = MOVETYPE_NOCLIP;
1261 if not (self.tur_defend)
1262 if (self.target != "")
1264 self.tur_defend = find(world, targetname, self.target);
1265 if (self.tur_defend == world)
1268 dprint("Turret has invalid defendpoint!\n");
1272 // In target defend mode, aim on the spot to defend when idle.
1273 if (self.tur_defend)
1274 self.idle_aim = self.tur_head.angles + angleofs(self.tur_head, self.tur_defend);
1276 self.idle_aim = '0 0 0';
1278 // Attach stdprocs. override when and what needed
1279 self.turret_firecheckfunc = turret_stdproc_firecheck;
1280 self.turret_firefunc = turret_stdproc_fire;
1281 self.event_damage = turret_stdproc_damage;
1283 if (self.turrcaps_flags & TFL_TURRCAPS_SUPPORT)
1284 self.turret_score_target = turret_stdproc_targetscore_support;
1286 self.turret_score_target = turret_stdproc_targetscore_generic;
1288 self.use = turret_stdproc_use;
1289 self.bot_attack = TRUE;
1292 self.nextthink = time + 1;
1293 self.nextthink += turret_count * sys_frametime;
1295 self.tur_head.team = self.team;
1296 self.view_ofs = '0 0 0';
1299 self.tur_dbg_start = self.nextthink;
1300 while (vlen(self.tur_dbg_rvec) < 2)
1301 self.tur_dbg_rvec = randomvec() * 4;
1303 self.tur_dbg_rvec_x = fabs(self.tur_dbg_rvec_x);
1304 self.tur_dbg_rvec_y = fabs(self.tur_dbg_rvec_y);
1305 self.tur_dbg_rvec_z = fabs(self.tur_dbg_rvec_z);
1309 self.turrcaps_flags |= TFL_TURRCAPS_ISTURRET;
1311 self.classname = "turret_main";
1313 self.tur_active = 1;
1315 // In ONS mode, and linked to a ONS ent. need to call the use to set team.
1316 if (g_onslaught && ee)
1323 turret_stdproc_respawn();
1325 if (!turret_tag_fire_update())
1326 dprint("Warning: Turret ",self.classname, " faild to initialize md3 tags\n");