Guest User

Untitled

a guest
Jun 18th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 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.handOn) {
  31. me.handOn = 'up'
  32. }
  33. var clockwise = {
  34. 'up': 'right',
  35. 'right': 'down',
  36. 'down': 'left',
  37. 'left': 'up'
  38. }
  39. var sunwise = {
  40. 'up': 'left',
  41. 'left': 'down',
  42. 'down': 'right',
  43. 'right': 'up'
  44. }
  45.  
  46. if (me.canMove(me.handOn)) {
  47. // gap to the left; turn left
  48. me.move(me.handOn)
  49. me.handOn = sunwise[me.handOn]
  50. } else if (!me.canMove(clockwise[me.handOn])) {
  51. // ran into a wall; turn right
  52. me.handOn = clockwise[me.handOn]
  53. me.move(clockwise[me.handOn])
  54. } else {
  55. // wall to the left; move straight
  56. me.move(clockwise[me.handOn])
  57. }
  58. }
  59. });
  60.  
  61. map.defineObject('barrier', {
  62. 'symbol': '░',
  63. 'color': 'purple',
  64. 'impassable': true,
  65. 'passableFor': ['robot']
  66. });
  67.  
  68. map.placeObject(0, map.getHeight() - 1, 'exit');
  69. map.placeObject(1, 1, 'robot');
  70. map.placeObject(map.getWidth() - 2, 8, 'blueKey');
  71. map.placeObject(map.getWidth() - 2, 9, 'barrier');
  72.  
  73. var autoGeneratedMaze = new ROT.Map.DividedMaze(map.getWidth(), 10);
  74. autoGeneratedMaze.create( function (x, y, mapValue) {
  75. // don't write maze over robot or barrier
  76. if ((x == 1 && y == 1) || (x == map.getWidth() - 2 && y >= 8)) {
  77. return 0;
  78. } else if (mapValue === 1) { //0 is empty space 1 is wall
  79. map.placeObject(x,y, 'block');
  80. } else {
  81. map.placeObject(x,y,'empty');
  82. }
  83. });
  84. }
  85.  
  86. function validateLevel(map) {
  87. map.validateExactlyXManyObjects(1, 'exit');
  88. map.validateExactlyXManyObjects(1, 'robot');
  89. map.validateAtMostXObjects(1, 'blueKey');
  90. }
  91.  
  92. function onExit(map) {
  93. if (!map.getPlayer().hasItem('blueKey')) {
  94. map.writeStatus("We need to get that key!");
  95. return false;
  96. } else {
  97. return true;
  98. }
  99. }
Add Comment
Please, Sign In to add comment