Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 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(player.getX(), player.getY());
  32. // getAdjacentEmptyCells gives array of ((x, y), direction) pairs
  33.  
  34. if (player.atLocation(map.getWidth() - 1, map.getHeight() - 2)) {
  35. me.move('right');
  36. } else if (
  37. player.atLocation(map.getWidth() - 2, map.getHeight() - 1)
  38. //me.move(moves[map.getRandomInt(0, moves.length - 1)][1]);
  39. ) {
  40. me.move('down');
  41. } else if (
  42. player.atLocation(map.getWidth() - 3, map.getHeight() - 2)
  43. //me.move(moves[map.getRandomInt(0, moves.length - 1)][1]);
  44. ) {
  45. me.move('left');
  46. } else if (
  47. player.atLocation(map.getWidth() - 2, map.getHeight() - 3)
  48. //me.move(moves[map.getRandomInt(0, moves.length - 1)][1]);
  49. ) {
  50. me.move('up');
  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.  
  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. map.defineObject('barrier', {
  102. 'symbol': '░',
  103. 'color': 'purple',
  104. 'impassable': true,
  105. 'passableFor': ['robot']
  106. });
  107.  
  108. map.placeObject(0, map.getHeight() - 1, 'exit');
  109. map.placeObject(1, 1, 'robot');
  110. map.placeObject(map.getWidth() - 2, 8, 'blueKey');
  111. map.placeObject(map.getWidth() - 2, 9, 'barrier');
  112.  
  113. var autoGeneratedMaze = new ROT.Map.DividedMaze(map.getWidth(), 10);
  114. autoGeneratedMaze.create( function (x, y, mapValue) {
  115. // don't write maze over robot or barrier
  116. if ((x == 1 && y == 1) || (x == map.getWidth() - 2 && y >= 8)) {
  117. return 0;
  118. } else if (mapValue === 1) { //0 is empty space 1 is wall
  119. map.placeObject(x,y, 'block');
  120. } else {
  121. map.placeObject(x,y,'empty');
  122. }
  123. });
  124. }
  125.  
  126. function validateLevel(map) {
  127. map.validateExactlyXManyObjects(1, 'exit');
  128. map.validateExactlyXManyObjects(1, 'robot');
  129. map.validateAtMostXObjects(1, 'blueKey');
  130. }
  131.  
  132. function onExit(map) {
  133. if (!map.getPlayer().hasItem('blueKey')) {
  134. map.writeStatus("We need to get that key!");
  135. return false;
  136. } else {
  137. return true;
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement