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[0] - 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(!IS_ONGROUND(self.enemy))
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(vdist((_turret.tur_shotorg - real_origin(_target)), >, _turret.target_range))
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 Turret tur = get_turretinfo(self.m_id);
192 if(self.damage_flags & TFL_DMG_DEATH_NORESPAWN)
194 tur.tr_death(tur, self);
196 remove(self.tur_head);
202 self.SendFlags |= TNSF_STATUS;
203 self.nextthink = time + 0.2;
204 self.think = turret_hide;
206 tur.tr_death(tur, self);
210 void turret_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector vforce)
213 if(this.deadflag == DEAD_DEAD)
216 // Inactive turrets take no damage. (hm..)
220 if(SAME_TEAM(this, attacker))
222 if(autocvar_g_friendlyfire)
223 damage = damage * autocvar_g_friendlyfire;
228 this.health -= damage;
230 // thorw head slightly off aim when hit?
231 if (this.damage_flags & TFL_DMG_HEADSHAKE)
233 this.tur_head.angles_x = this.tur_head.angles_x + (-0.5 + random()) * damage;
234 this.tur_head.angles_y = this.tur_head.angles_y + (-0.5 + random()) * damage;
236 this.SendFlags |= TNSF_ANG;
239 if (this.turret_flags & TUR_FLAG_MOVE)
240 this.velocity = this.velocity + vforce;
242 if (this.health <= 0)
244 this.event_damage = func_null;
245 this.tur_head.event_damage = func_null;
246 this.takedamage = DAMAGE_NO;
247 this.nextthink = time;
248 this.think = turret_die;
251 this.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 Turret tur = get_turretinfo(self.m_id);
280 tur.tr_setup(tur, self);
285 #define cvar_base "g_turrets_unit_"
287 void turrets_setframe(float _frame, float client_only)
289 if((client_only ? self.clientframe : self.frame ) != _frame)
291 self.SendFlags |= TNSF_ANIM;
292 self.anim_start_time = time;
296 self.clientframe = _frame;
302 bool turret_send(entity this, entity to, float sf)
305 WriteHeader(MSG_ENTITY, ENT_CLIENT_TURRET);
306 WriteByte(MSG_ENTITY, sf);
309 WriteByte(MSG_ENTITY, self.m_id);
311 WriteCoord(MSG_ENTITY, self.origin_x);
312 WriteCoord(MSG_ENTITY, self.origin_y);
313 WriteCoord(MSG_ENTITY, self.origin_z);
315 WriteAngle(MSG_ENTITY, self.angles_x);
316 WriteAngle(MSG_ENTITY, self.angles_y);
321 WriteShort(MSG_ENTITY, rint(self.tur_head.angles_x));
322 WriteShort(MSG_ENTITY, rint(self.tur_head.angles_y));
327 WriteShort(MSG_ENTITY, rint(self.tur_head.avelocity_x));
328 WriteShort(MSG_ENTITY, rint(self.tur_head.avelocity_y));
333 WriteShort(MSG_ENTITY, rint(self.origin_x));
334 WriteShort(MSG_ENTITY, rint(self.origin_y));
335 WriteShort(MSG_ENTITY, rint(self.origin_z));
337 WriteShort(MSG_ENTITY, rint(self.velocity_x));
338 WriteShort(MSG_ENTITY, rint(self.velocity_y));
339 WriteShort(MSG_ENTITY, rint(self.velocity_z));
341 WriteShort(MSG_ENTITY, rint(self.angles_y));
346 WriteCoord(MSG_ENTITY, self.anim_start_time);
347 WriteByte(MSG_ENTITY, self.frame);
352 WriteByte(MSG_ENTITY, self.team);
355 WriteByte(MSG_ENTITY, 0);
357 WriteByte(MSG_ENTITY, ceil((self.health / self.max_health) * 255));
363 void load_unit_settings(entity ent, bool is_reload)
365 string unitname = ent.netname;
371 if(!ent.turret_scale_damage) ent.turret_scale_damage = 1;
372 if(!ent.turret_scale_range) ent.turret_scale_range = 1;
373 if(!ent.turret_scale_refire) ent.turret_scale_refire = 1;
374 if(!ent.turret_scale_ammo) ent.turret_scale_ammo = 1;
375 if(!ent.turret_scale_aim) ent.turret_scale_aim = 1;
376 if(!ent.turret_scale_health) ent.turret_scale_health = 1;
377 if(!ent.turret_scale_respawn) ent.turret_scale_respawn = 1;
379 sbase = strcat(cvar_base,unitname);
383 ent.tur_head.avelocity = '0 0 0';
385 ent.tur_head.angles = '0 0 0';
388 ent.health = cvar(strcat(sbase,"_health")) * ent.turret_scale_health;
389 ent.respawntime = cvar(strcat(sbase,"_respawntime")) * ent.turret_scale_respawn;
391 ent.shot_dmg = cvar(strcat(sbase,"_shot_dmg")) * ent.turret_scale_damage;
392 ent.shot_refire = cvar(strcat(sbase,"_shot_refire")) * ent.turret_scale_refire;
393 ent.shot_radius = cvar(strcat(sbase,"_shot_radius")) * ent.turret_scale_damage;
394 ent.shot_speed = cvar(strcat(sbase,"_shot_speed"));
395 ent.shot_spread = cvar(strcat(sbase,"_shot_spread"));
396 ent.shot_force = cvar(strcat(sbase,"_shot_force")) * ent.turret_scale_damage;
397 ent.shot_volly = cvar(strcat(sbase,"_shot_volly"));
398 ent.shot_volly_refire = cvar(strcat(sbase,"_shot_volly_refire")) * ent.turret_scale_refire;
400 ent.target_range = cvar(strcat(sbase,"_target_range")) * ent.turret_scale_range;
401 ent.target_range_min = cvar(strcat(sbase,"_target_range_min")) * ent.turret_scale_range;
402 ent.target_range_optimal = cvar(strcat(sbase,"_target_range_optimal")) * ent.turret_scale_range;
403 //ent.target_range_fire = cvar(strcat(sbase,"_target_range_fire")) * ent.turret_scale_range;
405 ent.target_select_rangebias = cvar(strcat(sbase,"_target_select_rangebias"));
406 ent.target_select_samebias = cvar(strcat(sbase,"_target_select_samebias"));
407 ent.target_select_anglebias = cvar(strcat(sbase,"_target_select_anglebias"));
408 ent.target_select_playerbias = cvar(strcat(sbase,"_target_select_playerbias"));
409 //ent.target_select_fov = cvar(cvar_gets(sbase,"_target_select_fov"));
411 ent.ammo_max = cvar(strcat(sbase,"_ammo_max")) * ent.turret_scale_ammo;
412 ent.ammo_recharge = cvar(strcat(sbase,"_ammo_recharge")) * ent.turret_scale_ammo;
414 ent.aim_firetolerance_dist = cvar(strcat(sbase,"_aim_firetolerance_dist"));
415 ent.aim_speed = cvar(strcat(sbase,"_aim_speed")) * ent.turret_scale_aim;
416 ent.aim_maxrotate = cvar(strcat(sbase,"_aim_maxrot"));
417 ent.aim_maxpitch = cvar(strcat(sbase,"_aim_maxpitch"));
419 ent.track_type = cvar(strcat(sbase,"_track_type"));
420 ent.track_accel_pitch = cvar(strcat(sbase,"_track_accel_pitch"));
421 ent.track_accel_rotate = cvar(strcat(sbase,"_track_accel_rot"));
422 ent.track_blendrate = cvar(strcat(sbase,"_track_blendrate"));
425 Turret tur = get_turretinfo(self.m_id);
426 tur.tr_setup(tur, self);
430 void turret_projectile_explode()
433 self.takedamage = DAMAGE_NO;
434 self.event_damage = func_null;
437 d = RadiusDamage (self, self.owner, self.owner.shot_dmg, 0, self.owner.shot_radius, self, world, self.owner.shot_force, self.totalfrags, world);
438 self.owner.tur_debug_dmg_t_h = self.owner.tur_debug_dmg_t_h + d;
439 self.owner.tur_debug_dmg_t_f = self.owner.tur_debug_dmg_t_f + self.owner.shot_dmg;
441 RadiusDamage (self, self.realowner, self.owner.shot_dmg, 0, self.owner.shot_radius, self, world, self.owner.shot_force, self.totalfrags, world);
446 void turret_projectile_touch()
449 turret_projectile_explode();
452 void turret_projectile_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector vforce)
454 this.velocity += vforce;
455 this.health -= damage;
456 //this.realowner = attacker; // Dont change realowner, it does not make much sense for turrets
458 W_PrepareExplosionByDamage(this, this.owner, turret_projectile_explode);
461 entity turret_projectile(Sound _snd, float _size, float _health, float _death, float _proj_type, float _cull, float _cli_anim)
466 sound (self, CH_WEAPON_A, _snd, VOL_BASE, ATTEN_NORM);
468 setorigin(proj, self.tur_shotorg);
469 setsize(proj, '-0.5 -0.5 -0.5' * _size, '0.5 0.5 0.5' * _size);
471 proj.realowner = self;
472 proj.bot_dodge = true;
473 proj.bot_dodgerating = self.shot_dmg;
474 proj.think = turret_projectile_explode;
475 settouch(proj, turret_projectile_touch);
476 proj.nextthink = time + 9;
477 proj.movetype = MOVETYPE_FLYMISSILE;
478 proj.velocity = normalize(self.tur_shotdir_updated + randomvec() * self.shot_spread) * self.shot_speed;
479 proj.flags = FL_PROJECTILE;
480 proj.enemy = self.enemy;
481 proj.totalfrags = _death;
482 PROJECTILE_MAKETRIGGER(proj);
485 proj.health = _health;
486 proj.takedamage = DAMAGE_YES;
487 proj.event_damage = turret_projectile_damage;
490 proj.flags |= FL_NOTARGET;
492 CSQCProjectile(proj, _cli_anim, _proj_type, _cull);
498 ** updates enemy distances, predicted impact point/time
499 ** and updated aim<->predict impact distance.
501 void turret_do_updates(entity t_turret)
503 vector enemy_pos = real_origin(t_turret.enemy);
505 WITHSELF(t_turret, turret_tag_fire_update());
507 t_turret.tur_shotdir_updated = v_forward;
508 t_turret.tur_dist_enemy = vlen(t_turret.tur_shotorg - enemy_pos);
509 t_turret.tur_dist_aimpos = vlen(t_turret.tur_shotorg - t_turret.tur_aimpos);
511 /*if((t_turret.firecheck_flags & TFL_FIRECHECK_VERIFIED) && (t_turret.enemy))
513 oldpos = t_turret.enemy.origin;
514 setorigin(t_turret.enemy, t_turret.tur_aimpos);
515 tracebox(t_turret.tur_shotorg, '-1 -1 -1', '1 1 1', t_turret.tur_shotorg + (t_turret.tur_shotdir_updated * t_turret.tur_dist_aimpos), MOVE_NORMAL,t_turret);
516 setorigin(t_turret.enemy, oldpos);
518 if(trace_ent == t_turret.enemy)
519 t_turret.tur_dist_impact_to_aimpos = 0;
521 t_turret.tur_dist_impact_to_aimpos = vlen(trace_endpos - t_turret.tur_aimpos);
524 tracebox(t_turret.tur_shotorg, '-1 -1 -1','1 1 1', t_turret.tur_shotorg + (t_turret.tur_shotdir_updated * t_turret.tur_dist_aimpos), MOVE_NORMAL,t_turret);
526 t_turret.tur_dist_impact_to_aimpos = vlen(trace_endpos - t_turret.tur_aimpos) - (vlen(t_turret.enemy.maxs - t_turret.enemy.mins) * 0.5);
527 t_turret.tur_impactent = trace_ent;
528 t_turret.tur_impacttime = vlen(t_turret.tur_shotorg - trace_endpos) / t_turret.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)
706 if(MUTATOR_CALLHOOK(TurretValidateTarget, e_turret, e_target, validate_flags))
709 if (validate_flags & TFL_TARGETSELECT_NO)
712 // If only this was used more..
713 if (e_target.flags & FL_NOTARGET)
717 if(IS_VEHICLE(e_target))
719 if (e_target.vehicle_health <= 0)
722 else if (e_target.health <= 0)
724 else if(STAT(FROZEN, e_target) > 0)
728 if (IS_CLIENT(e_target))
730 if(!(validate_flags & TFL_TARGETSELECT_PLAYERS))
733 if (IS_DEAD(e_target))
738 if(validate_flags & TFL_TARGETSELECT_NOTURRETS)
739 if(e_target.owner.tur_head == e_target)
740 if(e_target.team != e_turret.team) // Dont break support units.
744 if (e_target.flags & FL_PROJECTILE)
745 if(!(validate_flags & TFL_TARGETSELECT_MISSILES))
748 if (validate_flags & TFL_TARGETSELECT_MISSILESONLY)
749 if(!(e_target.flags & FL_PROJECTILE))
753 if (validate_flags & TFL_TARGETSELECT_TEAMCHECK)
755 if (validate_flags & TFL_TARGETSELECT_OWNTEAM)
757 if (e_target.team != e_turret.team)
760 if (e_turret.team != e_target.owner.team)
765 if (e_target.team == e_turret.team)
768 if (e_turret.team == e_target.owner.team)
774 tvt_dist = vlen(e_turret.origin - real_origin(e_target));
775 if (validate_flags & TFL_TARGETSELECT_RANGELIMITS)
777 if (tvt_dist < e_turret.target_range_min)
780 if (tvt_dist > e_turret.target_range)
784 // Can we even aim this thing?
785 tvt_thadv = angleofs3(e_turret.tur_head.origin, e_turret.angles + e_turret.tur_head.angles, e_target);
786 tvt_tadv = shortangle_vxy(angleofs(e_turret, e_target), e_turret.angles);
787 tvt_thadf = vlen(tvt_thadv);
788 tvt_tadf = vlen(tvt_tadv);
791 if(validate_flags & TFL_TARGETSELECT_FOV)
793 if(e_turret.target_select_fov < tvt_thadf)
798 if (validate_flags & TFL_TARGETSELECT_ANGLELIMITS)
800 if (fabs(tvt_tadv_x) > e_turret.aim_maxpitch)
803 if (fabs(tvt_tadv_y) > e_turret.aim_maxrotate)
808 if (validate_flags & TFL_TARGETSELECT_LOS)
810 v_tmp = real_origin(e_target) + ((e_target.mins + e_target.maxs) * 0.5);
812 traceline(e_turret.origin + '0 0 16', v_tmp, 0, e_turret);
814 if(vdist(v_tmp - trace_endpos, >, e_turret.aim_firetolerance_dist))
818 if (e_target.classname == "grapplinghook")
822 if (e_target.classname == "func_button")
826 #ifdef TURRET_DEBUG_TARGETSELECT
827 LOG_TRACE("Target:",e_target.netname," is a valid target for ",e_turret.netname,"\n");
833 entity turret_select_target()
835 entity e; // target looper entity
836 float score; // target looper entity score
837 entity e_enemy; // currently best scoreing target
838 float m_score; // currently best scoreing target's score
841 if(self.enemy && self.enemy.takedamage && turret_validate_target(self,self.enemy,self.target_validate_flags) > 0)
843 e_enemy = self.enemy;
844 m_score = self.turret_score_target(self,e_enemy) * self.target_select_samebias;
847 e_enemy = self.enemy = world;
849 e = findradius(self.origin, self.target_range);
851 // Nothing to aim at?
859 float f = turret_validate_target(self, e, self.target_select_flags);
860 //dprint("F is: ", ftos(f), "\n");
863 score = self.turret_score_target(self,e);
864 if ((score > m_score) && (score > 0))
883 + TFL_FIRECHECK_WORLD
885 + TFL_FIRECHECK_DISTANCES
887 + TFL_FIRECHECK_AIMDIST
888 + TFL_FIRECHECK_REALDIST
889 - TFL_FIRECHECK_ANGLEDIST
890 - TFL_FIRECHECK_TEAMCECK
892 + TFL_FIRECHECK_AMMO_OWN
893 + TFL_FIRECHECK_AMMO_OTHER
894 + TFL_FIRECHECK_REFIRE
898 ** Preforms pre-fire checks based on the uints firecheck_flags
900 float turret_firecheck()
902 // This one just dont care =)
903 if (self.firecheck_flags & TFL_FIRECHECK_NO)
906 if (self.enemy == world)
910 if (self.firecheck_flags & TFL_FIRECHECK_REFIRE)
911 if (self.attack_finished_single[0] > time) return 0;
913 // Special case: volly fire turret that has to fire a full volly if a shot was fired.
914 if (self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
915 if (self.volly_counter != self.shot_volly)
916 if(self.ammo >= self.shot_dmg)
919 // Lack of zombies makes shooting dead things unnecessary :P
920 if (self.firecheck_flags & TFL_FIRECHECK_DEAD)
921 if (IS_DEAD(self.enemy))
925 if (self.firecheck_flags & TFL_FIRECHECK_AMMO_OWN)
926 if (self.ammo < self.shot_dmg)
929 // Other's ammo? (support-supply units)
930 if (self.firecheck_flags & TFL_FIRECHECK_AMMO_OTHER)
931 if (self.enemy.ammo >= self.enemy.ammo_max)
934 // Target of opertunity?
935 if(turret_validate_target(self, self.tur_impactent, self.target_validate_flags) > 0)
937 self.enemy = self.tur_impactent;
941 if (self.firecheck_flags & TFL_FIRECHECK_DISTANCES)
944 if (self.tur_dist_aimpos < self.target_range_min)
945 if(turret_validate_target(self, self.tur_impactent, self.target_validate_flags) > 0)
946 return 1; // Target of opertunity?
952 if (self.firecheck_flags & TFL_FIRECHECK_AFF)
953 if (self.tur_impactent.team == self.team)
956 // aim<->predicted impact
957 if (self.firecheck_flags & TFL_FIRECHECK_AIMDIST)
958 if (self.tur_dist_impact_to_aimpos > self.aim_firetolerance_dist)
962 if (self.shot_volly > 1)
963 if (self.volly_counter == self.shot_volly)
964 if (self.ammo < (self.shot_dmg * self.shot_volly))
967 /*if(self.firecheck_flags & TFL_FIRECHECK_VERIFIED)
968 if(self.tur_impactent != self.enemy)
974 bool turret_checkfire()
976 bool ret = false; // dummy
977 if(MUTATOR_CALLHOOK(Turret_CheckFire, ret))
980 return self.turret_firecheckfunc();
985 if (autocvar_g_turrets_nofire != 0)
988 if(MUTATOR_CALLHOOK(TurretFire, self))
991 Turret info = get_turretinfo(self.m_id);
992 info.tr_attack(info, self);
994 self.attack_finished_single[0] = 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[0] = time + self.shot_volly_refire;
1010 if (self.enemy) paint_target3(self.tur_aimpos, 64, self.tur_debug_rvec, self.tur_impacttime + 0.25);
1016 self.nextthink = time + self.ticrate;
1018 MUTATOR_CALLHOOK(TurretThink, self);
1021 if (self.tur_debug_tmr1 < time)
1023 if (self.enemy) paint_target (self.enemy,128,self.tur_debug_rvec,0.9);
1024 paint_target(self,256,self.tur_debug_rvec,0.9);
1025 self.tur_debug_tmr1 = time + 1;
1030 if (!(self.spawnflags & TSF_NO_AMMO_REGEN))
1031 if (self.ammo < self.ammo_max)
1032 self.ammo = min(self.ammo + self.ammo_recharge, self.ammo_max);
1034 // Inactive turrets needs to run the think loop,
1035 // So they can handle animation and wake up if need be.
1042 // This is typicaly used for zaping every target in range
1043 // turret_fusionreactor uses this to recharge friendlys.
1044 if (self.shoot_flags & TFL_SHOOT_HITALLVALID)
1046 // Do a self.turret_fire for every valid target.
1047 entity e = findradius(self.origin,self.target_range);
1052 if (turret_validate_target(self,e,self.target_validate_flags))
1056 turret_do_updates(self);
1058 if (turret_checkfire())
1067 else if(self.shoot_flags & TFL_SHOOT_CUSTOM)
1069 // This one is doing something.. oddball. assume its handles what needs to be handled.
1072 if(!(self.aim_flags & TFL_AIM_NO))
1073 self.tur_aimpos = turret_aim_generic();
1076 if(!(self.track_flags & TFL_TRACK_NO))
1079 turret_do_updates(self);
1082 if (turret_checkfire())
1087 // Special case for volly always. if it fired once it must compleate the volly.
1088 if(self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
1089 if(self.volly_counter != self.shot_volly)
1091 // Predict or whatnot
1092 if(!(self.aim_flags & TFL_AIM_NO))
1093 self.tur_aimpos = turret_aim_generic();
1096 if(!(self.track_flags & TFL_TRACK_NO))
1099 turret_do_updates(self);
1102 if (turret_checkfire())
1105 Turret tur = get_turretinfo(self.m_id);
1106 tur.tr_think(tur, self);
1111 // Check if we have a vailid enemy, and try to find one if we dont.
1113 // g_turrets_targetscan_maxdelay forces a target re-scan at least this often
1114 float do_target_scan = 0;
1115 if((self.target_select_time + autocvar_g_turrets_targetscan_maxdelay) < time)
1118 // Old target (if any) invalid?
1119 if(self.target_validate_time < time)
1120 if (turret_validate_target(self, self.enemy, self.target_validate_flags) <= 0)
1123 self.target_validate_time = time + 0.5;
1127 // But never more often then g_turrets_targetscan_mindelay!
1128 if (self.target_select_time + autocvar_g_turrets_targetscan_mindelay > time)
1133 self.enemy = turret_select_target();
1134 self.target_select_time = time;
1137 // No target, just go to idle, do any custom stuff and bail.
1138 if (self.enemy == world)
1141 if(!(self.track_flags & TFL_TRACK_NO))
1144 Turret tur = get_turretinfo(self.m_id);
1145 tur.tr_think(tur, self);
1151 self.lip = time + autocvar_g_turrets_aimidle_delay; // Keep track of the last time we had a target.
1154 if(!(self.aim_flags & TFL_AIM_NO))
1155 self.tur_aimpos = turret_aim_generic();
1158 if(!(self.track_flags & TFL_TRACK_NO))
1161 turret_do_updates(self);
1164 if (turret_checkfire())
1168 Turret tur = get_turretinfo(self.m_id);
1169 tur.tr_think(tur, self);
1173 When .used a turret switch team to activator.team.
1174 If activator is world, the turret go inactive.
1176 void turret_use(entity this, entity actor, entity trigger)
1178 LOG_TRACE("Turret ",this.netname, " used by ", actor.classname, "\n");
1180 this.team = actor.team;
1183 this.active = ACTIVE_NOT;
1185 this.active = ACTIVE_ACTIVE;
1191 Net_LinkEntity(self, true, 0, turret_send);
1192 self.think = turret_think;
1193 self.nextthink = time;
1194 self.tur_head.effects = EF_NODRAW;
1197 void turrets_manager_think()
1200 this.nextthink = time + 1;
1202 if (autocvar_g_turrets_reloadcvars == 1)
1204 FOREACH_ENTITY(IS_TURRET(it), {
1205 load_unit_settings(it, true);
1206 Turret tur = get_turretinfo(it.m_id);
1207 tur.tr_think(tur, it);
1209 cvar_set("g_turrets_reloadcvars", "0");
1213 void turret_initparams(entity tur)
1215 #define TRY(x) (x) ? (x)
1216 tur.respawntime = max (-1, (TRY(tur.respawntime) : 60 ));
1217 tur.shot_refire = bound(0.01, (TRY(tur.shot_refire) : 1 ), 9999);
1218 tur.shot_dmg = max (1, (TRY(tur.shot_dmg) : tur.shot_refire * 50 ));
1219 tur.shot_radius = max (1, (TRY(tur.shot_radius) : tur.shot_dmg * 0.5 ));
1220 tur.shot_speed = max (1, (TRY(tur.shot_speed) : 2500 ));
1221 tur.shot_spread = bound(0.0001, (TRY(tur.shot_spread) : 0.0125 ), 500);
1222 tur.shot_force = bound(0.001, (TRY(tur.shot_force) : tur.shot_dmg * 0.5 + tur.shot_radius * 0.5 ), 5000);
1223 tur.shot_volly = bound(1, (TRY(tur.shot_volly) : 1 ), floor(tur.ammo_max / tur.shot_dmg));
1224 tur.shot_volly_refire = bound(tur.shot_refire, (TRY(tur.shot_volly_refire) : tur.shot_refire * tur.shot_volly ), 60);
1225 tur.target_range = bound(0, (TRY(tur.target_range) : tur.shot_speed * 0.5 ), MAX_SHOT_DISTANCE);
1226 tur.target_range_min = bound(0, (TRY(tur.target_range_min) : tur.shot_radius * 2 ), MAX_SHOT_DISTANCE);
1227 tur.target_range_optimal = bound(0, (TRY(tur.target_range_optimal) : tur.target_range * 0.5 ), MAX_SHOT_DISTANCE);
1228 tur.aim_maxrotate = bound(0, (TRY(tur.aim_maxrotate) : 90 ), 360);
1229 tur.aim_maxpitch = bound(0, (TRY(tur.aim_maxpitch) : 20 ), 90);
1230 tur.aim_speed = bound(0.1, (TRY(tur.aim_speed) : 36 ), 1000);
1231 tur.aim_firetolerance_dist = bound(0.1, (TRY(tur.aim_firetolerance_dist) : 5 + (tur.shot_radius * 2) ), MAX_SHOT_DISTANCE);
1232 tur.target_select_rangebias = bound(-10, (TRY(tur.target_select_rangebias) : 1 ), 10);
1233 tur.target_select_samebias = bound(-10, (TRY(tur.target_select_samebias) : 1 ), 10);
1234 tur.target_select_anglebias = bound(-10, (TRY(tur.target_select_anglebias) : 1 ), 10);
1235 tur.target_select_missilebias = bound(-10, (TRY(tur.target_select_missilebias) : 1 ), 10);
1236 tur.target_select_playerbias = bound(-10, (TRY(tur.target_select_playerbias) : 1 ), 10);
1237 tur.ammo_max = max (tur.shot_dmg, (TRY(tur.ammo_max) : tur.shot_dmg * 10 ));
1238 tur.ammo_recharge = max (0, (TRY(tur.ammo_recharge) : tur.shot_dmg * 0.5 ));
1242 float turret_initialize(Turret tur)
1244 if(!autocvar_g_turrets)
1248 return false; // invalid turret
1250 // if tur_head exists, we can assume this turret re-spawned
1251 if(!self.tur_head) {
1252 tur.tr_precache(tur);
1255 entity e = find(world, classname, "turret_manager");
1258 e = new(turret_manager);
1259 e.think = turrets_manager_think;
1260 e.nextthink = time + 2;
1263 if(!(self.spawnflags & TSF_SUSPENDED))
1264 builtin_droptofloor();
1266 self.netname = tur.netname;
1267 load_unit_settings(self, 0);
1269 if(!self.team || !teamplay) { self.team = MAX_SHOT_DISTANCE; }
1270 if(!self.ticrate) { self.ticrate = ((self.turret_flags & TUR_FLAG_SUPPORT) ? 0.2 : 0.1); }
1271 if(!self.health) { self.health = 1000; }
1272 if(!self.shot_refire) { self.shot_refire = 1; }
1273 if(!self.tur_shotorg) { self.tur_shotorg = '50 0 50'; }
1274 if(!self.turret_flags) { self.turret_flags = TUR_FLAG_SPLASH | TUR_FLAG_MEDPROJ | TUR_FLAG_PLAYER; }
1275 if(!self.damage_flags) { self.damage_flags = TFL_DMG_YES | TFL_DMG_RETALIATE | TFL_DMG_AIMSHAKE; }
1276 if(!self.aim_flags) { self.aim_flags = TFL_AIM_LEAD | TFL_AIM_SHOTTIMECOMPENSATE; }
1277 if(!self.track_type) { self.track_type = TFL_TRACKTYPE_STEPMOTOR; }
1278 if(!self.track_flags) { self.track_flags = TFL_TRACK_PITCH | TFL_TRACK_ROTATE; }
1279 if(!self.ammo_flags) { self.ammo_flags = TFL_AMMO_ENERGY | TFL_AMMO_RECHARGE; }
1280 if(!self.target_select_flags) { self.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_TEAMCHECK | TFL_TARGETSELECT_RANGELIMITS | TFL_TARGETSELECT_ANGLELIMITS; }
1281 if(!self.firecheck_flags) { self.firecheck_flags = TFL_FIRECHECK_DEAD | TFL_FIRECHECK_DISTANCES | TFL_FIRECHECK_LOS
1282 | TFL_FIRECHECK_AIMDIST | TFL_FIRECHECK_TEAMCHECK | TFL_FIRECHECK_AMMO_OWN | TFL_FIRECHECK_REFIRE; }
1284 if(self.track_type != TFL_TRACKTYPE_STEPMOTOR)
1286 // Fluid / Ineria mode. Looks mutch nicer.
1287 // Can reduce aim preformance alot, needs a bit diffrent aimspeed
1289 self.aim_speed = bound(0.1, ((!self.aim_speed) ? 180 : self.aim_speed), 1000);
1291 if(!self.track_accel_pitch) { self.track_accel_pitch = 0.5; }
1292 if(!self.track_accel_rotate) { self.track_accel_rotate = 0.5; }
1293 if(!self.track_blendrate) { self.track_blendrate = 0.35; }
1296 turret_initparams(self);
1298 self.turret_flags = TUR_FLAG_ISTURRET | (tur.spawnflags);
1300 if(self.turret_flags & TUR_FLAG_SPLASH)
1301 self.aim_flags |= TFL_AIM_SPLASH;
1303 if(self.turret_flags & TUR_FLAG_MISSILE)
1304 self.target_select_flags |= TFL_TARGETSELECT_MISSILES;
1306 if(self.turret_flags & TUR_FLAG_PLAYER)
1307 self.target_select_flags |= TFL_TARGETSELECT_PLAYERS;
1309 if(self.spawnflags & TSL_NO_RESPAWN)
1310 self.damage_flags |= TFL_DMG_DEATH_NORESPAWN;
1312 if (self.turret_flags & TUR_FLAG_SUPPORT)
1313 self.turret_score_target = turret_targetscore_support;
1315 self.turret_score_target = turret_targetscore_generic;
1319 _setmodel(self, tur.model);
1320 setsize(self, tur.mins, tur.maxs);
1322 self.m_id = tur.m_id;
1323 self.classname = "turret_main";
1324 self.active = ACTIVE_ACTIVE;
1325 self.effects = EF_NODRAW;
1326 self.netname = tur.turret_name;
1327 self.ticrate = bound(sys_frametime, self.ticrate, 60);
1328 self.max_health = self.health;
1329 self.target_validate_flags = self.target_select_flags;
1330 self.ammo = self.ammo_max;
1331 self.ammo_recharge *= self.ticrate;
1332 self.solid = SOLID_BBOX;
1333 self.takedamage = DAMAGE_AIM;
1334 self.movetype = MOVETYPE_NOCLIP;
1335 self.view_ofs = '0 0 0';
1336 self.turret_firecheckfunc = turret_firecheck;
1337 self.event_damage = turret_damage;
1338 self.use = turret_use;
1339 self.bot_attack = true;
1340 self.nextthink = time + 1;
1341 self.nextthink += turret_count * sys_frametime;
1343 self.tur_head = new(turret_head);
1344 _setmodel(self.tur_head, tur.head_model);
1345 setsize(self.tur_head, '0 0 0', '0 0 0');
1346 setorigin(self.tur_head, '0 0 0');
1347 setattachment(self.tur_head, self, "tag_head");
1349 self.tur_head.netname = self.tur_head.classname;
1350 self.tur_head.team = self.team;
1351 self.tur_head.owner = self;
1352 self.tur_head.takedamage = DAMAGE_NO;
1353 self.tur_head.solid = SOLID_NOT;
1354 self.tur_head.movetype = self.movetype;
1356 if(!self.tur_defend)
1357 if(self.target != "")
1359 self.tur_defend = find(world, targetname, self.target);
1360 if (self.tur_defend == world)
1363 LOG_TRACE("Turret has invalid defendpoint!\n");
1367 if (self.tur_defend)
1368 self.idle_aim = self.tur_head.angles + angleofs(self.tur_head, self.tur_defend);
1370 self.idle_aim = '0 0 0';
1373 self.tur_debug_start = self.nextthink;
1374 while(vdist(self.tur_debug_rvec, <, 2))
1375 self.tur_debug_rvec = randomvec() * 4;
1377 self.tur_debug_rvec_x = fabs(self.tur_debug_rvec_x);
1378 self.tur_debug_rvec_y = fabs(self.tur_debug_rvec_y);
1379 self.tur_debug_rvec_z = fabs(self.tur_debug_rvec_z);
1384 turret_tag_fire_update();
1386 tur.tr_setup(tur, self);
1388 if(MUTATOR_CALLHOOK(TurretSpawn, self))