Skip to content

Commit

Permalink
新增超频计算
Browse files Browse the repository at this point in the history
  • Loading branch information
imsfc committed Sep 22, 2024
1 parent bf5be9b commit 4964b53
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 66 deletions.
22 changes: 22 additions & 0 deletions src/components/ModularFactoryDrawerContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,28 @@ function createColumns({
)
},
},
{
title: t('clockSpeed'),
key: 'clockSpeed',
minWidth: 120,
width: 140,
render: (row) => {
return (
<NInputNumber
value={
row.clockSpeed && new Decimal(row.clockSpeed).mul(100).toNumber()
}
onUpdateValue={(value) => {
row.clockSpeed = value
? new Decimal(value).div(100).toNumber()
: null // todo
}}
min={1}
max={250}
/>
)
},
},
{
title: t('building'),
key: 'building',
Expand Down
1 change: 1 addition & 0 deletions src/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ itemUnitName: unit | units
fluidUnitName:
unitsPerMinute: '{0} {1}/min'
seconds: s
clockSpeed: Clock Speed (%)
1 change: 1 addition & 0 deletions src/locales/zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ itemUnitName: 个
fluidUnitName: 立方米
unitsPerMinute: '每分钟 {0} {1}'
seconds:
clockSpeed: 超频 (%)
152 changes: 89 additions & 63 deletions src/stores/modularFactoryList.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { computed, ref } from 'vue'
import { defineStore } from 'pinia'
import { syncRef, useLocalStorage } from '@vueuse/core'
import Decimal from 'decimal.js'

import type {
AssemblyLine,
Expand All @@ -27,73 +28,97 @@ export const useModularFactoryList = defineStore('modularFactoryList', () => {
const assemblyLineComputedList = computed(() => {
const assemblyLineComputed: Record<Id, AssemblyLineComputed | null> = {}
data.value.forEach(({ data }) => {
data.forEach(({ id, targetItemId, targetItemSpeed, recipeId }) => {
if (recipeId && targetItemId && targetItemSpeed) {
const recipe = getRecipeById(recipeId)
const building = getBuildingById(recipe.producedInId)

// 100%频率下目标物品每次生产数量
const targetItemQuantityPerCycle = recipe.outputs.find(
({ itemId }) => itemId === targetItemId,
)!.quantityPerCycle

// 100%频率下单个建筑每秒产出的目标物品数量
const targetItemQuantityPerMinutePerBuilding =
calculateQuantityPerMinute(
targetItemQuantityPerCycle,
recipe.productionDuration,
data.forEach(
({ id, targetItemId, targetItemSpeed, recipeId, clockSpeed }) => {
if (recipeId && targetItemId && targetItemSpeed && clockSpeed) {
const recipe = getRecipeById(recipeId)
const building = getBuildingById(recipe.producedInId)

// 目标物品每周期生产数量
const targetItemQuantityPerCycle = recipe.outputs.find(
({ itemId }) => itemId === targetItemId,
)!.quantityPerCycle

// 目标物品 100% 频率每分钟生产数量
const targetItemQuantityPerMinutePerBuilding =
calculateQuantityPerMinute(
targetItemQuantityPerCycle,
recipe.productionDuration,
)

// 达到目标产量需要的总频率
const totalClockSpeed = new Decimal(targetItemSpeed)
.div(targetItemQuantityPerMinutePerBuilding)
.toNumber()

// 需要的建筑数量
const buildingQuantity = new Decimal(targetItemSpeed)
.div(
new Decimal(targetItemQuantityPerMinutePerBuilding).mul(
clockSpeed,
),
)
.toNumber()

// 目标物品 100% 频率的功率
const powerUsage =
building.powerUsage === 'variable'
? recipe.powerUsage
: building.powerUsage

// 总功率
const totalPowerUsage =
powerUsage &&
calculateTotalPowerUsage(powerUsage, buildingQuantity, clockSpeed)
const averageTotalPowerUsage =
totalPowerUsage && calculateAveragePowerUsage(totalPowerUsage)

// 流水线总输入
const inputs = recipe.inputs.map(({ itemId, quantityPerCycle }) => {
return {
itemId,
quantityPerMinute: new Decimal(
calculateQuantityPerMinute(
quantityPerCycle,
recipe.productionDuration,
),
)
.mul(totalClockSpeed)
.toNumber(),
}
})

// 流水线总输出
const outputs = recipe.outputs.map(
({ itemId, quantityPerCycle }) => {
return {
itemId,
quantityPerMinute: new Decimal(
calculateQuantityPerMinute(
quantityPerCycle,
recipe.productionDuration,
),
)
.mul(totalClockSpeed)
.toNumber(),
}
},
)

// 100%频率下需要的建筑数量
const buildingQuantity =
targetItemSpeed / targetItemQuantityPerMinutePerBuilding

const powerUsage =
building.powerUsage === 'variable'
? recipe.powerUsage
: building.powerUsage
const totalPowerUsage =
powerUsage && calculateTotalPowerUsage(powerUsage, buildingQuantity)
const averageTotalPowerUsage =
totalPowerUsage && calculateAveragePowerUsage(totalPowerUsage)

// 流水线总输入
const inputs = recipe.inputs.map(({ itemId, quantityPerCycle }) => {
return {
itemId,
quantityPerMinute:
calculateQuantityPerMinute(
quantityPerCycle,
recipe.productionDuration,
) * buildingQuantity,
assemblyLineComputed[id] = {
buildingId: building.id,
buildingQuantity,
powerUsage,
totalPowerUsage,
averageTotalPowerUsage,
inputs,
outputs,
}
})

// 流水线总输出
const outputs = recipe.outputs.map(({ itemId, quantityPerCycle }) => {
return {
itemId,
quantityPerMinute:
calculateQuantityPerMinute(
quantityPerCycle,
recipe.productionDuration,
) * buildingQuantity,
}
})

assemblyLineComputed[id] = {
buildingId: building.id,
buildingQuantity,
powerUsage,
totalPowerUsage,
averageTotalPowerUsage,
inputs,
outputs,
} else {
assemblyLineComputed[id] = null
}
} else {
assemblyLineComputed[id] = null
}
})
},
)
})
return assemblyLineComputed
})
Expand Down Expand Up @@ -136,6 +161,7 @@ export const useModularFactoryList = defineStore('modularFactoryList', () => {
targetItemId: null,
targetItemSpeed: null,
recipeId: null,
clockSpeed: 1,
})
return id
}
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export interface AssemblyLine {
targetItemId: Id | null
targetItemSpeed: number | null
recipeId: Id | null
clockSpeed: number | null
}

export interface AssemblyLineComputed {
Expand Down
10 changes: 7 additions & 3 deletions src/utils/dataCalculators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Decimal } from 'decimal.js'

import type { PowerUsage } from '@/types'

const overclockingPowerIndex = 1.321928

export const calculateQuantityPerMinute = (
quantityPerCycle: number,
productionDuration: number,
Expand All @@ -13,13 +15,15 @@ export const calculateQuantityPerMinute = (
export const calculateTotalPowerUsage = (
powerUsage: PowerUsage,
buildingQuantity: number,
clockSpeed: number,
): PowerUsage => {
const powerMul = new Decimal(clockSpeed).pow(overclockingPowerIndex)
if (typeof powerUsage === 'number') {
return new Decimal(powerUsage).mul(buildingQuantity).toNumber()
return powerMul.mul(powerUsage).mul(buildingQuantity).toNumber()
}
return [
new Decimal(powerUsage[0]).mul(buildingQuantity).toNumber(),
new Decimal(powerUsage[1]).mul(buildingQuantity).toNumber(),
powerMul.mul(powerUsage[0]).mul(buildingQuantity).toNumber(),
powerMul.mul(powerUsage[1]).mul(buildingQuantity).toNumber(),
]
}

Expand Down

0 comments on commit 4964b53

Please sign in to comment.