Advertisement
Guest User

Untitled

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