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