Hey guys, need some help on Summit’s gate coding in JavaScript. The following code doesn’t seem to do anything and my soldiers just scatter without attacking anything. Hero doesn’t do anything and won’t pick up flags. I’m not sure where the problem is so I’m posting all my code / functions below.
//Choose Hero strategy, "flag", "attack, or "shield"
this.chooseHeroStrategy = function () {
var flags = this.findFlags();
var enemies = this.findEnemies();
if (flags) {
return "flag";
}
else if (enemies) {
return "attack";
}
else {
return "shield";
}
};
//Execute a chosen Hero Strategy
this.executeHeroStrategy = function () {
var strategy = this.chooseHeroStrategy();
if (strategy == "flag") {
var flag = this.findFlag();
this.pickUpFlag(flag);
}
else if (strategy == "attack") {
var htarget = this.chooseHeroEnemy();
if(htarget) {
this.attack(htarget);
}
}
else if (strategy == "shield") {
this.shield();
}
};
//Prioritize enemies by either catapult, warlock, or nearest
this.chooseHeroEnemy = function () {
var enemies = this.findEnemies();
var nearenemy = this.findNearest(enemies);
var catapults = this.findByType("catapult", enemies);
var warlocks = this.findByType("warlocks", enemies);
if (catapults) {
var ctarget = this.findNearest(catapults);
if (ctarget) {
return ctarget;
}
}
else if (warlocks) {
var wtarget = (this.findNearest(warlocks));
if (wtarget) {
return wtarget;
}
}
else if (nearenemy) {
return nearenemy;
}
};
//Tell Allies to either "retreat", "attack", or "defend"
this.chooseAllyStrategy = function (ally) {
var enemies = this.findEnemies();
if (this.now() < 15) {
return "retreat";
}
else if (enemies) {
var nearenemy = ally.findNearest(enemies);
if (ally.distanceTo(nearenemy < 40)) {
return "attack"; }
}
else {
return "defend";
}
};
//Call Ally Strategy function and Execute it
this.executeAllyStrategy = function (allies) {
for (a=0; a < allies.length; a++) {
var ally = allies[a];
var strategy = this.chooseAllyStrategy(ally);
if (strategy == "retreat") {
this.allyRetreat(ally);
}
if (strategy == "attack") {
this.allyAttack(ally);
}
if (strategy == "defend") {
this.command(ally, "defend", this.pos);
}
}
};
this.allyRetreat = function (ally) {
var retreatpoint = {x: 0, y: 37};
this.command(ally, "move", retreatpoint);
};
this.allyAttack = function (ally) {
var enemies = this.findEnemies();
var nearenemy = ally.findNearest(enemies);
if (ally.type != "paladin") {
if (nearenemy) {
this.command(ally, "attack", nearenemy);
}
}
if (ally.type == "paladin") {
this.healcommand(ally);
}
};
this.lowesthealthfriend = function () {
var lowestfriend = null;
var lowfriends = this.findFriends();
lowfriends.push(this);
var healthrating = 9999;
if (this.health < 2000) {
lowestfriend = this;
}
else {
for (i = 0; i < lowfriends.length; i++) {
var lowfriend = lowfriends[i];
if (lowfriend.health < healthrating && lowfriend.health < lowfriend.maxHealth) {
lowestfriend = lowfriend;
healthrating = lowfriend.health;
}
}
}
if (lowestfriend) {
return lowestfriend;
}
};
this.healcommand = function(paladin) {
var healtarget = this.lowesthealthfriend();
if (healtarget) {
this.command(paladin, "cast", "heal", healtarget);
}
};
loop {
var allies = this.findFriends();
this.executeHeroStrategy();
this.executeAllyStrategy(allies);
}