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)
501 enemy_pos = real_origin(self.enemy);
503 turret_tag_fire_update();
505 self.tur_shotdir_updated = v_forward;
506 self.tur_dist_enemy = vlen(self.tur_shotorg - enemy_pos);
507 self.tur_dist_aimpos = vlen(self.tur_shotorg - self.tur_aimpos);
509 /*if((self.firecheck_flags & TFL_FIRECHECK_VERIFIED) && (self.enemy))
511 oldpos = self.enemy.origin;
512 setorigin(self.enemy, self.tur_aimpos);
513 tracebox(self.tur_shotorg, '-1 -1 -1', '1 1 1', self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos), MOVE_NORMAL,self);
514 setorigin(self.enemy, oldpos);
516 if(trace_ent == self.enemy)
517 self.tur_dist_impact_to_aimpos = 0;
519 self.tur_dist_impact_to_aimpos = vlen(trace_endpos - self.tur_aimpos);
522 tracebox(self.tur_shotorg, '-1 -1 -1','1 1 1', self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos), MOVE_NORMAL,self);
524 self.tur_dist_impact_to_aimpos = vlen(trace_endpos - self.tur_aimpos) - (vlen(self.enemy.maxs - self.enemy.mins) * 0.5);
525 self.tur_impactent = trace_ent;
526 self.tur_impacttime = vlen(self.tur_shotorg - trace_endpos) / self.shot_speed;
532 ** Handles head rotation according to
533 ** the units .track_type and .track_flags
535 .float turret_framecounter;
538 vector target_angle; // This is where we want to aim
539 vector move_angle; // This is where we can aim
542 v1 = self.tur_head.angles;
543 v2 = self.tur_head.avelocity;
545 if (self.track_flags == TFL_TRACK_NO)
549 target_angle = self.idle_aim - ('1 0 0' * self.aim_maxpitch);
550 else if (self.enemy == world)
553 target_angle = self.idle_aim + self.angles;
555 target_angle = vectoangles(normalize(self.tur_aimpos - self.tur_shotorg));
559 target_angle = vectoangles(normalize(self.tur_aimpos - self.tur_shotorg));
562 self.tur_head.angles_x = anglemods(self.tur_head.angles_x);
563 self.tur_head.angles_y = anglemods(self.tur_head.angles_y);
565 // Find the diffrence between where we currently aim and where we want to aim
566 //move_angle = target_angle - (self.angles + self.tur_head.angles);
567 //move_angle = shortangle_vxy(move_angle,(self.angles + self.tur_head.angles));
569 move_angle = AnglesTransform_ToAngles(AnglesTransform_LeftDivide(AnglesTransform_FromAngles(self.angles), AnglesTransform_FromAngles(target_angle))) - self.tur_head.angles;
570 move_angle = shortangle_vxy(move_angle, self.tur_head.angles);
572 switch(self.track_type)
574 case TFL_TRACKTYPE_STEPMOTOR:
575 f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
576 if (self.track_flags & TFL_TRACK_PITCH)
578 self.tur_head.angles_x += bound(-f_tmp,move_angle_x, f_tmp);
579 if(self.tur_head.angles_x > self.aim_maxpitch)
580 self.tur_head.angles_x = self.aim_maxpitch;
582 if(self.tur_head.angles_x < -self.aim_maxpitch)
583 self.tur_head.angles_x = self.aim_maxpitch;
586 if (self.track_flags & TFL_TRACK_ROTATE)
588 self.tur_head.angles_y += bound(-f_tmp, move_angle_y, f_tmp);
589 if(self.tur_head.angles_y > self.aim_maxrotate)
590 self.tur_head.angles_y = self.aim_maxrotate;
592 if(self.tur_head.angles_y < -self.aim_maxrotate)
593 self.tur_head.angles_y = self.aim_maxrotate;
597 self.SendFlags |= TNSF_ANG;
601 case TFL_TRACKTYPE_FLUIDINERTIA:
602 f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
603 move_angle_x = bound(-self.aim_speed, move_angle_x * self.track_accel_pitch * f_tmp, self.aim_speed);
604 move_angle_y = bound(-self.aim_speed, move_angle_y * self.track_accel_rotate * f_tmp, self.aim_speed);
605 move_angle = (self.tur_head.avelocity * self.track_blendrate) + (move_angle * (1 - self.track_blendrate));
608 case TFL_TRACKTYPE_FLUIDPRECISE:
610 move_angle_y = bound(-self.aim_speed, move_angle_y, self.aim_speed);
611 move_angle_x = bound(-self.aim_speed, move_angle_x, self.aim_speed);
617 if (self.track_flags & TFL_TRACK_PITCH)
619 self.tur_head.avelocity_x = move_angle_x;
620 if((self.tur_head.angles_x + self.tur_head.avelocity_x * self.ticrate) > self.aim_maxpitch)
622 self.tur_head.avelocity_x = 0;
623 self.tur_head.angles_x = self.aim_maxpitch;
625 self.SendFlags |= TNSF_ANG;
628 if((self.tur_head.angles_x + self.tur_head.avelocity_x * self.ticrate) < -self.aim_maxpitch)
630 self.tur_head.avelocity_x = 0;
631 self.tur_head.angles_x = -self.aim_maxpitch;
633 self.SendFlags |= TNSF_ANG;
638 if (self.track_flags & TFL_TRACK_ROTATE)
640 self.tur_head.avelocity_y = move_angle_y;
642 if((self.tur_head.angles_y + self.tur_head.avelocity_y * self.ticrate) > self.aim_maxrotate)
644 self.tur_head.avelocity_y = 0;
645 self.tur_head.angles_y = self.aim_maxrotate;
647 self.SendFlags |= TNSF_ANG;
650 if((self.tur_head.angles_y + self.tur_head.avelocity_y * self.ticrate) < -self.aim_maxrotate)
652 self.tur_head.avelocity_y = 0;
653 self.tur_head.angles_y = -self.aim_maxrotate;
655 self.SendFlags |= TNSF_ANG;
659 self.SendFlags |= TNSF_AVEL;
661 // Force a angle update every 10'th frame
662 self.turret_framecounter += 1;
663 if(self.turret_framecounter >= 10)
665 self.SendFlags |= TNSF_ANG;
666 self.turret_framecounter = 0;
671 + TFL_TARGETSELECT_NO
672 + TFL_TARGETSELECT_LOS
673 + TFL_TARGETSELECT_PLAYERS
674 + TFL_TARGETSELECT_MISSILES
675 - TFL_TARGETSELECT_TRIGGERTARGET
676 + TFL_TARGETSELECT_ANGLELIMITS
677 + TFL_TARGETSELECT_RANGELIMITS
678 + TFL_TARGETSELECT_TEAMCHECK
679 - TFL_TARGETSELECT_NOBUILTIN
680 + TFL_TARGETSELECT_OWNTEAM
684 ** Evaluate a entity for target valitity based on validate_flags
685 ** NOTE: the caller must check takedamage before calling this, to inline this check.
687 float turret_validate_target(entity e_turret, entity e_target, float validate_flags)
691 //if(!validate_flags & TFL_TARGETSELECT_NOBUILTIN)
697 if(e_target.owner == e_turret)
700 if(!checkpvs(e_target.origin, e_turret))
703 if(e_target.alpha <= 0.3)
707 if (substring(e_target.classname, 0, 10) == "onslaught_") // don't attack onslaught targets, that's the player's job!
710 if (validate_flags & TFL_TARGETSELECT_NO)
713 // If only this was used more..
714 if (e_target.flags & FL_NOTARGET)
718 if(IS_VEHICLE(e_target))
720 if (e_target.vehicle_health <= 0)
723 else if (e_target.health <= 0)
725 else if(e_target.frozen > 0)
729 if (IS_CLIENT(e_target))
731 if(!(validate_flags & TFL_TARGETSELECT_PLAYERS))
734 if (e_target.deadflag != DEAD_NO)
739 if(validate_flags & TFL_TARGETSELECT_NOTURRETS)
740 if(e_target.owner.tur_head == e_target)
741 if(e_target.team != e_turret.team) // Dont break support units.
745 if (e_target.flags & FL_PROJECTILE)
746 if(!(validate_flags & TFL_TARGETSELECT_MISSILES))
749 if (validate_flags & TFL_TARGETSELECT_MISSILESONLY)
750 if(!(e_target.flags & FL_PROJECTILE))
754 if (validate_flags & TFL_TARGETSELECT_TEAMCHECK)
756 if (validate_flags & TFL_TARGETSELECT_OWNTEAM)
758 if (e_target.team != e_turret.team)
761 if (e_turret.team != e_target.owner.team)
766 if (e_target.team == e_turret.team)
769 if (e_turret.team == e_target.owner.team)
775 tvt_dist = vlen(e_turret.origin - real_origin(e_target));
776 if (validate_flags & TFL_TARGETSELECT_RANGELIMITS)
778 if (tvt_dist < e_turret.target_range_min)
781 if (tvt_dist > e_turret.target_range)
785 // Can we even aim this thing?
786 tvt_thadv = angleofs3(e_turret.tur_head.origin, e_turret.angles + e_turret.tur_head.angles, e_target);
787 tvt_tadv = shortangle_vxy(angleofs(e_turret, e_target), e_turret.angles);
788 tvt_thadf = vlen(tvt_thadv);
789 tvt_tadf = vlen(tvt_tadv);
792 if(validate_flags & TFL_TARGETSELECT_FOV)
794 if(e_turret.target_select_fov < tvt_thadf)
799 if (validate_flags & TFL_TARGETSELECT_ANGLELIMITS)
801 if (fabs(tvt_tadv_x) > e_turret.aim_maxpitch)
804 if (fabs(tvt_tadv_y) > e_turret.aim_maxrotate)
809 if (validate_flags & TFL_TARGETSELECT_LOS)
811 v_tmp = real_origin(e_target) + ((e_target.mins + e_target.maxs) * 0.5);
813 traceline(e_turret.origin + '0 0 16', v_tmp, 0, e_turret);
815 if (e_turret.aim_firetolerance_dist < vlen(v_tmp - trace_endpos))
819 if (e_target.classname == "grapplinghook")
823 if (e_target.classname == "func_button")
827 #ifdef TURRET_DEBUG_TARGETSELECT
828 LOG_TRACE("Target:",e_target.netname," is a valid target for ",e_turret.netname,"\n");
834 entity turret_select_target()
836 entity e; // target looper entity
837 float score; // target looper entity score
838 entity e_enemy; // currently best scoreing target
839 float m_score; // currently best scoreing target's score
842 if(self.enemy && self.enemy.takedamage && turret_validate_target(self,self.enemy,self.target_validate_flags) > 0)
844 e_enemy = self.enemy;
845 m_score = self.turret_score_target(self,e_enemy) * self.target_select_samebias;
848 e_enemy = self.enemy = world;
850 e = findradius(self.origin, self.target_range);
852 // Nothing to aim at?
860 float f = turret_validate_target(self, e, self.target_select_flags);
861 //dprint("F is: ", ftos(f), "\n");
864 score = self.turret_score_target(self,e);
865 if ((score > m_score) && (score > 0))
884 + TFL_FIRECHECK_WORLD
886 + TFL_FIRECHECK_DISTANCES
888 + TFL_FIRECHECK_AIMDIST
889 + TFL_FIRECHECK_REALDIST
890 - TFL_FIRECHECK_ANGLEDIST
891 - TFL_FIRECHECK_TEAMCECK
893 + TFL_FIRECHECK_AMMO_OWN
894 + TFL_FIRECHECK_AMMO_OTHER
895 + TFL_FIRECHECK_REFIRE
899 ** Preforms pre-fire checks based on the uints firecheck_flags
901 float turret_firecheck()
903 // This one just dont care =)
904 if (self.firecheck_flags & TFL_FIRECHECK_NO)
907 if (self.enemy == world)
911 if (self.firecheck_flags & TFL_FIRECHECK_REFIRE)
912 if (self.attack_finished_single > time) return 0;
914 // Special case: volly fire turret that has to fire a full volly if a shot was fired.
915 if (self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
916 if (self.volly_counter != self.shot_volly)
917 if(self.ammo >= self.shot_dmg)
920 // Lack of zombies makes shooting dead things unnecessary :P
921 if (self.firecheck_flags & TFL_FIRECHECK_DEAD)
922 if (self.enemy.deadflag != DEAD_NO)
926 if (self.firecheck_flags & TFL_FIRECHECK_AMMO_OWN)
927 if (self.ammo < self.shot_dmg)
930 // Other's ammo? (support-supply units)
931 if (self.firecheck_flags & TFL_FIRECHECK_AMMO_OTHER)
932 if (self.enemy.ammo >= self.enemy.ammo_max)
935 // Target of opertunity?
936 if(turret_validate_target(self, self.tur_impactent, self.target_validate_flags) > 0)
938 self.enemy = self.tur_impactent;
942 if (self.firecheck_flags & TFL_FIRECHECK_DISTANCES)
945 if (self.tur_dist_aimpos < self.target_range_min)
946 if(turret_validate_target(self, self.tur_impactent, self.target_validate_flags) > 0)
947 return 1; // Target of opertunity?
953 if (self.firecheck_flags & TFL_FIRECHECK_AFF)
954 if (self.tur_impactent.team == self.team)
957 // aim<->predicted impact
958 if (self.firecheck_flags & TFL_FIRECHECK_AIMDIST)
959 if (self.tur_dist_impact_to_aimpos > self.aim_firetolerance_dist)
963 if (self.shot_volly > 1)
964 if (self.volly_counter == self.shot_volly)
965 if (self.ammo < (self.shot_dmg * self.shot_volly))
968 /*if(self.firecheck_flags & TFL_FIRECHECK_VERIFIED)
969 if(self.tur_impactent != self.enemy)
975 bool turret_checkfire()
977 bool ret = false; // dummy
978 if(MUTATOR_CALLHOOK(Turret_CheckFire, ret))
981 return self.turret_firecheckfunc();
986 if (autocvar_g_turrets_nofire != 0)
989 if(MUTATOR_CALLHOOK(TurretFire, self))
992 TUR_ACTION(self.turretid, TR_ATTACK);
994 self.attack_finished_single = time + self.shot_refire;
995 self.ammo -= self.shot_dmg;
996 self.volly_counter = self.volly_counter - 1;
998 if (self.volly_counter <= 0)
1000 self.volly_counter = self.shot_volly;
1002 if (self.shoot_flags & TFL_SHOOT_CLEARTARGET)
1005 if (self.shot_volly > 1)
1006 self.attack_finished_single = time + self.shot_volly_refire;
1010 if (self.enemy) paint_target3(self.tur_aimpos, 64, self.tur_debug_rvec, self.tur_impacttime + 0.25);
1018 self.nextthink = time + self.ticrate;
1020 // ONS uses somewhat backwards linking.
1026 e = find(world, targetname,self.target);
1031 if (self.team != self.tur_head.team)
1036 if (self.tur_debug_tmr1 < time)
1038 if (self.enemy) paint_target (self.enemy,128,self.tur_debug_rvec,0.9);
1039 paint_target(self,256,self.tur_debug_rvec,0.9);
1040 self.tur_debug_tmr1 = time + 1;
1045 if (!(self.spawnflags & TSF_NO_AMMO_REGEN))
1046 if (self.ammo < self.ammo_max)
1047 self.ammo = min(self.ammo + self.ammo_recharge, self.ammo_max);
1049 // Inactive turrets needs to run the think loop,
1050 // So they can handle animation and wake up if need be.
1057 // This is typicaly used for zaping every target in range
1058 // turret_fusionreactor uses this to recharge friendlys.
1059 if (self.shoot_flags & TFL_SHOOT_HITALLVALID)
1061 // Do a self.turret_fire for every valid target.
1062 e = findradius(self.origin,self.target_range);
1067 if (turret_validate_target(self,e,self.target_validate_flags))
1071 turret_do_updates(self);
1073 if (turret_checkfire())
1082 else if(self.shoot_flags & TFL_SHOOT_CUSTOM)
1084 // This one is doing something.. oddball. assume its handles what needs to be handled.
1087 if(!(self.aim_flags & TFL_AIM_NO))
1088 self.tur_aimpos = turret_aim_generic();
1091 if(!(self.track_flags & TFL_TRACK_NO))
1094 turret_do_updates(self);
1097 if (turret_checkfire())
1102 // Special case for volly always. if it fired once it must compleate the volly.
1103 if(self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
1104 if(self.volly_counter != self.shot_volly)
1106 // Predict or whatnot
1107 if(!(self.aim_flags & TFL_AIM_NO))
1108 self.tur_aimpos = turret_aim_generic();
1111 if(!(self.track_flags & TFL_TRACK_NO))
1114 turret_do_updates(self);
1117 if (turret_checkfire())
1120 TUR_ACTION(self.turretid, TR_THINK);
1125 // Check if we have a vailid enemy, and try to find one if we dont.
1127 // g_turrets_targetscan_maxdelay forces a target re-scan at least this often
1128 float do_target_scan = 0;
1129 if((self.target_select_time + autocvar_g_turrets_targetscan_maxdelay) < time)
1132 // Old target (if any) invalid?
1133 if(self.target_validate_time < time)
1134 if (turret_validate_target(self, self.enemy, self.target_validate_flags) <= 0)
1137 self.target_validate_time = time + 0.5;
1141 // But never more often then g_turrets_targetscan_mindelay!
1142 if (self.target_select_time + autocvar_g_turrets_targetscan_mindelay > time)
1147 self.enemy = turret_select_target();
1148 self.target_select_time = time;
1151 // No target, just go to idle, do any custom stuff and bail.
1152 if (self.enemy == world)
1155 if(!(self.track_flags & TFL_TRACK_NO))
1158 TUR_ACTION(self.turretid, TR_THINK);
1164 self.lip = time + autocvar_g_turrets_aimidle_delay; // Keep track of the last time we had a target.
1167 if(!(self.aim_flags & TFL_AIM_NO))
1168 self.tur_aimpos = turret_aim_generic();
1171 if(!(self.track_flags & TFL_TRACK_NO))
1174 turret_do_updates(self);
1177 if (turret_checkfire())
1181 TUR_ACTION(self.turretid, TR_THINK);
1185 When .used a turret switch team to activator.team.
1186 If activator is world, the turret go inactive.
1190 LOG_TRACE("Turret ",self.netname, " used by ", activator.classname, "\n");
1192 self.team = activator.team;
1195 self.active = ACTIVE_NOT;
1197 self.active = ACTIVE_ACTIVE;
1203 Net_LinkEntity(self, true, 0, turret_send);
1204 self.think = turret_think;
1205 self.nextthink = time;
1206 self.tur_head.effects = EF_NODRAW;
1209 void turrets_manager_think()
1211 self.nextthink = time + 1;
1214 if (autocvar_g_turrets_reloadcvars == 1)
1221 load_unit_settings(e,e.cvar_basename,1);
1222 TUR_ACTION(self.turretid, TR_THINK);
1227 cvar_set("g_turrets_reloadcvars","0");
1231 float turret_initialize(float tur_id)
1233 if(!autocvar_g_turrets)
1237 entity tur = get_turretinfo(tur_id);
1238 if(tur.turretid == 0)
1239 return false; // invalid turret
1241 if(!self.tur_head) { TUR_ACTION(tur_id, TR_PRECACHE); } // if tur_head exists, we can assume this turret re-spawned
1243 e = find(world, classname, "turret_manager");
1247 e.classname = "turret_manager";
1248 e.think = turrets_manager_think;
1249 e.nextthink = time + 2;
1252 if(!(self.spawnflags & TSF_SUSPENDED))
1253 builtin_droptofloor();
1255 self.cvar_basename = tur.cvar_basename;
1256 load_unit_settings(self, self.cvar_basename, 0);
1258 if(!self.team || !teamplay) { self.team = MAX_SHOT_DISTANCE; }
1259 if(!self.ticrate) { self.ticrate = ((self.turret_flags & TUR_FLAG_SUPPORT) ? 0.2 : 0.1); }
1260 if(!self.health) { self.health = 1000; }
1261 if(!self.shot_refire) { self.shot_refire = 1; }
1262 if(!self.tur_shotorg) { self.tur_shotorg = '50 0 50'; }
1263 if(!self.turret_flags) { self.turret_flags = TUR_FLAG_SPLASH | TUR_FLAG_MEDPROJ | TUR_FLAG_PLAYER; }
1264 if(!self.damage_flags) { self.damage_flags = TFL_DMG_YES | TFL_DMG_RETALIATE | TFL_DMG_AIMSHAKE; }
1265 if(!self.aim_flags) { self.aim_flags = TFL_AIM_LEAD | TFL_AIM_SHOTTIMECOMPENSATE; }
1266 if(!self.track_type) { self.track_type = TFL_TRACKTYPE_STEPMOTOR; }
1267 if(!self.track_flags) { self.track_flags = TFL_TRACK_PITCH | TFL_TRACK_ROTATE; }
1268 if(!self.ammo_flags) { self.ammo_flags = TFL_AMMO_ENERGY | TFL_AMMO_RECHARGE; }
1269 if(!self.target_select_flags) { self.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_TEAMCHECK | TFL_TARGETSELECT_RANGELIMITS | TFL_TARGETSELECT_ANGLELIMITS; }
1270 if(!self.firecheck_flags) { self.firecheck_flags = TFL_FIRECHECK_DEAD | TFL_FIRECHECK_DISTANCES | TFL_FIRECHECK_LOS
1271 | TFL_FIRECHECK_AIMDIST | TFL_FIRECHECK_TEAMCHECK | TFL_FIRECHECK_AMMO_OWN | TFL_FIRECHECK_REFIRE; }
1273 if(self.track_type != TFL_TRACKTYPE_STEPMOTOR)
1275 // Fluid / Ineria mode. Looks mutch nicer.
1276 // Can reduce aim preformance alot, needs a bit diffrent aimspeed
1278 self.aim_speed = bound(0.1, ((!self.aim_speed) ? 180 : self.aim_speed), 1000);
1280 if(!self.track_accel_pitch) { self.track_accel_pitch = 0.5; }
1281 if(!self.track_accel_rotate) { self.track_accel_rotate = 0.5; }
1282 if(!self.track_blendrate) { self.track_blendrate = 0.35; }
1285 self.respawntime = max(-1, ((!self.respawntime) ? 60 : self.respawntime));
1286 self.shot_refire = bound(0.01, ((!self.shot_refire) ? 1 : self.shot_refire), 9999);
1287 self.shot_dmg = max(1, ((!self.shot_dmg) ? self.shot_refire * 50 : self.shot_dmg));
1288 self.shot_radius = max(1, ((!self.shot_radius) ? self.shot_dmg * 0.5 : self.shot_radius));
1289 self.shot_speed = max(1, ((!self.shot_speed) ? 2500 : self.shot_speed));
1290 self.shot_spread = bound(0.0001, ((!self.shot_spread) ? 0.0125 : self.shot_spread), 500);
1291 self.shot_force = bound(0.001, ((!self.shot_force) ? self.shot_dmg * 0.5 + self.shot_radius * 0.5 : self.shot_force), 5000);
1292 self.shot_volly = bound(1, ((!self.shot_volly) ? 1 : self.shot_volly), floor(self.ammo_max / self.shot_dmg));
1293 self.shot_volly_refire = bound(self.shot_refire, ((!self.shot_volly_refire) ? self.shot_refire * self.shot_volly : self.shot_volly_refire), 60);
1294 self.target_range = bound(0, ((!self.target_range) ? self.shot_speed * 0.5 : self.target_range), MAX_SHOT_DISTANCE);
1295 self.target_range_min = bound(0, ((!self.target_range_min) ? self.shot_radius * 2 : self.target_range_min), MAX_SHOT_DISTANCE);
1296 self.target_range_optimal = bound(0, ((!self.target_range_optimal) ? self.target_range * 0.5 : self.target_range_optimal), MAX_SHOT_DISTANCE);
1297 self.aim_maxrotate = bound(0, ((!self.aim_maxrotate) ? 90 : self.aim_maxrotate), 360);
1298 self.aim_maxpitch = bound(0, ((!self.aim_maxpitch) ? 20 : self.aim_maxpitch), 90);
1299 self.aim_speed = bound(0.1, ((!self.aim_speed) ? 36 : self.aim_speed), 1000);
1300 self.aim_firetolerance_dist = bound(0.1, ((!self.aim_firetolerance_dist) ? 5 + (self.shot_radius * 2) : self.aim_firetolerance_dist), MAX_SHOT_DISTANCE);
1301 self.target_select_rangebias = bound(-10, ((!self.target_select_rangebias) ? 1 : self.target_select_rangebias), 10);
1302 self.target_select_samebias = bound(-10, ((!self.target_select_samebias) ? 1 : self.target_select_samebias), 10);
1303 self.target_select_anglebias = bound(-10, ((!self.target_select_anglebias) ? 1 : self.target_select_anglebias), 10);
1304 self.target_select_missilebias = bound(-10, ((!self.target_select_missilebias) ? 1 : self.target_select_missilebias), 10);
1305 self.target_select_playerbias = bound(-10, ((!self.target_select_playerbias) ? 1 : self.target_select_playerbias), 10);
1306 self.ammo_max = max(self.shot_dmg, ((!self.ammo_max) ? self.shot_dmg * 10 : self.ammo_max));
1307 self.ammo_recharge = max(0, ((!self.ammo_recharge) ? self.shot_dmg * 0.5 : self.ammo_recharge));
1309 self.turret_flags = TUR_FLAG_ISTURRET | (tur.spawnflags);
1311 if(self.turret_flags & TUR_FLAG_SPLASH)
1312 self.aim_flags |= TFL_AIM_SPLASH;
1314 if(self.turret_flags & TUR_FLAG_MISSILE)
1315 self.target_select_flags |= TFL_TARGETSELECT_MISSILES;
1317 if(self.turret_flags & TUR_FLAG_PLAYER)
1318 self.target_select_flags |= TFL_TARGETSELECT_PLAYERS;
1320 if(self.spawnflags & TSL_NO_RESPAWN)
1321 self.damage_flags |= TFL_DMG_DEATH_NORESPAWN;
1323 if (self.turret_flags & TUR_FLAG_SUPPORT)
1324 self.turret_score_target = turret_targetscore_support;
1326 self.turret_score_target = turret_targetscore_generic;
1330 setmodel(self, tur.model);
1331 setsize(self, tur.mins, tur.maxs);
1333 self.turretid = tur_id;
1334 self.classname = "turret_main";
1335 self.active = ACTIVE_ACTIVE;
1336 self.effects = EF_NODRAW;
1337 self.netname = TUR_NAME(tur_id);
1338 self.ticrate = bound(sys_frametime, self.ticrate, 60);
1339 self.max_health = self.health;
1340 self.target_validate_flags = self.target_select_flags;
1341 self.ammo = self.ammo_max;
1342 self.ammo_recharge *= self.ticrate;
1343 self.solid = SOLID_BBOX;
1344 self.takedamage = DAMAGE_AIM;
1345 self.movetype = MOVETYPE_NOCLIP;
1346 self.view_ofs = '0 0 0';
1347 self.turret_firecheckfunc = turret_firecheck;
1348 self.event_damage = turret_damage;
1349 self.use = turret_use;
1350 self.bot_attack = true;
1351 self.nextthink = time + 1;
1352 self.nextthink += turret_count * sys_frametime;
1354 self.tur_head = spawn();
1355 setmodel(self.tur_head, tur.head_model);
1356 setsize(self.tur_head, '0 0 0', '0 0 0');
1357 setorigin(self.tur_head, '0 0 0');
1358 setattachment(self.tur_head, self, "tag_head");
1360 self.tur_head.netname = self.tur_head.classname = "turret_head";
1361 self.tur_head.team = self.team;
1362 self.tur_head.owner = self;
1363 self.tur_head.takedamage = DAMAGE_NO;
1364 self.tur_head.solid = SOLID_NOT;
1365 self.tur_head.movetype = self.movetype;
1367 if(!self.tur_defend)
1368 if(self.target != "")
1370 self.tur_defend = find(world, targetname, self.target);
1371 if (self.tur_defend == world)
1374 LOG_TRACE("Turret has invalid defendpoint!\n");
1378 if (self.tur_defend)
1379 self.idle_aim = self.tur_head.angles + angleofs(self.tur_head, self.tur_defend);
1381 self.idle_aim = '0 0 0';
1384 self.tur_debug_start = self.nextthink;
1385 while (vlen(self.tur_debug_rvec) < 2)
1386 self.tur_debug_rvec = randomvec() * 4;
1388 self.tur_debug_rvec_x = fabs(self.tur_debug_rvec_x);
1389 self.tur_debug_rvec_y = fabs(self.tur_debug_rvec_y);
1390 self.tur_debug_rvec_z = fabs(self.tur_debug_rvec_z);
1395 turret_tag_fire_update();
1397 TUR_ACTION(tur_id, TR_SETUP);
1399 if(MUTATOR_CALLHOOK(TurretSpawn, self))