I don’t want my hero or friends to attack the gates until we’ve cleaned out each room. So in each unit type’s command function I put this code:
if enemy and enemy.type != "gate":
hero.command(troop, "attack", enemy)
However, once the ogres have been all killed, I use this function:
def wreckDoor(newloc):
gate = hero.findNearest(hero.findByType("gate"))
if gate:
hero.attack(gate)
else:
hero.moveXY(newloc, 33)
(I pass in an x value that will move the hero into that room and start the function I wrote for that room)
My problem is the hero will just keep saying “I can’t get there”. Then my friends come to the gate and start attacking it (witch should never happen). I one point, the hero would destroy the gate and could actually get all the way to the area with the gems and warlocks. Then I decided to streamline my targeting system with less code, so that warlocks, catapults, and towers could be handled with the same code. Now the hero will just stand there and try to attack the gate even after it dies. And after it kills the catapults, it will just stand there. Below is my complete code if that would help. I have the code set up to pass in warlocks and towers, but since I can’t even get passed the first room now, I haven’t written anything to deal with it.
# Fight your way into the Inner Sanctum of the ogre chieftain, and defeat her.
summonTypes = ["griffin-rider", "griffin-rider", "archer", "archer", "soldier", "soldier"]
#Management functions
def checklocation():
if hero.pos.x < 97:
location = "Outer Courtyard"
elif hero.pos.x == 97:
location = "Outer Gate"
elif hero.pos.x > 97 and hero.pos.x < 155:
location = "Inner Courtyard"
elif hero.pos.x == 155:
location = "Inner Gate"
elif hero.pos.x > 155 and hero.pos.x < 276:
location = "Inside"
elif hero.pos.x == 276:
location = "Reststop"
elif hero.pos.x == 286:
location = "Sanctum Gate"
elif hero.pos.x > 286:
location = "Sanctum"
return location
def lowestHealthPaladin():
lowestHealth = 99999
lowestFriend = None
friends = hero.findFriends()
for friend in friends:
if friend.type != "paladin":
continue
if friend.health < lowestHealth and friend.health < friend.maxHealth:
lowestHealth = friend.health
lowestFriend = friend
if hero.health <= 30:
lowestFriend == hero
return lowestFriend
#Action functions
def clearOrges(specialUnits):
units = hero.findFriends()
for unit in units:
if unit.type == "paladin":
commandPala(unit, specialUnits)
elif unit.type == "archer":
commandArcher(unit, specialUnits)
elif unit.type == "soilder":
commandTroop(unit, specialUnits)
elif unit.type == "griffin-rider":
commandRider(unit, specialUnits)
commandSelf(specialUnits)
def summonUnits():
type = summonTypes[len(hero.built) % len(summonTypes)]
if hero.gold >= hero.costOf(type):
hero.summon(type)
def rest():
paladins = hero.findByType("paladin")
cooldown = hero.getCooldown("heal")
for paladin in paladins:
if hero.maxHealth > hero.health:
if paladin.canCast("heal"):
hero.command(paladin, "cast", "heal", hero)
else:
hero.wait(cooldown)
return True
summonUnits()
def wreckDoor(newloc):
gate = hero.findNearest(hero.findByType("gate"))
if gate:
hero.attack(gate)
else:
hero.moveXY(newloc, 33)
#commanding functions
def commandPala(paladin, specialUnits):
target = lowestHealthPaladin()
enemy = paladin.findNearestEnemy()
if paladin.canCast("heal") and target:
hero.command(paladin, "cast", "heal", target)
elif enemy and enemy.type != "gate":
hero.command(paladin, "attack", enemy)
def commandArcher(archer, specialUnits):
enemy = archer.findNearestEnemy()
if enemy and enemy.type != "gate":
hero.command(archer, "attack", enemy)
def commandTroop(troop, specailUnits):
enemy = troop.findNearestEnemy()
if enemy and enemy.type != "gate":
hero.command(troop, "attack", enemy)
def commandRider(rider, specialUnits):
enemy = rider.findNearestEnemy()
if enemy and enemy.type != "gate":
hero.command(rider, "attack", enemy)
def commandSelf(specialUnits):
enemies = hero.findEnemies()
if specialUnits:
for specialUnit in specialUnits:
if specialUnit.type == "catapult":
pult = hero.findNearest(specialUnits)
hero.attack(pult)
else:
for enemy in enemies:
if enemy.type != "gate":
enemy = hero.findNearest(enemies)
hero.attack(enemy)
#area functions
def outerCourtyard():
enemy = hero.findNearestEnemy()
summonUnits();
catapults = hero.findByType("catapult")
if enemy and enemy.type != "gate":
clearOrges(catapults)
else:
rested = rest()
if rested == True:
hero.moveXY(97, 33)
def innerCourtyard():
enemy = hero.findNearestEnemy()
enemies = hero.findEnemies()
towers = hero.findByType("tower")
summonUnits();
if enemy and enemy.type != "gate":
clearOrges(towers)
else:
rested = rest()
if rested == True:
hero.moveXY(155, 33)
def inside():
enemy = hero.findNearestEnemy()
summonUnits();
if enemy and enemy.type != "gate":
clearOrges()
else:
rested = rest()
if rested == True:
hero.moveXY(276, 33)
def reststop():
enemy = hero.findNearestEnemy()
warlocks = hero.findByType("warlock")
summonUnits();
if enemy and enemy.type != "gate":
clearOrges(warlocks)
else:
rested = rest()
if rested == True:
#hero.moveXY(155, 33)
pass
#code
while True:
location = checklocation()
if location == "Outer Courtyard":
outerCourtyard()
elif location == "Outer Gate":
wreckDoor(100)
elif location == "Inner Courtyard":
innerCourtyard()
elif location == "Inner Gate":
wreckDoor(200)
elif location == "Inside":
inside()
elif location == "Reststop":
reststop()