Ok - I got a little ambitious on this level and wanted only the healthy soldiers to stand in the outside circle. I was getting the error message that there is possibly a bug with Python advanced feature parsing - the code below fixed that now. The soldiers are NOT doing what I expected. (Staying in a circle around the protectee.) I deleted out the archer code to try and isolate the problem, but no joy to date. I have even cut out the part where I was trying to add additional soldiers to the SoldiersArray below. Suggestions?
I was pretty sure the problem is here, but I was wrong.
defendPos = Vector.add(Vector(41,40), Vector.multiply(Vector.normalize(Vector.rotate(Vector(1,0),angle)),10))
I’ve played with the Vector.multiple parameter, so that’s not the issue.
def distanceBetween(pos1, pos2):
result = ((pos1.x-pos2.x)^2 + (pos1.y-pos2.y)^2 )^0.5
return result
# Healthy soldiers spread out in a circle and defend.
def commandSoldier():
countS = 0
# begin cycling through troops, identifying the number healthy and getting the sick to safety where they can wait to be healed. Note: soldiersArray is a global variable.
for troop in soldiersArray:
if troop:
if (troop.health < troop.maxHealth * 0.5): #for those that aren't doing so well
if distanceBetween(troop.pos, Vector(40,40)) > 3: #check they aren't already within 3 of (40,40)
tar = Vector.add(troop.pos, Vector.multiply(Vector.normalize(Vector.subtract(Vector(40,40),troop.pos)),2)) #Calculate a single step towards (40,40)
self.command(troop, "move", tar)
else:
countS = countS + 1 #This captures the # of healthy soldiers.
soldierIndex = 0
healtyIndex = 0
while soldierIndex < len(soldiersArray) and countS > 0:
troop = soldiersArray[soldierIndex]
if troop:
if (troop.health >= troop.maxHealth * 0.5): #if it is healthy
angle = 3.14159 * 2 * healthyIndex / countS
defendPos = Vector.add(Vector(41,40), Vector.multiply(Vector.normalize(Vector.rotate(Vector(1,0),angle)),20)) #calculating where to guard
if distanceBetween(troop.pos, defendPos) >2: #if not close to post
self.command(troop, "move", defendPos) # move to post
else:
self.command(troop, "defend", defendPos) #defend the post
healthyIndex = healthyIndex + 1 #this one was healthy, so increment
soldierIndex = soldierIndex + 1 #go on to next soldier
# Beginning of program
if self.gold > self.costOf("soldier"):
self.summon("soldier")
soldiersArray = self.findByType("soldier",self.findFriends())
loop:
commandSoldier()