Kelvintaph Crusader help

I’ve been trying to get my allies to attack the witch first, then the nearest enemy, but no matter what I try, They kill the witch, then stay still while the rest of the ogres clobber them. Here’s my code:

for friend in self.findFriends():
    witch = friend.findNearest(self.findByType("witch"))
    if witch:
        self.command(friend, "attack", witch)
    else:
        target = friend.findNearestEnemy()
        if target:
            self.command(friend, "attack", target)

Can you show me what I’m doing wrong?

Maybe you are using moveXY in your hero’s escape? I don’t see anything wrong with your code…

No, that’s not it. I commented out the hero part and they still failed to attack.

Maybe you didn’t put the for loop inside the “main” loop? (Happens to me once in a while.)

Here, I’ll post my whole code. That might make a difference.

loop:    
    for friend in self.findFriends():
        witch = friend.findNearest(self.findByType("witch"))
        if witch:
            self.command(friend, "attack", witch)
        else:
            target = friend.findNearestEnemy()
            if target:
                self.command(friend, "attack", target)
    
        
                
    while self.pos.x < 69:
        self.move({'x':69, 'y':15})
    self.say("Hi!")
    while self.pos.x > 37:
        self.move({'x':37, 'y':16})
    self.say("Bye!")
    for i in range(3):
        enemy = self.findNearest(self.findByType("catapult"))
        if enemy:
            self.attack(enemy)
            
    self.say("Hi!")
    while self.pos.x > 37:
        self.move({'x':37, 'y':16})
    while self.pos.x < 78:
        self.move({'x':78, 'y':14})

(tl;dr you are hurting yourself by using the while loops to move your hero into place, as well as taunting the ogres)

Order of execution:

  1. Command friends to attack
  2. Move until in position.
  3. Say “Hi!”
  4. Move until in position.
  5. Say “Bye!”
  6. Attack catapults from i=0 to 2
  7. Say “Hi!”
  8. Move until in position.
  9. Move until in position.
  10. Repeat the loop.

I have to taunt the ogres. I don’t have the Gilt Wristwatch, and so can’t use wait(). But I get your point. Thanks!

Even if you had “wait” it would still stop your loop, and so you would not be commanding your troops…

It was already mentioned, but to be clear, “while not in position: move()” is “exactly” what moveXY() does…

Umm, are you sure you shouldn’t be using:
friend.findNearest(friend.findEnemies())

Try replacing the third line with this:

    if witch and witch.health>0:

It might be an issue with this level in particular

@dwhittaker: friend.findEnemies() doesn’t work. I’ve checked.

trotod is right. A helpful rule of thumb is to make sure that you only take one action each run through your loop so that you are always responsive. A second rule (which you are following) is to always tell your minions what to do first thing, so that they act on the most recent information.

1 Like

Can anyone tell me what is wrong with my code?
(Some parts)

loop {
    //Witch
    var witch = this.findByType("Witch");

    //Friends
    var friends = this.findFriends();
    for(var i = 0; i < friends.length; ++i); {
        var friend = friends[i];
        //Kill witch
    if (witch) {
        if (friend) {
            this.command(friend, "attack", witch);
        }    
    }
}

This is only part of it.

var witch = this.findByType("Witch");

should be

var witch = this.findByType("witch")[0];

findByType returns an array, and the types are all lowercase-with-dashes.