Created
April 27, 2014 22:13
-
-
Save anonymous/11356905 to your computer and use it in GitHub Desktop.
Solution to level 6 in Untrusted: http://alex.nisnevich.com/untrusted/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**************** | |
* drones101.js * | |
**************** | |
* | |
* Do you remember, my dear Professor, a certain introductory | |
* computational rationality class you taught long ago? Assignment | |
* #2, behavior functions of autonomous agents? I remember that one | |
* fondly - but attack drones are so much easier to reason about | |
* when they're not staring you in the face, I would imagine! | |
*/ | |
function startLevel(map) { | |
function moveToward(obj, type) { | |
var target = obj.findNearest(type); | |
var leftDist = obj.getX() - target.x; | |
var upDist = obj.getY() - target.y; | |
var direction; | |
if (upDist == 0 && leftDist == 0) { | |
return; | |
} if (upDist > 0 && upDist >= leftDist) { | |
direction = 'up'; | |
} else if (upDist < 0 && upDist < leftDist) { | |
direction = 'down'; | |
} else if (leftDist > 0 && leftDist >= upDist) { | |
direction = 'left'; | |
} else { | |
direction = 'right'; | |
} | |
if (obj.canMove(direction)) { | |
obj.move(direction); | |
} | |
} | |
map.defineObject('attackDrone', { | |
'type': 'dynamic', | |
'symbol': 'd', | |
'color': 'red', | |
'onCollision': function (player) { | |
player.killedBy('an attack drone'); | |
}, | |
'behavior': function (me) { | |
moveToward(me, 'player'); | |
} | |
}); | |
map.placePlayer(1, 1); | |
map.placeObject(map.getWidth()-2, 12, 'attackDrone'); | |
map.placeObject(map.getWidth()-1, 12, 'exit'); | |
map.placeObject(map.getWidth()-1, 11, 'block'); | |
map.placeObject(map.getWidth()-2, 11, 'block'); | |
map.placeObject(map.getWidth()-1, 13, 'block'); | |
map.placeObject(map.getWidth()-2, 13, 'block'); | |
map.defineObject('helpDrone', { | |
'type': 'dynamic', | |
'symbol': 'd', | |
'color': 'yellow', | |
'onCollision': function (player) { | |
player.killedBy('an attack drone'); | |
}, | |
'behavior': function (me) { | |
moveToward(me, 'attackDrone'); | |
} | |
}); | |
function maybeCatch(obj, type) { | |
var target = obj.findNearest(type); | |
var leftDist = obj.getX() - target.x; | |
var upDist = obj.getY() - target.y; | |
var direction; | |
if (upDist == 0 && leftDist == 0) { | |
return; | |
} if (upDist > 0 && upDist >= leftDist) { | |
direction = 'up'; | |
} else if (upDist < 0 && upDist < leftDist) { | |
direction = 'down'; | |
} else if (leftDist > 0 && leftDist >= upDist) { | |
direction = 'left'; | |
} else { | |
direction = 'right'; | |
} | |
if (obj.canMove(direction)) { | |
obj.move(direction); | |
} else { | |
map.placeObject(target.x, target.y + 1, 'helpDrone'); | |
map.placeObject(target.x, target.y - 1, 'helpDrone'); | |
map.placeObject(target.x + 1, target.y, 'helpDrone'); | |
} | |
} | |
map.defineObject('policeDrone', { | |
'type': 'dynamic', | |
'symbol': 'd', | |
'color': 'blue', | |
'onCollision': function (player) { | |
player.killedBy('an attack drone'); | |
}, | |
'behavior': function (me) { | |
maybeCatch(me, 'attackDrone'); | |
} | |
}); | |
map.placeObject(map.getWidth()-20, 12, 'policeDrone'); | |
} | |
function validateLevel(map) { | |
map.validateExactlyXManyObjects(1, 'exit'); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment