-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbp_functions.py
More file actions
212 lines (177 loc) · 6.57 KB
/
bp_functions.py
File metadata and controls
212 lines (177 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
from bp_from_json import entity
import math
# ====================================
def get_iningredients(recipe, recipes, items, amount=1):
ingredients = {}
if recipe:
for i in recipes[recipe]["ingredients"]:
if i["name"] in items:
ingredients[i["name"]] = i["amount"] * amount
return ingredients
# ====================================
def add_filtr_constant_combinator(constant_combinator, type, name, count, index=None):
if index is None:
min_index = 0
for f in constant_combinator.data["control_behavior"]["filters"]:
min_index = max(min_index, f.get("index", 0))
index = min_index + 1
constant_combinator.data["control_behavior"]["filters"].append(
{"signal": {"type": type, "name": name}, "count": count, "index": index}
)
# ====================================
def add_constant_combinator(bp, x, y):
constant_combinator = entity.new_entity("constant-combinator", x, y)
constant_combinator.set("control_behavior", {"filters": []})
bp.append_entity(constant_combinator)
return constant_combinator
# ====================================
def add_machine(bp, name, x, y, recipe, name_of_modules="", number_of_modules=0):
# name = "assembling-machine-2"
assembly = entity.new_entity(name, x, y)
if recipe:
assembly.set("recipe", recipe)
if name_of_modules:
assembly.update_items(
{name_of_modules: number_of_modules}, name_verification=False
)
bp.append_entity(assembly)
return assembly
# ====================================
def add_passive_provider(bp, x, y, bar=None):
passive_provider = entity.new_entity("logistic-chest-passive-provider", x, y)
if bar is not None:
passive_provider.set("bar", bar)
bp.append_entity(passive_provider)
return passive_provider
# ====================================
def add_filter_inserter(
bp, name, x, y, direction, recipe, recipes, circuit_condition=None, connection=None
):
inserter = entity.new_entity(name, x, y)
inserter.set("direction", direction)
if recipe:
r = recipes[recipe]["product"]
f = list()
f.append({"index": 1, "name": r})
inserter.set("filters", f)
if circuit_condition is not None:
inserter.set("control_behavior", circuit_condition)
if connection is not None:
inserter.set("connections", connection)
bp.append_entity(inserter)
return inserter
# ====================================
def add_inserter(bp, name, x, y, direction):
inserter = entity.new_entity(name, x, y)
inserter.set("direction", direction)
bp.append_entity(inserter)
return inserter
# ====================================
def new_connection(entity_number):
return {"1": {"red": [{"entity_id": entity_number}]}}
# ====================================
def new_circuit_condition(recipe, recipes, constant, comparator="<"):
if recipe:
r = recipes[recipe]["product"]
return {
"circuit_condition": {
"first_signal": {"type": "item", "name": r},
"constant": constant,
"comparator": comparator,
}
}
else:
return None
# ====================================
def get_stack_size(recipe, recipes, items):
if recipe:
if recipe in recipes:
item_name = recipes[recipe]["product"]
else:
item_name = recipe
if item_name in items:
return items[item_name]
else:
return 0
else:
return None
# ====================================
def add_logistic_chest_requester(bp, x, y, ingredient=None, func_get_amount=None):
requester = entity.new_entity("logistic-chest-requester", x, y)
if ingredient is not None:
if isinstance(ingredient, tuple):
name, amount = ingredient
amount = func_get_amount(amount, name)
requester.append_request_filters(
{"index": 1, "name": name, "count": amount}
)
elif isinstance(ingredient, list):
for index, ing in enumerate(ingredient, start=1):
name, amount = ing
amount = func_get_amount(amount, name)
requester.append_request_filters(
{"index": index, "name": name, "count": amount}
)
bp.append_entity(requester)
return requester
# ====================================
def update_request_filters(entity, ingredient, func_get_amount):
index = len(entity.read("request_filters")) + 1
if isinstance(ingredient, tuple):
name, amount = ingredient
amount = func_get_amount(amount, name)
entity.append_request_filters({"index": index, "name": name, "count": amount})
elif isinstance(ingredient, list):
for ing in ingredient:
name, amount = ing
amount = func_get_amount(amount, name)
entity.append_request_filters(
{"index": index, "name": name, "count": amount}
)
index += 1
elif isinstance(ingredient, dict):
for name, amount in ingredient.items():
amount = func_get_amount(amount, name)
entity.append_request_filters(
{"index": index, "name": name, "count": amount}
)
index += 1
else:
print("ingredient = ", type(ingredient), ingredient)
raise Exception("unknown type")
# ====================================
def add_entity(bp, name, x, y):
e = entity.new_entity(name, x, y)
bp.append_entity(e)
return e
# ====================================
def get_amount(amount, name, items, full_stack=True):
if full_stack:
return math.ceil(items[name] / 4 / 2) * 4
else:
return math.ceil(amount / 4) * 4
######################################
#
# main
if __name__ == "__main__":
func_list = [
name
for (name, obj) in vars().items()
if hasattr(obj, "__class__") and obj.__class__.__name__ == "function"
]
for line in func_list:
print(f"from bp_functions import {line}")
"""
from bp_functions import get_iningredients
from bp_functions import add_machine
from bp_functions import add_passive_provider
from bp_functions import add_filter_inserter
from bp_functions import add_inserter
from bp_functions import new_connection
from bp_functions import new_circuit_condition
from bp_functions import get_stack_size
from bp_functions import add_logistic_chest_requester
from bp_functions import update_request_filters
from bp_functions import add_entity
from bp_functions import get_amount
"""