Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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) {
- const keyX = map.getWidth() - 2;
- const keyY = 8;
- const barrierX = map.getWidth() - 2;
- const barrierY = 9;
- /*function getTrace(steps, visited, posX, posY) {
- if (posX === targetX && posY === targetY) {
- return steps;
- }
- map.setSquareColor(posX, posY, 'red')
- const moves = map.getAdjacentEmptyCells(posX, posY);
- for(const move of moves) {
- const alreadyVisited = visited.find(pos =>
- pos[0] == move[0][0] && pos[1] == move[0][1]);
- if (alreadyVisited != undefined) {
- continue;
- }
- const result = getTrace(
- steps.concat(move[1]),
- visited.concat([posX, posY]),
- move[0][0],
- move[0][1]);
- if (result != undefined) { return result; }
- }
- }
- const path = getTrace([], [],me.getX(), me.getY());
- me.move(path[0]);*/
- // move randomly
- var moves = map.getAdjacentEmptyCells(me.getX(), me.getY());
- // getAdjacentEmptyCells gives array of ((x, y), direction) pairs
- //me.move(moves[map.getRandomInt(0, moves.length - 1)][1]);
- if (me.getX() == keyX &&
- me.getY() == keyY - 1) {
- // above key
- moves.push([[keyX, keyY],'down']);
- }
- if (me.getX() == keyX - 1 &&
- me.getY() == keyY) {
- // left from key
- moves.push([[keyX, keyY],'right']);
- }
- if (me.getX() == keyX &&
- me.getY() == keyY) {
- // on key
- moves.push([[barrierX, barrierY],'down']);
- }
- if (me.direction == undefined) {
- me.direction = 'right';
- }
- function directionPossible(direction) {
- return moves.find(move =>
- move[1] == direction) != undefined;
- }
- const probing = {
- up: 'left',
- down: 'right',
- right: 'up',
- left: 'down'
- };
- const nextDirection = {
- up: 'right',
- down: 'left',
- right: 'down',
- left: 'up'
- };
- while(true) {
- const probingDirection = probing[me.direction];
- if (directionPossible(probingDirection)) {
- me.direction = probingDirection;
- me.move(probingDirection);
- map.setSquareColor(0, 0, 'red');
- return;
- } else if (directionPossible(me.direction)){
- me.move(me.direction);
- map.setSquareColor(0, 0, 'green');
- return;
- } else {
- me.direction = nextDirection[me.direction];
- map.setSquareColor(0, 0, 'yellow');
- }
- }
- }
- });
- 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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement