]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/tturrets/system/system_main.qc
Merge remote-tracking branch 'origin/master' into tzork/turrets-csqc
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / tturrets / system / system_main.qc
1 #define cvar_base "g_turrets_unit_"
2
3 float TNSF_UPDATE       = 2;
4 float TNSF_STATUS       = 4;
5 float TNSF_SETUP        = 8;
6
7 float TNSF_ANG1         = 16;
8 float TNSF_AVEL1        = 32;
9 float TNSF_ANG2         = 64;
10 float TNSF_AVEL2        = 128;
11
12 float TNSF_FULL_UPDATE  = 16777215;
13 float turret_send(entity to, float sf)
14 {
15         WriteByte(MSG_ENTITY, ENT_CLIENT_TURRET);
16         
17         WriteByte(MSG_ENTITY, sf);
18         if(sf & TNSF_SETUP)
19         {
20             WriteByte(MSG_ENTITY, self.turret_type);
21             
22             WriteCoord(MSG_ENTITY, self.origin_x);
23             WriteCoord(MSG_ENTITY, self.origin_y);
24             WriteCoord(MSG_ENTITY, self.origin_z);
25             
26             WriteAngle(MSG_ENTITY, self.angles_x);
27             WriteAngle(MSG_ENTITY, self.angles_y);
28     }
29     
30     if(sf & TNSF_UPDATE)
31     {        
32         WriteAngle(MSG_ENTITY, self.tur_head.angles_x);
33             WriteAngle(MSG_ENTITY, self.tur_head.angles_y);         
34             WriteAngle(MSG_ENTITY, self.tur_head.avelocity_x);
35             WriteAngle(MSG_ENTITY, self.tur_head.avelocity_y);
36     }
37     
38     if(sf & TNSF_STATUS)
39     {        
40         WriteByte(MSG_ENTITY, self.team);
41         WriteByte(MSG_ENTITY, rint((self.health / self.tur_health) * 255));
42     }
43     
44         return TRUE;
45 }
46
47 void load_unit_settings(entity ent, string unitname, float is_reload)
48 {
49     string sbase;
50
51     if (ent == world)
52         return;
53
54     if not (ent.turret_scale_damage)    ent.turret_scale_damage  = 1;
55     if not (ent.turret_scale_range)     ent.turret_scale_range   = 1;
56     if not (ent.turret_scale_refire)    ent.turret_scale_refire  = 1;
57     if not (ent.turret_scale_ammo)      ent.turret_scale_ammo    = 1;
58     if not (ent.turret_scale_aim)       ent.turret_scale_aim     = 1;
59     if not (ent.turret_scale_health)    ent.turret_scale_health  = 1;
60     if not (ent.turret_scale_respawn)   ent.turret_scale_respawn = 1;
61
62     sbase = strcat(cvar_base,unitname);
63     if (is_reload)
64     {
65         ent.enemy = world;
66         ent.tur_head.avelocity = '0 0 0';
67
68         ent.tur_head.angles = '0 0 0';
69     }
70
71     ent.health      = cvar(strcat(sbase,"_health")) * ent.turret_scale_health;
72     ent.respawntime = cvar(strcat(sbase,"_respawntime")) * ent.turret_scale_respawn;
73
74     ent.shot_dmg          = cvar(strcat(sbase,"_shot_dmg")) * ent.turret_scale_damage;
75     ent.shot_refire       = cvar(strcat(sbase,"_shot_refire")) * ent.turret_scale_refire;
76     ent.shot_radius       = cvar(strcat(sbase,"_shot_radius")) * ent.turret_scale_damage;
77     ent.shot_speed        = cvar(strcat(sbase,"_shot_speed"));
78     ent.shot_spread       = cvar(strcat(sbase,"_shot_spread"));
79     ent.shot_force        = cvar(strcat(sbase,"_shot_force")) * ent.turret_scale_damage;
80     ent.shot_volly        = cvar(strcat(sbase,"_shot_volly"));
81     ent.shot_volly_refire = cvar(strcat(sbase,"_shot_volly_refire")) * ent.turret_scale_refire;
82
83     ent.target_range         = cvar(strcat(sbase,"_target_range")) * ent.turret_scale_range;
84     ent.target_range_min     = cvar(strcat(sbase,"_target_range_min")) * ent.turret_scale_range;
85     ent.target_range_optimal = cvar(strcat(sbase,"_target_range_optimal")) * ent.turret_scale_range;
86     //ent.target_range_fire    = cvar(strcat(sbase,"_target_range_fire")) * ent.turret_scale_range;
87
88     ent.target_select_rangebias  = cvar(strcat(sbase,"_target_select_rangebias"));
89     ent.target_select_samebias   = cvar(strcat(sbase,"_target_select_samebias"));
90     ent.target_select_anglebias  = cvar(strcat(sbase,"_target_select_anglebias"));
91     ent.target_select_playerbias = cvar(strcat(sbase,"_target_select_playerbias"));
92     //ent.target_select_fov = cvar(cvar_gets(sbase,"_target_select_fov"));
93
94     ent.ammo_max      = cvar(strcat(sbase,"_ammo_max")) * ent.turret_scale_ammo;
95     ent.ammo_recharge = cvar(strcat(sbase,"_ammo_recharge")) * ent.turret_scale_ammo;
96
97     ent.aim_firetolerance_dist = cvar(strcat(sbase,"_aim_firetolerance_dist"));
98     ent.aim_speed    = cvar(strcat(sbase,"_aim_speed")) * ent.turret_scale_aim;
99     ent.aim_maxrot   = cvar(strcat(sbase,"_aim_maxrot"));
100     ent.aim_maxpitch = cvar(strcat(sbase,"_aim_maxpitch"));
101
102     ent.track_type        = cvar(strcat(sbase,"_track_type"));
103     ent.track_accel_pitch = cvar(strcat(sbase,"_track_accel_pitch"));
104     ent.track_accel_rot   = cvar(strcat(sbase,"_track_accel_rot"));
105     ent.track_blendrate   = cvar(strcat(sbase,"_track_blendrate"));
106
107     if(is_reload)
108         if(ent.turret_respawnhook)
109             ent.turret_respawnhook();
110 }
111
112
113 /**
114 ** updates enemy distances, predicted impact point/time
115 ** and updated aim<->predict impact distance.
116 **/
117 void turret_do_updates(entity t_turret)
118 {
119     vector enemy_pos, oldpos;
120     entity oldself;
121
122     oldself = self;
123     self = t_turret;
124
125     enemy_pos = real_origin(self.enemy);
126
127     turret_tag_fire_update();
128
129     self.tur_shotdir_updated = v_forward;
130     self.tur_dist_enemy  = vlen(self.tur_shotorg - enemy_pos);
131     self.tur_dist_aimpos = vlen(self.tur_shotorg - self.tur_aimpos);
132
133     if((self.firecheck_flags & TFL_FIRECHECK_VERIFIED) && (self.enemy))
134     {
135         oldpos = self.enemy.origin;
136         setorigin(self.enemy,self.tur_aimpos);
137         tracebox(self.tur_shotorg, '-1 -1 -1','1 1 1',self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos),MOVE_NORMAL,self);
138         setorigin(self.enemy,oldpos);
139
140         if(trace_ent == self.enemy)
141             self.tur_dist_impact_to_aimpos = 0;
142         else
143             self.tur_dist_impact_to_aimpos = vlen(trace_endpos - self.tur_aimpos);
144     }
145     else
146         tracebox(self.tur_shotorg, '-1 -1 -1','1 1 1', self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos),MOVE_NORMAL,self);
147         
148         self.tur_dist_impact_to_aimpos = vlen(trace_endpos - self.tur_aimpos) - (vlen(self.enemy.maxs - self.enemy.mins) * 0.5);                
149         self.tur_impactent             = trace_ent;
150         self.tur_impacttime            = vlen(self.tur_shotorg - trace_endpos) / self.shot_speed;
151
152     self = oldself;
153 }
154
155 /*
156 vector turret_fovsearch_pingpong()
157 {
158     vector wish_angle;
159     if(self.phase < time)
160     {
161         if( self.tur_head.phase )
162             self.tur_head.phase = 0;
163         else
164             self.tur_head.phase = 1;
165         self.phase = time + 5;
166     }
167
168     if( self.tur_head.phase)
169         wish_angle = self.idle_aim + '0 1 0' * (self.aim_maxrot * (self.target_select_fov / 360));
170     else
171         wish_angle = self.idle_aim - '0 1 0' * (self.aim_maxrot * (self.target_select_fov / 360));
172
173     return wish_angle;
174 }
175
176 vector turret_fovsearch_steprot()
177 {
178     vector wish_angle;
179     //float rot_add;
180
181     wish_angle   = self.tur_head.angles;
182     wish_angle_x = self.idle_aim_x;
183
184     if (self.phase < time)
185     {
186         //rot_add = self.aim_maxrot / self.target_select_fov;
187         wish_angle_y += (self.target_select_fov * 2);
188
189         if(wish_angle_y > 360)
190             wish_angle_y = wish_angle_y - 360;
191
192          self.phase = time + 1.5;
193     }
194
195     return wish_angle;
196 }
197
198 vector turret_fovsearch_random()
199 {
200     vector wish_angle;
201
202     if (self.phase < time)
203     {
204         wish_angle_y = random() * self.aim_maxrot;
205         if(random() < 0.5)
206             wish_angle_y *= -1;
207
208         wish_angle_x = random() * self.aim_maxpitch;
209         if(random() < 0.5)
210             wish_angle_x *= -1;
211
212         self.phase = time + 5;
213
214         self.tur_aimpos = wish_angle;
215     }
216
217     return self.idle_aim + self.tur_aimpos;
218 }
219 */
220
221 /**
222 ** Handles head rotation according to
223 ** the units .track_type and .track_flags
224 **/
225 void turret_stdproc_track()
226 {
227     vector target_angle; // This is where we want to aim
228     vector move_angle;   // This is where we can aim
229     float f_tmp;
230     vector v1, v2;
231     v1 = self.tur_head.angles;
232     v2 = self.tur_head.avelocity;
233     
234     if (self.track_flags == TFL_TRACK_NO)
235         return;
236
237     if not (self.tur_active)
238         target_angle = self.idle_aim - ('1 0 0' * self.aim_maxpitch);
239     else if (self.enemy == world)
240     {
241         if(time > self.lip)
242             target_angle = self.idle_aim + self.angles;
243         else
244             target_angle = vectoangles(normalize(self.tur_aimpos - self.tur_shotorg));
245     }
246     else
247     {
248         target_angle = vectoangles(normalize(self.tur_aimpos - self.tur_shotorg)); 
249     }
250     
251     self.tur_head.angles_x = anglemods(self.tur_head.angles_x);
252     self.tur_head.angles_y = anglemods(self.tur_head.angles_y);
253
254     // Find the diffrence between where we currently aim and where we want to aim
255     move_angle = target_angle - (self.angles + self.tur_head.angles);
256     move_angle = shortangle_vxy(move_angle,(self.angles + self.tur_head.angles));
257
258     switch(self.track_type)
259     {
260         case TFL_TRACKTYPE_STEPMOTOR:
261             f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
262             if (self.track_flags & TFL_TRACK_PITCH)
263             {
264                 self.tur_head.angles_x += bound(-f_tmp,move_angle_x, f_tmp);
265                 if(self.tur_head.angles_x > self.aim_maxpitch)
266                     self.tur_head.angles_x = self.aim_maxpitch;
267
268                 if(self.tur_head.angles_x  < -self.aim_maxpitch)
269                     self.tur_head.angles_x = self.aim_maxpitch;
270             }
271
272             if (self.track_flags & TFL_TRACK_ROT)
273             {
274                 self.tur_head.angles_y += bound(-f_tmp, move_angle_y, f_tmp);
275                 if(self.tur_head.angles_y > self.aim_maxrot)
276                     self.tur_head.angles_y = self.aim_maxrot;
277
278                 if(self.tur_head.angles_y  < -self.aim_maxrot)
279                     self.tur_head.angles_y = self.aim_maxrot;
280             }
281             
282             // CSQC
283             if(self.SendEntity)
284                 self.SendFlags  = TNSF_STATUS;
285             return;
286
287         case TFL_TRACKTYPE_FLUIDINERTIA:
288             f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
289             move_angle_x = bound(-self.aim_speed, move_angle_x * self.track_accel_pitch * f_tmp, self.aim_speed);
290             move_angle_y = bound(-self.aim_speed, move_angle_y * self.track_accel_rot * f_tmp, self.aim_speed);
291             move_angle = (self.tur_head.avelocity * self.track_blendrate) + (move_angle * (1 - self.track_blendrate));
292             break;
293
294         case TFL_TRACKTYPE_FLUIDPRECISE:
295
296             move_angle_y = bound(-self.aim_speed, move_angle_y, self.aim_speed);
297             move_angle_x = bound(-self.aim_speed, move_angle_x, self.aim_speed);
298
299             break;
300     }
301
302     //  pitch
303     if (self.track_flags & TFL_TRACK_PITCH)
304     {
305         self.tur_head.avelocity_x = move_angle_x;
306         if((self.tur_head.angles_x + self.tur_head.avelocity_x * self.ticrate) > self.aim_maxpitch)
307         {
308             self.tur_head.avelocity_x = 0;
309             self.tur_head.angles_x = self.aim_maxpitch;
310         }
311         
312         if((self.tur_head.angles_x + self.tur_head.avelocity_x * self.ticrate) < -self.aim_maxpitch)
313         {
314             self.tur_head.avelocity_x = 0;
315             self.tur_head.angles_x = -self.aim_maxpitch;
316         }
317     }
318
319     //  rot
320     if (self.track_flags & TFL_TRACK_ROT)
321     {
322         self.tur_head.avelocity_y = move_angle_y;
323
324         if((self.tur_head.angles_y + self.tur_head.avelocity_y * self.ticrate) > self.aim_maxrot)
325         {
326             self.tur_head.avelocity_y = 0;
327             self.tur_head.angles_y = self.aim_maxrot;
328         }
329
330         if((self.tur_head.angles_y + self.tur_head.avelocity_y * self.ticrate) < -self.aim_maxrot)
331         {
332             self.tur_head.avelocity_y = 0;
333             self.tur_head.angles_y = -self.aim_maxrot;
334         }
335     }
336 }
337
338
339 /*
340  + = implemented
341  - = not implemented
342
343  + TFL_FIRECHECK_NO
344  + TFL_FIRECHECK_WORLD
345  + TFL_FIRECHECK_DEAD
346  + TFL_FIRECHECK_DISTANCES
347  - TFL_FIRECHECK_LOS
348  + TFL_FIRECHECK_AIMDIST
349  + TFL_FIRECHECK_REALDIST
350  - TFL_FIRECHECK_ANGLEDIST
351  - TFL_FIRECHECK_TEAMCECK
352  + TFL_FIRECHECK_AFF
353  + TFL_FIRECHECK_OWM_AMMO
354  + TFL_FIRECHECK_OTHER_AMMO
355  + TFL_FIRECHECK_REFIRE
356 */
357
358 /**
359 ** Preforms pre-fire checks based on the uints firecheck_flags
360 **/
361 float turret_stdproc_firecheck()
362 {
363     // This one just dont care =)
364     if (self.firecheck_flags & TFL_FIRECHECK_NO) return 1;
365
366     // Ready?
367     if (self.firecheck_flags & TFL_FIRECHECK_REFIRE)
368         if (self.attack_finished_single > time) return 0;
369
370     // Special case: volly fire turret that has to fire a full volly if a shot was fired.
371     if (self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
372         if (self.volly_counter != self.shot_volly)
373                         if(self.ammo >= self.shot_dmg)
374                                 return 1;               
375
376     // Lack of zombies makes shooting dead things unnecessary :P
377     if (self.firecheck_flags & TFL_FIRECHECK_DEAD)
378         if (self.enemy.deadflag != DEAD_NO)
379             return 0;
380
381     // Plz stop killing the world!
382     if (self.firecheck_flags & TFL_FIRECHECK_WORLD)
383         if (self.enemy == world)
384             return 0;
385
386     // Own ammo?
387     if (self.firecheck_flags & TFL_FIRECHECK_OWM_AMMO)
388         if (self.ammo < self.shot_dmg)
389             return 0;
390
391     // Other's ammo? (support-supply units)
392     if (self.firecheck_flags & TFL_FIRECHECK_OTHER_AMMO)
393         if (self.enemy.ammo >= self.enemy.ammo_max)
394             return 0;
395         
396         // Target of opertunity?
397         if(turret_validate_target(self, self.tur_impactent, self.target_validate_flags) > 0)
398         {
399                 self.enemy = self.tur_impactent;
400                 return 1;
401         }                               
402
403     if (self.firecheck_flags & TFL_FIRECHECK_DISTANCES)
404     {
405         // To close?
406         if (self.tur_dist_aimpos < self.target_range_min)
407                         if(turret_validate_target(self, self.tur_impactent, self.target_validate_flags) > 0)                    
408                                 return 1; // Target of opertunity?
409                         else 
410                                 return 0;                               
411     }
412
413     // Try to avoid FF?
414     if (self.firecheck_flags & TFL_FIRECHECK_AFF)
415         if (self.tur_impactent.team == self.team)
416             return 0;
417
418     // aim<->predicted impact
419     if (self.firecheck_flags & TFL_FIRECHECK_AIMDIST)
420         if (self.tur_dist_impact_to_aimpos > self.aim_firetolerance_dist)
421             return 0;
422
423     // Volly status
424     if (self.shot_volly > 1)
425         if (self.volly_counter == self.shot_volly)
426             if (self.ammo < (self.shot_dmg * self.shot_volly))
427                 return 0;
428
429     if(self.firecheck_flags & TFL_FIRECHECK_VERIFIED)
430         if(self.tur_impactent != self.enemy)
431             return 0;
432
433     return 1;
434 }
435
436 /*
437  + TFL_TARGETSELECT_NO
438  + TFL_TARGETSELECT_LOS
439  + TFL_TARGETSELECT_PLAYERS
440  + TFL_TARGETSELECT_MISSILES
441  - TFL_TARGETSELECT_TRIGGERTARGET
442  + TFL_TARGETSELECT_ANGLELIMITS
443  + TFL_TARGETSELECT_RANGELIMTS
444  + TFL_TARGETSELECT_TEAMCHECK
445  - TFL_TARGETSELECT_NOBUILTIN
446  + TFL_TARGETSELECT_OWNTEAM
447 */
448
449 /**
450 ** Evaluate a entity for target valitity based on validate_flags
451 ** NOTE: the caller must check takedamage before calling this, to inline this check.
452 **/
453 float turret_validate_target(entity e_turret, entity e_target, float validate_flags)
454 {
455     vector v_tmp;
456
457     //if(!validate_flags & TFL_TARGETSELECT_NOBUILTIN)
458     //    return -0.5;
459
460     if(e_target.owner == e_turret)
461         return -0.5;
462
463     if not(checkpvs(e_target.origin, e_turret))
464         return -1;
465
466     if not (e_target)
467         return -2;
468
469         if(g_onslaught)
470                 if (substring(e_target.classname, 0, 10) == "onslaught_") // don't attack onslaught targets, that's the player's job!
471                         return - 3;
472
473     if (validate_flags & TFL_TARGETSELECT_NO)
474         return -4;
475
476     // If only this was used more..
477     if (e_target.flags & FL_NOTARGET)
478         return -5;
479
480     // Cant touch this
481     if (e_target.health < 0)
482         return -6;
483
484     // player
485     if (e_target.flags & FL_CLIENT)
486     {
487         if not (validate_flags & TFL_TARGETSELECT_PLAYERS)
488             return -7;
489
490         if (e_target.deadflag != DEAD_NO)
491             return -8;
492     }
493
494         // enemy turrets
495         if (validate_flags & TFL_TARGETSELECT_NOTURRETS)
496         if (e_target.turret_firefunc || e_target.owner.tur_head == e_target)
497             if(e_target.team != e_turret.team) // Dont break support units.
498                 return -9;
499
500     // Missile
501     if (e_target.flags & FL_PROJECTILE)
502         if not (validate_flags & TFL_TARGETSELECT_MISSILES)
503             return -10;
504
505     if (validate_flags & TFL_TARGETSELECT_MISSILESONLY)
506         if not (e_target.flags & FL_PROJECTILE)
507             return -10.5;
508
509     // Team check
510     if (validate_flags & TFL_TARGETSELECT_TEAMCHECK)
511     {
512         if (validate_flags & TFL_TARGETSELECT_OWNTEAM)
513         {
514             if (e_target.team != e_turret.team)
515                 return -11;
516
517             if (e_turret.team != e_target.owner.team)
518                 return -12;
519         }
520         else
521         {
522             if (e_target.team == e_turret.team)
523                 return -13;
524
525             if (e_turret.team == e_target.owner.team)
526                 return -14;
527         }
528     }
529
530     // Range limits?
531     tvt_dist = vlen(e_turret.origin - real_origin(e_target));
532     if (validate_flags & TFL_TARGETSELECT_RANGELIMTS)
533     {
534         if (tvt_dist < e_turret.target_range_min)
535             return -15;
536
537         if (tvt_dist > e_turret.target_range)
538             return -16;
539     }
540
541     // Can we even aim this thing?
542     tvt_thadv = angleofs3(e_turret.tur_head.origin, e_turret.angles + e_turret.tur_head.angles, e_target);
543     tvt_tadv  = shortangle_vxy(angleofs(e_turret, e_target), e_turret.angles);
544     tvt_thadf = vlen(tvt_thadv);
545     tvt_tadf  = vlen(tvt_tadv);
546
547     /*
548     if(validate_flags & TFL_TARGETSELECT_FOV)
549     {
550         if(e_turret.target_select_fov < tvt_thadf)
551             return -21;
552     }
553     */
554
555     if (validate_flags & TFL_TARGETSELECT_ANGLELIMITS)
556     {
557         if (fabs(tvt_tadv_x) > e_turret.aim_maxpitch)
558             return -17;
559
560         if (fabs(tvt_tadv_y) > e_turret.aim_maxrot)
561             return -18;
562     }
563
564     // Line of sight?
565     if (validate_flags & TFL_TARGETSELECT_LOS)
566     {
567         v_tmp = real_origin(e_target) + ((e_target.mins + e_target.maxs) * 0.5);
568
569         traceline(e_turret.tur_shotorg, v_tmp, 0, e_turret);
570
571         if (e_turret.aim_firetolerance_dist < vlen(v_tmp - trace_endpos))
572             return -19;
573     }
574
575     if (e_target.classname == "grapplinghook")
576         return -20;
577
578     /*
579     if (e_target.classname == "func_button")
580         return -21;
581     */
582
583 #ifdef TURRET_DEBUG_TARGETSELECT
584     dprint("Target:",e_target.netname," is a valid target for ",e_turret.netname,"\n");
585 #endif
586
587     return 1;
588 }
589
590 entity turret_select_target()
591 {
592     entity e;        // target looper entity
593     float  score;    // target looper entity score
594     entity e_enemy;  // currently best scoreing target
595     float  m_score;  // currently best scoreing target's score
596
597     m_score = 0;
598     if(self.enemy)
599         if(self.enemy.takedamage)
600     if(turret_validate_target(self,self.enemy,self.target_validate_flags) > 0)
601     {
602         e_enemy = self.enemy;
603         m_score = self.turret_score_target(self,e_enemy) * self.target_select_samebias;
604     }
605     else
606         self.enemy = world;
607
608     e = findradius(self.origin, self.target_range);
609
610     // Nothing to aim at?
611     if (!e) 
612                 return world;
613
614     while (e)
615     {
616                 if(e.takedamage)
617                 {
618                         if (turret_validate_target(self, e, self.target_select_flags) > 0)
619                         {
620                                 score = self.turret_score_target(self,e);
621                                 if ((score > m_score) && (score > 0))
622                                 {
623                                         e_enemy = e;
624                                         m_score = score;
625                                 }
626                         }
627                 }
628         e = e.chain;
629     }
630
631     return e_enemy;
632 }
633
634 void turret_think()
635 {
636     entity e;
637
638     self.nextthink = time + self.ticrate;
639
640     // ONS uses somewhat backwards linking.
641     if (teams_matter)
642     {
643         if not (g_onslaught)
644             if (self.target)
645             {
646                 e = find(world, targetname,self.target);
647                 if (e != world)
648                     self.team = e.team;
649             }
650
651         if (self.team != self.tur_head.team)
652             turret_stdproc_respawn();
653     }
654
655 #ifdef TURRET_DEBUG
656     if (self.tur_dbg_tmr1 < time)
657     {
658         if (self.enemy) paint_target (self.enemy,128,self.tur_dbg_rvec,0.9);
659         paint_target(self,256,self.tur_dbg_rvec,0.9);
660         self.tur_dbg_tmr1 = time + 1;
661     }
662 #endif
663
664     // Handle ammo
665     if not (self.spawnflags & TSF_NO_AMMO_REGEN)
666     if (self.ammo < self.ammo_max)
667         self.ammo = min(self.ammo + self.ammo_recharge, self.ammo_max);
668
669     if (self.health < (self.tur_health * 0.5))
670                 if(random() < 0.25)
671                         te_spark(self.origin + '0 0 40', randomvec() * 256 + '0 0 256', 16);
672                         
673     // Inactive turrets needs to run the think loop,
674     // So they can handle animation and wake up if need be.
675     if not (self.tur_active)
676     {
677         turret_stdproc_track();
678         return;
679     }
680
681     // This is typicaly used for zaping every target in range
682     // turret_fusionreactor uses this to recharge friendlys.
683     if (self.shoot_flags & TFL_SHOOT_HITALLVALID)
684     {
685         // Do a self.turret_fire for every valid target.
686         e = findradius(self.origin,self.target_range);
687         while (e)
688         {
689                         if(e.takedamage)
690                         {
691                                 if (turret_validate_target(self,e,self.target_validate_flags))
692                                 {
693                                         self.enemy = e;
694
695                                         turret_do_updates(self);
696
697                                         if (self.turret_firecheckfunc())
698                                                 turret_fire();
699                                 }
700                         }
701
702             e = e.chain;
703         }
704         self.enemy = world;
705     }
706     else if(self.shoot_flags & TFL_SHOOT_CUSTOM)
707     {
708         // This one is doing something.. oddball. assume its handles what needs to be handled.
709
710         // Predict?
711         if not(self.aim_flags & TFL_AIM_NO)
712             self.tur_aimpos = turret_stdproc_aim_generic();
713
714         // Turn & pitch?
715         if not(self.track_flags & TFL_TRACK_NO)
716             turret_stdproc_track();
717
718         turret_do_updates(self);
719
720         // Fire?
721         if (self.turret_firecheckfunc())
722             turret_fire();
723     }
724     else
725     {
726         // Special case for volly always. if it fired once it must compleate the volly.
727         if(self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
728             if(self.volly_counter != self.shot_volly)
729             {
730                 // Predict or whatnot
731                 if not(self.aim_flags & TFL_AIM_NO)
732                     self.tur_aimpos = turret_stdproc_aim_generic();
733
734                 // Turn & pitch
735                 if not(self.track_flags & TFL_TRACK_NO)
736                     turret_stdproc_track();
737
738                 turret_do_updates(self);
739
740                 // Fire!
741                 if (self.turret_firecheckfunc() != 0)
742                     turret_fire();
743
744                 if(self.turret_postthink)
745                     self.turret_postthink();
746
747                 return;
748             }
749
750         // Check if we have a vailid enemy, and try to find one if we dont.
751
752         // g_turrets_targetscan_maxdelay forces a target re-scan at least this often
753         float do_target_scan;
754         if((self.target_select_time + autocvar_g_turrets_targetscan_maxdelay) < time)
755             do_target_scan = 1;
756
757         // Old target (if any) invalid?
758         if (turret_validate_target(self, self.enemy, self.target_validate_flags) <= 0)
759         {
760                 self.enemy = world;
761                 do_target_scan = 1;
762         }
763
764         // But never more often then g_turrets_targetscan_mindelay!
765         if (self.target_select_time + autocvar_g_turrets_targetscan_mindelay > time)
766             do_target_scan = 0;
767
768         if(do_target_scan)
769         {
770             self.enemy = turret_select_target();
771             self.target_select_time = time;
772         }
773
774         // No target, just go to idle, do any custom stuff and bail.
775         if (self.enemy == world)
776         {
777             // Turn & pitch
778             if not(self.track_flags & TFL_TRACK_NO)
779                 turret_stdproc_track();
780
781             // do any per-turret stuff
782             if(self.turret_postthink)
783                 self.turret_postthink();
784
785             // And bail.
786             return;
787         }
788         else
789             self.lip = time + autocvar_g_turrets_aimidle_delay; // Keep track of the last time we had a target.
790
791         // Predict?
792         if not(self.aim_flags & TFL_AIM_NO)
793             self.tur_aimpos = turret_stdproc_aim_generic();
794
795         // Turn & pitch?
796         if not(self.track_flags & TFL_TRACK_NO)
797             turret_stdproc_track();
798
799         turret_do_updates(self);
800
801         // Fire?
802         if (self.turret_firecheckfunc())
803             turret_fire();
804     }
805
806     // do any custom per-turret stuff
807     if(self.turret_postthink)
808         self.turret_postthink();
809 }
810
811 void turret_fire()
812 {
813     if (autocvar_g_turrets_nofire != 0)
814         return;
815
816     self.turret_firefunc();
817
818     self.attack_finished_single = time + self.shot_refire;
819     self.ammo -= self.shot_dmg;
820     self.volly_counter = self.volly_counter - 1;
821
822     if (self.volly_counter <= 0)
823     {
824         self.volly_counter = self.shot_volly;
825
826         if (self.shoot_flags & TFL_SHOOT_CLEARTARGET)
827             self.enemy = world;
828
829         if (self.shot_volly > 1)
830             self.attack_finished_single = time + self.shot_volly_refire;
831     }
832
833 #ifdef TURRET_DEBUG
834     if (self.enemy) paint_target3(self.tur_aimpos, 64, self.tur_dbg_rvec, self.tur_impacttime + 0.25);
835 #endif
836 }
837
838 void turret_stdproc_fire()
839 {
840     dprint("^1Bang, ^3your dead^7 ",self.enemy.netname,"! ^1(turret with no real firefunc)\n");
841 }
842
843 /*
844     When .used a turret switch team to activator.team.
845     If activator is world, the turret go inactive.
846 */
847 void turret_stdproc_use()
848 {
849     dprint("Turret ",self.netname, " used by ", activator.classname, "\n");
850
851     self.team = activator.team;
852
853     if(self.team == 0)
854         self.tur_active = 0;
855     else
856         self.tur_active = 1;
857
858 }
859
860 void turret_link()
861 {
862     Net_LinkEntity(self, TRUE, 0, turret_send);
863     self.think      = turret_think;
864     self.nextthink  = time;
865 }
866
867 void turrets_manager_think()
868 {
869     self.nextthink = time + 1;
870
871     entity e;
872     if (autocvar_g_turrets_reloadcvars == 1)
873     {
874         e = nextent(world);
875         while (e)
876         {
877             if (e.turrcaps_flags & TFL_TURRCAPS_ISTURRET)
878             {
879                 load_unit_settings(e,e.cvar_basename,1);
880                 if(e.turret_postthink)
881                     e.turret_postthink();
882             }
883
884             e = nextent(e);
885         }
886         cvar_set("g_turrets_reloadcvars","0");
887     }
888 }
889
890 /*
891 * Standard turret initialization. use this!
892 * (unless you have a very good reason not to)
893 * if the return value is 0, the turret should be removed.
894 */
895 float turret_stdproc_init (string cvar_base_name, float csqc_shared, string base, string head, float _turret_type)
896 {
897         entity e, ee;
898
899     // Are turrets allowed?
900     if (autocvar_g_turrets == 0)
901         return 0;
902     
903     if(_turret_type < 1 || _turret_type > TID_LAST)
904     {
905         dprint("Invalid / Unkown turret type\"", ftos(_turret_type), "\", aborting!\n");
906         return 0;
907     }    
908     self.turret_type = _turret_type;
909     
910     e = find(world, classname, "turret_manager");
911     if not (e)
912     {
913         e = spawn();
914
915         /*
916         setorigin(e,'0 0 0');
917         setmodel(e,"models/turrets/plasma.md3");
918         vector v;
919         v = gettaginfo(e,gettagindex(e,"tag_fire"));
920         if(v == '0 0 0')
921         {
922             //objerror("^1ERROR: Engine is borken! Turrets will NOT work. force g_turrets to 0 to run maps with turrets anyway.");
923             //crash();
924         }
925         setmodel(e,"");
926         */
927
928         e.classname = "turret_manager";
929         e.think = turrets_manager_think;
930         e.nextthink = time + 2;
931     }
932
933     /*
934     if(csqc_shared)
935     {
936         dprint("WARNING: turret requested csqc_shared but this is not implemented. Expect strange things to happen.\n");
937         csqc_shared = 0;
938     }
939     */
940     
941     if not (self.spawnflags & TSF_SUSPENDED)
942         droptofloor_builtin();
943
944     // Terrainbase spawnflag. This puts a enlongated model
945     // under the turret, so it looks ok on uneaven surfaces.
946     /*  TODO: Handle this with CSQC
947     if (self.spawnflags & TSF_TERRAINBASE)
948     {
949         entity tb;
950         tb = spawn();
951         setmodel(tb,"models/turrets/terrainbase.md3");
952         setorigin(tb,self.origin);
953         tb.solid = SOLID_BBOX;
954     }
955     */
956
957     self.cvar_basename = cvar_base_name;
958     load_unit_settings(self, self.cvar_basename, 0);
959
960     // Handle turret teams.
961     if (autocvar_g_assault != 0)
962     {
963         if not (self.team)
964             self.team = 14; // Assume turrets are on the defending side if not explicitly set otehrwize
965     }
966     else if not (teams_matter)
967                 self.team = MAX_SHOT_DISTANCE; // Group all turrets into the same team, so they dont kill eachother.
968         else if(g_onslaught && self.targetname)
969         {
970                 e = find(world,target,self.targetname);
971                 if(e != world)
972                 {
973                         self.team = e.team;
974                         ee = e;
975                 }
976         }
977         else if(!self.team)
978                 self.team = MAX_SHOT_DISTANCE; // Group all turrets into the same team, so they dont kill eachother.
979
980     /*
981     * Try to guess some reasonaly defaults
982     * for missing params and do sanety checks
983     * thise checks could produce some "interesting" results
984     * if it hits a glitch in my logic :P so try to set as mutch
985     * as possible beforehand.
986     */
987     if (self.turrcaps_flags & TFL_TURRCAPS_SUPPORT)
988         self.ticrate = 0.2;     // Support units generaly dont need to have a high speed ai-loop
989     else
990         self.ticrate = 0.1;     // 10 fps for normal turrets
991
992     self.ticrate = bound(sys_frametime, self.ticrate, 60);  // keep it sane
993
994 // General stuff
995     if (self.netname == "")
996         self.netname = self.classname;
997
998     if not (self.respawntime)
999         self.respawntime = 60;
1000     self.respawntime = max(-1, self.respawntime);
1001
1002     if not (self.health)
1003         self.health = 1000;
1004     self.tur_health = max(1, self.health);
1005
1006     if not (self.turrcaps_flags)
1007         self.turrcaps_flags = TFL_TURRCAPS_RADIUSDMG | TFL_TURRCAPS_MEDPROJ | TFL_TURRCAPS_PLAYERKILL;
1008
1009     if not (self.damage_flags)
1010         self.damage_flags = TFL_DMG_YES | TFL_DMG_RETALIATE | TFL_DMG_AIMSHAKE;
1011
1012 // Shot stuff.
1013     if not (self.shot_refire)
1014         self.shot_refire = 1;
1015     self.shot_refire = bound(0.01, self.shot_refire, 9999);
1016
1017     if not (self.shot_dmg)
1018         self.shot_dmg  = self.shot_refire * 50;
1019     self.shot_dmg = max(1, self.shot_dmg);
1020
1021     if not (self.shot_radius)
1022         self.shot_radius = self.shot_dmg * 0.5;
1023     self.shot_radius = max(1, self.shot_radius);
1024
1025     if not (self.shot_speed)
1026         self.shot_speed = 2500;
1027     self.shot_speed = max(1, self.shot_speed);
1028
1029     if not (self.shot_spread)
1030         self.shot_spread = 0.0125;
1031     self.shot_spread = bound(0.0001, self.shot_spread, 500);
1032
1033     if not (self.shot_force)
1034         self.shot_force = self.shot_dmg * 0.5 + self.shot_radius * 0.5;
1035     self.shot_force = bound(0.001, self.shot_force, 5000);
1036
1037     if not (self.shot_volly)
1038         self.shot_volly = 1;
1039     self.shot_volly = bound(1, self.shot_volly, floor(self.ammo_max / self.shot_dmg));
1040
1041     if not (self.shot_volly_refire)
1042         self.shot_volly_refire = self.shot_refire * self.shot_volly;
1043     self.shot_volly_refire = bound(self.shot_refire, self.shot_volly_refire, 60);
1044
1045     if not (self.firecheck_flags)
1046         self.firecheck_flags = TFL_FIRECHECK_WORLD | TFL_FIRECHECK_DEAD | TFL_FIRECHECK_DISTANCES |
1047                                TFL_FIRECHECK_LOS | TFL_FIRECHECK_AIMDIST | TFL_FIRECHECK_TEAMCECK |
1048                                TFL_FIRECHECK_OWM_AMMO | TFL_FIRECHECK_REFIRE | TFL_FIRECHECK_WORLD;
1049
1050 // Range stuff.
1051     if not (self.target_range)
1052         self.target_range = self.shot_speed * 0.5;
1053     self.target_range = bound(0, self.target_range, MAX_SHOT_DISTANCE);
1054
1055     if not (self.target_range_min)
1056         self.target_range_min = self.shot_radius * 2;
1057     self.target_range_min = bound(0, self.target_range_min, MAX_SHOT_DISTANCE);
1058
1059     if not (self.target_range_optimal)
1060         self.target_range_optimal = self.target_range * 0.5;
1061     self.target_range_optimal = bound(0, self.target_range_optimal, MAX_SHOT_DISTANCE);
1062
1063
1064 // Aim stuff.
1065     if not (self.aim_maxrot)
1066         self.aim_maxrot = 90;
1067     self.aim_maxrot = bound(0, self.aim_maxrot, 360);
1068
1069     if not (self.aim_maxpitch)
1070         self.aim_maxpitch = 20;
1071     self.aim_maxpitch = bound(0, self.aim_maxpitch, 90);
1072
1073     if not (self.aim_speed)
1074         self.aim_speed = 36;
1075     self.aim_speed  = bound(0.1, self.aim_speed, 1000);
1076
1077     if not (self.aim_firetolerance_dist)
1078         self.aim_firetolerance_dist  = 5 + (self.shot_radius * 2);
1079     self.aim_firetolerance_dist = bound(0.1, self.aim_firetolerance_dist, MAX_SHOT_DISTANCE);
1080
1081     if not (self.aim_flags)
1082     {
1083         self.aim_flags = TFL_AIM_LEAD | TFL_AIM_SHOTTIMECOMPENSATE;
1084         if(self.turrcaps_flags & TFL_TURRCAPS_RADIUSDMG)
1085             self.aim_flags |= TFL_AIM_GROUND2;
1086     }
1087
1088     if not (self.track_type)
1089         self.track_type = TFL_TRACKTYPE_STEPMOTOR;
1090
1091     if (self.track_type != TFL_TRACKTYPE_STEPMOTOR)
1092     {
1093         // Fluid / Ineria mode. Looks mutch nicer.
1094         // Can reduce aim preformance alot, needs a bit diffrent aimspeed
1095
1096         if not (self.aim_speed)
1097             self.aim_speed = 180;
1098         self.aim_speed = bound(0.1, self.aim_speed, 1000);
1099
1100         if not (self.track_accel_pitch)
1101             self.track_accel_pitch = 0.5;
1102
1103         if not (self.track_accel_rot)
1104             self.track_accel_rot   = 0.5;
1105
1106         if not (self.track_blendrate)
1107             self.track_blendrate   = 0.35;
1108     }
1109
1110     if (!self.track_flags)
1111         self.track_flags = TFL_TRACK_PITCH | TFL_TRACK_ROT;
1112
1113
1114 // Target selection stuff.
1115     if not (self.target_select_rangebias)
1116         self.target_select_rangebias = 1;
1117     self.target_select_rangebias = bound(-10, self.target_select_rangebias, 10);
1118
1119     if not (self.target_select_samebias)
1120         self.target_select_samebias = 1;
1121     self.target_select_samebias = bound(-10, self.target_select_samebias, 10);
1122
1123     if not (self.target_select_anglebias)
1124         self.target_select_anglebias = 1;
1125     self.target_select_anglebias = bound(-10, self.target_select_anglebias, 10);
1126
1127     if not (self.target_select_missilebias)
1128         self.target_select_missilebias = -10;
1129
1130     self.target_select_missilebias = bound(-10, self.target_select_missilebias, 10);
1131     self.target_select_playerbias = bound(-10, self.target_select_playerbias, 10);
1132
1133     if not (self.target_select_flags)
1134     {
1135             self.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_TEAMCHECK
1136                                      | TFL_TARGETSELECT_RANGELIMTS | TFL_TARGETSELECT_ANGLELIMITS;
1137
1138         if (self.turrcaps_flags & TFL_TURRCAPS_MISSILEKILL)
1139             self.target_select_flags |= TFL_TARGETSELECT_MISSILES;
1140
1141         if (self.turrcaps_flags & TFL_TURRCAPS_PLAYERKILL)
1142             self.target_select_flags |= TFL_TARGETSELECT_PLAYERS;
1143         //else
1144         //    self.target_select_flags = TFL_TARGETSELECT_NO;
1145     }
1146
1147     self.target_validate_flags = self.target_select_flags;
1148
1149
1150 // Ammo stuff
1151     if not (self.ammo_max)
1152         self.ammo_max = self.shot_dmg * 10;
1153     self.ammo_max = max(self.shot_dmg, self.ammo_max);
1154
1155     if not (self.ammo)
1156         self.ammo = self.shot_dmg * 5;
1157     self.ammo = bound(0,self.ammo, self.ammo_max);
1158
1159     if not (self.ammo_recharge)
1160         self.ammo_recharge = self.shot_dmg * 0.5;
1161     self.ammo_recharge = max(0 ,self.ammo_recharge);
1162
1163     // Convert the recharge from X per sec to X per ticrate
1164     self.ammo_recharge = self.ammo_recharge * self.ticrate;
1165
1166     if not (self.ammo_flags)
1167         self.ammo_flags = TFL_AMMO_ENERGY | TFL_AMMO_RECHARGE;
1168
1169 // Damage stuff
1170     if(self.spawnflags & TSL_NO_RESPAWN)
1171         if not (self.damage_flags & TFL_DMG_DEATH_NORESPAWN)
1172             self.damage_flags |= TFL_DMG_DEATH_NORESPAWN;
1173
1174 // Offsets & origins
1175     if (!self.tur_shotorg)   self.tur_shotorg = '50 0 50';
1176
1177 // Gane hooks
1178         if(MUTATOR_CALLHOOK(TurretSpawn))
1179                 return 0;
1180
1181 // End of default & sanety checks, start building the turret.
1182
1183 // Spawn extra bits
1184     self.tur_head         = spawn();
1185     self.tur_head.netname = self.tur_head.classname = "turret_head";
1186     self.tur_head.team    = self.team;
1187     self.tur_head.owner   = self;
1188
1189     setmodel(self, base);
1190     setmodel(self.tur_head, head);
1191
1192     setsize(self, '-32 -32 0', '32 32 64');
1193     setsize(self.tur_head, '0 0 0', '0 0 0');
1194
1195     setorigin(self.tur_head, '0 0 0');
1196     setattachment(self.tur_head, self, "tag_head");
1197
1198     if (!self.health)
1199         self.health = 150;
1200
1201     self.tur_health          = self.health;
1202     self.solid               = SOLID_BBOX;
1203     self.tur_head.solid      = SOLID_NOT;
1204     self.takedamage          = DAMAGE_AIM;
1205     self.tur_head.takedamage = DAMAGE_NO;
1206     self.movetype            = MOVETYPE_NOCLIP;
1207     self.tur_head.movetype   = MOVETYPE_NOCLIP;
1208
1209     // Defend mode?
1210     if not (self.tur_defend)
1211     if (self.target != "")
1212     {
1213         self.tur_defend = find(world, targetname, self.target);
1214         if (self.tur_defend == world)
1215         {
1216             self.target = "";
1217             dprint("Turret has invalid defendpoint!\n");
1218         }
1219     }
1220
1221     // In target defend mode, aim on the spot to defend when idle.
1222     if (self.tur_defend)
1223         self.idle_aim  = self.tur_head.angles + angleofs(self.tur_head, self.tur_defend);
1224     else
1225         self.idle_aim  = '0 0 0';
1226
1227     // Team color
1228     if (self.team == COLOR_TEAM1) self.colormod = '1.4 0.8 0.8';
1229     if (self.team == COLOR_TEAM2) self.colormod = '0.8 0.8 1.4';
1230
1231     // Attach stdprocs. override when and what needed
1232     if (self.turrcaps_flags & TFL_TURRCAPS_SUPPORT)
1233     {
1234         self.turret_score_target    = turret_stdproc_targetscore_support;
1235         self.turret_firecheckfunc   = turret_stdproc_firecheck;
1236         self.turret_firefunc        = turret_stdproc_fire;
1237         self.event_damage           = turret_stdproc_damage;
1238     }
1239     else
1240     {
1241         self.turret_score_target    = turret_stdproc_targetscore_generic;
1242         self.turret_firecheckfunc   = turret_stdproc_firecheck;
1243         self.turret_firefunc        = turret_stdproc_fire;
1244         self.event_damage           = turret_stdproc_damage;
1245     }
1246
1247     self.use = turret_stdproc_use;
1248     self.bot_attack = TRUE;
1249
1250     ++turret_count;
1251     self.nextthink = time + 1;
1252     self.nextthink +=  turret_count * sys_frametime;
1253
1254     self.tur_head.team = self.team;
1255     self.view_ofs = '0 0 0';
1256
1257 #ifdef TURRET_DEBUG
1258     self.tur_dbg_start = self.nextthink;
1259     while (vlen(self.tur_dbg_rvec) < 2)
1260         self.tur_dbg_rvec  = randomvec() * 4;
1261
1262     self.tur_dbg_rvec_x = fabs(self.tur_dbg_rvec_x);
1263     self.tur_dbg_rvec_y = fabs(self.tur_dbg_rvec_y);
1264     self.tur_dbg_rvec_z = fabs(self.tur_dbg_rvec_z);
1265 #endif
1266
1267     // Its all good.
1268     self.turrcaps_flags |= TFL_TURRCAPS_ISTURRET;
1269
1270     self.classname = "turret_main";
1271
1272     self.tur_active = 1;
1273
1274     // In ONS mode, and linked to a ONS ent. need to call the use to set team.
1275     if (g_onslaught && ee)
1276     {
1277         activator = ee;
1278         self.use();
1279     }
1280     
1281         turret_stdproc_respawn();
1282         
1283     // Initiate the main AI loop
1284     if(csqc_shared)
1285         self.think     = turret_link;
1286     else
1287         self.think     = turret_think;
1288
1289     return 1;
1290 }
1291
1292