Brewball: More Help With Vectors

I’m trying to work my way through Brewball (Glacier). I’m not so great with Vectors, but have been trying to implement the code from Skating Away. I’m actually pretty proud of myself - I’ve gone from being clueless to getting the first potion. The problem is, even after catching the first potion, my hero doesn’t go back to Omarn, and I’m not sure why not.

Here’s the part of my code I think is relevant:

while True:
    
    potion = hero.findFriendlyMissiles()[0]
    firetraps = hero.findHazards()
    nearestTrap = hero.findNearest(firetraps)
    # Remember that a Fire Trap will trigger if you move closer than 3 meters!
    omarn = hero.findByType("potion-master")[0]
    if potion:
        dest = potion.targetPos;
        goal = Vector.subtract(dest, hero.pos)
        goal = Vector.normalize(goal)
        goal = Vector.multiply(goal, 5)
        
        disToTrap = hero.distanceTo(nearestTrap)
        if disToTrap < 5:
            trapVector = Vector.subtract(hero.pos, nearestTrap.pos)
            trapVector = Vector.normalize(trapVector)
            trapVector = Vector.multiply(trapVector, 5)
            goal = Vector.add(trapVector, goal)
        
        moveToPos = Vector.add(hero.pos, goal)
        if hero.pos == dest:
            hero.wait(5)
        else:
            hero.move(moveToPos)
        

I even threw in that bit at the end in the hope that it would stop the hero from continuing after arriving where the potion is supposed to land, but it didn’t work.

Any help would be greatly appreciated.

Try using functions instead of only a while loop. Add a function and put that function in the loop with some other codes.

2 Likes

@Chaboi_3000 I’m curious about your reasoning. I thought creating functions would make my hero run into mines, because the function would run from wherever the hero was, and not be updating itself based on the hero’s new position as quickly. I don’t know if I’m right, but I do know that now my hero is running into the first mine in the path of the potion. It could very well be that you had something else in mind, though, so I’ll post the new code (as in, enough of it to be relevant):

def vectorize(pos1, pos2, size=5):
    goal = Vector.subtract(pos1, pos2)
    goal = Vector.normalize(goal)
    goal = Vector.multiply(goal, size)
    return goal

def avoid_trap(trap, hero, goal):
    trap_vector = Vector.subtract(trap, hero)
    trap_vector = Vector.normalize(trap_vector)
    trap_vector = Vector.multiply(trap_vector, 5)
    goal = Vector.add(trap_vector, goal)
    return goal

while True:
    
    potion = hero.findFriendlyMissiles()[0]
    firetraps = hero.findHazards()
    nearestTrap = hero.findNearest(firetraps)
    # Remember that a Fire Trap will trigger if you move closer than 3 meters!
   
    if potion:
        dest = potion.targetPos
        goal = vectorize(dest, hero.pos)
        
        disToTrap = hero.distanceTo(nearestTrap)
        if disToTrap < 5:
            # Avoid conflict with identical variable names - 
            goal1 = goal
            goal = avoid_trap(nearestTrap.pos, hero.pos, goal1)
        
        moveToPos = Vector.add(hero.pos, goal)
        hero.move(moveToPos)

I’m still quite new to vectors, so my assumption is that my logic is off. I would appreciate it if someone could point me to the problems.

Getting close, @Shmoogy, just a little more to go. Try updating the nearest trap in the if statement.

2 Likes

@Chaboi_3000 I get what you’re saying (I think), namely that my hero is moving past the nearest trap and getting blown up on the next one before the where-loop goes back around for another go, which would automatically update what the nearest trap is. Is the code really that slow?

If that’s the case, though, I have no idea where or how to update the nearest trap without messing up the code entirely. If I go into recursion or looping to plot a path through all the mines, I don’t know how to get back out of it and actually get anything done.

My experience with the code is that the hero blows up on the first mine in his way, too. Shouldn’t that mean that the problem is in the mine detection in the code?

Try reviewing the levels circle walking and skating away again. I hope you do it! And just to tell you here’s the two levels.
35

1 Like