Skip to content

Instantly share code, notes, and snippets.

@CoderPuppy
Created May 11, 2014 22:25
Show Gist options
  • Select an option

  • Save CoderPuppy/8477196d5ad8afb167e7 to your computer and use it in GitHub Desktop.

Select an option

Save CoderPuppy/8477196d5ad8afb167e7 to your computer and use it in GitHub Desktop.
Untrusted Lv13
/*
* robotMaze.js
*
* The blue key is inside a labyrinth, and extracting
* it will not be easy.
*
* It's a good thing that you're a AI expert, or
* we would have to leave empty-handed.
*/
function startLevel(map) {
// Hint: you can press R or 5 to "rest" and not move the
// player, while the robot moves around.
map.getRandomInt = function(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
map.placePlayer(map.getWidth()-1, map.getHeight()-1);
var player = map.getPlayer();
map.defineObject('robot', {
'type': 'dynamic',
'symbol': 'R',
'color': 'gray',
'onCollision': function (player, me) {
me.giveItemTo(player, 'blueKey');
},
'behavior': function (me) {
try {
var oppT = {
up: 'down',
left: 'right',
right: 'left',
down: 'up'
}
var offT = {
up: [ 0, -1 ],
left: [ -1, 0 ],
right: [ 1, 0 ],
down: [ 0, 1 ]
}
var x = me.getX(), y = me.getY()
var testX, testY, testDir, testMovesN, testMoves, testMove
var testMoves = map.getAdjacentEmptyCells(x, y);
var dirs = []
// getAdjacentEmptyCells gives array of ((x, y), direction) pairs
for(var i = 0; i < testMoves.length; i++) {
testMove = testMoves[i]
testX = testMove[0][1]
testY = testMove[0][1]
testDir = testMove[1]
while(true) {
testMoves = map.getAdjacentEmptyCells(testX, testY)
.map(function(m) {
return m[1]
})
testMovesN = testMoves.length
if(~testMoves.indexOf(testDir)) {
testMovesN -= 1
}
if(~testMoves.indexOf(oppT[testDir])) {
testMovesN -= 1
}
if(testMovesN > 0) {
dirs.push(testDir)
/*map.writeStatus(
'testDir: ' + JSON.stringify(testDir) + ', ' +
'dirs: ' + JSON.stringify(testMoves)
)*/
}
if(testMoves.indexOf(testDir)) {
break
} else {
testX += offT[testDir][0]
testY += offT[testDir][1]
}
}
}
map.writeStatus('here: ' + JSON.stringify(dirs))
me.move(dirs[map.getRandomInt(0, dirs.length)])
} catch(e) {
//map.writeStatus(JSON.stringify(e.stack).substr(20))
throw e
}
}
});
map.defineObject('barrier', {
'symbol': '░',
'color': 'purple',
'impassable': true,
'passableFor': ['robot']
});
map.placeObject(0, map.getHeight() - 1, 'exit');
map.placeObject(1, 1, 'robot');
map.placeObject(map.getWidth() - 2, 8, 'blueKey');
map.placeObject(map.getWidth() - 2, 9, 'barrier');
var autoGeneratedMaze = new ROT.Map.DividedMaze(map.getWidth(), 10);
autoGeneratedMaze.create( function (x, y, mapValue) {
// don't write maze over robot or barrier
if ((x == 1 && y == 1) || (x == map.getWidth() - 2 && y >= 8)) {
return 0;
} else if (mapValue === 1) { //0 is empty space 1 is wall
map.placeObject(x,y, 'block');
} else {
map.placeObject(x,y,'empty');
}
});
}
function validateLevel(map) {
map.validateExactlyXManyObjects(1, 'exit');
map.validateExactlyXManyObjects(1, 'robot');
map.validateAtMostXObjects(1, 'blueKey');
}
function onExit(map) {
if (!map.getPlayer().hasItem('blueKey')) {
map.writeStatus("We need to get that key!");
return false;
} else {
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment