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