Pender's trial infinite loop help

return;  //Commented out to stop infinite loop.
return;  //Commented out to stop infinite loop.
var summonTypes = ['archer', 'soldier'];

function summonTroops() {
    if (hero.gold >= hero.costOf(summonTypes)) {
        hero.summon(summonTypes);
    }
}

function commandTroops() {
    var friends = hero.findFriends();
    for (var i = 0; i < friends.length; i ++) {
        var friend = friends[i];
        if (friend.type == "archer") {
            commandArcher(friend);
        }
        if (friend.type == "soldier") {
            commandSoldier(friend);
        }
        if (friend.type == "peasant") {
            commandPeasant(friend);
        }
    }
}

function commandArcher(archer) {
    var enemy = hero.findNearestEnemy();
    if (enemy) {
        while (enemy.health > 0) {
            hero.command(archer, "attack", enemy);
        }
    }
    else {
        hero.command(archer, 'move', hero.pos);
    }
}

function commandSoldier(soldier) {
    var enemy = hero.findNearestEnemy();
    if (enemy) {
        while (enemy.health > 0) {
            hero.command(soldier, "attack", enemy);
        }
    }
    else {
        hero.command(soldier, "move", enemy);
    }
}

function commandPeasant(peasant) {
    var item = hero.findNearestItem();
    while (item){
        hero.command(peasant, "move", item.pos);
    }
}

function collect() {
    var item = hero.findNearestItem();
    if (item) {
        hero.move(item.pos);
    }
}

commandTroops();
while(true) {
    collect();
}

as u can see.

deleate this lol
try to make less units as well…
UR PC IS GONNA GET JAMMED

already still doesn’t work.

I agree with ritic, how about you do only griffins, because what you have is a bit too much, the level doesn’t need all of this.

Also, do you want to command your troops only once or continuously?

wait i have new code


var summonTypes = ['archer', 'soldier'];

function summonTroops() {
    var type = summonTypes[hero.built.length % summonTypes.length];
    if (hero.gold > hero.costOf(type)) {
        hero.summon(type);
    }
}

function commandTroops() {
    var friends = hero.findFriends();
    for (var i = 0; i < friends.length; i ++) {
        var friend = friends[i];
        if (friend.type == "archer") {
            commandArcher(friend);
        }
        if (friend.type == "soldier") {
            commandSoldier(friend);
        }
        if (friend.type == "peasant") {
            commandPeasant(friend);
        }
    }
}

function commandArcher(archer) {
    var enemy = hero.findNearestEnemy();
    if (enemy) {
        hero.command(archer, "attack", enemy);
    }
    else {
        hero.command(archer, 'move', hero.pos);
    }
}

function commandSoldier(soldier) {
    var enemy = hero.findNearestEnemy();
    if (enemy) {
        hero.command(soldier, "attack", enemy);
      
    }
    else {
        hero.command(soldier, "move", hero.pos);
    }
}

function commandPeasant(peasant) {
    var item = hero.findNearestItem();
    if (item){
        hero.command(peasant, "move", item.pos);
    }
}

function collect() {
    var item = hero.findNearestItem();
    if (item) {
        hero.move(item.pos);
    }
}

while(true) {
    collect();
    summonTroops();
    commandTroops();
}

So what’s the problem?

it work well but i get error code has infinite loop or code slow

Basically do this:

  1. Make a function where you find the peasant and command it to collect coins
  2. Make another function for summoning griffins
  3. Make the last function for commanding the griffins
  4. Make a while-true loop calling these three functions

And the error of infinite loop didn’t appear for me when i ran your code

i can’t summon griffin

Hmm, then do for archers

summon hmmm
what can u summon
the one that costs the most

i have boss star 2 20 charac

hey why can i just do summon troops function method?

That’s fine, you can use it, it was just a suggestion

1 Like

bruh too laggy 20 char

still get infinite loop

New code please? (20chars)

here i don’t need to reset the whole code i just get the error

var pos = [];
pos[0] = {'x': 23, 'y': 42};
pos[1] = {'x': 24, 'y': 31};
var summonTypes = ['archer', 'soldier'];

function summonTroops() {
    var type = summonTypes[hero.built.length % summonTypes.length];
    if (hero.gold > hero.costOf(type)) {
        hero.summon(type);
    }
}

function soldier() {
    var friends = hero.findFriends();
    for (var i = 0; i < friends.length; i ++) {
        var friend = friends[i];
        if (friend.type == "soldier") {
            commandSoldier(friend);
        }
    }
}

function archer() {
    var friends = hero.findFriends();
    for (var i = 0; i < friends.length; i ++) {
        var friend = friends[i];
        if (friend.type == "archer") {
            commandArcher(friend);
        }
    }
}

function commandArcher(archer) {
    var enemy = hero.findNearestEnemy();
    if (enemy) {
        hero.command(archer, "attack", enemy);
    }
    else {
        hero.command(archer, 'move', {'x': 24, 'y': 36});
    }
}

function peasant() {
    var friends = hero.findFriends();
    for (var i = 0; i < friends.length; i ++) {
        var friend = friends[i];
        if (friend.type == "peasant") {
            commandPeasant(friend);
        }
    }
}

function commandSoldier(soldier) {
    var enemy = hero.findNearestEnemy();
    if (enemy) {
        hero.command(soldier, "attack", enemy);
      
    }
    else {
        hero.command(soldier, "move", {'x': 24, 'y': 29});
    }
}

function commandPeasant(peasant) {
    var item = hero.findNearestItem();
    if (item){
        hero.command(peasant, "move", item.pos);
    }
}


function collect() {
    var item = hero.findNearestItem();
    if (item) {
        hero.move(item.pos);
    }
}

function all() {
    peasant();
    soldier();
    archer();
}

while(true) {
    collect();
    summonTroops();
    all();
}

where do you get the error?