Last active
January 5, 2021 06:38
-
-
Save jacobrosenthal/bb3dc76ef4fe58060c1df1fb5aad39b7 to your computer and use it in GitHub Desktop.
global war on xmas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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