-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_construction_train.py
More file actions
323 lines (266 loc) · 9.58 KB
/
example_construction_train.py
File metadata and controls
323 lines (266 loc) · 9.58 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
"""
construction_train
"""
from bp_from_json import blueprint
from bp_from_json import dict_bp
from bp_from_json import entity
from bp_from_json import get_items
import sys
import os
import argparse
import math
import uuid
import enum
#############################################
def debug(*args):
if opt.d:
print(*args, file=sys.stderr, flush=True)
#############################################
@enum.unique
class type_of_train(enum.Enum):
requester_trains = 1
filtered_trains = 2
#############################################
def get_y(train_number):
return train_number * 6
#############################################
def add_train(bp, train_number, locomotives, cars, station_name):
train_car_position = 0
train_length = (locomotives + cars) * 7 - 1
number_of_rails = math.ceil(train_length / 2) + 2
for i in range(number_of_rails):
rail = entity.new_entity(
"straight-rail", i * 2 - 1, get_y(train_number) + 1, direction=2
)
bp.append_entity(rail)
train_stop = entity.new_entity(
"train-stop", 7 * train_car_position + 1, get_y(train_number) - 1, direction=6
)
train_stop.set_station(station_name)
bp.append_entity(train_stop)
for i in range(locomotives):
locomotive = entity.new_entity(
"locomotive",
7 * train_car_position + 4,
get_y(train_number) + 1,
orientation=0.75,
)
locomotive.update_items({"nuclear-fuel": 3}, name_verification=False)
bp.append_entity(locomotive)
train_car_position += 1
return train_car_position
#############################################
def create_wagon(train_car_position, train_number):
return entity.new_entity(
"cargo-wagon",
7 * train_car_position + 4,
get_y(train_number) + 1,
orientation=0.75,
)
#############################################
def wagon_close_slots(cargo_wagon, slot_count):
if slot_count < 40:
cargo_wagon.set_inventory_bar(slot_count)
while slot_count < 40:
cargo_wagon.set_inventory_filter(
{"index": slot_count + 1, "name": "linked-chest"}
)
slot_count += 1
#############################################
def append_chests(bp, filtrs, train_car_position, train_number, items):
for i, (key, val) in enumerate(filtrs.items()):
if i <= 5:
x1 = 7 * train_car_position + 1.5 + i
y1 = get_y(train_number) - 0.5
y2 = get_y(train_number) - 1.5
d = 1
elif i <= 11:
x1 = 7 * train_car_position + 1.5 + i - 6
y1 = get_y(train_number) + 2.5
y2 = get_y(train_number) + 3.5
d = 4
inserter = entity.new_entity("stack-inserter", x1, y1)
inserter.set("direction", d)
bp.append_entity(inserter)
requester = entity.new_entity(
"logistic-chest-requester",
x1,
y2,
)
requester.append_request_filters({"index": 1, "name": key, "count": items[key]})
requester.set_request_from_buffers("true")
bp.append_entity(requester)
filtrs.clear()
#############################################
def requester_trains(
bp, contents, train_number, train_car_position, locomotives, cars, station_name
):
cargo_wagon = create_wagon(train_car_position, train_number)
slot_count = 0
items = get_items()
for item, amount in contents.items():
stack_size = items[item]
if item == "landfill":
# for landfill, we start a new train,
# so it's easier to remove these trains from the bp
bp.append_entity(cargo_wagon)
train_number += 1
train_car_position = add_train(
bp, train_number, locomotives, cars, station_name
)
slot_count = 0
cargo_wagon = create_wagon(train_car_position, train_number)
while amount > 0:
slots = math.ceil(amount / stack_size)
if slot_count + slots > 40:
add_items = (40 - slot_count) * stack_size
amount -= add_items
cargo_wagon.update_items({item: add_items}, name_verification=False)
# Add a new wagon
train_car_position += 1
if train_car_position >= locomotives + cars:
train_number += 1
train_car_position = add_train(
bp, train_number, locomotives, cars, station_name
)
bp.append_entity(cargo_wagon)
slot_count = 0
cargo_wagon = create_wagon(train_car_position, train_number)
else:
cargo_wagon.update_items({item: amount}, name_verification=False)
amount = 0
slot_count += slots
bp.append_entity(cargo_wagon)
bp.set_icons(1, "virtual", "signal-B")
bp.set_icons(2, "item", "construction-robot")
#############################################
def filtered_trains(
bp, contents, train_number, train_car_position, locomotives, cars, station_name
):
cargo_wagon = create_wagon(train_car_position, train_number)
slot_count = 0
filtrs = dict_bp()
items = get_items()
for item, amount in contents.items():
stack_size = items[item]
slots = math.ceil(amount / stack_size)
if item == "landfill":
# for landfill, we start a new train,
# so it's easier to remove these trains from the bp
wagon_close_slots(cargo_wagon, slot_count)
append_chests(bp, filtrs, train_car_position, train_number, items)
bp.append_entity(cargo_wagon)
train_number += 1
train_car_position = add_train(
bp, train_number, locomotives, cars, station_name
)
slot_count = 0
cargo_wagon = create_wagon(train_car_position, train_number)
for _ in range(slots):
new_item = item not in filtrs
if slot_count >= 40 or (len(filtrs) >= 12 and new_item):
# Add a new wagon
wagon_close_slots(cargo_wagon, slot_count)
append_chests(bp, filtrs, train_car_position, train_number, items)
train_car_position += 1
if train_car_position >= locomotives + cars:
train_number += 1
train_car_position = add_train(
bp, train_number, locomotives, cars, station_name
)
bp.append_entity(cargo_wagon)
slot_count = 0
cargo_wagon = create_wagon(train_car_position, train_number)
cargo_wagon.set_inventory_filter({"index": slot_count + 1, "name": item})
filtrs += {item: 1}
slot_count += 1
wagon_close_slots(cargo_wagon, slot_count)
append_chests(bp, filtrs, train_car_position, train_number, items)
bp.append_entity(cargo_wagon)
bp.set_icons(1, "virtual", "signal-B")
bp.set_icons(2, "item", "logistic-chest-requester")
#############################################
def get_bp(locomotives, cars, contents, station_name, type_of_Train):
bp = blueprint.new_blueprint()
train_number = 0
train_car_position = add_train(bp, train_number, locomotives, cars, station_name)
if type_of_Train == type_of_train.requester_trains:
requester_trains(
bp,
contents,
train_number,
train_car_position,
locomotives,
cars,
station_name,
)
elif type_of_Train == type_of_train.filtered_trains:
filtered_trains(
bp,
contents,
train_number,
train_car_position,
locomotives,
cars,
station_name,
)
bp.set_label_color(1, 0, 1)
bp.set_label(f"{locomotives}-{cars} construction_train")
print("==================================")
print(str(type_of_Train))
print(bp.to_str())
print("==================================")
######################################
#
# main
if __name__ == "__main__":
exchange_str = ""
parser = argparse.ArgumentParser(
description="example: python construction_train.py"
)
parser.add_argument(
"-d", "--debug", action="store_true", dest="d", help="debug output on STDERR"
)
opt = parser.parse_args()
locomotives = input("locomotives:")
cars = input("cars:")
exchange_str = input("bp to be built:(string or filename.txt)")
if os.path.exists(exchange_str):
bp = blueprint.from_file(exchange_str)
else:
bp = blueprint.from_string(exchange_str)
necessary_items_for_construction = bp.get_all_items()
print("\nbp contains:")
print(necessary_items_for_construction)
# "item name": amount
additional_items = dict_bp(
{
"construction-robot": 1350,
"logistic-robot": 350,
"radar": 50,
"repair-pack": 100,
"cliff-explosives": 100,
"laser-turret": 50,
}
)
print("\nadditional items:")
print(additional_items)
contents = additional_items + necessary_items_for_construction
contents += bp.get_all_tiles()
debug("\ncontents:")
debug(contents)
station_name = str(uuid.uuid4())
get_bp(
int(locomotives),
int(cars),
contents,
station_name,
type_of_train.requester_trains,
)
get_bp(
int(locomotives),
int(cars),
contents,
station_name,
type_of_train.filtered_trains,
)