Getting controlled by the enemy hero (Dueling Grounds)

Hi everyone,
I just start playing as a ranger, and I don’t really know all the “distancing” for the bow thing. This error did not occur to me when I was fighting other players, can someone tell me why this is happening?

By the way, I didn’t have that much gems for all good equipment for ritic, if I have any equipment loss, tell me, thanks.

Here’s my code:

# Defeat the enemy hero in a duel!

def findEnemyHero():
    enemyHero = None
    bestHealth = 0
    for enemy in hero.findEnemies():
        if enemy.maxHealth > bestHealth:
            bestHealth = enemy.health
            enemyHero = enemy
    return enemyHero

while True:
    enemy = findEnemyHero()
    nearest = hero.findNearestEnemy()
    mPos = hero.pos
    if enemy and hero.distanceTo(nearest) > 10:
        dist = hero.distanceTo(enemy)
        ePos = enemy.pos
        mX = mPos.x
        mY = mPos.y
        eX = ePos.x
        eY = ePos.y
        if mX > eX:
            endX = 12
            buildX = mX - 4
        else:
            endX = 66
            buildX = mX + 4
        if mY > eY:
            endY = 16
            buildY = mY - 3
        else:
            endY = 52
            buildY = mY + 3
        if hero.isReady("shadow-vortex"):
            hero.shadowVortex(enemy.pos, Vector(endX, endY))
        if hero.isReady("wall-of-darkness") and hero.time > 10:
            hero.wallOfDarkness([hero.pos, Vector(buildX, buildY)]);
        hero.scattershot(enemy)
    elif nearest:
        hero.scattershot(nearest)

Screenshot:

Welcome to the forum @Watamelon ! :partying_face: This is a friendly place where you can ask help on levels, report bugs, or just chat with other coders! Have a great time :slight_smile:

Can you please post your gear?

You don’t need the gear, you can clearly see that they has the method ‘scattershot’

Seems like an error with internal code

Could you please post your full code? :slight_smile:

20 chars say code is posted

It was edited after I posted it, but thanks for notifying

Just do enemy = hero.findNearestEnemy() at the top instead of trying to find the opponent on every iteration and try the code then

Maybe try to write if enemy and nearest and hero.distanceTo(nearest) >10:?
Also, before scattershot you need to check if enemy:

I want myself to eliminate the hero first, not the nearest, that would be too slow.

I changed gift of the trees with the precision rifle, and I got the dragontooth, here’s my new code:

enemy = hero.findNearest([e for e in hero.findEnemies() if e.id is "Hero Placeholder"])
if enemy:
    if hero.distanceTo(enemy) < 25:
        hero.blink(enemy.pos)
    else:
        hero.phaseShift()
    hero.backstab(enemy)
    if hero.isReady("phase-shift"):
        hero.phaseShift()
        hero.moveXY(11, 16)
    else:
        hero.blink(Vector(11, 16))
        for i in range(2):
            if hero.pos.x != 11 and hero.pos.y != 16:
                hero.blink(Vector(11, 16))
else:
    hero.phaseShift()
    hero.moveXY(11, 16)
x1 = 9
x2 = 16
y1 = 22
y2 = 14

while True:
    enemy = hero.findNearest([e for e in hero.findEnemies() if e.id is "Hero Placeholder"])
    nearest = hero.findNearestEnemy()
    mPos = hero.pos
    time = hero.time
    if hero.gold > 80:
        hero.buildXY("paladin", 13, 19)
    for friend in hero.findByType("paladin"):
        if friend.team == "ogres":
            if friend.canCast("heal") and hero.health < 1400:
                hero.command(friend, "cast", "heal", hero)
            else:
                hero.command(friend, "shield")
    if enemy and nearest and hero.distanceTo(nearest) > 10:
        dist = hero.distanceTo(enemy)
        ePos = enemy.pos
        mX = mPos.x
        mY = mPos.y
        eX = ePos.x
        eY = ePos.y
        if mX > eX:
            endX = 12
        else:
            endX = 66
        if mY > eY:
            endY = 16
        else:
            endY = 52
        if hero.isReady("wall-of-darkness"):
            hero.wallOfDarkness([Vector(x1, y1), Vector(x2, y1), Vector(x2, y2)]);
        if hero.isReady("shadow-vortex") and dist <= 30 and time > 10:
            hero.shadowVortex(enemy.pos, Vector(endX, endY))
        hero.attack(enemy)
    elif nearest:
        hero.attack(nearest)
    if hero.health < 800 and hero.canCast("time-travel"):
        hero.cast("time-travel", hero)

So my new strategy is to go into a corner and defend, making a wall and sniping people. It seems that weapons with more range can lower the chance of this error happening. But the problem is that when I face enemies that cast invis, my hero stop running.

Exactly, but if you do enemy = hero.findNearestEnemy() at the top, then the only nearest enemy at the start of the game is the enemy hero…

Ok, got it. Thanks .

But think it again, if enemy cast invis, either the variable will get redefined, either hero will stay idle. And that is my problem.

Guys, I got a new code. This code can sucessfully beat basically all people who don’t cast invis. But when I duel people who cast invis, same error occurs and I can’t do anything.

# Defeat the enemy hero in a duel!

def onSpawn():
    while True:
        if pet.isReady("shape-shift"):
            pet.shapeShift()
        pet.moveXY(40, 36)

pet.on("spawn", onSpawn)

