I have passed the level because my inventory is good, but my code doesn’t work the way I expect. Can anyone please explain what’s wrong?
The game loop contains several fuctions:
- checks if hero needs healing
- checks if there is a green to pick up
- fights
- if a black flag is set, pick up mushrooms
The fight()
function consists of the following steps:
- obtain array of enemies
- check if array is empty
2.1) if array is empty - return - if array is not empty, loop through array and attack oasis giardians, throwers and shamans if any
- update array of enemies
- if array is not empty and
5.1) if enemy №1 is near - attack enemy
5.2) call functionfight()
again
5.3) if enemy №1 is far - attack enemy
5.4) call functionfight()
again - if array is empty - return
This way newly arrived ranged units should be attacked first. If there are any melee units left nearby, at step 5.1, the closest of them should be attacked. When the function fight() runs again and there are no ranged units, a closest melee enemy should be attacked from the updated array of enemies. When there is no enemy left near, the hero should go to a far enemy, attack and the function should run until the enemy array is empty.
Instead I see Senick attack far enemies while surrounded by near enemies.
The steps 5.1 and 5.2 seem to get skipped and I don’t see why.
If I remove steps 5.3 and 5.4 the array of enemies doesn’t get updated at all.
I’ve spent quite a while trying to spot what’s wrong, but had no luck so far. I will very very much appreciate some help!
This is my code:
function stayFit(){
if(hero.health < hero.maxHealth - 200 && hero.isReady("heal") ){
hero.heal(hero);
hero.say("I heal");
}
}
function goOnManual(){
var flag=hero.findFlag("green");
if (flag){
hero.pickUpFlag(flag);
}
}
function fight(){
var enemies=hero.findEnemies();
var enemyIndex=0;
if (enemies.length === 0){
return;
} else {
while(enemyIndex < enemies.length){
var enemy=enemies[enemyIndex];
if ( enemy.type=="oasis-guardian" && hero.isReady("throw") && hero.distanceTo(enemy) < 30){
hero.throw(enemy);
while(enemy.health>0){
hero.attack(enemy);
}
} else if ( (enemy.type=="thrower" || enemy.type=="shaman") && hero.distanceTo(enemy) < 30){
hero.attack(enemy);
}
enemyIndex++;
}
enemies=hero.findEnemies();
if (enemies.length){
if (hero.distanceTo(enemies[0]) < 30) {
hero.attack(enemies[0]);
hero.say("Near enemy");
fight();
} else {
hero.move(enemies[0].pos);
hero.attack(enemies[0]);
hero.say("Far enemy");
fight();
}
} else {
return;
}
}
}
function collectMushrooms(){
var mushroomFlag=hero.findFlag("black");
if (mushroomFlag){
var mushrooms=hero.findItems();
var mushroomIndex=0;
while(mushroomIndex < mushrooms.length){
var mushroom=mushrooms[mushroomIndex];
hero.moveXY(mushroom.pos.x, mushroom.pos.y);
mushroomIndex++;
}
hero.pickUpFlag(mushroomFlag);
}
}
while(true){
stayFit();
goOnManual();
fight();
collectMushrooms();
}
My inventory includes:
Fine Telephoto Glasses
Precision Rifle
Sparkbomb
Hardened Emerald Chainmail body piece