hero.findEnemies questions


#1

is it possible to make it so it only looks for enemies within a certain range instead of the normal 60m?
in Backwoods Brawl I would like it so that my hero only cleaves when there is 3+ enemies within the range of the cleave… but it looks at all the enemies instead of just the ones I want it to look at.
thanks.


#2

How to cleave when multiple enemies nearby


#3

sorry… I forgot to mention I’m using Javascript… :neutral_face:


#4

That’s alright, I can convert @sinogermany’s code into javascript if you want.


#5

Thank you @Deadpool198! :slight_smile:


#6

I’ll do it later today. I’m happy to help!


#7
function findInRangeEnemies(enemies) {
    var arrayOfInRangeEnemies = [];
    for (var enemyIndex in enemies) {
        var enemy = enemies[enemyIndex];
        if (enemy && hero.distanceTo(enemy) <= 10) {
            arrayOfInRangeEnemies.push(enemy);
        }
    }
    return arrayOfInRangeEnemies;
}

while(true) {
    var enemies = hero.findEnemies();
    var enemy = hero.findNearest(enemies);
    var inRangeEnemies = findInRangeEnemies(enemies);
    if(inRangeEnemies.length > 3 && hero.isReady("cleave")) {
        hero.cleave(enemy);
    }
}

To change how many enemies you want in range to cleave, change this:
inRangeEnemies.length > 3 e.g to: > 5

and to change how far away you want your enemies to be before you slaughter them with your sharp sword(:dagger: :smiling_imp:) change this:
hero.distanceTo(enemy) <= 10 e.g to: <= 5

Ok, I hope that helps, and sorry if it doesn’t work, I have tried it, but I’m not 100% sure about javascript because I’ve done codeCombat in python so far.

Anyway, I hope it does,
:lion: :lion: :lion:


#8

:+1: @Deadpool198 !
You function is nicer than the original one! Tested in leave it to cleaver.

Put in main to be fully working:

    if (enemy && enemy.health > 0){
        if(inRangeEnemies.length > 3 && hero.isReady("cleave")) 
            hero.cleave(enemy);
        else 
            hero.attack(enemy);   
    }

edit : and if you prefer the original:

function countNearbyEnemies(range_in_metres){
    var enemies = hero.findEnemies();
    for (var i = -1, count = 0, length = enemies.length; ++i < length; ){
        var enemy = enemies[i];
        if (hero.distanceTo(enemy) < range_in_metres)
            count ++;
    }        
     return count;
}

function worthwhileCleaving(minimumNearbyEnemiesCount){
    var range = 5; // you can change it to whatever makes sense in a level
    var count = countNearbyEnemies(range);
    return count >= minimumNearbyEnemiesCount; // true or false
}
while (true){
    var enemy = hero.findNearest(hero.findEnemies());
    if (enemy && enemy.health>0){
        if (hero.isReady('cleave') && worthwhileCleaving(4))
            hero.cleave(enemy);
        else 
            hero.attack(enemy);
    }    
}

#9

Thank you, I’m flattered. :blush: :grin:


#10

This helped me too, thank you!


#11

I’m glad, (20 characters)


#12

Found Why is using “for…in” with array iteration a bad idea?
so changed the code:

    var count = 0;
    for (var i in enemies)

to

for (var i = -1, count = 0, length = enemies.length; ++i < length; )