Guest User

Untitled

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