-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.py
150 lines (128 loc) · 4.56 KB
/
compile.py
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
import csv
from draftsman.blueprintable import Blueprint
from draftsman.constants import Direction
from draftsman.data import items
from draftsman.entity import ConstantCombinator
from draftsman.entity import DeciderCombinator
from draftsman.data import signals
debug = False
hide_signals = []
filename = "program.csv"
def CreateCell(all_signals, x, y):
cell_select = DeciderCombinator()
cell_select.direction = Direction.WEST
cell_select.tile_position = (x, y)
cell_select.id = "{}_{}_select".format(x, y);
cell_select.set_decider_conditions("signal-I", "=", 1000001 + y, "signal-everything")
x += 2
cell_data = ConstantCombinator()
cell_data.direction = Direction.WEST
cell_data.tile_position = (x, y)
cell_data.position = (x, y)
cell_data.id = "{}_{}_data".format(x, y);
cells = [cell_data]
count = 0
for key in all_signals.keys():
cell_data.set_signal(count, key, all_signals[key])
count += 1
if count == 20:
x += 1
count = 0
cell_data = ConstantCombinator()
cell_data.direction = Direction.WEST
cell_data.tile_position = (x, y)
cell_data.position = (x, y)
cell_data.id = "{}_{}_data".format(x, y);
cells.append(cell_data)
if count == 0:
cells.pop()
return [cell_select, *cells]
def ToSigned(x):
x= x& 0xFFFFFFFF
if (x& 0x80000000) != 0:
x -= 1
x = (~x) & 0xFFFFFFFF
x = -x
return x
def ParseProgram(blueprint):
keys = [*hide_signals, *signals.item, *signals.fluid, *signals.virtual]
keys.remove("signal-I")
keys.remove("signal-W")
keys.remove("signal-X")
signals_count = 0
with open(filename) as f:
render = csv.reader(f)
# header_row = next(render)
# print(header_row)
x, y = 0, 0
idx = 0
all_signals = {}
for row in render:
cnt = 0
tmp = 0
for item in row:
try:
tmp = (tmp << 8) + int(item)
if debug:
print("tmp> ", tmp)
cnt = cnt+1
if cnt == 4:
all_signals[keys[idx]] = ToSigned(tmp)
cnt = 0
tmp = 0
if debug:
print(idx, tmp, hex(tmp), (keys[idx]))
print("all_signals> ", all_signals)
idx += 1
if idx >= len(keys):
idx = 0
cell = CreateCell(all_signals, x, y)
for item in cell:
blueprint.entities.append(item)
signals_count += len(all_signals)
all_signals = {}
y += 1
except ValueError:
# do nothing
pass
signals_count += len(all_signals)
cell = CreateCell(all_signals, x, y)
for item in cell:
blueprint.entities.append(item)
return signals_count
if __name__ == "__main__":
keys = [*hide_signals, *signals.item, *signals.fluid, *signals.virtual]
cnt = 1
for key in keys:
# print(cnt, ": ", key)
cnt += 1
cnt = 1
for item in items.raw:
if "flags" in items.raw[item]:
if "hidden" in items.raw[item]["flags"]:
continue
if items.raw[item]["name"] in keys:
continue
print(cnt, ": ", items.raw[item]["name"])
cnt += 1
blueprint = Blueprint()
signals_count= ParseProgram(blueprint)
print("signals_count: ", signals_count)
row = int((signals_count + 259) / 259)
y = 0
for id in range(row):
combinator_cnt = int((signals_count + 19) / 20)
select_id = "{}_{}_select".format(0, y)
for x in range(combinator_cnt):
data_id = "{}_{}_data".format(x + 2, y)
prev_id = "{}_{}_data".format(x + 2 - 1, y)
# print(x, prev_id, data_id)
if x == 0:
blueprint.add_circuit_connection("green", select_id, data_id, 1, 1)
else:
blueprint.add_circuit_connection("green", prev_id, data_id, 1, 1)
#blueprint.add_circuit_connection("green", prev_select_id, select_id, 2, 2)
#blueprint.add_circuit_connection("red", prev_select_id, select_id, 1, 1)
y += 1
signals_count -= 259
print(blueprint.to_string())