Skip to content
This repository has been archived by the owner on Oct 1, 2020. It is now read-only.

Commit

Permalink
fix(ai): AI now wanders the dungeon more intelligently. monsters now …
Browse files Browse the repository at this point in the history
…spawn every 20 steps (instead of 100) to compensate.
  • Loading branch information
seiyria committed Dec 1, 2015
1 parent 39d9426 commit f622789
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/js/angular/controllers/Party.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ module.controller('PartyMemberEdit', ($scope, $uibModalInstance, TemplateDataMan
$scope.buff = upgradesBySplit('Buff:', 'None');

$scope.ais = [
{ key: 'Wander', val: undefined }
{ key: 'Explore Dungeon', val: undefined },
{ key: 'Wander', val: 'Wander' }
];
});
2 changes: 1 addition & 1 deletion src/js/rogue/constants/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default {
minStatValue: 3,
alignThreshold: 100,
baseAC: 10,
spawnSteps: 100,
spawnSteps: 20,
nameLength: 12,

display: {
Expand Down
66 changes: 66 additions & 0 deletions src/js/rogue/content/behaviors/targetting.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,72 @@ class WandersBehavior extends Behavior {
}
export const Wanders = () => new WandersBehavior();

class ExploresDungeonBehavior extends Behavior {
constructor() { super(Priority.MOVE); }

getCentralCoords(room) {
return {
x: room._x1 + ~~((room._x2 - room._x1)/2),
y: room._y1 + ~~((room._y2 - room._y1)/2)
};
}

checkForRoomActivity(me) {
// mark the current room, if any, as explored
const currentRoom = _.find(GameState.world.tiles[me.z].rooms, room => {
return room._x1 < me.x && room._x2 > me.x &&
room._y1 < me.y && room._x2 > me.y;
});

if(!currentRoom) return;
currentRoom.isExplored = true;
}

act(me) {
if(!me._path) return;

const rooms = GameState.world.tiles[me.z].rooms;

// take a random step if you've explored all the rooms
if(_.all(rooms, room => room.isExplored)) {
me.stepRandomly();
return;
}

if(!this.target) {
this.nextRoom = _.reject(rooms, room => room.isExplored)[0];
this.target = this.getCentralCoords(this.nextRoom);
}

// check for stairs down but only if you're not on the last floor (because those stairs down don't do anything if they're there)
const stairsInRange = GameState.world.getValidTilesInRange(me.x, me.y, me.z, 10, tile => tile.constructor.name === 'StairsDown')[0];
if(stairsInRange && GameState.currentFloor !== GameState.world.depth-1) {
this.target = { x: stairsInRange.x, y: stairsInRange.y };
}

// if there is unexplored rooms or stairs in sight, go to the light
if(this.target) {
const pathToTarget = me.rebuildPathingMap(this.target.x, this.target.y);
me.stepTowards(this.target, pathToTarget);

if(this.target.x === me.x && this.target.y === me.y) {
this.checkForRoomActivity(me);
this.target = null;
this.nextRoom.isExplored = true;
}
return;
}

// there is nothing, all is vain, step randomly
me.stepRandomly();
}

step(me) {
this.checkForRoomActivity(me);
}
}
export const ExploresDungeon = () => new ExploresDungeonBehavior();

/* has very loud footsteps. pretty much, only players have or need this */
class AlertsOnStepBehavior extends Behavior {
constructor() { super(Priority.ALWAYS); }
Expand Down
9 changes: 6 additions & 3 deletions src/js/rogue/definitions/character.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,13 +440,16 @@ export default class Character extends Entity {
this.doBehavior('step');
}

stepTowards(target) {
if(!this.canSee(target)) {
stepTowards(target, basePath = target._path) {

// first check if it's just a coordinate pair
if(_.keys(target).length > 2 && !this.canSee(target)) {
return this.stepRandomly();
}

const path = [];
const addPath = (x, y) => path.push({ x, y });
target._path.compute(this.x, this.y, addPath);
basePath.compute(this.x, this.y, addPath);

path.shift();
const step = path.shift();
Expand Down
13 changes: 7 additions & 6 deletions src/js/rogue/definitions/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ export default class Player extends Character {

getDefaultExploreBehavior(template) {
const behaviors = {
Wander: Behaviors.Wanders
Wander: Behaviors.Wanders,
Explore: Behaviors.ExploresDungeon
};
this.behaviors.push(behaviors[template.ai || 'Wander']());
this.behaviors.push(behaviors[template.ai || 'Explore']());
}

getSpawnSteps() {
Expand Down Expand Up @@ -70,8 +71,8 @@ export default class Player extends Character {
super.act();
setTimeout(function() { engine.unlock(); }, Settings.game.turnDelay/livingPlayers.length);
}
this.rebuildPathingMap();

this._path = this.rebuildPathingMap();

if(this.currentTurn % this.getSpawnSteps() === 0) {
this.spawnMonster();
Expand All @@ -88,15 +89,15 @@ export default class Player extends Character {
}
}

rebuildPathingMap() {
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;
return GameState.world.isTilePassable(x, y, this.z) || isMe || isAttackable;
};

this._path = new ROT.Path.Dijkstra(this.x, this.y, canPass, { topology: 8 });
return new ROT.Path.Dijkstra(targetX, targetY, canPass, { topology: 8 });
}

die(killer) {
Expand Down
2 changes: 1 addition & 1 deletion src/js/rogue/worldgen/maptypes/altar.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ export default class Altar extends Generator {
const altarTile = this.getRandomFloorTile(map);
this.placeTile(map, Tiles.SelykAltar, altarTile.x, altarTile.y, z);

return { map, stairs, mapName: `Selyk's Altar`, shortMapName: 'Altar' };
return { map, stairs, rooms: [room], mapName: `Selyk's Altar`, shortMapName: 'Altar' };
}
}
16 changes: 9 additions & 7 deletions src/js/rogue/worldgen/maptypes/dungeon.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ export default class Dungeon extends Generator {
this.placeCorridorTiles(map, corridor, z);
});

if(digger.getRooms().length < 2) {
const rooms = digger.getRooms();

if(rooms.length < 2) {
Log('DungeonGenerator', 'Only one room was generated, this is probably a rare bug.');
}

// handle room outlines and doors
_.each(digger.getRooms(), (room) => {
_.each(rooms, (room) => {

// draw left and right walls
this.drawVerticalWalls(map, room, z);
Expand All @@ -46,15 +48,15 @@ export default class Dungeon extends Generator {
});

const [stairsUp, stairsDown] = this.getStairs(z);
const rooms = _.sample(digger.getRooms(), 2);
const chosenRooms = _.sample(digger.getRooms(), 2);
const stairs = [
this.placeStairsInRoom(map, rooms[0], z, stairsUp),
stairsDown ? this.placeStairsInRoom(map, rooms[1], z, stairsDown) : null
this.placeStairsInRoom(map, chosenRooms[0], z, stairsUp),
stairsDown ? this.placeStairsInRoom(map, chosenRooms[1], z, stairsDown) : null
];

this.attemptFeaturePlacement(map, z, digger.getRooms());
this.attemptFeaturePlacement(map, z, rooms);

return { map, stairs, mapName: 'The Dungeons of Doom', shortMapName: 'Dungeon' };
return { map, stairs, rooms, mapName: 'The Dungeons of Doom', shortMapName: 'Dungeon' };
}

static placeCorridorTiles(map, corridor, z) {
Expand Down
5 changes: 3 additions & 2 deletions src/js/rogue/worldgen/world.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ export default class World {
}

setMapAt(floor, i) {
const { map, mapName, shortMapName, stairs } = floor;
const { map, mapName, rooms, shortMapName, stairs } = floor;
this.tiles[i] = map;
this.tiles[i].mapName = mapName;
this.tiles[i].shortMapName = shortMapName;
this.tiles[i].rooms = rooms;
const [upStairs, downStairs] = stairs;

this.stairs[i] = {};
Expand Down Expand Up @@ -114,7 +115,7 @@ export default class World {
tile.x = x;
tile.y = y;
tile.z = z;
this.tiles [z][x][y] = tile;
this.tiles[z][x][y] = tile;
}

isVoid(x, y, z) {
Expand Down

0 comments on commit f622789

Please sign in to comment.