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; )

#13

in this, it looks like the variable count isn’t doing anything, why is it there?


#14

yeah it is in fact a bit weird

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

using “,” is not always correct you should use semi colon each time." ; "

should be:

for (var i = 0; i < enemies.length; i++)

It’s about the same syntax in C++
using multiples " = " in a for loop is bad pratice because it can cause confusion or assignment
even >= and <= is avoided.

also doing ++i is incrementing the i before the line of code is done. If you want to check index 0 you’d want it after.

This is why I put i++ instead of ++i and i = 0 instead of i = -1

you still need the count var for the enemy count though. the function return it.

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

#15

This construction may be error prone, but it’s perfectly legal in java-script. And it differs from C++:Block Scoping Rules
" Variables declared with var do not have block scope… “standalone” blocks are valid syntax, you do not want to use standalone blocks in JavaScript, because they don’t do what you think they do, if you think they do anything like such blocks in C or Java."
T
he code runs smoothly in both versions : ascending and descending order

//  see execution at https://goo.gl/4gMkh2
var numbers = [11, 55, 77, 88, 99, 22, 33, 44,  66];
function countNumbersGreaterThan(numbers, testNumber){
    // error prone but perfectly legal in javascript    
    // for (var i = -1, count = 0, length = numbers.length; ++i < length; ){
        // if you didn' bother iterating in reverse order
        for (var i = numbers.length , count = 0 ; --i > -1 ; ){
        var number = numbers[i];
        console.log(number + ' = numbers[' + i + ']'); 
        if (testNumber < number)
            count ++;
    }        
    return count;
}
console.log(countNumbersGreaterThan( numbers, 23))

image
both version executed in 50 steps
and the normal version:

// see the execution at https://goo.gl/nEAFFo 
var numbers = [11, 55, 77, 88, 99, 22, 33, 44,  66];
function countNumbersGreaterThan(numbers, testNumber){
   var count = 0;
   for (var i = 0; i < numbers.length; i++ ){
        var number = numbers[i];
        console.log(number + ' = numbers[' + i + ']'); 
        if (testNumber < number)
            count ++;
    }        
    return count;
}
console.log(countNumbersGreaterThan( numbers, 23))

image
execution takes more steps?