Libary tactian (java script)

my code

/// 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.length; i++) {
        if (enemies[i].health > mostHealth) {
            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 friends = hero.findFriends();
    for(var i=0; i < friends.length; i++) {
        if(friend.type == "archer"){
            var archerfindNearestEnemy = friend.distanceTo(enemy) < 15;
        }
    
        var nearest = archerfindNearestEnemy;
        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 doesnt 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 q=0; q < archers.length; q++){
        var archer = archers[q];
        commandArcher(archer, i, archer.length);
}
    
}
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);
    if(soldier.health > 60) {
    hero.command(soldier, "defend", defendPos);
   }else {
    hero.command(soldier, "defend", {'x': 40, 'y': 42})
   }
}

Can you please properly indent your code? It’s really hard to read with how you indented it…

1 Like

archer die only one archer

i use this gear

there are few monster left

command the soldiers to defend archers in a even amount
(ex: if there are 5 archers and 10 soldiers, 2 soldiers will protec every archer)

nonono, the level has a guide for the code, soldiers = outer circle, archers = inner circle, there is no specific unit to defend for any soldier/archer

no at the start have 4 archer and 8 soldier and 1 archer die

how to command soldier to defend archer

Use the command soldier function, which places the soldiers in a circle around the archers and Hushbaum.

its already cirleing the archer

Oh the soldiers are trying to get heals. I thought they were just defending that way.

I don’t see anything else really wrong, maybe its just the tiny amount of dps missing when the soldiers try to heal that causes the ogres to overwhelm the other soldiers.

soldier are fine its about archer

no like the soldiers retreat so the archers die

make giga Chad soldiers bring heals to minichad archers

u wan’t to the soldier retreat so the archer die?

bring?? how to bring heal?

No i’m saying that’s what they are doing.