Advertisement
Guest User

Untitled

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