3 #include "../../g_damage.qh"
4 #include "../../bot/bot.qh"
6 #define cvar_base "g_turrets_unit_"
8 void turrets_setframe(float _frame, float client_only)
10 if((client_only ? self.clientframe : self.frame ) != _frame)
12 self.SendFlags |= TNSF_ANIM;
13 self.anim_start_time = time;
17 self.clientframe = _frame;
23 float turret_send(entity to, int sf)
26 WriteByte(MSG_ENTITY, ENT_CLIENT_TURRET);
27 WriteByte(MSG_ENTITY, sf);
30 WriteByte(MSG_ENTITY, self.turret_type);
32 WriteCoord(MSG_ENTITY, self.origin.x);
33 WriteCoord(MSG_ENTITY, self.origin.y);
34 WriteCoord(MSG_ENTITY, self.origin.z);
36 WriteAngle(MSG_ENTITY, self.angles.x);
37 WriteAngle(MSG_ENTITY, self.angles.y);
42 WriteShort(MSG_ENTITY, rint(self.tur_head.angles.x));
43 WriteShort(MSG_ENTITY, rint(self.tur_head.angles.y));
48 WriteShort(MSG_ENTITY, rint(self.tur_head.avelocity.x));
49 WriteShort(MSG_ENTITY, rint(self.tur_head.avelocity.y));
54 WriteShort(MSG_ENTITY, rint(self.origin.x));
55 WriteShort(MSG_ENTITY, rint(self.origin.y));
56 WriteShort(MSG_ENTITY, rint(self.origin.z));
58 WriteShort(MSG_ENTITY, rint(self.velocity.x));
59 WriteShort(MSG_ENTITY, rint(self.velocity.y));
60 WriteShort(MSG_ENTITY, rint(self.velocity.z));
62 WriteShort(MSG_ENTITY, rint(self.angles.y));
67 WriteCoord(MSG_ENTITY, self.anim_start_time);
68 WriteByte(MSG_ENTITY, self.frame);
73 WriteByte(MSG_ENTITY, self.team);
76 WriteByte(MSG_ENTITY, 0);
78 WriteByte(MSG_ENTITY, ceil((self.health / self.tur_health) * 255));
84 void load_unit_settings(entity ent, string unitname, float is_reload)
91 if (!ent.turret_scale_damage) ent.turret_scale_damage = 1;
92 if (!ent.turret_scale_range) ent.turret_scale_range = 1;
93 if (!ent.turret_scale_refire) ent.turret_scale_refire = 1;
94 if (!ent.turret_scale_ammo) ent.turret_scale_ammo = 1;
95 if (!ent.turret_scale_aim) ent.turret_scale_aim = 1;
96 if (!ent.turret_scale_health) ent.turret_scale_health = 1;
97 if (!ent.turret_scale_respawn) ent.turret_scale_respawn = 1;
99 sbase = strcat(cvar_base,unitname);
103 ent.tur_head.avelocity = '0 0 0';
105 ent.tur_head.angles = '0 0 0';
108 ent.health = cvar(strcat(sbase,"_health")) * ent.turret_scale_health;
109 ent.respawntime = cvar(strcat(sbase,"_respawntime")) * ent.turret_scale_respawn;
111 ent.shot_dmg = cvar(strcat(sbase,"_shot_dmg")) * ent.turret_scale_damage;
112 ent.shot_refire = cvar(strcat(sbase,"_shot_refire")) * ent.turret_scale_refire;
113 ent.shot_radius = cvar(strcat(sbase,"_shot_radius")) * ent.turret_scale_damage;
114 ent.shot_speed = cvar(strcat(sbase,"_shot_speed"));
115 ent.shot_spread = cvar(strcat(sbase,"_shot_spread"));
116 ent.shot_force = cvar(strcat(sbase,"_shot_force")) * ent.turret_scale_damage;
117 ent.shot_volly = cvar(strcat(sbase,"_shot_volly"));
118 ent.shot_volly_refire = cvar(strcat(sbase,"_shot_volly_refire")) * ent.turret_scale_refire;
120 ent.target_range = cvar(strcat(sbase,"_target_range")) * ent.turret_scale_range;
121 ent.target_range_min = cvar(strcat(sbase,"_target_range_min")) * ent.turret_scale_range;
122 ent.target_range_optimal = cvar(strcat(sbase,"_target_range_optimal")) * ent.turret_scale_range;
123 //ent.target_range_fire = cvar(strcat(sbase,"_target_range_fire")) * ent.turret_scale_range;
125 ent.target_select_rangebias = cvar(strcat(sbase,"_target_select_rangebias"));
126 ent.target_select_samebias = cvar(strcat(sbase,"_target_select_samebias"));
127 ent.target_select_anglebias = cvar(strcat(sbase,"_target_select_anglebias"));
128 ent.target_select_playerbias = cvar(strcat(sbase,"_target_select_playerbias"));
129 //ent.target_select_fov = cvar(cvar_gets(sbase,"_target_select_fov"));
131 ent.ammo_max = cvar(strcat(sbase,"_ammo_max")) * ent.turret_scale_ammo;
132 ent.ammo_recharge = cvar(strcat(sbase,"_ammo_recharge")) * ent.turret_scale_ammo;
134 ent.aim_firetolerance_dist = cvar(strcat(sbase,"_aim_firetolerance_dist"));
135 ent.aim_speed = cvar(strcat(sbase,"_aim_speed")) * ent.turret_scale_aim;
136 ent.aim_maxrot = cvar(strcat(sbase,"_aim_maxrot"));
137 ent.aim_maxpitch = cvar(strcat(sbase,"_aim_maxpitch"));
139 ent.track_type = cvar(strcat(sbase,"_track_type"));
140 ent.track_accel_pitch = cvar(strcat(sbase,"_track_accel_pitch"));
141 ent.track_accel_rot = cvar(strcat(sbase,"_track_accel_rot"));
142 ent.track_blendrate = cvar(strcat(sbase,"_track_blendrate"));
145 if(ent.turret_respawnhook)
146 ent.turret_respawnhook();
149 void turret_projectile_explode()
152 self.takedamage = DAMAGE_NO;
153 self.event_damage = func_null;
156 d = RadiusDamage (self, self.owner, self.owner.shot_dmg, 0, self.owner.shot_radius, self, world, self.owner.shot_force, self.totalfrags, world);
157 self.owner.tur_dbg_dmg_t_h = self.owner.tur_dbg_dmg_t_h + d;
158 self.owner.tur_dbg_dmg_t_f = self.owner.tur_dbg_dmg_t_f + self.owner.shot_dmg;
160 RadiusDamage (self, self.realowner, self.owner.shot_dmg, 0, self.owner.shot_radius, self, world, self.owner.shot_force, self.totalfrags, world);
165 void turret_projectile_touch()
168 turret_projectile_explode();
171 void turret_projectile_damage(entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector vforce)
173 self.velocity += vforce;
174 self.health -= damage;
175 //self.realowner = attacker; // Dont change realowner, it does not make much sense for turrets
177 W_PrepareExplosionByDamage(self.owner, turret_projectile_explode);
180 entity turret_projectile(string _snd, float _size, float _health, float _death, float _proj_type, float _cull, float _cli_anim)
184 sound (self, CH_WEAPON_A, _snd, VOL_BASE, ATTEN_NORM);
186 setorigin(proj, self.tur_shotorg);
187 setsize(proj, '-0.5 -0.5 -0.5' * _size, '0.5 0.5 0.5' * _size);
189 proj.realowner = self;
190 proj.bot_dodge = true;
191 proj.bot_dodgerating = self.shot_dmg;
192 proj.think = turret_projectile_explode;
193 proj.touch = turret_projectile_touch;
194 proj.nextthink = time + 9;
195 proj.movetype = MOVETYPE_FLYMISSILE;
196 proj.velocity = normalize(self.tur_shotdir_updated + randomvec() * self.shot_spread) * self.shot_speed;
197 proj.flags = FL_PROJECTILE;
198 proj.enemy = self.enemy;
199 proj.totalfrags = _death;
200 PROJECTILE_MAKETRIGGER(proj);
203 proj.health = _health;
204 proj.takedamage = DAMAGE_YES;
205 proj.event_damage = turret_projectile_damage;
208 proj.flags |= FL_NOTARGET;
210 CSQCProjectile(proj, _cli_anim, _proj_type, _cull);
216 ** updates enemy distances, predicted impact point/time
217 ** and updated aim<->predict impact distance.
219 void turret_do_updates(entity t_turret)
227 enemy_pos = real_origin(self.enemy);
229 turret_tag_fire_update();
231 self.tur_shotdir_updated = v_forward;
232 self.tur_dist_enemy = vlen(self.tur_shotorg - enemy_pos);
233 self.tur_dist_aimpos = vlen(self.tur_shotorg - self.tur_aimpos);
235 /*if((self.firecheck_flags & TFL_FIRECHECK_VERIFIED) && (self.enemy))
237 oldpos = self.enemy.origin;
238 setorigin(self.enemy, self.tur_aimpos);
239 tracebox(self.tur_shotorg, '-1 -1 -1', '1 1 1', self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos), MOVE_NORMAL,self);
240 setorigin(self.enemy, oldpos);
242 if(trace_ent == self.enemy)
243 self.tur_dist_impact_to_aimpos = 0;
245 self.tur_dist_impact_to_aimpos = vlen(trace_endpos - self.tur_aimpos);
248 tracebox(self.tur_shotorg, '-1 -1 -1','1 1 1', self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos), MOVE_NORMAL,self);
250 self.tur_dist_impact_to_aimpos = vlen(trace_endpos - self.tur_aimpos) - (vlen(self.enemy.maxs - self.enemy.mins) * 0.5);
251 self.tur_impactent = trace_ent;
252 self.tur_impacttime = vlen(self.tur_shotorg - trace_endpos) / self.shot_speed;
258 vector turret_fovsearch_pingpong()
261 if(self.phase < time)
263 if( self.tur_head.phase )
264 self.tur_head.phase = 0;
266 self.tur_head.phase = 1;
267 self.phase = time + 5;
270 if( self.tur_head.phase)
271 wish_angle = self.idle_aim + '0 1 0' * (self.aim_maxrot * (self.target_select_fov / 360));
273 wish_angle = self.idle_aim - '0 1 0' * (self.aim_maxrot * (self.target_select_fov / 360));
278 vector turret_fovsearch_steprot()
283 wish_angle = self.tur_head.angles;
284 wish_angle_x = self.idle_aim_x;
286 if (self.phase < time)
288 //rot_add = self.aim_maxrot / self.target_select_fov;
289 wish_angle_y += (self.target_select_fov * 2);
291 if(wish_angle_y > 360)
292 wish_angle_y = wish_angle_y - 360;
294 self.phase = time + 1.5;
300 vector turret_fovsearch_random()
304 if (self.phase < time)
306 wish_angle_y = random() * self.aim_maxrot;
310 wish_angle_x = random() * self.aim_maxpitch;
314 self.phase = time + 5;
316 self.tur_aimpos = wish_angle;
319 return self.idle_aim + self.tur_aimpos;
324 ** Handles head rotation according to
325 ** the units .track_type and .track_flags
327 .float turret_framecounter;
328 void turret_stdproc_track()
330 vector target_angle; // This is where we want to aim
331 vector move_angle; // This is where we can aim
334 v1 = self.tur_head.angles;
335 v2 = self.tur_head.avelocity;
337 if (self.track_flags == TFL_TRACK_NO)
341 target_angle = self.idle_aim - ('1 0 0' * self.aim_maxpitch);
342 else if (self.enemy == world)
345 target_angle = self.idle_aim + self.angles;
347 target_angle = vectoangles(normalize(self.tur_aimpos - self.tur_shotorg));
351 target_angle = vectoangles(normalize(self.tur_aimpos - self.tur_shotorg));
354 self.tur_head.angles_x = anglemods(self.tur_head.angles.x);
355 self.tur_head.angles_y = anglemods(self.tur_head.angles.y);
357 // Find the diffrence between where we currently aim and where we want to aim
358 //move_angle = target_angle - (self.angles + self.tur_head.angles);
359 //move_angle = shortangle_vxy(move_angle,(self.angles + self.tur_head.angles));
361 move_angle = AnglesTransform_ToAngles(AnglesTransform_LeftDivide(AnglesTransform_FromAngles(self.angles), AnglesTransform_FromAngles(target_angle))) - self.tur_head.angles;
362 move_angle = shortangle_vxy(move_angle, self.tur_head.angles);
364 switch(self.track_type)
366 case TFL_TRACKTYPE_STEPMOTOR:
367 f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
368 if (self.track_flags & TFL_TRACK_PITCH)
370 self.tur_head.angles_x += bound(-f_tmp,move_angle.x, f_tmp);
371 if(self.tur_head.angles.x > self.aim_maxpitch)
372 self.tur_head.angles_x = self.aim_maxpitch;
374 if(self.tur_head.angles.x < -self.aim_maxpitch)
375 self.tur_head.angles_x = self.aim_maxpitch;
378 if (self.track_flags & TFL_TRACK_ROT)
380 self.tur_head.angles_y += bound(-f_tmp, move_angle.y, f_tmp);
381 if(self.tur_head.angles.y > self.aim_maxrot)
382 self.tur_head.angles_y = self.aim_maxrot;
384 if(self.tur_head.angles.y < -self.aim_maxrot)
385 self.tur_head.angles_y = self.aim_maxrot;
389 self.SendFlags |= TNSF_ANG;
393 case TFL_TRACKTYPE_FLUIDINERTIA:
394 f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
395 move_angle.x = bound(-self.aim_speed, move_angle.x * self.track_accel_pitch * f_tmp, self.aim_speed);
396 move_angle.y = bound(-self.aim_speed, move_angle.y * self.track_accel_rot * f_tmp, self.aim_speed);
397 move_angle = (self.tur_head.avelocity * self.track_blendrate) + (move_angle * (1 - self.track_blendrate));
400 case TFL_TRACKTYPE_FLUIDPRECISE:
402 move_angle.y = bound(-self.aim_speed, move_angle.y, self.aim_speed);
403 move_angle.x = bound(-self.aim_speed, move_angle.x, self.aim_speed);
409 if (self.track_flags & TFL_TRACK_PITCH)
411 self.tur_head.avelocity_x = move_angle.x;
412 if((self.tur_head.angles.x + self.tur_head.avelocity.x * self.ticrate) > self.aim_maxpitch)
414 self.tur_head.avelocity_x = 0;
415 self.tur_head.angles_x = self.aim_maxpitch;
417 self.SendFlags |= TNSF_ANG;
420 if((self.tur_head.angles.x + self.tur_head.avelocity.x * self.ticrate) < -self.aim_maxpitch)
422 self.tur_head.avelocity_x = 0;
423 self.tur_head.angles_x = -self.aim_maxpitch;
425 self.SendFlags |= TNSF_ANG;
430 if (self.track_flags & TFL_TRACK_ROT)
432 self.tur_head.avelocity_y = move_angle.y;
434 if((self.tur_head.angles.y + self.tur_head.avelocity.y * self.ticrate) > self.aim_maxrot)
436 self.tur_head.avelocity_y = 0;
437 self.tur_head.angles_y = self.aim_maxrot;
439 self.SendFlags |= TNSF_ANG;
442 if((self.tur_head.angles.y + self.tur_head.avelocity.y * self.ticrate) < -self.aim_maxrot)
444 self.tur_head.avelocity_y = 0;
445 self.tur_head.angles_y = -self.aim_maxrot;
447 self.SendFlags |= TNSF_ANG;
451 self.SendFlags |= TNSF_AVEL;
453 // Force a angle update every 10'th frame
454 self.turret_framecounter += 1;
455 if(self.turret_framecounter >= 10)
457 self.SendFlags |= TNSF_ANG;
458 self.turret_framecounter = 0;
468 + TFL_FIRECHECK_WORLD
470 + TFL_FIRECHECK_DISTANCES
472 + TFL_FIRECHECK_AIMDIST
473 + TFL_FIRECHECK_REALDIST
474 - TFL_FIRECHECK_ANGLEDIST
475 - TFL_FIRECHECK_TEAMCECK
477 + TFL_FIRECHECK_OWM_AMMO
478 + TFL_FIRECHECK_OTHER_AMMO
479 + TFL_FIRECHECK_REFIRE
483 ** Preforms pre-fire checks based on the uints firecheck_flags
485 float turret_stdproc_firecheck()
487 // This one just dont care =)
488 if (self.firecheck_flags & TFL_FIRECHECK_NO)
491 if (self.enemy == world)
495 if (self.firecheck_flags & TFL_FIRECHECK_REFIRE)
496 if (self.attack_finished_single > time) return 0;
498 // Special case: volly fire turret that has to fire a full volly if a shot was fired.
499 if (self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
500 if (self.volly_counter != self.shot_volly)
501 if(self.ammo >= self.shot_dmg)
504 // Lack of zombies makes shooting dead things unnecessary :P
505 if (self.firecheck_flags & TFL_FIRECHECK_DEAD)
506 if (self.enemy.deadflag != DEAD_NO)
510 if (self.firecheck_flags & TFL_FIRECHECK_OWM_AMMO)
511 if (self.ammo < self.shot_dmg)
514 // Other's ammo? (support-supply units)
515 if (self.firecheck_flags & TFL_FIRECHECK_OTHER_AMMO)
516 if (self.enemy.ammo >= self.enemy.ammo_max)
519 // Target of opertunity?
520 if(turret_validate_target(self, self.tur_impactent, self.target_validate_flags) > 0)
522 self.enemy = self.tur_impactent;
526 if (self.firecheck_flags & TFL_FIRECHECK_DISTANCES)
529 if (self.tur_dist_aimpos < self.target_range_min)
530 if(turret_validate_target(self, self.tur_impactent, self.target_validate_flags) > 0)
531 return 1; // Target of opertunity?
537 if (self.firecheck_flags & TFL_FIRECHECK_AFF)
538 if (self.tur_impactent.team == self.team)
541 // aim<->predicted impact
542 if (self.firecheck_flags & TFL_FIRECHECK_AIMDIST)
543 if (self.tur_dist_impact_to_aimpos > self.aim_firetolerance_dist)
547 if (self.shot_volly > 1)
548 if (self.volly_counter == self.shot_volly)
549 if (self.ammo < (self.shot_dmg * self.shot_volly))
552 /*if(self.firecheck_flags & TFL_FIRECHECK_VERIFIED)
553 if(self.tur_impactent != self.enemy)
560 + TFL_TARGETSELECT_NO
561 + TFL_TARGETSELECT_LOS
562 + TFL_TARGETSELECT_PLAYERS
563 + TFL_TARGETSELECT_MISSILES
564 - TFL_TARGETSELECT_TRIGGERTARGET
565 + TFL_TARGETSELECT_ANGLELIMITS
566 + TFL_TARGETSELECT_RANGELIMTS
567 + TFL_TARGETSELECT_TEAMCHECK
568 - TFL_TARGETSELECT_NOBUILTIN
569 + TFL_TARGETSELECT_OWNTEAM
573 ** Evaluate a entity for target valitity based on validate_flags
574 ** NOTE: the caller must check takedamage before calling this, to inline this check.
576 float turret_validate_target(entity e_turret, entity e_target, float validate_flags)
580 //if(!validate_flags & TFL_TARGETSELECT_NOBUILTIN)
583 if(e_target.owner == e_turret)
586 if (!checkpvs(e_target.origin, e_turret))
593 if (substring(e_target.classname, 0, 10) == "onslaught_") // don't attack onslaught targets, that's the player's job!
596 if (validate_flags & TFL_TARGETSELECT_NO)
599 // If only this was used more..
600 if (e_target.flags & FL_NOTARGET)
604 if(e_target.vehicle_flags & VHF_ISVEHICLE)
606 if (e_target.vehicle_health <= 0)
609 else if (e_target.health <= 0)
613 if (IS_CLIENT(e_target))
615 if (!(validate_flags & TFL_TARGETSELECT_PLAYERS))
618 if (e_target.deadflag != DEAD_NO)
623 if (validate_flags & TFL_TARGETSELECT_NOTURRETS)
624 if (e_target.turret_firefunc || e_target.owner.tur_head == e_target)
625 if(e_target.team != e_turret.team) // Dont break support units.
629 if (e_target.flags & FL_PROJECTILE)
630 if (!(validate_flags & TFL_TARGETSELECT_MISSILES))
633 if (validate_flags & TFL_TARGETSELECT_MISSILESONLY)
634 if (!(e_target.flags & FL_PROJECTILE))
638 if (validate_flags & TFL_TARGETSELECT_TEAMCHECK)
640 if (validate_flags & TFL_TARGETSELECT_OWNTEAM)
642 if (e_target.team != e_turret.team)
645 if (e_turret.team != e_target.owner.team)
650 if (e_target.team == e_turret.team)
653 if (e_turret.team == e_target.owner.team)
659 tvt_dist = vlen(e_turret.origin - real_origin(e_target));
660 if (validate_flags & TFL_TARGETSELECT_RANGELIMTS)
662 if (tvt_dist < e_turret.target_range_min)
665 if (tvt_dist > e_turret.target_range)
669 // Can we even aim this thing?
670 tvt_thadv = angleofs3(e_turret.tur_head.origin, e_turret.angles + e_turret.tur_head.angles, e_target);
671 tvt_tadv = shortangle_vxy(angleofs(e_turret, e_target), e_turret.angles);
672 tvt_thadf = vlen(tvt_thadv);
673 tvt_tadf = vlen(tvt_tadv);
676 if(validate_flags & TFL_TARGETSELECT_FOV)
678 if(e_turret.target_select_fov < tvt_thadf)
683 if (validate_flags & TFL_TARGETSELECT_ANGLELIMITS)
685 if (fabs(tvt_tadv.x) > e_turret.aim_maxpitch)
688 if (fabs(tvt_tadv.y) > e_turret.aim_maxrot)
693 if (validate_flags & TFL_TARGETSELECT_LOS)
695 v_tmp = real_origin(e_target) + ((e_target.mins + e_target.maxs) * 0.5);
697 traceline(e_turret.origin + '0 0 16', v_tmp, 0, e_turret);
699 if (e_turret.aim_firetolerance_dist < vlen(v_tmp - trace_endpos))
703 if (e_target.classname == "grapplinghook")
707 if (e_target.classname == "func_button")
711 #ifdef TURRET_DEBUG_TARGETSELECT
712 dprint("Target:",e_target.netname," is a valid target for ",e_turret.netname,"\n");
718 entity turret_select_target()
720 entity e; // target looper entity
721 float score; // target looper entity score
722 entity e_enemy; // currently best scoreing target
723 float m_score; // currently best scoreing target's score
726 if(self.enemy && self.enemy.takedamage && turret_validate_target(self,self.enemy,self.target_validate_flags) > 0)
728 e_enemy = self.enemy;
729 m_score = self.turret_score_target(self,e_enemy) * self.target_select_samebias;
732 e_enemy = self.enemy = world;
734 e = findradius(self.origin, self.target_range);
736 // Nothing to aim at?
744 float f = turret_validate_target(self, e, self.target_select_flags);
745 //dprint("F is: ", ftos(f), "\n");
748 score = self.turret_score_target(self,e);
749 if ((score > m_score) && (score > 0))
766 self.nextthink = time + self.ticrate;
768 // ONS uses somewhat backwards linking.
774 e = find(world, targetname,self.target);
779 if (self.team != self.tur_head.team)
780 turret_stdproc_respawn();
784 if (self.tur_dbg_tmr1 < time)
786 if (self.enemy) paint_target (self.enemy,128,self.tur_dbg_rvec,0.9);
787 paint_target(self,256,self.tur_dbg_rvec,0.9);
788 self.tur_dbg_tmr1 = time + 1;
793 if (!(self.spawnflags & TSF_NO_AMMO_REGEN))
794 if (self.ammo < self.ammo_max)
795 self.ammo = min(self.ammo + self.ammo_recharge, self.ammo_max);
797 // Inactive turrets needs to run the think loop,
798 // So they can handle animation and wake up if need be.
801 turret_stdproc_track();
805 // This is typicaly used for zaping every target in range
806 // turret_fusionreactor uses this to recharge friendlys.
807 if (self.shoot_flags & TFL_SHOOT_HITALLVALID)
809 // Do a self.turret_fire for every valid target.
810 e = findradius(self.origin,self.target_range);
815 if (turret_validate_target(self,e,self.target_validate_flags))
819 turret_do_updates(self);
821 if (self.turret_firecheckfunc())
830 else if(self.shoot_flags & TFL_SHOOT_CUSTOM)
832 // This one is doing something.. oddball. assume its handles what needs to be handled.
835 if (!(self.aim_flags & TFL_AIM_NO))
836 self.tur_aimpos = turret_stdproc_aim_generic();
839 if (!(self.track_flags & TFL_TRACK_NO))
840 turret_stdproc_track();
842 turret_do_updates(self);
845 if (self.turret_firecheckfunc())
850 // Special case for volly always. if it fired once it must compleate the volly.
851 if(self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
852 if(self.volly_counter != self.shot_volly)
854 // Predict or whatnot
855 if (!(self.aim_flags & TFL_AIM_NO))
856 self.tur_aimpos = turret_stdproc_aim_generic();
859 if (!(self.track_flags & TFL_TRACK_NO))
860 turret_stdproc_track();
862 turret_do_updates(self);
865 if (self.turret_firecheckfunc() != 0)
868 if(self.turret_postthink)
869 self.turret_postthink();
874 // Check if we have a vailid enemy, and try to find one if we dont.
876 // g_turrets_targetscan_maxdelay forces a target re-scan at least this often
877 float do_target_scan = 0;
878 if((self.target_select_time + autocvar_g_turrets_targetscan_maxdelay) < time)
881 // Old target (if any) invalid?
882 if(self.target_validate_time < time)
883 if (turret_validate_target(self, self.enemy, self.target_validate_flags) <= 0)
886 self.target_validate_time = time + 0.5;
890 // But never more often then g_turrets_targetscan_mindelay!
891 if (self.target_select_time + autocvar_g_turrets_targetscan_mindelay > time)
896 self.enemy = turret_select_target();
897 self.target_select_time = time;
900 // No target, just go to idle, do any custom stuff and bail.
901 if (self.enemy == world)
904 if (!(self.track_flags & TFL_TRACK_NO))
905 turret_stdproc_track();
907 // do any per-turret stuff
908 if(self.turret_postthink)
909 self.turret_postthink();
915 self.lip = time + autocvar_g_turrets_aimidle_delay; // Keep track of the last time we had a target.
918 if (!(self.aim_flags & TFL_AIM_NO))
919 self.tur_aimpos = turret_stdproc_aim_generic();
922 if (!(self.track_flags & TFL_TRACK_NO))
923 turret_stdproc_track();
925 turret_do_updates(self);
928 if (self.turret_firecheckfunc())
932 // do any custom per-turret stuff
933 if(self.turret_postthink)
934 self.turret_postthink();
939 if (autocvar_g_turrets_nofire)
942 self.turret_firefunc();
944 self.attack_finished_single = time + self.shot_refire;
945 self.ammo -= self.shot_dmg;
946 self.volly_counter = self.volly_counter - 1;
948 if (self.volly_counter <= 0)
950 self.volly_counter = self.shot_volly;
952 if (self.shoot_flags & TFL_SHOOT_CLEARTARGET)
955 if (self.shot_volly > 1)
956 self.attack_finished_single = time + self.shot_volly_refire;
960 if (self.enemy) paint_target3(self.tur_aimpos, 64, self.tur_dbg_rvec, self.tur_impacttime + 0.25);
964 void turret_stdproc_fire()
966 dprint("^1Bang, ^3your dead^7 ",self.enemy.netname,"! ^1(turret with no real firefunc)\n");
970 When .used a turret switch team to activator.team.
971 If activator is world, the turret go inactive.
973 void turret_stdproc_use()
975 dprint("Turret ",self.netname, " used by ", activator.classname, "\n");
977 self.team = activator.team;
980 self.active = ACTIVE_NOT;
982 self.active = ACTIVE_ACTIVE;
988 Net_LinkEntity(self, true, 0, turret_send);
989 self.think = turret_think;
990 self.nextthink = time;
991 self.tur_head.effects = EF_NODRAW;
994 void turrets_manager_think()
996 self.nextthink = time + 1;
999 if (autocvar_g_turrets_reloadcvars)
1004 if (e.turrcaps_flags & TFL_TURRCAPS_ISTURRET)
1006 load_unit_settings(e,e.cvar_basename,1);
1007 if(e.turret_postthink)
1008 e.turret_postthink();
1013 cvar_set("g_turrets_reloadcvars","0");
1018 * Standard turret initialization. use this!
1019 * (unless you have a very good reason not to)
1020 * if the return value is 0, the turret should be removed.
1022 float turret_stdproc_init (string cvar_base_name, string base, string head, float _turret_type)
1024 entity e, ee = world;
1026 // Are turrets allowed?
1027 if (autocvar_g_turrets == 0)
1030 if(_turret_type < 1 || _turret_type > TID_LAST)
1032 dprint("Invalid / Unkown turret type\"", ftos(_turret_type), "\", aborting!\n");
1035 self.turret_type = _turret_type;
1037 e = find(world, classname, "turret_manager");
1041 e.classname = "turret_manager";
1042 e.think = turrets_manager_think;
1043 e.nextthink = time + 2;
1046 if (!(self.spawnflags & TSF_SUSPENDED))
1047 builtin_droptofloor(); // why can't we use regular droptofloor here?
1049 self.cvar_basename = cvar_base_name;
1050 load_unit_settings(self, self.cvar_basename, 0);
1052 self.effects = EF_NODRAW;
1054 // Handle turret teams.
1056 self.team = MAX_SHOT_DISTANCE; // Group all turrets into the same team, so they dont kill eachother.
1057 else if(g_onslaught && self.targetname)
1059 e = find(world,target,self.targetname);
1067 self.team = MAX_SHOT_DISTANCE; // Group all turrets into the same team, so they dont kill eachother.
1070 * Try to guess some reasonaly defaults
1071 * for missing params and do sanety checks
1072 * thise checks could produce some "interesting" results
1073 * if it hits a glitch in my logic :P so try to set as mutch
1074 * as possible beforehand.
1078 if (self.turrcaps_flags & TFL_TURRCAPS_SUPPORT)
1079 self.ticrate = 0.2; // Support units generaly dont need to have a high speed ai-loop
1081 self.ticrate = 0.1; // 10 fps for normal turrets
1084 self.ticrate = bound(sys_frametime, self.ticrate, 60); // keep it sane
1087 if (self.netname == "")
1088 self.netname = self.classname;
1090 if (!self.respawntime)
1091 self.respawntime = 60;
1092 self.respawntime = max(-1, self.respawntime);
1096 self.tur_health = max(1, self.health);
1097 self.bot_attack = true;
1098 self.monster_attack = true;
1100 if (!self.turrcaps_flags)
1101 self.turrcaps_flags = TFL_TURRCAPS_RADIUSDMG | TFL_TURRCAPS_MEDPROJ | TFL_TURRCAPS_PLAYERKILL;
1103 if (!self.damage_flags)
1104 self.damage_flags = TFL_DMG_YES | TFL_DMG_RETALIATE | TFL_DMG_AIMSHAKE;
1107 if (!self.shot_refire)
1108 self.shot_refire = 1;
1109 self.shot_refire = bound(0.01, self.shot_refire, 9999);
1112 self.shot_dmg = self.shot_refire * 50;
1113 self.shot_dmg = max(1, self.shot_dmg);
1115 if (!self.shot_radius)
1116 self.shot_radius = self.shot_dmg * 0.5;
1117 self.shot_radius = max(1, self.shot_radius);
1119 if (!self.shot_speed)
1120 self.shot_speed = 2500;
1121 self.shot_speed = max(1, self.shot_speed);
1123 if (!self.shot_spread)
1124 self.shot_spread = 0.0125;
1125 self.shot_spread = bound(0.0001, self.shot_spread, 500);
1127 if (!self.shot_force)
1128 self.shot_force = self.shot_dmg * 0.5 + self.shot_radius * 0.5;
1129 self.shot_force = bound(0.001, self.shot_force, 5000);
1131 if (!self.shot_volly)
1132 self.shot_volly = 1;
1133 self.shot_volly = bound(1, self.shot_volly, floor(self.ammo_max / self.shot_dmg));
1135 if (!self.shot_volly_refire)
1136 self.shot_volly_refire = self.shot_refire * self.shot_volly;
1137 self.shot_volly_refire = bound(self.shot_refire, self.shot_volly_refire, 60);
1139 if (!self.firecheck_flags)
1140 self.firecheck_flags = TFL_FIRECHECK_DEAD | TFL_FIRECHECK_DISTANCES |
1141 TFL_FIRECHECK_LOS | TFL_FIRECHECK_AIMDIST | TFL_FIRECHECK_TEAMCECK |
1142 TFL_FIRECHECK_OWM_AMMO | TFL_FIRECHECK_REFIRE;
1145 if (!self.target_range)
1146 self.target_range = self.shot_speed * 0.5;
1147 self.target_range = bound(0, self.target_range, MAX_SHOT_DISTANCE);
1149 if (!self.target_range_min)
1150 self.target_range_min = self.shot_radius * 2;
1151 self.target_range_min = bound(0, self.target_range_min, MAX_SHOT_DISTANCE);
1153 if (!self.target_range_optimal)
1154 self.target_range_optimal = self.target_range * 0.5;
1155 self.target_range_optimal = bound(0, self.target_range_optimal, MAX_SHOT_DISTANCE);
1159 if (!self.aim_maxrot)
1160 self.aim_maxrot = 90;
1161 self.aim_maxrot = bound(0, self.aim_maxrot, 360);
1163 if (!self.aim_maxpitch)
1164 self.aim_maxpitch = 20;
1165 self.aim_maxpitch = bound(0, self.aim_maxpitch, 90);
1167 if (!self.aim_speed)
1168 self.aim_speed = 36;
1169 self.aim_speed = bound(0.1, self.aim_speed, 1000);
1171 if (!self.aim_firetolerance_dist)
1172 self.aim_firetolerance_dist = 5 + (self.shot_radius * 2);
1173 self.aim_firetolerance_dist = bound(0.1, self.aim_firetolerance_dist, MAX_SHOT_DISTANCE);
1175 if (!self.aim_flags)
1177 self.aim_flags = TFL_AIM_LEAD | TFL_AIM_SHOTTIMECOMPENSATE;
1178 if(self.turrcaps_flags & TFL_TURRCAPS_RADIUSDMG)
1179 self.aim_flags |= TFL_AIM_GROUNDGROUND;
1182 if (!self.track_type)
1183 self.track_type = TFL_TRACKTYPE_STEPMOTOR;
1185 if (self.track_type != TFL_TRACKTYPE_STEPMOTOR)
1187 // Fluid / Ineria mode. Looks mutch nicer.
1188 // Can reduce aim preformance alot, needs a bit diffrent aimspeed
1190 if (!self.aim_speed)
1191 self.aim_speed = 180;
1192 self.aim_speed = bound(0.1, self.aim_speed, 1000);
1194 if (!self.track_accel_pitch)
1195 self.track_accel_pitch = 0.5;
1197 if (!self.track_accel_rot)
1198 self.track_accel_rot = 0.5;
1200 if (!self.track_blendrate)
1201 self.track_blendrate = 0.35;
1204 if (!self.track_flags)
1205 self.track_flags = TFL_TRACK_PITCH | TFL_TRACK_ROT;
1208 // Target selection stuff.
1209 if (!self.target_select_rangebias)
1210 self.target_select_rangebias = 1;
1211 self.target_select_rangebias = bound(-10, self.target_select_rangebias, 10);
1213 if (!self.target_select_samebias)
1214 self.target_select_samebias = 1;
1215 self.target_select_samebias = bound(-10, self.target_select_samebias, 10);
1217 if (!self.target_select_anglebias)
1218 self.target_select_anglebias = 1;
1219 self.target_select_anglebias = bound(-10, self.target_select_anglebias, 10);
1221 if (!self.target_select_missilebias)
1222 self.target_select_missilebias = -10;
1224 self.target_select_missilebias = bound(-10, self.target_select_missilebias, 10);
1225 self.target_select_playerbias = bound(-10, self.target_select_playerbias, 10);
1227 if (!self.target_select_flags)
1229 self.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_TEAMCHECK
1230 | TFL_TARGETSELECT_RANGELIMTS | TFL_TARGETSELECT_ANGLELIMITS;
1232 if (self.turrcaps_flags & TFL_TURRCAPS_MISSILEKILL)
1233 self.target_select_flags |= TFL_TARGETSELECT_MISSILES;
1235 if (self.turrcaps_flags & TFL_TURRCAPS_PLAYERKILL)
1236 self.target_select_flags |= TFL_TARGETSELECT_PLAYERS;
1238 // self.target_select_flags = TFL_TARGETSELECT_NO;
1241 self.target_validate_flags = self.target_select_flags;
1245 self.ammo_max = self.shot_dmg * 10;
1246 self.ammo_max = max(self.shot_dmg, self.ammo_max);
1249 self.ammo = self.shot_dmg * 5;
1250 self.ammo = bound(0,self.ammo, self.ammo_max);
1252 if (!self.ammo_recharge)
1253 self.ammo_recharge = self.shot_dmg * 0.5;
1254 self.ammo_recharge = max(0 ,self.ammo_recharge);
1256 // Convert the recharge from X per sec to X per ticrate
1257 self.ammo_recharge = self.ammo_recharge * self.ticrate;
1259 if (!self.ammo_flags)
1260 self.ammo_flags = TFL_AMMO_ENERGY | TFL_AMMO_RECHARGE;
1263 if(self.spawnflags & TSL_NO_RESPAWN)
1264 if (!(self.damage_flags & TFL_DMG_DEATH_NORESPAWN))
1265 self.damage_flags |= TFL_DMG_DEATH_NORESPAWN;
1267 // Offsets & origins
1268 if (!self.tur_shotorg) self.tur_shotorg = '50 0 50';
1274 if(MUTATOR_CALLHOOK(TurretSpawn))
1277 // End of default & sanety checks, start building the turret.
1280 self.tur_head = spawn();
1281 self.tur_head.netname = self.tur_head.classname = "turret_head";
1282 self.tur_head.team = self.team;
1283 self.tur_head.owner = self;
1285 setmodel(self, base);
1286 setmodel(self.tur_head, head);
1288 setsize(self, '-32 -32 0', '32 32 64');
1289 setsize(self.tur_head, '0 0 0', '0 0 0');
1291 setorigin(self.tur_head, '0 0 0');
1292 setattachment(self.tur_head, self, "tag_head");
1294 self.tur_health = self.health;
1295 self.solid = SOLID_BBOX;
1296 self.tur_head.solid = SOLID_NOT;
1297 self.takedamage = DAMAGE_AIM;
1298 self.tur_head.takedamage = DAMAGE_NO;
1299 self.movetype = MOVETYPE_NOCLIP;
1300 self.tur_head.movetype = MOVETYPE_NOCLIP;
1303 if (!self.tur_defend)
1304 if (self.target != "")
1306 self.tur_defend = find(world, targetname, self.target);
1307 if (self.tur_defend == world)
1310 dprint("Turret has invalid defendpoint!\n");
1314 // In target defend mode, aim on the spot to defend when idle.
1315 if (self.tur_defend)
1316 self.idle_aim = self.tur_head.angles + angleofs(self.tur_head, self.tur_defend);
1318 self.idle_aim = '0 0 0';
1320 // Attach stdprocs. override when and what needed
1321 self.turret_firecheckfunc = turret_stdproc_firecheck;
1322 self.turret_firefunc = turret_stdproc_fire;
1323 self.event_damage = turret_stdproc_damage;
1325 if (self.turrcaps_flags & TFL_TURRCAPS_SUPPORT)
1326 self.turret_score_target = turret_stdproc_targetscore_support;
1328 self.turret_score_target = turret_stdproc_targetscore_generic;
1330 self.use = turret_stdproc_use;
1333 self.nextthink = time + 1;
1334 self.nextthink += turret_count * sys_frametime;
1336 self.tur_head.team = self.team;
1337 self.view_ofs = '0 0 0';
1340 self.tur_dbg_start = self.nextthink;
1341 while (vlen(self.tur_dbg_rvec) < 2)
1342 self.tur_dbg_rvec = randomvec() * 4;
1344 self.tur_dbg_rvec_x = fabs(self.tur_dbg_rvec.x);
1345 self.tur_dbg_rvec_y = fabs(self.tur_dbg_rvec.y);
1346 self.tur_dbg_rvec_z = fabs(self.tur_dbg_rvec.z);
1350 self.turrcaps_flags |= TFL_TURRCAPS_ISTURRET;
1352 self.classname = "turret_main";
1354 self.active = ACTIVE_ACTIVE;
1356 // In ONS mode, and linked to a ONS ent. need to call the use to set team.
1357 if (g_onslaught && ee)
1364 turret_stdproc_respawn();
1365 turret_tag_fire_update();