Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 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.  
  31. if (me.canMove('up')
  32. && !me.canMove('down')
  33. && !me.canMove('left')
  34. && !me.canMove('right')
  35. ) {
  36. me.move('up');
  37. map.placeObject(me.getX(), me.getY()+1, 'block');
  38. } else if (!me.canMove('up')
  39. && me.canMove('down')
  40. && !me.canMove('left')
  41. && !me.canMove('right')
  42. ) {
  43. me.move('down');
  44. map.placeObject(me.getX(), me.getY()-1, 'block');
  45. } else if (!me.canMove('up')
  46. && !me.canMove('down')
  47. && me.canMove('left')
  48. && !me.canMove('right')
  49. ) {
  50. me.move('left');
  51. map.placeObject(me.getX()+1, me.getY(), 'block');
  52. } else if (!me.canMove('up')
  53. && !me.canMove('down')
  54. && !me.canMove('left')
  55. && me.canMove('right')
  56. ) {
  57. me.move('right');
  58. map.placeObject(me.getX()-1, me.getY(), 'block');
  59.  
  60. } else if (me.canMove('down')) {
  61. me.move('down');
  62. } else if (me.canMove('right')) {
  63. me.move('right');
  64. } else if (me.canMove('up')) {
  65. me.move('up');
  66. map.placeObject(me.getX(), me.getY()+1, 'block');
  67. } else if (me.canMove('left')) {
  68. me.move('left');
  69. map.placeObject(me.getX()+1, me.getY(), 'block');
  70. }
  71.  
  72.  
  73. // move randomly
  74. //var moves = map.getAdjacentEmptyCells(me.getX(), me.getY());
  75. // getAdjacentEmptyCells gives array of ((x, y), direction) pairs
  76. //me.move(moves[map.getRandomInt(0, moves.length - 1)][1]);
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123. }
  124. });
  125.  
  126. map.defineObject('barrier', {
  127. 'symbol': '░',
  128. 'color': 'purple',
  129. 'impassable': true,
  130. 'passableFor': ['robot']
  131. });
  132.  
  133. map.placeObject(0, map.getHeight() - 1, 'exit');
  134. map.placeObject(1, 1, 'robot');
  135. map.placeObject(map.getWidth() - 2, 8, 'blueKey');
  136. map.placeObject(map.getWidth() - 2, 9, 'barrier');
  137.  
  138. var autoGeneratedMaze = new ROT.Map.DividedMaze(map.getWidth(), 10);
  139. autoGeneratedMaze.create( function (x, y, mapValue) {
  140. // don't write maze over robot or barrier
  141. if ((x == 1 && y == 1) || (x == map.getWidth() - 2 && y >= 8)) {
  142. return 0;
  143. } else if (mapValue === 1) { //0 is empty space 1 is wall
  144. map.placeObject(x,y, 'block');
  145. } else {
  146. map.placeObject(x,y,'empty');
  147. }
  148. });
  149. }
  150.  
  151. function validateLevel(map) {
  152. map.validateExactlyXManyObjects(1, 'exit');
  153. map.validateExactlyXManyObjects(1, 'robot');
  154. map.validateAtMostXObjects(1, 'blueKey');
  155. }
  156.  
  157. function onExit(map) {
  158. if (!map.getPlayer().hasItem('blueKey')) {
  159. map.writeStatus("We need to get that key!");
  160. return false;
  161. } else {
  162. return true;
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement