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