forked from chocolatiers/doom-utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multigen.c
403 lines (334 loc) · 7.98 KB
/
multigen.c
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
#define VERSION "1.0"
/*
=============================================================================
DOOM STATESCR
by John Carmack
=============================================================================
*/
#include "cmdlib.h"
#include "scriplib.h"
typedef struct
{
long sprite;
long frame;
long tics;
long action;
long nextstate;
long misc1, misc2;
} state_t;
#define MAXSTATES 2048
#define MAXTHINKS 256
char spritename[MAXSTATES][5];
int numsprites;
char actionname[MAXTHINKS][32];
int numactions;
char statename[MAXSTATES][32];
char nextname[MAXSTATES][32];
state_t states[MAXSTATES];
int numstates;
state_t *st;
#define MAXTYPES 256
#define MAXINFO 64
typedef struct
{
char *name;
char *base;
} info_t;
info_t baseinfo[MAXINFO];
char typename[MAXTYPES][32];
char *info[MAXTYPES][MAXINFO];
int numtypes;
int numinfo;
int nummisc;
/*
===============
=
= ParseState
=
===============
*/
void ParseState (void)
{
int j;
for (j=0 ; j<numstates ; j++)
if (!strcmpi(token,statename[j]))
Error ("line %i: duplicate state name (%s)",scriptline,token);
strcpy (statename[numstates],token);
GetToken (false); // sprite
for (j=0 ; j<numsprites ; j++)
if (!strcmpi(token,spritename[j]))
break;
if (j==numsprites)
{
numsprites++;
strcpy (spritename[j], token);
}
st->sprite = j;
GetToken (false); // frame
st->frame = toupper(token[0])-'A';
if (token[1] == '*')
{
st->frame |= 0x8000; // full bright flag
if (strlen (token) != 2)
Error ("line %i: bad frame",scriptline);
}
else if (strlen (token) != 1)
Error ("line %i: bad frame",scriptline);
GetToken (false); // tics
st->tics = atoi (token);
GetToken (false); // action
for (j=0 ; j<numactions ; j++)
if (!strcmpi(token,actionname[j]))
break;
if (j==numactions)
{
numactions++;
strcpy (actionname[j], token);
}
st->action = j;
GetToken (false); // next
strcpy (nextname[numstates],token); // resolved after reading all states
if (TokenAvailable ())
{
GetToken (false); // misc1
st->misc1 = atoi (token);
if (TokenAvailable ())
{
GetToken (false); // misc2
st->misc2 = atoi (token);
}
}
numstates++;
st++;
}
/*
===============
=
= ParseInfo
=
===============
*/
void ParseInfo (void)
{
int i;
if (token[0] == '$')
{
GetToken (false);
if (token[0] == '+')
{
sprintf (typename[numtypes],"MT_MISC%i",nummisc);
nummisc++;
}
else
strcpy (typename[numtypes], token);
numtypes++;
return;
}
if (!numtypes)
Error ("A type mnust be defined before declaring info");
// find which field name
for (i=0 ; i<numinfo ; i++)
if (!strcmp (token, baseinfo[i].name))
break;
if (i==numinfo)
Error ("Unknown info type %s",token);
GetToken (false);
info[numtypes-1][i] = malloc(strlen(token)+1);
strcpy (info[numtypes-1][i], token);
}
/*
===================
=
= main
=
===================
*/
int main (void)
{
FILE *stenum;
int i,j;
printf ("\nMULTIGEN "VERSION" by John Carmack, copyright (c) 1994 Id Software\n");
printf ("processing multigen.txt\n");
LoadScriptFile ("multigen.txt");
//
// init state definitions
//
strcpy (actionname[0], "NULL");
numactions = 1;
numstates = numsprites = 0;
st = states;
//
// parse info defaults
//
numtypes = 0;
GetToken (true);
if (token[0] != '$')
Error ("first command must be $ DEFAULT");
GetToken (false);
if (strcmp (token,"DEFAULT"))
Error ("first command must be $ DEFAULT");
numinfo = 0;
do
{
GetToken (true);
if (endofscript)
Error ("end of script in defaults");
if (token[0] == '$' || (token[0] == 'S' && token[1] == '_') )
{
UnGetToken ();
break;
}
baseinfo[numinfo].name = malloc (strlen(token)+1);
strcpy (baseinfo[numinfo].name, token);
GetToken (false);
baseinfo[numinfo].base = malloc (strlen(token)+1);
strcpy (baseinfo[numinfo].base, token);
numinfo++;
} while (1);
//
// parse state and info definitions
//
do
{
GetToken (true);
if (endofscript)
break;
if (token[0] == 'S' && token[1] == '_')
ParseState ();
else
ParseInfo ();
} while (1);
//
// resolve next state numbers
//
for (i=0 ; i<numstates ; i++)
{
for (j=0 ; j<numstates ; j++)
if (!strcmpi(nextname[i],statename[j]))
break;
if (j==numstates)
printf ("Unresolved nextstate: %s\n",nextname[i]);
states[i].nextstate = j;
}
//=============================================
//
// write info.h file
//
//=============================================
stenum = fopen ("info.h","w");
fprintf (stenum, "// generated by multigen\n\n");
//
// write sprite names
//
fprintf (stenum,"typedef enum {\n");
for (i=0 ; i<numsprites ; i++)
fprintf (stenum,"SPR_%s,\n",spritename[i]);
fprintf (stenum,"NUMSPRITES\n} spritenum_t;\n\n");
//
// write state names
//
fprintf (stenum,"typedef enum {\n");
for (i=0 ; i<numstates ; i++)
fprintf (stenum,"%s,\n",statename[i]);
fprintf (stenum,"NUMSTATES\n} statenum_t;\n\n");
fprintf (stenum, "typedef struct\n"
"{\n"
" spritenum_t sprite;\n"
" long frame;\n"
" long tics;\n"
" void (*action) ();\n"
" statenum_t nextstate;\n"
" long misc1, misc2;\n"
"} state_t;\n\n");
fprintf (stenum, "extern state_t states[NUMSTATES];\n");
fprintf (stenum, "extern char *sprnames[NUMSPRITES];\n\n");
//
// write info def
//
fprintf (stenum, "\n\ntypedef enum {\n");
for (i=0 ; i<numtypes ; i++)
fprintf (stenum,"%s,\n",typename[i]);
fprintf (stenum, "NUMMOBJTYPES} mobjtype_t;\n\n");
fprintf (stenum, "typedef struct {\n");
for (j=0 ; j<numinfo ; j++)
{
if ( !strncmp(baseinfo[j].name,"str_",4) )
fprintf (stenum, " char *%s;\n", baseinfo[j].name );
else
fprintf (stenum, " int %s;\n", baseinfo[j].name );
}
fprintf (stenum, "} mobjinfo_t;\n\n");
fprintf (stenum, "extern mobjinfo_t mobjinfo[NUMMOBJTYPES];\n\n");
fclose (stenum);
//===========================================
//
// write states.c file
//
//===========================================
stenum = fopen ("info.c","w");
fprintf (stenum, "#include \"doomdef.h\"\n");
fprintf (stenum, "// generated by multigen\n\n");
//
// write sprite names for initial sprite loading
//
fprintf (stenum, "char *sprnames[NUMSPRITES] = {\n");
for (i=0 ; i<numsprites ; i++)
{
fprintf (stenum, "\"%s\"", spritename[i]);
if (i != numsprites-1)
{
fprintf (stenum, ",");
if (i%10 == 9)
fprintf (stenum, "\n");
}
}
fprintf (stenum, "\n};\n\n");
//
// write action names
//
for (i=1 ; i<numactions ; i++) // skip NULL at 0
fprintf (stenum,"void %s ();\n", actionname[i]);
fprintf (stenum,"\n");
//
// write state structures
//
fprintf (stenum, "state_t states[NUMSTATES] = {\n");
for (i=0, st=states ; i<numstates ; i++,st++)
{
fprintf (stenum, "{SPR_%s,%li,%li,%s,%s,%li,%li}",
spritename[st->sprite], st->frame,
st->tics, actionname[st->action], statename[st->nextstate],
st->misc1, st->misc2);
if (i != numstates-1)
fprintf (stenum, ",");
fprintf (stenum, "\t// %s\n",statename[i]);
}
fprintf (stenum, "};\n\n");
//
// write info declarations
//
fprintf (stenum, "\nmobjinfo_t mobjinfo[NUMMOBJTYPES] = {\n");
for (i=0 ; i<numtypes ; i++)
{
fprintf (stenum,"\n{ // %s\n", typename[i]);
for (j=0 ; j<numinfo ; j++)
{
if (info[i][j])
fprintf (stenum, "%s", info[i][j] );
else
fprintf (stenum, "%s", baseinfo[j].base );
if (j != numinfo-1)
fprintf (stenum, ",");
fprintf (stenum," // %s\n", baseinfo[j].name);
}
fprintf (stenum, " }");
if (i != numtypes-1)
fprintf (stenum, ",");
fprintf (stenum, "\n");
}
fprintf (stenum, "};\n\n");
fclose (stenum);
printf ("%u states parsed\n",numstates);
printf ("%i types parsed\n", numtypes);
return 0;
}