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