Debian Codegraph @

terps/advsys/advdbs.c

raw symbols SCIP
1 /* advdbs.c - adventure database access routines */
2 /*
3 Copyright (c) 1986, by David Michael Betz
4 All rights reserved
5 */
6
7 #include "header.h"
8 #include "advint.h"
9 #include "advdbs.h"
10 #include <setjmp.h>
11 #include <string.h>
12
13 #define RMODE 0x8001
14
15 /* global variables */
16 int h_init; /* initialization code */
17 int h_update; /* update code */
18 int h_before; /* before handler code */
19 int h_after; /* after handler code */
20 int h_error; /* error handling code */
21 strid_t datafd; /* data file descriptor */
22
23 /* external variables */
24 extern jmp_buf restart;
25
26 /* table base addresses */
27 char *wtable; /* word table */
28 char *wtypes; /* word type table */
29 int wcount; /* number of words */
30 char *otable; /* object table */
31 int ocount; /* number of objects */
32 char *atable; /* action table */
33 int acount; /* number of actions */
34 char *vtable; /* variable table */
35 int vcount; /* number of variables */
36 char *data; /* base of data tables */
37 char *base; /* current base address */
38 char *dbase; /* base of the data space */
39 char *cbase; /* base of the code space */
40 glui32 length; /* length of resident data structures */
41
42 /* data file header */
43 static char hdr[HDR_SIZE];
44
45 /* save parameters */
46 static long saveoff; /* save data file offset */
47 static char *save; /* save area base address */
48 static glui32 slen; /* save area length */
49
50 /* db_init - read and decode the data file header */
51 void db_init(strid_t realfd)
52 {
53 int woff,ooff,aoff,voff,n;
54
55 /* open the data file */
56 datafd = realfd; // glk_stream_open_file(name, filemode_Read, 0);
57
58 // if (datafd == NULL)
59 // error("can't open data file");
60
61 /* read the header */
62 if (glk_get_buffer_stream(datafd,hdr,HDR_SIZE) != HDR_SIZE)
63 error("bad data file");
64
65 complement(hdr,HDR_SIZE);
66 base = hdr;
67
68 /* check the magic information */
69 if (strncmp(&hdr[HDR_MAGIC],"ADVSYS",6) != 0)
70 error("not an adventure data file");
71
72 /* check the version number */
73 if ((n = getword(HDR_VERSION)) < 101 || n > VERSION)
74 error("wrong version number");
75
76 /* decode the resident data length header field */
77 length = getword(HDR_LENGTH);
78
79 /* allocate space for the resident data structure */
80 if ((data = malloc(length)) == 0)
81 error("insufficient memory");
82
83 /* compute the offset to the data */
84 saveoff = (long)getword(HDR_DATBLK) * 512L;
85
86 /* read the resident data structure */
87 glk_stream_set_position(datafd,saveoff,seekmode_Start);
88 if (glk_get_buffer_stream(datafd,data,length) != length)
89 error("bad data file");
90 complement(data,length);
91
92 /* get the table base addresses */
93 wtable = data + (woff = getword(HDR_WTABLE));
94 wtypes = data + getword(HDR_WTYPES) - 1;
95 otable = data + (ooff = getword(HDR_OTABLE));
96 atable = data + (aoff = getword(HDR_ATABLE));
97 vtable = data + (voff = getword(HDR_VTABLE));
98
99 /* get the save data area */
100 saveoff += (long)getword(HDR_SAVE);
101 save = data + getword(HDR_SAVE);
102 slen = getword(HDR_SLEN);
103
104 /* get the base of the data and code spaces */
105 dbase = data + getword(HDR_DBASE);
106 cbase = data + getword(HDR_CBASE);
107
108 /* initialize the message routines */
109 msg_init(datafd,getword(HDR_MSGBLK));
110
111 /* get the code pointers */
112 h_init = getword(HDR_INIT);
113 h_update = getword(HDR_UPDATE);
114 h_before = getword(HDR_BEFORE);
115 h_after = getword(HDR_AFTER);
116 h_error = getword(HDR_ERROR);
117
118 /* get the table lengths */
119 base = data;
120 wcount = getword(woff);
121 ocount = getword(ooff);
122 acount = getword(aoff);
123 vcount = getword(voff);
124
125 /* setup the base of the resident data */
126 base = dbase;
127
128 /* set the object count */
129 setvalue(V_OCOUNT,ocount);
130
131 /* CHANGED FOR GLK */
132
133 #ifdef GARGLK
134 garglk_set_story_name(&hdr[HDR_ANAME]);
135 #endif
136
137 #ifdef WINDOWS
138 {
139 int i;
140 char *buf;
141
142 i = strlen(&hdr[HDR_ANAME]);
143 i += 1;
144 i += strlen("GLK AdvSys - ");
145
146 buf = malloc( sizeof(char) * i );
147 wsprintf(buf, "GLK AdvSys - %s", &hdr[HDR_ANAME]);
148 winglk_window_set_title(buf);
149 }
150 #endif
151 }
152
153 /* db_save - save the current database */
154 int db_save()
155 {
156 return (advsave(&hdr[HDR_ANAME],20,save,slen) ? T : NIL);
157 }
158
159 /* db_restore - restore a saved database */
160 int db_restore()
161 {
162 return (advrestore(&hdr[HDR_ANAME],20,save,slen) ? T : NIL);
163 }
164
165 /* db_restart - restart the current game */
166 int db_restart()
167 {
168 glk_stream_set_position(datafd,saveoff,seekmode_Start);
169 if (glk_get_buffer_stream(datafd,save,slen) != slen)
170 return (NIL);
171 complement(save,slen);
172 setvalue(V_OCOUNT,ocount);
173
174 longjmp(restart,1);
175 }
176
177 /* complement - complement a block of memory */
178 void complement(char *adr,int len)
179 {
180 for (; len--; adr++)
181 *adr = ~(*adr + 30);
182 }
183
184 /* findword - find a word in the dictionary */
185 int findword(char *word)
186 {
187 char sword[WRDSIZE+1];
188 int wrd,i;
189
190 /* shorten the word */
191 strncpy(sword,word,WRDSIZE); sword[WRDSIZE] = 0;
192
193 /* look up the word */
194 for (i = 1; i <= wcount; i++) {
195 wrd = getwloc(i);
196 if (strcmp(base+wrd+2,sword) == 0)
197 return (getword(wrd));
198 }
199 return (NIL);
200 }
201
202 /* wtype - return the type of a word */
203 int wtype(int wrd)
204 {
205 return (wtypes[wrd]);
206 }
207
208 /* match - match an object against a name and list of adjectives */
209 int match(int obj,int noun,int *adjs)
210 {
211 int *aptr;
212
213 if (!hasnoun(obj,noun))
214 return (FALSE);
215 for (aptr = adjs; *aptr != NIL; aptr++)
216 if (!hasadjective(obj,*aptr))
217 return (FALSE);
218 return (TRUE);
219 }
220
221 /* checkverb - check to see if this is a valid verb */
222 int checkverb(int *verbs)
223 {
224 int act;
225
226 /* look up the action */
227 for (act = 1; act <= acount; act++)
228 if (hasverb(act,verbs))
229 return (act);
230 return (NIL);
231 }
232
233 /* findaction - find an action matching a description */
234 int findaction(int *verbs,int preposition,int flag)
235 {
236 int act,mask;
237
238 /* look up the action */
239 for (act = 1; act <= acount; act++) {
240 if (preposition && !haspreposition(act,preposition))
241 continue;
242 if (!hasverb(act,verbs))
243 continue;
244 mask = ~getabyte(act,A_MASK);
245 if ((flag & mask) == (getabyte(act,A_FLAG) & mask))
246 return (act);
247 }
248 return (NIL);
249 }
250
251 /* getp - get the value of an object property */
252 int getp(int obj,int prop)
253 {
254 int p;
255
256 for (; obj; obj = getofield(obj,O_CLASS))
257 if (p = findprop(obj,prop))
258 return (getofield(obj,p));
259 return (NIL);
260 }
261
262 /* setp - set the value of an object property */
263 int setp(int obj,int prop,int val)
264 {
265 int p;
266
267 for (; obj; obj = getofield(obj,O_CLASS))
268 if (p = findprop(obj,prop))
269 return (putofield(obj,p,val));
270 return (NIL);
271 }
272
273 /* findprop - find a property */
274 int findprop(int obj,int prop)
275 {
276 int n,i,p;
277
278 n = getofield(obj,O_NPROPERTIES);
279 for (i = p = 0; i < n; i++, p += 4)
280 if ((getofield(obj,O_PROPERTIES+p) & ~P_CLASS) == prop)
281 return (O_PROPERTIES+p+2);
282 return (NIL);
283 }
284
285 /* hasnoun - check to see if an object has a specified noun */
286 int hasnoun(int obj,int noun)
287 {
288 while (obj) {
289 if (inlist(getofield(obj,O_NOUNS),noun))
290 return (TRUE);
291 obj = getofield(obj,O_CLASS);
292 }
293 return (FALSE);
294 }
295
296 /* hasadjective - check to see if an object has a specified adjective */
297 int hasadjective(int obj,int adjective)
298 {
299 while (obj) {
300 if (inlist(getofield(obj,O_ADJECTIVES),adjective))
301 return (TRUE);
302 obj = getofield(obj,O_CLASS);
303 }
304 return (FALSE);
305 }
306
307 /* hasverb - check to see if this action has this verb */
308 int hasverb(int act,int *verbs)
309 {
310 int link,word,*verb;
311
312 /* get the list of verbs */
313 link = getafield(act,A_VERBS);
314
315 /* look for this verb */
316 while (link != NIL) {
317 verb = verbs;
318 word = getword(link+L_DATA);
319 while (*verb != NIL && word != NIL) {
320 if (*verb != getword(word+L_DATA))
321 break;
322 verb++;
323 word = getword(word+L_NEXT);
324 }
325 if (*verb == NIL && word == NIL)
326 return (TRUE);
327 link = getword(link+L_NEXT);
328 }
329 return (FALSE);
330 }
331
332 /* haspreposition - check to see if an action has a specified preposition */
333 int haspreposition(int act, int preposition)
334 {
335 return (inlist(getafield(act,A_PREPOSITIONS),preposition));
336 }
337
338 /* inlist - check to see if a word is an element of a list */
339 int inlist(int link, int word)
340 {
341 while (link != NIL) {
342 if (word == getword(link+L_DATA))
343 return (TRUE);
344 link = getword(link+L_NEXT);
345 }
346 return (FALSE);
347 }
348
349 /* getofield - get a field from an object */
350 int getofield(int obj, int off)
351 {
352 return (getword(getoloc(obj)+off));
353 }
354
355 /* putofield - put a field into an object */
356 int putofield(int obj,int off,int val)
357 {
358 return (putword(getoloc(obj)+off,val));
359 }
360
361 /* getafield - get a field from an action */
362 int getafield(int act,int off)
363 {
364 return (getword(getaloc(act)+off));
365 }
366
367 /* getabyte - get a byte field from an action */
368 int getabyte(int act,int off)
369 {
370 return (getbyte(getaloc(act)+off));
371 }
372
373 /* getoloc - get an object from the object table */
374 int getoloc(int n)
375 {
376 if (n < 1 || n > ocount)
377 nerror("object number out of range: %d",n);
378 return (getdword(otable+n+n));
379 }
380
381 /* getaloc - get an action from the action table */
382 int getaloc(int n)
383 {
384 if (n < 1 || n > acount)
385 nerror("action number out of range: %d",n);
386 return (getdword(atable+n+n));
387 }
388
389 /* getvalue - get the value of a variable from the variable table */
390 int getvalue(int n)
391 {
392 if (n < 1 || n > vcount)
393 nerror("variable number out of range: %d",n);
394 return (getdword(vtable+n+n));
395 }
396
397 /* setvalue - set the value of a variable in the variable table */
398 int setvalue(int n, int v)
399 {
400 if (n < 1 || n > vcount)
401 nerror("variable number out of range: %d",n);
402 return (putdword(vtable+n+n,v));
403 }
404
405 /* getwloc - get a word from the word table */
406 int getwloc(int n)
407 {
408 if (n < 1 || n > wcount)
409 nerror("word number out of range: %d",n);
410 return (getdword(wtable+n+n));
411 }
412
413 /* getword - get a word from the data array */
414 int getword(int n)
415 {
416 return (getdword(base+n));
417 }
418
419 /* putword - put a word into the data array */
420 int putword(int n,int w)
421 {
422 return (putdword(base+n,w));
423 }
424
425 /* getbyte - get a byte from the data array */
426 int getbyte(int n)
427 {
428 return (*(base+n) & 0xFF);
429 }
430
431 /* getcbyte - get a code byte */
432 int getcbyte(int n)
433 {
434 return (*(cbase+n) & 0xFF);
435 }
436
437 /* getcword - get a code word */
438 int getcword(int n)
439 {
440 return (getdword(cbase+n));
441 }
442
443 /* getdword - get a word from the data array */
444 int getdword(char *p)
445 {
446 return (((*p & 0xFF) | (*(p+1) << 8))&0xFFFF);
447 }
448
449 /* putdword - put a word into the data array */
450 int putdword(char *p,int w)
451 {
452 *p = w; *(p+1) = w >> 8;
453 return (w);
454 }
455
456 /* nerror - handle errors with numeric arguments */
457 void nerror(char *fmt,int n)
458 {
459 char buf[100];
460 sprintf(buf,fmt,n);
461 error(buf);
462 }