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