1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
/**
AI Ranged Weapons
Functionality that helps the AI use ranged weapons. Handles:
* Bow
* Blunderbuss
* Grenade launcher
@author Sven2, Maikel
*/
/*-- General Ranged Weapon --*/
private func ExecuteRanged(effect fx)
{
// Still carrying the bow?
if (fx.weapon->Contained() != fx.Target)
{
fx.weapon = fx.post_aim_weapon = nil;
return false;
}
// Finish shooting process.
if (fx.post_aim_weapon)
{
// Wait max one second after shot (otherwise may be locked in wait animation forever if something goes wrong during shot).
if (FrameCounter() - fx.post_aim_weapon_time <= (fx.post_aim_weapon->~GetShootTime() ?? 35))
if (this->IsAimingOrLoading(fx))
return true;
fx.post_aim_weapon = nil;
}
// Target still in guard range?
if (!this->CheckTargetInGuardRange(fx))
return false;
// Look at target.
this->ExecuteLookAtTarget(fx);
// Make sure we can shoot.
if (!this->IsAimingOrLoading(fx) || !fx.aim_weapon || !fx.aim_weapon->~IsRangedWeapon())
{
this->CancelAiming(fx);
if (!this->CheckHandsAction(fx)) return true;
// Start aiming.
this->SelectItem(fx, fx.weapon);
if (!fx.weapon->ControlUseStart(fx.Target, fx.target->GetX() - fx.Target->GetX(), fx.target->GetY() - fx.Target->GetY()))
return false;
fx.aim_weapon = fx.weapon;
fx.aim_time = fx.time;
fx.post_aim_weapon = nil;
// Enough for now.
return;
}
// Stuck in aim procedure check?
if (GetEffect("IntAimCheckProcedure", fx.Target) && !fx.Target->ReadyToAction())
return this->ExecuteStand(fx);
// Calculate offset to target. Take movement into account.
var x = fx.Target->GetX(), y = fx.Target->GetY(), tx = fx.target->GetX(), ty = fx.target->GetY();
// Aim for the head of crew members (y-4) so it's harder to evade by jumping.
if (fx.target->GetOCF() & OCF_CrewMember)
ty = ty - 4;
// Aim just above the base of structures, this makes the changes higher for explosives to detonate on the basement or solid material.
if (fx.target->GetCategory() & C4D_Structure)
ty = ty + fx.target->GetBottom() - 2;
var d = Distance(x, y, tx, ty);
// Projected travel time of the arrow.
var dt = d * 10 / fx.projectile_speed;
tx += this->GetTargetXDir(fx.target, dt);
ty += this->GetTargetYDir(fx.target, dt);
if (!fx.target->GetContact(-1))
if (!fx.target->GetCategory() & C4D_StaticBack)
ty += GetGravity() * dt * dt / 200;
// Get shooting angle.
var shooting_angle;
if (fx.ranged_direct)
{
// For straight shots it is just the angle to the target if the path is free.
if (PathFree(x, y, tx, ty))
shooting_angle = Angle(x, y, tx, ty, 10);
}
// For ballistic shots get the angle (path free check is done inside).
// The lower of the two angles is preferentially returned.
else
shooting_angle = this->GetBallisticAngle(x, y, tx, ty, fx.projectile_speed, 160);
// If we have a valid shooting angle we can proceed.
if (shooting_angle != nil)
{
// No ally on path? Also search for allied animals, just in case.
// Ignore this if requested or if no friendly fire rules is active.
var ally;
if (!fx.ignore_allies || FindObject(Find_ID(Rule_NoFriendlyFire)))
ally = FindObject(Find_OnLine(0, 0, tx - x, ty - y), Find_Exclude(fx.Target), Find_OCF(OCF_Alive), Find_Owner(fx.Target->GetOwner()));
if (ally)
{
// Try to jump, if not possible just wait.
if (this->ExecuteJump(fx))
return true;
}
else
{
// Aim / shoot there.
x = Sin(shooting_angle, 1000, 10);
y = -Cos(shooting_angle, 1000, 10);
fx.aim_weapon->ControlUseHolding(fx.Target, x, y);
if (fx.Target->IsAiming() && fx.time >= fx.aim_time + fx.aim_weapon->GetReloadTime())
{
fx.aim_weapon->ControlUseStop(fx.Target, x, y);
// Assign post-aim status to allow slower shoot animations to pass.
fx.post_aim_weapon = fx.aim_weapon;
fx.post_aim_weapon_time = FrameCounter();
fx.aim_weapon = nil;
}
return true;
}
}
// Path not free or out of range. Just wait for enemy to come or move to it if in agressive mode.
fx.aim_weapon->ControlUseHolding(fx.Target, tx - x, ty - y);
if (fx.is_aggressive && d > 40)
fx.Target->SetCommand("MoveTo", nil, fx.target->GetX(), fx.target->GetY());
// Might also change target if current is unreachable.
var new_target;
if (!Random(3))
if (new_target = this->FindTarget(fx))
fx.target = new_target;
return true;
}
private func IsAimingOrLoading(effect fx)
{
return !!GetEffect("IntAim*", fx.Target);
}
/*-- Bow --*/
private func HasArrows(effect fx, object weapon)
{
if (weapon->Contents(0))
return true;
if (FindObject(Find_Container(fx.Target), Find_Func("IsArrow")))
return true;
return false;
}
/*-- Blunderbuss --*/
private func HasAmmo(effect fx, object weapon)
{
if (weapon->Contents(0))
return true;
if (FindObject(Find_Container(fx.Target), Find_Func("IsBullet")))
return true;
return false;
}
/*-- Grenade Launcher --*/
private func HasBombs(effect fx, object weapon)
{
if (weapon->Contents(0))
return true;
if (FindObject(Find_Container(fx.Target), Find_Func("IsGrenadeLauncherAmmo")))
return true;
return false;
}
|