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