SHARE
TWEET

Untitled

a guest Jul 18th, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * robotMaze.js
  3.  *
  4.  * The blue key is inside a labyrinth, and extracting
  5.  * it will not be easy.
  6.  *
  7.  * It's a good thing that you're a AI expert, or
  8.  * we would have to leave empty-handed.
  9.  */
  10.  
  11. function startLevel(map) {
  12.     // Hint: you can press R or 5 to "rest" and not move the
  13.     // player, while the robot moves around.
  14.  
  15.     map.getRandomInt = function(min, max) {
  16.         return Math.floor(Math.random() * (max - min + 1)) + min;
  17.     }
  18.  
  19.     map.placePlayer(map.getWidth()-1, map.getHeight()-1);
  20.     var player = map.getPlayer();
  21.  
  22.     map.defineObject('robot', {
  23.         'type': 'dynamic',
  24.         'symbol': 'R',
  25.         'color': 'gray',
  26.         'onCollision': function (player, me) {
  27.             me.giveItemTo(player, 'blueKey');
  28.         },
  29.         'behavior': function (me) {
  30.    
  31.                  // move randomly
  32.             var moves = map.getAdjacentEmptyCells(me.getX(), me.getY());
  33.             // getAdjacentEmptyCells gives array of ((x, y), direction) pairs
  34.             //me.move(moves[map.getRandomInt(0, moves.length - 1)][1]);
  35.             if (typeof( me.direction ) === 'undefined') {
  36.               me.direction = 'up';
  37.               me.dmx = {}
  38.               me.dmx['up'] = [0, -1];
  39.               me.dmx['right'] = [1, 0];
  40.               me.dmx['down'] = [0, 1];
  41.               me.dmx['left'] = [-1, 0];
  42.              
  43.               me.cwmx = {}
  44.               me.cwmx['up'] = 'right';
  45.               me.cwmx['right'] = 'down';
  46.               me.cwmx['down'] = 'left';
  47.               me.cwmx['left'] = 'up';
  48.               me.ccwmx = {}
  49.               me.ccwmx['up'] = 'left';
  50.               me.ccwmx['right'] = 'up';
  51.               me.ccwmx['down'] = 'right';
  52.               me.ccwmx['left'] = 'down';
  53.  
  54.                 me.getLeft = function() {
  55.                 xy = me.dmx[ me.ccwmx[ me.direction ] ];
  56.                 //xy = me.dmx[ 'up' ];
  57.                 //alert(me.direction+"xy = " + xy[0] + ", " + xy[1]);
  58.                 xy = [me.getX() + xy[0], me.getY() + xy[1]];
  59.                 r = map.getObjectTypeAt( xy[0], xy[1] );
  60.                 //alert("l = " + r + " at " + xy[0] + ", " + xy[1] + ":" + xy);
  61.                 return r;
  62.               }
  63.                 me.getFront = function() {
  64.                 xy = me.dmx[ me.direction ];
  65.                 xy = [me.getX() + xy[0], me.getY() + xy[1]];
  66.                 r = map.getObjectTypeAt( xy[0], xy[1] );
  67.                 //alert("r = " + r + " at " + xy[0] + ", " + xy[1]);
  68.                 return r;
  69.               }
  70.             }
  71.             if ('empty' === me.getLeft()) {
  72.               // turn left
  73.               me.direction = me.ccwmx[ me.direction ];
  74.             } else if ('empty' !== me.getFront() &&
  75.                        'barrier' !== me.getFront()) {
  76.               // turn right
  77.               console.log('front = '+me.getFront());
  78.               me.direction = me.cwmx[ me.direction ];
  79.             }
  80.             me.move( me.direction );
  81.  
  82.  
  83.  
  84.  
  85.         }
  86.     });
  87.  
  88.     map.defineObject('barrier', {
  89.         'symbol': '░',
  90.         'color': 'purple',
  91.         'impassable': true,
  92.         'passableFor': ['robot']
  93.     });
  94.  
  95.     map.placeObject(0, map.getHeight() - 1, 'exit');
  96.     map.placeObject(1, 1, 'robot');
  97.     map.placeObject(map.getWidth() - 2, 8, 'blueKey');
  98.     map.placeObject(map.getWidth() - 2, 9, 'barrier');
  99.  
  100.     var autoGeneratedMaze = new ROT.Map.DividedMaze(map.getWidth(), 10);
  101.     autoGeneratedMaze.create( function (x, y, mapValue) {
  102.         // don't write maze over robot or barrier
  103.         if ((x == 1 && y == 1) || (x == map.getWidth() - 2 && y >= 8)) {
  104.             return 0;
  105.         } else if (mapValue === 1) { //0 is empty space 1 is wall
  106.             map.placeObject(x,y, 'block');
  107.         } else {
  108.             map.placeObject(x,y,'empty');
  109.         }
  110.     });
  111. }
  112.  
  113. function validateLevel(map) {
  114.     map.validateExactlyXManyObjects(1, 'exit');
  115.     map.validateExactlyXManyObjects(1, 'robot');
  116.     map.validateAtMostXObjects(1, 'blueKey');
  117. }
  118.  
  119. function onExit(map) {
  120.     if (!map.getPlayer().hasItem('blueKey')) {
  121.         map.writeStatus("We need to get that key!");
  122.         return false;
  123.     } else {
  124.         return true;
  125.     }
  126. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top