33import re
44import sys
55from collections import OrderedDict , Counter
6+ from pprint import pprint
67
78# We use ceil() at the end of calculations, and don't want floating point error
89# to make us slightly over an integer and cause us to add another for no reason.
910from fractions import Fraction
1011
1112
12- def get_recipes (datafile , module_priorities ):
13+ def get_recipes (datafile , module_priorities , verbose = False ):
1314 """Data file consists of one entry per line. Each entry is either a recipe, building or module.
1415 Building lines look like:
1516 BUILDING builds at SPEED[ with N modules]
@@ -69,6 +70,8 @@ def get_recipes(datafile, module_priorities):
6970 input_amount , input_name = part .split (' ' , 1 )
7071 input_amount = int (input_amount )
7172 inputs [input_name ] = input_amount
73+ if name in items :
74+ raise ValueError ('Recipe for {!r} already declared' .format (name ))
7275 items [name ] = amount , time , building , inputs , prod
7376 continue
7477
@@ -78,6 +81,8 @@ def get_recipes(datafile, module_priorities):
7881 speed = Fraction (speed )
7982 prod = Fraction (prod ) if prod else 0
8083 name = name .lower ()
84+ if name in modules :
85+ raise ValueError ('Module {!r} already declared' .format (name ))
8186 modules [name ] = speed , prod
8287 continue
8388
@@ -102,6 +107,9 @@ def get_recipes(datafile, module_priorities):
102107 inputs = {input_name : Fraction (input_amount ) / amount for input_name , input_amount in inputs .items ()}
103108 results [item ] = building , throughput , inputs , mods
104109
110+ if verbose :
111+ pprint (results )
112+
105113 return results
106114
107115
@@ -155,7 +163,7 @@ def merge_into(a, b):
155163 a [k ] = a .get (k , 0 ) + v
156164
157165
158- def main (item , rate , datafile = 'factorio_recipes' , modules = '' , fractional = False ):
166+ def main (item , rate , datafile = 'factorio_recipes' , modules = '' , fractional = False , verbose = False ):
159167 """Calculate ratios and output number of production facilities needed
160168 to craft a specific output at a specific rate in Factorio.
161169 Requires a data file specifying available recipies and buildings. See source for syntax.
@@ -173,7 +181,7 @@ def main(item, rate, datafile='factorio_recipes', modules='', fractional=False):
173181 - No dependency cycles (eg. coal liquification, Kovarex enrichment)
174182 """
175183 modules = [name .strip ().lower () for name in modules .split (',' )] if modules else []
176- recipes = get_recipes (datafile , modules )
184+ recipes = get_recipes (datafile , modules , verbose )
177185 rate = Fraction (rate )
178186 results = solve (recipes , item , rate )
179187 for item , amount in results .items ():
0 commit comments