Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 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. const keyX = map.getWidth() - 2;
  31. const keyY = 8;
  32. const barrierX = map.getWidth() - 2;
  33. const barrierY = 9;
  34.  
  35.  
  36. /*function getTrace(steps, visited, posX, posY) {
  37. if (posX === targetX && posY === targetY) {
  38. return steps;
  39. }
  40. map.setSquareColor(posX, posY, 'red')
  41. const moves = map.getAdjacentEmptyCells(posX, posY);
  42. for(const move of moves) {
  43. const alreadyVisited = visited.find(pos =>
  44. pos[0] == move[0][0] && pos[1] == move[0][1]);
  45. if (alreadyVisited != undefined) {
  46. continue;
  47. }
  48. const result = getTrace(
  49. steps.concat(move[1]),
  50. visited.concat([posX, posY]),
  51. move[0][0],
  52. move[0][1]);
  53. if (result != undefined) { return result; }
  54. }
  55. }
  56.  
  57. const path = getTrace([], [],me.getX(), me.getY());
  58. me.move(path[0]);*/
  59.  
  60. // move randomly
  61. var moves = map.getAdjacentEmptyCells(me.getX(), me.getY());
  62. // getAdjacentEmptyCells gives array of ((x, y), direction) pairs
  63. //me.move(moves[map.getRandomInt(0, moves.length - 1)][1]);
  64.  
  65.  
  66. if (me.getX() == keyX &&
  67. me.getY() == keyY - 1) {
  68. // above key
  69. moves.push([[keyX, keyY],'down']);
  70. }
  71. if (me.getX() == keyX - 1 &&
  72. me.getY() == keyY) {
  73. // left from key
  74. moves.push([[keyX, keyY],'right']);
  75. }
  76. if (me.getX() == keyX &&
  77. me.getY() == keyY) {
  78. // on key
  79. moves.push([[barrierX, barrierY],'down']);
  80. }
  81.  
  82. if (me.direction == undefined) {
  83. me.direction = 'right';
  84. }
  85. function directionPossible(direction) {
  86. return moves.find(move =>
  87. move[1] == direction) != undefined;
  88. }
  89.  
  90. const probing = {
  91. up: 'left',
  92. down: 'right',
  93. right: 'up',
  94. left: 'down'
  95. };
  96.  
  97. const nextDirection = {
  98. up: 'right',
  99. down: 'left',
  100. right: 'down',
  101. left: 'up'
  102. };
  103.  
  104. while(true) {
  105. const probingDirection = probing[me.direction];
  106.  
  107. if (directionPossible(probingDirection)) {
  108. me.direction = probingDirection;
  109. me.move(probingDirection);
  110. map.setSquareColor(0, 0, 'red');
  111. return;
  112. } else if (directionPossible(me.direction)){
  113. me.move(me.direction);
  114. map.setSquareColor(0, 0, 'green');
  115. return;
  116. } else {
  117. me.direction = nextDirection[me.direction];
  118. map.setSquareColor(0, 0, 'yellow');
  119. }
  120.  
  121. }
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168. }
  169. });
  170.  
  171. map.defineObject('barrier', {
  172. 'symbol': '░',
  173. 'color': 'purple',
  174. 'impassable': true,
  175. 'passableFor': ['robot']
  176. });
  177.  
  178. map.placeObject(0, map.getHeight() - 1, 'exit');
  179. map.placeObject(1, 1, 'robot');
  180. map.placeObject(map.getWidth() - 2, 8, 'blueKey');
  181. map.placeObject(map.getWidth() - 2, 9, 'barrier');
  182.  
  183. var autoGeneratedMaze = new ROT.Map.DividedMaze(map.getWidth(), 10);
  184. autoGeneratedMaze.create( function (x, y, mapValue) {
  185. // don't write maze over robot or barrier
  186. if ((x == 1 && y == 1) || (x == map.getWidth() - 2 && y >= 8)) {
  187. return 0;
  188. } else if (mapValue === 1) { //0 is empty space 1 is wall
  189. map.placeObject(x,y, 'block');
  190. } else {
  191. map.placeObject(x,y,'empty');
  192. }
  193. });
  194. }
  195.  
  196. function validateLevel(map) {
  197. map.validateExactlyXManyObjects(1, 'exit');
  198. map.validateExactlyXManyObjects(1, 'robot');
  199. map.validateAtMostXObjects(1, 'blueKey');
  200. }
  201.  
  202. function onExit(map) {
  203. if (!map.getPlayer().hasItem('blueKey')) {
  204. map.writeStatus("We need to get that key!");
  205. return false;
  206. } else {
  207. return true;
  208. }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement