Summits gate Javascript


#1

I am having a little trouble with my code it says that paladin has no method"canCast" but the paladins had the method in grim determination, so if someone could tell me what I am doing wrong, that would be great.

// Fight your way into the Inner Sanctum of the ogre chieftain, and defeat her.
function command(){
var friends = hero.findFriends();
var paladin = hero.findByType(“paladin”);
for(var i = 0; i < friends.length; i += 1){
var friend = friends[i];
var enemy = friend.findNearestEnemy();
if (friend && enemy) {
if (enemy.type == (“catapult”)) {
hero.command(friends[0], “attack”, catapult);
hero.command(friends[1], “attack”, catapult);
}else{
hero.command(friend, “attack”, enemy);
if (hero.health < 2500 && paladin.canCast(“heal”)) {
hero.command(paladin, “cast”, “heal”, hero);
}
}
}
}
}
function summon(){
if (hero.gold > hero.costOf(“soldier”)) {
hero.summon(“soldier”);
}
}

function fight(){
var enemy = hero.findNearestEnemy();
if (enemy) {
if (hero.canCast (“chain-lightning”)) {
hero.cast(“chain-lightning”, enemy);
hero.shield();
}else if(hero.isReady(“bash”)){
hero.bash(enemy);
hero.shield();
}else{
hero.attack(enemy);
hero.shield();
}
}
}
while(true) {
summon();
fight();
command();
}


#2

sorry if I am not formatting properly, I have no idea how to use the back tick thing


#3

@Not_a_User

// Fight your way into the Inner Sanctum of the ogre chieftain, and defeat her.
function command(){
    var friends = hero.findFriends();
    var paladin = hero.findByType("paladin");
    for(var i = 0; i < friends.length; i += 1){
        var friend = friends[i];
        var enemy = friend.findNearestEnemy();
        if (friend && enemy) {
            if (enemy.type == ("catapult")) {
                hero.command(friends[0], "attack", catapult);
                hero.command(friends[1], "attack", catapult);
            }else{
                hero.command(friend, "attack", enemy);
                if (hero.health < 2500 && paladin.canCast("heal")) {
                    hero.command(paladin, "cast", "heal", hero);
                }
            }
        }
    }
}
function summon(){
    if (hero.gold > hero.costOf("soldier")) {
        hero.summon("soldier");
    }
}

function fight(){
    var enemy = hero.findNearestEnemy();
    if (enemy) {
        if (hero.canCast ("chain-lightning")) {
            hero.cast("chain-lightning", enemy);
            hero.shield();
        }else if(hero.isReady("bash")){
        hero.bash(enemy);
        hero.shield();
        }else{
            hero.attack(enemy);
            hero.shield();
        }
    }
}
while(true) {
    summon();
    fight();
    command();
}

#4

You’re using canCast wrong.

it’s “unit”.canCast(“spell”, target)


#5

so I would write

"paladin".canCast("heal", hero);

?


#6

No quotes. Yeah everything else is fine.


#7

Thanks :smile: I really needed that advice


#8

wait it didn’t work it just said “try hero.health”


#9

Okay I loaded your code and I find several errors at first glance.

#1: catapult isn’t defined. u said "if enemy.type == “catapult” but u never defined what catapult is.

#2: paladin, you defined it as hero.findByType(“paladin”), which will return an array of all living paladins, not 1, which is why the canCast heal won’t work.

Also as a tip, unless your hero is within 3 meters of an enemy, use "hero.move(enemy.pos); ) so you can command your troops more and they won’t just stand there and do nothing.

Also, you have to use flags. Because in the end I see you standing in a corner summoning soldiers. Flags or Twilight Glasses are necessary.


#10

i use twilight glasses but I will try fixing the other errors


#11

but how do I define the paladins, do I make another for loop?


#12

make another for loop.

Actually, make an entire other function so the code is cleaner


#13

ok thanks
I will try but I got to eat dinner first.


#14

well, I tried fixing it, but it showed me another error:

// Fight your way into the Inner Sanctum of the ogre chieftain, and defeat her.
// Fight your way into the Inner Sanctum of the ogre chieftain, and defeat her.
function disassembleCatapults(){
    var catapults = hero.findByType("catapult");
    var friends = hero.findFriends();
    for(var i = 0; i < catapults.length; i += 1){
        var catapult = catapults[i];
         if (catapult) {
                hero.command(friends[0], "attack", catapult[0]);
                hero.command(friends[1], "attack", catapult[1]);
        }else{
            break;
        }
    }
}
function healHero(){
    var paladins = hero.findByType("paladin");
    for(var i = 0; i < paladins.length; i += 1){
        var paladin = paladins[i];
    if (hero.health < 2500 && paladin.canCast("heal", hero)) {
                    hero.command(paladin, "cast", "heal", hero);
        }
    }
}
function command(){
    var friends = hero.findFriends();
    var paladin = hero.findNearest("paladin");
    for(var i = 0; i < friends.length; i += 1){
        var friend = friends[i];
        var catapult = hero.findByType("catapult");
        var enemy = friend.findNearestEnemy();
        if (friend && enemy) {
                hero.command(friend, "attack", enemy);
        }
    }
}
function summon(){
    if (hero.gold > hero.costOf("soldier")) {
        hero.summon("soldier");
    }
}

function fight(){
    var enemy = hero.findNearestEnemy();
    if (enemy) {
        if (hero.canCast ("chain-lightning")) {
            hero.cast("chain-lightning", enemy);
            hero.shield();
        }else if(hero.isReady("bash")){
        hero.bash(enemy);
        hero.shield();
        }else{
            hero.attack(enemy);
            hero.shield();
        }
    }
}
while(true) {
    summon();
    fight();
    command();
    healHero();
    disassembleCatapults();
}

I ran into problems here:

function disassembleCatapults(){
    var catapults = hero.findByType("catapult");
    var friends = hero.findFriends();
    for(var i = 0; i < catapults.length; i += 1){
        var catapult = catapults[i];
         if (catapult) {
                hero.command(friends[0], "attack", catapult[0]);
                hero.command(friends[1], "attack", catapult[1]);
        }else{
            break;
        }
    }
}

It said
image


#15

The catapult is already dead.

To be honest all you have to do to destroy the catapults is to 1. move there, 2. stand there, and 3. move away once the catapult shoots the missile.


#16

I changed it and it stopped bugging, but my troops died and the tower finished me off.

// Fight your way into the Inner Sanctum of the ogre chieftain, and defeat her.
// Fight your way into the Inner Sanctum of the ogre chieftain, and defeat her.
function disassembleCatapults(){
    var catapults = hero.findByType("catapult");
    var friends = hero.findFriends();
    for(var i = 0; i < catapults.length; i += 1){
        var catapult = catapults[i];
         if (catapult) {
                hero.moveXY(catapult.pos.x, catapult.pos.y);
        }else{
            break;
        }
    }
}
function healHero(){
    var paladins = hero.findByType("paladin");
    for(var i = 0; i < paladins.length; i += 1){
        var paladin = paladins[i];
    if (hero.health < 2500 && paladin.canCast("heal", hero)) {
                    hero.command(paladin, "cast", "heal", hero);
        }
    }
}
function command(){
    var friends = hero.findFriends();
    var paladin = hero.findNearest("paladin");
    for(var i = 0; i < friends.length; i += 1){
        var friend = friends[i];
        var catapult = hero.findByType("catapult");
        var enemy = friend.findNearestEnemy();
        if (friend && enemy) {
                hero.command(friend, "attack", enemy);
        }
    }
}
function summon(){
    if (hero.gold > hero.costOf("soldier")) {
        hero.summon("soldier");
    }
}

function fight(){
    var enemy = hero.findNearestEnemy();
    if (enemy) {
        if (hero.canCast ("chain-lightning")) {
            hero.cast("chain-lightning", enemy);
            hero.shield();
        }else if(hero.isReady("bash")){
        hero.bash(enemy);
        hero.shield();
        }else{
            hero.attack(enemy);
            hero.shield();
        }
    }
}
while(true) {
    summon();
    fight();
    command();
    healHero();
    disassembleCatapults();
}

#17

Remove all the hero.shield() and also change the canCast for chain lightning


#18

ok, but how would I change the canCast?


#19
if ( hero.canCast(“chain-lightning”) )
# and
if ( hero.isReady(“chain-lightning”) )
# are equivalent

if ( hero.canCast("chain-lightning", enemy) )

defines if hero can physically attain the enemy ( you can see the enemy with see-through-wall glasses, but you cannot harm him)


#20

so you are saying that I put ‘enemy’ after canCast,
I updated the code but my hero died quicker