1 #include "sv_turrets.qh"
4 #include <common/mapobjects/defs.qh>
5 #include <server/bot/api.qh>
6 #include <server/damage.qh>
7 #include <server/weapons/common.qh>
8 #include <server/weapons/weaponsystem.qh>
9 #include <server/world.qh>
12 vector turret_aim_generic(entity this)
16 float distance, impact_time = 0, i, mintime;
18 turret_tag_fire_update(this);
20 if(this.aim_flags & TFL_AIM_SIMPLE)
21 return real_origin(this.enemy);
23 mintime = max(this.attack_finished_single[0] - time,0) + sys_frametime;
26 pre_pos = real_origin(this.enemy);
29 if (this.aim_flags & TFL_AIM_LEAD)
31 if (this.aim_flags & TFL_AIM_SHOTTIMECOMPENSATE) // Need to conpensate for shot traveltime
35 distance = vlen(prep - this.tur_shotorg);
36 impact_time = distance / this.shot_speed;
38 prep = pre_pos + (this.enemy.velocity * (impact_time + mintime));
40 if(this.aim_flags & TFL_AIM_ZPREDICT)
41 if(!IS_ONGROUND(this.enemy))
42 if(this.enemy.move_movetype == MOVETYPE_WALK || this.enemy.move_movetype == MOVETYPE_TOSS || this.enemy.move_movetype == MOVETYPE_BOUNCE)
46 vz = this.enemy.velocity_z;
47 for(i = 0; i < impact_time; i += sys_frametime)
49 vz = vz - (autocvar_sv_gravity * sys_frametime);
50 prep_z = prep_z + vz * sys_frametime;
56 pre_pos = pre_pos + this.enemy.velocity * mintime;
59 if(this.aim_flags & TFL_AIM_SPLASH)
61 //tracebox(pre_pos + '0 0 32',this.enemy.mins,this.enemy.maxs,pre_pos -'0 0 64',MOVE_WORLDONLY,this.enemy);
62 traceline(pre_pos + '0 0 32',pre_pos -'0 0 64',MOVE_WORLDONLY,this.enemy);
63 if(trace_fraction != 1.0)
64 pre_pos = trace_endpos;
70 float turret_targetscore_support(entity _turret,entity _target)
72 float score; // Total score
73 float s_score = 0, d_score;
75 if (_turret.enemy == _target) s_score = 1;
77 d_score = min(_turret.target_range_optimal,tvt_dist) / max(_turret.target_range_optimal,tvt_dist);
79 score = (d_score * _turret.target_select_rangebias) +
80 (s_score * _turret.target_select_samebias);
86 * Generic bias aware score system.
88 float turret_targetscore_generic(entity _turret, entity _target)
90 float d_dist; // Defendmode Distance
91 float score; // Total score
92 float d_score; // Distance score
93 float a_score; // Angular score
94 float m_score = 0; // missile score
95 float p_score = 0; // player score
96 float ikr; // ideal kill range
98 if (_turret.tur_defend)
100 d_dist = vlen(real_origin(_target) - _turret.tur_defend.origin);
101 ikr = vlen(_turret.origin - _turret.tur_defend.origin);
102 d_score = 1 - d_dist / _turret.target_range;
106 // Make a normlized value base on the targets distance from our optimal killzone
107 ikr = _turret.target_range_optimal;
108 d_score = min(ikr, tvt_dist) / max(ikr, tvt_dist);
111 a_score = 1 - tvt_thadf / _turret.aim_maxrot;
113 if ((_turret.target_select_missilebias > 0) && (_target.flags & FL_PROJECTILE))
116 if ((_turret.target_select_playerbias > 0) && IS_CLIENT(_target))
119 d_score = max(d_score, 0);
120 a_score = max(a_score, 0);
121 m_score = max(m_score, 0);
122 p_score = max(p_score, 0);
124 score = (d_score * _turret.target_select_rangebias) +
125 (a_score * _turret.target_select_anglebias) +
126 (m_score * _turret.target_select_missilebias) +
127 (p_score * _turret.target_select_playerbias);
129 if(vdist((_turret.tur_shotorg - real_origin(_target)), >, _turret.target_range))
136 string sd,sa,sm,sp,ss;
137 string sdt,sat,smt,spt;
140 d_score *= _turret.target_select_rangebias;
143 //sv = ftos(v_score);
144 //v_score *= _turret.target_select_samebias;
145 //svt = ftos(v_score);
148 a_score *= _turret.target_select_anglebias;
152 m_score *= _turret.target_select_missilebias;
156 p_score *= _turret.target_select_playerbias;
161 bprint("^3Target scores^7 \[ ",_turret.netname, " \] ^3for^7 \[ ", _target.netname," \]\n");
162 bprint("^5Range:\[ ",sd, " \]^2+bias:\[ ",sdt," \]\n");
163 bprint("^5Angle:\[ ",sa, " \]^2+bias:\[ ",sat," \]\n");
164 bprint("^5Missile:\[ ",sm," \]^2+bias:\[ ",smt," \]\n");
165 bprint("^5Player:\[ ",sp, " \]^2+bias:\[ ",spt," \]\n");
166 bprint("^3Total (w/bias):\[^1",ss,"\]\n");
173 // Generic damage handling
174 void turret_hide(entity this)
176 this.effects |= EF_NODRAW;
177 this.nextthink = time + this.respawntime - 0.2;
178 setthink(this, turret_respawn);
181 void turret_die(entity this)
183 this.deadflag = DEAD_DEAD;
184 this.tur_head.deadflag = this.deadflag;
186 // Unsolidify and hide real parts
187 this.solid = SOLID_NOT;
188 this.tur_head.solid = this.solid;
190 this.event_damage = func_null;
191 this.event_heal = func_null;
192 this.takedamage = DAMAGE_NO;
194 SetResourceExplicit(this, RES_HEALTH, 0);
197 //RadiusDamage (this,this, min(this.ammo,50),min(this.ammo,50) * 0.25,250,NULL,min(this.ammo,50)*5,DEATH_TURRET,NULL);
199 Turret tur = get_turretinfo(this.m_id);
200 if(this.damage_flags & TFL_DMG_DEATH_NORESPAWN)
202 // do a simple explosion effect here, since CSQC can't do it on a to-be-removed entity
203 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
204 Send_Effect(EFFECT_ROCKET_EXPLODE, this.origin, '0 0 0', 1);
206 tur.tr_death(tur, this);
208 delete(this.tur_head);
214 this.SendFlags |= TNSF_STATUS;
215 this.nextthink = time + 0.2;
216 setthink(this, turret_hide);
218 tur.tr_death(tur, this);
222 void turret_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector vforce)
225 if(this.deadflag == DEAD_DEAD)
228 // Inactive turrets take no damage. (hm..)
232 if(SAME_TEAM(this, attacker))
234 if(autocvar_g_friendlyfire)
235 damage = damage * autocvar_g_friendlyfire;
240 TakeResource(this, RES_HEALTH, damage);
242 // thorw head slightly off aim when hit?
243 if (this.damage_flags & TFL_DMG_HEADSHAKE)
245 this.tur_head.angles_x = this.tur_head.angles_x + (-0.5 + random()) * damage;
246 this.tur_head.angles_y = this.tur_head.angles_y + (-0.5 + random()) * damage;
248 this.SendFlags |= TNSF_ANG;
251 if (this.turret_flags & TUR_FLAG_MOVE)
252 this.velocity = this.velocity + vforce;
254 if (GetResource(this, RES_HEALTH) <= 0)
256 this.event_damage = func_null;
257 this.tur_head.event_damage = func_null;
258 this.event_heal = func_null;
259 this.tur_head.event_heal = func_null;
260 this.takedamage = DAMAGE_NO;
261 this.nextthink = time;
262 setthink(this, turret_die);
265 this.SendFlags |= TNSF_STATUS;
268 bool turret_heal(entity targ, entity inflictor, float amount, float limit)
270 float true_limit = ((limit != RES_LIMIT_NONE) ? limit : targ.max_health);
271 if(GetResource(targ, RES_HEALTH) <= 0 || GetResource(targ, RES_HEALTH) >= true_limit)
274 GiveResourceWithLimit(targ, RES_HEALTH, amount, true_limit);
275 targ.SendFlags |= TNSF_STATUS;
279 void turret_think(entity this);
280 void turret_respawn(entity this)
282 // Make sure all parts belong to the same team since
283 // this function doubles as "teamchange" function.
284 this.tur_head.team = this.team;
285 this.effects &= ~EF_NODRAW;
286 this.deadflag = DEAD_NO;
287 this.effects = EF_LOWPRECISION;
288 this.solid = SOLID_BBOX;
289 this.takedamage = DAMAGE_AIM;
290 this.event_damage = turret_damage;
291 this.event_heal = turret_heal;
292 this.avelocity = '0 0 0';
293 this.tur_head.avelocity = this.avelocity;
294 this.tur_head.angles = this.idle_aim;
295 SetResourceExplicit(this, RES_HEALTH, this.max_health);
297 this.volly_counter = this.shot_volly;
298 this.ammo = this.ammo_max;
300 this.nextthink = time + this.ticrate;
301 setthink(this, turret_think);
303 this.SendFlags = TNSF_FULL_UPDATE;
305 Turret tur = get_turretinfo(this.m_id);
306 tur.tr_setup(tur, this);
308 setorigin(this, this.origin); // make sure it's linked to the area grid
314 void turrets_setframe(entity this, float _frame, float client_only)
316 if((client_only ? this.clientframe : this.frame ) != _frame)
318 this.SendFlags |= TNSF_ANIM;
319 this.anim_start_time = time;
323 this.clientframe = _frame;
329 bool turret_send(entity this, entity to, float sf)
331 WriteHeader(MSG_ENTITY, ENT_CLIENT_TURRET);
332 WriteByte(MSG_ENTITY, sf);
335 WriteByte(MSG_ENTITY, this.m_id);
337 WriteVector(MSG_ENTITY, this.origin);
339 WriteAngleVector2D(MSG_ENTITY, this.angles);
344 WriteShort(MSG_ENTITY, rint(this.tur_head.angles_x));
345 WriteShort(MSG_ENTITY, rint(this.tur_head.angles_y));
350 WriteShort(MSG_ENTITY, rint(this.tur_head.avelocity_x));
351 WriteShort(MSG_ENTITY, rint(this.tur_head.avelocity_y));
356 WriteVector(MSG_ENTITY, this.origin);
358 WriteVector(MSG_ENTITY, this.velocity);
360 WriteShort(MSG_ENTITY, rint(this.angles_y));
365 WriteCoord(MSG_ENTITY, this.anim_start_time);
366 WriteByte(MSG_ENTITY, this.frame);
371 WriteByte(MSG_ENTITY, this.team);
373 if(GetResource(this, RES_HEALTH) <= 0)
374 WriteByte(MSG_ENTITY, 0);
376 WriteByte(MSG_ENTITY, ceil((GetResource(this, RES_HEALTH) / this.max_health) * 255));
382 void load_unit_settings(entity ent, bool is_reload)
387 if(!ent.turret_scale_damage) ent.turret_scale_damage = 1;
388 if(!ent.turret_scale_range) ent.turret_scale_range = 1;
389 if(!ent.turret_scale_refire) ent.turret_scale_refire = 1;
390 if(!ent.turret_scale_ammo) ent.turret_scale_ammo = 1;
391 if(!ent.turret_scale_aim) ent.turret_scale_aim = 1;
392 if(!ent.turret_scale_health) ent.turret_scale_health = 1;
393 if(!ent.turret_scale_respawn) ent.turret_scale_respawn = 1;
398 ent.tur_head.avelocity = '0 0 0';
400 ent.tur_head.angles = '0 0 0';
403 string unitname = ent.netname;
404 #define X(class, prefix, fld, type) ent.fld = cvar(strcat("g_turrets_unit_", prefix, "_", #fld));
405 TR_PROPS_COMMON(X, , unitname)
408 ent.ammo_max *= ent.turret_scale_ammo;
409 ent.ammo_recharge *= ent.turret_scale_ammo;
410 ent.aim_speed *= ent.turret_scale_aim;
411 SetResourceExplicit(ent, RES_HEALTH, GetResource(ent, RES_HEALTH) * ent.turret_scale_health);
412 ent.respawntime *= ent.turret_scale_respawn;
413 ent.shot_dmg *= ent.turret_scale_damage;
414 ent.shot_refire *= ent.turret_scale_refire;
415 ent.shot_radius *= ent.turret_scale_damage;
416 ent.shot_force *= ent.turret_scale_damage;
417 ent.shot_volly_refire *= ent.turret_scale_refire;
418 ent.target_range *= ent.turret_scale_range;
419 ent.target_range_min *= ent.turret_scale_range;
420 ent.target_range_optimal *= ent.turret_scale_range;
423 Turret tur = get_turretinfo(ent.m_id);
424 tur.tr_setup(tur, ent);
428 void turret_projectile_explode(entity this)
431 this.takedamage = DAMAGE_NO;
432 this.event_damage = func_null;
435 d = RadiusDamage (this, this.owner, this.owner.shot_dmg, 0, this.owner.shot_radius, this, NULL, this.owner.shot_force, this.projectiledeathtype, DMG_NOWEP, NULL);
436 this.owner.tur_debug_dmg_t_h = this.owner.tur_debug_dmg_t_h + d;
437 this.owner.tur_debug_dmg_t_f = this.owner.tur_debug_dmg_t_f + this.owner.shot_dmg;
439 RadiusDamage (this, this.realowner, this.owner.shot_dmg, 0, this.owner.shot_radius, this, NULL, this.owner.shot_force, this.projectiledeathtype, DMG_NOWEP, NULL);
444 void turret_projectile_touch(entity this, entity toucher)
446 PROJECTILE_TOUCH(this, toucher);
447 turret_projectile_explode(this);
450 void turret_projectile_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector vforce)
452 this.velocity += vforce;
453 TakeResource(this, RES_HEALTH, damage);
454 //this.realowner = attacker; // Dont change realowner, it does not make much sense for turrets
455 if(GetResource(this, RES_HEALTH) <= 0)
456 W_PrepareExplosionByDamage(this, this.owner, turret_projectile_explode);
459 entity turret_projectile(entity actor, Sound _snd, float _size, float _health, float _death, float _proj_type, float _cull, float _cli_anim)
464 sound (actor, CH_WEAPON_A, _snd, VOL_BASE, ATTEN_NORM);
466 setorigin(proj, actor.tur_shotorg);
467 setsize(proj, '-0.5 -0.5 -0.5' * _size, '0.5 0.5 0.5' * _size);
469 proj.realowner = actor;
470 proj.bot_dodge = true;
471 proj.bot_dodgerating = actor.shot_dmg;
472 setthink(proj, turret_projectile_explode);
473 settouch(proj, turret_projectile_touch);
474 proj.nextthink = time + 9;
475 set_movetype(proj, MOVETYPE_FLYMISSILE);
476 proj.velocity = normalize(actor.tur_shotdir_updated + randomvec() * actor.shot_spread) * actor.shot_speed;
477 proj.flags = FL_PROJECTILE;
478 IL_PUSH(g_projectiles, proj);
479 IL_PUSH(g_bot_dodge, proj);
480 proj.enemy = actor.enemy;
481 proj.projectiledeathtype = _death;
482 PROJECTILE_MAKETRIGGER(proj);
485 SetResourceExplicit(proj, RES_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 turret_tag_fire_update(t_turret);
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;
536 void turret_track(entity this)
538 vector target_angle; // This is where we want to aim
539 vector move_angle; // This is where we can aim
542 v1 = this.tur_head.angles;
543 v2 = this.tur_head.avelocity;
545 if (this.track_flags == TFL_TRACK_NO)
549 target_angle = this.idle_aim - ('1 0 0' * this.aim_maxpitch);
550 else if (this.enemy == NULL)
553 target_angle = this.idle_aim + this.angles;
555 target_angle = vectoangles(normalize(this.tur_aimpos - this.tur_shotorg));
559 target_angle = vectoangles(normalize(this.tur_aimpos - this.tur_shotorg));
562 this.tur_head.angles_x = anglemods(this.tur_head.angles_x);
563 this.tur_head.angles_y = anglemods(this.tur_head.angles_y);
565 // Find the diffrence between where we currently aim and where we want to aim
566 //move_angle = target_angle - (this.angles + this.tur_head.angles);
567 //move_angle = shortangle_vxy(move_angle,(this.angles + this.tur_head.angles));
569 move_angle = AnglesTransform_ToAngles(AnglesTransform_LeftDivide(AnglesTransform_FromAngles(this.angles), AnglesTransform_FromAngles(target_angle))) - this.tur_head.angles;
570 move_angle = shortangle_vxy(move_angle, this.tur_head.angles);
572 switch(this.track_type)
574 case TFL_TRACKTYPE_STEPMOTOR:
575 f_tmp = this.aim_speed * this.ticrate; // dgr/sec -> dgr/tic
576 if (this.track_flags & TFL_TRACK_PITCH)
578 this.tur_head.angles_x += bound(-f_tmp,move_angle_x, f_tmp);
579 if(this.tur_head.angles_x > this.aim_maxpitch)
580 this.tur_head.angles_x = this.aim_maxpitch;
582 if(this.tur_head.angles_x < -this.aim_maxpitch)
583 this.tur_head.angles_x = this.aim_maxpitch;
586 if (this.track_flags & TFL_TRACK_ROTATE)
588 this.tur_head.angles_y += bound(-f_tmp, move_angle_y, f_tmp);
589 if(this.tur_head.angles_y > this.aim_maxrot)
590 this.tur_head.angles_y = this.aim_maxrot;
592 if(this.tur_head.angles_y < -this.aim_maxrot)
593 this.tur_head.angles_y = this.aim_maxrot;
597 this.SendFlags |= TNSF_ANG;
601 case TFL_TRACKTYPE_FLUIDINERTIA:
602 f_tmp = this.aim_speed * this.ticrate; // dgr/sec -> dgr/tic
603 move_angle_x = bound(-this.aim_speed, move_angle_x * this.track_accel_pitch * f_tmp, this.aim_speed);
604 move_angle_y = bound(-this.aim_speed, move_angle_y * this.track_accel_rot * f_tmp, this.aim_speed);
605 move_angle = (this.tur_head.avelocity * this.track_blendrate) + (move_angle * (1 - this.track_blendrate));
608 case TFL_TRACKTYPE_FLUIDPRECISE:
610 move_angle_y = bound(-this.aim_speed, move_angle_y, this.aim_speed);
611 move_angle_x = bound(-this.aim_speed, move_angle_x, this.aim_speed);
617 if (this.track_flags & TFL_TRACK_PITCH)
619 this.tur_head.avelocity_x = move_angle_x;
620 if((this.tur_head.angles_x + this.tur_head.avelocity_x * this.ticrate) > this.aim_maxpitch)
622 this.tur_head.avelocity_x = 0;
623 this.tur_head.angles_x = this.aim_maxpitch;
625 this.SendFlags |= TNSF_ANG;
628 if((this.tur_head.angles_x + this.tur_head.avelocity_x * this.ticrate) < -this.aim_maxpitch)
630 this.tur_head.avelocity_x = 0;
631 this.tur_head.angles_x = -this.aim_maxpitch;
633 this.SendFlags |= TNSF_ANG;
638 if (this.track_flags & TFL_TRACK_ROTATE)
640 this.tur_head.avelocity_y = move_angle_y;
642 if((this.tur_head.angles_y + this.tur_head.avelocity_y * this.ticrate) > this.aim_maxrot)
644 this.tur_head.avelocity_y = 0;
645 this.tur_head.angles_y = this.aim_maxrot;
647 this.SendFlags |= TNSF_ANG;
650 if((this.tur_head.angles_y + this.tur_head.avelocity_y * this.ticrate) < -this.aim_maxrot)
652 this.tur_head.avelocity_y = 0;
653 this.tur_head.angles_y = -this.aim_maxrot;
655 this.SendFlags |= TNSF_ANG;
659 this.SendFlags |= TNSF_AVEL;
661 // Force a angle update every 10'th frame
662 this.turret_framecounter += 1;
663 if(this.turret_framecounter >= 10)
665 this.SendFlags |= TNSF_ANG;
666 this.turret_framecounter = 0;
671 + TFL_TARGETSELECT_NO
672 + TFL_TARGETSELECT_LOS
673 + TFL_TARGETSELECT_PLAYERS
674 + TFL_TARGETSELECT_MISSILES
675 + TFL_TARGETSELECT_VEHICLES
676 - TFL_TARGETSELECT_TRIGGERTARGET
677 + TFL_TARGETSELECT_ANGLELIMITS
678 + TFL_TARGETSELECT_RANGELIMITS
679 + TFL_TARGETSELECT_TEAMCHECK
680 - TFL_TARGETSELECT_NOBUILTIN
681 + TFL_TARGETSELECT_OWNTEAM
685 ** Evaluate a entity for target valitity based on validate_flags
686 ** NOTE: the caller must check takedamage before calling this, to inline this check.
688 float turret_validate_target(entity e_turret, entity e_target, float validate_flags)
692 //if(!validate_flags & TFL_TARGETSELECT_NOBUILTIN)
698 // Don't attack against owner
699 if(e_target.owner == e_turret || e_target == e_turret.realowner)
702 if(!checkpvs(e_target.origin, e_turret))
705 if(e_target.alpha != 0 && e_target.alpha <= 0.3)
708 if(MUTATOR_CALLHOOK(TurretValidateTarget, e_turret, e_target, validate_flags))
709 return M_ARGV(3, float);
711 if (validate_flags & TFL_TARGETSELECT_NO)
714 // If only this was used more..
715 if (e_target.flags & FL_NOTARGET)
719 if (GetResource(e_target, RES_HEALTH) <= 0)
721 else if (STAT(FROZEN, e_target))
725 if(IS_VEHICLE(e_target))
727 if ((validate_flags & TFL_TARGETSELECT_VEHICLES) && !e_target.owner)
732 if (IS_CLIENT(e_target))
734 if(!(validate_flags & TFL_TARGETSELECT_PLAYERS))
737 if (IS_DEAD(e_target))
742 if(validate_flags & TFL_TARGETSELECT_NOTURRETS)
743 if(e_target.owner.tur_head == e_target)
744 if(e_target.team != e_turret.team) // Dont break support units.
748 if (e_target.flags & FL_PROJECTILE)
749 if(!(validate_flags & TFL_TARGETSELECT_MISSILES))
752 if (validate_flags & TFL_TARGETSELECT_MISSILESONLY)
753 if(!(e_target.flags & FL_PROJECTILE))
757 if (validate_flags & TFL_TARGETSELECT_TEAMCHECK)
759 if (validate_flags & TFL_TARGETSELECT_OWNTEAM)
761 if (e_target.team != e_turret.team)
764 if (e_turret.team != e_target.owner.team)
767 if (e_turret.team != e_target.aiment.team)
768 return -12; // portals
772 if (e_target.team == e_turret.team)
775 if (e_turret.team == e_target.owner.team)
778 if (e_turret.team == e_target.aiment.team)
779 return -14; // portals
784 tvt_dist = vlen(e_turret.origin - real_origin(e_target));
785 if (validate_flags & TFL_TARGETSELECT_RANGELIMITS)
787 if (tvt_dist < e_turret.target_range_min)
790 if (tvt_dist > e_turret.target_range)
794 // Can we even aim this thing?
795 tvt_thadv = angleofs3(e_turret.tur_head.origin, e_turret.angles + e_turret.tur_head.angles, e_target.origin);
796 tvt_tadv = shortangle_vxy(angleofs(e_turret, e_target), e_turret.angles);
797 tvt_thadf = vlen(tvt_thadv);
800 if(validate_flags & TFL_TARGETSELECT_FOV)
802 if(e_turret.target_select_fov < tvt_thadf)
807 if (validate_flags & TFL_TARGETSELECT_ANGLELIMITS)
809 if (fabs(tvt_tadv_x) > e_turret.aim_maxpitch)
812 if (fabs(tvt_tadv_y) > e_turret.aim_maxrot)
817 if (validate_flags & TFL_TARGETSELECT_LOS)
819 v_tmp = real_origin(e_target) + ((e_target.mins + e_target.maxs) * 0.5);
821 traceline(e_turret.origin + '0 0 16', v_tmp, 0, e_turret);
823 if(vdist(v_tmp - trace_endpos, >, e_turret.aim_firetolerance_dist))
827 if (e_target.classname == "grapplinghook")
831 if (e_target.classname == "func_button")
835 #ifdef TURRET_DEBUG_TARGETSELECT
836 LOG_TRACE("Target:",e_target.netname," is a valid target for ",e_turret.netname);
842 entity turret_select_target(entity this)
844 entity e; // target looper entity
845 float score; // target looper entity score
846 entity e_enemy; // currently best scoreing target
847 float m_score; // currently best scoreing target's score
850 if(this.enemy && this.enemy.takedamage && turret_validate_target(this,this.enemy,this.target_validate_flags) > 0)
852 e_enemy = this.enemy;
853 m_score = this.turret_score_target(this,e_enemy) * this.target_select_samebias;
856 e_enemy = this.enemy = NULL;
858 e = findradius(this.origin, this.target_range);
860 // Nothing to aim at?
868 float f = turret_validate_target(this, e, this.target_select_flags);
869 //dprint("F is: ", ftos(f), "\n");
872 score = this.turret_score_target(this,e);
873 if ((score > m_score) && (score > 0))
892 + TFL_FIRECHECK_WORLD
894 + TFL_FIRECHECK_DISTANCES
896 + TFL_FIRECHECK_AIMDIST
897 + TFL_FIRECHECK_REALDIST
898 - TFL_FIRECHECK_ANGLEDIST
899 - TFL_FIRECHECK_TEAMCECK
901 + TFL_FIRECHECK_AMMO_OWN
902 + TFL_FIRECHECK_AMMO_OTHER
903 + TFL_FIRECHECK_REFIRE
907 ** Preforms pre-fire checks based on the uints firecheck_flags
909 bool turret_firecheck(entity this)
911 // This one just dont care =)
912 if (this.firecheck_flags & TFL_FIRECHECK_NO)
915 if (this.enemy == NULL)
919 if (this.firecheck_flags & TFL_FIRECHECK_REFIRE)
920 if (this.attack_finished_single[0] > time) return false;
922 // Special case: volly fire turret that has to fire a full volly if a shot was fired.
923 if (this.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
924 if (this.volly_counter != this.shot_volly)
925 if(this.ammo >= this.shot_dmg)
928 // Lack of zombies makes shooting dead things unnecessary :P
929 if (this.firecheck_flags & TFL_FIRECHECK_DEAD)
930 if (IS_DEAD(this.enemy))
934 if (this.firecheck_flags & TFL_FIRECHECK_AMMO_OWN)
935 if (this.ammo < this.shot_dmg)
938 // Other's ammo? (support-supply units)
939 if (this.firecheck_flags & TFL_FIRECHECK_AMMO_OTHER)
940 if (this.enemy.ammo >= this.enemy.ammo_max)
943 // Target of opertunity?
944 if(turret_validate_target(this, this.tur_impactent, this.target_validate_flags) > 0)
946 this.enemy = this.tur_impactent;
950 if (this.firecheck_flags & TFL_FIRECHECK_DISTANCES)
953 if (this.tur_dist_aimpos < this.target_range_min)
955 if(turret_validate_target(this, this.tur_impactent, this.target_validate_flags) > 0)
956 return true; // Target of opertunity?
962 if (this.firecheck_flags & TFL_FIRECHECK_AFF)
963 if (this.tur_impactent.team == this.team)
966 // aim<->predicted impact
967 if (this.firecheck_flags & TFL_FIRECHECK_AIMDIST)
968 if (this.tur_dist_impact_to_aimpos > this.aim_firetolerance_dist)
972 if (this.shot_volly > 1)
973 if (this.volly_counter == this.shot_volly)
974 if (this.ammo < (this.shot_dmg * this.shot_volly))
977 /*if(this.firecheck_flags & TFL_FIRECHECK_VERIFIED)
978 if(this.tur_impactent != this.enemy)
984 bool turret_checkfire(entity this)
986 if(MUTATOR_CALLHOOK(Turret_CheckFire, this))
987 return M_ARGV(1, bool);
989 return this.turret_firecheckfunc(this);
992 void turret_fire(entity this)
994 if (autocvar_g_turrets_nofire != 0)
997 if(MUTATOR_CALLHOOK(TurretFire, this))
1000 Turret info = get_turretinfo(this.m_id);
1001 info.tr_attack(info, this);
1003 this.attack_finished_single[0] = time + this.shot_refire;
1004 this.ammo -= this.shot_dmg;
1005 this.volly_counter = this.volly_counter - 1;
1007 if (this.volly_counter <= 0)
1009 this.volly_counter = this.shot_volly;
1011 if (this.shoot_flags & TFL_SHOOT_CLEARTARGET)
1014 if (this.shot_volly > 1)
1015 this.attack_finished_single[0] = time + this.shot_volly_refire;
1019 if (this.enemy) paint_target3(this.tur_aimpos, 64, this.tur_debug_rvec, this.tur_impacttime + 0.25);
1023 void turret_think(entity this)
1025 this.nextthink = time + this.ticrate;
1027 MUTATOR_CALLHOOK(TurretThink, this);
1030 if (this.tur_debug_tmr1 < time)
1032 if (this.enemy) paint_target (this.enemy,128,this.tur_debug_rvec,0.9);
1033 paint_target(this,256,this.tur_debug_rvec,0.9);
1034 this.tur_debug_tmr1 = time + 1;
1039 if (!(this.spawnflags & TSF_NO_AMMO_REGEN))
1040 if (this.ammo < this.ammo_max)
1041 this.ammo = min(this.ammo + this.ammo_recharge, this.ammo_max);
1043 // Inactive turrets needs to run the think loop,
1044 // So they can handle animation and wake up if need be.
1051 // This is typicaly used for zaping every target in range
1052 // turret_fusionreactor uses this to recharge friendlys.
1053 if (this.shoot_flags & TFL_SHOOT_HITALLVALID)
1055 // Do a this.turret_fire for every valid target.
1056 entity e = findradius(this.origin,this.target_range);
1061 if (turret_validate_target(this,e,this.target_validate_flags))
1065 turret_do_updates(this);
1067 if (turret_checkfire(this))
1076 else if(this.shoot_flags & TFL_SHOOT_CUSTOM)
1078 // This one is doing something.. oddball. assume its handles what needs to be handled.
1081 if(!(this.aim_flags & TFL_AIM_NO))
1082 this.tur_aimpos = turret_aim_generic(this);
1085 if(!(this.track_flags & TFL_TRACK_NO))
1088 turret_do_updates(this);
1091 if (turret_checkfire(this))
1096 // Special case for volly always. if it fired once it must compleate the volly.
1097 if(this.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
1098 if(this.volly_counter != this.shot_volly)
1100 // Predict or whatnot
1101 if(!(this.aim_flags & TFL_AIM_NO))
1102 this.tur_aimpos = turret_aim_generic(this);
1105 if(!(this.track_flags & TFL_TRACK_NO))
1108 turret_do_updates(this);
1111 if (turret_checkfire(this))
1114 Turret tur = get_turretinfo(this.m_id);
1115 tur.tr_think(tur, this);
1120 // Check if we have a vailid enemy, and try to find one if we dont.
1122 // g_turrets_targetscan_maxdelay forces a target re-scan at least this often
1123 float do_target_scan = 0;
1124 if((this.target_select_time + autocvar_g_turrets_targetscan_maxdelay) < time)
1127 // Old target (if any) invalid?
1128 if(this.target_validate_time < time)
1129 if (turret_validate_target(this, this.enemy, this.target_validate_flags) <= 0)
1132 this.target_validate_time = time + 0.5;
1136 // But never more often then g_turrets_targetscan_mindelay!
1137 if (this.target_select_time + autocvar_g_turrets_targetscan_mindelay > time)
1142 this.enemy = turret_select_target(this);
1143 this.target_select_time = time;
1146 // No target, just go to idle, do any custom stuff and bail.
1147 if (this.enemy == NULL)
1150 if(!(this.track_flags & TFL_TRACK_NO))
1153 Turret tur = get_turretinfo(this.m_id);
1154 tur.tr_think(tur, this);
1160 this.lip = time + autocvar_g_turrets_aimidle_delay; // Keep track of the last time we had a target.
1163 if(!(this.aim_flags & TFL_AIM_NO))
1164 this.tur_aimpos = turret_aim_generic(this);
1167 if(!(this.track_flags & TFL_TRACK_NO))
1170 turret_do_updates(this);
1173 if (turret_checkfire(this))
1177 Turret tur = get_turretinfo(this.m_id);
1178 tur.tr_think(tur, this);
1182 When .used a turret switch team to activator.team.
1183 If activator is NULL, the turret go inactive.
1185 void turret_use(entity this, entity actor, entity trigger)
1187 LOG_TRACE("Turret ",this.netname, " used by ", actor.classname);
1189 this.team = actor.team;
1192 this.active = ACTIVE_NOT;
1194 this.active = ACTIVE_ACTIVE;
1198 void turret_link(entity this)
1200 Net_LinkEntity(this, true, 0, turret_send);
1201 setthink(this, turret_think);
1202 this.nextthink = time;
1203 this.tur_head.effects = EF_NODRAW;
1206 void turrets_manager_think(entity this)
1208 this.nextthink = time + 1;
1210 if (autocvar_g_turrets_reloadcvars == 1)
1212 IL_EACH(g_turrets, true,
1214 load_unit_settings(it, true);
1215 Turret tur = get_turretinfo(it.m_id);
1216 tur.tr_think(tur, it);
1218 cvar_set("g_turrets_reloadcvars", "0");
1222 void turret_initparams(entity tur)
1224 #define TRY(x) (x) ? (x)
1225 tur.respawntime = max (-1, (TRY(tur.respawntime) : 60 ));
1226 tur.shot_refire = bound(0.01, (TRY(tur.shot_refire) : 1 ), 9999);
1227 tur.shot_dmg = max (1, (TRY(tur.shot_dmg) : tur.shot_refire * 50 ));
1228 tur.shot_radius = max (1, (TRY(tur.shot_radius) : tur.shot_dmg * 0.5 ));
1229 tur.shot_speed = max (1, (TRY(tur.shot_speed) : 2500 ));
1230 tur.shot_spread = bound(0.0001, (TRY(tur.shot_spread) : 0.0125 ), 500);
1231 tur.shot_force = bound(0.001, (TRY(tur.shot_force) : tur.shot_dmg * 0.5 + tur.shot_radius * 0.5 ), 5000);
1232 tur.shot_volly = bound(1, (TRY(tur.shot_volly) : 1 ), floor(tur.ammo_max / tur.shot_dmg));
1233 tur.shot_volly_refire = bound(tur.shot_refire, (TRY(tur.shot_volly_refire) : tur.shot_refire * tur.shot_volly ), 60);
1234 tur.target_range = bound(0, (TRY(tur.target_range) : tur.shot_speed * 0.5 ), max_shot_distance);
1235 tur.target_range_min = bound(0, (TRY(tur.target_range_min) : tur.shot_radius * 2 ), max_shot_distance);
1236 tur.target_range_optimal = bound(0, (TRY(tur.target_range_optimal) : tur.target_range * 0.5 ), max_shot_distance);
1237 tur.aim_maxrot = bound(0, (TRY(tur.aim_maxrot) : 90 ), 360);
1238 tur.aim_maxpitch = bound(0, (TRY(tur.aim_maxpitch) : 20 ), 90);
1239 tur.aim_speed = bound(0.1, (TRY(tur.aim_speed) : 36 ), 1000);
1240 tur.aim_firetolerance_dist = bound(0.1, (TRY(tur.aim_firetolerance_dist) : 5 + (tur.shot_radius * 2) ), max_shot_distance);
1241 tur.target_select_rangebias = bound(-10, (TRY(tur.target_select_rangebias) : 1 ), 10);
1242 tur.target_select_samebias = bound(-10, (TRY(tur.target_select_samebias) : 1 ), 10);
1243 tur.target_select_anglebias = bound(-10, (TRY(tur.target_select_anglebias) : 1 ), 10);
1244 tur.target_select_missilebias = bound(-10, (TRY(tur.target_select_missilebias) : 1 ), 10);
1245 tur.target_select_playerbias = bound(-10, (TRY(tur.target_select_playerbias) : 1 ), 10);
1246 tur.ammo_max = max (tur.shot_dmg, (TRY(tur.ammo_max) : tur.shot_dmg * 10 ));
1247 tur.ammo_recharge = max (0, (TRY(tur.ammo_recharge) : tur.shot_dmg * 0.5 ));
1251 bool turret_closetotarget(entity this, vector targ, float range)
1253 vector path_extra_size = '1 1 1' * range;
1254 return boxesoverlap(targ - path_extra_size, targ + path_extra_size, this.absmin - path_extra_size, this.absmax + path_extra_size);
1257 void turret_findtarget(entity this)
1259 entity e = find(NULL, classname, "turret_manager");
1262 e = new_pure(turret_manager);
1263 setthink(e, turrets_manager_think);
1264 e.nextthink = time + 2;
1267 entity targ = find(NULL, targetname, this.target);
1268 if(targ.classname == "turret_checkpoint")
1269 return; // turrets don't defend checkpoints?
1274 LOG_TRACE("Turret has invalid defendpoint!");
1277 this.tur_defend = targ;
1278 this.idle_aim = this.tur_head.angles + angleofs(this.tur_head, targ);
1281 void turret_reset(entity this)
1283 turret_respawn(this);
1286 bool turret_initialize(entity this, Turret tur)
1288 if(!autocvar_g_turrets)
1292 return false; // invalid turret
1294 // if tur_head exists, we can assume this turret re-spawned
1295 if(!this.tur_head) {
1296 tur.tr_precache(tur);
1297 IL_PUSH(g_turrets, this);
1298 IL_PUSH(g_bot_targets, this);
1301 if(!(this.spawnflags & TSF_SUSPENDED))
1304 this.netname = tur.netname;
1305 load_unit_settings(this, 0);
1307 if(!this.team || !teamplay) { this.team = FLOAT_MAX; }
1308 if(!this.ticrate) { this.ticrate = ((this.turret_flags & TUR_FLAG_SUPPORT) ? 0.2 : 0.1); }
1309 if(!GetResource(this, RES_HEALTH)) { SetResourceExplicit(this, RES_HEALTH, 1000); }
1310 if(!this.shot_refire) { this.shot_refire = 1; }
1311 if(!this.tur_shotorg) { this.tur_shotorg = '50 0 50'; }
1312 if(!this.turret_flags) { this.turret_flags = TUR_FLAG_SPLASH | TUR_FLAG_MEDPROJ | TUR_FLAG_PLAYER; }
1313 if(!this.damage_flags) { this.damage_flags = TFL_DMG_YES | TFL_DMG_RETALIATE | TFL_DMG_AIMSHAKE; }
1314 if(!this.aim_flags) { this.aim_flags = TFL_AIM_LEAD | TFL_AIM_SHOTTIMECOMPENSATE; }
1315 if(!this.track_type) { this.track_type = TFL_TRACKTYPE_STEPMOTOR; }
1316 if(!this.track_flags) { this.track_flags = TFL_TRACK_PITCH | TFL_TRACK_ROTATE; }
1317 if(!this.ammo_flags) { this.ammo_flags = TFL_AMMO_ENERGY | TFL_AMMO_RECHARGE; }
1318 if(!this.target_select_flags) { this.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_TEAMCHECK | TFL_TARGETSELECT_RANGELIMITS | TFL_TARGETSELECT_ANGLELIMITS; }
1319 if(!this.firecheck_flags) { this.firecheck_flags = TFL_FIRECHECK_DEAD | TFL_FIRECHECK_DISTANCES | TFL_FIRECHECK_LOS
1320 | TFL_FIRECHECK_AIMDIST | TFL_FIRECHECK_TEAMCHECK | TFL_FIRECHECK_AMMO_OWN | TFL_FIRECHECK_REFIRE; }
1322 if(this.track_type != TFL_TRACKTYPE_STEPMOTOR)
1324 // Fluid / Ineria mode. Looks mutch nicer.
1325 // Can reduce aim preformance alot, needs a bit diffrent aimspeed
1327 this.aim_speed = bound(0.1, ((!this.aim_speed) ? 180 : this.aim_speed), 1000);
1329 if(!this.track_accel_pitch) { this.track_accel_pitch = 0.5; }
1330 if(!this.track_accel_rot) { this.track_accel_rot = 0.5; }
1331 if(!this.track_blendrate) { this.track_blendrate = 0.35; }
1334 turret_initparams(this);
1336 this.turret_flags = TUR_FLAG_ISTURRET | (tur.spawnflags);
1338 if(this.turret_flags & TUR_FLAG_SPLASH)
1339 this.aim_flags |= TFL_AIM_SPLASH;
1341 if(this.turret_flags & TUR_FLAG_MISSILE)
1342 this.target_select_flags |= TFL_TARGETSELECT_MISSILES;
1344 if(this.turret_flags & TUR_FLAG_PLAYER)
1345 this.target_select_flags |= TFL_TARGETSELECT_PLAYERS;
1347 if(this.spawnflags & TSL_NO_RESPAWN)
1348 this.damage_flags |= TFL_DMG_DEATH_NORESPAWN;
1350 if (this.turret_flags & TUR_FLAG_SUPPORT)
1351 this.turret_score_target = turret_targetscore_support;
1353 this.turret_score_target = turret_targetscore_generic;
1357 _setmodel(this, tur.model);
1358 setsize(this, tur.m_mins, tur.m_maxs);
1360 this.m_id = tur.m_id;
1361 this.active = ACTIVE_ACTIVE;
1362 this.effects = EF_NODRAW;
1363 this.netname = tur.turret_name;
1364 this.ticrate = bound(sys_frametime, this.ticrate, 60);
1365 this.max_health = GetResource(this, RES_HEALTH);
1366 this.target_validate_flags = this.target_select_flags;
1367 this.ammo = this.ammo_max;
1368 this.ammo_recharge *= this.ticrate;
1369 this.solid = SOLID_BBOX;
1370 this.takedamage = DAMAGE_AIM;
1371 set_movetype(this, MOVETYPE_NOCLIP);
1372 this.view_ofs = '0 0 0';
1373 this.idle_aim = '0 0 0';
1374 this.turret_firecheckfunc = turret_firecheck;
1375 this.event_damage = turret_damage;
1376 this.event_heal = turret_heal;
1377 this.use = turret_use;
1378 this.bot_attack = true;
1379 this.nextthink = time + 1 + turret_count * sys_frametime;
1380 this.reset = turret_reset;
1382 this.tur_head = new(turret_head);
1383 _setmodel(this.tur_head, tur.head_model);
1384 setsize(this.tur_head, '0 0 0', '0 0 0');
1385 setorigin(this.tur_head, '0 0 0');
1386 setattachment(this.tur_head, this, "tag_head");
1388 this.tur_head.netname = this.tur_head.classname;
1389 this.tur_head.team = this.team;
1390 this.tur_head.owner = this;
1391 this.tur_head.takedamage = DAMAGE_NO;
1392 this.tur_head.solid = SOLID_NOT;
1393 set_movetype(this.tur_head, this.move_movetype);
1395 this.weaponentities[0] = this; // lol
1397 if(!this.tur_defend && this.target != "")
1398 InitializeEntity(this, turret_findtarget, INITPRIO_FINDTARGET);
1401 this.tur_debug_start = this.nextthink;
1402 while(vdist(this.tur_debug_rvec, <, 2))
1403 this.tur_debug_rvec = randomvec() * 4;
1405 this.tur_debug_rvec_x = fabs(this.tur_debug_rvec_x);
1406 this.tur_debug_rvec_y = fabs(this.tur_debug_rvec_y);
1407 this.tur_debug_rvec_z = fabs(this.tur_debug_rvec_z);
1411 turret_respawn(this);
1412 turret_tag_fire_update(this);
1414 tur.tr_setup(tur, this);
1416 if(MUTATOR_CALLHOOK(TurretSpawn, this))