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