Library Tactician Dec '15/ Apr '21

Okay… nothings wrong now, except soldiers are running away.
Would you like to see my current code?

yup i would like to see

Mk.6 of my code…

// 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();
    // 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.
this.commandArcher = function(archer, archerIndex, numArchers) {
    var nearest = archer.findNearestEnemy();
    if(archerTarget) {
        this.command(archer, "attack", archerTarget);
    } else if(nearest) {
        this.command(archer, "attack", nearest);
    }
};

var archerTarget = null;
this.buildXY("arrow-tower", 36, 30);
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");
    for(var i=0; i < soldiers.length; i++) {
        var soldier = soldiers[i];
        this.commandArcher(soldier, i, soldiers.length);
    }
    // use commandArcher() to command your archers
    var archers = this.findByType("archer");
    for (var a=0; a < archers.length; a++) {
        var archer = archers[a];
        this.commandArcher(archer, a, archers.length);
    }
}

i recommend you build two archers at the start it would help a load

or soldiers it would all help

Start in your main loop and read the code line by line to see where there may be a problem.

You seem to be editing things that don’t need to be changed. For example, the default commandArcher function requires only 1 parameter. Why does your commandArcher require 3? You don’t use the other 2 parameters inside the function. Did you reload the code? If so, why did you change the commandArcher function?

So I’ve changed this.commandArchers(archer, archers[a], archers.length) to this.commandArchers(archer)… what next?

Check what else you changed from the default code. Go through the main loop line by line. See if you typed anything wrong.

I’m not sure… everything seems in order.

build a archer then a soldier when you start

of two archers then try submitting a few times

or just submit a few times

  1. Check if you have a red X under your hero. That will mean an error on the code and everything will stop working.

  2. Give an extra parameter to findByType, it should be:
    findByType(type_to_select, list_from_which_you_select)

var friends = this.findFriends();
var soldiers = this.findByType("soldier",friends);
var archers = this.findByType("archer",friends);
this.say("found "+soldiers.length+ " soldiers");
this.say("found "+archers.length+" archers");

The this.say are to debug your code and make sure you are finding your archers. IF you are finding them, remove the this.say because they are slowing you down.

Once you are sure you find an archers debug the commandArcher function by adding 2 this.say there also:

this.commandArcher = function(archer, archerIndex, numArchers) {
    var nearest = archer.findNearestEnemy();
    if(archerTarget) {
        this.command(archer, "attack", archerTarget);
        this.say(archer+" attacking "+archerTarget);

    } else if(nearest) {
        this.command(archer, "attack", nearest);
        this.say(archer+" attacking "+nearest);
    }
};

if it shows your archers attacking the correct enemy, remove the this.say as they slow you down

For Steven_Kang2003: Thanks for the help on troops, but if the troops I summon get killed, it registers on the objectives.
For AdrianCgw: There is no X under my hero, and the codes added do work, execpt for the this.say(archer+" attacking "+archerTarget); part, as my hero keeps saying [object Object] attacking Skrungt, even after Skrungt dies.

no I won when i built two archers
first try :grin:

I’ve submitted 5 time with no success…

i guess i got lucky :slightly_smiling:

Hold on if you start with 45 gold how do you summon two archers?:expressionless:

i wait and for it to go to 50
and i usually survive

Hmm… I’ll try that…