daily pastebin goal
59%
SHARE
TWEET

Untitled

a guest Apr 16th, 2018 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /****************
  2.  * drones101.js *
  3.  ****************
  4.  *
  5.  * Do you remember, my dear Professor, a certain introductory
  6.  * computational rationality class you taught long ago? Assignment
  7.  * #2, behavior functions of autonomous agents? I remember that one
  8.  * fondly - but attack drones are so much easier to reason about
  9.  * when they're not staring you in the face, I would imagine!
  10.  */
  11.  
  12. function startLevel(map) {
  13.     function moveToward(obj, type) {
  14.         var target = obj.findNearest(type);
  15.         var leftDist = obj.getX() - target.x;
  16.         var upDist = obj.getY() - target.y;
  17.  
  18.         var direction;
  19.         if (upDist == 0 && leftDist == 0) {
  20.             return;
  21.         } if (upDist > 0 && upDist >= leftDist) {
  22.             direction = 'up';
  23.         } else if (upDist < 0 && upDist < leftDist) {
  24.             direction = 'down';
  25.         } else if (leftDist > 0 && leftDist >= upDist) {
  26.             direction = 'left';
  27.         } else {
  28.             direction = 'right';
  29.         }
  30.  
  31.         if (obj.canMove(direction)) {
  32.             obj.move(direction);
  33.         }
  34.     }
  35.  
  36.     map.defineObject('attackDrone', {
  37.         'type': 'dynamic',
  38.         'symbol': 'd',
  39.         'color': 'red',
  40.         'onCollision': function (player) {
  41.             player.killedBy('an attack drone');
  42.         },
  43.         'behavior': function (me) {
  44.             moveToward(me, 'player');
  45.         }
  46.     });
  47.  
  48.  
  49.     map.placePlayer(1, 1);
  50.     map.placeObject(map.getWidth()-2, 12, 'attackDrone');
  51.     map.placeObject(map.getWidth()-1, 12, 'exit');
  52.  
  53.     map.placeObject(map.getWidth()-1, 11, 'block');
  54.     map.placeObject(map.getWidth()-2, 11, 'block');
  55.     map.placeObject(map.getWidth()-1, 13, 'block');
  56.     map.placeObject(map.getWidth()-2, 13, 'block');
  57.  
  58. map.defineObject('myattackDrone', {
  59.         'type': 'dynamic',
  60.         'symbol': 'd',
  61.         'color': 'blue',
  62.         'onCollision': function (player) {
  63.             moveToward(player, 'myattackDrone');
  64.         },
  65.         'behavior': function (me) {
  66.             moveToward(me, 'myattackDrone');
  67.         }
  68.     });
  69. map.placeObject(map.getWidth()-7, 10, 'myattackDrone');
  70. map.placeObject(map.getWidth()-7, 11, 'myattackDrone');
  71. map.placeObject(map.getWidth()-7, 12, 'myattackDrone');
  72. map.placeObject(map.getWidth()-7,13, 'myattackDrone');
  73.  
  74.  
  75. }
  76.  
  77. function validateLevel(map) {
  78.     map.validateExactlyXManyObjects(1, 'exit');
  79. }
RAW Paste Data
Top