3 #include "../../server/autocvars.qh"
6 vector turret_aim_generic()
10 float distance, impact_time = 0, i, mintime;
12 turret_tag_fire_update();
14 if(self.aim_flags & TFL_AIM_SIMPLE)
15 return real_origin(self.enemy);
17 mintime = max(self.attack_finished_single - time,0) + sys_frametime;
20 pre_pos = real_origin(self.enemy);
23 if (self.aim_flags & TFL_AIM_LEAD)
25 if (self.aim_flags & TFL_AIM_SHOTTIMECOMPENSATE) // Need to conpensate for shot traveltime
29 distance = vlen(prep - self.tur_shotorg);
30 impact_time = distance / self.shot_speed;
32 prep = pre_pos + (self.enemy.velocity * (impact_time + mintime));
34 if(self.aim_flags & TFL_AIM_ZPREDICT)
35 if(!(self.enemy.flags & FL_ONGROUND))
36 if(self.enemy.movetype == MOVETYPE_WALK || self.enemy.movetype == MOVETYPE_TOSS || self.enemy.movetype == MOVETYPE_BOUNCE)
40 vz = self.enemy.velocity_z;
41 for(i = 0; i < impact_time; i += sys_frametime)
43 vz = vz - (autocvar_sv_gravity * sys_frametime);
44 prep_z = prep_z + vz * sys_frametime;
50 pre_pos = pre_pos + self.enemy.velocity * mintime;
53 if(self.aim_flags & TFL_AIM_SPLASH)
55 //tracebox(pre_pos + '0 0 32',self.enemy.mins,self.enemy.maxs,pre_pos -'0 0 64',MOVE_WORLDONLY,self.enemy);
56 traceline(pre_pos + '0 0 32',pre_pos -'0 0 64',MOVE_WORLDONLY,self.enemy);
57 if(trace_fraction != 1.0)
58 pre_pos = trace_endpos;
64 float turret_targetscore_support(entity _turret,entity _target)
66 float score; // Total score
67 float s_score = 0, d_score;
69 if (_turret.enemy == _target) s_score = 1;
71 d_score = min(_turret.target_range_optimal,tvt_dist) / max(_turret.target_range_optimal,tvt_dist);
73 score = (d_score * _turret.target_select_rangebias) +
74 (s_score * _turret.target_select_samebias);
80 * Generic bias aware score system.
82 float turret_targetscore_generic(entity _turret, entity _target)
84 float d_dist; // Defendmode Distance
85 float score; // Total score
86 float d_score; // Distance score
87 float a_score; // Angular score
88 float m_score = 0; // missile score
89 float p_score = 0; // player score
90 float ikr; // ideal kill range
92 if (_turret.tur_defend)
94 d_dist = vlen(real_origin(_target) - _turret.tur_defend.origin);
95 ikr = vlen(_turret.origin - _turret.tur_defend.origin);
96 d_score = 1 - d_dist / _turret.target_range;
100 // Make a normlized value base on the targets distance from our optimal killzone
101 ikr = _turret.target_range_optimal;
102 d_score = min(ikr, tvt_dist) / max(ikr, tvt_dist);
105 a_score = 1 - tvt_thadf / _turret.aim_maxrotate;
107 if ((_turret.target_select_missilebias > 0) && (_target.flags & FL_PROJECTILE))
110 if ((_turret.target_select_playerbias > 0) && IS_CLIENT(_target))
113 d_score = max(d_score, 0);
114 a_score = max(a_score, 0);
115 m_score = max(m_score, 0);
116 p_score = max(p_score, 0);
118 score = (d_score * _turret.target_select_rangebias) +
119 (a_score * _turret.target_select_anglebias) +
120 (m_score * _turret.target_select_missilebias) +
121 (p_score * _turret.target_select_playerbias);
123 if(_turret.target_range < vlen(_turret.tur_shotorg - real_origin(_target)))
130 string sd,sa,sm,sp,ss;
131 string sdt,sat,smt,spt;
134 d_score *= _turret.target_select_rangebias;
137 //sv = ftos(v_score);
138 //v_score *= _turret.target_select_samebias;
139 //svt = ftos(v_score);
142 a_score *= _turret.target_select_anglebias;
146 m_score *= _turret.target_select_missilebias;
150 p_score *= _turret.target_select_playerbias;
155 bprint("^3Target scores^7 \[ ",_turret.netname, " \] ^3for^7 \[ ", _target.netname," \]\n");
156 bprint("^5Range:\[ ",sd, " \]^2+bias:\[ ",sdt," \]\n");
157 bprint("^5Angle:\[ ",sa, " \]^2+bias:\[ ",sat," \]\n");
158 bprint("^5Missile:\[ ",sm," \]^2+bias:\[ ",smt," \]\n");
159 bprint("^5Player:\[ ",sp, " \]^2+bias:\[ ",spt," \]\n");
160 bprint("^3Total (w/bias):\[^1",ss,"\]\n");
167 // Generic damage handling
170 self.effects |= EF_NODRAW;
171 self.nextthink = time + self.respawntime - 0.2;
172 self.think = turret_respawn;
177 self.deadflag = DEAD_DEAD;
178 self.tur_head.deadflag = self.deadflag;
180 // Unsolidify and hide real parts
181 self.solid = SOLID_NOT;
182 self.tur_head.solid = self.solid;
184 self.event_damage = func_null;
185 self.takedamage = DAMAGE_NO;
190 //RadiusDamage (self,self, min(self.ammo,50),min(self.ammo,50) * 0.25,250,world,min(self.ammo,50)*5,DEATH_TURRET,world);
192 if(self.damage_flags & TFL_DMG_DEATH_NORESPAWN)
194 TUR_ACTION(self.turretid, TR_DEATH);
196 remove(self.tur_head);
202 self.SendFlags |= TNSF_STATUS;
203 self.nextthink = time + 0.2;
204 self.think = turret_hide;
206 TUR_ACTION(self.turretid, TR_DEATH);
210 void turret_damage (entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector vforce)
213 if(self.deadflag == DEAD_DEAD)
216 // Inactive turrets take no damage. (hm..)
220 if(SAME_TEAM(self, attacker))
222 if(autocvar_g_friendlyfire)
223 damage = damage * autocvar_g_friendlyfire;
228 self.health -= damage;
230 // thorw head slightly off aim when hit?
231 if (self.damage_flags & TFL_DMG_HEADSHAKE)
233 self.tur_head.angles_x = self.tur_head.angles_x + (-0.5 + random()) * damage;
234 self.tur_head.angles_y = self.tur_head.angles_y + (-0.5 + random()) * damage;
236 self.SendFlags |= TNSF_ANG;
239 if (self.turret_flags & TUR_FLAG_MOVE)
240 self.velocity = self.velocity + vforce;
242 if (self.health <= 0)
244 self.event_damage = func_null;
245 self.tur_head.event_damage = func_null;
246 self.takedamage = DAMAGE_NO;
247 self.nextthink = time;
248 self.think = turret_die;
251 self.SendFlags |= TNSF_STATUS;
255 void turret_respawn()
257 // Make sure all parts belong to the same team since
258 // this function doubles as "teamchange" function.
259 self.tur_head.team = self.team;
260 self.effects &= ~EF_NODRAW;
261 self.deadflag = DEAD_NO;
262 self.effects = EF_LOWPRECISION;
263 self.solid = SOLID_BBOX;
264 self.takedamage = DAMAGE_AIM;
265 self.event_damage = turret_damage;
266 self.avelocity = '0 0 0';
267 self.tur_head.avelocity = self.avelocity;
268 self.tur_head.angles = self.idle_aim;
269 self.health = self.max_health;
271 self.volly_counter = self.shot_volly;
272 self.ammo = self.ammo_max;
274 self.nextthink = time + self.ticrate;
275 self.think = turret_think;
277 self.SendFlags = TNSF_FULL_UPDATE;
279 TUR_ACTION(self.turretid, TR_SETUP);
284 #define cvar_base "g_turrets_unit_"
286 void turrets_setframe(float _frame, float client_only)
288 if((client_only ? self.clientframe : self.frame ) != _frame)
290 self.SendFlags |= TNSF_ANIM;
291 self.anim_start_time = time;
295 self.clientframe = _frame;
301 float turret_send(entity to, float sf)
304 WriteByte(MSG_ENTITY, ENT_CLIENT_TURRET);
305 WriteByte(MSG_ENTITY, sf);
308 WriteByte(MSG_ENTITY, self.turretid);
310 WriteCoord(MSG_ENTITY, self.origin_x);
311 WriteCoord(MSG_ENTITY, self.origin_y);
312 WriteCoord(MSG_ENTITY, self.origin_z);
314 WriteAngle(MSG_ENTITY, self.angles_x);
315 WriteAngle(MSG_ENTITY, self.angles_y);
320 WriteShort(MSG_ENTITY, rint(self.tur_head.angles_x));
321 WriteShort(MSG_ENTITY, rint(self.tur_head.angles_y));
326 WriteShort(MSG_ENTITY, rint(self.tur_head.avelocity_x));
327 WriteShort(MSG_ENTITY, rint(self.tur_head.avelocity_y));
332 WriteShort(MSG_ENTITY, rint(self.origin_x));
333 WriteShort(MSG_ENTITY, rint(self.origin_y));
334 WriteShort(MSG_ENTITY, rint(self.origin_z));
336 WriteShort(MSG_ENTITY, rint(self.velocity_x));
337 WriteShort(MSG_ENTITY, rint(self.velocity_y));
338 WriteShort(MSG_ENTITY, rint(self.velocity_z));
340 WriteShort(MSG_ENTITY, rint(self.angles_y));
345 WriteCoord(MSG_ENTITY, self.anim_start_time);
346 WriteByte(MSG_ENTITY, self.frame);
351 WriteByte(MSG_ENTITY, self.team);
354 WriteByte(MSG_ENTITY, 0);
356 WriteByte(MSG_ENTITY, ceil((self.health / self.max_health) * 255));
362 void load_unit_settings(entity ent, string unitname, float is_reload)
369 if(!ent.turret_scale_damage) ent.turret_scale_damage = 1;
370 if(!ent.turret_scale_range) ent.turret_scale_range = 1;
371 if(!ent.turret_scale_refire) ent.turret_scale_refire = 1;
372 if(!ent.turret_scale_ammo) ent.turret_scale_ammo = 1;
373 if(!ent.turret_scale_aim) ent.turret_scale_aim = 1;
374 if(!ent.turret_scale_health) ent.turret_scale_health = 1;
375 if(!ent.turret_scale_respawn) ent.turret_scale_respawn = 1;
377 sbase = strcat(cvar_base,unitname);
381 ent.tur_head.avelocity = '0 0 0';
383 ent.tur_head.angles = '0 0 0';
386 ent.health = cvar(strcat(sbase,"_health")) * ent.turret_scale_health;
387 ent.respawntime = cvar(strcat(sbase,"_respawntime")) * ent.turret_scale_respawn;
389 ent.shot_dmg = cvar(strcat(sbase,"_shot_dmg")) * ent.turret_scale_damage;
390 ent.shot_refire = cvar(strcat(sbase,"_shot_refire")) * ent.turret_scale_refire;
391 ent.shot_radius = cvar(strcat(sbase,"_shot_radius")) * ent.turret_scale_damage;
392 ent.shot_speed = cvar(strcat(sbase,"_shot_speed"));
393 ent.shot_spread = cvar(strcat(sbase,"_shot_spread"));
394 ent.shot_force = cvar(strcat(sbase,"_shot_force")) * ent.turret_scale_damage;
395 ent.shot_volly = cvar(strcat(sbase,"_shot_volly"));
396 ent.shot_volly_refire = cvar(strcat(sbase,"_shot_volly_refire")) * ent.turret_scale_refire;
398 ent.target_range = cvar(strcat(sbase,"_target_range")) * ent.turret_scale_range;
399 ent.target_range_min = cvar(strcat(sbase,"_target_range_min")) * ent.turret_scale_range;
400 ent.target_range_optimal = cvar(strcat(sbase,"_target_range_optimal")) * ent.turret_scale_range;
401 //ent.target_range_fire = cvar(strcat(sbase,"_target_range_fire")) * ent.turret_scale_range;
403 ent.target_select_rangebias = cvar(strcat(sbase,"_target_select_rangebias"));
404 ent.target_select_samebias = cvar(strcat(sbase,"_target_select_samebias"));
405 ent.target_select_anglebias = cvar(strcat(sbase,"_target_select_anglebias"));
406 ent.target_select_playerbias = cvar(strcat(sbase,"_target_select_playerbias"));
407 //ent.target_select_fov = cvar(cvar_gets(sbase,"_target_select_fov"));
409 ent.ammo_max = cvar(strcat(sbase,"_ammo_max")) * ent.turret_scale_ammo;
410 ent.ammo_recharge = cvar(strcat(sbase,"_ammo_recharge")) * ent.turret_scale_ammo;
412 ent.aim_firetolerance_dist = cvar(strcat(sbase,"_aim_firetolerance_dist"));
413 ent.aim_speed = cvar(strcat(sbase,"_aim_speed")) * ent.turret_scale_aim;
414 ent.aim_maxrotate = cvar(strcat(sbase,"_aim_maxrot"));
415 ent.aim_maxpitch = cvar(strcat(sbase,"_aim_maxpitch"));
417 ent.track_type = cvar(strcat(sbase,"_track_type"));
418 ent.track_accel_pitch = cvar(strcat(sbase,"_track_accel_pitch"));
419 ent.track_accel_rotate = cvar(strcat(sbase,"_track_accel_rot"));
420 ent.track_blendrate = cvar(strcat(sbase,"_track_blendrate"));
423 TUR_ACTION(self.turretid, TR_SETUP);
426 void turret_projectile_explode()
429 self.takedamage = DAMAGE_NO;
430 self.event_damage = func_null;
433 d = RadiusDamage (self, self.owner, self.owner.shot_dmg, 0, self.owner.shot_radius, self, world, self.owner.shot_force, self.totalfrags, world);
434 self.owner.tur_debug_dmg_t_h = self.owner.tur_debug_dmg_t_h + d;
435 self.owner.tur_debug_dmg_t_f = self.owner.tur_debug_dmg_t_f + self.owner.shot_dmg;
437 RadiusDamage (self, self.realowner, self.owner.shot_dmg, 0, self.owner.shot_radius, self, world, self.owner.shot_force, self.totalfrags, world);
442 void turret_projectile_touch()
445 turret_projectile_explode();
448 void turret_projectile_damage(entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector vforce)
450 self.velocity += vforce;
451 self.health -= damage;
452 //self.realowner = attacker; // Dont change realowner, it does not make much sense for turrets
454 W_PrepareExplosionByDamage(self.owner, turret_projectile_explode);
457 entity turret_projectile(string _snd, float _size, float _health, float _death, float _proj_type, float _cull, float _cli_anim)
461 _sound (self, CH_WEAPON_A, _snd, VOL_BASE, ATTEN_NORM);
463 setorigin(proj, self.tur_shotorg);
464 setsize(proj, '-0.5 -0.5 -0.5' * _size, '0.5 0.5 0.5' * _size);
466 proj.realowner = self;
467 proj.bot_dodge = true;
468 proj.bot_dodgerating = self.shot_dmg;
469 proj.think = turret_projectile_explode;
470 proj.touch = turret_projectile_touch;
471 proj.nextthink = time + 9;
472 proj.movetype = MOVETYPE_FLYMISSILE;
473 proj.velocity = normalize(self.tur_shotdir_updated + randomvec() * self.shot_spread) * self.shot_speed;
474 proj.flags = FL_PROJECTILE;
475 proj.enemy = self.enemy;
476 proj.totalfrags = _death;
477 PROJECTILE_MAKETRIGGER(proj);
480 proj.health = _health;
481 proj.takedamage = DAMAGE_YES;
482 proj.event_damage = turret_projectile_damage;
485 proj.flags |= FL_NOTARGET;
487 CSQCProjectile(proj, _cli_anim, _proj_type, _cull);
493 ** updates enemy distances, predicted impact point/time
494 ** and updated aim<->predict impact distance.
496 void turret_do_updates(entity t_turret)
502 enemy_pos = real_origin(self.enemy);
504 turret_tag_fire_update();
506 self.tur_shotdir_updated = v_forward;
507 self.tur_dist_enemy = vlen(self.tur_shotorg - enemy_pos);
508 self.tur_dist_aimpos = vlen(self.tur_shotorg - self.tur_aimpos);
510 /*if((self.firecheck_flags & TFL_FIRECHECK_VERIFIED) && (self.enemy))
512 oldpos = self.enemy.origin;
513 setorigin(self.enemy, self.tur_aimpos);
514 tracebox(self.tur_shotorg, '-1 -1 -1', '1 1 1', self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos), MOVE_NORMAL,self);
515 setorigin(self.enemy, oldpos);
517 if(trace_ent == self.enemy)
518 self.tur_dist_impact_to_aimpos = 0;
520 self.tur_dist_impact_to_aimpos = vlen(trace_endpos - self.tur_aimpos);
523 tracebox(self.tur_shotorg, '-1 -1 -1','1 1 1', self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos), MOVE_NORMAL,self);
525 self.tur_dist_impact_to_aimpos = vlen(trace_endpos - self.tur_aimpos) - (vlen(self.enemy.maxs - self.enemy.mins) * 0.5);
526 self.tur_impactent = trace_ent;
527 self.tur_impacttime = vlen(self.tur_shotorg - trace_endpos) / self.shot_speed;
533 ** Handles head rotation according to
534 ** the units .track_type and .track_flags
536 .float turret_framecounter;
539 vector target_angle; // This is where we want to aim
540 vector move_angle; // This is where we can aim
543 v1 = self.tur_head.angles;
544 v2 = self.tur_head.avelocity;
546 if (self.track_flags == TFL_TRACK_NO)
550 target_angle = self.idle_aim - ('1 0 0' * self.aim_maxpitch);
551 else if (self.enemy == world)
554 target_angle = self.idle_aim + self.angles;
556 target_angle = vectoangles(normalize(self.tur_aimpos - self.tur_shotorg));
560 target_angle = vectoangles(normalize(self.tur_aimpos - self.tur_shotorg));
563 self.tur_head.angles_x = anglemods(self.tur_head.angles_x);
564 self.tur_head.angles_y = anglemods(self.tur_head.angles_y);
566 // Find the diffrence between where we currently aim and where we want to aim
567 //move_angle = target_angle - (self.angles + self.tur_head.angles);
568 //move_angle = shortangle_vxy(move_angle,(self.angles + self.tur_head.angles));
570 move_angle = AnglesTransform_ToAngles(AnglesTransform_LeftDivide(AnglesTransform_FromAngles(self.angles), AnglesTransform_FromAngles(target_angle))) - self.tur_head.angles;
571 move_angle = shortangle_vxy(move_angle, self.tur_head.angles);
573 switch(self.track_type)
575 case TFL_TRACKTYPE_STEPMOTOR:
576 f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
577 if (self.track_flags & TFL_TRACK_PITCH)
579 self.tur_head.angles_x += bound(-f_tmp,move_angle_x, f_tmp);
580 if(self.tur_head.angles_x > self.aim_maxpitch)
581 self.tur_head.angles_x = self.aim_maxpitch;
583 if(self.tur_head.angles_x < -self.aim_maxpitch)
584 self.tur_head.angles_x = self.aim_maxpitch;
587 if (self.track_flags & TFL_TRACK_ROTATE)
589 self.tur_head.angles_y += bound(-f_tmp, move_angle_y, f_tmp);
590 if(self.tur_head.angles_y > self.aim_maxrotate)
591 self.tur_head.angles_y = self.aim_maxrotate;
593 if(self.tur_head.angles_y < -self.aim_maxrotate)
594 self.tur_head.angles_y = self.aim_maxrotate;
598 self.SendFlags |= TNSF_ANG;
602 case TFL_TRACKTYPE_FLUIDINERTIA:
603 f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
604 move_angle_x = bound(-self.aim_speed, move_angle_x * self.track_accel_pitch * f_tmp, self.aim_speed);
605 move_angle_y = bound(-self.aim_speed, move_angle_y * self.track_accel_rotate * f_tmp, self.aim_speed);
606 move_angle = (self.tur_head.avelocity * self.track_blendrate) + (move_angle * (1 - self.track_blendrate));
609 case TFL_TRACKTYPE_FLUIDPRECISE:
611 move_angle_y = bound(-self.aim_speed, move_angle_y, self.aim_speed);
612 move_angle_x = bound(-self.aim_speed, move_angle_x, self.aim_speed);
618 if (self.track_flags & TFL_TRACK_PITCH)
620 self.tur_head.avelocity_x = move_angle_x;
621 if((self.tur_head.angles_x + self.tur_head.avelocity_x * self.ticrate) > self.aim_maxpitch)
623 self.tur_head.avelocity_x = 0;
624 self.tur_head.angles_x = self.aim_maxpitch;
626 self.SendFlags |= TNSF_ANG;
629 if((self.tur_head.angles_x + self.tur_head.avelocity_x * self.ticrate) < -self.aim_maxpitch)
631 self.tur_head.avelocity_x = 0;
632 self.tur_head.angles_x = -self.aim_maxpitch;
634 self.SendFlags |= TNSF_ANG;
639 if (self.track_flags & TFL_TRACK_ROTATE)
641 self.tur_head.avelocity_y = move_angle_y;
643 if((self.tur_head.angles_y + self.tur_head.avelocity_y * self.ticrate) > self.aim_maxrotate)
645 self.tur_head.avelocity_y = 0;
646 self.tur_head.angles_y = self.aim_maxrotate;
648 self.SendFlags |= TNSF_ANG;
651 if((self.tur_head.angles_y + self.tur_head.avelocity_y * self.ticrate) < -self.aim_maxrotate)
653 self.tur_head.avelocity_y = 0;
654 self.tur_head.angles_y = -self.aim_maxrotate;
656 self.SendFlags |= TNSF_ANG;
660 self.SendFlags |= TNSF_AVEL;
662 // Force a angle update every 10'th frame
663 self.turret_framecounter += 1;
664 if(self.turret_framecounter >= 10)
666 self.SendFlags |= TNSF_ANG;
667 self.turret_framecounter = 0;
672 + TFL_TARGETSELECT_NO
673 + TFL_TARGETSELECT_LOS
674 + TFL_TARGETSELECT_PLAYERS
675 + TFL_TARGETSELECT_MISSILES
676 - TFL_TARGETSELECT_TRIGGERTARGET
677 + TFL_TARGETSELECT_ANGLELIMITS
678 + TFL_TARGETSELECT_RANGELIMITS
679 + TFL_TARGETSELECT_TEAMCHECK
680 - TFL_TARGETSELECT_NOBUILTIN
681 + TFL_TARGETSELECT_OWNTEAM
685 ** Evaluate a entity for target valitity based on validate_flags
686 ** NOTE: the caller must check takedamage before calling this, to inline this check.
688 float turret_validate_target(entity e_turret, entity e_target, float validate_flags)
692 //if(!validate_flags & TFL_TARGETSELECT_NOBUILTIN)
698 if(e_target.owner == e_turret)
701 if(!checkpvs(e_target.origin, e_turret))
704 if(e_target.alpha <= 0.3)
708 if (substring(e_target.classname, 0, 10) == "onslaught_") // don't attack onslaught targets, that's the player's job!
711 if (validate_flags & TFL_TARGETSELECT_NO)
714 // If only this was used more..
715 if (e_target.flags & FL_NOTARGET)
719 if(IS_VEHICLE(e_target))
721 if (e_target.vehicle_health <= 0)
724 else if (e_target.health <= 0)
726 else if(e_target.frozen > 0)
730 if (IS_CLIENT(e_target))
732 if(!(validate_flags & TFL_TARGETSELECT_PLAYERS))
735 if (e_target.deadflag != DEAD_NO)
740 if(validate_flags & TFL_TARGETSELECT_NOTURRETS)
741 if(e_target.owner.tur_head == e_target)
742 if(e_target.team != e_turret.team) // Dont break support units.
746 if (e_target.flags & FL_PROJECTILE)
747 if(!(validate_flags & TFL_TARGETSELECT_MISSILES))
750 if (validate_flags & TFL_TARGETSELECT_MISSILESONLY)
751 if(!(e_target.flags & FL_PROJECTILE))
755 if (validate_flags & TFL_TARGETSELECT_TEAMCHECK)
757 if (validate_flags & TFL_TARGETSELECT_OWNTEAM)
759 if (e_target.team != e_turret.team)
762 if (e_turret.team != e_target.owner.team)
767 if (e_target.team == e_turret.team)
770 if (e_turret.team == e_target.owner.team)
776 tvt_dist = vlen(e_turret.origin - real_origin(e_target));
777 if (validate_flags & TFL_TARGETSELECT_RANGELIMITS)
779 if (tvt_dist < e_turret.target_range_min)
782 if (tvt_dist > e_turret.target_range)
786 // Can we even aim this thing?
787 tvt_thadv = angleofs3(e_turret.tur_head.origin, e_turret.angles + e_turret.tur_head.angles, e_target);
788 tvt_tadv = shortangle_vxy(angleofs(e_turret, e_target), e_turret.angles);
789 tvt_thadf = vlen(tvt_thadv);
790 tvt_tadf = vlen(tvt_tadv);
793 if(validate_flags & TFL_TARGETSELECT_FOV)
795 if(e_turret.target_select_fov < tvt_thadf)
800 if (validate_flags & TFL_TARGETSELECT_ANGLELIMITS)
802 if (fabs(tvt_tadv_x) > e_turret.aim_maxpitch)
805 if (fabs(tvt_tadv_y) > e_turret.aim_maxrotate)
810 if (validate_flags & TFL_TARGETSELECT_LOS)
812 v_tmp = real_origin(e_target) + ((e_target.mins + e_target.maxs) * 0.5);
814 traceline(e_turret.origin + '0 0 16', v_tmp, 0, e_turret);
816 if (e_turret.aim_firetolerance_dist < vlen(v_tmp - trace_endpos))
820 if (e_target.classname == "grapplinghook")
824 if (e_target.classname == "func_button")
828 #ifdef TURRET_DEBUG_TARGETSELECT
829 LOG_TRACE("Target:",e_target.netname," is a valid target for ",e_turret.netname,"\n");
835 entity turret_select_target()
837 entity e; // target looper entity
838 float score; // target looper entity score
839 entity e_enemy; // currently best scoreing target
840 float m_score; // currently best scoreing target's score
843 if(self.enemy && self.enemy.takedamage && turret_validate_target(self,self.enemy,self.target_validate_flags) > 0)
845 e_enemy = self.enemy;
846 m_score = self.turret_score_target(self,e_enemy) * self.target_select_samebias;
849 e_enemy = self.enemy = world;
851 e = findradius(self.origin, self.target_range);
853 // Nothing to aim at?
861 float f = turret_validate_target(self, e, self.target_select_flags);
862 //dprint("F is: ", ftos(f), "\n");
865 score = self.turret_score_target(self,e);
866 if ((score > m_score) && (score > 0))
885 + TFL_FIRECHECK_WORLD
887 + TFL_FIRECHECK_DISTANCES
889 + TFL_FIRECHECK_AIMDIST
890 + TFL_FIRECHECK_REALDIST
891 - TFL_FIRECHECK_ANGLEDIST
892 - TFL_FIRECHECK_TEAMCECK
894 + TFL_FIRECHECK_AMMO_OWN
895 + TFL_FIRECHECK_AMMO_OTHER
896 + TFL_FIRECHECK_REFIRE
900 ** Preforms pre-fire checks based on the uints firecheck_flags
902 float turret_firecheck()
904 // This one just dont care =)
905 if (self.firecheck_flags & TFL_FIRECHECK_NO)
908 if (self.enemy == world)
912 if (self.firecheck_flags & TFL_FIRECHECK_REFIRE)
913 if (self.attack_finished_single > time) return 0;
915 // Special case: volly fire turret that has to fire a full volly if a shot was fired.
916 if (self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
917 if (self.volly_counter != self.shot_volly)
918 if(self.ammo >= self.shot_dmg)
921 // Lack of zombies makes shooting dead things unnecessary :P
922 if (self.firecheck_flags & TFL_FIRECHECK_DEAD)
923 if (self.enemy.deadflag != DEAD_NO)
927 if (self.firecheck_flags & TFL_FIRECHECK_AMMO_OWN)
928 if (self.ammo < self.shot_dmg)
931 // Other's ammo? (support-supply units)
932 if (self.firecheck_flags & TFL_FIRECHECK_AMMO_OTHER)
933 if (self.enemy.ammo >= self.enemy.ammo_max)
936 // Target of opertunity?
937 if(turret_validate_target(self, self.tur_impactent, self.target_validate_flags) > 0)
939 self.enemy = self.tur_impactent;
943 if (self.firecheck_flags & TFL_FIRECHECK_DISTANCES)
946 if (self.tur_dist_aimpos < self.target_range_min)
947 if(turret_validate_target(self, self.tur_impactent, self.target_validate_flags) > 0)
948 return 1; // Target of opertunity?
954 if (self.firecheck_flags & TFL_FIRECHECK_AFF)
955 if (self.tur_impactent.team == self.team)
958 // aim<->predicted impact
959 if (self.firecheck_flags & TFL_FIRECHECK_AIMDIST)
960 if (self.tur_dist_impact_to_aimpos > self.aim_firetolerance_dist)
964 if (self.shot_volly > 1)
965 if (self.volly_counter == self.shot_volly)
966 if (self.ammo < (self.shot_dmg * self.shot_volly))
969 /*if(self.firecheck_flags & TFL_FIRECHECK_VERIFIED)
970 if(self.tur_impactent != self.enemy)
976 bool turret_checkfire()
978 bool ret = false; // dummy
979 if(MUTATOR_CALLHOOK(Turret_CheckFire, ret))
982 return self.turret_firecheckfunc();
987 if (autocvar_g_turrets_nofire != 0)
990 if(MUTATOR_CALLHOOK(TurretFire, self))
993 TUR_ACTION(self.turretid, TR_ATTACK);
995 self.attack_finished_single = time + self.shot_refire;
996 self.ammo -= self.shot_dmg;
997 self.volly_counter = self.volly_counter - 1;
999 if (self.volly_counter <= 0)
1001 self.volly_counter = self.shot_volly;
1003 if (self.shoot_flags & TFL_SHOOT_CLEARTARGET)
1006 if (self.shot_volly > 1)
1007 self.attack_finished_single = time + self.shot_volly_refire;
1011 if (self.enemy) paint_target3(self.tur_aimpos, 64, self.tur_debug_rvec, self.tur_impacttime + 0.25);
1019 self.nextthink = time + self.ticrate;
1021 // ONS uses somewhat backwards linking.
1027 e = find(world, targetname,self.target);
1032 if (self.team != self.tur_head.team)
1037 if (self.tur_debug_tmr1 < time)
1039 if (self.enemy) paint_target (self.enemy,128,self.tur_debug_rvec,0.9);
1040 paint_target(self,256,self.tur_debug_rvec,0.9);
1041 self.tur_debug_tmr1 = time + 1;
1046 if (!(self.spawnflags & TSF_NO_AMMO_REGEN))
1047 if (self.ammo < self.ammo_max)
1048 self.ammo = min(self.ammo + self.ammo_recharge, self.ammo_max);
1050 // Inactive turrets needs to run the think loop,
1051 // So they can handle animation and wake up if need be.
1058 // This is typicaly used for zaping every target in range
1059 // turret_fusionreactor uses this to recharge friendlys.
1060 if (self.shoot_flags & TFL_SHOOT_HITALLVALID)
1062 // Do a self.turret_fire for every valid target.
1063 e = findradius(self.origin,self.target_range);
1068 if (turret_validate_target(self,e,self.target_validate_flags))
1072 turret_do_updates(self);
1074 if (turret_checkfire())
1083 else if(self.shoot_flags & TFL_SHOOT_CUSTOM)
1085 // This one is doing something.. oddball. assume its handles what needs to be handled.
1088 if(!(self.aim_flags & TFL_AIM_NO))
1089 self.tur_aimpos = turret_aim_generic();
1092 if(!(self.track_flags & TFL_TRACK_NO))
1095 turret_do_updates(self);
1098 if (turret_checkfire())
1103 // Special case for volly always. if it fired once it must compleate the volly.
1104 if(self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
1105 if(self.volly_counter != self.shot_volly)
1107 // Predict or whatnot
1108 if(!(self.aim_flags & TFL_AIM_NO))
1109 self.tur_aimpos = turret_aim_generic();
1112 if(!(self.track_flags & TFL_TRACK_NO))
1115 turret_do_updates(self);
1118 if (turret_checkfire())
1121 TUR_ACTION(self.turretid, TR_THINK);
1126 // Check if we have a vailid enemy, and try to find one if we dont.
1128 // g_turrets_targetscan_maxdelay forces a target re-scan at least this often
1129 float do_target_scan = 0;
1130 if((self.target_select_time + autocvar_g_turrets_targetscan_maxdelay) < time)
1133 // Old target (if any) invalid?
1134 if(self.target_validate_time < time)
1135 if (turret_validate_target(self, self.enemy, self.target_validate_flags) <= 0)
1138 self.target_validate_time = time + 0.5;
1142 // But never more often then g_turrets_targetscan_mindelay!
1143 if (self.target_select_time + autocvar_g_turrets_targetscan_mindelay > time)
1148 self.enemy = turret_select_target();
1149 self.target_select_time = time;
1152 // No target, just go to idle, do any custom stuff and bail.
1153 if (self.enemy == world)
1156 if(!(self.track_flags & TFL_TRACK_NO))
1159 TUR_ACTION(self.turretid, TR_THINK);
1165 self.lip = time + autocvar_g_turrets_aimidle_delay; // Keep track of the last time we had a target.
1168 if(!(self.aim_flags & TFL_AIM_NO))
1169 self.tur_aimpos = turret_aim_generic();
1172 if(!(self.track_flags & TFL_TRACK_NO))
1175 turret_do_updates(self);
1178 if (turret_checkfire())
1182 TUR_ACTION(self.turretid, TR_THINK);
1186 When .used a turret switch team to activator.team.
1187 If activator is world, the turret go inactive.
1191 LOG_TRACE("Turret ",self.netname, " used by ", activator.classname, "\n");
1193 self.team = activator.team;
1196 self.active = ACTIVE_NOT;
1198 self.active = ACTIVE_ACTIVE;
1204 Net_LinkEntity(self, true, 0, turret_send);
1205 self.think = turret_think;
1206 self.nextthink = time;
1207 self.tur_head.effects = EF_NODRAW;
1210 void turrets_manager_think()
1212 self.nextthink = time + 1;
1215 if (autocvar_g_turrets_reloadcvars == 1)
1222 load_unit_settings(e,e.cvar_basename,1);
1223 TUR_ACTION(self.turretid, TR_THINK);
1228 cvar_set("g_turrets_reloadcvars","0");
1232 float turret_initialize(float tur_id)
1234 if(!autocvar_g_turrets)
1238 entity tur = get_turretinfo(tur_id);
1239 if(tur.turretid == 0)
1240 return false; // invalid turret
1242 if(!self.tur_head) { TUR_ACTION(tur_id, TR_PRECACHE); } // if tur_head exists, we can assume this turret re-spawned
1244 e = find(world, classname, "turret_manager");
1248 e.classname = "turret_manager";
1249 e.think = turrets_manager_think;
1250 e.nextthink = time + 2;
1253 if(!(self.spawnflags & TSF_SUSPENDED))
1254 builtin_droptofloor();
1256 self.cvar_basename = tur.cvar_basename;
1257 load_unit_settings(self, self.cvar_basename, 0);
1259 if(!self.team || !teamplay) { self.team = MAX_SHOT_DISTANCE; }
1260 if(!self.ticrate) { self.ticrate = ((self.turret_flags & TUR_FLAG_SUPPORT) ? 0.2 : 0.1); }
1261 if(!self.health) { self.health = 1000; }
1262 if(!self.shot_refire) { self.shot_refire = 1; }
1263 if(!self.tur_shotorg) { self.tur_shotorg = '50 0 50'; }
1264 if(!self.turret_flags) { self.turret_flags = TUR_FLAG_SPLASH | TUR_FLAG_MEDPROJ | TUR_FLAG_PLAYER; }
1265 if(!self.damage_flags) { self.damage_flags = TFL_DMG_YES | TFL_DMG_RETALIATE | TFL_DMG_AIMSHAKE; }
1266 if(!self.aim_flags) { self.aim_flags = TFL_AIM_LEAD | TFL_AIM_SHOTTIMECOMPENSATE; }
1267 if(!self.track_type) { self.track_type = TFL_TRACKTYPE_STEPMOTOR; }
1268 if(!self.track_flags) { self.track_flags = TFL_TRACK_PITCH | TFL_TRACK_ROTATE; }
1269 if(!self.ammo_flags) { self.ammo_flags = TFL_AMMO_ENERGY | TFL_AMMO_RECHARGE; }
1270 if(!self.target_select_flags) { self.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_TEAMCHECK | TFL_TARGETSELECT_RANGELIMITS | TFL_TARGETSELECT_ANGLELIMITS; }
1271 if(!self.firecheck_flags) { self.firecheck_flags = TFL_FIRECHECK_DEAD | TFL_FIRECHECK_DISTANCES | TFL_FIRECHECK_LOS
1272 | TFL_FIRECHECK_AIMDIST | TFL_FIRECHECK_TEAMCHECK | TFL_FIRECHECK_AMMO_OWN | TFL_FIRECHECK_REFIRE; }
1274 if(self.track_type != TFL_TRACKTYPE_STEPMOTOR)
1276 // Fluid / Ineria mode. Looks mutch nicer.
1277 // Can reduce aim preformance alot, needs a bit diffrent aimspeed
1279 self.aim_speed = bound(0.1, ((!self.aim_speed) ? 180 : self.aim_speed), 1000);
1281 if(!self.track_accel_pitch) { self.track_accel_pitch = 0.5; }
1282 if(!self.track_accel_rotate) { self.track_accel_rotate = 0.5; }
1283 if(!self.track_blendrate) { self.track_blendrate = 0.35; }
1286 self.respawntime = max(-1, ((!self.respawntime) ? 60 : self.respawntime));
1287 self.shot_refire = bound(0.01, ((!self.shot_refire) ? 1 : self.shot_refire), 9999);
1288 self.shot_dmg = max(1, ((!self.shot_dmg) ? self.shot_refire * 50 : self.shot_dmg));
1289 self.shot_radius = max(1, ((!self.shot_radius) ? self.shot_dmg * 0.5 : self.shot_radius));
1290 self.shot_speed = max(1, ((!self.shot_speed) ? 2500 : self.shot_speed));
1291 self.shot_spread = bound(0.0001, ((!self.shot_spread) ? 0.0125 : self.shot_spread), 500);
1292 self.shot_force = bound(0.001, ((!self.shot_force) ? self.shot_dmg * 0.5 + self.shot_radius * 0.5 : self.shot_force), 5000);
1293 self.shot_volly = bound(1, ((!self.shot_volly) ? 1 : self.shot_volly), floor(self.ammo_max / self.shot_dmg));
1294 self.shot_volly_refire = bound(self.shot_refire, ((!self.shot_volly_refire) ? self.shot_refire * self.shot_volly : self.shot_volly_refire), 60);
1295 self.target_range = bound(0, ((!self.target_range) ? self.shot_speed * 0.5 : self.target_range), MAX_SHOT_DISTANCE);
1296 self.target_range_min = bound(0, ((!self.target_range_min) ? self.shot_radius * 2 : self.target_range_min), MAX_SHOT_DISTANCE);
1297 self.target_range_optimal = bound(0, ((!self.target_range_optimal) ? self.target_range * 0.5 : self.target_range_optimal), MAX_SHOT_DISTANCE);
1298 self.aim_maxrotate = bound(0, ((!self.aim_maxrotate) ? 90 : self.aim_maxrotate), 360);
1299 self.aim_maxpitch = bound(0, ((!self.aim_maxpitch) ? 20 : self.aim_maxpitch), 90);
1300 self.aim_speed = bound(0.1, ((!self.aim_speed) ? 36 : self.aim_speed), 1000);
1301 self.aim_firetolerance_dist = bound(0.1, ((!self.aim_firetolerance_dist) ? 5 + (self.shot_radius * 2) : self.aim_firetolerance_dist), MAX_SHOT_DISTANCE);
1302 self.target_select_rangebias = bound(-10, ((!self.target_select_rangebias) ? 1 : self.target_select_rangebias), 10);
1303 self.target_select_samebias = bound(-10, ((!self.target_select_samebias) ? 1 : self.target_select_samebias), 10);
1304 self.target_select_anglebias = bound(-10, ((!self.target_select_anglebias) ? 1 : self.target_select_anglebias), 10);
1305 self.target_select_missilebias = bound(-10, ((!self.target_select_missilebias) ? 1 : self.target_select_missilebias), 10);
1306 self.target_select_playerbias = bound(-10, ((!self.target_select_playerbias) ? 1 : self.target_select_playerbias), 10);
1307 self.ammo_max = max(self.shot_dmg, ((!self.ammo_max) ? self.shot_dmg * 10 : self.ammo_max));
1308 self.ammo_recharge = max(0, ((!self.ammo_recharge) ? self.shot_dmg * 0.5 : self.ammo_recharge));
1310 self.turret_flags = TUR_FLAG_ISTURRET | (tur.spawnflags);
1312 if(self.turret_flags & TUR_FLAG_SPLASH)
1313 self.aim_flags |= TFL_AIM_SPLASH;
1315 if(self.turret_flags & TUR_FLAG_MISSILE)
1316 self.target_select_flags |= TFL_TARGETSELECT_MISSILES;
1318 if(self.turret_flags & TUR_FLAG_PLAYER)
1319 self.target_select_flags |= TFL_TARGETSELECT_PLAYERS;
1321 if(self.spawnflags & TSL_NO_RESPAWN)
1322 self.damage_flags |= TFL_DMG_DEATH_NORESPAWN;
1324 if (self.turret_flags & TUR_FLAG_SUPPORT)
1325 self.turret_score_target = turret_targetscore_support;
1327 self.turret_score_target = turret_targetscore_generic;
1331 _setmodel(self, tur.model);
1332 setsize(self, tur.mins, tur.maxs);
1334 self.turretid = tur_id;
1335 self.classname = "turret_main";
1336 self.active = ACTIVE_ACTIVE;
1337 self.effects = EF_NODRAW;
1338 self.netname = TUR_NAME(tur_id);
1339 self.ticrate = bound(sys_frametime, self.ticrate, 60);
1340 self.max_health = self.health;
1341 self.target_validate_flags = self.target_select_flags;
1342 self.ammo = self.ammo_max;
1343 self.ammo_recharge *= self.ticrate;
1344 self.solid = SOLID_BBOX;
1345 self.takedamage = DAMAGE_AIM;
1346 self.movetype = MOVETYPE_NOCLIP;
1347 self.view_ofs = '0 0 0';
1348 self.turret_firecheckfunc = turret_firecheck;
1349 self.event_damage = turret_damage;
1350 self.use = turret_use;
1351 self.bot_attack = true;
1352 self.nextthink = time + 1;
1353 self.nextthink += turret_count * sys_frametime;
1355 self.tur_head = spawn();
1356 _setmodel(self.tur_head, tur.head_model);
1357 setsize(self.tur_head, '0 0 0', '0 0 0');
1358 setorigin(self.tur_head, '0 0 0');
1359 setattachment(self.tur_head, self, "tag_head");
1361 self.tur_head.netname = self.tur_head.classname = "turret_head";
1362 self.tur_head.team = self.team;
1363 self.tur_head.owner = self;
1364 self.tur_head.takedamage = DAMAGE_NO;
1365 self.tur_head.solid = SOLID_NOT;
1366 self.tur_head.movetype = self.movetype;
1368 if(!self.tur_defend)
1369 if(self.target != "")
1371 self.tur_defend = find(world, targetname, self.target);
1372 if (self.tur_defend == world)
1375 LOG_TRACE("Turret has invalid defendpoint!\n");
1379 if (self.tur_defend)
1380 self.idle_aim = self.tur_head.angles + angleofs(self.tur_head, self.tur_defend);
1382 self.idle_aim = '0 0 0';
1385 self.tur_debug_start = self.nextthink;
1386 while (vlen(self.tur_debug_rvec) < 2)
1387 self.tur_debug_rvec = randomvec() * 4;
1389 self.tur_debug_rvec_x = fabs(self.tur_debug_rvec_x);
1390 self.tur_debug_rvec_y = fabs(self.tur_debug_rvec_y);
1391 self.tur_debug_rvec_z = fabs(self.tur_debug_rvec_z);
1396 turret_tag_fire_update();
1398 TUR_ACTION(tur_id, TR_SETUP);
1400 if(MUTATOR_CALLHOOK(TurretSpawn, self))