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