Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  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. // move randomly
  31. var moves = map.getAdjacentEmptyCells(me.getX(), me.getY());
  32. // getAdjacentEmptyCells gives array of ((x, y), direction) pairs
  33.  
  34. if(me.canMove('down'))return me.move('down')
  35. if(me.canMove('right'))return me.move('right')
  36. me.move('up')
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73. }
  74. });
  75.  
  76. map.defineObject('barrier', {
  77. 'symbol': '░',
  78. 'color': 'purple',
  79. 'impassable': true,
  80. 'passableFor': ['robot']
  81. });
  82.  
  83. map.placeObject(0, map.getHeight() - 1, 'exit');
  84. map.placeObject(1, 1, 'robot');
  85. map.placeObject(map.getWidth() - 2, 8, 'blueKey');
  86. map.placeObject(map.getWidth() - 2, 9, 'barrier');
  87.  
  88. var autoGeneratedMaze = new ROT.Map.DividedMaze(map.getWidth(), 10);
  89. autoGeneratedMaze.create( function (x, y, mapValue) {
  90. // don't write maze over robot or barrier
  91. if ((x == 1 && y == 1) || (x == map.getWidth() - 2 && y >= 8)) {
  92. return 0;
  93. } else if (mapValue === 1) { //0 is empty space 1 is wall
  94. map.placeObject(x,y, 'block');
  95. } else {
  96. map.placeObject(x,y,'empty');
  97. }
  98. });
  99. }
  100.  
  101. function validateLevel(map) {
  102. map.validateExactlyXManyObjects(1, 'exit');
  103. map.validateExactlyXManyObjects(1, 'robot');
  104. map.validateAtMostXObjects(1, 'blueKey');
  105. }
  106.  
  107. function onExit(map) {
  108. if (!map.getPlayer().hasItem('blueKey')) {
  109. map.writeStatus("We need to get that key!");
  110. return false;
  111. } else {
  112. return true;
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement