[SOLVED]Help With Library tactician JavaScript

I need help with the level Library Tactician

This is my current 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 enemy in enemies){
        if (enemy && enemy.health > 15) {
            bestTarget = enemy;
        }
    }
    // 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 friend in friends){
        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 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 archer = hero.findByType("archer");
    for(var i=0; i < soldiers.length; i++) {
        var soldier = soldiers[i];
        commandSoldier(soldier, i, soldiers.length);
    }
    // use commandArcher() to command your archers
    commandArcher(archer);
    
}

I lose at around 22 seconds where one of my soldiers dies

1 Like

Are there any errors?

1 Like

Could you please send me the link to level?

Here you go:

There are no errors

You must command soldiers to go to Hushbaum, when they have little health

1 Like

They constantly return to hushbaum

1 Like

Do you have this solution?

if (soldier.health > 60)
1 Like

I don’t quite know what you mean by that

1 Like

So, if soldier’s health > 60 he goes to defend, else he comes to Hushbaum

1 Like

But where do I put that?

1 Like

change “archer” to “archers”

use a for loop like the one for soldiers

// use a for loop
for (var q=0; q < archers.length; q++;){
var archer = archers[q];
commandArcher(archer)
}

Try using your Python code for this level(convert to Javscript)

Okay, give me a second for that @sci12, here is my updated 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 enemy in enemies){
        if (enemy && enemy.health > 15) {
            bestTarget = enemy;
        }
    }
    // 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 friend in friends){
        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 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 q=0; q < archers.length; q++){
        var archer = archers[q];
        commandArcher(archer);
}
    
}

Did it work? 20 chars.

add this to your function commandSoldier:

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})
}
        

sorry, my javascript is bad

I did not have to use the extra if statement for the commandSoldier.

well, that’s your code, I’m suggesting mine

it worked, too. :wink:

Im still working on converting my code right now, my second line says “expected an identifier and instead saw ‘.’” my second line is: if hero.gold is >= hero.costOf("soldier"){

remove is from the statement