Sarven Shepherd Targeting Issue

Been beating my head against the wall for 3 days trying to figure out what I’m doing wrong. Seems like I attack every other enemy?

Tried to get it to speak whenever the array gets marked up, but said Found0, Killed1, Found3, Killed4. 2 completely got skipped, and so will 5 (with slightly different equipment for better performance).

Please, Internet Kenobi, you’re my only hope!

var enemyIndex = 0;
//set targeting array to 0
loop {
    var enemies = this.findEnemies();
    var enemy = enemies[enemyIndex];
     //setup target vars
    if (enemies[enemyIndex]) {
        //while there are targets...
        if (enemy.type != 'sand-yak') {
            //that aren't sand-yaks...
            while (enemy.health > 0) {
                //and they're still alive...
                if (this.isReady("cleave")&&this.distanceTo(enemy)<10) {
                    this.cleave(enemy);
                    //cleavecode
                }
                else if (this.isReady("bash")&&this.distanceTo(enemy)<3) {
                    //bashcode
                    this.bash(enemy);
                }
                else this.attack(enemy);
                    //If those don't work, just stab it!
            }
        this.say("Killed" +enemyIndex+"!");
        //diagnostic
        enemyIndex+=1;
        //When it's dead, mark targeting var up one
    }
    else this.say("Found"+enemyIndex);
        //diagnostic
        enemyIndex+=1;
        //If targeting a sand-yak, mark targeting var up one
    }
}

I think you are calling:

    enemyIndex+=1;

twice. Second one occurs when you get to the end of the if enemies line. First one occurs if the enemy isn’t a sand-yak.

If the enemy is a sand-yak, it should only cycle once through this section.

    var enemyIndex = 0;
    //set targeting array to 0
loop {
var enemies = this.findEnemies();
var enemy = enemies[enemyIndex];
 //setup target vars
if (enemies[enemyIndex]) {
    //while there are targets...
    this.say("Found"+enemyIndex);
    //diagnostic
    enemyIndex+=1;
    //If targeting a sand-yak, mark targeting var up one
    }

I’ve modified my code a little bit for more diagnostics. If it isn’t a sand yak, it should cycle once, if it is a sand yak, it should cycle once. There shouldn’t be any both scenario.

Diagnostics return FoundSandYak0, FoundEnemy1, Killed1, FoundSandYak2, FoundSandYak3, FoundEnemy4(just as it comes on screen alongside another that isn’t recognized),Killed4, then stands there like a cheese and gets killed.

For some reason half of the enemies aren’t getting arrays assigned to them? Either that or my reasoning isn’t quite right in some regard. I just started learning coding maybe 2 weeks ago, so the latter wouldn’t surprise me. Please advise?

// Use while loops to pick out the ogre,
var enemyIndex = 0;
//set targeting array to 0
loop {
    var enemies = this.findEnemies();
    var enemy = enemies[enemyIndex];
     //setup target vars
    if (enemies[enemyIndex]) {
        //while there are targets...
        if (enemy.type != 'sand-yak') {
            this.say("Found Enemy" + enemyIndex
            //that aren't sand-yaks...
            while (enemy.health > 0) {
                //and they're still alive...
                if (this.isReady("cleave")&&this.distanceTo(enemy)<10) {
                    this.cleave(enemy);
                    //cleavecode
                }
                else if (this.isReady("bash")&&this.distanceTo(enemy)<3) {
                    //bashcode
                    this.bash(enemy);
                }
                else this.attack(enemy);
                    //If those don't work, just stab it!
            }
            this.say("Killed" +enemyIndex+"!");
            //diagnostic
            enemyIndex+=1;
            //When it's dead, mark targeting var up one
        }
        else if (enemy.type == "sand-yak") {
            this.say("Found Sand Yak"+enemyIndex);
            //diagnostic
            enemyIndex+=1;
        }
        //If targeting a sand-yak, mark targeting var up one
    }
}

This is looking so complicated.
And I see some flaws.

Your enemyIndex is set to 0 outside your loop, which means it will never be reset.
You’re assigning enemy before you check wether there is an enemy.
You check if (a), then else if (!a). While this is not directly wrong, it is confusing and error-prone.

I’ll fixed it for you (I deleted all comments exept some explaining ones)

loop{
    var enemies = this.findEnemies();
    var enemyIndex = 0;
    
    while (enemyIndex < enemies.length){
        var enemy = enemies[enemyIndex];
        enemyIndex += 1;       //Increase enemyIndex in every case
        
        if (enemy.type != 'sand-yak') {
            while (enemy.health > 0) {
                // You really don't want to cleave in this level. Except you can kill a sand-yak

                //We don't need to check the distance.
                //The hero will simply walk to the enemy if he is out of range
                if (this.isReady("bash")) { 
                    this.bash(enemy);
                }
                else{
                    this.attack(enemy);
                }
            }
        }
    }
}
1 Like

that gave me few hours to figure it out the code. I never knew about the “bash” keys instead of using “cleave” because this level does not want to to hit the sand-yak or they will kill you immediately.Also, I was struggled was having to while(enemyIndex < enemies.length) vs while (enemyIndex <= enemies.length) before the WHOLE code is running. The difference is less than or equal to or less than . that’s got me really confused till i realized about the increment by enemyIndex. or they wont attack anyway.