The sad thing is I’ve only coded the first part, but anytime I change a parameter and hit “run” the tab tends to crash almost immediately. I’ll be the first to admit this is not the most parsimonious code the world has ever seen, but I’m beginning to get really frustrated. Is there anything I could do to make the code less likely to crash? (Also, while I’m asking, I guess I really should ask if there is a way to calculate where a catapult missle is likely to splash down. I can find the missle position (x,y,z) and velocity [ missle[0].velocity.x etc.]. )
Thanks in advance!
def palHeal(pal, troopArray, healthF, selfHealB):
if pal.canCast("heal"):
if (selfHealB and (self.health < self.maxHealth*healthF)):
if self.distanceTo(pal) < 30:
self.command(pal, "cast", "heal", self)
else:
self.command(pal, "move", self.pos)
else:
tarA = None
for tr in troops:
if ((tr.health/tr.maxHealth < healthF) and (tr.health > 5) and (pal.distanceTo(tr) < 30)):
tarA = tr
healthF = tr.health / tr.maxHealth
if tarA:
self.command(pal, "cast", "heal", tarA)
else:
if self.distanceTo(pal) > 30:
self.command(pal, "move", self.pos)
def archerTarget(arch, range):
enemies = arch.findEnemies()
if len(enemies) < 1:
answer = None
elif len(enemies) < 2:
answer = enemies[0]
else:
answer = arch.findNearest(enemies)
dToEnemies = arch.distanceTo(answer)
for enemy in enemies:
if arch.distanceTo(enemy) < range:
if ((enemy.maxHealth > answer.maxHealth) and (enemy.health > 0)):
answer = enemy
dToEnemies = arch.distanceTo(answer)
elif ((enemy.maxHealth >= answer.maxHealth) and (enemy.health > 0) and arch.distanceTo(enemy) < dToEnemies):
answer = enemy
dToEnemies = arch.distanceTo(enemy)
return answer
def chainLightning():
enemies = self.findEnemies()
if len(enemies) > 4:
tarA = self.findNearest(enemies)
if self.distanceTo(tarA) < 30 and tarA.health > 95 and self.canCast("chain-lightning", tarA):
self.cast("chain-lightning", tarA)
#Starting main code
while self.now() < 1:
self.move(Vector(61,42))
UpFirst = True
stepCounter = 0
maxStep = 5
loop:
chainLightning()
catapult = self.findByType("catapult", self.findEnemies())
#Soldiers' orders
troops = self.findByType("soldier", self.findFriends())
if len(troops) > 0:
for t in troops:
if len(catapult) > 0:
if UpFirst:
posA = Vector.add(t.pos, Vector(2, 3))
UpFirst = False
else:
posA = Vector.add(t.pos, Vector(2,-3))
UpFirst = True
if self.isPathClear(t.pos, posA):
if len(catapult) > 0:
self.command(t, "move", posA)
self.command(t, "defend", posA)
else:
if len(self.findEnemies()) > 0:
self.command(t, "attack", t.findNearest(self.fineEnemies()))
#Archers' orders
troops = self.findByType("archer", self.findFriends())
if len(troops) > 0:
for archer in troops:
tarA = archerTarget(archer, 15)
if archer.distanceTo(tarA) < 20 and tarA.type != "door":
if len(catapult) > 0:
if UpFirst:
posA = Vector.add(archer.pos, Vector(2,4))
UpFirst = False
else:
posA = Vector.add(archer.pos, Vector(2,-4))
UpFirst = True
self.command(archer, "move", posA)
self.command(archer, "attack", tarA)
else:
if UpFirst and len(catapult)>0:
UpFirst = False
posA = Vector.add(archer.pos, Vector(1,3))
if self.isPathClear(archer.pos, posA) and len(catapult)>0:
self.command(archer, "defend", posA)
elif len(catapult)>0:
UpFirst = True
posA = Vector.add(archer.pos, Vector(1,-3))
if self.isPathClear(archer.pos, posA) and len(catapult)>0:
self.command(archer, "defend", posA)
else:
if tarA.type != "door":
self.command(archer, "attack", tarA)
#Paladins' orders
troops = self.findByType("paladin", self.findFriends())
if len(troops) > 0:
for pal in troops:
if len(catapult) > 0:
if UpFirst:
UpFirst = False
self.command(pal, "move", Vector.add(pal.pos, Vector(-1, 3)))
else:
UpFirst = True
self.command(pal, "move", Vector.add(pal.pos, Vector(-1, -3)))
palHeal(pal, self.findFriends(), 0.6, True)
#Attack on killable targets in range
if len(self.findEnemies()) > 0:
tarA = self.findNearest(self.findEnemies())
while ((len(self.findEnemies()) > 0) and (self.distanceTo(tarA) < self.attackRange) and (tarA.health < self.attackDamage * 2)):
if self.isReady("bash"):
self.bash(tarA)
else:
self.attack(tarA)
tarA = self.findNearest(self.findEnemies())
#Moving a soldier and character towards catapults
catapult = self.findByType("catapult", self.findEnemies())
if len(catapult)==2:
if catapult[0]:
allTroops = self.findByType("soldier", self.findFriends())
if len(allTroops) > 0:
troop = catapult[0].findNearest(allTroops)
self.command(troop, "attack", catapult[0])
if catapult[1]:
allTroops = self.findByType("soldier", self.findFriends())
if len(allTroops) > 0:
troop = catapult[1].findNearest(allTroops)
self.command(troop, "attack", catapult[1])
stepCounter = stepCounter + 1
tarA = self.findNearest(catapult)
if tarA:
if ((self.distanceTo(tarA) < self.attackRange + 4) or (self.pos.x > 80)):
self.attack(tarA)
elif UpFirst:
if catapult[0]:
tarA = catapult[0].pos
else:
tarA = None
if tarA:
self.move(tarA)
if stepCounter == maxStep:
UpFirst = False
stepCounter = 0
else:
if catapult[1]:
self.move(catapult[1].pos)
if stepCounter == maxStep:
UpFirst = True
stepCounter = 0
elif len(catapult)==1:
if (catapult[0] and (len(self.findByType("soldier", self.findFriends())) > 0)):
troop = catapult[0].findNearest(self.findByType("soldier", self.findFriends()))
self.command(troop, "attack", catapult[0])
stepCounter = stepCounter + 1
if stepCounter == maxStep:
stepCounter = 0
upFirst = not(upFirst)
if catapult[0]:
self.attack(catapult[0])
else:
enemies = self.findEnemies()
if len(enemies) > 0:
if self.findNearest(enemies).type != "door":
self.attack(self.findNearest(enemies))
enemies = self.findEnemies()
if ((len(enemies) < 1) or (len(enemies) == 1 and enemies[0].type == "door")):
break
allTroops = self.findFriends()
if len(allTroops) > 0:
for troop in allTroops:
self.command(troop, "move", Vector(75,34))
self.moveXY(88, 33)
pal = self.findByType("paladin")
stepCounter = 0
while stepCounter < 6 and self.now()<30:
stepCounter = stepCounter + 1
for p in pal:
palHeal(p, self.findFriends(), 0.5, True)
self.move(Vector.add(self.pos, Vector(2, 0)))
self.move(Vector.add(self.pos, Vector(-1,0)))