Repository navigation
Expand file tree
/
Copy pathemplacement.cpp
More file actions
387 lines (295 loc) · 9.6 KB
/
Copy pathemplacement.cpp
File metadata and controls
387 lines (295 loc) · 9.6 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include "bl.hpp"
#include "detail.hpp"
#include <algorithm>
#include <ranges>
#include <vector>
// Same as `bl::block_wire` except that world IDs are used to represent the
// blocks instead of unique IDs, since the game uses world IDs for wires.
class pending_wire {
public:
std::size_t from_blk{};
std::size_t to_blk{};
char from_con{};
char to_con{};
};
static auto gen_world_ids(bl::emplacement const* em) {
// World IDs don't *have* to begin at 1.
std::size_t world_id{};
std::unordered_map<bl::unique_id, std::size_t> world_ids{};
for (auto const& item: em->blks) { world_ids[item.first] = world_id++; }
return world_ids;
}
bl::emplacement::emplacement(std::string_view src) {
stream main{src};
std::unordered_map<std::size_t, unique_id> ids{};
std::vector<pending_wire> pending_wires{};
while (!main.eof()) {
stream blk_beg{main};
main.read_until(";");
// Subsection of the `main` stream containing the current block.
stream blk{
{blk_beg.i, main.i}
};
// Munch the `;`, if there was any.
main.read_single();
std::array<char, 3> encoded_name{blk.read_single().value()};
if (is_one_of(encoded_name[0], "#%")) {
encoded_name[1] = blk.read_single().value();
}
stream encoded_beg{blk};
blk.read_until("/=>^");
// Subsection of the `blk` stream containing the encoded name,
// position, rotation, color, and material.
stream encoded{
{encoded_beg.i, blk.i}
};
block info{};
info.name = decode_block_name(encoded_name.data());
info.pos = encoded.read<4>().value();
auto traits{block_name_traits(info.name)};
// Let's say that we have a block `G$BwfA3`, where the rotation
// and material were specified but the color was not. Well,
// then...
// ...the encoded rotation would be read successfully because
// `A3` is left in the stream, which is enough to read the `A`
// and decode it into a rotation. This results in the default
// rotation (`A`) being used, which is correct. Then...
info.rot = encoded.read_single().value_or(rotation().encode());
// ...the encoded color could not be read because only `3` is
// left in the `encoded` stream, which is not enough for a
// complete encoded color. This results in a default color being
// used instead, which is correct. Then...
info.color = encoded.read<4>().value_or(vec3(255).encode());
auto default_encoded_mat{
material_traits(material::eDefault).encoded
};
// ...the encoded material would be read successfuly because `3`
// is enough to decode it into a material. This results in the
// material corresponding to `3` (Diamond Plate) being used,
// which is correct. Success.
info.mat = decode_material(
encoded.read_single().value_or(default_encoded_mat)
);
bool slash_found{};
if (blk.peek_single() == '/') { slash_found = true; }
bool val_end_sep_found{};
stream value_beg{blk};
stream value_cur{blk};
// This effectively sets the `blk` stream to be positioned
// at the last `=`, `>`, or `^` in the current block. However if
// a value separator was found, `>` is ignored.
for (;;) {
auto look_for{slash_found ? "=^" : "=>^"};
if (is_one_of(value_cur.peek_single(), look_for)) {
blk = value_cur;
val_end_sep_found = true;
}
if (value_cur.eof()) { break; }
if (value_cur.read_single() == '/'
&& !traits.component
&& is_one_of(value_cur.peek_single(), "=^")) {
value_cur.read_single();
}
}
// If a `=` or `^` wasn't found, then we just use the end of the
// block.
if (!val_end_sep_found) { blk = value_cur; }
stream value{
{value_beg.i, blk.i}
};
pending_wire pending1{};
// Values.
if (value.peek_single() == '/') {
// Consume the `/`.
value.read_single();
while (!value.eof()) {
char c{value.read_single().value()};
// Value escape.
if (
c == '/'
&& is_one_of(value.peek_single(), "=^")
) {
c = value.read_single().value();
}
info.value += c;
}
}
if (blk.eof()) {
// Move on if there's nothing left in this block.
ids[pending1.to_blk] = this->place(info);
continue;
}
// Otherwise we deal with world IDs and wires.
info.activated = blk.peek_single() == '>';
if (is_one_of(blk.peek_single(), "=>")) {
// Consume the `=` or `>`.
blk.read_single();
pending1.to_blk = decode_world_id(blk.read_until(""));
ids[pending1.to_blk] = this->place(info);
continue;
}
// Consume the `^` (since `=` and `>` have been ruled out).
blk.read_single();
// First destination connector.
pending1.to_con = blk.read_single().value();
// World ID.
pending1.to_blk = bl::decode_world_id(blk.read_until("_-"));
bool to_self1{blk.peek_single() == '-'};
// Consume the `_` or `-`.
blk.read_single();
pending1.from_con = blk.read_single().value();
pending1.from_blk = {
to_self1 ? pending1.to_blk
: bl::decode_world_id(blk.read_until(".*"))
};
pending_wires.push_back(pending1);
// The final stretch.
while (!blk.eof()) {
pending_wire pending{};
bool to_self{blk.peek_single() == '*'};
// Consume the `.` or `*`.
blk.read_single();
pending.to_blk = pending1.to_blk;
pending.to_con = blk.read_single().value();
auto from_blk_con{blk.read_until(".*")};
std::string_view from_blk{
from_blk_con
| std::views::take(from_blk_con.length() - 1)
};
char from_con{from_blk_con.back()};
pending.from_blk = to_self
? pending.to_blk
: bl::decode_world_id(from_blk);
pending.from_con = from_con;
pending_wires.push_back(pending);
}
ids[pending1.to_blk] = this->place(info);
}
// Now we just need to actually create the pending wires we've
// accumulated. Wires aren't created immediately after parsing them
// because they may reference other blocks that haven't been parsed yet.
for (auto const& wire: pending_wires) {
this->connect(
{ids.at(wire.from_blk), ids.at(wire.to_blk),
wire.from_con, wire.to_con}
);
}
}
std::string bl::emplacement::save() const {
std::vector<block_wire> sorted_wires{};
sorted_wires.reserve(this->wires.size());
for (auto const& item: this->wires) {
sorted_wires.push_back(item.second);
}
std::ranges::stable_sort(
sorted_wires, [](block_wire const& a, block_wire const& b) {
return a.to_blk < b.to_blk;
}
);
std::unordered_map<unique_id, std::span<block_wire const>> in_wires{};
std::span<block_wire const> span{
sorted_wires.cbegin(), sorted_wires.cbegin()
};
for (auto const& wire: sorted_wires) {
if (!span.empty() && wire.to_blk != span.back().to_blk) {
in_wires[span.back().to_blk] = span;
span = {span.end(), span.end()};
}
span = {span.begin(), span.end() + 1};
}
if (!span.empty()) { in_wires[span.back().to_blk] = span; }
auto world_ids{gen_world_ids(this)};
std::string out{};
for (auto const& [id, blk]: this->blks) {
auto traits{block_name_traits(blk.name)};
// Encoded name.
out += traits.encoded;
// Then the position.
out.append(blk.pos.encode().data(), 4);
// This is where it gets a little interesting. The rotation must
// be specified if anything after it (color, material, value,
// world ID, wires) will be specified.
if (blk.rot != rotation()
|| blk.color != vec3(255)
|| blk.mat != material::eDefault
|| !blk.value.empty()
|| traits.component) {
out += blk.rot.encode();
}
// Colors don't have any particular restriction.
if (blk.color != vec3(255)) {
out.append(blk.color.encode().data(), 4);
}
// Materials have no restrictions either. This means that you
// can specify the rotation and the material but not the color,
// and it'll still work.
if (blk.mat != material::eDefault) {
out += material_traits(blk.mat).encoded;
}
if (!blk.value.empty()) {
// Values begin with a `/`.
out += '/';
if (traits.component) {
out += blk.value;
} else {
// Non-component blocks need `=`s and `^`s to be
// "escaped" using `/`, so to speak.
for (auto c: blk.value) {
if (is_one_of(c, "=^")) { out += '/'; }
out += c;
}
}
}
if (!traits.component) {
out += ';';
continue;
}
// Encode any wires if the block is a component.
// `=` introduces a world ID. `>` is the same but indicates that
// the block is activated. Finally, `^` introduces wires.
auto wire_span_it{in_wires.find(id)};
bool has_wires{wire_span_it != in_wires.cend()};
std::span<bl::block_wire const> wire_span{};
if (has_wires) { wire_span = wire_span_it->second; }
auto wire_it{wire_span.begin()};
out += blk.activated ? '>' : (has_wires ? '^' : '=');
// Destination connector.
if (has_wires) { out += wire_it->to_con; }
std::size_t cur_world_id{world_ids.at(id)};
// Destination world ID.
out += bl::encode_world_id(cur_world_id);
if (!has_wires) {
out += ';';
continue;
}
auto from_world_id1{world_ids.at(wire_it->from_blk)};
bool to_self1{cur_world_id == from_world_id1};
out += to_self1 ? '-' : '_';
// Source connector.
out += wire_it->from_con;
// Source world ID. This is omitted if the wire is
// targetting itself.
if (!to_self1) { out += bl::encode_world_id(from_world_id1); }
++wire_it;
// Now for the rest of the wires.
for (; wire_it != wire_span.end(); ++wire_it) {
auto from_world_id{world_ids.at(wire_it->from_blk)};
bool to_self{cur_world_id == from_world_id};
out += to_self ? '*' : '.';
// Destination connector.
out += wire_it->to_con;
// Source world ID. This is omitted if the wire is
// targetting itself.
if (!to_self) {
out += bl::encode_world_id(from_world_id);
}
// Source connector.
out += wire_it->from_con;
}
// Block terminator.
out += ';';
}
// Remove the trailing `;`.
if (!this->blks.empty()) { out.pop_back(); }
return out;
}