Summit's gate (Javascript)

I gotta admit, I’m very good at programming, but this level just trumps me.
My code:

function findUnitWithLeastHealth () {
    let friends = hero.findFriends();
    var worstUnit = friends[0];
    for (let friend of friends) if (friend.health < worstUnit.health) worstUnit = friend;
    if (worstUnit.health > worstUnit.maxHealth/1.25) return null;
    return worstUnit;
}
function withinType (type, notTypes) {
    if (!(notTypes)) notTypes = [];
    var section;
    var enemiesInSection = [];
    if (hero.pos.x < 100) section = 1;
    else if (hero.pos.x < 158) section = 2;
    else section = 3;
    var list;
    if (type) list = hero.findByType(type);
    else list = hero.findEnemies();
    for (let enemy of list) {
        var enemySection;
        if (hero.pos.x < 100) enemySection = 1;
        else if (hero.pos.x < 158) enemySection = 2;
        else enemySection = 3;
        if (enemySection == section && notTypes.indexOf(enemy.type) == -1) enemiesInSection.push(enemy);
    }
    return enemiesInSection;
}
while (true) {
    let enemies = hero.findEnemies();
    var enemiesToAttack = [];
    for (let enemy of enemies) if (enemy.type != "tower" && (enemy.type != "door" || withinType(undefined, ["catapult", "tower"]))) enemiesToAttack.push(enemy);
    let enemy = hero.findNearest(enemiesToAttack);
    if (enemy) {
        if (hero.distanceTo(enemy) <= 3) hero.attack(enemy);
        else hero.move(enemy.pos);
    }
    for (let unit of hero.findFriends()) {
        if (unit.type == "paladin") {
            hero.command(unit, "move", {x: unit.pos.x+1, y: unit.pos.y});
            let unitToHeal = findUnitWithLeastHealth();
            if (unitToHeal && unit.canCast("heal")) hero.command(unit, "cast", "heal", unitToHeal);
            else {
                let unitEnemy = unit.findNearestEnemy();
                if (unitEnemy) hero.command(unit, "attack", unitEnemy);
            }
        } else {
            let unitEnemy = unit.findNearestEnemy();
            if (unitEnemy) hero.command(unit, "attack", unitEnemy);
        }
    }
}

don’t understand why it won’t at least go inside the inner gate…
Also, I know there could be some better versions, right now, I’m just focusing on making it work. ._.

New code:

function commandSoldier(unit, i, length) {
    if (unit.team == "ogres") return;
    let angle = Math.PI * 2 * i / length;
    let defendPos = {x: hero.pos.x, y: hero.pos.y};
    defendPos.x += 5 * Math.cos(angle);
    defendPos.y += 5 * Math.sin(angle);
    var enemy;
    if (unit.pos.x > 255 && unit.pos.x < 300) enemy = hero.findByType("warlock")[0];
    else enemy = hero.findNearestEnemy();
    if (enemy && hero.distanceTo(enemy) <= 7.5) hero.command(unit, "attack", enemy);
    else hero.command(unit, "move", defendPos);
}
while (true) {
    let item = hero.findNearestItem();
    if (hero.pos.x < 240) hero.move({x: hero.pos.x + 1, y: hero.pos.y});
    else if (hero.pos.x < 276) hero.move({x: 276, y: 34});
    hero.consecrate();
    if (hero.gold >= 20) hero.summon("soldier");
    let enemy = hero.findNearestEnemy();
    if (enemy && hero.distanceTo(enemy) <= 3) hero.attack(enemy);
    let paladins = hero.findByType("paladin");
    for (let i in paladins) {
        let paladin = paladins[i];
        let enemy = hero.findByType("catapult")[i];
        if (enemy) hero.command(paladin, "attack", enemy);
    }
    let soldiers = hero.findByType("soldier");
    for (let i in soldiers) {
        let soldier = soldiers[i];
        commandSoldier(soldier, i, soldiers.length);
    }
}

Equipment:

  • boots of leaping
  • runesword
  • worker’s gloves
  • engraved wrist watch
  • leather belt
  • twilight glasses
  • sapphire sense stone
  • order of the paladin
  • basic flags
  • boss star III
  • worn dragonplate helmet
  • worn dragonplate
  • deflector

The above still fails at the warlocks…

Solved. (20 charsss)

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.