Debian Codegraph @

terps/agility/agxfile.c

raw symbols SCIP
1 /* agxfile.c-- functions to read and write AGX files */
2 /* */
3 /* Copyright (C) 1996-1999,2001 Robert Masenten */
4 /* This program may be redistributed under the terms of the
5 GNU General Public License, version 2; see agility.h for details. */
6 /* */
7 /* This is part of the source for both AGiliTy: the (Mostly) Universal */
8 /* AGT Interpreter and for the Magx adventure game compiler. */
9
10 /* #define DEBUG_AGX */
11
12
13
14 /* NOTES ON CHANGING THE AGX FILE FORMAT
15
16 First of all, don't.
17
18 One of the benefits of adventure creation systems like this is
19 that the same game files can be played on a variety of different
20 platforms without any extra effort on the part of the game
21 author. If you change the file format, this is no longer true: games
22 created under the new format won't run on the old interpreters.
23
24 Even if you distribute a new interpreter with your game, there are two
25 problems:
26
27 i) People on other platforms won't be able to play your game unless
28 and until your modified interpreter is ported to their machine. Since
29 I-F players as a group tend to use a wider range of different computers
30 and operating systems than the population at large, this is bad.
31
32 ii) Even for machines that you port your modified interpreter to,
33 people will now need to maintain two interpreters: the original one
34 (for most of the games) and your modified one (for your new game).
35 This is not only a nuisance but it wastes disk space.
36
37
38 If you *do* decide to change the file format anyhow, please adhere to
39 the following guidelines, to minimize confusion.
40
41 GUIDLINES FOR NEW FILE FORMAT VERSIONS
42
43 File format version are labled by a series of four bytes near the
44 beginning of the file. (They are actually the fifth, sixth, seventh,
45 and eight bytes-- the first four bytes are the file format signature
46 that indicate the file is an AGX file and not, say, a PCX file)
47
48 In order, they are the version owner id and number, and the
49 extension owner id and number. In "vanilla" AGX, both owner id's are
50 'R', the version number is 2, and the extension number is 1 (the
51 extension associated with AGiliTy 1.0). (There are AGX files with
52 version numbers 0 and 1 created by earlier releases of agt2agx. In
53 fact, for downward compatibility, agt2agx will sometimes create a file
54 of version 1 and extnum 7 if later features aren't being used.)
55
56 I will discuss the difference between "versions" and "extensions"
57 further below, but briefly: extensions are minor changes whereas versions
58 represent vast reoganizations of the file format. The routines below
59 will still try to read in a file with an unrecognized extension, but
60 they will give up on an unrecognized version.
61
62 If you create a new extension, then you should first change the
63 extension owner id to something else; the owner id is intended to
64 indicate who is responsible for defining this extension; 'R' indicates
65 Robert Masenten (the author of this file) and so shouldn't be used by anyone
66 else. The id isn't required to be a printable character.
67
68 You can then define the extension number however you want. The
69 extension number is intended to differentiate between different
70 extensions defined by the same source (e.g. two extensions both
71 defined by me would have the same owner id but different extension
72 numbers). The extensions that I define are numbered sequentially
73 starting at 0, but you don't have to follow this convention if you
74 don't want to.
75
76 Finally, send me an e-mail note telling me what you've done so I can
77 keep track of the different extensions and prevent conflicts between
78 owner-ids.
79
80 Creating a new version works the same way: change the version owner-id to
81 something no one is using and set the version number however you want.
82 If you're defining a new version, you can do whatever you want with
83 the two extension bytes.
84
85
86 EXTENSIONS AND VERSIONS
87
88 For purposes of the file format, an 'extension' is a change to the
89 format that follows certain restrictions given below; a 'version' is one that
90 violates one or more of these restrictions.
91
92 If at all possible you should try to fit your changes to the format
93 within the limitations of an 'extension': it is more likely that other
94 programs will work with your new file format and it is also more
95 likely that your modified interpreter will still be able to understand
96 the original file format.
97
98 An extension shouldn't change any of the existing data fields; nor
99 should it insert new data fields into the middle of records. An
100 extension *may* add new fields onto the end of one or more of the
101 records and it can define new blocks.
102
103 Examples of things that would be extensions (create a new extension
104 id and number, but keep the version the same):
105
106 --Adding a new field onto the end of the creature record, containing
107 the code for a sound file that should be played whenever
108 the creature is in the room.
109
110 --Adding a new block to the file containing debugging information for
111 the new AGT compiler you've just written, numbered 35.
112
113
114 Things that would *not* be extensions (create a new version id and
115 number; do what you want with the extension id and number)
116
117 --Going to 32-bit object numbers for everything. (There *are*
118 sneaky ways you could make this an extension; but not if you just do this
119 by converting all the int16 fields in the file to int32)
120
121 --Changing metacommands to accept an arbitrary string of tokens
122 instead of just ACTOR,VERB NOUN PREP OBJECT.
123
124
125
126 A FEW NOTES ON BACKWARD COMPATIBILITY
127
128 (These notes only apply if you are creating an extension; if you are
129 creating a new version, then anything goes)
130
131 If you add a new field onto an existing record (like the creature
132 soundtrack example above) and read in an old-format file, then the new
133 data fields will be automatically initialized to zero so long as none
134 of them are individually longer than 81 bytes (if any *are* longer than
135 81 bytes then the file routines may break). (There is nothing magic
136 about 81 bytes; it just happens to be the length of the largest data
137 structure that shows up in 'vanilla' AGX files).
138
139 If you add a new block, then you should check the extension number
140 of the file and if the block doesn't exists be prepared to either
141 initialize the data to something sensible or to exit cleanly with an
142 error message.
143
144 */
145
146 /* AGX File format versions and corresponding versions of AGiliTy
147 and Magx: (versions given as 'Version-Extension')
148
149 AGX AGiliTy Magx
150 0-0 0.5
151 0-1 0.7 0.1
152 1-0 0.7.2 0.2
153 1-1 0.8
154 1-2 0.8.1 0.3
155 1-3 0.8.3 0.4
156 1-4 0.8.5
157 1-5 0.8.6 0.5
158 1-6 0.8.7 0.5.2
159 1-7 0.8.8 0.6
160 2-0 0.8.9 0.6.1
161 2-1 1.0 0.6.3
162 2-2 1.1 0.6.4
163 */
164
165 /* Changes is AGX File format versions:
166 0-0: Original format.
167 0-1: Add
168 PURE_TIME, PURE_OBJ_DESC, exitmsg_base
169 noun.related_name
170 command.noun_adj, command.obj_adj
171 1-0: Change index file format, fixing a bug
172 1-1: Add
173 Multi-word verb block(28)
174 Preposition block(29)
175 (room|noun|creature).oclass
176 1-2: Add (room|noun|creature).unused
177 1-3: Add PURE_GRAMMAR
178 1-4: Add (noun|creature).isglobal and (noun|creature).flagnum
179 Add TWO_CYCLE
180 1-5: Add min_ver
181 Add PURE_AFTER
182 1-6: Add (noun|creature).seen
183 1-7: Add objflag and objprop blocks (with corrosponding
184 support data in the gameinfo block).
185 2-0: No change in file format; version upped to protect against
186 a bug in early versions of the AGX file code.
187 2-1: Added (noun|creature).proper
188 2-2: Added noun_obj and obj_obj to cmd header
189 Added objflag.ystr, objflag.nstr, objprop.str_cnt, objprop.str_list
190 Added propstr block.
191 Added fallback_ext to file header.
192 */
193
194 #include "agility.h"
195 #include <assert.h>
196 #include <ctype.h>
197 #include <stddef.h>
198
199 #define AGX_NUMBLOCK 37
200 #define AGT_FILE_SIG 0x51C1C758L
201
202 /* AGX File format:
203 (This tends to lag a little behind the code below;
204 you should double check against the actual file_info definitions
205 below)
206 All integer values stored little-endian.
207 desc_ptrs: int32 ofs, int32 leng (both in lines)
208 dictionary word: int16
209 slist ptr: int16
210 tline: char[81]
211 filename: char[10]
212 rbool values are packed into bytes, 1 bit per value, from lsb to msb.
213 cfgopt: 0=false, 1=true, 2=leave alone
214
215 Mandatory blocks are marked with astericks.
216
217 *File header: 16 bytes
218 uint 32 File ID [AGT_FILE_SIG, 4 bytes]
219 byte Version owner: 'R'
220 byte Version 0
221 byte Extension owner 'R'
222 byte Extension 0
223 char[2]: '\n\r' -- to catch download errors
224 byte Extension fallback. For non-'R' extensions, this gives the
225 'R' extension to fall back to.
226 char[5] Reserved for future use (should be 0 right now)
227 *0-File index:
228 For each block (including itself): [16 bytes]
229 uint32 starting offset
230 uint32 block size
231 uint32 number of records
232 uint32 size of a record (recsize*numrec == blocksize)
233 11-Description strings (block of tline)
234 12-Command text (block of int16)
235 *1-Game header
236 uint16 AGT_version_code; +1 for "big/soggy" games
237 uint32 game_sig (game signature, used to check save files and debug info)
238 rbool debug_mode, freeze_mode, milltime_mode, bold_mode,
239 have_meta, mars_fix, intro_first, TWO_CYCLE;
240 uchar score_mode, statusmode;
241 uint16 max_lives
242 uint32 max_score;
243 uint16 start_time, delta_time;
244 descr_ptr intro_ptr, title_ptr, ins_ptr;
245 int16 start_room, treas_room, resurrect_room
246 int16 first_room, first_noun, first_creat
247 int16 FLAG_NUM, CNT_NUM, VAR_NUM
248 int16 BASE_VERB
249 cfgopt PURE_ANSWER, PURE_TIME, PURE_ROOMTITLE;
250 cfgopt PURE_AND, PURE_METAVERB;
251 cfgopt PURE_SYN, PURE_NOUN, PURE_ADJ;
252 cfgopt PURE_DUMMY, PURE_SUBNAME, PURE_PROSUB;
253 cfgopt PURE_HOSTILE, PURE_GETHOSTILE;
254 cfgopt PURE_DISAMBIG, PURE_ALL;
255 cfgopt irun_mode, verboseflag;
256 cfgopt PURE_GRAMMAR (Extension R1-R3)
257 rbool TWO_CYCLE (R1-R4)
258 PURE_AFTER (R1-R5)
259 int16 min_ver
260 uchar font_status;
261 int16 num_rflags, num_nflags, num_cflags;
262 int16 num_rprops, num_nprops, num_cprops;
263 2-Room data (room_rec format, pointers->int ref into static string)
264 include help, desc, special ptrs
265 3-Noun data (noun_rec format)
266 include noun, text, turn, push, pull, play ptrs
267 4-Creature data (creat_rec format)
268 include creature, talk, ask ptrs
269 5-Command headers (cmd_rec format), pointers into command text
270 must be in increasing order.
271 6-Standard error message ptrs (array of descptr
272 7-Message ptrs (array of descptr)
273 8-Question pointers (array of descptr)
274 9-Answer pointers (array of descptr)
275 10-User strings (array of tline)
276 *13-Static string block (block of chars)
277 14-Subroutine dictionary ids (array of word:int16)
278 *15-Synlist (for verbs) (array of slist:int16)
279 16-Pix names (array of word:int16 -- pointers into dictionary)
280 17-Global nouns (array of word:int16 -- ptrs into dictionary)
281 18-Flag nouns (array of word:int16)
282 *19-Syntbl (block of word:int16)
283 *20-Dictionary text (block of char)
284 *21-Dictionary 'index' (array of uint32)
285 22-OPT block (14 bytes)
286 23-Picture filename ptrs
287 24-Pix filename ptrs
288 25-Font filename ptrs
289 26-Sound filename ptrs
290 27-VOC block, an array of verbinfo_rec
291 28-Multi-word verbs (Extension R1-R1)
292 29-Prep table (Extension R1-R1)
293 30- ObjFlag Data (Extension R1-R7)
294 31- ObjProp Data (Extension R1-R7)
295 32- ObjFlag Lookup (Extension R1-R7)
296 33- ObjProp Lookup (Extension R1-R7)
297 34- ObjProp string pointers (array of FT_STR) (Extension R2-R2)
298 35- Variable itemization array (Extension R2-R2)
299 36- Flag itemization array (Extension R2-R2)
300
301 */
302
303 /* AGT Version IDs; +1 for LARGE/SOGGY
304 00000=v1.0
305 01800=v1.18
306 01900=v1.19
307 02000=v1.20 ("Early Classic")
308 03200=v1.32/COS
309 03500=v1.35 ("Classic")
310 05000=v1.5/H
311 05050=v1.5/F (MDT)
312 05070=v1.6 (PORK)
313 08200=v1.82
314 08300=v1.83
315 10000=ME/1.0
316 15000=ME/1.5
317 15500=ME/1.55
318 16000=ME/1.6
319 20000=Magx/0.0
320 etc.
321 */
322
323
324
325
326 /* ------------------------------------------------------------- */
327 /* AGX Block Descriptions */
328 /* ------------------------------------------------------------- */
329
330
331 static integer old_base_verb;
332
333 /* AGX file info blocks */
334
335 #define g(ft,dt,var) {ft,dt,&var,0}
336 #define r(ft,dt,str,f) {ft,dt,NULL,offsetof(str,f)}
337 #define dptype {FT_DESCPTR,DT_DESCPTR,NULL,0}
338 #define xx DT_DEFAULT
339 #define u16 FT_UINT16
340 #define u32 FT_UINT32
341 #define bb FT_BOOL
342 #define i16 FT_INT16
343
344 static file_info fi_gameinfo[]={
345 /* 0 */
346 g(FT_VERSION,xx,aver), /* FT_VERSION converter also sets ver */
347 g(u32,DT_LONG,game_sig),
348 /* 6 */
349 g(bb,xx,debug_mode), g(bb,xx,freeze_mode), g(bb,xx,milltime_mode),
350 g(bb,xx,bold_mode), g(bb,xx,have_meta), g(bb,xx,mars_fix),
351 g(bb,xx,intro_first),g(bb,xx,box_title),
352 /* 7 */
353 g(FT_BYTE,xx,score_mode), g(FT_BYTE,xx,statusmode),
354 g(i16,xx,max_lives), g(u32,DT_LONG,max_score),
355 /* 15 */
356 g(i16,xx,start_time),g(i16,xx,delta_time),
357 /* 19 */
358 g(FT_DESCPTR,xx,intro_ptr), g(FT_DESCPTR,xx,title_ptr),
359 g(FT_DESCPTR,xx,ins_ptr),
360 /* 43 */
361 g(i16,xx,treas_room),
362 g(i16,xx,start_room),g(i16,xx,resurrect_room),
363 g(i16,xx,first_room), g(i16,xx,first_noun),
364 g(i16,xx,first_creat), g(i16,xx,FLAG_NUM),
365 g(i16,xx,CNT_NUM), g(i16,xx,VAR_NUM),
366 g(i16,xx,old_base_verb),
367 /* 63 */
368 g(FT_CFG,xx,PURE_ANSWER), g(FT_CFG,xx,PURE_ROOMTITLE),
369 g(FT_CFG,xx,PURE_AND), g(FT_CFG,xx,PURE_METAVERB),
370 g(FT_CFG,xx,PURE_SYN), g(FT_CFG,xx,PURE_NOUN), g(FT_CFG,xx,PURE_ADJ),
371 g(FT_CFG,xx,PURE_DUMMY), g(FT_CFG,xx,PURE_SUBNAME),
372 g(FT_CFG,xx,PURE_PROSUB), g(FT_CFG,xx,PURE_HOSTILE),
373 g(FT_CFG,xx,PURE_GETHOSTILE), g(FT_CFG,xx,PURE_DISAMBIG),
374 g(FT_CFG,xx,PURE_ALL),
375 g(FT_CFG,xx,irun_mode),g(FT_CFG,xx,verboseflag),
376 g(FT_CFG,xx,PURE_TIME), /* Ext R0-1 */
377 g(FT_CFG,xx,PURE_OBJ_DESC), /* Ext R0-1 */
378 /* 81 */
379 g(i16,xx,exitmsg_base), /* Ext R0-1 */
380 /* 83 */
381 g(FT_CFG,xx,PURE_GRAMMAR), /* Ext R1-3 */
382 g(bb,xx,TWO_CYCLE), /* Ext R1-4 */
383 g(bb,xx,PURE_AFTER), /* Ext R1-5 */
384 g(i16,xx,min_ver), /* Ext R1-5 */
385 g(FT_BYTE,xx,font_status), /* Ext R1-5 */
386 g(i16,xx,num_rflags),g(i16,xx,num_nflags),g(i16,xx,num_cflags),/* Ext R1-7 */
387 g(i16,xx,num_rprops),g(i16,xx,num_nprops),g(i16,xx,num_cprops),/* Ext R1-7 */
388 endrec
389 };
390
391 static file_info fi_room[]={
392 dptype, /* help */
393 dptype, /* desc */
394 dptype, /* special */
395 r(FT_STR,xx,room_rec,name),
396 r(FT_INT32,xx,room_rec,flag_noun_bits),
397 r(FT_INT32,xx,room_rec,PIX_bits),
398 r(FT_SLIST,xx,room_rec,replacing_word),
399 r(FT_WORD,xx,room_rec,replace_word),
400 r(FT_WORD,xx,room_rec,autoverb),
401 r(FT_PATHARRAY,xx,room_rec,path),
402 r(FT_INT16,xx,room_rec,key),
403 r(FT_INT16,xx,room_rec,points),
404 r(FT_INT16,xx,room_rec,light),
405 r(FT_INT16,xx,room_rec,pict),
406 r(FT_INT16,xx,room_rec,initdesc),
407 r(bb,xx,room_rec,seen), r(bb,xx,room_rec,locked_door),
408 r(bb,xx,room_rec,end),r(bb,xx,room_rec,win),r(bb,xx,room_rec,killplayer),
409 r(bb,xx,room_rec,unused), /* Ext R1-2: Can add here since rbool */
410 r(FT_INT16,xx,room_rec,oclass), /* Ext R1-1 */
411 endrec
412 };
413
414 static file_info fi_noun[]={
415 dptype, /* Noun */
416 dptype, /* Text */
417 dptype, dptype, dptype, dptype, /* Turn, push, pull, play */
418 r(FT_STR,xx,noun_rec,shortdesc),
419 r(FT_STR,xx,noun_rec,position),
420 r(FT_SLIST,xx,noun_rec,syns),
421 r(FT_WORD,xx,noun_rec,name), r(FT_WORD,xx,noun_rec,adj),
422 /* r(FT_WORD,xx,noun_rec,pos_prep), r(FT_WORD,xx,noun_rec,pos_name),*/
423 r(FT_INT16,xx,noun_rec,nearby_noun),
424 r(FT_INT16,xx,noun_rec,num_shots), r(FT_INT16,xx,noun_rec,points),
425 r(FT_INT16,xx,noun_rec,weight), r(FT_INT16,xx,noun_rec,size),
426 r(FT_INT16,xx,noun_rec,key),
427 r(FT_INT16,xx,noun_rec,initdesc), r(FT_INT16,xx,noun_rec,pict),
428 r(FT_INT16,xx,noun_rec,location),
429 r(bb,xx,noun_rec,plural),
430 r(bb,xx,noun_rec,something_pos_near_noun),
431 r(bb,xx,noun_rec,has_syns),
432 r(bb,xx,noun_rec,pushable), r(bb,xx,noun_rec,pullable),
433 r(bb,xx,noun_rec,turnable), r(bb,xx,noun_rec,playable),
434 r(bb,xx,noun_rec,readable), r(bb,xx,noun_rec,on),
435 r(bb,xx,noun_rec,closable), r(bb,xx,noun_rec,open),
436 r(bb,xx,noun_rec,lockable), r(bb,xx,noun_rec,locked),
437 r(bb,xx,noun_rec,edible), r(bb,xx,noun_rec,wearable),
438 r(bb,xx,noun_rec,drinkable), r(bb,xx,noun_rec,poisonous),
439 r(bb,xx,noun_rec,movable), r(bb,xx,noun_rec,light),
440 r(bb,xx,noun_rec,shootable), r(bb,xx,noun_rec,win),
441 r(bb,xx,noun_rec,unused), /* Ext R1-2: Can add here since packed rbool*/
442 r(bb,xx,noun_rec,isglobal), /* Ext R1-4: ditto (&room for 1 more). */
443 r(FT_WORD,xx,noun_rec,related_name), /* Ext R0-1 */
444 r(FT_INT16,xx,noun_rec,oclass), /* Ext R1-1 */
445 r(FT_INT16,xx,noun_rec,flagnum), /* Ext R1-4 */
446 r(bb,xx,noun_rec,seen), /* Ext R1-6 */
447 r(bb,xx,noun_rec,proper), /* Ext R2-1 */
448 endrec
449 };
450
451 static file_info fi_creat[]={
452 dptype, /* Creature */
453 dptype, dptype, /* Talk, ask */
454 r(FT_STR,xx,creat_rec,shortdesc),
455 r(FT_SLIST,xx,creat_rec,syns),
456 r(FT_WORD,xx,creat_rec,name),r(FT_WORD,xx,creat_rec,adj),
457 r(FT_INT16,xx,creat_rec,location),
458 r(FT_INT16,xx,creat_rec,weapon), r(FT_INT16,xx,creat_rec,points),
459 r(FT_INT16,xx,creat_rec,counter), r(FT_INT16,xx,creat_rec,threshold),
460 r(FT_INT16,xx,creat_rec,timethresh), r(FT_INT16,xx,creat_rec,timecounter),
461 r(FT_INT16,xx,creat_rec,pict), r(FT_INT16,xx,creat_rec,initdesc),
462 r(bb,xx,creat_rec,has_syns), r(bb,xx,creat_rec,groupmemb),
463 r(bb,xx,creat_rec,hostile),
464 r(bb,xx,creat_rec,unused), /* Ext R1-2: Can add since packed rbool */
465 r(bb,xx,creat_rec,isglobal), /* Ext R1-4: ditto (& space for 3 more) */
466 r(FT_BYTE,xx,creat_rec,gender),
467 r(FT_INT16,xx,creat_rec,oclass), /* Ext R1-1 */
468 r(FT_INT16,xx,creat_rec,flagnum), /* Ext R1-4 */
469 r(bb,xx,creat_rec,seen), /* Ext R1-6 */
470 r(bb,xx,creat_rec,proper), /* Ext R2-1 */
471 endrec
472 };
473
474 static file_info fi_cmdhead[]={
475 {FT_CMDPTR,DT_CMDPTR,NULL,0},
476 r(FT_INT16,xx,cmd_rec,actor),
477 r(FT_WORD,xx,cmd_rec,verbcmd), r(FT_WORD,xx,cmd_rec,nouncmd),
478 r(FT_WORD,xx,cmd_rec,objcmd), r(FT_WORD,xx,cmd_rec,prep),
479 r(FT_INT16,xx,cmd_rec,cmdsize),
480 r(FT_WORD,xx,cmd_rec,noun_adj), r(FT_WORD,xx,cmd_rec,obj_adj), /* Ext R0-1*/
481 r(FT_INT16,xx,cmd_rec,noun_obj), /* Ext R2-2 */
482 r(FT_INT16,xx,cmd_rec,obj_obj), /* Ext R2-2 */
483 endrec
484 };
485
486 static file_info fi_verbentry[]={
487 r(FT_WORD,xx,verbentry_rec,verb),
488 r(FT_WORD,xx,verbentry_rec,prep),
489 r(FT_INT16,xx,verbentry_rec,objnum),
490 endrec
491 };
492
493
494 static file_info fi_descptr[]={
495 r(FT_INT32,xx,descr_ptr,start),
496 r(FT_INT32,xx,descr_ptr,size),
497 endrec
498 };
499
500 static file_info fi_tline[]={
501 {FT_TLINE,xx,NULL,0},
502 endrec
503 };
504
505 static file_info fi_attrrec[]={ /* Ext R1-R7 */
506 r(FT_INT32,xx,attrdef_rec,r),
507 r(FT_INT32,xx,attrdef_rec,n),
508 r(FT_INT32,xx,attrdef_rec,c),
509 r(FT_BYTE,xx,attrdef_rec,rbit),
510 r(FT_BYTE,xx,attrdef_rec,nbit),
511 r(FT_BYTE,xx,attrdef_rec,cbit),
512 r(FT_STR,xx,attrdef_rec,ystr), /* Ext R2-R2 */
513 r(FT_STR,xx,attrdef_rec,nstr), /* Ext R2-R2 */
514 endrec
515 };
516
517 static file_info fi_proprec[]={ /* Ext R1-R7 */
518 r(FT_INT32,xx,propdef_rec,r),
519 r(FT_INT32,xx,propdef_rec,n),
520 r(FT_INT32,xx,propdef_rec,c),
521 r(FT_INT32,xx,propdef_rec,str_cnt), /* Ext R2-R2 */
522 r(FT_INT32,xx,propdef_rec,str_list), /* Ext R2-R2 */
523 endrec
524 };
525
526 static file_info fi_varrec[]={ /* Ext R2-R2 */
527 r(FT_INT32,xx,vardef_rec,str_cnt),
528 r(FT_INT32,xx,vardef_rec,str_list),
529 endrec
530 };
531
532 static file_info fi_flagrec[]={ /* Ext R2-R2 */
533 r(FT_STR,xx,flagdef_rec,ystr),
534 r(FT_STR,xx,flagdef_rec,nstr),
535 endrec
536 };
537
538 #undef g
539 #undef r
540 #undef xx
541 #undef u16
542 #undef u32
543 #undef bb
544 #undef i16
545 #undef dptype
546
547 static void set_endrec(file_info *fi,int index)
548 {
549 fi[index].ftype=FT_END;
550 fi[index].dtype=0;
551 fi[index].ptr=NULL;
552 fi[index].offset=0;
553 }
554
555 /* ------------------------------------------------------------- */
556
557 /* If <to_intern> is true, convert "0" string ptrs to "yes/no" ptrs.
558 If it is false, convert the other way. */
559 /* This is done for the sake of downward compatibility.
560 It *does* mean that the first string in static_str cannot
561 be an attribute's yes/no string. */
562
563 /* "0" pointers in this case will actually be equal to static_str
564 (since that is the base point for all pointers to static strings.) */
565
566 const char base_yesstr[]="yes";
567 const char base_nostr[]="no";
568
569 static void conv_fstr(const char **s, rbool yes, rbool to_intern)
570 {
571 if (to_intern) { /* Convert to internal form */
572 assert(*s!=NULL);
573 if (*s==static_str) *s= yes ? base_yesstr : base_nostr;
574 } else { /* convert to external form */
575 if (*s==NULL || *s== base_yesstr || *s==base_nostr)
576 *s=static_str;
577 }
578 }
579
580 static void fix_objflag_str(rbool to_intern)
581 {
582 int i;
583 for(i=0;i<oflag_cnt;i++) {
584 conv_fstr(&attrtable[i].ystr,1,to_intern);
585 conv_fstr(&attrtable[i].nstr,0,to_intern);
586 }
587 if (flagtable)
588 for(i=0;i<=FLAG_NUM;i++) {
589 conv_fstr(&flagtable[i].ystr,1,to_intern);
590 conv_fstr(&flagtable[i].nstr,0,to_intern);
591 }
592 }
593
594 /* ------------------------------------------------------------- */
595 /* AGX Reading Code */
596 /* ------------------------------------------------------------- */
597
598
599 static long descr_ofs;
600
601 void agx_close_descr(void)
602 {
603 if (mem_descr!=NULL)
604 rfree(mem_descr);
605 else if (descr_ofs!=-1)
606 buffclose(); /* This closes the whole AGX file */
607 }
608
609 descr_line *agx_read_descr(long start,long size)
610 {
611 long i, line, len;
612 descr_line *txt;
613 char *buff;
614
615 if (size<=0) return NULL;
616
617 if (mem_descr==NULL && descr_ofs!=-1)
618 buff=read_recblock(NULL,FT_CHAR,size,
619 descr_ofs+start, size*ft_leng[FT_CHAR]);
620 else
621 buff=mem_descr+start;
622
623 len=0;
624 for(i=0;i<size;i++) /* Count the number of lines */
625 if (buff[i]==0) len++;
626 txt=rmalloc(sizeof(descr_line)*(len+1));
627 txt[0]=buff;
628 i=0;
629 for(line=1;line<len;) /* Determine where each of the lines is */
630 if (buff[i++]==0)
631 txt[line++]=buff+i;
632 txt[len]=NULL; /* Mark the end of the array */
633 return txt;
634 }
635
636
637 /* We need to read in command text and use cmd_rec[] values to
638 rebuild command[].data. We are guaranteed that cmd_rec[] is in
639 increasing order */
640
641 static void read_command(long cmdcnt, long cmdofs, rbool diag)
642 {
643 long i;
644
645 for(i=0;i<last_cmd;i++) {
646 command[i].data=rmalloc(sizeof(integer)*command[i].cmdsize);
647 read_recblock(command[i].data,FT_INT16,command[i].cmdsize,
648 cmdofs+2*cmd_ptr[i],2*command[i].cmdsize);
649 }
650 if (!diag) rfree(cmd_ptr);
651 }
652
653
654 /* Correct for differences between old_base_verb and BASE_VERB.
655 This means that the interpreter's set of built-inv verbs has changed
656 since the file was created. */
657 static void correct_synlist(void)
658 {
659 int i;
660 if (BASE_VERB==old_base_verb) return; /* Nothing needs to be done */
661
662 /* Need to move everything >= old_base_verb to BASE_VERB */
663 memmove(synlist+BASE_VERB,synlist+old_base_verb,
664 sizeof(slist)*(DVERB+MAX_SUB) );
665
666 if (BASE_VERB<old_base_verb) /* We've _lost_ verbs */
667 agtwarn("Missing built-in verbs.",0);
668
669 /* Now we need to give the "new" verbs empty synonym lists */
670 for(i=old_base_verb;i<BASE_VERB;i++)
671 synlist[i]=synptr;
672 addsyn(-1);
673 }
674
675
676
677 static void set_roomdesc(file_info fi[])
678 {
679 fi[0].ptr=help_ptr=rmalloc(sizeof(descr_ptr)*(maxroom-first_room+1));
680 fi[1].ptr=room_ptr=rmalloc(sizeof(descr_ptr)*(maxroom-first_room+1));
681 fi[2].ptr=special_ptr=rmalloc(sizeof(descr_ptr)*(maxroom-first_room+1));
682 }
683
684 static void wset_roomdesc(file_info fi[])
685 {
686 fi[0].ptr=help_ptr;
687 fi[1].ptr=room_ptr;
688 fi[2].ptr=special_ptr;
689 }
690
691 static void set_noundesc(file_info *fi)
692 {
693 fi[0].ptr=noun_ptr=rmalloc(sizeof(descr_ptr)*(maxnoun-first_noun+1));
694 fi[1].ptr=text_ptr=rmalloc(sizeof(descr_ptr)*(maxnoun-first_noun+1));
695 fi[2].ptr=turn_ptr=rmalloc(sizeof(descr_ptr)*(maxnoun-first_noun+1));
696 fi[3].ptr=push_ptr=rmalloc(sizeof(descr_ptr)*(maxnoun-first_noun+1));
697 fi[4].ptr=pull_ptr=rmalloc(sizeof(descr_ptr)*(maxnoun-first_noun+1));
698 fi[5].ptr=play_ptr=rmalloc(sizeof(descr_ptr)*(maxnoun-first_noun+1));
699 }
700
701 static void wset_noundesc(file_info *fi)
702 {
703 fi[0].ptr=noun_ptr;
704 fi[1].ptr=text_ptr;
705 fi[2].ptr=turn_ptr;
706 fi[3].ptr=push_ptr;
707 fi[4].ptr=pull_ptr;
708 fi[5].ptr=play_ptr;
709 }
710
711 static void set_creatdesc(file_info *fi)
712 {
713 fi[0].ptr=creat_ptr=rmalloc(sizeof(descr_ptr)*(maxcreat-first_creat+1));
714 fi[1].ptr=talk_ptr=rmalloc(sizeof(descr_ptr)*(maxcreat-first_creat+1));
715 fi[2].ptr=ask_ptr=rmalloc(sizeof(descr_ptr)*(maxcreat-first_creat+1));
716 }
717
718 static void wset_creatdesc(file_info *fi)
719 {
720 fi[0].ptr=creat_ptr;
721 fi[1].ptr=talk_ptr;
722 fi[2].ptr=ask_ptr;
723 }
724
725 static void set_cmdptr(file_info *fi)
726 {
727 fi[0].ptr=cmd_ptr=rmalloc(sizeof(long)*last_cmd);
728 }
729
730 static void wset_cmdptr(file_info *fi)
731 {
732 fi[0].ptr=cmd_ptr;
733 }
734
735
736 typedef struct { /* Entries in the index header of the AGX file */
737 uint32 file_offset;
738 uint32 blocksize;
739 uint32 numrec;
740 uint32 recsize;
741 } index_rec;
742
743 static file_info fi_index[]={
744 {FT_UINT32, DT_DEFAULT, NULL, offsetof(index_rec,file_offset)},
745 {FT_UINT32, DT_DEFAULT, NULL, offsetof(index_rec,blocksize)},
746 {FT_UINT32, DT_DEFAULT, NULL, offsetof(index_rec,numrec)},
747 {FT_UINT32, DT_DEFAULT, NULL, offsetof(index_rec,recsize)},
748 endrec
749 };
750
751
752 /*
753 uint32 File ID ['....' 4 bytes]
754 byte Version owner: 'R'
755 byte Version 0
756 byte Extension owner 'R'
757 byte Extension 0
758 */
759
760 typedef struct {
761 unsigned long fileid;
762 unsigned long res1; /* Reserved for future use */
763 uchar res2;
764 uchar eol_chk1; /* Catch non-binary upload errors */
765 uchar eol_chk2;
766 uchar ver_own;
767 uchar version;
768 uchar ext_own;
769 uchar extnum;
770 uchar fallback_ext; /* For non-'R' extensions, this is the 'R' extension
771 to fall back to. */
772 } file_head_rec;
773
774 static file_info fi_header[]={
775 {FT_UINT32, DT_LONG, NULL, offsetof(file_head_rec,fileid)}, /* File ID */
776 {FT_BYTE, DT_DEFAULT, NULL, offsetof(file_head_rec,ver_own)}, /* Owner */
777 {FT_BYTE, DT_DEFAULT, NULL, offsetof(file_head_rec,version)}, /* Version */
778 {FT_BYTE, DT_DEFAULT, NULL, offsetof(file_head_rec,ext_own)}, /*Ext owner*/
779 {FT_BYTE, DT_DEFAULT, NULL, offsetof(file_head_rec,extnum)}, /* Ext vers */
780 {FT_BYTE, DT_DEFAULT, NULL, offsetof(file_head_rec,eol_chk1)},
781 {FT_BYTE, DT_DEFAULT, NULL, offsetof(file_head_rec,eol_chk2)},
782 {FT_BYTE, DT_DEFAULT, NULL, offsetof(file_head_rec,fallback_ext)},
783 {FT_BYTE, DT_DEFAULT, NULL, offsetof(file_head_rec,res2)},
784 {FT_UINT32, DT_DEFAULT, NULL, offsetof(file_head_rec,res1)},
785 endrec
786 };
787
788 static char *block_name[AGX_NUMBLOCK]={
789 "Index","Game Info","Room(DA2)","Noun(DA3)","Creature(DA4)",
790 "Command Header(DA5)","Error Message(STD)","Message",
791 "Question","Answer","User String","Description Text(D$$)",
792 "Command Tokens(DA6)","Static String","Subroutine ID",
793 "Verb Synonym","RoomPIX","Global Noun","Flag Noun","Word Lists(Syntbl)",
794 "Dictionary Text","Dictionary Index","OPT",
795 "Picture Filename","RoomPIX Filename","Font Filename","Sound Filename",
796 "Menu(VOC)","Multi-word Verb","Preposition","ObjFlag","ObjProp",
797 "Attrtable","PropTable","PropStr","Itemized Variables",
798 "Itemized Flags"
799 };
800
801
802 /* Return 0 on failure, 1 on success */
803 int read_agx(fc_type fc, rbool diag)
804 {
805 file_head_rec filehead;
806 unsigned long fsize;
807 index_rec *index;
808 long i;
809 int index_recsize;
810 int index_start;
811
812 agx_file=1;
813 fsize=buffopen(fc,fAGX,16,NULL,1);
814 if (fsize==0) {
815 agx_file=0;
816 return 0;
817 }
818
819 /* Read header */
820 read_recarray(&filehead,sizeof(file_head_rec),1,fi_header,
821 "File Header",0,compute_recsize(fi_header));
822 if (filehead.fileid!=AGT_FILE_SIG) {
823 buffclose();
824 return 0;
825 }
826 if (DIAG) {
827 rprintf("AGX file format");
828 if (isprint(filehead.ver_own) && isprint(filehead.ext_own) )
829 rprintf(" Version:%c%d\tExtension:%c%d\n",
830 filehead.ver_own,filehead.version,
831 filehead.ext_own,filehead.extnum);
832 else
833 rprintf(" Version:%d:%d\tExtension:%d:%d\n",
834 filehead.ver_own,filehead.version,
835 filehead.ext_own,filehead.extnum);
836 }
837 if (filehead.ver_own!='R' || filehead.version>2) {
838 rprintf("Unsupported AGX file version.\n");
839 rprintf(" Either the file is corrupted or or you need a more recent "
840 "version of AGiliTy.\n");
841 rprintf("\n");
842 fatal("Can't read AGX file.");
843 }
844
845 index_recsize=compute_recsize(fi_index);
846 if (filehead.version==0) {
847 if (debug_da1)
848 rprintf("[AGX version 0: obsolete.]\n");
849 index_recsize+=8; /* Extra junk block in version 0. */
850 index_start=8;
851 } else {
852 index_start=16;
853 if (filehead.eol_chk1!='\n' || filehead.eol_chk2!='\r')
854 fatal("File apparently downloaded as non-binary file.");
855 }
856 if (filehead.ext_own!='R'
857 || (filehead.version==0 && filehead.extnum>1)
858 || (filehead.version==1 && filehead.extnum>7)
859 || (filehead.version==2 && filehead.extnum>2) )
860 agtwarn("Unrecognized extension to AGX file format.",0);
861 if (filehead.ext_own!='R') { /* Assume lowest common denomenator */
862 if (filehead.version<2)
863 fatal("Extensions of AGX beta versions not supported.");
864 if (filehead.fallback_ext<1) filehead.fallback_ext=1;
865 }
866
867 /* Now read master index */
868 /* This assumes that the file is long enough to absorb any
869 'extra' blocks we read in in early versions with fewer blocks. */
870 /* (Right now, this must be true: the next block alone is big enough) */
871 index=read_recarray(NULL,sizeof(index_rec),AGX_NUMBLOCK,
872 fi_index, "File Index",index_start,
873 index_recsize*AGX_NUMBLOCK);
874
875 /* Zero index entries for any blocks that are beyond the bounds of the
876 file's index */
877 if (AGX_NUMBLOCK>index[0].numrec)
878 memset(index+index[0].numrec,0,
879 (AGX_NUMBLOCK-index[0].numrec)*sizeof(index_rec));
880
881 if (DIAG) {
882 rprintf("\n");
883 rprintf("File Index:\n");
884 rprintf(" Offset Size NumRec RecSz\n");
885 rprintf(" ------ ------ ------ ------\n");
886 for(i=0;i<AGX_NUMBLOCK;i++)
887 rprintf("%2d: %6d %6d %6d %6d %s\n", i,
888 index[i].file_offset, index[i].blocksize,
889 index[i].numrec, index[i].recsize, block_name[i]);
890 }
891 if (index[0].file_offset!=index_start)
892 fatal("File header corrupted.");
893
894 for(i=0;i<AGX_NUMBLOCK;i++) { /* Error checking */
895 #ifdef DEBUG_AGX
896 rprintf(" Verifying block %d...\n",i);
897 #endif
898 if (index[i].recsize*index[i].numrec!=index[i].blocksize)
899 fatal("File header corrupted.");
900 if (index[i].file_offset+index[i].blocksize>fsize)
901 fatal("File index points past end of file.");
902 }
903
904 /* Check for mandatory fields */
905 if (!index[0].numrec /* File index */
906 || !index[1].numrec /* Game header */
907 || !index[13].numrec /* Static string block */
908 || !index[15].numrec /* Synonym list */
909 || !index[19].numrec /* Syntbl */
910 || !index[20].numrec /* Dictionary text */
911 || !index[21].numrec /* Dictionary index */
912 )
913 fatal("AGX file missing mandatory block.");
914
915
916 read_globalrec(fi_gameinfo,"Game Info",index[1].file_offset,
917 index[1].blocksize);
918 if (filehead.version==0 && filehead.extnum==0) {
919 exitmsg_base=1000;
920 if (aver>=AGT15)
921 box_title=1;
922 }
923 if (index[1].blocksize==83 && filehead.version==1 && filehead.extnum>=5) {
924 /* Detect 0.8-compatibility hack */
925 filehead.extnum=2;
926 }
927 if (filehead.version==0 || (filehead.version==1 && filehead.extnum<5)) {
928 if (aver>=AGT182 && aver<=AGT18MAX)
929 {if (filehead.extnum<4) TWO_CYCLE=1;}
930 else
931 PURE_AFTER=1;
932 }
933
934 /* Need to read in ss_array before rooms/nouns/creatures */
935 ss_size=ss_end=index[13].numrec;
936 static_str=read_recblock(NULL,FT_CHAR,
937 index[13].numrec,index[13].file_offset,
938 index[13].blocksize);
939
940 synptr=syntbl_size=index[19].numrec;
941 syntbl=read_recblock(NULL,FT_WORD,index[19].numrec,index[19].file_offset,
942 index[19].blocksize);
943
944 maxroom=first_room+index[2].numrec-1;
945 set_roomdesc(fi_room);
946 room=read_recarray(NULL,sizeof(room_rec),index[2].numrec,
947 fi_room,"Room",index[2].file_offset,index[2].blocksize);
948
949 maxnoun=first_noun+index[3].numrec-1;
950 set_noundesc(fi_noun);
951 noun=read_recarray(NULL,sizeof(noun_rec),index[3].numrec,
952 fi_noun,"Noun",index[3].file_offset,index[3].blocksize);
953
954 last_obj=maxcreat=first_creat+index[4].numrec-1;
955 set_creatdesc(fi_creat);
956 creature=read_recarray(NULL,sizeof(creat_rec),index[4].numrec,
957 fi_creat,"Creature",index[4].file_offset,
958 index[4].blocksize);
959
960 last_cmd=index[5].numrec;
961 set_cmdptr(fi_cmdhead);
962 command=read_recarray(NULL,sizeof(cmd_rec),index[5].numrec,
963 fi_cmdhead,"Metacommand",index[5].file_offset,
964 index[5].blocksize);
965 if (filehead.ext_own!='R' && filehead.fallback_ext<=1) {
966 int i;
967 for(i=0;i<last_cmd;i++)
968 command[i].noun_obj=command[i].obj_obj=0;
969 }
970
971 NUM_ERR=index[6].numrec;
972 err_ptr=read_recarray(NULL,sizeof(descr_ptr),index[6].numrec,
973 fi_descptr,"Error Message",index[6].file_offset,
974 index[6].blocksize);
975
976 last_message=index[7].numrec;
977 msg_ptr=read_recarray(NULL,sizeof(descr_ptr),index[7].numrec,
978 fi_descptr,"Message",index[7].file_offset,
979 index[7].blocksize);
980
981 MaxQuestion=index[8].numrec;
982 question=answer=NULL;
983 quest_ptr=read_recarray(NULL,sizeof(descr_ptr),index[8].numrec,
984 fi_descptr,"Question",index[8].file_offset,
985 index[8].blocksize);
986 if (index[9].numrec!=index[8].numrec)
987 fatal("File corrputed: questions and answers don't match.");
988 ans_ptr=read_recarray(NULL,sizeof(descr_ptr),index[9].numrec,
989 fi_descptr,"Answer",index[9].file_offset,
990 index[9].blocksize);
991
992 MAX_USTR=index[10].numrec;
993 userstr=read_recarray(NULL,sizeof(tline),index[10].numrec,
994 fi_tline,"User String",index[10].file_offset,
995 index[10].blocksize);
996
997 MAX_SUB=index[14].numrec;
998 sub_name=read_recblock(NULL,FT_WORD,index[14].numrec,index[14].file_offset,
999 index[14].blocksize);
1000
1001 if (index[16].numrec>MAX_PIX) {
1002 index[16].numrec=MAX_PIX;
1003 index[16].blocksize=index[16].recsize*index[16].numrec;
1004 }
1005 maxpix=index[16].numrec;
1006 for(i=0;i<MAX_PIX;i++) pix_name[i]=0; /* In case there are less than
1007 MAX_PIX names */
1008 read_recblock(pix_name,FT_WORD,index[16].numrec,index[16].file_offset,
1009 index[16].blocksize);
1010
1011 numglobal=index[17].numrec;
1012 globalnoun=read_recblock(NULL,FT_WORD,
1013 index[17].numrec,index[17].file_offset,
1014 index[17].blocksize);
1015
1016 if (index[18].numrec>MAX_FLAG_NOUN) {
1017 index[18].numrec=MAX_FLAG_NOUN;
1018 index[18].blocksize=index[18].recsize*index[18].numrec;
1019 }
1020
1021 for(i=0;i<MAX_FLAG_NOUN;i++) flag_noun[i]=0;
1022 read_recblock(flag_noun,FT_WORD,index[18].numrec,index[18].file_offset,
1023 index[18].blocksize);
1024
1025
1026
1027 DVERB=index[15].numrec-old_base_verb-MAX_SUB;
1028 synlist=read_recblock(NULL,FT_SLIST,index[15].numrec,index[15].file_offset,
1029 index[15].blocksize);
1030 correct_synlist();
1031
1032 num_comb=index[28].numrec;
1033 comblist=read_recblock(NULL,FT_SLIST,index[28].numrec,index[28].file_offset,
1034 index[28].blocksize);
1035
1036 num_prep=index[29].numrec;
1037 userprep=read_recblock(NULL,FT_SLIST,index[29].numrec,index[29].file_offset,
1038 index[29].blocksize);
1039
1040 /* dicstr must be read in before dict */
1041 dictstrsize=dictstrptr=index[20].numrec;
1042 dictstr=read_recblock(NULL,FT_CHAR,index[20].numrec,index[20].file_offset,
1043 index[20].blocksize);
1044
1045 dp=index[21].numrec;
1046 dict=read_recblock(NULL,FT_DICTPTR,
1047 index[21].numrec,index[21].file_offset,
1048 index[21].blocksize);
1049
1050 have_opt=(index[22].numrec!=0);
1051 for(i=0;i<14;i++) opt_data[i]=0;
1052 if (have_opt) {
1053 if (index[22].numrec>14) index[22].numrec=14;
1054 read_recblock(opt_data,FT_BYTE,index[22].numrec,index[22].file_offset,
1055 index[22].blocksize);
1056 }
1057
1058 maxpict=index[23].numrec;
1059 pictlist=read_recblock(NULL,FT_STR,index[23].numrec,index[23].file_offset,
1060 index[23].blocksize);
1061 maxpix=index[24].numrec;
1062 pixlist=read_recblock(NULL,FT_STR,index[24].numrec,index[24].file_offset,
1063 index[24].blocksize);
1064 maxfont=index[25].numrec;
1065 fontlist=read_recblock(NULL,FT_STR,index[25].numrec,index[25].file_offset,
1066 index[25].blocksize);
1067 maxsong=index[26].numrec;
1068 songlist=read_recblock(NULL,FT_STR,index[26].numrec,index[26].file_offset,
1069 index[26].blocksize);
1070
1071 vm_size=index[27].numrec;
1072 verbinfo=read_recarray(NULL,sizeof(verbentry_rec),index[27].numrec,
1073 fi_verbentry,"Menu Vocabulary",index[27].file_offset,
1074 index[27].blocksize);
1075
1076 /* Check that objflag and objprop fields are of correct size */
1077 if (index[30].numrec != objextsize(0))
1078 fatal("Object flag block not of the correct size.");
1079
1080 if (index[31].numrec != objextsize(1))
1081 fatal("Object property block not of the correct size.");
1082
1083 objflag=read_recblock(NULL,FT_BYTE,index[30].numrec,index[30].file_offset,
1084 index[30].blocksize);
1085 objprop=read_recblock(NULL,FT_INT32,index[31].numrec,index[31].file_offset,
1086 index[31].blocksize);
1087
1088 oflag_cnt=index[32].numrec;
1089 attrtable=read_recarray(NULL,sizeof(attrdef_rec),index[32].numrec,
1090 fi_attrrec,"Object Flag Table",
1091 index[32].file_offset,
1092 index[32].blocksize);
1093 /* Objflags are converted to internal form later, after
1094 block 36 has been read in. */
1095
1096 oprop_cnt=index[33].numrec;
1097 proptable=read_recarray(NULL,sizeof(propdef_rec),index[33].numrec,
1098 fi_proprec,"Object Property Table",
1099 index[33].file_offset,
1100 index[33].blocksize);
1101
1102 if (filehead.ext_own!='R' && filehead.fallback_ext<=1) {
1103 /* Non-standard extension */
1104 int i;
1105 for(i=0;i<oflag_cnt;i++) /* These are converted later */
1106 attrtable[i].ystr=NULL; attrtable[i].nstr=NULL;
1107 for(i=0;i<oprop_cnt;i++)
1108 proptable[i].str_cnt=0;
1109 propstr_size=0; propstr=NULL;
1110 vartable=NULL; flagtable=NULL;
1111 } else { /* Normal case */
1112 propstr_size=index[34].numrec;
1113 propstr=read_recblock(NULL,FT_STR,index[34].numrec,
1114 index[34].file_offset,index[34].blocksize);
1115
1116 if (index[35].numrec && index[35].numrec!=VAR_NUM+1)
1117 fatal("AGX file corrupted: variable itemization table size mismatch.");
1118 vartable=read_recarray(NULL,sizeof(vardef_rec),index[35].numrec,
1119 fi_varrec,"Variable Itemization Table",
1120 index[35].file_offset,
1121 index[35].blocksize);
1122
1123 if (index[36].numrec && index[36].numrec!=FLAG_NUM+1)
1124 fatal("AGX file corrupted: flag itemization table size mismatch.");
1125 flagtable=read_recarray(NULL,sizeof(flagdef_rec),index[36].numrec,
1126 fi_flagrec,"Flag Itemization Table",
1127 index[36].file_offset,
1128 index[36].blocksize);
1129 }
1130
1131 fix_objflag_str(1); /* Convert flags and objflags to internal form */
1132
1133
1134 /* Block 12: Command text */
1135 read_command(index[12].numrec,index[12].file_offset, diag);
1136
1137 /* Block 11 is description block; it doesn't get read in by
1138 agxread() but during play */
1139 if (index[11].blocksize<=descr_maxmem) {
1140 /* ... if we decided to load descriptions into memory */
1141 mem_descr=read_recblock(NULL,FT_CHAR,index[11].numrec,
1142 index[11].file_offset,
1143 index[11].blocksize);
1144 buffclose(); /* Don't need to keep it open */
1145 descr_ofs=-1;
1146 } else {
1147 descr_ofs=index[11].file_offset;
1148 mem_descr=NULL;
1149 }
1150
1151 reinit_dict();
1152 cmds_syns_canon();
1153 return 1;
1154 }
1155
1156
1157
1158
1159 /* ------------------------------------------------------------- */
1160 /* AGX Writing Code */
1161 /* ------------------------------------------------------------- */
1162
1163 static index_rec *gindex;
1164
1165
1166 /* This patches the block descriptions to create AGiliTy-0.8
1167 compatible files. This is just a quick hack to solve a short-term
1168 problem. */
1169 void patch_08(void)
1170 {
1171 set_endrec(fi_gameinfo,48); /* Should give size of 83 */
1172 set_endrec(fi_noun,45);
1173 set_endrec(fi_creat,23);
1174 }
1175
1176
1177 /* This writes the file header; it needs to be called near the
1178 end */
1179 void write_header(void)
1180 {
1181 int i;
1182 rbool simple;
1183 file_head_rec filehead;
1184
1185 filehead.fileid=AGT_FILE_SIG;
1186 filehead.ver_own=filehead.ext_own='R';
1187 /* The following will be converted to 1-7 if advanced features aren't
1188 being used. */
1189 filehead.version=2;
1190 filehead.extnum=2;
1191 filehead.fallback_ext=2; /* 'R' extension to fall back to;
1192 only meaningful if ext_own is *not* 'R' */
1193 filehead.eol_chk1='\n';
1194 filehead.eol_chk2='\r';
1195 filehead.res1=0;
1196 filehead.res2=0;
1197
1198 /* This automatically patches the block descriptions to create
1199 pre-AGiliTy-0.8.8 compatible files. If it can't (because the
1200 file uses 0.8.8+ features) then it leaves the version at 2;
1201 otherwise the version is reduced to 1. */
1202 /* The files thus created are actually hybrid files-- they
1203 have some 0.8.8+ features, just not the ones that might
1204 break pre-0.8.8 interpreters. */
1205 simple=1;
1206 for(i=30;i<AGX_NUMBLOCK;i++)
1207 if (gindex[i].numrec!=0) simple=0;
1208 if (simple) {
1209 gindex[0].numrec=30; /* 0.8.7 compatibility */
1210 gindex[0].blocksize=gindex[0].recsize*gindex[0].numrec;
1211 filehead.version=1;
1212 filehead.extnum=7;
1213 }
1214 write_recarray(&filehead,sizeof(file_head_rec),1,fi_header,0);
1215 }
1216
1217
1218 static void agx_compute_index(void)
1219 /* This computes the blocksize and offset values for all blocks */
1220 {
1221 int i;
1222
1223 for(i=0;i<AGX_NUMBLOCK;i++)
1224 gindex[i].blocksize=gindex[i].recsize*gindex[i].numrec;
1225 gindex[0].file_offset=16;
1226 gindex[11].file_offset=gindex[0].file_offset+gindex[0].blocksize;
1227 gindex[12].file_offset=gindex[11].file_offset+gindex[11].blocksize;
1228 gindex[1].file_offset=gindex[12].file_offset+gindex[12].blocksize;
1229 for(i=2;i<=AGX_NUMBLOCK-1;i++)
1230 if (i==13)
1231 gindex[13].file_offset=gindex[10].file_offset+gindex[10].blocksize;
1232 else if (i!=11 && i!=12)
1233 gindex[i].file_offset=gindex[i-1].file_offset+gindex[i-1].blocksize;
1234 }
1235
1236
1237 /* Create the preliminary gindex for the new file and set it up so we can
1238 write descriptions to the new file */
1239 void agx_create(fc_type fc)
1240 {
1241 int i;
1242
1243 bw_open(fc,fAGX);
1244 gindex=rmalloc(sizeof(index_rec)*AGX_NUMBLOCK);
1245
1246 gindex[0].numrec=AGX_NUMBLOCK;
1247 for(i=1;i<AGX_NUMBLOCK;i++) /* Initialize the rest to 0 */
1248 gindex[i].numrec=0;
1249
1250 /* This writes random data to the file; their only purpose
1251 is to prevent problems with seeking beyond the end of file */
1252 write_recarray(NULL,sizeof(file_head_rec),1,fi_header,0);
1253 write_recarray(NULL,sizeof(index_rec),AGX_NUMBLOCK,fi_index,16);
1254
1255 old_base_verb=BASE_VERB; /* This will be constant for any given version
1256 of the interpreter, but may change across
1257 versions of the interpreter */
1258 /* Set record sizes */
1259 gindex[0].recsize=compute_recsize(fi_index);
1260 gindex[1].recsize=compute_recsize(fi_gameinfo);
1261 gindex[2].recsize=compute_recsize(fi_room);
1262 gindex[3].recsize=compute_recsize(fi_noun);
1263 gindex[4].recsize=compute_recsize(fi_creat);
1264 gindex[5].recsize=compute_recsize(fi_cmdhead);
1265 gindex[6].recsize=gindex[7].recsize=gindex[8].recsize=
1266 gindex[9].recsize=compute_recsize(fi_descptr);
1267 gindex[10].recsize=ft_leng[FT_TLINE];
1268 gindex[11].recsize=ft_leng[FT_CHAR];
1269 gindex[12].recsize=ft_leng[FT_INT16];
1270 gindex[13].recsize=gindex[20].recsize=ft_leng[FT_CHAR];
1271 gindex[14].recsize=gindex[16].recsize=gindex[17].recsize=
1272 gindex[18].recsize=ft_leng[FT_WORD];
1273 gindex[15].recsize=ft_leng[FT_SLIST];
1274 gindex[19].recsize=ft_leng[FT_WORD];
1275 gindex[21].recsize=ft_leng[FT_DICTPTR];
1276 gindex[22].recsize=ft_leng[FT_BYTE];
1277 gindex[23].recsize=gindex[24].recsize=gindex[25].recsize=
1278 gindex[26].recsize=ft_leng[FT_STR];
1279 gindex[27].recsize=compute_recsize(fi_verbentry);
1280 gindex[28].recsize=ft_leng[FT_SLIST];
1281 gindex[29].recsize=ft_leng[FT_SLIST];
1282 gindex[30].recsize=ft_leng[FT_BYTE];
1283 gindex[31].recsize=ft_leng[FT_INT32];
1284 gindex[32].recsize=compute_recsize(fi_attrrec);
1285 gindex[33].recsize=compute_recsize(fi_proprec);
1286 gindex[34].recsize=ft_leng[FT_STR];
1287 gindex[35].recsize=compute_recsize(fi_varrec);
1288 gindex[36].recsize=compute_recsize(fi_flagrec);
1289
1290 agx_compute_index(); /* Only the first 10 blocks will be correct */
1291 /* The important thing is to get the offset of block 11, the desciption
1292 text block, so we can write to it. */
1293 /* Block 11 is the description block; it doesn't get written by agxwrite()
1294 but by its own routines. */
1295 }
1296
1297
1298 static void agx_finish_index(void)
1299 {
1300 /* Still have 11, 27-29 */
1301 /* Block 12 is taken care of elsewhere (in write_command) */
1302
1303 gindex[1].numrec=1;
1304 gindex[2].numrec=rangefix(maxroom-first_room+1);
1305 gindex[3].numrec=rangefix(maxnoun-first_noun+1);
1306 gindex[4].numrec=rangefix(maxcreat-first_creat+1);
1307 gindex[5].numrec=last_cmd;
1308 gindex[6].numrec=NUM_ERR;
1309 gindex[7].numrec=last_message;
1310 gindex[8].numrec=gindex[9].numrec=MaxQuestion;
1311 if (userstr!=NULL)
1312 gindex[10].numrec=MAX_USTR;
1313 else gindex[10].numrec=0;
1314 gindex[13].numrec=ss_end;
1315 gindex[14].numrec=MAX_SUB;
1316 gindex[15].numrec=TOTAL_VERB;
1317 gindex[16].numrec=maxpix;
1318 gindex[17].numrec=numglobal;
1319 gindex[19].numrec=synptr;
1320 gindex[20].numrec=dictstrptr;
1321 gindex[21].numrec=dp;
1322 gindex[23].numrec=maxpict;
1323 gindex[24].numrec=maxpix;
1324 gindex[25].numrec=maxfont;
1325 gindex[26].numrec=maxsong;
1326 gindex[27].numrec=vm_size;
1327 gindex[28].numrec=num_comb;
1328 gindex[29].numrec=num_prep;
1329 gindex[30].numrec=objextsize(0);
1330 gindex[31].numrec=objextsize(1);
1331 gindex[32].numrec=oflag_cnt;
1332 gindex[33].numrec=oprop_cnt;
1333 gindex[34].numrec=propstr_size;
1334 gindex[35].numrec= ( vartable ? VAR_NUM+1 : 0 );
1335 gindex[36].numrec= ( flagtable ? FLAG_NUM+1 : 0 );
1336
1337 /* These may also be zero (?) */
1338 gindex[22].numrec= have_opt ? 14 : 0;
1339 gindex[18].numrec=MAX_FLAG_NOUN;
1340
1341 agx_compute_index(); /* This time it will be complete except for
1342 the VOC-TTL-INS blocks at the end */
1343 }
1344
1345
1346
1347 /* The following routine writes a description to disk,
1348 and stores the size and length in dp */
1349 void write_descr(descr_ptr *dp,descr_line *txt)
1350 {
1351 long i;
1352 long size;
1353 char *buff, *buffptr, *src;
1354
1355 size=0;
1356 if (txt==NULL) {
1357 dp->start=0;
1358 dp->size=0;
1359 return;
1360 }
1361
1362 for(i=0;txt[i]!=NULL;i++) /* Compute size */
1363 size+=strlen(txt[i])+1; /* Remember trailing \0 */
1364 buff=rmalloc(sizeof(char)*size);
1365
1366 buffptr=buff;
1367 for(i=0;txt[i]!=NULL;i++) {
1368 for(src=txt[i];*src!=0;src++,buffptr++)
1369 *buffptr=*src;
1370 *buffptr++=0;
1371 }
1372 dp->start=gindex[11].numrec;
1373 dp->size=size;
1374 gindex[11].numrec+=
1375 write_recblock(buff,FT_CHAR,size,
1376 gindex[11].file_offset+gindex[11].numrec);
1377 rfree(buff);
1378 }
1379
1380 /* Write command text to file and return number of bytes written. */
1381 static long write_command(long cmdofs)
1382 {
1383 long i, cnt;
1384
1385 cmd_ptr=rmalloc(sizeof(long)*last_cmd);
1386 cnt=0;
1387 for(i=0;i<last_cmd;i++) {
1388 cmd_ptr[i]=cnt;
1389 write_recblock(command[i].data,FT_INT16,command[i].cmdsize,
1390 cmdofs+2*cnt);
1391 cnt+=command[i].cmdsize;
1392 }
1393 return cnt;
1394 }
1395
1396
1397
1398
1399 /* Write the bulk of the AGX file. This requires that the descriptions,
1400 etc. have already been written */
1401 void agx_write(void)
1402 {
1403 gindex[11].blocksize=gindex[11].numrec*gindex[11].recsize;
1404 gindex[12].file_offset=gindex[11].file_offset+gindex[11].blocksize;
1405
1406 gindex[12].numrec=write_command(gindex[12].file_offset);
1407
1408 agx_finish_index();
1409
1410 /* Need to write these blocks in order */
1411
1412 write_globalrec(fi_gameinfo,gindex[1].file_offset);
1413
1414 wset_roomdesc(fi_room);
1415 write_recarray(room,sizeof(room_rec),gindex[2].numrec,
1416 fi_room,gindex[2].file_offset);
1417
1418 wset_noundesc(fi_noun);
1419 write_recarray(noun,sizeof(noun_rec),gindex[3].numrec,
1420 fi_noun,gindex[3].file_offset);
1421
1422 wset_creatdesc(fi_creat);
1423 write_recarray(creature,sizeof(creat_rec),gindex[4].numrec,
1424 fi_creat,gindex[4].file_offset);
1425
1426 wset_cmdptr(fi_cmdhead);
1427 write_recarray(command,sizeof(cmd_rec),gindex[5].numrec,
1428 fi_cmdhead,gindex[5].file_offset);
1429
1430 write_recarray(err_ptr,sizeof(descr_ptr),gindex[6].numrec,
1431 fi_descptr,gindex[6].file_offset);
1432 write_recarray(msg_ptr,sizeof(descr_ptr),gindex[7].numrec,
1433 fi_descptr,gindex[7].file_offset);
1434 write_recarray(quest_ptr,sizeof(descr_ptr),gindex[8].numrec,
1435 fi_descptr,gindex[8].file_offset);
1436 write_recarray(ans_ptr,sizeof(descr_ptr),gindex[9].numrec,
1437 fi_descptr,gindex[9].file_offset);
1438
1439 if (userstr!=NULL)
1440 write_recarray(userstr,sizeof(tline),gindex[10].numrec,
1441 fi_tline,gindex[10].file_offset);
1442
1443 write_recblock(static_str,FT_CHAR,
1444 gindex[13].numrec,gindex[13].file_offset);
1445
1446 write_recblock(sub_name,FT_WORD,gindex[14].numrec,gindex[14].file_offset);
1447 write_recblock(synlist,FT_SLIST,gindex[15].numrec,gindex[15].file_offset);
1448 write_recblock(pix_name,FT_WORD,gindex[16].numrec,gindex[16].file_offset);
1449 write_recblock(globalnoun,FT_WORD,gindex[17].numrec,gindex[17].file_offset);
1450 write_recblock(flag_noun,FT_WORD,gindex[18].numrec,gindex[18].file_offset);
1451 write_recblock(syntbl,FT_WORD,gindex[19].numrec,gindex[19].file_offset);
1452 write_recblock(dictstr,FT_CHAR,gindex[20].numrec,gindex[20].file_offset);
1453 write_recblock(dict,FT_DICTPTR,gindex[21].numrec,gindex[21].file_offset);
1454 if (have_opt)
1455 write_recblock(opt_data,FT_BYTE,gindex[22].numrec,gindex[22].file_offset);
1456
1457 write_recblock(pictlist,FT_STR,gindex[23].numrec,gindex[23].file_offset);
1458 write_recblock(pixlist,FT_STR,gindex[24].numrec,gindex[24].file_offset);
1459 write_recblock(fontlist,FT_STR,gindex[25].numrec,gindex[25].file_offset);
1460 write_recblock(songlist,FT_STR,gindex[26].numrec,gindex[26].file_offset);
1461
1462 write_recarray(verbinfo,sizeof(verbentry_rec),gindex[27].numrec,
1463 fi_verbentry,gindex[27].file_offset);
1464 write_recblock(comblist,FT_SLIST,gindex[28].numrec,gindex[28].file_offset);
1465 write_recblock(userprep,FT_SLIST,gindex[29].numrec,gindex[29].file_offset);
1466 write_recblock(objflag,FT_BYTE,gindex[30].numrec,gindex[30].file_offset);
1467 write_recblock(objprop,FT_INT32,gindex[31].numrec,gindex[31].file_offset);
1468 fix_objflag_str(0); /* Convert to external form */
1469 write_recarray(attrtable,sizeof(attrdef_rec),
1470 gindex[32].numrec,fi_attrrec,gindex[32].file_offset);
1471 write_recarray(proptable,sizeof(propdef_rec),
1472 gindex[33].numrec,fi_proprec,gindex[33].file_offset);
1473 write_recblock(propstr,FT_STR,gindex[34].numrec,gindex[34].file_offset);
1474 write_recarray(vartable,sizeof(vardef_rec),
1475 gindex[35].numrec,fi_varrec,gindex[35].file_offset);
1476 write_recarray(flagtable,sizeof(flagdef_rec),
1477 gindex[36].numrec,fi_flagrec,gindex[36].file_offset);
1478 fix_objflag_str(1); /* Restore to internal form */
1479 }
1480
1481
1482 /* Write header and master gindex and then close AGX file */
1483 void agx_wclose(void)
1484 {
1485 write_header();
1486 write_recarray(gindex,sizeof(index_rec),AGX_NUMBLOCK,fi_index,16);
1487 bw_close();
1488 rfree(gindex);
1489 }
1490
1491
1492 void agx_wabort(void)
1493 {
1494 bw_abort();
1495 rfree(gindex);
1496 }