]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/tturrets/system/system_main.qc
Make most server includes order insensitive
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / tturrets / system / system_main.qc
1 #include "../../_.qh"
2
3 #include "../../g_damage.qh"
4 #include "../../bot/bot.qh"
5
6 #define cvar_base "g_turrets_unit_"
7 .float clientframe;
8 void turrets_setframe(float _frame, float client_only)
9 {
10     if((client_only ? self.clientframe : self.frame ) != _frame)
11     {
12         self.SendFlags |= TNSF_ANIM;
13         self.anim_start_time = time;
14     }
15
16      if(client_only)
17         self.clientframe = _frame;
18     else
19         self.frame = _frame;
20
21 }
22
23 float turret_send(entity to, int sf)
24 {
25
26         WriteByte(MSG_ENTITY, ENT_CLIENT_TURRET);
27         WriteByte(MSG_ENTITY, sf);
28         if(sf & TNSF_SETUP)
29         {
30             WriteByte(MSG_ENTITY, self.turret_type);
31
32             WriteCoord(MSG_ENTITY, self.origin.x);
33             WriteCoord(MSG_ENTITY, self.origin.y);
34             WriteCoord(MSG_ENTITY, self.origin.z);
35
36             WriteAngle(MSG_ENTITY, self.angles.x);
37             WriteAngle(MSG_ENTITY, self.angles.y);
38     }
39
40     if(sf & TNSF_ANG)
41     {
42         WriteShort(MSG_ENTITY, rint(self.tur_head.angles.x));
43         WriteShort(MSG_ENTITY, rint(self.tur_head.angles.y));
44     }
45
46     if(sf & TNSF_AVEL)
47     {
48         WriteShort(MSG_ENTITY, rint(self.tur_head.avelocity.x));
49         WriteShort(MSG_ENTITY, rint(self.tur_head.avelocity.y));
50     }
51
52     if(sf & TNSF_MOVE)
53     {
54         WriteShort(MSG_ENTITY, rint(self.origin.x));
55         WriteShort(MSG_ENTITY, rint(self.origin.y));
56         WriteShort(MSG_ENTITY, rint(self.origin.z));
57
58         WriteShort(MSG_ENTITY, rint(self.velocity.x));
59         WriteShort(MSG_ENTITY, rint(self.velocity.y));
60         WriteShort(MSG_ENTITY, rint(self.velocity.z));
61
62         WriteShort(MSG_ENTITY, rint(self.angles.y));
63     }
64
65     if(sf & TNSF_ANIM)
66     {
67         WriteCoord(MSG_ENTITY, self.anim_start_time);
68         WriteByte(MSG_ENTITY, self.frame);
69     }
70
71     if(sf & TNSF_STATUS)
72     {
73         WriteByte(MSG_ENTITY, self.team);
74
75         if(self.health <= 0)
76             WriteByte(MSG_ENTITY, 0);
77         else
78             WriteByte(MSG_ENTITY, ceil((self.health / self.tur_health) * 255));
79     }
80
81         return true;
82 }
83
84 void load_unit_settings(entity ent, string unitname, float is_reload)
85 {
86     string sbase;
87
88     if (ent == world)
89         return;
90
91     if (!ent.turret_scale_damage)    ent.turret_scale_damage  = 1;
92     if (!ent.turret_scale_range)     ent.turret_scale_range   = 1;
93     if (!ent.turret_scale_refire)    ent.turret_scale_refire  = 1;
94     if (!ent.turret_scale_ammo)      ent.turret_scale_ammo    = 1;
95     if (!ent.turret_scale_aim)       ent.turret_scale_aim     = 1;
96     if (!ent.turret_scale_health)    ent.turret_scale_health  = 1;
97     if (!ent.turret_scale_respawn)   ent.turret_scale_respawn = 1;
98
99     sbase = strcat(cvar_base,unitname);
100     if (is_reload)
101     {
102         ent.enemy = world;
103         ent.tur_head.avelocity = '0 0 0';
104
105         ent.tur_head.angles = '0 0 0';
106     }
107
108     ent.health      = cvar(strcat(sbase,"_health")) * ent.turret_scale_health;
109     ent.respawntime = cvar(strcat(sbase,"_respawntime")) * ent.turret_scale_respawn;
110
111     ent.shot_dmg          = cvar(strcat(sbase,"_shot_dmg")) * ent.turret_scale_damage;
112     ent.shot_refire       = cvar(strcat(sbase,"_shot_refire")) * ent.turret_scale_refire;
113     ent.shot_radius       = cvar(strcat(sbase,"_shot_radius")) * ent.turret_scale_damage;
114     ent.shot_speed        = cvar(strcat(sbase,"_shot_speed"));
115     ent.shot_spread       = cvar(strcat(sbase,"_shot_spread"));
116     ent.shot_force        = cvar(strcat(sbase,"_shot_force")) * ent.turret_scale_damage;
117     ent.shot_volly        = cvar(strcat(sbase,"_shot_volly"));
118     ent.shot_volly_refire = cvar(strcat(sbase,"_shot_volly_refire")) * ent.turret_scale_refire;
119
120     ent.target_range         = cvar(strcat(sbase,"_target_range")) * ent.turret_scale_range;
121     ent.target_range_min     = cvar(strcat(sbase,"_target_range_min")) * ent.turret_scale_range;
122     ent.target_range_optimal = cvar(strcat(sbase,"_target_range_optimal")) * ent.turret_scale_range;
123     //ent.target_range_fire    = cvar(strcat(sbase,"_target_range_fire")) * ent.turret_scale_range;
124
125     ent.target_select_rangebias  = cvar(strcat(sbase,"_target_select_rangebias"));
126     ent.target_select_samebias   = cvar(strcat(sbase,"_target_select_samebias"));
127     ent.target_select_anglebias  = cvar(strcat(sbase,"_target_select_anglebias"));
128     ent.target_select_playerbias = cvar(strcat(sbase,"_target_select_playerbias"));
129     //ent.target_select_fov = cvar(cvar_gets(sbase,"_target_select_fov"));
130
131     ent.ammo_max      = cvar(strcat(sbase,"_ammo_max")) * ent.turret_scale_ammo;
132     ent.ammo_recharge = cvar(strcat(sbase,"_ammo_recharge")) * ent.turret_scale_ammo;
133
134     ent.aim_firetolerance_dist = cvar(strcat(sbase,"_aim_firetolerance_dist"));
135     ent.aim_speed    = cvar(strcat(sbase,"_aim_speed")) * ent.turret_scale_aim;
136     ent.aim_maxrot   = cvar(strcat(sbase,"_aim_maxrot"));
137     ent.aim_maxpitch = cvar(strcat(sbase,"_aim_maxpitch"));
138
139     ent.track_type        = cvar(strcat(sbase,"_track_type"));
140     ent.track_accel_pitch = cvar(strcat(sbase,"_track_accel_pitch"));
141     ent.track_accel_rot   = cvar(strcat(sbase,"_track_accel_rot"));
142     ent.track_blendrate   = cvar(strcat(sbase,"_track_blendrate"));
143
144     if(is_reload)
145         if(ent.turret_respawnhook)
146             ent.turret_respawnhook();
147 }
148
149 void turret_projectile_explode()
150 {
151
152     self.takedamage = DAMAGE_NO;
153     self.event_damage = func_null;
154 #ifdef TURRET_DEBUG
155     float d;
156     d = RadiusDamage (self, self.owner, self.owner.shot_dmg, 0, self.owner.shot_radius, self, world, self.owner.shot_force, self.totalfrags, world);
157     self.owner.tur_dbg_dmg_t_h = self.owner.tur_dbg_dmg_t_h + d;
158     self.owner.tur_dbg_dmg_t_f = self.owner.tur_dbg_dmg_t_f + self.owner.shot_dmg;
159 #else
160     RadiusDamage (self, self.realowner, self.owner.shot_dmg, 0, self.owner.shot_radius, self, world, self.owner.shot_force, self.totalfrags, world);
161 #endif
162     remove(self);
163 }
164
165 void turret_projectile_touch()
166 {
167     PROJECTILE_TOUCH;
168     turret_projectile_explode();
169 }
170
171 void turret_projectile_damage(entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector vforce)
172 {
173     self.velocity  += vforce;
174     self.health    -= damage;
175     //self.realowner  = attacker; // Dont change realowner, it does not make much sense for turrets
176     if(self.health <= 0)
177         W_PrepareExplosionByDamage(self.owner, turret_projectile_explode);
178 }
179
180 entity turret_projectile(string _snd, float _size, float _health, float _death, float _proj_type, float _cull, float _cli_anim)
181 {
182     entity proj;
183
184     sound (self, CH_WEAPON_A, _snd, VOL_BASE, ATTEN_NORM);
185     proj                 = spawn ();
186     setorigin(proj, self.tur_shotorg);
187     setsize(proj, '-0.5 -0.5 -0.5' * _size, '0.5 0.5 0.5' * _size);
188     proj.owner           = self;
189     proj.realowner       = self;
190     proj.bot_dodge       = true;
191     proj.bot_dodgerating = self.shot_dmg;
192     proj.think           = turret_projectile_explode;
193     proj.touch           = turret_projectile_touch;
194     proj.nextthink       = time + 9;
195     proj.movetype        = MOVETYPE_FLYMISSILE;
196     proj.velocity        = normalize(self.tur_shotdir_updated + randomvec() * self.shot_spread) * self.shot_speed;
197     proj.flags           = FL_PROJECTILE;
198     proj.enemy           = self.enemy;
199     proj.totalfrags      = _death;
200     PROJECTILE_MAKETRIGGER(proj);
201     if(_health)
202     {
203         proj.health         = _health;
204         proj.takedamage     = DAMAGE_YES;
205         proj.event_damage   = turret_projectile_damage;
206     }
207     else
208         proj.flags |= FL_NOTARGET;
209
210     CSQCProjectile(proj, _cli_anim, _proj_type, _cull);
211
212     return proj;
213 }
214
215 /**
216 ** updates enemy distances, predicted impact point/time
217 ** and updated aim<->predict impact distance.
218 **/
219 void turret_do_updates(entity t_turret)
220 {
221     vector enemy_pos;
222     entity oldself;
223
224     oldself = self;
225     self = t_turret;
226
227     enemy_pos = real_origin(self.enemy);
228
229     turret_tag_fire_update();
230
231     self.tur_shotdir_updated = v_forward;
232     self.tur_dist_enemy  = vlen(self.tur_shotorg - enemy_pos);
233     self.tur_dist_aimpos = vlen(self.tur_shotorg - self.tur_aimpos);
234
235     /*if((self.firecheck_flags & TFL_FIRECHECK_VERIFIED) && (self.enemy))
236     {
237         oldpos = self.enemy.origin;
238         setorigin(self.enemy, self.tur_aimpos);
239         tracebox(self.tur_shotorg, '-1 -1 -1', '1 1 1', self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos), MOVE_NORMAL,self);
240         setorigin(self.enemy, oldpos);
241
242         if(trace_ent == self.enemy)
243             self.tur_dist_impact_to_aimpos = 0;
244         else
245             self.tur_dist_impact_to_aimpos = vlen(trace_endpos - self.tur_aimpos);
246     }
247     else*/
248         tracebox(self.tur_shotorg, '-1 -1 -1','1 1 1', self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos), MOVE_NORMAL,self);
249
250         self.tur_dist_impact_to_aimpos = vlen(trace_endpos - self.tur_aimpos) - (vlen(self.enemy.maxs - self.enemy.mins) * 0.5);
251         self.tur_impactent             = trace_ent;
252         self.tur_impacttime            = vlen(self.tur_shotorg - trace_endpos) / self.shot_speed;
253
254     self = oldself;
255 }
256
257 /*
258 vector turret_fovsearch_pingpong()
259 {
260     vector wish_angle;
261     if(self.phase < time)
262     {
263         if( self.tur_head.phase )
264             self.tur_head.phase = 0;
265         else
266             self.tur_head.phase = 1;
267         self.phase = time + 5;
268     }
269
270     if( self.tur_head.phase)
271         wish_angle = self.idle_aim + '0 1 0' * (self.aim_maxrot * (self.target_select_fov / 360));
272     else
273         wish_angle = self.idle_aim - '0 1 0' * (self.aim_maxrot * (self.target_select_fov / 360));
274
275     return wish_angle;
276 }
277
278 vector turret_fovsearch_steprot()
279 {
280     vector wish_angle;
281     //float rot_add;
282
283     wish_angle   = self.tur_head.angles;
284     wish_angle_x = self.idle_aim_x;
285
286     if (self.phase < time)
287     {
288         //rot_add = self.aim_maxrot / self.target_select_fov;
289         wish_angle_y += (self.target_select_fov * 2);
290
291         if(wish_angle_y > 360)
292             wish_angle_y = wish_angle_y - 360;
293
294          self.phase = time + 1.5;
295     }
296
297     return wish_angle;
298 }
299
300 vector turret_fovsearch_random()
301 {
302     vector wish_angle;
303
304     if (self.phase < time)
305     {
306         wish_angle_y = random() * self.aim_maxrot;
307         if(random() < 0.5)
308             wish_angle_y *= -1;
309
310         wish_angle_x = random() * self.aim_maxpitch;
311         if(random() < 0.5)
312             wish_angle_x *= -1;
313
314         self.phase = time + 5;
315
316         self.tur_aimpos = wish_angle;
317     }
318
319     return self.idle_aim + self.tur_aimpos;
320 }
321 */
322
323 /**
324 ** Handles head rotation according to
325 ** the units .track_type and .track_flags
326 **/
327 .float turret_framecounter;
328 void turret_stdproc_track()
329 {
330     vector target_angle; // This is where we want to aim
331     vector move_angle;   // This is where we can aim
332     float f_tmp;
333     vector v1, v2;
334     v1 = self.tur_head.angles;
335     v2 = self.tur_head.avelocity;
336
337     if (self.track_flags == TFL_TRACK_NO)
338         return;
339
340     if (!self.active)
341         target_angle = self.idle_aim - ('1 0 0' * self.aim_maxpitch);
342     else if (self.enemy == world)
343     {
344         if(time > self.lip)
345             target_angle = self.idle_aim + self.angles;
346         else
347             target_angle = vectoangles(normalize(self.tur_aimpos - self.tur_shotorg));
348     }
349     else
350     {
351         target_angle = vectoangles(normalize(self.tur_aimpos - self.tur_shotorg));
352     }
353
354     self.tur_head.angles_x = anglemods(self.tur_head.angles.x);
355     self.tur_head.angles_y = anglemods(self.tur_head.angles.y);
356
357     // Find the diffrence between where we currently aim and where we want to aim
358     //move_angle = target_angle - (self.angles + self.tur_head.angles);
359     //move_angle = shortangle_vxy(move_angle,(self.angles + self.tur_head.angles));
360
361     move_angle = AnglesTransform_ToAngles(AnglesTransform_LeftDivide(AnglesTransform_FromAngles(self.angles), AnglesTransform_FromAngles(target_angle))) - self.tur_head.angles;
362     move_angle = shortangle_vxy(move_angle, self.tur_head.angles);
363
364     switch(self.track_type)
365     {
366         case TFL_TRACKTYPE_STEPMOTOR:
367             f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
368             if (self.track_flags & TFL_TRACK_PITCH)
369             {
370                 self.tur_head.angles_x += bound(-f_tmp,move_angle.x, f_tmp);
371                 if(self.tur_head.angles.x > self.aim_maxpitch)
372                     self.tur_head.angles_x = self.aim_maxpitch;
373
374                 if(self.tur_head.angles.x  < -self.aim_maxpitch)
375                     self.tur_head.angles_x = self.aim_maxpitch;
376             }
377
378             if (self.track_flags & TFL_TRACK_ROT)
379             {
380                 self.tur_head.angles_y += bound(-f_tmp, move_angle.y, f_tmp);
381                 if(self.tur_head.angles.y > self.aim_maxrot)
382                     self.tur_head.angles_y = self.aim_maxrot;
383
384                 if(self.tur_head.angles.y  < -self.aim_maxrot)
385                     self.tur_head.angles_y = self.aim_maxrot;
386             }
387
388             // CSQC
389             self.SendFlags  |= TNSF_ANG;
390
391             return;
392
393         case TFL_TRACKTYPE_FLUIDINERTIA:
394             f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
395             move_angle.x = bound(-self.aim_speed, move_angle.x * self.track_accel_pitch * f_tmp, self.aim_speed);
396             move_angle.y = bound(-self.aim_speed, move_angle.y * self.track_accel_rot * f_tmp, self.aim_speed);
397             move_angle = (self.tur_head.avelocity * self.track_blendrate) + (move_angle * (1 - self.track_blendrate));
398             break;
399
400         case TFL_TRACKTYPE_FLUIDPRECISE:
401
402             move_angle.y = bound(-self.aim_speed, move_angle.y, self.aim_speed);
403             move_angle.x = bound(-self.aim_speed, move_angle.x, self.aim_speed);
404
405             break;
406     }
407
408     //  pitch
409     if (self.track_flags & TFL_TRACK_PITCH)
410     {
411         self.tur_head.avelocity_x = move_angle.x;
412         if((self.tur_head.angles.x + self.tur_head.avelocity.x * self.ticrate) > self.aim_maxpitch)
413         {
414             self.tur_head.avelocity_x = 0;
415             self.tur_head.angles_x = self.aim_maxpitch;
416
417             self.SendFlags  |= TNSF_ANG;
418         }
419
420         if((self.tur_head.angles.x + self.tur_head.avelocity.x * self.ticrate) < -self.aim_maxpitch)
421         {
422             self.tur_head.avelocity_x = 0;
423             self.tur_head.angles_x = -self.aim_maxpitch;
424
425             self.SendFlags  |= TNSF_ANG;
426         }
427     }
428
429     //  rot
430     if (self.track_flags & TFL_TRACK_ROT)
431     {
432         self.tur_head.avelocity_y = move_angle.y;
433
434         if((self.tur_head.angles.y + self.tur_head.avelocity.y * self.ticrate) > self.aim_maxrot)
435         {
436             self.tur_head.avelocity_y = 0;
437             self.tur_head.angles_y = self.aim_maxrot;
438
439             self.SendFlags  |= TNSF_ANG;
440         }
441
442         if((self.tur_head.angles.y + self.tur_head.avelocity.y * self.ticrate) < -self.aim_maxrot)
443         {
444             self.tur_head.avelocity_y = 0;
445             self.tur_head.angles_y = -self.aim_maxrot;
446
447             self.SendFlags  |= TNSF_ANG;
448         }
449     }
450
451     self.SendFlags  |= TNSF_AVEL;
452
453     // Force a angle update every 10'th frame
454     self.turret_framecounter += 1;
455     if(self.turret_framecounter >= 10)
456     {
457         self.SendFlags |= TNSF_ANG;
458         self.turret_framecounter = 0;
459     }
460 }
461
462
463 /*
464  + = implemented
465  - = not implemented
466
467  + TFL_FIRECHECK_NO
468  + TFL_FIRECHECK_WORLD
469  + TFL_FIRECHECK_DEAD
470  + TFL_FIRECHECK_DISTANCES
471  - TFL_FIRECHECK_LOS
472  + TFL_FIRECHECK_AIMDIST
473  + TFL_FIRECHECK_REALDIST
474  - TFL_FIRECHECK_ANGLEDIST
475  - TFL_FIRECHECK_TEAMCECK
476  + TFL_FIRECHECK_AFF
477  + TFL_FIRECHECK_OWM_AMMO
478  + TFL_FIRECHECK_OTHER_AMMO
479  + TFL_FIRECHECK_REFIRE
480 */
481
482 /**
483 ** Preforms pre-fire checks based on the uints firecheck_flags
484 **/
485 float turret_stdproc_firecheck()
486 {
487     // This one just dont care =)
488     if (self.firecheck_flags & TFL_FIRECHECK_NO)
489         return 1;
490
491     if (self.enemy == world)
492         return 0;
493
494     // Ready?
495     if (self.firecheck_flags & TFL_FIRECHECK_REFIRE)
496         if (self.attack_finished_single > time) return 0;
497
498     // Special case: volly fire turret that has to fire a full volly if a shot was fired.
499     if (self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
500         if (self.volly_counter != self.shot_volly)
501                         if(self.ammo >= self.shot_dmg)
502                                 return 1;
503
504     // Lack of zombies makes shooting dead things unnecessary :P
505     if (self.firecheck_flags & TFL_FIRECHECK_DEAD)
506         if (self.enemy.deadflag != DEAD_NO)
507             return 0;
508
509     // Own ammo?
510     if (self.firecheck_flags & TFL_FIRECHECK_OWM_AMMO)
511         if (self.ammo < self.shot_dmg)
512             return 0;
513
514     // Other's ammo? (support-supply units)
515     if (self.firecheck_flags & TFL_FIRECHECK_OTHER_AMMO)
516         if (self.enemy.ammo >= self.enemy.ammo_max)
517             return 0;
518
519         // Target of opertunity?
520         if(turret_validate_target(self, self.tur_impactent, self.target_validate_flags) > 0)
521         {
522                 self.enemy = self.tur_impactent;
523                 return 1;
524         }
525
526     if (self.firecheck_flags & TFL_FIRECHECK_DISTANCES)
527     {
528         // To close?
529         if (self.tur_dist_aimpos < self.target_range_min)
530                         if(turret_validate_target(self, self.tur_impactent, self.target_validate_flags) > 0)
531                                 return 1; // Target of opertunity?
532                         else
533                                 return 0;
534     }
535
536     // Try to avoid FF?
537     if (self.firecheck_flags & TFL_FIRECHECK_AFF)
538         if (self.tur_impactent.team == self.team)
539             return 0;
540
541     // aim<->predicted impact
542     if (self.firecheck_flags & TFL_FIRECHECK_AIMDIST)
543         if (self.tur_dist_impact_to_aimpos > self.aim_firetolerance_dist)
544             return 0;
545
546     // Volly status
547     if (self.shot_volly > 1)
548         if (self.volly_counter == self.shot_volly)
549             if (self.ammo < (self.shot_dmg * self.shot_volly))
550                 return 0;
551
552     /*if(self.firecheck_flags & TFL_FIRECHECK_VERIFIED)
553         if(self.tur_impactent != self.enemy)
554             return 0;*/
555
556     return 1;
557 }
558
559 /*
560  + TFL_TARGETSELECT_NO
561  + TFL_TARGETSELECT_LOS
562  + TFL_TARGETSELECT_PLAYERS
563  + TFL_TARGETSELECT_MISSILES
564  - TFL_TARGETSELECT_TRIGGERTARGET
565  + TFL_TARGETSELECT_ANGLELIMITS
566  + TFL_TARGETSELECT_RANGELIMTS
567  + TFL_TARGETSELECT_TEAMCHECK
568  - TFL_TARGETSELECT_NOBUILTIN
569  + TFL_TARGETSELECT_OWNTEAM
570 */
571
572 /**
573 ** Evaluate a entity for target valitity based on validate_flags
574 ** NOTE: the caller must check takedamage before calling this, to inline this check.
575 **/
576 float turret_validate_target(entity e_turret, entity e_target, float validate_flags)
577 {
578     vector v_tmp;
579
580     //if(!validate_flags & TFL_TARGETSELECT_NOBUILTIN)
581     //    return -0.5;
582
583     if(e_target.owner == e_turret)
584         return -0.5;
585
586     if (!checkpvs(e_target.origin, e_turret))
587         return -1;
588
589     if (!e_target)
590         return -2;
591
592         if(g_onslaught)
593                 if (substring(e_target.classname, 0, 10) == "onslaught_") // don't attack onslaught targets, that's the player's job!
594                         return - 3;
595
596     if (validate_flags & TFL_TARGETSELECT_NO)
597         return -4;
598
599     // If only this was used more..
600     if (e_target.flags & FL_NOTARGET)
601         return -5;
602
603     // Cant touch this
604     if(e_target.vehicle_flags & VHF_ISVEHICLE)
605     {
606         if (e_target.vehicle_health <= 0)
607             return -6;
608     }
609     else if (e_target.health <= 0)
610         return -6;
611
612     // player
613     if (IS_CLIENT(e_target))
614     {
615         if (!(validate_flags & TFL_TARGETSELECT_PLAYERS))
616             return -7;
617
618         if (e_target.deadflag != DEAD_NO)
619             return -8;
620     }
621
622         // enemy turrets
623         if (validate_flags & TFL_TARGETSELECT_NOTURRETS)
624         if (e_target.turret_firefunc || e_target.owner.tur_head == e_target)
625             if(e_target.team != e_turret.team) // Dont break support units.
626                 return -9;
627
628     // Missile
629     if (e_target.flags & FL_PROJECTILE)
630         if (!(validate_flags & TFL_TARGETSELECT_MISSILES))
631             return -10;
632
633     if (validate_flags & TFL_TARGETSELECT_MISSILESONLY)
634         if (!(e_target.flags & FL_PROJECTILE))
635             return -10.5;
636
637     // Team check
638     if (validate_flags & TFL_TARGETSELECT_TEAMCHECK)
639     {
640         if (validate_flags & TFL_TARGETSELECT_OWNTEAM)
641         {
642             if (e_target.team != e_turret.team)
643                 return -11;
644
645             if (e_turret.team != e_target.owner.team)
646                 return -12;
647         }
648         else
649         {
650             if (e_target.team == e_turret.team)
651                 return -13;
652
653             if (e_turret.team == e_target.owner.team)
654                 return -14;
655         }
656     }
657
658     // Range limits?
659     tvt_dist = vlen(e_turret.origin - real_origin(e_target));
660     if (validate_flags & TFL_TARGETSELECT_RANGELIMTS)
661     {
662         if (tvt_dist < e_turret.target_range_min)
663             return -15;
664
665         if (tvt_dist > e_turret.target_range)
666             return -16;
667     }
668
669     // Can we even aim this thing?
670     tvt_thadv = angleofs3(e_turret.tur_head.origin, e_turret.angles + e_turret.tur_head.angles, e_target);
671     tvt_tadv  = shortangle_vxy(angleofs(e_turret, e_target), e_turret.angles);
672     tvt_thadf = vlen(tvt_thadv);
673     tvt_tadf  = vlen(tvt_tadv);
674
675     /*
676     if(validate_flags & TFL_TARGETSELECT_FOV)
677     {
678         if(e_turret.target_select_fov < tvt_thadf)
679             return -21;
680     }
681     */
682
683     if (validate_flags & TFL_TARGETSELECT_ANGLELIMITS)
684     {
685         if (fabs(tvt_tadv.x) > e_turret.aim_maxpitch)
686             return -17;
687
688         if (fabs(tvt_tadv.y) > e_turret.aim_maxrot)
689             return -18;
690     }
691
692     // Line of sight?
693     if (validate_flags & TFL_TARGETSELECT_LOS)
694     {
695         v_tmp = real_origin(e_target) + ((e_target.mins + e_target.maxs) * 0.5);
696
697         traceline(e_turret.origin + '0 0 16', v_tmp, 0, e_turret);
698
699         if (e_turret.aim_firetolerance_dist < vlen(v_tmp - trace_endpos))
700             return -19;
701     }
702
703     if (e_target.classname == "grapplinghook")
704         return -20;
705
706     /*
707     if (e_target.classname == "func_button")
708         return -21;
709     */
710
711 #ifdef TURRET_DEBUG_TARGETSELECT
712     dprint("Target:",e_target.netname," is a valid target for ",e_turret.netname,"\n");
713 #endif
714
715     return 1;
716 }
717
718 entity turret_select_target()
719 {
720     entity e;        // target looper entity
721     float  score;    // target looper entity score
722     entity e_enemy;  // currently best scoreing target
723     float  m_score;  // currently best scoreing target's score
724
725     m_score = 0;
726     if(self.enemy && self.enemy.takedamage && turret_validate_target(self,self.enemy,self.target_validate_flags) > 0)
727     {
728         e_enemy = self.enemy;
729         m_score = self.turret_score_target(self,e_enemy) * self.target_select_samebias;
730     }
731     else
732         e_enemy = self.enemy = world;
733
734     e = findradius(self.origin, self.target_range);
735
736     // Nothing to aim at?
737     if (!e)
738                 return world;
739
740     while (e)
741     {
742                 if(e.takedamage)
743                 {
744                     float f = turret_validate_target(self, e, self.target_select_flags);
745                     //dprint("F is: ", ftos(f), "\n");
746                         if ( f > 0)
747                         {
748                                 score = self.turret_score_target(self,e);
749                                 if ((score > m_score) && (score > 0))
750                                 {
751                                         e_enemy = e;
752                                         m_score = score;
753                                 }
754                         }
755                 }
756         e = e.chain;
757     }
758
759     return e_enemy;
760 }
761
762 void turret_think()
763 {
764     entity e;
765
766     self.nextthink = time + self.ticrate;
767
768     // ONS uses somewhat backwards linking.
769     if (teamplay)
770     {
771         if (g_onslaught)
772             if (self.target)
773             {
774                 e = find(world, targetname,self.target);
775                 if (e != world)
776                     self.team = e.team;
777             }
778
779         if (self.team != self.tur_head.team)
780             turret_stdproc_respawn();
781     }
782
783 #ifdef TURRET_DEBUG
784     if (self.tur_dbg_tmr1 < time)
785     {
786         if (self.enemy) paint_target (self.enemy,128,self.tur_dbg_rvec,0.9);
787         paint_target(self,256,self.tur_dbg_rvec,0.9);
788         self.tur_dbg_tmr1 = time + 1;
789     }
790 #endif
791
792     // Handle ammo
793     if (!(self.spawnflags & TSF_NO_AMMO_REGEN))
794     if (self.ammo < self.ammo_max)
795         self.ammo = min(self.ammo + self.ammo_recharge, self.ammo_max);
796
797     // Inactive turrets needs to run the think loop,
798     // So they can handle animation and wake up if need be.
799     if (!self.active)
800     {
801         turret_stdproc_track();
802         return;
803     }
804
805     // This is typicaly used for zaping every target in range
806     // turret_fusionreactor uses this to recharge friendlys.
807     if (self.shoot_flags & TFL_SHOOT_HITALLVALID)
808     {
809         // Do a self.turret_fire for every valid target.
810         e = findradius(self.origin,self.target_range);
811         while (e)
812         {
813                         if(e.takedamage)
814                         {
815                                 if (turret_validate_target(self,e,self.target_validate_flags))
816                                 {
817                                         self.enemy = e;
818
819                                         turret_do_updates(self);
820
821                                         if (self.turret_firecheckfunc())
822                                                 turret_fire();
823                                 }
824                         }
825
826             e = e.chain;
827         }
828         self.enemy = world;
829     }
830     else if(self.shoot_flags & TFL_SHOOT_CUSTOM)
831     {
832         // This one is doing something.. oddball. assume its handles what needs to be handled.
833
834         // Predict?
835         if (!(self.aim_flags & TFL_AIM_NO))
836             self.tur_aimpos = turret_stdproc_aim_generic();
837
838         // Turn & pitch?
839         if (!(self.track_flags & TFL_TRACK_NO))
840             turret_stdproc_track();
841
842         turret_do_updates(self);
843
844         // Fire?
845         if (self.turret_firecheckfunc())
846             turret_fire();
847     }
848     else
849     {
850         // Special case for volly always. if it fired once it must compleate the volly.
851         if(self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
852             if(self.volly_counter != self.shot_volly)
853             {
854                 // Predict or whatnot
855                 if (!(self.aim_flags & TFL_AIM_NO))
856                     self.tur_aimpos = turret_stdproc_aim_generic();
857
858                 // Turn & pitch
859                 if (!(self.track_flags & TFL_TRACK_NO))
860                     turret_stdproc_track();
861
862                 turret_do_updates(self);
863
864                 // Fire!
865                 if (self.turret_firecheckfunc() != 0)
866                     turret_fire();
867
868                 if(self.turret_postthink)
869                     self.turret_postthink();
870
871                 return;
872             }
873
874         // Check if we have a vailid enemy, and try to find one if we dont.
875
876         // g_turrets_targetscan_maxdelay forces a target re-scan at least this often
877         float do_target_scan = 0;
878         if((self.target_select_time + autocvar_g_turrets_targetscan_maxdelay) < time)
879             do_target_scan = 1;
880
881         // Old target (if any) invalid?
882         if(self.target_validate_time < time)
883         if (turret_validate_target(self, self.enemy, self.target_validate_flags) <= 0)
884         {
885                 self.enemy = world;
886                 self.target_validate_time = time + 0.5;
887                 do_target_scan = 1;
888         }
889
890         // But never more often then g_turrets_targetscan_mindelay!
891         if (self.target_select_time + autocvar_g_turrets_targetscan_mindelay > time)
892             do_target_scan = 0;
893
894         if(do_target_scan)
895         {
896             self.enemy = turret_select_target();
897             self.target_select_time = time;
898         }
899
900         // No target, just go to idle, do any custom stuff and bail.
901         if (self.enemy == world)
902         {
903             // Turn & pitch
904             if (!(self.track_flags & TFL_TRACK_NO))
905                 turret_stdproc_track();
906
907             // do any per-turret stuff
908             if(self.turret_postthink)
909                 self.turret_postthink();
910
911             // And bail.
912             return;
913         }
914         else
915             self.lip = time + autocvar_g_turrets_aimidle_delay; // Keep track of the last time we had a target.
916
917         // Predict?
918         if (!(self.aim_flags & TFL_AIM_NO))
919             self.tur_aimpos = turret_stdproc_aim_generic();
920
921         // Turn & pitch?
922         if (!(self.track_flags & TFL_TRACK_NO))
923             turret_stdproc_track();
924
925         turret_do_updates(self);
926
927         // Fire?
928         if (self.turret_firecheckfunc())
929             turret_fire();
930     }
931
932     // do any custom per-turret stuff
933     if(self.turret_postthink)
934         self.turret_postthink();
935 }
936
937 void turret_fire()
938 {
939     if (autocvar_g_turrets_nofire)
940         return;
941
942     self.turret_firefunc();
943
944     self.attack_finished_single = time + self.shot_refire;
945     self.ammo -= self.shot_dmg;
946     self.volly_counter = self.volly_counter - 1;
947
948     if (self.volly_counter <= 0)
949     {
950         self.volly_counter = self.shot_volly;
951
952         if (self.shoot_flags & TFL_SHOOT_CLEARTARGET)
953             self.enemy = world;
954
955         if (self.shot_volly > 1)
956             self.attack_finished_single = time + self.shot_volly_refire;
957     }
958
959 #ifdef TURRET_DEBUG
960     if (self.enemy) paint_target3(self.tur_aimpos, 64, self.tur_dbg_rvec, self.tur_impacttime + 0.25);
961 #endif
962 }
963
964 void turret_stdproc_fire()
965 {
966     dprint("^1Bang, ^3your dead^7 ",self.enemy.netname,"! ^1(turret with no real firefunc)\n");
967 }
968
969 /*
970     When .used a turret switch team to activator.team.
971     If activator is world, the turret go inactive.
972 */
973 void turret_stdproc_use()
974 {
975     dprint("Turret ",self.netname, " used by ", activator.classname, "\n");
976
977     self.team = activator.team;
978
979     if(self.team == 0)
980         self.active = ACTIVE_NOT;
981     else
982         self.active = ACTIVE_ACTIVE;
983
984 }
985
986 void turret_link()
987 {
988     Net_LinkEntity(self, true, 0, turret_send);
989     self.think      = turret_think;
990     self.nextthink  = time;
991     self.tur_head.effects = EF_NODRAW;
992 }
993
994 void turrets_manager_think()
995 {
996     self.nextthink = time + 1;
997
998     entity e;
999     if (autocvar_g_turrets_reloadcvars)
1000     {
1001         e = nextent(world);
1002         while (e)
1003         {
1004             if (e.turrcaps_flags & TFL_TURRCAPS_ISTURRET)
1005             {
1006                 load_unit_settings(e,e.cvar_basename,1);
1007                 if(e.turret_postthink)
1008                     e.turret_postthink();
1009             }
1010
1011             e = nextent(e);
1012         }
1013         cvar_set("g_turrets_reloadcvars","0");
1014     }
1015 }
1016
1017 /*
1018 * Standard turret initialization. use this!
1019 * (unless you have a very good reason not to)
1020 * if the return value is 0, the turret should be removed.
1021 */
1022 float turret_stdproc_init (string cvar_base_name, string base, string head, float _turret_type)
1023 {
1024         entity e, ee = world;
1025
1026     // Are turrets allowed?
1027     if (autocvar_g_turrets == 0)
1028         return 0;
1029
1030     if(_turret_type < 1 || _turret_type > TID_LAST)
1031     {
1032         dprint("Invalid / Unkown turret type\"", ftos(_turret_type), "\", aborting!\n");
1033         return 0;
1034     }
1035     self.turret_type = _turret_type;
1036
1037     e = find(world, classname, "turret_manager");
1038     if (!e)
1039     {
1040         e = spawn();
1041         e.classname = "turret_manager";
1042         e.think = turrets_manager_think;
1043         e.nextthink = time + 2;
1044     }
1045
1046     if (!(self.spawnflags & TSF_SUSPENDED))
1047         builtin_droptofloor(); // why can't we use regular droptofloor here?
1048
1049     self.cvar_basename = cvar_base_name;
1050     load_unit_settings(self, self.cvar_basename, 0);
1051
1052     self.effects = EF_NODRAW;
1053
1054     // Handle turret teams.
1055     if (!teamplay)
1056                 self.team = MAX_SHOT_DISTANCE; // Group all turrets into the same team, so they dont kill eachother.
1057         else if(g_onslaught && self.targetname)
1058         {
1059                 e = find(world,target,self.targetname);
1060                 if(e != world)
1061                 {
1062                         self.team = e.team;
1063                         ee = e;
1064                 }
1065         }
1066         else if(!self.team)
1067                 self.team = MAX_SHOT_DISTANCE; // Group all turrets into the same team, so they dont kill eachother.
1068
1069     /*
1070     * Try to guess some reasonaly defaults
1071     * for missing params and do sanety checks
1072     * thise checks could produce some "interesting" results
1073     * if it hits a glitch in my logic :P so try to set as mutch
1074     * as possible beforehand.
1075     */
1076     if (!self.ticrate)
1077     {
1078         if (self.turrcaps_flags & TFL_TURRCAPS_SUPPORT)
1079             self.ticrate = 0.2;     // Support units generaly dont need to have a high speed ai-loop
1080         else
1081             self.ticrate = 0.1;     // 10 fps for normal turrets
1082     }
1083
1084     self.ticrate = bound(sys_frametime, self.ticrate, 60);  // keep it sane
1085
1086 // General stuff
1087     if (self.netname == "")
1088         self.netname = self.classname;
1089
1090     if (!self.respawntime)
1091         self.respawntime = 60;
1092     self.respawntime = max(-1, self.respawntime);
1093
1094     if (!self.health)
1095         self.health = 1000;
1096     self.tur_health = max(1, self.health);
1097     self.bot_attack = true;
1098     self.monster_attack = true;
1099
1100     if (!self.turrcaps_flags)
1101         self.turrcaps_flags = TFL_TURRCAPS_RADIUSDMG | TFL_TURRCAPS_MEDPROJ | TFL_TURRCAPS_PLAYERKILL;
1102
1103     if (!self.damage_flags)
1104         self.damage_flags = TFL_DMG_YES | TFL_DMG_RETALIATE | TFL_DMG_AIMSHAKE;
1105
1106 // Shot stuff.
1107     if (!self.shot_refire)
1108         self.shot_refire = 1;
1109     self.shot_refire = bound(0.01, self.shot_refire, 9999);
1110
1111     if (!self.shot_dmg)
1112         self.shot_dmg  = self.shot_refire * 50;
1113     self.shot_dmg = max(1, self.shot_dmg);
1114
1115     if (!self.shot_radius)
1116         self.shot_radius = self.shot_dmg * 0.5;
1117     self.shot_radius = max(1, self.shot_radius);
1118
1119     if (!self.shot_speed)
1120         self.shot_speed = 2500;
1121     self.shot_speed = max(1, self.shot_speed);
1122
1123     if (!self.shot_spread)
1124         self.shot_spread = 0.0125;
1125     self.shot_spread = bound(0.0001, self.shot_spread, 500);
1126
1127     if (!self.shot_force)
1128         self.shot_force = self.shot_dmg * 0.5 + self.shot_radius * 0.5;
1129     self.shot_force = bound(0.001, self.shot_force, 5000);
1130
1131     if (!self.shot_volly)
1132         self.shot_volly = 1;
1133     self.shot_volly = bound(1, self.shot_volly, floor(self.ammo_max / self.shot_dmg));
1134
1135     if (!self.shot_volly_refire)
1136         self.shot_volly_refire = self.shot_refire * self.shot_volly;
1137     self.shot_volly_refire = bound(self.shot_refire, self.shot_volly_refire, 60);
1138
1139     if (!self.firecheck_flags)
1140         self.firecheck_flags = TFL_FIRECHECK_DEAD | TFL_FIRECHECK_DISTANCES |
1141                                TFL_FIRECHECK_LOS | TFL_FIRECHECK_AIMDIST | TFL_FIRECHECK_TEAMCECK |
1142                                TFL_FIRECHECK_OWM_AMMO | TFL_FIRECHECK_REFIRE;
1143
1144 // Range stuff.
1145     if (!self.target_range)
1146         self.target_range = self.shot_speed * 0.5;
1147     self.target_range = bound(0, self.target_range, MAX_SHOT_DISTANCE);
1148
1149     if (!self.target_range_min)
1150         self.target_range_min = self.shot_radius * 2;
1151     self.target_range_min = bound(0, self.target_range_min, MAX_SHOT_DISTANCE);
1152
1153     if (!self.target_range_optimal)
1154         self.target_range_optimal = self.target_range * 0.5;
1155     self.target_range_optimal = bound(0, self.target_range_optimal, MAX_SHOT_DISTANCE);
1156
1157
1158 // Aim stuff.
1159     if (!self.aim_maxrot)
1160         self.aim_maxrot = 90;
1161     self.aim_maxrot = bound(0, self.aim_maxrot, 360);
1162
1163     if (!self.aim_maxpitch)
1164         self.aim_maxpitch = 20;
1165     self.aim_maxpitch = bound(0, self.aim_maxpitch, 90);
1166
1167     if (!self.aim_speed)
1168         self.aim_speed = 36;
1169     self.aim_speed  = bound(0.1, self.aim_speed, 1000);
1170
1171     if (!self.aim_firetolerance_dist)
1172         self.aim_firetolerance_dist  = 5 + (self.shot_radius * 2);
1173     self.aim_firetolerance_dist = bound(0.1, self.aim_firetolerance_dist, MAX_SHOT_DISTANCE);
1174
1175     if (!self.aim_flags)
1176     {
1177         self.aim_flags = TFL_AIM_LEAD | TFL_AIM_SHOTTIMECOMPENSATE;
1178         if(self.turrcaps_flags & TFL_TURRCAPS_RADIUSDMG)
1179             self.aim_flags |= TFL_AIM_GROUNDGROUND;
1180     }
1181
1182     if (!self.track_type)
1183         self.track_type = TFL_TRACKTYPE_STEPMOTOR;
1184
1185     if (self.track_type != TFL_TRACKTYPE_STEPMOTOR)
1186     {
1187         // Fluid / Ineria mode. Looks mutch nicer.
1188         // Can reduce aim preformance alot, needs a bit diffrent aimspeed
1189
1190         if (!self.aim_speed)
1191             self.aim_speed = 180;
1192         self.aim_speed = bound(0.1, self.aim_speed, 1000);
1193
1194         if (!self.track_accel_pitch)
1195             self.track_accel_pitch = 0.5;
1196
1197         if (!self.track_accel_rot)
1198             self.track_accel_rot   = 0.5;
1199
1200         if (!self.track_blendrate)
1201             self.track_blendrate   = 0.35;
1202     }
1203
1204     if (!self.track_flags)
1205         self.track_flags = TFL_TRACK_PITCH | TFL_TRACK_ROT;
1206
1207
1208 // Target selection stuff.
1209     if (!self.target_select_rangebias)
1210         self.target_select_rangebias = 1;
1211     self.target_select_rangebias = bound(-10, self.target_select_rangebias, 10);
1212
1213     if (!self.target_select_samebias)
1214         self.target_select_samebias = 1;
1215     self.target_select_samebias = bound(-10, self.target_select_samebias, 10);
1216
1217     if (!self.target_select_anglebias)
1218         self.target_select_anglebias = 1;
1219     self.target_select_anglebias = bound(-10, self.target_select_anglebias, 10);
1220
1221     if (!self.target_select_missilebias)
1222         self.target_select_missilebias = -10;
1223
1224     self.target_select_missilebias = bound(-10, self.target_select_missilebias, 10);
1225     self.target_select_playerbias = bound(-10, self.target_select_playerbias, 10);
1226
1227     if (!self.target_select_flags)
1228     {
1229             self.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_TEAMCHECK
1230                                      | TFL_TARGETSELECT_RANGELIMTS | TFL_TARGETSELECT_ANGLELIMITS;
1231
1232         if (self.turrcaps_flags & TFL_TURRCAPS_MISSILEKILL)
1233             self.target_select_flags |= TFL_TARGETSELECT_MISSILES;
1234
1235         if (self.turrcaps_flags & TFL_TURRCAPS_PLAYERKILL)
1236             self.target_select_flags |= TFL_TARGETSELECT_PLAYERS;
1237         //else
1238         //    self.target_select_flags = TFL_TARGETSELECT_NO;
1239     }
1240
1241     self.target_validate_flags = self.target_select_flags;
1242
1243 // Ammo stuff
1244     if (!self.ammo_max)
1245         self.ammo_max = self.shot_dmg * 10;
1246     self.ammo_max = max(self.shot_dmg, self.ammo_max);
1247
1248     if (!self.ammo)
1249         self.ammo = self.shot_dmg * 5;
1250     self.ammo = bound(0,self.ammo, self.ammo_max);
1251
1252     if (!self.ammo_recharge)
1253         self.ammo_recharge = self.shot_dmg * 0.5;
1254     self.ammo_recharge = max(0 ,self.ammo_recharge);
1255
1256     // Convert the recharge from X per sec to X per ticrate
1257     self.ammo_recharge = self.ammo_recharge * self.ticrate;
1258
1259     if (!self.ammo_flags)
1260         self.ammo_flags = TFL_AMMO_ENERGY | TFL_AMMO_RECHARGE;
1261
1262 // Damage stuff
1263     if(self.spawnflags & TSL_NO_RESPAWN)
1264         if (!(self.damage_flags & TFL_DMG_DEATH_NORESPAWN))
1265             self.damage_flags |= TFL_DMG_DEATH_NORESPAWN;
1266
1267 // Offsets & origins
1268     if (!self.tur_shotorg)   self.tur_shotorg = '50 0 50';
1269
1270     if (!self.health)
1271         self.health = 150;
1272
1273 // Game hooks
1274         if(MUTATOR_CALLHOOK(TurretSpawn))
1275                 return 0;
1276
1277 // End of default & sanety checks, start building the turret.
1278
1279 // Spawn extra bits
1280     self.tur_head         = spawn();
1281     self.tur_head.netname = self.tur_head.classname = "turret_head";
1282     self.tur_head.team    = self.team;
1283     self.tur_head.owner   = self;
1284
1285     setmodel(self, base);
1286     setmodel(self.tur_head, head);
1287
1288     setsize(self, '-32 -32 0', '32 32 64');
1289     setsize(self.tur_head, '0 0 0', '0 0 0');
1290
1291     setorigin(self.tur_head, '0 0 0');
1292     setattachment(self.tur_head, self, "tag_head");
1293
1294     self.tur_health          = self.health;
1295     self.solid               = SOLID_BBOX;
1296     self.tur_head.solid      = SOLID_NOT;
1297     self.takedamage          = DAMAGE_AIM;
1298     self.tur_head.takedamage = DAMAGE_NO;
1299     self.movetype            = MOVETYPE_NOCLIP;
1300     self.tur_head.movetype   = MOVETYPE_NOCLIP;
1301
1302     // Defend mode?
1303     if (!self.tur_defend)
1304     if (self.target != "")
1305     {
1306         self.tur_defend = find(world, targetname, self.target);
1307         if (self.tur_defend == world)
1308         {
1309             self.target = "";
1310             dprint("Turret has invalid defendpoint!\n");
1311         }
1312     }
1313
1314     // In target defend mode, aim on the spot to defend when idle.
1315     if (self.tur_defend)
1316         self.idle_aim  = self.tur_head.angles + angleofs(self.tur_head, self.tur_defend);
1317     else
1318         self.idle_aim  = '0 0 0';
1319
1320     // Attach stdprocs. override when and what needed
1321     self.turret_firecheckfunc   = turret_stdproc_firecheck;
1322     self.turret_firefunc        = turret_stdproc_fire;
1323     self.event_damage           = turret_stdproc_damage;
1324
1325     if (self.turrcaps_flags & TFL_TURRCAPS_SUPPORT)
1326         self.turret_score_target    = turret_stdproc_targetscore_support;
1327     else
1328         self.turret_score_target    = turret_stdproc_targetscore_generic;
1329
1330     self.use = turret_stdproc_use;
1331
1332     ++turret_count;
1333     self.nextthink = time + 1;
1334     self.nextthink +=  turret_count * sys_frametime;
1335
1336     self.tur_head.team = self.team;
1337     self.view_ofs = '0 0 0';
1338
1339 #ifdef TURRET_DEBUG
1340     self.tur_dbg_start = self.nextthink;
1341     while (vlen(self.tur_dbg_rvec) < 2)
1342         self.tur_dbg_rvec  = randomvec() * 4;
1343
1344     self.tur_dbg_rvec_x = fabs(self.tur_dbg_rvec.x);
1345     self.tur_dbg_rvec_y = fabs(self.tur_dbg_rvec.y);
1346     self.tur_dbg_rvec_z = fabs(self.tur_dbg_rvec.z);
1347 #endif
1348
1349     // Its all good.
1350     self.turrcaps_flags |= TFL_TURRCAPS_ISTURRET;
1351
1352     self.classname = "turret_main";
1353
1354     self.active = ACTIVE_ACTIVE;
1355
1356     // In ONS mode, and linked to a ONS ent. need to call the use to set team.
1357     if (g_onslaught && ee)
1358     {
1359         activator = ee;
1360         self.use();
1361     }
1362
1363         turret_link();
1364         turret_stdproc_respawn();
1365     turret_tag_fire_update();
1366
1367     return 1;
1368 }
1369
1370