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")
583 if (e_target.classname == "func_button")
586 #ifdef TURRET_DEBUG_TARGETSELECT
587 dprint("Target:",e_target.netname," is a valid target for ",e_turret.netname,"\n");
593 entity turret_select_target()
595 entity e; // target looper entity
596 float score; // target looper entity score
597 entity e_enemy; // currently best scoreing target
598 float m_score; // currently best scoreing target's score
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;
609 e = findradius(self.origin,self.target_range);
611 // Nothing to aim at?
612 if (!e) return world;
616 f = turret_validate_target(self,e,self.target_select_flags);
619 score = self.turret_score_target(self,e);
620 if ((score > m_score) && (score > 0))
636 self.nextthink = time + self.ticrate;
638 // ONS uses somewhat backwards linking.
644 e = find(world,targetname,self.target);
649 if (self.team != self.tur_head.team)
650 turret_stdproc_respawn();
654 if (cvar("g_turrets_reloadcvars") == 1)
659 if (e.tur_head != world)
662 load_unit_settings(e,e.cvar_basename,1);
663 if(e.turret_postthink)
664 e.turret_postthink();
670 cvar_set("g_turrets_reloadcvars","0");
674 if (self.tur_dbg_tmr1 < time)
676 if (self.enemy) paint_target (self.enemy,128,self.tur_dbg_rvec,0.9);
677 paint_target(self,256,self.tur_dbg_rvec,0.9);
678 self.tur_dbg_tmr1 = time + 1;
683 if not (self.spawnflags & TSF_NO_AMMO_REGEN)
684 if (self.ammo < self.ammo_max)
685 self.ammo = min(self.ammo + self.ammo_recharge,self.ammo_max);
688 // Inactive turrets needs to run the think loop,
689 // So they can handle animation and wake up if need be.
690 if not (self.tur_active)
692 turret_stdproc_track();
696 //This is just wrong :|
697 if(self.deadflag != DEAD_NO)
699 dprint("WARNING: dead turret running the think function!\n");
703 // This is typicaly used for zaping every target in range
704 // turret_fusionreactor uses this to recharge friendlys.
705 if (self.shoot_flags & TFL_SHOOT_HITALLVALID)
708 // Do a self.turret_fire for every valid target.
709 e = findradius(self.origin,self.target_range);
712 if (turret_validate_target(self,e,self.target_validate_flags))
716 turret_do_updates(self);
718 if (self.turret_firecheckfunc())
726 else if(self.shoot_flags & TFL_SHOOT_CUSTOM)
728 // This one is doing something.. oddball. assume its handles what needs to be handled.
731 if not(self.aim_flags & TFL_AIM_NO)
732 self.tur_aimpos = turret_stdproc_aim_generic();
735 if not(self.track_flags & TFL_TRACK_NO)
736 turret_stdproc_track();
738 turret_do_updates(self);
741 if (self.turret_firecheckfunc())
746 // Special case for volly always. if it fired once it must compleate the volly.
747 if(self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
748 if(self.volly_counter != self.shot_volly)
750 // Predict or whatnot
751 if not(self.aim_flags & TFL_AIM_NO)
752 self.tur_aimpos = turret_stdproc_aim_generic();
755 if not(self.track_flags & TFL_TRACK_NO)
756 turret_stdproc_track();
758 turret_do_updates(self);
761 if (self.turret_firecheckfunc() != 0)
764 if(self.turret_postthink)
765 self.turret_postthink();
770 // Check if we have a vailid enemy, and try to find one if we dont.
771 if( ((self.target_select_time + cvar("g_turrets_targetscan_maxdelay")) < time)
772 || (turret_validate_target(self,self.enemy,self.target_validate_flags) <= 0) )
773 if not (self.target_select_time + cvar("g_turrets_targetscan_mindelay") > time)
775 self.enemy = turret_select_target();
777 self.target_select_time = time;
782 // No target, just go to idle, do any custom stuff and bail.
783 if (self.enemy == world)
786 if not(self.track_flags & TFL_TRACK_NO)
787 turret_stdproc_track();
789 // do any per-turret stuff
790 if(self.turret_postthink)
791 self.turret_postthink();
797 self.lip = time + cvar("g_turrets_aimidle_delay"); // Keep track of the last time we had a target.
800 if not(self.aim_flags & TFL_AIM_NO)
801 self.tur_aimpos = turret_stdproc_aim_generic();
804 if not(self.track_flags & TFL_TRACK_NO)
805 turret_stdproc_track();
807 turret_do_updates(self);
809 if (self.turret_firecheckfunc())
813 // do any per-turret stuff
814 if(self.turret_postthink)
815 self.turret_postthink();
820 if (cvar("g_turrets_nofire") != 0)
823 if ((!self.tur_active) || (self.deadflag != DEAD_NO))
826 self.turret_firefunc();
828 self.attack_finished_single = time + self.shot_refire;
829 self.ammo = self.ammo - self.shot_dmg;
830 self.volly_counter = self.volly_counter - 1;
831 if (self.volly_counter <= 0)
833 self.volly_counter = self.shot_volly;
835 if (self.shoot_flags & TFL_SHOOT_CLEARTARGET)
838 if (self.shot_volly > 1)
839 self.attack_finished_single = time + self.shot_volly_refire;
844 if (self.enemy) paint_target3(self.tur_aimpos, 64, self.tur_dbg_rvec, self.tur_impacttime + 0.25);
848 void turret_stdproc_fire()
850 dprint("^1Bang, ^3your dead^7 ",self.enemy.netname,"! ^1(turret with no real firefunc)\n");
854 When .used a turret switch team to activator.team.
855 If activator is world, the turrets goes inactive.
857 void turret_stdproc_use()
859 dprint("Turret ",self.netname, " used by ",activator.classname,"\n");
861 self.team = activator.team;
872 //Net_LinkEntity(self, FALSE, 0, Turret_SendEntity);
873 self.think = turret_think;
874 self.nextthink = time;
878 * Standard turret initialization. use this!
879 * (unless you have a very good reason not to)
880 * if the return value is 0, the turret should be removed.
882 float turret_stdproc_init (string cvar_base_name, float csqc_shared)
888 dprint("turrets: csqc_shared requested but not implemented. expect strange things to happen.\n");
892 // Are turrets allowed atm?
893 if (cvar("g_turrets") == 0)
896 // Better more then once then never.
897 // turret_gibs_precash();
899 // Terrainbase spawnflag. This puts a enlongated model
900 // under the turret, so it looks ok on uneaven surfaces.
901 if (self.spawnflags & TSF_TERRAINBASE)
904 //precache_model("models/turrets/terrainbase.md3");
906 setmodel(tb,"models/turrets/terrainbase.md3");
907 setorigin(tb,self.origin);
908 tb.solid = SOLID_BBOX;
912 self.cvar_basename = cvar_base_name;
913 load_unit_settings(self,self.cvar_basename,0);
915 // Handle turret teams.
916 if (cvar("g_assult") != 0)
919 self.team = 14; // Assume turrets are on the defending side if not explicitly set otehrwize
922 self.team = MAX_SHOT_DISTANCE; // Group all turrets into the same team iso they dont kill eachother.
923 else if(g_onslaught && self.targetname)
925 e = find(world,target,self.targetname);
933 self.team = MAX_SHOT_DISTANCE; // Group all turrets into the same team iso they dont kill eachother.
938 * Try to guess some reasonaly defaults
939 * for missing params and do sanety checks
940 * thise checks could produce some "interesting" results
941 * if it hits a glitch in my logic :P so try to set as mutch
942 * as possible beforehand.
944 if (self.turrcaps_flags & TFL_TURRCAPS_SUPPORT)
945 if (!self.ticrate) self.ticrate = 0.2; // Support units generaly dont need to have a high speed ai-loop
947 if (!self.ticrate) self.ticrate = 0.1; // 10 fps for normal turrets
949 self.ticrate = bound(sys_ticrate,self.ticrate,60); // keep it sane
952 if (self.netname == "")
953 self.netname = self.classname;
955 if (!self.respawntime)
956 self.respawntime = 60;
957 self.respawntime = max(-1,self.respawntime);
961 self.tur_health = max(1,self.health);
963 if (!self.turrcaps_flags)
964 self.turrcaps_flags = TFL_TURRCAPS_RADIUSDMG | TFL_TURRCAPS_MEDPROJ | TFL_TURRCAPS_PLAYERKILL;
966 if (!self.damage_flags)
967 self.damage_flags = TFL_DMG_YES | TFL_DMG_RETALIATE | TFL_DMG_AIMSHAKE;
970 if (!self.shot_refire)
971 self.shot_refire = 1;
972 self.shot_refire = bound(0.01,self.shot_refire,9999);
975 self.shot_dmg = self.shot_refire * 50;
976 self.shot_dmg = max(1,self.shot_dmg);
978 if (!self.shot_radius)
979 self.shot_radius = self.shot_dmg * 0.5;
980 self.shot_radius = max(1,self.shot_radius);
982 if (!self.shot_speed)
983 self.shot_speed = 2500;
984 self.shot_speed = max(1,self.shot_speed);
986 if (!self.shot_spread)
987 self.shot_spread = 0.0125;
988 self.shot_spread = bound(0.0001,self.shot_spread,500);
990 if (!self.shot_force)
991 self.shot_force = self.shot_dmg * 0.5 + self.shot_radius * 0.5;
992 self.shot_force = bound(0.001,self.shot_force,MAX_SHOT_DISTANCE * 0.5);
994 if (!self.shot_volly)
996 self.shot_volly = bound(1,self.shot_volly,floor(self.ammo_max / self.shot_dmg));
998 if (!self.shot_volly_refire)
999 self.shot_volly_refire = self.shot_refire * self.shot_volly;
1000 self.shot_volly_refire = bound(self.shot_refire,self.shot_volly_refire,60);
1002 if (!self.firecheck_flags)
1003 self.firecheck_flags = TFL_FIRECHECK_WORLD | TFL_FIRECHECK_DEAD | TFL_FIRECHECK_DISTANCES |
1004 TFL_FIRECHECK_LOS | TFL_FIRECHECK_AIMDIST | TFL_FIRECHECK_TEAMCECK |
1005 TFL_FIRECHECK_OWM_AMMO | TFL_FIRECHECK_REFIRE | TFL_FIRECHECK_WORLD;
1008 if (!self.target_range)
1009 self.target_range = self.shot_speed * 0.5;
1010 self.target_range = bound(0,self.target_range,MAX_SHOT_DISTANCE);
1012 if (!self.target_range_min)
1013 self.target_range_min = self.shot_radius * 2;
1014 self.target_range_min = bound(0,self.target_range_min,MAX_SHOT_DISTANCE);
1016 //if (!self.target_range_fire)
1017 // self.target_range_fire = self.target_range * 0.8;
1018 //self.target_range_fire = bound(0,self.target_range_fire,MAX_SHOT_DISTANCE);
1020 if (!self.target_range_optimal)
1021 self.target_range_optimal = self.target_range * 0.5;
1022 self.target_range_optimal = bound(0,self.target_range_optimal,MAX_SHOT_DISTANCE);
1026 if (!self.aim_maxrot)
1027 self.aim_maxrot = 90;
1028 self.aim_maxrot = bound(0,self.aim_maxrot,360);
1030 if (!self.aim_maxpitch)
1031 self.aim_maxpitch = 20;
1032 self.aim_maxpitch = bound(0,self.aim_maxpitch,90);
1034 if (!self.aim_speed)
1035 self.aim_speed = 36;
1036 self.aim_speed = bound(0.1,self.aim_speed, 1000);
1038 if (!self.aim_firetolerance_dist)
1039 self.aim_firetolerance_dist = 5 + (self.shot_radius * 2);
1040 self.aim_firetolerance_dist = bound(0.1,self.aim_firetolerance_dist,MAX_SHOT_DISTANCE);
1042 if (!self.aim_flags)
1044 self.aim_flags = TFL_AIM_LEAD | TFL_AIM_SHOTTIMECOMPENSATE;
1045 if(self.turrcaps_flags & TFL_TURRCAPS_RADIUSDMG)
1046 self.aim_flags |= TFL_AIM_GROUND2;
1049 // Sill the most tested (and aim-effective)
1050 if (!self.track_type) self.track_type = TFL_TRACKTYPE_STEPMOTOR;
1052 if (self.track_type != TFL_TRACKTYPE_STEPMOTOR)
1054 // Fluid / Ineria mode. Looks mutch nicer, bit experimental &
1055 // Can inmapt aim preformance alot.
1056 // needs a bit diffrent aimspeed
1058 if (!self.aim_speed)
1059 self.aim_speed = 180;
1060 self.aim_speed = bound(0.1,self.aim_speed, 1000);
1062 if (!self.track_accel_pitch)
1063 self.track_accel_pitch = 0.5;
1065 if (!self.track_accel_rot)
1066 self.track_accel_rot = 0.5;
1068 if (!self.track_blendrate)
1069 self.track_blendrate = 0.35;
1072 if (!self.track_flags)
1073 self.track_flags = TFL_TRACK_PITCH | TFL_TRACK_ROT;
1076 // Target selection stuff.
1077 if (!self.target_select_rangebias)
1078 self.target_select_rangebias = 1;
1079 self.target_select_rangebias = bound(-10,self.target_select_rangebias,10);
1081 if (!self.target_select_samebias)
1082 self.target_select_samebias = 1;
1083 self.target_select_samebias = bound(-10,self.target_select_samebias,10);
1085 if (!self.target_select_anglebias)
1086 self.target_select_anglebias = 1;
1087 self.target_select_anglebias = bound(-10,self.target_select_anglebias,10);
1089 if (!self.target_select_missilebias)
1090 self.target_select_missilebias = -10;
1092 self.target_select_missilebias = bound(-10,self.target_select_missilebias,10);
1093 self.target_select_playerbias = bound(-10,self.target_select_playerbias,10);
1095 if (!self.target_select_flags)
1097 self.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_TEAMCHECK
1098 | TFL_TARGETSELECT_RANGELIMTS | TFL_TARGETSELECT_ANGLELIMITS;
1100 if (self.turrcaps_flags & TFL_TURRCAPS_MISSILEKILL)
1101 self.target_select_flags |= TFL_TARGETSELECT_MISSILES;
1103 if (self.turrcaps_flags & TFL_TURRCAPS_PLAYERKILL)
1104 self.target_select_flags |= TFL_TARGETSELECT_PLAYERS;
1106 // self.target_select_flags = TFL_TARGETSELECT_NO;
1109 self.target_validate_flags = self.target_select_flags;
1114 self.ammo_max = self.shot_dmg * 10;
1115 self.ammo_max = max(self.shot_dmg,self.ammo_max);
1118 self.ammo = self.shot_dmg * 5;
1119 self.ammo = bound(0,self.ammo,self.ammo_max);
1121 if (!self.ammo_recharge)
1122 self.ammo_recharge = self.shot_dmg * 0.5;
1123 self.ammo_recharge = max(0,self.ammo_recharge);
1125 // Convert the recharge from X per sec to X per ticrate
1126 self.ammo_recharge = self.ammo_recharge * self.ticrate;
1128 if (!self.ammo_flags)
1129 self.ammo_flags = TFL_AMMO_ENERGY | TFL_AMMO_RECHARGE;
1132 if(self.spawnflags & TSL_NO_RESPAWN)
1133 if not (self.damage_flags & TFL_DMG_DEATH_NORESPAWN)
1134 self.damage_flags |= TFL_DMG_DEATH_NORESPAWN;
1136 // Offsets & origins
1137 if (!self.tur_shotorg) self.tur_shotorg = '50 0 50';
1139 // End of default & sanety checks, start building the turret.
1142 self.tur_head = spawn();
1143 self.tur_head.netname = self.tur_head.classname = "turret_head";
1144 self.tur_head.team = self.team;
1145 self.tur_head.owner = self;
1148 if(!self.tur_defend)
1149 if (self.target != "")
1151 self.tur_defend = find(world, targetname, self.target);
1152 if (self.tur_defend == world)
1155 dprint("Turret has invalid defendpoint!\n");
1159 // Put pices in place
1160 if not (self.turrcaps_flags & TFL_TURRCAPS_HEADATTACHED)
1161 setorigin(self.tur_head,self.origin);
1163 // In target defend mode, aim on the spot to defend when idle.
1164 if(self.turrcaps_flags & TFL_TURRCAPS_HEADATTACHED)
1166 if (self.tur_defend)
1167 self.idle_aim = self.tur_head.angles + angleofs(self.tur_head,self.tur_defend);
1169 self.idle_aim = '0 0 0';
1173 if (self.tur_defend)
1174 self.idle_aim = self.tur_head.angles + angleofs(self.tur_head,self.tur_defend);
1176 self.idle_aim = self.angles;
1179 if not (self.turrcaps_flags & TFL_TURRCAPS_HEADATTACHED)
1180 self.tur_head.angles = self.idle_aim;
1185 self.tur_health = self.health;
1186 self.tur_head.health = self.health;
1188 self.solid = SOLID_BBOX;
1189 self.tur_head.solid = SOLID_BBOX;
1191 self.takedamage = DAMAGE_AIM;
1192 self.tur_head.takedamage = DAMAGE_AIM;
1194 self.movetype = MOVETYPE_NOCLIP;
1195 self.tur_head.movetype = MOVETYPE_NOCLIP;
1198 if (self.team == COLOR_TEAM1) self.colormod = '1.4 0.8 0.8';
1199 if (self.team == COLOR_TEAM2) self.colormod = '0.8 0.8 1.4';
1201 // Attach stdprocs. override when and what needed
1202 if (self.turrcaps_flags & TFL_TURRCAPS_SUPPORT)
1204 self.turret_score_target = turret_stdproc_targetscore_support;
1205 self.turret_firecheckfunc = turret_stdproc_firecheck;
1206 self.turret_firefunc = turret_stdproc_fire;
1207 //self.turret_postthink = turret_stdproc_nothing;
1208 self.event_damage = turret_stdproc_damage;
1209 self.tur_head.event_damage = turret_stdproc_damage;
1213 self.turret_score_target = turret_stdproc_targetscore_generic;
1214 self.turret_firecheckfunc = turret_stdproc_firecheck;
1215 self.turret_firefunc = turret_stdproc_fire;
1216 //self.turret_postthink = turret_stdproc_nothing;
1217 self.event_damage = turret_stdproc_damage;
1218 self.tur_head.event_damage = turret_stdproc_damage;
1219 //self.turret_addtarget = turret_stdproc_false;
1222 self.use = turret_stdproc_use;
1223 self.bot_attack = TRUE;
1225 // Initiate the main AI loop
1227 self.think = turret_link;
1229 self.think = turret_think;
1231 self.nextthink = time + self.ticrate;
1233 self.tur_head.team = self.team;
1234 self.view_ofs = '0 0 0';
1237 self.tur_dbg_start = self.nextthink;
1238 while (vlen(self.tur_dbg_rvec) < 2)
1239 self.tur_dbg_rvec = randomvec() * 4;
1241 self.tur_dbg_rvec_x = fabs(self.tur_dbg_rvec_x);
1242 self.tur_dbg_rvec_y = fabs(self.tur_dbg_rvec_y);
1243 self.tur_dbg_rvec_z = fabs(self.tur_dbg_rvec_z);
1247 self.classname = "turret_main";
1249 self.tur_active = 1;
1251 // In ONS mode, and linked to a ONS ent. need to call the use to set team.
1252 if (g_onslaught && ee)