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);
71 WriteByte(MSG_ENTITY, 0);
73 WriteByte(MSG_ENTITY, ceil((self.health / self.tur_health) * 255));
79 void load_unit_settings(entity ent, string unitname, float is_reload)
86 if not (ent.turret_scale_damage) ent.turret_scale_damage = 1;
87 if not (ent.turret_scale_range) ent.turret_scale_range = 1;
88 if not (ent.turret_scale_refire) ent.turret_scale_refire = 1;
89 if not (ent.turret_scale_ammo) ent.turret_scale_ammo = 1;
90 if not (ent.turret_scale_aim) ent.turret_scale_aim = 1;
91 if not (ent.turret_scale_health) ent.turret_scale_health = 1;
92 if not (ent.turret_scale_respawn) ent.turret_scale_respawn = 1;
94 sbase = strcat(cvar_base,unitname);
98 ent.tur_head.avelocity = '0 0 0';
100 ent.tur_head.angles = '0 0 0';
103 ent.health = cvar(strcat(sbase,"_health")) * ent.turret_scale_health;
104 ent.respawntime = cvar(strcat(sbase,"_respawntime")) * ent.turret_scale_respawn;
106 ent.shot_dmg = cvar(strcat(sbase,"_shot_dmg")) * ent.turret_scale_damage;
107 ent.shot_refire = cvar(strcat(sbase,"_shot_refire")) * ent.turret_scale_refire;
108 ent.shot_radius = cvar(strcat(sbase,"_shot_radius")) * ent.turret_scale_damage;
109 ent.shot_speed = cvar(strcat(sbase,"_shot_speed"));
110 ent.shot_spread = cvar(strcat(sbase,"_shot_spread"));
111 ent.shot_force = cvar(strcat(sbase,"_shot_force")) * ent.turret_scale_damage;
112 ent.shot_volly = cvar(strcat(sbase,"_shot_volly"));
113 ent.shot_volly_refire = cvar(strcat(sbase,"_shot_volly_refire")) * ent.turret_scale_refire;
115 ent.target_range = cvar(strcat(sbase,"_target_range")) * ent.turret_scale_range;
116 ent.target_range_min = cvar(strcat(sbase,"_target_range_min")) * ent.turret_scale_range;
117 ent.target_range_optimal = cvar(strcat(sbase,"_target_range_optimal")) * ent.turret_scale_range;
118 //ent.target_range_fire = cvar(strcat(sbase,"_target_range_fire")) * ent.turret_scale_range;
120 ent.target_select_rangebias = cvar(strcat(sbase,"_target_select_rangebias"));
121 ent.target_select_samebias = cvar(strcat(sbase,"_target_select_samebias"));
122 ent.target_select_anglebias = cvar(strcat(sbase,"_target_select_anglebias"));
123 ent.target_select_playerbias = cvar(strcat(sbase,"_target_select_playerbias"));
124 //ent.target_select_fov = cvar(cvar_gets(sbase,"_target_select_fov"));
126 ent.ammo_max = cvar(strcat(sbase,"_ammo_max")) * ent.turret_scale_ammo;
127 ent.ammo_recharge = cvar(strcat(sbase,"_ammo_recharge")) * ent.turret_scale_ammo;
129 ent.aim_firetolerance_dist = cvar(strcat(sbase,"_aim_firetolerance_dist"));
130 ent.aim_speed = cvar(strcat(sbase,"_aim_speed")) * ent.turret_scale_aim;
131 ent.aim_maxrot = cvar(strcat(sbase,"_aim_maxrot"));
132 ent.aim_maxpitch = cvar(strcat(sbase,"_aim_maxpitch"));
134 ent.track_type = cvar(strcat(sbase,"_track_type"));
135 ent.track_accel_pitch = cvar(strcat(sbase,"_track_accel_pitch"));
136 ent.track_accel_rot = cvar(strcat(sbase,"_track_accel_rot"));
137 ent.track_blendrate = cvar(strcat(sbase,"_track_blendrate"));
140 if(ent.turret_respawnhook)
141 ent.turret_respawnhook();
144 void turret_projectile_explode()
146 self.takedamage = DAMAGE_NO;
147 self.event_damage = SUB_Null;
151 d = RadiusDamage (self, self.owner, self.owner.shot_dmg, 0, self.owner.shot_radius, self, self.owner.shot_force, self.totalfrags, world);
152 self.owner.tur_dbg_dmg_t_h = self.owner.tur_dbg_dmg_t_h + d;
153 self.owner.tur_dbg_dmg_t_f = self.owner.tur_dbg_dmg_t_f + self.owner.shot_dmg;
155 RadiusDamage (self, self.realowner, self.owner.shot_dmg, 0, self.owner.shot_radius, self, self.owner.shot_force, self.totalfrags, world);
160 void turret_projectile_touch()
163 turret_projectile_explode();
166 void turret_projectile_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector vforce)
168 self.velocity += vforce;
169 self.health -= damage;
170 //self.realowner = attacker; // Dont change realowner, it does not make much sense for turrets
172 turret_projectile_explode();
175 entity turret_projectile(string _snd, float _size, float _health, float _death, float _proj_type, float _cull, float _cli_anim)
179 sound (self, CH_WEAPON_A, _snd, VOL_BASE, ATTN_NORM);
181 setorigin(proj, self.tur_shotorg);
182 setsize(proj, '-0.5 -0.5 -0.5' * _size, '0.5 0.5 0.5' * _size);
184 proj.realowner = self;
185 proj.bot_dodge = TRUE;
186 proj.bot_dodgerating = self.shot_dmg;
187 proj.think = turret_projectile_explode;
188 proj.touch = turret_projectile_touch;
189 proj.nextthink = time + 9;
190 proj.movetype = MOVETYPE_FLYMISSILE;
191 proj.velocity = normalize(self.tur_shotdir_updated + randomvec() * self.shot_spread) * self.shot_speed;
192 proj.flags = FL_PROJECTILE;
193 proj.enemy = self.enemy;
194 proj.totalfrags = _death;
195 PROJECTILE_MAKETRIGGER(proj);
198 proj.health = _health;
199 proj.takedamage = DAMAGE_YES;
200 proj.event_damage = turret_projectile_damage;
203 proj.flags |= FL_NOTARGET;
205 CSQCProjectile(proj, _cli_anim, _proj_type, _cull);
211 ** updates enemy distances, predicted impact point/time
212 ** and updated aim<->predict impact distance.
214 void turret_do_updates(entity t_turret)
216 vector enemy_pos, oldpos;
222 enemy_pos = real_origin(self.enemy);
224 turret_tag_fire_update();
226 self.tur_shotdir_updated = v_forward;
227 self.tur_dist_enemy = vlen(self.tur_shotorg - enemy_pos);
228 self.tur_dist_aimpos = vlen(self.tur_shotorg - self.tur_aimpos);
230 if((self.firecheck_flags & TFL_FIRECHECK_VERIFIED) && (self.enemy))
232 oldpos = self.enemy.origin;
233 setorigin(self.enemy,self.tur_aimpos);
234 tracebox(self.tur_shotorg, '-1 -1 -1','1 1 1',self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos),MOVE_NORMAL,self);
235 setorigin(self.enemy,oldpos);
237 if(trace_ent == self.enemy)
238 self.tur_dist_impact_to_aimpos = 0;
240 self.tur_dist_impact_to_aimpos = vlen(trace_endpos - self.tur_aimpos);
243 tracebox(self.tur_shotorg, '-1 -1 -1','1 1 1', self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos),MOVE_NORMAL,self);
245 self.tur_dist_impact_to_aimpos = vlen(trace_endpos - self.tur_aimpos) - (vlen(self.enemy.maxs - self.enemy.mins) * 0.5);
246 self.tur_impactent = trace_ent;
247 self.tur_impacttime = vlen(self.tur_shotorg - trace_endpos) / self.shot_speed;
253 vector turret_fovsearch_pingpong()
256 if(self.phase < time)
258 if( self.tur_head.phase )
259 self.tur_head.phase = 0;
261 self.tur_head.phase = 1;
262 self.phase = time + 5;
265 if( self.tur_head.phase)
266 wish_angle = self.idle_aim + '0 1 0' * (self.aim_maxrot * (self.target_select_fov / 360));
268 wish_angle = self.idle_aim - '0 1 0' * (self.aim_maxrot * (self.target_select_fov / 360));
273 vector turret_fovsearch_steprot()
278 wish_angle = self.tur_head.angles;
279 wish_angle_x = self.idle_aim_x;
281 if (self.phase < time)
283 //rot_add = self.aim_maxrot / self.target_select_fov;
284 wish_angle_y += (self.target_select_fov * 2);
286 if(wish_angle_y > 360)
287 wish_angle_y = wish_angle_y - 360;
289 self.phase = time + 1.5;
295 vector turret_fovsearch_random()
299 if (self.phase < time)
301 wish_angle_y = random() * self.aim_maxrot;
305 wish_angle_x = random() * self.aim_maxpitch;
309 self.phase = time + 5;
311 self.tur_aimpos = wish_angle;
314 return self.idle_aim + self.tur_aimpos;
319 ** Handles head rotation according to
320 ** the units .track_type and .track_flags
322 .float turret_framecounter;
323 void turret_stdproc_track()
325 vector target_angle; // This is where we want to aim
326 vector move_angle; // This is where we can aim
329 v1 = self.tur_head.angles;
330 v2 = self.tur_head.avelocity;
332 if (self.track_flags == TFL_TRACK_NO)
335 if not (self.tur_active)
336 target_angle = self.idle_aim - ('1 0 0' * self.aim_maxpitch);
337 else if (self.enemy == world)
340 target_angle = self.idle_aim + self.angles;
342 target_angle = vectoangles(normalize(self.tur_aimpos - self.tur_shotorg));
346 target_angle = vectoangles(normalize(self.tur_aimpos - self.tur_shotorg));
349 self.tur_head.angles_x = anglemods(self.tur_head.angles_x);
350 self.tur_head.angles_y = anglemods(self.tur_head.angles_y);
352 // Find the diffrence between where we currently aim and where we want to aim
353 //move_angle = target_angle - (self.angles + self.tur_head.angles);
354 //move_angle = shortangle_vxy(move_angle,(self.angles + self.tur_head.angles));
356 move_angle = AnglesTransform_ToAngles(AnglesTransform_LeftDivide(AnglesTransform_FromAngles(self.angles), AnglesTransform_FromAngles(target_angle))) - self.tur_head.angles;
357 move_angle = shortangle_vxy(move_angle, self.tur_head.angles);
359 switch(self.track_type)
361 case TFL_TRACKTYPE_STEPMOTOR:
362 f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
363 if (self.track_flags & TFL_TRACK_PITCH)
365 self.tur_head.angles_x += bound(-f_tmp,move_angle_x, f_tmp);
366 if(self.tur_head.angles_x > self.aim_maxpitch)
367 self.tur_head.angles_x = self.aim_maxpitch;
369 if(self.tur_head.angles_x < -self.aim_maxpitch)
370 self.tur_head.angles_x = self.aim_maxpitch;
373 if (self.track_flags & TFL_TRACK_ROT)
375 self.tur_head.angles_y += bound(-f_tmp, move_angle_y, f_tmp);
376 if(self.tur_head.angles_y > self.aim_maxrot)
377 self.tur_head.angles_y = self.aim_maxrot;
379 if(self.tur_head.angles_y < -self.aim_maxrot)
380 self.tur_head.angles_y = self.aim_maxrot;
384 self.SendFlags = TNSF_ANG;
388 case TFL_TRACKTYPE_FLUIDINERTIA:
389 f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
390 move_angle_x = bound(-self.aim_speed, move_angle_x * self.track_accel_pitch * f_tmp, self.aim_speed);
391 move_angle_y = bound(-self.aim_speed, move_angle_y * self.track_accel_rot * f_tmp, self.aim_speed);
392 move_angle = (self.tur_head.avelocity * self.track_blendrate) + (move_angle * (1 - self.track_blendrate));
395 case TFL_TRACKTYPE_FLUIDPRECISE:
397 move_angle_y = bound(-self.aim_speed, move_angle_y, self.aim_speed);
398 move_angle_x = bound(-self.aim_speed, move_angle_x, self.aim_speed);
404 if (self.track_flags & TFL_TRACK_PITCH)
406 self.tur_head.avelocity_x = move_angle_x;
407 if((self.tur_head.angles_x + self.tur_head.avelocity_x * self.ticrate) > self.aim_maxpitch)
409 self.tur_head.avelocity_x = 0;
410 self.tur_head.angles_x = self.aim_maxpitch;
412 self.SendFlags |= TNSF_ANG;
415 if((self.tur_head.angles_x + self.tur_head.avelocity_x * self.ticrate) < -self.aim_maxpitch)
417 self.tur_head.avelocity_x = 0;
418 self.tur_head.angles_x = -self.aim_maxpitch;
420 self.SendFlags |= TNSF_ANG;
425 if (self.track_flags & TFL_TRACK_ROT)
427 self.tur_head.avelocity_y = move_angle_y;
429 if((self.tur_head.angles_y + self.tur_head.avelocity_y * self.ticrate) > self.aim_maxrot)
431 self.tur_head.avelocity_y = 0;
432 self.tur_head.angles_y = self.aim_maxrot;
434 self.SendFlags |= TNSF_ANG;
437 if((self.tur_head.angles_y + self.tur_head.avelocity_y * self.ticrate) < -self.aim_maxrot)
439 self.tur_head.avelocity_y = 0;
440 self.tur_head.angles_y = -self.aim_maxrot;
442 self.SendFlags |= TNSF_ANG;
446 self.SendFlags |= TNSF_AVEL;
448 // Force a angle update every 10'th frame
449 self.turret_framecounter += 1;
450 if(self.turret_framecounter >= 10)
452 self.SendFlags |= TNSF_ANG;
453 self.turret_framecounter = 0;
463 + TFL_FIRECHECK_WORLD
465 + TFL_FIRECHECK_DISTANCES
467 + TFL_FIRECHECK_AIMDIST
468 + TFL_FIRECHECK_REALDIST
469 - TFL_FIRECHECK_ANGLEDIST
470 - TFL_FIRECHECK_TEAMCECK
472 + TFL_FIRECHECK_OWM_AMMO
473 + TFL_FIRECHECK_OTHER_AMMO
474 + TFL_FIRECHECK_REFIRE
478 ** Preforms pre-fire checks based on the uints firecheck_flags
480 float turret_stdproc_firecheck()
482 // This one just dont care =)
483 if (self.firecheck_flags & TFL_FIRECHECK_NO) return 1;
486 if (self.firecheck_flags & TFL_FIRECHECK_REFIRE)
487 if (self.attack_finished_single > time) return 0;
489 // Special case: volly fire turret that has to fire a full volly if a shot was fired.
490 if (self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
491 if (self.volly_counter != self.shot_volly)
492 if(self.ammo >= self.shot_dmg)
495 // Lack of zombies makes shooting dead things unnecessary :P
496 if (self.firecheck_flags & TFL_FIRECHECK_DEAD)
497 if (self.enemy.deadflag != DEAD_NO)
500 // Plz stop killing the world!
501 if (self.firecheck_flags & TFL_FIRECHECK_WORLD)
502 if (self.enemy == world)
506 if (self.firecheck_flags & TFL_FIRECHECK_OWM_AMMO)
507 if (self.ammo < self.shot_dmg)
510 // Other's ammo? (support-supply units)
511 if (self.firecheck_flags & TFL_FIRECHECK_OTHER_AMMO)
512 if (self.enemy.ammo >= self.enemy.ammo_max)
515 // Target of opertunity?
516 if(turret_validate_target(self, self.tur_impactent, self.target_validate_flags) > 0)
518 self.enemy = self.tur_impactent;
522 if (self.firecheck_flags & TFL_FIRECHECK_DISTANCES)
525 if (self.tur_dist_aimpos < self.target_range_min)
526 if(turret_validate_target(self, self.tur_impactent, self.target_validate_flags) > 0)
527 return 1; // Target of opertunity?
533 if (self.firecheck_flags & TFL_FIRECHECK_AFF)
534 if (self.tur_impactent.team == self.team)
537 // aim<->predicted impact
538 if (self.firecheck_flags & TFL_FIRECHECK_AIMDIST)
539 if (self.tur_dist_impact_to_aimpos > self.aim_firetolerance_dist)
543 if (self.shot_volly > 1)
544 if (self.volly_counter == self.shot_volly)
545 if (self.ammo < (self.shot_dmg * self.shot_volly))
548 if(self.firecheck_flags & TFL_FIRECHECK_VERIFIED)
549 if(self.tur_impactent != self.enemy)
556 + TFL_TARGETSELECT_NO
557 + TFL_TARGETSELECT_LOS
558 + TFL_TARGETSELECT_PLAYERS
559 + TFL_TARGETSELECT_MISSILES
560 - TFL_TARGETSELECT_TRIGGERTARGET
561 + TFL_TARGETSELECT_ANGLELIMITS
562 + TFL_TARGETSELECT_RANGELIMTS
563 + TFL_TARGETSELECT_TEAMCHECK
564 - TFL_TARGETSELECT_NOBUILTIN
565 + TFL_TARGETSELECT_OWNTEAM
569 ** Evaluate a entity for target valitity based on validate_flags
570 ** NOTE: the caller must check takedamage before calling this, to inline this check.
572 float turret_validate_target(entity e_turret, entity e_target, float validate_flags)
576 //if(!validate_flags & TFL_TARGETSELECT_NOBUILTIN)
579 if(e_target.owner == e_turret)
582 if not(checkpvs(e_target.origin, e_turret))
589 if (substring(e_target.classname, 0, 10) == "onslaught_") // don't attack onslaught targets, that's the player's job!
592 if (validate_flags & TFL_TARGETSELECT_NO)
595 // If only this was used more..
596 if (e_target.flags & FL_NOTARGET)
600 if (e_target.health < 0)
604 if (e_target.flags & FL_CLIENT)
606 if not (validate_flags & TFL_TARGETSELECT_PLAYERS)
609 if (e_target.deadflag != DEAD_NO)
614 if (validate_flags & TFL_TARGETSELECT_NOTURRETS)
615 if (e_target.turret_firefunc || e_target.owner.tur_head == e_target)
616 if(e_target.team != e_turret.team) // Dont break support units.
620 if (e_target.flags & FL_PROJECTILE)
621 if not (validate_flags & TFL_TARGETSELECT_MISSILES)
624 if (validate_flags & TFL_TARGETSELECT_MISSILESONLY)
625 if not (e_target.flags & FL_PROJECTILE)
629 if (validate_flags & TFL_TARGETSELECT_TEAMCHECK)
631 if (validate_flags & TFL_TARGETSELECT_OWNTEAM)
633 if (e_target.team != e_turret.team)
636 if (e_turret.team != e_target.owner.team)
641 if (e_target.team == e_turret.team)
644 if (e_turret.team == e_target.owner.team)
650 tvt_dist = vlen(e_turret.origin - real_origin(e_target));
651 if (validate_flags & TFL_TARGETSELECT_RANGELIMTS)
653 if (tvt_dist < e_turret.target_range_min)
656 if (tvt_dist > e_turret.target_range)
660 // Can we even aim this thing?
661 tvt_thadv = angleofs3(e_turret.tur_head.origin, e_turret.angles + e_turret.tur_head.angles, e_target);
662 tvt_tadv = shortangle_vxy(angleofs(e_turret, e_target), e_turret.angles);
663 tvt_thadf = vlen(tvt_thadv);
664 tvt_tadf = vlen(tvt_tadv);
667 if(validate_flags & TFL_TARGETSELECT_FOV)
669 if(e_turret.target_select_fov < tvt_thadf)
674 if (validate_flags & TFL_TARGETSELECT_ANGLELIMITS)
676 if (fabs(tvt_tadv_x) > e_turret.aim_maxpitch)
679 if (fabs(tvt_tadv_y) > e_turret.aim_maxrot)
684 if (validate_flags & TFL_TARGETSELECT_LOS)
686 v_tmp = real_origin(e_target) + ((e_target.mins + e_target.maxs) * 0.5);
688 traceline(e_turret.tur_shotorg, v_tmp, 0, e_turret);
690 if (e_turret.aim_firetolerance_dist < vlen(v_tmp - trace_endpos))
694 if (e_target.classname == "grapplinghook")
698 if (e_target.classname == "func_button")
702 #ifdef TURRET_DEBUG_TARGETSELECT
703 dprint("Target:",e_target.netname," is a valid target for ",e_turret.netname,"\n");
709 entity turret_select_target()
711 entity e; // target looper entity
712 float score; // target looper entity score
713 entity e_enemy; // currently best scoreing target
714 float m_score; // currently best scoreing target's score
718 if(self.enemy.takedamage)
719 if(turret_validate_target(self,self.enemy,self.target_validate_flags) > 0)
721 e_enemy = self.enemy;
722 m_score = self.turret_score_target(self,e_enemy) * self.target_select_samebias;
727 e = findradius(self.origin, self.target_range);
729 // Nothing to aim at?
737 if (turret_validate_target(self, e, self.target_select_flags) > 0)
739 score = self.turret_score_target(self,e);
740 if ((score > m_score) && (score > 0))
757 self.nextthink = time + self.ticrate;
759 // ONS uses somewhat backwards linking.
765 e = find(world, targetname,self.target);
770 if (self.team != self.tur_head.team)
771 turret_stdproc_respawn();
775 if (self.tur_dbg_tmr1 < time)
777 if (self.enemy) paint_target (self.enemy,128,self.tur_dbg_rvec,0.9);
778 paint_target(self,256,self.tur_dbg_rvec,0.9);
779 self.tur_dbg_tmr1 = time + 1;
784 if not (self.spawnflags & TSF_NO_AMMO_REGEN)
785 if (self.ammo < self.ammo_max)
786 self.ammo = min(self.ammo + self.ammo_recharge, self.ammo_max);
788 // Inactive turrets needs to run the think loop,
789 // So they can handle animation and wake up if need be.
790 if not (self.tur_active)
792 turret_stdproc_track();
796 // This is typicaly used for zaping every target in range
797 // turret_fusionreactor uses this to recharge friendlys.
798 if (self.shoot_flags & TFL_SHOOT_HITALLVALID)
800 // Do a self.turret_fire for every valid target.
801 e = findradius(self.origin,self.target_range);
806 if (turret_validate_target(self,e,self.target_validate_flags))
810 turret_do_updates(self);
812 if (self.turret_firecheckfunc())
821 else if(self.shoot_flags & TFL_SHOOT_CUSTOM)
823 // This one is doing something.. oddball. assume its handles what needs to be handled.
826 if not(self.aim_flags & TFL_AIM_NO)
827 self.tur_aimpos = turret_stdproc_aim_generic();
830 if not(self.track_flags & TFL_TRACK_NO)
831 turret_stdproc_track();
833 turret_do_updates(self);
836 if (self.turret_firecheckfunc())
841 // Special case for volly always. if it fired once it must compleate the volly.
842 if(self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
843 if(self.volly_counter != self.shot_volly)
845 // Predict or whatnot
846 if not(self.aim_flags & TFL_AIM_NO)
847 self.tur_aimpos = turret_stdproc_aim_generic();
850 if not(self.track_flags & TFL_TRACK_NO)
851 turret_stdproc_track();
853 turret_do_updates(self);
856 if (self.turret_firecheckfunc() != 0)
859 if(self.turret_postthink)
860 self.turret_postthink();
865 // Check if we have a vailid enemy, and try to find one if we dont.
867 // g_turrets_targetscan_maxdelay forces a target re-scan at least this often
868 float do_target_scan;
869 if((self.target_select_time + autocvar_g_turrets_targetscan_maxdelay) < time)
872 // Old target (if any) invalid?
873 if(self.target_validate_time < time)
874 if (turret_validate_target(self, self.enemy, self.target_validate_flags) <= 0)
877 self.target_validate_time = time + 0.5;
881 // But never more often then g_turrets_targetscan_mindelay!
882 if (self.target_select_time + autocvar_g_turrets_targetscan_mindelay > time)
887 self.enemy = turret_select_target();
888 self.target_select_time = time;
891 // No target, just go to idle, do any custom stuff and bail.
892 if (self.enemy == world)
895 if not(self.track_flags & TFL_TRACK_NO)
896 turret_stdproc_track();
898 // do any per-turret stuff
899 if(self.turret_postthink)
900 self.turret_postthink();
906 self.lip = time + autocvar_g_turrets_aimidle_delay; // Keep track of the last time we had a target.
909 if not(self.aim_flags & TFL_AIM_NO)
910 self.tur_aimpos = turret_stdproc_aim_generic();
913 if not(self.track_flags & TFL_TRACK_NO)
914 turret_stdproc_track();
916 turret_do_updates(self);
919 if (self.turret_firecheckfunc())
923 // do any custom per-turret stuff
924 if(self.turret_postthink)
925 self.turret_postthink();
930 if (autocvar_g_turrets_nofire != 0)
933 self.turret_firefunc();
935 self.attack_finished_single = time + self.shot_refire;
936 self.ammo -= self.shot_dmg;
937 self.volly_counter = self.volly_counter - 1;
939 if (self.volly_counter <= 0)
941 self.volly_counter = self.shot_volly;
943 if (self.shoot_flags & TFL_SHOOT_CLEARTARGET)
946 if (self.shot_volly > 1)
947 self.attack_finished_single = time + self.shot_volly_refire;
951 if (self.enemy) paint_target3(self.tur_aimpos, 64, self.tur_dbg_rvec, self.tur_impacttime + 0.25);
955 void turret_stdproc_fire()
957 dprint("^1Bang, ^3your dead^7 ",self.enemy.netname,"! ^1(turret with no real firefunc)\n");
961 When .used a turret switch team to activator.team.
962 If activator is world, the turret go inactive.
964 void turret_stdproc_use()
966 dprint("Turret ",self.netname, " used by ", activator.classname, "\n");
968 self.team = activator.team;
979 Net_LinkEntity(self, TRUE, 0, turret_send);
980 self.think = turret_think;
981 self.nextthink = time;
982 self.tur_head.effects = EF_NODRAW;
985 void turrets_manager_think()
987 self.nextthink = time + 1;
990 if (autocvar_g_turrets_reloadcvars == 1)
995 if (e.turrcaps_flags & TFL_TURRCAPS_ISTURRET)
997 load_unit_settings(e,e.cvar_basename,1);
998 if(e.turret_postthink)
999 e.turret_postthink();
1004 cvar_set("g_turrets_reloadcvars","0");
1009 * Standard turret initialization. use this!
1010 * (unless you have a very good reason not to)
1011 * if the return value is 0, the turret should be removed.
1013 float turret_stdproc_init (string cvar_base_name, string base, string head, float _turret_type)
1017 // Are turrets allowed?
1018 if (autocvar_g_turrets == 0)
1021 if(_turret_type < 1 || _turret_type > TID_LAST)
1023 dprint("Invalid / Unkown turret type\"", ftos(_turret_type), "\", aborting!\n");
1026 self.turret_type = _turret_type;
1028 e = find(world, classname, "turret_manager");
1034 setorigin(e,'0 0 0');
1035 setmodel(e,"models/turrets/plasma.md3");
1037 v = gettaginfo(e,gettagindex(e,"tag_fire"));
1040 //objerror("^1ERROR: Engine is borken! Turrets will NOT work. force g_turrets to 0 to run maps with turrets anyway.");
1046 e.classname = "turret_manager";
1047 e.think = turrets_manager_think;
1048 e.nextthink = time + 2;
1054 dprint("WARNING: turret requested csqc_shared but this is not implemented. Expect strange things to happen.\n");
1059 if not (self.spawnflags & TSF_SUSPENDED)
1060 droptofloor_builtin();
1062 // Terrainbase spawnflag. This puts a enlongated model
1063 // under the turret, so it looks ok on uneaven surfaces.
1064 /* TODO: Handle this with CSQC
1065 if (self.spawnflags & TSF_TERRAINBASE)
1069 setmodel(tb,"models/turrets/terrainbase.md3");
1070 setorigin(tb,self.origin);
1071 tb.solid = SOLID_BBOX;
1075 self.cvar_basename = cvar_base_name;
1076 load_unit_settings(self, self.cvar_basename, 0);
1078 self.effects = EF_NODRAW;
1080 // Handle turret teams.
1081 if (autocvar_g_assault != 0)
1084 self.team = 14; // Assume turrets are on the defending side if not explicitly set otehrwize
1086 else if not (teamplay)
1087 self.team = MAX_SHOT_DISTANCE; // Group all turrets into the same team, so they dont kill eachother.
1088 else if(g_onslaught && self.targetname)
1090 e = find(world,target,self.targetname);
1098 self.team = MAX_SHOT_DISTANCE; // Group all turrets into the same team, so they dont kill eachother.
1101 * Try to guess some reasonaly defaults
1102 * for missing params and do sanety checks
1103 * thise checks could produce some "interesting" results
1104 * if it hits a glitch in my logic :P so try to set as mutch
1105 * as possible beforehand.
1107 if not(self.ticrate)
1109 if (self.turrcaps_flags & TFL_TURRCAPS_SUPPORT)
1110 self.ticrate = 0.2; // Support units generaly dont need to have a high speed ai-loop
1112 self.ticrate = 0.1; // 10 fps for normal turrets
1115 self.ticrate = bound(sys_frametime, self.ticrate, 60); // keep it sane
1118 if (self.netname == "")
1119 self.netname = self.classname;
1121 if not (self.respawntime)
1122 self.respawntime = 60;
1123 self.respawntime = max(-1, self.respawntime);
1125 if not (self.health)
1127 self.tur_health = max(1, self.health);
1129 if not (self.turrcaps_flags)
1130 self.turrcaps_flags = TFL_TURRCAPS_RADIUSDMG | TFL_TURRCAPS_MEDPROJ | TFL_TURRCAPS_PLAYERKILL;
1132 if not (self.damage_flags)
1133 self.damage_flags = TFL_DMG_YES | TFL_DMG_RETALIATE | TFL_DMG_AIMSHAKE;
1136 if not (self.shot_refire)
1137 self.shot_refire = 1;
1138 self.shot_refire = bound(0.01, self.shot_refire, 9999);
1140 if not (self.shot_dmg)
1141 self.shot_dmg = self.shot_refire * 50;
1142 self.shot_dmg = max(1, self.shot_dmg);
1144 if not (self.shot_radius)
1145 self.shot_radius = self.shot_dmg * 0.5;
1146 self.shot_radius = max(1, self.shot_radius);
1148 if not (self.shot_speed)
1149 self.shot_speed = 2500;
1150 self.shot_speed = max(1, self.shot_speed);
1152 if not (self.shot_spread)
1153 self.shot_spread = 0.0125;
1154 self.shot_spread = bound(0.0001, self.shot_spread, 500);
1156 if not (self.shot_force)
1157 self.shot_force = self.shot_dmg * 0.5 + self.shot_radius * 0.5;
1158 self.shot_force = bound(0.001, self.shot_force, 5000);
1160 if not (self.shot_volly)
1161 self.shot_volly = 1;
1162 self.shot_volly = bound(1, self.shot_volly, floor(self.ammo_max / self.shot_dmg));
1164 if not (self.shot_volly_refire)
1165 self.shot_volly_refire = self.shot_refire * self.shot_volly;
1166 self.shot_volly_refire = bound(self.shot_refire, self.shot_volly_refire, 60);
1168 if not (self.firecheck_flags)
1169 self.firecheck_flags = TFL_FIRECHECK_WORLD | TFL_FIRECHECK_DEAD | TFL_FIRECHECK_DISTANCES |
1170 TFL_FIRECHECK_LOS | TFL_FIRECHECK_AIMDIST | TFL_FIRECHECK_TEAMCECK |
1171 TFL_FIRECHECK_OWM_AMMO | TFL_FIRECHECK_REFIRE | TFL_FIRECHECK_WORLD;
1174 if not (self.target_range)
1175 self.target_range = self.shot_speed * 0.5;
1176 self.target_range = bound(0, self.target_range, MAX_SHOT_DISTANCE);
1178 if not (self.target_range_min)
1179 self.target_range_min = self.shot_radius * 2;
1180 self.target_range_min = bound(0, self.target_range_min, MAX_SHOT_DISTANCE);
1182 if not (self.target_range_optimal)
1183 self.target_range_optimal = self.target_range * 0.5;
1184 self.target_range_optimal = bound(0, self.target_range_optimal, MAX_SHOT_DISTANCE);
1188 if not (self.aim_maxrot)
1189 self.aim_maxrot = 90;
1190 self.aim_maxrot = bound(0, self.aim_maxrot, 360);
1192 if not (self.aim_maxpitch)
1193 self.aim_maxpitch = 20;
1194 self.aim_maxpitch = bound(0, self.aim_maxpitch, 90);
1196 if not (self.aim_speed)
1197 self.aim_speed = 36;
1198 self.aim_speed = bound(0.1, self.aim_speed, 1000);
1200 if not (self.aim_firetolerance_dist)
1201 self.aim_firetolerance_dist = 5 + (self.shot_radius * 2);
1202 self.aim_firetolerance_dist = bound(0.1, self.aim_firetolerance_dist, MAX_SHOT_DISTANCE);
1204 if not (self.aim_flags)
1206 self.aim_flags = TFL_AIM_LEAD | TFL_AIM_SHOTTIMECOMPENSATE;
1207 if(self.turrcaps_flags & TFL_TURRCAPS_RADIUSDMG)
1208 self.aim_flags |= TFL_AIM_GROUND2;
1211 if not (self.track_type)
1212 self.track_type = TFL_TRACKTYPE_STEPMOTOR;
1214 if (self.track_type != TFL_TRACKTYPE_STEPMOTOR)
1216 // Fluid / Ineria mode. Looks mutch nicer.
1217 // Can reduce aim preformance alot, needs a bit diffrent aimspeed
1219 if not (self.aim_speed)
1220 self.aim_speed = 180;
1221 self.aim_speed = bound(0.1, self.aim_speed, 1000);
1223 if not (self.track_accel_pitch)
1224 self.track_accel_pitch = 0.5;
1226 if not (self.track_accel_rot)
1227 self.track_accel_rot = 0.5;
1229 if not (self.track_blendrate)
1230 self.track_blendrate = 0.35;
1233 if (!self.track_flags)
1234 self.track_flags = TFL_TRACK_PITCH | TFL_TRACK_ROT;
1237 // Target selection stuff.
1238 if not (self.target_select_rangebias)
1239 self.target_select_rangebias = 1;
1240 self.target_select_rangebias = bound(-10, self.target_select_rangebias, 10);
1242 if not (self.target_select_samebias)
1243 self.target_select_samebias = 1;
1244 self.target_select_samebias = bound(-10, self.target_select_samebias, 10);
1246 if not (self.target_select_anglebias)
1247 self.target_select_anglebias = 1;
1248 self.target_select_anglebias = bound(-10, self.target_select_anglebias, 10);
1250 if not (self.target_select_missilebias)
1251 self.target_select_missilebias = -10;
1253 self.target_select_missilebias = bound(-10, self.target_select_missilebias, 10);
1254 self.target_select_playerbias = bound(-10, self.target_select_playerbias, 10);
1256 if not (self.target_select_flags)
1258 self.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_TEAMCHECK
1259 | TFL_TARGETSELECT_RANGELIMTS | TFL_TARGETSELECT_ANGLELIMITS;
1261 if (self.turrcaps_flags & TFL_TURRCAPS_MISSILEKILL)
1262 self.target_select_flags |= TFL_TARGETSELECT_MISSILES;
1264 if (self.turrcaps_flags & TFL_TURRCAPS_PLAYERKILL)
1265 self.target_select_flags |= TFL_TARGETSELECT_PLAYERS;
1267 // self.target_select_flags = TFL_TARGETSELECT_NO;
1270 self.target_validate_flags = self.target_select_flags;
1273 if not (self.ammo_max)
1274 self.ammo_max = self.shot_dmg * 10;
1275 self.ammo_max = max(self.shot_dmg, self.ammo_max);
1278 self.ammo = self.shot_dmg * 5;
1279 self.ammo = bound(0,self.ammo, self.ammo_max);
1281 if not (self.ammo_recharge)
1282 self.ammo_recharge = self.shot_dmg * 0.5;
1283 self.ammo_recharge = max(0 ,self.ammo_recharge);
1285 // Convert the recharge from X per sec to X per ticrate
1286 self.ammo_recharge = self.ammo_recharge * self.ticrate;
1288 if not (self.ammo_flags)
1289 self.ammo_flags = TFL_AMMO_ENERGY | TFL_AMMO_RECHARGE;
1292 if(self.spawnflags & TSL_NO_RESPAWN)
1293 if not (self.damage_flags & TFL_DMG_DEATH_NORESPAWN)
1294 self.damage_flags |= TFL_DMG_DEATH_NORESPAWN;
1296 // Offsets & origins
1297 if (!self.tur_shotorg) self.tur_shotorg = '50 0 50';
1303 if(MUTATOR_CALLHOOK(TurretSpawn))
1306 // End of default & sanety checks, start building the turret.
1309 self.tur_head = spawn();
1310 self.tur_head.netname = self.tur_head.classname = "turret_head";
1311 self.tur_head.team = self.team;
1312 self.tur_head.owner = self;
1314 setmodel(self, base);
1315 setmodel(self.tur_head, head);
1317 setsize(self, '-32 -32 0', '32 32 64');
1318 setsize(self.tur_head, '0 0 0', '0 0 0');
1320 setorigin(self.tur_head, '0 0 0');
1321 setattachment(self.tur_head, self, "tag_head");
1323 self.tur_health = self.health;
1324 self.solid = SOLID_BBOX;
1325 self.tur_head.solid = SOLID_NOT;
1326 self.takedamage = DAMAGE_AIM;
1327 self.tur_head.takedamage = DAMAGE_NO;
1328 self.movetype = MOVETYPE_NOCLIP;
1329 self.tur_head.movetype = MOVETYPE_NOCLIP;
1332 if not (self.tur_defend)
1333 if (self.target != "")
1335 self.tur_defend = find(world, targetname, self.target);
1336 if (self.tur_defend == world)
1339 dprint("Turret has invalid defendpoint!\n");
1343 // In target defend mode, aim on the spot to defend when idle.
1344 if (self.tur_defend)
1345 self.idle_aim = self.tur_head.angles + angleofs(self.tur_head, self.tur_defend);
1347 self.idle_aim = '0 0 0';
1349 // Attach stdprocs. override when and what needed
1350 self.turret_firecheckfunc = turret_stdproc_firecheck;
1351 self.turret_firefunc = turret_stdproc_fire;
1352 self.event_damage = turret_stdproc_damage;
1354 if (self.turrcaps_flags & TFL_TURRCAPS_SUPPORT)
1355 self.turret_score_target = turret_stdproc_targetscore_support;
1357 self.turret_score_target = turret_stdproc_targetscore_generic;
1359 self.use = turret_stdproc_use;
1360 self.bot_attack = TRUE;
1363 self.nextthink = time + 1;
1364 self.nextthink += turret_count * sys_frametime;
1366 self.tur_head.team = self.team;
1367 self.view_ofs = '0 0 0';
1370 self.tur_dbg_start = self.nextthink;
1371 while (vlen(self.tur_dbg_rvec) < 2)
1372 self.tur_dbg_rvec = randomvec() * 4;
1374 self.tur_dbg_rvec_x = fabs(self.tur_dbg_rvec_x);
1375 self.tur_dbg_rvec_y = fabs(self.tur_dbg_rvec_y);
1376 self.tur_dbg_rvec_z = fabs(self.tur_dbg_rvec_z);
1380 self.turrcaps_flags |= TFL_TURRCAPS_ISTURRET;
1382 self.classname = "turret_main";
1384 self.tur_active = 1;
1386 // In ONS mode, and linked to a ONS ent. need to call the use to set team.
1387 if (g_onslaught && ee)
1394 turret_stdproc_respawn();
1396 if (!turret_tag_fire_update())
1397 dprint("Warning: Turret ",self.classname, " faild to initialize md3 tags\n");