Advertisement
Guest User

Untitled

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