Small problem in "The Two Flowers"


#1

Hello friends!

I believe I did the code as described in the level comments but the soldiers are acting a bit wonky. Sometimes they just stand there taking a beating while their friends are doing perfectly as commanded.

I tried calling the command function more often than the coin-picking and summoning, and I imagine that reduced the problem a little but I can’t tell for sure.

Being a newbie I can’t tell what else to change. I suppose it could be the command function, using those for-loops are still making me a bit confused. Anyway, here is the code:

Code
def summonSoldiers():
    [...]

def commandSoldiers():
    [...]

def pickUpNearestCoin():
    [...]    

loop:
    soldiers = self.findByType("soldier")
    summonSoldiers()
    commandSoldiers()
    pickUpNearestCoin()

#2

Let’s run through your loop:

soldiers = self.findByType("soldier") #Hm…no soldiers…returns 0
summonSoldiers() #Do I have enough gold to make a soldier? Yes! *summons Agnes*
commandSoldiers() #Hm…using soldiers from earlier…no soldiers!
pickUpNearestCoin() #Picks up nearest coin

Your soldiers variable is defined too early. Try putting it inside the commandSoldiers function.


#3

That makes so much sense. Almost makes me feel silly. Thank you sir!


#4

def radialToCartesian(radius, degrees):
    radian   = 180 / Math.PI
    radianOfCharacter = degrees / radian 
    xOffset = radius * Math.cos(radianOfCharacter)
    yOffset = radius * Math.sin(radianOfCharacter)
    Pos = [0 , 0] 
    Pos[0] = xOffset
    Pos[1] = yOffset
    return Pos


# findSoldierOffset figures out the position a soldier should stand at in relation to the peasant.
def findSoldierOffset(soldiers, i):
    # The first argument 'soldiers' should be an array of your soldiers.
    soldier = soldiers[i]
    angle = i * (360 / len(soldiers))
    # The second argument 'i' is the index of the soldier (in soldiers) you want to find the position for.
    return angle 
#    return radialToCartesian(5, angle)

# Use findByType to get an array of your soldiers.
def moveSoldiers( ):
	soldiers = self.findFriends() 
	peasant = self.findByType("peasant")[0]
    # Use a for-loop to iterate over range(len(soldiers)).
	for i in range(len(soldiers)):
		# soldier = soldiers[i]; 
		angle = findSoldierOffset(soldiers , i );#self.say( angle ) 
		Pos = radialToCartesian( 5, angle );
		PosX_ToMove = Pos[0] + peasant.pos['x']
		PosY_ToMove = Pos[1] + peasant.pos['y']
		PosToMove = {'x':PosX_ToMove , 'y':PosY_ToMove}
          
		if soldiers[i].type == "soldier" :
			soldier = soldiers[i]
			self.command( soldier, "move", PosToMove)
	return 0 
 
def summonSoldiers():
    if self.gold >= self.costOf("soldier"):
        self.summon("soldier")

def pickUpNearestCoin():
    peasant = self.findByType("peasant")[0]
    enemy = peasant.findNearest( self.findEnemies() )
    
    if enemy:
        if self.isReady("cleave"):
            self.cleave(enemy)
    coins = self.findItems()
    coin = peasant.findNearest( coins )
    self.moveXY( coin.pos.x , coin.pos.y)
    return 0



loop:
	pickUpNearestCoin()
	summonSoldiers()
	moveSoldiers( )
		

It sometimes works well, but sometimes doesn`t.

Because of a lot of enemies, I used code which help for solving ring-bearer
and cleave skill to kill many enemies at one time.