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.event_heal = func_null;
186 this.takedamage = DAMAGE_NO;
188 SetResourceExplicit(this, RES_HEALTH, 0);
191 //RadiusDamage (this,this, min(this.ammo,50),min(this.ammo,50) * 0.25,250,NULL,min(this.ammo,50)*5,DEATH_TURRET,NULL);
193 Turret tur = get_turretinfo(this.m_id);
194 if(this.damage_flags & TFL_DMG_DEATH_NORESPAWN)
196 // do a simple explosion effect here, since CSQC can't do it on a to-be-removed entity
197 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
198 Send_Effect(EFFECT_ROCKET_EXPLODE, this.origin, '0 0 0', 1);
200 tur.tr_death(tur, this);
202 delete(this.tur_head);
208 this.SendFlags |= TNSF_STATUS;
209 this.nextthink = time + 0.2;
210 setthink(this, turret_hide);
212 tur.tr_death(tur, this);
216 void turret_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector vforce)
219 if(this.deadflag == DEAD_DEAD)
222 // Inactive turrets take no damage. (hm..)
226 if(SAME_TEAM(this, attacker))
228 if(autocvar_g_friendlyfire)
229 damage = damage * autocvar_g_friendlyfire;
234 TakeResource(this, RES_HEALTH, damage);
236 // thorw head slightly off aim when hit?
237 if (this.damage_flags & TFL_DMG_HEADSHAKE)
239 this.tur_head.angles_x = this.tur_head.angles_x + (-0.5 + random()) * damage;
240 this.tur_head.angles_y = this.tur_head.angles_y + (-0.5 + random()) * damage;
242 this.SendFlags |= TNSF_ANG;
245 if (this.turret_flags & TUR_FLAG_MOVE)
246 this.velocity = this.velocity + vforce;
248 if (GetResource(this, RES_HEALTH) <= 0)
250 this.event_damage = func_null;
251 this.tur_head.event_damage = func_null;
252 this.event_heal = func_null;
253 this.tur_head.event_heal = func_null;
254 this.takedamage = DAMAGE_NO;
255 this.nextthink = time;
256 setthink(this, turret_die);
259 this.SendFlags |= TNSF_STATUS;
262 bool turret_heal(entity targ, entity inflictor, float amount, float limit)
264 float true_limit = ((limit != RES_LIMIT_NONE) ? limit : targ.max_health);
265 if(GetResource(targ, RES_HEALTH) <= 0 || GetResource(targ, RES_HEALTH) >= true_limit)
268 GiveResourceWithLimit(targ, RES_HEALTH, amount, true_limit);
269 targ.SendFlags |= TNSF_STATUS;
273 void turret_think(entity this);
274 void turret_respawn(entity this)
276 // Make sure all parts belong to the same team since
277 // this function doubles as "teamchange" function.
278 this.tur_head.team = this.team;
279 this.effects &= ~EF_NODRAW;
280 this.deadflag = DEAD_NO;
281 this.effects = EF_LOWPRECISION;
282 this.solid = SOLID_BBOX;
283 this.takedamage = DAMAGE_AIM;
284 this.event_damage = turret_damage;
285 this.event_heal = turret_heal;
286 this.avelocity = '0 0 0';
287 this.tur_head.avelocity = this.avelocity;
288 this.tur_head.angles = this.idle_aim;
289 SetResourceExplicit(this, RES_HEALTH, this.max_health);
291 this.volly_counter = this.shot_volly;
292 this.ammo = this.ammo_max;
294 this.nextthink = time + this.ticrate;
295 setthink(this, turret_think);
297 this.SendFlags = TNSF_FULL_UPDATE;
299 Turret tur = get_turretinfo(this.m_id);
300 tur.tr_setup(tur, this);
305 #define cvar_base "g_turrets_unit_"
307 void turrets_setframe(entity this, float _frame, float client_only)
309 if((client_only ? this.clientframe : this.frame ) != _frame)
311 this.SendFlags |= TNSF_ANIM;
312 this.anim_start_time = time;
316 this.clientframe = _frame;
322 bool turret_send(entity this, entity to, float sf)
325 WriteHeader(MSG_ENTITY, ENT_CLIENT_TURRET);
326 WriteByte(MSG_ENTITY, sf);
329 WriteByte(MSG_ENTITY, this.m_id);
331 WriteVector(MSG_ENTITY, this.origin);
333 WriteAngle(MSG_ENTITY, this.angles_x);
334 WriteAngle(MSG_ENTITY, this.angles_y);
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)
379 string unitname = ent.netname;
385 if(!ent.turret_scale_damage) ent.turret_scale_damage = 1;
386 if(!ent.turret_scale_range) ent.turret_scale_range = 1;
387 if(!ent.turret_scale_refire) ent.turret_scale_refire = 1;
388 if(!ent.turret_scale_ammo) ent.turret_scale_ammo = 1;
389 if(!ent.turret_scale_aim) ent.turret_scale_aim = 1;
390 if(!ent.turret_scale_health) ent.turret_scale_health = 1;
391 if(!ent.turret_scale_respawn) ent.turret_scale_respawn = 1;
393 sbase = strcat(cvar_base,unitname);
397 ent.tur_head.avelocity = '0 0 0';
399 ent.tur_head.angles = '0 0 0';
402 SetResourceExplicit(ent, RES_HEALTH, cvar(strcat(sbase,"_health")) * ent.turret_scale_health);
403 ent.respawntime = cvar(strcat(sbase,"_respawntime")) * ent.turret_scale_respawn;
405 ent.shot_dmg = cvar(strcat(sbase,"_shot_dmg")) * ent.turret_scale_damage;
406 ent.shot_refire = cvar(strcat(sbase,"_shot_refire")) * ent.turret_scale_refire;
407 ent.shot_radius = cvar(strcat(sbase,"_shot_radius")) * ent.turret_scale_damage;
408 ent.shot_speed = cvar(strcat(sbase,"_shot_speed"));
409 ent.shot_spread = cvar(strcat(sbase,"_shot_spread"));
410 ent.shot_force = cvar(strcat(sbase,"_shot_force")) * ent.turret_scale_damage;
411 ent.shot_volly = cvar(strcat(sbase,"_shot_volly"));
412 ent.shot_volly_refire = cvar(strcat(sbase,"_shot_volly_refire")) * ent.turret_scale_refire;
414 ent.target_range = cvar(strcat(sbase,"_target_range")) * ent.turret_scale_range;
415 ent.target_range_min = cvar(strcat(sbase,"_target_range_min")) * ent.turret_scale_range;
416 ent.target_range_optimal = cvar(strcat(sbase,"_target_range_optimal")) * ent.turret_scale_range;
417 //ent.target_range_fire = cvar(strcat(sbase,"_target_range_fire")) * ent.turret_scale_range;
419 ent.target_select_rangebias = cvar(strcat(sbase,"_target_select_rangebias"));
420 ent.target_select_samebias = cvar(strcat(sbase,"_target_select_samebias"));
421 ent.target_select_anglebias = cvar(strcat(sbase,"_target_select_anglebias"));
422 ent.target_select_missilebias = cvar(strcat(sbase,"_target_select_missilebias"));
423 ent.target_select_playerbias = cvar(strcat(sbase,"_target_select_playerbias"));
424 //ent.target_select_fov = cvar(cvar_gets(sbase,"_target_select_fov"));
426 ent.ammo_max = cvar(strcat(sbase,"_ammo_max")) * ent.turret_scale_ammo;
427 ent.ammo_recharge = cvar(strcat(sbase,"_ammo_recharge")) * ent.turret_scale_ammo;
429 ent.aim_firetolerance_dist = cvar(strcat(sbase,"_aim_firetolerance_dist"));
430 ent.aim_speed = cvar(strcat(sbase,"_aim_speed")) * ent.turret_scale_aim;
431 ent.aim_maxrotate = cvar(strcat(sbase,"_aim_maxrot"));
432 ent.aim_maxpitch = cvar(strcat(sbase,"_aim_maxpitch"));
434 ent.track_type = cvar(strcat(sbase,"_track_type"));
435 ent.track_accel_pitch = cvar(strcat(sbase,"_track_accel_pitch"));
436 ent.track_accel_rotate = cvar(strcat(sbase,"_track_accel_rot"));
437 ent.track_blendrate = cvar(strcat(sbase,"_track_blendrate"));
440 Turret tur = get_turretinfo(ent.m_id);
441 tur.tr_setup(tur, ent);
445 void turret_projectile_explode(entity this)
448 this.takedamage = DAMAGE_NO;
449 this.event_damage = func_null;
452 d = RadiusDamage (this, this.owner, this.owner.shot_dmg, 0, this.owner.shot_radius, this, NULL, this.owner.shot_force, this.totalfrags, DMG_NOWEP, NULL);
453 this.owner.tur_debug_dmg_t_h = this.owner.tur_debug_dmg_t_h + d;
454 this.owner.tur_debug_dmg_t_f = this.owner.tur_debug_dmg_t_f + this.owner.shot_dmg;
456 RadiusDamage (this, this.realowner, this.owner.shot_dmg, 0, this.owner.shot_radius, this, NULL, this.owner.shot_force, this.totalfrags, DMG_NOWEP, NULL);
461 void turret_projectile_touch(entity this, entity toucher)
463 PROJECTILE_TOUCH(this, toucher);
464 turret_projectile_explode(this);
467 void turret_projectile_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector vforce)
469 this.velocity += vforce;
470 TakeResource(this, RES_HEALTH, damage);
471 //this.realowner = attacker; // Dont change realowner, it does not make much sense for turrets
472 if(GetResource(this, RES_HEALTH) <= 0)
473 W_PrepareExplosionByDamage(this, this.owner, turret_projectile_explode);
476 entity turret_projectile(entity actor, Sound _snd, float _size, float _health, float _death, float _proj_type, float _cull, float _cli_anim)
481 sound (actor, CH_WEAPON_A, _snd, VOL_BASE, ATTEN_NORM);
483 setorigin(proj, actor.tur_shotorg);
484 setsize(proj, '-0.5 -0.5 -0.5' * _size, '0.5 0.5 0.5' * _size);
486 proj.realowner = actor;
487 proj.bot_dodge = true;
488 proj.bot_dodgerating = actor.shot_dmg;
489 setthink(proj, turret_projectile_explode);
490 settouch(proj, turret_projectile_touch);
491 proj.nextthink = time + 9;
492 set_movetype(proj, MOVETYPE_FLYMISSILE);
493 proj.velocity = normalize(actor.tur_shotdir_updated + randomvec() * actor.shot_spread) * actor.shot_speed;
494 proj.flags = FL_PROJECTILE;
495 IL_PUSH(g_projectiles, proj);
496 IL_PUSH(g_bot_dodge, proj);
497 proj.enemy = actor.enemy;
498 proj.totalfrags = _death;
499 PROJECTILE_MAKETRIGGER(proj);
502 SetResourceExplicit(proj, RES_HEALTH, _health);
503 proj.takedamage = DAMAGE_YES;
504 proj.event_damage = turret_projectile_damage;
507 proj.flags |= FL_NOTARGET;
509 CSQCProjectile(proj, _cli_anim, _proj_type, _cull);
515 ** updates enemy distances, predicted impact point/time
516 ** and updated aim<->predict impact distance.
518 void turret_do_updates(entity t_turret)
520 vector enemy_pos = real_origin(t_turret.enemy);
522 turret_tag_fire_update(t_turret);
524 t_turret.tur_shotdir_updated = v_forward;
525 t_turret.tur_dist_enemy = vlen(t_turret.tur_shotorg - enemy_pos);
526 t_turret.tur_dist_aimpos = vlen(t_turret.tur_shotorg - t_turret.tur_aimpos);
528 /*if((t_turret.firecheck_flags & TFL_FIRECHECK_VERIFIED) && (t_turret.enemy))
530 oldpos = t_turret.enemy.origin;
531 setorigin(t_turret.enemy, t_turret.tur_aimpos);
532 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 setorigin(t_turret.enemy, oldpos);
535 if(trace_ent == t_turret.enemy)
536 t_turret.tur_dist_impact_to_aimpos = 0;
538 t_turret.tur_dist_impact_to_aimpos = vlen(trace_endpos - t_turret.tur_aimpos);
541 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);
543 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);
544 t_turret.tur_impactent = trace_ent;
545 t_turret.tur_impacttime = vlen(t_turret.tur_shotorg - trace_endpos) / t_turret.shot_speed;
549 ** Handles head rotation according to
550 ** the units .track_type and .track_flags
552 .float turret_framecounter;
553 void turret_track(entity this)
555 vector target_angle; // This is where we want to aim
556 vector move_angle; // This is where we can aim
559 v1 = this.tur_head.angles;
560 v2 = this.tur_head.avelocity;
562 if (this.track_flags == TFL_TRACK_NO)
566 target_angle = this.idle_aim - ('1 0 0' * this.aim_maxpitch);
567 else if (this.enemy == NULL)
570 target_angle = this.idle_aim + this.angles;
572 target_angle = vectoangles(normalize(this.tur_aimpos - this.tur_shotorg));
576 target_angle = vectoangles(normalize(this.tur_aimpos - this.tur_shotorg));
579 this.tur_head.angles_x = anglemods(this.tur_head.angles_x);
580 this.tur_head.angles_y = anglemods(this.tur_head.angles_y);
582 // Find the diffrence between where we currently aim and where we want to aim
583 //move_angle = target_angle - (this.angles + this.tur_head.angles);
584 //move_angle = shortangle_vxy(move_angle,(this.angles + this.tur_head.angles));
586 move_angle = AnglesTransform_ToAngles(AnglesTransform_LeftDivide(AnglesTransform_FromAngles(this.angles), AnglesTransform_FromAngles(target_angle))) - this.tur_head.angles;
587 move_angle = shortangle_vxy(move_angle, this.tur_head.angles);
589 switch(this.track_type)
591 case TFL_TRACKTYPE_STEPMOTOR:
592 f_tmp = this.aim_speed * this.ticrate; // dgr/sec -> dgr/tic
593 if (this.track_flags & TFL_TRACK_PITCH)
595 this.tur_head.angles_x += bound(-f_tmp,move_angle_x, f_tmp);
596 if(this.tur_head.angles_x > this.aim_maxpitch)
597 this.tur_head.angles_x = this.aim_maxpitch;
599 if(this.tur_head.angles_x < -this.aim_maxpitch)
600 this.tur_head.angles_x = this.aim_maxpitch;
603 if (this.track_flags & TFL_TRACK_ROTATE)
605 this.tur_head.angles_y += bound(-f_tmp, move_angle_y, f_tmp);
606 if(this.tur_head.angles_y > this.aim_maxrotate)
607 this.tur_head.angles_y = this.aim_maxrotate;
609 if(this.tur_head.angles_y < -this.aim_maxrotate)
610 this.tur_head.angles_y = this.aim_maxrotate;
614 this.SendFlags |= TNSF_ANG;
618 case TFL_TRACKTYPE_FLUIDINERTIA:
619 f_tmp = this.aim_speed * this.ticrate; // dgr/sec -> dgr/tic
620 move_angle_x = bound(-this.aim_speed, move_angle_x * this.track_accel_pitch * f_tmp, this.aim_speed);
621 move_angle_y = bound(-this.aim_speed, move_angle_y * this.track_accel_rotate * f_tmp, this.aim_speed);
622 move_angle = (this.tur_head.avelocity * this.track_blendrate) + (move_angle * (1 - this.track_blendrate));
625 case TFL_TRACKTYPE_FLUIDPRECISE:
627 move_angle_y = bound(-this.aim_speed, move_angle_y, this.aim_speed);
628 move_angle_x = bound(-this.aim_speed, move_angle_x, this.aim_speed);
634 if (this.track_flags & TFL_TRACK_PITCH)
636 this.tur_head.avelocity_x = move_angle_x;
637 if((this.tur_head.angles_x + this.tur_head.avelocity_x * this.ticrate) > this.aim_maxpitch)
639 this.tur_head.avelocity_x = 0;
640 this.tur_head.angles_x = this.aim_maxpitch;
642 this.SendFlags |= TNSF_ANG;
645 if((this.tur_head.angles_x + this.tur_head.avelocity_x * this.ticrate) < -this.aim_maxpitch)
647 this.tur_head.avelocity_x = 0;
648 this.tur_head.angles_x = -this.aim_maxpitch;
650 this.SendFlags |= TNSF_ANG;
655 if (this.track_flags & TFL_TRACK_ROTATE)
657 this.tur_head.avelocity_y = move_angle_y;
659 if((this.tur_head.angles_y + this.tur_head.avelocity_y * this.ticrate) > this.aim_maxrotate)
661 this.tur_head.avelocity_y = 0;
662 this.tur_head.angles_y = this.aim_maxrotate;
664 this.SendFlags |= TNSF_ANG;
667 if((this.tur_head.angles_y + this.tur_head.avelocity_y * this.ticrate) < -this.aim_maxrotate)
669 this.tur_head.avelocity_y = 0;
670 this.tur_head.angles_y = -this.aim_maxrotate;
672 this.SendFlags |= TNSF_ANG;
676 this.SendFlags |= TNSF_AVEL;
678 // Force a angle update every 10'th frame
679 this.turret_framecounter += 1;
680 if(this.turret_framecounter >= 10)
682 this.SendFlags |= TNSF_ANG;
683 this.turret_framecounter = 0;
688 + TFL_TARGETSELECT_NO
689 + TFL_TARGETSELECT_LOS
690 + TFL_TARGETSELECT_PLAYERS
691 + TFL_TARGETSELECT_MISSILES
692 + TFL_TARGETSELECT_VEHICLES
693 - TFL_TARGETSELECT_TRIGGERTARGET
694 + TFL_TARGETSELECT_ANGLELIMITS
695 + TFL_TARGETSELECT_RANGELIMITS
696 + TFL_TARGETSELECT_TEAMCHECK
697 - TFL_TARGETSELECT_NOBUILTIN
698 + TFL_TARGETSELECT_OWNTEAM
702 ** Evaluate a entity for target valitity based on validate_flags
703 ** NOTE: the caller must check takedamage before calling this, to inline this check.
705 float turret_validate_target(entity e_turret, entity e_target, float validate_flags)
709 //if(!validate_flags & TFL_TARGETSELECT_NOBUILTIN)
715 if(e_target.owner == e_turret)
718 if(!checkpvs(e_target.origin, e_turret))
721 if(e_target.alpha != 0 && e_target.alpha <= 0.3)
724 if(MUTATOR_CALLHOOK(TurretValidateTarget, e_turret, e_target, validate_flags))
725 return M_ARGV(3, float);
727 if (validate_flags & TFL_TARGETSELECT_NO)
730 // If only this was used more..
731 if (e_target.flags & FL_NOTARGET)
735 if (GetResource(e_target, RES_HEALTH) <= 0)
737 else if (STAT(FROZEN, e_target))
741 if(IS_VEHICLE(e_target))
743 if ((validate_flags & TFL_TARGETSELECT_VEHICLES) && !e_target.owner)
748 if (IS_CLIENT(e_target))
750 if(!(validate_flags & TFL_TARGETSELECT_PLAYERS))
753 if (IS_DEAD(e_target))
758 if(validate_flags & TFL_TARGETSELECT_NOTURRETS)
759 if(e_target.owner.tur_head == e_target)
760 if(e_target.team != e_turret.team) // Dont break support units.
764 if (e_target.flags & FL_PROJECTILE)
765 if(!(validate_flags & TFL_TARGETSELECT_MISSILES))
768 if (validate_flags & TFL_TARGETSELECT_MISSILESONLY)
769 if(!(e_target.flags & FL_PROJECTILE))
773 if (validate_flags & TFL_TARGETSELECT_TEAMCHECK)
775 if (validate_flags & TFL_TARGETSELECT_OWNTEAM)
777 if (e_target.team != e_turret.team)
780 if (e_turret.team != e_target.owner.team)
785 if (e_target.team == e_turret.team)
788 if (e_turret.team == e_target.owner.team)
794 tvt_dist = vlen(e_turret.origin - real_origin(e_target));
795 if (validate_flags & TFL_TARGETSELECT_RANGELIMITS)
797 if (tvt_dist < e_turret.target_range_min)
800 if (tvt_dist > e_turret.target_range)
804 // Can we even aim this thing?
805 tvt_thadv = angleofs3(e_turret.tur_head.origin, e_turret.angles + e_turret.tur_head.angles, e_target.origin);
806 tvt_tadv = shortangle_vxy(angleofs(e_turret, e_target), e_turret.angles);
807 tvt_thadf = vlen(tvt_thadv);
808 tvt_tadf = vlen(tvt_tadv);
811 if(validate_flags & TFL_TARGETSELECT_FOV)
813 if(e_turret.target_select_fov < tvt_thadf)
818 if (validate_flags & TFL_TARGETSELECT_ANGLELIMITS)
820 if (fabs(tvt_tadv_x) > e_turret.aim_maxpitch)
823 if (fabs(tvt_tadv_y) > e_turret.aim_maxrotate)
828 if (validate_flags & TFL_TARGETSELECT_LOS)
830 v_tmp = real_origin(e_target) + ((e_target.mins + e_target.maxs) * 0.5);
832 traceline(e_turret.origin + '0 0 16', v_tmp, 0, e_turret);
834 if(vdist(v_tmp - trace_endpos, >, e_turret.aim_firetolerance_dist))
838 if (e_target.classname == "grapplinghook")
842 if (e_target.classname == "func_button")
846 #ifdef TURRET_DEBUG_TARGETSELECT
847 LOG_TRACE("Target:",e_target.netname," is a valid target for ",e_turret.netname);
853 entity turret_select_target(entity this)
855 entity e; // target looper entity
856 float score; // target looper entity score
857 entity e_enemy; // currently best scoreing target
858 float m_score; // currently best scoreing target's score
861 if(this.enemy && this.enemy.takedamage && turret_validate_target(this,this.enemy,this.target_validate_flags) > 0)
863 e_enemy = this.enemy;
864 m_score = this.turret_score_target(this,e_enemy) * this.target_select_samebias;
867 e_enemy = this.enemy = NULL;
869 e = findradius(this.origin, this.target_range);
871 // Nothing to aim at?
879 float f = turret_validate_target(this, e, this.target_select_flags);
880 //dprint("F is: ", ftos(f), "\n");
883 score = this.turret_score_target(this,e);
884 if ((score > m_score) && (score > 0))
903 + TFL_FIRECHECK_WORLD
905 + TFL_FIRECHECK_DISTANCES
907 + TFL_FIRECHECK_AIMDIST
908 + TFL_FIRECHECK_REALDIST
909 - TFL_FIRECHECK_ANGLEDIST
910 - TFL_FIRECHECK_TEAMCECK
912 + TFL_FIRECHECK_AMMO_OWN
913 + TFL_FIRECHECK_AMMO_OTHER
914 + TFL_FIRECHECK_REFIRE
918 ** Preforms pre-fire checks based on the uints firecheck_flags
920 bool turret_firecheck(entity this)
922 // This one just dont care =)
923 if (this.firecheck_flags & TFL_FIRECHECK_NO)
926 if (this.enemy == NULL)
930 if (this.firecheck_flags & TFL_FIRECHECK_REFIRE)
931 if (this.attack_finished_single[0] > time) return false;
933 // Special case: volly fire turret that has to fire a full volly if a shot was fired.
934 if (this.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
935 if (this.volly_counter != this.shot_volly)
936 if(this.ammo >= this.shot_dmg)
939 // Lack of zombies makes shooting dead things unnecessary :P
940 if (this.firecheck_flags & TFL_FIRECHECK_DEAD)
941 if (IS_DEAD(this.enemy))
945 if (this.firecheck_flags & TFL_FIRECHECK_AMMO_OWN)
946 if (this.ammo < this.shot_dmg)
949 // Other's ammo? (support-supply units)
950 if (this.firecheck_flags & TFL_FIRECHECK_AMMO_OTHER)
951 if (this.enemy.ammo >= this.enemy.ammo_max)
954 // Target of opertunity?
955 if(turret_validate_target(this, this.tur_impactent, this.target_validate_flags) > 0)
957 this.enemy = this.tur_impactent;
961 if (this.firecheck_flags & TFL_FIRECHECK_DISTANCES)
964 if (this.tur_dist_aimpos < this.target_range_min)
966 if(turret_validate_target(this, this.tur_impactent, this.target_validate_flags) > 0)
967 return true; // Target of opertunity?
973 if (this.firecheck_flags & TFL_FIRECHECK_AFF)
974 if (this.tur_impactent.team == this.team)
977 // aim<->predicted impact
978 if (this.firecheck_flags & TFL_FIRECHECK_AIMDIST)
979 if (this.tur_dist_impact_to_aimpos > this.aim_firetolerance_dist)
983 if (this.shot_volly > 1)
984 if (this.volly_counter == this.shot_volly)
985 if (this.ammo < (this.shot_dmg * this.shot_volly))
988 /*if(this.firecheck_flags & TFL_FIRECHECK_VERIFIED)
989 if(this.tur_impactent != this.enemy)
995 bool turret_checkfire(entity this)
997 if(MUTATOR_CALLHOOK(Turret_CheckFire, this))
998 return M_ARGV(1, bool);
1000 return this.turret_firecheckfunc(this);
1003 void turret_fire(entity this)
1005 if (autocvar_g_turrets_nofire != 0)
1008 if(MUTATOR_CALLHOOK(TurretFire, this))
1011 Turret info = get_turretinfo(this.m_id);
1012 info.tr_attack(info, this);
1014 this.attack_finished_single[0] = time + this.shot_refire;
1015 this.ammo -= this.shot_dmg;
1016 this.volly_counter = this.volly_counter - 1;
1018 if (this.volly_counter <= 0)
1020 this.volly_counter = this.shot_volly;
1022 if (this.shoot_flags & TFL_SHOOT_CLEARTARGET)
1025 if (this.shot_volly > 1)
1026 this.attack_finished_single[0] = time + this.shot_volly_refire;
1030 if (this.enemy) paint_target3(this.tur_aimpos, 64, this.tur_debug_rvec, this.tur_impacttime + 0.25);
1034 void turret_think(entity this)
1036 this.nextthink = time + this.ticrate;
1038 MUTATOR_CALLHOOK(TurretThink, this);
1041 if (this.tur_debug_tmr1 < time)
1043 if (this.enemy) paint_target (this.enemy,128,this.tur_debug_rvec,0.9);
1044 paint_target(this,256,this.tur_debug_rvec,0.9);
1045 this.tur_debug_tmr1 = time + 1;
1050 if (!(this.spawnflags & TSF_NO_AMMO_REGEN))
1051 if (this.ammo < this.ammo_max)
1052 this.ammo = min(this.ammo + this.ammo_recharge, this.ammo_max);
1054 // Inactive turrets needs to run the think loop,
1055 // So they can handle animation and wake up if need be.
1062 // This is typicaly used for zaping every target in range
1063 // turret_fusionreactor uses this to recharge friendlys.
1064 if (this.shoot_flags & TFL_SHOOT_HITALLVALID)
1066 // Do a this.turret_fire for every valid target.
1067 entity e = findradius(this.origin,this.target_range);
1072 if (turret_validate_target(this,e,this.target_validate_flags))
1076 turret_do_updates(this);
1078 if (turret_checkfire(this))
1087 else if(this.shoot_flags & TFL_SHOOT_CUSTOM)
1089 // This one is doing something.. oddball. assume its handles what needs to be handled.
1092 if(!(this.aim_flags & TFL_AIM_NO))
1093 this.tur_aimpos = turret_aim_generic(this);
1096 if(!(this.track_flags & TFL_TRACK_NO))
1099 turret_do_updates(this);
1102 if (turret_checkfire(this))
1107 // Special case for volly always. if it fired once it must compleate the volly.
1108 if(this.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
1109 if(this.volly_counter != this.shot_volly)
1111 // Predict or whatnot
1112 if(!(this.aim_flags & TFL_AIM_NO))
1113 this.tur_aimpos = turret_aim_generic(this);
1116 if(!(this.track_flags & TFL_TRACK_NO))
1119 turret_do_updates(this);
1122 if (turret_checkfire(this))
1125 Turret tur = get_turretinfo(this.m_id);
1126 tur.tr_think(tur, this);
1131 // Check if we have a vailid enemy, and try to find one if we dont.
1133 // g_turrets_targetscan_maxdelay forces a target re-scan at least this often
1134 float do_target_scan = 0;
1135 if((this.target_select_time + autocvar_g_turrets_targetscan_maxdelay) < time)
1138 // Old target (if any) invalid?
1139 if(this.target_validate_time < time)
1140 if (turret_validate_target(this, this.enemy, this.target_validate_flags) <= 0)
1143 this.target_validate_time = time + 0.5;
1147 // But never more often then g_turrets_targetscan_mindelay!
1148 if (this.target_select_time + autocvar_g_turrets_targetscan_mindelay > time)
1153 this.enemy = turret_select_target(this);
1154 this.target_select_time = time;
1157 // No target, just go to idle, do any custom stuff and bail.
1158 if (this.enemy == NULL)
1161 if(!(this.track_flags & TFL_TRACK_NO))
1164 Turret tur = get_turretinfo(this.m_id);
1165 tur.tr_think(tur, this);
1171 this.lip = time + autocvar_g_turrets_aimidle_delay; // Keep track of the last time we had a target.
1174 if(!(this.aim_flags & TFL_AIM_NO))
1175 this.tur_aimpos = turret_aim_generic(this);
1178 if(!(this.track_flags & TFL_TRACK_NO))
1181 turret_do_updates(this);
1184 if (turret_checkfire(this))
1188 Turret tur = get_turretinfo(this.m_id);
1189 tur.tr_think(tur, this);
1193 When .used a turret switch team to activator.team.
1194 If activator is NULL, the turret go inactive.
1196 void turret_use(entity this, entity actor, entity trigger)
1198 LOG_TRACE("Turret ",this.netname, " used by ", actor.classname);
1200 this.team = actor.team;
1203 this.active = ACTIVE_NOT;
1205 this.active = ACTIVE_ACTIVE;
1209 void turret_link(entity this)
1211 Net_LinkEntity(this, true, 0, turret_send);
1212 setthink(this, turret_think);
1213 this.nextthink = time;
1214 this.tur_head.effects = EF_NODRAW;
1217 void turrets_manager_think(entity this)
1219 this.nextthink = time + 1;
1221 if (autocvar_g_turrets_reloadcvars == 1)
1223 IL_EACH(g_turrets, true,
1225 load_unit_settings(it, true);
1226 Turret tur = get_turretinfo(it.m_id);
1227 tur.tr_think(tur, it);
1229 cvar_set("g_turrets_reloadcvars", "0");
1233 void turret_initparams(entity tur)
1235 #define TRY(x) (x) ? (x)
1236 tur.respawntime = max (-1, (TRY(tur.respawntime) : 60 ));
1237 tur.shot_refire = bound(0.01, (TRY(tur.shot_refire) : 1 ), 9999);
1238 tur.shot_dmg = max (1, (TRY(tur.shot_dmg) : tur.shot_refire * 50 ));
1239 tur.shot_radius = max (1, (TRY(tur.shot_radius) : tur.shot_dmg * 0.5 ));
1240 tur.shot_speed = max (1, (TRY(tur.shot_speed) : 2500 ));
1241 tur.shot_spread = bound(0.0001, (TRY(tur.shot_spread) : 0.0125 ), 500);
1242 tur.shot_force = bound(0.001, (TRY(tur.shot_force) : tur.shot_dmg * 0.5 + tur.shot_radius * 0.5 ), 5000);
1243 tur.shot_volly = bound(1, (TRY(tur.shot_volly) : 1 ), floor(tur.ammo_max / tur.shot_dmg));
1244 tur.shot_volly_refire = bound(tur.shot_refire, (TRY(tur.shot_volly_refire) : tur.shot_refire * tur.shot_volly ), 60);
1245 tur.target_range = bound(0, (TRY(tur.target_range) : tur.shot_speed * 0.5 ), max_shot_distance);
1246 tur.target_range_min = bound(0, (TRY(tur.target_range_min) : tur.shot_radius * 2 ), max_shot_distance);
1247 tur.target_range_optimal = bound(0, (TRY(tur.target_range_optimal) : tur.target_range * 0.5 ), max_shot_distance);
1248 tur.aim_maxrotate = bound(0, (TRY(tur.aim_maxrotate) : 90 ), 360);
1249 tur.aim_maxpitch = bound(0, (TRY(tur.aim_maxpitch) : 20 ), 90);
1250 tur.aim_speed = bound(0.1, (TRY(tur.aim_speed) : 36 ), 1000);
1251 tur.aim_firetolerance_dist = bound(0.1, (TRY(tur.aim_firetolerance_dist) : 5 + (tur.shot_radius * 2) ), max_shot_distance);
1252 tur.target_select_rangebias = bound(-10, (TRY(tur.target_select_rangebias) : 1 ), 10);
1253 tur.target_select_samebias = bound(-10, (TRY(tur.target_select_samebias) : 1 ), 10);
1254 tur.target_select_anglebias = bound(-10, (TRY(tur.target_select_anglebias) : 1 ), 10);
1255 tur.target_select_missilebias = bound(-10, (TRY(tur.target_select_missilebias) : 1 ), 10);
1256 tur.target_select_playerbias = bound(-10, (TRY(tur.target_select_playerbias) : 1 ), 10);
1257 tur.ammo_max = max (tur.shot_dmg, (TRY(tur.ammo_max) : tur.shot_dmg * 10 ));
1258 tur.ammo_recharge = max (0, (TRY(tur.ammo_recharge) : tur.shot_dmg * 0.5 ));
1262 bool turret_closetotarget(entity this, vector targ)
1264 vector path_extra_size = '64 64 64';
1265 return boxesoverlap(targ - path_extra_size, targ + path_extra_size, this.absmin - path_extra_size, this.absmax + path_extra_size);
1268 void turret_findtarget(entity this)
1270 entity e = find(NULL, classname, "turret_manager");
1273 e = new(turret_manager);
1274 setthink(e, turrets_manager_think);
1275 e.nextthink = time + 2;
1278 entity targ = find(NULL, targetname, this.target);
1279 if(targ.classname == "turret_checkpoint")
1280 return; // turrets don't defend checkpoints?
1285 LOG_TRACE("Turret has invalid defendpoint!");
1288 this.tur_defend = targ;
1289 this.idle_aim = this.tur_head.angles + angleofs(this.tur_head, targ);
1292 bool turret_initialize(entity this, Turret tur)
1294 if(!autocvar_g_turrets)
1298 return false; // invalid turret
1300 // if tur_head exists, we can assume this turret re-spawned
1301 if(!this.tur_head) {
1302 tur.tr_precache(tur);
1303 IL_PUSH(g_turrets, this);
1304 IL_PUSH(g_bot_targets, this);
1307 if(!(this.spawnflags & TSF_SUSPENDED))
1310 this.netname = tur.netname;
1311 load_unit_settings(this, 0);
1313 if(!this.team || !teamplay) { this.team = FLOAT_MAX; }
1314 if(!this.ticrate) { this.ticrate = ((this.turret_flags & TUR_FLAG_SUPPORT) ? 0.2 : 0.1); }
1315 if(!GetResource(this, RES_HEALTH)) { SetResourceExplicit(this, RES_HEALTH, 1000); }
1316 if(!this.shot_refire) { this.shot_refire = 1; }
1317 if(!this.tur_shotorg) { this.tur_shotorg = '50 0 50'; }
1318 if(!this.turret_flags) { this.turret_flags = TUR_FLAG_SPLASH | TUR_FLAG_MEDPROJ | TUR_FLAG_PLAYER; }
1319 if(!this.damage_flags) { this.damage_flags = TFL_DMG_YES | TFL_DMG_RETALIATE | TFL_DMG_AIMSHAKE; }
1320 if(!this.aim_flags) { this.aim_flags = TFL_AIM_LEAD | TFL_AIM_SHOTTIMECOMPENSATE; }
1321 if(!this.track_type) { this.track_type = TFL_TRACKTYPE_STEPMOTOR; }
1322 if(!this.track_flags) { this.track_flags = TFL_TRACK_PITCH | TFL_TRACK_ROTATE; }
1323 if(!this.ammo_flags) { this.ammo_flags = TFL_AMMO_ENERGY | TFL_AMMO_RECHARGE; }
1324 if(!this.target_select_flags) { this.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_TEAMCHECK | TFL_TARGETSELECT_RANGELIMITS | TFL_TARGETSELECT_ANGLELIMITS; }
1325 if(!this.firecheck_flags) { this.firecheck_flags = TFL_FIRECHECK_DEAD | TFL_FIRECHECK_DISTANCES | TFL_FIRECHECK_LOS
1326 | TFL_FIRECHECK_AIMDIST | TFL_FIRECHECK_TEAMCHECK | TFL_FIRECHECK_AMMO_OWN | TFL_FIRECHECK_REFIRE; }
1328 if(this.track_type != TFL_TRACKTYPE_STEPMOTOR)
1330 // Fluid / Ineria mode. Looks mutch nicer.
1331 // Can reduce aim preformance alot, needs a bit diffrent aimspeed
1333 this.aim_speed = bound(0.1, ((!this.aim_speed) ? 180 : this.aim_speed), 1000);
1335 if(!this.track_accel_pitch) { this.track_accel_pitch = 0.5; }
1336 if(!this.track_accel_rotate) { this.track_accel_rotate = 0.5; }
1337 if(!this.track_blendrate) { this.track_blendrate = 0.35; }
1340 turret_initparams(this);
1342 this.turret_flags = TUR_FLAG_ISTURRET | (tur.spawnflags);
1344 if(this.turret_flags & TUR_FLAG_SPLASH)
1345 this.aim_flags |= TFL_AIM_SPLASH;
1347 if(this.turret_flags & TUR_FLAG_MISSILE)
1348 this.target_select_flags |= TFL_TARGETSELECT_MISSILES;
1350 if(this.turret_flags & TUR_FLAG_PLAYER)
1351 this.target_select_flags |= TFL_TARGETSELECT_PLAYERS;
1353 if(this.spawnflags & TSL_NO_RESPAWN)
1354 this.damage_flags |= TFL_DMG_DEATH_NORESPAWN;
1356 if (this.turret_flags & TUR_FLAG_SUPPORT)
1357 this.turret_score_target = turret_targetscore_support;
1359 this.turret_score_target = turret_targetscore_generic;
1363 _setmodel(this, tur.model);
1364 setsize(this, tur.m_mins, tur.m_maxs);
1366 this.m_id = tur.m_id;
1367 this.classname = "turret_main";
1368 this.active = ACTIVE_ACTIVE;
1369 this.effects = EF_NODRAW;
1370 this.netname = tur.turret_name;
1371 this.ticrate = bound(sys_frametime, this.ticrate, 60);
1372 this.max_health = GetResource(this, RES_HEALTH);
1373 this.target_validate_flags = this.target_select_flags;
1374 this.ammo = this.ammo_max;
1375 this.ammo_recharge *= this.ticrate;
1376 this.solid = SOLID_BBOX;
1377 this.takedamage = DAMAGE_AIM;
1378 set_movetype(this, MOVETYPE_NOCLIP);
1379 this.view_ofs = '0 0 0';
1380 this.idle_aim = '0 0 0';
1381 this.turret_firecheckfunc = turret_firecheck;
1382 this.event_damage = turret_damage;
1383 this.event_heal = turret_heal;
1384 this.use = turret_use;
1385 this.bot_attack = true;
1386 this.nextthink = time + 1;
1387 this.nextthink += turret_count * sys_frametime;
1389 this.tur_head = new(turret_head);
1390 _setmodel(this.tur_head, tur.head_model);
1391 setsize(this.tur_head, '0 0 0', '0 0 0');
1392 setorigin(this.tur_head, '0 0 0');
1393 setattachment(this.tur_head, this, "tag_head");
1395 this.tur_head.netname = this.tur_head.classname;
1396 this.tur_head.team = this.team;
1397 this.tur_head.owner = this;
1398 this.tur_head.takedamage = DAMAGE_NO;
1399 this.tur_head.solid = SOLID_NOT;
1400 set_movetype(this.tur_head, this.move_movetype);
1402 this.weaponentities[0] = this; // lol
1404 if(!this.tur_defend && this.target != "")
1405 InitializeEntity(this, turret_findtarget, INITPRIO_FINDTARGET);
1408 this.tur_debug_start = this.nextthink;
1409 while(vdist(this.tur_debug_rvec, <, 2))
1410 this.tur_debug_rvec = randomvec() * 4;
1412 this.tur_debug_rvec_x = fabs(this.tur_debug_rvec_x);
1413 this.tur_debug_rvec_y = fabs(this.tur_debug_rvec_y);
1414 this.tur_debug_rvec_z = fabs(this.tur_debug_rvec_z);
1418 turret_respawn(this);
1419 turret_tag_fire_update(this);
1421 tur.tr_setup(tur, this);
1423 if(MUTATOR_CALLHOOK(TurretSpawn, this))