Skip to content

Instantly share code, notes, and snippets.

@jacobrosenthal
Last active January 5, 2021 06:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacobrosenthal/bb3dc76ef4fe58060c1df1fb5aad39b7 to your computer and use it in GitHub Desktop.
Save jacobrosenthal/bb3dc76ef4fe58060c1df1fb5aad39b7 to your computer and use it in GitHub Desktop.
global war on xmas
class Plugin {
/**
* A constructor can be used to keep track of information.
*/
constructor() {
const planets = df.getMyPlanets();
for (const planet of planets) {
captureArtifacts(planet.locationId, 80);
}
}
}
plugin.register(new Plugin());
function captureArtifacts(fromId, maxDistributeEnergyPercent) {
const to = df.getPlanetWithId(fromId);
const from = df.getPlanetWithId(fromId);
// Rejected if has pending outbound moves
const unconfirmed = df.getUnconfirmedMoves().filter(move => move.from === fromId)
if (unconfirmed.length !== 0) {
//console.log("rejecting candidate for having unconfirmed outbound moves");
return;
}
const candidates_ = df.getPlanetsInRange(fromId, maxDistributeEnergyPercent)
.filter(p => p.owner !== df.account)
.filter(p => df.isPlanetMineable(p))
.filter(p => !p.hasTriedFindingArtifact)
.map(to => {
return [to, distance(from, to)]
})
.sort((a, b) => a[1] - b[1]);
let i = 0;
const energyBudget = Math.floor((maxDistributeEnergyPercent / 100) * to.energy);
//console.log("energyBudget ", energyBudget);
let energySpent = 0;
while (energyBudget - energySpent > 0 && i < candidates_.length) {
const energyLeft = energyBudget - energySpent;
// Remember its a tuple of candidates and their distance
const candidate = candidates_[i++][0];
//console.log(candidate);
// Rejected if has unconfirmed pending arrivals
const moves = df.getUnconfirmedMoves().filter(move => move.to === candidate.locationId)
if (moves.length !== 0) {
//console.log("rejecting candidate for having unconfirmed moves");
continue;
}
// Rejected if has pending arrivals
const arrivals = getArrivalsForPlanet(candidate.locationId);
if (arrivals.length !== 0) {
//console.log("rejecting candidate for having arrivals");
continue;
}
//overkill assume theyre at full when we get there
const energyArriving = (candidate.energyCap * 0.15) + (candidate.energyCap * (candidate.defense / 100));
// needs to be a whole number for the contract
const energyNeeded = Math.ceil(df.getEnergyNeededForMove(fromId, candidate.locationId, energyArriving));
if (energyLeft - energyNeeded < 0) {
//console.log("rejecting candidate energyNeeded ", energyNeeded, " exceeds energyLeft ", energyLeft);
continue;
}
console.log(`df.move("${fromId}","${candidate.locationId}",${energyNeeded},0)`);
df.move(fromId, candidate.locationId, energyNeeded, 0);
energySpent += energyNeeded;
}
}
function getArrivalsForPlanet(planetId) {
return df.getAllVoyages().filter(arrival => arrival.toPlanet === planetId);
}
//returns tuples of [planet,distance]
function distance(from, to) {
let fromloc = from.location;
let toloc = to.location;
return Math.sqrt((fromloc.coords.x - toloc.coords.x) ** 2 + (fromloc.coords.y - toloc.coords.y) ** 2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment