1 #include "sv_turrets.qh"
3 #include <server/autocvars.qh>
4 #include <server/g_damage.qh>
5 #include <server/weapons/weaponsystem.qh>
6 #include <common/mapobjects/defs.qh>
9 vector turret_aim_generic(entity this)
13 float distance, impact_time = 0, i, mintime;
15 turret_tag_fire_update(this);
17 if(this.aim_flags & TFL_AIM_SIMPLE)
18 return real_origin(this.enemy);
20 mintime = max(this.attack_finished_single[0] - time,0) + sys_frametime;
23 pre_pos = real_origin(this.enemy);
26 if (this.aim_flags & TFL_AIM_LEAD)
28 if (this.aim_flags & TFL_AIM_SHOTTIMECOMPENSATE) // Need to conpensate for shot traveltime
32 distance = vlen(prep - this.tur_shotorg);
33 impact_time = distance / this.shot_speed;
35 prep = pre_pos + (this.enemy.velocity * (impact_time + mintime));
37 if(this.aim_flags & TFL_AIM_ZPREDICT)
38 if(!IS_ONGROUND(this.enemy))
39 if(this.enemy.move_movetype == MOVETYPE_WALK || this.enemy.move_movetype == MOVETYPE_TOSS || this.enemy.move_movetype == MOVETYPE_BOUNCE)
43 vz = this.enemy.velocity_z;
44 for(i = 0; i < impact_time; i += sys_frametime)
46 vz = vz - (autocvar_sv_gravity * sys_frametime);
47 prep_z = prep_z + vz * sys_frametime;
53 pre_pos = pre_pos + this.enemy.velocity * mintime;
56 if(this.aim_flags & TFL_AIM_SPLASH)
58 //tracebox(pre_pos + '0 0 32',this.enemy.mins,this.enemy.maxs,pre_pos -'0 0 64',MOVE_WORLDONLY,this.enemy);
59 traceline(pre_pos + '0 0 32',pre_pos -'0 0 64',MOVE_WORLDONLY,this.enemy);
60 if(trace_fraction != 1.0)
61 pre_pos = trace_endpos;
67 float turret_targetscore_support(entity _turret,entity _target)
69 float score; // Total score
70 float s_score = 0, d_score;
72 if (_turret.enemy == _target) s_score = 1;
74 d_score = min(_turret.target_range_optimal,tvt_dist) / max(_turret.target_range_optimal,tvt_dist);
76 score = (d_score * _turret.target_select_rangebias) +
77 (s_score * _turret.target_select_samebias);
83 * Generic bias aware score system.
85 float turret_targetscore_generic(entity _turret, entity _target)
87 float d_dist; // Defendmode Distance
88 float score; // Total score
89 float d_score; // Distance score
90 float a_score; // Angular score
91 float m_score = 0; // missile score
92 float p_score = 0; // player score
93 float ikr; // ideal kill range
95 if (_turret.tur_defend)
97 d_dist = vlen(real_origin(_target) - _turret.tur_defend.origin);
98 ikr = vlen(_turret.origin - _turret.tur_defend.origin);
99 d_score = 1 - d_dist / _turret.target_range;
103 // Make a normlized value base on the targets distance from our optimal killzone
104 ikr = _turret.target_range_optimal;
105 d_score = min(ikr, tvt_dist) / max(ikr, tvt_dist);
108 a_score = 1 - tvt_thadf / _turret.aim_maxrot;
110 if ((_turret.target_select_missilebias > 0) && (_target.flags & FL_PROJECTILE))
113 if ((_turret.target_select_playerbias > 0) && IS_CLIENT(_target))
116 d_score = max(d_score, 0);
117 a_score = max(a_score, 0);
118 m_score = max(m_score, 0);
119 p_score = max(p_score, 0);
121 score = (d_score * _turret.target_select_rangebias) +
122 (a_score * _turret.target_select_anglebias) +
123 (m_score * _turret.target_select_missilebias) +
124 (p_score * _turret.target_select_playerbias);
126 if(vdist((_turret.tur_shotorg - real_origin(_target)), >, _turret.target_range))
133 string sd,sa,sm,sp,ss;
134 string sdt,sat,smt,spt;
137 d_score *= _turret.target_select_rangebias;
140 //sv = ftos(v_score);
141 //v_score *= _turret.target_select_samebias;
142 //svt = ftos(v_score);
145 a_score *= _turret.target_select_anglebias;
149 m_score *= _turret.target_select_missilebias;
153 p_score *= _turret.target_select_playerbias;
158 bprint("^3Target scores^7 \[ ",_turret.netname, " \] ^3for^7 \[ ", _target.netname," \]\n");
159 bprint("^5Range:\[ ",sd, " \]^2+bias:\[ ",sdt," \]\n");
160 bprint("^5Angle:\[ ",sa, " \]^2+bias:\[ ",sat," \]\n");
161 bprint("^5Missile:\[ ",sm," \]^2+bias:\[ ",smt," \]\n");
162 bprint("^5Player:\[ ",sp, " \]^2+bias:\[ ",spt," \]\n");
163 bprint("^3Total (w/bias):\[^1",ss,"\]\n");
170 // Generic damage handling
171 void turret_hide(entity this)
173 this.effects |= EF_NODRAW;
174 this.nextthink = time + this.respawntime - 0.2;
175 setthink(this, turret_respawn);
178 void turret_die(entity this)
180 this.deadflag = DEAD_DEAD;
181 this.tur_head.deadflag = this.deadflag;
183 // Unsolidify and hide real parts
184 this.solid = SOLID_NOT;
185 this.tur_head.solid = this.solid;
187 this.event_damage = func_null;
188 this.event_heal = func_null;
189 this.takedamage = DAMAGE_NO;
191 SetResourceExplicit(this, RES_HEALTH, 0);
194 //RadiusDamage (this,this, min(this.ammo,50),min(this.ammo,50) * 0.25,250,NULL,min(this.ammo,50)*5,DEATH_TURRET,NULL);
196 Turret tur = get_turretinfo(this.m_id);
197 if(this.damage_flags & TFL_DMG_DEATH_NORESPAWN)
199 // do a simple explosion effect here, since CSQC can't do it on a to-be-removed entity
200 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
201 Send_Effect(EFFECT_ROCKET_EXPLODE, this.origin, '0 0 0', 1);
203 tur.tr_death(tur, this);
205 delete(this.tur_head);
211 this.SendFlags |= TNSF_STATUS;
212 this.nextthink = time + 0.2;
213 setthink(this, turret_hide);
215 tur.tr_death(tur, this);
219 void turret_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector vforce)
222 if(this.deadflag == DEAD_DEAD)
225 // Inactive turrets take no damage. (hm..)
229 if(SAME_TEAM(this, attacker))
231 if(autocvar_g_friendlyfire)
232 damage = damage * autocvar_g_friendlyfire;
237 TakeResource(this, RES_HEALTH, damage);
239 // thorw head slightly off aim when hit?
240 if (this.damage_flags & TFL_DMG_HEADSHAKE)
242 this.tur_head.angles_x = this.tur_head.angles_x + (-0.5 + random()) * damage;
243 this.tur_head.angles_y = this.tur_head.angles_y + (-0.5 + random()) * damage;
245 this.SendFlags |= TNSF_ANG;
248 if (this.turret_flags & TUR_FLAG_MOVE)
249 this.velocity = this.velocity + vforce;
251 if (GetResource(this, RES_HEALTH) <= 0)
253 this.event_damage = func_null;
254 this.tur_head.event_damage = func_null;
255 this.event_heal = func_null;
256 this.tur_head.event_heal = func_null;
257 this.takedamage = DAMAGE_NO;
258 this.nextthink = time;
259 setthink(this, turret_die);
262 this.SendFlags |= TNSF_STATUS;
265 bool turret_heal(entity targ, entity inflictor, float amount, float limit)
267 float true_limit = ((limit != RES_LIMIT_NONE) ? limit : targ.max_health);
268 if(GetResource(targ, RES_HEALTH) <= 0 || GetResource(targ, RES_HEALTH) >= true_limit)
271 GiveResourceWithLimit(targ, RES_HEALTH, amount, true_limit);
272 targ.SendFlags |= TNSF_STATUS;
276 void turret_think(entity this);
277 void turret_respawn(entity this)
279 // Make sure all parts belong to the same team since
280 // this function doubles as "teamchange" function.
281 this.tur_head.team = this.team;
282 this.effects &= ~EF_NODRAW;
283 this.deadflag = DEAD_NO;
284 this.effects = EF_LOWPRECISION;
285 this.solid = SOLID_BBOX;
286 this.takedamage = DAMAGE_AIM;
287 this.event_damage = turret_damage;
288 this.event_heal = turret_heal;
289 this.avelocity = '0 0 0';
290 this.tur_head.avelocity = this.avelocity;
291 this.tur_head.angles = this.idle_aim;
292 SetResourceExplicit(this, RES_HEALTH, this.max_health);
294 this.volly_counter = this.shot_volly;
295 this.ammo = this.ammo_max;
297 this.nextthink = time + this.ticrate;
298 setthink(this, turret_think);
300 this.SendFlags = TNSF_FULL_UPDATE;
302 Turret tur = get_turretinfo(this.m_id);
303 tur.tr_setup(tur, this);
309 void turrets_setframe(entity this, float _frame, float client_only)
311 if((client_only ? this.clientframe : this.frame ) != _frame)
313 this.SendFlags |= TNSF_ANIM;
314 this.anim_start_time = time;
318 this.clientframe = _frame;
324 bool turret_send(entity this, entity to, float sf)
326 WriteHeader(MSG_ENTITY, ENT_CLIENT_TURRET);
327 WriteByte(MSG_ENTITY, sf);
330 WriteByte(MSG_ENTITY, this.m_id);
332 WriteVector(MSG_ENTITY, this.origin);
334 WriteAngleVector2D(MSG_ENTITY, this.angles);
339 WriteShort(MSG_ENTITY, rint(this.tur_head.angles_x));
340 WriteShort(MSG_ENTITY, rint(this.tur_head.angles_y));
345 WriteShort(MSG_ENTITY, rint(this.tur_head.avelocity_x));
346 WriteShort(MSG_ENTITY, rint(this.tur_head.avelocity_y));
351 WriteVector(MSG_ENTITY, this.origin);
353 WriteVector(MSG_ENTITY, this.velocity);
355 WriteShort(MSG_ENTITY, rint(this.angles_y));
360 WriteCoord(MSG_ENTITY, this.anim_start_time);
361 WriteByte(MSG_ENTITY, this.frame);
366 WriteByte(MSG_ENTITY, this.team);
368 if(GetResource(this, RES_HEALTH) <= 0)
369 WriteByte(MSG_ENTITY, 0);
371 WriteByte(MSG_ENTITY, ceil((GetResource(this, RES_HEALTH) / this.max_health) * 255));
377 void load_unit_settings(entity ent, bool is_reload)
382 if(!ent.turret_scale_damage) ent.turret_scale_damage = 1;
383 if(!ent.turret_scale_range) ent.turret_scale_range = 1;
384 if(!ent.turret_scale_refire) ent.turret_scale_refire = 1;
385 if(!ent.turret_scale_ammo) ent.turret_scale_ammo = 1;
386 if(!ent.turret_scale_aim) ent.turret_scale_aim = 1;
387 if(!ent.turret_scale_health) ent.turret_scale_health = 1;
388 if(!ent.turret_scale_respawn) ent.turret_scale_respawn = 1;
393 ent.tur_head.avelocity = '0 0 0';
395 ent.tur_head.angles = '0 0 0';
398 string unitname = ent.netname;
399 #define X(class, prefix, fld, type) ent.fld = cvar(strcat("g_turrets_unit_", prefix, "_", #fld));
400 TR_PROPS_COMMON(X, , unitname)
403 ent.ammo_max *= ent.turret_scale_ammo;
404 ent.ammo_recharge *= ent.turret_scale_ammo;
405 ent.aim_speed *= ent.turret_scale_aim;
406 SetResourceExplicit(ent, RES_HEALTH, GetResource(ent, RES_HEALTH) * ent.turret_scale_health);
407 ent.respawntime *= ent.turret_scale_respawn;
408 ent.shot_dmg *= ent.turret_scale_damage;
409 ent.shot_refire *= ent.turret_scale_refire;
410 ent.shot_radius *= ent.turret_scale_damage;
411 ent.shot_force *= ent.turret_scale_damage;
412 ent.shot_volly_refire *= ent.turret_scale_refire;
413 ent.target_range *= ent.turret_scale_range;
414 ent.target_range_min *= ent.turret_scale_range;
415 ent.target_range_optimal *= ent.turret_scale_range;
418 Turret tur = get_turretinfo(ent.m_id);
419 tur.tr_setup(tur, ent);
423 void turret_projectile_explode(entity this)
426 this.takedamage = DAMAGE_NO;
427 this.event_damage = func_null;
430 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);
431 this.owner.tur_debug_dmg_t_h = this.owner.tur_debug_dmg_t_h + d;
432 this.owner.tur_debug_dmg_t_f = this.owner.tur_debug_dmg_t_f + this.owner.shot_dmg;
434 RadiusDamage (this, this.realowner, this.owner.shot_dmg, 0, this.owner.shot_radius, this, NULL, this.owner.shot_force, this.projectiledeathtype, DMG_NOWEP, NULL);
439 void turret_projectile_touch(entity this, entity toucher)
441 PROJECTILE_TOUCH(this, toucher);
442 turret_projectile_explode(this);
445 void turret_projectile_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector vforce)
447 this.velocity += vforce;
448 TakeResource(this, RES_HEALTH, damage);
449 //this.realowner = attacker; // Dont change realowner, it does not make much sense for turrets
450 if(GetResource(this, RES_HEALTH) <= 0)
451 W_PrepareExplosionByDamage(this, this.owner, turret_projectile_explode);
454 entity turret_projectile(entity actor, Sound _snd, float _size, float _health, float _death, float _proj_type, float _cull, float _cli_anim)
459 sound (actor, CH_WEAPON_A, _snd, VOL_BASE, ATTEN_NORM);
461 setorigin(proj, actor.tur_shotorg);
462 setsize(proj, '-0.5 -0.5 -0.5' * _size, '0.5 0.5 0.5' * _size);
464 proj.realowner = actor;
465 proj.bot_dodge = true;
466 proj.bot_dodgerating = actor.shot_dmg;
467 setthink(proj, turret_projectile_explode);
468 settouch(proj, turret_projectile_touch);
469 proj.nextthink = time + 9;
470 set_movetype(proj, MOVETYPE_FLYMISSILE);
471 proj.velocity = normalize(actor.tur_shotdir_updated + randomvec() * actor.shot_spread) * actor.shot_speed;
472 proj.flags = FL_PROJECTILE;
473 IL_PUSH(g_projectiles, proj);
474 IL_PUSH(g_bot_dodge, proj);
475 proj.enemy = actor.enemy;
476 proj.projectiledeathtype = _death;
477 PROJECTILE_MAKETRIGGER(proj);
480 SetResourceExplicit(proj, RES_HEALTH, _health);
481 proj.takedamage = DAMAGE_YES;
482 proj.event_damage = turret_projectile_damage;
485 proj.flags |= FL_NOTARGET;
487 CSQCProjectile(proj, _cli_anim, _proj_type, _cull);
493 ** updates enemy distances, predicted impact point/time
494 ** and updated aim<->predict impact distance.
496 void turret_do_updates(entity t_turret)
498 vector enemy_pos = real_origin(t_turret.enemy);
500 turret_tag_fire_update(t_turret);
502 t_turret.tur_shotdir_updated = v_forward;
503 t_turret.tur_dist_enemy = vlen(t_turret.tur_shotorg - enemy_pos);
504 t_turret.tur_dist_aimpos = vlen(t_turret.tur_shotorg - t_turret.tur_aimpos);
506 /*if((t_turret.firecheck_flags & TFL_FIRECHECK_VERIFIED) && (t_turret.enemy))
508 oldpos = t_turret.enemy.origin;
509 setorigin(t_turret.enemy, t_turret.tur_aimpos);
510 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);
511 setorigin(t_turret.enemy, oldpos);
513 if(trace_ent == t_turret.enemy)
514 t_turret.tur_dist_impact_to_aimpos = 0;
516 t_turret.tur_dist_impact_to_aimpos = vlen(trace_endpos - t_turret.tur_aimpos);
519 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);
521 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);
522 t_turret.tur_impactent = trace_ent;
523 t_turret.tur_impacttime = vlen(t_turret.tur_shotorg - trace_endpos) / t_turret.shot_speed;
527 ** Handles head rotation according to
528 ** the units .track_type and .track_flags
530 .float turret_framecounter;
531 void turret_track(entity this)
533 vector target_angle; // This is where we want to aim
534 vector move_angle; // This is where we can aim
537 v1 = this.tur_head.angles;
538 v2 = this.tur_head.avelocity;
540 if (this.track_flags == TFL_TRACK_NO)
544 target_angle = this.idle_aim - ('1 0 0' * this.aim_maxpitch);
545 else if (this.enemy == NULL)
548 target_angle = this.idle_aim + this.angles;
550 target_angle = vectoangles(normalize(this.tur_aimpos - this.tur_shotorg));
554 target_angle = vectoangles(normalize(this.tur_aimpos - this.tur_shotorg));
557 this.tur_head.angles_x = anglemods(this.tur_head.angles_x);
558 this.tur_head.angles_y = anglemods(this.tur_head.angles_y);
560 // Find the diffrence between where we currently aim and where we want to aim
561 //move_angle = target_angle - (this.angles + this.tur_head.angles);
562 //move_angle = shortangle_vxy(move_angle,(this.angles + this.tur_head.angles));
564 move_angle = AnglesTransform_ToAngles(AnglesTransform_LeftDivide(AnglesTransform_FromAngles(this.angles), AnglesTransform_FromAngles(target_angle))) - this.tur_head.angles;
565 move_angle = shortangle_vxy(move_angle, this.tur_head.angles);
567 switch(this.track_type)
569 case TFL_TRACKTYPE_STEPMOTOR:
570 f_tmp = this.aim_speed * this.ticrate; // dgr/sec -> dgr/tic
571 if (this.track_flags & TFL_TRACK_PITCH)
573 this.tur_head.angles_x += bound(-f_tmp,move_angle_x, f_tmp);
574 if(this.tur_head.angles_x > this.aim_maxpitch)
575 this.tur_head.angles_x = this.aim_maxpitch;
577 if(this.tur_head.angles_x < -this.aim_maxpitch)
578 this.tur_head.angles_x = this.aim_maxpitch;
581 if (this.track_flags & TFL_TRACK_ROTATE)
583 this.tur_head.angles_y += bound(-f_tmp, move_angle_y, f_tmp);
584 if(this.tur_head.angles_y > this.aim_maxrot)
585 this.tur_head.angles_y = this.aim_maxrot;
587 if(this.tur_head.angles_y < -this.aim_maxrot)
588 this.tur_head.angles_y = this.aim_maxrot;
592 this.SendFlags |= TNSF_ANG;
596 case TFL_TRACKTYPE_FLUIDINERTIA:
597 f_tmp = this.aim_speed * this.ticrate; // dgr/sec -> dgr/tic
598 move_angle_x = bound(-this.aim_speed, move_angle_x * this.track_accel_pitch * f_tmp, this.aim_speed);
599 move_angle_y = bound(-this.aim_speed, move_angle_y * this.track_accel_rot * f_tmp, this.aim_speed);
600 move_angle = (this.tur_head.avelocity * this.track_blendrate) + (move_angle * (1 - this.track_blendrate));
603 case TFL_TRACKTYPE_FLUIDPRECISE:
605 move_angle_y = bound(-this.aim_speed, move_angle_y, this.aim_speed);
606 move_angle_x = bound(-this.aim_speed, move_angle_x, this.aim_speed);
612 if (this.track_flags & TFL_TRACK_PITCH)
614 this.tur_head.avelocity_x = move_angle_x;
615 if((this.tur_head.angles_x + this.tur_head.avelocity_x * this.ticrate) > this.aim_maxpitch)
617 this.tur_head.avelocity_x = 0;
618 this.tur_head.angles_x = this.aim_maxpitch;
620 this.SendFlags |= TNSF_ANG;
623 if((this.tur_head.angles_x + this.tur_head.avelocity_x * this.ticrate) < -this.aim_maxpitch)
625 this.tur_head.avelocity_x = 0;
626 this.tur_head.angles_x = -this.aim_maxpitch;
628 this.SendFlags |= TNSF_ANG;
633 if (this.track_flags & TFL_TRACK_ROTATE)
635 this.tur_head.avelocity_y = move_angle_y;
637 if((this.tur_head.angles_y + this.tur_head.avelocity_y * this.ticrate) > this.aim_maxrot)
639 this.tur_head.avelocity_y = 0;
640 this.tur_head.angles_y = this.aim_maxrot;
642 this.SendFlags |= TNSF_ANG;
645 if((this.tur_head.angles_y + this.tur_head.avelocity_y * this.ticrate) < -this.aim_maxrot)
647 this.tur_head.avelocity_y = 0;
648 this.tur_head.angles_y = -this.aim_maxrot;
650 this.SendFlags |= TNSF_ANG;
654 this.SendFlags |= TNSF_AVEL;
656 // Force a angle update every 10'th frame
657 this.turret_framecounter += 1;
658 if(this.turret_framecounter >= 10)
660 this.SendFlags |= TNSF_ANG;
661 this.turret_framecounter = 0;
666 + TFL_TARGETSELECT_NO
667 + TFL_TARGETSELECT_LOS
668 + TFL_TARGETSELECT_PLAYERS
669 + TFL_TARGETSELECT_MISSILES
670 + TFL_TARGETSELECT_VEHICLES
671 - TFL_TARGETSELECT_TRIGGERTARGET
672 + TFL_TARGETSELECT_ANGLELIMITS
673 + TFL_TARGETSELECT_RANGELIMITS
674 + TFL_TARGETSELECT_TEAMCHECK
675 - TFL_TARGETSELECT_NOBUILTIN
676 + TFL_TARGETSELECT_OWNTEAM
680 ** Evaluate a entity for target valitity based on validate_flags
681 ** NOTE: the caller must check takedamage before calling this, to inline this check.
683 float turret_validate_target(entity e_turret, entity e_target, float validate_flags)
687 //if(!validate_flags & TFL_TARGETSELECT_NOBUILTIN)
693 if(e_target.owner == e_turret)
696 if(!checkpvs(e_target.origin, e_turret))
699 if(e_target.alpha != 0 && e_target.alpha <= 0.3)
702 if(MUTATOR_CALLHOOK(TurretValidateTarget, e_turret, e_target, validate_flags))
703 return M_ARGV(3, float);
705 if (validate_flags & TFL_TARGETSELECT_NO)
708 // If only this was used more..
709 if (e_target.flags & FL_NOTARGET)
713 if (GetResource(e_target, RES_HEALTH) <= 0)
715 else if (STAT(FROZEN, e_target))
719 if(IS_VEHICLE(e_target))
721 if ((validate_flags & TFL_TARGETSELECT_VEHICLES) && !e_target.owner)
726 if (IS_CLIENT(e_target))
728 if(!(validate_flags & TFL_TARGETSELECT_PLAYERS))
731 if (IS_DEAD(e_target))
736 if(validate_flags & TFL_TARGETSELECT_NOTURRETS)
737 if(e_target.owner.tur_head == e_target)
738 if(e_target.team != e_turret.team) // Dont break support units.
742 if (e_target.flags & FL_PROJECTILE)
743 if(!(validate_flags & TFL_TARGETSELECT_MISSILES))
746 if (validate_flags & TFL_TARGETSELECT_MISSILESONLY)
747 if(!(e_target.flags & FL_PROJECTILE))
751 if (validate_flags & TFL_TARGETSELECT_TEAMCHECK)
753 if (validate_flags & TFL_TARGETSELECT_OWNTEAM)
755 if (e_target.team != e_turret.team)
758 if (e_turret.team != e_target.owner.team)
761 if (e_turret.team != e_target.aiment.team)
762 return -12; // portals
766 if (e_target.team == e_turret.team)
769 if (e_turret.team == e_target.owner.team)
772 if (e_turret.team == e_target.aiment.team)
773 return -14; // portals
778 tvt_dist = vlen(e_turret.origin - real_origin(e_target));
779 if (validate_flags & TFL_TARGETSELECT_RANGELIMITS)
781 if (tvt_dist < e_turret.target_range_min)
784 if (tvt_dist > e_turret.target_range)
788 // Can we even aim this thing?
789 tvt_thadv = angleofs3(e_turret.tur_head.origin, e_turret.angles + e_turret.tur_head.angles, e_target.origin);
790 tvt_tadv = shortangle_vxy(angleofs(e_turret, e_target), e_turret.angles);
791 tvt_thadf = vlen(tvt_thadv);
794 if(validate_flags & TFL_TARGETSELECT_FOV)
796 if(e_turret.target_select_fov < tvt_thadf)
801 if (validate_flags & TFL_TARGETSELECT_ANGLELIMITS)
803 if (fabs(tvt_tadv_x) > e_turret.aim_maxpitch)
806 if (fabs(tvt_tadv_y) > e_turret.aim_maxrot)
811 if (validate_flags & TFL_TARGETSELECT_LOS)
813 v_tmp = real_origin(e_target) + ((e_target.mins + e_target.maxs) * 0.5);
815 traceline(e_turret.origin + '0 0 16', v_tmp, 0, e_turret);
817 if(vdist(v_tmp - trace_endpos, >, e_turret.aim_firetolerance_dist))
821 if (e_target.classname == "grapplinghook")
825 if (e_target.classname == "func_button")
829 #ifdef TURRET_DEBUG_TARGETSELECT
830 LOG_TRACE("Target:",e_target.netname," is a valid target for ",e_turret.netname);
836 entity turret_select_target(entity this)
838 entity e; // target looper entity
839 float score; // target looper entity score
840 entity e_enemy; // currently best scoreing target
841 float m_score; // currently best scoreing target's score
844 if(this.enemy && this.enemy.takedamage && turret_validate_target(this,this.enemy,this.target_validate_flags) > 0)
846 e_enemy = this.enemy;
847 m_score = this.turret_score_target(this,e_enemy) * this.target_select_samebias;
850 e_enemy = this.enemy = NULL;
852 e = findradius(this.origin, this.target_range);
854 // Nothing to aim at?
862 float f = turret_validate_target(this, e, this.target_select_flags);
863 //dprint("F is: ", ftos(f), "\n");
866 score = this.turret_score_target(this,e);
867 if ((score > m_score) && (score > 0))
886 + TFL_FIRECHECK_WORLD
888 + TFL_FIRECHECK_DISTANCES
890 + TFL_FIRECHECK_AIMDIST
891 + TFL_FIRECHECK_REALDIST
892 - TFL_FIRECHECK_ANGLEDIST
893 - TFL_FIRECHECK_TEAMCECK
895 + TFL_FIRECHECK_AMMO_OWN
896 + TFL_FIRECHECK_AMMO_OTHER
897 + TFL_FIRECHECK_REFIRE
901 ** Preforms pre-fire checks based on the uints firecheck_flags
903 bool turret_firecheck(entity this)
905 // This one just dont care =)
906 if (this.firecheck_flags & TFL_FIRECHECK_NO)
909 if (this.enemy == NULL)
913 if (this.firecheck_flags & TFL_FIRECHECK_REFIRE)
914 if (this.attack_finished_single[0] > time) return false;
916 // Special case: volly fire turret that has to fire a full volly if a shot was fired.
917 if (this.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
918 if (this.volly_counter != this.shot_volly)
919 if(this.ammo >= this.shot_dmg)
922 // Lack of zombies makes shooting dead things unnecessary :P
923 if (this.firecheck_flags & TFL_FIRECHECK_DEAD)
924 if (IS_DEAD(this.enemy))
928 if (this.firecheck_flags & TFL_FIRECHECK_AMMO_OWN)
929 if (this.ammo < this.shot_dmg)
932 // Other's ammo? (support-supply units)
933 if (this.firecheck_flags & TFL_FIRECHECK_AMMO_OTHER)
934 if (this.enemy.ammo >= this.enemy.ammo_max)
937 // Target of opertunity?
938 if(turret_validate_target(this, this.tur_impactent, this.target_validate_flags) > 0)
940 this.enemy = this.tur_impactent;
944 if (this.firecheck_flags & TFL_FIRECHECK_DISTANCES)
947 if (this.tur_dist_aimpos < this.target_range_min)
949 if(turret_validate_target(this, this.tur_impactent, this.target_validate_flags) > 0)
950 return true; // Target of opertunity?
956 if (this.firecheck_flags & TFL_FIRECHECK_AFF)
957 if (this.tur_impactent.team == this.team)
960 // aim<->predicted impact
961 if (this.firecheck_flags & TFL_FIRECHECK_AIMDIST)
962 if (this.tur_dist_impact_to_aimpos > this.aim_firetolerance_dist)
966 if (this.shot_volly > 1)
967 if (this.volly_counter == this.shot_volly)
968 if (this.ammo < (this.shot_dmg * this.shot_volly))
971 /*if(this.firecheck_flags & TFL_FIRECHECK_VERIFIED)
972 if(this.tur_impactent != this.enemy)
978 bool turret_checkfire(entity this)
980 if(MUTATOR_CALLHOOK(Turret_CheckFire, this))
981 return M_ARGV(1, bool);
983 return this.turret_firecheckfunc(this);
986 void turret_fire(entity this)
988 if (autocvar_g_turrets_nofire != 0)
991 if(MUTATOR_CALLHOOK(TurretFire, this))
994 Turret info = get_turretinfo(this.m_id);
995 info.tr_attack(info, this);
997 this.attack_finished_single[0] = time + this.shot_refire;
998 this.ammo -= this.shot_dmg;
999 this.volly_counter = this.volly_counter - 1;
1001 if (this.volly_counter <= 0)
1003 this.volly_counter = this.shot_volly;
1005 if (this.shoot_flags & TFL_SHOOT_CLEARTARGET)
1008 if (this.shot_volly > 1)
1009 this.attack_finished_single[0] = time + this.shot_volly_refire;
1013 if (this.enemy) paint_target3(this.tur_aimpos, 64, this.tur_debug_rvec, this.tur_impacttime + 0.25);
1017 void turret_think(entity this)
1019 this.nextthink = time + this.ticrate;
1021 MUTATOR_CALLHOOK(TurretThink, this);
1024 if (this.tur_debug_tmr1 < time)
1026 if (this.enemy) paint_target (this.enemy,128,this.tur_debug_rvec,0.9);
1027 paint_target(this,256,this.tur_debug_rvec,0.9);
1028 this.tur_debug_tmr1 = time + 1;
1033 if (!(this.spawnflags & TSF_NO_AMMO_REGEN))
1034 if (this.ammo < this.ammo_max)
1035 this.ammo = min(this.ammo + this.ammo_recharge, this.ammo_max);
1037 // Inactive turrets needs to run the think loop,
1038 // So they can handle animation and wake up if need be.
1045 // This is typicaly used for zaping every target in range
1046 // turret_fusionreactor uses this to recharge friendlys.
1047 if (this.shoot_flags & TFL_SHOOT_HITALLVALID)
1049 // Do a this.turret_fire for every valid target.
1050 entity e = findradius(this.origin,this.target_range);
1055 if (turret_validate_target(this,e,this.target_validate_flags))
1059 turret_do_updates(this);
1061 if (turret_checkfire(this))
1070 else if(this.shoot_flags & TFL_SHOOT_CUSTOM)
1072 // This one is doing something.. oddball. assume its handles what needs to be handled.
1075 if(!(this.aim_flags & TFL_AIM_NO))
1076 this.tur_aimpos = turret_aim_generic(this);
1079 if(!(this.track_flags & TFL_TRACK_NO))
1082 turret_do_updates(this);
1085 if (turret_checkfire(this))
1090 // Special case for volly always. if it fired once it must compleate the volly.
1091 if(this.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
1092 if(this.volly_counter != this.shot_volly)
1094 // Predict or whatnot
1095 if(!(this.aim_flags & TFL_AIM_NO))
1096 this.tur_aimpos = turret_aim_generic(this);
1099 if(!(this.track_flags & TFL_TRACK_NO))
1102 turret_do_updates(this);
1105 if (turret_checkfire(this))
1108 Turret tur = get_turretinfo(this.m_id);
1109 tur.tr_think(tur, this);
1114 // Check if we have a vailid enemy, and try to find one if we dont.
1116 // g_turrets_targetscan_maxdelay forces a target re-scan at least this often
1117 float do_target_scan = 0;
1118 if((this.target_select_time + autocvar_g_turrets_targetscan_maxdelay) < time)
1121 // Old target (if any) invalid?
1122 if(this.target_validate_time < time)
1123 if (turret_validate_target(this, this.enemy, this.target_validate_flags) <= 0)
1126 this.target_validate_time = time + 0.5;
1130 // But never more often then g_turrets_targetscan_mindelay!
1131 if (this.target_select_time + autocvar_g_turrets_targetscan_mindelay > time)
1136 this.enemy = turret_select_target(this);
1137 this.target_select_time = time;
1140 // No target, just go to idle, do any custom stuff and bail.
1141 if (this.enemy == NULL)
1144 if(!(this.track_flags & TFL_TRACK_NO))
1147 Turret tur = get_turretinfo(this.m_id);
1148 tur.tr_think(tur, this);
1154 this.lip = time + autocvar_g_turrets_aimidle_delay; // Keep track of the last time we had a target.
1157 if(!(this.aim_flags & TFL_AIM_NO))
1158 this.tur_aimpos = turret_aim_generic(this);
1161 if(!(this.track_flags & TFL_TRACK_NO))
1164 turret_do_updates(this);
1167 if (turret_checkfire(this))
1171 Turret tur = get_turretinfo(this.m_id);
1172 tur.tr_think(tur, this);
1176 When .used a turret switch team to activator.team.
1177 If activator is NULL, the turret go inactive.
1179 void turret_use(entity this, entity actor, entity trigger)
1181 LOG_TRACE("Turret ",this.netname, " used by ", actor.classname);
1183 this.team = actor.team;
1186 this.active = ACTIVE_NOT;
1188 this.active = ACTIVE_ACTIVE;
1192 void turret_link(entity this)
1194 Net_LinkEntity(this, true, 0, turret_send);
1195 setthink(this, turret_think);
1196 this.nextthink = time;
1197 this.tur_head.effects = EF_NODRAW;
1200 void turrets_manager_think(entity this)
1202 this.nextthink = time + 1;
1204 if (autocvar_g_turrets_reloadcvars == 1)
1206 IL_EACH(g_turrets, true,
1208 load_unit_settings(it, true);
1209 Turret tur = get_turretinfo(it.m_id);
1210 tur.tr_think(tur, it);
1212 cvar_set("g_turrets_reloadcvars", "0");
1216 void turret_initparams(entity tur)
1218 #define TRY(x) (x) ? (x)
1219 tur.respawntime = max (-1, (TRY(tur.respawntime) : 60 ));
1220 tur.shot_refire = bound(0.01, (TRY(tur.shot_refire) : 1 ), 9999);
1221 tur.shot_dmg = max (1, (TRY(tur.shot_dmg) : tur.shot_refire * 50 ));
1222 tur.shot_radius = max (1, (TRY(tur.shot_radius) : tur.shot_dmg * 0.5 ));
1223 tur.shot_speed = max (1, (TRY(tur.shot_speed) : 2500 ));
1224 tur.shot_spread = bound(0.0001, (TRY(tur.shot_spread) : 0.0125 ), 500);
1225 tur.shot_force = bound(0.001, (TRY(tur.shot_force) : tur.shot_dmg * 0.5 + tur.shot_radius * 0.5 ), 5000);
1226 tur.shot_volly = bound(1, (TRY(tur.shot_volly) : 1 ), floor(tur.ammo_max / tur.shot_dmg));
1227 tur.shot_volly_refire = bound(tur.shot_refire, (TRY(tur.shot_volly_refire) : tur.shot_refire * tur.shot_volly ), 60);
1228 tur.target_range = bound(0, (TRY(tur.target_range) : tur.shot_speed * 0.5 ), max_shot_distance);
1229 tur.target_range_min = bound(0, (TRY(tur.target_range_min) : tur.shot_radius * 2 ), max_shot_distance);
1230 tur.target_range_optimal = bound(0, (TRY(tur.target_range_optimal) : tur.target_range * 0.5 ), max_shot_distance);
1231 tur.aim_maxrot = bound(0, (TRY(tur.aim_maxrot) : 90 ), 360);
1232 tur.aim_maxpitch = bound(0, (TRY(tur.aim_maxpitch) : 20 ), 90);
1233 tur.aim_speed = bound(0.1, (TRY(tur.aim_speed) : 36 ), 1000);
1234 tur.aim_firetolerance_dist = bound(0.1, (TRY(tur.aim_firetolerance_dist) : 5 + (tur.shot_radius * 2) ), max_shot_distance);
1235 tur.target_select_rangebias = bound(-10, (TRY(tur.target_select_rangebias) : 1 ), 10);
1236 tur.target_select_samebias = bound(-10, (TRY(tur.target_select_samebias) : 1 ), 10);
1237 tur.target_select_anglebias = bound(-10, (TRY(tur.target_select_anglebias) : 1 ), 10);
1238 tur.target_select_missilebias = bound(-10, (TRY(tur.target_select_missilebias) : 1 ), 10);
1239 tur.target_select_playerbias = bound(-10, (TRY(tur.target_select_playerbias) : 1 ), 10);
1240 tur.ammo_max = max (tur.shot_dmg, (TRY(tur.ammo_max) : tur.shot_dmg * 10 ));
1241 tur.ammo_recharge = max (0, (TRY(tur.ammo_recharge) : tur.shot_dmg * 0.5 ));
1245 bool turret_closetotarget(entity this, vector targ)
1247 vector path_extra_size = '64 64 64';
1248 return boxesoverlap(targ - path_extra_size, targ + path_extra_size, this.absmin - path_extra_size, this.absmax + path_extra_size);
1251 void turret_findtarget(entity this)
1253 entity e = find(NULL, classname, "turret_manager");
1256 e = new(turret_manager);
1257 setthink(e, turrets_manager_think);
1258 e.nextthink = time + 2;
1261 entity targ = find(NULL, targetname, this.target);
1262 if(targ.classname == "turret_checkpoint")
1263 return; // turrets don't defend checkpoints?
1268 LOG_TRACE("Turret has invalid defendpoint!");
1271 this.tur_defend = targ;
1272 this.idle_aim = this.tur_head.angles + angleofs(this.tur_head, targ);
1275 void turret_reset(entity this)
1277 turret_respawn(this);
1280 bool turret_initialize(entity this, Turret tur)
1282 if(!autocvar_g_turrets)
1286 return false; // invalid turret
1288 // if tur_head exists, we can assume this turret re-spawned
1289 if(!this.tur_head) {
1290 tur.tr_precache(tur);
1291 IL_PUSH(g_turrets, this);
1292 IL_PUSH(g_bot_targets, this);
1295 if(!(this.spawnflags & TSF_SUSPENDED))
1298 this.netname = tur.netname;
1299 load_unit_settings(this, 0);
1301 if(!this.team || !teamplay) { this.team = FLOAT_MAX; }
1302 if(!this.ticrate) { this.ticrate = ((this.turret_flags & TUR_FLAG_SUPPORT) ? 0.2 : 0.1); }
1303 if(!GetResource(this, RES_HEALTH)) { SetResourceExplicit(this, RES_HEALTH, 1000); }
1304 if(!this.shot_refire) { this.shot_refire = 1; }
1305 if(!this.tur_shotorg) { this.tur_shotorg = '50 0 50'; }
1306 if(!this.turret_flags) { this.turret_flags = TUR_FLAG_SPLASH | TUR_FLAG_MEDPROJ | TUR_FLAG_PLAYER; }
1307 if(!this.damage_flags) { this.damage_flags = TFL_DMG_YES | TFL_DMG_RETALIATE | TFL_DMG_AIMSHAKE; }
1308 if(!this.aim_flags) { this.aim_flags = TFL_AIM_LEAD | TFL_AIM_SHOTTIMECOMPENSATE; }
1309 if(!this.track_type) { this.track_type = TFL_TRACKTYPE_STEPMOTOR; }
1310 if(!this.track_flags) { this.track_flags = TFL_TRACK_PITCH | TFL_TRACK_ROTATE; }
1311 if(!this.ammo_flags) { this.ammo_flags = TFL_AMMO_ENERGY | TFL_AMMO_RECHARGE; }
1312 if(!this.target_select_flags) { this.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_TEAMCHECK | TFL_TARGETSELECT_RANGELIMITS | TFL_TARGETSELECT_ANGLELIMITS; }
1313 if(!this.firecheck_flags) { this.firecheck_flags = TFL_FIRECHECK_DEAD | TFL_FIRECHECK_DISTANCES | TFL_FIRECHECK_LOS
1314 | TFL_FIRECHECK_AIMDIST | TFL_FIRECHECK_TEAMCHECK | TFL_FIRECHECK_AMMO_OWN | TFL_FIRECHECK_REFIRE; }
1316 if(this.track_type != TFL_TRACKTYPE_STEPMOTOR)
1318 // Fluid / Ineria mode. Looks mutch nicer.
1319 // Can reduce aim preformance alot, needs a bit diffrent aimspeed
1321 this.aim_speed = bound(0.1, ((!this.aim_speed) ? 180 : this.aim_speed), 1000);
1323 if(!this.track_accel_pitch) { this.track_accel_pitch = 0.5; }
1324 if(!this.track_accel_rot) { this.track_accel_rot = 0.5; }
1325 if(!this.track_blendrate) { this.track_blendrate = 0.35; }
1328 turret_initparams(this);
1330 this.turret_flags = TUR_FLAG_ISTURRET | (tur.spawnflags);
1332 if(this.turret_flags & TUR_FLAG_SPLASH)
1333 this.aim_flags |= TFL_AIM_SPLASH;
1335 if(this.turret_flags & TUR_FLAG_MISSILE)
1336 this.target_select_flags |= TFL_TARGETSELECT_MISSILES;
1338 if(this.turret_flags & TUR_FLAG_PLAYER)
1339 this.target_select_flags |= TFL_TARGETSELECT_PLAYERS;
1341 if(this.spawnflags & TSL_NO_RESPAWN)
1342 this.damage_flags |= TFL_DMG_DEATH_NORESPAWN;
1344 if (this.turret_flags & TUR_FLAG_SUPPORT)
1345 this.turret_score_target = turret_targetscore_support;
1347 this.turret_score_target = turret_targetscore_generic;
1351 _setmodel(this, tur.model);
1352 setsize(this, tur.m_mins, tur.m_maxs);
1354 this.m_id = tur.m_id;
1355 this.active = ACTIVE_ACTIVE;
1356 this.effects = EF_NODRAW;
1357 this.netname = tur.turret_name;
1358 this.ticrate = bound(sys_frametime, this.ticrate, 60);
1359 this.max_health = GetResource(this, RES_HEALTH);
1360 this.target_validate_flags = this.target_select_flags;
1361 this.ammo = this.ammo_max;
1362 this.ammo_recharge *= this.ticrate;
1363 this.solid = SOLID_BBOX;
1364 this.takedamage = DAMAGE_AIM;
1365 set_movetype(this, MOVETYPE_NOCLIP);
1366 this.view_ofs = '0 0 0';
1367 this.idle_aim = '0 0 0';
1368 this.turret_firecheckfunc = turret_firecheck;
1369 this.event_damage = turret_damage;
1370 this.event_heal = turret_heal;
1371 this.use = turret_use;
1372 this.bot_attack = true;
1373 this.nextthink = time + 1 + turret_count * sys_frametime;
1374 this.reset = turret_reset;
1376 this.tur_head = new(turret_head);
1377 _setmodel(this.tur_head, tur.head_model);
1378 setsize(this.tur_head, '0 0 0', '0 0 0');
1379 setorigin(this.tur_head, '0 0 0');
1380 setattachment(this.tur_head, this, "tag_head");
1382 this.tur_head.netname = this.tur_head.classname;
1383 this.tur_head.team = this.team;
1384 this.tur_head.owner = this;
1385 this.tur_head.takedamage = DAMAGE_NO;
1386 this.tur_head.solid = SOLID_NOT;
1387 set_movetype(this.tur_head, this.move_movetype);
1389 this.weaponentities[0] = this; // lol
1391 if(!this.tur_defend && this.target != "")
1392 InitializeEntity(this, turret_findtarget, INITPRIO_FINDTARGET);
1395 this.tur_debug_start = this.nextthink;
1396 while(vdist(this.tur_debug_rvec, <, 2))
1397 this.tur_debug_rvec = randomvec() * 4;
1399 this.tur_debug_rvec_x = fabs(this.tur_debug_rvec_x);
1400 this.tur_debug_rvec_y = fabs(this.tur_debug_rvec_y);
1401 this.tur_debug_rvec_z = fabs(this.tur_debug_rvec_z);
1405 turret_respawn(this);
1406 turret_tag_fire_update(this);
1408 tur.tr_setup(tur, this);
1410 if(MUTATOR_CALLHOOK(TurretSpawn, this))