Repository navigation
Expand file tree
/
Copy pathlevelc.cpp
More file actions
388 lines (340 loc) · 12.2 KB
/
Copy pathlevelc.cpp
File metadata and controls
388 lines (340 loc) · 12.2 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
388
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <array>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
constexpr int W = 16; // width
constexpr int H = 15; // height
constexpr char ralph_char = '&';
using grid_t = std::array<std::array<char, W>, H>;
struct coord_t {
int x;
int y;
};
bool operator==(coord_t lhs, coord_t rhs) {
return lhs.x == rhs.x && lhs.y == rhs.y;
}
bool operator!=(coord_t lhs, coord_t rhs) {
return !(lhs == rhs);
}
struct enemy_t {
int attr;
coord_t pos;
};
int compress_xy(coord_t c) {
return c.x | ((c.y * 16) & 0b11110000);
}
// Use this to find ralph and the gem.
coord_t get_1_object(grid_t grid, char obj_ch, std::string error_prefix) {
coord_t ret = {0};
for(int y = 0; y != H; ++y)
for(int x = 0; x != W; ++x) {
if(grid[y][x] == obj_ch) {
if(ret != coord_t{0}) {
throw std::runtime_error(
error_prefix + ": duplicate chars: " + obj_ch);
}
ret = {x,y};
}
}
return ret;
}
// Modifies grid by replacing enemy chars with '.'
std::vector<enemy_t> extract_enemies(grid_t& grid, std::string error_prefix) {
constexpr int SPEED1 = 0b00000000;
constexpr int SPEED2 = 0b00000001;
constexpr int SPEED3 = 0b00000010;
constexpr int SPEED4 = 0b00000011;
// Bouncey movement
constexpr int RIGHT = 0b00000000;
constexpr int DOWN = 0b00000100;
constexpr int LEFT = 0b00001000;
constexpr int UP = 0b00001100;
constexpr int REVERSE = 0b00100000;
constexpr int TURN_RIGHT = 0b00010000;
constexpr int TURN_LEFT = 0b00110000;
// Circular movement
constexpr int CIRCULAR = 0b01000000;
constexpr int ANGLE_270 = 0b00110000;
constexpr int ANGLE_180 = 0b00100000;
constexpr int ANGLE_90 = 0b00010000;
constexpr int HALF_RADIUS = 0b00000100;
constexpr int QUARTER_RADIUS = 0b00001000;
constexpr int EIGHTH_RADIUS = 0b00001100;
std::vector<enemy_t> ret;
for(int y = 0; y != H; ++y)
for(int x = 0; x != W; ++x) {
enemy_t e = {0, {x,y}};
switch(grid[y][x]) {
case '>': e.attr |= SPEED4;
case 'R': e.attr |= SPEED2;
case 'r': e.attr |= REVERSE | RIGHT; break;
case 'v': e.attr |= SPEED4;
case 'D': e.attr |= SPEED2;
case 'd': e.attr |= REVERSE | DOWN; break;
case '<': e.attr |= SPEED4;
case 'L': e.attr |= SPEED2;
case 'l': e.attr |= REVERSE | LEFT; break;
case '^': e.attr |= SPEED4;
case 'U': e.attr |= SPEED2;
case 'u': e.attr |= REVERSE | UP; break;
case '[': e.attr |= TURN_RIGHT | LEFT; break;
case '{': e.attr |= TURN_RIGHT | LEFT | SPEED2; break;
case ']': e.attr |= TURN_RIGHT | RIGHT; break;
case '}': e.attr |= TURN_RIGHT | RIGHT | SPEED2; break;
case '(': e.attr |= TURN_LEFT | LEFT | SPEED2; break;
case ')': e.attr |= TURN_LEFT | RIGHT | SPEED2; break;
case '+': e.attr = CIRCULAR | EIGHTH_RADIUS | SPEED2; break;
case '*': e.attr = CIRCULAR | EIGHTH_RADIUS | SPEED3; break;
case 'o': e.attr = CIRCULAR | QUARTER_RADIUS | SPEED2; break;
case 'O': e.attr = CIRCULAR | HALF_RADIUS | SPEED2; break;
case 'q': e.attr = CIRCULAR | ANGLE_180 | QUARTER_RADIUS | SPEED2; break;
case 'Q': e.attr = CIRCULAR | ANGLE_180 | HALF_RADIUS | SPEED2; break;
case 'j': e.attr = CIRCULAR | ANGLE_90 | QUARTER_RADIUS | SPEED2; break;
case 'J': e.attr = CIRCULAR | ANGLE_90 | HALF_RADIUS | SPEED2; break;
case 'h': e.attr = CIRCULAR | ANGLE_270 | QUARTER_RADIUS | SPEED2; break;
case 'H': e.attr = CIRCULAR | ANGLE_270 | HALF_RADIUS | SPEED2; break;
case '@':
ret.push_back({ CIRCULAR | SPEED3 | ANGLE_180 | EIGHTH_RADIUS, {x,y}});
ret.push_back({ CIRCULAR | SPEED3 | QUARTER_RADIUS, {x,y}});
ret.push_back({ CIRCULAR | SPEED3 | ANGLE_180 | HALF_RADIUS, {x,y}});
goto alreadyPushed;
case 'c':
ret.push_back({ CIRCULAR | SPEED2 | QUARTER_RADIUS, {x,y}});
ret.push_back({ CIRCULAR | ANGLE_90 | SPEED2 | QUARTER_RADIUS, {x,y}});
ret.push_back({ CIRCULAR | ANGLE_180 | SPEED2 | QUARTER_RADIUS, {x,y}});
goto alreadyPushed;
case 'k':
ret.push_back({ CIRCULAR | ANGLE_270 | SPEED2 | QUARTER_RADIUS, {x,y}});
ret.push_back({ CIRCULAR | SPEED2 | QUARTER_RADIUS, {x,y}});
ret.push_back({ CIRCULAR | ANGLE_180 | SPEED2 | QUARTER_RADIUS, {x,y}});
goto alreadyPushed;
case '=':
e.attr |= TURN_RIGHT | RIGHT | SPEED2;
ret.push_back(e);
grid[y][x] = ',';
continue;
default: continue;
}
ret.push_back(e);
alreadyPushed:
grid[y][x] = '.';
}
return ret;
}
std::vector<unsigned char> get_uncompressed_tiles(grid_t grid,
std::string error_prefix,
int i, int size) {
std::vector<unsigned char> uncompressed_tiles;
for(int y = 0; y != H; ++y)
for(int x = 0; x != W; ++x) {
char const ch = grid[y][x];
switch(ch) {
case '`':
uncompressed_tiles.push_back(3);
break;
case ',':
uncompressed_tiles.push_back(0);
break;
case '\'':
case '.':
uncompressed_tiles.push_back(1);
break;
case '%':
uncompressed_tiles.push_back(12);
break;
case 'X':
uncompressed_tiles.push_back(13);
break;
case '#':
uncompressed_tiles.push_back(14);
break;
case 'E':
uncompressed_tiles.push_back(15);
break;
case '"':
if(i == (size - 1))
uncompressed_tiles.push_back(7);
else
uncompressed_tiles.push_back(6);
break;
case 't':
uncompressed_tiles.push_back(9);
break;
case 'e':
uncompressed_tiles.push_back(10);
break;
case '!':
uncompressed_tiles.push_back(11);
break;
case ralph_char:
if(i == 0)
uncompressed_tiles.push_back(1);
else
uncompressed_tiles.push_back(2);
break;
default:
throw std::runtime_error(error_prefix
+ ": bad character; " + ch);
}
}
return uncompressed_tiles;
}
std::vector<unsigned char> compress_tiles(std::vector<unsigned char> un) {
int total_count = 0;
std::vector<unsigned char> compressed;
for(int i = 0; i < un.size();) {
unsigned char ch = un[i];
if(ch >= 16)
throw std::runtime_error("bad tile");
int count = 0;
for(;un[i] == ch && count < 16 && i < un.size(); ++i,++count);
total_count += count;
compressed.push_back(ch | ((count-1) << 4));
}
return compressed;
}
std::string level_label(int i) {
return "level_" + std::to_string(i + 1);
}
void write_levels(char const* filename, std::vector<grid_t> levels,
bool reversed = false) {
std::ofstream outfile(filename);
if(!outfile.is_open()) {
std::fprintf(stderr, "unable to open file: \"%s\"\n", filename);
throw EXIT_FAILURE;
}
outfile << ".include \"globals.inc\"\n";
outfile << "num_levels = " << levels.size() << '\n';
outfile << ".segment \"RODATA\"\n\n";
outfile << "level_index:";
for(int i = 0; i != levels.size(); ++i) {
if(i % 4 == 0)
outfile << "\n.dbyt ";
else
outfile << ',';
outfile << level_label(i);
}
outfile << "\n\n";
coord_t prev_door;
for(int i = 0; i != levels.size(); ++i) {
std::string error_prefix = "level " + std::to_string(i+1);
outfile << level_label(i) << ":\n";
coord_t ralph = get_1_object(levels[i], ralph_char, error_prefix);
if(ralph == coord_t{0})
throw std::runtime_error(error_prefix + ": missing ralph");
outfile << "; Ralph start position:\n";
outfile << ".byt " << compress_xy(ralph) << '\n';
coord_t exit = get_1_object(levels[i], '"', error_prefix);
if(exit == coord_t{0})
throw std::runtime_error(error_prefix + ": missing exit");
if(i > 0 && ((reversed && exit != prev_door)
|| (!reversed && ralph != prev_door))) {
throw std::runtime_error(
error_prefix + ": entry/exit mismatch");
}
if(reversed)
prev_door = ralph;
else
prev_door = exit;
coord_t gem = get_1_object(levels[i], '\'', error_prefix);
outfile << "; Gem position:\n";
outfile << ".byt " << compress_xy(gem) << '\n';
std::vector<enemy_t> enemies = extract_enemies(levels[i],
error_prefix);
if(enemies.size() > 16)
throw std::runtime_error(error_prefix + ": too many enemies");
outfile << "; Num enemies:\n";
outfile << ".byt " << enemies.size() << '\n';
if(enemies.size() > 0) {
outfile << "; Enemy attributes array:\n.byt ";
for(int i = 0; i != enemies.size(); ++i) {
if(i != 0)
outfile << ',';
outfile << enemies[i].attr;
}
outfile << '\n';
outfile << "; Enemy positions:\n.byt ";
for(int i = 0; i != enemies.size(); ++i) {
if(i != 0)
outfile << ',';
outfile << compress_xy(enemies[i].pos);
}
outfile << '\n';
}
// Read tiles and compress them
std::vector<unsigned char> compressed_tiles
= compress_tiles(get_uncompressed_tiles(levels[i], error_prefix,
i, levels.size()));
// Write tiles
outfile << "; Tiles:\n";
for(int i = 0; i != compressed_tiles.size(); ++i) {
if(i % 16 == 0)
outfile << "\n.byt ";
else
outfile << ',';
outfile << (int)compressed_tiles[i];
}
outfile << '\n';
}
}
std::vector<std::string> read_lines(char const* filename) {
std::ifstream infile(filename);
if(!infile.is_open()) {
std::fprintf(stderr, "unable to open file: \"%s\"\n", filename);
throw EXIT_FAILURE;
}
std::string line;
std::vector<std::string> lines;
while(std::getline(infile, line))
lines.push_back(line);
return lines;
}
int main(int argc, char** argv) {
if(argc != 3) {
std::fprintf(stderr, "usage: levelc [file]\n");
return EXIT_FAILURE;
}
std::vector<grid_t> levels;
char* const in_filename = argv[1];
char* const out_filename = argv[2];
std::vector<std::string> lines = read_lines(in_filename);
std::reverse(lines.begin(), lines.end());
int line_number = 1;
while(!lines.empty() && lines.back().empty()) {
lines.pop_back();
++line_number;
}
while(lines.size() >= H) {
grid_t new_level;
for(int i = 0; i != H; ++i, ++line_number) {
if(lines.back().length() < W) {
std::fprintf(stderr, "parse error; line %i too short\n",
line_number);
return EXIT_FAILURE;
}
std::copy(lines.back().begin(), lines.back().end(),
new_level[i].begin());
lines.pop_back();
}
levels.push_back(new_level);
while(!lines.empty() && lines.back().empty()) {
lines.pop_back();
++line_number;
}
}
if(lines.size() > 1) {
std::fprintf(stderr, "incomplete level at end\n");
return EXIT_FAILURE;
}
// Set this to true to make testing levels easier.
constexpr bool reverse = 0;
if(reverse)
std::reverse(levels.begin(), levels.end());
write_levels(out_filename, levels, reverse);
std::printf("finished!\n");
}