1 #include "sv_turrets.qh"
3 #include <server/autocvars.qh>
6 vector turret_aim_generic(entity this)
10 float distance, impact_time = 0, i, mintime;
12 turret_tag_fire_update(this);
14 if(this.aim_flags & TFL_AIM_SIMPLE)
15 return real_origin(this.enemy);
17 mintime = max(this.attack_finished_single[0] - time,0) + sys_frametime;
20 pre_pos = real_origin(this.enemy);
23 if (this.aim_flags & TFL_AIM_LEAD)
25 if (this.aim_flags & TFL_AIM_SHOTTIMECOMPENSATE) // Need to conpensate for shot traveltime
29 distance = vlen(prep - this.tur_shotorg);
30 impact_time = distance / this.shot_speed;
32 prep = pre_pos + (this.enemy.velocity * (impact_time + mintime));
34 if(this.aim_flags & TFL_AIM_ZPREDICT)
35 if(!IS_ONGROUND(this.enemy))
36 if(this.enemy.move_movetype == MOVETYPE_WALK || this.enemy.move_movetype == MOVETYPE_TOSS || this.enemy.move_movetype == MOVETYPE_BOUNCE)
40 vz = this.enemy.velocity_z;
41 for(i = 0; i < impact_time; i += sys_frametime)
43 vz = vz - (autocvar_sv_gravity * sys_frametime);
44 prep_z = prep_z + vz * sys_frametime;
50 pre_pos = pre_pos + this.enemy.velocity * mintime;
53 if(this.aim_flags & TFL_AIM_SPLASH)
55 //tracebox(pre_pos + '0 0 32',this.enemy.mins,this.enemy.maxs,pre_pos -'0 0 64',MOVE_WORLDONLY,this.enemy);
56 traceline(pre_pos + '0 0 32',pre_pos -'0 0 64',MOVE_WORLDONLY,this.enemy);
57 if(trace_fraction != 1.0)
58 pre_pos = trace_endpos;
64 float turret_targetscore_support(entity _turret,entity _target)
66 float score; // Total score
67 float s_score = 0, d_score;
69 if (_turret.enemy == _target) s_score = 1;
71 d_score = min(_turret.target_range_optimal,tvt_dist) / max(_turret.target_range_optimal,tvt_dist);
73 score = (d_score * _turret.target_select_rangebias) +
74 (s_score * _turret.target_select_samebias);
80 * Generic bias aware score system.
82 float turret_targetscore_generic(entity _turret, entity _target)
84 float d_dist; // Defendmode Distance
85 float score; // Total score
86 float d_score; // Distance score
87 float a_score; // Angular score
88 float m_score = 0; // missile score
89 float p_score = 0; // player score
90 float ikr; // ideal kill range
92 if (_turret.tur_defend)
94 d_dist = vlen(real_origin(_target) - _turret.tur_defend.origin);
95 ikr = vlen(_turret.origin - _turret.tur_defend.origin);
96 d_score = 1 - d_dist / _turret.target_range;
100 // Make a normlized value base on the targets distance from our optimal killzone
101 ikr = _turret.target_range_optimal;
102 d_score = min(ikr, tvt_dist) / max(ikr, tvt_dist);
105 a_score = 1 - tvt_thadf / _turret.aim_maxrotate;
107 if ((_turret.target_select_missilebias > 0) && (_target.flags & FL_PROJECTILE))
110 if ((_turret.target_select_playerbias > 0) && IS_CLIENT(_target))
113 d_score = max(d_score, 0);
114 a_score = max(a_score, 0);
115 m_score = max(m_score, 0);
116 p_score = max(p_score, 0);
118 score = (d_score * _turret.target_select_rangebias) +
119 (a_score * _turret.target_select_anglebias) +
120 (m_score * _turret.target_select_missilebias) +
121 (p_score * _turret.target_select_playerbias);
123 if(vdist((_turret.tur_shotorg - real_origin(_target)), >, _turret.target_range))
130 string sd,sa,sm,sp,ss;
131 string sdt,sat,smt,spt;
134 d_score *= _turret.target_select_rangebias;
137 //sv = ftos(v_score);
138 //v_score *= _turret.target_select_samebias;
139 //svt = ftos(v_score);
142 a_score *= _turret.target_select_anglebias;
146 m_score *= _turret.target_select_missilebias;
150 p_score *= _turret.target_select_playerbias;
155 bprint("^3Target scores^7 \[ ",_turret.netname, " \] ^3for^7 \[ ", _target.netname," \]\n");
156 bprint("^5Range:\[ ",sd, " \]^2+bias:\[ ",sdt," \]\n");
157 bprint("^5Angle:\[ ",sa, " \]^2+bias:\[ ",sat," \]\n");
158 bprint("^5Missile:\[ ",sm," \]^2+bias:\[ ",smt," \]\n");
159 bprint("^5Player:\[ ",sp, " \]^2+bias:\[ ",spt," \]\n");
160 bprint("^3Total (w/bias):\[^1",ss,"\]\n");
167 // Generic damage handling
168 void turret_hide(entity this)
170 this.effects |= EF_NODRAW;
171 this.nextthink = time + this.respawntime - 0.2;
172 setthink(this, turret_respawn);
175 void turret_die(entity this)
177 this.deadflag = DEAD_DEAD;
178 this.tur_head.deadflag = this.deadflag;
180 // Unsolidify and hide real parts
181 this.solid = SOLID_NOT;
182 this.tur_head.solid = this.solid;
184 this.event_damage = func_null;
185 this.takedamage = DAMAGE_NO;
190 //RadiusDamage (this,this, min(this.ammo,50),min(this.ammo,50) * 0.25,250,NULL,min(this.ammo,50)*5,DEATH_TURRET,NULL);
192 Turret tur = get_turretinfo(this.m_id);
193 if(this.damage_flags & TFL_DMG_DEATH_NORESPAWN)
195 // do a simple explosion effect here, since CSQC can't do it on a to-be-removed entity
196 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
197 Send_Effect(EFFECT_ROCKET_EXPLODE, this.origin, '0 0 0', 1);
199 tur.tr_death(tur, this);
201 delete(this.tur_head);
207 this.SendFlags |= TNSF_STATUS;
208 this.nextthink = time + 0.2;
209 setthink(this, turret_hide);
211 tur.tr_death(tur, this);
215 void turret_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector vforce)
218 if(this.deadflag == DEAD_DEAD)
221 // Inactive turrets take no damage. (hm..)
225 if(SAME_TEAM(this, attacker))
227 if(autocvar_g_friendlyfire)
228 damage = damage * autocvar_g_friendlyfire;
233 this.health -= damage;
235 // thorw head slightly off aim when hit?
236 if (this.damage_flags & TFL_DMG_HEADSHAKE)
238 this.tur_head.angles_x = this.tur_head.angles_x + (-0.5 + random()) * damage;
239 this.tur_head.angles_y = this.tur_head.angles_y + (-0.5 + random()) * damage;
241 this.SendFlags |= TNSF_ANG;
244 if (this.turret_flags & TUR_FLAG_MOVE)
245 this.velocity = this.velocity + vforce;
247 if (this.health <= 0)
249 this.event_damage = func_null;
250 this.tur_head.event_damage = func_null;
251 this.takedamage = DAMAGE_NO;
252 this.nextthink = time;
253 setthink(this, turret_die);
256 this.SendFlags |= TNSF_STATUS;
259 void turret_think(entity this);
260 void turret_respawn(entity this)
262 // Make sure all parts belong to the same team since
263 // this function doubles as "teamchange" function.
264 this.tur_head.team = this.team;
265 this.effects &= ~EF_NODRAW;
266 this.deadflag = DEAD_NO;
267 this.effects = EF_LOWPRECISION;
268 this.solid = SOLID_BBOX;
269 this.takedamage = DAMAGE_AIM;
270 this.event_damage = turret_damage;
271 this.avelocity = '0 0 0';
272 this.tur_head.avelocity = this.avelocity;
273 this.tur_head.angles = this.idle_aim;
274 this.health = this.max_health;
276 this.volly_counter = this.shot_volly;
277 this.ammo = this.ammo_max;
279 this.nextthink = time + this.ticrate;
280 setthink(this, turret_think);
282 this.SendFlags = TNSF_FULL_UPDATE;
284 Turret tur = get_turretinfo(this.m_id);
285 tur.tr_setup(tur, this);
290 #define cvar_base "g_turrets_unit_"
292 void turrets_setframe(entity this, float _frame, float client_only)
294 if((client_only ? this.clientframe : this.frame ) != _frame)
296 this.SendFlags |= TNSF_ANIM;
297 this.anim_start_time = time;
301 this.clientframe = _frame;
307 bool turret_send(entity this, entity to, float sf)
310 WriteHeader(MSG_ENTITY, ENT_CLIENT_TURRET);
311 WriteByte(MSG_ENTITY, sf);
314 WriteByte(MSG_ENTITY, this.m_id);
316 WriteCoord(MSG_ENTITY, this.origin_x);
317 WriteCoord(MSG_ENTITY, this.origin_y);
318 WriteCoord(MSG_ENTITY, this.origin_z);
320 WriteAngle(MSG_ENTITY, this.angles_x);
321 WriteAngle(MSG_ENTITY, this.angles_y);
326 WriteShort(MSG_ENTITY, rint(this.tur_head.angles_x));
327 WriteShort(MSG_ENTITY, rint(this.tur_head.angles_y));
332 WriteShort(MSG_ENTITY, rint(this.tur_head.avelocity_x));
333 WriteShort(MSG_ENTITY, rint(this.tur_head.avelocity_y));
338 WriteShort(MSG_ENTITY, rint(this.origin_x));
339 WriteShort(MSG_ENTITY, rint(this.origin_y));
340 WriteShort(MSG_ENTITY, rint(this.origin_z));
342 WriteShort(MSG_ENTITY, rint(this.velocity_x));
343 WriteShort(MSG_ENTITY, rint(this.velocity_y));
344 WriteShort(MSG_ENTITY, rint(this.velocity_z));
346 WriteShort(MSG_ENTITY, rint(this.angles_y));
351 WriteCoord(MSG_ENTITY, this.anim_start_time);
352 WriteByte(MSG_ENTITY, this.frame);
357 WriteByte(MSG_ENTITY, this.team);
360 WriteByte(MSG_ENTITY, 0);
362 WriteByte(MSG_ENTITY, ceil((this.health / this.max_health) * 255));
368 void load_unit_settings(entity ent, bool is_reload)
370 string unitname = ent.netname;
376 if(!ent.turret_scale_damage) ent.turret_scale_damage = 1;
377 if(!ent.turret_scale_range) ent.turret_scale_range = 1;
378 if(!ent.turret_scale_refire) ent.turret_scale_refire = 1;
379 if(!ent.turret_scale_ammo) ent.turret_scale_ammo = 1;
380 if(!ent.turret_scale_aim) ent.turret_scale_aim = 1;
381 if(!ent.turret_scale_health) ent.turret_scale_health = 1;
382 if(!ent.turret_scale_respawn) ent.turret_scale_respawn = 1;
384 sbase = strcat(cvar_base,unitname);
388 ent.tur_head.avelocity = '0 0 0';
390 ent.tur_head.angles = '0 0 0';
393 ent.health = cvar(strcat(sbase,"_health")) * ent.turret_scale_health;
394 ent.respawntime = cvar(strcat(sbase,"_respawntime")) * ent.turret_scale_respawn;
396 ent.shot_dmg = cvar(strcat(sbase,"_shot_dmg")) * ent.turret_scale_damage;
397 ent.shot_refire = cvar(strcat(sbase,"_shot_refire")) * ent.turret_scale_refire;
398 ent.shot_radius = cvar(strcat(sbase,"_shot_radius")) * ent.turret_scale_damage;
399 ent.shot_speed = cvar(strcat(sbase,"_shot_speed"));
400 ent.shot_spread = cvar(strcat(sbase,"_shot_spread"));
401 ent.shot_force = cvar(strcat(sbase,"_shot_force")) * ent.turret_scale_damage;
402 ent.shot_volly = cvar(strcat(sbase,"_shot_volly"));
403 ent.shot_volly_refire = cvar(strcat(sbase,"_shot_volly_refire")) * ent.turret_scale_refire;
405 ent.target_range = cvar(strcat(sbase,"_target_range")) * ent.turret_scale_range;
406 ent.target_range_min = cvar(strcat(sbase,"_target_range_min")) * ent.turret_scale_range;
407 ent.target_range_optimal = cvar(strcat(sbase,"_target_range_optimal")) * ent.turret_scale_range;
408 //ent.target_range_fire = cvar(strcat(sbase,"_target_range_fire")) * ent.turret_scale_range;
410 ent.target_select_rangebias = cvar(strcat(sbase,"_target_select_rangebias"));
411 ent.target_select_samebias = cvar(strcat(sbase,"_target_select_samebias"));
412 ent.target_select_anglebias = cvar(strcat(sbase,"_target_select_anglebias"));
413 ent.target_select_playerbias = cvar(strcat(sbase,"_target_select_playerbias"));
414 //ent.target_select_fov = cvar(cvar_gets(sbase,"_target_select_fov"));
416 ent.ammo_max = cvar(strcat(sbase,"_ammo_max")) * ent.turret_scale_ammo;
417 ent.ammo_recharge = cvar(strcat(sbase,"_ammo_recharge")) * ent.turret_scale_ammo;
419 ent.aim_firetolerance_dist = cvar(strcat(sbase,"_aim_firetolerance_dist"));
420 ent.aim_speed = cvar(strcat(sbase,"_aim_speed")) * ent.turret_scale_aim;
421 ent.aim_maxrotate = cvar(strcat(sbase,"_aim_maxrot"));
422 ent.aim_maxpitch = cvar(strcat(sbase,"_aim_maxpitch"));
424 ent.track_type = cvar(strcat(sbase,"_track_type"));
425 ent.track_accel_pitch = cvar(strcat(sbase,"_track_accel_pitch"));
426 ent.track_accel_rotate = cvar(strcat(sbase,"_track_accel_rot"));
427 ent.track_blendrate = cvar(strcat(sbase,"_track_blendrate"));
430 Turret tur = get_turretinfo(ent.m_id);
431 tur.tr_setup(tur, ent);
435 void turret_projectile_explode(entity this)
438 this.takedamage = DAMAGE_NO;
439 this.event_damage = func_null;
442 d = RadiusDamage (this, this.owner, this.owner.shot_dmg, 0, this.owner.shot_radius, this, NULL, this.owner.shot_force, this.totalfrags, NULL);
443 this.owner.tur_debug_dmg_t_h = this.owner.tur_debug_dmg_t_h + d;
444 this.owner.tur_debug_dmg_t_f = this.owner.tur_debug_dmg_t_f + this.owner.shot_dmg;
446 RadiusDamage (this, this.realowner, this.owner.shot_dmg, 0, this.owner.shot_radius, this, NULL, this.owner.shot_force, this.totalfrags, NULL);
451 void turret_projectile_touch(entity this, entity toucher)
453 PROJECTILE_TOUCH(this, toucher);
454 turret_projectile_explode(this);
457 void turret_projectile_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector vforce)
459 this.velocity += vforce;
460 this.health -= damage;
461 //this.realowner = attacker; // Dont change realowner, it does not make much sense for turrets
463 W_PrepareExplosionByDamage(this, this.owner, turret_projectile_explode);
466 entity turret_projectile(entity actor, Sound _snd, float _size, float _health, float _death, float _proj_type, float _cull, float _cli_anim)
471 sound (actor, CH_WEAPON_A, _snd, VOL_BASE, ATTEN_NORM);
473 setorigin(proj, actor.tur_shotorg);
474 setsize(proj, '-0.5 -0.5 -0.5' * _size, '0.5 0.5 0.5' * _size);
476 proj.realowner = actor;
477 proj.bot_dodge = true;
478 proj.bot_dodgerating = actor.shot_dmg;
479 setthink(proj, turret_projectile_explode);
480 settouch(proj, turret_projectile_touch);
481 proj.nextthink = time + 9;
482 set_movetype(proj, MOVETYPE_FLYMISSILE);
483 proj.velocity = normalize(actor.tur_shotdir_updated + randomvec() * actor.shot_spread) * actor.shot_speed;
484 proj.flags = FL_PROJECTILE;
485 IL_PUSH(g_projectiles, proj);
486 IL_PUSH(g_bot_dodge, proj);
487 proj.enemy = actor.enemy;
488 proj.totalfrags = _death;
489 PROJECTILE_MAKETRIGGER(proj);
492 proj.health = _health;
493 proj.takedamage = DAMAGE_YES;
494 proj.event_damage = turret_projectile_damage;
497 proj.flags |= FL_NOTARGET;
499 CSQCProjectile(proj, _cli_anim, _proj_type, _cull);
505 ** updates enemy distances, predicted impact point/time
506 ** and updated aim<->predict impact distance.
508 void turret_do_updates(entity t_turret)
510 vector enemy_pos = real_origin(t_turret.enemy);
512 turret_tag_fire_update(t_turret);
514 t_turret.tur_shotdir_updated = v_forward;
515 t_turret.tur_dist_enemy = vlen(t_turret.tur_shotorg - enemy_pos);
516 t_turret.tur_dist_aimpos = vlen(t_turret.tur_shotorg - t_turret.tur_aimpos);
518 /*if((t_turret.firecheck_flags & TFL_FIRECHECK_VERIFIED) && (t_turret.enemy))
520 oldpos = t_turret.enemy.origin;
521 setorigin(t_turret.enemy, t_turret.tur_aimpos);
522 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);
523 setorigin(t_turret.enemy, oldpos);
525 if(trace_ent == t_turret.enemy)
526 t_turret.tur_dist_impact_to_aimpos = 0;
528 t_turret.tur_dist_impact_to_aimpos = vlen(trace_endpos - t_turret.tur_aimpos);
531 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);
533 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);
534 t_turret.tur_impactent = trace_ent;
535 t_turret.tur_impacttime = vlen(t_turret.tur_shotorg - trace_endpos) / t_turret.shot_speed;
539 ** Handles head rotation according to
540 ** the units .track_type and .track_flags
542 .float turret_framecounter;
543 void turret_track(entity this)
545 vector target_angle; // This is where we want to aim
546 vector move_angle; // This is where we can aim
549 v1 = this.tur_head.angles;
550 v2 = this.tur_head.avelocity;
552 if (this.track_flags == TFL_TRACK_NO)
556 target_angle = this.idle_aim - ('1 0 0' * this.aim_maxpitch);
557 else if (this.enemy == NULL)
560 target_angle = this.idle_aim + this.angles;
562 target_angle = vectoangles(normalize(this.tur_aimpos - this.tur_shotorg));
566 target_angle = vectoangles(normalize(this.tur_aimpos - this.tur_shotorg));
569 this.tur_head.angles_x = anglemods(this.tur_head.angles_x);
570 this.tur_head.angles_y = anglemods(this.tur_head.angles_y);
572 // Find the diffrence between where we currently aim and where we want to aim
573 //move_angle = target_angle - (this.angles + this.tur_head.angles);
574 //move_angle = shortangle_vxy(move_angle,(this.angles + this.tur_head.angles));
576 move_angle = AnglesTransform_ToAngles(AnglesTransform_LeftDivide(AnglesTransform_FromAngles(this.angles), AnglesTransform_FromAngles(target_angle))) - this.tur_head.angles;
577 move_angle = shortangle_vxy(move_angle, this.tur_head.angles);
579 switch(this.track_type)
581 case TFL_TRACKTYPE_STEPMOTOR:
582 f_tmp = this.aim_speed * this.ticrate; // dgr/sec -> dgr/tic
583 if (this.track_flags & TFL_TRACK_PITCH)
585 this.tur_head.angles_x += bound(-f_tmp,move_angle_x, f_tmp);
586 if(this.tur_head.angles_x > this.aim_maxpitch)
587 this.tur_head.angles_x = this.aim_maxpitch;
589 if(this.tur_head.angles_x < -this.aim_maxpitch)
590 this.tur_head.angles_x = this.aim_maxpitch;
593 if (this.track_flags & TFL_TRACK_ROTATE)
595 this.tur_head.angles_y += bound(-f_tmp, move_angle_y, f_tmp);
596 if(this.tur_head.angles_y > this.aim_maxrotate)
597 this.tur_head.angles_y = this.aim_maxrotate;
599 if(this.tur_head.angles_y < -this.aim_maxrotate)
600 this.tur_head.angles_y = this.aim_maxrotate;
604 this.SendFlags |= TNSF_ANG;
608 case TFL_TRACKTYPE_FLUIDINERTIA:
609 f_tmp = this.aim_speed * this.ticrate; // dgr/sec -> dgr/tic
610 move_angle_x = bound(-this.aim_speed, move_angle_x * this.track_accel_pitch * f_tmp, this.aim_speed);
611 move_angle_y = bound(-this.aim_speed, move_angle_y * this.track_accel_rotate * f_tmp, this.aim_speed);
612 move_angle = (this.tur_head.avelocity * this.track_blendrate) + (move_angle * (1 - this.track_blendrate));
615 case TFL_TRACKTYPE_FLUIDPRECISE:
617 move_angle_y = bound(-this.aim_speed, move_angle_y, this.aim_speed);
618 move_angle_x = bound(-this.aim_speed, move_angle_x, this.aim_speed);
624 if (this.track_flags & TFL_TRACK_PITCH)
626 this.tur_head.avelocity_x = move_angle_x;
627 if((this.tur_head.angles_x + this.tur_head.avelocity_x * this.ticrate) > this.aim_maxpitch)
629 this.tur_head.avelocity_x = 0;
630 this.tur_head.angles_x = this.aim_maxpitch;
632 this.SendFlags |= TNSF_ANG;
635 if((this.tur_head.angles_x + this.tur_head.avelocity_x * this.ticrate) < -this.aim_maxpitch)
637 this.tur_head.avelocity_x = 0;
638 this.tur_head.angles_x = -this.aim_maxpitch;
640 this.SendFlags |= TNSF_ANG;
645 if (this.track_flags & TFL_TRACK_ROTATE)
647 this.tur_head.avelocity_y = move_angle_y;
649 if((this.tur_head.angles_y + this.tur_head.avelocity_y * this.ticrate) > this.aim_maxrotate)
651 this.tur_head.avelocity_y = 0;
652 this.tur_head.angles_y = this.aim_maxrotate;
654 this.SendFlags |= TNSF_ANG;
657 if((this.tur_head.angles_y + this.tur_head.avelocity_y * this.ticrate) < -this.aim_maxrotate)
659 this.tur_head.avelocity_y = 0;
660 this.tur_head.angles_y = -this.aim_maxrotate;
662 this.SendFlags |= TNSF_ANG;
666 this.SendFlags |= TNSF_AVEL;
668 // Force a angle update every 10'th frame
669 this.turret_framecounter += 1;
670 if(this.turret_framecounter >= 10)
672 this.SendFlags |= TNSF_ANG;
673 this.turret_framecounter = 0;
678 + TFL_TARGETSELECT_NO
679 + TFL_TARGETSELECT_LOS
680 + TFL_TARGETSELECT_PLAYERS
681 + TFL_TARGETSELECT_MISSILES
682 - TFL_TARGETSELECT_TRIGGERTARGET
683 + TFL_TARGETSELECT_ANGLELIMITS
684 + TFL_TARGETSELECT_RANGELIMITS
685 + TFL_TARGETSELECT_TEAMCHECK
686 - TFL_TARGETSELECT_NOBUILTIN
687 + TFL_TARGETSELECT_OWNTEAM
691 ** Evaluate a entity for target valitity based on validate_flags
692 ** NOTE: the caller must check takedamage before calling this, to inline this check.
694 float turret_validate_target(entity e_turret, entity e_target, float validate_flags)
698 //if(!validate_flags & TFL_TARGETSELECT_NOBUILTIN)
704 if(e_target.owner == e_turret)
707 if(!checkpvs(e_target.origin, e_turret))
710 if(e_target.alpha <= 0.3)
713 if(MUTATOR_CALLHOOK(TurretValidateTarget, e_turret, e_target, validate_flags))
714 return M_ARGV(3, float);
716 if (validate_flags & TFL_TARGETSELECT_NO)
719 // If only this was used more..
720 if (e_target.flags & FL_NOTARGET)
724 if(IS_VEHICLE(e_target))
726 if (e_target.vehicle_health <= 0)
729 else if (e_target.health <= 0)
731 else if(STAT(FROZEN, e_target) > 0)
735 if (IS_CLIENT(e_target))
737 if(!(validate_flags & TFL_TARGETSELECT_PLAYERS))
740 if (IS_DEAD(e_target))
745 if(validate_flags & TFL_TARGETSELECT_NOTURRETS)
746 if(e_target.owner.tur_head == e_target)
747 if(e_target.team != e_turret.team) // Dont break support units.
751 if (e_target.flags & FL_PROJECTILE)
752 if(!(validate_flags & TFL_TARGETSELECT_MISSILES))
755 if (validate_flags & TFL_TARGETSELECT_MISSILESONLY)
756 if(!(e_target.flags & FL_PROJECTILE))
760 if (validate_flags & TFL_TARGETSELECT_TEAMCHECK)
762 if (validate_flags & TFL_TARGETSELECT_OWNTEAM)
764 if (e_target.team != e_turret.team)
767 if (e_turret.team != e_target.owner.team)
772 if (e_target.team == e_turret.team)
775 if (e_turret.team == e_target.owner.team)
781 tvt_dist = vlen(e_turret.origin - real_origin(e_target));
782 if (validate_flags & TFL_TARGETSELECT_RANGELIMITS)
784 if (tvt_dist < e_turret.target_range_min)
787 if (tvt_dist > e_turret.target_range)
791 // Can we even aim this thing?
792 tvt_thadv = angleofs3(e_turret.tur_head.origin, e_turret.angles + e_turret.tur_head.angles, e_target.origin);
793 tvt_tadv = shortangle_vxy(angleofs(e_turret, e_target), e_turret.angles);
794 tvt_thadf = vlen(tvt_thadv);
795 tvt_tadf = vlen(tvt_tadv);
798 if(validate_flags & TFL_TARGETSELECT_FOV)
800 if(e_turret.target_select_fov < tvt_thadf)
805 if (validate_flags & TFL_TARGETSELECT_ANGLELIMITS)
807 if (fabs(tvt_tadv_x) > e_turret.aim_maxpitch)
810 if (fabs(tvt_tadv_y) > e_turret.aim_maxrotate)
815 if (validate_flags & TFL_TARGETSELECT_LOS)
817 v_tmp = real_origin(e_target) + ((e_target.mins + e_target.maxs) * 0.5);
819 traceline(e_turret.origin + '0 0 16', v_tmp, 0, e_turret);
821 if(vdist(v_tmp - trace_endpos, >, e_turret.aim_firetolerance_dist))
825 if (e_target.classname == "grapplinghook")
829 if (e_target.classname == "func_button")
833 #ifdef TURRET_DEBUG_TARGETSELECT
834 LOG_TRACE("Target:",e_target.netname," is a valid target for ",e_turret.netname);
840 entity turret_select_target(entity this)
842 entity e; // target looper entity
843 float score; // target looper entity score
844 entity e_enemy; // currently best scoreing target
845 float m_score; // currently best scoreing target's score
848 if(this.enemy && this.enemy.takedamage && turret_validate_target(this,this.enemy,this.target_validate_flags) > 0)
850 e_enemy = this.enemy;
851 m_score = this.turret_score_target(this,e_enemy) * this.target_select_samebias;
854 e_enemy = this.enemy = NULL;
856 e = findradius(this.origin, this.target_range);
858 // Nothing to aim at?
866 float f = turret_validate_target(this, e, this.target_select_flags);
867 //dprint("F is: ", ftos(f), "\n");
870 score = this.turret_score_target(this,e);
871 if ((score > m_score) && (score > 0))
890 + TFL_FIRECHECK_WORLD
892 + TFL_FIRECHECK_DISTANCES
894 + TFL_FIRECHECK_AIMDIST
895 + TFL_FIRECHECK_REALDIST
896 - TFL_FIRECHECK_ANGLEDIST
897 - TFL_FIRECHECK_TEAMCECK
899 + TFL_FIRECHECK_AMMO_OWN
900 + TFL_FIRECHECK_AMMO_OTHER
901 + TFL_FIRECHECK_REFIRE
905 ** Preforms pre-fire checks based on the uints firecheck_flags
907 bool turret_firecheck(entity this)
909 // This one just dont care =)
910 if (this.firecheck_flags & TFL_FIRECHECK_NO)
913 if (this.enemy == NULL)
917 if (this.firecheck_flags & TFL_FIRECHECK_REFIRE)
918 if (this.attack_finished_single[0] > time) return false;
920 // Special case: volly fire turret that has to fire a full volly if a shot was fired.
921 if (this.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
922 if (this.volly_counter != this.shot_volly)
923 if(this.ammo >= this.shot_dmg)
926 // Lack of zombies makes shooting dead things unnecessary :P
927 if (this.firecheck_flags & TFL_FIRECHECK_DEAD)
928 if (IS_DEAD(this.enemy))
932 if (this.firecheck_flags & TFL_FIRECHECK_AMMO_OWN)
933 if (this.ammo < this.shot_dmg)
936 // Other's ammo? (support-supply units)
937 if (this.firecheck_flags & TFL_FIRECHECK_AMMO_OTHER)
938 if (this.enemy.ammo >= this.enemy.ammo_max)
941 // Target of opertunity?
942 if(turret_validate_target(this, this.tur_impactent, this.target_validate_flags) > 0)
944 this.enemy = this.tur_impactent;
948 if (this.firecheck_flags & TFL_FIRECHECK_DISTANCES)
951 if (this.tur_dist_aimpos < this.target_range_min)
952 if(turret_validate_target(this, this.tur_impactent, this.target_validate_flags) > 0)
953 return true; // Target of opertunity?
959 if (this.firecheck_flags & TFL_FIRECHECK_AFF)
960 if (this.tur_impactent.team == this.team)
963 // aim<->predicted impact
964 if (this.firecheck_flags & TFL_FIRECHECK_AIMDIST)
965 if (this.tur_dist_impact_to_aimpos > this.aim_firetolerance_dist)
969 if (this.shot_volly > 1)
970 if (this.volly_counter == this.shot_volly)
971 if (this.ammo < (this.shot_dmg * this.shot_volly))
974 /*if(this.firecheck_flags & TFL_FIRECHECK_VERIFIED)
975 if(this.tur_impactent != this.enemy)
981 bool turret_checkfire(entity this)
983 if(MUTATOR_CALLHOOK(Turret_CheckFire, this))
984 return M_ARGV(1, bool);
986 return this.turret_firecheckfunc(this);
989 void turret_fire(entity this)
991 if (autocvar_g_turrets_nofire != 0)
994 if(MUTATOR_CALLHOOK(TurretFire, this))
997 Turret info = get_turretinfo(this.m_id);
998 info.tr_attack(info, this);
1000 this.attack_finished_single[0] = time + this.shot_refire;
1001 this.ammo -= this.shot_dmg;
1002 this.volly_counter = this.volly_counter - 1;
1004 if (this.volly_counter <= 0)
1006 this.volly_counter = this.shot_volly;
1008 if (this.shoot_flags & TFL_SHOOT_CLEARTARGET)
1011 if (this.shot_volly > 1)
1012 this.attack_finished_single[0] = time + this.shot_volly_refire;
1016 if (this.enemy) paint_target3(this.tur_aimpos, 64, this.tur_debug_rvec, this.tur_impacttime + 0.25);
1020 void turret_think(entity this)
1022 this.nextthink = time + this.ticrate;
1024 MUTATOR_CALLHOOK(TurretThink, this);
1027 if (this.tur_debug_tmr1 < time)
1029 if (this.enemy) paint_target (this.enemy,128,this.tur_debug_rvec,0.9);
1030 paint_target(this,256,this.tur_debug_rvec,0.9);
1031 this.tur_debug_tmr1 = time + 1;
1036 if (!(this.spawnflags & TSF_NO_AMMO_REGEN))
1037 if (this.ammo < this.ammo_max)
1038 this.ammo = min(this.ammo + this.ammo_recharge, this.ammo_max);
1040 // Inactive turrets needs to run the think loop,
1041 // So they can handle animation and wake up if need be.
1048 // This is typicaly used for zaping every target in range
1049 // turret_fusionreactor uses this to recharge friendlys.
1050 if (this.shoot_flags & TFL_SHOOT_HITALLVALID)
1052 // Do a this.turret_fire for every valid target.
1053 entity e = findradius(this.origin,this.target_range);
1058 if (turret_validate_target(this,e,this.target_validate_flags))
1062 turret_do_updates(this);
1064 if (turret_checkfire(this))
1073 else if(this.shoot_flags & TFL_SHOOT_CUSTOM)
1075 // This one is doing something.. oddball. assume its handles what needs to be handled.
1078 if(!(this.aim_flags & TFL_AIM_NO))
1079 this.tur_aimpos = turret_aim_generic(this);
1082 if(!(this.track_flags & TFL_TRACK_NO))
1085 turret_do_updates(this);
1088 if (turret_checkfire(this))
1093 // Special case for volly always. if it fired once it must compleate the volly.
1094 if(this.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
1095 if(this.volly_counter != this.shot_volly)
1097 // Predict or whatnot
1098 if(!(this.aim_flags & TFL_AIM_NO))
1099 this.tur_aimpos = turret_aim_generic(this);
1102 if(!(this.track_flags & TFL_TRACK_NO))
1105 turret_do_updates(this);
1108 if (turret_checkfire(this))
1111 Turret tur = get_turretinfo(this.m_id);
1112 tur.tr_think(tur, this);
1117 // Check if we have a vailid enemy, and try to find one if we dont.
1119 // g_turrets_targetscan_maxdelay forces a target re-scan at least this often
1120 float do_target_scan = 0;
1121 if((this.target_select_time + autocvar_g_turrets_targetscan_maxdelay) < time)
1124 // Old target (if any) invalid?
1125 if(this.target_validate_time < time)
1126 if (turret_validate_target(this, this.enemy, this.target_validate_flags) <= 0)
1129 this.target_validate_time = time + 0.5;
1133 // But never more often then g_turrets_targetscan_mindelay!
1134 if (this.target_select_time + autocvar_g_turrets_targetscan_mindelay > time)
1139 this.enemy = turret_select_target(this);
1140 this.target_select_time = time;
1143 // No target, just go to idle, do any custom stuff and bail.
1144 if (this.enemy == NULL)
1147 if(!(this.track_flags & TFL_TRACK_NO))
1150 Turret tur = get_turretinfo(this.m_id);
1151 tur.tr_think(tur, this);
1157 this.lip = time + autocvar_g_turrets_aimidle_delay; // Keep track of the last time we had a target.
1160 if(!(this.aim_flags & TFL_AIM_NO))
1161 this.tur_aimpos = turret_aim_generic(this);
1164 if(!(this.track_flags & TFL_TRACK_NO))
1167 turret_do_updates(this);
1170 if (turret_checkfire(this))
1174 Turret tur = get_turretinfo(this.m_id);
1175 tur.tr_think(tur, this);
1179 When .used a turret switch team to activator.team.
1180 If activator is NULL, the turret go inactive.
1182 void turret_use(entity this, entity actor, entity trigger)
1184 LOG_TRACE("Turret ",this.netname, " used by ", actor.classname);
1186 this.team = actor.team;
1189 this.active = ACTIVE_NOT;
1191 this.active = ACTIVE_ACTIVE;
1195 void turret_link(entity this)
1197 Net_LinkEntity(this, true, 0, turret_send);
1198 setthink(this, turret_think);
1199 this.nextthink = time;
1200 this.tur_head.effects = EF_NODRAW;
1203 void turrets_manager_think(entity this)
1205 this.nextthink = time + 1;
1207 if (autocvar_g_turrets_reloadcvars == 1)
1209 IL_EACH(g_turrets, true,
1211 load_unit_settings(it, true);
1212 Turret tur = get_turretinfo(it.m_id);
1213 tur.tr_think(tur, it);
1215 cvar_set("g_turrets_reloadcvars", "0");
1219 void turret_initparams(entity tur)
1221 #define TRY(x) (x) ? (x)
1222 tur.respawntime = max (-1, (TRY(tur.respawntime) : 60 ));
1223 tur.shot_refire = bound(0.01, (TRY(tur.shot_refire) : 1 ), 9999);
1224 tur.shot_dmg = max (1, (TRY(tur.shot_dmg) : tur.shot_refire * 50 ));
1225 tur.shot_radius = max (1, (TRY(tur.shot_radius) : tur.shot_dmg * 0.5 ));
1226 tur.shot_speed = max (1, (TRY(tur.shot_speed) : 2500 ));
1227 tur.shot_spread = bound(0.0001, (TRY(tur.shot_spread) : 0.0125 ), 500);
1228 tur.shot_force = bound(0.001, (TRY(tur.shot_force) : tur.shot_dmg * 0.5 + tur.shot_radius * 0.5 ), 5000);
1229 tur.shot_volly = bound(1, (TRY(tur.shot_volly) : 1 ), floor(tur.ammo_max / tur.shot_dmg));
1230 tur.shot_volly_refire = bound(tur.shot_refire, (TRY(tur.shot_volly_refire) : tur.shot_refire * tur.shot_volly ), 60);
1231 tur.target_range = bound(0, (TRY(tur.target_range) : tur.shot_speed * 0.5 ), max_shot_distance);
1232 tur.target_range_min = bound(0, (TRY(tur.target_range_min) : tur.shot_radius * 2 ), max_shot_distance);
1233 tur.target_range_optimal = bound(0, (TRY(tur.target_range_optimal) : tur.target_range * 0.5 ), max_shot_distance);
1234 tur.aim_maxrotate = bound(0, (TRY(tur.aim_maxrotate) : 90 ), 360);
1235 tur.aim_maxpitch = bound(0, (TRY(tur.aim_maxpitch) : 20 ), 90);
1236 tur.aim_speed = bound(0.1, (TRY(tur.aim_speed) : 36 ), 1000);
1237 tur.aim_firetolerance_dist = bound(0.1, (TRY(tur.aim_firetolerance_dist) : 5 + (tur.shot_radius * 2) ), max_shot_distance);
1238 tur.target_select_rangebias = bound(-10, (TRY(tur.target_select_rangebias) : 1 ), 10);
1239 tur.target_select_samebias = bound(-10, (TRY(tur.target_select_samebias) : 1 ), 10);
1240 tur.target_select_anglebias = bound(-10, (TRY(tur.target_select_anglebias) : 1 ), 10);
1241 tur.target_select_missilebias = bound(-10, (TRY(tur.target_select_missilebias) : 1 ), 10);
1242 tur.target_select_playerbias = bound(-10, (TRY(tur.target_select_playerbias) : 1 ), 10);
1243 tur.ammo_max = max (tur.shot_dmg, (TRY(tur.ammo_max) : tur.shot_dmg * 10 ));
1244 tur.ammo_recharge = max (0, (TRY(tur.ammo_recharge) : tur.shot_dmg * 0.5 ));
1248 void turret_findtarget(entity this)
1250 entity e = find(NULL, classname, "turret_manager");
1253 e = new(turret_manager);
1254 setthink(e, turrets_manager_think);
1255 e.nextthink = time + 2;
1258 entity targ = find(NULL, targetname, this.target);
1259 if(targ.classname == "turret_checkpoint")
1260 return; // turrets don't defend checkpoints?
1265 LOG_TRACE("Turret has invalid defendpoint!");
1268 this.tur_defend = targ;
1269 this.idle_aim = this.tur_head.angles + angleofs(this.tur_head, targ);
1272 bool turret_initialize(entity this, Turret tur)
1274 if(!autocvar_g_turrets)
1278 return false; // invalid turret
1280 // if tur_head exists, we can assume this turret re-spawned
1281 if(!this.tur_head) {
1282 tur.tr_precache(tur);
1283 IL_PUSH(g_turrets, this);
1284 IL_PUSH(g_bot_targets, this);
1287 if(!(this.spawnflags & TSF_SUSPENDED))
1290 this.netname = tur.netname;
1291 load_unit_settings(this, 0);
1293 if(!this.team || !teamplay) { this.team = FLOAT_MAX; }
1294 if(!this.ticrate) { this.ticrate = ((this.turret_flags & TUR_FLAG_SUPPORT) ? 0.2 : 0.1); }
1295 if(!this.health) { this.health = 1000; }
1296 if(!this.shot_refire) { this.shot_refire = 1; }
1297 if(!this.tur_shotorg) { this.tur_shotorg = '50 0 50'; }
1298 if(!this.turret_flags) { this.turret_flags = TUR_FLAG_SPLASH | TUR_FLAG_MEDPROJ | TUR_FLAG_PLAYER; }
1299 if(!this.damage_flags) { this.damage_flags = TFL_DMG_YES | TFL_DMG_RETALIATE | TFL_DMG_AIMSHAKE; }
1300 if(!this.aim_flags) { this.aim_flags = TFL_AIM_LEAD | TFL_AIM_SHOTTIMECOMPENSATE; }
1301 if(!this.track_type) { this.track_type = TFL_TRACKTYPE_STEPMOTOR; }
1302 if(!this.track_flags) { this.track_flags = TFL_TRACK_PITCH | TFL_TRACK_ROTATE; }
1303 if(!this.ammo_flags) { this.ammo_flags = TFL_AMMO_ENERGY | TFL_AMMO_RECHARGE; }
1304 if(!this.target_select_flags) { this.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_TEAMCHECK | TFL_TARGETSELECT_RANGELIMITS | TFL_TARGETSELECT_ANGLELIMITS; }
1305 if(!this.firecheck_flags) { this.firecheck_flags = TFL_FIRECHECK_DEAD | TFL_FIRECHECK_DISTANCES | TFL_FIRECHECK_LOS
1306 | TFL_FIRECHECK_AIMDIST | TFL_FIRECHECK_TEAMCHECK | TFL_FIRECHECK_AMMO_OWN | TFL_FIRECHECK_REFIRE; }
1308 if(this.track_type != TFL_TRACKTYPE_STEPMOTOR)
1310 // Fluid / Ineria mode. Looks mutch nicer.
1311 // Can reduce aim preformance alot, needs a bit diffrent aimspeed
1313 this.aim_speed = bound(0.1, ((!this.aim_speed) ? 180 : this.aim_speed), 1000);
1315 if(!this.track_accel_pitch) { this.track_accel_pitch = 0.5; }
1316 if(!this.track_accel_rotate) { this.track_accel_rotate = 0.5; }
1317 if(!this.track_blendrate) { this.track_blendrate = 0.35; }
1320 turret_initparams(this);
1322 this.turret_flags = TUR_FLAG_ISTURRET | (tur.spawnflags);
1324 if(this.turret_flags & TUR_FLAG_SPLASH)
1325 this.aim_flags |= TFL_AIM_SPLASH;
1327 if(this.turret_flags & TUR_FLAG_MISSILE)
1328 this.target_select_flags |= TFL_TARGETSELECT_MISSILES;
1330 if(this.turret_flags & TUR_FLAG_PLAYER)
1331 this.target_select_flags |= TFL_TARGETSELECT_PLAYERS;
1333 if(this.spawnflags & TSL_NO_RESPAWN)
1334 this.damage_flags |= TFL_DMG_DEATH_NORESPAWN;
1336 if (this.turret_flags & TUR_FLAG_SUPPORT)
1337 this.turret_score_target = turret_targetscore_support;
1339 this.turret_score_target = turret_targetscore_generic;
1343 _setmodel(this, tur.model);
1344 setsize(this, tur.m_mins, tur.m_maxs);
1346 this.m_id = tur.m_id;
1347 this.classname = "turret_main";
1348 this.active = ACTIVE_ACTIVE;
1349 this.effects = EF_NODRAW;
1350 this.netname = tur.turret_name;
1351 this.ticrate = bound(sys_frametime, this.ticrate, 60);
1352 this.max_health = this.health;
1353 this.target_validate_flags = this.target_select_flags;
1354 this.ammo = this.ammo_max;
1355 this.ammo_recharge *= this.ticrate;
1356 this.solid = SOLID_BBOX;
1357 this.takedamage = DAMAGE_AIM;
1358 set_movetype(this, MOVETYPE_NOCLIP);
1359 this.view_ofs = '0 0 0';
1360 this.idle_aim = '0 0 0';
1361 this.turret_firecheckfunc = turret_firecheck;
1362 this.event_damage = turret_damage;
1363 this.use = turret_use;
1364 this.bot_attack = true;
1365 this.nextthink = time + 1;
1366 this.nextthink += turret_count * sys_frametime;
1368 this.tur_head = new(turret_head);
1369 _setmodel(this.tur_head, tur.head_model);
1370 setsize(this.tur_head, '0 0 0', '0 0 0');
1371 setorigin(this.tur_head, '0 0 0');
1372 setattachment(this.tur_head, this, "tag_head");
1374 this.tur_head.netname = this.tur_head.classname;
1375 this.tur_head.team = this.team;
1376 this.tur_head.owner = this;
1377 this.tur_head.takedamage = DAMAGE_NO;
1378 this.tur_head.solid = SOLID_NOT;
1379 set_movetype(this.tur_head, this.move_movetype);
1381 this.weaponentities[0] = this; // lol
1383 if(!this.tur_defend && this.target != "")
1384 InitializeEntity(this, turret_findtarget, INITPRIO_FINDTARGET);
1387 this.tur_debug_start = this.nextthink;
1388 while(vdist(this.tur_debug_rvec, <, 2))
1389 this.tur_debug_rvec = randomvec() * 4;
1391 this.tur_debug_rvec_x = fabs(this.tur_debug_rvec_x);
1392 this.tur_debug_rvec_y = fabs(this.tur_debug_rvec_y);
1393 this.tur_debug_rvec_z = fabs(this.tur_debug_rvec_z);
1397 turret_respawn(this);
1398 turret_tag_fire_update(this);
1400 tur.tr_setup(tur, this);
1402 if(MUTATOR_CALLHOOK(TurretSpawn, this))