Advertisement
Guest User

Untitled

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