Library Tactician Javascript


#1

For some reason this code doesn’t work. Any ideas?

this.commandSoldier = function(soldier, soldierIndex, numSoldiers) {
    var angle = Math.PI * 3 * soldierIndex / numSoldiers;
    var defendPos = {
        x: 41,
        y: 40
    };
    defendPos.x += 10 * Math.cos(angle);
    defendPos.y += 10 * Math.sin(angle);
    this.command(soldier, "defend", defendPos);
};
this.findStrongestTarget = function() {
    var mostHealth = 0;
    var bestTarget = null;
    var enemies = this.findEnemies();
    bestTarget = enemies.mostHealth;
    if (bestTarget && bestTarget.health > 15) {
        return bestTarget;
    } else {
        return null;
    }
};
this.commandArcher = function() {
    var archer = this.findByType("archer");
    archerTarget = this.findStrongestTarget();
    var nearest = this.findNearest(this.findEnemies());
    if (archerTarget) {
        this.command(archer, "attack", archerTarget);
    }
};
var archerTarget = null;
loop {
    if (this.gold >= this.costOf("soldier")) {
        this.summon("soldier");
    }
    if (!archerTarget || archerTarget.health <= 0) {
        archerTarget = this.findStrongestTarget();
    }
    var friends = this.findFriends();
    var soldiers = this.findByType("soldier");
    for (var i = 0; i < soldiers.length; i++) {
        var soldier = soldiers[i];
        this.commandSoldier(soldier, i, soldiers.length);
    }
    this.commandArcher();
}

Thanks!


#2

There is no such attribute as enemies.mostHealth. That would explain your problem.


#3

You’ll have to figure out how to check each enemy in the enemies array’s health, then compare them, then choose the one with the most health.

For hints on how to do this, play Mad Maxer (and the follow up levels if you’re a subscriber).


#4

Ok. I changed my code and I get even further than before but I still fail the level. Any other suggestions? :confused:

// 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.
this.commandSoldier = function(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);
    this.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.
this.findStrongestTarget = function() {
    var mostHealth = 0;
    var bestTarget = null;
    var enemies = this.findEnemies();
    var enemyIndex = 0;
    // Figure out which enemy has the most health, and set bestTarget to be that enemy.
    if(enemies){
    while(enemyIndex < enemies.length){
         var enemy = enemies[enemyIndex];
        enemyIndex += 1;
        
        if(enemy.heath > mostHealth){
            mostHealth = enemy;
            bestTarget = mostHealth;
        }
    }
    }
    // 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.
this.commandArcher = function(archer) {
    var nearest = archer.findNearestEnemy();
    if(archerTarget) {
        this.command(archer, "attack", archerTarget);
    } else if(nearest) {
        this.command(archer, "attack", nearest);
    }
};

var archerTarget = null;
loop {
    // If archerTarget is dead 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 = this.findStrongestTarget();
    }
    var friends = this.findFriends();
    var soldiers = this.findByType("soldier");
    var archers = this.findByType("archer");
    for(var i=0; i < soldiers.length; i++) {
        var soldier = soldiers[i];
        this.commandSoldier(soldier, i, soldiers.length);
    }
    // use commandArcher() to command your archers
   for(var j=0; j < archers.length; j++) {
        var archer = archers[j];
        this.commandArcher(archer);
    }
}

Thanks!


#5

Ok. I changed my code and I get even further than before but I still fail the level. Any other suggestions? :confused:
Here is 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.
this.commandSoldier = function(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);
    this.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.
this.findStrongestTarget = function() {
    var mostHealth = 0;
    var bestTarget = null;
    var enemies = this.findEnemies();
    var enemyIndex = 0;
    // Figure out which enemy has the most health, and set bestTarget to be that enemy.
    if(enemies){
    while(enemyIndex < enemies.length){
         var enemy = enemies[enemyIndex];
        enemyIndex += 1;
        
        if(enemy.heath > mostHealth){
            mostHealth = enemy;
            bestTarget = mostHealth;
        }
    }
    }
    // 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.
this.commandArcher = function(archer) {
    var nearest = archer.findNearestEnemy();
    if(archerTarget) {
        this.command(archer, "attack", archerTarget);
    } else if(nearest) {
        this.command(archer, "attack", nearest);
    }
};

var archerTarget = null;
loop {
    // If archerTarget is dead 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 = this.findStrongestTarget();
    }
    var friends = this.findFriends();
    var soldiers = this.findByType("soldier");
    var archers = this.findByType("archer");
    for(var i=0; i < soldiers.length; i++) {
        var soldier = soldiers[i];
        this.commandSoldier(soldier, i, soldiers.length);
    }
    // use commandArcher() to command your archers
   for(var j=0; j < archers.length; j++) {
        var archer = archers[j];
        this.commandArcher(archer);
    }
}

Thanks!


#8

You might have to change the commandSoldier function… I had to.