1 #define cvar_base "g_turrets_unit_"
4 float turret_customizeentityforclient()
8 float Turret_SendEntity(entity to, float sf)
11 WriteByte(MSG_ENTITY, ENT_CLIENT_TURRET);
12 WriteCoord(MSG_ENTITY, self.tur_head.angles_x);
13 WriteCoord(MSG_ENTITY, self.tur_head.angles_y);
14 WriteByte(MSG_ENTITY, self.tur_head.frame);
16 //WriteCoord(MSG_ENTITY, self.tur_head.angles_z);
22 void load_unit_settings(entity ent,string unitname,float is_reload)
26 // dprint("Reloading turret ",e_turret.netname,"\n");
31 if not (ent.turret_scale_damage) ent.turret_scale_damage = 1;
32 if not (ent.turret_scale_range) ent.turret_scale_range = 1;
33 if not (ent.turret_scale_refire) ent.turret_scale_refire = 1;
34 if not (ent.turret_scale_ammo) ent.turret_scale_ammo = 1;
35 if not (ent.turret_scale_aim) ent.turret_scale_aim = 1;
36 if not (ent.turret_scale_health) ent.turret_scale_health = 1;
37 if not (ent.turret_scale_respawn) ent.turret_scale_respawn = 1;
39 sbase = strcat(cvar_base,unitname);
43 ent.tur_head.avelocity = '0 0 0';
45 if (ent.turrcaps_flags & TFL_TURRCAPS_HEADATTACHED)
46 ent.tur_head.angles = '0 0 0';
48 ent.tur_head.angles = ent.angles;
51 ent.health = cvar(strcat(sbase,"_health")) * ent.turret_scale_health;
52 ent.respawntime = cvar(strcat(sbase,"_respawntime")) * ent.turret_scale_respawn;
54 ent.shot_dmg = cvar(strcat(sbase,"_shot_dmg")) * ent.turret_scale_damage;
55 ent.shot_refire = cvar(strcat(sbase,"_shot_refire")) * ent.turret_scale_refire;
56 ent.shot_radius = cvar(strcat(sbase,"_shot_radius")) * ent.turret_scale_damage;
57 ent.shot_speed = cvar(strcat(sbase,"_shot_speed"));
58 ent.shot_spread = cvar(strcat(sbase,"_shot_spread"));
59 ent.shot_force = cvar(strcat(sbase,"_shot_force")) * ent.turret_scale_damage;
60 ent.shot_volly = cvar(strcat(sbase,"_shot_volly"));
61 ent.shot_volly_refire = cvar(strcat(sbase,"_shot_volly_refire")) * ent.turret_scale_refire;
63 ent.target_range = cvar(strcat(sbase,"_target_range")) * ent.turret_scale_range;
64 ent.target_range_min = cvar(strcat(sbase,"_target_range_min")) * ent.turret_scale_range;
65 //ent.target_range_fire = cvar(strcat(sbase,"_target_range_fire")) * ent.turret_scale_range;
66 ent.target_range_optimal = cvar(strcat(sbase,"_target_range_optimal")) * ent.turret_scale_range;
68 ent.target_select_rangebias = cvar(strcat(sbase,"_target_select_rangebias"));
69 ent.target_select_samebias = cvar(strcat(sbase,"_target_select_samebias"));
70 ent.target_select_anglebias = cvar(strcat(sbase,"_target_select_anglebias"));
71 ent.target_select_playerbias = cvar(strcat(sbase,"_target_select_playerbias"));
72 //ent.target_select_fov = cvar(cvar_gets(sbase,"_target_select_fov"));
74 ent.ammo_max = cvar(strcat(sbase,"_ammo_max")) * ent.turret_scale_ammo;
75 ent.ammo_recharge = cvar(strcat(sbase,"_ammo_recharge")) * ent.turret_scale_ammo;
77 ent.aim_firetolerance_dist = cvar(strcat(sbase,"_aim_firetolerance_dist"));
78 ent.aim_speed = cvar(strcat(sbase,"_aim_speed")) * ent.turret_scale_aim;
79 ent.aim_maxrot = cvar(strcat(sbase,"_aim_maxrot"));
80 ent.aim_maxpitch = cvar(strcat(sbase,"_aim_maxpitch"));
82 ent.track_type = cvar(strcat(sbase,"_track_type"));
83 ent.track_accel_pitch = cvar(strcat(sbase,"_track_accel_pitch"));
84 ent.track_accel_rot = cvar(strcat(sbase,"_track_accel_rot"));
85 ent.track_blendrate = cvar(strcat(sbase,"_track_blendrate"));
88 if(ent.turret_respawnhook)
89 ent.turret_respawnhook();
94 float turret_stdproc_true()
99 float turret_stdproc_false()
105 void turret_stdproc_nothing()
112 ** updates enemy distances, predicted impact point/time
113 ** and updated aim<->predict impact distance.
115 void turret_do_updates(entity t_turret)
117 vector enemy_pos,oldpos;
123 enemy_pos = real_origin(self.enemy);
125 turret_tag_fire_update();
127 self.tur_shotdir_updated = normalize(v_forward);
129 self.tur_dist_enemy = vlen(self.tur_shotorg - enemy_pos);
130 self.tur_dist_aimpos = vlen(self.tur_shotorg - self.tur_aimpos);
132 if((self.firecheck_flags & TFL_FIRECHECK_VERIFIED) && (self.enemy))
134 oldpos = self.enemy.origin;
135 setorigin(self.enemy,self.tur_aimpos);
136 tracebox(self.tur_shotorg, '-1 -1 -1','1 1 1',self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos),MOVE_NORMAL,self);
137 setorigin(self.enemy,oldpos);
139 if(trace_ent == self.enemy)
140 self.tur_dist_impact_to_aimpos = 0;
142 self.tur_dist_impact_to_aimpos = vlen(trace_endpos - self.tur_aimpos);// - (vlen(self.enemy.maxs - self.enemy.mins)*0.5);
144 self.tur_impactent = trace_ent;
145 self.tur_impacttime = vlen(self.tur_shotorg - trace_endpos) / self.shot_speed;
149 tracebox(self.tur_shotorg, '-1 -1 -1','1 1 1',self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos),MOVE_NORMAL,self);
150 //traceline(self.tur_shotorg, self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos),MOVE_NORMAL,self);
152 self.tur_dist_impact_to_aimpos = vlen(trace_endpos - self.tur_aimpos) - (vlen(self.enemy.maxs - self.enemy.mins)*0.5);
153 self.tur_impactent = trace_ent;
154 self.tur_impacttime = vlen(self.tur_shotorg - trace_endpos) / self.shot_speed;
161 vector turret_fovsearch_pingpong()
164 if(self.phase < time)
166 if( self.tur_head.phase )
167 self.tur_head.phase = 0;
169 self.tur_head.phase = 1;
170 self.phase = time + 5;
173 if( self.tur_head.phase)
174 wish_angle = self.idle_aim + '0 1 0' * (self.aim_maxrot * (self.target_select_fov / 360));
176 wish_angle = self.idle_aim - '0 1 0' * (self.aim_maxrot * (self.target_select_fov / 360));
181 vector turret_fovsearch_steprot()
186 wish_angle = self.tur_head.angles;
187 wish_angle_x = self.idle_aim_x;
189 if (self.phase < time)
191 //rot_add = self.aim_maxrot / self.target_select_fov;
192 wish_angle_y += (self.target_select_fov * 2);
194 if(wish_angle_y > 360)
195 wish_angle_y = wish_angle_y - 360;
197 self.phase = time + 1.5;
203 vector turret_fovsearch_random()
207 if (self.phase < time)
209 wish_angle_y = random() * self.aim_maxrot;
213 wish_angle_x = random() * self.aim_maxpitch;
217 self.phase = time + 5;
219 self.tur_aimpos = wish_angle;
222 return self.idle_aim + self.tur_aimpos;
227 ** Handles head rotation according to
228 ** the units .track_type and .track_flags
231 void turret_stdproc_track()
233 vector target_angle; // This is where we want to aim
234 vector move_angle; // This is where we can aim
238 if (self.track_flags == TFL_TRACK_NO)
241 if not (self.tur_active)
242 target_angle = self.idle_aim - ('1 0 0' * self.aim_maxpitch);
243 else if (self.enemy == world)
246 if (self.turrcaps_flags & TFL_TURRCAPS_HEADATTACHED)
247 target_angle = self.idle_aim + self.angles;
249 target_angle = self.idle_aim;
251 target_angle = vectoangles(normalize(self.tur_aimpos - self.tur_shotorg));
255 // Find the direction
256 target_angle = normalize(self.tur_aimpos - self.tur_shotorg);
257 target_angle = vectoangles(target_angle); // And make a angle
260 self.tur_head.angles_x = safeangle(self.tur_head.angles_x);
261 self.tur_head.angles_y = safeangle(self.tur_head.angles_y);
263 // Find the diffrence between where we currently aim and where we want to aim
264 if (self.turrcaps_flags & TFL_TURRCAPS_HEADATTACHED)
266 move_angle = target_angle - (self.angles + self.tur_head.angles);
267 move_angle = shortangle_vxy(move_angle,(self.angles + self.tur_head.angles));
273 move_angle = target_angle - self.tur_head.angles;
274 move_angle = shortangle_vxy(move_angle,self.tur_head.angles);
278 switch(self.track_type)
280 case TFL_TRACKTYPE_STEPMOTOR:
281 f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
282 if (self.track_flags & TFL_TRACK_PITCH)
284 self.tur_head.angles_x += bound(-f_tmp,move_angle_x, f_tmp);
285 if(self.tur_head.angles_x + a_off_x > self.aim_maxpitch)
286 self.tur_head.angles_x = a_off_x + self.aim_maxpitch;
288 if(self.tur_head.angles_x + a_off_x < -self.aim_maxpitch)
289 self.tur_head.angles_x = a_off_x - self.aim_maxpitch;
292 if (self.track_flags & TFL_TRACK_ROT)
294 self.tur_head.angles_y += bound(-f_tmp, move_angle_y, f_tmp);
295 if((self.tur_head.angles_y - a_off_y) > self.aim_maxrot)
296 self.tur_head.angles_y = a_off_y + self.aim_maxrot;
298 if((self.tur_head.angles_y - a_off_y) < -self.aim_maxrot)
299 self.tur_head.angles_y = a_off_y - self.aim_maxrot;
304 case TFL_TRACKTYPE_FLUIDINERTIA:
305 f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
306 move_angle_x = bound(-self.aim_speed, move_angle_x * self.track_accel_pitch * f_tmp,self.aim_speed);
307 move_angle_y = bound(-self.aim_speed, move_angle_y * self.track_accel_rot * f_tmp,self.aim_speed);
308 move_angle = (self.tur_head.avelocity * self.track_blendrate) + (move_angle * (1 - self.track_blendrate));
311 case TFL_TRACKTYPE_FLUIDPRECISE:
313 move_angle_y = bound(-self.aim_speed, move_angle_y, self.aim_speed);
314 move_angle_x = bound(-self.aim_speed, move_angle_x, self.aim_speed);
320 if (self.track_flags & TFL_TRACK_PITCH)
322 self.tur_head.avelocity_x = move_angle_x;
323 if((self.tur_head.angles_x + self.tur_head.avelocity_x * self.ticrate) + a_off_x > self.aim_maxpitch)
325 self.tur_head.avelocity_x = 0;
326 self.tur_head.angles_x = a_off_x + self.aim_maxpitch;
328 if((self.tur_head.angles_x + self.tur_head.avelocity_x * self.ticrate) + a_off_x < -self.aim_maxpitch)
330 self.tur_head.avelocity_x = 0;
331 self.tur_head.angles_x = a_off_x - self.aim_maxpitch;
337 if (self.track_flags & TFL_TRACK_ROT)
339 self.tur_head.avelocity_y = move_angle_y;
341 if(((self.tur_head.angles_y + self.tur_head.avelocity_y * self.ticrate)- a_off_y) > self.aim_maxrot)
343 self.tur_head.avelocity_y = 0;
344 self.tur_head.angles_y = a_off_y + self.aim_maxrot;
347 if(((self.tur_head.angles_y + self.tur_head.avelocity_y * self.ticrate) - a_off_y) < -self.aim_maxrot)
349 self.tur_head.avelocity_y = 0;
350 self.tur_head.angles_y = a_off_y - self.aim_maxrot;
363 + TFL_FIRECHECK_WORLD
365 + TFL_FIRECHECK_DISTANCES
367 + TFL_FIRECHECK_AIMDIST
368 + TFL_FIRECHECK_REALDIST
369 - TFL_FIRECHECK_ANGLEDIST
370 - TFL_FIRECHECK_TEAMCECK
372 + TFL_FIRECHECK_OWM_AMMO
373 + TFL_FIRECHECK_OTHER_AMMO
374 + TFL_FIRECHECK_REFIRE
378 ** Preforms pre-fire checks based on the uints firecheck_flags
380 float turret_stdproc_firecheck()
382 // This one just dont care =)
383 if (self.firecheck_flags & TFL_FIRECHECK_NO) return 1;
386 if (self.firecheck_flags & TFL_FIRECHECK_REFIRE)
387 if (self.attack_finished_single >= time) return 0;
389 // Special case: volly fire turret that has to fire a full volly if a shot was fired.
390 if (self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
391 if not (self.volly_counter == self.shot_volly)
394 // Lack of zombies makes shooting dead things unnecessary :P
395 if (self.firecheck_flags & TFL_FIRECHECK_DEAD)
396 if (self.enemy.deadflag != DEAD_NO)
399 // Plz stop killing the world!
400 if (self.firecheck_flags & TFL_FIRECHECK_WORLD)
401 if (self.enemy == world)
405 if (self.firecheck_flags & TFL_FIRECHECK_OWM_AMMO)
406 if (self.ammo < self.shot_dmg)
409 // Other's ammo? (support-supply units)
410 if (self.firecheck_flags & TFL_FIRECHECK_OTHER_AMMO)
411 if (self.enemy.ammo >= self.enemy.ammo_max)
414 if (self.firecheck_flags & TFL_FIRECHECK_DISTANCES)
416 // Not close enougth?
417 //if (self.tur_dist_aimpos > self.target_range_fire) return 0;
420 if (self.tur_dist_aimpos < self.target_range_min)
425 if (self.firecheck_flags & TFL_FIRECHECK_AFF)
426 if (self.tur_impactent.team == self.team)
429 // aim<->predicted impact
430 if (self.firecheck_flags & TFL_FIRECHECK_AIMDIST)
431 if (self.tur_dist_impact_to_aimpos > self.aim_firetolerance_dist)
434 //if (self.tur_impactent != self.enemy)
437 if (self.shot_volly > 1)
438 if (self.volly_counter == self.shot_volly)
439 if (self.ammo < (self.shot_dmg * self.shot_volly))
442 if(self.firecheck_flags & TFL_FIRECHECK_VERIFIED)
443 if(self.tur_impactent != self.enemy)
450 + TFL_TARGETSELECT_NO
451 + TFL_TARGETSELECT_LOS
452 + TFL_TARGETSELECT_PLAYERS
453 + TFL_TARGETSELECT_MISSILES
454 - TFL_TARGETSELECT_TRIGGERTARGET
455 + TFL_TARGETSELECT_ANGLELIMITS
456 + TFL_TARGETSELECT_RANGELIMTS
457 + TFL_TARGETSELECT_TEAMCHECK
458 - TFL_TARGETSELECT_NOBUILTIN
459 + TFL_TARGETSELECT_OWNTEAM
463 ** Evaluate a entity for target valitity based on validate_flags
464 ** NOTE: the caller must check takedamage before calling this, to inline this check.
466 float turret_validate_target(entity e_turret,entity e_target,float validate_flags)
470 //if(!validate_flags & TFL_TARGETSELECT_NOBUILTIN)
473 if(e_target.owner == e_turret)
476 if not(checkpvs(e_target.origin, e_turret))
483 if (substring(e_target.classname, 0, 10) == "onslaught_") // don't attack onslaught targets, that's the player's job!
486 if (validate_flags & TFL_TARGETSELECT_NO)
489 // If only this was used more..
490 if (e_target.flags & FL_NOTARGET)
494 if (e_target.health < 0)
498 if (e_target.flags & FL_CLIENT)
500 if not (validate_flags & TFL_TARGETSELECT_PLAYERS)
503 if (e_target.deadflag != DEAD_NO)
508 if (validate_flags & TFL_TARGETSELECT_NOTURRETS)
509 if (e_target.turret_firefunc || e_target.owner.tur_head == e_target)
510 if(e_target.team != e_turret.team) // Dont break support units.
514 if (e_target.flags & FL_PROJECTILE)
515 if not (validate_flags & TFL_TARGETSELECT_MISSILES)
518 if (validate_flags & TFL_TARGETSELECT_MISSILESONLY)
519 if not (e_target.flags & FL_PROJECTILE)
523 if (validate_flags & TFL_TARGETSELECT_TEAMCHECK)
525 if (validate_flags & TFL_TARGETSELECT_OWNTEAM)
527 if (e_target.team != e_turret.team)
530 if (e_turret.team != e_target.owner.team)
535 if (e_target.team == e_turret.team)
538 if (e_turret.team == e_target.owner.team)
544 tvt_dist = vlen(e_turret.origin - real_origin(e_target));
545 if (validate_flags & TFL_TARGETSELECT_RANGELIMTS)
547 if (tvt_dist < e_turret.target_range_min)
550 if (tvt_dist > e_turret.target_range)
554 // Can we even aim this thing?
555 if(e_turret.turrcaps_flags & TFL_TURRCAPS_HEADATTACHED)
557 tvt_thadv = angleofs3(e_turret.tur_head.origin,e_turret.angles + e_turret.tur_head.angles ,e_target);
558 //tvt_thadv = angleofs(e_turret.angles,e_target);
562 tvt_thadv = angleofs(e_turret.tur_head,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);
590 traceline(e_turret.tur_shotorg,v_tmp,0,e_turret);
592 if (e_turret.aim_firetolerance_dist < vlen(v_tmp - trace_endpos))
596 if (e_target.classname == "grapplinghook")
600 if (e_target.classname == "func_button")
604 #ifdef TURRET_DEBUG_TARGETSELECT
605 dprint("Target:",e_target.netname," is a valid target for ",e_turret.netname,"\n");
611 entity turret_select_target()
613 entity e; // target looper entity
614 float score; // target looper entity score
615 entity e_enemy; // currently best scoreing target
616 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?
633 if (!e) return world;
639 f = turret_validate_target(self,e,self.target_select_flags);
642 score = self.turret_score_target(self,e);
643 if ((score > m_score) && (score > 0))
660 self.nextthink = time + self.ticrate;
662 // ONS uses somewhat backwards linking.
668 e = find(world,targetname,self.target);
673 if (self.team != self.tur_head.team)
674 turret_stdproc_respawn();
678 if (self.tur_dbg_tmr1 < time)
680 if (self.enemy) paint_target (self.enemy,128,self.tur_dbg_rvec,0.9);
681 paint_target(self,256,self.tur_dbg_rvec,0.9);
682 self.tur_dbg_tmr1 = time + 1;
687 if not (self.spawnflags & TSF_NO_AMMO_REGEN)
688 if (self.ammo < self.ammo_max)
689 self.ammo = min(self.ammo + self.ammo_recharge,self.ammo_max);
692 // Inactive turrets needs to run the think loop,
693 // So they can handle animation and wake up if need be.
694 if not (self.tur_active)
696 turret_stdproc_track();
700 //This is just wrong :| and unlikely to ever happen.
702 if(self.deadflag != DEAD_NO)
704 dprint("WARNING: dead turret running the think function!\n");
709 // This is typicaly used for zaping every target in range
710 // turret_fusionreactor uses this to recharge friendlys.
711 if (self.shoot_flags & TFL_SHOOT_HITALLVALID)
713 // Do a self.turret_fire for every valid target.
714 e = findradius(self.origin,self.target_range);
719 if (turret_validate_target(self,e,self.target_validate_flags))
723 turret_do_updates(self);
725 if (self.turret_firecheckfunc())
734 else if(self.shoot_flags & TFL_SHOOT_CUSTOM)
736 // This one is doing something.. oddball. assume its handles what needs to be handled.
739 if not(self.aim_flags & TFL_AIM_NO)
740 self.tur_aimpos = turret_stdproc_aim_generic();
743 if not(self.track_flags & TFL_TRACK_NO)
744 turret_stdproc_track();
746 turret_do_updates(self);
749 if (self.turret_firecheckfunc())
754 // Special case for volly always. if it fired once it must compleate the volly.
755 if(self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
756 if(self.volly_counter != self.shot_volly)
758 // Predict or whatnot
759 if not(self.aim_flags & TFL_AIM_NO)
760 self.tur_aimpos = turret_stdproc_aim_generic();
763 if not(self.track_flags & TFL_TRACK_NO)
764 turret_stdproc_track();
766 turret_do_updates(self);
769 if (self.turret_firecheckfunc() != 0)
772 if(self.turret_postthink)
773 self.turret_postthink();
778 // Check if we have a vailid enemy, and try to find one if we dont.
780 // g_turrets_targetscan_maxdelay forces a target re-scan this often
781 float do_target_scan;
782 if((self.target_select_time + cvar("g_turrets_targetscan_maxdelay")) < time)
785 // Old target (if any) invalid?
786 if (turret_validate_target(self,self.enemy,self.target_validate_flags) <= 0)
789 // But never more often then g_turrets_targetscan_mindelay!
790 if (self.target_select_time + cvar("g_turrets_targetscan_mindelay") > time)
795 self.enemy = turret_select_target();
796 self.target_select_time = time;
799 // No target, just go to idle, do any custom stuff and bail.
800 if (self.enemy == world)
803 if not(self.track_flags & TFL_TRACK_NO)
804 turret_stdproc_track();
806 // do any per-turret stuff
807 if(self.turret_postthink)
808 self.turret_postthink();
814 self.lip = time + cvar("g_turrets_aimidle_delay"); // Keep track of the last time we had a target.
817 if not(self.aim_flags & TFL_AIM_NO)
818 self.tur_aimpos = turret_stdproc_aim_generic();
821 if not(self.track_flags & TFL_TRACK_NO)
822 turret_stdproc_track();
824 turret_do_updates(self);
827 if (self.turret_firecheckfunc())
831 // do any per-turret stuff
832 if(self.turret_postthink)
833 self.turret_postthink();
838 if (cvar("g_turrets_nofire") != 0)
842 // unlikely to ever happen.
843 if (self.deadflag != DEAD_NO)
846 if not (self.tur_active)
850 self.turret_firefunc();
852 self.attack_finished_single = time + self.shot_refire;
853 self.ammo -= self.shot_dmg;
854 self.volly_counter = self.volly_counter - 1;
856 if (self.volly_counter <= 0)
858 self.volly_counter = self.shot_volly;
860 if (self.shoot_flags & TFL_SHOOT_CLEARTARGET)
863 if (self.shot_volly > 1)
864 self.attack_finished_single = time + self.shot_volly_refire;
869 if (self.enemy) paint_target3(self.tur_aimpos, 64, self.tur_dbg_rvec, self.tur_impacttime + 0.25);
873 void turret_stdproc_fire()
875 dprint("^1Bang, ^3your dead^7 ",self.enemy.netname,"! ^1(turret with no real firefunc)\n");
879 When .used a turret switch team to activator.team.
880 If activator is world, the turrets goes inactive.
882 void turret_stdproc_use()
884 dprint("Turret ",self.netname, " used by ",activator.classname,"\n");
886 self.team = activator.team;
897 //Net_LinkEntity(self, FALSE, 0, Turret_SendEntity);
898 self.think = turret_think;
899 self.nextthink = time;
901 void turrets_manager_think()
903 self.nextthink = time + 1;
906 if (cvar("g_turrets_reloadcvars") == 1)
911 if (e.tur_head != world)
914 load_unit_settings(e,e.cvar_basename,1);
915 if(e.turret_postthink)
916 e.turret_postthink();
921 cvar_set("g_turrets_reloadcvars","0");
926 * Standard turret initialization. use this!
927 * (unless you have a very good reason not to)
928 * if the return value is 0, the turret should be removed.
930 float turret_stdproc_init (string cvar_base_name, float csqc_shared, string base, string head)
935 e = find(world, classname, "turret_manager");
939 e.classname = "turret_manager";
940 e.think = turrets_manager_think;
941 e.nextthink = time + 2;
947 dprint("turrets: csqc_shared requested but not implemented. expect strange things to happen.\n");
951 // Are turrets allowed atm?
952 if (cvar("g_turrets") == 0)
955 // Better more then once then never.
956 // turret_gibs_precash();
958 // Terrainbase spawnflag. This puts a enlongated model
959 // under the turret, so it looks ok on uneaven surfaces.
960 if (self.spawnflags & TSF_TERRAINBASE)
964 setmodel(tb,"models/turrets/terrainbase.md3");
965 setorigin(tb,self.origin);
966 tb.solid = SOLID_BBOX;
970 self.cvar_basename = cvar_base_name;
971 load_unit_settings(self,self.cvar_basename,0);
973 // Handle turret teams.
974 if (cvar("g_assult") != 0)
977 self.team = 14; // Assume turrets are on the defending side if not explicitly set otehrwize
979 else if not (teamplay)
980 self.team = MAX_SHOT_DISTANCE; // Group all turrets into the same team iso they dont kill eachother.
981 else if(g_onslaught && self.targetname)
983 e = find(world,target,self.targetname);
991 self.team = MAX_SHOT_DISTANCE; // Group all turrets into the same team iso they dont kill eachother.
996 * Try to guess some reasonaly defaults
997 * for missing params and do sanety checks
998 * thise checks could produce some "interesting" results
999 * if it hits a glitch in my logic :P so try to set as mutch
1000 * as possible beforehand.
1002 if (self.turrcaps_flags & TFL_TURRCAPS_SUPPORT)
1003 if (!self.ticrate) self.ticrate = 0.2; // Support units generaly dont need to have a high speed ai-loop
1005 if (!self.ticrate) self.ticrate = 0.1; // 10 fps for normal turrets
1007 self.ticrate = bound(sys_ticrate,self.ticrate,60); // keep it sane
1010 if (self.netname == "")
1011 self.netname = self.classname;
1013 if (!self.respawntime)
1014 self.respawntime = 60;
1015 self.respawntime = max(-1,self.respawntime);
1019 self.tur_health = max(1,self.health);
1021 if (!self.turrcaps_flags)
1022 self.turrcaps_flags = TFL_TURRCAPS_RADIUSDMG | TFL_TURRCAPS_MEDPROJ | TFL_TURRCAPS_PLAYERKILL;
1024 if (!self.damage_flags)
1025 self.damage_flags = TFL_DMG_YES | TFL_DMG_RETALIATE | TFL_DMG_AIMSHAKE;
1028 if (!self.shot_refire)
1029 self.shot_refire = 1;
1030 self.shot_refire = bound(0.01,self.shot_refire,9999);
1033 self.shot_dmg = self.shot_refire * 50;
1034 self.shot_dmg = max(1,self.shot_dmg);
1036 if (!self.shot_radius)
1037 self.shot_radius = self.shot_dmg * 0.5;
1038 self.shot_radius = max(1,self.shot_radius);
1040 if (!self.shot_speed)
1041 self.shot_speed = 2500;
1042 self.shot_speed = max(1,self.shot_speed);
1044 if (!self.shot_spread)
1045 self.shot_spread = 0.0125;
1046 self.shot_spread = bound(0.0001,self.shot_spread,500);
1048 if (!self.shot_force)
1049 self.shot_force = self.shot_dmg * 0.5 + self.shot_radius * 0.5;
1050 self.shot_force = bound(0.001,self.shot_force,MAX_SHOT_DISTANCE * 0.5);
1052 if (!self.shot_volly)
1053 self.shot_volly = 1;
1054 self.shot_volly = bound(1,self.shot_volly,floor(self.ammo_max / self.shot_dmg));
1056 if (!self.shot_volly_refire)
1057 self.shot_volly_refire = self.shot_refire * self.shot_volly;
1058 self.shot_volly_refire = bound(self.shot_refire,self.shot_volly_refire,60);
1060 if (!self.firecheck_flags)
1061 self.firecheck_flags = TFL_FIRECHECK_WORLD | TFL_FIRECHECK_DEAD | TFL_FIRECHECK_DISTANCES |
1062 TFL_FIRECHECK_LOS | TFL_FIRECHECK_AIMDIST | TFL_FIRECHECK_TEAMCECK |
1063 TFL_FIRECHECK_OWM_AMMO | TFL_FIRECHECK_REFIRE | TFL_FIRECHECK_WORLD;
1066 if (!self.target_range)
1067 self.target_range = self.shot_speed * 0.5;
1068 self.target_range = bound(0,self.target_range,MAX_SHOT_DISTANCE);
1070 if (!self.target_range_min)
1071 self.target_range_min = self.shot_radius * 2;
1072 self.target_range_min = bound(0,self.target_range_min,MAX_SHOT_DISTANCE);
1074 //if (!self.target_range_fire)
1075 // self.target_range_fire = self.target_range * 0.8;
1076 //self.target_range_fire = bound(0,self.target_range_fire,MAX_SHOT_DISTANCE);
1078 if (!self.target_range_optimal)
1079 self.target_range_optimal = self.target_range * 0.5;
1080 self.target_range_optimal = bound(0,self.target_range_optimal,MAX_SHOT_DISTANCE);
1084 if (!self.aim_maxrot)
1085 self.aim_maxrot = 90;
1086 self.aim_maxrot = bound(0,self.aim_maxrot,360);
1088 if (!self.aim_maxpitch)
1089 self.aim_maxpitch = 20;
1090 self.aim_maxpitch = bound(0,self.aim_maxpitch,90);
1092 if (!self.aim_speed)
1093 self.aim_speed = 36;
1094 self.aim_speed = bound(0.1,self.aim_speed, 1000);
1096 if (!self.aim_firetolerance_dist)
1097 self.aim_firetolerance_dist = 5 + (self.shot_radius * 2);
1098 self.aim_firetolerance_dist = bound(0.1,self.aim_firetolerance_dist,MAX_SHOT_DISTANCE);
1100 if (!self.aim_flags)
1102 self.aim_flags = TFL_AIM_LEAD | TFL_AIM_SHOTTIMECOMPENSATE;
1103 if(self.turrcaps_flags & TFL_TURRCAPS_RADIUSDMG)
1104 self.aim_flags |= TFL_AIM_GROUND2;
1107 // Sill the most tested (and aim-effective)
1108 if (!self.track_type) self.track_type = TFL_TRACKTYPE_STEPMOTOR;
1110 if (self.track_type != TFL_TRACKTYPE_STEPMOTOR)
1112 // Fluid / Ineria mode. Looks mutch nicer, bit experimental &
1113 // Can inmapt aim preformance alot.
1114 // needs a bit diffrent aimspeed
1116 if (!self.aim_speed)
1117 self.aim_speed = 180;
1118 self.aim_speed = bound(0.1,self.aim_speed, 1000);
1120 if (!self.track_accel_pitch)
1121 self.track_accel_pitch = 0.5;
1123 if (!self.track_accel_rot)
1124 self.track_accel_rot = 0.5;
1126 if (!self.track_blendrate)
1127 self.track_blendrate = 0.35;
1130 if (!self.track_flags)
1131 self.track_flags = TFL_TRACK_PITCH | TFL_TRACK_ROT;
1134 // Target selection stuff.
1135 if (!self.target_select_rangebias)
1136 self.target_select_rangebias = 1;
1137 self.target_select_rangebias = bound(-10,self.target_select_rangebias,10);
1139 if (!self.target_select_samebias)
1140 self.target_select_samebias = 1;
1141 self.target_select_samebias = bound(-10,self.target_select_samebias,10);
1143 if (!self.target_select_anglebias)
1144 self.target_select_anglebias = 1;
1145 self.target_select_anglebias = bound(-10,self.target_select_anglebias,10);
1147 if (!self.target_select_missilebias)
1148 self.target_select_missilebias = -10;
1150 self.target_select_missilebias = bound(-10,self.target_select_missilebias,10);
1151 self.target_select_playerbias = bound(-10,self.target_select_playerbias,10);
1153 if (!self.target_select_flags)
1155 self.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_TEAMCHECK
1156 | TFL_TARGETSELECT_RANGELIMTS | TFL_TARGETSELECT_ANGLELIMITS;
1158 if (self.turrcaps_flags & TFL_TURRCAPS_MISSILEKILL)
1159 self.target_select_flags |= TFL_TARGETSELECT_MISSILES;
1161 if (self.turrcaps_flags & TFL_TURRCAPS_PLAYERKILL)
1162 self.target_select_flags |= TFL_TARGETSELECT_PLAYERS;
1164 // self.target_select_flags = TFL_TARGETSELECT_NO;
1167 self.target_validate_flags = self.target_select_flags;
1172 self.ammo_max = self.shot_dmg * 10;
1173 self.ammo_max = max(self.shot_dmg,self.ammo_max);
1176 self.ammo = self.shot_dmg * 5;
1177 self.ammo = bound(0,self.ammo,self.ammo_max);
1179 if (!self.ammo_recharge)
1180 self.ammo_recharge = self.shot_dmg * 0.5;
1181 self.ammo_recharge = max(0,self.ammo_recharge);
1183 // Convert the recharge from X per sec to X per ticrate
1184 self.ammo_recharge = self.ammo_recharge * self.ticrate;
1186 if (!self.ammo_flags)
1187 self.ammo_flags = TFL_AMMO_ENERGY | TFL_AMMO_RECHARGE;
1190 if(self.spawnflags & TSL_NO_RESPAWN)
1191 if not (self.damage_flags & TFL_DMG_DEATH_NORESPAWN)
1192 self.damage_flags |= TFL_DMG_DEATH_NORESPAWN;
1194 // Offsets & origins
1195 if (!self.tur_shotorg) self.tur_shotorg = '50 0 50';
1197 // End of default & sanety checks, start building the turret.
1200 self.tur_head = spawn();
1201 self.tur_head.netname = self.tur_head.classname = "turret_head";
1202 self.tur_head.team = self.team;
1203 self.tur_head.owner = self;
1205 setmodel(self,base);
1206 setmodel(self.tur_head,head);
1208 setsize(self,'-32 -32 0','32 32 32');
1209 setsize(self.tur_head,'-32 -32 0','32 32 32');
1213 if(!self.tur_defend)
1214 if (self.target != "")
1216 self.tur_defend = find(world, targetname, self.target);
1217 if (self.tur_defend == world)
1220 dprint("Turret has invalid defendpoint!\n");
1224 // Put pices in place
1225 if not (self.turrcaps_flags & TFL_TURRCAPS_HEADATTACHED)
1226 setorigin(self.tur_head,self.origin);
1228 // In target defend mode, aim on the spot to defend when idle.
1229 if(self.turrcaps_flags & TFL_TURRCAPS_HEADATTACHED)
1231 if (self.tur_defend)
1232 self.idle_aim = self.tur_head.angles + angleofs(self.tur_head,self.tur_defend);
1234 self.idle_aim = '0 0 0';
1238 if (self.tur_defend)
1239 self.idle_aim = self.tur_head.angles + angleofs(self.tur_head,self.tur_defend);
1241 self.idle_aim = self.angles;
1244 if not (self.turrcaps_flags & TFL_TURRCAPS_HEADATTACHED)
1245 self.tur_head.angles = self.idle_aim;
1250 self.tur_health = self.health;
1251 self.tur_head.health = self.health;
1253 self.solid = SOLID_BBOX;
1254 self.tur_head.solid = SOLID_BBOX;
1256 self.takedamage = DAMAGE_AIM;
1257 self.tur_head.takedamage = DAMAGE_AIM;
1259 self.movetype = MOVETYPE_NOCLIP;
1260 self.tur_head.movetype = MOVETYPE_NOCLIP;
1263 if (self.team == COLOR_TEAM1) self.colormod = '1.4 0.8 0.8';
1264 if (self.team == COLOR_TEAM2) self.colormod = '0.8 0.8 1.4';
1266 // Attach stdprocs. override when and what needed
1267 if (self.turrcaps_flags & TFL_TURRCAPS_SUPPORT)
1269 self.turret_score_target = turret_stdproc_targetscore_support;
1270 self.turret_firecheckfunc = turret_stdproc_firecheck;
1271 self.turret_firefunc = turret_stdproc_fire;
1272 //self.turret_postthink = turret_stdproc_nothing;
1273 self.event_damage = turret_stdproc_damage;
1274 self.tur_head.event_damage = turret_stdproc_damage;
1278 self.turret_score_target = turret_stdproc_targetscore_generic;
1279 self.turret_firecheckfunc = turret_stdproc_firecheck;
1280 self.turret_firefunc = turret_stdproc_fire;
1281 //self.turret_postthink = turret_stdproc_nothing;
1282 self.event_damage = turret_stdproc_damage;
1283 self.tur_head.event_damage = turret_stdproc_damage;
1284 //self.turret_addtarget = turret_stdproc_false;
1287 self.use = turret_stdproc_use;
1288 self.bot_attack = TRUE;
1290 // Initiate the main AI loop
1292 self.think = turret_link;
1294 self.think = turret_think;
1297 self.nextthink = time + 1;
1298 self.nextthink += turret_count * sys_ticrate;
1300 self.tur_head.team = self.team;
1301 self.view_ofs = '0 0 0';
1304 self.tur_dbg_start = self.nextthink;
1305 while (vlen(self.tur_dbg_rvec) < 2)
1306 self.tur_dbg_rvec = randomvec() * 4;
1308 self.tur_dbg_rvec_x = fabs(self.tur_dbg_rvec_x);
1309 self.tur_dbg_rvec_y = fabs(self.tur_dbg_rvec_y);
1310 self.tur_dbg_rvec_z = fabs(self.tur_dbg_rvec_z);
1314 self.classname = "turret_main";
1316 self.tur_active = 1;
1318 // In ONS mode, and linked to a ONS ent. need to call the use to set team.
1319 if (g_onslaught && ee)