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