Hello,

I’m working on Zero Sums and other looting arena. I would like to have a feed back on the code below. You can copy paste the entire code in ZeroSums, and that would work.

I’ve been working with the console open, and I’ve been monitoring the computational time per frame, and I tried to improve it. I also tried to make it as clear as possible to understand.

Explainations :

The idea is to find a good path to your next coin. Your next coin has to :

Be nearby

Have a good value. Both of these are taken into account in // (1) (see code)

Be closer to me than to my opponent (because he’ll grab it first). This is // (2)

Prevent me to go to a “low gold” zone. This is both the // (3) and the // (0).

This last condition is expensive in computation, and only useful from time to time. It happened to me that I’ve been stuck in a path away from my own golden shower, going into a corner of the battlefield with very low income, and sometimes this is the difference between victory or death. Also I like the maths behind it, because I think it’s efficient mathematically.

The “no low gold zone” is achieved as explained in my other post here

I think the way I wrote it is fairly efficient. Maybe there are better ways, but I was somewhat happy with the if-condition // (3) because it filters out a lot of non necessary processing time. The computation of the goldBarycenter could also be computed upstream (and not every frame, as it doesn’t move a lot).

Feel free to give any feedback, and/or to use my code (or part of it) in your arenas to loot coins !

```
var pender=this.findNearest(this.findEnemies());
loop {
var allCoins=this.findByType("coin");
var nextCoin=findNextCoin(this);
this.move(nextCoin.pos);
}
function findNextCoin(parThis){
var leastCoinDistWeighted=100;
var leastCoinDistWeighted2=100;
var myPos={x:parThis.pos.x,y:parThis.pos.y};
//Computing the barycenter of the coins, and a vector related // (0)
var tempX=0; var tempY=0; var totalValue=0;
for(var iCoin=0;iCoin<allCoins.length;iCoin++){
var currCoin=allCoins[iCoin];
var currValue=currCoin.value;
tempX+=currCoin.pos.x*currValue;
tempY+=currCoin.pos.y*currValue;
totalValue+=currValue;
}
var goldBary={x:tempX/totalValue,y:tempY/totalValue};
var v1={x:goldBary.x-myPos.x,y:goldBary.y-myPos.y};
var v1Norm=Math.sqrt(v1.x*v1.x+v1.y*v1.y);
// Deciding which coin to pick next
for(iCoin=0;iCoin<allCoins.length;iCoin++){
currCoin=allCoins[iCoin];
var currCoinDist=parThis.distanceTo(currCoin);
var currCoinDistWeighted=currCoinDist/currCoin.value;
if (currCoinDistWeighted < leastCoinDistWeighted) {// (1)
if(pender && currCoin.distanceTo(pender) > currCoinDist){ //(2)
leastCoinDistWeighted=currCoinDistWeighted;
if(leastCoinDistWeighted<15){// (3)
var v2={x:currCoin.pos.x-myPos.x,y:currCoin.pos.y-myPos.y};
var v2Norm=Math.sqrt(v2.x*v2.x+v2.y*v2.y);
var cosineV1V2=(v1.x*v2.x+v1.y*v2.y)/(v1Norm*v2Norm);
var coefTowardGoldBary=3+cosineV1V2;
var currCoinDistWeighted2=currCoinDistWeighted/coefTowardGoldBary;
if(currCoinDistWeighted2<leastCoinDistWeighted2){
leastCoinDistWeighted2=currCoinDistWeighted2;
nextCoin=currCoin;
}
}else{
nextCoin=currCoin;
} //(3)
} //(2)
} //(1)
} //(for iCoins)
return nextCoin;
}
```