[SOLVED] Library Tacticion, Cloudrip Mountain, CodeCombat!

Archer keeps dying! Please Help

hero.jumpTo({x: 39, y: 38});
 while(true) {
    var enemy = hero.findNearestEnemy();
     hero.attack(enemy);
 }
// Hushbaum has been ambushed by ogres!
// She is busy healing her soldiers, you should command them to fight!
// The ogres will send more troops if they think they can get to Hushbaum or your archers, so keep them inside the circle!
// Soldiers spread out in a circle and defend.
function commandSoldier(soldier, soldierIndex, numSoldiers) {
    var angle = Math.PI * 2 * soldierIndex / numSoldiers;
    var defendPos = {x: 41, y: 40};
    defendPos.x += 10 * Math.cos(angle);
    defendPos.y += 10 * Math.sin(angle);
    hero.command(soldier, "defend", defendPos);
}

// Find the strongest target (most health)
// This function returns something! When you call the function, you will get some value back.
function findStrongestTarget() {
    var mostHealth = 0;
    var bestTarget = null;
    var enemies = hero.findEnemies();
    // Figure out which enemy has the most health, and set bestTarget to be that enemy.
    for(var i =0;i < enemies.lenght; ++i)
    {
        if(mostHealth < enemies[i].health)
        {
            mostHealth = enemies[i].health;
            bestTarget  = enemies[i];
        }
    }    
    // Only focus archers' fire if there is a big ogre.
    if (bestTarget && bestTarget.health > 15) {
        return bestTarget;
    } else {
        return null;
    }
}


// If the strongestTarget has more than 15 health, attack that target. Otherwise, attack the nearest target.
function commandArcher(archer) {
    var nearest = archer.findNearestEnemy();
    if(archerTarget) {
        hero.command(archer, "attack", archerTarget);
    } else if(nearest) {
        hero.command(archer, "attack", nearest);
    }
}

var archerTarget = null;
while(true) {
    // If archerTarget is defeated or doesn't exist, find a new one.
    if(!archerTarget || archerTarget.health <= 0) {
        // Set archerTarget to be the target that is returned by findStrongestTarget()
        archerTarget = findStrongestTarget();
    }
    var friends = hero.findFriends();
    var soldiers = hero.findByType("soldier");
    // Create a variable containing your archers.
    var archers = hero.findByType("archers");
    for(var i=0; i < soldiers.length; i++) {
        var soldier = soldiers[i];
        commandSoldier(soldier, i, soldiers.length);
    }
    // use commandArcher() to command your archers
    for(var i=0; i < archers.length; i++) {
       
        commandArcher(archers[i],archerTarget);
    }    
}

I want to help you, but i dont do java, sry

ok do u know anyone who does javascript

 hero.jumpTo({x: 39, y: 38});
 while(true) {
    var enemy = hero.findNearestEnemy();
     hero.attack(enemy);
 }
// Hushbaum has been ambushed by ogres!
// She is busy healing her soldiers, you should command them to fight!
// The ogres will send more troops if they think they can get to Hushbaum or your archers, so keep them inside the circle!
// Soldiers spread out in a circle and defend.
function commandSoldier(soldier, soldierIndex, numSoldiers) {
    var angle = Math.PI * 2 * soldierIndex / numSoldiers;
    var defendPos = {x: 41, y: 40};
    defendPos.x += 10 * Math.cos(angle);
    defendPos.y += 10 * Math.sin(angle);
    hero.command(soldier, "defend", defendPos);
}

// Find the strongest target (most health)
// This function returns something! When you call the function, you will get some value back.
function findStrongestTarget() {
    var mostHealth = 0;
    var bestTarget = null;
    var enemies = hero.findEnemies();
    // Figure out which enemy has the most health, and set bestTarget to be that enemy.
    for(var i =0;i < enemies.lenght; ++i)
    {
        if(mostHealth < enemies[i].health)
        {
            mostHealth = enemies[i].health;
            bestTarget  = enemies[i];
        }
    }    
    // Only focus archers' fire if there is a big ogre.
    if (bestTarget && bestTarget.health > 15) {
        return bestTarget;
    } else {
        return null;
    }
}


// If the strongestTarget has more than 15 health, attack that target. Otherwise, attack the nearest target.
function commandArcher(archer) {
    var nearest = archer.findNearestEnemy();
    if(archerTarget) {
        hero.command(archer, "attack", archerTarget);
    } else if(nearest) {
        hero.command(archer, "attack", nearest);
    }
}

var archerTarget = null;
while(true) {
    // If archerTarget is defeated or doesn't exist, find a new one.
    if(!archerTarget || archerTarget.health <= 0) {
        // Set archerTarget to be the target that is returned by findStrongestTarget()
        archerTarget = findStrongestTarget();
    }
    var friends = hero.findFriends();
    var soldiers = hero.findByType("soldier");
    // Create a variable containing your archers.
    var archers = hero.findByType("archers");
    for(var i=0; i < soldiers.length; i++) {
        var soldier = soldiers[i];
        commandSoldier(soldier, i, soldiers.length);
    }
    // use commandArcher() to command your archers
    for(var i=0; i < archers.length; i++) {
       
        commandArcher(archers[i],archerTarget);
    }    
}

In your commandSoldier function, try making the last line (hero.command) an if/else statement, where you are testing soldier.health…something like:

if soldier health is greater than (you decide which)
   then defendPos
if not then
   defend the center of the circle

When a soldiers health gets dangerously low, this will remove them from direct combat until Hushbaum can heal them, which once healed, they will then return to their position in the circle and continue attacking.

The code you placed before the comments is pointless…it will not work. By design, the hero cannot attack, only command troops.

ok thank you dedreous

Cuz i pased the level

1 Like

Good to hear! Don’t forget to mark the level as solved…this lets folks know that help is no longer needed.

@Jeremy_Yu please do what @dedreous said in the previous reply.

@GraySkayl007 please do not revive dead topics unless you have a similar issue as the person thay created the topic had. @Jeremy_Yu had not been active for a while, so there is a great chance that he will not see your message.

Andrei

I’m sorry, i didn’t notice that.

1 Like

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