while True:
    enemy = hero.findNearest([e for e in hero.findEnemies() if e.id is "Hero Placeholder"])
    nearest = hero.findNearestEnemy()
    mPos = hero.pos
    time = hero.time
    blink = hero.isReady("blink")
    if hero.gold > 50:
        hero.summon("griffin-rider")
    for friend in hero.findByType("griffin-rider"):
        if friend.team == "ogres":
            target = friend.findNearestEnemy()
            if enemy:
                hero.command(friend, "attack", enemy)
            elif target:
                hero.command(friend, "attack", target)
    if enemy and nearest and hero.distanceTo(nearest) > 10:
        dist = hero.distanceTo(enemy)
        ePos = enemy.pos
        mX = mPos.x
        mY = mPos.y
        eX = ePos.x
        eY = ePos.y
        if mX > eX:
            endX = 12
            buildX = mX - 4
            blinkX = mX + 25
        else:
            endX = 66
            buildX = mX + 4
            blinkX = mX - 25
        if mY > eY:
            endY = 16
            buildY = mY - 3
            blinkY = mY + 25
        else:
            endY = 52
            buildY = mY + 3
            blinkY = mY - 25
        if hero.isReady("backstab"):
            lastX = mX
            lastY = mY
            if hero.isReady("phase-shift"):
                hero.phaseShift()
            hero.backstab(enemy)
            if blink:
                hero.blink(Vector(lastX, lastY))
        if dist < 12 and blink:
            hero.blink(Vector(blinkX, blinkY))
        if hero.isReady("shadow-vortex"):
            hero.shadowVortex(enemy.pos, Vector(endX, endY))
        if hero.isReady("wall-of-darkness") and time > 10:
            hero.wallOfDarkness([hero.pos, Vector(buildX, buildY)]);
        hero.scattershot(enemy)
    elif nearest:
        hero.scattershot(enemy)
    if hero.health < 800 and hero.canCast("time-travel"):
        hero.cast("time-travel", hero)

@PeterPalov I beat you!

I bought the blue fox. This lowers the chance of error occuring, and this will make enemy stop hiding. But can someone tell me the root of this cause?

Performance & accuracy tip: use [e for e in hero.findEnemies() if "Hero" in e.id][0] instead of hero.findNearest([e for e in hero.findEnemies() if e.id is "Hero Placeholder"]) (unless in for strings/arrays doesn’t work in CodeCombat…)

@moonwatcher348 @PeterPalov @Aya Same problem is occuring now. But it’s WAYY worser.
I didn’t even equip an invis ring, but I am somehow getting the invis effect and my hero is going idle, not even attacking decoys anymore.

def onSpawn():
    while True:
        if pet.isReady("shape-shift"):
            pet.shapeShift()
        if enemyHero:
            ePos = enemyHero.pos
            eX = ePos.x
            eY = ePos.y
            mX = mPos.x
            mY = mPos.y
            pet.moveXY((eX + mX) / 2 - (mX - eX), (eY + mY) / 2 - (mY - eY))
        else:
            pet.moveXY(40, 35)

def findEnemyHero():
    enemies = hero.findEnemies()
    enemyHero = None
    for enemy in enemies:
        if enemy.id == "Hero Placeholder":
            enemyHero = enemy
    return enemyHero

def sumDecoys():
    decoys = hero.findByType("decoy")
    count = 0
    for i in range(len(decoys)):
        decoy = decoys[i]
        if decoy.team == "ogres":
            count += 1
    return count

def decoyDistance():
    closestDist = 99999
    closestDecoy = None
    allDecoys = hero.findByType("decoy")
    for j in range(decoysCount):
        closest = allDecoys[j]
        if enemyHero and closest and closest.team == "ogres":
            distToHero = closest.distanceTo(enemyHero)
            if closest.distanceTo(enemyHero) < closestDist:
                closestDist = distToHero
                closestDecoy = closest
    return closestDist

pet.on("spawn", onSpawn)

while True:
    enemyHero = findEnemyHero()
    enemy = hero.findNearestEnemy()
    decoysCount = sumDecoys()
    decoyDist = decoyDistance()
    mPos = hero.pos
    gold = hero.gold
    if hero.canCast("earthskin"):
        hero.cast("earthskin", hero)
    if gold > 25 and enemyHero:
        ePos = enemyHero.pos
        eX = ePos.x
        eY = ePos.y
        buildPos = Vector.subtract(ePos, mPos)
        buildPos = Vector.normalize(buildPos)
        buildPos = Vector.multiply(buildPos, 4)
        buildPos = Vector.add(mPos, buildPos)
        buildX = buildPos.x
        buildY = buildPos.y
        if decoysCount < 2 or decoyDist > 30:
            hero.buildXY("decoy", buildX, buildY)
        elif gold > 50:
            hero.summon("griffin-rider")
    elif gold > 50:
        hero.summon("griffin-rider")
    for friend in hero.findByType("griffin-rider"):
        if friend.team == "ogres":
            target = friend.findNearestEnemy()
            if enemyHero:
                hero.command(friend, "attack", enemyHero)
            elif target:
                hero.command(friend, "attack", target)
            else:
                hero.command(friend, "defend", hero)
    if enemyHero:
        dist = hero.distanceTo(enemyHero)
        if hero.isReady("throw"):
            hero.throw(enemyHero)
        if hero.isReady("chain-lightning"):
            hero.cast("chain-lightning", enemyHero)
        if hero.isReady("bash"):
            hero.bash(enemyHero)
        if hero.isReady("stomp"):
            hero.stomp(enemyHero)
        if hero.isReady("build"):
            hero.buildXY("bear-trap", eX, eY)
    else:
        hero.shield()

I don’t know if this is it, but the enemy could be casting invis on you. Just a thought, I’ll see if I can think of anything else.

Edit: Edit deleted

Definitely more than 20 but I’ll type this anyway.

1 Like

It runs this error:

You cannot cast spellName "invisibility" on target "Hero Placeholder"

and the hero says:
I can't cast that spell #{spellName} on #{target}

1 Like