Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 46 additions & 26 deletions src/resources/2017/AsdonMartin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import {
getAcquirePrice,
getAverageAdventures,
getRange,
have as haveItem,
} from "../../lib.js";
import { $effect, $item, $items } from "../../template-string.js";
Expand Down Expand Up @@ -116,6 +117,46 @@ function tryInsert(item: Item, count: number) {
}
}

function fillWith(
item: Item,
targetUnits: number,
obtain: (amount: number) => void,
) {
while (getFuel() < targetUnits) {
const [, maxFuelFromItem] = getRange(item.adventures);
const unitsNeeded = targetUnits - getFuel();
const minimumFuel = Math.ceil(unitsNeeded / maxFuelFromItem);
obtain(minimumFuel);
const retrievalSuccess = itemAmount(item) >= minimumFuel;
tryInsert(item, minimumFuel);
if (!retrievalSuccess) break;
}
}

const obtainBread = (amount: number) => {
const breadToMake = amount - itemAmount($item`loaf of soda bread`);
if (breadToMake <= 0) return;
if (
npcPrice($item`wad of dough`) === 0 &&
npcPrice($item`all-purpose flower`) > 0
) {
const maxTries = Math.ceil(breadToMake / 35); // minimum amount of wad of dough created from all-purpose flower is 35
for (
let i = 0;
i < maxTries && availableAmount($item`wad of dough`) < breadToMake;
i++
) {
buy($item`all-purpose flower`);
use($item`all-purpose flower`);
}
}
try {
retrieveItem(amount, $item`loaf of soda bread`);
} catch (e) {
logger.debug(`error retrieving soda bread: ${e}`);
}
};

/**
* Fill your Asdon Martin to the given fuel level in the cheapest way possible
*
Expand Down Expand Up @@ -152,35 +193,14 @@ export function fillTo(targetUnits: number): boolean {
: currentPrice);
logger.debug(`price ceiling: ${priceCeiling}`);

const count = Math.ceil(targetUnits / getAverageAdventures(currentFuel));

withProperty("autoBuyPriceLimit", priceCeiling, () =>
retrieveItem(count, currentFuel),
fillWith(currentFuel, targetUnits, (amount) =>
withProperty("autoBuyPriceLimit", priceCeiling, () =>
retrieveItem(amount, currentFuel),
),
);

tryInsert(currentFuel, count);
}
} else {
const fuel = $item`loaf of soda bread`;
const count = Math.ceil(targetUnits / getAverageAdventures(fuel));

const breadToMake = count - itemAmount(fuel);
if (
npcPrice($item`wad of dough`) === 0 &&
npcPrice($item`all-purpose flower`) > 0
) {
const maxTries = Math.ceil(breadToMake / 35); // minimum amount of wad of dough created from all-purpose flower is 35
for (
let i = 0;
i < maxTries && availableAmount($item`wad of dough`) < breadToMake;
i++
) {
buy($item`all-purpose flower`);
use($item`all-purpose flower`);
}
}
retrieveItem(count, fuel);
tryInsert(fuel, count);
fillWith($item`loaf of soda bread`, targetUnits, obtainBread);
}
return getFuel() >= targetUnits;
}
Expand Down
Loading