Skip to content

Commit

Permalink
fix(workshop): Consume rate not applied consistently
Browse files Browse the repository at this point in the history
The consume rate was only applied to resources that have a capacity. The behavior also felt entirely inconsistent.
Now, the consume rate is applied consistently, whenever a part of KS requests to know how much of a resource is available for use.

Fixes #159
  • Loading branch information
oliversalzburg committed Mar 5, 2023
1 parent bab36eb commit c51cc5b
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 51 deletions.
2 changes: 1 addition & 1 deletion packages/userscript/source/ScienceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class ScienceManager extends UpgradeManager {
let prices = UserScript.window.dojo.clone(tech.prices);
prices = this._host.gamePage.village.getEffectLeader("scientist", prices);
for (const resource of prices) {
if (this._workshopManager.getValueAvailable(resource.name, true) < resource.val) {
if (this._workshopManager.getValueAvailable(resource.name) < resource.val) {
continue workLoop;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/userscript/source/SpaceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class SpaceManager implements Automation {
const prices = mustExist(model.model.prices);
for (const resource of prices) {
// If we can't afford this resource price, continue with the next mission.
if (this._workshopManager.getValueAvailable(resource.name, true) < resource.val) {
if (this._workshopManager.getValueAvailable(resource.name) < resource.val) {
continue missionLoop;
}
}
Expand Down
14 changes: 6 additions & 8 deletions packages/userscript/source/TradeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ export class TradeManager implements Automation {
}

const racePanels = this._host.gamePage.diplomacyTab.racePanels;
cultureVal = this._workshopManager.getValueAvailable("culture", true);
cultureVal = this._workshopManager.getValueAvailable("culture");

const embassyBulk: Partial<
Record<
Expand Down Expand Up @@ -365,7 +365,7 @@ export class TradeManager implements Automation {
if (emBulk.val === 0) {
continue;
}
cultureVal = this._workshopManager.getValueAvailable("culture", true);
cultureVal = this._workshopManager.getValueAvailable("culture");
if (cultureVal < emBulk.priceSum) {
cwarn("Something has gone horribly wrong.", emBulk.priceSum, cultureVal);
}
Expand Down Expand Up @@ -428,7 +428,7 @@ export class TradeManager implements Automation {
let refreshRequired = false;

// Get the currently available catpower.
let manpower = this._workshopManager.getValueAvailable("manpower", true);
let manpower = this._workshopManager.getValueAvailable("manpower");
// TODO: These should be checked in reverse order. Otherwise the check for lizards
// can cause the zebras to be discovered at later stages in the game. Then it
// gets to the check for the zebras and doesn't explore again, as they're
Expand Down Expand Up @@ -762,14 +762,12 @@ export class TradeManager implements Automation {
for (const [resource, required] of objectEntries(materials)) {
// If this resource is manpower, the amount of trades it allows is straight forward.
if (resource === "manpower") {
total = this._workshopManager.getValueAvailable(resource, true) / required;
total = this._workshopManager.getValueAvailable(resource) / required;
} else {
// For other resources, use a different path to determine the available resource
// amount.
// TODO: It's unclear how this works
total =
this._workshopManager.getValueAvailable(resource, limited, this.settings.trigger) /
required;
total = this._workshopManager.getValueAvailable(resource) / required;
}

// Set the amount to the lowest amount of possible trades seen yet.
Expand Down Expand Up @@ -903,7 +901,7 @@ export class TradeManager implements Automation {
const materials = this.getMaterials(name);
for (const [resource, amount] of objectEntries<Resource, number>(materials)) {
// Check if we have a sufficient amount of that resource in storage.
if (this._workshopManager.getValueAvailable(resource, true) < amount) {
if (this._workshopManager.getValueAvailable(resource) < amount) {
return false;
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/userscript/source/VillageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,9 @@ export class VillageManager implements Automation {
// Check if we can afford a festival.
const craftManager = this._workshopManager;
if (
craftManager.getValueAvailable("manpower", true) < 1500 ||
craftManager.getValueAvailable("culture", true) < 5000 ||
craftManager.getValueAvailable("parchment", true) < 2500
craftManager.getValueAvailable("manpower") < 1500 ||
craftManager.getValueAvailable("culture") < 5000 ||
craftManager.getValueAvailable("parchment") < 2500
) {
return;
}
Expand Down
44 changes: 13 additions & 31 deletions packages/userscript/source/WorkshopManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class WorkshopManager extends UpgradeManager implements Automation {
prices = this._host.gamePage.village.getEffectLeader("scientist", prices);
for (const resource of prices) {
// If we can't afford this resource price, continue with the next upgrade.
if (this.getValueAvailable(resource.name, true) < resource.val) {
if (this.getValueAvailable(resource.name) < resource.val) {
continue workLoop;
}
}
Expand Down Expand Up @@ -193,7 +193,9 @@ export class WorkshopManager extends UpgradeManager implements Automation {
0 < materialResource.maxValue ||
// For materials that are also crafted, if they have already been crafted to their `max`,
// treat them the same as capped source materials, to avoid the same conflict.
(materialCraft ? materialCraft.max - materialResource.value < 1 : false) ||
(!isNil(materialCraft) && -1 < materialCraft.max
? materialCraft.max - materialResource.value < 1
: false) ||
// Handle the ship override.
(craft.resource === "ship" && this.settings.shipOverride.enabled)
) {
Expand All @@ -205,9 +207,9 @@ export class WorkshopManager extends UpgradeManager implements Automation {

// Quantity of source and target resource currently available.
const availableSource =
this.getValueAvailable(material.resource, true) /
this.getValueAvailable(material.resource) /
mustExist(billOfMaterials.get(material.resource)).length;
const availableTarget = this.getValueAvailable(craft.resource, true);
const availableTarget = this.getValueAvailable(craft.resource);

// How much source resource is consumed and target resource is crafted per craft operation.
const recipeRequires = materialAmount;
Expand Down Expand Up @@ -324,7 +326,7 @@ export class WorkshopManager extends UpgradeManager implements Automation {

const materials = this.getMaterials(name);
for (const [material, amount] of objectEntries(materials)) {
if (this.getValueAvailable(material, true) < amount) {
if (this.getValueAvailable(material) < amount) {
return false;
}
}
Expand Down Expand Up @@ -475,15 +477,9 @@ export class WorkshopManager extends UpgradeManager implements Automation {
* to use.
*
* @param name The resource to check.
* @param all ?
* @param typeTrigger The trigger value associated with this check.
* @returns The available amount of the resource.
*/
getValueAvailable(
name: Resource,
all: boolean | undefined = undefined,
typeTrigger: number | undefined = undefined
): number {
getValueAvailable(name: Resource): number {
// How many items to keep in stock.
let stock = this.getStock(name);

Expand Down Expand Up @@ -512,26 +508,12 @@ export class WorkshopManager extends UpgradeManager implements Automation {
// Subtract the amount to keep in stock.
value = Math.max(value - stock, 0);

// If the user has not requested "all", and this is a capped resource.
// TODO: This makes absolutely no sense. This should likely be a different method.
if (!all && 0 < this.getResource(name).maxValue) {
// Determine our de-facto trigger value to use.
let trigger: number;
if (!typeTrigger && typeTrigger !== 0) {
trigger = this.settings.trigger;
} else {
trigger = typeTrigger;
}

// Determine the consume rate. Either it's configured on the resource, or globally.
// If the consume rate is 0.6, we'll always only make 60% of the resource available.
const resourceSettings = this._host.engine.settings.resources.resources[name];
const consume = resourceSettings.consume;

value -= Math.min(this.getResource(name).maxValue * trigger, value) * (1 - consume);
}
// Determine the consume rate.
// If the consume rate is 0.6, we'll always only make 60% of the resource available.
const resourceSettings = this._host.engine.settings.resources.resources[name];
const consume = resourceSettings.consume;

return value;
return value * consume;
}

/**
Expand Down
11 changes: 4 additions & 7 deletions packages/userscript/source/helper/BulkPurchaseHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export class BulkPurchaseHelper {
// Create a copy of the currently available resources.
const tempPool: Record<Resource, number> = {} as Record<Resource, number>;
for (const res of this._host.gamePage.resPool.resources) {
tempPool[res.name] = this._workshopManager.getValueAvailable(res.name, true);
tempPool[res.name] = this._workshopManager.getValueAvailable(res.name);
}

for (const potentialBuild of potentialBuilds) {
Expand Down Expand Up @@ -578,10 +578,7 @@ export class BulkPurchaseHelper {
0.75
);
const oilPrice = finalResourcePrice * (1 - oilModifier);
if (
this._workshopManager.getValueAvailable("oil", true) <
oilPrice * Math.pow(1.05, build.val)
) {
if (this._workshopManager.getValueAvailable("oil") < oilPrice * Math.pow(1.05, build.val)) {
return false;
}

Expand All @@ -593,14 +590,14 @@ export class BulkPurchaseHelper {
);
const karmaPrice = finalResourcePrice * (1 - karmaModifier);
if (
this._workshopManager.getValueAvailable("karma", true) <
this._workshopManager.getValueAvailable("karma") <
karmaPrice * Math.pow(priceRatio, build.val)
) {
return false;
}
} else {
if (
this._workshopManager.getValueAvailable(price.name, true) <
this._workshopManager.getValueAvailable(price.name) <
finalResourcePrice * Math.pow(priceRatio, build.val)
) {
return false;
Expand Down

0 comments on commit c51cc5b

Please sign in to comment.