Skip to content

Commit

Permalink
Merge pull request #112 from tvwenger/byproduct_bugfix
Browse files Browse the repository at this point in the history
fix byproduct bug
  • Loading branch information
tvwenger authored Jan 5, 2022
2 parents 974733d + a76f555 commit 8ea3da5
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 24 deletions.
38 changes: 36 additions & 2 deletions src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,29 @@ function handleByproducts(factory: FactoryGraph) {
}

for (const [byproduct, quantity] of container.recipe.byproducts.entries()) {
// maximum transfer rate supported by a single transfer unit
const maxTransferRate = byproduct.transferBatchSize / byproduct.transferTime

// Check if this container already has a transfer unit for the byproduct
let found = false
for (const consumer of container.consumers) {
if (isTransferUnit(consumer) && consumer.item == byproduct) {
found = true
// ensure that the transfer rate is set
consumer.setTransferRate(container, container.ingress(consumer.item))
consumer.setTransferRate(container, container.ingress(byproduct))
// ensure that we do not exceed one transfer unit for this byproduct
// THIS IS A HACK
if (consumer.requiredTransferRate > maxTransferRate) {
for (const tuContainer of consumer.inputs) {
const share = container.ingress(byproduct) / maxTransferRate
consumer.setTransferRate(
tuContainer,
share * container.ingress(byproduct),
)
}
consumer.requiredTransferRate = maxTransferRate
}
break
}
}
if (found) {
Expand All @@ -84,9 +100,21 @@ function handleByproducts(factory: FactoryGraph) {
const transferUnits = factory.getByproductTransferUnits(byproduct)
for (const transferUnit of transferUnits) {
if (transferUnit.canAddIncomingLink) {
foundTransferUnit = true
transferUnit.addInput(container)
transferUnit.increaseTransferRate(container, container.ingress(byproduct))
foundTransferUnit = true
// ensure that we do not exceed one transfer unit for this byproduct
// THIS IS A HACK
if (transferUnit.requiredTransferRate > maxTransferRate) {
for (const tuContainer of transferUnit.inputs) {
const share = container.ingress(byproduct) / maxTransferRate
transferUnit.setTransferRate(
tuContainer,
share * container.ingress(byproduct),
)
}
transferUnit.requiredTransferRate = maxTransferRate
}
break
}
}
Expand Down Expand Up @@ -137,6 +165,12 @@ function handleByproducts(factory: FactoryGraph) {
transferUnit.increaseRequiredTransferRate(container.ingress(byproduct))
transferUnit.addInput(container)
transferUnit.increaseTransferRate(container, container.ingress(byproduct))
// ensure that we do not exceed one transfer unit for this byproduct
// THIS IS A HACK
if (transferUnit.requiredTransferRate > maxTransferRate) {
transferUnit.setTransferRate(container, maxTransferRate)
transferUnit.requiredTransferRate = maxTransferRate
}
}
}
}
Expand Down
34 changes: 15 additions & 19 deletions src/items.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Item from "antd/lib/list/Item"
import { ItemRender } from "antd/lib/upload/interface"
import { TALENTS, TalentSubject, TalentType } from "./talents"

export type Liter = number
Expand Down Expand Up @@ -278,15 +280,13 @@ export function getRecipe(item: Item, talentLevels: { [key: string]: number }) {
// Create and return modified recipe
const quantity = oldRecipe.quantity * (1.0 + output_mod)
const time = oldRecipe.time * (1.0 - time_mod)
const ingredients = oldRecipe.ingredients
for (const [key, value] of ingredients.entries()) {
const ingredients: Map<Item, number> = new Map()
for (const [key, value] of oldRecipe.ingredients.entries()) {
ingredients.set(key, value * (1.0 - input_mod))
}
const byproducts = oldRecipe.byproducts
for (const [key, value] of byproducts.entries()) {
if (!isCatalyst(key)) {
byproducts.set(key, value * (1.0 + output_mod))
}
const byproducts: Map<Item, number> = new Map()
for (const [key, value] of oldRecipe.byproducts.entries()) {
byproducts.set(key, value * (1.0 + output_mod))
}
const newRecipe = recipe(item, quantity, time, oldRecipe.industry, byproducts, ingredients)
return newRecipe
Expand Down Expand Up @@ -349,19 +349,15 @@ export function getRequiredOres(
ores[ingredient.name] = 0
}
ores[ingredient.name] += ingredientQuantity / batchSize
} else if (requiredOres[ingredient.name] !== undefined) {
// Use already calculated values
for (const [ore, oreQuantity] of Object.entries(requiredOres[ingredient.name])) {
if (ores[ore] === undefined) {
ores[ore] = 0
}
ores[ore] += (oreQuantity * ingredientQuantity) / batchSize
}
} else {
// Recursively call this function
const ingredientOres = getRequiredOres(ingredient, requiredOres, talentLevels)
requiredOres[ingredient.name] = ingredientOres
for (const [ore, oreQuantity] of Object.entries(ingredientOres)) {
// Use already calculated values if possible
let ingredientOres = requiredOres[ingredient.name]
if (ingredientOres === undefined) {
// Recursively call this function
ingredientOres = getRequiredOres(ingredient, requiredOres, talentLevels)
requiredOres[ingredient.name] = ingredientOres
}
for (const [ore, oreQuantity] of Object.entries(requiredOres[ingredient.name])) {
if (ores[ore] === undefined) {
ores[ore] = 0
}
Expand Down
3 changes: 2 additions & 1 deletion src/ui/info.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ export function Info({ setAppState }: InfoProps) {
</ul>
<b>Note:</b> You can also select the factory production list using a comma separated
values (CSV) file. Create a plain text file formatted like:
<pre>Item Name, Number Produced per Day, Maintain Value</pre>. For example:
<pre>Item Name, Number Produced per Day, Maintain Value</pre>
For example:
<pre>
Container S, 10, 50
<br />
Expand Down
32 changes: 30 additions & 2 deletions src/ui/render-factory.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from "react"
import { Button, Row, Col, Table, Space, Divider } from "antd"
import { Button, Row, Col, Table, Space, Divider, Popover } from "antd"
import { Category, Tier, Item, CONTAINERS_ASCENDING_BY_CAPACITY, getRequiredOres } from "../items"
import { FactoryGraph } from "../graph"
import { FactoryState } from "./factory"
Expand Down Expand Up @@ -194,6 +194,7 @@ export function FactoryVisualization({
const schematicCount: { [key: string]: number } = {}

// Get ore values
const requiredOres: { [key: string]: { [key: string]: number } } = {}
const oreValues: { [key: string]: number } = {}

if (factory !== undefined) {
Expand Down Expand Up @@ -239,7 +240,6 @@ export function FactoryVisualization({
})

// get ore requirements per item
const requiredOres: { [key: string]: { [key: string]: number } } = {}
selection.map((item) => {
const ores = getRequiredOres(item, requiredOres, factory.talentLevels)
requiredOres[item.name] = ores
Expand Down Expand Up @@ -332,6 +332,34 @@ export function FactoryVisualization({
title: "Item",
dataIndex: "item",
key: "item",
render: (value: string) => {
const content = (
<div>
{Object.keys(requiredOres[value]).map((ore) => (
<p key={ore}>
{ore +
": " +
Math.round(requiredOres[value][ore]) +
" @ " +
Math.round(orePrices[ore]) +
" quanta/L = " +
Math.round(requiredOres[value][ore] * orePrices[ore]) +
" quanta"}
</p>
))}
</div>
)
return (
<Popover
placement="topLeft"
title={value}
content={content}
trigger="hover"
>
<a>{value}</a>
</Popover>
)
},
},
{
title: "Ore Value",
Expand Down

0 comments on commit 8ea3da5

Please sign in to comment.