Guest User

Untitled

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