Skip to content

[WIP] new tower placement algorithm, fixes bugs and improves #451

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions src/prototype_roomPosition.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

RoomPosition.prototype.checkTowerFillerPos = function() {
if (this.isBorder(3)) {
RoomPosition.prototype.checkTowerLinkPos = function() {
if (this.isBorder(2)) {
return false;
}

Expand Down
78 changes: 45 additions & 33 deletions src/prototype_room_init.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,62 +166,72 @@ Room.prototype.setPosition = function(type, pos, value, positionType = 'structur
this.setMemoryCostMatrix(costMatrix);
};

Room.prototype.setTowerFillerIterate = function(roomName, offsetDirection) {
let linkSet = false;
let towerFillerSet = false;
let positionsFound = false;
const path = this.getMemoryPath('pathStart-' + roomName);
/**
* Places one tower, filler, link along the path from a specific exit.
*
* @param {String} roomName
* @param {Number} offsetDirection (-1 or +1)
* @return {Boolean} success
*/
Room.prototype.setTowerIterate = function(roomName, offsetDirection) {
const path = this.getMemoryPathSidewalk('pathStart-' + roomName, offsetDirection);
let linkPos = null;
let fillerPos = null;
for (let pathIndex = path.length - 1; pathIndex >= 1; pathIndex--) {
const posPath = path[pathIndex];
const posPathObject = new RoomPosition(posPath.x, posPath.y, posPath.roomName);
const posPathNext = path[pathIndex - 1];

const directionNext = posPathObject.getDirectionTo(posPathNext.x, posPathNext.y, posPathNext.roomName);

const pos = posPathObject.getAdjacentPosition(directionNext + offsetDirection);
const pos = new RoomPosition(posPath.x, posPath.y, posPath.roomName);
const terrain = pos.lookFor(LOOK_TERRAIN)[0];

if (!pos.checkTowerFillerPos()) {
if (terrain === 'wall' || !pos.checkTowerLinkPos() || pos.isEqualTo(linkPos) || pos.isEqualTo(fillerPos)) {
linkPos = null;
fillerPos = null;
continue;
}

const terrain = pos.lookFor(LOOK_TERRAIN)[0];
if (terrain === 'wall') {
break;
}

if (!linkSet) {
this.setPosition('link', pos, config.layout.structureAvoid);
linkSet = true;
if (linkPos === null) {
linkPos = pos;
continue;
}
if (!towerFillerSet) {
this.setPosition('towerfiller', pos, config.layout.creepAvoid, 'creep');
towerFillerSet = true;

if (fillerPos === null) {
fillerPos = pos;
continue;
}

this.setPosition('link', linkPos, config.layout.structureAvoid);
this.setPosition('towerfiller', fillerPos, config.layout.creepAvoid, 'creep');
this.setPosition('tower', pos, config.layout.structureAvoid);
positionsFound = true;
break;
return true;
}

return positionsFound;
return false;
};

Room.prototype.setTowerFiller = function() {
const exits = _.map(Game.map.describeExits(this.name));

this.memory.position.creep.towerfiller = [];
for (let index = 0; index < CONTROLLER_STRUCTURES.tower[8] - 1; index++) {
let towersPlaced = 0;
let towersPlacedThisRound = 0;
for (let index = 0; towersPlaced < CONTROLLER_STRUCTURES.tower[8] - 1; index++) {
if (index % (exits.length * 2) === 0) {
if (index > 0 && towersPlacedThisRound === 0) {
// tried every exit, both sides, placed no towers, nowhere left to try
return false;
}
towersPlacedThisRound = 0;
}
const roomName = exits[index % exits.length];
if (!roomName) {
break;
return false;
}
for (let offsetDirection = 2; offsetDirection < 7; offsetDirection += 4) {
if (this.setTowerFillerIterate(roomName, offsetDirection)) {
break;
}
const offsetDirection = (Math.floor(index / exits.length) % 2) * 2 - 1;
if (this.setTowerIterate(roomName, offsetDirection)) {
towersPlaced++;
towersPlacedThisRound++;
}
}
return true;
};

Room.prototype.checkLabStructures = function(structurePos, pathPos) {
Expand Down Expand Up @@ -339,7 +349,9 @@ Room.prototype.setStructuresIteratePos = function(structurePos, pathI, path) {
};

Room.prototype.setStructures = function(path) {
this.setTowerFiller();
if (!this.setTowerFiller()) {
this.log('Failed to place all towers');
}

for (let pathI = 0; pathI < path.length; pathI++) {
const pathPos = new RoomPosition(path[pathI].x, path[pathI].y, this.name);
Expand Down
87 changes: 87 additions & 0 deletions src/prototype_room_memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,93 @@ Room.prototype.getMemoryPath = function(name) {
return false;
};

/**
* Returns a pseudo-path including every tile adjacent to one side of the given path
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function should be placed in _routing or somewhere else, but not in _memory file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm conflicted. It's theoretically a routing function, but the only place it's currently called is in_memory. I decided to put it here until/unless it gets used somewhere else. I could easily be convinced otherwise.

*
* @param {String} name - the name of the path
* @param {Number} side - +1 or -1 indicating which side of the path
* @return {array|boolean} path
*/
Room.prototype.getMemoryPathSidewalk = function(name, side = 1) {
const path = this.getMemoryPath(name);
if (!path) {
return false;
}
const sidewalk = [];
let prevDirection = null;
for (let pathIndex = 0; pathIndex < path.length; pathIndex++) {
const posPath = path[pathIndex];
const posPathObject = new RoomPosition(posPath.x, posPath.y, posPath.roomName);
const posPathNext = pathIndex < path.length - 1 ? path[pathIndex + 1] : null;
const directionNext = pathIndex < path.length - 1 ?
posPathObject.getDirectionTo(posPathNext.x, posPathNext.y, posPathNext.roomName) :
prevDirection;
prevDirection = prevDirection === null ? directionNext : prevDirection;

if (directionNext === prevDirection) {
// straight line
sidewalk.push(posPathObject.getAdjacentPosition(directionNext + side * 2 + 8));
if (directionNext % 2 === 0) {
// diagonal
sidewalk.push(posPathObject.getAdjacentPosition(directionNext + side + 8));
}
} else {
// corner
if (prevDirection % 2 === 0) {
if (directionNext % 2 === 0) {
// diagonal to diagonal
if ((directionNext - prevDirection + 8) % 8 === (side * 2 + 8) % 8) {
// turn towards side
// do nothing
} else {
// turn away from side
sidewalk.push(posPathObject.getAdjacentPosition(prevDirection + side * 2 + 8));
sidewalk.push(posPathObject.getAdjacentPosition(prevDirection + side + 8));
sidewalk.push(posPathObject.getAdjacentPosition(prevDirection));
sidewalk.push(posPathObject.getAdjacentPosition(prevDirection - side + 8));
}
} else {
// diagonal to orthogonal
if ((directionNext - prevDirection + 8) % 8 === (side + 8) % 8) {
// turn towards side
// do nothing
} else {
// turn away from side
sidewalk.push(posPathObject.getAdjacentPosition(prevDirection + side * 2 + 8));
sidewalk.push(posPathObject.getAdjacentPosition(prevDirection + side + 8));
}
}
} else {
if (directionNext % 2 === 0) {
// orthogonal to diagonal
if ((directionNext - prevDirection + 8) % 8 === (side + 8) % 8) {
// turn towards side
sidewalk.push(posPathObject.getAdjacentPosition(prevDirection + side * 2 + 8));
} else {
// turn away from side
sidewalk.push(posPathObject.getAdjacentPosition(prevDirection + side * 2 + 8));
sidewalk.push(posPathObject.getAdjacentPosition(prevDirection + side + 8));
sidewalk.push(posPathObject.getAdjacentPosition(prevDirection));
}
} else {
// orthogonal to orthogonal
if ((directionNext - prevDirection + 8) % 8 === (side * 2 + 8) % 8) {
// turn towards side
pathIndex++;
} else {
// turn away from side
sidewalk.push(posPathObject.getAdjacentPosition(prevDirection + side * 2 + 8));
sidewalk.push(posPathObject.getAdjacentPosition(prevDirection + side + 8));
sidewalk.push(posPathObject.getAdjacentPosition(prevDirection));
}
}
}
}
prevDirection = directionNext;
}
return sidewalk;
};

/**
* Cleans the cache and memory from all paths
*/
Expand Down