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