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)
29 if not (ent.turret_scale_damage) ent.turret_scale_damage = 1;
30 if not (ent.turret_scale_range) ent.turret_scale_range = 1;
31 if not (ent.turret_scale_refire) ent.turret_scale_refire = 1;
32 if not (ent.turret_scale_ammo) ent.turret_scale_ammo = 1;
33 if not (ent.turret_scale_aim) ent.turret_scale_aim = 1;
34 if not (ent.turret_scale_health) ent.turret_scale_health = 1;
35 if not (ent.turret_scale_respawn) ent.turret_scale_respawn = 1;
37 sbase = strcat(cvar_base,unitname);
41 ent.tur_head.avelocity = '0 0 0';
43 ent.tur_head.angles = '0 0 0';
46 ent.health = cvar(strcat(sbase,"_health")) * ent.turret_scale_health;
47 ent.respawntime = cvar(strcat(sbase,"_respawntime")) * ent.turret_scale_respawn;
49 ent.shot_dmg = cvar(strcat(sbase,"_shot_dmg")) * ent.turret_scale_damage;
50 ent.shot_refire = cvar(strcat(sbase,"_shot_refire")) * ent.turret_scale_refire;
51 ent.shot_radius = cvar(strcat(sbase,"_shot_radius")) * ent.turret_scale_damage;
52 ent.shot_speed = cvar(strcat(sbase,"_shot_speed"));
53 ent.shot_spread = cvar(strcat(sbase,"_shot_spread"));
54 ent.shot_force = cvar(strcat(sbase,"_shot_force")) * ent.turret_scale_damage;
55 ent.shot_volly = cvar(strcat(sbase,"_shot_volly"));
56 ent.shot_volly_refire = cvar(strcat(sbase,"_shot_volly_refire")) * ent.turret_scale_refire;
58 ent.target_range = cvar(strcat(sbase,"_target_range")) * ent.turret_scale_range;
59 ent.target_range_min = cvar(strcat(sbase,"_target_range_min")) * ent.turret_scale_range;
60 ent.target_range_optimal = cvar(strcat(sbase,"_target_range_optimal")) * ent.turret_scale_range;
61 //ent.target_range_fire = cvar(strcat(sbase,"_target_range_fire")) * ent.turret_scale_range;
63 ent.target_select_rangebias = cvar(strcat(sbase,"_target_select_rangebias"));
64 ent.target_select_samebias = cvar(strcat(sbase,"_target_select_samebias"));
65 ent.target_select_anglebias = cvar(strcat(sbase,"_target_select_anglebias"));
66 ent.target_select_playerbias = cvar(strcat(sbase,"_target_select_playerbias"));
67 //ent.target_select_fov = cvar(cvar_gets(sbase,"_target_select_fov"));
69 ent.ammo_max = cvar(strcat(sbase,"_ammo_max")) * ent.turret_scale_ammo;
70 ent.ammo_recharge = cvar(strcat(sbase,"_ammo_recharge")) * ent.turret_scale_ammo;
72 ent.aim_firetolerance_dist = cvar(strcat(sbase,"_aim_firetolerance_dist"));
73 ent.aim_speed = cvar(strcat(sbase,"_aim_speed")) * ent.turret_scale_aim;
74 ent.aim_maxrot = cvar(strcat(sbase,"_aim_maxrot"));
75 ent.aim_maxpitch = cvar(strcat(sbase,"_aim_maxpitch"));
77 ent.track_type = cvar(strcat(sbase,"_track_type"));
78 ent.track_accel_pitch = cvar(strcat(sbase,"_track_accel_pitch"));
79 ent.track_accel_rot = cvar(strcat(sbase,"_track_accel_rot"));
80 ent.track_blendrate = cvar(strcat(sbase,"_track_blendrate"));
83 if(ent.turret_respawnhook)
84 ent.turret_respawnhook();
89 ** updates enemy distances, predicted impact point/time
90 ** and updated aim<->predict impact distance.
92 void turret_do_updates(entity t_turret)
94 vector enemy_pos, oldpos;
100 enemy_pos = real_origin(self.enemy);
102 turret_tag_fire_update();
104 self.tur_shotdir_updated = v_forward;
105 self.tur_dist_enemy = vlen(self.tur_shotorg - enemy_pos);
106 self.tur_dist_aimpos = vlen(self.tur_shotorg - self.tur_aimpos);
108 if((self.firecheck_flags & TFL_FIRECHECK_VERIFIED) && (self.enemy))
110 oldpos = self.enemy.origin;
111 setorigin(self.enemy,self.tur_aimpos);
112 tracebox(self.tur_shotorg, '-1 -1 -1','1 1 1',self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos),MOVE_NORMAL,self);
113 setorigin(self.enemy,oldpos);
115 if(trace_ent == self.enemy)
116 self.tur_dist_impact_to_aimpos = 0;
118 self.tur_dist_impact_to_aimpos = vlen(trace_endpos - self.tur_aimpos);
123 tracebox(self.tur_shotorg, '-1 -1 -1','1 1 1', self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos),MOVE_NORMAL,self);
125 self.tur_dist_impact_to_aimpos = vlen(trace_endpos - self.tur_aimpos) - (vlen(self.enemy.maxs - self.enemy.mins) * 0.5);
126 self.tur_impactent = trace_ent;
127 self.tur_impacttime = vlen(self.tur_shotorg - trace_endpos) / self.shot_speed;
133 vector turret_fovsearch_pingpong()
136 if(self.phase < time)
138 if( self.tur_head.phase )
139 self.tur_head.phase = 0;
141 self.tur_head.phase = 1;
142 self.phase = time + 5;
145 if( self.tur_head.phase)
146 wish_angle = self.idle_aim + '0 1 0' * (self.aim_maxrot * (self.target_select_fov / 360));
148 wish_angle = self.idle_aim - '0 1 0' * (self.aim_maxrot * (self.target_select_fov / 360));
153 vector turret_fovsearch_steprot()
158 wish_angle = self.tur_head.angles;
159 wish_angle_x = self.idle_aim_x;
161 if (self.phase < time)
163 //rot_add = self.aim_maxrot / self.target_select_fov;
164 wish_angle_y += (self.target_select_fov * 2);
166 if(wish_angle_y > 360)
167 wish_angle_y = wish_angle_y - 360;
169 self.phase = time + 1.5;
175 vector turret_fovsearch_random()
179 if (self.phase < time)
181 wish_angle_y = random() * self.aim_maxrot;
185 wish_angle_x = random() * self.aim_maxpitch;
189 self.phase = time + 5;
191 self.tur_aimpos = wish_angle;
194 return self.idle_aim + self.tur_aimpos;
199 ** Handles head rotation according to
200 ** the units .track_type and .track_flags
202 void turret_stdproc_track()
204 vector target_angle; // This is where we want to aim
205 vector move_angle; // This is where we can aim
208 if (self.track_flags == TFL_TRACK_NO)
211 if not (self.tur_active)
212 target_angle = self.idle_aim - ('1 0 0' * self.aim_maxpitch);
213 else if (self.enemy == world)
216 target_angle = self.idle_aim + self.angles;
218 target_angle = vectoangles(normalize(self.tur_aimpos - self.tur_shotorg));
222 target_angle = vectoangles(normalize(self.tur_aimpos - self.tur_shotorg));
225 self.tur_head.angles_x = anglemods(self.tur_head.angles_x);
226 self.tur_head.angles_y = anglemods(self.tur_head.angles_y);
228 // Find the diffrence between where we currently aim and where we want to aim
229 move_angle = target_angle - (self.angles + self.tur_head.angles);
230 move_angle = shortangle_vxy(move_angle,(self.angles + self.tur_head.angles));
232 switch(self.track_type)
234 case TFL_TRACKTYPE_STEPMOTOR:
235 f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
236 if (self.track_flags & TFL_TRACK_PITCH)
238 self.tur_head.angles_x += bound(-f_tmp,move_angle_x, f_tmp);
239 if(self.tur_head.angles_x > self.aim_maxpitch)
240 self.tur_head.angles_x = self.aim_maxpitch;
242 if(self.tur_head.angles_x < -self.aim_maxpitch)
243 self.tur_head.angles_x = self.aim_maxpitch;
246 if (self.track_flags & TFL_TRACK_ROT)
248 self.tur_head.angles_y += bound(-f_tmp, move_angle_y, f_tmp);
249 if(self.tur_head.angles_y > self.aim_maxrot)
250 self.tur_head.angles_y = self.aim_maxrot;
252 if(self.tur_head.angles_y < -self.aim_maxrot)
253 self.tur_head.angles_y = self.aim_maxrot;
258 case TFL_TRACKTYPE_FLUIDINERTIA:
259 f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
260 move_angle_x = bound(-self.aim_speed, move_angle_x * self.track_accel_pitch * f_tmp, self.aim_speed);
261 move_angle_y = bound(-self.aim_speed, move_angle_y * self.track_accel_rot * f_tmp, self.aim_speed);
262 move_angle = (self.tur_head.avelocity * self.track_blendrate) + (move_angle * (1 - self.track_blendrate));
265 case TFL_TRACKTYPE_FLUIDPRECISE:
267 move_angle_y = bound(-self.aim_speed, move_angle_y, self.aim_speed);
268 move_angle_x = bound(-self.aim_speed, move_angle_x, self.aim_speed);
274 if (self.track_flags & TFL_TRACK_PITCH)
276 self.tur_head.avelocity_x = move_angle_x;
277 if((self.tur_head.angles_x + self.tur_head.avelocity_x * self.ticrate) > self.aim_maxpitch)
279 self.tur_head.avelocity_x = 0;
280 self.tur_head.angles_x = self.aim_maxpitch;
283 if((self.tur_head.angles_x + self.tur_head.avelocity_x * self.ticrate) < -self.aim_maxpitch)
285 self.tur_head.avelocity_x = 0;
286 self.tur_head.angles_x = -self.aim_maxpitch;
291 if (self.track_flags & TFL_TRACK_ROT)
293 self.tur_head.avelocity_y = move_angle_y;
295 if((self.tur_head.angles_y + self.tur_head.avelocity_y * self.ticrate) > self.aim_maxrot)
297 self.tur_head.avelocity_y = 0;
298 self.tur_head.angles_y = self.aim_maxrot;
301 if((self.tur_head.angles_y + self.tur_head.avelocity_y * self.ticrate) < -self.aim_maxrot)
303 self.tur_head.avelocity_y = 0;
304 self.tur_head.angles_y = -self.aim_maxrot;
315 + TFL_FIRECHECK_WORLD
317 + TFL_FIRECHECK_DISTANCES
319 + TFL_FIRECHECK_AIMDIST
320 + TFL_FIRECHECK_REALDIST
321 - TFL_FIRECHECK_ANGLEDIST
322 - TFL_FIRECHECK_TEAMCECK
324 + TFL_FIRECHECK_OWM_AMMO
325 + TFL_FIRECHECK_OTHER_AMMO
326 + TFL_FIRECHECK_REFIRE
330 ** Preforms pre-fire checks based on the uints firecheck_flags
332 float turret_stdproc_firecheck()
334 // This one just dont care =)
335 if (self.firecheck_flags & TFL_FIRECHECK_NO) return 1;
338 if (self.firecheck_flags & TFL_FIRECHECK_REFIRE)
339 if (self.attack_finished_single > time) return 0;
341 // Special case: volly fire turret that has to fire a full volly if a shot was fired.
342 if (self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
343 if (self.volly_counter != self.shot_volly)
344 if(self.ammo >= self.shot_dmg)
347 // Lack of zombies makes shooting dead things unnecessary :P
348 if (self.firecheck_flags & TFL_FIRECHECK_DEAD)
349 if (self.enemy.deadflag != DEAD_NO)
352 // Plz stop killing the world!
353 if (self.firecheck_flags & TFL_FIRECHECK_WORLD)
354 if (self.enemy == world)
358 if (self.firecheck_flags & TFL_FIRECHECK_OWM_AMMO)
359 if (self.ammo < self.shot_dmg)
362 // Other's ammo? (support-supply units)
363 if (self.firecheck_flags & TFL_FIRECHECK_OTHER_AMMO)
364 if (self.enemy.ammo >= self.enemy.ammo_max)
367 // Target of opertunity?
368 if(turret_validate_target(self, self.tur_impactent, self.target_validate_flags) > 0)
370 self.enemy = self.tur_impactent;
374 if (self.firecheck_flags & TFL_FIRECHECK_DISTANCES)
377 if (self.tur_dist_aimpos < self.target_range_min)
378 if(turret_validate_target(self, self.tur_impactent, self.target_validate_flags) > 0)
379 return 1; // Target of opertunity?
385 if (self.firecheck_flags & TFL_FIRECHECK_AFF)
386 if (self.tur_impactent.team == self.team)
389 // aim<->predicted impact
390 if (self.firecheck_flags & TFL_FIRECHECK_AIMDIST)
391 if (self.tur_dist_impact_to_aimpos > self.aim_firetolerance_dist)
395 if (self.shot_volly > 1)
396 if (self.volly_counter == self.shot_volly)
397 if (self.ammo < (self.shot_dmg * self.shot_volly))
400 if(self.firecheck_flags & TFL_FIRECHECK_VERIFIED)
401 if(self.tur_impactent != self.enemy)
408 + TFL_TARGETSELECT_NO
409 + TFL_TARGETSELECT_LOS
410 + TFL_TARGETSELECT_PLAYERS
411 + TFL_TARGETSELECT_MISSILES
412 - TFL_TARGETSELECT_TRIGGERTARGET
413 + TFL_TARGETSELECT_ANGLELIMITS
414 + TFL_TARGETSELECT_RANGELIMTS
415 + TFL_TARGETSELECT_TEAMCHECK
416 - TFL_TARGETSELECT_NOBUILTIN
417 + TFL_TARGETSELECT_OWNTEAM
421 ** Evaluate a entity for target valitity based on validate_flags
422 ** NOTE: the caller must check takedamage before calling this, to inline this check.
424 float turret_validate_target(entity e_turret, entity e_target, float validate_flags)
428 //if(!validate_flags & TFL_TARGETSELECT_NOBUILTIN)
431 if(e_target.owner == e_turret)
434 if not(checkpvs(e_target.origin, e_turret))
441 if (substring(e_target.classname, 0, 10) == "onslaught_") // don't attack onslaught targets, that's the player's job!
444 if (validate_flags & TFL_TARGETSELECT_NO)
447 // If only this was used more..
448 if (e_target.flags & FL_NOTARGET)
452 if (e_target.health < 0)
456 if (e_target.flags & FL_CLIENT)
458 if not (validate_flags & TFL_TARGETSELECT_PLAYERS)
461 if (e_target.deadflag != DEAD_NO)
466 if (validate_flags & TFL_TARGETSELECT_NOTURRETS)
467 if (e_target.turret_firefunc || e_target.owner.tur_head == e_target)
468 if(e_target.team != e_turret.team) // Dont break support units.
472 if (e_target.flags & FL_PROJECTILE)
473 if not (validate_flags & TFL_TARGETSELECT_MISSILES)
476 if (validate_flags & TFL_TARGETSELECT_MISSILESONLY)
477 if not (e_target.flags & FL_PROJECTILE)
481 if (validate_flags & TFL_TARGETSELECT_TEAMCHECK)
483 if (validate_flags & TFL_TARGETSELECT_OWNTEAM)
485 if (e_target.team != e_turret.team)
488 if (e_turret.team != e_target.owner.team)
493 if (e_target.team == e_turret.team)
496 if (e_turret.team == e_target.owner.team)
502 tvt_dist = vlen(e_turret.origin - real_origin(e_target));
503 if (validate_flags & TFL_TARGETSELECT_RANGELIMTS)
505 if (tvt_dist < e_turret.target_range_min)
508 if (tvt_dist > e_turret.target_range)
512 // Can we even aim this thing?
513 tvt_thadv = angleofs3(e_turret.tur_head.origin, e_turret.angles + e_turret.tur_head.angles, e_target);
514 tvt_tadv = shortangle_vxy(angleofs(e_turret, e_target), e_turret.angles);
515 tvt_thadf = vlen(tvt_thadv);
516 tvt_tadf = vlen(tvt_tadv);
519 if(validate_flags & TFL_TARGETSELECT_FOV)
521 if(e_turret.target_select_fov < tvt_thadf)
526 if (validate_flags & TFL_TARGETSELECT_ANGLELIMITS)
528 if (fabs(tvt_tadv_x) > e_turret.aim_maxpitch)
531 if (fabs(tvt_tadv_y) > e_turret.aim_maxrot)
536 if (validate_flags & TFL_TARGETSELECT_LOS)
538 v_tmp = real_origin(e_target) + ((e_target.mins + e_target.maxs) * 0.5);
540 traceline(e_turret.tur_shotorg, v_tmp, 0, e_turret);
542 if (e_turret.aim_firetolerance_dist < vlen(v_tmp - trace_endpos))
546 if (e_target.classname == "grapplinghook")
550 if (e_target.classname == "func_button")
554 #ifdef TURRET_DEBUG_TARGETSELECT
555 dprint("Target:",e_target.netname," is a valid target for ",e_turret.netname,"\n");
561 entity turret_select_target()
563 entity e; // target looper entity
564 float score; // target looper entity score
565 entity e_enemy; // currently best scoreing target
566 float m_score; // currently best scoreing target's score
570 if(self.enemy.takedamage)
571 if(turret_validate_target(self,self.enemy,self.target_validate_flags) > 0)
573 e_enemy = self.enemy;
574 m_score = self.turret_score_target(self,e_enemy) * self.target_select_samebias;
579 e = findradius(self.origin, self.target_range);
581 // Nothing to aim at?
589 if (turret_validate_target(self, e, self.target_select_flags) > 0)
591 score = self.turret_score_target(self,e);
592 if ((score > m_score) && (score > 0))
609 self.nextthink = time + self.ticrate;
611 // ONS uses somewhat backwards linking.
617 e = find(world, targetname,self.target);
622 if (self.team != self.tur_head.team)
623 turret_stdproc_respawn();
627 if (self.tur_dbg_tmr1 < time)
629 if (self.enemy) paint_target (self.enemy,128,self.tur_dbg_rvec,0.9);
630 paint_target(self,256,self.tur_dbg_rvec,0.9);
631 self.tur_dbg_tmr1 = time + 1;
636 if not (self.spawnflags & TSF_NO_AMMO_REGEN)
637 if (self.ammo < self.ammo_max)
638 self.ammo = min(self.ammo + self.ammo_recharge, self.ammo_max);
640 if (self.health < (self.tur_health * 0.5))
642 te_spark(self.origin + '0 0 40', randomvec() * 256 + '0 0 256', 16);
644 // Inactive turrets needs to run the think loop,
645 // So they can handle animation and wake up if need be.
646 if not (self.tur_active)
648 turret_stdproc_track();
652 // This is typicaly used for zaping every target in range
653 // turret_fusionreactor uses this to recharge friendlys.
654 if (self.shoot_flags & TFL_SHOOT_HITALLVALID)
656 // Do a self.turret_fire for every valid target.
657 e = findradius(self.origin,self.target_range);
662 if (turret_validate_target(self,e,self.target_validate_flags))
666 turret_do_updates(self);
668 if (self.turret_firecheckfunc())
677 else if(self.shoot_flags & TFL_SHOOT_CUSTOM)
679 // This one is doing something.. oddball. assume its handles what needs to be handled.
682 if not(self.aim_flags & TFL_AIM_NO)
683 self.tur_aimpos = turret_stdproc_aim_generic();
686 if not(self.track_flags & TFL_TRACK_NO)
687 turret_stdproc_track();
689 turret_do_updates(self);
692 if (self.turret_firecheckfunc())
697 // Special case for volly always. if it fired once it must compleate the volly.
698 if(self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
699 if(self.volly_counter != self.shot_volly)
701 // Predict or whatnot
702 if not(self.aim_flags & TFL_AIM_NO)
703 self.tur_aimpos = turret_stdproc_aim_generic();
706 if not(self.track_flags & TFL_TRACK_NO)
707 turret_stdproc_track();
709 turret_do_updates(self);
712 if (self.turret_firecheckfunc() != 0)
715 if(self.turret_postthink)
716 self.turret_postthink();
721 // Check if we have a vailid enemy, and try to find one if we dont.
723 // g_turrets_targetscan_maxdelay forces a target re-scan at least this often
724 float do_target_scan;
725 if((self.target_select_time + autocvar_g_turrets_targetscan_maxdelay) < time)
728 // Old target (if any) invalid?
729 if (turret_validate_target(self, self.enemy, self.target_validate_flags) <= 0)
735 // But never more often then g_turrets_targetscan_mindelay!
736 if (self.target_select_time + autocvar_g_turrets_targetscan_mindelay > time)
741 self.enemy = turret_select_target();
742 self.target_select_time = time;
745 // No target, just go to idle, do any custom stuff and bail.
746 if (self.enemy == world)
749 if not(self.track_flags & TFL_TRACK_NO)
750 turret_stdproc_track();
752 // do any per-turret stuff
753 if(self.turret_postthink)
754 self.turret_postthink();
760 self.lip = time + autocvar_g_turrets_aimidle_delay; // Keep track of the last time we had a target.
763 if not(self.aim_flags & TFL_AIM_NO)
764 self.tur_aimpos = turret_stdproc_aim_generic();
767 if not(self.track_flags & TFL_TRACK_NO)
768 turret_stdproc_track();
770 turret_do_updates(self);
773 if (self.turret_firecheckfunc())
777 // do any custom per-turret stuff
778 if(self.turret_postthink)
779 self.turret_postthink();
784 if (autocvar_g_turrets_nofire != 0)
787 self.turret_firefunc();
789 self.attack_finished_single = time + self.shot_refire;
790 self.ammo -= self.shot_dmg;
791 self.volly_counter = self.volly_counter - 1;
793 if (self.volly_counter <= 0)
795 self.volly_counter = self.shot_volly;
797 if (self.shoot_flags & TFL_SHOOT_CLEARTARGET)
800 if (self.shot_volly > 1)
801 self.attack_finished_single = time + self.shot_volly_refire;
805 if (self.enemy) paint_target3(self.tur_aimpos, 64, self.tur_dbg_rvec, self.tur_impacttime + 0.25);
809 void turret_stdproc_fire()
811 dprint("^1Bang, ^3your dead^7 ",self.enemy.netname,"! ^1(turret with no real firefunc)\n");
815 When .used a turret switch team to activator.team.
816 If activator is world, the turret go inactive.
818 void turret_stdproc_use()
820 dprint("Turret ",self.netname, " used by ", activator.classname, "\n");
822 self.team = activator.team;
833 //Net_LinkEntity(self, FALSE, 0, Turret_SendEntity);
834 self.think = turret_think;
835 self.nextthink = time;
838 void turrets_manager_think()
840 self.nextthink = time + 1;
843 if (autocvar_g_turrets_reloadcvars == 1)
848 if (e.turrcaps_flags & TFL_TURRCAPS_ISTURRET)
850 load_unit_settings(e,e.cvar_basename,1);
851 if(e.turret_postthink)
852 e.turret_postthink();
857 cvar_set("g_turrets_reloadcvars","0");
862 * Standard turret initialization. use this!
863 * (unless you have a very good reason not to)
864 * if the return value is 0, the turret should be removed.
866 float turret_stdproc_init (string cvar_base_name, float csqc_shared, string base, string head)
870 // Are turrets allowed?
871 if (autocvar_g_turrets == 0)
875 e = find(world, classname, "turret_manager");
881 setorigin(e,'0 0 0');
882 setmodel(e,"models/turrets/plasma.md3");
884 v = gettaginfo(e,gettagindex(e,"tag_fire"));
887 //objerror("^1ERROR: Engine is borken! Turrets will NOT work. force g_turrets to 0 to run maps with turrets anyway.");
893 e.classname = "turret_manager";
894 e.think = turrets_manager_think;
895 e.nextthink = time + 2;
900 dprint("WARNING: turret requested csqc_shared but this is not implemented. Expect strange things to happen.\n");
904 if not (self.spawnflags & TSF_SUSPENDED)
905 droptofloor_builtin();
907 // Terrainbase spawnflag. This puts a enlongated model
908 // under the turret, so it looks ok on uneaven surfaces.
909 if (self.spawnflags & TSF_TERRAINBASE)
913 setmodel(tb,"models/turrets/terrainbase.md3");
914 setorigin(tb,self.origin);
915 tb.solid = SOLID_BBOX;
918 self.cvar_basename = cvar_base_name;
919 load_unit_settings(self,self.cvar_basename, 0);
921 // Handle turret teams.
922 if (autocvar_g_assault != 0)
925 self.team = 14; // Assume turrets are on the defending side if not explicitly set otehrwize
927 else if not (teamplay)
928 self.team = MAX_SHOT_DISTANCE; // Group all turrets into the same team, so they dont kill eachother.
929 else if(g_onslaught && self.targetname)
931 e = find(world,target,self.targetname);
939 self.team = MAX_SHOT_DISTANCE; // Group all turrets into the same team, so they dont kill eachother.
942 * Try to guess some reasonaly defaults
943 * for missing params and do sanety checks
944 * thise checks could produce some "interesting" results
945 * if it hits a glitch in my logic :P so try to set as mutch
946 * as possible beforehand.
948 if (self.turrcaps_flags & TFL_TURRCAPS_SUPPORT)
949 self.ticrate = 0.2; // Support units generaly dont need to have a high speed ai-loop
951 self.ticrate = 0.1; // 10 fps for normal turrets
953 self.ticrate = bound(sys_frametime, self.ticrate, 60); // keep it sane
956 if (self.netname == "")
957 self.netname = self.classname;
959 if not (self.respawntime)
960 self.respawntime = 60;
961 self.respawntime = max(-1, self.respawntime);
965 self.tur_health = max(1, self.health);
967 if not (self.turrcaps_flags)
968 self.turrcaps_flags = TFL_TURRCAPS_RADIUSDMG | TFL_TURRCAPS_MEDPROJ | TFL_TURRCAPS_PLAYERKILL;
970 if not (self.damage_flags)
971 self.damage_flags = TFL_DMG_YES | TFL_DMG_RETALIATE | TFL_DMG_AIMSHAKE;
974 if not (self.shot_refire)
975 self.shot_refire = 1;
976 self.shot_refire = bound(0.01, self.shot_refire, 9999);
978 if not (self.shot_dmg)
979 self.shot_dmg = self.shot_refire * 50;
980 self.shot_dmg = max(1, self.shot_dmg);
982 if not (self.shot_radius)
983 self.shot_radius = self.shot_dmg * 0.5;
984 self.shot_radius = max(1, self.shot_radius);
986 if not (self.shot_speed)
987 self.shot_speed = 2500;
988 self.shot_speed = max(1, self.shot_speed);
990 if not (self.shot_spread)
991 self.shot_spread = 0.0125;
992 self.shot_spread = bound(0.0001, self.shot_spread, 500);
994 if not (self.shot_force)
995 self.shot_force = self.shot_dmg * 0.5 + self.shot_radius * 0.5;
996 self.shot_force = bound(0.001, self.shot_force, 5000);
998 if not (self.shot_volly)
1000 self.shot_volly = bound(1, self.shot_volly, floor(self.ammo_max / self.shot_dmg));
1002 if not (self.shot_volly_refire)
1003 self.shot_volly_refire = self.shot_refire * self.shot_volly;
1004 self.shot_volly_refire = bound(self.shot_refire, self.shot_volly_refire, 60);
1006 if not (self.firecheck_flags)
1007 self.firecheck_flags = TFL_FIRECHECK_WORLD | TFL_FIRECHECK_DEAD | TFL_FIRECHECK_DISTANCES |
1008 TFL_FIRECHECK_LOS | TFL_FIRECHECK_AIMDIST | TFL_FIRECHECK_TEAMCECK |
1009 TFL_FIRECHECK_OWM_AMMO | TFL_FIRECHECK_REFIRE | TFL_FIRECHECK_WORLD;
1012 if not (self.target_range)
1013 self.target_range = self.shot_speed * 0.5;
1014 self.target_range = bound(0, self.target_range, MAX_SHOT_DISTANCE);
1016 if not (self.target_range_min)
1017 self.target_range_min = self.shot_radius * 2;
1018 self.target_range_min = bound(0, self.target_range_min, MAX_SHOT_DISTANCE);
1020 if not (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 not (self.aim_maxrot)
1027 self.aim_maxrot = 90;
1028 self.aim_maxrot = bound(0, self.aim_maxrot, 360);
1030 if not (self.aim_maxpitch)
1031 self.aim_maxpitch = 20;
1032 self.aim_maxpitch = bound(0, self.aim_maxpitch, 90);
1034 if not (self.aim_speed)
1035 self.aim_speed = 36;
1036 self.aim_speed = bound(0.1, self.aim_speed, 1000);
1038 if not (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 not (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 if not (self.track_type)
1050 self.track_type = TFL_TRACKTYPE_STEPMOTOR;
1052 if (self.track_type != TFL_TRACKTYPE_STEPMOTOR)
1054 // Fluid / Ineria mode. Looks mutch nicer.
1055 // Can reduce aim preformance alot, needs a bit diffrent aimspeed
1057 if not (self.aim_speed)
1058 self.aim_speed = 180;
1059 self.aim_speed = bound(0.1, self.aim_speed, 1000);
1061 if not (self.track_accel_pitch)
1062 self.track_accel_pitch = 0.5;
1064 if not (self.track_accel_rot)
1065 self.track_accel_rot = 0.5;
1067 if not (self.track_blendrate)
1068 self.track_blendrate = 0.35;
1071 if (!self.track_flags)
1072 self.track_flags = TFL_TRACK_PITCH | TFL_TRACK_ROT;
1075 // Target selection stuff.
1076 if not (self.target_select_rangebias)
1077 self.target_select_rangebias = 1;
1078 self.target_select_rangebias = bound(-10, self.target_select_rangebias, 10);
1080 if not (self.target_select_samebias)
1081 self.target_select_samebias = 1;
1082 self.target_select_samebias = bound(-10, self.target_select_samebias, 10);
1084 if not (self.target_select_anglebias)
1085 self.target_select_anglebias = 1;
1086 self.target_select_anglebias = bound(-10, self.target_select_anglebias, 10);
1088 if not (self.target_select_missilebias)
1089 self.target_select_missilebias = -10;
1091 self.target_select_missilebias = bound(-10, self.target_select_missilebias, 10);
1092 self.target_select_playerbias = bound(-10, self.target_select_playerbias, 10);
1094 if not (self.target_select_flags)
1096 self.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_TEAMCHECK
1097 | TFL_TARGETSELECT_RANGELIMTS | TFL_TARGETSELECT_ANGLELIMITS;
1099 if (self.turrcaps_flags & TFL_TURRCAPS_MISSILEKILL)
1100 self.target_select_flags |= TFL_TARGETSELECT_MISSILES;
1102 if (self.turrcaps_flags & TFL_TURRCAPS_PLAYERKILL)
1103 self.target_select_flags |= TFL_TARGETSELECT_PLAYERS;
1105 // self.target_select_flags = TFL_TARGETSELECT_NO;
1108 self.target_validate_flags = self.target_select_flags;
1112 if not (self.ammo_max)
1113 self.ammo_max = self.shot_dmg * 10;
1114 self.ammo_max = max(self.shot_dmg, self.ammo_max);
1117 self.ammo = self.shot_dmg * 5;
1118 self.ammo = bound(0,self.ammo, self.ammo_max);
1120 if not (self.ammo_recharge)
1121 self.ammo_recharge = self.shot_dmg * 0.5;
1122 self.ammo_recharge = max(0 ,self.ammo_recharge);
1124 // Convert the recharge from X per sec to X per ticrate
1125 self.ammo_recharge = self.ammo_recharge * self.ticrate;
1127 if not (self.ammo_flags)
1128 self.ammo_flags = TFL_AMMO_ENERGY | TFL_AMMO_RECHARGE;
1131 if(self.spawnflags & TSL_NO_RESPAWN)
1132 if not (self.damage_flags & TFL_DMG_DEATH_NORESPAWN)
1133 self.damage_flags |= TFL_DMG_DEATH_NORESPAWN;
1135 // Offsets & origins
1136 if (!self.tur_shotorg) self.tur_shotorg = '50 0 50';
1139 if(MUTATOR_CALLHOOK(TurretSpawn))
1142 // End of default & sanety checks, start building the turret.
1145 self.tur_head = spawn();
1146 self.tur_head.netname = self.tur_head.classname = "turret_head";
1147 self.tur_head.team = self.team;
1148 self.tur_head.owner = self;
1150 setmodel(self, base);
1151 setmodel(self.tur_head, head);
1153 setsize(self, '-32 -32 0', '32 32 64');
1154 setsize(self.tur_head, '0 0 0', '0 0 0');
1156 setorigin(self.tur_head, '0 0 0');
1157 setattachment(self.tur_head, self, "tag_head");
1162 self.tur_health = self.health;
1163 self.solid = SOLID_BBOX;
1164 self.tur_head.solid = SOLID_NOT;
1165 self.takedamage = DAMAGE_AIM;
1166 self.tur_head.takedamage = DAMAGE_NO;
1167 self.movetype = MOVETYPE_NOCLIP;
1168 self.tur_head.movetype = MOVETYPE_NOCLIP;
1171 if not (self.tur_defend)
1172 if (self.target != "")
1174 self.tur_defend = find(world, targetname, self.target);
1175 if (self.tur_defend == world)
1178 dprint("Turret has invalid defendpoint!\n");
1182 // In target defend mode, aim on the spot to defend when idle.
1183 if (self.tur_defend)
1184 self.idle_aim = self.tur_head.angles + angleofs(self.tur_head, self.tur_defend);
1186 self.idle_aim = '0 0 0';
1189 if (self.team == COLOR_TEAM1) self.colormod = '1.4 0.8 0.8';
1190 if (self.team == COLOR_TEAM2) self.colormod = '0.8 0.8 1.4';
1192 // Attach stdprocs. override when and what needed
1193 if (self.turrcaps_flags & TFL_TURRCAPS_SUPPORT)
1195 self.turret_score_target = turret_stdproc_targetscore_support;
1196 self.turret_firecheckfunc = turret_stdproc_firecheck;
1197 self.turret_firefunc = turret_stdproc_fire;
1198 self.event_damage = turret_stdproc_damage;
1202 self.turret_score_target = turret_stdproc_targetscore_generic;
1203 self.turret_firecheckfunc = turret_stdproc_firecheck;
1204 self.turret_firefunc = turret_stdproc_fire;
1205 self.event_damage = turret_stdproc_damage;
1208 self.use = turret_stdproc_use;
1209 self.bot_attack = TRUE;
1211 // Initiate the main AI loop
1213 self.think = turret_link;
1215 self.think = turret_think;
1218 self.nextthink = time + 1;
1219 self.nextthink += turret_count * sys_frametime;
1221 self.tur_head.team = self.team;
1222 self.view_ofs = '0 0 0';
1225 self.tur_dbg_start = self.nextthink;
1226 while (vlen(self.tur_dbg_rvec) < 2)
1227 self.tur_dbg_rvec = randomvec() * 4;
1229 self.tur_dbg_rvec_x = fabs(self.tur_dbg_rvec_x);
1230 self.tur_dbg_rvec_y = fabs(self.tur_dbg_rvec_y);
1231 self.tur_dbg_rvec_z = fabs(self.tur_dbg_rvec_z);
1235 self.turrcaps_flags |= TFL_TURRCAPS_ISTURRET;
1237 self.classname = "turret_main";
1239 self.tur_active = 1;
1241 // In ONS mode, and linked to a ONS ent. need to call the use to set team.
1242 if (g_onslaught && ee)
1248 turret_stdproc_respawn();