Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
setTimeout(() => {
Game.engine.unlock()
let ladder = Game.getNearestLadder()
let levelTransition = Game.getNearestLevelTransition()
if (ladder !== null && ladder.direction !== 'up') {
let ladderPath = new ROT.Path.AStar(ladder.x, ladder.y, pathfinding)
let pathToLadder = []
ladderPath.compute(Game.player.x, Game.player.y, (x, y) => {
pathToLadder.push([x, y])
})
if (pathToLadder.length > 1) {
let newPos = pathToLadder[1] // 1 past the current position
Game.player.commandQueue.push({
fn: () => {
Game.player.tryMove(newPos[0], newPos[1])
}
})
} else {
Game.player.commandQueue.push({
fn: () => Game.player.climb()
})
}
const moveDwarves = () => {
const pathfinding = (x, y) => {
return Game.inbounds(x, y) && !Game.getTile(x, y).blocked()
}
let portalToTown = levelTransition[0]
let pathToExit = new ROT.Path.AStar(portalToTown.x, portalToTown.y, pathfinding)
const recomputePath = () => {
pathToExit = new ROT.Path.AStar(portalToTown.x, portalToTown.y, pathfinding)
}
let [d1, d2] = dwarves
// if (!removedSpritesAbove) {
// d1.removeSpriteAbove()
// d2.removeSpriteAbove()
// removedSpritesAbove = true
// }
let d1Path = []
let d2Path = []
pathToExit.compute(d1.x, d1.y, (x, y) => {
d1Path.push([x, y])
})
pathToExit.compute(d2.x, d2.y, (x, y) => {
d2Path.push([x, y])
recalculatePath() {
this.path = new ROT.Path.AStar(this.x, this.y, pathfinding)
}
export function createFovDijkstraMap(start, notVisibleTiles, blockedPredicate = outOfBoundsOrBlockedByAnything) {
const reachable = floodFill(start)
let dijkstraMap = new ROT.Path.Dijkstra(start.x, start.y, (x, y) => !blockedPredicate(x, y) && reachable[key(x, y)])
let distanceTransform = {}
for (let { x, y } of notVisibleTiles) {
let coord = key(x, y)
distanceTransform[coord] = 0
let steps = []
dijkstraMap.compute(x, y, (sx, sy) => {
steps.push({ x: sx, y: sy })
})
let distance = 0
for (let step of steps) {
distance += 1
let stepKey = key(step.x, step.y)
if (stepKey in distanceTransform && distance > distanceTransform[stepKey]) {
distance = distanceTransform[stepKey]
}
distanceTransform[stepKey] = distance
const recomputePath = () => {
pathToExit = new ROT.Path.AStar(portalToTown.x, portalToTown.y, pathfinding)
}
let [d1, d2] = dwarves
return actor => {
let target = data.entity
let stopCondition = data.stopCondition ? data.stopCondition : (...args) => false
if (stopCondition(data)) return
let configuredPathfinding = configurablePathfinding({
excludeOrigin: true,
origin: { x: actor.x, y: actor.y },
excludeTarget: true,
target
})
let path = new ROT.Path.AStar(target.x, target.y, configuredPathfinding)
let steps = []
path.compute(actor.x, actor.y, (x, y) => {
if (actor.x !== x || actor.y !== y) steps.push({ x, y })
})
if (steps.length >= 1) {
let { x, y } = steps[0]
actor.tryMove(x, y)
if (data.finalAction) data.finalAction()
}
if (steps.length > 1) {
actor.addGoal(AStarPathingGoal(data))
}
}
}
rebuildPathingMap(targetX = this.x, targetY = this.y) {
const canPass = (x, y) => {
const entity = GameState.world.getEntity(x, y, this.z);
const isAttackable = entity && this.canAttack(entity);
const isMe = this.x === x && this.y === y;
const isPlayer = entity && entity.constructor.name === 'Player';
return GameState.world.isTilePassable(x, y, this.z) || isPlayer || isMe || isAttackable;
};
return new ROT.Path.Dijkstra(targetX, targetY, canPass, { topology: 8 });
}
animate(owner, target, callback) {
if(!this.glyph) return callback();
const engine = GameState.game.engine;
engine.lock();
const canPass = (x, y) => {
const entity = GameState.world.getEntity(x, y, owner.z);
const isAttackable = entity && owner.canAttack(entity);
const isMe = owner.x === x && owner.y === y;
return GameState.world.isTilePassable(x, y, owner.z, false) || isMe || isAttackable;
};
const astar = new ROT.Path.AStar(target.x, target.y, canPass, { topology: 8 });
let path = [];
const pathCallback = function(x, y) {
path.push({ x, y });
};
astar.compute(owner.x, owner.y, pathCallback);
path.shift();
if(!path.length) path = [{ x: owner.x, y: owner.y }];
const projectile = new Projectile(this.glyph);
projectile.z = owner.z;
projectile.x = path[0].x;
getAlternatePathTo(target) {
const canPass = (x, y) => {
const entity = GameState.world.getEntity(x, y, this.z);
const isAttackable = entity && this.canAttack(entity);
const isMe = this.x === x && this.y === y;
return GameState.world.isTilePassable(x, y, this.z) || isMe || isAttackable;
};
const astar = new ROT.Path.AStar(target.x, target.y, canPass);
const path = [];
astar.compute(this.x, this.y, (x, y) => path.push({ x, y }));
path.shift();
const step = path.shift();
if(!step) return null;
return path;
}