]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/tturrets/system/system_main.qc
Scaled the weapon models down about 30% to match 2.5.2 sizes. Except the tuba, I...
[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         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         traceline(e_turret.tur_shotorg,v_tmp,0,e_turret);
571
572         if (e_turret.aim_firetolerance_dist < vlen(v_tmp - trace_endpos))
573             return -19;
574     }
575
576     if (e_target.classname == "grapplinghook")
577         return -20;
578
579     /*
580     if (e_target.classname == "func_button")
581         return -21;
582     */
583
584 #ifdef TURRET_DEBUG_TARGETSELECT
585     dprint("Target:",e_target.netname," is a valid target for ",e_turret.netname,"\n");
586 #endif
587
588     return 1;
589 }
590
591 entity turret_select_target()
592 {
593     entity e;        // target looper entity
594     float  score;    // target looper entity score
595     entity e_enemy;  // currently best scoreing target
596     float  m_score;  // currently best scoreing target's score
597     float f;
598
599     m_score = 0;
600     if(self.enemy)
601         if(self.enemy.takedamage)
602     if(turret_validate_target(self,self.enemy,self.target_validate_flags) > 0)
603     {
604         e_enemy = self.enemy;
605         m_score = self.turret_score_target(self,e_enemy) * self.target_select_samebias;
606     }
607     else
608         self.enemy = world;
609
610     e = findradius(self.origin,self.target_range);
611
612     // Nothing to aim at?
613     if (!e) return world;
614
615     while (e)
616     {
617                 if(e.takedamage)
618                 {
619                         f = turret_validate_target(self,e,self.target_select_flags);
620                         if (f > 0)
621                         {
622                                 score = self.turret_score_target(self,e);
623                                 if ((score > m_score) && (score > 0))
624                                 {
625                                         e_enemy = e;
626                                         m_score = score;
627                                 }
628                         }
629                 }
630         e = e.chain;
631     }
632
633     return e_enemy;
634 }
635
636 void turret_think()
637 {
638     entity e;
639
640     self.nextthink = time + self.ticrate;
641
642     // ONS uses somewhat backwards linking.
643     if (teamplay)
644     {
645         if not (g_onslaught)
646             if (self.target)
647             {
648                 e = find(world,targetname,self.target);
649                 if (e != world)
650                     self.team = e.team;
651             }
652
653         if (self.team != self.tur_head.team)
654             turret_stdproc_respawn();
655     }
656
657 #ifdef TURRET_DEBUG
658     if (self.tur_dbg_tmr1 < time)
659     {
660         if (self.enemy) paint_target (self.enemy,128,self.tur_dbg_rvec,0.9);
661         paint_target(self,256,self.tur_dbg_rvec,0.9);
662         self.tur_dbg_tmr1 = time + 1;
663     }
664 #endif
665
666     // Handle ammo
667     if not (self.spawnflags & TSF_NO_AMMO_REGEN)
668     if (self.ammo < self.ammo_max)
669         self.ammo = min(self.ammo + self.ammo_recharge,self.ammo_max);
670
671
672     // Inactive turrets needs to run the think loop,
673     // So they can handle animation and wake up if need be.
674     if not (self.tur_active)
675     {
676         turret_stdproc_track();
677         return;
678     }
679
680     //This is just wrong :| and unlikely to ever happen.
681     /*
682     if(self.deadflag != DEAD_NO)
683     {
684         dprint("WARNING: dead turret running the think function!\n");
685         return;
686     }
687     */
688
689     // This is typicaly used for zaping every target in range
690     // turret_fusionreactor uses this to recharge friendlys.
691     if (self.shoot_flags & TFL_SHOOT_HITALLVALID)
692     {
693         // Do a self.turret_fire for every valid target.
694         e = findradius(self.origin,self.target_range);
695         while (e)
696         {
697                         if(e.takedamage)
698                         {
699                                 if (turret_validate_target(self,e,self.target_validate_flags))
700                                 {
701                                         self.enemy = e;
702
703                                         turret_do_updates(self);
704
705                                         if (self.turret_firecheckfunc())
706                                                 turret_fire();
707                                 }
708                         }
709
710             e = e.chain;
711         }
712         self.enemy = world;
713     }
714     else if(self.shoot_flags & TFL_SHOOT_CUSTOM)
715     {
716         // This one is doing something.. oddball. assume its handles what needs to be handled.
717
718         // Predict?
719         if not(self.aim_flags & TFL_AIM_NO)
720             self.tur_aimpos = turret_stdproc_aim_generic();
721
722         // Turn & pitch?
723         if not(self.track_flags & TFL_TRACK_NO)
724             turret_stdproc_track();
725
726         turret_do_updates(self);
727
728         // Fire?
729         if (self.turret_firecheckfunc())
730             turret_fire();
731     }
732     else
733     {
734         // Special case for volly always. if it fired once it must compleate the volly.
735         if(self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
736             if(self.volly_counter != self.shot_volly)
737             {
738                 // Predict or whatnot
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() != 0)
750                     turret_fire();
751
752                 if(self.turret_postthink)
753                     self.turret_postthink();
754
755                 return;
756             }
757
758         // Check if we have a vailid enemy, and try to find one if we dont.
759
760         // g_turrets_targetscan_maxdelay forces a target re-scan this often
761         float do_target_scan;
762         if((self.target_select_time + cvar("g_turrets_targetscan_maxdelay")) < time)
763             do_target_scan = 1;
764
765         // Old target (if any) invalid?
766         if (turret_validate_target(self,self.enemy,self.target_validate_flags) <= 0)
767             do_target_scan = 1;
768
769         // But never more often then g_turrets_targetscan_mindelay!
770         if (self.target_select_time + cvar("g_turrets_targetscan_mindelay") > time)
771             do_target_scan = 0;
772
773         if(do_target_scan)
774         {
775             self.enemy = turret_select_target();
776             self.target_select_time = time;
777         }
778
779         // No target, just go to idle, do any custom stuff and bail.
780         if (self.enemy == world)
781         {
782             // Turn & pitch
783             if not(self.track_flags & TFL_TRACK_NO)
784                 turret_stdproc_track();
785
786             // do any per-turret stuff
787             if(self.turret_postthink)
788                 self.turret_postthink();
789
790             // And bail.
791             return;
792         }
793         else
794             self.lip = time + cvar("g_turrets_aimidle_delay"); // Keep track of the last time we had a target.
795
796         // Predict?
797         if not(self.aim_flags & TFL_AIM_NO)
798             self.tur_aimpos = turret_stdproc_aim_generic();
799
800         // Turn & pitch?
801         if not(self.track_flags & TFL_TRACK_NO)
802             turret_stdproc_track();
803
804         turret_do_updates(self);
805
806         // Fire?
807         if (self.turret_firecheckfunc())
808             turret_fire();
809     }
810
811     // do any per-turret stuff
812     if(self.turret_postthink)
813         self.turret_postthink();
814 }
815
816 void turret_fire()
817 {
818     if (cvar("g_turrets_nofire") != 0)
819         return;
820
821     /*
822     // unlikely to ever happen.
823     if (self.deadflag != DEAD_NO)
824         return;
825
826     if not (self.tur_active)
827         return;
828     */
829
830     self.turret_firefunc();
831
832     self.attack_finished_single = time + self.shot_refire;
833     self.ammo -= self.shot_dmg;
834     self.volly_counter = self.volly_counter - 1;
835
836     if (self.volly_counter <= 0)
837     {
838         self.volly_counter = self.shot_volly;
839
840         if (self.shoot_flags & TFL_SHOOT_CLEARTARGET)
841             self.enemy = world;
842
843         if (self.shot_volly > 1)
844             self.attack_finished_single = time + self.shot_volly_refire;
845     }
846
847 #ifdef TURRET_DEBUG
848     if (self.enemy) paint_target3(self.tur_aimpos, 64, self.tur_dbg_rvec, self.tur_impacttime + 0.25);
849 #endif
850 }
851
852 void turret_stdproc_fire()
853 {
854     dprint("^1Bang, ^3your dead^7 ",self.enemy.netname,"! ^1(turret with no real firefunc)\n");
855 }
856
857 /*
858     When .used a turret switch team to activator.team.
859     If activator is world, the turrets goes inactive.
860 */
861 void turret_stdproc_use()
862 {
863     dprint("Turret ",self.netname, " used by ",activator.classname,"\n");
864
865     self.team = activator.team;
866
867     if(self.team == 0)
868         self.tur_active = 0;
869     else
870         self.tur_active = 1;
871
872 }
873
874 void turret_link()
875 {
876     //Net_LinkEntity(self, FALSE, 0, Turret_SendEntity);
877     self.think      = turret_think;
878     self.nextthink  = time;
879 }
880
881 void turrets_manager_think()
882 {
883     self.nextthink = time + 1;
884
885     entity e;
886     if (cvar("g_turrets_reloadcvars") == 1)
887     {
888         e = nextent(world);
889         while (e)
890         {
891             if (e.turrcaps_flags & TFL_TURRCAPS_ISTURRET)
892             {
893                 load_unit_settings(e,e.cvar_basename,1);
894                 if(e.turret_postthink)
895                     e.turret_postthink();
896             }
897
898             e = nextent(e);
899         }
900         cvar_set("g_turrets_reloadcvars","0");
901     }
902 }
903
904 /*
905 * Standard turret initialization. use this!
906 * (unless you have a very good reason not to)
907 * if the return value is 0, the turret should be removed.
908 */
909 float turret_stdproc_init (string cvar_base_name, float csqc_shared, string base, string head)
910 {
911         entity e, ee;
912
913     // Are turrets allowed atm?
914     if (cvar("g_turrets") == 0)
915         return 0;
916
917
918     e = find(world, classname, "turret_manager");
919     if not (e)
920     {
921         e = spawn();
922
923         setorigin(e,'0 0 0');
924         setmodel(e,"models/turrets/plasma.md3");
925         vector v;
926         v = gettaginfo(e,gettagindex(e,"tag_fire"));
927         if(v == '0 0 0')
928         {
929             objerror("^1ERROR: Engine is borken! Turrets will NOT work. force g_turrets to 0 to run maps with turrets anyway.");
930             crash();
931         }
932         setmodel(e,"");
933
934         e.classname = "turret_manager";
935         e.think = turrets_manager_think;
936         e.nextthink = time + 2;
937     }
938
939     if(csqc_shared)
940     {
941         dprint("WARNING: turret requested csqc_shared but this is not implemented. Expect strange things to happen.\n");
942         csqc_shared = 0;
943     }
944
945     // Terrainbase spawnflag. This puts a enlongated model
946     // under the turret, so it looks ok on uneaven surfaces.
947     if (self.spawnflags & TSF_TERRAINBASE)
948     {
949         entity tb;
950         tb = spawn();
951         setmodel(tb,"models/turrets/terrainbase.md3");
952         setorigin(tb,self.origin);
953         tb.solid = SOLID_BBOX;
954     }
955
956     self.cvar_basename = cvar_base_name;
957     load_unit_settings(self,self.cvar_basename, 0);
958
959     // Handle turret teams.
960     if (cvar("g_assault") != 0)
961     {
962         if not (self.team)
963             self.team = 14; // Assume turrets are on the defending side if not explicitly set otehrwize
964     }
965     else if not (teamplay)
966                 self.team = MAX_SHOT_DISTANCE; // Group all turrets into the same team iso they dont kill eachother.
967         else if(g_onslaught && self.targetname)
968         {
969                 e = find(world,target,self.targetname);
970                 if(e != world)
971                 {
972                         self.team = e.team;
973                         ee = e;
974                 }
975         }
976         else if(!self.team)
977                 self.team = MAX_SHOT_DISTANCE; // Group all turrets into the same team iso they dont kill eachother.
978
979     /*
980     * Try to guess some reasonaly defaults
981     * for missing params and do sanety checks
982     * thise checks could produce some "interesting" results
983     * if it hits a glitch in my logic :P so try to set as mutch
984     * as possible beforehand.
985     */
986     if (self.turrcaps_flags & TFL_TURRCAPS_SUPPORT)
987         if not (self.ticrate) self.ticrate = 0.2;     // Support units generaly dont need to have a high speed ai-loop
988     else
989         if not (self.ticrate) self.ticrate = 0.1;     // 10 fps for normal turrets
990
991     self.ticrate = bound(sys_frametime,self.ticrate,60);  // keep it sane
992
993 // General stuff
994     if (self.netname == "")
995         self.netname = self.classname;
996
997     if not (self.respawntime)
998         self.respawntime = 60;
999     self.respawntime = max(-1,self.respawntime);
1000
1001     if not (self.health)
1002         self.health = 1000;
1003     self.tur_health = max(1,self.health);
1004
1005     if not (self.turrcaps_flags)
1006         self.turrcaps_flags = TFL_TURRCAPS_RADIUSDMG | TFL_TURRCAPS_MEDPROJ | TFL_TURRCAPS_PLAYERKILL;
1007
1008     if (!self.damage_flags)
1009         self.damage_flags = TFL_DMG_YES | TFL_DMG_RETALIATE | TFL_DMG_AIMSHAKE;
1010
1011 // Shot stuff.
1012     if not (self.shot_refire)
1013         self.shot_refire = 1;
1014     self.shot_refire = bound(0.01,self.shot_refire,9999);
1015
1016     if not (self.shot_dmg)
1017         self.shot_dmg  = self.shot_refire * 50;
1018     self.shot_dmg = max(1,self.shot_dmg);
1019
1020     if not (self.shot_radius)
1021         self.shot_radius = self.shot_dmg * 0.5;
1022     self.shot_radius = max(1,self.shot_radius);
1023
1024     if not (self.shot_speed)
1025         self.shot_speed = 2500;
1026     self.shot_speed = max(1,self.shot_speed);
1027
1028     if not (self.shot_spread)
1029         self.shot_spread = 0.0125;
1030     self.shot_spread = bound(0.0001,self.shot_spread,500);
1031
1032     if not (self.shot_force)
1033         self.shot_force = self.shot_dmg * 0.5 + self.shot_radius * 0.5;
1034     self.shot_force = bound(0.001,self.shot_force,MAX_SHOT_DISTANCE * 0.5);
1035
1036     if not (self.shot_volly)
1037         self.shot_volly = 1;
1038     self.shot_volly = bound(1,self.shot_volly,floor(self.ammo_max / self.shot_dmg));
1039
1040     if not (self.shot_volly_refire)
1041         self.shot_volly_refire = self.shot_refire * self.shot_volly;
1042     self.shot_volly_refire = bound(self.shot_refire,self.shot_volly_refire,60);
1043
1044     if not (self.firecheck_flags)
1045         self.firecheck_flags = TFL_FIRECHECK_WORLD | TFL_FIRECHECK_DEAD | TFL_FIRECHECK_DISTANCES |
1046                                TFL_FIRECHECK_LOS | TFL_FIRECHECK_AIMDIST | TFL_FIRECHECK_TEAMCECK |
1047                                TFL_FIRECHECK_OWM_AMMO | TFL_FIRECHECK_REFIRE | TFL_FIRECHECK_WORLD;
1048
1049 // Range stuff.
1050     if not (self.target_range)
1051         self.target_range = self.shot_speed * 0.5;
1052     self.target_range = bound(0,self.target_range,MAX_SHOT_DISTANCE);
1053
1054     if not (self.target_range_min)
1055         self.target_range_min = self.shot_radius * 2;
1056     self.target_range_min = bound(0,self.target_range_min,MAX_SHOT_DISTANCE);
1057
1058     //if (!self.target_range_fire)
1059     //    self.target_range_fire = self.target_range * 0.8;
1060     //self.target_range_fire = bound(0,self.target_range_fire,MAX_SHOT_DISTANCE);
1061
1062     if not (self.target_range_optimal)
1063         self.target_range_optimal = self.target_range * 0.5;
1064     self.target_range_optimal = bound(0,self.target_range_optimal,MAX_SHOT_DISTANCE);
1065
1066
1067 // Aim stuff.
1068     if not (self.aim_maxrot)
1069         self.aim_maxrot = 90;
1070     self.aim_maxrot = bound(0,self.aim_maxrot,360);
1071
1072     if not (self.aim_maxpitch)
1073         self.aim_maxpitch = 20;
1074     self.aim_maxpitch = bound(0,self.aim_maxpitch,90);
1075
1076     if not (self.aim_speed)
1077         self.aim_speed = 36;
1078     self.aim_speed  = bound(0.1,self.aim_speed, 1000);
1079
1080     if not (self.aim_firetolerance_dist)
1081         self.aim_firetolerance_dist  = 5 + (self.shot_radius * 2);
1082     self.aim_firetolerance_dist = bound(0.1,self.aim_firetolerance_dist,MAX_SHOT_DISTANCE);
1083
1084     if not (self.aim_flags)
1085     {
1086         self.aim_flags = TFL_AIM_LEAD | TFL_AIM_SHOTTIMECOMPENSATE;
1087         if(self.turrcaps_flags & TFL_TURRCAPS_RADIUSDMG)
1088             self.aim_flags |= TFL_AIM_GROUND2;
1089     }
1090
1091     // Sill the most tested (and aim-effective)
1092     if not (self.track_type)
1093         self.track_type = TFL_TRACKTYPE_STEPMOTOR;
1094
1095     if (self.track_type != TFL_TRACKTYPE_STEPMOTOR)
1096     {
1097         // Fluid / Ineria mode. Looks mutch nicer, bit experimental &
1098         // Can inmapt aim preformance alot.
1099         // needs a bit diffrent aimspeed
1100
1101         if not (self.aim_speed)
1102             self.aim_speed = 180;
1103         self.aim_speed = bound(0.1,self.aim_speed, 1000);
1104
1105         if not (self.track_accel_pitch)
1106             self.track_accel_pitch = 0.5;
1107
1108         if not (self.track_accel_rot)
1109             self.track_accel_rot   = 0.5;
1110
1111         if not (self.track_blendrate)
1112             self.track_blendrate   = 0.35;
1113     }
1114
1115     if (!self.track_flags)
1116         self.track_flags = TFL_TRACK_PITCH | TFL_TRACK_ROT;
1117
1118
1119 // Target selection stuff.
1120     if not (self.target_select_rangebias)
1121         self.target_select_rangebias = 1;
1122     self.target_select_rangebias = bound(-10,self.target_select_rangebias,10);
1123
1124     if not (self.target_select_samebias)
1125         self.target_select_samebias = 1;
1126     self.target_select_samebias = bound(-10,self.target_select_samebias,10);
1127
1128     if not (self.target_select_anglebias)
1129         self.target_select_anglebias = 1;
1130     self.target_select_anglebias = bound(-10,self.target_select_anglebias,10);
1131
1132     if not (self.target_select_missilebias)
1133         self.target_select_missilebias = -10;
1134
1135     self.target_select_missilebias = bound(-10,self.target_select_missilebias,10);
1136     self.target_select_playerbias = bound(-10,self.target_select_playerbias,10);
1137
1138     if not (self.target_select_flags)
1139     {
1140             self.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_TEAMCHECK
1141                                      | TFL_TARGETSELECT_RANGELIMTS | TFL_TARGETSELECT_ANGLELIMITS;
1142
1143         if (self.turrcaps_flags & TFL_TURRCAPS_MISSILEKILL)
1144             self.target_select_flags |= TFL_TARGETSELECT_MISSILES;
1145
1146         if (self.turrcaps_flags & TFL_TURRCAPS_PLAYERKILL)
1147             self.target_select_flags |= TFL_TARGETSELECT_PLAYERS;
1148         //else
1149         //    self.target_select_flags = TFL_TARGETSELECT_NO;
1150     }
1151
1152     self.target_validate_flags = self.target_select_flags;
1153
1154
1155 // Ammo stuff
1156     if not (self.ammo_max)
1157         self.ammo_max = self.shot_dmg * 10;
1158     self.ammo_max = max(self.shot_dmg,self.ammo_max);
1159
1160     if not (self.ammo)
1161         self.ammo = self.shot_dmg * 5;
1162     self.ammo = bound(0,self.ammo,self.ammo_max);
1163
1164     if not (self.ammo_recharge)
1165         self.ammo_recharge = self.shot_dmg * 0.5;
1166     self.ammo_recharge = max(0,self.ammo_recharge);
1167
1168     // Convert the recharge from X per sec to X per ticrate
1169     self.ammo_recharge = self.ammo_recharge * self.ticrate;
1170
1171     if not (self.ammo_flags)
1172         self.ammo_flags = TFL_AMMO_ENERGY | TFL_AMMO_RECHARGE;
1173
1174 // Damage stuff
1175     if(self.spawnflags & TSL_NO_RESPAWN)
1176         if not (self.damage_flags & TFL_DMG_DEATH_NORESPAWN)
1177             self.damage_flags |= TFL_DMG_DEATH_NORESPAWN;
1178
1179 // Offsets & origins
1180     if (!self.tur_shotorg)   self.tur_shotorg = '50 0 50';
1181
1182 // End of default & sanety checks, start building the turret.
1183
1184 // Spawn extra bits
1185     self.tur_head         = spawn();
1186     self.tur_head.netname = self.tur_head.classname = "turret_head";
1187     self.tur_head.team    = self.team;
1188     self.tur_head.owner   = self;
1189
1190     setmodel(self,base);
1191     setmodel(self.tur_head,head);
1192
1193     setsize(self,'-32 -32 0','32 32 64');
1194     setsize(self.tur_head,'0 0 0','0 0 0');
1195
1196     setorigin(self.tur_head,'0 0 0');
1197     setattachment(self.tur_head, self, "tag_head");
1198
1199     if (!self.health)
1200         self.health = 150;
1201
1202     self.tur_health          = self.health;
1203     self.solid               = SOLID_BBOX;
1204     self.tur_head.solid      = SOLID_NOT;
1205     self.takedamage          = DAMAGE_AIM;
1206     self.tur_head.takedamage = DAMAGE_NO;
1207     self.movetype            = MOVETYPE_NOCLIP;
1208     self.tur_head.movetype   = MOVETYPE_NOCLIP;
1209
1210     // Defend mode?
1211     if not (self.tur_defend)
1212     if (self.target != "")
1213     {
1214         self.tur_defend = find(world, targetname, self.target);
1215         if (self.tur_defend == world)
1216         {
1217             self.target = "";
1218             dprint("Turret has invalid defendpoint!\n");
1219         }
1220     }
1221
1222     // In target defend mode, aim on the spot to defend when idle.
1223     if (self.tur_defend)
1224         self.idle_aim  = self.tur_head.angles + angleofs(self.tur_head,self.tur_defend);
1225     else
1226         self.idle_aim  = '0 0 0';
1227
1228     // Team color
1229     if (self.team == COLOR_TEAM1) self.colormod = '1.4 0.8 0.8';
1230     if (self.team == COLOR_TEAM2) self.colormod = '0.8 0.8 1.4';
1231
1232     // Attach stdprocs. override when and what needed
1233     if (self.turrcaps_flags & TFL_TURRCAPS_SUPPORT)
1234     {
1235         self.turret_score_target    = turret_stdproc_targetscore_support;
1236         self.turret_firecheckfunc   = turret_stdproc_firecheck;
1237         self.turret_firefunc        = turret_stdproc_fire;
1238         self.event_damage           = turret_stdproc_damage;
1239     }
1240     else
1241     {
1242         self.turret_score_target    = turret_stdproc_targetscore_generic;
1243         self.turret_firecheckfunc   = turret_stdproc_firecheck;
1244         self.turret_firefunc        = turret_stdproc_fire;
1245         self.event_damage           = turret_stdproc_damage;
1246     }
1247
1248     self.use = turret_stdproc_use;
1249     self.bot_attack = TRUE;
1250
1251     // Initiate the main AI loop
1252     if(csqc_shared)
1253         self.think     = turret_link;
1254     else
1255         self.think     = turret_think;
1256
1257     ++turret_count;
1258     self.nextthink = time + 1;
1259     self.nextthink +=  turret_count * sys_frametime;
1260
1261     self.tur_head.team = self.team;
1262     self.view_ofs = '0 0 0';
1263
1264 #ifdef TURRET_DEBUG
1265     self.tur_dbg_start = self.nextthink;
1266     while (vlen(self.tur_dbg_rvec) < 2)
1267         self.tur_dbg_rvec  = randomvec() * 4;
1268
1269     self.tur_dbg_rvec_x = fabs(self.tur_dbg_rvec_x);
1270     self.tur_dbg_rvec_y = fabs(self.tur_dbg_rvec_y);
1271     self.tur_dbg_rvec_z = fabs(self.tur_dbg_rvec_z);
1272 #endif
1273
1274     // Its all good.
1275     self.turrcaps_flags |= TFL_TURRCAPS_ISTURRET;
1276
1277     self.classname = "turret_main";
1278
1279     self.tur_active = 1;
1280
1281     // In ONS mode, and linked to a ONS ent. need to call the use to set team.
1282     if (g_onslaught && ee)
1283     {
1284         activator = ee;
1285         self.use();
1286     }
1287
1288     return 1;
1289 }
1290
1291