ss14_chemistry_site/reworked/calc.py

51 lines
1.2 KiB
Python
Raw Normal View History

global expanded
def expand_recipe(recipe, recipes, main = False):
2024-05-01 12:07:43 +00:00
global expanded
if main:
expanded = {}
2024-05-01 12:07:43 +00:00
ok = False
part = 1 # Одна часть
2024-05-01 12:07:43 +00:00
while not ok:
ok = True
2024-05-04 10:20:24 +00:00
vol_in = 0 # Объём мин. рецепта (вход)
# Перебираем элементы
2024-05-01 12:07:43 +00:00
for el in recipe:
# Если составное
2024-05-01 12:07:43 +00:00
if el in recipes:
# Одна часть должна делиться без остатка!
2024-05-01 12:07:43 +00:00
if part % recipes[el].out != 0:
ok = False
part += 1
expanded = {}
break
else:
2024-05-04 10:20:24 +00:00
vol_in += expand_recipe(recipes[el].comps, recipes)
2024-05-01 12:07:43 +00:00
else:
if el in expanded:
expanded[el] += recipe[el]*part
else:
expanded[el] = recipe[el]*part
2024-05-04 10:20:24 +00:00
vol_in += recipe[el]*part
if main:
2024-05-04 10:20:24 +00:00
return expanded, vol_in, part
else:
2024-05-04 10:20:24 +00:00
return vol_in
2024-05-01 12:07:43 +00:00
def calc(element, amount, recipes):
# Получаем характеристику элемента
recipe, vol_out = recipes[element].comps, recipes[element].out
# Расчитываем минимальный рецепт
expanded, vol_in, part = expand_recipe(recipe, recipes, True)
2024-05-01 12:07:43 +00:00
need = amount//vol_in
for i in expanded:
expanded[i] = expanded[i]*need
vol_in *= need
vol_out *= part*need
2024-05-01 12:07:43 +00:00
return expanded, vol_in, vol_out
2024-05-01 12:07:43 +00:00