This repository has been archived by the owner on Jul 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
statescr.c
248 lines (205 loc) · 5.16 KB
/
statescr.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
#define VERSION "1.1"
/*
=============================================================================
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 1024
#define MAXTHINKS 128
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;
/*
===================
=
= main
=
===================
*/
int main (void)
{
FILE *stenum;
int i,j;
state_t *st;
printf ("\nSTATESCR "VERSION" by John Carmack, copyright (c) 1993 Id Software\n");
printf ("processing statescr.ss\n");
LoadScriptFile ("statescr.ss");
//
// parse state definitions
//
strcpy (actionname[0], "NULL");
numactions = 1;
numstates = numsprites = 0;
st = states;
do
{
GetToken (true);
if (endofscript)
break;
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++;
} 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 states.h file
//
//=============================================
stenum = fopen ("states.h","w");
fprintf (stenum, "// generated by statescr\n\n");
#if 0
"#ifndef __P_LOCAL__\n"
"#include \"P_local.h\"\n"
"#endif\n\n");
#endif
//
// 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");
fclose (stenum);
//===========================================
//
// write states.c file
//
//===========================================
stenum = fopen ("states.c","w");
fprintf (stenum, "// generated by statescr\n\n");
fprintf (stenum, "#include \"states.h\"\n");
fprintf (stenum, "#include <stdlib.h>\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");
fclose (stenum);
printf ("%u states parsed\n",numstates);
return 0;
}