From 57d66ec8b6c515ea24eb32d86a1cbd1d32026735 Mon Sep 17 00:00:00 2001 From: Robalian Date: Sun, 13 Sep 2020 00:02:50 +0200 Subject: [PATCH] A function for opening ramparts for allied creeps moving through a room --- README.md | 1 + .../TypeScript/openingRampartsForAllies.ts | 60 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 src/misc/TypeScript/openingRampartsForAllies.ts diff --git a/README.md b/README.md index b95aa99..b49aaf1 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,7 @@ For pull requests, we use [GitConsensus](https://www.gitconsensus.com/) to allow |TS| [Creep intent tracker.ts](/src/misc/TypeScript/Creep%20intent%20tracker.ts) |unfleshedone|intent tracker implementation| |TS| [moving.average.ts](/src/misc/TypeScript/moving.average.ts) |unsleshedone|moving average implementation| |TS| [Typescript roomScan.ts](/src/misc/TypeScript/Typescript%20roomScan.ts) |crzytrane|room scanner?| +|TS| [Typescript openingRampartsForAllies.ts](/src/misc/TypeScript/openingRampartsForAllies.ts) |Robalian|A function for opening ramparts for allied creeps moving through a room| || [migrate room to sim.md](/src/misc/migrate%20room%20to%20sim.md) |semperrabbit|how to migrate room to sim| || [screeps body calculator.md](/src/misc/screeps%20body%20calculator.md) |nitroevil|link to creep calculator| |KT| [DistanceTransform](src/misc/Kotlin/DistanceTransform/) |Vipo|Algoritm for finding open areas in rooms| diff --git a/src/misc/TypeScript/openingRampartsForAllies.ts b/src/misc/TypeScript/openingRampartsForAllies.ts new file mode 100644 index 0000000..c6d006e --- /dev/null +++ b/src/misc/TypeScript/openingRampartsForAllies.ts @@ -0,0 +1,60 @@ +const ALLIES = new Set([ + "ally 1", + "ally_2" +]); +const offsetX = [0, 0, 1, 1, 1, 0, -1, -1, -1]; +const offsetY = [0, -1, -1, 0, 1, 1, 1, 0, -1]; +​ +// roomName, rampartIds +let rampartsToClose: Record = {}; +​ +/** + * opens ramparts around creeps of whitelisted players + * + * as long as allies ignore your ramparts when pathfinding + * their creeps should have no problem going through your room + */ +export function letAlliesIn(room: Room) { + // init rampartsToClose for this room + if (!rampartsToClose[room.name]) { + rampartsToClose[room.name] = room.find(FIND_MY_STRUCTURES) + .filter(r => r.structureType === STRUCTURE_RAMPART && r.isPublic) + .map(r => r.id); + } +​ + let alliesInRoom = room.find(FIND_HOSTILE_CREEPS).filter(c => ALLIES.has(c.owner.username)); + // close ramparts and remove them from a list + rampartsToClose[room.name] = rampartsToClose[room.name].filter(rampartId => { + let r = Game.getObjectById(rampartId); + if (!r || !r.isPublic) + return false; + const anyAllyNearRamp = alliesInRoom.some(ally => ally.pos.isNearTo(r.pos) && !ally.pos.isEqualTo(r.pos)); + if (anyAllyNearRamp) + return true; + r.setPublic(false); + return true; + }); +​ + let rampartCount = _.sum(room.find(FIND_MY_STRUCTURES), s => s.structureType === STRUCTURE_RAMPART ? 1 : 0); + if (alliesInRoom.length === 0 || rampartCount === 0) + return; +​ + // might be a good idea + //if (isUnderAttack(room)) + // return; +​ + // + for (let ally of alliesInRoom) { + for (let dir = TOP; dir <= TOP_LEFT; ++dir) { + let x = ally.pos.x + offsetX[dir]; + let y = ally.pos.y + offsetY[dir]; + if (x < 0 || x > 49 || y < 0 || y > 49) + continue; + let rampartAtPos = room.lookForAt(LOOK_STRUCTURES, x, y).find(s => s.structureType === STRUCTURE_RAMPART) as StructureRampart; + if (rampartAtPos && rampartAtPos.my && !rampartAtPos.isPublic) { + rampartAtPos.setPublic(true); + rampartsToClose[room.name].push(rampartAtPos.id); + } + } + } +}