Kelvintaph Burgler: Trying to run away from Chieftain [Solved]

In the Kelvintaph Burgler level I want my archers to fire at the chieftain but from a distance so he can’t attack them. I’m trying to use vectors (unsuccessfully) My archers (I’m only trying to implement it for one of them at the moment) keep running directly into the chieftain and get killed.

Can anyone give me some idea what I’m doing wrong please I’m not great with Vectors at the moment.

The bit of code relating to the archer movement is here and the full code is below it.

        if archers[0]:
            targetPos=chieftains[0].pos
            #hero.say(archers[0]+ ' '+archers[0].pos)
            distance=archers[0].distanceTo(targetPos)
            #hero.say(archers[0]+ ' distance '+distance)
            if distance<15:
                #hero.say(archers[0]+ ' distance '+distance)
                chfVector=Vector.subtract(archers[0].pos,targetPos)
                chfVector1=Vector.normalize(chfVector)
                chfVector15=Vector.multiply(chfVector1,15)
                goal=Vector.add(chfVector15,targetPos)
                moveToPos=Vector.add(archers[0].pos,goal)
                hero.command(archers[0],'move',moveToPos)
                #hero.command(archers[0],'attack',chieftains[0])
        if archers[1]:
            #hero.command(archers[1],'attack',chieftains[0])
            pass
        if soldiers[0]:
            hero.command(soldiers[0],'attack',chieftains[0])
        if soldiers[1]:
            hero.command(soldiers[1],'attack',chieftains[0])
        commandPalatt()

The full code is here:


# The ice gate will open when both ogres are defeated.
def commandPalatt():
    armyExit=Vector( 78,40)
    paladin=hero.findByType('paladin',hero.findFriends())
    witch=hero.findByType('witch',hero.findEnemies())
    chieftain=hero.findByType('chieftain',hero.findEnemies())
    #if paladin and  paladin.canCast("heal", paladin) and paladin.health<paladin.maxHealth*0.8:
    if paladin[0] and  paladin[0].health<paladin[0].maxHealth*0.5:
        hero.command(paladin[0],'cast','heal',paladin[0])
    elif witch[0] and paladin[0]:
        hero.command(paladin[0], "attack", witch[0])
    elif paladin[0] and chieftain[0]:
        hero.command(paladin[0], "attack", chieftain[0])
    elif paladin[0]:
        hero.command(paladin[0], "move", armyExit)

def commandRest():
    #hero.say('in command rest')
    armyExit=Vector( 78,40)
    chieftain=hero.findByType('chieftain',hero.findEnemies())
    witch=hero.findByType('witch',hero.findEnemies())
    friends=hero.findFriends()
    for friend in friends:
        if friend and chieftain[0] and friend.type!='paladin':
            #hero.say('in command rest C')
            #hero.say(friend+' attack chieftain')
            hero.command(friend, "attack", chieftain[0])
        elif friend and witch[0] and friend.type!='paladin':
            hero.say('in command rest W')
            hero.say( friend + ' attack witch')
            hero.command(friend, "attack", witch[0])
        elif friend:
            hero.command(friend, "move", armyExit)

    
def moveArmy(pos):
    army=hero.findFriends()
    for arm in army:
        hero.command(arm, "move", pos)

enemies=hero.findEnemies()
robots=[]
for enemy in enemies:
    #hero.say(enemies[7].type)
    #hero.say(enemy.type[0:5])
    if enemy.type[0:5]=='robot':
        robots.append(enemy)
        
midX=(robots[0].pos.x+robots[1].pos.x)/2  
midY=(robots[0].pos.y+robots[1].pos.y)/2  
#hero.say('x: '+midX +" y: "+midY)

midPoint=Vector((midX+2),midY)
#while hero.pos.x<midPoint.x-2:
#    hero.move(midPoint)
    


iceGatePos=Vector(61,15)
#while hero.pos!=iceGatePos:
#    hero.move(iceGatePos)
armyExit=Vector( 78,40) 

    
counter=0
#hero.say('before enemyMissile'+ ' '+counter)  
heroExitPos=Vector(78,14)
enemyMissile =  hero.findNearest(hero.findEnemyMissiles())

