# Grim Determination (JavaScript)

Having some problems successfully completing this level. I think I’ve done fair amount of optimization, including:

1. Moving the paladins forward as to avoid the enemy missile strike blow-back into the mine field.
2. Coin collection optimization of the two peasants to pump out griffin units as fast as possible.

But my units are continually overrun. Would someone please look over my code and let me know what could be the problem? TIA.

``````// Your goal is to protect Reynaldo

// Find the paladin with the lowest health.
var lowestHealth = 99999;
var lowestFriend = null;
var friends = this.findFriends();
for(var f=0; f < friends.length; f++) {
var friend = friends[f];
if(friend.type != "paladin") { continue; }
if(friend.health < lowestHealth && friend.health < friend.maxHealth) {
lowestHealth = friend.health;
lowestFriend = friend;
}
}
return lowestFriend;
};

if (lowPal) {
}
}
else {
}
}
};

this.commandPeasant = function(peasant) {
peasants = this.findByType("peasant");
for (var i =0 ; i < peasants.length ; i ++) {
peasant = peasants[i];
coins = this.findItems();
nearestCoin = peasant.findNearest(coins);

if (i === 0 && nearestCoin.pos.y < 41){
this.command(peasant, "move", nearestCoin.pos);
}
else if (i === 1 && nearestCoin.pos.y >= 41){
this.command(peasant, "move", nearestCoin.pos);
}
}
};

this.commandGriffin = function(griffin) {
var enemy = griffin.findNearestEnemy();
if (enemy) {
this.command(griffin, "attack", enemy);
}
};

this.commandFriends = function() {
var friends = this.findFriends();
for(var i=0; i < friends.length; i++) {
var friend = friends[i];
if(friend.type == "peasant") {
this.commandPeasant(friend);
} else if(friend.type == "griffin-rider") {
this.commandGriffin(friend);
} else if(friend.type == "paladin") {
}
}
};

loop {
this.commandFriends();

// Summon griffin riders!
if (this.gold > this.costOf("griffin-rider")){
this.summon("griffin-rider");
}

// Move Paladins forward to avoid the enemy missle blowback into mine field
var friends = this.findFriends();
for (var i = 0; i < friends.length; i++) {
var friend = friends[i];
var friendPOSx = 70;
var friendPOSy = friend.pos.y;
var friendPOS = {'x':friendPOSx,'y':friendPOSy};
this.command(friend, "move", friendPOS );
}
}
}``````

You only command your paladins to shield if there is no `lowPal`, that is, if all the paladins are at max health. Move that block one level forward. That way they will shield if they can not cast `heal`.

Chronist,

Thanks for the suggestion. I made the change you suggested, unfortunately the end result is pretty much the same. Shortly into running the code the skeletons begin at the bottom of the paladin column and kill the paladins one at a time moving upwards, following that the mine field is breached, killing Reynaldo.

Revised Code
``````// Your goal is to protect Reynaldo

// Find the paladin with the lowest health.
var lowestHealth = 99999;
var lowestFriend = null;
var friends = this.findFriends();
for(var f=0; f < friends.length; f++) {
var friend = friends[f];
if(friend.type != "paladin") { continue; }
if(friend.health < lowestHealth && friend.health < friend.maxHealth) {
lowestHealth = friend.health;
lowestFriend = friend;
}
}
return lowestFriend;
};

if (lowPal) {
}
else {
}
}
else {
}
};

this.commandPeasant = function(peasant) {
peasants = this.findByType("peasant");
for (var i =0 ; i < peasants.length ; i ++) {
peasant = peasants[i];
coins = this.findItems();
nearestCoin = peasant.findNearest(coins);

if (i === 0 && nearestCoin.pos.y < 41){
this.command(peasant, "move", nearestCoin.pos);
}
else if (i === 1 && nearestCoin.pos.y >= 41){
this.command(peasant, "move", nearestCoin.pos);
}
}
};

this.commandGriffin = function(griffin) {
var enemy = griffin.findNearestEnemy();
if (enemy) {
this.command(griffin, "attack", enemy);
}
};

this.commandFriends = function() {
var friends = this.findFriends();
for(var i=0; i < friends.length; i++) {
var friend = friends[i];
if(friend.type == "peasant") {
this.commandPeasant(friend);
} else if(friend.type == "griffin-rider") {
this.commandGriffin(friend);
} else if(friend.type == "paladin") {
}
}
};

loop {
this.commandFriends();

// Summon griffin riders!
if (this.gold > this.costOf("griffin-rider")){
this.summon("griffin-rider");
}

// Move Paladins forward to avoid the enemy missle blowback into mine field
var friends = this.findFriends();
for (var i = 0; i < friends.length; i++) {
var friend = friends[i];
var friendPOSx = 70;
var friendPOSy = friend.pos.y;
var friendPOS = {'x':friendPOSx,'y':friendPOSy};
this.command(friend, "move", friendPOS );
}
}
}

``````

It occurs to me that maybe it’s unclear that they can attack.

1 Like

Oh My Gerd.

That was the problem Catsync. The level instructions did not mention to command the paladins attack, nor that they had that ability for that matter. I had assumed the paladins’ roles were to draw the oncoming enemies to them and they would hunker down shielding and healing while the griffin riders were amassed and picked the enemies off. Added an `else if` to command paladin attack and instant level success. Did not even end up needing the code segment to move the paladins away from the minefield to avoid the enemy missile blow-back. Epic levels of frustration achieved and time wasted. Thanks so much for the suggestion!

LOL!!! I completed the mission by doing just that. I never had them attack, I just sent out griffin-riders to attack, while they shielded. the problem I kept running into was that they would get hit and knocked back into the mines, blowing up Reynaldo. It never even occurred to me that they could attack, with the instructions for shield command and all. Well, whatever. I passed the mission anyway, I just did it the hard way!

Heheh, ok, it should be more obvious now

When I tried to make them attack they still get pushed into the mines???
How am I supposed to fix this?
Am I supposed to move them forward a bit first or what?

You can move the forward at the start of the level an then let them fight normally

``````while( this.now() < LETS_FIGHT_NOW)
// move forward a bit
// summon griffins or let some time pass
loop {    //main loop
``````
1 Like

One other thing I noticed reading through that hasn’t been mentioned.
Your collection routine, says collect nearest if it is in the sector you designated for that peasant. (Topside/bottom side)

But you don’t have it step through to the next furthest coin, or move to a default location or in some other way have that peasant do something meaningful if the peasant is detecting a nearest coin on the other side.

Strange, your code only creates the error “Line 59: ReferenceError: peasants is not defined”…