forked from zrax/tworld
-
Notifications
You must be signed in to change notification settings - Fork 1
/
res.c
400 lines (362 loc) · 11.1 KB
/
res.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
/* res.c: Functions for loading resources from external files.
*
* Copyright (C) 2001-2014 by Brian Raiter and Eric Schmidt, under the GNU
* General Public License. No warranty. See COPYING for details.
*/
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "defs.h"
#include "fileio.h"
#include "err.h"
#include "oshw.h"
#include "messages.h"
#include "unslist.h"
#include "res.h"
/*
* The resource ID numbers
*/
#define RES_IMG_BASE 0
#define RES_IMG_TILES (RES_IMG_BASE + 0)
#define RES_IMG_FONT (RES_IMG_BASE + 1)
#define RES_IMG_LAST RES_IMG_FONT
#define RES_CLR_BASE (RES_IMG_LAST + 1)
#define RES_CLR_BKGND (RES_CLR_BASE + 0)
#define RES_CLR_TEXT (RES_CLR_BASE + 1)
#define RES_CLR_BOLD (RES_CLR_BASE + 2)
#define RES_CLR_DIM (RES_CLR_BASE + 3)
#define RES_CLR_LAST RES_CLR_DIM
#define RES_TXT_BASE (RES_CLR_LAST + 1)
#define RES_TXT_UNSLIST (RES_TXT_BASE + 0)
#define RES_TXT_MESSAGE (RES_TXT_BASE + 1)
#define RES_TXT_LAST RES_TXT_MESSAGE
#define RES_SND_BASE (RES_TXT_LAST + 1)
#define RES_SND_CHIP_LOSES (RES_SND_BASE + SND_CHIP_LOSES)
#define RES_SND_CHIP_WINS (RES_SND_BASE + SND_CHIP_WINS)
#define RES_SND_TIME_OUT (RES_SND_BASE + SND_TIME_OUT)
#define RES_SND_TIME_LOW (RES_SND_BASE + SND_TIME_LOW)
#define RES_SND_DEREZZ (RES_SND_BASE + SND_DEREZZ)
#define RES_SND_CANT_MOVE (RES_SND_BASE + SND_CANT_MOVE)
#define RES_SND_IC_COLLECTED (RES_SND_BASE + SND_IC_COLLECTED)
#define RES_SND_ITEM_COLLECTED (RES_SND_BASE + SND_ITEM_COLLECTED)
#define RES_SND_BOOTS_STOLEN (RES_SND_BASE + SND_BOOTS_STOLEN)
#define RES_SND_TELEPORTING (RES_SND_BASE + SND_TELEPORTING)
#define RES_SND_DOOR_OPENED (RES_SND_BASE + SND_DOOR_OPENED)
#define RES_SND_SOCKET_OPENED (RES_SND_BASE + SND_SOCKET_OPENED)
#define RES_SND_BUTTON_PUSHED (RES_SND_BASE + SND_BUTTON_PUSHED)
#define RES_SND_TILE_EMPTIED (RES_SND_BASE + SND_TILE_EMPTIED)
#define RES_SND_WALL_CREATED (RES_SND_BASE + SND_WALL_CREATED)
#define RES_SND_TRAP_ENTERED (RES_SND_BASE + SND_TRAP_ENTERED)
#define RES_SND_BOMB_EXPLODES (RES_SND_BASE + SND_BOMB_EXPLODES)
#define RES_SND_WATER_SPLASH (RES_SND_BASE + SND_WATER_SPLASH)
#define RES_SND_SKATING_TURN (RES_SND_BASE + SND_SKATING_TURN)
#define RES_SND_BLOCK_MOVING (RES_SND_BASE + SND_BLOCK_MOVING)
#define RES_SND_SKATING_FORWARD (RES_SND_BASE + SND_SKATING_FORWARD)
#define RES_SND_SLIDING (RES_SND_BASE + SND_SLIDING)
#define RES_SND_SLIDEWALKING (RES_SND_BASE + SND_SLIDEWALKING)
#define RES_SND_ICEWALKING (RES_SND_BASE + SND_ICEWALKING)
#define RES_SND_WATERWALKING (RES_SND_BASE + SND_WATERWALKING)
#define RES_SND_FIREWALKING (RES_SND_BASE + SND_FIREWALKING)
#define RES_SND_LAST RES_SND_FIREWALKING
#define RES_COUNT (RES_SND_LAST + 1)
/* Structure for enumerating the resource names.
*/
typedef struct rcitem {
char const *name;
int numeric;
} rcitem;
/* Union for storing the resource values.
*/
typedef union resourceitem {
int num;
char str[256];
} resourceitem;
/* The complete list of resource names.
*/
static rcitem rclist[RES_COUNT] = {
{ "tileimages", FALSE },
{ "font", FALSE },
{ "backgroundcolor", FALSE },
{ "textcolor", FALSE },
{ "boldtextcolor", FALSE },
{ "dimtextcolor", FALSE },
{ "unsolvablelist", FALSE },
{ "endmessages", FALSE },
{ "chipdeathsound", FALSE },
{ "levelcompletesound", FALSE },
{ "chipdeathbytimesound", FALSE },
{ "ticksound", FALSE },
{ "derezzsound", FALSE },
{ "blockedmovesound", FALSE },
{ "pickupchipsound", FALSE },
{ "pickuptoolsound", FALSE },
{ "thiefsound", FALSE },
{ "teleportsound", FALSE },
{ "opendoorsound", FALSE },
{ "socketsound", FALSE },
{ "switchsound", FALSE },
{ "tileemptiedsound", FALSE },
{ "wallcreatedsound", FALSE },
{ "trapenteredsound", FALSE },
{ "bombsound", FALSE },
{ "splashsound", FALSE },
{ "blockmovingsound", FALSE },
{ "skatingforwardsound", FALSE },
{ "skatingturnsound", FALSE },
{ "slidingsound", FALSE },
{ "slidewalkingsound", FALSE },
{ "icewalkingsound", FALSE },
{ "waterwalkingsound", FALSE },
{ "firewalkingsound", FALSE }
};
/* The complete collection of resource values.
*/
static resourceitem allresources[Ruleset_Count][RES_COUNT];
/* The resource values for the current ruleset.
*/
static resourceitem *resources = NULL;
/* The ruleset-independent resource values.
*/
static resourceitem *globalresources = allresources[Ruleset_None];
/* The active ruleset.
*/
static int currentruleset = Ruleset_None;
/* The directory containing all the resource files.
*/
char *resdir = NULL;
/* A few resources have non-empty default values.
*/
static void initresourcedefaults(void)
{
strcpy(allresources[Ruleset_None][RES_IMG_TILES].str, "tiles.bmp");
strcpy(allresources[Ruleset_None][RES_IMG_FONT].str, "font.bmp");
strcpy(allresources[Ruleset_None][RES_CLR_BKGND].str, "000000");
strcpy(allresources[Ruleset_None][RES_CLR_TEXT].str, "FFFFFF");
strcpy(allresources[Ruleset_None][RES_CLR_BOLD].str, "FFFF00");
strcpy(allresources[Ruleset_None][RES_CLR_DIM].str, "C0C0C0");
memcpy(&allresources[Ruleset_MS], globalresources,
sizeof allresources[Ruleset_MS]);
memcpy(&allresources[Ruleset_Lynx], globalresources,
sizeof allresources[Ruleset_Lynx]);
}
/* Iterate through the lines of the rc file, storing the values in the
* allresources array. Lines consisting only of whitespace, or with an
* octothorpe as the first non-whitespace character, are skipped over.
* Lines containing a ruleset in brackets introduce ruleset-specific
* resource values. Ruleset-independent values are copied into each of
* the ruleset-specific entries. FALSE is returned if the rc file
* could not be opened.
*/
static int readrcfile(void)
{
resourceitem item;
fileinfo file = {0};
char buf[256];
char name[256];
char *p;
int ruleset;
int lineno, i, j;
if (!openfileindir(&file, resdir, "rc", "r", "can't open"))
return FALSE;
ruleset = Ruleset_None;
for (lineno = 1 ; ; ++lineno) {
i = sizeof buf - 1;
if (!filegetline(&file, buf, &i, NULL))
break;
for (p = buf ; isspace(*p) ; ++p) ;
if (!*p || *p == '#')
continue;
if (sscanf(buf, "[%[^]]]", name) == 1) {
for (p = name ; (*p = tolower(*p)) != '\0' ; ++p) ;
if (!strcmp(name, "ms"))
ruleset = Ruleset_MS;
else if (!strcmp(name, "lynx"))
ruleset = Ruleset_Lynx;
else if (!strcmp(name, "all"))
ruleset = Ruleset_None;
else
warn("rc:%d: syntax error", lineno);
continue;
}
if (sscanf(buf, "%[^=]=%s", name, item.str) != 2) {
warn("rc:%d: syntax error", lineno);
continue;
}
for (p = name ; (*p = tolower(*p)) != '\0' ; ++p) ;
for (i = sizeof rclist / sizeof *rclist - 1 ; i >= 0 ; --i)
if (!strcmp(name, rclist[i].name))
break;
if (i < 0) {
warn("rc:%d: illegal resource name \"%s\"", lineno, name);
continue;
}
if (rclist[i].numeric) {
i = atoi(item.str);
item.num = i;
}
allresources[ruleset][i] = item;
if (ruleset == Ruleset_None)
for (j = Ruleset_None ; j < Ruleset_Count ; ++j)
allresources[j][i] = item;
}
fileclose(&file, NULL);
return TRUE;
}
/*
* Resource-loading functions
*/
/* Parse the color-definition resource values.
*/
static int loadcolors(void)
{
long bkgnd, text, bold, dim;
char *end;
bkgnd = strtol(resources[RES_CLR_BKGND].str, &end, 16);
if (*end || bkgnd < 0 || bkgnd > 0xFFFFFF) {
warn("rc: invalid color ID for background");
bkgnd = -1;
}
text = strtol(resources[RES_CLR_TEXT].str, &end, 16);
if (*end || text < 0 || text > 0xFFFFFF) {
warn("rc: invalid color ID for text");
text = -1;
}
bold = strtol(resources[RES_CLR_BOLD].str, &end, 16);
if (*end || bold < 0 || bold > 0xFFFFFF) {
warn("rc: invalid color ID for bold text");
bold = -1;
}
dim = strtol(resources[RES_CLR_DIM].str, &end, 16);
if (*end || dim < 0 || dim > 0xFFFFFF) {
warn("rc: invalid color ID for dim text");
dim = -1;
}
setcolors(bkgnd, text, bold, dim);
return TRUE;
}
/* Attempt to load the tile images.
*/
static int loadimages(void)
{
char *path;
int f;
f = FALSE;
path = getpathbuffer();
if (*resources[RES_IMG_TILES].str) {
combinepath(path, resdir, resources[RES_IMG_TILES].str);
f = loadtileset(path, TRUE);
}
if (!f && resources != globalresources
&& *globalresources[RES_IMG_TILES].str) {
combinepath(path, resdir, globalresources[RES_IMG_TILES].str);
f = loadtileset(path, TRUE);
}
free(path);
if (!f)
errmsg(resdir, "no valid tilesets found");
return f;
}
/* Load the font resource.
*/
static int loadfont(void)
{
char *path;
int f;
f = FALSE;
path = getpathbuffer();
if (*resources[RES_IMG_FONT].str) {
combinepath(path, resdir, resources[RES_IMG_FONT].str);
f = loadfontfromfile(path, TRUE);
}
if (!f && resources != globalresources
&& *globalresources[RES_IMG_FONT].str) {
combinepath(path, resdir, globalresources[RES_IMG_FONT].str);
f = loadfontfromfile(path, TRUE);
}
free(path);
if (!f)
errmsg(resdir, "no valid font found");
return f;
}
/* Load the list of unsolvable levels.
*/
typedef int (*txtloader)(char const * fname);
static int loadtxtresource(int resid, txtloader loadfunc)
{
char const *filename;
if (*resources[resid].str)
filename = resources[resid].str;
else if (resources != globalresources
&& *globalresources[resid].str)
filename = globalresources[resid].str;
else
return FALSE;
return loadfunc(filename);
}
/* Load all of the sound resources.
*/
static int loadsounds(void)
{
char *path;
int count;
int n, f;
path = getpathbuffer();
count = 0;
for (n = 0 ; n < SND_COUNT ; ++n) {
f = FALSE;
if (*resources[RES_SND_BASE + n].str) {
combinepath(path, resdir, resources[RES_SND_BASE + n].str);
f = loadsfxfromfile(n, path);
}
if (!f && resources != globalresources
&& *globalresources[RES_SND_BASE + n].str) {
combinepath(path, resdir, globalresources[RES_SND_BASE + n].str);
f = loadsfxfromfile(n, path);
}
if (f)
++count;
}
free(path);
return count;
}
/* Load all resources that are available. FALSE is returned if the
* tile images could not be loaded. (Sounds are not required in order
* to run, and by this point we should already have a valid font and
* color scheme set.)
*/
int loadgameresources(int ruleset)
{
currentruleset = ruleset;
resources = allresources[ruleset];
loadcolors();
loadfont();
if (!loadimages())
return FALSE;
if (loadsounds() == 0)
setaudiosystem(FALSE);
return TRUE;
}
/* Parse the rc file and load the font and color scheme. FALSE is returned
* if an error occurs.
*/
int initresources(void)
{
initresourcedefaults();
resources = allresources[Ruleset_None];
if (!readrcfile() || !loadcolors() || !loadfont())
return FALSE;
loadtxtresource(RES_TXT_UNSLIST, loadunslistfromfile);
loadtxtresource(RES_TXT_MESSAGE, loadmessagesfromfile);
return TRUE;
}
/* Free all resources.
*/
void freeallresources(void)
{
int n;
freefont();
freetileset();
clearunslist();
for (n = 0 ; n < SND_COUNT ; ++n)
freesfx(n);
}