Guest User

Untitled

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