canCast as a null check


#1

I am experimenting with using canCast as my null check instead of "if enemy:"
When I run my code, the hero never stops casting, even if there are no enemies and he has already cast raise dead. He therefore never collects items. He also never uses self. attack(enemy) by the way, he seems to always cast something. Any ideas?


loop:
    target=self.findNearest(self.findEnemies())
    if target:
        if target.type is not "sand-yak":
            if target.type is not "zombie":
                enemy=target
    item=self.findNearest(self.findItems())
    flag=self.findFlag()
    drain=self.canCast("drain-life", enemy)
    fear=self.canCast("fear", enemy)
    dead=self.canCast("raise-dead")
    root=self.canCast("root", enemy)
    if flag:
        x=flag.pos.x
        y=flag.pos.y
        self.pickUpFlag(flag)
    if root:
        self.cast("root", enemy)
    if fear:
        self.cast("fear", enemy)
    if dead:
        self.castRaiseDead()
    if drain:
        self.cast("drain-life", enemy)
    elif enemy:
        self.attack(enemy)
    elif item:
        x=item.pos.x
        y=item.pos.y
        self.moveXY(x, y)

#2

What if you reset enemy to None at the beginning of your loop? Currently it will always keep the old enemy even after you kill it, because there’s nothing to null it out–you only assign from target to enemy in the case where you have a target match.


#3

Interesting… added that and now I get an error when he casts drain that he needs something to cast on.

loop:
    enemy=None
    target=self.findNearest(self.findEnemies())
    if target:
        if target.type is not "sand-yak":
            if target.type is not "zombie":
                enemy=target
    item=self.findNearest(self.findItems())
    flag=self.findFlag()
    drain=self.canCast("drain-life", enemy)
    fear=self.canCast("fear", enemy)
    dead=self.canCast("raise-dead")
    root=self.canCast("root", enemy)
    if flag:
        x=flag.pos.x
        y=flag.pos.y
        self.pickUpFlag(flag)
    if root:
        self.cast("root", enemy)
    if fear:
        self.cast("fear", enemy)
    if dead:
        self.castRaiseDead()
    if drain:
        self.cast("drain-life", enemy)

#4

What if you change those if to elif? Generally things will work best if you only take one action per loop, as otherwise your targets might have gotten stale.

I know there is a bug with raise dead where it can currently need you to target yourself (I will fix this soon); what if you try that, even though it appears to be saying it’s drain life that needs the target?