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