Just a simple, and painful to use calculator for the game Factorio written in Python
Révision | fb2acd03a0a290adbe4199e47f2f446edd753d63 (tree) |
---|---|
l'heure | 2017-08-06 04:55:22 |
Auteur | Eric Hopper <hopper@omni...> |
Commiter | Eric Hopper |
Next iteration. Factory planning list, actual production estimate.
@@ -28,14 +28,23 @@ | ||
28 | 28 | |
29 | 29 | @property |
30 | 30 | def base_rate(self): |
31 | - base_rate = (self._produced / _F(1,1)) / (self._time / _F(1,1)) | |
32 | - return base_rate | |
31 | + if self._produced is None: | |
32 | + return None | |
33 | + else: | |
34 | + base_rate = (self._produced / _F(1,1)) / (self._time / _F(1,1)) | |
35 | + return base_rate | |
33 | 36 | |
34 | 37 | def factories(self, rate): |
35 | - return rate / self.base_rate | |
38 | + if self._produced is None: | |
39 | + return None | |
40 | + else: | |
41 | + return rate / self.base_rate | |
36 | 42 | |
37 | 43 | def rate_with_factories(self, numfactories): |
38 | - return numfactories * self.base_rate | |
44 | + if self._produced is None: | |
45 | + return None | |
46 | + else: | |
47 | + return numfactories * self.base_rate | |
39 | 48 | |
40 | 49 | class ItemSet(set): |
41 | 50 | def __init__(self, *args, **kargs): |
@@ -77,7 +86,7 @@ | ||
77 | 86 | return 0 |
78 | 87 | produced = dest_item._produced / _F(1,1) |
79 | 88 | scale = rate / produced |
80 | - print(f"name, scale == {dest_item._name}, {scale}") | |
89 | +# print(f"name, scale == {dest_item._name}, {scale}") | |
81 | 90 | total = 0 |
82 | 91 | for sub_item_ct, sub_item in dest_item._ingredients: |
83 | 92 | sub_rate = production_rate(sub_item, scale * sub_item_ct, source_item) |
@@ -87,3 +96,36 @@ | ||
87 | 96 | def how_many_produced(source_item, rate, dest_item): |
88 | 97 | forward_rate = production_rate(dest_item, _F(1,1), source_item) |
89 | 98 | return rate / forward_rate |
99 | + | |
100 | +def factories_for_each(dest_item, rate): | |
101 | + items_so_far = set() | |
102 | + factory_list = [] | |
103 | + def recursive_count(dest_item, rate, cur_source=None): | |
104 | + if cur_source is None: | |
105 | + cur_source = dest_item | |
106 | + if cur_source in items_so_far: | |
107 | + return | |
108 | + items_so_far.add(cur_source) | |
109 | + source_rate = production_rate(dest_item, rate, cur_source) | |
110 | + if cur_source._produced is None: | |
111 | + factory_list.append((None, None, source_rate, cur_source)) | |
112 | + else: | |
113 | + factories = cur_source.factories(source_rate) | |
114 | + int_fact = factories // _F(1,1) | |
115 | + if (factories - int_fact) > 0: | |
116 | + int_fact += 1 | |
117 | + assert(int_fact >= factories) | |
118 | + factory_list.append((int_fact, factories, source_rate, cur_source)) | |
119 | + for _, next_source in cur_source._ingredients: | |
120 | + recursive_count(dest_item, rate, next_source) | |
121 | + recursive_count(dest_item, rate) | |
122 | + return factory_list | |
123 | + | |
124 | +def actual_production(dest_item, factory_list): | |
125 | + def produced_for_each(dest_item, factory_list): | |
126 | + for int_fact, _, _, item in factory_list: | |
127 | + if int_fact is not None: | |
128 | + rate = (_F(int_fact, 1) * item._produced) / item._time | |
129 | + cur_produced = how_many_produced(item, rate, dest_item) | |
130 | + yield cur_produced | |
131 | + return min(produced_for_each(dest_item, factory_list)) |