Librarian Tactician - fun with Vectors (solved)


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. :frowning:

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.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)
                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.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
                    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.costOf("soldier"):
soldiersArray = self.findByType("soldier",self.findFriends())


Ok - color me confused. I went back to the flower drawing level and the following function will construction a figure with CountM points size away from fx,fy … or roughly that. So I’m confused as to why it didn’t work above.

def FigureAroundPoint(fx, fy, CountM, size):
    count = 0
    while count < CountM:
        angle = Math.PI * 2 * count/CountM
        defendPos = Vector.add(Vector(fx,fy), Vector.multiply(Vector.normalize(Vector.rotate(Vector(1,0),angle)),size))   #calculating where to guard
        self.moveXY(defendPos.x, defendPos.y)
        count = count + 1

This can be closed now. I finally found where I slightly misspelled a variable name, so it didn’t get initialized properly. !#@ and !#@
“healtyIndex = 0”