Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkaulfers/13be177a008624340071050516ce6108 to your computer and use it in GitHub Desktop.
Save mkaulfers/13be177a008624340071050516ce6108 to your computer and use it in GitHub Desktop.
This code is a small code-snippet that will allow you to pass in a center position, and a size, as well as a room object to get back the nearest position where your search will fit, excluding any terrain. and structures. Useful for dynamic bases. It also has an iterator that can be adjusted to your liking, however it should be noted the more of …
private static validPos(x: number, y: number, size: number, room: Room): boolean {
if (x < 0 || y < 0) return false
if (x >= 50 || y >= 50) return false
if (x - Math.floor(size / 2) < 0 || y - Math.floor(size / 2) < 0) return false
if (x + Math.floor(size / 2) >= 50 || y + Math.floor(size / 2) >= 50) return false
let results = room.lookAtArea(
y - Math.floor(size / 2),
x - Math.floor(size / 2),
y + Math.floor(size / 2),
x + Math.floor(size / 2),
true
).filter( x => x.type != LOOK_CREEPS &&
x.terrain != 'plain' &&
x.terrain != 'swamp' &&
x.type != LOOK_POWER_CREEPS &&
x.type != LOOK_TOMBSTONES &&
x.type != LOOK_RUINS &&
x.type != LOOK_NUKES)
if (results.length > 0) return false
return true
}
/**
*
* @param x - x of Pos/RoomPosition
* @param y - y of Pos/RoomPosition
* @param size - EVEN numbers are treated as a radius
* - (EX: 2 sets a search size of 5x5, 4 sets a search of 9x9).
*
* ODD numbers are treated as a diameter
* - (Ex 5 is 5x5, 7 is 7x7).
*/
static findPosForStamp(x: number, y: number, size: number, room: Room): RoomPosition | undefined {
if (size % 2 == 0) {
size = size * 2 + 1
}
let queue: number[][] = []
queue.push([x, y])
let iteration = 0
while (queue.length > 0 && iteration < 100) {
let currPos = queue[queue.length - 1]
queue.pop()
let posX = currPos[0]
let posY = currPos[1]
if (this.validPos(posX + 1, posY, size, room)) {
return new RoomPosition(posX + 1, posY, room.name)
} else {
queue.push([posX + 1, posY])
}
if (this.validPos(posX - 1, posY, size, room)) {
return new RoomPosition(posX - 1, posY, room.name)
} else {
queue.push([Math.floor(posX - 1), posY])
}
if (this.validPos(posX, posY + 1, size, room)) {
return new RoomPosition(posX, posY + 1, room.name)
} else {
queue.push([posX, posY + 1])
}
if (this.validPos(posX, posY - 1, size, room)) {
return new RoomPosition(posX, posY - 1, room.name)
} else {
queue.push([posX, posY - 1])
}
iteration++
}
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment