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