Skip to content

Instantly share code, notes, and snippets.

@Untrusted-Game
Created March 24, 2019 18:43
Show Gist options
  • Save Untrusted-Game/9c8805b3b983d030ed016db41d1bc809 to your computer and use it in GitHub Desktop.
Save Untrusted-Game/9c8805b3b983d030ed016db41d1bc809 to your computer and use it in GitHub Desktop.
Solution to level 10 in Untrusted: http://alex.nisnevich.com/untrusted/
/*************
* ambush.js *
*************
*
* Oh. Oh, I see. This wasn't quite part of the plan.
*
* Looks like they won't let you take the Algorithm
* without a fight. You'll need to carefully weave your
* way through the guard drones.
*
* Well, time to improvise. Let's mess with their programming
* a little, shall we?
*/
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) {
if(me.getY()==13 && me.getX() == 23)
{
me.move('left');
}else{
me.move('up');
}
}
});
map.defineObject('reinforcementDrone', {
'type': 'dynamic',
'symbol': 'd',
'color': 'yellow',
'onCollision': function (player) {
player.killedBy('a reinforcement drone');
},
'behavior': function (me) {
me.move('up');
}
});
map.defineObject('defenseDrone', {
'type': 'dynamic',
'symbol': 'd',
'color': 'green',
'onCollision': function (player) {
player.killedBy('a defense drone');
},
'behavior': function (me) {
if(me.getY()==13 && me.getX() == 27)
{
me.move('right');
}else{
me.move('up');
}
}
});
// just for decoration
map.defineObject('water', {
'symbol': '░',
'color': '#44f'
});
map.placePlayer(0, 12);
for (var x = 0; x < map.getWidth(); x++) {
map.placeObject(x, 10, 'block');
map.placeObject(x, 14, 'block');
for (var y = 20; y < map.getHeight(); y++) {
map.placeObject(x, y, 'water');
}
}
map.placeObject(23, 11, 'attackDrone');
map.placeObject(23, 12, 'attackDrone');
map.placeObject(23, 13, 'attackDrone');
map.placeObject(27, 11, 'defenseDrone');
map.placeObject(27, 12, 'defenseDrone');
map.placeObject(27, 13, 'defenseDrone');
map.placeObject(24, 11, 'reinforcementDrone');
map.placeObject(25, 11, 'reinforcementDrone');
map.placeObject(26, 11, 'reinforcementDrone');
map.placeObject(24, 13, 'reinforcementDrone');
map.placeObject(25, 13, 'reinforcementDrone');
map.placeObject(26, 13, 'reinforcementDrone');
map.placeObject(map.getWidth()-1, 12, 'exit');
}
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