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 (!ent.turret_scale_damage) ent.turret_scale_damage = 1;
32 if (!ent.turret_scale_range) ent.turret_scale_range = 1;
33 if (!ent.turret_scale_refire) ent.turret_scale_refire = 1;
34 if (!ent.turret_scale_ammo) ent.turret_scale_ammo = 1;
35 if (!ent.turret_scale_aim) ent.turret_scale_aim = 1;
36 if (!ent.turret_scale_health) ent.turret_scale_health = 1;
37 if (!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)
135 oldpos = self.enemy.origin;
136 setorigin(self.enemy,self.tur_aimpos);
139 //dprint("NN: ", self.netname," THVN: ",self.tur_head.classname," frame:",ftos(self.tur_head.frame),"\n");
140 //dprint("self.tur_shotorg: ",vtos(self.tur_shotorg),"\n");
141 tracebox(self.tur_shotorg, '-1 -1 -1','1 1 1',self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos),MOVE_NORMAL,self);
143 if(self.firecheck_flags & TFL_FIRECHECK_VERIFIED)
145 setorigin(self.enemy,oldpos);
147 //self.tur_impactpoint = trace_endpos;
148 self.tur_impactent = trace_ent;
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_impacttime = vlen(self.tur_shotorg - trace_endpos) / self.shot_speed;
156 vector turret_fovsearch_pingpong()
159 if(self.phase < time)
161 if( self.tur_head.phase )
162 self.tur_head.phase = 0;
164 self.tur_head.phase = 1;
165 self.phase = time + 5;
168 if( self.tur_head.phase)
169 wish_angle = self.idle_aim + '0 1 0' * (self.aim_maxrot * (self.target_select_fov / 360));
171 wish_angle = self.idle_aim - '0 1 0' * (self.aim_maxrot * (self.target_select_fov / 360));
176 vector turret_fovsearch_steprot()
181 wish_angle = self.tur_head.angles;
182 wish_angle_x = self.idle_aim_x;
184 if (self.phase < time)
186 //rot_add = self.aim_maxrot / self.target_select_fov;
187 wish_angle_y += (self.target_select_fov * 2);
189 if(wish_angle_y > 360)
190 wish_angle_y = wish_angle_y - 360;
192 self.phase = time + 1.5;
198 vector turret_fovsearch_random()
202 if (self.phase < time)
204 wish_angle_y = random() * self.aim_maxrot;
208 wish_angle_x = random() * self.aim_maxpitch;
212 self.phase = time + 5;
214 self.tur_aimpos = wish_angle;
217 return self.idle_aim + self.tur_aimpos;
222 ** Handles head rotation according to
223 ** the units .track_type and .track_flags
226 void turret_stdproc_track()
228 vector target_angle; // This is where we want to aim
229 vector move_angle; // This is where we can aim
232 if (self.track_flags == TFL_TRACK_NO)
236 target_angle = self.idle_aim - ('1 0 0' * self.aim_maxpitch);
237 else if (self.enemy == world)
240 if (self.turrcaps_flags & TFL_TURRCAPS_HEADATTACHED)
241 target_angle = self.idle_aim + self.angles;
243 target_angle = self.idle_aim;
245 target_angle = vectoangles(normalize(self.tur_aimpos - self.tur_shotorg));
249 // Find the direction
250 target_angle = normalize(self.tur_aimpos - self.tur_shotorg);
251 target_angle = vectoangles(target_angle); // And make a angle
254 self.tur_head.angles_x = safeangle(self.tur_head.angles_x);
255 self.tur_head.angles_y = safeangle(self.tur_head.angles_y);
257 // Find the diffrence between where we currently aim and where we want to aim
261 if (self.turrcaps_flags & TFL_TURRCAPS_HEADATTACHED)
263 move_angle = target_angle - (self.angles + self.tur_head.angles);
264 move_angle = shortangle_vxy(move_angle,(self.angles + self.tur_head.angles));
270 move_angle = target_angle - self.tur_head.angles;
271 move_angle = shortangle_vxy(move_angle,self.tur_head.angles);
275 switch(self.track_type)
277 case TFL_TRACKTYPE_STEPMOTOR:
278 f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
279 if (self.track_flags & TFL_TRACK_PITCH)
281 self.tur_head.angles_x += bound(-f_tmp,move_angle_x, f_tmp);
282 if(self.tur_head.angles_x + a_off_x > self.aim_maxpitch)
283 self.tur_head.angles_x = a_off_x + self.aim_maxpitch;
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;
289 if (self.track_flags & TFL_TRACK_ROT)
291 self.tur_head.angles_y += bound(-f_tmp, move_angle_y, f_tmp);
292 if((self.tur_head.angles_y - a_off_y) > self.aim_maxrot)
293 self.tur_head.angles_y = a_off_y + self.aim_maxrot;
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;
301 case TFL_TRACKTYPE_FLUIDINERTIA:
302 f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
303 move_angle_x = bound(-self.aim_speed, move_angle_x * self.track_accel_pitch * f_tmp,self.aim_speed);
304 move_angle_y = bound(-self.aim_speed, move_angle_y * self.track_accel_rot * f_tmp,self.aim_speed);
305 move_angle = (self.tur_head.avelocity * self.track_blendrate) + (move_angle * (1 - self.track_blendrate));
308 case TFL_TRACKTYPE_FLUIDPRECISE:
310 move_angle_y = bound(-self.aim_speed, move_angle_y, self.aim_speed);
311 move_angle_x = bound(-self.aim_speed, move_angle_x, self.aim_speed);
317 if (self.track_flags & TFL_TRACK_PITCH)
319 self.tur_head.avelocity_x = move_angle_x;
320 if((self.tur_head.angles_x + self.tur_head.avelocity_x * self.ticrate) + a_off_x > self.aim_maxpitch)
322 self.tur_head.avelocity_x = 0;
323 self.tur_head.angles_x = a_off_x + self.aim_maxpitch;
325 if((self.tur_head.angles_x + self.tur_head.avelocity_x * self.ticrate) + a_off_x < -self.aim_maxpitch)
327 self.tur_head.avelocity_x = 0;
328 self.tur_head.angles_x = a_off_x - self.aim_maxpitch;
334 if (self.track_flags & TFL_TRACK_ROT)
336 self.tur_head.avelocity_y = move_angle_y;
338 if(((self.tur_head.angles_y + self.tur_head.avelocity_y * self.ticrate)- a_off_y) > self.aim_maxrot)
340 self.tur_head.avelocity_y = 0;
341 self.tur_head.angles_y = a_off_y + self.aim_maxrot;
344 if(((self.tur_head.angles_y + self.tur_head.avelocity_y * self.ticrate) - a_off_y) < -self.aim_maxrot)
346 self.tur_head.avelocity_y = 0;
347 self.tur_head.angles_y = a_off_y - self.aim_maxrot;
360 + TFL_FIRECHECK_WORLD
362 + TFL_FIRECHECK_DISTANCES
364 + TFL_FIRECHECK_AIMDIST
365 + TFL_FIRECHECK_REALDIST
366 - TFL_FIRECHECK_ANGLEDIST
367 - TFL_FIRECHECK_TEAMCECK
369 + TFL_FIRECHECK_OWM_AMMO
370 + TFL_FIRECHECK_OTHER_AMMO
371 + TFL_FIRECHECK_REFIRE
375 ** Preforms pre-fire checks based on the uints firecheck_flags
377 float turret_stdproc_firecheck()
379 // This one just dont care =)
380 if (self.firecheck_flags & TFL_FIRECHECK_NO) return 1;
383 if (self.firecheck_flags & TFL_FIRECHECK_REFIRE)
384 if (self.attack_finished_single >= time) return 0;
386 // Special case: volly fire turret that has to fire a full volly if a shot was fired.
387 if((self.shoot_flags & TFL_SHOOT_VOLLYALWAYS) && (self.volly_counter != self.shot_volly))
390 // Lack of zombies makes shooting dead things unnecessary :P
391 if (self.firecheck_flags & TFL_FIRECHECK_DEAD)
392 if (self.enemy.deadflag != DEAD_NO) return 0;
394 // Plz stop killing the world!
395 if (self.firecheck_flags & TFL_FIRECHECK_WORLD)
396 if (self.enemy == world) return 0;
399 if (self.firecheck_flags & TFL_FIRECHECK_OWM_AMMO)
400 if (self.ammo < self.shot_dmg) return 0;
402 // Other's ammo? (support-supply units)
403 if (self.firecheck_flags & TFL_FIRECHECK_OTHER_AMMO)
404 if (self.enemy.ammo >= self.enemy.ammo_max) return 0;
406 if (self.firecheck_flags & TFL_FIRECHECK_DISTANCES)
408 // Not close enougth?
409 //if (self.tur_dist_aimpos > self.target_range_fire) return 0;
412 if (self.tur_dist_aimpos < self.target_range_min) return 0;
416 if (self.firecheck_flags & TFL_FIRECHECK_AFF)
417 if (self.tur_impactent.team == self.team) return 0;
419 // aim<->predicted impact
420 if (self.firecheck_flags & TFL_FIRECHECK_AIMDIST)
421 if (self.tur_dist_impact_to_aimpos > self.aim_firetolerance_dist)
422 if (self.tur_impactent != self.enemy)
426 if (self.shot_volly > 1)
427 if (self.volly_counter == self.shot_volly)
428 if (self.ammo < (self.shot_dmg * self.shot_volly))
431 if(self.firecheck_flags & TFL_FIRECHECK_VERIFIED)
432 if(self.tur_impactent != self.enemy)
439 + TFL_TARGETSELECT_NO
440 + TFL_TARGETSELECT_LOS
441 + TFL_TARGETSELECT_PLAYERS
442 + TFL_TARGETSELECT_MISSILES
443 - TFL_TARGETSELECT_TRIGGERTARGET
444 + TFL_TARGETSELECT_ANGLELIMITS
445 + TFL_TARGETSELECT_RANGELIMTS
446 + TFL_TARGETSELECT_TEAMCHECK
447 - TFL_TARGETSELECT_NOBUILTIN
448 + TFL_TARGETSELECT_OWNTEAM
452 ** Evaluate a entity for target valitity based on validate_flags
454 float turret_validate_target(entity e_turret,entity e_target,float validate_flags)
458 //if(!validate_flags & TFL_TARGETSELECT_NOBUILTIN)
461 if(e_target.owner == e_turret)
464 if not(checkpvs(e_target.origin, e_turret))
467 if (!e_target)// == world)
471 if (substring(e_target.classname, 0, 10) == "onslaught_") // don't attack onslaught targets, that's the player's job!
474 if (validate_flags & TFL_TARGETSELECT_NO)
477 // If only this was used more..
478 if (e_target.flags & FL_NOTARGET)
482 if ((e_target.takedamage == DAMAGE_NO) || (e_target.health < 0))
486 if (e_target.flags & FL_CLIENT)
488 if not (validate_flags & TFL_TARGETSELECT_PLAYERS)
491 if (e_target.deadflag != DEAD_NO)
496 if (validate_flags & TFL_TARGETSELECT_NOTURRETS)
497 if (e_target.turret_firefunc || e_target.owner.tur_head == e_target)
498 if(e_target.team != e_turret.team) // Dont break support units.
502 if (e_target.flags & FL_PROJECTILE)
503 if not (validate_flags & TFL_TARGETSELECT_MISSILES)
507 if (validate_flags & TFL_TARGETSELECT_TEAMCHECK)
509 if (validate_flags & TFL_TARGETSELECT_OWNTEAM)
511 if (e_target.team != e_turret.team)
514 if (e_turret.team != e_target.owner.team)
519 if (e_target.team == e_turret.team)
522 if (e_turret.team == e_target.owner.team)
528 tvt_dist = vlen(e_turret.origin - real_origin(e_target));
529 if (validate_flags & TFL_TARGETSELECT_RANGELIMTS)
531 if (tvt_dist < e_turret.target_range_min)
534 if (tvt_dist > e_turret.target_range)
538 // Can we even aim this thing?
539 if(e_turret.turrcaps_flags & TFL_TURRCAPS_HEADATTACHED)
541 tvt_thadv = angleofs3(e_turret.tur_head.origin,e_turret.angles + e_turret.tur_head.angles ,e_target);
542 //tvt_thadv = angleofs(e_turret.angles,e_target);
546 tvt_thadv = angleofs(e_turret.tur_head,e_target);
549 tvt_tadv = shortangle_vxy(angleofs(e_turret,e_target),e_turret.angles);
550 tvt_thadf = vlen(tvt_thadv);
551 tvt_tadf = vlen(tvt_tadv);
554 if(validate_flags & TFL_TARGETSELECT_FOV)
556 if(e_turret.target_select_fov < tvt_thadf)
561 if (validate_flags & TFL_TARGETSELECT_ANGLELIMITS)
563 if (fabs(tvt_tadv_x) > e_turret.aim_maxpitch)
566 if (fabs(tvt_tadv_y) > e_turret.aim_maxrot)
571 if (validate_flags & TFL_TARGETSELECT_LOS)
573 v_tmp = real_origin(e_target) + ((e_target.mins + e_target.maxs) * 0.5);
574 traceline(e_turret.tur_shotorg,v_tmp,0,e_turret);
576 if (e_turret.aim_firetolerance_dist < vlen(v_tmp - trace_endpos))
580 if (e_target.classname == "grapplinghook")
584 if (e_target.classname == "func_button")
588 #ifdef TURRET_DEBUG_TARGETSELECT
589 dprint("Target:",e_target.netname," is a valid target for ",e_turret.netname,"\n");
595 entity turret_select_target()
597 entity e; // target looper entity
598 float score; // target looper entity score
599 entity e_enemy; // currently best scoreing target
600 float m_score; // currently best scoreing target's score
605 if(turret_validate_target(self,self.enemy,self.target_validate_flags) > 0)
607 e_enemy = self.enemy;
608 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;
618 f = turret_validate_target(self,e,self.target_select_flags);
621 score = self.turret_score_target(self,e);
622 if ((score > m_score) && (score > 0))
638 self.nextthink = time + self.ticrate;
640 // ONS uses somewhat backwards linking.
646 e = find(world,targetname,self.target);
651 if (self.team != self.tur_head.team)
652 turret_stdproc_respawn();
656 if (cvar("g_turrets_reloadcvars") == 1)
661 if (e.tur_head != world)
664 load_unit_settings(e,e.cvar_basename,1);
665 if(e.turret_postthink)
666 e.turret_postthink();
672 cvar_set("g_turrets_reloadcvars","0");
676 if (self.tur_dbg_tmr1 < time)
678 if (self.enemy) paint_target (self.enemy,128,self.tur_dbg_rvec,0.9);
679 paint_target(self,256,self.tur_dbg_rvec,0.9);
680 self.tur_dbg_tmr1 = time + 1;
685 if not (self.spawnflags & TSF_NO_AMMO_REGEN)
686 if (self.ammo < self.ammo_max)
687 self.ammo = min(self.ammo + self.ammo_recharge,self.ammo_max);
690 // Inactive turrets needs to run the think loop,
691 // So they can handle animation and wake up if need be.
692 if not (self.tur_active)
694 turret_stdproc_track();
698 //This is just wrong :|
699 if(self.deadflag != DEAD_NO)
701 dprint("WARNING: dead turret running the think function!\n");
705 // This is typicaly used for zaping every target in range
706 // turret_fusionreactor uses this to recharge friendlys.
707 if (self.shoot_flags & TFL_SHOOT_HITALLVALID)
710 // Do a self.turret_fire for every valid target.
711 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())
728 else if(self.shoot_flags & TFL_SHOOT_CUSTOM)
730 // This one is doing something.. oddball. assume its handles what needs to be handled.
733 if not(self.aim_flags & TFL_AIM_NO)
734 self.tur_aimpos = turret_stdproc_aim_generic();
737 if not(self.track_flags & TFL_TRACK_NO)
738 turret_stdproc_track();
740 turret_do_updates(self);
743 if (self.turret_firecheckfunc())
748 // Special case for volly always. if it fired once it must compleate the volly.
749 if(self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
750 if(self.volly_counter != self.shot_volly)
752 // Predict or whatnot
753 if not(self.aim_flags & TFL_AIM_NO)
754 self.tur_aimpos = turret_stdproc_aim_generic();
757 if not(self.track_flags & TFL_TRACK_NO)
758 turret_stdproc_track();
760 turret_do_updates(self);
763 if (self.turret_firecheckfunc() != 0)
766 if(self.turret_postthink)
767 self.turret_postthink();
772 // Check if we have a vailid enemy, and try to find one if we dont.
773 if( ((self.target_select_time + cvar("g_turrets_targetscan_maxdelay")) < time)
774 || (turret_validate_target(self,self.enemy,self.target_validate_flags) <= 0) )
775 if not (self.target_select_time + cvar("g_turrets_targetscan_mindelay") > time)
777 self.enemy = turret_select_target();
779 self.target_select_time = time;
784 // No target, just go to idle, do any custom stuff and bail.
785 if (self.enemy == world)
788 if not(self.track_flags & TFL_TRACK_NO)
789 turret_stdproc_track();
791 // do any per-turret stuff
792 if(self.turret_postthink)
793 self.turret_postthink();
799 self.lip = time + cvar("g_turrets_aimidle_delay"); // Keep track of the last time we had a target.
802 if not(self.aim_flags & TFL_AIM_NO)
803 self.tur_aimpos = turret_stdproc_aim_generic();
806 if not(self.track_flags & TFL_TRACK_NO)
807 turret_stdproc_track();
809 turret_do_updates(self);
811 if (self.turret_firecheckfunc())
815 // do any per-turret stuff
816 if(self.turret_postthink)
817 self.turret_postthink();
822 if (cvar("g_turrets_nofire") != 0)
825 if ((!self.tur_active) || (self.deadflag != DEAD_NO))
828 self.turret_firefunc();
830 self.attack_finished_single = time + self.shot_refire;
831 self.ammo = self.ammo - self.shot_dmg;
832 self.volly_counter = self.volly_counter - 1;
833 if (self.volly_counter <= 0)
835 self.volly_counter = self.shot_volly;
837 if (self.shoot_flags & TFL_SHOOT_CLEARTARGET)
840 if (self.shot_volly > 1)
841 self.attack_finished_single = time + self.shot_volly_refire;
846 if (self.enemy) paint_target3(self.tur_aimpos, 64, self.tur_dbg_rvec, self.tur_impacttime + 0.25);
850 void turret_stdproc_fire()
852 dprint("^1Bang, ^3your dead^7 ",self.enemy.netname,"! ^1(turret with no real firefunc)\n");
856 When .used a turret switch team to activator.team.
857 If activator is world, the turrets goes inactive.
859 void turret_stdproc_use()
861 dprint("Turret ",self.netname, " used by ",activator.classname,"\n");
863 self.team = activator.team;
874 //Net_LinkEntity(self, FALSE, 0, Turret_SendEntity);
875 self.think = turret_think;
876 self.nextthink = time;
880 * Standard turret initialization. use this!
881 * (unless you have a very good reason not to)
882 * if the return value is 0, the turret should be removed.
884 float turret_stdproc_init (string cvar_base_name, float csqc_shared)
890 dprint("turrets: csqc_shared requested but not implemented. expect strange things to happen.\n");
894 // Are turrets allowed atm?
895 if (cvar("g_turrets") == 0)
898 // Better more then once then never.
899 // turret_gibs_precash();
901 // Terrainbase spawnflag. This puts a enlongated model
902 // under the turret, so it looks ok on uneaven surfaces.
903 if (self.spawnflags & TSF_TERRAINBASE)
906 //precache_model("models/turrets/terrainbase.md3");
908 setmodel(tb,"models/turrets/terrainbase.md3");
909 setorigin(tb,self.origin);
910 tb.solid = SOLID_BBOX;
914 self.cvar_basename = cvar_base_name;
915 load_unit_settings(self,self.cvar_basename,0);
917 // Handle turret teams.
918 if (cvar("g_assult") != 0)
921 self.team = 14; // Assume turrets are on the defending side if not explicitly set otehrwize
924 self.team = MAX_SHOT_DISTANCE; // Group all turrets into the same team iso they dont kill eachother.
925 else if(g_onslaught && self.targetname)
927 e = find(world,target,self.targetname);
935 self.team = MAX_SHOT_DISTANCE; // Group all turrets into the same team iso they dont kill eachother.
940 * Try to guess some reasonaly defaults
941 * for missing params and do sanety checks
942 * thise checks could produce some "interesting" results
943 * if it hits a glitch in my logic :P so try to set as mutch
944 * as possible beforehand.
946 if (self.turrcaps_flags & TFL_TURRCAPS_SUPPORT)
947 if (!self.ticrate) self.ticrate = 0.2; // Support units generaly dont need to have a high speed ai-loop
949 if (!self.ticrate) self.ticrate = 0.1; // 10 fps for normal turrets
951 self.ticrate = bound(sys_ticrate,self.ticrate,60); // keep it sane
954 if (self.netname == "")
955 self.netname = self.classname;
957 if (!self.respawntime)
958 self.respawntime = 60;
959 self.respawntime = max(-1,self.respawntime);
963 self.tur_health = max(1,self.health);
965 if (!self.turrcaps_flags)
966 self.turrcaps_flags = TFL_TURRCAPS_RADIUSDMG | TFL_TURRCAPS_MEDPROJ | TFL_TURRCAPS_PLAYERKILL;
968 if (!self.damage_flags)
969 self.damage_flags = TFL_DMG_YES | TFL_DMG_RETALIATE | TFL_DMG_AIMSHAKE;
972 if (!self.shot_refire)
973 self.shot_refire = 1;
974 self.shot_refire = bound(0.01,self.shot_refire,9999);
977 self.shot_dmg = self.shot_refire * 50;
978 self.shot_dmg = max(1,self.shot_dmg);
980 if (!self.shot_radius)
981 self.shot_radius = self.shot_dmg * 0.5;
982 self.shot_radius = max(1,self.shot_radius);
984 if (!self.shot_speed)
985 self.shot_speed = 2500;
986 self.shot_speed = max(1,self.shot_speed);
988 if (!self.shot_spread)
989 self.shot_spread = 0.0125;
990 self.shot_spread = bound(0.0001,self.shot_spread,500);
992 if (!self.shot_force)
993 self.shot_force = self.shot_dmg * 0.5 + self.shot_radius * 0.5;
994 self.shot_force = bound(0.001,self.shot_force,MAX_SHOT_DISTANCE * 0.5);
996 if (!self.shot_volly)
998 self.shot_volly = bound(1,self.shot_volly,floor(self.ammo_max / self.shot_dmg));
1000 if (!self.shot_volly_refire)
1001 self.shot_volly_refire = self.shot_refire * self.shot_volly;
1002 self.shot_volly_refire = bound(self.shot_refire,self.shot_volly_refire,60);
1004 if (!self.firecheck_flags)
1005 self.firecheck_flags = TFL_FIRECHECK_WORLD | TFL_FIRECHECK_DEAD | TFL_FIRECHECK_DISTANCES |
1006 TFL_FIRECHECK_LOS | TFL_FIRECHECK_AIMDIST | TFL_FIRECHECK_TEAMCECK |
1007 TFL_FIRECHECK_OWM_AMMO | TFL_FIRECHECK_REFIRE | TFL_FIRECHECK_WORLD;
1010 if (!self.target_range)
1011 self.target_range = self.shot_speed * 0.5;
1012 self.target_range = bound(0,self.target_range,MAX_SHOT_DISTANCE);
1014 if (!self.target_range_min)
1015 self.target_range_min = self.shot_radius * 2;
1016 self.target_range_min = bound(0,self.target_range_min,MAX_SHOT_DISTANCE);
1018 //if (!self.target_range_fire)
1019 // self.target_range_fire = self.target_range * 0.8;
1020 //self.target_range_fire = bound(0,self.target_range_fire,MAX_SHOT_DISTANCE);
1022 if (!self.target_range_optimal)
1023 self.target_range_optimal = self.target_range * 0.5;
1024 self.target_range_optimal = bound(0,self.target_range_optimal,MAX_SHOT_DISTANCE);
1028 if (!self.aim_maxrot)
1029 self.aim_maxrot = 90;
1030 self.aim_maxrot = bound(0,self.aim_maxrot,360);
1032 if (!self.aim_maxpitch)
1033 self.aim_maxpitch = 20;
1034 self.aim_maxpitch = bound(0,self.aim_maxpitch,90);
1036 if (!self.aim_speed)
1037 self.aim_speed = 36;
1038 self.aim_speed = bound(0.1,self.aim_speed, 1000);
1040 if (!self.aim_firetolerance_dist)
1041 self.aim_firetolerance_dist = 5 + (self.shot_radius * 2);
1042 self.aim_firetolerance_dist = bound(0.1,self.aim_firetolerance_dist,MAX_SHOT_DISTANCE);
1044 if (!self.aim_flags)
1046 self.aim_flags = TFL_AIM_LEAD | TFL_AIM_SHOTTIMECOMPENSATE;
1047 if(self.turrcaps_flags & TFL_TURRCAPS_RADIUSDMG)
1048 self.aim_flags |= TFL_AIM_GROUND2;
1051 // Sill the most tested (and aim-effective)
1052 if (!self.track_type) self.track_type = TFL_TRACKTYPE_STEPMOTOR;
1054 if (self.track_type != TFL_TRACKTYPE_STEPMOTOR)
1056 // Fluid / Ineria mode. Looks mutch nicer, bit experimental &
1057 // Can inmapt aim preformance alot.
1058 // needs a bit diffrent aimspeed
1060 if (!self.aim_speed)
1061 self.aim_speed = 180;
1062 self.aim_speed = bound(0.1,self.aim_speed, 1000);
1064 if (!self.track_accel_pitch)
1065 self.track_accel_pitch = 0.5;
1067 if (!self.track_accel_rot)
1068 self.track_accel_rot = 0.5;
1070 if (!self.track_blendrate)
1071 self.track_blendrate = 0.35;
1074 if (!self.track_flags)
1075 self.track_flags = TFL_TRACK_PITCH | TFL_TRACK_ROT;
1078 // Target selection stuff.
1079 if (!self.target_select_rangebias)
1080 self.target_select_rangebias = 1;
1081 self.target_select_rangebias = bound(-10,self.target_select_rangebias,10);
1083 if (!self.target_select_samebias)
1084 self.target_select_samebias = 1;
1085 self.target_select_samebias = bound(-10,self.target_select_samebias,10);
1087 if (!self.target_select_anglebias)
1088 self.target_select_anglebias = 1;
1089 self.target_select_anglebias = bound(-10,self.target_select_anglebias,10);
1091 if (!self.target_select_missilebias)
1092 self.target_select_missilebias = -10;
1094 self.target_select_missilebias = bound(-10,self.target_select_missilebias,10);
1095 self.target_select_playerbias = bound(-10,self.target_select_playerbias,10);
1097 if (!self.target_select_flags)
1099 self.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_TEAMCHECK
1100 | TFL_TARGETSELECT_RANGELIMTS | TFL_TARGETSELECT_ANGLELIMITS;
1102 if (self.turrcaps_flags & TFL_TURRCAPS_MISSILEKILL)
1103 self.target_select_flags |= TFL_TARGETSELECT_MISSILES;
1105 if (self.turrcaps_flags & TFL_TURRCAPS_PLAYERKILL)
1106 self.target_select_flags |= TFL_TARGETSELECT_PLAYERS;
1108 // self.target_select_flags = TFL_TARGETSELECT_NO;
1111 self.target_validate_flags = self.target_select_flags;
1116 self.ammo_max = self.shot_dmg * 10;
1117 self.ammo_max = max(self.shot_dmg,self.ammo_max);
1120 self.ammo = self.shot_dmg * 5;
1121 self.ammo = bound(0,self.ammo,self.ammo_max);
1123 if (!self.ammo_recharge)
1124 self.ammo_recharge = self.shot_dmg * 0.5;
1125 self.ammo_recharge = max(0,self.ammo_recharge);
1127 // Convert the recharge from X per sec to X per ticrate
1128 self.ammo_recharge = self.ammo_recharge * self.ticrate;
1130 if (!self.ammo_flags)
1131 self.ammo_flags = TFL_AMMO_ENERGY | TFL_AMMO_RECHARGE;
1134 if(self.spawnflags & TSL_NO_RESPAWN)
1135 if not (self.damage_flags & TFL_DMG_DEATH_NORESPAWN)
1136 self.damage_flags |= TFL_DMG_DEATH_NORESPAWN;
1138 // Offsets & origins
1139 if (!self.tur_shotorg) self.tur_shotorg = '50 0 50';
1141 // End of default & sanety checks, start building the turret.
1144 self.tur_head = spawn();
1145 self.tur_head.netname = self.tur_head.classname = "turret_head";
1146 self.tur_head.team = self.team;
1147 self.tur_head.owner = self;
1150 if(!self.tur_defend)
1151 if (self.target != "")
1153 self.tur_defend = find(world, targetname, self.target);
1154 if (self.tur_defend == world)
1157 dprint("Turret has invalid defendpoint!\n");
1161 // Put pices in place
1162 if not (self.turrcaps_flags & TFL_TURRCAPS_HEADATTACHED)
1163 setorigin(self.tur_head,self.origin);
1165 // In target defend mode, aim on the spot to defend when idle.
1166 if(self.turrcaps_flags & TFL_TURRCAPS_HEADATTACHED)
1168 if (self.tur_defend)
1169 self.idle_aim = self.tur_head.angles + angleofs(self.tur_head,self.tur_defend);
1171 self.idle_aim = '0 0 0';
1175 if (self.tur_defend)
1176 self.idle_aim = self.tur_head.angles + angleofs(self.tur_head,self.tur_defend);
1178 self.idle_aim = self.angles;
1181 if not (self.turrcaps_flags & TFL_TURRCAPS_HEADATTACHED)
1182 self.tur_head.angles = self.idle_aim;
1187 self.tur_health = self.health;
1188 self.tur_head.health = self.health;
1190 self.solid = SOLID_BBOX;
1191 self.tur_head.solid = SOLID_BBOX;
1193 self.takedamage = DAMAGE_AIM;
1194 self.tur_head.takedamage = DAMAGE_AIM;
1196 self.movetype = MOVETYPE_NOCLIP;
1197 self.tur_head.movetype = MOVETYPE_NOCLIP;
1200 if (self.team == COLOR_TEAM1) self.colormod = '1.4 0.8 0.8';
1201 if (self.team == COLOR_TEAM2) self.colormod = '0.8 0.8 1.4';
1203 // Attach stdprocs. override when and what needed
1204 if (self.turrcaps_flags & TFL_TURRCAPS_SUPPORT)
1206 self.turret_score_target = turret_stdproc_targetscore_support;
1207 self.turret_firecheckfunc = turret_stdproc_firecheck;
1208 self.turret_firefunc = turret_stdproc_fire;
1209 //self.turret_postthink = turret_stdproc_nothing;
1210 self.event_damage = turret_stdproc_damage;
1211 self.tur_head.event_damage = turret_stdproc_damage;
1215 self.turret_score_target = turret_stdproc_targetscore_generic;
1216 self.turret_firecheckfunc = turret_stdproc_firecheck;
1217 self.turret_firefunc = turret_stdproc_fire;
1218 //self.turret_postthink = turret_stdproc_nothing;
1219 self.event_damage = turret_stdproc_damage;
1220 self.tur_head.event_damage = turret_stdproc_damage;
1221 //self.turret_addtarget = turret_stdproc_false;
1224 self.use = turret_stdproc_use;
1225 self.bot_attack = TRUE;
1227 // Initiate the main AI loop
1229 self.think = turret_link;
1231 self.think = turret_think;
1233 self.nextthink = time + self.ticrate;
1235 self.tur_head.team = self.team;
1236 self.view_ofs = '0 0 0';
1239 self.tur_dbg_start = self.nextthink;
1240 while (vlen(self.tur_dbg_rvec) < 2)
1241 self.tur_dbg_rvec = randomvec() * 4;
1243 self.tur_dbg_rvec_x = fabs(self.tur_dbg_rvec_x);
1244 self.tur_dbg_rvec_y = fabs(self.tur_dbg_rvec_y);
1245 self.tur_dbg_rvec_z = fabs(self.tur_dbg_rvec_z);
1249 self.classname = "turret_main";
1251 self.tur_active = 1;
1253 // In ONS mode, and linked to a ONS ent. need to call the use to set team.
1254 if (g_onslaught && ee)