2 #include "../../server/autocvars.qh"
5 vector turret_aim_generic()
9 float distance, impact_time = 0, i, mintime;
11 turret_tag_fire_update();
13 if(self.aim_flags & TFL_AIM_SIMPLE)
14 return real_origin(self.enemy);
16 mintime = max(self.attack_finished_single - time,0) + sys_frametime;
19 pre_pos = real_origin(self.enemy);
22 if (self.aim_flags & TFL_AIM_LEAD)
24 if (self.aim_flags & TFL_AIM_SHOTTIMECOMPENSATE) // Need to conpensate for shot traveltime
28 distance = vlen(prep - self.tur_shotorg);
29 impact_time = distance / self.shot_speed;
31 prep = pre_pos + (self.enemy.velocity * (impact_time + mintime));
33 if(self.aim_flags & TFL_AIM_ZPREDICT)
34 if(!(self.enemy.flags & FL_ONGROUND))
35 if(self.enemy.movetype == MOVETYPE_WALK || self.enemy.movetype == MOVETYPE_TOSS || self.enemy.movetype == MOVETYPE_BOUNCE)
39 vz = self.enemy.velocity_z;
40 for(i = 0; i < impact_time; i += sys_frametime)
42 vz = vz - (autocvar_sv_gravity * sys_frametime);
43 prep_z = prep_z + vz * sys_frametime;
49 pre_pos = pre_pos + self.enemy.velocity * mintime;
52 if(self.aim_flags & TFL_AIM_SPLASH)
54 //tracebox(pre_pos + '0 0 32',self.enemy.mins,self.enemy.maxs,pre_pos -'0 0 64',MOVE_WORLDONLY,self.enemy);
55 traceline(pre_pos + '0 0 32',pre_pos -'0 0 64',MOVE_WORLDONLY,self.enemy);
56 if(trace_fraction != 1.0)
57 pre_pos = trace_endpos;
63 float turret_targetscore_support(entity _turret,entity _target)
65 float score; // Total score
66 float s_score = 0, d_score;
68 if (_turret.enemy == _target) s_score = 1;
70 d_score = min(_turret.target_range_optimal,tvt_dist) / max(_turret.target_range_optimal,tvt_dist);
72 score = (d_score * _turret.target_select_rangebias) +
73 (s_score * _turret.target_select_samebias);
79 * Generic bias aware score system.
81 float turret_targetscore_generic(entity _turret, entity _target)
83 float d_dist; // Defendmode Distance
84 float score; // Total score
85 float d_score; // Distance score
86 float a_score; // Angular score
87 float m_score = 0; // missile score
88 float p_score = 0; // player score
89 float ikr; // ideal kill range
91 if (_turret.tur_defend)
93 d_dist = vlen(real_origin(_target) - _turret.tur_defend.origin);
94 ikr = vlen(_turret.origin - _turret.tur_defend.origin);
95 d_score = 1 - d_dist / _turret.target_range;
99 // Make a normlized value base on the targets distance from our optimal killzone
100 ikr = _turret.target_range_optimal;
101 d_score = min(ikr, tvt_dist) / max(ikr, tvt_dist);
104 a_score = 1 - tvt_thadf / _turret.aim_maxrotate;
106 if ((_turret.target_select_missilebias > 0) && (_target.flags & FL_PROJECTILE))
109 if ((_turret.target_select_playerbias > 0) && IS_CLIENT(_target))
112 d_score = max(d_score, 0);
113 a_score = max(a_score, 0);
114 m_score = max(m_score, 0);
115 p_score = max(p_score, 0);
117 score = (d_score * _turret.target_select_rangebias) +
118 (a_score * _turret.target_select_anglebias) +
119 (m_score * _turret.target_select_missilebias) +
120 (p_score * _turret.target_select_playerbias);
122 if(_turret.target_range < vlen(_turret.tur_shotorg - real_origin(_target)))
129 string sd,sa,sm,sp,ss;
130 string sdt,sat,smt,spt;
133 d_score *= _turret.target_select_rangebias;
136 //sv = ftos(v_score);
137 //v_score *= _turret.target_select_samebias;
138 //svt = ftos(v_score);
141 a_score *= _turret.target_select_anglebias;
145 m_score *= _turret.target_select_missilebias;
149 p_score *= _turret.target_select_playerbias;
154 bprint("^3Target scores^7 \[ ",_turret.netname, " \] ^3for^7 \[ ", _target.netname," \]\n");
155 bprint("^5Range:\[ ",sd, " \]^2+bias:\[ ",sdt," \]\n");
156 bprint("^5Angle:\[ ",sa, " \]^2+bias:\[ ",sat," \]\n");
157 bprint("^5Missile:\[ ",sm," \]^2+bias:\[ ",smt," \]\n");
158 bprint("^5Player:\[ ",sp, " \]^2+bias:\[ ",spt," \]\n");
159 bprint("^3Total (w/bias):\[^1",ss,"\]\n");
166 // Generic damage handling
169 self.effects |= EF_NODRAW;
170 self.nextthink = time + self.respawntime - 0.2;
171 self.think = turret_respawn;
176 self.deadflag = DEAD_DEAD;
177 self.tur_head.deadflag = self.deadflag;
179 // Unsolidify and hide real parts
180 self.solid = SOLID_NOT;
181 self.tur_head.solid = self.solid;
183 self.event_damage = func_null;
184 self.takedamage = DAMAGE_NO;
189 //RadiusDamage (self,self, min(self.ammo,50),min(self.ammo,50) * 0.25,250,world,min(self.ammo,50)*5,DEATH_TURRET,world);
191 if(self.damage_flags & TFL_DMG_DEATH_NORESPAWN)
193 TUR_ACTION(self.turretid, TR_DEATH);
195 remove(self.tur_head);
201 self.SendFlags |= TNSF_STATUS;
202 self.nextthink = time + 0.2;
203 self.think = turret_hide;
205 TUR_ACTION(self.turretid, TR_DEATH);
209 void turret_damage (entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector vforce)
212 if(self.deadflag == DEAD_DEAD)
215 // Inactive turrets take no damage. (hm..)
219 if(SAME_TEAM(self, attacker))
221 if(autocvar_g_friendlyfire)
222 damage = damage * autocvar_g_friendlyfire;
227 self.health -= damage;
229 // thorw head slightly off aim when hit?
230 if (self.damage_flags & TFL_DMG_HEADSHAKE)
232 self.tur_head.angles_x = self.tur_head.angles_x + (-0.5 + random()) * damage;
233 self.tur_head.angles_y = self.tur_head.angles_y + (-0.5 + random()) * damage;
235 self.SendFlags |= TNSF_ANG;
238 if (self.turret_flags & TUR_FLAG_MOVE)
239 self.velocity = self.velocity + vforce;
241 if (self.health <= 0)
243 self.event_damage = func_null;
244 self.tur_head.event_damage = func_null;
245 self.takedamage = DAMAGE_NO;
246 self.nextthink = time;
247 self.think = turret_die;
250 self.SendFlags |= TNSF_STATUS;
254 void turret_respawn()
256 // Make sure all parts belong to the same team since
257 // this function doubles as "teamchange" function.
258 self.tur_head.team = self.team;
259 self.effects &= ~EF_NODRAW;
260 self.deadflag = DEAD_NO;
261 self.effects = EF_LOWPRECISION;
262 self.solid = SOLID_BBOX;
263 self.takedamage = DAMAGE_AIM;
264 self.event_damage = turret_damage;
265 self.avelocity = '0 0 0';
266 self.tur_head.avelocity = self.avelocity;
267 self.tur_head.angles = self.idle_aim;
268 self.health = self.max_health;
270 self.volly_counter = self.shot_volly;
271 self.ammo = self.ammo_max;
273 self.nextthink = time + self.ticrate;
274 self.think = turret_think;
276 self.SendFlags = TNSF_FULL_UPDATE;
278 TUR_ACTION(self.turretid, TR_SETUP);
283 #define cvar_base "g_turrets_unit_"
285 void turrets_setframe(float _frame, float client_only)
287 if((client_only ? self.clientframe : self.frame ) != _frame)
289 self.SendFlags |= TNSF_ANIM;
290 self.anim_start_time = time;
294 self.clientframe = _frame;
300 float turret_send(entity to, float sf)
303 WriteByte(MSG_ENTITY, ENT_CLIENT_TURRET);
304 WriteByte(MSG_ENTITY, sf);
307 WriteByte(MSG_ENTITY, self.turretid);
309 WriteCoord(MSG_ENTITY, self.origin_x);
310 WriteCoord(MSG_ENTITY, self.origin_y);
311 WriteCoord(MSG_ENTITY, self.origin_z);
313 WriteAngle(MSG_ENTITY, self.angles_x);
314 WriteAngle(MSG_ENTITY, self.angles_y);
319 WriteShort(MSG_ENTITY, rint(self.tur_head.angles_x));
320 WriteShort(MSG_ENTITY, rint(self.tur_head.angles_y));
325 WriteShort(MSG_ENTITY, rint(self.tur_head.avelocity_x));
326 WriteShort(MSG_ENTITY, rint(self.tur_head.avelocity_y));
331 WriteShort(MSG_ENTITY, rint(self.origin_x));
332 WriteShort(MSG_ENTITY, rint(self.origin_y));
333 WriteShort(MSG_ENTITY, rint(self.origin_z));
335 WriteShort(MSG_ENTITY, rint(self.velocity_x));
336 WriteShort(MSG_ENTITY, rint(self.velocity_y));
337 WriteShort(MSG_ENTITY, rint(self.velocity_z));
339 WriteShort(MSG_ENTITY, rint(self.angles_y));
344 WriteCoord(MSG_ENTITY, self.anim_start_time);
345 WriteByte(MSG_ENTITY, self.frame);
350 WriteByte(MSG_ENTITY, self.team);
353 WriteByte(MSG_ENTITY, 0);
355 WriteByte(MSG_ENTITY, ceil((self.health / self.max_health) * 255));
361 void load_unit_settings(entity ent, string unitname, float is_reload)
368 if(!ent.turret_scale_damage) ent.turret_scale_damage = 1;
369 if(!ent.turret_scale_range) ent.turret_scale_range = 1;
370 if(!ent.turret_scale_refire) ent.turret_scale_refire = 1;
371 if(!ent.turret_scale_ammo) ent.turret_scale_ammo = 1;
372 if(!ent.turret_scale_aim) ent.turret_scale_aim = 1;
373 if(!ent.turret_scale_health) ent.turret_scale_health = 1;
374 if(!ent.turret_scale_respawn) ent.turret_scale_respawn = 1;
376 sbase = strcat(cvar_base,unitname);
380 ent.tur_head.avelocity = '0 0 0';
382 ent.tur_head.angles = '0 0 0';
385 ent.health = cvar(strcat(sbase,"_health")) * ent.turret_scale_health;
386 ent.respawntime = cvar(strcat(sbase,"_respawntime")) * ent.turret_scale_respawn;
388 ent.shot_dmg = cvar(strcat(sbase,"_shot_dmg")) * ent.turret_scale_damage;
389 ent.shot_refire = cvar(strcat(sbase,"_shot_refire")) * ent.turret_scale_refire;
390 ent.shot_radius = cvar(strcat(sbase,"_shot_radius")) * ent.turret_scale_damage;
391 ent.shot_speed = cvar(strcat(sbase,"_shot_speed"));
392 ent.shot_spread = cvar(strcat(sbase,"_shot_spread"));
393 ent.shot_force = cvar(strcat(sbase,"_shot_force")) * ent.turret_scale_damage;
394 ent.shot_volly = cvar(strcat(sbase,"_shot_volly"));
395 ent.shot_volly_refire = cvar(strcat(sbase,"_shot_volly_refire")) * ent.turret_scale_refire;
397 ent.target_range = cvar(strcat(sbase,"_target_range")) * ent.turret_scale_range;
398 ent.target_range_min = cvar(strcat(sbase,"_target_range_min")) * ent.turret_scale_range;
399 ent.target_range_optimal = cvar(strcat(sbase,"_target_range_optimal")) * ent.turret_scale_range;
400 //ent.target_range_fire = cvar(strcat(sbase,"_target_range_fire")) * ent.turret_scale_range;
402 ent.target_select_rangebias = cvar(strcat(sbase,"_target_select_rangebias"));
403 ent.target_select_samebias = cvar(strcat(sbase,"_target_select_samebias"));
404 ent.target_select_anglebias = cvar(strcat(sbase,"_target_select_anglebias"));
405 ent.target_select_playerbias = cvar(strcat(sbase,"_target_select_playerbias"));
406 //ent.target_select_fov = cvar(cvar_gets(sbase,"_target_select_fov"));
408 ent.ammo_max = cvar(strcat(sbase,"_ammo_max")) * ent.turret_scale_ammo;
409 ent.ammo_recharge = cvar(strcat(sbase,"_ammo_recharge")) * ent.turret_scale_ammo;
411 ent.aim_firetolerance_dist = cvar(strcat(sbase,"_aim_firetolerance_dist"));
412 ent.aim_speed = cvar(strcat(sbase,"_aim_speed")) * ent.turret_scale_aim;
413 ent.aim_maxrotate = cvar(strcat(sbase,"_aim_maxrot"));
414 ent.aim_maxpitch = cvar(strcat(sbase,"_aim_maxpitch"));
416 ent.track_type = cvar(strcat(sbase,"_track_type"));
417 ent.track_accel_pitch = cvar(strcat(sbase,"_track_accel_pitch"));
418 ent.track_accel_rotate = cvar(strcat(sbase,"_track_accel_rot"));
419 ent.track_blendrate = cvar(strcat(sbase,"_track_blendrate"));
422 TUR_ACTION(self.turretid, TR_SETUP);
425 void turret_projectile_explode()
428 self.takedamage = DAMAGE_NO;
429 self.event_damage = func_null;
432 d = RadiusDamage (self, self.owner, self.owner.shot_dmg, 0, self.owner.shot_radius, self, world, self.owner.shot_force, self.totalfrags, world);
433 self.owner.tur_debug_dmg_t_h = self.owner.tur_debug_dmg_t_h + d;
434 self.owner.tur_debug_dmg_t_f = self.owner.tur_debug_dmg_t_f + self.owner.shot_dmg;
436 RadiusDamage (self, self.realowner, self.owner.shot_dmg, 0, self.owner.shot_radius, self, world, self.owner.shot_force, self.totalfrags, world);
441 void turret_projectile_touch()
444 turret_projectile_explode();
447 void turret_projectile_damage(entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector vforce)
449 self.velocity += vforce;
450 self.health -= damage;
451 //self.realowner = attacker; // Dont change realowner, it does not make much sense for turrets
453 W_PrepareExplosionByDamage(self.owner, turret_projectile_explode);
456 entity turret_projectile(string _snd, float _size, float _health, float _death, float _proj_type, float _cull, float _cli_anim)
460 sound (self, CH_WEAPON_A, _snd, VOL_BASE, ATTEN_NORM);
462 setorigin(proj, self.tur_shotorg);
463 setsize(proj, '-0.5 -0.5 -0.5' * _size, '0.5 0.5 0.5' * _size);
465 proj.realowner = self;
466 proj.bot_dodge = true;
467 proj.bot_dodgerating = self.shot_dmg;
468 proj.think = turret_projectile_explode;
469 proj.touch = turret_projectile_touch;
470 proj.nextthink = time + 9;
471 proj.movetype = MOVETYPE_FLYMISSILE;
472 proj.velocity = normalize(self.tur_shotdir_updated + randomvec() * self.shot_spread) * self.shot_speed;
473 proj.flags = FL_PROJECTILE;
474 proj.enemy = self.enemy;
475 proj.totalfrags = _death;
476 PROJECTILE_MAKETRIGGER(proj);
479 proj.health = _health;
480 proj.takedamage = DAMAGE_YES;
481 proj.event_damage = turret_projectile_damage;
484 proj.flags |= FL_NOTARGET;
486 CSQCProjectile(proj, _cli_anim, _proj_type, _cull);
492 ** updates enemy distances, predicted impact point/time
493 ** and updated aim<->predict impact distance.
495 void turret_do_updates(entity t_turret)
503 enemy_pos = real_origin(self.enemy);
505 turret_tag_fire_update();
507 self.tur_shotdir_updated = v_forward;
508 self.tur_dist_enemy = vlen(self.tur_shotorg - enemy_pos);
509 self.tur_dist_aimpos = vlen(self.tur_shotorg - self.tur_aimpos);
511 /*if((self.firecheck_flags & TFL_FIRECHECK_VERIFIED) && (self.enemy))
513 oldpos = self.enemy.origin;
514 setorigin(self.enemy, self.tur_aimpos);
515 tracebox(self.tur_shotorg, '-1 -1 -1', '1 1 1', self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos), MOVE_NORMAL,self);
516 setorigin(self.enemy, oldpos);
518 if(trace_ent == self.enemy)
519 self.tur_dist_impact_to_aimpos = 0;
521 self.tur_dist_impact_to_aimpos = vlen(trace_endpos - self.tur_aimpos);
524 tracebox(self.tur_shotorg, '-1 -1 -1','1 1 1', self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos), MOVE_NORMAL,self);
526 self.tur_dist_impact_to_aimpos = vlen(trace_endpos - self.tur_aimpos) - (vlen(self.enemy.maxs - self.enemy.mins) * 0.5);
527 self.tur_impactent = trace_ent;
528 self.tur_impacttime = vlen(self.tur_shotorg - trace_endpos) / self.shot_speed;
534 ** Handles head rotation according to
535 ** the units .track_type and .track_flags
537 .float turret_framecounter;
540 vector target_angle; // This is where we want to aim
541 vector move_angle; // This is where we can aim
544 v1 = self.tur_head.angles;
545 v2 = self.tur_head.avelocity;
547 if (self.track_flags == TFL_TRACK_NO)
551 target_angle = self.idle_aim - ('1 0 0' * self.aim_maxpitch);
552 else if (self.enemy == world)
555 target_angle = self.idle_aim + self.angles;
557 target_angle = vectoangles(normalize(self.tur_aimpos - self.tur_shotorg));
561 target_angle = vectoangles(normalize(self.tur_aimpos - self.tur_shotorg));
564 self.tur_head.angles_x = anglemods(self.tur_head.angles_x);
565 self.tur_head.angles_y = anglemods(self.tur_head.angles_y);
567 // Find the diffrence between where we currently aim and where we want to aim
568 //move_angle = target_angle - (self.angles + self.tur_head.angles);
569 //move_angle = shortangle_vxy(move_angle,(self.angles + self.tur_head.angles));
571 move_angle = AnglesTransform_ToAngles(AnglesTransform_LeftDivide(AnglesTransform_FromAngles(self.angles), AnglesTransform_FromAngles(target_angle))) - self.tur_head.angles;
572 move_angle = shortangle_vxy(move_angle, self.tur_head.angles);
574 switch(self.track_type)
576 case TFL_TRACKTYPE_STEPMOTOR:
577 f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
578 if (self.track_flags & TFL_TRACK_PITCH)
580 self.tur_head.angles_x += bound(-f_tmp,move_angle_x, f_tmp);
581 if(self.tur_head.angles_x > self.aim_maxpitch)
582 self.tur_head.angles_x = self.aim_maxpitch;
584 if(self.tur_head.angles_x < -self.aim_maxpitch)
585 self.tur_head.angles_x = self.aim_maxpitch;
588 if (self.track_flags & TFL_TRACK_ROTATE)
590 self.tur_head.angles_y += bound(-f_tmp, move_angle_y, f_tmp);
591 if(self.tur_head.angles_y > self.aim_maxrotate)
592 self.tur_head.angles_y = self.aim_maxrotate;
594 if(self.tur_head.angles_y < -self.aim_maxrotate)
595 self.tur_head.angles_y = self.aim_maxrotate;
599 self.SendFlags |= TNSF_ANG;
603 case TFL_TRACKTYPE_FLUIDINERTIA:
604 f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
605 move_angle_x = bound(-self.aim_speed, move_angle_x * self.track_accel_pitch * f_tmp, self.aim_speed);
606 move_angle_y = bound(-self.aim_speed, move_angle_y * self.track_accel_rotate * f_tmp, self.aim_speed);
607 move_angle = (self.tur_head.avelocity * self.track_blendrate) + (move_angle * (1 - self.track_blendrate));
610 case TFL_TRACKTYPE_FLUIDPRECISE:
612 move_angle_y = bound(-self.aim_speed, move_angle_y, self.aim_speed);
613 move_angle_x = bound(-self.aim_speed, move_angle_x, self.aim_speed);
619 if (self.track_flags & TFL_TRACK_PITCH)
621 self.tur_head.avelocity_x = move_angle_x;
622 if((self.tur_head.angles_x + self.tur_head.avelocity_x * self.ticrate) > self.aim_maxpitch)
624 self.tur_head.avelocity_x = 0;
625 self.tur_head.angles_x = self.aim_maxpitch;
627 self.SendFlags |= TNSF_ANG;
630 if((self.tur_head.angles_x + self.tur_head.avelocity_x * self.ticrate) < -self.aim_maxpitch)
632 self.tur_head.avelocity_x = 0;
633 self.tur_head.angles_x = -self.aim_maxpitch;
635 self.SendFlags |= TNSF_ANG;
640 if (self.track_flags & TFL_TRACK_ROTATE)
642 self.tur_head.avelocity_y = move_angle_y;
644 if((self.tur_head.angles_y + self.tur_head.avelocity_y * self.ticrate) > self.aim_maxrotate)
646 self.tur_head.avelocity_y = 0;
647 self.tur_head.angles_y = self.aim_maxrotate;
649 self.SendFlags |= TNSF_ANG;
652 if((self.tur_head.angles_y + self.tur_head.avelocity_y * self.ticrate) < -self.aim_maxrotate)
654 self.tur_head.avelocity_y = 0;
655 self.tur_head.angles_y = -self.aim_maxrotate;
657 self.SendFlags |= TNSF_ANG;
661 self.SendFlags |= TNSF_AVEL;
663 // Force a angle update every 10'th frame
664 self.turret_framecounter += 1;
665 if(self.turret_framecounter >= 10)
667 self.SendFlags |= TNSF_ANG;
668 self.turret_framecounter = 0;
673 + TFL_TARGETSELECT_NO
674 + TFL_TARGETSELECT_LOS
675 + TFL_TARGETSELECT_PLAYERS
676 + TFL_TARGETSELECT_MISSILES
677 - TFL_TARGETSELECT_TRIGGERTARGET
678 + TFL_TARGETSELECT_ANGLELIMITS
679 + TFL_TARGETSELECT_RANGELIMITS
680 + TFL_TARGETSELECT_TEAMCHECK
681 - TFL_TARGETSELECT_NOBUILTIN
682 + TFL_TARGETSELECT_OWNTEAM
686 ** Evaluate a entity for target valitity based on validate_flags
687 ** NOTE: the caller must check takedamage before calling this, to inline this check.
689 float turret_validate_target(entity e_turret, entity e_target, float validate_flags)
693 //if(!validate_flags & TFL_TARGETSELECT_NOBUILTIN)
699 if(e_target.owner == e_turret)
702 if(!checkpvs(e_target.origin, e_turret))
705 if(e_target.alpha <= 0.3)
709 if (substring(e_target.classname, 0, 10) == "onslaught_") // don't attack onslaught targets, that's the player's job!
712 if (validate_flags & TFL_TARGETSELECT_NO)
715 // If only this was used more..
716 if (e_target.flags & FL_NOTARGET)
720 if(IS_VEHICLE(e_target))
722 if (e_target.vehicle_health <= 0)
725 else if (e_target.health <= 0)
727 else if(e_target.frozen > 0)
731 if (IS_CLIENT(e_target))
733 if(!(validate_flags & TFL_TARGETSELECT_PLAYERS))
736 if (e_target.deadflag != DEAD_NO)
741 if(validate_flags & TFL_TARGETSELECT_NOTURRETS)
742 if(e_target.owner.tur_head == e_target)
743 if(e_target.team != e_turret.team) // Dont break support units.
747 if (e_target.flags & FL_PROJECTILE)
748 if(!(validate_flags & TFL_TARGETSELECT_MISSILES))
751 if (validate_flags & TFL_TARGETSELECT_MISSILESONLY)
752 if(!(e_target.flags & FL_PROJECTILE))
756 if (validate_flags & TFL_TARGETSELECT_TEAMCHECK)
758 if (validate_flags & TFL_TARGETSELECT_OWNTEAM)
760 if (e_target.team != e_turret.team)
763 if (e_turret.team != e_target.owner.team)
768 if (e_target.team == e_turret.team)
771 if (e_turret.team == e_target.owner.team)
777 tvt_dist = vlen(e_turret.origin - real_origin(e_target));
778 if (validate_flags & TFL_TARGETSELECT_RANGELIMITS)
780 if (tvt_dist < e_turret.target_range_min)
783 if (tvt_dist > e_turret.target_range)
787 // Can we even aim this thing?
788 tvt_thadv = angleofs3(e_turret.tur_head.origin, e_turret.angles + e_turret.tur_head.angles, e_target);
789 tvt_tadv = shortangle_vxy(angleofs(e_turret, e_target), e_turret.angles);
790 tvt_thadf = vlen(tvt_thadv);
791 tvt_tadf = vlen(tvt_tadv);
794 if(validate_flags & TFL_TARGETSELECT_FOV)
796 if(e_turret.target_select_fov < tvt_thadf)
801 if (validate_flags & TFL_TARGETSELECT_ANGLELIMITS)
803 if (fabs(tvt_tadv_x) > e_turret.aim_maxpitch)
806 if (fabs(tvt_tadv_y) > e_turret.aim_maxrotate)
811 if (validate_flags & TFL_TARGETSELECT_LOS)
813 v_tmp = real_origin(e_target) + ((e_target.mins + e_target.maxs) * 0.5);
815 traceline(e_turret.origin + '0 0 16', v_tmp, 0, e_turret);
817 if (e_turret.aim_firetolerance_dist < vlen(v_tmp - trace_endpos))
821 if (e_target.classname == "grapplinghook")
825 if (e_target.classname == "func_button")
829 #ifdef TURRET_DEBUG_TARGETSELECT
830 LOG_TRACE("Target:",e_target.netname," is a valid target for ",e_turret.netname,"\n");
836 entity turret_select_target()
838 entity e; // target looper entity
839 float score; // target looper entity score
840 entity e_enemy; // currently best scoreing target
841 float m_score; // currently best scoreing target's score
844 if(self.enemy && self.enemy.takedamage && turret_validate_target(self,self.enemy,self.target_validate_flags) > 0)
846 e_enemy = self.enemy;
847 m_score = self.turret_score_target(self,e_enemy) * self.target_select_samebias;
850 e_enemy = self.enemy = world;
852 e = findradius(self.origin, self.target_range);
854 // Nothing to aim at?
862 float f = turret_validate_target(self, e, self.target_select_flags);
863 //dprint("F is: ", ftos(f), "\n");
866 score = self.turret_score_target(self,e);
867 if ((score > m_score) && (score > 0))
886 + TFL_FIRECHECK_WORLD
888 + TFL_FIRECHECK_DISTANCES
890 + TFL_FIRECHECK_AIMDIST
891 + TFL_FIRECHECK_REALDIST
892 - TFL_FIRECHECK_ANGLEDIST
893 - TFL_FIRECHECK_TEAMCECK
895 + TFL_FIRECHECK_AMMO_OWN
896 + TFL_FIRECHECK_AMMO_OTHER
897 + TFL_FIRECHECK_REFIRE
901 ** Preforms pre-fire checks based on the uints firecheck_flags
903 float turret_firecheck()
905 // This one just dont care =)
906 if (self.firecheck_flags & TFL_FIRECHECK_NO)
909 if (self.enemy == world)
913 if (self.firecheck_flags & TFL_FIRECHECK_REFIRE)
914 if (self.attack_finished_single > time) return 0;
916 // Special case: volly fire turret that has to fire a full volly if a shot was fired.
917 if (self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
918 if (self.volly_counter != self.shot_volly)
919 if(self.ammo >= self.shot_dmg)
922 // Lack of zombies makes shooting dead things unnecessary :P
923 if (self.firecheck_flags & TFL_FIRECHECK_DEAD)
924 if (self.enemy.deadflag != DEAD_NO)
928 if (self.firecheck_flags & TFL_FIRECHECK_AMMO_OWN)
929 if (self.ammo < self.shot_dmg)
932 // Other's ammo? (support-supply units)
933 if (self.firecheck_flags & TFL_FIRECHECK_AMMO_OTHER)
934 if (self.enemy.ammo >= self.enemy.ammo_max)
937 // Target of opertunity?
938 if(turret_validate_target(self, self.tur_impactent, self.target_validate_flags) > 0)
940 self.enemy = self.tur_impactent;
944 if (self.firecheck_flags & TFL_FIRECHECK_DISTANCES)
947 if (self.tur_dist_aimpos < self.target_range_min)
948 if(turret_validate_target(self, self.tur_impactent, self.target_validate_flags) > 0)
949 return 1; // Target of opertunity?
955 if (self.firecheck_flags & TFL_FIRECHECK_AFF)
956 if (self.tur_impactent.team == self.team)
959 // aim<->predicted impact
960 if (self.firecheck_flags & TFL_FIRECHECK_AIMDIST)
961 if (self.tur_dist_impact_to_aimpos > self.aim_firetolerance_dist)
965 if (self.shot_volly > 1)
966 if (self.volly_counter == self.shot_volly)
967 if (self.ammo < (self.shot_dmg * self.shot_volly))
970 /*if(self.firecheck_flags & TFL_FIRECHECK_VERIFIED)
971 if(self.tur_impactent != self.enemy)
977 bool turret_checkfire()
979 bool ret = false; // dummy
980 if(MUTATOR_CALLHOOK(Turret_CheckFire, ret))
983 return self.turret_firecheckfunc();
988 if (autocvar_g_turrets_nofire != 0)
991 if(MUTATOR_CALLHOOK(TurretFire, self))
994 TUR_ACTION(self.turretid, TR_ATTACK);
996 self.attack_finished_single = time + self.shot_refire;
997 self.ammo -= self.shot_dmg;
998 self.volly_counter = self.volly_counter - 1;
1000 if (self.volly_counter <= 0)
1002 self.volly_counter = self.shot_volly;
1004 if (self.shoot_flags & TFL_SHOOT_CLEARTARGET)
1007 if (self.shot_volly > 1)
1008 self.attack_finished_single = time + self.shot_volly_refire;
1012 if (self.enemy) paint_target3(self.tur_aimpos, 64, self.tur_debug_rvec, self.tur_impacttime + 0.25);
1020 self.nextthink = time + self.ticrate;
1022 // ONS uses somewhat backwards linking.
1028 e = find(world, targetname,self.target);
1033 if (self.team != self.tur_head.team)
1038 if (self.tur_debug_tmr1 < time)
1040 if (self.enemy) paint_target (self.enemy,128,self.tur_debug_rvec,0.9);
1041 paint_target(self,256,self.tur_debug_rvec,0.9);
1042 self.tur_debug_tmr1 = time + 1;
1047 if (!(self.spawnflags & TSF_NO_AMMO_REGEN))
1048 if (self.ammo < self.ammo_max)
1049 self.ammo = min(self.ammo + self.ammo_recharge, self.ammo_max);
1051 // Inactive turrets needs to run the think loop,
1052 // So they can handle animation and wake up if need be.
1059 // This is typicaly used for zaping every target in range
1060 // turret_fusionreactor uses this to recharge friendlys.
1061 if (self.shoot_flags & TFL_SHOOT_HITALLVALID)
1063 // Do a self.turret_fire for every valid target.
1064 e = findradius(self.origin,self.target_range);
1069 if (turret_validate_target(self,e,self.target_validate_flags))
1073 turret_do_updates(self);
1075 if (turret_checkfire())
1084 else if(self.shoot_flags & TFL_SHOOT_CUSTOM)
1086 // This one is doing something.. oddball. assume its handles what needs to be handled.
1089 if(!(self.aim_flags & TFL_AIM_NO))
1090 self.tur_aimpos = turret_aim_generic();
1093 if(!(self.track_flags & TFL_TRACK_NO))
1096 turret_do_updates(self);
1099 if (turret_checkfire())
1104 // Special case for volly always. if it fired once it must compleate the volly.
1105 if(self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
1106 if(self.volly_counter != self.shot_volly)
1108 // Predict or whatnot
1109 if(!(self.aim_flags & TFL_AIM_NO))
1110 self.tur_aimpos = turret_aim_generic();
1113 if(!(self.track_flags & TFL_TRACK_NO))
1116 turret_do_updates(self);
1119 if (turret_checkfire())
1122 TUR_ACTION(self.turretid, TR_THINK);
1127 // Check if we have a vailid enemy, and try to find one if we dont.
1129 // g_turrets_targetscan_maxdelay forces a target re-scan at least this often
1130 float do_target_scan = 0;
1131 if((self.target_select_time + autocvar_g_turrets_targetscan_maxdelay) < time)
1134 // Old target (if any) invalid?
1135 if(self.target_validate_time < time)
1136 if (turret_validate_target(self, self.enemy, self.target_validate_flags) <= 0)
1139 self.target_validate_time = time + 0.5;
1143 // But never more often then g_turrets_targetscan_mindelay!
1144 if (self.target_select_time + autocvar_g_turrets_targetscan_mindelay > time)
1149 self.enemy = turret_select_target();
1150 self.target_select_time = time;
1153 // No target, just go to idle, do any custom stuff and bail.
1154 if (self.enemy == world)
1157 if(!(self.track_flags & TFL_TRACK_NO))
1160 TUR_ACTION(self.turretid, TR_THINK);
1166 self.lip = time + autocvar_g_turrets_aimidle_delay; // Keep track of the last time we had a target.
1169 if(!(self.aim_flags & TFL_AIM_NO))
1170 self.tur_aimpos = turret_aim_generic();
1173 if(!(self.track_flags & TFL_TRACK_NO))
1176 turret_do_updates(self);
1179 if (turret_checkfire())
1183 TUR_ACTION(self.turretid, TR_THINK);
1187 When .used a turret switch team to activator.team.
1188 If activator is world, the turret go inactive.
1192 LOG_TRACE("Turret ",self.netname, " used by ", activator.classname, "\n");
1194 self.team = activator.team;
1197 self.active = ACTIVE_NOT;
1199 self.active = ACTIVE_ACTIVE;
1205 Net_LinkEntity(self, true, 0, turret_send);
1206 self.think = turret_think;
1207 self.nextthink = time;
1208 self.tur_head.effects = EF_NODRAW;
1211 void turrets_manager_think()
1213 self.nextthink = time + 1;
1216 if (autocvar_g_turrets_reloadcvars == 1)
1223 load_unit_settings(e,e.cvar_basename,1);
1224 TUR_ACTION(self.turretid, TR_THINK);
1229 cvar_set("g_turrets_reloadcvars","0");
1233 float turret_initialize(float tur_id)
1235 if(!autocvar_g_turrets)
1239 entity tur = get_turretinfo(tur_id);
1240 if(tur.turretid == 0)
1241 return false; // invalid turret
1243 if(!self.tur_head) { TUR_ACTION(tur_id, TR_PRECACHE); } // if tur_head exists, we can assume this turret re-spawned
1245 e = find(world, classname, "turret_manager");
1249 e.classname = "turret_manager";
1250 e.think = turrets_manager_think;
1251 e.nextthink = time + 2;
1254 if(!(self.spawnflags & TSF_SUSPENDED))
1255 builtin_droptofloor();
1257 self.cvar_basename = tur.cvar_basename;
1258 load_unit_settings(self, self.cvar_basename, 0);
1260 if(!self.team || !teamplay) { self.team = MAX_SHOT_DISTANCE; }
1261 if(!self.ticrate) { self.ticrate = ((self.turret_flags & TUR_FLAG_SUPPORT) ? 0.2 : 0.1); }
1262 if(!self.health) { self.health = 1000; }
1263 if(!self.shot_refire) { self.shot_refire = 1; }
1264 if(!self.tur_shotorg) { self.tur_shotorg = '50 0 50'; }
1265 if(!self.turret_flags) { self.turret_flags = TUR_FLAG_SPLASH | TUR_FLAG_MEDPROJ | TUR_FLAG_PLAYER; }
1266 if(!self.damage_flags) { self.damage_flags = TFL_DMG_YES | TFL_DMG_RETALIATE | TFL_DMG_AIMSHAKE; }
1267 if(!self.aim_flags) { self.aim_flags = TFL_AIM_LEAD | TFL_AIM_SHOTTIMECOMPENSATE; }
1268 if(!self.track_type) { self.track_type = TFL_TRACKTYPE_STEPMOTOR; }
1269 if(!self.track_flags) { self.track_flags = TFL_TRACK_PITCH | TFL_TRACK_ROTATE; }
1270 if(!self.ammo_flags) { self.ammo_flags = TFL_AMMO_ENERGY | TFL_AMMO_RECHARGE; }
1271 if(!self.target_select_flags) { self.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_TEAMCHECK | TFL_TARGETSELECT_RANGELIMITS | TFL_TARGETSELECT_ANGLELIMITS; }
1272 if(!self.firecheck_flags) { self.firecheck_flags = TFL_FIRECHECK_DEAD | TFL_FIRECHECK_DISTANCES | TFL_FIRECHECK_LOS
1273 | TFL_FIRECHECK_AIMDIST | TFL_FIRECHECK_TEAMCHECK | TFL_FIRECHECK_AMMO_OWN | TFL_FIRECHECK_REFIRE; }
1275 if(self.track_type != TFL_TRACKTYPE_STEPMOTOR)
1277 // Fluid / Ineria mode. Looks mutch nicer.
1278 // Can reduce aim preformance alot, needs a bit diffrent aimspeed
1280 self.aim_speed = bound(0.1, ((!self.aim_speed) ? 180 : self.aim_speed), 1000);
1282 if(!self.track_accel_pitch) { self.track_accel_pitch = 0.5; }
1283 if(!self.track_accel_rotate) { self.track_accel_rotate = 0.5; }
1284 if(!self.track_blendrate) { self.track_blendrate = 0.35; }
1287 self.respawntime = max(-1, ((!self.respawntime) ? 60 : self.respawntime));
1288 self.shot_refire = bound(0.01, ((!self.shot_refire) ? 1 : self.shot_refire), 9999);
1289 self.shot_dmg = max(1, ((!self.shot_dmg) ? self.shot_refire * 50 : self.shot_dmg));
1290 self.shot_radius = max(1, ((!self.shot_radius) ? self.shot_dmg * 0.5 : self.shot_radius));
1291 self.shot_speed = max(1, ((!self.shot_speed) ? 2500 : self.shot_speed));
1292 self.shot_spread = bound(0.0001, ((!self.shot_spread) ? 0.0125 : self.shot_spread), 500);
1293 self.shot_force = bound(0.001, ((!self.shot_force) ? self.shot_dmg * 0.5 + self.shot_radius * 0.5 : self.shot_force), 5000);
1294 self.shot_volly = bound(1, ((!self.shot_volly) ? 1 : self.shot_volly), floor(self.ammo_max / self.shot_dmg));
1295 self.shot_volly_refire = bound(self.shot_refire, ((!self.shot_volly_refire) ? self.shot_refire * self.shot_volly : self.shot_volly_refire), 60);
1296 self.target_range = bound(0, ((!self.target_range) ? self.shot_speed * 0.5 : self.target_range), MAX_SHOT_DISTANCE);
1297 self.target_range_min = bound(0, ((!self.target_range_min) ? self.shot_radius * 2 : self.target_range_min), MAX_SHOT_DISTANCE);
1298 self.target_range_optimal = bound(0, ((!self.target_range_optimal) ? self.target_range * 0.5 : self.target_range_optimal), MAX_SHOT_DISTANCE);
1299 self.aim_maxrotate = bound(0, ((!self.aim_maxrotate) ? 90 : self.aim_maxrotate), 360);
1300 self.aim_maxpitch = bound(0, ((!self.aim_maxpitch) ? 20 : self.aim_maxpitch), 90);
1301 self.aim_speed = bound(0.1, ((!self.aim_speed) ? 36 : self.aim_speed), 1000);
1302 self.aim_firetolerance_dist = bound(0.1, ((!self.aim_firetolerance_dist) ? 5 + (self.shot_radius * 2) : self.aim_firetolerance_dist), MAX_SHOT_DISTANCE);
1303 self.target_select_rangebias = bound(-10, ((!self.target_select_rangebias) ? 1 : self.target_select_rangebias), 10);
1304 self.target_select_samebias = bound(-10, ((!self.target_select_samebias) ? 1 : self.target_select_samebias), 10);
1305 self.target_select_anglebias = bound(-10, ((!self.target_select_anglebias) ? 1 : self.target_select_anglebias), 10);
1306 self.target_select_missilebias = bound(-10, ((!self.target_select_missilebias) ? 1 : self.target_select_missilebias), 10);
1307 self.target_select_playerbias = bound(-10, ((!self.target_select_playerbias) ? 1 : self.target_select_playerbias), 10);
1308 self.ammo_max = max(self.shot_dmg, ((!self.ammo_max) ? self.shot_dmg * 10 : self.ammo_max));
1309 self.ammo_recharge = max(0, ((!self.ammo_recharge) ? self.shot_dmg * 0.5 : self.ammo_recharge));
1311 self.turret_flags = TUR_FLAG_ISTURRET | (tur.spawnflags);
1313 if(self.turret_flags & TUR_FLAG_SPLASH)
1314 self.aim_flags |= TFL_AIM_SPLASH;
1316 if(self.turret_flags & TUR_FLAG_MISSILE)
1317 self.target_select_flags |= TFL_TARGETSELECT_MISSILES;
1319 if(self.turret_flags & TUR_FLAG_PLAYER)
1320 self.target_select_flags |= TFL_TARGETSELECT_PLAYERS;
1322 if(self.spawnflags & TSL_NO_RESPAWN)
1323 self.damage_flags |= TFL_DMG_DEATH_NORESPAWN;
1325 if (self.turret_flags & TUR_FLAG_SUPPORT)
1326 self.turret_score_target = turret_targetscore_support;
1328 self.turret_score_target = turret_targetscore_generic;
1332 setmodel(self, tur.model);
1333 setsize(self, tur.mins, tur.maxs);
1335 self.turretid = tur_id;
1336 self.classname = "turret_main";
1337 self.active = ACTIVE_ACTIVE;
1338 self.effects = EF_NODRAW;
1339 self.netname = TUR_NAME(tur_id);
1340 self.ticrate = bound(sys_frametime, self.ticrate, 60);
1341 self.max_health = self.health;
1342 self.target_validate_flags = self.target_select_flags;
1343 self.ammo = self.ammo_max;
1344 self.ammo_recharge *= self.ticrate;
1345 self.solid = SOLID_BBOX;
1346 self.takedamage = DAMAGE_AIM;
1347 self.movetype = MOVETYPE_NOCLIP;
1348 self.view_ofs = '0 0 0';
1349 self.turret_firecheckfunc = turret_firecheck;
1350 self.event_damage = turret_damage;
1351 self.use = turret_use;
1352 self.bot_attack = true;
1353 self.nextthink = time + 1;
1354 self.nextthink += turret_count * sys_frametime;
1356 self.tur_head = spawn();
1357 setmodel(self.tur_head, tur.head_model);
1358 setsize(self.tur_head, '0 0 0', '0 0 0');
1359 setorigin(self.tur_head, '0 0 0');
1360 setattachment(self.tur_head, self, "tag_head");
1362 self.tur_head.netname = self.tur_head.classname = "turret_head";
1363 self.tur_head.team = self.team;
1364 self.tur_head.owner = self;
1365 self.tur_head.takedamage = DAMAGE_NO;
1366 self.tur_head.solid = SOLID_NOT;
1367 self.tur_head.movetype = self.movetype;
1369 if(!self.tur_defend)
1370 if(self.target != "")
1372 self.tur_defend = find(world, targetname, self.target);
1373 if (self.tur_defend == world)
1376 LOG_TRACE("Turret has invalid defendpoint!\n");
1380 if (self.tur_defend)
1381 self.idle_aim = self.tur_head.angles + angleofs(self.tur_head, self.tur_defend);
1383 self.idle_aim = '0 0 0';
1386 self.tur_debug_start = self.nextthink;
1387 while (vlen(self.tur_debug_rvec) < 2)
1388 self.tur_debug_rvec = randomvec() * 4;
1390 self.tur_debug_rvec_x = fabs(self.tur_debug_rvec_x);
1391 self.tur_debug_rvec_y = fabs(self.tur_debug_rvec_y);
1392 self.tur_debug_rvec_z = fabs(self.tur_debug_rvec_z);
1397 turret_tag_fire_update();
1399 TUR_ACTION(tur_id, TR_SETUP);
1401 if(MUTATOR_CALLHOOK(TurretSpawn, self))