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