daily pastebin goal
23%
SHARE
TWEET

Untitled

a guest Aug 10th, 2018 52 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.  
  59.     for (x = 5; x <= map.getWidth() - 5; x++) {
  60.         map.placeObject(x, 10, 'block');
  61.     }
  62.  
  63. }
  64.  
  65. function validateLevel(map) {
  66.     map.validateExactlyXManyObjects(1, 'exit');
  67. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top