-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Code(feat) fighter ai #17
base: experimental
Are you sure you want to change the base?
Changes from all commits
4913eca
2ee13f0
b0ac3e1
7525f98
e6a9003
814e622
1e5e437
487d12b
112611c
560faaa
0936b18
03368ca
b9c3a15
e3be745
617f55f
2934c5a
37bd67f
867a285
845545c
39cf643
7159a93
34ab72b
4fa77cb
bf27d35
b5d648a
783c9c0
1dad564
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -2456,6 +2456,39 @@ | |||
|
||||
|
||||
|
||||
bool AI::MoveThrough(Ship &ship, Command &command, const Point &targetPosition, | ||||
const Point &targetVelocity, double radius, double slow) | ||||
{ | ||||
const Point &position = ship.Position(); | ||||
const Point &velocity = ship.Velocity(); | ||||
const Angle &angle = ship.Facing(); | ||||
Point dp = targetPosition - position; | ||||
Point dv = targetVelocity - velocity; | ||||
|
||||
double speed = dv.Length(); | ||||
|
||||
bool isClose = (dp.Length() < radius); | ||||
if(isClose && speed < slow) | ||||
return true; | ||||
|
||||
bool shouldReverse = false; | ||||
// dp = targetPosition - StoppingPoint(ship, targetVelocity, shouldReverse); | ||||
bool isFacing = (dp.Unit().Dot(angle.Unit()) > .95); | ||||
if(!isClose || (!isFacing && !shouldReverse)) | ||||
command.SetTurn(TurnToward(ship, dp)); | ||||
if(isFacing) | ||||
command.SetThrust(1.); | ||||
else if(shouldReverse) | ||||
{ | ||||
command.SetTurn(TurnToward(ship, velocity)); | ||||
command.SetThrust(-1.); | ||||
} | ||||
|
||||
return false; | ||||
} | ||||
|
||||
|
||||
|
||||
bool AI::Stop(Ship &ship, Command &command, double maxSpeed, const Point direction) | ||||
{ | ||||
const Point &velocity = ship.Velocity(); | ||||
|
@@ -2522,6 +2555,180 @@ | |||
|
||||
|
||||
|
||||
void AI::StrikeThrough(Ship& ship, Command& command, const Ship& target) | ||||
{ | ||||
int targetTurretRange = target.GetAICache().TurretRange(); | ||||
int weaponsRange = 864; // need to calculate turn around distance v attacker gun range. | ||||
Point direction = ship.Position() - target.Position(); | ||||
double length = direction.Length(); | ||||
|
||||
Point northUnit = target.Facing().Unit(); | ||||
Point westUnit(northUnit.Y(), -northUnit.X()); | ||||
Point eastUnit(-northUnit.Y(), northUnit.X()); | ||||
|
||||
// Point east = (target.Position() + eastUnit * targetTurretRange); | ||||
// Point west = (target.Position() + westUnit * targetTurretRange); | ||||
|
||||
// Point frontAvoid = (direction.Unit().Cross(target.Facing().Unit()) < 0. ? west : east); | ||||
// Point velocity = ship.Velocity(); | ||||
bool inFront = direction.Unit().Dot(target.Facing().Unit()) > .9; | ||||
bool isFacing = direction.Unit().Dot(ship.Facing().Unit()) < 0; | ||||
bool outerRadius = direction.Length() < targetTurretRange * 1.2; // should be max target gun & turret range | ||||
// double speed = ship.MaxVelocity(); | ||||
|
||||
if(outerRadius) | ||||
{ | ||||
// if(inFront && isFacing) | ||||
// { | ||||
// MoveThrough(ship, command, frontAvoid, target.Velocity(), 20., speed); | ||||
// if(command.Has(Command::FORWARD) && ShouldUseAfterburner(ship)) | ||||
// command |= Command::AFTERBURNER; | ||||
// } | ||||
// else if(inFront) | ||||
// { | ||||
// command |= Command::FORWARD; | ||||
// } | ||||
|
||||
if(inFront && isFacing) | ||||
{ | ||||
command.SetTurn(TurnToward(ship, TargetAim(ship, target))); | ||||
command.SetLateralThrust(-1); | ||||
command.SetThrust(1.); | ||||
} | ||||
else if(length >= weaponsRange) | ||||
{ | ||||
command.SetTurn(TurnToward(ship, TargetAim(ship, target))); | ||||
command.SetThrust(1.); | ||||
} | ||||
else if(length < weaponsRange && ship.Facing().Unit().Dot(ship.Velocity().Unit()) > .7) | ||||
{ | ||||
command.SetTurn(TurnToward(ship, TargetAim(ship, target))); | ||||
command.SetThrust(1.); | ||||
} | ||||
else if(length < weaponsRange && ship.Facing().Unit().Dot(ship.Velocity().Unit()) < .7) | ||||
{ | ||||
command.SetTurn(TurnToward(ship, TargetAim(ship, target))); | ||||
} | ||||
else if(length < weaponsRange) | ||||
{ | ||||
command.SetThrust(1.); | ||||
} | ||||
} | ||||
else | ||||
{ | ||||
command.SetTurn(TurnToward(ship, TargetAim(ship, target))); | ||||
command.SetThrust(1.); | ||||
} | ||||
} | ||||
|
||||
|
||||
|
||||
void AI::AttackRear(Ship& ship, Command& command, const Ship& target) | ||||
{ | ||||
int weaponsRange = 370; // attacker weapon range this will be passed in eventually | ||||
int targetTurretRange = target.GetAICache().TurretRange(); | ||||
Point offset = target.Position() - target.Facing().Unit() * weaponsRange; | ||||
Check failure on line 2630 in source/AI.cpp GitHub Actions / Linux
Check failure on line 2630 in source/AI.cpp GitHub Actions / MacOS
Check failure on line 2630 in source/AI.cpp GitHub Actions / Ubuntu (24.04, GLES)
Check failure on line 2630 in source/AI.cpp GitHub Actions / MacOS
Check failure on line 2630 in source/AI.cpp GitHub Actions / Windows
|
||||
Point direction = ship.Position() - target.Position(); | ||||
Point d = (target.Position() + target.Velocity()) - (ship.Position() + ship.Velocity()); | ||||
|
||||
Point northUnit = target.Facing().Unit(); | ||||
Point southUnit = -target.Facing().Unit(); | ||||
Point westUnit(northUnit.Y(), -northUnit.X()); | ||||
Point eastUnit(-northUnit.Y(), northUnit.X()); | ||||
|
||||
Point northWestUnit(northUnit.X() + westUnit.X(), northUnit.Y() + westUnit.Y()); | ||||
Point northWest = (target.Position() + northWestUnit * targetTurretRange / 1.41); | ||||
|
||||
Point northEastUnit(northUnit.X() + eastUnit.X(), northUnit.Y() + eastUnit.Y()); | ||||
Point northEast = (target.Position() + northEastUnit * targetTurretRange / 1.41); | ||||
|
||||
Point southWestUnit(southUnit.X() + westUnit.X(), southUnit.Y() + westUnit.Y()); | ||||
Point southWest = (target.Position() + southWestUnit * targetTurretRange / 1.41); | ||||
|
||||
Point southEastUnit(southUnit.X() + eastUnit.X(), southUnit.Y() + eastUnit.Y()); | ||||
Point southEast = (target.Position() + southEastUnit * targetTurretRange / 1.41); | ||||
|
||||
Point frontAvoid = (direction.Cross(ship.Facing().Unit()) < 0. ? northWest : northEast); | ||||
Check failure on line 2651 in source/AI.cpp GitHub Actions / Linux
Check failure on line 2651 in source/AI.cpp GitHub Actions / MacOS
Check failure on line 2651 in source/AI.cpp GitHub Actions / Ubuntu (24.04, GLES)
Check failure on line 2651 in source/AI.cpp GitHub Actions / MacOS
Check failure on line 2651 in source/AI.cpp GitHub Actions / Windows
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
Point rearAvoid = (direction.Cross(target.Facing().Unit()) > 0. ? southWest : southEast); | ||||
Check failure on line 2652 in source/AI.cpp GitHub Actions / Linux
Check failure on line 2652 in source/AI.cpp GitHub Actions / MacOS
Check failure on line 2652 in source/AI.cpp GitHub Actions / Ubuntu (24.04, GLES)
Check failure on line 2652 in source/AI.cpp GitHub Actions / MacOS
Check failure on line 2652 in source/AI.cpp GitHub Actions / Windows
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
|
||||
bool isBehind = direction.Unit().Dot(target.Facing().Unit()) < -.98; | ||||
bool justLeft = direction.Unit().Dot(target.Facing().Unit()) < -.8 && direction.Cross(target.Facing().Unit()) > 0; | ||||
Check failure on line 2655 in source/AI.cpp GitHub Actions / Linux
Check failure on line 2655 in source/AI.cpp GitHub Actions / MacOS
Check failure on line 2655 in source/AI.cpp GitHub Actions / Ubuntu (24.04, GLES)
Check failure on line 2655 in source/AI.cpp GitHub Actions / MacOS
Check failure on line 2655 in source/AI.cpp GitHub Actions / Windows
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
bool justRight = direction.Unit().Dot(target.Facing().Unit()) < -.8 && direction.Cross(target.Facing().Unit()) <= 0; | ||||
Check failure on line 2656 in source/AI.cpp GitHub Actions / Linux
Check failure on line 2656 in source/AI.cpp GitHub Actions / MacOS
Check failure on line 2656 in source/AI.cpp GitHub Actions / Ubuntu (24.04, GLES)
Check failure on line 2656 in source/AI.cpp GitHub Actions / MacOS
Check failure on line 2656 in source/AI.cpp GitHub Actions / Windows
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
bool inFront = direction.Unit().Dot(target.Facing().Unit()) > .75; | ||||
|
||||
bool atSide = (!inFront && !isBehind); | ||||
Check failure on line 2659 in source/AI.cpp GitHub Actions / Linux
Check failure on line 2659 in source/AI.cpp GitHub Actions / MacOS
Check failure on line 2659 in source/AI.cpp GitHub Actions / Ubuntu (24.04, GLES)
Check failure on line 2659 in source/AI.cpp GitHub Actions / MacOS
Check failure on line 2659 in source/AI.cpp GitHub Actions / Windows
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
bool inRange = direction.Length() < weaponsRange; | ||||
bool tooClose = direction.Length() < weaponsRange * .9; | ||||
bool outerRadius = direction.Length() < weaponsRange * 2; | ||||
double reverseSpeed = ship.MaxReverseVelocity(); | ||||
double speed = ship.MaxVelocity(); | ||||
Check failure on line 2664 in source/AI.cpp GitHub Actions / Linux
Check failure on line 2664 in source/AI.cpp GitHub Actions / MacOS
Check failure on line 2664 in source/AI.cpp GitHub Actions / Ubuntu (24.04, GLES)
Check failure on line 2664 in source/AI.cpp GitHub Actions / MacOS
Check failure on line 2664 in source/AI.cpp GitHub Actions / Windows
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
|
||||
|
||||
if (outerRadius) | ||||
{ | ||||
if (!isBehind) // not behind the target | ||||
{ | ||||
command.SetTurn(TurnToward(ship, TargetAim(ship, target))-0.2); | ||||
command.SetLateralThrust(TurnToward(ship, TargetAim(ship, target))); | ||||
if(!inRange) command.SetThrust(1.); | ||||
ship.SetSwizzle(27); | ||||
} | ||||
/*if (inFront) | ||||
{ | ||||
MoveThrough(ship, command, frontAvoid, target.Velocity(), 20., speed); | ||||
ship.SetSwizzle(27); | ||||
} | ||||
else if(atSide) | ||||
{ | ||||
MoveThrough(ship, command, rearAvoid, target.Velocity(), 20., speed); | ||||
ship.SetSwizzle(0); | ||||
} | ||||
else if(!isBehind) // not behind target | ||||
{ | ||||
MoveTo(ship, command, offset, target.Velocity(), 80., 20); | ||||
ship.SetSwizzle(8); | ||||
} | ||||
else if(!inRange) // behind but too far away | ||||
{ | ||||
command.SetTurn(TurnToward(ship, TargetAim(ship, target))); | ||||
command.SetThrust(1.); | ||||
ship.SetSwizzle(5); | ||||
}*/ | ||||
else if(tooClose) // behind target but too close | ||||
{ | ||||
if(reverseSpeed && target.Velocity().Dot(-d.Unit()) <= reverseSpeed) | ||||
{ | ||||
command.SetTurn(TurnToward(ship, d)-0.2); | ||||
command.SetLateralThrust(-1.); | ||||
if(ship.Facing().Unit().Dot(d) >= 0.) | ||||
command.SetThrust(-1.); | ||||
} | ||||
else | ||||
{ | ||||
command.SetTurn(TurnToward(ship, d) - 0.2); | ||||
command.SetLateralThrust(-1.); | ||||
} | ||||
ship.SetSwizzle(14); | ||||
} | ||||
else if(inRange) // behind target, in sweet spot. | ||||
{ | ||||
// if(justRight) command.SetLateralThrust(-1.); | ||||
// else if(justLeft) command.SetLateralThrust(1.); | ||||
command.SetLateralThrust(-1.); | ||||
command.SetTurn(TurnToward(ship, TargetAim(ship, target))); | ||||
ship.SetSwizzle(3); | ||||
} | ||||
} | ||||
else | ||||
{ | ||||
command.SetTurn(TurnToward(ship, TargetAim(ship, target))); | ||||
command.SetThrust(1.); | ||||
ship.SetSwizzle(26); | ||||
} | ||||
} | ||||
|
||||
|
||||
|
||||
void AI::PrepareForHyperspace(Ship &ship, Command &command) | ||||
{ | ||||
bool hasHyperdrive = ship.JumpNavigation().HasHyperdrive(); | ||||
|
@@ -2725,7 +2932,10 @@ | |||
MoveToAttack(ship, command, target); | ||||
return; | ||||
} | ||||
|
||||
if(target.TrueTurnRate() < ship.Acceleration() * 1.2) | ||||
{ | ||||
AttackRear(ship, command, target); | ||||
} | ||||
// Check if this ship is fast enough to keep distance from target. | ||||
// Have a 10% minimum to avoid ships getting in a chase loop. | ||||
const bool isAbleToRun = target.MaxVelocity() * SAFETY_MULTIPLIER < ship.MaxVelocity(); | ||||
|
@@ -2785,6 +2995,9 @@ | |||
} | ||||
} | ||||
// Fire if we can or move closer to use all weapons. | ||||
if(target.TrueTurnRate() < ship.Acceleration() * 1.2) AttackRear(ship, command, target); | ||||
else if(ship.Attributes().Get("rear")) StrikeThrough(ship, command, target); | ||||
else if(ship.Attributes().Get("strike")) StrikeThrough(ship, command, target); | ||||
else | ||||
if(weaponDistanceFromTarget < shortestRange * .75) | ||||
AimToAttack(ship, command, target); | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.