Skip to content
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

Pathing refactor #2

Merged
merged 44 commits into from
Nov 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
ebdf75f
Partial refactoring of pathing
ryanrolds Sep 29, 2021
442f5b1
More work on pathing
ryanrolds Sep 30, 2021
ea9dfbb
More work
ryanrolds Sep 30, 2021
997d27d
Bug fixes
ryanrolds Sep 30, 2021
45f5775
More changes
ryanrolds Sep 30, 2021
7ff7524
Bug fixes
ryanrolds Sep 30, 2021
692e7e3
Moved buffere to use new pathfinding logic
ryanrolds Sep 30, 2021
d4eac6a
Merge branch 'main' into pathing_refactor
ryanrolds Oct 3, 2021
4e23edf
Getting private server working
ryanrolds Oct 4, 2021
94c2505
Bug fixes to pathfinding and private screeps support
ryanrolds Oct 7, 2021
da23ca3
Pathing and other bug fixes
ryanrolds Oct 8, 2021
c939e83
Merge branch 'main' into pathing_refactor
ryanrolds Oct 8, 2021
4c2956f
Reducing rooms
ryanrolds Oct 10, 2021
efc3c01
Merge branch 'main' into pathing_refactor
ryanrolds Oct 10, 2021
a86820f
Reduced test rooms
ryanrolds Oct 12, 2021
0311d96
More bug fixes. Stop requesting harvesters/miners in hostile rooms
ryanrolds Oct 12, 2021
d9ce9b0
More creep behavior fixes
ryanrolds Oct 12, 2021
a98160e
Created improved run until condition met function. Fixed bug in upgra…
ryanrolds Oct 16, 2021
f89bbd4
Lots of work on autobase building
ryanrolds Oct 17, 2021
2fdbb68
Reset trip counter for haulers
ryanrolds Oct 17, 2021
d76664f
Merge branch 'pathing_refactor' of github.com:ryanrolds/screeps into …
ryanrolds Oct 17, 2021
ee58ca5
Bug fixes, whitepace changes, avoidUnloggedRoom pathfinding policy, i…
ryanrolds Oct 19, 2021
2ec90d8
Bug fixes, whitepace changes, avoidUnloggedRoom pathfinding policy, i…
ryanrolds Oct 19, 2021
7b9d758
More work
ryanrolds Oct 19, 2021
9c1dae5
some work on distance matrix
ryanrolds Oct 20, 2021
297ce89
Working distance matrix for locating new bases
ryanrolds Oct 20, 2021
92b9a55
Fix bug with cost matrix distance
ryanrolds Oct 26, 2021
a74c2c6
Merge remote-tracking branch 'refs/remotes/origin/pathing_refactor' i…
ryanrolds Oct 26, 2021
0d60a63
Merge branch 'main' into pathing_refactor
ryanrolds Oct 31, 2021
6328b07
partial
ryanrolds Oct 31, 2021
63891c5
Merge remote-tracking branch 'refs/remotes/origin/pathing_refactor' i…
ryanrolds Oct 31, 2021
ee8c0f5
Refactoed and added tests to path cache
ryanrolds Nov 1, 2021
fe663e2
Merge branch 'pathing_refactor' of github.com:ryanrolds/screeps into …
ryanrolds Nov 1, 2021
280daf0
Harvesters upgrade if no storage or buildings. Other fixes
ryanrolds Nov 2, 2021
9b121b1
Fix for LRU overflow
ryanrolds Nov 3, 2021
0fd8f8e
Still working on bugs in the path cache
ryanrolds Nov 4, 2021
a56a270
Still working on bugs in the path cache
ryanrolds Nov 4, 2021
e3d7123
Fixed bug in path cache, increased cache size
ryanrolds Nov 4, 2021
e71f5b3
Hardened weak room check
ryanrolds Nov 4, 2021
9825dc1
Improved pathfinding over long distances and complex shapes
ryanrolds Nov 5, 2021
652d3cc
Improved pathfinding over long distances and complex shapes
ryanrolds Nov 5, 2021
456d843
Improved pathfinding over long distances and complex shapes
ryanrolds Nov 5, 2021
d4d8895
Fixes for terminal, towers, defense, and offense
ryanrolds Nov 7, 2021
058c251
Some map work
ryanrolds Nov 7, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ There are a couple of helpful global variables:
There are some debugging tools built into the project:

* Run and draw path - `AI.getPathDebugger().debug(new RoomPosition(43, 13, 'E21S48'), new RoomPosition(25,25,'E26S51'), 3, {avoidHostiles: true, avoidOwnedRooms: true, avoidFriendlyRooms: false, maxOps: 2000})`
* Run and draw cost matrix - `AI.getCostMatrixDebugger().debug("W8N4", 'open_space')` and `AI.getCostMatrixDebugger().clear()`\
* Get debug info on path cache - `AI.kingdom.getPathCache().debug()`

### Creeps

Expand Down
12 changes: 6 additions & 6 deletions src/behavior.build.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ const selectSite = behaviorTree.leafNode(
return 1 - site.progress / site.progressTotal;
case STRUCTURE_STORAGE:
return 2 - site.progress / site.progressTotal;
case STRUCTURE_LINK:
case STRUCTURE_CONTAINER:
return 3 - site.progress / site.progressTotal;
case STRUCTURE_TERMINAL:
case STRUCTURE_LINK:
return 4 - site.progress / site.progressTotal;
case STRUCTURE_RAMPART:
case STRUCTURE_TERMINAL:
return 5 - site.progress / site.progressTotal;
case STRUCTURE_EXTRACTOR:
case STRUCTURE_RAMPART:
return 6 - site.progress / site.progressTotal;
case STRUCTURE_LAB:
case STRUCTURE_EXTRACTOR:
return 7 - site.progress / site.progressTotal;
case STRUCTURE_CONTAINER:
case STRUCTURE_LAB:
return 8 - site.progress / site.progressTotal;
case STRUCTURE_EXTENSION:
return 9 - site.progress / site.progressTotal;
Expand Down
39 changes: 20 additions & 19 deletions src/behavior.harvest.js → src/behavior.harvest.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

const behaviorTree = require('./lib.behaviortree');
const {FAILURE, SUCCESS, RUNNING} = require('./lib.behaviortree');
const behaviorMovement = require('./behavior.movement');
const {MEMORY_SOURCE, MEMORY_ASSIGN_ROOM} = require('./constants.memory');
const {numEnemeiesNearby, numOfSourceSpots} = require('./helpers.proximity');
import * as behaviorTree from "./lib.behaviortree";
import {FAILURE, SUCCESS, RUNNING} from "./lib.behaviortree";
import * as behaviorMovement from "./behavior.movement";
import {MEMORY_SOURCE, MEMORY_ASSIGN_ROOM} from "./constants.memory";
import {numEnemeiesNearby, numOfSourceSpots} from "./helpers.proximity";

module.exports.selectHarvestSource = behaviorTree.leafNode(
export const selectHarvestSource = behaviorTree.leafNode(
'bt.harvest.selectHarvestSource',
(creep, trace, kingdom) => {
let sources = creep.room.find(FIND_SOURCES);
Expand Down Expand Up @@ -42,11 +42,11 @@ module.exports.selectHarvestSource = behaviorTree.leafNode(
},
);

module.exports.moveToHarvestRoom = behaviorTree.repeatUntilSuccess(
export const moveToHarvestRoom = behaviorTree.repeatUntilSuccess(
'bt.movement.room.harvest',
behaviorTree.leafNode(
'move_to_harvest_room',
(creep) => {
(creep, trace, kingdom) => {
const room = creep.memory[MEMORY_ASSIGN_ROOM];
// If creep doesn't have a harvest room assigned, we are done
if (!room) {
Expand All @@ -61,43 +61,44 @@ module.exports.moveToHarvestRoom = behaviorTree.repeatUntilSuccess(
reusePath: 50,
maxOps: 1500,
});
trace.log('move to', {result});

if (result === ERR_NO_PATH) {
return FAILURE;
}
if (result === ERR_INVALID_ARGS) {
return FAILURE;
}

return RUNNING;
},
),
);

module.exports.moveToHarvest = behaviorTree.leafNode(
export const moveToHarvest = behaviorTree.leafNode(
'move_to_source',
(creep) => {
return behaviorMovement.moveToSource(creep, 1, false, 50, 1500);
},
);

module.exports.harvest = behaviorTree.leafNode(
export const harvest = behaviorTree.leafNode(
'fill_creep',
(creep) => {
const destination = Game.getObjectById(creep.memory.source);
(creep, trace, kingdom) => {
const destination = Game.getObjectById<Id<Source>>(creep.memory[MEMORY_SOURCE]);
if (!destination) {
return FAILURE;
}

const result = creep.harvest(destination);
if (result === ERR_FULL) {
return SUCCESS;
}
trace.log('harvest', {result});

if (creep.store.getFreeCapacity() === 0) {
return SUCCESS;
}

// NOTICE switched this to FAILURE from RUNNING, this may cause problems
if (result === ERR_NOT_ENOUGH_RESOURCES) {
return RUNNING;
return FAILURE;
}

if (result === OK) {
return RUNNING;
}
Expand Down
35 changes: 17 additions & 18 deletions src/behavior.movement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import {FAILURE, SUCCESS, RUNNING, NodeTickResult} from "./lib.behaviortree";

import * as MEMORY from "./constants.memory";
import {MEMORY_ORIGIN, MEMORY_SOURCE} from "./constants.memory";
import {PathCache, PathCacheItem, PathFinderPolicy} from "./lib.path_cache";
import {PathCache, PathCacheItem} from "./lib.path_cache";
import {Tracer} from "./lib.tracing";
import {Kingdom} from "./org.kingdom";
import {FindPathPolicy} from "./lib.pathing";
import {common} from "./lib.pathing_policies";

const MAX_POSITION_TTL = 5;
const MEMORY_MOVE_POS_TTL = 'move_pos_ttl';
Expand Down Expand Up @@ -145,7 +147,7 @@ const getDestinationFromMemory = (creep: Creep, memoryId: string): RoomPosition
};

const getAndSetCreepPath = (pathCache: PathCache, creep: Creep, destination: RoomPosition,
range: number, policy: PathFinderPolicy, trace: Tracer): [PathFinderPath, string, string] => {
range: number, policy: FindPathPolicy, trace: Tracer): [PathFinderPath, string, string] => {
const path = pathCache.getPath(creep.pos, destination, range, policy, trace);
const originKey = pathCache.getKey(creep.pos, 0);
const destKey = pathCache.getKey(destination, range);
Expand All @@ -160,7 +162,7 @@ const clearMovementCache = (creep) => {
};

const updateCreepCachedPath = (kingdom: Kingdom, creep: Creep, destination: RoomPosition,
range: number, policy: PathFinderPolicy, trace: Tracer): PathFinderPath => {
range: number, policy: FindPathPolicy, trace: Tracer): PathFinderPath => {
const pathCache = kingdom.getPathCache();

let path: PathCacheItem = null;
Expand All @@ -170,7 +172,7 @@ const updateCreepCachedPath = (kingdom: Kingdom, creep: Creep, destination: Room
trace.log('keys', {originKey, destKey});

if (originKey && destKey) {
path = pathCache.getCachedPath(originKey, destKey);
path = pathCache.getCachedPath(originKey, destKey, trace);
}

if (path) {
Expand All @@ -197,7 +199,7 @@ const updateCreepCachedPath = (kingdom: Kingdom, creep: Creep, destination: Room
return null;
}

export const cachedMoveToMemoryPos = (memoryId: string, range: number = 1, policy: PathFinderPolicy) => {
export const cachedMoveToMemoryPos = (memoryId: string, range: number = 1, policy: FindPathPolicy) => {
return behaviorTree.leafNode(
'cached_move_to_position',
(creep, trace, kingdom) => {
Expand All @@ -213,7 +215,7 @@ export const cachedMoveToMemoryPos = (memoryId: string, range: number = 1, polic
);
};

export const cachedMoveToMemoryObjectId = (memoryId: string, range: number = 1, policy: PathFinderPolicy) => {
export const cachedMoveToMemoryObjectId = (memoryId: string, range: number = 1, policy: FindPathPolicy) => {
return behaviorTree.leafNode(
'cached_move_to_object_id',
(creep, trace, kingdom) => {
Expand All @@ -230,7 +232,7 @@ export const cachedMoveToMemoryObjectId = (memoryId: string, range: number = 1,
};

const cachedMoveToPosition = (kingdom: Kingdom, creep: Creep, destination: RoomPosition,
range: number = 1, policy: PathFinderPolicy, trace: Tracer) => {
range: number = 1, policy: FindPathPolicy, trace: Tracer) => {

// Check if creep has arrived
if (creep.pos.inRangeTo(destination, range)) {
Expand All @@ -245,14 +247,18 @@ const cachedMoveToPosition = (kingdom: Kingdom, creep: Creep, destination: RoomP

if (!stuck) {
const pathfinderResult = updateCreepCachedPath(kingdom, creep, destination, range, policy, trace)
trace.log('pathfinder result', {path: pathfinderResult.path});
trace.log('pathfinder result', {result: pathfinderResult, creepName: creep.name, destination, range, policy});
if (!pathfinderResult) {
trace.log('no path found', {destination, range});
return FAILURE;
}

result = creep.moveByPath(pathfinderResult.path);
trace.log('move by path result', {result, path: pathfinderResult.path});
} else {
const moveOpts = getMoveOpts(false, 50, policy.maxOps);
const moveOpts = getMoveOpts(false, 50, policy.path.maxOps);
result = creep.moveTo(destination, moveOpts);
trace.log('move result', {result, origin: creep.pos, destination});
trace.log('stuck move result', {result, origin: creep.pos, destination});
}

if (result === ERR_NO_PATH) {
Expand Down Expand Up @@ -357,13 +363,6 @@ export const fillCreepFromDestination = (creep, trace) => {
};

export const moveToShard = (shardMemoryKey) => {
const policy: PathFinderPolicy = {
avoidHostiles: false,
avoidOwnedRooms: true,
avoidFriendlyRooms: false,
maxOps: 2500,
};

return behaviorTree.repeatUntilConditionMet(
'moveToShard',
(creep, trace, kingdom) => {
Expand Down Expand Up @@ -412,7 +411,7 @@ export const moveToShard = (shardMemoryKey) => {
return SUCCESS;
},
),
cachedMoveToMemoryPos(MEMORY.MEMORY_DESTINATION_POS, 0, policy),
cachedMoveToMemoryPos(MEMORY.MEMORY_DESTINATION_POS, 0, common),
],
),
);
Expand Down
18 changes: 10 additions & 8 deletions src/behavior.room.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const behaviorTree = require('./lib.behaviortree');
const {FAILURE, SUCCESS, RUNNING} = require('./lib.behaviortree');
const behaviorHarvest = require('./behavior.harvest');
const behaviorMovement = require('./behavior.movement');
const {MEMORY_SOURCE, MEMORY_DESTINATION} = require('./constants.memory');
const {common} = require('./lib.pathing_policies');

const pickupDroppedEnergy = behaviorTree.leafNode(
'janitor',
Expand Down Expand Up @@ -100,7 +102,7 @@ const selectStorage = behaviorTree.leafNode(

behaviorMovement.setDestination(creep, null);

trace.log('did not find reserver with energy');
trace.log('did not find reserve with energy', {roomName: creep.room.name});
return FAILURE;
},
);
Expand All @@ -126,7 +128,7 @@ const selectContainer = behaviorTree.leafNode(

behaviorMovement.setDestination(creep, null);

trace.log('did not find an energy source');
trace.log('did not find a container with energy');
return FAILURE;
},
);
Expand All @@ -136,7 +138,7 @@ const selectMoveFill = (selector) => {
'fill_from_selector',
[
selector,
behaviorMovement.moveToDestination(1),
behaviorMovement.cachedMoveToMemoryObjectId(MEMORY_DESTINATION, 1, common),
behaviorTree.leafNode(
'fill_creep',
(creep, trace) => {
Expand All @@ -152,13 +154,13 @@ const fillCreepFromSource = behaviorTree.sequenceNode(
'fill_from_source',
[
behaviorHarvest.selectHarvestSource,
behaviorHarvest.moveToHarvest,
behaviorMovement.cachedMoveToMemoryObjectId(MEMORY_SOURCE, 1, common),
behaviorHarvest.harvest,
],
);

module.exports.getSomeEnergy = behaviorTree.repeatUntilConditionMet(
'get_energy_until_success',
module.exports.getSomeEnergy = behaviorTree.runUntilConditionMet(
'get_some_energy_until_success',
(creep, trace, kingdom) => {
const freeCapacity = creep.store.getFreeCapacity(RESOURCE_ENERGY);
trace.log('creep free capacity', {freeCapacity});
Expand Down Expand Up @@ -186,8 +188,8 @@ module.exports.getEnergy = behaviorTree.repeatUntilConditionMet(
behaviorTree.selectorNode(
'select_and_fill_with_energy',
[
selectMoveFill(selectNearbyLink),
selectMoveFill(selectStorage),
selectMoveFill(selectNearbyLink),
selectMoveFill(selectContainer),
pickupDroppedEnergy,
fillCreepFromSource,
Expand Down Expand Up @@ -256,7 +258,7 @@ module.exports.recycleCreep = behaviorTree.leafNode(
const spawn = spawns[0];
if (creep.pos.inRangeTo(spawn, 1)) {
const result = spawn.recycleCreep(creep);
trace.log('recycled creep', {result});
trace.notice('recycled creep', {result});
return RUNNING;
}

Expand Down
9 changes: 5 additions & 4 deletions src/behavior.storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ const selectRoomDropoff = module.exports.selectRoomDropoff = behaviorTree.select
return SUCCESS;
}


return FAILURE;
},
),
Expand All @@ -74,7 +73,8 @@ const selectRoomDropoff = module.exports.selectRoomDropoff = behaviorTree.select
const targets = creep.pos.findInRange(FIND_STRUCTURES, 2, {
filter: (structure) => {
return structure.structureType == STRUCTURE_CONTAINER &&
structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0;
structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0 &&
structure.isActive();
},
});

Expand All @@ -98,7 +98,8 @@ const selectRoomDropoff = module.exports.selectRoomDropoff = behaviorTree.select
filter: (structure) => {
// TODO things seeking to gain energy should use another function
return structure.structureType == STRUCTURE_LINK &&
structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0;
structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0 &&
structure.isActive();
},
});

Expand Down Expand Up @@ -132,7 +133,7 @@ const selectRoomDropoff = module.exports.selectRoomDropoff = behaviorTree.select
return FAILURE;
}

if (!room.room.storage) {
if (!room.room.storage?.isActive()) {
return FAILURE;
}

Expand Down
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export interface ColonyConfig {
isPublic: boolean;
primary: string;
rooms: string[];
automated: boolean;
origin: RoomPosition;
}

export interface KingdomConfig {
Expand Down
4 changes: 2 additions & 2 deletions src/constants.creeps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const DEFINITIONS = {
},
[WORKER_DEFENDER]: {
energyLimit: null,
energyMinimum: 2000,
energyMinimum: 1000,
parts: [MOVE, HEAL, MOVE, RANGED_ATTACK, MOVE, RANGED_ATTACK, MOVE, TOUGH, MOVE, RANGED_ATTACK],
base: [MOVE, RANGED_ATTACK],
boosts: ['rangedAttack'],
Expand Down Expand Up @@ -88,7 +88,7 @@ export const DEFINITIONS = {
},
[WORKER_ATTACKER]: {
energyLimit: null,
energyMinimum: 6000,
energyMinimum: 3000,
parts: [WORK, TOUGH, MOVE, HEAL, MOVE, HEAL, MOVE, RANGED_ATTACK, WORK],
base: [TOUGH, MOVE, MOVE, MOVE, MOVE, MOVE, HEAL, HEAL, HEAL, HEAL, HEAL, RANGED_ATTACK, WORK, WORK],
//boosts: ['dismantle', 'heal', 'damage'],
Expand Down
1 change: 0 additions & 1 deletion src/constants.memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export const PATH_CACHE = 'path_cache';
export const MEMORY_DESTINATION_POS = 'destination_pos';

// Long term memory IDs
export const MEMORY_HARVEST_CONTAINER = 'harvest_container';
export const MEMORY_WITHDRAW = 'withdraw';
export const MEMORY_WITHDRAW_ROOM = 'withdraw_room';
export const MEMORY_DROPOFF = 'transfer';
Expand Down
Loading