Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 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. var x = me.getX();
  32. var y = me.getY();
  33. var endX = map.getWidth() - 2;
  34.  
  35. if ((x == endX) && ((y == 7) || (y == 8) || (y == 9))) {
  36. // At key (or above) or barrier, move down.
  37. me.move('down');
  38. } else if ((x == endX-1) && ((y == 7) || (y == 8))) {
  39. // To the left of key (getAdjecent won't pick this up).
  40. if (me.canMove('right')) {
  41. me.move('right');
  42. } else {
  43. me.move('down');
  44. }
  45. } else if (! ((x == endX) && (y == 10)) ) {
  46. // Not outside.. figure out our move.
  47.  
  48. // Get available moves from here.
  49. var moves = map.getAdjacentEmptyCells(me.getX(), me.getY());
  50.  
  51. // See if we're at a 'deadspot'
  52. var deadspot = false;
  53. if (moves.length == 1) {
  54. deadspot = true;
  55. } else if (moves.length == 2) {
  56. // Probably a better way to figure this out, but...
  57. var topLeftCorner =
  58. (!me.canMove('left') && !me.canMove('up')) &&
  59. (map.getObjectTypeAt(x+1,y+1) != 'block' );
  60.  
  61. var topRightCorner =
  62. (!me.canMove('right') && !me.canMove('up')) &&
  63. (map.getObjectTypeAt(x-1,y+1) != 'block' );
  64.  
  65. var bottomLeftCorner =
  66. (!me.canMove('right') && !me.canMove('down')) &&
  67. (map.getObjectTypeAt(x+1,y-1) != 'block' );
  68.  
  69. var bottomRightCorner =
  70. (!me.canMove('left') && !me.canMove('down')) &&
  71. (map.getObjectTypeAt(x-1,y-1) != 'block' );
  72.  
  73. deadspot = topLeftCorner || topRightCorner ||
  74. bottomLeftCorner || bottomRightCorner;
  75. }
  76.  
  77. // Move randomly.
  78. me.move(moves[map.getRandomInt(0, moves.length - 1)][1]);
  79.  
  80. moves = map.getAdjacentEmptyCells(me.getX(), me.getY());
  81. if (deadspot == true) {
  82. // We have a deadspot - persist it with a block!
  83. map.placeObject(x,y, 'block');
  84. }
  85.  
  86. } // else we're free stay still!
  87.  
  88.  
  89. }
  90. });
  91.  
  92. map.defineObject('barrier', {
  93. 'symbol': '░',
  94. 'color': 'purple',
  95. 'impassable': true,
  96. 'passableFor': ['robot']
  97. });
  98.  
  99. map.placeObject(0, map.getHeight() - 1, 'exit');
  100. map.placeObject(1, 1, 'robot');
  101. map.placeObject(map.getWidth() - 2, 8, 'blueKey');
  102. map.placeObject(map.getWidth() - 2, 9, 'barrier');
  103.  
  104. var autoGeneratedMaze = new ROT.Map.DividedMaze(map.getWidth(), 10);
  105. autoGeneratedMaze.create( function (x, y, mapValue) {
  106. // don't write maze over robot or barrier
  107. if ((x == 1 && y == 1) || (x == map.getWidth() - 2 && y >= 8)) {
  108. return 0;
  109. } else if (mapValue === 1) { //0 is empty space 1 is wall
  110. map.placeObject(x,y, 'block');
  111. } else {
  112. map.placeObject(x,y,'empty');
  113. }
  114. });
  115. }
  116.  
  117. function validateLevel(map) {
  118. map.validateExactlyXManyObjects(1, 'exit');
  119. map.validateExactlyXManyObjects(1, 'robot');
  120. map.validateAtMostXObjects(1, 'blueKey');
  121. }
  122.  
  123. function onExit(map) {
  124. if (!map.getPlayer().hasItem('blueKey')) {
  125. map.writeStatus("We need to get that key!");
  126. return false;
  127. } else {
  128. return true;
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement