Before, my peasant wasn’t moving, in serpent savings, but I was able to fix it. However, I keep running into the walls. Do you know how to avoid it? Here’s my code:
function findBestItem(friend, coins) {
var richestvalue = 0;
var closestitem = 9999;
var best = null;
for (var i = 0; i < coins.length; i++) {
var coin = coins[i];
if (coin.value > richestvalue && friend.distanceTo(coin) < closestitem) {
best = coin;
closestitem = friend.distanceTo(coin);
richestvalue = coin.value;
}
}
return best;
}
while (true){
var friend = hero.findFriends()[0];
var tails = hero.findEnemies();
var coins = hero.findItems();
// Command the peasant to collect a coin, while avoiding the tails.
// It may help to create a value/distance coin selector function for the peasant.
// Create a findBestItem function that matches the below parameters
var coin = findBestItem(friend, coins);
var goal = coin.pos;
// Enemy avoidance concepts from levels like "Skating Away" may help.
// Try writing a code that moves away from the friend.snakeBackward
for (var j = 0; j < tails.length; j++) {
var yak = friend.snakeBackward;
if (yak) {
// First, make a Vector from the yak to you
var safePos = Vector.subtract(friend.pos, yak.pos);
safePos = Vector.normalize(safePos);
safePos = Vector.multiply(safePos, 4);
// Once you have the 10m vector away from the yak, use Vector.add to add it to your goal vector!
goal = Vector.add(goal, safePos);
}
}
hero.command(friend, "move", goal);
}