1 #define cvar_base "g_turrets_unit_"
3 float turret_send(entity to, float sf)
5 WriteByte(MSG_ENTITY, ENT_CLIENT_TURRET);
7 WriteByte(MSG_ENTITY, sf);
10 WriteByte(MSG_ENTITY, self.turret_type);
12 WriteCoord(MSG_ENTITY, self.origin_x);
13 WriteCoord(MSG_ENTITY, self.origin_y);
14 WriteCoord(MSG_ENTITY, self.origin_z);
16 WriteAngle(MSG_ENTITY, self.angles_x);
17 WriteAngle(MSG_ENTITY, self.angles_y);
22 WriteAngle(MSG_ENTITY, self.tur_head.angles_x);
23 WriteAngle(MSG_ENTITY, self.tur_head.angles_y);
28 WriteAngle(MSG_ENTITY, self.tur_head.avelocity_x);
29 WriteAngle(MSG_ENTITY, self.tur_head.avelocity_y);
34 WriteByte(MSG_ENTITY, self.team);
35 WriteByte(MSG_ENTITY, rint((self.health / self.tur_health) * 255));
41 void load_unit_settings(entity ent, string unitname, float is_reload)
48 if not (ent.turret_scale_damage) ent.turret_scale_damage = 1;
49 if not (ent.turret_scale_range) ent.turret_scale_range = 1;
50 if not (ent.turret_scale_refire) ent.turret_scale_refire = 1;
51 if not (ent.turret_scale_ammo) ent.turret_scale_ammo = 1;
52 if not (ent.turret_scale_aim) ent.turret_scale_aim = 1;
53 if not (ent.turret_scale_health) ent.turret_scale_health = 1;
54 if not (ent.turret_scale_respawn) ent.turret_scale_respawn = 1;
56 sbase = strcat(cvar_base,unitname);
60 ent.tur_head.avelocity = '0 0 0';
62 ent.tur_head.angles = '0 0 0';
65 ent.health = cvar(strcat(sbase,"_health")) * ent.turret_scale_health;
66 ent.respawntime = cvar(strcat(sbase,"_respawntime")) * ent.turret_scale_respawn;
68 ent.shot_dmg = cvar(strcat(sbase,"_shot_dmg")) * ent.turret_scale_damage;
69 ent.shot_refire = cvar(strcat(sbase,"_shot_refire")) * ent.turret_scale_refire;
70 ent.shot_radius = cvar(strcat(sbase,"_shot_radius")) * ent.turret_scale_damage;
71 ent.shot_speed = cvar(strcat(sbase,"_shot_speed"));
72 ent.shot_spread = cvar(strcat(sbase,"_shot_spread"));
73 ent.shot_force = cvar(strcat(sbase,"_shot_force")) * ent.turret_scale_damage;
74 ent.shot_volly = cvar(strcat(sbase,"_shot_volly"));
75 ent.shot_volly_refire = cvar(strcat(sbase,"_shot_volly_refire")) * ent.turret_scale_refire;
77 ent.target_range = cvar(strcat(sbase,"_target_range")) * ent.turret_scale_range;
78 ent.target_range_min = cvar(strcat(sbase,"_target_range_min")) * ent.turret_scale_range;
79 ent.target_range_optimal = cvar(strcat(sbase,"_target_range_optimal")) * ent.turret_scale_range;
80 //ent.target_range_fire = cvar(strcat(sbase,"_target_range_fire")) * ent.turret_scale_range;
82 ent.target_select_rangebias = cvar(strcat(sbase,"_target_select_rangebias"));
83 ent.target_select_samebias = cvar(strcat(sbase,"_target_select_samebias"));
84 ent.target_select_anglebias = cvar(strcat(sbase,"_target_select_anglebias"));
85 ent.target_select_playerbias = cvar(strcat(sbase,"_target_select_playerbias"));
86 //ent.target_select_fov = cvar(cvar_gets(sbase,"_target_select_fov"));
88 ent.ammo_max = cvar(strcat(sbase,"_ammo_max")) * ent.turret_scale_ammo;
89 ent.ammo_recharge = cvar(strcat(sbase,"_ammo_recharge")) * ent.turret_scale_ammo;
91 ent.aim_firetolerance_dist = cvar(strcat(sbase,"_aim_firetolerance_dist"));
92 ent.aim_speed = cvar(strcat(sbase,"_aim_speed")) * ent.turret_scale_aim;
93 ent.aim_maxrot = cvar(strcat(sbase,"_aim_maxrot"));
94 ent.aim_maxpitch = cvar(strcat(sbase,"_aim_maxpitch"));
96 ent.track_type = cvar(strcat(sbase,"_track_type"));
97 ent.track_accel_pitch = cvar(strcat(sbase,"_track_accel_pitch"));
98 ent.track_accel_rot = cvar(strcat(sbase,"_track_accel_rot"));
99 ent.track_blendrate = cvar(strcat(sbase,"_track_blendrate"));
102 if(ent.turret_respawnhook)
103 ent.turret_respawnhook();
108 ** updates enemy distances, predicted impact point/time
109 ** and updated aim<->predict impact distance.
111 void turret_do_updates(entity t_turret)
113 vector enemy_pos, oldpos;
119 enemy_pos = real_origin(self.enemy);
121 turret_tag_fire_update();
123 self.tur_shotdir_updated = v_forward;
124 self.tur_dist_enemy = vlen(self.tur_shotorg - enemy_pos);
125 self.tur_dist_aimpos = vlen(self.tur_shotorg - self.tur_aimpos);
127 if((self.firecheck_flags & TFL_FIRECHECK_VERIFIED) && (self.enemy))
129 oldpos = self.enemy.origin;
130 setorigin(self.enemy,self.tur_aimpos);
131 tracebox(self.tur_shotorg, '-1 -1 -1','1 1 1',self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos),MOVE_NORMAL,self);
132 setorigin(self.enemy,oldpos);
134 if(trace_ent == self.enemy)
135 self.tur_dist_impact_to_aimpos = 0;
137 self.tur_dist_impact_to_aimpos = vlen(trace_endpos - self.tur_aimpos);
140 tracebox(self.tur_shotorg, '-1 -1 -1','1 1 1', self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos),MOVE_NORMAL,self);
142 self.tur_dist_impact_to_aimpos = vlen(trace_endpos - self.tur_aimpos) - (vlen(self.enemy.maxs - self.enemy.mins) * 0.5);
143 self.tur_impactent = trace_ent;
144 self.tur_impacttime = vlen(self.tur_shotorg - trace_endpos) / self.shot_speed;
150 vector turret_fovsearch_pingpong()
153 if(self.phase < time)
155 if( self.tur_head.phase )
156 self.tur_head.phase = 0;
158 self.tur_head.phase = 1;
159 self.phase = time + 5;
162 if( self.tur_head.phase)
163 wish_angle = self.idle_aim + '0 1 0' * (self.aim_maxrot * (self.target_select_fov / 360));
165 wish_angle = self.idle_aim - '0 1 0' * (self.aim_maxrot * (self.target_select_fov / 360));
170 vector turret_fovsearch_steprot()
175 wish_angle = self.tur_head.angles;
176 wish_angle_x = self.idle_aim_x;
178 if (self.phase < time)
180 //rot_add = self.aim_maxrot / self.target_select_fov;
181 wish_angle_y += (self.target_select_fov * 2);
183 if(wish_angle_y > 360)
184 wish_angle_y = wish_angle_y - 360;
186 self.phase = time + 1.5;
192 vector turret_fovsearch_random()
196 if (self.phase < time)
198 wish_angle_y = random() * self.aim_maxrot;
202 wish_angle_x = random() * self.aim_maxpitch;
206 self.phase = time + 5;
208 self.tur_aimpos = wish_angle;
211 return self.idle_aim + self.tur_aimpos;
216 ** Handles head rotation according to
217 ** the units .track_type and .track_flags
219 .float turret_framecounter;
220 void turret_stdproc_track()
222 vector target_angle; // This is where we want to aim
223 vector move_angle; // This is where we can aim
226 v1 = self.tur_head.angles;
227 v2 = self.tur_head.avelocity;
229 if (self.track_flags == TFL_TRACK_NO)
232 if not (self.tur_active)
233 target_angle = self.idle_aim - ('1 0 0' * self.aim_maxpitch);
234 else if (self.enemy == world)
237 target_angle = self.idle_aim + self.angles;
239 target_angle = vectoangles(normalize(self.tur_aimpos - self.tur_shotorg));
243 target_angle = vectoangles(normalize(self.tur_aimpos - self.tur_shotorg));
246 self.tur_head.angles_x = anglemods(self.tur_head.angles_x);
247 self.tur_head.angles_y = anglemods(self.tur_head.angles_y);
249 // Find the diffrence between where we currently aim and where we want to aim
250 move_angle = target_angle - (self.angles + self.tur_head.angles);
251 move_angle = shortangle_vxy(move_angle,(self.angles + self.tur_head.angles));
253 switch(self.track_type)
255 case TFL_TRACKTYPE_STEPMOTOR:
256 f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
257 if (self.track_flags & TFL_TRACK_PITCH)
259 self.tur_head.angles_x += bound(-f_tmp,move_angle_x, f_tmp);
260 if(self.tur_head.angles_x > self.aim_maxpitch)
261 self.tur_head.angles_x = self.aim_maxpitch;
263 if(self.tur_head.angles_x < -self.aim_maxpitch)
264 self.tur_head.angles_x = self.aim_maxpitch;
267 if (self.track_flags & TFL_TRACK_ROT)
269 self.tur_head.angles_y += bound(-f_tmp, move_angle_y, f_tmp);
270 if(self.tur_head.angles_y > self.aim_maxrot)
271 self.tur_head.angles_y = self.aim_maxrot;
273 if(self.tur_head.angles_y < -self.aim_maxrot)
274 self.tur_head.angles_y = self.aim_maxrot;
279 self.SendFlags = TNSF_ANG;
283 case TFL_TRACKTYPE_FLUIDINERTIA:
284 f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
285 move_angle_x = bound(-self.aim_speed, move_angle_x * self.track_accel_pitch * f_tmp, self.aim_speed);
286 move_angle_y = bound(-self.aim_speed, move_angle_y * self.track_accel_rot * f_tmp, self.aim_speed);
287 move_angle = (self.tur_head.avelocity * self.track_blendrate) + (move_angle * (1 - self.track_blendrate));
290 case TFL_TRACKTYPE_FLUIDPRECISE:
292 move_angle_y = bound(-self.aim_speed, move_angle_y, self.aim_speed);
293 move_angle_x = bound(-self.aim_speed, move_angle_x, self.aim_speed);
299 if (self.track_flags & TFL_TRACK_PITCH)
301 self.tur_head.avelocity_x = move_angle_x;
302 if((self.tur_head.angles_x + self.tur_head.avelocity_x * self.ticrate) > self.aim_maxpitch)
304 self.tur_head.avelocity_x = 0;
305 self.tur_head.angles_x = self.aim_maxpitch;
308 self.SendFlags |= TNSF_ANG | TNSF_AVEL;
311 if((self.tur_head.angles_x + self.tur_head.avelocity_x * self.ticrate) < -self.aim_maxpitch)
313 self.tur_head.avelocity_x = 0;
314 self.tur_head.angles_x = -self.aim_maxpitch;
317 self.SendFlags |= TNSF_ANG | TNSF_AVEL;
322 if (self.track_flags & TFL_TRACK_ROT)
324 self.tur_head.avelocity_y = move_angle_y;
326 if((self.tur_head.angles_y + self.tur_head.avelocity_y * self.ticrate) > self.aim_maxrot)
328 self.tur_head.avelocity_y = 0;
329 self.tur_head.angles_y = self.aim_maxrot;
332 self.SendFlags |= TNSF_ANG | TNSF_AVEL;
335 if((self.tur_head.angles_y + self.tur_head.avelocity_y * self.ticrate) < -self.aim_maxrot)
337 self.tur_head.avelocity_y = 0;
338 self.tur_head.angles_y = -self.aim_maxrot;
341 self.SendFlags |= TNSF_ANG | TNSF_AVEL;
347 self.turret_framecounter += 1;
348 if(self.turret_framecounter >= 4)
350 self.SendFlags |= TNSF_ANG | TNSF_AVEL;
351 self.turret_framecounter = 0;
354 self.SendFlags |= TNSF_AVEL;
366 + TFL_FIRECHECK_WORLD
368 + TFL_FIRECHECK_DISTANCES
370 + TFL_FIRECHECK_AIMDIST
371 + TFL_FIRECHECK_REALDIST
372 - TFL_FIRECHECK_ANGLEDIST
373 - TFL_FIRECHECK_TEAMCECK
375 + TFL_FIRECHECK_OWM_AMMO
376 + TFL_FIRECHECK_OTHER_AMMO
377 + TFL_FIRECHECK_REFIRE
381 ** Preforms pre-fire checks based on the uints firecheck_flags
383 float turret_stdproc_firecheck()
385 // This one just dont care =)
386 if (self.firecheck_flags & TFL_FIRECHECK_NO) return 1;
389 if (self.firecheck_flags & TFL_FIRECHECK_REFIRE)
390 if (self.attack_finished_single > time) return 0;
392 // Special case: volly fire turret that has to fire a full volly if a shot was fired.
393 if (self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
394 if (self.volly_counter != self.shot_volly)
395 if(self.ammo >= self.shot_dmg)
398 // Lack of zombies makes shooting dead things unnecessary :P
399 if (self.firecheck_flags & TFL_FIRECHECK_DEAD)
400 if (self.enemy.deadflag != DEAD_NO)
403 // Plz stop killing the world!
404 if (self.firecheck_flags & TFL_FIRECHECK_WORLD)
405 if (self.enemy == world)
409 if (self.firecheck_flags & TFL_FIRECHECK_OWM_AMMO)
410 if (self.ammo < self.shot_dmg)
413 // Other's ammo? (support-supply units)
414 if (self.firecheck_flags & TFL_FIRECHECK_OTHER_AMMO)
415 if (self.enemy.ammo >= self.enemy.ammo_max)
418 // Target of opertunity?
419 if(turret_validate_target(self, self.tur_impactent, self.target_validate_flags) > 0)
421 self.enemy = self.tur_impactent;
425 if (self.firecheck_flags & TFL_FIRECHECK_DISTANCES)
428 if (self.tur_dist_aimpos < self.target_range_min)
429 if(turret_validate_target(self, self.tur_impactent, self.target_validate_flags) > 0)
430 return 1; // Target of opertunity?
436 if (self.firecheck_flags & TFL_FIRECHECK_AFF)
437 if (self.tur_impactent.team == self.team)
440 // aim<->predicted impact
441 if (self.firecheck_flags & TFL_FIRECHECK_AIMDIST)
442 if (self.tur_dist_impact_to_aimpos > self.aim_firetolerance_dist)
446 if (self.shot_volly > 1)
447 if (self.volly_counter == self.shot_volly)
448 if (self.ammo < (self.shot_dmg * self.shot_volly))
451 if(self.firecheck_flags & TFL_FIRECHECK_VERIFIED)
452 if(self.tur_impactent != self.enemy)
459 + TFL_TARGETSELECT_NO
460 + TFL_TARGETSELECT_LOS
461 + TFL_TARGETSELECT_PLAYERS
462 + TFL_TARGETSELECT_MISSILES
463 - TFL_TARGETSELECT_TRIGGERTARGET
464 + TFL_TARGETSELECT_ANGLELIMITS
465 + TFL_TARGETSELECT_RANGELIMTS
466 + TFL_TARGETSELECT_TEAMCHECK
467 - TFL_TARGETSELECT_NOBUILTIN
468 + TFL_TARGETSELECT_OWNTEAM
472 ** Evaluate a entity for target valitity based on validate_flags
473 ** NOTE: the caller must check takedamage before calling this, to inline this check.
475 float turret_validate_target(entity e_turret, entity e_target, float validate_flags)
479 //if(!validate_flags & TFL_TARGETSELECT_NOBUILTIN)
482 if(e_target.owner == e_turret)
485 if not(checkpvs(e_target.origin, e_turret))
492 if (substring(e_target.classname, 0, 10) == "onslaught_") // don't attack onslaught targets, that's the player's job!
495 if (validate_flags & TFL_TARGETSELECT_NO)
498 // If only this was used more..
499 if (e_target.flags & FL_NOTARGET)
503 if (e_target.health < 0)
507 if (e_target.flags & FL_CLIENT)
509 if not (validate_flags & TFL_TARGETSELECT_PLAYERS)
512 if (e_target.deadflag != DEAD_NO)
517 if (validate_flags & TFL_TARGETSELECT_NOTURRETS)
518 if (e_target.turret_firefunc || e_target.owner.tur_head == e_target)
519 if(e_target.team != e_turret.team) // Dont break support units.
523 if (e_target.flags & FL_PROJECTILE)
524 if not (validate_flags & TFL_TARGETSELECT_MISSILES)
527 if (validate_flags & TFL_TARGETSELECT_MISSILESONLY)
528 if not (e_target.flags & FL_PROJECTILE)
532 if (validate_flags & TFL_TARGETSELECT_TEAMCHECK)
534 if (validate_flags & TFL_TARGETSELECT_OWNTEAM)
536 if (e_target.team != e_turret.team)
539 if (e_turret.team != e_target.owner.team)
544 if (e_target.team == e_turret.team)
547 if (e_turret.team == e_target.owner.team)
553 tvt_dist = vlen(e_turret.origin - real_origin(e_target));
554 if (validate_flags & TFL_TARGETSELECT_RANGELIMTS)
556 if (tvt_dist < e_turret.target_range_min)
559 if (tvt_dist > e_turret.target_range)
563 // Can we even aim this thing?
564 tvt_thadv = angleofs3(e_turret.tur_head.origin, e_turret.angles + e_turret.tur_head.angles, e_target);
565 tvt_tadv = shortangle_vxy(angleofs(e_turret, e_target), e_turret.angles);
566 tvt_thadf = vlen(tvt_thadv);
567 tvt_tadf = vlen(tvt_tadv);
570 if(validate_flags & TFL_TARGETSELECT_FOV)
572 if(e_turret.target_select_fov < tvt_thadf)
577 if (validate_flags & TFL_TARGETSELECT_ANGLELIMITS)
579 if (fabs(tvt_tadv_x) > e_turret.aim_maxpitch)
582 if (fabs(tvt_tadv_y) > e_turret.aim_maxrot)
587 if (validate_flags & TFL_TARGETSELECT_LOS)
589 v_tmp = real_origin(e_target) + ((e_target.mins + e_target.maxs) * 0.5);
591 traceline(e_turret.tur_shotorg, v_tmp, 0, e_turret);
593 if (e_turret.aim_firetolerance_dist < vlen(v_tmp - trace_endpos))
597 if (e_target.classname == "grapplinghook")
601 if (e_target.classname == "func_button")
605 #ifdef TURRET_DEBUG_TARGETSELECT
606 dprint("Target:",e_target.netname," is a valid target for ",e_turret.netname,"\n");
612 entity turret_select_target()
614 entity e; // target looper entity
615 float score; // target looper entity score
616 entity e_enemy; // currently best scoreing target
617 float m_score; // currently best scoreing target's score
621 if(self.enemy.takedamage)
622 if(turret_validate_target(self,self.enemy,self.target_validate_flags) > 0)
624 e_enemy = self.enemy;
625 m_score = self.turret_score_target(self,e_enemy) * self.target_select_samebias;
630 e = findradius(self.origin, self.target_range);
632 // Nothing to aim at?
640 if (turret_validate_target(self, e, self.target_select_flags) > 0)
642 score = self.turret_score_target(self,e);
643 if ((score > m_score) && (score > 0))
660 self.nextthink = time + self.ticrate;
661 self.SendFlags = TNSF_UPDATE | TNSF_STATUS | TNSF_ANG | TNSF_AVEL;
663 // ONS uses somewhat backwards linking.
669 e = find(world, targetname,self.target);
674 if (self.team != self.tur_head.team)
675 turret_stdproc_respawn();
679 if (self.tur_dbg_tmr1 < time)
681 if (self.enemy) paint_target (self.enemy,128,self.tur_dbg_rvec,0.9);
682 paint_target(self,256,self.tur_dbg_rvec,0.9);
683 self.tur_dbg_tmr1 = time + 1;
688 if not (self.spawnflags & TSF_NO_AMMO_REGEN)
689 if (self.ammo < self.ammo_max)
690 self.ammo = min(self.ammo + self.ammo_recharge, self.ammo_max);
692 if (self.health < (self.tur_health * 0.5))
694 te_spark(self.origin + '0 0 40', randomvec() * 256 + '0 0 256', 16);
696 // Inactive turrets needs to run the think loop,
697 // So they can handle animation and wake up if need be.
698 if not (self.tur_active)
700 turret_stdproc_track();
704 // This is typicaly used for zaping every target in range
705 // turret_fusionreactor uses this to recharge friendlys.
706 if (self.shoot_flags & TFL_SHOOT_HITALLVALID)
708 // Do a self.turret_fire for every valid target.
709 e = findradius(self.origin,self.target_range);
714 if (turret_validate_target(self,e,self.target_validate_flags))
718 turret_do_updates(self);
720 if (self.turret_firecheckfunc())
729 else if(self.shoot_flags & TFL_SHOOT_CUSTOM)
731 // This one is doing something.. oddball. assume its handles what needs to be handled.
734 if not(self.aim_flags & TFL_AIM_NO)
735 self.tur_aimpos = turret_stdproc_aim_generic();
738 if not(self.track_flags & TFL_TRACK_NO)
739 turret_stdproc_track();
741 turret_do_updates(self);
744 if (self.turret_firecheckfunc())
749 // Special case for volly always. if it fired once it must compleate the volly.
750 if(self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
751 if(self.volly_counter != self.shot_volly)
753 // Predict or whatnot
754 if not(self.aim_flags & TFL_AIM_NO)
755 self.tur_aimpos = turret_stdproc_aim_generic();
758 if not(self.track_flags & TFL_TRACK_NO)
759 turret_stdproc_track();
761 turret_do_updates(self);
764 if (self.turret_firecheckfunc() != 0)
767 if(self.turret_postthink)
768 self.turret_postthink();
773 // Check if we have a vailid enemy, and try to find one if we dont.
775 // g_turrets_targetscan_maxdelay forces a target re-scan at least this often
776 float do_target_scan;
777 if((self.target_select_time + autocvar_g_turrets_targetscan_maxdelay) < time)
780 // Old target (if any) invalid?
781 if (turret_validate_target(self, self.enemy, self.target_validate_flags) <= 0)
787 // But never more often then g_turrets_targetscan_mindelay!
788 if (self.target_select_time + autocvar_g_turrets_targetscan_mindelay > time)
793 self.enemy = turret_select_target();
794 self.target_select_time = time;
797 // No target, just go to idle, do any custom stuff and bail.
798 if (self.enemy == world)
801 if not(self.track_flags & TFL_TRACK_NO)
802 turret_stdproc_track();
804 // do any per-turret stuff
805 if(self.turret_postthink)
806 self.turret_postthink();
812 self.lip = time + autocvar_g_turrets_aimidle_delay; // Keep track of the last time we had a target.
815 if not(self.aim_flags & TFL_AIM_NO)
816 self.tur_aimpos = turret_stdproc_aim_generic();
819 if not(self.track_flags & TFL_TRACK_NO)
820 turret_stdproc_track();
822 turret_do_updates(self);
825 if (self.turret_firecheckfunc())
829 // do any custom per-turret stuff
830 if(self.turret_postthink)
831 self.turret_postthink();
836 if (autocvar_g_turrets_nofire != 0)
839 self.turret_firefunc();
841 self.attack_finished_single = time + self.shot_refire;
842 self.ammo -= self.shot_dmg;
843 self.volly_counter = self.volly_counter - 1;
845 if (self.volly_counter <= 0)
847 self.volly_counter = self.shot_volly;
849 if (self.shoot_flags & TFL_SHOOT_CLEARTARGET)
852 if (self.shot_volly > 1)
853 self.attack_finished_single = time + self.shot_volly_refire;
857 if (self.enemy) paint_target3(self.tur_aimpos, 64, self.tur_dbg_rvec, self.tur_impacttime + 0.25);
861 void turret_stdproc_fire()
863 dprint("^1Bang, ^3your dead^7 ",self.enemy.netname,"! ^1(turret with no real firefunc)\n");
867 When .used a turret switch team to activator.team.
868 If activator is world, the turret go inactive.
870 void turret_stdproc_use()
872 dprint("Turret ",self.netname, " used by ", activator.classname, "\n");
874 self.team = activator.team;
885 Net_LinkEntity(self, TRUE, 0, turret_send);
886 self.think = turret_think;
887 self.nextthink = time;
890 void turrets_manager_think()
892 self.nextthink = time + 1;
895 if (autocvar_g_turrets_reloadcvars == 1)
900 if (e.turrcaps_flags & TFL_TURRCAPS_ISTURRET)
902 load_unit_settings(e,e.cvar_basename,1);
903 if(e.turret_postthink)
904 e.turret_postthink();
909 cvar_set("g_turrets_reloadcvars","0");
914 * Standard turret initialization. use this!
915 * (unless you have a very good reason not to)
916 * if the return value is 0, the turret should be removed.
918 float turret_stdproc_init (string cvar_base_name, float csqc_shared, string base, string head, float _turret_type)
922 // Are turrets allowed?
923 if (autocvar_g_turrets == 0)
926 if(_turret_type < 1 || _turret_type > TID_LAST)
928 dprint("Invalid / Unkown turret type\"", ftos(_turret_type), "\", aborting!\n");
931 self.turret_type = _turret_type;
933 e = find(world, classname, "turret_manager");
939 setorigin(e,'0 0 0');
940 setmodel(e,"models/turrets/plasma.md3");
942 v = gettaginfo(e,gettagindex(e,"tag_fire"));
945 //objerror("^1ERROR: Engine is borken! Turrets will NOT work. force g_turrets to 0 to run maps with turrets anyway.");
951 e.classname = "turret_manager";
952 e.think = turrets_manager_think;
953 e.nextthink = time + 2;
955 #ifndef TTURRETS_CSQC
961 dprint("WARNING: turret requested csqc_shared but this is not implemented. Expect strange things to happen.\n");
966 if not (self.spawnflags & TSF_SUSPENDED)
967 droptofloor_builtin();
969 // Terrainbase spawnflag. This puts a enlongated model
970 // under the turret, so it looks ok on uneaven surfaces.
971 /* TODO: Handle this with CSQC
972 if (self.spawnflags & TSF_TERRAINBASE)
976 setmodel(tb,"models/turrets/terrainbase.md3");
977 setorigin(tb,self.origin);
978 tb.solid = SOLID_BBOX;
982 self.cvar_basename = cvar_base_name;
983 load_unit_settings(self, self.cvar_basename, 0);
985 // Handle turret teams.
986 if (autocvar_g_assault != 0)
989 self.team = 14; // Assume turrets are on the defending side if not explicitly set otehrwize
991 else if not (teams_matter)
992 self.team = MAX_SHOT_DISTANCE; // Group all turrets into the same team, so they dont kill eachother.
993 else if(g_onslaught && self.targetname)
995 e = find(world,target,self.targetname);
1003 self.team = MAX_SHOT_DISTANCE; // Group all turrets into the same team, so they dont kill eachother.
1006 * Try to guess some reasonaly defaults
1007 * for missing params and do sanety checks
1008 * thise checks could produce some "interesting" results
1009 * if it hits a glitch in my logic :P so try to set as mutch
1010 * as possible beforehand.
1012 if (self.turrcaps_flags & TFL_TURRCAPS_SUPPORT)
1013 self.ticrate = 0.2; // Support units generaly dont need to have a high speed ai-loop
1015 self.ticrate = 0.1; // 10 fps for normal turrets
1017 self.ticrate = bound(sys_frametime, self.ticrate, 60); // keep it sane
1020 if (self.netname == "")
1021 self.netname = self.classname;
1023 if not (self.respawntime)
1024 self.respawntime = 60;
1025 self.respawntime = max(-1, self.respawntime);
1027 if not (self.health)
1029 self.tur_health = max(1, self.health);
1031 if not (self.turrcaps_flags)
1032 self.turrcaps_flags = TFL_TURRCAPS_RADIUSDMG | TFL_TURRCAPS_MEDPROJ | TFL_TURRCAPS_PLAYERKILL;
1034 if not (self.damage_flags)
1035 self.damage_flags = TFL_DMG_YES | TFL_DMG_RETALIATE | TFL_DMG_AIMSHAKE;
1038 if not (self.shot_refire)
1039 self.shot_refire = 1;
1040 self.shot_refire = bound(0.01, self.shot_refire, 9999);
1042 if not (self.shot_dmg)
1043 self.shot_dmg = self.shot_refire * 50;
1044 self.shot_dmg = max(1, self.shot_dmg);
1046 if not (self.shot_radius)
1047 self.shot_radius = self.shot_dmg * 0.5;
1048 self.shot_radius = max(1, self.shot_radius);
1050 if not (self.shot_speed)
1051 self.shot_speed = 2500;
1052 self.shot_speed = max(1, self.shot_speed);
1054 if not (self.shot_spread)
1055 self.shot_spread = 0.0125;
1056 self.shot_spread = bound(0.0001, self.shot_spread, 500);
1058 if not (self.shot_force)
1059 self.shot_force = self.shot_dmg * 0.5 + self.shot_radius * 0.5;
1060 self.shot_force = bound(0.001, self.shot_force, 5000);
1062 if not (self.shot_volly)
1063 self.shot_volly = 1;
1064 self.shot_volly = bound(1, self.shot_volly, floor(self.ammo_max / self.shot_dmg));
1066 if not (self.shot_volly_refire)
1067 self.shot_volly_refire = self.shot_refire * self.shot_volly;
1068 self.shot_volly_refire = bound(self.shot_refire, self.shot_volly_refire, 60);
1070 if not (self.firecheck_flags)
1071 self.firecheck_flags = TFL_FIRECHECK_WORLD | TFL_FIRECHECK_DEAD | TFL_FIRECHECK_DISTANCES |
1072 TFL_FIRECHECK_LOS | TFL_FIRECHECK_AIMDIST | TFL_FIRECHECK_TEAMCECK |
1073 TFL_FIRECHECK_OWM_AMMO | TFL_FIRECHECK_REFIRE | TFL_FIRECHECK_WORLD;
1076 if not (self.target_range)
1077 self.target_range = self.shot_speed * 0.5;
1078 self.target_range = bound(0, self.target_range, MAX_SHOT_DISTANCE);
1080 if not (self.target_range_min)
1081 self.target_range_min = self.shot_radius * 2;
1082 self.target_range_min = bound(0, self.target_range_min, MAX_SHOT_DISTANCE);
1084 if not (self.target_range_optimal)
1085 self.target_range_optimal = self.target_range * 0.5;
1086 self.target_range_optimal = bound(0, self.target_range_optimal, MAX_SHOT_DISTANCE);
1090 if not (self.aim_maxrot)
1091 self.aim_maxrot = 90;
1092 self.aim_maxrot = bound(0, self.aim_maxrot, 360);
1094 if not (self.aim_maxpitch)
1095 self.aim_maxpitch = 20;
1096 self.aim_maxpitch = bound(0, self.aim_maxpitch, 90);
1098 if not (self.aim_speed)
1099 self.aim_speed = 36;
1100 self.aim_speed = bound(0.1, self.aim_speed, 1000);
1102 if not (self.aim_firetolerance_dist)
1103 self.aim_firetolerance_dist = 5 + (self.shot_radius * 2);
1104 self.aim_firetolerance_dist = bound(0.1, self.aim_firetolerance_dist, MAX_SHOT_DISTANCE);
1106 if not (self.aim_flags)
1108 self.aim_flags = TFL_AIM_LEAD | TFL_AIM_SHOTTIMECOMPENSATE;
1109 if(self.turrcaps_flags & TFL_TURRCAPS_RADIUSDMG)
1110 self.aim_flags |= TFL_AIM_GROUND2;
1113 if not (self.track_type)
1114 self.track_type = TFL_TRACKTYPE_STEPMOTOR;
1116 if (self.track_type != TFL_TRACKTYPE_STEPMOTOR)
1118 // Fluid / Ineria mode. Looks mutch nicer.
1119 // Can reduce aim preformance alot, needs a bit diffrent aimspeed
1121 if not (self.aim_speed)
1122 self.aim_speed = 180;
1123 self.aim_speed = bound(0.1, self.aim_speed, 1000);
1125 if not (self.track_accel_pitch)
1126 self.track_accel_pitch = 0.5;
1128 if not (self.track_accel_rot)
1129 self.track_accel_rot = 0.5;
1131 if not (self.track_blendrate)
1132 self.track_blendrate = 0.35;
1135 if (!self.track_flags)
1136 self.track_flags = TFL_TRACK_PITCH | TFL_TRACK_ROT;
1139 // Target selection stuff.
1140 if not (self.target_select_rangebias)
1141 self.target_select_rangebias = 1;
1142 self.target_select_rangebias = bound(-10, self.target_select_rangebias, 10);
1144 if not (self.target_select_samebias)
1145 self.target_select_samebias = 1;
1146 self.target_select_samebias = bound(-10, self.target_select_samebias, 10);
1148 if not (self.target_select_anglebias)
1149 self.target_select_anglebias = 1;
1150 self.target_select_anglebias = bound(-10, self.target_select_anglebias, 10);
1152 if not (self.target_select_missilebias)
1153 self.target_select_missilebias = -10;
1155 self.target_select_missilebias = bound(-10, self.target_select_missilebias, 10);
1156 self.target_select_playerbias = bound(-10, self.target_select_playerbias, 10);
1158 if not (self.target_select_flags)
1160 self.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_TEAMCHECK
1161 | TFL_TARGETSELECT_RANGELIMTS | TFL_TARGETSELECT_ANGLELIMITS;
1163 if (self.turrcaps_flags & TFL_TURRCAPS_MISSILEKILL)
1164 self.target_select_flags |= TFL_TARGETSELECT_MISSILES;
1166 if (self.turrcaps_flags & TFL_TURRCAPS_PLAYERKILL)
1167 self.target_select_flags |= TFL_TARGETSELECT_PLAYERS;
1169 // self.target_select_flags = TFL_TARGETSELECT_NO;
1172 self.target_validate_flags = self.target_select_flags;
1176 if not (self.ammo_max)
1177 self.ammo_max = self.shot_dmg * 10;
1178 self.ammo_max = max(self.shot_dmg, self.ammo_max);
1181 self.ammo = self.shot_dmg * 5;
1182 self.ammo = bound(0,self.ammo, self.ammo_max);
1184 if not (self.ammo_recharge)
1185 self.ammo_recharge = self.shot_dmg * 0.5;
1186 self.ammo_recharge = max(0 ,self.ammo_recharge);
1188 // Convert the recharge from X per sec to X per ticrate
1189 self.ammo_recharge = self.ammo_recharge * self.ticrate;
1191 if not (self.ammo_flags)
1192 self.ammo_flags = TFL_AMMO_ENERGY | TFL_AMMO_RECHARGE;
1195 if(self.spawnflags & TSL_NO_RESPAWN)
1196 if not (self.damage_flags & TFL_DMG_DEATH_NORESPAWN)
1197 self.damage_flags |= TFL_DMG_DEATH_NORESPAWN;
1199 // Offsets & origins
1200 if (!self.tur_shotorg) self.tur_shotorg = '50 0 50';
1203 if(MUTATOR_CALLHOOK(TurretSpawn))
1206 // End of default & sanety checks, start building the turret.
1209 self.tur_head = spawn();
1210 self.tur_head.netname = self.tur_head.classname = "turret_head";
1211 self.tur_head.team = self.team;
1212 self.tur_head.owner = self;
1214 setmodel(self, base);
1215 setmodel(self.tur_head, head);
1217 setsize(self, '-32 -32 0', '32 32 64');
1218 setsize(self.tur_head, '0 0 0', '0 0 0');
1220 setorigin(self.tur_head, '0 0 0');
1221 setattachment(self.tur_head, self, "tag_head");
1226 self.tur_health = self.health;
1227 self.solid = SOLID_BBOX;
1228 self.tur_head.solid = SOLID_NOT;
1229 self.takedamage = DAMAGE_AIM;
1230 self.tur_head.takedamage = DAMAGE_NO;
1231 self.movetype = MOVETYPE_NOCLIP;
1232 self.tur_head.movetype = MOVETYPE_NOCLIP;
1235 if not (self.tur_defend)
1236 if (self.target != "")
1238 self.tur_defend = find(world, targetname, self.target);
1239 if (self.tur_defend == world)
1242 dprint("Turret has invalid defendpoint!\n");
1246 // In target defend mode, aim on the spot to defend when idle.
1247 if (self.tur_defend)
1248 self.idle_aim = self.tur_head.angles + angleofs(self.tur_head, self.tur_defend);
1250 self.idle_aim = '0 0 0';
1253 if (self.team == COLOR_TEAM1) self.colormod = '1.4 0.8 0.8';
1254 if (self.team == COLOR_TEAM2) self.colormod = '0.8 0.8 1.4';
1256 // Attach stdprocs. override when and what needed
1257 if (self.turrcaps_flags & TFL_TURRCAPS_SUPPORT)
1259 self.turret_score_target = turret_stdproc_targetscore_support;
1260 self.turret_firecheckfunc = turret_stdproc_firecheck;
1261 self.turret_firefunc = turret_stdproc_fire;
1262 self.event_damage = turret_stdproc_damage;
1266 self.turret_score_target = turret_stdproc_targetscore_generic;
1267 self.turret_firecheckfunc = turret_stdproc_firecheck;
1268 self.turret_firefunc = turret_stdproc_fire;
1269 self.event_damage = turret_stdproc_damage;
1272 self.use = turret_stdproc_use;
1273 self.bot_attack = TRUE;
1276 self.nextthink = time + 1;
1277 self.nextthink += turret_count * sys_frametime;
1279 self.tur_head.team = self.team;
1280 self.view_ofs = '0 0 0';
1283 self.tur_dbg_start = self.nextthink;
1284 while (vlen(self.tur_dbg_rvec) < 2)
1285 self.tur_dbg_rvec = randomvec() * 4;
1287 self.tur_dbg_rvec_x = fabs(self.tur_dbg_rvec_x);
1288 self.tur_dbg_rvec_y = fabs(self.tur_dbg_rvec_y);
1289 self.tur_dbg_rvec_z = fabs(self.tur_dbg_rvec_z);
1293 self.turrcaps_flags |= TFL_TURRCAPS_ISTURRET;
1295 self.classname = "turret_main";
1297 self.tur_active = 1;
1299 // In ONS mode, and linked to a ONS ent. need to call the use to set team.
1300 if (g_onslaught && ee)
1306 Net_LinkEntity(self, TRUE, 0, turret_send);
1307 turret_stdproc_respawn();
1309 // Initiate the main AI loop
1310 /*self.nextthink = time;
1312 self.think = turret_link;
1314 self.think = turret_think;