#if enemyMissile:
#    hero.say(enemyMissile+ ' '+counter)
while True: 
#Army stuff
    witches=hero.findByType('witch',hero.findEnemies())
    chieftains=hero.findByType('chieftain',hero.findEnemies())
    paladins=hero.findByType('paladin', hero.findFriends())
    soldiers=hero.findByType('soldier', hero.findFriends())
    archers=hero.findByType('archer', hero.findFriends())
    if paladins[0]:
        paladin=paladins[0]
    if soldiers[0] and paladins[0] and soldiers[0].pos.x<25:
        #hero.say('section 1')
        stage1=True
        pos=Vector(27,41)
        moveArmy(pos)
    elif soldiers[0] and paladins[0] and soldiers[0].pos.x<43 and stage1==True:
        #hero.say('section 3')
        stage2=True
        pos=Vector(45,49)
        
        moveArmy(pos)
    elif archers[0] and paladins[0] and archers[0].pos.x>28 and stage2==True   :
        #hero.say('section 4')
        stage3=True
        targetPos=chieftains[0].pos
        
        
        if archers[0]:
            targetPos=chieftains[0].pos
            #hero.say(archers[0]+ ' '+archers[0].pos)
            distance=archers[0].distanceTo(targetPos)
            #hero.say(archers[0]+ ' distance '+distance)
            if distance<15:
                #hero.say(archers[0]+ ' distance '+distance)
                chfVector=Vector.subtract(archers[0].pos,targetPos)
                chfVector1=Vector.normalize(chfVector)
                chfVector15=Vector.multiply(chfVector1,15)
                goal=Vector.add(chfVector15,targetPos)
                moveToPos=Vector.add(archers[0].pos,goal)
                hero.command(archers[0],'move',moveToPos)
                #hero.command(archers[0],'attack',chieftains[0])
        if archers[1]:
            #hero.command(archers[1],'attack',chieftains[0])
            pass
        if soldiers[0]:
            hero.command(soldiers[0],'attack',chieftains[0])
        if soldiers[1]:
            hero.command(soldiers[1],'attack',chieftains[0])
        commandPalatt()



#hero stuff
    enemyMissile =  hero.findNearest(hero.findEnemyMissiles())
    if not chieftains[0] and not witches[0]:
        hero.move(heroExitPos)
    elif enemyMissile:
        counter+=1
        iceGateVectorFromHero=Vector.subtract(iceGatePos,hero.pos)
        iceGateVectorFromHero=Vector.normalize(iceGateVectorFromHero)
        iceGateVectorFromHero=Vector.multiply(iceGateVectorFromHero,5)
        enemyMissile =  hero.findNearest(hero.findEnemyMissiles())
        if enemyMissile:
            #hero.say(enemyMissile+ ' '+counter)
            distToEM=hero.distanceTo(enemyMissile)
            #hero.say(distToEM)
            if distToEM<99:
                missileAttackVector=Vector.subtract(hero.pos,enemyMissile.pos)
                missileAttackVector1=Vector.normalize(missileAttackVector)
                missileAttackVector5=Vector.multiply(missileAttackVector1,5)
                #hero.say(missileAttackVector)
                goal=Vector.add(missileAttackVector5,iceGateVectorFromHero)
                pass
            if enemyMissile:
                moveToPos=Vector.add(hero.pos,goal)
                hero.move(moveToPos)
    commandPalatt()

One question, how far are you in code combat? If you are past the desert, i can’t help you.

This is in Kelvintaph Glacier…

1 Like

how did you know? 20

Cause i’ve did the level. And the level has KELVINTAPH in the name.

2 Likes

Hi,

Yes it is in Kelvintaph Glacier.

I’m finding the Vector stuff a bit tricky.

I think this bit is the bit which is wrong. If you make a vector between the archer and the chieftain, then add that vector to the archer’s position, moveToPos will be the chieftain’s exact position. Essentially you worked out the difference between your archer and the chieftain in a vector, but then you just added the difference to the archer’s position, so you made its target the chieftain’s position. Instead you want to subtract the difference from the archer’s position, so the archer’s target will be directly opposite the chieftain’s position.
Danny

2 Likes

Thanks very much Danny. I have managed to mess up the rest of my code (the hero bit) but thanks for the explanation I’ll give this a go.

Yay it worked the archer ran away (into the Yak so he can’t really fire any arrows, however it is definitely a start . Thanks v much!

1 Like

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.