Last active
January 5, 2021 04:21
-
-
Save jacobrosenthal/6a4a0616963110793eae890a52f42f5b to your computer and use it in GitHub Desktop.
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() { | |
captureArtifacts(ui.getSelectedPlanet().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 => df.isPlanetMineable(p)) | |
.filter(p => p.owner === "0x0000000000000000000000000000000000000000") | |
.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; | |
} | |
const energyArriving = (candidate.energyCap * 0.15) + (candidate.energy * (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