Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Brewball Help With Vectors

// Say anything within 10m of Omarn for him to throw a potion.
// Catch the potion by standing near it before it lands.
while (true) {
    var potion = hero.findFriendlyMissiles()[0];
    var firetraps = hero.findNearest(hero.findHazards());
    // Remember that a Fire Trap will trigger if you move closer than 3 meters!
    var omarn = hero.findByType("potion-master")[0];
    if (potion) {
        var dest = new Vector(potion.Targetpos);
        // Go get the potion.
        var potion = Vector.subtract(dest, hero.pos);
        potion = Vector.normalize(potion);
        potion = Vector.multiply(potion, 5);
        if (hero.distanceTo(firetraps) < 3) {
            var mineVec = Vector.subtract(hero.pos, firetraps.pos);
            mineVec = Vector.normalize(mineVec);
            mineVec = Vector.multiply(mineVec, 3);
            potion = Vector.add(potion, mineVec);
        var moveToPos = Vector.add(hero.pos, potion);
        hero.move(moveToPos);    // Warning: isPathClear doesn't work with Hazards!
    } else {
        if (omarn && hero.distanceTo(omarn) > 10) {
            // Move back to Omarn.
        } else {
            hero.say("Hup, hup!");

The code seems to move my character to the negatives, however I don’t understand why. When debugging it by saying what potion was, it kept on saying negative x and y coordinates

You need to change this line:

var dest = new Vector(potion.Targetpos);

You don’t need the new or the Vector. Just potion.targetPos (not Targetpos).
Your code will still need tweaking & you’ll need to add vectors for you return journey, but the hero should go in the right direction now.

1 Like

Thanks Danny! I think I also wrote Targetpos wrong so when I changed it, it finally moved to the potion. The return was easy because it was just the same with the mine vectors except you had to use Omarn’s position instead of the potion.