Skip to content

Instantly share code, notes, and snippets.

@0x4a
Last active August 29, 2015 14:00
Show Gist options
  • Save 0x4a/11126096 to your computer and use it in GitHub Desktop.
Save 0x4a/11126096 to your computer and use it in GitHub Desktop.
untrusted #js #game solution level 13 (https://alexnisnevich.github.io/untrusted/)
/*
* 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) {
// constants
var debugX = 0,
debugY = 10;
var keyX = map.getWidth() - 2,
keyY = 8;
function setDebugColor(orientation) {
switch (orientation) {
case 'right':
map.setSquareColor(debugX, debugY, '#ff1');
break;
case 'down':
map.setSquareColor(debugX, debugY, '#1f1');
break;
case 'left':
map.setSquareColor(debugX, debugY, '#11f');
break;
case 'up':
map.setSquareColor(debugX, debugY, '#f11');
break;
default:
map.setSquareColor(debugX, debugY, '#fff');
}
}
function turnRight(orientation) {
switch (orientation) {
case 'right':
return 'down';
case 'down':
return 'left';
case 'left':
return 'up';
case 'up':
return 'right';
default:
return false;
}
}
function turnLeft(orientation) {
map.setSquareColor(me.getX(), me.getY(), '#511');
switch (orientation) {
case 'right':
return 'up';
case 'down':
return 'right';
case 'left':
return 'down';
case 'up':
return 'left';
default:
return false;
}
}
function followTheWall(orientation) {
var out = turnRight(orientation);
map.setSquareColor(me.getX(), me.getY(), '#151');
me.move(out);
return out;
}
// init
if (typeof this.dir === "undefined") {
// debug status
map.defineObject('debug', {'symbol': '▀', 'color': '#000'} );
map.placeObject(debugX, debugY, 'debug');
map.setSquareColor(debugX, debugY, '#fff');
//get directions
if ( me.canMove('right') )
this.dir = 'right';
else if ( me.canMove('down') )
this.dir = 'down';
else if ( me.canMove('left') )
this.dir = 'left';
else if ( me.canMove('up') )
this.dir = 'up';
// set debug-display
setDebugColor(this.dir);
}
// walk maze
if (typeof this.found === "undefined") {
// get directions
if (me.canMove(turnRight(this.dir)))
this.dir = followTheWall(this.dir);
else if (me.canMove(this.dir))
me.move(this.dir);
else
this.dir = turnLeft(this.dir);
// set debug-display
setDebugColor(this.dir);
// found key
if (me.getX() == keyX && me.getY() == keyY) {
this.found = true;
map.setSquareColor(debugX, debugY, '#0f0');
map.setSquareColor(debugX, debugY + 1, '#0f0');
}
}
// search player
else {
if (!map.getPlayer().hasItem('blueKey')) {
var player = map.getPlayer();
var distX = me.getX() - player.getX();
var distY = me.getY() - player.getY();
var direction;
if (distY == 3 && distX == 3)
return;
if (distY > 0 && distY >= distX)
direction = 'up';
else if (distY < 0 && distY < distX)
direction = 'down';
else if (distX > 0 && distX >= distY)
direction = 'left';
else
direction = 'right';
if (me.canMove(direction))
me.move(direction);
}
else {
map.setSquareColor(me.getX(), me.getY(), '#151');
return;
}
}
}
});
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