1 // =========================
2 // SVQC Turret Properties
3 // =========================
7 vector turret_aim_generic()
11 float distance, impact_time = 0, i, mintime;
13 turret_tag_fire_update();
15 if(self.aim_flags & TFL_AIM_SIMPLE)
16 return real_origin(self.enemy);
18 mintime = max(self.attack_finished_single - time,0) + sys_frametime;
21 pre_pos = real_origin(self.enemy);
24 if (self.aim_flags & TFL_AIM_LEAD)
26 if (self.aim_flags & TFL_AIM_SHOTTIMECOMPENSATE) // Need to conpensate for shot traveltime
28 // FIXME: this cant be the best way to do this..
31 for(i = 0; i < 4; ++i)
33 distance = vlen(prep - self.tur_shotorg);
34 impact_time = distance / self.shot_speed;
35 prep = pre_pos + self.enemy.velocity * impact_time;
38 prep = pre_pos + (self.enemy.velocity * (impact_time + mintime));
40 if(self.aim_flags & TFL_AIM_ZPREDICT)
41 if not(self.enemy.flags & FL_ONGROUND)
42 if(self.enemy.movetype == MOVETYPE_WALK || self.enemy.movetype == MOVETYPE_TOSS || self.enemy.movetype == MOVETYPE_BOUNCE)
46 vz = self.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 + self.enemy.velocity * mintime;
59 if(self.aim_flags & TFL_AIM_SPLASH)
61 //tracebox(pre_pos + '0 0 32',self.enemy.mins,self.enemy.maxs,pre_pos -'0 0 64',MOVE_WORLDONLY,self.enemy);
62 traceline(pre_pos + '0 0 32',pre_pos -'0 0 64',MOVE_WORLDONLY,self.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_maxrotate;
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(_turret.target_range < vlen(_turret.tur_shotorg - real_origin(_target)))
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_respawn;
177 self.effects |= EF_NODRAW;
178 self.nextthink = time + self.respawntime - 0.2;
179 self.think = turret_respawn;
184 self.deadflag = DEAD_DEAD;
185 self.tur_head.deadflag = self.deadflag;
187 // Unsolidify and hide real parts
188 self.solid = SOLID_NOT;
189 self.tur_head.solid = self.solid;
191 self.event_damage = func_null;
192 self.takedamage = DAMAGE_NO;
197 //RadiusDamage (self,self, min(self.ammo,50),min(self.ammo,50) * 0.25,250,world,min(self.ammo,50)*5,DEATH_TURRET,world);
199 if(self.damage_flags & TFL_DMG_DEATH_NORESPAWN)
201 TUR_ACTION(self.turretid, TR_DEATH);
203 remove(self.tur_head);
209 self.SendFlags |= TNSF_STATUS;
210 self.nextthink = time + 0.2;
211 self.think = turret_hide;
213 TUR_ACTION(self.turretid, TR_DEATH);
217 void turret_damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector vforce)
220 if(self.deadflag == DEAD_DEAD)
223 // Inactive turrets take no damage. (hm..)
228 if (self.team == attacker.team)
230 // This does not happen anymore. Re-enable if you fix that.
231 if(IS_REAL_CLIENT(attacker))
232 sprint(attacker, "\{1}Turret tells you: I'm on your team!\n");
234 if(autocvar_g_friendlyfire)
235 damage = damage * autocvar_g_friendlyfire;
240 self.health -= damage;
242 // thorw head slightly off aim when hit?
243 if (self.damage_flags & TFL_DMG_HEADSHAKE)
245 self.tur_head.angles_x = self.tur_head.angles_x + (-0.5 + random()) * damage;
246 self.tur_head.angles_y = self.tur_head.angles_y + (-0.5 + random()) * damage;
248 self.SendFlags |= TNSF_ANG;
251 if (self.turret_flags & TUR_FLAG_MOVE)
252 self.velocity = self.velocity + vforce;
254 if (self.health <= 0)
256 self.event_damage = func_null;
257 self.tur_head.event_damage = func_null;
258 self.takedamage = DAMAGE_NO;
259 self.nextthink = time;
260 self.think = turret_die;
263 self.SendFlags |= TNSF_STATUS;
267 void turret_respawn()
269 // Make sure all parts belong to the same team since
270 // this function doubles as "teamchange" function.
271 self.tur_head.team = self.team;
272 self.effects &= ~EF_NODRAW;
273 self.deadflag = DEAD_NO;
274 self.effects = EF_LOWPRECISION;
275 self.solid = SOLID_BBOX;
276 self.takedamage = DAMAGE_AIM;
277 self.event_damage = turret_damage;
278 self.avelocity = '0 0 0';
279 self.tur_head.avelocity = self.avelocity;
280 self.tur_head.angles = self.idle_aim;
281 self.health = self.max_health;
283 self.volly_counter = self.shot_volly;
284 self.ammo = self.ammo_max;
286 self.nextthink = time + self.ticrate;
287 self.think = turret_think;
289 self.SendFlags = TNSF_FULL_UPDATE;
291 TUR_ACTION(self.turretid, TR_SETUP);
296 #define cvar_base "g_turrets_unit_"
298 void turrets_setframe(float _frame, float client_only)
300 if((client_only ? self.clientframe : self.frame ) != _frame)
302 self.SendFlags |= TNSF_ANIM;
303 self.anim_start_time = time;
307 self.clientframe = _frame;
313 float turret_send(entity to, float sf)
316 WriteByte(MSG_ENTITY, ENT_CLIENT_TURRET);
317 WriteByte(MSG_ENTITY, sf);
320 WriteByte(MSG_ENTITY, self.turretid);
322 WriteCoord(MSG_ENTITY, self.origin_x);
323 WriteCoord(MSG_ENTITY, self.origin_y);
324 WriteCoord(MSG_ENTITY, self.origin_z);
326 WriteAngle(MSG_ENTITY, self.angles_x);
327 WriteAngle(MSG_ENTITY, self.angles_y);
332 WriteShort(MSG_ENTITY, rint(self.tur_head.angles_x));
333 WriteShort(MSG_ENTITY, rint(self.tur_head.angles_y));
338 WriteShort(MSG_ENTITY, rint(self.tur_head.avelocity_x));
339 WriteShort(MSG_ENTITY, rint(self.tur_head.avelocity_y));
344 WriteShort(MSG_ENTITY, rint(self.origin_x));
345 WriteShort(MSG_ENTITY, rint(self.origin_y));
346 WriteShort(MSG_ENTITY, rint(self.origin_z));
348 WriteShort(MSG_ENTITY, rint(self.velocity_x));
349 WriteShort(MSG_ENTITY, rint(self.velocity_y));
350 WriteShort(MSG_ENTITY, rint(self.velocity_z));
352 WriteShort(MSG_ENTITY, rint(self.angles_y));
357 WriteCoord(MSG_ENTITY, self.anim_start_time);
358 WriteByte(MSG_ENTITY, self.frame);
363 WriteByte(MSG_ENTITY, self.team);
366 WriteByte(MSG_ENTITY, 0);
368 WriteByte(MSG_ENTITY, ceil((self.health / self.max_health) * 255));
374 void load_unit_settings(entity ent, string unitname, float is_reload)
381 if not (ent.turret_scale_damage) ent.turret_scale_damage = 1;
382 if not (ent.turret_scale_range) ent.turret_scale_range = 1;
383 if not (ent.turret_scale_refire) ent.turret_scale_refire = 1;
384 if not (ent.turret_scale_ammo) ent.turret_scale_ammo = 1;
385 if not (ent.turret_scale_aim) ent.turret_scale_aim = 1;
386 if not (ent.turret_scale_health) ent.turret_scale_health = 1;
387 if not (ent.turret_scale_respawn) ent.turret_scale_respawn = 1;
389 sbase = strcat(cvar_base,unitname);
393 ent.tur_head.avelocity = '0 0 0';
395 ent.tur_head.angles = '0 0 0';
398 ent.health = cvar(strcat(sbase,"_health")) * ent.turret_scale_health;
399 ent.respawntime = cvar(strcat(sbase,"_respawntime")) * ent.turret_scale_respawn;
401 ent.shot_dmg = cvar(strcat(sbase,"_shot_dmg")) * ent.turret_scale_damage;
402 ent.shot_refire = cvar(strcat(sbase,"_shot_refire")) * ent.turret_scale_refire;
403 ent.shot_radius = cvar(strcat(sbase,"_shot_radius")) * ent.turret_scale_damage;
404 ent.shot_speed = cvar(strcat(sbase,"_shot_speed"));
405 ent.shot_spread = cvar(strcat(sbase,"_shot_spread"));
406 ent.shot_force = cvar(strcat(sbase,"_shot_force")) * ent.turret_scale_damage;
407 ent.shot_volly = cvar(strcat(sbase,"_shot_volly"));
408 ent.shot_volly_refire = cvar(strcat(sbase,"_shot_volly_refire")) * ent.turret_scale_refire;
410 ent.target_range = cvar(strcat(sbase,"_target_range")) * ent.turret_scale_range;
411 ent.target_range_min = cvar(strcat(sbase,"_target_range_min")) * ent.turret_scale_range;
412 ent.target_range_optimal = cvar(strcat(sbase,"_target_range_optimal")) * ent.turret_scale_range;
413 //ent.target_range_fire = cvar(strcat(sbase,"_target_range_fire")) * ent.turret_scale_range;
415 ent.target_select_rangebias = cvar(strcat(sbase,"_target_select_rangebias"));
416 ent.target_select_samebias = cvar(strcat(sbase,"_target_select_samebias"));
417 ent.target_select_anglebias = cvar(strcat(sbase,"_target_select_anglebias"));
418 ent.target_select_playerbias = cvar(strcat(sbase,"_target_select_playerbias"));
419 //ent.target_select_fov = cvar(cvar_gets(sbase,"_target_select_fov"));
421 ent.ammo_max = cvar(strcat(sbase,"_ammo_max")) * ent.turret_scale_ammo;
422 ent.ammo_recharge = cvar(strcat(sbase,"_ammo_recharge")) * ent.turret_scale_ammo;
424 ent.aim_firetolerance_dist = cvar(strcat(sbase,"_aim_firetolerance_dist"));
425 ent.aim_speed = cvar(strcat(sbase,"_aim_speed")) * ent.turret_scale_aim;
426 ent.aim_maxrotate = cvar(strcat(sbase,"_aim_maxrot"));
427 ent.aim_maxpitch = cvar(strcat(sbase,"_aim_maxpitch"));
429 ent.track_type = cvar(strcat(sbase,"_track_type"));
430 ent.track_accel_pitch = cvar(strcat(sbase,"_track_accel_pitch"));
431 ent.track_accel_rotate = cvar(strcat(sbase,"_track_accel_rot"));
432 ent.track_blendrate = cvar(strcat(sbase,"_track_blendrate"));
435 TUR_ACTION(self.turretid, TR_SETUP);
438 void turret_projectile_explode()
441 self.takedamage = DAMAGE_NO;
442 self.event_damage = func_null;
445 d = RadiusDamage (self, self.owner, self.owner.shot_dmg, 0, self.owner.shot_radius, self, self.owner.shot_force, self.totalfrags, world);
446 self.owner.tur_debug_dmg_t_h = self.owner.tur_debug_dmg_t_h + d;
447 self.owner.tur_debug_dmg_t_f = self.owner.tur_debug_dmg_t_f + self.owner.shot_dmg;
449 RadiusDamage (self, self.realowner, self.owner.shot_dmg, 0, self.owner.shot_radius, self, self.owner.shot_force, self.totalfrags, world);
454 void turret_projectile_touch()
457 turret_projectile_explode();
460 void turret_projectile_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector vforce)
462 self.velocity += vforce;
463 self.health -= damage;
464 //self.realowner = attacker; // Dont change realowner, it does not make much sense for turrets
466 W_PrepareExplosionByDamage(self.owner, turret_projectile_explode);
469 entity turret_projectile(string _snd, float _size, float _health, float _death, float _proj_type, float _cull, float _cli_anim)
473 sound (self, CH_WEAPON_A, _snd, VOL_BASE, ATTEN_NORM);
475 setorigin(proj, self.tur_shotorg);
476 setsize(proj, '-0.5 -0.5 -0.5' * _size, '0.5 0.5 0.5' * _size);
478 proj.realowner = self;
479 proj.bot_dodge = TRUE;
480 proj.bot_dodgerating = self.shot_dmg;
481 proj.think = turret_projectile_explode;
482 proj.touch = turret_projectile_touch;
483 proj.nextthink = time + 9;
484 proj.movetype = MOVETYPE_FLYMISSILE;
485 proj.velocity = normalize(self.tur_shotdir_updated + randomvec() * self.shot_spread) * self.shot_speed;
486 proj.flags = FL_PROJECTILE;
487 proj.enemy = self.enemy;
488 proj.totalfrags = _death;
489 PROJECTILE_MAKETRIGGER(proj);
492 proj.health = _health;
493 proj.takedamage = DAMAGE_YES;
494 proj.event_damage = turret_projectile_damage;
497 proj.flags |= FL_NOTARGET;
499 CSQCProjectile(proj, _cli_anim, _proj_type, _cull);
505 ** updates enemy distances, predicted impact point/time
506 ** and updated aim<->predict impact distance.
508 void turret_do_updates(entity t_turret)
516 enemy_pos = real_origin(self.enemy);
518 turret_tag_fire_update();
520 self.tur_shotdir_updated = v_forward;
521 self.tur_dist_enemy = vlen(self.tur_shotorg - enemy_pos);
522 self.tur_dist_aimpos = vlen(self.tur_shotorg - self.tur_aimpos);
524 /*if((self.firecheck_flags & TFL_FIRECHECK_VERIFIED) && (self.enemy))
526 oldpos = self.enemy.origin;
527 setorigin(self.enemy, self.tur_aimpos);
528 tracebox(self.tur_shotorg, '-1 -1 -1', '1 1 1', self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos), MOVE_NORMAL,self);
529 setorigin(self.enemy, oldpos);
531 if(trace_ent == self.enemy)
532 self.tur_dist_impact_to_aimpos = 0;
534 self.tur_dist_impact_to_aimpos = vlen(trace_endpos - self.tur_aimpos);
537 tracebox(self.tur_shotorg, '-1 -1 -1','1 1 1', self.tur_shotorg + (self.tur_shotdir_updated * self.tur_dist_aimpos), MOVE_NORMAL,self);
539 self.tur_dist_impact_to_aimpos = vlen(trace_endpos - self.tur_aimpos) - (vlen(self.enemy.maxs - self.enemy.mins) * 0.5);
540 self.tur_impactent = trace_ent;
541 self.tur_impacttime = vlen(self.tur_shotorg - trace_endpos) / self.shot_speed;
547 ** Handles head rotation according to
548 ** the units .track_type and .track_flags
550 .float turret_framecounter;
553 vector target_angle; // This is where we want to aim
554 vector move_angle; // This is where we can aim
557 v1 = self.tur_head.angles;
558 v2 = self.tur_head.avelocity;
560 if (self.track_flags == TFL_TRACK_NO)
564 target_angle = self.idle_aim - ('1 0 0' * self.aim_maxpitch);
565 else if (self.enemy == world)
568 target_angle = self.idle_aim + self.angles;
570 target_angle = vectoangles(normalize(self.tur_aimpos - self.tur_shotorg));
574 target_angle = vectoangles(normalize(self.tur_aimpos - self.tur_shotorg));
577 self.tur_head.angles_x = anglemods(self.tur_head.angles_x);
578 self.tur_head.angles_y = anglemods(self.tur_head.angles_y);
580 // Find the diffrence between where we currently aim and where we want to aim
581 //move_angle = target_angle - (self.angles + self.tur_head.angles);
582 //move_angle = shortangle_vxy(move_angle,(self.angles + self.tur_head.angles));
584 move_angle = AnglesTransform_ToAngles(AnglesTransform_LeftDivide(AnglesTransform_FromAngles(self.angles), AnglesTransform_FromAngles(target_angle))) - self.tur_head.angles;
585 move_angle = shortangle_vxy(move_angle, self.tur_head.angles);
587 switch(self.track_type)
589 case TFL_TRACKTYPE_STEPMOTOR:
590 f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
591 if (self.track_flags & TFL_TRACK_PITCH)
593 self.tur_head.angles_x += bound(-f_tmp,move_angle_x, f_tmp);
594 if(self.tur_head.angles_x > self.aim_maxpitch)
595 self.tur_head.angles_x = self.aim_maxpitch;
597 if(self.tur_head.angles_x < -self.aim_maxpitch)
598 self.tur_head.angles_x = self.aim_maxpitch;
601 if (self.track_flags & TFL_TRACK_ROTATE)
603 self.tur_head.angles_y += bound(-f_tmp, move_angle_y, f_tmp);
604 if(self.tur_head.angles_y > self.aim_maxrotate)
605 self.tur_head.angles_y = self.aim_maxrotate;
607 if(self.tur_head.angles_y < -self.aim_maxrotate)
608 self.tur_head.angles_y = self.aim_maxrotate;
612 self.SendFlags |= TNSF_ANG;
616 case TFL_TRACKTYPE_FLUIDINERTIA:
617 f_tmp = self.aim_speed * self.ticrate; // dgr/sec -> dgr/tic
618 move_angle_x = bound(-self.aim_speed, move_angle_x * self.track_accel_pitch * f_tmp, self.aim_speed);
619 move_angle_y = bound(-self.aim_speed, move_angle_y * self.track_accel_rotate * f_tmp, self.aim_speed);
620 move_angle = (self.tur_head.avelocity * self.track_blendrate) + (move_angle * (1 - self.track_blendrate));
623 case TFL_TRACKTYPE_FLUIDPRECISE:
625 move_angle_y = bound(-self.aim_speed, move_angle_y, self.aim_speed);
626 move_angle_x = bound(-self.aim_speed, move_angle_x, self.aim_speed);
632 if (self.track_flags & TFL_TRACK_PITCH)
634 self.tur_head.avelocity_x = move_angle_x;
635 if((self.tur_head.angles_x + self.tur_head.avelocity_x * self.ticrate) > self.aim_maxpitch)
637 self.tur_head.avelocity_x = 0;
638 self.tur_head.angles_x = self.aim_maxpitch;
640 self.SendFlags |= TNSF_ANG;
643 if((self.tur_head.angles_x + self.tur_head.avelocity_x * self.ticrate) < -self.aim_maxpitch)
645 self.tur_head.avelocity_x = 0;
646 self.tur_head.angles_x = -self.aim_maxpitch;
648 self.SendFlags |= TNSF_ANG;
653 if (self.track_flags & TFL_TRACK_ROTATE)
655 self.tur_head.avelocity_y = move_angle_y;
657 if((self.tur_head.angles_y + self.tur_head.avelocity_y * self.ticrate) > self.aim_maxrotate)
659 self.tur_head.avelocity_y = 0;
660 self.tur_head.angles_y = self.aim_maxrotate;
662 self.SendFlags |= TNSF_ANG;
665 if((self.tur_head.angles_y + self.tur_head.avelocity_y * self.ticrate) < -self.aim_maxrotate)
667 self.tur_head.avelocity_y = 0;
668 self.tur_head.angles_y = -self.aim_maxrotate;
670 self.SendFlags |= TNSF_ANG;
674 self.SendFlags |= TNSF_AVEL;
676 // Force a angle update every 10'th frame
677 self.turret_framecounter += 1;
678 if(self.turret_framecounter >= 10)
680 self.SendFlags |= TNSF_ANG;
681 self.turret_framecounter = 0;
686 + TFL_TARGETSELECT_NO
687 + TFL_TARGETSELECT_LOS
688 + TFL_TARGETSELECT_PLAYERS
689 + TFL_TARGETSELECT_MISSILES
690 - TFL_TARGETSELECT_TRIGGERTARGET
691 + TFL_TARGETSELECT_ANGLELIMITS
692 + TFL_TARGETSELECT_RANGELIMITS
693 + TFL_TARGETSELECT_TEAMCHECK
694 - TFL_TARGETSELECT_NOBUILTIN
695 + TFL_TARGETSELECT_OWNTEAM
699 ** Evaluate a entity for target valitity based on validate_flags
700 ** NOTE: the caller must check takedamage before calling this, to inline this check.
702 float turret_validate_target(entity e_turret, entity e_target, float validate_flags)
706 //if(!validate_flags & TFL_TARGETSELECT_NOBUILTIN)
709 if(e_target.owner == e_turret)
712 if not(checkpvs(e_target.origin, e_turret))
719 if (substring(e_target.classname, 0, 10) == "onslaught_") // don't attack onslaught targets, that's the player's job!
722 if (validate_flags & TFL_TARGETSELECT_NO)
725 // If only this was used more..
726 if (e_target.flags & FL_NOTARGET)
730 if(e_target.vehicle_flags & VHF_ISVEHICLE)
732 if (e_target.vehicle_health <= 0)
735 else if (e_target.health <= 0)
739 if (IS_CLIENT(e_target))
741 if not (validate_flags & TFL_TARGETSELECT_PLAYERS)
744 if (e_target.deadflag != DEAD_NO)
749 if(validate_flags & TFL_TARGETSELECT_NOTURRETS)
750 if(e_target.owner.tur_head == e_target)
751 if(e_target.team != e_turret.team) // Dont break support units.
755 if (e_target.flags & FL_PROJECTILE)
756 if not (validate_flags & TFL_TARGETSELECT_MISSILES)
759 if (validate_flags & TFL_TARGETSELECT_MISSILESONLY)
760 if not (e_target.flags & FL_PROJECTILE)
764 if (validate_flags & TFL_TARGETSELECT_TEAMCHECK)
766 if (validate_flags & TFL_TARGETSELECT_OWNTEAM)
768 if (e_target.team != e_turret.team)
771 if (e_turret.team != e_target.owner.team)
776 if (e_target.team == e_turret.team)
779 if (e_turret.team == e_target.owner.team)
785 tvt_dist = vlen(e_turret.origin - real_origin(e_target));
786 if (validate_flags & TFL_TARGETSELECT_RANGELIMITS)
788 if (tvt_dist < e_turret.target_range_min)
791 if (tvt_dist > e_turret.target_range)
795 // Can we even aim this thing?
796 tvt_thadv = angleofs3(e_turret.tur_head.origin, e_turret.angles + e_turret.tur_head.angles, e_target);
797 tvt_tadv = shortangle_vxy(angleofs(e_turret, e_target), e_turret.angles);
798 tvt_thadf = vlen(tvt_thadv);
799 tvt_tadf = vlen(tvt_tadv);
802 if(validate_flags & TFL_TARGETSELECT_FOV)
804 if(e_turret.target_select_fov < tvt_thadf)
809 if (validate_flags & TFL_TARGETSELECT_ANGLELIMITS)
811 if (fabs(tvt_tadv_x) > e_turret.aim_maxpitch)
814 if (fabs(tvt_tadv_y) > e_turret.aim_maxrotate)
819 if (validate_flags & TFL_TARGETSELECT_LOS)
821 v_tmp = real_origin(e_target) + ((e_target.mins + e_target.maxs) * 0.5);
823 traceline(e_turret.origin + '0 0 16', v_tmp, 0, e_turret);
825 if (e_turret.aim_firetolerance_dist < vlen(v_tmp - trace_endpos))
829 if (e_target.classname == "grapplinghook")
833 if (e_target.classname == "func_button")
837 #ifdef TURRET_DEBUG_TARGETSELECT
838 dprint("Target:",e_target.netname," is a valid target for ",e_turret.netname,"\n");
844 entity turret_select_target()
846 entity e; // target looper entity
847 float score; // target looper entity score
848 entity e_enemy; // currently best scoreing target
849 float m_score; // currently best scoreing target's score
852 if(self.enemy && self.enemy.takedamage && turret_validate_target(self,self.enemy,self.target_validate_flags) > 0)
854 e_enemy = self.enemy;
855 m_score = self.turret_score_target(self,e_enemy) * self.target_select_samebias;
858 e_enemy = self.enemy = world;
860 e = findradius(self.origin, self.target_range);
862 // Nothing to aim at?
870 float f = turret_validate_target(self, e, self.target_select_flags);
871 //dprint("F is: ", ftos(f), "\n");
874 score = self.turret_score_target(self,e);
875 if ((score > m_score) && (score > 0))
894 + TFL_FIRECHECK_WORLD
896 + TFL_FIRECHECK_DISTANCES
898 + TFL_FIRECHECK_AIMDIST
899 + TFL_FIRECHECK_REALDIST
900 - TFL_FIRECHECK_ANGLEDIST
901 - TFL_FIRECHECK_TEAMCECK
903 + TFL_FIRECHECK_AMMO_OWN
904 + TFL_FIRECHECK_AMMO_OTHER
905 + TFL_FIRECHECK_REFIRE
909 ** Preforms pre-fire checks based on the uints firecheck_flags
911 float turret_firecheck()
913 // This one just dont care =)
914 if (self.firecheck_flags & TFL_FIRECHECK_NO)
917 if (self.enemy == world)
921 if (self.firecheck_flags & TFL_FIRECHECK_REFIRE)
922 if (self.attack_finished_single > time) return 0;
924 // Special case: volly fire turret that has to fire a full volly if a shot was fired.
925 if (self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
926 if (self.volly_counter != self.shot_volly)
927 if(self.ammo >= self.shot_dmg)
930 // Lack of zombies makes shooting dead things unnecessary :P
931 if (self.firecheck_flags & TFL_FIRECHECK_DEAD)
932 if (self.enemy.deadflag != DEAD_NO)
936 if (self.firecheck_flags & TFL_FIRECHECK_AMMO_OWN)
937 if (self.ammo < self.shot_dmg)
940 // Other's ammo? (support-supply units)
941 if (self.firecheck_flags & TFL_FIRECHECK_AMMO_OTHER)
942 if (self.enemy.ammo >= self.enemy.ammo_max)
945 // Target of opertunity?
946 if(turret_validate_target(self, self.tur_impactent, self.target_validate_flags) > 0)
948 self.enemy = self.tur_impactent;
952 if (self.firecheck_flags & TFL_FIRECHECK_DISTANCES)
955 if (self.tur_dist_aimpos < self.target_range_min)
956 if(turret_validate_target(self, self.tur_impactent, self.target_validate_flags) > 0)
957 return 1; // Target of opertunity?
963 if (self.firecheck_flags & TFL_FIRECHECK_AFF)
964 if (self.tur_impactent.team == self.team)
967 // aim<->predicted impact
968 if (self.firecheck_flags & TFL_FIRECHECK_AIMDIST)
969 if (self.tur_dist_impact_to_aimpos > self.aim_firetolerance_dist)
973 if (self.shot_volly > 1)
974 if (self.volly_counter == self.shot_volly)
975 if (self.ammo < (self.shot_dmg * self.shot_volly))
978 /*if(self.firecheck_flags & TFL_FIRECHECK_VERIFIED)
979 if(self.tur_impactent != self.enemy)
987 if (autocvar_g_turrets_nofire != 0)
990 TUR_ACTION(self.turretid, TR_ATTACK);
992 self.attack_finished_single = time + self.shot_refire;
993 self.ammo -= self.shot_dmg;
994 self.volly_counter = self.volly_counter - 1;
996 if (self.volly_counter <= 0)
998 self.volly_counter = self.shot_volly;
1000 if (self.shoot_flags & TFL_SHOOT_CLEARTARGET)
1003 if (self.shot_volly > 1)
1004 self.attack_finished_single = time + self.shot_volly_refire;
1008 if (self.enemy) paint_target3(self.tur_aimpos, 64, self.tur_debug_rvec, self.tur_impacttime + 0.25);
1016 self.nextthink = time + self.ticrate;
1018 // ONS uses somewhat backwards linking.
1024 e = find(world, targetname,self.target);
1029 if (self.team != self.tur_head.team)
1034 if (self.tur_debug_tmr1 < time)
1036 if (self.enemy) paint_target (self.enemy,128,self.tur_debug_rvec,0.9);
1037 paint_target(self,256,self.tur_debug_rvec,0.9);
1038 self.tur_debug_tmr1 = time + 1;
1043 if not (self.spawnflags & TSF_NO_AMMO_REGEN)
1044 if (self.ammo < self.ammo_max)
1045 self.ammo = min(self.ammo + self.ammo_recharge, self.ammo_max);
1047 // Inactive turrets needs to run the think loop,
1048 // So they can handle animation and wake up if need be.
1049 if not (self.active)
1055 // This is typicaly used for zaping every target in range
1056 // turret_fusionreactor uses this to recharge friendlys.
1057 if (self.shoot_flags & TFL_SHOOT_HITALLVALID)
1059 // Do a self.turret_fire for every valid target.
1060 e = findradius(self.origin,self.target_range);
1065 if (turret_validate_target(self,e,self.target_validate_flags))
1069 turret_do_updates(self);
1071 if (self.turret_firecheckfunc())
1080 else if(self.shoot_flags & TFL_SHOOT_CUSTOM)
1082 // This one is doing something.. oddball. assume its handles what needs to be handled.
1085 if not(self.aim_flags & TFL_AIM_NO)
1086 self.tur_aimpos = turret_aim_generic();
1089 if not(self.track_flags & TFL_TRACK_NO)
1092 turret_do_updates(self);
1095 if (self.turret_firecheckfunc())
1100 // Special case for volly always. if it fired once it must compleate the volly.
1101 if(self.shoot_flags & TFL_SHOOT_VOLLYALWAYS)
1102 if(self.volly_counter != self.shot_volly)
1104 // Predict or whatnot
1105 if not(self.aim_flags & TFL_AIM_NO)
1106 self.tur_aimpos = turret_aim_generic();
1109 if not(self.track_flags & TFL_TRACK_NO)
1112 turret_do_updates(self);
1115 if (self.turret_firecheckfunc() != 0)
1118 TUR_ACTION(self.turretid, TR_THINK);
1123 // Check if we have a vailid enemy, and try to find one if we dont.
1125 // g_turrets_targetscan_maxdelay forces a target re-scan at least this often
1126 float do_target_scan = 0;
1127 if((self.target_select_time + autocvar_g_turrets_targetscan_maxdelay) < time)
1130 // Old target (if any) invalid?
1131 if(self.target_validate_time < time)
1132 if (turret_validate_target(self, self.enemy, self.target_validate_flags) <= 0)
1135 self.target_validate_time = time + 0.5;
1139 // But never more often then g_turrets_targetscan_mindelay!
1140 if (self.target_select_time + autocvar_g_turrets_targetscan_mindelay > time)
1145 self.enemy = turret_select_target();
1146 self.target_select_time = time;
1149 // No target, just go to idle, do any custom stuff and bail.
1150 if (self.enemy == world)
1153 if not(self.track_flags & TFL_TRACK_NO)
1156 TUR_ACTION(self.turretid, TR_THINK);
1162 self.lip = time + autocvar_g_turrets_aimidle_delay; // Keep track of the last time we had a target.
1165 if not(self.aim_flags & TFL_AIM_NO)
1166 self.tur_aimpos = turret_aim_generic();
1169 if not(self.track_flags & TFL_TRACK_NO)
1172 turret_do_updates(self);
1175 if (self.turret_firecheckfunc())
1179 TUR_ACTION(self.turretid, TR_THINK);
1183 When .used a turret switch team to activator.team.
1184 If activator is world, the turret go inactive.
1188 dprint("Turret ",self.netname, " used by ", activator.classname, "\n");
1190 self.team = activator.team;
1193 self.active = ACTIVE_NOT;
1195 self.active = ACTIVE_ACTIVE;
1201 Net_LinkEntity(self, TRUE, 0, turret_send);
1202 self.think = turret_think;
1203 self.nextthink = time;
1204 self.tur_head.effects = EF_NODRAW;
1207 void turrets_manager_think()
1209 self.nextthink = time + 1;
1212 if (autocvar_g_turrets_reloadcvars == 1)
1217 if (e.turret_flags & TUR_FLAG_ISTURRET)
1219 load_unit_settings(e,e.cvar_basename,1);
1220 TUR_ACTION(self.turretid, TR_THINK);
1225 cvar_set("g_turrets_reloadcvars","0");
1229 float turret_initialize(float tur_id)
1231 if not(autocvar_g_turrets)
1235 entity tur = get_turretinfo(tur_id);
1236 if(tur.turretid == 0)
1237 return FALSE; // invalid turret
1239 e = find(world, classname, "turret_manager");
1243 e.classname = "turret_manager";
1244 e.think = turrets_manager_think;
1245 e.nextthink = time + 2;
1248 if not(self.spawnflags & TSF_SUSPENDED)
1249 builtin_droptofloor();
1251 self.cvar_basename = tur.cvar_basename;
1252 load_unit_settings(self, self.cvar_basename, 0);
1254 if not(self.team || teamplay) { self.team = MAX_SHOT_DISTANCE; }
1255 if not(self.ticrate) { self.ticrate = ((self.turret_flags & TUR_FLAG_SUPPORT) ? 0.2 : 0.1); }
1256 if not(self.health) { self.health = 1000; }
1257 if not(self.shot_refire) { self.shot_refire = 1; }
1258 if not(self.tur_shotorg) { self.tur_shotorg = '50 0 50'; }
1259 if not(self.turret_flags) { self.turret_flags = TUR_FLAG_SPLASH | TUR_FLAG_MEDPROJ | TUR_FLAG_PLAYER; }
1260 if not(self.damage_flags) { self.damage_flags = TFL_DMG_YES | TFL_DMG_RETALIATE | TFL_DMG_AIMSHAKE; }
1261 if not(self.aim_flags) { self.aim_flags = TFL_AIM_LEAD | TFL_AIM_SHOTTIMECOMPENSATE; }
1262 if not(self.track_type) { self.track_type = TFL_TRACKTYPE_STEPMOTOR; }
1263 if not(self.track_flags) { self.track_flags = TFL_TRACK_PITCH | TFL_TRACK_ROTATE; }
1264 if not(self.ammo_flags) { self.ammo_flags = TFL_AMMO_ENERGY | TFL_AMMO_RECHARGE; }
1265 if not(self.target_select_flags){ self.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_TEAMCHECK | TFL_TARGETSELECT_RANGELIMITS | TFL_TARGETSELECT_ANGLELIMITS; }
1266 if not(self.firecheck_flags) { self.firecheck_flags = TFL_FIRECHECK_DEAD | TFL_FIRECHECK_DISTANCES | TFL_FIRECHECK_LOS
1267 | TFL_FIRECHECK_AIMDIST | TFL_FIRECHECK_TEAMCHECK | TFL_FIRECHECK_AMMO_OWN | TFL_FIRECHECK_REFIRE; }
1269 if(self.track_type != TFL_TRACKTYPE_STEPMOTOR)
1271 // Fluid / Ineria mode. Looks mutch nicer.
1272 // Can reduce aim preformance alot, needs a bit diffrent aimspeed
1274 self.aim_speed = bound(0.1, ((!self.aim_speed) ? 180 : self.aim_speed), 1000);
1276 if not(self.track_accel_pitch) { self.track_accel_pitch = 0.5; }
1277 if not(self.track_accel_rotate) { self.track_accel_rotate = 0.5; }
1278 if not(self.track_blendrate) { self.track_blendrate = 0.35; }
1281 self.respawntime = max(-1, ((!self.respawntime) ? 60 : self.respawntime));
1282 self.shot_refire = bound(0.01, ((!self.shot_refire) ? 1 : self.shot_refire), 9999);
1283 self.shot_dmg = max(1, ((!self.shot_dmg) ? self.shot_refire * 50 : self.shot_dmg));
1284 self.shot_radius = max(1, ((!self.shot_radius) ? self.shot_dmg * 0.5 : self.shot_radius));
1285 self.shot_speed = max(1, ((!self.shot_speed) ? 2500 : self.shot_speed));
1286 self.shot_spread = bound(0.0001, ((!self.shot_spread) ? 0.0125 : self.shot_spread), 500);
1287 self.shot_force = bound(0.001, ((!self.shot_force) ? self.shot_dmg * 0.5 + self.shot_radius * 0.5 : self.shot_force), 5000);
1288 self.shot_volly = bound(1, ((!self.shot_volly) ? 1 : self.shot_volly), floor(self.ammo_max / self.shot_dmg));
1289 self.shot_volly_refire = bound(self.shot_refire, ((!self.shot_volly_refire) ? self.shot_refire * self.shot_volly : self.shot_volly_refire), 60);
1290 self.target_range = bound(0, ((!self.target_range) ? self.shot_speed * 0.5 : self.target_range), MAX_SHOT_DISTANCE);
1291 self.target_range_min = bound(0, ((!self.target_range_min) ? self.shot_radius * 2 : self.target_range_min), MAX_SHOT_DISTANCE);
1292 self.target_range_optimal = bound(0, ((!self.target_range_optimal) ? self.target_range * 0.5 : self.target_range_optimal), MAX_SHOT_DISTANCE);
1293 self.aim_maxrotate = bound(0, ((!self.aim_maxrotate) ? 90 : self.aim_maxrotate), 360);
1294 self.aim_maxpitch = bound(0, ((!self.aim_maxpitch) ? 20 : self.aim_maxpitch), 90);
1295 self.aim_speed = bound(0.1, ((!self.aim_speed) ? 36 : self.aim_speed), 1000);
1296 self.aim_firetolerance_dist = bound(0.1, ((!self.aim_firetolerance_dist) ? 5 + (self.shot_radius * 2) : self.aim_firetolerance_dist), MAX_SHOT_DISTANCE);
1297 self.target_select_rangebias = bound(-10, ((!self.target_select_rangebias) ? 1 : self.target_select_rangebias), 10);
1298 self.target_select_samebias = bound(-10, ((!self.target_select_samebias) ? 1 : self.target_select_samebias), 10);
1299 self.target_select_anglebias = bound(-10, ((!self.target_select_anglebias) ? 1 : self.target_select_anglebias), 10);
1300 self.target_select_missilebias = bound(-10, ((!self.target_select_missilebias) ? 1 : self.target_select_missilebias), 10);
1301 self.target_select_playerbias = bound(-10, ((!self.target_select_playerbias) ? 1 : self.target_select_playerbias), 10);
1302 self.ammo_max = max(self.shot_dmg, ((!self.ammo_max) ? self.shot_dmg * 10 : self.ammo_max));
1303 self.ammo_recharge = max(0, ((!self.ammo_recharge) ? self.shot_dmg * 0.5 : self.ammo_recharge));
1305 self.turret_flags = TUR_FLAG_ISTURRET | (tur.spawnflags);
1307 if(self.turret_flags & TUR_FLAG_SPLASH)
1308 self.aim_flags |= TFL_AIM_SPLASH;
1310 if(self.turret_flags & TUR_FLAG_MISSILE)
1311 self.target_select_flags |= TFL_TARGETSELECT_MISSILES;
1313 if(self.turret_flags & TUR_FLAG_PLAYER)
1314 self.target_select_flags |= TFL_TARGETSELECT_PLAYERS;
1316 if(self.spawnflags & TSL_NO_RESPAWN)
1317 self.damage_flags |= TFL_DMG_DEATH_NORESPAWN;
1319 if (self.turret_flags & TUR_FLAG_SUPPORT)
1320 self.turret_score_target = turret_targetscore_support;
1322 self.turret_score_target = turret_targetscore_generic;
1326 setmodel(self, tur.model);
1327 setsize(self, tur.mins, tur.maxs);
1329 self.turretid = tur_id;
1330 self.classname = "turret_main";
1331 self.active = ACTIVE_ACTIVE;
1332 self.effects = EF_NODRAW;
1333 self.netname = TUR_NAME(tur_id);
1334 self.ticrate = bound(sys_frametime, self.ticrate, 60);
1335 self.max_health = self.health;
1336 self.target_validate_flags = self.target_select_flags;
1337 self.ammo = self.ammo_max;
1338 self.ammo_recharge *= self.ticrate;
1339 self.solid = SOLID_BBOX;
1340 self.takedamage = DAMAGE_AIM;
1341 self.movetype = MOVETYPE_NOCLIP;
1342 self.view_ofs = '0 0 0';
1343 self.turret_firecheckfunc = turret_firecheck;
1344 self.event_damage = turret_damage;
1345 self.use = turret_use;
1346 self.bot_attack = TRUE;
1347 self.nextthink = time + 1;
1348 self.nextthink += turret_count * sys_frametime;
1350 self.tur_head = spawn();
1351 setmodel(self.tur_head, tur.head_model);
1352 setsize(self.tur_head, '0 0 0', '0 0 0');
1353 setorigin(self.tur_head, '0 0 0');
1354 setattachment(self.tur_head, self, "tag_head");
1356 self.tur_head.netname = self.tur_head.classname = "turret_head";
1357 self.tur_head.team = self.team;
1358 self.tur_head.owner = self;
1359 self.tur_head.takedamage = DAMAGE_NO;
1360 self.tur_head.solid = SOLID_NOT;
1361 self.tur_head.movetype = self.movetype;
1363 if not(self.tur_defend)
1364 if(self.target != "")
1366 self.tur_defend = find(world, targetname, self.target);
1367 if (self.tur_defend == world)
1370 dprint("Turret has invalid defendpoint!\n");
1374 if (self.tur_defend)
1375 self.idle_aim = self.tur_head.angles + angleofs(self.tur_head, self.tur_defend);
1377 self.idle_aim = '0 0 0';
1380 self.tur_debug_start = self.nextthink;
1381 while (vlen(self.tur_debug_rvec) < 2)
1382 self.tur_debug_rvec = randomvec() * 4;
1384 self.tur_debug_rvec_x = fabs(self.tur_debug_rvec_x);
1385 self.tur_debug_rvec_y = fabs(self.tur_debug_rvec_y);
1386 self.tur_debug_rvec_z = fabs(self.tur_debug_rvec_z);
1391 turret_tag_fire_update();
1393 TUR_ACTION(tur_id, TR_SETUP);
1395 if(MUTATOR_CALLHOOK(TurretSpawn))