Don’t say I didn’t warn you…
I’m working on refactoring. I’ll probably adopt @Hellenar’s idea of breaking the stages into functions. I’m very open to additional suggestions for refactoring from anyone energetic enough to wade through this.
(@Chaboi_3000, you’ll see I tried using flags, too. That usually didn’t work, though there was one time where I was running on the small screen (not submitting), and a flag I had placed the last time I had submitted came on as my hero was attacking the towers, and that time, it actually went in to stage 3, causing her to stop attacking the towers and getting zapped up.)
A Whole Lot of Code
summonTypes = ['soldier', 'soldier', 'archer', 'archer']
tactic = 'defend'
stage = 1
def summonTroops():
type = summonTypes[len(hero.built) % len(summonTypes)]
if hero.gold > hero.costOf(type):
hero.summon(type)
def lowestHealthPaladin():
lowestHealth = 99999
lowestFriend = None
friends = hero.findFriends()
for friend in friends:
if friend.health < lowestHealth and friend.health < friend.maxHealth:
lowestHealth = friend.health
lowestFriend = friend
return lowestFriend
def commandPaladin(paladin):
if (paladin.canCast("heal")):
if (hero.health < hero.maxHealth * 0.8):
target = self
else:
target = lowestHealthPaladin()
if target:
hero.command(paladin, "cast", "heal", target)
elif (paladin.health < 100):
hero.command(paladin, "shield")
else:
target = hero.findNearestEnemy()
if warlock:
target = warlock
if target:
hero.command(paladin, "attack", target)
def SoldierAttack(soldier):
target = hero.findNearestEnemy()
if warlock:
target = warlock
if target:
if stage != 2 or soldier.type != 'archer':
hero.command(soldier, "attack", target)
else:
if target.type == 'tower':
if target.pos.y < 35:
hero.command(soldier, "move", {'x': 110, 'y': 28})
hero.command(soldier, "attack", target)
else:
hero.command(soldier, "move", {'x': 110, 'y': 37})
hero.command(soldier, "attack", target)
if stage == 1:
if soldier.pos.x > 81:
hero.command(soldier, "defend", {'x': 70, 'y': 34})
def SoldierDefend(soldier):
if stage == 1:
hero.command(soldier, "defend", {'x': 1, 'y': 40})
elif stage == 2:
hero.command(soldier, "defend", {'x': 92, 'y': 34})
elif stage == 3:
hero.command(soldier, "defend", {"x": 277, "y": 34})
elif stage == 4:
hero.command(soldier, "defend", {'x': 278, 'y': 34})
else:
hero.command(soldier, "defend", hero)
def commandSoldiers():
soldiers = hero.findFriends()
for soldier in soldiers:
if soldier.type == "paladin":
commandPaladin(soldier)
elif tactic == 'attack':
SoldierAttack(soldier)
elif tactic == 'defend':
SoldierDefend(soldier)
def moveTo(position):
if hero.isReady("jump"):
hero.jumpTo(position)
else:
hero.move(position)
def attack(target):
if target:
if hero.distanceTo(target) > 45:
moveTo(target.pos)
else:
hero.attack(target)
def announceStage():
hero.say("STAGE " + stage + "!!!")
def pickUpNearestItem():
nearestItem = hero.findNearest(hero.findItems())
if nearestItem:
moveTo(nearestItem.pos)
commandSoldiers()
hero.moveXY(31, 56)
while True:
greenFlag = hero.findFlag("green")
if stage == 1:
catapults = hero.findByType('catapult')
catapult = catapults[0]
catapult1 = catapults[1]
warlock = hero.findNearest(hero.findByType('warlock'))
target = hero.findNearestEnemy()
nearestItem = hero.findNearestItem()
if catapult or catapult1:
attack(hero.findNearest(catapults))
elif stage == 1:
global tactic
tactic = 'attack'
commandSoldiers()
if target:
hero.attack(target)
if target.type == 'tower':
global stage
stage = 2
global tactic
tactic = 'defend'
commandSoldiers()
elif stage == 2:
bTowers = hero.findByType("tower")
bTower = bTowers[0]
bTower1 = bTowers[1]
if bTower or bTower1:
if hero.pos.x < 108:
hero.moveXY(129, 34)
target = hero.findNearestEnemy()
if hero.canCast("chain-lightning", target):
hero.cast("chain-lightning", target)
hero.attack(target)
hero.attack(target)
hero.attack(target)
global tactic
tactic = 'attack'
commandSoldiers()
if greenFlag:
global stage
stage = 3
announceStage()
commandSoldiers()
hero.pickUpFlag(greenFlag)
else:
global stage
stage = 3
announceStage()
commandSoldiers()
hero.moveXY(270, 34)
elif stage == 3:
global tactic
tactic = 'defend'
commandSoldiers()
hero.moveXY(270, 34)
if hero.pos.x >= 267 and hero.pos.x < 290:
pet.moveXY(277, 65)
pet.moveXY(261, 60)
pet.moveXY(263, 56)
pet.moveXY(276, 54)
pet.moveXY(275, 13)
pet.moveXY(262, 9)
pet.moveXY(269, 3)
pet.moveXY(277, 33)
pickUpNearestItem()