So guys, I was looking at how people come up with beautiful solutions for positioning their archers to attack enemies, and I’ve found this code by user “trotod” in the level Mixed Unit Tactics (in the leaderboards), I was wondering could anyone explain these 2 sections in the code? (I wrote in bold under them) Thanks in advance for any answers
// Practice using modulo to loop over an array
// jshint asi:true, eqnull:true
// Choose the mix and order of units you want to summon by populating this array:
var summonTypes = ['archer', 'archer', 'soldier', 'soldier', 'soldier', 'soldier', 'soldier'];
this.summonTroops = function() {
// Use % to wrap around the summonTypes array based on this.built.length
var type = summonTypes[this.built.length % summonTypes.length];
if (this.gold >= this.costOf(type)) this.summon(type);
//this.say("I should summon troops!");
};
this.findFarthest = function (units) {
var farthest
for (var i = 0, len = units.length; i < len; i++) {
if (!farthest || this.distanceTo(farthest) < this.distanceTo(units[i])) {
farthest = units[i]
}
}
return farthest
}
this.commandSoldier = function (soldier) {
var enemy = soldier.findNearest(this.findEnemies().filter(function (e) { return e.type !== 'palisade'; }));****
**var farthestArcher = this.findFarthest(this.findByType('archer'))**
if (enemy) {
this.command(soldier, 'attack', enemy);
}
else if (farthestArcher && soldier.pos.x < farthestArcher.pos.x + 3) {
this.command(soldier, 'move', { x: farthestArcher.pos.x + 3, y: soldier.pos.y })
}
else if (soldier.pos.x < 40) {
this.command(soldier, 'move', { x: 40, y: soldier.pos.y })
}
else this.command(soldier, 'defend', soldier.pos);
};
what is this .filter(function(e)) thing doing? is it what they call “a closure”?
this.commandArcher = function (archer) {
var enemy = this.findNearest(this.findEnemies().filter(function (e) { return e.type !== 'palisade'; }));
if (enemy) this.command(archer, 'attack', enemy);
else this.command(archer, 'defend', archer.pos);
};
this.commandTroops = function () {
var friends = this.findFriends();
for (var i = 0, l = friends.length; i < l; i++) {
var friend = friends[i];
if (friend.type === 'soldier') this.commandSoldier(friend);
else if (friend.type === 'archer') this.commandArcher(friend);
}
};
this.gatherGold = function () {
var bestPos = getBestPos(this.pos, this.findItems())
if (bestPos) this.move(bestPos)
};
loop {
this.summonTroops();
this.commandTroops();
this.gatherGold();
}
function computeGravitationalForce (p1, p2, mass) {
var distance = p1.distance(p2)
var magnitude = mass / (distance * distance)
var direction = Vector.normalize(Vector.subtract(p1, p2))
var force = Vector.multiply(direction, magnitude)
return force
}
function computeNetForce (forces) {
var force = new Vector(0, 0)
for (var i = 0, len = forces.length; i < len; i++) {
force = Vector.add(force, forces[i])
}
return force
}
function getBestPos (origin, coins) {
var forces = []
if (!coins.length) return
for (var i = 0, len = coins.length; i < len; i++) {
var coin = coins[i]
forces.push(computeGravitationalForce(coin.pos, origin, coin.value))
}
var bestPos = Vector.add(origin, Vector.multiply(Vector.normalize(computeNetForce(forces)), 8))
return bestPos
}
W H A T the hell just happened here? - computerNetForce?, computerGravitationalForce?