Skip to content

Instantly share code, notes, and snippets.

@jonahwilliams
Created January 19, 2021 00:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonahwilliams/9a5599f28191d744ade9474d2ae458d3 to your computer and use it in GitHub Desktop.
Save jonahwilliams/9a5599f28191d744ade9474d2ae458d3 to your computer and use it in GitHub Desktop.
if (energy <= 0) {
return false;
}
updateVisibility();
var candidates = <Agent>[];
var friends = <Agent>[];
for (var agent in stage.agents) {
if (agent is Tombstone) {
continue;
}
if (visibility[agent.logicalPosition]) {
if (isHostileTo(agent)) {
candidates.add(agent);
} else {
friends.add(agent);
}
}
}
if (candidates.isEmpty && _lastHostileLocation == null) {
return false;
}
Path? path;
Agent? target;
for (var candidate in candidates) {
path = const Pathfinder().findPath(
logicalPosition,
candidate.logicalPosition,
stage.board,
maxCost: 20,
);
if (path != null) {
target = candidate;
_lastHostileLocation = candidate.logicalPosition;
_lastHostileAgent = candidate;
break;
}
}
if ((path == null || target == null) && _lastHostileLocation != null) {
path = const Pathfinder().findPath(
logicalPosition,
_lastHostileLocation!,
stage.board,
maxCost: 20,
);
}
// If the agent has reached the last known hostile location
// and not found a foe, then cheat a bit and check if the
// foe is within 3 tiles. If so, pathfind as long as the
// cost is similar. If this is not the case, radio other
// friendlies to ask for a _lastKnownLocation.
if (_lastHostileLocation == logicalPosition) {
if (_lastHostileAgent!.logicalPosition.octileDistance(logicalPosition) <
3.5) {
path = const Pathfinder().findPath(
logicalPosition,
_lastHostileAgent!.logicalPosition,
stage.board,
maxCost: 3.5,
);
if (path != null) {
_lastHostileLocation = _lastHostileAgent!.logicalPosition;
}
} else {
for (var friend in friends) {
if (friend is SimulatedAgent &&
friend._lastHostileLocation != _lastHostileLocation) {
_lastHostileLocation = friend._lastHostileLocation;
path = const Pathfinder().findPath(
logicalPosition,
_lastHostileAgent!.logicalPosition,
stage.board,
maxCost: 20,
);
break;
}
}
}
}
if (path == null) {
return false;
}
if (target != null && path.point == target.logicalPosition) {
addEvent(AttackEvent(this, target));
energy = 0;
return true;
}
var nextPath = path.child!;
var delta = nextPath.point - logicalPosition;
var swapAgent = stage.board[nextPath.point].agent;
if (swapAgent != null && !swapAgent.isHostileTo(this)) {
energy = 0;
return true;
}
addEvent(MoveStartEvent(delta, this));
energy = 0;
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment