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 ent.tur_head.angles = '0 0 0';
48 ent.health = cvar(strcat(sbase,"_health")) * ent.turret_scale_health;
49 ent.respawntime = cvar(strcat(sbase,"_respawntime")) * ent.turret_scale_respawn;
51 ent.shot_dmg = cvar(strcat(sbase,"_shot_dmg")) * ent.turret_scale_damage;
52 ent.shot_refire = cvar(strcat(sbase,"_shot_refire")) * ent.turret_scale_refire;
53 ent.shot_radius = cvar(strcat(sbase,"_shot_radius")) * ent.turret_scale_damage;
54 ent.shot_speed = cvar(strcat(sbase,"_shot_speed"));
55 ent.shot_spread = cvar(strcat(sbase,"_shot_spread"));
56 ent.shot_force = cvar(strcat(sbase,"_shot_force")) * ent.turret_scale_damage;
57 ent.shot_volly = cvar(strcat(sbase,"_shot_volly"));
58 ent.shot_volly_refire = cvar(strcat(sbase,"_shot_volly_refire")) * ent.turret_scale_refire;
60 ent.target_range = cvar(strcat(sbase,"_target_range")) * ent.turret_scale_range;
61 ent.target_range_min = cvar(strcat(sbase,"_target_range_min")) * ent.turret_scale_range;
62 //ent.target_range_fire = cvar(strcat(sbase,"_target_range_fire")) * ent.turret_scale_range;
63 ent.target_range_optimal = cvar(strcat(sbase,"_target_range_optimal")) * ent.turret_scale_range;
65 ent.target_select_rangebias = cvar(strcat(sbase,"_target_select_rangebias"));
66 ent.target_select_samebias = cvar(strcat(sbase,"_target_select_samebias"));
67 ent.target_select_anglebias = cvar(strcat(sbase,"_target_select_anglebias"));
68 ent.target_select_playerbias = cvar(strcat(sbase,"_target_select_playerbias"));
69 //ent.target_select_fov = cvar(cvar_gets(sbase,"_target_select_fov"));
71 ent.ammo_max = cvar(strcat(sbase,"_ammo_max")) * ent.turret_scale_ammo;
72 ent.ammo_recharge = cvar(strcat(sbase,"_ammo_recharge")) * ent.turret_scale_ammo;
74 ent.aim_firetolerance_dist = cvar(strcat(sbase,"_aim_firetolerance_dist"));
75 ent.aim_speed = cvar(strcat(sbase,"_aim_speed")) * ent.turret_scale_aim;
76 ent.aim_maxrot = cvar(strcat(sbase,"_aim_maxrot"));
77 ent.aim_maxpitch = cvar(strcat(sbase,"_aim_maxpitch"));
79 ent.track_type = cvar(strcat(sbase,"_track_type"));
80 ent.track_accel_pitch = cvar(strcat(sbase,"_track_accel_pitch"));
81 ent.track_accel_rot = cvar(strcat(sbase,"_track_accel_rot"));
82 ent.track_blendrate = cvar(strcat(sbase,"_track_blendrate"));
85 if(ent.turret_respawnhook)
86 ent.turret_respawnhook();
91 float turret_stdproc_true()
96 float turret_stdproc_false()
102 void turret_stdproc_nothing()
109 ** updates enemy distances, predicted impact point/time
110 ** and updated aim<->predict impact distance.
112 void turret_do_updates(entity t_turret)
114 vector enemy_pos,oldpos;
120 enemy_pos = real_origin(self.enemy);
122 turret_tag_fire_update();
124 self.tur_shotdir_updated = normalize(v_forward);
126 self.tur_dist_enemy = vlen(self.tur_shotorg - enemy_pos);
127 self.tur_dist_aimpos = vlen(self.tur_shotorg - self.tur_aimpos);
129 if((self.firecheck_flags & TFL_FIRECHECK_VERIFIED) && (self.enemy))
131 oldpos = self.enemy.origin;
132 setorigin(self.enemy,self.tur_aimpos);
133 tracebox(self.tur_shotorg, '-1 -1 -1','1 1 1',self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos),MOVE_NORMAL,self);
134 setorigin(self.enemy,oldpos);
136 if(trace_ent == self.enemy)
137 self.tur_dist_impact_to_aimpos = 0;
139 self.tur_dist_impact_to_aimpos = vlen(trace_endpos - self.tur_aimpos);// - (vlen(self.enemy.maxs - self.enemy.mins)*0.5);
141 self.tur_impactent = trace_ent;
142 self.tur_impacttime = vlen(self.tur_shotorg - trace_endpos) / self.shot_speed;
146 tracebox(self.tur_shotorg, '-1 -1 -1','1 1 1',self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos),MOVE_NORMAL,self);
147 //traceline(self.tur_shotorg, self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos),MOVE_NORMAL,self);
149 self.tur_dist_impact_to_aimpos = vlen(trace_endpos - self.tur_aimpos) - (vlen(self.enemy.maxs - self.enemy.mins)*0.5);
150 self.tur_impactent = trace_ent;
151 self.tur_impacttime = vlen(self.tur_shotorg - trace_endpos) / self.shot_speed;
158 vector turret_fovsearch_pingpong()
161 if(self.phase < time)
163 if( self.tur_head.phase )
164 self.tur_head.phase = 0;
166 self.tur_head.phase = 1;
167 self.phase = time + 5;
170 if( self.tur_head.phase)
171 wish_angle = self.idle_aim + '0 1 0' * (self.aim_maxrot * (self.target_select_fov / 360));
173 wish_angle = self.idle_aim - '0 1 0' * (self.aim_maxrot * (self.target_select_fov / 360));
178 vector turret_fovsearch_steprot()
183 wish_angle = self.tur_head.angles;
184 wish_angle_x = self.idle_aim_x;
186 if (self.phase < time)
188 //rot_add = self.aim_maxrot / self.target_select_fov;
189 wish_angle_y += (self.target_select_fov * 2);
191 if(wish_angle_y > 360)
192 wish_angle_y = wish_angle_y - 360;
194 self.phase = time + 1.5;
200 vector turret_fovsearch_random()
204 if (self.phase < time)
206 wish_angle_y = random() * self.aim_maxrot;
210 wish_angle_x = random() * self.aim_maxpitch;
214 self.phase = time + 5;
216 self.tur_aimpos = wish_angle;
219 return self.idle_aim + self.tur_aimpos;
224 ** Handles head rotation according to
225 ** the units .track_type and .track_flags
228 void turret_stdproc_track()
230 vector target_angle; // This is where we want to aim
231 vector move_angle; // This is where we can aim
234 if (self.track_flags == TFL_TRACK_NO)
237 if not (self.tur_active)
238 target_angle = self.idle_aim - ('1 0 0' * self.aim_maxpitch);
239 else if (self.enemy == world)
242 target_angle = self.idle_aim + self.angles;
244 target_angle = vectoangles(normalize(self.tur_aimpos - self.tur_shotorg));
248 // Find the direction
249 target_angle = normalize(self.tur_aimpos - self.tur_shotorg);
250 target_angle = vectoangles(target_angle); // And make a angle
253 self.tur_head.angles_x = safeangle(self.tur_head.angles_x);
254 self.tur_head.angles_y = safeangle(self.tur_head.angles_y);
256 // Find the diffrence between where we currently aim and where we want to aim
257 move_angle = target_angle - (self.angles + self.tur_head.angles);
258 move_angle = shortangle_vxy(move_angle,(self.angles + self.tur_head.angles));
263 switch(self.track_type)
265 case TFL_TRACKTYPE_STEPMOTOR:
266 f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
267 if (self.track_flags & TFL_TRACK_PITCH)
269 self.tur_head.angles_x += bound(-f_tmp,move_angle_x, f_tmp);
270 if(self.tur_head.angles_x > self.aim_maxpitch)
271 self.tur_head.angles_x = self.aim_maxpitch;
273 if(self.tur_head.angles_x < -self.aim_maxpitch)
274 self.tur_head.angles_x = self.aim_maxpitch;
277 if (self.track_flags & TFL_TRACK_ROT)
279 self.tur_head.angles_y += bound(-f_tmp, move_angle_y, f_tmp);
280 if(self.tur_head.angles_y > self.aim_maxrot)
281 self.tur_head.angles_y = self.aim_maxrot;
283 if(self.tur_head.angles_y < -self.aim_maxrot)
284 self.tur_head.angles_y = self.aim_maxrot;
289 case TFL_TRACKTYPE_FLUIDINERTIA:
290 f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
291 move_angle_x = bound(-self.aim_speed, move_angle_x * self.track_accel_pitch * f_tmp,self.aim_speed);
292 move_angle_y = bound(-self.aim_speed, move_angle_y * self.track_accel_rot * f_tmp,self.aim_speed);
293 move_angle = (self.tur_head.avelocity * self.track_blendrate) + (move_angle * (1 - self.track_blendrate));
296 case TFL_TRACKTYPE_FLUIDPRECISE:
298 move_angle_y = bound(-self.aim_speed, move_angle_y, self.aim_speed);
299 move_angle_x = bound(-self.aim_speed, move_angle_x, self.aim_speed);
305 if (self.track_flags & TFL_TRACK_PITCH)
307 self.tur_head.avelocity_x = move_angle_x;
308 if((self.tur_head.angles_x + self.tur_head.avelocity_x * self.ticrate) > self.aim_maxpitch)
310 self.tur_head.avelocity_x = 0;
311 self.tur_head.angles_x = self.aim_maxpitch;
313 if((self.tur_head.angles_x + self.tur_head.avelocity_x * self.ticrate) < -self.aim_maxpitch)
315 self.tur_head.avelocity_x = 0;
316 self.tur_head.angles_x = self.aim_maxpitch;
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 if((self.tur_head.angles_y + self.tur_head.avelocity_y * self.ticrate) < -self.aim_maxrot)
334 self.tur_head.avelocity_y = 0;
335 self.tur_head.angles_y = self.aim_maxrot;
348 + TFL_FIRECHECK_WORLD
350 + TFL_FIRECHECK_DISTANCES
352 + TFL_FIRECHECK_AIMDIST
353 + TFL_FIRECHECK_REALDIST
354 - TFL_FIRECHECK_ANGLEDIST
355 - TFL_FIRECHECK_TEAMCECK
357 + TFL_FIRECHECK_OWM_AMMO
358 + TFL_FIRECHECK_OTHER_AMMO
359 + TFL_FIRECHECK_REFIRE
363 ** Preforms pre-fire checks based on the uints firecheck_flags
365 float turret_stdproc_firecheck()
367 // This one just dont care =)
368 if (self.firecheck_flags & TFL_FIRECHECK_NO) return 1;
371 if (self.firecheck_flags & TFL_FIRECHECK_REFIRE)
372 if (self.attack_finished_single >= time) return 0;
374 // Special case: volly fire turret that has to fire a full volly if a shot was fired.
375 if (self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
376 if not (self.volly_counter == self.shot_volly)
379 // Lack of zombies makes shooting dead things unnecessary :P
380 if (self.firecheck_flags & TFL_FIRECHECK_DEAD)
381 if (self.enemy.deadflag != DEAD_NO)
384 // Plz stop killing the world!
385 if (self.firecheck_flags & TFL_FIRECHECK_WORLD)
386 if (self.enemy == world)
390 if (self.firecheck_flags & TFL_FIRECHECK_OWM_AMMO)
391 if (self.ammo < self.shot_dmg)
394 // Other's ammo? (support-supply units)
395 if (self.firecheck_flags & TFL_FIRECHECK_OTHER_AMMO)
396 if (self.enemy.ammo >= self.enemy.ammo_max)
399 if (self.firecheck_flags & TFL_FIRECHECK_DISTANCES)
401 // Not close enougth?
402 //if (self.tur_dist_aimpos > self.target_range_fire) return 0;
405 if (self.tur_dist_aimpos < self.target_range_min)
410 if (self.firecheck_flags & TFL_FIRECHECK_AFF)
411 if (self.tur_impactent.team == self.team)
414 // aim<->predicted impact
415 if (self.firecheck_flags & TFL_FIRECHECK_AIMDIST)
416 if (self.tur_dist_impact_to_aimpos > self.aim_firetolerance_dist)
419 //if (self.tur_impactent != self.enemy)
422 if (self.shot_volly > 1)
423 if (self.volly_counter == self.shot_volly)
424 if (self.ammo < (self.shot_dmg * self.shot_volly))
427 if(self.firecheck_flags & TFL_FIRECHECK_VERIFIED)
428 if(self.tur_impactent != self.enemy)
435 + TFL_TARGETSELECT_NO
436 + TFL_TARGETSELECT_LOS
437 + TFL_TARGETSELECT_PLAYERS
438 + TFL_TARGETSELECT_MISSILES
439 - TFL_TARGETSELECT_TRIGGERTARGET
440 + TFL_TARGETSELECT_ANGLELIMITS
441 + TFL_TARGETSELECT_RANGELIMTS
442 + TFL_TARGETSELECT_TEAMCHECK
443 - TFL_TARGETSELECT_NOBUILTIN
444 + TFL_TARGETSELECT_OWNTEAM
448 ** Evaluate a entity for target valitity based on validate_flags
449 ** NOTE: the caller must check takedamage before calling this, to inline this check.
451 float turret_validate_target(entity e_turret,entity e_target,float validate_flags)
455 //if(!validate_flags & TFL_TARGETSELECT_NOBUILTIN)
458 if(e_target.owner == e_turret)
461 if not(checkpvs(e_target.origin, e_turret))
468 if (substring(e_target.classname, 0, 10) == "onslaught_") // don't attack onslaught targets, that's the player's job!
471 if (validate_flags & TFL_TARGETSELECT_NO)
474 // If only this was used more..
475 if (e_target.flags & FL_NOTARGET)
479 if (e_target.health < 0)
483 if (e_target.flags & FL_CLIENT)
485 if not (validate_flags & TFL_TARGETSELECT_PLAYERS)
488 if (e_target.deadflag != DEAD_NO)
493 if (validate_flags & TFL_TARGETSELECT_NOTURRETS)
494 if (e_target.turret_firefunc || e_target.owner.tur_head == e_target)
495 if(e_target.team != e_turret.team) // Dont break support units.
499 if (e_target.flags & FL_PROJECTILE)
500 if not (validate_flags & TFL_TARGETSELECT_MISSILES)
503 if (validate_flags & TFL_TARGETSELECT_MISSILESONLY)
504 if not (e_target.flags & FL_PROJECTILE)
508 if (validate_flags & TFL_TARGETSELECT_TEAMCHECK)
510 if (validate_flags & TFL_TARGETSELECT_OWNTEAM)
512 if (e_target.team != e_turret.team)
515 if (e_turret.team != e_target.owner.team)
520 if (e_target.team == e_turret.team)
523 if (e_turret.team == e_target.owner.team)
529 tvt_dist = vlen(e_turret.origin - real_origin(e_target));
530 if (validate_flags & TFL_TARGETSELECT_RANGELIMTS)
532 if (tvt_dist < e_turret.target_range_min)
535 if (tvt_dist > e_turret.target_range)
539 // Can we even aim this thing?
540 tvt_thadv = angleofs3(e_turret.tur_head.origin,e_turret.angles + e_turret.tur_head.angles ,e_target);
541 //tvt_thadv = angleofs(e_turret.angles,e_target);
545 tvt_tadv = shortangle_vxy(angleofs(e_turret,e_target),e_turret.angles);
546 tvt_thadf = vlen(tvt_thadv);
547 tvt_tadf = vlen(tvt_tadv);
550 if(validate_flags & TFL_TARGETSELECT_FOV)
552 if(e_turret.target_select_fov < tvt_thadf)
557 if (validate_flags & TFL_TARGETSELECT_ANGLELIMITS)
559 if (fabs(tvt_tadv_x) > e_turret.aim_maxpitch)
562 if (fabs(tvt_tadv_y) > e_turret.aim_maxrot)
567 if (validate_flags & TFL_TARGETSELECT_LOS)
569 v_tmp = real_origin(e_target) + ((e_target.mins + e_target.maxs) * 0.5);
571 traceline(e_turret.tur_shotorg, v_tmp, 0, e_turret);
573 if (e_turret.aim_firetolerance_dist < vlen(v_tmp - trace_endpos))
577 if (e_target.classname == "grapplinghook")
581 if (e_target.classname == "func_button")
585 #ifdef TURRET_DEBUG_TARGETSELECT
586 dprint("Target:",e_target.netname," is a valid target for ",e_turret.netname,"\n");
592 entity turret_select_target()
594 entity e; // target looper entity
595 float score; // target looper entity score
596 entity e_enemy; // currently best scoreing target
597 float m_score; // currently best scoreing target's score
602 if(self.enemy.takedamage)
603 if(turret_validate_target(self,self.enemy,self.target_validate_flags) > 0)
605 e_enemy = self.enemy;
606 m_score = self.turret_score_target(self,e_enemy) * self.target_select_samebias;
611 e = findradius(self.origin,self.target_range);
613 // Nothing to aim at?
614 if (!e) return world;
620 f = turret_validate_target(self,e,self.target_select_flags);
623 score = self.turret_score_target(self,e);
624 if ((score > m_score) && (score > 0))
641 self.nextthink = time + self.ticrate;
643 // ONS uses somewhat backwards linking.
649 e = find(world,targetname,self.target);
654 if (self.team != self.tur_head.team)
655 turret_stdproc_respawn();
659 if (self.tur_dbg_tmr1 < time)
661 if (self.enemy) paint_target (self.enemy,128,self.tur_dbg_rvec,0.9);
662 paint_target(self,256,self.tur_dbg_rvec,0.9);
663 self.tur_dbg_tmr1 = time + 1;
668 if not (self.spawnflags & TSF_NO_AMMO_REGEN)
669 if (self.ammo < self.ammo_max)
670 self.ammo = min(self.ammo + self.ammo_recharge,self.ammo_max);
673 // Inactive turrets needs to run the think loop,
674 // So they can handle animation and wake up if need be.
675 if not (self.tur_active)
677 turret_stdproc_track();
681 //This is just wrong :| and unlikely to ever happen.
683 if(self.deadflag != DEAD_NO)
685 dprint("WARNING: dead turret running the think function!\n");
690 // This is typicaly used for zaping every target in range
691 // turret_fusionreactor uses this to recharge friendlys.
692 if (self.shoot_flags & TFL_SHOOT_HITALLVALID)
694 // Do a self.turret_fire for every valid target.
695 e = findradius(self.origin,self.target_range);
700 if (turret_validate_target(self,e,self.target_validate_flags))
704 turret_do_updates(self);
706 if (self.turret_firecheckfunc())
715 else if(self.shoot_flags & TFL_SHOOT_CUSTOM)
717 // This one is doing something.. oddball. assume its handles what needs to be handled.
720 if not(self.aim_flags & TFL_AIM_NO)
721 self.tur_aimpos = turret_stdproc_aim_generic();
724 if not(self.track_flags & TFL_TRACK_NO)
725 turret_stdproc_track();
727 turret_do_updates(self);
730 if (self.turret_firecheckfunc())
735 // Special case for volly always. if it fired once it must compleate the volly.
736 if(self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
737 if(self.volly_counter != self.shot_volly)
739 // Predict or whatnot
740 if not(self.aim_flags & TFL_AIM_NO)
741 self.tur_aimpos = turret_stdproc_aim_generic();
744 if not(self.track_flags & TFL_TRACK_NO)
745 turret_stdproc_track();
747 turret_do_updates(self);
750 if (self.turret_firecheckfunc() != 0)
753 if(self.turret_postthink)
754 self.turret_postthink();
759 // Check if we have a vailid enemy, and try to find one if we dont.
761 // g_turrets_targetscan_maxdelay forces a target re-scan this often
762 float do_target_scan;
763 if((self.target_select_time + cvar("g_turrets_targetscan_maxdelay")) < time)
766 // Old target (if any) invalid?
767 if (turret_validate_target(self,self.enemy,self.target_validate_flags) <= 0)
770 // But never more often then g_turrets_targetscan_mindelay!
771 if (self.target_select_time + cvar("g_turrets_targetscan_mindelay") > time)
776 self.enemy = turret_select_target();
777 self.target_select_time = time;
780 // No target, just go to idle, do any custom stuff and bail.
781 if (self.enemy == world)
784 if not(self.track_flags & TFL_TRACK_NO)
785 turret_stdproc_track();
787 // do any per-turret stuff
788 if(self.turret_postthink)
789 self.turret_postthink();
795 self.lip = time + cvar("g_turrets_aimidle_delay"); // Keep track of the last time we had a target.
798 if not(self.aim_flags & TFL_AIM_NO)
799 self.tur_aimpos = turret_stdproc_aim_generic();
802 if not(self.track_flags & TFL_TRACK_NO)
803 turret_stdproc_track();
805 turret_do_updates(self);
808 if (self.turret_firecheckfunc())
812 // do any per-turret stuff
813 if(self.turret_postthink)
814 self.turret_postthink();
819 if (cvar("g_turrets_nofire") != 0)
823 // unlikely to ever happen.
824 if (self.deadflag != DEAD_NO)
827 if not (self.tur_active)
831 self.turret_firefunc();
833 self.attack_finished_single = time + self.shot_refire;
834 self.ammo -= self.shot_dmg;
835 self.volly_counter = self.volly_counter - 1;
837 if (self.volly_counter <= 0)
839 self.volly_counter = self.shot_volly;
841 if (self.shoot_flags & TFL_SHOOT_CLEARTARGET)
844 if (self.shot_volly > 1)
845 self.attack_finished_single = time + self.shot_volly_refire;
849 if (self.enemy) paint_target3(self.tur_aimpos, 64, self.tur_dbg_rvec, self.tur_impacttime + 0.25);
853 void turret_stdproc_fire()
855 dprint("^1Bang, ^3your dead^7 ",self.enemy.netname,"! ^1(turret with no real firefunc)\n");
859 When .used a turret switch team to activator.team.
860 If activator is world, the turrets goes inactive.
862 void turret_stdproc_use()
864 dprint("Turret ",self.netname, " used by ",activator.classname,"\n");
866 self.team = activator.team;
877 //Net_LinkEntity(self, FALSE, 0, Turret_SendEntity);
878 self.think = turret_think;
879 self.nextthink = time;
882 void turrets_manager_think()
884 self.nextthink = time + 1;
887 if (cvar("g_turrets_reloadcvars") == 1)
892 if (e.turrcaps_flags & TFL_TURRCAPS_ISTURRET)
894 load_unit_settings(e,e.cvar_basename,1);
895 if(e.turret_postthink)
896 e.turret_postthink();
901 cvar_set("g_turrets_reloadcvars","0");
906 * Standard turret initialization. use this!
907 * (unless you have a very good reason not to)
908 * if the return value is 0, the turret should be removed.
910 float turret_stdproc_init (string cvar_base_name, float csqc_shared, string base, string head)
914 // Are turrets allowed atm?
915 if (cvar("g_turrets") == 0)
919 e = find(world, classname, "turret_manager");
924 setorigin(e,'0 0 0');
925 setmodel(e,"models/turrets/plasma.md3");
927 v = gettaginfo(e,gettagindex(e,"tag_fire"));
930 //objerror("^1ERROR: Engine is borken! Turrets will NOT work. force g_turrets to 0 to run maps with turrets anyway.");
935 e.classname = "turret_manager";
936 e.think = turrets_manager_think;
937 e.nextthink = time + 2;
942 dprint("WARNING: turret requested csqc_shared but this is not implemented. Expect strange things to happen.\n");
946 if not (self.spawnflags & TSF_SUSPENDED)
947 droptofloor_builtin();
949 // Terrainbase spawnflag. This puts a enlongated model
950 // under the turret, so it looks ok on uneaven surfaces.
951 if (self.spawnflags & TSF_TERRAINBASE)
955 setmodel(tb,"models/turrets/terrainbase.md3");
956 setorigin(tb,self.origin);
957 tb.solid = SOLID_BBOX;
960 self.cvar_basename = cvar_base_name;
961 load_unit_settings(self,self.cvar_basename, 0);
963 // Handle turret teams.
964 if (cvar("g_assault") != 0)
967 self.team = 14; // Assume turrets are on the defending side if not explicitly set otehrwize
969 else if not (teamplay)
970 self.team = MAX_SHOT_DISTANCE; // Group all turrets into the same team iso they dont kill eachother.
971 else if(g_onslaught && self.targetname)
973 e = find(world,target,self.targetname);
981 self.team = MAX_SHOT_DISTANCE; // Group all turrets into the same team iso they dont kill eachother.
984 * Try to guess some reasonaly defaults
985 * for missing params and do sanety checks
986 * thise checks could produce some "interesting" results
987 * if it hits a glitch in my logic :P so try to set as mutch
988 * as possible beforehand.
990 if (self.turrcaps_flags & TFL_TURRCAPS_SUPPORT)
991 if not (self.ticrate) self.ticrate = 0.2; // Support units generaly dont need to have a high speed ai-loop
993 if not (self.ticrate) self.ticrate = 0.1; // 10 fps for normal turrets
995 self.ticrate = bound(sys_frametime,self.ticrate,60); // keep it sane
998 if (self.netname == "")
999 self.netname = self.classname;
1001 if not (self.respawntime)
1002 self.respawntime = 60;
1003 self.respawntime = max(-1,self.respawntime);
1005 if not (self.health)
1007 self.tur_health = max(1,self.health);
1009 if not (self.turrcaps_flags)
1010 self.turrcaps_flags = TFL_TURRCAPS_RADIUSDMG | TFL_TURRCAPS_MEDPROJ | TFL_TURRCAPS_PLAYERKILL;
1012 if (!self.damage_flags)
1013 self.damage_flags = TFL_DMG_YES | TFL_DMG_RETALIATE | TFL_DMG_AIMSHAKE;
1016 if not (self.shot_refire)
1017 self.shot_refire = 1;
1018 self.shot_refire = bound(0.01,self.shot_refire,9999);
1020 if not (self.shot_dmg)
1021 self.shot_dmg = self.shot_refire * 50;
1022 self.shot_dmg = max(1,self.shot_dmg);
1024 if not (self.shot_radius)
1025 self.shot_radius = self.shot_dmg * 0.5;
1026 self.shot_radius = max(1,self.shot_radius);
1028 if not (self.shot_speed)
1029 self.shot_speed = 2500;
1030 self.shot_speed = max(1,self.shot_speed);
1032 if not (self.shot_spread)
1033 self.shot_spread = 0.0125;
1034 self.shot_spread = bound(0.0001,self.shot_spread,500);
1036 if not (self.shot_force)
1037 self.shot_force = self.shot_dmg * 0.5 + self.shot_radius * 0.5;
1038 self.shot_force = bound(0.001,self.shot_force,MAX_SHOT_DISTANCE * 0.5);
1040 if not (self.shot_volly)
1041 self.shot_volly = 1;
1042 self.shot_volly = bound(1,self.shot_volly,floor(self.ammo_max / self.shot_dmg));
1044 if not (self.shot_volly_refire)
1045 self.shot_volly_refire = self.shot_refire * self.shot_volly;
1046 self.shot_volly_refire = bound(self.shot_refire,self.shot_volly_refire,60);
1048 if not (self.firecheck_flags)
1049 self.firecheck_flags = TFL_FIRECHECK_WORLD | TFL_FIRECHECK_DEAD | TFL_FIRECHECK_DISTANCES |
1050 TFL_FIRECHECK_LOS | TFL_FIRECHECK_AIMDIST | TFL_FIRECHECK_TEAMCECK |
1051 TFL_FIRECHECK_OWM_AMMO | TFL_FIRECHECK_REFIRE | TFL_FIRECHECK_WORLD;
1054 if not (self.target_range)
1055 self.target_range = self.shot_speed * 0.5;
1056 self.target_range = bound(0,self.target_range,MAX_SHOT_DISTANCE);
1058 if not (self.target_range_min)
1059 self.target_range_min = self.shot_radius * 2;
1060 self.target_range_min = bound(0,self.target_range_min,MAX_SHOT_DISTANCE);
1062 //if (!self.target_range_fire)
1063 // self.target_range_fire = self.target_range * 0.8;
1064 //self.target_range_fire = bound(0,self.target_range_fire,MAX_SHOT_DISTANCE);
1066 if not (self.target_range_optimal)
1067 self.target_range_optimal = self.target_range * 0.5;
1068 self.target_range_optimal = bound(0,self.target_range_optimal,MAX_SHOT_DISTANCE);
1072 if not (self.aim_maxrot)
1073 self.aim_maxrot = 90;
1074 self.aim_maxrot = bound(0,self.aim_maxrot,360);
1076 if not (self.aim_maxpitch)
1077 self.aim_maxpitch = 20;
1078 self.aim_maxpitch = bound(0,self.aim_maxpitch,90);
1080 if not (self.aim_speed)
1081 self.aim_speed = 36;
1082 self.aim_speed = bound(0.1,self.aim_speed, 1000);
1084 if not (self.aim_firetolerance_dist)
1085 self.aim_firetolerance_dist = 5 + (self.shot_radius * 2);
1086 self.aim_firetolerance_dist = bound(0.1,self.aim_firetolerance_dist,MAX_SHOT_DISTANCE);
1088 if not (self.aim_flags)
1090 self.aim_flags = TFL_AIM_LEAD | TFL_AIM_SHOTTIMECOMPENSATE;
1091 if(self.turrcaps_flags & TFL_TURRCAPS_RADIUSDMG)
1092 self.aim_flags |= TFL_AIM_GROUND2;
1095 // Sill the most tested (and aim-effective)
1096 if not (self.track_type)
1097 self.track_type = TFL_TRACKTYPE_STEPMOTOR;
1099 if (self.track_type != TFL_TRACKTYPE_STEPMOTOR)
1101 // Fluid / Ineria mode. Looks mutch nicer, bit experimental &
1102 // Can inmapt aim preformance alot.
1103 // needs a bit diffrent aimspeed
1105 if not (self.aim_speed)
1106 self.aim_speed = 180;
1107 self.aim_speed = bound(0.1,self.aim_speed, 1000);
1109 if not (self.track_accel_pitch)
1110 self.track_accel_pitch = 0.5;
1112 if not (self.track_accel_rot)
1113 self.track_accel_rot = 0.5;
1115 if not (self.track_blendrate)
1116 self.track_blendrate = 0.35;
1119 if (!self.track_flags)
1120 self.track_flags = TFL_TRACK_PITCH | TFL_TRACK_ROT;
1123 // Target selection stuff.
1124 if not (self.target_select_rangebias)
1125 self.target_select_rangebias = 1;
1126 self.target_select_rangebias = bound(-10,self.target_select_rangebias,10);
1128 if not (self.target_select_samebias)
1129 self.target_select_samebias = 1;
1130 self.target_select_samebias = bound(-10,self.target_select_samebias,10);
1132 if not (self.target_select_anglebias)
1133 self.target_select_anglebias = 1;
1134 self.target_select_anglebias = bound(-10,self.target_select_anglebias,10);
1136 if not (self.target_select_missilebias)
1137 self.target_select_missilebias = -10;
1139 self.target_select_missilebias = bound(-10,self.target_select_missilebias,10);
1140 self.target_select_playerbias = bound(-10,self.target_select_playerbias,10);
1142 if not (self.target_select_flags)
1144 self.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_TEAMCHECK
1145 | TFL_TARGETSELECT_RANGELIMTS | TFL_TARGETSELECT_ANGLELIMITS;
1147 if (self.turrcaps_flags & TFL_TURRCAPS_MISSILEKILL)
1148 self.target_select_flags |= TFL_TARGETSELECT_MISSILES;
1150 if (self.turrcaps_flags & TFL_TURRCAPS_PLAYERKILL)
1151 self.target_select_flags |= TFL_TARGETSELECT_PLAYERS;
1153 // self.target_select_flags = TFL_TARGETSELECT_NO;
1156 self.target_validate_flags = self.target_select_flags;
1160 if not (self.ammo_max)
1161 self.ammo_max = self.shot_dmg * 10;
1162 self.ammo_max = max(self.shot_dmg,self.ammo_max);
1165 self.ammo = self.shot_dmg * 5;
1166 self.ammo = bound(0,self.ammo,self.ammo_max);
1168 if not (self.ammo_recharge)
1169 self.ammo_recharge = self.shot_dmg * 0.5;
1170 self.ammo_recharge = max(0,self.ammo_recharge);
1172 // Convert the recharge from X per sec to X per ticrate
1173 self.ammo_recharge = self.ammo_recharge * self.ticrate;
1175 if not (self.ammo_flags)
1176 self.ammo_flags = TFL_AMMO_ENERGY | TFL_AMMO_RECHARGE;
1179 if(self.spawnflags & TSL_NO_RESPAWN)
1180 if not (self.damage_flags & TFL_DMG_DEATH_NORESPAWN)
1181 self.damage_flags |= TFL_DMG_DEATH_NORESPAWN;
1183 // Offsets & origins
1184 if (!self.tur_shotorg) self.tur_shotorg = '50 0 50';
1186 // End of default & sanety checks, start building the turret.
1189 self.tur_head = spawn();
1190 self.tur_head.netname = self.tur_head.classname = "turret_head";
1191 self.tur_head.team = self.team;
1192 self.tur_head.owner = self;
1194 setmodel(self,base);
1195 setmodel(self.tur_head,head);
1197 setsize(self,'-32 -32 0','32 32 64');
1198 setsize(self.tur_head,'0 0 0','0 0 0');
1200 setorigin(self.tur_head,'0 0 0');
1201 setattachment(self.tur_head, self, "tag_head");
1206 self.tur_health = self.health;
1207 self.solid = SOLID_BBOX;
1208 self.tur_head.solid = SOLID_NOT;
1209 self.takedamage = DAMAGE_AIM;
1210 self.tur_head.takedamage = DAMAGE_NO;
1211 self.movetype = MOVETYPE_NOCLIP;
1212 self.tur_head.movetype = MOVETYPE_NOCLIP;
1215 if not (self.tur_defend)
1216 if (self.target != "")
1218 self.tur_defend = find(world, targetname, self.target);
1219 if (self.tur_defend == world)
1222 dprint("Turret has invalid defendpoint!\n");
1226 // In target defend mode, aim on the spot to defend when idle.
1227 if (self.tur_defend)
1228 self.idle_aim = self.tur_head.angles + angleofs(self.tur_head,self.tur_defend);
1230 self.idle_aim = '0 0 0';
1233 if (self.team == COLOR_TEAM1) self.colormod = '1.4 0.8 0.8';
1234 if (self.team == COLOR_TEAM2) self.colormod = '0.8 0.8 1.4';
1236 // Attach stdprocs. override when and what needed
1237 if (self.turrcaps_flags & TFL_TURRCAPS_SUPPORT)
1239 self.turret_score_target = turret_stdproc_targetscore_support;
1240 self.turret_firecheckfunc = turret_stdproc_firecheck;
1241 self.turret_firefunc = turret_stdproc_fire;
1242 self.event_damage = turret_stdproc_damage;
1246 self.turret_score_target = turret_stdproc_targetscore_generic;
1247 self.turret_firecheckfunc = turret_stdproc_firecheck;
1248 self.turret_firefunc = turret_stdproc_fire;
1249 self.event_damage = turret_stdproc_damage;
1252 self.use = turret_stdproc_use;
1253 self.bot_attack = TRUE;
1255 // Initiate the main AI loop
1257 self.think = turret_link;
1259 self.think = turret_think;
1262 self.nextthink = time + 1;
1263 self.nextthink += turret_count * sys_frametime;
1265 self.tur_head.team = self.team;
1266 self.view_ofs = '0 0 0';
1269 self.tur_dbg_start = self.nextthink;
1270 while (vlen(self.tur_dbg_rvec) < 2)
1271 self.tur_dbg_rvec = randomvec() * 4;
1273 self.tur_dbg_rvec_x = fabs(self.tur_dbg_rvec_x);
1274 self.tur_dbg_rvec_y = fabs(self.tur_dbg_rvec_y);
1275 self.tur_dbg_rvec_z = fabs(self.tur_dbg_rvec_z);
1279 self.turrcaps_flags |= TFL_TURRCAPS_ISTURRET;
1281 self.classname = "turret_main";
1283 self.tur_active = 1;
1285 // In ONS mode, and linked to a ONS ent. need to call the use to set team.
1286 if (g_onslaught && ee)