Quick, simple question - Sarven Sum


#1

Quick question guys. Below is my code for Sarven Sum:

whiteX = {'x':27, 'y':42}
redX = {'x':151 , 'y': 118}
while True:
    traps = hero.findByType("fire-trap")
    lowestTrap = None
    maxTrapVal = 999999
    trapIndex = 0
    if hero.health < hero.maxHealth / 2:
        if hero.canCast("regen", self):
            hero.cast("regen", self)
    while trapIndex < len(traps):
        if traps[trapIndex].value < maxTrapVal:
            lowestTrap = traps[trapIndex]
            maxTrapVal = lowestTrap.value
        trapIndex += 1
    traps = hero.findByType("fire-trap")
    highestTrap = None
    minTrapVal = 0
    trapIndex = 0
    while trapIndex < len(traps):
        if traps[trapIndex].value > minTrapVal:
            highestTrap = traps[trapIndex]
            minTrapVal = highestTrap.value
        trapIndex += 1

    hero.moveXY(28, 42)
    hero.say(lowestTrap.value + highestTrap.value)
    break
while True:
    enemies = hero.findEnemies()
    potion = hero.findByType("potion", hero.findItems())
    nearestEnemy = None
    maxDistance = 99999
    enemyIndex = 0
    while enemyIndex < len(enemies):
        if hero.distanceTo(enemies[enemyIndex]) < maxDistance:
            nearestEnemy = enemies[enemyIndex]
            maxDistance = hero.distanceTo(nearestEnemy)
        enemyIndex += 1
        break
    if nearestEnemy:
        while nearestEnemy.health > 0:
            if hero.canCast("lightning-bolt", nearestEnemy):
                hero.cast("lightning-bolt", nearestEnemy)
            else:
                hero.attack(nearestEnemy)
    else:
        hero.move(redX)

My question is in regards to the very last line…

Originally it was hero.moveXY(151, 118) and what would happen is my hero, after disabling the traps, would just start moving right to the coordinates regardless of if enemies were nearby, even though the code cleary says “if there are enemies nearby, kill them fools first.”(or does it?) When I remembered that whiteX and redX were defined by default, I changed it to the current code and it worked fine. Level is solved, just looking for some understanding here.

Thanks!


#2

The difference between the two is in hero.moveXY vs_ hero.move_. hero.moveXY sends your hero all the way to (x, y), while hero.move just moves your hero slightly towards the given target. What probably happened in the first version is you checked for enemies once, found none, and set your course to (151, 118). Once on that course, your hero can’t do anything else until he completes his action.

Tl;dr: you want to use hero.move when you’re putting it inside a while loop, so your hero can check for things and take actions in between steps.


#3

Makes sense, thanks!