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