Skip to content

Commit

Permalink
Properly(?) cover extraction case where a provider may run out of EMC…
Browse files Browse the repository at this point in the history
… mid-operation
  • Loading branch information
62832 committed Apr 28, 2024
1 parent 16aeaec commit 321ed80
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions src/main/java/gripe/_90/appliede/module/EMCStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,42 +68,47 @@ public long extract(AEKey what, long amount, Actionable mode, IActionSource sour
}

var knowledge = service.getKnowledge();
var extracted = BigInteger.ZERO;
var extracted = 0L;
var multiplier = AppliedE.TIER_LIMIT.pow(emc.getTier() - 1);

var providers = new ArrayList<>(knowledge.getProviders());
Collections.shuffle(providers);

var toExtract = BigInteger.valueOf(amount).multiply(multiplier);
var divisor = BigInteger.valueOf(knowledge.getProviders().size());
var quotient = toExtract.divide(divisor);
var remainder = toExtract.remainder(divisor).longValue();

for (var p = 0; p < providers.size(); p++) {
var provider = providers.get(p);
var currentEmc = provider.getEmc();
var toExtractFrom = quotient.add(p < remainder ? BigInteger.ONE : BigInteger.ZERO);
while (!providers.isEmpty() && extracted < amount) {
Collections.shuffle(providers);

if (currentEmc.compareTo(toExtractFrom) <= 0) {
if (mode == Actionable.MODULATE) {
provider.setEmc(BigInteger.ZERO);
}
var toExtract = BigInteger.valueOf(amount - extracted).multiply(multiplier);
var divisor = BigInteger.valueOf(knowledge.getProviders().size());
var quotient = toExtract.divide(divisor);
var remainder = toExtract.remainder(divisor).longValue();

extracted = extracted.add(currentEmc.divide(multiplier));
} else {
if (mode == Actionable.MODULATE) {
provider.setEmc(currentEmc.subtract(toExtractFrom));
for (var p = 0; p < providers.size(); p++) {
var provider = providers.get(p);
var currentEmc = provider.getEmc();
var toExtractFrom = quotient.add(p < remainder ? BigInteger.ONE : BigInteger.ZERO);

if (currentEmc.compareTo(toExtractFrom) <= 0) {
if (mode == Actionable.MODULATE) {
provider.setEmc(BigInteger.ZERO);
}

extracted += currentEmc.divide(multiplier).longValue();
// provider exhausted, remove from providers and re-extract deficit from remaining providers
providers.remove(provider);
} else {
if (mode == Actionable.MODULATE) {
provider.setEmc(currentEmc.subtract(toExtractFrom));
}

extracted += toExtractFrom.divide(multiplier).longValue();
}

extracted = extracted.add(toExtractFrom.divide(multiplier));
}
}

if (mode == Actionable.MODULATE) {
service.syncEmc();
}

return extracted.min(AppliedE.TIER_LIMIT).longValue();
return extracted;
}

@Override
Expand Down

0 comments on commit 321ed80

Please sign in to comment.