-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen_text_cpp.cxx
480 lines (357 loc) · 11.1 KB
/
gen_text_cpp.cxx
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// Zork I: The Great Underground Empire
// (c) 1980 by INFOCOM, Inc.
// C port and parser (c) 2021 by Donnie Russell II
// This source code is provided for personal, educational use only.
// You are welcome to use this source code to develop your own works,
// but the story-related content belongs to the original authors of Zork.
#include "def.h"
struct LABEL_STRUCT {const char *label; const char *text;};
extern struct LABEL_STRUCT RoomDesc[NUM_ROOMS];
extern struct LABEL_STRUCT BlockMsg[NUM_BLOCK_MESSAGES];
extern struct LABEL_STRUCT ObjectDesc[NUM_OBJECTS];
//*****************************************************************************
typedef struct NODE_TYPE
{
struct NODE_TYPE *left, *right;
int freq;
char c;
} *NODE;
struct NODE_TYPE NodePool[256];
NODE NodeQueueArray[255];
NODE *NodeQueue = NodeQueueArray - 1; // node indexes start at 1
int NumNodes, EndNodeIndex = 1;
int CharFrequency[128];
char HuffmanCode[128][32];
NODE NewNode(int freq, char c, NODE a, NODE b)
{
NODE n = NodePool + NumNodes++;
if (freq) n->c = c, n->freq = freq;
else
{
n->left = a, n->right = b;
n->freq = a->freq + b->freq;
}
return n;
}
// priority queue
void InsertNode(NODE n)
{
int j, i = EndNodeIndex++;
while ((j = i / 2))
{
if (NodeQueue[j]->freq <= n->freq) break;
NodeQueue[i] = NodeQueue[j], i = j;
}
NodeQueue[i] = n;
}
NODE RemoveNode(void)
{
int i, l;
NODE n = NodeQueue[i = 1];
if (EndNodeIndex < 2) return 0;
EndNodeIndex--;
while ((l = i * 2) < EndNodeIndex)
{
if (l + 1 < EndNodeIndex && NodeQueue[l + 1]->freq < NodeQueue[l]->freq) l++;
NodeQueue[i] = NodeQueue[l], i = l;
}
NodeQueue[i] = NodeQueue[EndNodeIndex];
return n;
}
// walk down tree
void BuildCode(NODE n, char *s, int len)
{
if (n->c)
{
s[len] = 0;
strcpy(HuffmanCode[n->c], s);
return;
}
s[len] = '0'; BuildCode(n->left, s, len + 1); // go left
s[len] = '1'; BuildCode(n->right, s, len + 1); // go right
}
void BuildHuffmanTree(void)
{
int i;
char c[16];
for (i=0; i<128; i++)
if (CharFrequency[i]) InsertNode(NewNode(CharFrequency[i], i, 0, 0));
while (EndNodeIndex > 2)
InsertNode(NewNode(0, 0, RemoveNode(), RemoveNode()));
BuildCode(NodeQueue[1], c, 0);
}
//*****************************************************************************
//*****************************************************************************
int Count, bit, byte, bytes_out, bytes_in;
char PrintStringMode;
char String[2048][2048];
unsigned short StartP[2048];
unsigned char StartB[2048];
#define SUBST \
if (size == 1) {c = 1; size--;} \
else if (size >= 4 && p[0] == 't' && p[1] == 'h' && p[2] == 'e' && p[3] == ' ') {c = 2; p += 4; size -= 4;} \
else if (size >= 4 && p[0] == 'T' && p[1] == 'h' && p[2] == 'e' && p[3] == ' ') {c = 3; p += 4; size -= 4;} \
else if (size >= 4 && p[0] == 'i' && p[1] == 'n' && p[2] == 'g' && p[3] == ' ') {c = 4; p += 4; size -= 4;} \
else if (size >= 4 && p[0] == 'y' && p[1] == 'o' && p[2] == 'u' && p[3] == ' ') {c = 5; p += 4; size -= 4;} \
else if (size >= 4 && p[0] == 'Y' && p[1] == 'o' && p[2] == 'u' && p[3] == ' ') {c = 6; p += 4; size -= 4;} \
else if (size >= 4 && p[0] == 'a' && p[1] == 'n' && p[2] == 'd' && p[3] == ' ') {c = 7; p += 4; size -= 4;} \
else if (size >= 4 && p[0] == ' ' && p[1] == 'i' && p[2] == 's' && p[3] == ' ') {c = 8; p += 4; size -= 4;} \
else if (size >= 4 && p[0] == ' ' && p[1] == 't' && p[2] == 'o' && p[3] == ' ') {c = 9; p += 4; size -= 4;} \
else if (size >= 4 && p[0] == ' ' && p[1] == 'o' && p[2] == 'f' && p[3] == ' ') {c = 11; p += 4; size -= 4;} \
else if (size >= 4 && p[0] == 'h' && p[1] == 'e' && p[2] == 'r' && p[3] == 'e') {c = 12; p += 4; size -= 4;} \
else if (size >= 4 && p[0] == 'y' && p[1] == 'o' && p[2] == 'u' && p[3] == 'r') {c = 13; p += 4; size -= 4;} \
else if (size >= 4 && p[0] == 'w' && p[1] == 'i' && p[2] == 't' && p[3] == 'h') {c = 14; p += 4; size -= 4;} \
else {c = *p++; size--;}
void PrintString(FILE *out2, FILE *out1, const char *p, int size)
{
int j;
for (j=0; j<Count; j++)
if (memcmp(String[j], p, size) == 0)
{
fprintf(out1, "%i", j);
return;
}
memcpy(String[Count], p, size);
Count++;
if (PrintStringMode)
{
fprintf(out1, "%i", Count-1);
StartP[Count-1] = bytes_out;
StartB[Count-1] = bit;
bytes_in += size;
fputs("\n ", out2);
while (size)
{
int i, len;
unsigned char c;
SUBST
len = strlen(HuffmanCode[c]);
for (i=0; i<len; i++)
{
if (HuffmanCode[c][i] == '1') byte |= 1<<bit;
if (bit == 7)
{
fprintf(out2, "%i,", byte);
bytes_out++;
bit = 0;
byte = 0;
}
else bit++;
}
}
}
else
while (size)
{
unsigned char c;
SUBST
CharFrequency[(int)c]++;
if (c == 1) c = '\n';
else if (c == 10) c = '|';
else if (c < 32) c = '_';
fputc(c, out2);
}
}
void ProcessText(const char *filename_in, const char *filename_out1, FILE *out2)
{
FILE *f, *out1;
int size;
char *buffer, *p, *string, *s;
const char *match1 = "PrintLine(\"", *match1c = "PrintLineIndex(";
int match1_len = 11;
const char *match2 = "PrintText(\"", *match2c = "PrintTextIndex(";
int match2_len = 11;
f = fopen(filename_in, "rb");
fseek(f, 0, SEEK_END);
size = ftell(f);
fclose(f);
buffer = (char *)malloc(size);
string = (char *)malloc(size);
f = fopen(filename_in, "rb");
fread(buffer, 1, size, f);
fclose(f);
out1 = fopen(filename_out1, "wb");
p = buffer;
while (p < buffer+size)
{
if ((p-buffer <= size-match1_len && memcmp(p, match1, match1_len) == 0) ||
(p-buffer <= size-match2_len && memcmp(p, match2, match2_len) == 0))
{
if (memcmp(p, match1, match1_len) == 0) {fputs(match1c, out1); p += match1_len;}
else {fputs(match2c, out1); p += match2_len;}
s = string;
while (p < buffer+size)
{
if (*p == '\\') // escape sequence
{
p++;
if (p < buffer+size)
{
if (*p == 'n') *s++ = '\n';
else if (*p == '\"') *s++ = '\"';
else if (*p == '\\') *s++ = '\\';
p++;
}
}
else if (*p == '\"') // end of string
{
p++;
*s++ = 0;
PrintString(out2, out1, string, s-string);
break;
}
else
*s++ = *p++;
}
}
else putc(*p++, out1);
}
fclose(f);
fclose(out1);
free(buffer);
free(string);
}
void ProcessData(FILE *f, const char *listname, struct LABEL_STRUCT list[], int listsize)
{
if (PrintStringMode)
{
int j;
bit = 0;
byte = 0;
bytes_out = 0;
bytes_in = 0;
Count = 0;
fprintf(f, "\nextern const uint8_t %s[] PROGMEM =\n{", listname);
for (j=0; j<listsize; j++)
{
unsigned char *p = (unsigned char *)list[j].text;
int size = strlen(list[j].text)+1;
StartP[Count] = bytes_out;
StartB[Count] = bit;
Count++;
bytes_in += size;
fputs("\n ", f);
while (size)
{
int i, len;
unsigned char c;
SUBST
len = strlen(HuffmanCode[c]);
for (i=0; i<len; i++)
{
if (HuffmanCode[c][i] == '1') byte |= 1<<bit;
if (bit == 7)
{
fprintf(f, "%i,", byte);
bytes_out++;
bit = 0;
byte = 0;
}
else bit++;
}
}
}
if (bit > 0)
{
fprintf(f, "%i,", byte);
bytes_out++;
}
fprintf(f, "\n};\n");
printf("%s: Bytes Out = %i %% Comp = %i\n", listname, bytes_out, 100*bytes_out/bytes_in);
fprintf(f, "\nextern const uint16_t %sStartP[] PROGMEM =\n{\n", listname);
for (j=5; j<Count; j+=5)
fprintf(f, " %i, // %i\n", StartP[j], j);
fputs("};\n", f);
fprintf(f, "\nextern const uint8_t %sStartB[] PROGMEM =\n{\n", listname);
for (j=5; j<Count; j+=5)
fprintf(f, " %i, // %i\n", StartB[j], j);
fputs("};\n", f);
}
else
{
int j;
for (j=0; j<listsize; j++)
{
unsigned char *p = (unsigned char *)list[j].text;
int size = strlen(list[j].text)+1;
while (size)
{
unsigned char c;
SUBST
CharFrequency[(int)c]++;
if (c == 1) c = '\n';
else if (c == 10) c = '|';
else if (c < 32) c = '_';
fputc(c, f);
}
}
}
}
void ProcessTextRoutine(FILE *out2)
{
Count = 0;
if (PrintStringMode)
{
fputs("#include \"def.h\"\n\nextern const uint8_t CompString[] PROGMEM =\n{", out2);
bit = 0;
byte = 0;
bytes_out = 0;
bytes_in = 0;
}
ProcessText( "game.cxx" , "_game.cpp" , out2 );
ProcessText( "parser.cxx" , "_parser.cpp" , out2 );
ProcessText( "villains.cxx" , "_villains.cpp" , out2 );
if (PrintStringMode)
{
int i;
if (bit > 0)
{
fprintf(out2, "%i,", byte);
bytes_out++;
}
fputs("\n};\n", out2);
printf("CompString: Bytes Out = %i %% Comp = %i\n", bytes_out, 100*bytes_out/bytes_in);
fprintf(out2, "\nextern const uint16_t CompStringStartP[] PROGMEM =\n{\n");
for (i=10; i<Count; i+=10)
fprintf(out2, " %i, // %i\n", StartP[i], i);
fputs("};\n", out2);
fprintf(out2, "\nextern const uint8_t CompStringStartB[] PROGMEM =\n{\n");
for (i=10; i<Count; i+=10)
fprintf(out2, " %i, // %i\n", StartB[i], i);
fputs("};\n", out2);
}
ProcessData(out2, "RoomDesc", RoomDesc, NUM_ROOMS);
ProcessData(out2, "BlockMsg", BlockMsg, NUM_BLOCK_MESSAGES);
ProcessData(out2, "ObjectDesc", ObjectDesc, NUM_OBJECTS);
}
//*****************************************************************************
//#############################################################################
int main(void)
{
FILE *out2;
int i;
PrintStringMode = 0;
out2 = fopen("_strings.txt", "wb");
ProcessTextRoutine(out2);
fclose(out2);
BuildHuffmanTree();
PrintStringMode = 1;
out2 = fopen("_text.cpp", "wb");
ProcessTextRoutine(out2);
fputs("\nextern const uint8_t HuffmanTree[] PROGMEM =\n{\n", out2);
for (i=0; i<256; i++)
{
int c = NodePool[i].c;
int left = NodePool[i].left - NodePool;
int right = NodePool[i].right - NodePool;
if (left < 0) left = 0;
if (right < 0) right = 0;
if (c == 0 && left == 0 && right == 0) break;
fprintf(out2, " %i,%i,%i,\n", c, left, right);
}
fputs("};\n", out2);
fprintf(out2, "\nextern const uint8_t TreeStartIndex = %i;\n", i-1);
fclose(out2);
return 0;
}
//#############################################################################