Debian Codegraph @

terps/scare/sctafpar.c

raw symbols SCIP
1 /* vi: set ts=2 shiftwidth=2 expandtab:
2 *
3 * Copyright (C) 2003-2008 Simon Baldwin and Mark J. Tilford
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 * USA
18 */
19
20 /*
21 * Module notes:
22 *
23 * o Adds new "types" to jAsea's property descriptor: 'M' for multiline
24 * strings, 'Z', 'F'/'T', and 'E' for defaulted integers, booleans, and
25 * strings, 'i', 'b', and 's' for ignored integers, booleans, and strings,
26 * and '{...}' and '|...|' for "special" descriptions and version fixups
27 * that can't be described as things stand.
28 *
29 * o Adds new 'G' expression test, to check Global boolean.
30 *
31 * o The stack "adjustment" stuff is a bit of a bother.
32 */
33
34 #include <assert.h>
35 #include <limits.h>
36 #include <setjmp.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40
41 #include "scare.h"
42 #include "scprotos.h"
43
44
45 /* Assorted definitions and constants. */
46 static const sc_char NUL = '\0';
47 enum
48 { PARSE_TEMP_LENGTH = 256,
49 PARSE_MAX_DEPTH = 32
50 };
51
52 /* Multiline separator sequences for the various versions supported. */
53 enum { SEPARATOR_SIZE = 3 };
54 static const sc_byte V400_SEPARATOR[SEPARATOR_SIZE] = {0xbd, 0xd0, 0x00};
55 static const sc_byte V390_SEPARATOR[SEPARATOR_SIZE] = {0x2a, 0x2a, 0x00};
56 static const sc_byte V380_SEPARATOR[SEPARATOR_SIZE] = {0x2a, 0x2a, 0x00};
57
58
59 /*
60 * Tables of properties descriptors. These strings define the structure of
61 * a TAF file. Field keys are:
62 *
63 * $,#,B,M - string, integer, boolean, and multiline properties
64 * E,F,T,Z - string, integer, and boolean properties not in the TAF;
65 * set to "", FALSE, TRUE and zero on parsing (version < 4)
66 * i,s,b - string, integer, and boolean in the TAF, but not stored
67 * [num] - arrays of property, fixed size to num
68 * V - variable sized array of property, size in input file
69 * W - like V, but size - 1 in input file (version < 4)
70 * <class> - class of property, separate parse target (recurse)
71 * ?[!]expr: - conditional property based on expr
72 * G[!]expr: - conditional property based on expr using globals
73 * |...| - fixup specials for versions < 4
74 * {special} - because some things just defy description
75 */
76 typedef struct
77 {
78 const sc_char *const class_name;
79 const sc_char *const descriptor;
80 } sc_parse_schema_t;
81
82 /* Version 4.0 TAF file properties descriptor table. */
83 static const sc_parse_schema_t V400_PARSE_SCHEMA[] = {
84 {"_GAME_",
85 "<HEADER>Header <GLOBAL>Globals V<ROOM>Rooms V<OBJECT>Objects V<TASK>Tasks"
86 " V<EVENT>Events V<NPC>NPCs V<ROOM_GROUP>RoomGroups V<SYNONYM>Synonyms"
87 " V<VARIABLE>Variables V<ALR>ALRs BCustomFont ?BCustomFont:$FontNameSize"
88 " $CompileDate"},
89 {"HEADER",
90 "MStartupText #StartRoom MWinText"},
91 {"GLOBAL",
92 "$GameName $GameAuthor $DontUnderstand #Perspective BShowExits #WaitTurns"
93 " BDispFirstRoom BBattleSystem #MaxScore $PlayerName BPromptName $PlayerDesc"
94 " #Task ?!#Task=0:$AltDesc #Position #ParentObject #PlayerGender"
95 " #MaxSize #MaxWt ?GBattleSystem:<BATTLE>Battle BEightPointCompass bNoDebug"
96 " BNoScoreNotify BNoMap bNoAutoComplete bNoControlPanel bNoMouse BSound"
97 " BGraphics <RESOURCE>IntroRes <RESOURCE>WinRes BStatusBox $StatusBoxText"
98 " iUnk1 iUnk2 BEmbedded"},
99 {"BATTLE",
100 "iStaminaLo iStaminaHi iStrengthLo iStrengthHi iAccuracyLo iAccuracyHi"
101 " iDefenseLo iDefenseHi iAgilityLo iAgilityHi iRecovery"},
102 {"ROOM",
103 "$Short $Long ?GEightPointCompass:[12]<ROOM_EXIT>Exits"
104 " ?!GEightPointCompass:[8]<ROOM_EXIT>Exits <RESOURCE>Res V<ROOM_ALT>Alts"
105 " ?!GNoMap:bHideOnMap"},
106 {"ROOM_EXIT",
107 "{V400_ROOM_EXIT:#Dest_#Var1_#Var2_#Var3}"},
108 {"ROOM_ALT",
109 "$M1 #Type <RESOURCE>Res1 $M2 #Var2 <RESOURCE>Res2 #HideObjects $Changed"
110 " #Var3 #DisplayRoom"},
111 {"RESOURCE",
112 "?GSound:$SoundFile,#SoundLen,ZSoundOffset"
113 " ?GGraphics:$GraphicFile,#GraphicLen,ZGraphicOffset {V400_RESOURCE}"},
114 {"OBJECT",
115 "$Prefix $Short V$Alias BStatic $Description #InitialPosition #Task"
116 " BTaskNotDone $AltDesc ?BStatic:<ROOM_LIST1>Where BContainer BSurface"
117 " #Capacity ?!BStatic:BWearable,#SizeWeight,#Parent"
118 " ?BStatic:{OBJECT:#Parent} #Openable ?#Openable=5:#Key ?#Openable=6:#Key"
119 " ?#Openable=7:#Key #SitLie ?!BStatic:BEdible BReadable ?BReadable:$ReadText"
120 " ?!BStatic:BWeapon #CurrentState ?!#CurrentState=0:$States,BStateListed"
121 " BListFlag <RESOURCE>Res1 <RESOURCE>Res2 ?GBattleSystem:<OBJ_BATTLE>Battle"
122 " $InRoomDesc #OnlyWhenNotMoved"},
123 {"OBJ_BATTLE",
124 "iProtectionValue iHitValue iMethod iAccuracy"},
125 {"ROOM_LIST1",
126 "#Type {ROOM_LIST1}"},
127 {"TASK",
128 "V$Command $CompleteText $ReverseMessage $RepeatText $AdditionalMessage"
129 " #ShowRoomDesc BRepeatable BReversible V$ReverseCommand <ROOM_LIST0>Where"
130 " $Question ?$Question:$Hint1,$Hint2 V<TASK_RESTR>Restrictions"
131 " V<TASK_ACTION>Actions $RestrMask <RESOURCE>Res"},
132 {"TASK_RESTR",
133 "#Type ?#Type=0:#Var1,#Var2,#Var3 ?#Type=1:#Var1,#Var2 ?#Type=2:#Var1,#Var2"
134 " ?#Type=3:#Var1,#Var2,#Var3 ?#Type=4:#Var1,#Var2,#Var3,$Var4 $FailMessage"},
135 {"TASK_ACTION",
136 "#Type ?#Type=0:#Var1,#Var2,#Var3 ?#Type=1:#Var1,#Var2,#Var3"
137 " ?#Type=2:#Var1,#Var2 ?#Type=3:#Var1,#Var2,#Var3,$Expr,#Var5"
138 " ?#Type=4:#Var1 ?#Type=5:#Var1,#Var2 ?#Type=6:#Var1,#Var2,#Var3"
139 " ?#Type=7:iVar1,iVar2,iVar3"},
140 {"ROOM_LIST0",
141 "#Type {ROOM_LIST0}"},
142 {"EVENT",
143 "$Short #StarterType ?#StarterType=2:#StartTime,#EndTime"
144 " ?#StarterType=3:#TaskNum #RestartType BTaskFinished #Time1 #Time2"
145 " $StartText $LookText $FinishText <ROOM_LIST0>Where #PauseTask"
146 " BPauserCompleted #PrefTime1 $PrefText1 #ResumeTask BResumerCompleted"
147 " #PrefTime2 $PrefText2 #Obj2 #Obj2Dest #Obj3 #Obj3Dest #Obj1 #Obj1Dest"
148 " #TaskAffected [5]<RESOURCE>Res"},
149 {"NPC",
150 "$Name $Prefix V$Alias $Descr #StartRoom $AltText #Task V<TOPIC>Topics"
151 " V<WALK>Walks BShowEnterExit ?BShowEnterExit:$EnterText,$ExitText"
152 " $InRoomText #Gender [4]<RESOURCE>Res ?GBattleSystem:<NPC_BATTLE>Battle"},
153 {"NPC_BATTLE",
154 "iAttitude iStaminaLo iStaminaHi iStrengthLo iStrengthHi iAccuracyLo"
155 " iAccuracyHi iDefenseLo iDefenseHi iAgilityLo iAgilityHi iSpeed"
156 " iKilledTask iRecovery iStaminaTask"},
157 {"TOPIC",
158 "$Subject $Reply #Task $AltReply"},
159 {"WALK",
160 "#NumStops BLoop #StartTask #CharTask #MeetObject #ObjectTask #StoppingTask"
161 " #MeetChar $ChangedDesc {WALK:#Rooms_#Times}"},
162 {"ROOM_GROUP",
163 "$Name {ROOM_GROUP:[]BList}"},
164 {"SYNONYM",
165 "$Replacement $Original"},
166 {"VARIABLE",
167 "$Name #Type $Value"},
168 {"ALR",
169 "$Original $Replacement"},
170 {NULL, NULL}
171 };
172
173 /* Version 3.9 TAF file properties descriptor table. */
174 static const sc_parse_schema_t V390_PARSE_SCHEMA[] = {
175 {"_GAME_",
176 "<HEADER>Header <GLOBAL>Globals V<ROOM>Rooms V<OBJECT>Objects V<TASK>Tasks"
177 " V<EVENT>Events V<NPC>NPCs V<ROOM_GROUP>RoomGroups V<SYNONYM>Synonyms"
178 " V<VARIABLE>Variables V<ALR>ALRs BCustomFont ?BCustomFont:$FontNameSize"
179 " $CompileDate sPassword"},
180 {"HEADER",
181 "MStartupText #StartRoom MWinText"},
182 {"GLOBAL",
183 "$GameName $GameAuthor $DontUnderstand #Perspective BShowExits #WaitTurns"
184 " BDispFirstRoom BBattleSystem #MaxScore $PlayerName BPromptName $PlayerDesc"
185 " #Task ?!#Task=0:$AltDesc #Position #ParentObject #PlayerGender"
186 " #MaxSize #MaxWt ?GBattleSystem:<BATTLE>Battle BEightPointCompass bNoDebug"
187 " BNoScoreNotify BNoMap bNoAutoComplete bNoControlPanel bNoMouse"
188 " BSound BGraphics <RESOURCE>IntroRes <RESOURCE>WinRes FStatusBox"
189 " EStatusBoxText iUnk1 iUnk2 FEmbedded"},
190 {"BATTLE",
191 "iStamina iStrength iDefense"},
192 {"ROOM",
193 "$Short $Long $LastDesc ?GEightPointCompass:[12]<ROOM_EXIT>Exits"
194 " ?!GEightPointCompass:[8]<ROOM_EXIT>Exits $AddDesc1 #Task1 $AddDesc2 #Task2"
195 " #Obj $AltDesc #TypeHideObjects <RESOURCE>Res <RESOURCE>LastRes"
196 " <RESOURCE>Task1Res <RESOURCE>Task2Res <RESOURCE>AltRes"
197 " ?!GNoMap:bHideOnMap |V390_ROOM:_Alts_|"},
198 {"ROOM_EXIT",
199 "{V390_V380_ROOM_EXIT:#Dest_#Var1_#Var2_ZVar3}"},
200 {"RESOURCE",
201 "?GSound:$SoundFile,ZSoundLen,ZSoundOffset"
202 " ?GGraphics:$GraphicFile,ZGraphicLen,ZGraphicOffset"},
203 {"OBJECT",
204 "$Prefix $Short"
205 " [1]$Alias BStatic $Description #InitialPosition #Task BTaskNotDone"
206 " $AltDesc ?BStatic:<ROOM_LIST1>Where BContainer BSurface #Capacity"
207 " ?!BStatic:BWearable,#SizeWeight,#Parent ?BStatic:{OBJECT:#Parent}"
208 " #Openable |V390_OBJECT:_Openable_,Key| #SitLie ?!BStatic:BEdible BReadable"
209 " ?BReadable:$ReadText ?!BStatic:BWeapon ZCurrentState FListFlag"
210 " <RESOURCE>Res1 <RESOURCE>Res2 ?GBattleSystem:<OBJ_BATTLE>Battle"
211 " EInRoomDesc ZOnlyWhenNotMoved"},
212 {"OBJ_BATTLE",
213 "iProtectionValue iHitValue iMethod"},
214 {"ROOM_LIST1",
215 "#Type {ROOM_LIST1}"},
216 {"TASK",
217 "W$Command $CompleteText $ReverseMessage $RepeatText $AdditionalMessage"
218 " #ShowRoomDesc BRepeatable BReversible W$ReverseCommand <ROOM_LIST0>Where"
219 " $Question ?$Question:$Hint1,$Hint2 V<TASK_RESTR>Restrictions"
220 " V<TASK_ACTION>Actions |V390_TASK:$RestrMask| <RESOURCE>Res"},
221 {"TASK_RESTR",
222 "#Type ?#Type=0:#Var1,#Var2,#Var3 ?#Type=1:#Var1,#Var2 ?#Type=2:#Var1,#Var2"
223 " ?#Type=3:#Var1,#Var2,#Var3 ?#Type=4:#Var1,#Var2,#Var3,EVar4"
224 ",|V390_TASK_RESTR:Var1>0?#Var1++| $FailMessage"},
225 {"TASK_ACTION",
226 "#Type |V390_TASK_ACTION:Type>4?#Type++| ?#Type=0:#Var1,#Var2,#Var3"
227 " ?#Type=1:#Var1,#Var2,#Var3 ?#Type=2:#Var1,#Var2"
228 " ?#Type=3:#Var1,#Var2,#Var3,|V390_TASK_ACTION:$Expr_#Var5|"
229 " ?#Type=4:#Var1 ?#Type=6:#Var1,ZVar2,ZVar3 ?#Type=7:iVar1,iVar2,iVar3"},
230 {"ROOM_LIST0",
231 "#Type {ROOM_LIST0}"},
232 {"EVENT",
233 "$Short #StarterType ?#StarterType=2:#StartTime,#EndTime"
234 " ?#StarterType=3:#TaskNum #RestartType BTaskFinished #Time1 #Time2"
235 " $StartText $LookText $FinishText <ROOM_LIST0>Where #PauseTask"
236 " BPauserCompleted #PrefTime1 $PrefText1 #ResumeTask BResumerCompleted"
237 " #PrefTime2 $PrefText2 #Obj2 #Obj2Dest #Obj3 #Obj3Dest #Obj1 #Obj1Dest"
238 " #TaskAffected [5]<RESOURCE>Res"},
239 {"NPC",
240 "$Name $Prefix [1]$Alias $Descr #StartRoom $AltText #Task V<TOPIC>Topics"
241 " V<WALK>Walks BShowEnterExit ?BShowEnterExit:$EnterText,$ExitText"
242 " $InRoomText #Gender [4]<RESOURCE>Res ?GBattleSystem:<NPC_BATTLE>Battle"},
243 {"NPC_BATTLE",
244 "iAttitude iStamina iStrength iDefense iSpeed iKilledTask"},
245 {"TOPIC",
246 "$Subject $Reply #Task $AltReply"},
247 {"WALK",
248 "#NumStops BLoop #StartTask #CharTask #MeetObject #ObjectTask #StoppingTask"
249 " ZMeetChar $ChangedDesc {WALK:#Rooms_#Times}"},
250 {"ROOM_GROUP",
251 "$Name {ROOM_GROUP:[]BList}"},
252 {"SYNONYM",
253 "$Replacement $Original"},
254 {"VARIABLE",
255 "$Name ZType $Value"},
256 {"ALR",
257 "$Original $Replacement"},
258 {NULL, NULL}
259 };
260
261 /* Version 3.8 TAF file properties descriptor table. */
262 static const sc_parse_schema_t V380_PARSE_SCHEMA[] = {
263 {"_GAME_",
264 "<HEADER>Header <GLOBAL>Globals V<ROOM>Rooms V<OBJECT>Objects V<TASK>Tasks"
265 " V<EVENT>Events V<NPC>NPCs V<ROOM_GROUP>RoomGroups V<SYNONYM>Synonyms"
266 " FCustomFont $CompileDate sPassword |V380_GLOBAL:_MaxScore_|"
267 " |V380_OBJECT:_InitialPositions_|"},
268 {"HEADER",
269 "MStartupText #StartRoom MWinText"},
270 {"GLOBAL",
271 "$GameName $GameAuthor #MaxCarried |V380_MaxSize_MaxWt_| $DontUnderstand"
272 " #Perspective BShowExits #WaitTurns FDispFirstRoom FBattleSystem"
273 " EPlayerName FPromptName EPlayerDesc ZTask ZPosition ZParentObject"
274 " ZPlayerGender FEightPointCompass TNoScoreNotify FSound FGraphics"
275 " FStatusBox EStatusBoxText FEmbedded"},
276 {"ROOM",
277 "$Short $Long $LastDesc [8]<ROOM_EXIT>Exits $AddDesc1 #Task1 $AddDesc2"
278 " #Task2 #Obj $AltDesc #TypeHideObjects |V380_ROOM:_Alts_|"},
279 {"ROOM_EXIT",
280 "{V390_V380_ROOM_EXIT:#Dest_#Var1_#Var2_ZVar3}"},
281 {"OBJECT",
282 "$Prefix $Short [1]$Alias BStatic $Description #InitialPosition #Task"
283 " BTaskNotDone $AltDesc ?BStatic:<ROOM_LIST1>Where #SurfaceContainer"
284 " FSurface ?#SurfaceContainer=2:TSurface FContainer"
285 " ?#SurfaceContainer=1:TContainer #Capacity |V380_OBJECT:#Capacity*10+2|"
286 " ?!BStatic:BWearable,#SizeWeight,#Parent ?BStatic:{OBJECT:#Parent}"
287 " #Openable |V380_OBJECT:_Openable_,Key| #SitLie ?!BStatic:BEdible BReadable"
288 " ?BReadable:$ReadText ?!BStatic:BWeapon ZCurrentState FListFlag"
289 " EInRoomDesc ZOnlyWhenNotMoved"},
290 {"ROOM_LIST1",
291 "#Type {ROOM_LIST1}"},
292 {"TASK",
293 "W$Command $CompleteText $ReverseMessage $RepeatText $AdditionalMessage"
294 " #ShowRoomDesc BRepeatable #Score BSingleScore [6]<TASK_MOVE>Movements"
295 " BReversible W$ReverseCommand #WearObj1 #WearObj2 #HoldObj1 #HoldObj2"
296 " #HoldObj3 #Obj1 #Task BTaskNotDone $TaskMsg $HoldMsg $WearMsg $CompanyMsg"
297 " BNotInSameRoom #NPC $Obj1Msg #Obj1Room <ROOM_LIST0>Where BKillsPlayer"
298 " BHoldingSameRoom $Question ?$Question:$Hint1,$Hint2 #Obj2"
299 " ?!#Obj2=0:#Obj2Var1,#Obj2Var2,$Obj2Msg BWinGame |V380_TASK:_Actions_|"
300 " |V380_TASK:_Restrictions_|"},
301 {"TASK_MOVE",
302 "#Var1 #Var2 #Var3"},
303 {"ROOM_LIST0",
304 "#Type {ROOM_LIST0}"},
305 {"EVENT",
306 "$Short #StarterType ?#StarterType=2:#StartTime,#EndTime"
307 " ?#StarterType=3:#TaskNum #RestartType BTaskFinished #Time1 #Time2"
308 " $StartText $LookText $FinishText <ROOM_LIST0>Where #PauseTask"
309 " BPauserCompleted #PrefTime1 $PrefText1 #ResumeTask BResumerCompleted"
310 " #PrefTime2 $PrefText2 #Obj2 #Obj2Dest #Obj3 #Obj3Dest #Obj1 #Obj1Dest"
311 " #TaskAffected"},
312 {"NPC",
313 "$Name $Prefix [1]$Alias $Descr #StartRoom $AltText #Task V<TOPIC>Topics"
314 " V<WALK>Walks BShowEnterExit ?BShowEnterExit:$EnterText,$ExitText"
315 " $InRoomText ZGender"},
316 {"TOPIC",
317 "$Subject $Reply #Task $AltReply"},
318 {"WALK",
319 "#NumStops BLoop #StartTask #CharTask #MeetObject"
320 " ?!#MeetObject=0:|V380_WALK:_MeetObject_| #ObjectTask ZMeetChar"
321 " {WALK:#Rooms_#Times} ZStoppingTask EChangedDesc"},
322 {"ROOM_GROUP",
323 "$Name {ROOM_GROUP:[]BList}"},
324 {"SYNONYM",
325 "$Replacement $Original"},
326 {NULL, NULL}
327 };
328
329
330 /*
331 * parse_select_schema()
332 *
333 * Select one of the parse schemata based on a TAF file.
334 */
335 static const sc_parse_schema_t *
336 parse_select_schema (sc_tafref_t taf)
337 {
338 /* Switch based on the TAF file version. */
339 switch (taf_get_version (taf))
340 {
341 case TAF_VERSION_400:
342 return V400_PARSE_SCHEMA;
343 case TAF_VERSION_390:
344 return V390_PARSE_SCHEMA;
345 case TAF_VERSION_380:
346 return V380_PARSE_SCHEMA;
347 default:
348 sc_fatal ("parse_select_schema: invalid TAF file version\n");
349 return NULL;
350 }
351 }
352
353
354 /* The uncompressed TAF file from which we get all our data. */
355 static sc_tafref_t parse_taf = NULL;
356 static sc_int parse_tafline = 0;
357
358 /* The parse schema selected for this TAF file. */
359 static sc_parse_schema_t const *parse_schema = NULL;
360
361 /* Properties bundle and trace flag, set before parsing. */
362 static sc_prop_setref_t parse_bundle = NULL;
363 static sc_bool parse_trace = FALSE;
364
365 /*
366 * Stack of property keys. The stack is filled by parsing, and written
367 * to the property store on parse terminals.
368 */
369 static sc_vartype_t parse_vt_key[PARSE_MAX_DEPTH];
370 static sc_char parse_format[PARSE_MAX_DEPTH];
371 static sc_int parse_depth = 0;
372
373
374 /*
375 * parse_push_key()
376 * parse_pop_key()
377 *
378 * Push a key of the given type onto the property key stack, and pop a key
379 * off on unwind.
380 */
381 static void
382 parse_push_key (sc_vartype_t vt_key, sc_char type)
383 {
384 if (parse_depth == PARSE_MAX_DEPTH)
385 sc_fatal ("parse_push_key: stack overrun\n");
386
387 /* Push the key, and its associated type. */
388 parse_vt_key[parse_depth] = vt_key;
389 parse_format[parse_depth] = type;
390 parse_depth++;
391 }
392
393 static void
394 parse_pop_key (void)
395 {
396 /* Check the stack has something to pop, then pop it. */
397 if (parse_depth == 0)
398 sc_fatal ("parse_pop_key: stack underrun\n");
399 parse_depth--;
400 }
401
402
403 /*
404 * parse_retrieve_stack()
405 *
406 * This is ugly. The parse produces indexes before the things that they
407 * index. An expedient fix is to switch i-s keys before storing a property
408 * value
409 */
410 static void
411 parse_retrieve_stack (sc_char format[], sc_vartype_t vt_key[], sc_int *depth)
412 {
413 sc_int index_;
414
415 /* Switch index-string key pairs. */
416 for (index_ = 0; index_ < parse_depth; index_++)
417 {
418 if (index_ < parse_depth - 1
419 && parse_format[index_] == PROP_KEY_INTEGER
420 && parse_format[index_ + 1] == PROP_KEY_STRING)
421 {
422 /* Swap format and key elements. */
423 format[index_] = parse_format[index_ + 1];
424 format[index_ + 1] = parse_format[index_];
425 vt_key[index_] = parse_vt_key[index_ + 1];
426 vt_key[index_ + 1] = parse_vt_key[index_];
427
428 index_++;
429 }
430 else
431 {
432 /* Simple copy of format and key elements. */
433 format[index_] = parse_format[index_];
434 vt_key[index_] = parse_vt_key[index_];
435 }
436 }
437
438 /* Return the parse depth. */
439 *depth = parse_depth;
440 }
441
442
443 /*
444 * parse_stack_backtrace()
445 *
446 * Dump the parse stack. Used for diagnostics on finding what we think may
447 * be a bad game.
448 */
449 static void
450 parse_stack_backtrace (void)
451 {
452 sc_vartype_t vt_key[PARSE_MAX_DEPTH];
453 sc_char format[PARSE_MAX_DEPTH];
454 sc_int depth, index_;
455
456 parse_retrieve_stack (format, vt_key, &depth);
457
458 sc_error ("parse_stack_backtrace: version %s schema parsed to depth %ld\n",
459 (parse_schema == V400_PARSE_SCHEMA) ? "4.00" :
460 (parse_schema == V390_PARSE_SCHEMA) ? "3.90" :
461 (parse_schema == V380_PARSE_SCHEMA) ? "3.80" : "[Invalid]",
462 depth);
463
464 sc_error ("parse_stack_backtrace: parse stack backtrace follows...\n");
465 for (index_ = 0; index_ < depth; index_++)
466 {
467 sc_char type;
468
469 type = format[index_];
470 if (type == PROP_KEY_INTEGER)
471 sc_error ("%2ld - [%c] %ld\n", index_, type, vt_key[index_].integer);
472 else if (type == PROP_KEY_STRING)
473 sc_error ("%2ld - [%c] \"%s\"\n", index_, type, vt_key[index_].string);
474 else
475 sc_error ("%2ld - [%c] %p\n", index_, type, vt_key[index_].voidp);
476 }
477 }
478
479
480 /*
481 * parse_put_property()
482 * parse_get_property()
483 *
484 * Write or read a property based on the keys amassed so far.
485 */
486 static void
487 parse_put_property (sc_vartype_t vt_value, sc_char type)
488 {
489 sc_vartype_t vt_key[PARSE_MAX_DEPTH];
490 sc_char format[PARSE_MAX_DEPTH + 4];
491 sc_int depth;
492
493 /* Retrieve the adjusted stack. */
494 parse_retrieve_stack (format + 3, vt_key, &depth);
495
496 /* Complete the format for the property put. */
497 format[0] = type;
498 format[1] = '-';
499 format[2] = '>';
500 format[depth + 3] = NUL;
501
502 /* Store the property under the stacked keys. */
503 assert (parse_bundle);
504 prop_put (parse_bundle, format, vt_value, vt_key);
505 }
506
507 static sc_bool
508 parse_get_property (sc_vartype_t *vt_rvalue, sc_char type)
509 {
510 sc_vartype_t vt_key[PARSE_MAX_DEPTH];
511 sc_char format[PARSE_MAX_DEPTH + 4];
512 sc_int depth;
513 sc_bool status;
514
515 /* Retrieve the adjusted stack. */
516 parse_retrieve_stack (format + 3, vt_key, &depth);
517
518 /* Complete the format for the property put. */
519 format[0] = type;
520 format[1] = '<';
521 format[2] = '-';
522 format[depth + 3] = NUL;
523
524 /* Retrieve the property using the stacked keys. */
525 assert (parse_bundle);
526 status = prop_get (parse_bundle, format, vt_rvalue, vt_key);
527
528 return status;
529 }
530
531
532 /*
533 * parse_get_child_count()
534 *
535 * Convenience form of parse_get_property(), retrieve an integer property
536 * indicating the child count of the effectively stacked node, or zero if
537 * no such node exists.
538 */
539 static sc_int
540 parse_get_child_count (void)
541 {
542 sc_vartype_t vt_rvalue;
543
544 if (!parse_get_property (&vt_rvalue, PROP_INTEGER))
545 vt_rvalue.integer = 0;
546
547 return vt_rvalue.integer;
548 }
549
550
551 /*
552 * parse_get_integer_property()
553 * parse_get_boolean_property()
554 * parse_get_string_property()
555 *
556 * Convenience forms of parse_get_property(), retrieve directly, and report
557 * a fatal error if the property does not exist.
558 */
559 static sc_int
560 parse_get_integer_property (void)
561 {
562 sc_vartype_t vt_rvalue;
563
564 if (!parse_get_property (&vt_rvalue, PROP_INTEGER))
565 sc_fatal ("parse_get_integer_property: missing property\n");
566
567 return vt_rvalue.integer;
568 }
569
570 static sc_bool
571 parse_get_boolean_property (void)
572 {
573 sc_vartype_t vt_rvalue;
574
575 if (!parse_get_property (&vt_rvalue, PROP_BOOLEAN))
576 sc_fatal ("parse_get_boolean_property: missing property\n");
577
578 return vt_rvalue.boolean;
579 }
580
581 static const sc_char *
582 parse_get_string_property (void)
583 {
584 sc_vartype_t vt_rvalue;
585
586 if (!parse_get_property (&vt_rvalue, PROP_STRING))
587 sc_fatal ("parse_get_string_property: missing property\n");
588
589 return vt_rvalue.string;
590 }
591
592
593 /* Parse error jump buffer. */
594 static jmp_buf parse_taf_error;
595
596 /* Pushback line, and pushback requested flag. */
597 static const sc_char *parse_pushback_line = NULL;
598 static sc_bool parse_use_pushback = FALSE;
599
600 /*
601 * parse_get_taf_string()
602 * parse_get_taf_integer()
603 * parse_get_taf_boolean()
604 * parse_taf_pushback()
605 *
606 * Wrapper round obtaining the next TAF file line, with variants to convert
607 * the line content into an integer or boolean, and a function for effective
608 * TAF line pushback.
609 */
610 static const sc_char *
611 parse_get_taf_string (void)
612 {
613 const sc_char *line;
614
615 /* If pushback requested, use that instead of reading. */
616 if (parse_use_pushback)
617 {
618 /* Use the pushback line, and clear the request. */
619 assert (parse_pushback_line);
620 line = parse_pushback_line;
621 parse_use_pushback = FALSE;
622 }
623 else
624 {
625 /* Get the next line, and complain if absent. */
626 line = taf_next_line (parse_taf);
627 if (!line)
628 {
629 sc_error ("parse_get_taf_string:"
630 " out of TAF data at line %ld\n", parse_tafline);
631 parse_stack_backtrace ();
632 longjmp (parse_taf_error, 1);
633 }
634
635 /* Note this line for possible pushback. */
636 parse_pushback_line = line;
637 }
638
639 /* Print out the line we're parsing if tracing. */
640 if (parse_trace)
641 sc_trace ("Parse: read in line %ld : %s\n", parse_tafline, line);
642
643 parse_tafline++;
644 return line;
645 }
646
647 static sc_int
648 parse_get_taf_integer (void)
649 {
650 const sc_char *line;
651 sc_int integer;
652
653 /* Get line, and scan for a single integer; return it. */
654 line = parse_get_taf_string ();
655 if (sscanf (line, "%ld", &integer) != 1)
656 {
657 sc_error ("parse_get_taf_integer:"
658 " invalid integer at line %ld\n", parse_tafline - 1);
659 parse_stack_backtrace ();
660 longjmp (parse_taf_error, 1);
661 }
662
663 return integer;
664 }
665
666 static sc_bool
667 parse_get_taf_boolean (void)
668 {
669 const sc_char *line;
670 sc_uint boolean;
671
672 /*
673 * Get line, and scan for a single integer; check it's a valid-looking flag,
674 * and return it.
675 */
676 line = parse_get_taf_string ();
677 if (sscanf (line, "%lu", &boolean) != 1)
678 {
679 sc_error ("parse_get_taf_boolean:"
680 " invalid boolean at line %ld\n", parse_tafline - 1);
681 parse_stack_backtrace ();
682 longjmp (parse_taf_error, 1);
683 }
684 if (boolean != 0 && boolean != 1)
685 {
686 sc_error ("parse_get_taf_boolean:"
687 " warning: suspect boolean at line %ld\n", parse_tafline - 1);
688 }
689
690 return boolean != 0;
691 }
692
693 static void
694 parse_taf_pushback (void)
695 {
696 if (parse_use_pushback || !parse_pushback_line)
697 sc_fatal ("parse_taf_pushback: too much pushback requested\n");
698
699 /* Set pushback request, and decrement line counter. */
700 parse_use_pushback = TRUE;
701 parse_tafline--;
702
703 /* Note pushback for tracing purposes. */
704 if (parse_trace)
705 sc_trace ("Parse: push back at line %ld\n", parse_tafline);
706 }
707
708
709 /* Enumerations of parse types found in the parse schema. */
710 enum
711 { PARSE_INTEGER = '#',
712 PARSE_DEFAULT_ZERO = 'Z',
713 PARSE_BOOLEAN = 'B',
714 PARSE_DEFAULT_FALSE = 'F',
715 PARSE_DEFAULT_TRUE = 'T',
716 PARSE_STRING = '$',
717 PARSE_DEFAULT_EMPTY = 'E',
718 PARSE_MULTILINE = 'M',
719 PARSE_VECTOR = 'V',
720 PARSE_VECTOR_ALTERNATE = 'W',
721 PARSE_ARRAY = '[',
722 PARSE_EXPRESSION = '?',
723 PARSE_EXPRESSION_NOT = '!',
724 PARSE_GLOBAL_EXPRESSION = 'G',
725 PARSE_CLASS = '<',
726 PARSE_FIXUP = '|',
727 PARSE_SPECIAL = '{',
728 PARSE_IGNORE_INTEGER = 'i',
729 PARSE_IGNORE_BOOLEAN = 'b',
730 PARSE_IGNORE_STRING = 's'
731 };
732
733 /* Forward declarations of parse functions for recursion. */
734 static void parse_element (const sc_char *element);
735 static void parse_class (const sc_char *class_);
736 static void parse_descriptor (const sc_char *descriptor);
737
738 /*
739 * parse_array()
740 *
741 * Parse a descriptor [] array.
742 */
743 static void
744 parse_array (const sc_char *array)
745 {
746 sc_int count, index_;
747 sc_char element[PARSE_TEMP_LENGTH];
748
749 if (parse_trace)
750 sc_trace ("Parse: entering array %s\n", array);
751
752 /* Find the count of elements in the array, and the element itself. */
753 if (sscanf (array, "[%ld]%[^ ]", &count, element) != 2)
754 sc_fatal ("parse_array: bad array, %s\n", array);
755
756 /* Parse the element for array count iterations, each a key. */
757 for (index_ = 0; index_ < count; index_++)
758 {
759 sc_vartype_t vt_key;
760
761 vt_key.integer = index_;
762 parse_push_key (vt_key, PROP_KEY_INTEGER);
763
764 parse_element (element);
765
766 parse_pop_key ();
767 }
768
769 if (parse_trace)
770 sc_trace ("Parse: leaving array %s\n", array);
771 }
772
773
774 /*
775 * parse_vector_common()
776 * parse_vector()
777 * parse_vector_alternate()
778 *
779 * Parse a variable-length vector of properties.
780 */
781 static void
782 parse_vector_common (const sc_char *vector, sc_int count)
783 {
784 sc_int index_;
785
786 /* Parse the vector property count times, pushing a key on each. */
787 for (index_ = 0; index_ < count; index_++)
788 {
789 sc_vartype_t vt_key;
790
791 vt_key.integer = index_;
792 parse_push_key (vt_key, PROP_KEY_INTEGER);
793
794 parse_element (vector + 1);
795
796 parse_pop_key ();
797 }
798 }
799
800 static void
801 parse_vector (const sc_char *vector)
802 {
803 sc_int count;
804
805 if (parse_trace)
806 sc_trace ("Parse: entering vector %s\n", vector);
807
808 /* Find the count of elements in the vector, and parse. */
809 count = parse_get_taf_integer ();
810 parse_vector_common (vector, count);
811
812 if (parse_trace)
813 sc_trace ("Parse: leaving vector %s\n", vector);
814 }
815
816 static void
817 parse_vector_alternate (const sc_char *vector)
818 {
819 sc_int count;
820
821 if (parse_trace)
822 sc_trace ("Parse: entering alternate vector %s\n", vector);
823
824 /* Element count, this is a vector described by size - 1. */
825 count = parse_get_taf_integer () + 1;
826 parse_vector_common (vector, count);
827
828 if (parse_trace)
829 sc_trace ("Parse: leaving alternate vector %s\n", vector);
830 }
831
832
833 /*
834 * parse_test_expression()
835 * parse_expression()
836 *
837 * Parse a conditional field definition, with runtime test.
838 */
839 static sc_bool
840 parse_test_expression (const sc_char *test_expression)
841 {
842 sc_vartype_t vt_key;
843 sc_char plhs[PARSE_TEMP_LENGTH];
844 sc_int rhs;
845 sc_bool retval = FALSE;
846
847 /* Identify the type of expression to evaluate. */
848 switch (test_expression[0])
849 {
850 case PARSE_BOOLEAN:
851 /* Read boolean property and return its value. */
852 vt_key.string = test_expression + 1;
853 parse_push_key (vt_key, PROP_KEY_STRING);
854 retval = parse_get_boolean_property ();
855 parse_pop_key ();
856 break;
857
858 case PARSE_INTEGER:
859 /* Get the left and right sides of = comparison. */
860 if (sscanf (test_expression, "#%[^=]=%ld", plhs, &rhs) != 2)
861 {
862 sc_fatal ("parse_test_expression: bad = compare, %s\n",
863 test_expression + 1);
864 }
865
866 /* Read integer property and return comparison. */
867 vt_key.string = plhs;
868 parse_push_key (vt_key, PROP_KEY_STRING);
869 retval = (parse_get_integer_property () == rhs);
870 parse_pop_key ();
871 break;
872
873 case PARSE_STRING:
874 /* Read property and return TRUE if not an empty string. */
875 vt_key.string = test_expression + 1;
876 parse_push_key (vt_key, PROP_KEY_STRING);
877 retval = !sc_strempty (parse_get_string_property ());
878 parse_pop_key ();
879 break;
880
881 case PARSE_GLOBAL_EXPRESSION:
882 {
883 sc_vartype_t vt_gkey[2];
884
885 /* Read the given Global boolean property and return it. */
886 vt_gkey[0].string = "Globals";
887 vt_gkey[1].string = test_expression + 1;
888 retval = prop_get_boolean (parse_bundle, "B<-ss", vt_gkey);
889 break;
890 }
891
892 default:
893 sc_fatal ("parse_test_expression:"
894 " bad expression, %s\n", test_expression + 1);
895 }
896
897 if (parse_trace)
898 sc_trace ("Parse: expression is %s\n", retval ? "true" : "false");
899
900 return retval;
901 }
902
903 static void
904 parse_expression (const sc_char *expression)
905 {
906 sc_char test_expression[PARSE_TEMP_LENGTH];
907 sc_bool is_present;
908
909 if (parse_trace)
910 sc_trace ("Parse: entering expression %s\n", expression);
911
912 /* Isolate the test part of the expression. */
913 if (sscanf (expression, "?%[^:]", test_expression) != 1)
914 sc_fatal ("parse_expression: bad expression, %s\n", expression);
915
916 /* Handle the remainder of the expression only if test passes. */
917 is_present = (test_expression[0] == PARSE_EXPRESSION_NOT)
918 ? !parse_test_expression (test_expression + 1)
919 : parse_test_expression (test_expression);
920 if (is_present)
921 {
922 sc_int next;
923
924 /*
925 * Following the ':' may be a single element, or a comma-separated list.
926 */
927 for (next = strlen (test_expression) + 2; expression[next] != NUL; )
928 {
929 sc_char element[PARSE_TEMP_LENGTH];
930
931 /* Get the next individual element to parse. */
932 if (sscanf (expression + next, "%[^,]", element) != 1)
933 sc_fatal ("parse_expression: bad list, %s\n", expression + next);
934
935 /* Parse this isolated element. */
936 parse_element (element);
937
938 /* Advance to the start of the next element. */
939 next += strlen (element);
940 next += strspn (expression + next, ",");
941 }
942 }
943
944 if (parse_trace)
945 sc_trace ("Parse: leaving expression %s\n", expression);
946 }
947
948
949 /*
950 * parse_read_multiline()
951 *
952 * Helper for parse_terminal(), reads in a multiline string. The return
953 * string is malloc'ed, and the caller needs to handle that.
954 */
955 static sc_char *
956 parse_read_multiline (void)
957 {
958 const sc_byte *separator = NULL;
959 const sc_char *line;
960 sc_char *multiline;
961
962 /* Select the appropriate multiline separator. */
963 switch (taf_get_version (parse_taf))
964 {
965 case TAF_VERSION_400:
966 separator = V400_SEPARATOR;
967 break;
968 case TAF_VERSION_390:
969 separator = V390_SEPARATOR;
970 break;
971 case TAF_VERSION_380:
972 separator = V380_SEPARATOR;
973 break;
974 default:
975 sc_fatal ("parse_read_multiline: invalid TAF file version\n");
976 break;
977 }
978
979 /* Take a simple copy of the first line. */
980 line = parse_get_taf_string ();
981 multiline = sc_malloc (strlen (line) + 1);
982 strcpy (multiline, line);
983
984 /* Now concatenate until separator found. */
985 line = parse_get_taf_string ();
986 while (memcmp (line, separator, SEPARATOR_SIZE) != 0)
987 {
988 multiline = sc_realloc (multiline,
989 strlen (multiline) + strlen (line) + 2);
990 strcat (multiline, "\n");
991 strcat (multiline, line);
992 line = parse_get_taf_string ();
993 }
994
995 return multiline;
996 }
997
998
999 /*
1000 * parse_terminal()
1001 *
1002 * Common handler for string, integer, boolean, and multiline parse terminals.
1003 */
1004 static void
1005 parse_terminal (const sc_char *terminal)
1006 {
1007 sc_vartype_t vt_key, vt_value;
1008
1009 if (parse_trace)
1010 sc_trace ("Parse: entering terminal %s\n", terminal);
1011
1012 /* Push the key string. */
1013 vt_key.string = terminal + 1;
1014 parse_push_key (vt_key, PROP_KEY_STRING);
1015
1016 /* Retrieve, or invent, then store the value. */
1017 switch (terminal[0])
1018 {
1019 case PARSE_INTEGER:
1020 vt_value.integer = parse_get_taf_integer ();
1021 parse_put_property (vt_value, PROP_INTEGER);
1022 break;
1023 case PARSE_DEFAULT_ZERO:
1024 vt_value.integer = 0;
1025 parse_put_property (vt_value, PROP_INTEGER);
1026 break;
1027
1028 case PARSE_BOOLEAN:
1029 vt_value.boolean = parse_get_taf_boolean ();
1030 parse_put_property (vt_value, PROP_BOOLEAN);
1031 break;
1032 case PARSE_DEFAULT_FALSE:
1033 case PARSE_DEFAULT_TRUE:
1034 vt_value.boolean = (terminal[0] == PARSE_DEFAULT_TRUE);
1035 parse_put_property (vt_value, PROP_BOOLEAN);
1036 break;
1037
1038 case PARSE_STRING:
1039 vt_value.string = parse_get_taf_string ();
1040 parse_put_property (vt_value, PROP_STRING);
1041 break;
1042 case PARSE_DEFAULT_EMPTY:
1043 vt_value.string = "";
1044 parse_put_property (vt_value, PROP_STRING);
1045 break;
1046
1047 case PARSE_MULTILINE:
1048 /* Assign to and adopt mutable string rather than const string. */
1049 vt_value.mutable_string = parse_read_multiline ();
1050 parse_put_property (vt_value, PROP_STRING);
1051
1052 assert (parse_bundle);
1053 prop_adopt (parse_bundle, vt_value.mutable_string);
1054 break;
1055
1056 case PARSE_IGNORE_INTEGER:
1057 (void) parse_get_taf_integer ();
1058 break;
1059 case PARSE_IGNORE_BOOLEAN:
1060 (void) parse_get_taf_boolean ();
1061 break;
1062 case PARSE_IGNORE_STRING:
1063 (void) parse_get_taf_string ();
1064 break;
1065
1066 default:
1067 sc_fatal ("parse_terminal: bad type, %c\n", terminal[0]);
1068 }
1069
1070 /* Pop terminal key. */
1071 parse_pop_key ();
1072
1073 if (parse_trace)
1074 sc_trace ("Parse: leaving terminal %s\n", terminal);
1075 }
1076
1077
1078 /*
1079 * Resources table. This table enables resource offsets to be calculated
1080 * for the various sound and graphic resources encountered on parsing
1081 * version 4.0 games. It's unused if the version is not 4.0.
1082 */
1083 typedef struct
1084 {
1085 sc_char *name;
1086 sc_uint hash;
1087 sc_int length;
1088 sc_int offset;
1089 } sc_parse_resource_t;
1090
1091 enum { RESOURCE_GROW_INCREMENT = 32 };
1092 static sc_int parse_resources_length = 0;
1093 static sc_int parse_resources_size = 0;
1094 static sc_parse_resource_t *parse_resources = NULL;
1095
1096
1097 /*
1098 * parse_clear_v400_resources_table()
1099 *
1100 * Free and clear down the version 4.0 resources table.
1101 */
1102 static void
1103 parse_clear_v400_resources_table (void)
1104 {
1105 /* Free allocated memory and return to initial values. */
1106 if (parse_resources)
1107 {
1108 sc_int index_;
1109
1110 for (index_ = 0; index_ < parse_resources_length; index_++)
1111 sc_free (parse_resources[index_].name);
1112
1113 sc_free (parse_resources);
1114 parse_resources = NULL;
1115 }
1116 parse_resources_length = 0;
1117 parse_resources_size = 0;
1118 }
1119
1120
1121 /*
1122 * parse_get_v400_resource_offset()
1123 *
1124 * Notes version 4.0 resource names encountered in the parse, and their
1125 * lengths, and builds up a list of resources with their data offsets. The
1126 * function assumes that resources are appended to the TAF file in the
1127 * order in which they are encountered when reading through the TAF file.
1128 *
1129 * A warning -- this function may return a new length. Resources that
1130 * have been seen once already have non-useful (though apparently non-zero)
1131 * lengths; this function needs to handle that. The caller needs to compare
1132 * length with real_length to see if that happened.
1133 */
1134 static sc_int
1135 parse_get_v400_resource_offset (const sc_char *name,
1136 sc_int length, sc_int *real_length)
1137 {
1138 sc_char *clean_name;
1139 sc_uint hash;
1140 sc_int index_, offset;
1141
1142 /*
1143 * Take a copy of the name, and remove any trailing "##" looping sound
1144 * indicator flag. Who thinks this junk up?
1145 */
1146 clean_name = sc_malloc (strlen (name) + 1);
1147 strcpy (clean_name, name);
1148 if (strcmp (clean_name + strlen (clean_name) - 2, "##") == 0)
1149 clean_name[strlen (clean_name) - 2] = NUL;
1150
1151 /*
1152 * Scan the current resources list for a matching name, and if the resource
1153 * is already known, return its offset. The hash check is an attempt to
1154 * improve the search times, relative to using only string comparisons --
1155 * the table's not fully hashed. If found, we need to also pass back the
1156 * corrected length.
1157 */
1158 offset = -1;
1159 hash = sc_hash (clean_name);
1160 for (index_ = 0; index_ < parse_resources_length; index_++)
1161 {
1162 if (parse_resources[index_].hash == hash
1163 && strcmp (parse_resources[index_].name, clean_name) == 0)
1164 {
1165 offset = parse_resources[index_].offset;
1166 break;
1167 }
1168 }
1169 if (offset != -1)
1170 {
1171 *real_length = parse_resources[index_].length;
1172 sc_free (clean_name);
1173 return offset;
1174 }
1175
1176 /* Resize the resources table if required. */
1177 if (parse_resources_length == parse_resources_size)
1178 {
1179 parse_resources_size += RESOURCE_GROW_INCREMENT;
1180 parse_resources = sc_realloc (parse_resources,
1181 parse_resources_size *
1182 sizeof (parse_resources[0]));
1183 }
1184
1185 /*
1186 * Calculate the offset. For the first resource, it's zero; for others,
1187 * it's one after the prior entry's offset and length.
1188 */
1189 if (parse_resources_length == 0)
1190 offset = 0;
1191 else
1192 {
1193 offset = parse_resources[parse_resources_length - 1].offset
1194 + parse_resources[parse_resources_length - 1].length + 1;
1195 }
1196
1197 /* Add details to the table. */
1198 parse_resources[parse_resources_length].name = clean_name;
1199 parse_resources[parse_resources_length].hash = hash;
1200 parse_resources[parse_resources_length].offset = offset;
1201 parse_resources[parse_resources_length].length = length;
1202 parse_resources_length++;
1203
1204 *real_length = length;
1205 return offset;
1206 }
1207
1208
1209 /*
1210 * parse_handle_v400_resources()
1211 *
1212 * Extra special handling for version 4.0 resources; extracts details of
1213 * the resource just parsed, and adds an offset property for each defined.
1214 *
1215 * A warning -- Adrift seems to use -ve numbers as lengths for resources
1216 * already parsed, where TAF files include the resource. It's unclear
1217 * what the -ve values mean, so here we ignore them and work off the
1218 * resource file name given. This means we have to look for length not
1219 * equal to zero, not just lengths greater than zero.
1220 *
1221 * TODO Work out what this means. The -ve lengths look like a form of
1222 * 'resource number'; -(length+2) is tantalizingly close to the index into
1223 * our parse_resources table, but not always...
1224 */
1225 static void
1226 parse_handle_v400_resources (sc_bool has_sound, sc_bool has_graphics)
1227 {
1228 sc_vartype_t vt_key, vt_value;
1229 const sc_char *file;
1230 sc_int length, offset;
1231
1232 /*
1233 * Retrieve the file and length for the sound just parsed. If there's a
1234 * file of non-zero length, rewrite its offset.
1235 */
1236 if (has_sound)
1237 {
1238 /* Retrieve the file and length information. */
1239 vt_key.string = "SoundFile";
1240 parse_push_key (vt_key, PROP_KEY_STRING);
1241 file = parse_get_string_property ();
1242 parse_pop_key ();
1243
1244 vt_key.string = "SoundLen";
1245 parse_push_key (vt_key, PROP_KEY_STRING);
1246 length = parse_get_integer_property ();
1247 parse_pop_key ();
1248
1249 /*
1250 * If defined and has a length, rewrite the offset, and also the length
1251 * in case changed.
1252 */
1253 if (!sc_strempty (file) && length != 0)
1254 {
1255 sc_int real_length;
1256
1257 offset = parse_get_v400_resource_offset (file, length, &real_length);
1258 vt_key.string = "SoundOffset";
1259 parse_push_key (vt_key, PROP_KEY_STRING);
1260
1261 vt_value.integer = offset;
1262 parse_put_property (vt_value, PROP_INTEGER);
1263
1264 parse_pop_key ();
1265
1266 /* Rewrite length if changed. */
1267 if (real_length != length)
1268 {
1269 vt_key.string = "SoundLen";
1270 parse_push_key (vt_key, PROP_KEY_STRING);
1271
1272 vt_value.integer = real_length;
1273 parse_put_property (vt_value, PROP_INTEGER);
1274
1275 parse_pop_key ();
1276 }
1277 }
1278 }
1279
1280 /* Now do the same thing for graphics. */
1281 if (has_graphics)
1282 {
1283 /* Retrieve the file and length information. */
1284 vt_key.string = "GraphicFile";
1285 parse_push_key (vt_key, PROP_KEY_STRING);
1286 file = parse_get_string_property ();
1287 parse_pop_key ();
1288
1289 vt_key.string = "GraphicLen";
1290 parse_push_key (vt_key, PROP_KEY_STRING);
1291 length = parse_get_integer_property ();
1292 parse_pop_key ();
1293
1294 /*
1295 * If defined and has a length, rewrite the offset, and also the length
1296 * in case changed.
1297 */
1298 if (!sc_strempty (file) && length != 0)
1299 {
1300 sc_int real_length;
1301
1302 offset = parse_get_v400_resource_offset (file, length, &real_length);
1303 vt_key.string = "GraphicOffset";
1304 parse_push_key (vt_key, PROP_KEY_STRING);
1305
1306 vt_value.integer = offset;
1307 parse_put_property (vt_value, PROP_INTEGER);
1308
1309 parse_pop_key ();
1310
1311 /* Rewrite length if changed. */
1312 if (real_length != length)
1313 {
1314 vt_key.string = "GraphicLen";
1315 parse_push_key (vt_key, PROP_KEY_STRING);
1316
1317 vt_value.integer = real_length;
1318 parse_put_property (vt_value, PROP_INTEGER);
1319
1320 parse_pop_key ();
1321 }
1322 }
1323 }
1324 }
1325
1326
1327 /*
1328 * parse_special()
1329 *
1330 * Handler for special items that can't be described accurately, and
1331 * therefore need careful treatment.
1332 */
1333 static void
1334 parse_special (const sc_char *special)
1335 {
1336 if (parse_trace)
1337 sc_trace ("Parse: entering special %s\n", special);
1338
1339 /* Special handling for version 4.0 resources. */
1340 if (strcmp (special, "{V400_RESOURCE}") == 0)
1341 {
1342 sc_vartype_t vt_key[2];
1343 sc_bool has_sound, has_graphics;
1344
1345 /* Get sound and graphics global flags. */
1346 vt_key[0].string = "Globals";
1347 vt_key[1].string = "Sound";
1348 has_sound = prop_get_boolean (parse_bundle, "B<-ss", vt_key);
1349
1350 vt_key[1].string = "Graphics";
1351 has_graphics = prop_get_boolean (parse_bundle, "B<-ss", vt_key);
1352
1353 /* Apply special handling to the resources. */
1354 parse_handle_v400_resources (has_sound, has_graphics);
1355 }
1356
1357 /* Parse a version 4.0 optional set of room exit information. */
1358 else if (strcmp (special, "{V400_ROOM_EXIT:#Dest_#Var1_#Var2_#Var3}") == 0)
1359 {
1360 sc_int flag;
1361
1362 /* Get next flag, and if true, pushback and parse. */
1363 flag = parse_get_taf_integer ();
1364 if (flag != 0)
1365 {
1366 parse_taf_pushback ();
1367 parse_descriptor ("#Dest #Var1 #Var2 #Var3");
1368 }
1369 }
1370
1371 /* Parse version 3.9 and version 3.8 optional room exit information. */
1372 else if (strcmp (special,
1373 "{V390_V380_ROOM_EXIT:#Dest_#Var1_#Var2_ZVar3}") == 0)
1374 {
1375 sc_int flag;
1376
1377 /* Get next flag, and if true, pushback and parse. */
1378 flag = parse_get_taf_integer ();
1379 if (flag != 0)
1380 {
1381 parse_taf_pushback ();
1382 parse_descriptor ("#Dest #Var1 #Var2 ZVar3");
1383 }
1384 }
1385
1386 /* Parse room lists, with optional extra room. */
1387 else if (strcmp (special, "{ROOM_LIST0}") == 0
1388 || strcmp (special, "{ROOM_LIST1}") == 0)
1389 {
1390 sc_vartype_t vt_key, vt_value;
1391 sc_int room_count, num_rooms, type, index_;
1392
1393 /* Retrieve the room list type. */
1394 vt_key.string = "Type";
1395 parse_push_key (vt_key, PROP_KEY_STRING);
1396 type = parse_get_integer_property ();
1397 parse_pop_key ();
1398
1399 /* Write remaining room list depending on the type. */
1400 switch (type)
1401 {
1402 case ROOMLIST_NO_ROOMS:
1403 case ROOMLIST_ALL_ROOMS:
1404 case ROOMLIST_NPC_PART:
1405 break;
1406
1407 case ROOMLIST_ONE_ROOM:
1408 /* Store this room as the single list entry. */
1409 parse_element ("#Room");
1410 break;
1411
1412 case ROOMLIST_SOME_ROOMS:
1413 /* Get count of rooms defined, add one if necessary. */
1414 vt_key.string = "Rooms";
1415 room_count = prop_get_child_count (parse_bundle, "I<-s", &vt_key);
1416
1417 if (strcmp (special, "{ROOM_LIST1}") == 0)
1418 num_rooms = room_count + 1;
1419 else
1420 num_rooms = room_count;
1421
1422 /* Store an array of rooms flags for each room. */
1423 vt_key.string = "Rooms";
1424 parse_push_key (vt_key, PROP_KEY_STRING);
1425 for (index_ = 0; index_ < num_rooms; index_++)
1426 {
1427 sc_bool this_room;
1428
1429 /* Get flag for this room. */
1430 this_room = parse_get_taf_boolean ();
1431
1432 /* Store flag directly. */
1433 vt_key.integer = index_;
1434 parse_push_key (vt_key, PROP_KEY_INTEGER);
1435 vt_value.boolean = this_room;
1436 parse_put_property (vt_value, PROP_BOOLEAN);
1437 parse_pop_key ();
1438 }
1439 parse_pop_key ();
1440 break;
1441
1442 default:
1443 sc_fatal ("parse_special: bad type, %ld\n", type);
1444 }
1445 }
1446
1447 /* Parse Parent number iff this object is an NPC part. */
1448 else if (strcmp (special, "{OBJECT:#Parent}") == 0)
1449 {
1450 sc_vartype_t vt_key;
1451 sc_int type;
1452
1453 /* Check object's Where room list Type for NPC part. */
1454 vt_key.string = "Where";
1455 parse_push_key (vt_key, PROP_KEY_STRING);
1456 vt_key.string = "Type";
1457 parse_push_key (vt_key, PROP_KEY_STRING);
1458 type = parse_get_integer_property ();
1459 parse_pop_key ();
1460 parse_pop_key ();
1461
1462 /* Get Parent if the object is part of an NPC. */
1463 if (type == ROOMLIST_NPC_PART)
1464 parse_element ("#Parent");
1465 }
1466
1467 /* Parse a list of rooms and times for a walk. */
1468 else if (strcmp (special, "{WALK:#Rooms_#Times}") == 0)
1469 {
1470 sc_vartype_t vt_key, vt_value;
1471 sc_int num_stops, index_;
1472
1473 /* Obtain the count of stops in this walk. */
1474 vt_key.string = "NumStops";
1475 parse_push_key (vt_key, PROP_KEY_STRING);
1476 num_stops = parse_get_integer_property ();
1477 parse_pop_key ();
1478
1479 /* Look for a room and time for each stop. */
1480 for (index_ = 0; index_ < num_stops; index_++)
1481 {
1482 sc_int room, time;
1483
1484 /* Parse and store Rooms[index_]. */
1485 vt_key.string = "Rooms";
1486 parse_push_key (vt_key, PROP_KEY_STRING);
1487 vt_key.integer = index_;
1488 parse_push_key (vt_key, PROP_KEY_INTEGER);
1489
1490 room = parse_get_taf_integer ();
1491
1492 vt_value.integer = room;
1493 parse_put_property (vt_value, PROP_INTEGER);
1494 parse_pop_key ();
1495 parse_pop_key ();
1496
1497 /* Parse and store Times[index_]. */
1498 vt_key.string = "Times";
1499 parse_push_key (vt_key, PROP_KEY_STRING);
1500 vt_key.integer = index_;
1501 parse_push_key (vt_key, PROP_KEY_INTEGER);
1502
1503 time = parse_get_taf_integer ();
1504
1505 vt_value.integer = time;
1506 parse_put_property (vt_value, PROP_INTEGER);
1507 parse_pop_key ();
1508 parse_pop_key ();
1509 }
1510 }
1511
1512 /* Parse a room group variable size boolean list. */
1513 else if (strcmp (special, "{ROOM_GROUP:[]BList}") == 0)
1514 {
1515 sc_vartype_t vt_key, vt_value;
1516 sc_int num_rooms, index_, l2index_;
1517 sc_bool in_group;
1518
1519 /* Get the count of rooms defined. */
1520 vt_key.string = "Rooms";
1521 num_rooms = prop_get_integer (parse_bundle, "I<-s", &vt_key);
1522
1523 /* Read a boolean for each room. */
1524 l2index_ = 0;
1525 for (index_ = 0; index_ < num_rooms; index_++)
1526 {
1527 in_group = parse_get_taf_boolean ();
1528
1529 /* Store raw flag as List[index_]. */
1530 vt_key.string = "List";
1531 parse_push_key (vt_key, PROP_KEY_STRING);
1532 vt_key.integer = index_;
1533 parse_push_key (vt_key, PROP_KEY_INTEGER);
1534 vt_value.boolean = in_group;
1535 parse_put_property (vt_value, PROP_BOOLEAN);
1536
1537 parse_pop_key ();
1538 parse_pop_key ();
1539
1540 /* Store in-group index'es as List2[0..n]. */
1541 if (in_group)
1542 {
1543 vt_key.string = "List2";
1544 parse_push_key (vt_key, PROP_KEY_STRING);
1545 vt_key.integer = l2index_;
1546 parse_push_key (vt_key, PROP_KEY_INTEGER);
1547 vt_value.integer = index_;
1548 parse_put_property (vt_value, PROP_INTEGER);
1549
1550 parse_pop_key ();
1551 parse_pop_key ();
1552
1553 l2index_++;
1554 }
1555 }
1556 }
1557
1558 /* Error if no special handler available. */
1559 else
1560 {
1561 sc_fatal ("parse_special: no handler for \"%s\"\n", special);
1562 }
1563
1564 if (parse_trace)
1565 sc_trace ("Parse: leaving special %s\n", special);
1566 }
1567
1568
1569 /*
1570 * parse_fixup_v390_v380_room_alt()
1571 *
1572 * Helper for parse_fixup_v390_v380_room_alts(). Handles creation of
1573 * version 4.0 room alts for version 3.9 and version 3.8 games.
1574 */
1575 static void
1576 parse_fixup_v390_v380_room_alt (const sc_char *m1, sc_int type,
1577 const sc_char *resource1,
1578 const sc_char *m2, sc_int var2,
1579 const sc_char *resource2,
1580 sc_int hide_objects,
1581 const sc_char *changed,
1582 sc_int var3, sc_int display_room)
1583 {
1584 sc_vartype_t vt_key, vt_value, vt_gkey[2];
1585 sc_bool has_sound, has_graphics;
1586 sc_int alt_count;
1587 const sc_char *soundfile1, *graphicfile1;
1588 const sc_char *soundfile2, *graphicfile2;
1589
1590 /*
1591 * Initialize resource files to empty, for cases where no resource is copied
1592 * over from the main room (NULL resource1/2).
1593 */
1594 soundfile1 = "";
1595 graphicfile1 = "";
1596 soundfile2 = "";
1597 graphicfile2 = "";
1598
1599 /* Get sound and graphics flags, always FALSE for version 3.8. */
1600 vt_gkey[0].string = "Globals";
1601 vt_gkey[1].string = "Sound";
1602 has_sound = prop_get_boolean (parse_bundle, "B<-ss", vt_gkey);
1603
1604 vt_gkey[1].string = "Graphics";
1605 has_graphics = prop_get_boolean (parse_bundle, "B<-ss", vt_gkey);
1606
1607 /* Get a count of alts so far defined for the room. */
1608 vt_key.string = "Alts";
1609 parse_push_key (vt_key, PROP_KEY_STRING);
1610 alt_count = parse_get_child_count ();
1611 parse_pop_key ();
1612
1613 /*
1614 * Lookup any resource details now, and save them. Because this is not
1615 * version 4.0, we can ignore lengths, and set them to zero when needed.
1616 */
1617 if (has_sound || has_graphics)
1618 {
1619 if (resource1)
1620 {
1621 vt_key.string = resource1;
1622 parse_push_key (vt_key, PROP_KEY_STRING);
1623 if (has_sound)
1624 {
1625 vt_key.string = "SoundFile";
1626 parse_push_key (vt_key, PROP_KEY_STRING);
1627 soundfile1 = parse_get_string_property ();
1628 parse_pop_key ();
1629 }
1630 if (has_graphics)
1631 {
1632 vt_key.string = "GraphicFile";
1633 parse_push_key (vt_key, PROP_KEY_STRING);
1634 graphicfile1 = parse_get_string_property ();
1635 parse_pop_key ();
1636 }
1637 parse_pop_key ();
1638 }
1639
1640 if (resource2)
1641 {
1642 vt_key.string = resource2;
1643 parse_push_key (vt_key, PROP_KEY_STRING);
1644 if (has_sound)
1645 {
1646 vt_key.string = "SoundFile";
1647 parse_push_key (vt_key, PROP_KEY_STRING);
1648 soundfile2 = parse_get_string_property ();
1649 parse_pop_key ();
1650 }
1651 if (has_graphics)
1652 {
1653 vt_key.string = "GraphicFile";
1654 parse_push_key (vt_key, PROP_KEY_STRING);
1655 graphicfile2 = parse_get_string_property ();
1656 parse_pop_key ();
1657 }
1658 parse_pop_key ();
1659 }
1660 }
1661
1662 /*
1663 * Create a room alt to match data passed in. Start with the Alts string
1664 * and the index to the alt being written. To correctly emulate the parse,
1665 * we also have to reverse the "Alts" and the index, as parse_put_property()
1666 * will swap them. Madness.
1667 */
1668 vt_key.integer = alt_count;
1669 parse_push_key (vt_key, PROP_KEY_INTEGER);
1670 vt_key.string = "Alts";
1671 parse_push_key (vt_key, PROP_KEY_STRING);
1672
1673 /* Write M1 and Type. */
1674 vt_key.string = "M1";
1675 parse_push_key (vt_key, PROP_KEY_STRING);
1676 vt_value.string = m1;
1677 parse_put_property (vt_value, PROP_STRING);
1678 parse_pop_key ();
1679 vt_key.string = "Type";
1680 parse_push_key (vt_key, PROP_KEY_STRING);
1681 vt_value.integer = type;
1682 parse_put_property (vt_value, PROP_INTEGER);
1683 parse_pop_key ();
1684
1685 /* If resources, add these as retrieved above. */
1686 if (has_sound || has_graphics)
1687 {
1688 vt_key.string = "Res1";
1689 parse_push_key (vt_key, PROP_KEY_STRING);
1690 if (has_sound)
1691 {
1692 vt_key.string = "SoundFile";
1693 parse_push_key (vt_key, PROP_KEY_STRING);
1694 vt_value.string = soundfile1;
1695 parse_put_property (vt_value, PROP_STRING);
1696 parse_pop_key ();
1697 vt_key.string = "SoundLen";
1698 parse_push_key (vt_key, PROP_KEY_STRING);
1699 vt_value.integer = 0;
1700 parse_put_property (vt_value, PROP_INTEGER);
1701 parse_pop_key ();
1702 }
1703 if (has_graphics)
1704 {
1705 vt_key.string = "GraphicFile";
1706 parse_push_key (vt_key, PROP_KEY_STRING);
1707 vt_value.string = graphicfile1;
1708 parse_put_property (vt_value, PROP_STRING);
1709 parse_pop_key ();
1710 vt_key.string = "GraphicLen";
1711 parse_push_key (vt_key, PROP_KEY_STRING);
1712 vt_value.integer = 0;
1713 parse_put_property (vt_value, PROP_INTEGER);
1714 parse_pop_key ();
1715 }
1716 parse_pop_key ();
1717 }
1718
1719 /* Write M2 and Var2. */
1720 vt_key.string = "M2";
1721 parse_push_key (vt_key, PROP_KEY_STRING);
1722 vt_value.string = m2;
1723 parse_put_property (vt_value, PROP_STRING);
1724 parse_pop_key ();
1725 vt_key.string = "Var2";
1726 parse_push_key (vt_key, PROP_KEY_STRING);
1727 vt_value.integer = var2;
1728 parse_put_property (vt_value, PROP_INTEGER);
1729 parse_pop_key ();
1730
1731 /* If resources, again add these as retrieved above. */
1732 if (has_sound || has_graphics)
1733 {
1734 vt_key.string = "Res2";
1735 parse_push_key (vt_key, PROP_KEY_STRING);
1736 if (has_sound)
1737 {
1738 vt_key.string = "SoundFile";
1739 parse_push_key (vt_key, PROP_KEY_STRING);
1740 vt_value.string = soundfile2;
1741 parse_put_property (vt_value, PROP_STRING);
1742 parse_pop_key ();
1743 vt_key.string = "SoundLen";
1744 parse_push_key (vt_key, PROP_KEY_STRING);
1745 vt_value.integer = 0;
1746 parse_put_property (vt_value, PROP_INTEGER);
1747 parse_pop_key ();
1748 }
1749 if (has_graphics)
1750 {
1751 vt_key.string = "GraphicFile";
1752 parse_push_key (vt_key, PROP_KEY_STRING);
1753 vt_value.string = graphicfile2;
1754 parse_put_property (vt_value, PROP_STRING);
1755 parse_pop_key ();
1756 vt_key.string = "GraphicLen";
1757 parse_push_key (vt_key, PROP_KEY_STRING);
1758 vt_value.integer = 0;
1759 parse_put_property (vt_value, PROP_INTEGER);
1760 parse_pop_key ();
1761 }
1762 parse_pop_key ();
1763 }
1764
1765 /* Finish off with the last four alt properties. */
1766 vt_key.string = "HideObjects";
1767 parse_push_key (vt_key, PROP_KEY_STRING);
1768 vt_value.integer = hide_objects;
1769 parse_put_property (vt_value, PROP_INTEGER);
1770 parse_pop_key ();
1771 vt_key.string = "Changed";
1772 parse_push_key (vt_key, PROP_KEY_STRING);
1773 vt_value.string = changed;
1774 parse_put_property (vt_value, PROP_STRING);
1775 parse_pop_key ();
1776 vt_key.string = "Var3";
1777 parse_push_key (vt_key, PROP_KEY_STRING);
1778 vt_value.integer = var3;
1779 parse_put_property (vt_value, PROP_INTEGER);
1780 parse_pop_key ();
1781 vt_key.string = "DisplayRoom";
1782 parse_push_key (vt_key, PROP_KEY_STRING);
1783 vt_value.integer = display_room;
1784 parse_put_property (vt_value, PROP_INTEGER);
1785 parse_pop_key ();
1786
1787 parse_pop_key ();
1788 parse_pop_key ();
1789 }
1790
1791
1792 /* Multiplier for combination AltDesc Type and HideObject values. */
1793 enum { V390_V380_ALT_TYPEHIDE_MULT = 10 };
1794
1795 /*
1796 * parse_fixup_v390_v380_room_alts()
1797 *
1798 * Common helper function for parse_fixup_v390() and parse_fixup_v380(),
1799 * converts version 3.9 and version 3.8 fixed room description alts into
1800 * an equivalent array of version 4.0 style room alts.
1801 */
1802 static void
1803 parse_fixup_v390_v380_room_alts (void)
1804 {
1805 sc_vartype_t vt_key;
1806 const sc_char *m1, *m2, *changed;
1807 sc_int type, var2, hide_objects, var3, display_room;
1808
1809 /* Room alt invariants. */
1810 m2 = ""; /* No else text */
1811 changed = ""; /* No changed room name */
1812
1813 /*
1814 * Create a room alt to override all others, controlled by an object
1815 * condition and with optional object hiding.
1816 */
1817 type = 2; /* Object condition */
1818 display_room = 0; /* Override all others */
1819
1820 vt_key.string = "Obj";
1821 parse_push_key (vt_key, PROP_KEY_STRING);
1822 var3 = parse_get_integer_property ();
1823 parse_pop_key ();
1824
1825 if (var3 > 0)
1826 {
1827 sc_int typehideobjects;
1828
1829 vt_key.string = "AltDesc";
1830 parse_push_key (vt_key, PROP_KEY_STRING);
1831 m1 = parse_get_string_property ();
1832 parse_pop_key ();
1833
1834 vt_key.string = "TypeHideObjects";
1835 parse_push_key (vt_key, PROP_KEY_STRING);
1836 typehideobjects = parse_get_integer_property ();
1837 parse_pop_key ();
1838
1839 var2 = typehideobjects / V390_V380_ALT_TYPEHIDE_MULT;
1840 hide_objects = typehideobjects % V390_V380_ALT_TYPEHIDE_MULT;
1841
1842 parse_fixup_v390_v380_room_alt (m1, type, "AltRes",
1843 m2, var2, NULL,
1844 hide_objects, changed, var3,
1845 display_room);
1846 }
1847
1848 /*
1849 * If a second task alternate description is defined, create a room alt to
1850 * add after the main description, one that stops printing once done.
1851 */
1852 type = 0; /* Task condition */
1853 display_room = 1; /* Print after main and stop */
1854
1855 vt_key.string = "Task2";
1856 parse_push_key (vt_key, PROP_KEY_STRING);
1857 var2 = parse_get_integer_property ();
1858 parse_pop_key ();
1859
1860 if (var2 > 0)
1861 {
1862 vt_key.string = "AddDesc2";
1863 parse_push_key (vt_key, PROP_KEY_STRING);
1864 m1 = parse_get_string_property ();
1865 parse_pop_key ();
1866
1867 var3 = 0;
1868 hide_objects = 0;
1869
1870 parse_fixup_v390_v380_room_alt (m1, type, "Task2Res",
1871 m2, var2, NULL,
1872 hide_objects, changed, var3,
1873 display_room);
1874 }
1875
1876 /* Do the same for any first task additional description. */
1877 type = 0; /* Task condition */
1878 display_room = 1; /* Print after main and stop */
1879
1880 vt_key.string = "Task1";
1881 parse_push_key (vt_key, PROP_KEY_STRING);
1882 var2 = parse_get_integer_property ();
1883 parse_pop_key ();
1884
1885 if (var2 > 0)
1886 {
1887 vt_key.string = "AddDesc1";
1888 parse_push_key (vt_key, PROP_KEY_STRING);
1889 m1 = parse_get_string_property ();
1890 parse_pop_key ();
1891
1892 var3 = 0;
1893 hide_objects = 0;
1894
1895 parse_fixup_v390_v380_room_alt (m1, type, "Task1Res",
1896 m2, var2, NULL,
1897 hide_objects, changed, var3,
1898 display_room);
1899 }
1900
1901 /*
1902 * If still printing at this point, we need a catch-all room alt that will
1903 * print. So create one with an always true condition.
1904 */
1905 type = 0; /* Task condition */
1906 display_room = 2; /* Lowest priority output */
1907
1908 vt_key.string = "LastDesc";
1909 parse_push_key (vt_key, PROP_KEY_STRING);
1910 m1 = parse_get_string_property ();
1911 parse_pop_key ();
1912
1913 if (!sc_strempty (m1))
1914 {
1915 var2 = 0; /* No task - always TRUE */
1916 var3 = 0;
1917 hide_objects = 0;
1918
1919 parse_fixup_v390_v380_room_alt (m1, type, "LastRes",
1920 m2, var2, NULL,
1921 hide_objects, changed, var3,
1922 display_room);
1923 }
1924 }
1925
1926
1927 /*
1928 * parse_fixup_v390()
1929 *
1930 * Handler for fixup special items to help with conversions from TAF version
1931 * 3.9 format into version 4.0.
1932 */
1933 static void
1934 parse_fixup_v390 (const sc_char *fixup)
1935 {
1936 if (parse_trace)
1937 sc_trace ("Parse: entering version 3.9 fixup %s\n", fixup);
1938
1939 /* Fixup a version 3.9 task action by incrementing Type > 4. */
1940 if (strcmp (fixup, "|V390_TASK_ACTION:Type>4?#Type++|") == 0)
1941 {
1942 sc_vartype_t vt_key, vt_value;
1943 sc_int type;
1944
1945 /* Retrieve Type, and if > 4, increment. */
1946 vt_key.string = "Type";
1947 parse_push_key (vt_key, PROP_KEY_STRING);
1948 type = parse_get_integer_property ();
1949
1950 if (type > 4)
1951 {
1952 vt_value.integer = type + 1;
1953 parse_put_property (vt_value, PROP_INTEGER);
1954 }
1955
1956 parse_pop_key ();
1957 }
1958
1959 /* Handle either Expr or Var5 for version 3.9 task actions. */
1960 else if (strcmp (fixup, "|V390_TASK_ACTION:$Expr_#Var5|") == 0)
1961 {
1962 sc_vartype_t vt_key;
1963 sc_int var2;
1964
1965 /* Either Expr or Var5, depending on Var2. */
1966 vt_key.string = "Var2";
1967 parse_push_key (vt_key, PROP_KEY_STRING);
1968 var2 = parse_get_integer_property ();
1969 parse_pop_key ();
1970
1971 if (var2 == 5)
1972 parse_descriptor ("$Expr ZVar5");
1973 else
1974 parse_descriptor ("EExpr #Var5");
1975 }
1976
1977 /*
1978 * Exchange openable values 5 and 6, and write -1 key for openable objects.
1979 */
1980 else if (strcmp (fixup, "|V390_OBJECT:_Openable_,Key|") == 0)
1981 {
1982 sc_vartype_t vt_key, vt_value;
1983 sc_int openable;
1984
1985 /* Retrieve Openable, and if 5 or 6, exchange. */
1986 vt_key.string = "Openable";
1987 parse_push_key (vt_key, PROP_KEY_STRING);
1988 openable = parse_get_integer_property ();
1989
1990 if (openable == 5 || openable == 6)
1991 {
1992 vt_value.integer = (openable == 5) ? 6 : 5;
1993 parse_put_property (vt_value, PROP_INTEGER);
1994 }
1995
1996 parse_pop_key ();
1997
1998 /* For openable objects, store a Key of -1. */
1999 if (openable == 5 || openable == 6)
2000 {
2001 vt_key.string = "Key";
2002 parse_push_key (vt_key, PROP_KEY_STRING);
2003 vt_value.integer = -1;
2004 parse_put_property (vt_value, PROP_INTEGER);
2005 parse_pop_key ();
2006 }
2007 }
2008
2009 /* Create a RestrMask that 'and's all the restrictions together. */
2010 else if (strcmp (fixup, "|V390_TASK:$RestrMask|") == 0)
2011 {
2012 sc_vartype_t vt_key, vt_value;
2013 sc_int restriction_count;
2014
2015 /* Get a count of restrictions. */
2016 vt_key.string = "Restrictions";
2017 parse_push_key (vt_key, PROP_KEY_STRING);
2018 restriction_count = parse_get_child_count ();
2019 parse_pop_key ();
2020
2021 /* Allocate and fill a new mask for these restrictions. */
2022 if (restriction_count > 0)
2023 {
2024 sc_char *restrmask;
2025 sc_int index_;
2026
2027 restrmask = sc_malloc (2 * restriction_count);
2028 strcpy (restrmask, "#");
2029 for (index_ = 1; index_ < restriction_count; index_++)
2030 strcat (restrmask, "A#");
2031
2032 vt_key.string = "RestrMask";
2033 parse_push_key (vt_key, PROP_KEY_STRING);
2034 vt_value.string = restrmask;
2035 parse_put_property (vt_value, PROP_STRING);
2036 parse_pop_key ();
2037
2038 prop_adopt (parse_bundle, restrmask);
2039 }
2040 }
2041
2042 /*
2043 * Increment var1 for variable restrictions to compensate for there being no
2044 * referenced text comparison (no string variables).
2045 */
2046 else if (strcmp (fixup, "|V390_TASK_RESTR:Var1>0?#Var1++|") == 0)
2047 {
2048 sc_vartype_t vt_key, vt_value;
2049 sc_int var1;
2050
2051 /* Retrieve Var1, and if greater than zero, increment. */
2052 vt_key.string = "Var1";
2053 parse_push_key (vt_key, PROP_KEY_STRING);
2054 var1 = parse_get_integer_property ();
2055
2056 if (var1 > 0)
2057 {
2058 vt_value.integer = var1 + 1;
2059 parse_put_property (vt_value, PROP_INTEGER);
2060 }
2061
2062 parse_pop_key ();
2063 }
2064
2065 /* Convert version 3.9 fixed alts into a version 4.0 array. */
2066 else if (strcmp (fixup, "|V390_ROOM:_Alts_|") == 0)
2067 {
2068 parse_fixup_v390_v380_room_alts ();
2069 }
2070
2071 /* Error if no fixup special handler available. */
2072 else
2073 {
2074 sc_fatal ("parse_fixup_v390: no handler for \"%s\"\n", fixup);
2075 }
2076
2077 if (parse_trace)
2078 sc_trace ("Parse: leaving version 3.9 fixup %s\n", fixup);
2079 }
2080
2081
2082 /*
2083 * Object surface and container masks for version 3.8 object fixup, container
2084 * capacity conversion factor and default object sizing, and the count of
2085 * task movements in a version 3.8 task.
2086 */
2087 enum { V380_OBJ_IS_SURFACE = 2, V380_OBJ_IS_CONTAINER = 1 };
2088 enum { V380_OBJ_CAPACITY_MULT = 10, V380_OBJ_DEFAULT_SIZE = 2 };
2089 enum { V380_TASK_MOVEMENTS = 6 };
2090
2091 /*
2092 * parse_fixup_v380_action()
2093 *
2094 * Helper for parse_fixup_v380(), adds a task action.
2095 */
2096 static void
2097 parse_fixup_v380_action (sc_int type, sc_int var_count,
2098 sc_int var1, sc_int var2, sc_int var3)
2099 {
2100 sc_vartype_t vt_key, vt_value;
2101 sc_int action_count;
2102
2103 /* Get a count of actions so far defined for the task. */
2104 vt_key.string = "Actions";
2105 parse_push_key (vt_key, PROP_KEY_STRING);
2106 action_count = parse_get_child_count ();
2107 parse_pop_key ();
2108
2109 /* Write actions key, reversed to emulate parse actions. */
2110 vt_key.integer = action_count;
2111 parse_push_key (vt_key, PROP_KEY_INTEGER);
2112 vt_key.string = "Actions";
2113 parse_push_key (vt_key, PROP_KEY_STRING);
2114
2115 /* Write new action according to the given arguments. */
2116 vt_key.string = "Type";
2117 parse_push_key (vt_key, PROP_KEY_STRING);
2118 vt_value.integer = type;
2119 parse_put_property (vt_value, PROP_INTEGER);
2120 parse_pop_key ();
2121
2122 vt_key.string = "Var1";
2123 parse_push_key (vt_key, PROP_KEY_STRING);
2124 vt_value.integer = var1;
2125 parse_put_property (vt_value, PROP_INTEGER);
2126 parse_pop_key ();
2127
2128 if (var_count > 1)
2129 {
2130 vt_key.string = "Var2";
2131 parse_push_key (vt_key, PROP_KEY_STRING);
2132 vt_value.integer = var2;
2133 parse_put_property (vt_value, PROP_INTEGER);
2134 parse_pop_key ();
2135 }
2136
2137 if (var_count > 2)
2138 {
2139 vt_key.string = "Var3";
2140 parse_push_key (vt_key, PROP_KEY_STRING);
2141 vt_value.integer = var3;
2142 parse_put_property (vt_value, PROP_INTEGER);
2143 parse_pop_key ();
2144 }
2145
2146 parse_pop_key ();
2147 parse_pop_key ();
2148 }
2149
2150
2151 /*
2152 * parse_fixup_v380_movement()
2153 *
2154 * Helper for parse_fixup_v380(), converts a task movement into an action.
2155 */
2156 static void
2157 parse_fixup_v380_movement (sc_int mvar1, sc_int mvar2, sc_int mvar3)
2158 {
2159 sc_int var1;
2160
2161 /* If nothing was selected to move, ignore the call. */
2162 if (mvar1 == 0)
2163 return;
2164
2165 /*
2166 * Accept only player moves into rooms. Other combinations, such as move
2167 * player to worn by player, are unlikely. And move player to same room as
2168 * player isn't useful.
2169 */
2170 if (mvar1 == 1)
2171 {
2172 if (mvar3 == 0 && mvar2 >= 2)
2173 parse_fixup_v380_action (1, 3, 0, 0, mvar2 - 2);
2174 return;
2175 }
2176
2177 /*
2178 * Convert movement var1 into action var1. Var1 is the dynamic object + 3,
2179 * or 2 for referenced object, or 0 for all held.
2180 */
2181 switch (mvar1)
2182 {
2183 case 2:
2184 var1 = 2;
2185 break; /* Referenced obj */
2186 case 3:
2187 var1 = 0;
2188 break; /* All held */
2189 default:
2190 var1 = mvar1 - 1;
2191 break; /* Dynamic obj */
2192 }
2193
2194 /* Dissect the rest of the movement. */
2195 switch (mvar3)
2196 {
2197 case 0: /* To room */
2198 /*
2199 * Convert movement var2 into action var2 and var3. Var2 is 0 for move
2200 * to room, 6 for move to player room. Var3 is 0 for hidden, otherwise
2201 * the room number plus one.
2202 */
2203 if (mvar2 == 0) /* Hidden */
2204 parse_fixup_v380_action (0, 3, var1, 0, 0);
2205 else if (mvar2 == 1) /* Player room */
2206 parse_fixup_v380_action (0, 3, var1, 6, 0);
2207 else /* Specified room */
2208 parse_fixup_v380_action (0, 3, var1, 0, mvar2 - 1);
2209 break;
2210
2211 case 1: /* To inside */
2212 case 2: /* To onto */
2213 /*
2214 * Convert movement var2 and var3 into action var3 and var2, a simple
2215 * conversion, but check that var2 is not 'not selected' first.
2216 */
2217 if (mvar2 > 0)
2218 parse_fixup_v380_action (0, 3, var1, mvar3 + 1, mvar2 - 1);
2219 break;
2220
2221 case 3: /* To held by */
2222 case 4: /* To worn by */
2223 /*
2224 * Convert movement var2 and var3 into action var3 and var2, in this
2225 * case a simple conversion, since version 4.0 task actions are close
2226 * here.
2227 */
2228 parse_fixup_v380_action (0, 3, var1, mvar3 + 1, mvar2);
2229 break;
2230
2231 default:
2232 sc_fatal ("parse_fixup_v380_movement: invalid mvar3, %ld\n", mvar3);
2233 }
2234 }
2235
2236
2237 /*
2238 * parse_fixup_v380_restr()
2239 *
2240 * Helper for parse_fixup_v380(), adds a task restriction.
2241 */
2242 static void
2243 parse_fixup_v380_restr (sc_int type, sc_int var_count,
2244 sc_int var1, sc_int var2, sc_int var3,
2245 const sc_char *failmessage)
2246 {
2247 sc_vartype_t vt_key, vt_value;
2248 sc_int restriction_count;
2249
2250 /* Get a count of restrictions so far defined for the task. */
2251 vt_key.string = "Restrictions";
2252 parse_push_key (vt_key, PROP_KEY_STRING);
2253 restriction_count = parse_get_child_count ();
2254 parse_pop_key ();
2255
2256 /* Write restrictions key, reversed to emulate parse actions. */
2257 vt_key.integer = restriction_count;
2258 parse_push_key (vt_key, PROP_KEY_INTEGER);
2259 vt_key.string = "Restrictions";
2260 parse_push_key (vt_key, PROP_KEY_STRING);
2261
2262 /* Write new restriction according to the given arguments. */
2263 vt_key.string = "Type";
2264 parse_push_key (vt_key, PROP_KEY_STRING);
2265 vt_value.integer = type;
2266 parse_put_property (vt_value, PROP_INTEGER);
2267 parse_pop_key ();
2268
2269 vt_key.string = "Var1";
2270 parse_push_key (vt_key, PROP_KEY_STRING);
2271 vt_value.integer = var1;
2272 parse_put_property (vt_value, PROP_INTEGER);
2273 parse_pop_key ();
2274
2275 if (var_count > 1)
2276 {
2277 vt_key.string = "Var2";
2278 parse_push_key (vt_key, PROP_KEY_STRING);
2279 vt_value.integer = var2;
2280 parse_put_property (vt_value, PROP_INTEGER);
2281 parse_pop_key ();
2282 }
2283
2284 if (var_count > 2)
2285 {
2286 vt_key.string = "Var3";
2287 parse_push_key (vt_key, PROP_KEY_STRING);
2288 vt_value.integer = var3;
2289 parse_put_property (vt_value, PROP_INTEGER);
2290 parse_pop_key ();
2291 }
2292
2293 vt_key.string = "FailMessage";
2294 parse_push_key (vt_key, PROP_KEY_STRING);
2295 vt_value.string = failmessage;
2296 parse_put_property (vt_value, PROP_STRING);
2297 parse_pop_key ();
2298
2299 parse_pop_key ();
2300 parse_pop_key ();
2301 }
2302
2303
2304 /*
2305 * parse_fixup_v380_obj_restr()
2306 * parse_fixup_v380_task_restr()
2307 * parse_fixup_v380_wear_restr()
2308 * parse_fixup_v380_npc_restr()
2309 * parse_fixup_v380_objroom_restr()
2310 * parse_fixup_v380_objstate_restr()
2311 *
2312 * Helper handlers for parse_fixup_v380(); create task restrictions.
2313 */
2314 static void
2315 parse_fixup_v380_obj_restr (sc_bool holding,
2316 sc_int holdobj, const sc_char *failmessage)
2317 {
2318 /* Ignore if no object selected. */
2319 if (holdobj > 0)
2320 {
2321 sc_int var1, var2;
2322
2323 /*
2324 * Create version 4.0 task restriction to check for either the
2325 * referenced object or a dynamic object being either held or in the
2326 * same room (visible to player).
2327 */
2328 var1 = (holdobj == 1) ? 2 : holdobj + 1;
2329 var2 = holding ? 1 : 3;
2330 parse_fixup_v380_restr (0, 3, var1, var2, 0, failmessage);
2331 }
2332 }
2333
2334 static void
2335 parse_fixup_v380_task_restr (sc_bool tasknotdone, sc_int task,
2336 const sc_char *failmessage)
2337 {
2338 /* Ignore if no task selected. */
2339 if (task > 0)
2340 {
2341 sc_int var2;
2342
2343 /* Create version 4.0 restriction to check task state. */
2344 var2 = tasknotdone ? 1 : 0;
2345 parse_fixup_v380_restr (2, 2, task, var2, 0, failmessage);
2346 }
2347 }
2348
2349 static void
2350 parse_fixup_v380_wear_restr (sc_int wearobj, const sc_char *failmessage)
2351 {
2352 /* Ignore if no object selected. */
2353 if (wearobj > 0)
2354 {
2355 sc_vartype_t vt_key[3];
2356 sc_int object_count, object, dynamic, obj_index;
2357
2358 /*
2359 * Create version 4.0 restrictions for something or nothing worn by
2360 * player.
2361 */
2362 if (wearobj == 1)
2363 {
2364 parse_fixup_v380_restr (0, 3, 1, 2, 0, failmessage);
2365 return;
2366 }
2367 else if (wearobj == 2)
2368 {
2369 parse_fixup_v380_restr (0, 3, 0, 2, 0, failmessage);
2370 return;
2371 }
2372
2373 /* Get the count of objects defined. */
2374 vt_key[0].string = "Objects";
2375 object_count = prop_get_child_count (parse_bundle, "I<-s", vt_key);
2376
2377 /* Convert wearobj from worn index to object index. */
2378 wearobj -= 2;
2379 for (object = 0; object < object_count && wearobj > 0; object++)
2380 {
2381 sc_bool bstatic, wearable;
2382
2383 vt_key[1].integer = object;
2384 vt_key[2].string = "Static";
2385 bstatic = prop_get_boolean (parse_bundle, "B<-sis", vt_key);
2386 if (!bstatic)
2387 {
2388 vt_key[2].string = "Wearable";
2389 wearable = prop_get_boolean (parse_bundle, "B<-sis", vt_key);
2390 if (wearable)
2391 wearobj--;
2392 }
2393 }
2394 obj_index = object - 1;
2395
2396 /* Now convert wearobj from object index to dynamic index. */
2397 dynamic = 0;
2398 for (object = 0; object <= obj_index; object++)
2399 {
2400 sc_bool bstatic;
2401
2402 vt_key[1].integer = object;
2403 vt_key[2].string = "Static";
2404 bstatic = prop_get_boolean (parse_bundle, "B<-sis", vt_key);
2405 if (!bstatic)
2406 dynamic++;
2407 }
2408 dynamic--;
2409
2410 /* Create version 4.0 restriction for object worn by player. */
2411 parse_fixup_v380_restr (0, 3, dynamic + 3, 2, 0, failmessage);
2412 }
2413 }
2414
2415 static void
2416 parse_fixup_v380_npc_restr (sc_bool notinsameroom, sc_int npc,
2417 const sc_char *failmessage)
2418 {
2419 /* Ignore if no NPC selected. */
2420 if (npc > 0)
2421 {
2422 sc_int var2;
2423
2424 if (npc == 1)
2425 {
2426 /* Create restriction to look for alone, or not. */
2427 var2 = notinsameroom ? 3 : 2;
2428 parse_fixup_v380_restr (3, 3, 0, var2, 0, failmessage);
2429 return;
2430 }
2431
2432 /* Create restriction to look for company. */
2433 var2 = notinsameroom ? 1 : 0;
2434 parse_fixup_v380_restr (3, 3, 0, var2, npc, failmessage);
2435 }
2436 }
2437
2438 static void
2439 parse_fixup_v380_objroom_restr (sc_int obj, sc_int objroom,
2440 const sc_char *failmessage)
2441 {
2442 /* Ignore if no object selected. */
2443 if (obj > 0)
2444 {
2445 /* Create version 4.0 restriction to check object in room. */
2446 parse_fixup_v380_restr (0, 3, obj + 1, 0, objroom, failmessage);
2447 }
2448 }
2449
2450 static void
2451 parse_fixup_v380_objstate_restr (sc_int obj, sc_int ivar1, sc_int ivar2,
2452 const sc_char *failmessage)
2453 {
2454 sc_vartype_t vt_key[3];
2455 sc_int object, dynamic, var2, var3;
2456
2457 /* Initialize variables to avoid gcc warnings. */
2458 var2 = -1;
2459 var3 = -1;
2460
2461 /* Ignore restrictions with no "type". */
2462 if (ivar1 == 0)
2463 return;
2464
2465 /* Look for opened/closed restrictions, convert and return. */
2466 if (ivar1 == 3 || ivar1 == 4)
2467 {
2468 sc_int stateful;
2469
2470 /* Convert obj from object to openable (stateful) index. */
2471 stateful = 0;
2472 for (object = 0; object <= obj - 1; object++)
2473 {
2474 sc_int openable;
2475
2476 vt_key[0].string = "Objects";
2477 vt_key[1].integer = object;
2478 vt_key[2].string = "Openable";
2479 openable = prop_get_integer (parse_bundle, "I<-sis", vt_key);
2480 if (openable > 0)
2481 stateful++;
2482 }
2483 stateful--;
2484
2485 /*
2486 * Create a version 4.0 restriction that checks that an object's state
2487 * is open (var2 = 0) or closed (var2 = 1).
2488 */
2489 var2 = (ivar1 == 3) ? 0 : 1;
2490 parse_fixup_v380_restr (1, 2, stateful + 1, var2, 0, failmessage);
2491 return;
2492 }
2493
2494 /* Convert obj from object to dynamic index. */
2495 dynamic = 0;
2496 for (object = 0; object <= obj - 1; object++)
2497 {
2498 sc_bool bstatic;
2499
2500 vt_key[0].string = "Objects";
2501 vt_key[1].integer = object;
2502 vt_key[2].string = "Static";
2503 bstatic = prop_get_boolean (parse_bundle, "B<-sis", vt_key);
2504 if (!bstatic)
2505 dynamic++;
2506 }
2507 dynamic--;
2508
2509 /* Create version 4.0 object location restrictions for the rest. */
2510 switch (ivar1)
2511 {
2512 case 1:
2513 var2 = 4;
2514 var3 = ivar2;
2515 break; /* Inside */
2516 case 2:
2517 var2 = 5;
2518 var3 = ivar2;
2519 break; /* On */
2520 case 5:
2521 var2 = 1;
2522 var3 = ivar2 + 1;
2523 break; /* Held by */
2524 case 6:
2525 var2 = 2;
2526 var3 = ivar2 + 1;
2527 break; /* Worn by */
2528 default:
2529 sc_fatal ("parse_fixup_v380_objstate_restr: invalid ivar1, %ld\n", ivar1);
2530 }
2531 parse_fixup_v380_restr (0, 3, dynamic + 3, var2, var3, failmessage);
2532 }
2533
2534
2535 /*
2536 * parse_fixup_v380()
2537 *
2538 * Handler for fixup special items to help with conversions from TAF version
2539 * 3.8 format into version 4.0.
2540 */
2541 static void
2542 parse_fixup_v380 (const sc_char *fixup)
2543 {
2544 if (parse_trace)
2545 sc_trace ("Parse: entering version 3.8 fixup %s\n", fixup);
2546
2547 /* Convert container capacity attributes to version 4.0 values. */
2548 if (strcmp (fixup, "|V380_OBJECT:#Capacity*10+2|") == 0)
2549 {
2550 sc_vartype_t vt_key, vt_value;
2551 sc_int surfacecontainer;
2552
2553 /* Get the object surface and container attributes. */
2554 vt_key.string = "SurfaceContainer";
2555 parse_push_key (vt_key, PROP_KEY_STRING);
2556 surfacecontainer = parse_get_integer_property ();
2557 parse_pop_key ();
2558
2559 /* Convert capacity from version 3.8 format to version 4.0. */
2560 if (surfacecontainer == V380_OBJ_IS_CONTAINER)
2561 {
2562 sc_int capacity;
2563
2564 vt_key.string = "Capacity";
2565 parse_push_key (vt_key, PROP_KEY_STRING);
2566 capacity = parse_get_integer_property ();
2567
2568 capacity = capacity * V380_OBJ_CAPACITY_MULT + V380_OBJ_DEFAULT_SIZE;
2569
2570 vt_value.integer = capacity;
2571 parse_put_property (vt_value, PROP_INTEGER);
2572 parse_pop_key ();
2573 }
2574 }
2575
2576 /*
2577 * Exchange openable values 5 and 6, watch for a possible 1 from a 3.8 game
2578 * (interpret as 0), and write -1 key for openable objects.
2579 */
2580 else if (strcmp (fixup, "|V380_OBJECT:_Openable_,Key|") == 0)
2581 {
2582 sc_vartype_t vt_key, vt_value;
2583 sc_int openable;
2584
2585 /* Retrieve Openable, and if 5 or 6, exchange. */
2586 vt_key.string = "Openable";
2587 parse_push_key (vt_key, PROP_KEY_STRING);
2588 openable = parse_get_integer_property ();
2589
2590 if (openable == 5 || openable == 6)
2591 {
2592 vt_value.integer = (openable == 5) ? 6 : 5;
2593 parse_put_property (vt_value, PROP_INTEGER);
2594 }
2595
2596 /* If the odd value of 1, rewrite as zero. */
2597 else if (openable == 1)
2598 {
2599 vt_value.integer = 0;
2600 parse_put_property (vt_value, PROP_INTEGER);
2601 }
2602
2603 parse_pop_key ();
2604
2605 /* For openable objects, store a Key of -1. */
2606 if (openable == 5 || openable == 6)
2607 {
2608 vt_key.string = "Key";
2609 parse_push_key (vt_key, PROP_KEY_STRING);
2610 vt_value.integer = -1;
2611 parse_put_property (vt_value, PROP_INTEGER);
2612 parse_pop_key ();
2613 }
2614 }
2615
2616 /* Create version 4.0 task actions from a version 3.8 task. */
2617 else if (strcmp (fixup, "|V380_TASK:_Actions_|") == 0)
2618 {
2619 sc_vartype_t vt_key;
2620 sc_int score;
2621 sc_bool killsplayer, wingame;
2622 sc_int movement;
2623
2624 /* Retrieve the score change for the task. */
2625 vt_key.string = "Score";
2626 parse_push_key (vt_key, PROP_KEY_STRING);
2627 score = parse_get_integer_property ();
2628 parse_pop_key ();
2629
2630 /* Create any appropriate score change action. */
2631 if (score != 0)
2632 parse_fixup_v380_action (4, 1, score, 0, 0);
2633
2634 /* Get player death and game winning flags. */
2635 vt_key.string = "KillsPlayer";
2636 parse_push_key (vt_key, PROP_KEY_STRING);
2637 killsplayer = parse_get_boolean_property ();
2638 parse_pop_key ();
2639 vt_key.string = "WinGame";
2640 parse_push_key (vt_key, PROP_KEY_STRING);
2641 wingame = parse_get_boolean_property ();
2642 parse_pop_key ();
2643
2644 /* Create any appropriate game ending actions. */
2645 if (killsplayer)
2646 parse_fixup_v380_action (6, 1, 2, 0, 0);
2647 if (wingame)
2648 parse_fixup_v380_action (6, 1, 0, 0, 0);
2649
2650 /* Handle each defined movement for the task. */
2651 for (movement = 0; movement < V380_TASK_MOVEMENTS; movement++)
2652 {
2653 sc_int mvar1, mvar2, mvar3;
2654
2655 vt_key.integer = movement;
2656 parse_push_key (vt_key, PROP_KEY_INTEGER);
2657 vt_key.string = "Movements";
2658 parse_push_key (vt_key, PROP_KEY_STRING);
2659
2660 /* Retrieve the movement parameters. */
2661 vt_key.string = "Var1";
2662 parse_push_key (vt_key, PROP_KEY_STRING);
2663 mvar1 = parse_get_integer_property ();
2664 parse_pop_key ();
2665 vt_key.string = "Var2";
2666 parse_push_key (vt_key, PROP_KEY_STRING);
2667 mvar2 = parse_get_integer_property ();
2668 parse_pop_key ();
2669 vt_key.string = "Var3";
2670 parse_push_key (vt_key, PROP_KEY_STRING);
2671 mvar3 = parse_get_integer_property ();
2672 parse_pop_key ();
2673
2674 parse_pop_key ();
2675 parse_pop_key ();
2676
2677 /* Create the corresponding task action. */
2678 parse_fixup_v380_movement (mvar1, mvar2, mvar3);
2679 }
2680 }
2681
2682 /* Create version 4.0 task restrictions from a version 3.8 task. */
2683 else if (strcmp (fixup, "|V380_TASK:_Restrictions_|") == 0)
2684 {
2685 sc_vartype_t vt_key, vt_value;
2686 sc_bool holding, tasknotdone, notinsameroom;
2687 sc_int holdobj1, holdobj2, holdobj3, task;
2688 sc_int wearobj1, wearobj2, npc, obj1, obj1room, obj2;
2689 const sc_char *holdmsg, *taskmsg, *wearmsg, *companymsg;
2690 const sc_char *obj1msg;
2691 sc_int restriction_count;
2692
2693 /* Create restrictions for objects not held or absent. */
2694 vt_key.string = "HoldingSameRoom";
2695 parse_push_key (vt_key, PROP_KEY_STRING);
2696 holding = parse_get_boolean_property ();
2697 parse_pop_key ();
2698
2699 vt_key.string = "HoldObj1";
2700 parse_push_key (vt_key, PROP_KEY_STRING);
2701 holdobj1 = parse_get_integer_property ();
2702 parse_pop_key ();
2703
2704 vt_key.string = "HoldObj2";
2705 parse_push_key (vt_key, PROP_KEY_STRING);
2706 holdobj2 = parse_get_integer_property ();
2707 parse_pop_key ();
2708
2709 vt_key.string = "HoldObj3";
2710 parse_push_key (vt_key, PROP_KEY_STRING);
2711 holdobj3 = parse_get_integer_property ();
2712 parse_pop_key ();
2713
2714 vt_key.string = "HoldMsg";
2715 parse_push_key (vt_key, PROP_KEY_STRING);
2716 holdmsg = parse_get_string_property ();
2717 parse_pop_key ();
2718
2719 parse_fixup_v380_obj_restr (holding, holdobj1, holdmsg);
2720 parse_fixup_v380_obj_restr (holding, holdobj2, holdmsg);
2721 parse_fixup_v380_obj_restr (holding, holdobj3, holdmsg);
2722
2723 /* Create any task state restriction. */
2724 vt_key.string = "TaskNotDone";
2725 parse_push_key (vt_key, PROP_KEY_STRING);
2726 tasknotdone = parse_get_boolean_property ();
2727 parse_pop_key ();
2728
2729 vt_key.string = "Task";
2730 parse_push_key (vt_key, PROP_KEY_STRING);
2731 task = parse_get_integer_property ();
2732 parse_pop_key ();
2733
2734 vt_key.string = "TaskMsg";
2735 parse_push_key (vt_key, PROP_KEY_STRING);
2736 taskmsg = parse_get_string_property ();
2737 parse_pop_key ();
2738
2739 parse_fixup_v380_task_restr (tasknotdone, task, taskmsg);
2740
2741 /* Create any object not worn restrictions. */
2742 vt_key.string = "WearObj1";
2743 parse_push_key (vt_key, PROP_KEY_STRING);
2744 wearobj1 = parse_get_integer_property ();
2745 parse_pop_key ();
2746
2747 vt_key.string = "WearObj2";
2748 parse_push_key (vt_key, PROP_KEY_STRING);
2749 wearobj2 = parse_get_integer_property ();
2750 parse_pop_key ();
2751
2752 vt_key.string = "WearMsg";
2753 parse_push_key (vt_key, PROP_KEY_STRING);
2754 wearmsg = parse_get_string_property ();
2755 parse_pop_key ();
2756
2757 parse_fixup_v380_wear_restr (wearobj1, wearmsg);
2758 parse_fixup_v380_wear_restr (wearobj2, wearmsg);
2759
2760 /* Check for presence/absence of NPCs restriction. */
2761 vt_key.string = "NotInSameRoom";
2762 parse_push_key (vt_key, PROP_KEY_STRING);
2763 notinsameroom = parse_get_boolean_property ();
2764 parse_pop_key ();
2765
2766 vt_key.string = "NPC";
2767 parse_push_key (vt_key, PROP_KEY_STRING);
2768 npc = parse_get_integer_property ();
2769 parse_pop_key ();
2770
2771 vt_key.string = "CompanyMsg";
2772 parse_push_key (vt_key, PROP_KEY_STRING);
2773 companymsg = parse_get_string_property ();
2774 parse_pop_key ();
2775
2776 parse_fixup_v380_npc_restr (notinsameroom, npc, companymsg);
2777
2778 /* Create any object location restriction. */
2779 vt_key.string = "Obj1";
2780 parse_push_key (vt_key, PROP_KEY_STRING);
2781 obj1 = parse_get_integer_property ();
2782 parse_pop_key ();
2783
2784 vt_key.string = "Obj1Room";
2785 parse_push_key (vt_key, PROP_KEY_STRING);
2786 obj1room = parse_get_integer_property ();
2787 parse_pop_key ();
2788
2789 vt_key.string = "Obj1Msg";
2790 parse_push_key (vt_key, PROP_KEY_STRING);
2791 obj1msg = parse_get_string_property ();
2792 parse_pop_key ();
2793
2794 parse_fixup_v380_objroom_restr (obj1, obj1room, obj1msg);
2795
2796 /* And finally, any object state restriction. */
2797 vt_key.string = "Obj2";
2798 parse_push_key (vt_key, PROP_KEY_STRING);
2799 obj2 = parse_get_integer_property ();
2800 parse_pop_key ();
2801
2802 if (obj2 > 0)
2803 {
2804 sc_int var1, var2;
2805 const sc_char *obj2msg;
2806
2807 vt_key.string = "Obj2Var1";
2808 parse_push_key (vt_key, PROP_KEY_STRING);
2809 var1 = parse_get_integer_property ();
2810 parse_pop_key ();
2811
2812 vt_key.string = "Obj2Var2";
2813 parse_push_key (vt_key, PROP_KEY_STRING);
2814 var2 = parse_get_integer_property ();
2815 parse_pop_key ();
2816
2817 vt_key.string = "Obj2Msg";
2818 parse_push_key (vt_key, PROP_KEY_STRING);
2819 obj2msg = parse_get_string_property ();
2820 parse_pop_key ();
2821
2822 parse_fixup_v380_objstate_restr (obj2, var1, var2, obj2msg);
2823 }
2824
2825 /* Get a count of restrictions created. */
2826 vt_key.string = "Restrictions";
2827 parse_push_key (vt_key, PROP_KEY_STRING);
2828 restriction_count = parse_get_child_count ();
2829 parse_pop_key ();
2830
2831 /* Allocate and fill a new mask for these restrictions. */
2832 if (restriction_count > 0)
2833 {
2834 sc_char *restrmask;
2835 sc_int index_;
2836
2837 restrmask = sc_malloc (2 * restriction_count);
2838 strcpy (restrmask, "#");
2839 for (index_ = 1; index_ < restriction_count; index_++)
2840 strcat (restrmask, "A#");
2841
2842 vt_key.string = "RestrMask";
2843 parse_push_key (vt_key, PROP_KEY_STRING);
2844 vt_value.string = restrmask;
2845 parse_put_property (vt_value, PROP_STRING);
2846 parse_pop_key ();
2847
2848 prop_adopt (parse_bundle, restrmask);
2849 }
2850 }
2851
2852 /*
2853 * Adjust dynamic object initial positions and parents (where contained
2854 * or on surfaces) into version 4.0 range.
2855 */
2856 else if (strcmp (fixup, "|V380_OBJECT:_InitialPositions_|") == 0)
2857 {
2858 sc_vartype_t vt_key[3];
2859 sc_int object_count, object, *object_type;
2860
2861 /* Get a count of objects. */
2862 vt_key[0].string = "Objects";
2863 object_count = prop_get_child_count (parse_bundle, "I<-s", vt_key);
2864
2865 /* Build an array of object container/surface types. */
2866 object_type = sc_malloc (object_count * sizeof (*object_type));
2867 for (object = 0; object < object_count; object++)
2868 {
2869 vt_key[1].integer = object;
2870 vt_key[2].string = "SurfaceContainer";
2871 object_type[object] = prop_get_integer (parse_bundle,
2872 "I<-sis", vt_key);
2873 }
2874
2875 /* Adjust each object's initial position if necessary. */
2876 for (object = 0; object < object_count; object++)
2877 {
2878 sc_vartype_t vt_value;
2879 sc_bool is_static;
2880 sc_int initialposition;
2881
2882 /* Ignore static objects; we only want dynamic ones. */
2883 vt_key[1].integer = object;
2884 vt_key[2].string = "Static";
2885 is_static = prop_get_boolean (parse_bundle, "B<-sis", vt_key);
2886 if (is_static)
2887 continue;
2888
2889 /* If initial position is above on/in, increment. */
2890 vt_key[1].integer = object;
2891 vt_key[2].string = "InitialPosition";
2892 initialposition = prop_get_integer (parse_bundle, "I<-sis", vt_key);
2893 if (initialposition > 2)
2894 {
2895 vt_value.integer = initialposition + 1;
2896 prop_put (parse_bundle, "I->sis", vt_value, vt_key);
2897 }
2898
2899 /*
2900 * If initial position is on or in, decide which, depending on the
2901 * type of the parent. From this, expand initial position into a
2902 * version 4.0 value.
2903 */
2904 if (initialposition == 2)
2905 {
2906 sc_int count, parent, index_;
2907
2908 /* Get parent container/surface index. */
2909 vt_key[1].integer = object;
2910 vt_key[2].string = "Parent";
2911 count = prop_get_integer (parse_bundle, "I<-sis", vt_key);
2912
2913 /* Convert container/surface index. */
2914 for (parent = 0; parent < object_count && count >= 0; parent++)
2915 {
2916 if (object_type[parent] == V380_OBJ_IS_CONTAINER
2917 || object_type[parent] == V380_OBJ_IS_SURFACE)
2918 count--;
2919 }
2920 parent--;
2921
2922 /* If parent is a surface, adjust position. */
2923 if (object_type[parent] == V380_OBJ_IS_SURFACE)
2924 {
2925 vt_key[2].string = "InitialPosition";
2926 vt_value.integer = initialposition + 1;
2927 prop_put (parse_bundle, "I->sis", vt_value, vt_key);
2928 }
2929
2930 /*
2931 * For both, adjust parent to be an object index for that type
2932 * of object only.
2933 */
2934 count = 0;
2935 for (index_ = 0; index_ < parent; index_++)
2936 {
2937 if (object_type[index_] == object_type[parent])
2938 count++;
2939 }
2940 vt_key[2].string = "Parent";
2941 vt_value.integer = count;
2942 prop_put (parse_bundle, "I->sis", vt_value, vt_key);
2943 }
2944 }
2945
2946 /* Done with temporary array. */
2947 sc_free (object_type);
2948 }
2949
2950 /* Convert carry limit into version 4.0-like size and weight limits. */
2951 else if (strcmp (fixup, "|V380_MaxSize_MaxWt_|") == 0)
2952 {
2953 sc_vartype_t vt_key, vt_value;
2954 sc_int maxcarried;
2955
2956 vt_key.string = "MaxCarried";
2957 parse_push_key (vt_key, PROP_KEY_STRING);
2958 maxcarried = parse_get_integer_property ();
2959 parse_pop_key ();
2960
2961 vt_value.integer = maxcarried * V380_OBJ_CAPACITY_MULT
2962 + V380_OBJ_DEFAULT_SIZE;
2963
2964 vt_key.string = "MaxSize";
2965 parse_push_key (vt_key, PROP_KEY_STRING);
2966 parse_put_property (vt_value, PROP_INTEGER);
2967 parse_pop_key ();
2968
2969 vt_key.string = "MaxWt";
2970 parse_push_key (vt_key, PROP_KEY_STRING);
2971 parse_put_property (vt_value, PROP_INTEGER);
2972 parse_pop_key ();
2973 }
2974
2975 /* Add up positive scoring tasks to arrive at max score. */
2976 else if (strcmp (fixup, "|V380_GLOBAL:_MaxScore_|") == 0)
2977 {
2978 sc_vartype_t vt_key[3], vt_value;
2979 sc_int task_count, maxscore, task;
2980
2981 /* Get a count of tasks. */
2982 vt_key[0].string = "Tasks";
2983 task_count = prop_get_child_count (parse_bundle, "I<-s", vt_key);
2984
2985 /* Sum positive scoring tasks. */
2986 maxscore = 0;
2987 for (task = 0; task < task_count; task++)
2988 {
2989 sc_int score;
2990
2991 vt_key[1].integer = task;
2992 vt_key[2].string = "Score";
2993 score = prop_get_integer (parse_bundle, "I<-sis", vt_key);
2994 if (score > 0)
2995 maxscore += score;
2996 }
2997
2998 /* Write MaxScore global property. */
2999 vt_key[0].string = "Globals";
3000 vt_key[1].string = "MaxScore";
3001 vt_value.integer = maxscore;
3002 prop_put (parse_bundle, "I->ss", vt_value, vt_key);
3003 }
3004
3005 /* Convert walk meetobject from dynamic index to object. */
3006 else if (strcmp (fixup, "|V380_WALK:_MeetObject_|") == 0)
3007 {
3008 sc_vartype_t vt_key, vt_value, vt_gkey[3];
3009 sc_int meetobject, count, object_count, object;
3010
3011 vt_key.string = "MeetObject";
3012 parse_push_key (vt_key, PROP_KEY_STRING);
3013 meetobject = parse_get_integer_property ();
3014
3015 /* Get a count of objects. */
3016 vt_gkey[0].string = "Objects";
3017 object_count = prop_get_child_count (parse_bundle, "I<-s", vt_gkey);
3018
3019 /* Convert dynamic index to object, and rewrite. */
3020 count = meetobject - 1;
3021 for (object = 0; object < object_count && count >= 0; object++)
3022 {
3023 sc_bool bstatic;
3024
3025 vt_gkey[1].integer = object;
3026 vt_gkey[2].string = "Static";
3027 bstatic = prop_get_boolean (parse_bundle, "B<-sis", vt_gkey);
3028 if (!bstatic)
3029 count--;
3030 }
3031 object--;
3032
3033 vt_value.integer = object;
3034 parse_put_property (vt_value, PROP_INTEGER);
3035 parse_pop_key ();
3036 }
3037
3038 /* Convert version 3.8 room data into a version 4.0 alts array. */
3039 else if (strcmp (fixup, "|V380_ROOM:_Alts_|") == 0)
3040 {
3041 parse_fixup_v390_v380_room_alts ();
3042 }
3043
3044 /* Error if no fixup special handler available. */
3045 else
3046 {
3047 sc_fatal ("parse_fixup_v380: no handler for \"%s\"\n", fixup);
3048 }
3049
3050 if (parse_trace)
3051 sc_trace ("Parse: leaving version 3.8 fixup %s\n", fixup);
3052 }
3053
3054
3055 /*
3056 * parse_fixup()
3057 *
3058 * Handler for fixup special items to help with conversions from TAF version
3059 * 3.9 and version 3.8 formats into version 4.0.
3060 */
3061 static void
3062 parse_fixup (const sc_char *fixup)
3063 {
3064 /*
3065 * Pick a fixup handler specific to the TAF version. This helps keep
3066 * fixup code separate, rather than glommed into one large function.
3067 */
3068 switch (taf_get_version (parse_taf))
3069 {
3070 case TAF_VERSION_400:
3071 sc_fatal ("parse_fixup: unexpected call\n");
3072 break;
3073 case TAF_VERSION_390:
3074 parse_fixup_v390 (fixup);
3075 break;
3076 case TAF_VERSION_380:
3077 parse_fixup_v380 (fixup);
3078 break;
3079 default:
3080 sc_fatal ("parse_fixup: invalid TAF file version\n");
3081 break;
3082 }
3083 }
3084
3085
3086 /*
3087 * parse_element()
3088 *
3089 * Parse a class descriptor element.
3090 */
3091 static void
3092 parse_element (const sc_char *element)
3093 {
3094 if (parse_trace)
3095 sc_trace ("Parse: entering element %s\n", element);
3096
3097 /* Determine the element type from the first character. */
3098 switch (element[0])
3099 {
3100 case PARSE_ARRAY:
3101 parse_array (element);
3102 break;
3103 case PARSE_VECTOR:
3104 parse_vector (element);
3105 break;
3106 case PARSE_VECTOR_ALTERNATE:
3107 parse_vector_alternate (element);
3108 break;
3109 case PARSE_CLASS:
3110 parse_class (element);
3111 break;
3112 case PARSE_EXPRESSION:
3113 parse_expression (element);
3114 break;
3115 case PARSE_SPECIAL:
3116 parse_special (element);
3117 break;
3118 case PARSE_FIXUP:
3119 parse_fixup (element);
3120 break;
3121
3122 case PARSE_INTEGER:
3123 case PARSE_DEFAULT_ZERO:
3124 case PARSE_BOOLEAN:
3125 case PARSE_DEFAULT_TRUE:
3126 case PARSE_DEFAULT_FALSE:
3127 case PARSE_STRING:
3128 case PARSE_DEFAULT_EMPTY:
3129 case PARSE_IGNORE_INTEGER:
3130 case PARSE_IGNORE_BOOLEAN:
3131 case PARSE_IGNORE_STRING:
3132 case PARSE_MULTILINE:
3133 parse_terminal (element);
3134 break;
3135 default:
3136 sc_fatal ("parse_element: bad type, %c\n", element[0]);
3137 }
3138
3139 if (parse_trace)
3140 sc_trace ("Parse: leaving element %s\n", element);
3141 }
3142
3143
3144 /*
3145 * parse_descriptor()
3146 *
3147 * Parse a class's properties descriptor list.
3148 */
3149 static void
3150 parse_descriptor (const sc_char *descriptor)
3151 {
3152 sc_int next;
3153
3154 /* Find and parse each element in the descriptor. */
3155 for (next = 0; descriptor[next] != NUL; )
3156 {
3157 sc_char element[PARSE_TEMP_LENGTH];
3158
3159 /* Isolate the next descriptor element. */
3160 if (sscanf (descriptor + next, "%[^ ]", element) != 1)
3161 sc_fatal ("parse_element: no element, %s\n", descriptor + next);
3162
3163 /* Parse this isolated element. */
3164 parse_element (element);
3165
3166 /* Advance over the element and any trailing whitespace. */
3167 next += strlen (element);
3168 next += strspn (descriptor + next, " ");
3169 }
3170 }
3171
3172
3173 /*
3174 * parse_class()
3175 *
3176 * Parse a class of properties.
3177 */
3178 static void
3179 parse_class (const sc_char *class_)
3180 {
3181 sc_char class_name[PARSE_TEMP_LENGTH];
3182 sc_int index_;
3183 sc_vartype_t vt_key;
3184
3185 /* Isolate the class name. */
3186 if (sscanf (class_, "<%[^>]", class_name) != 1)
3187 sc_fatal ("parse_class: error in class, %s\n", class_);
3188 if (parse_trace)
3189 sc_trace ("Parse: entering class %s\n", class_name);
3190
3191 /* Find the class in the parse schema, and fail if not found. */
3192 for (index_ = 0; parse_schema[index_].class_name; index_++)
3193 {
3194 if (strcmp (parse_schema[index_].class_name, class_name) == 0)
3195 break;
3196 }
3197 if (!parse_schema[index_].class_name)
3198 sc_fatal ("parse_class: class not described, %s\n", class_name);
3199
3200 /*
3201 * Unless we are at the top level of the parse schema, push the class tag
3202 * as a key. The top level is "_GAME_", index_ 0, and isn't part of key
3203 * formation.
3204 */
3205 if (index_ > 0)
3206 {
3207 vt_key.string = class_ + strlen (class_name) + 2;
3208 parse_push_key (vt_key, PROP_KEY_STRING);
3209 }
3210
3211 /* Parse each element in the descriptor. */
3212 parse_descriptor (parse_schema[index_].descriptor);
3213
3214 /* Pop a key if the class tag was pushed above. */
3215 if (index_ > 0)
3216 parse_pop_key ();
3217
3218 if (parse_trace)
3219 sc_trace ("Parse: leaving class %s\n", class_name);
3220 }
3221
3222
3223 /*
3224 * parse_add_walkalerts()
3225 *
3226 * Add a list of all NPC walks started by each task. This is post-processing
3227 * that occurs after the TAF file has been successfully parsed.
3228 */
3229 static void
3230 parse_add_walkalerts (sc_prop_setref_t bundle)
3231 {
3232 sc_vartype_t vt_key[5];
3233 sc_int npcs_count, npc;
3234
3235 /* Get the count of NPCs. */
3236 vt_key[0].string = "NPCs";
3237 npcs_count = prop_get_child_count (bundle, "I<-s", vt_key);
3238
3239 /* Set up each NPC. */
3240 for (npc = 0; npc < npcs_count; npc++)
3241 {
3242 sc_int walk_count, walk;
3243
3244 /* Get NPC walk details. */
3245 vt_key[1].integer = npc;
3246 vt_key[2].string = "Walks";
3247 walk_count = prop_get_child_count (bundle, "I<-sis", vt_key);
3248
3249 for (walk = 0; walk < walk_count; walk++)
3250 {
3251 sc_int starttask;
3252
3253 /* Get start task of walk. */
3254 vt_key[3].integer = walk;
3255 vt_key[4].string = "StartTask";
3256 starttask = prop_get_integer (bundle, "I<-sisis", vt_key) - 1;
3257 if (starttask >= 0)
3258 {
3259 sc_vartype_t vt_key2[4], vt_value;
3260 sc_int count;
3261
3262 /* Count existing walkalerts for the task. */
3263 vt_key2[0].string = "Tasks";
3264 vt_key2[1].integer = starttask;
3265 vt_key2[2].string = "NPCWalkAlert";
3266 count = prop_get_child_count (bundle, "I<-sis", vt_key2);
3267
3268 /* Add two more -- NPC and walk. */
3269 vt_key2[3].integer = count;
3270 vt_value.integer = npc;
3271 prop_put (bundle, "I->sisi", vt_value, vt_key2);
3272 vt_key2[3].integer = count + 1;
3273 vt_value.integer = walk;
3274 prop_put (bundle, "I->sisi", vt_value, vt_key2);
3275 }
3276 }
3277 }
3278 }
3279
3280
3281 /*
3282 * parse_add_movetimes()
3283 *
3284 * Add a list of move times to all NPC walks. This is post-processing that
3285 * occurs after the TAF file has been successfully parsed.
3286 */
3287 static void
3288 parse_add_movetimes (sc_prop_setref_t bundle)
3289 {
3290 sc_vartype_t vt_key[6];
3291 sc_int npcs_count, npc;
3292
3293 /* Get the count of NPCs. */
3294 vt_key[0].string = "NPCs";
3295 npcs_count = prop_get_child_count (bundle, "I<-s", vt_key);
3296
3297 /* Set up each NPC. */
3298 for (npc = 0; npc < npcs_count; npc++)
3299 {
3300 sc_int walk_count, walk;
3301
3302 /* Get NPC walk details. */
3303 vt_key[1].integer = npc;
3304 vt_key[2].string = "Walks";
3305 walk_count = prop_get_child_count (bundle, "I<-sis", vt_key);
3306
3307 for (walk = 0; walk < walk_count; walk++)
3308 {
3309 sc_int waittimes;
3310 sc_int *movetimes, index_;
3311 sc_vartype_t vt_value;
3312
3313 vt_key[3].integer = walk;
3314 vt_key[4].string = "Times";
3315 waittimes = prop_get_child_count (bundle, "I<-sisis", vt_key);
3316
3317 movetimes = sc_malloc ((waittimes + 1) * sizeof (*movetimes));
3318 memset (movetimes, 0, (waittimes + 1) * sizeof (*movetimes));
3319 for (index_ = waittimes - 1; index_ >= 0; index_--)
3320 {
3321 vt_key[4].string = "Times";
3322 vt_key[5].integer = index_;
3323 movetimes[index_] = prop_get_integer (bundle, "I<-sisisi", vt_key)
3324 + movetimes[index_ + 1];
3325 }
3326 movetimes[waittimes] = -2;
3327
3328 for (index_ = 0; index_ <= waittimes; index_++)
3329 {
3330 vt_key[4].string = "MoveTimes";
3331 vt_key[5].integer = index_;
3332 vt_value.integer = movetimes[index_];
3333 prop_put (bundle, "I->sisisi", vt_value, vt_key);
3334 }
3335 sc_free (movetimes);
3336 }
3337 }
3338 }
3339
3340
3341 /*
3342 * parse_add_alrs_index()
3343 *
3344 * Sort ALRs by original string length and store an indexer property, so
3345 * that ALR replacements look at longer strings before shorter ones.
3346 */
3347 static void
3348 parse_add_alrs_index (sc_prop_setref_t bundle)
3349 {
3350 sc_vartype_t vt_key[3];
3351 sc_int alr_count, index_, alr;
3352 sc_int *alr_lengths, longest, shortest, length;
3353
3354 /* Count ALRs, and set invariant part of properties key. */
3355 vt_key[0].string = "ALRs";
3356 alr_count = prop_get_child_count (bundle, "I<-s", vt_key);
3357
3358 /*
3359 * Set up an array of the lengths of ALR original strings, and while at it,
3360 * get the shortest and longest defined.
3361 */
3362 alr_lengths = sc_malloc (alr_count * sizeof (*alr_lengths));
3363 shortest = INT_MAX;
3364 longest = 0;
3365 for (index_ = 0; index_ < alr_count; index_++)
3366 {
3367 const sc_char *original;
3368
3369 vt_key[1].integer = index_;
3370 vt_key[2].string = "Original";
3371 original = prop_get_string (bundle, "S<-sis", vt_key);
3372 length = strlen (original);
3373
3374 alr_lengths[index_] = length;
3375 shortest = (length < shortest) ? length : shortest;
3376 longest = (length > longest) ? length : longest;
3377 }
3378
3379 /*
3380 * Now write a set of secondary properties that define the order of handling
3381 * for ALRs. Our friend qsort() can't help here as it doesn't define the
3382 * final ordering of equal members, and we need here to retain file ordering
3383 * for ALR originals of the same length.
3384 */
3385 vt_key[0].string = "ALRs2";
3386 alr = 0;
3387 for (length = longest; length >= shortest; length--)
3388 {
3389 /* Find and add each ALR of this length. */
3390 for (index_ = 0; index_ < alr_count; index_++)
3391 {
3392 if (alr_lengths[index_] == length)
3393 {
3394 sc_vartype_t vt_value;
3395
3396 vt_key[1].integer = alr++;
3397 vt_key[2].string = "ALRIndex";
3398 vt_value.integer = index_;
3399 prop_put (bundle, "I->sis", vt_value, vt_key);
3400 }
3401 }
3402 }
3403 assert (alr == alr_count);
3404
3405 /* Done with ALR lengths array. */
3406 sc_free (alr_lengths);
3407 }
3408
3409
3410 /*
3411 * parse_add_resources_offset()
3412 *
3413 * Add the resources offset to the properties as an extra game property
3414 * for version 4.0 games. For version 3.9 and version 3.8 games, write
3415 * zero; only version 4.0 games can embed their resources into the TAF file.
3416 */
3417 static void
3418 parse_add_resources_offset (sc_prop_setref_t bundle, sc_tafref_t taf)
3419 {
3420 sc_vartype_t vt_key[2], vt_value;
3421 sc_bool embedded;
3422 sc_int offset;
3423
3424 /*
3425 * Get the resources offset from the TAF, or default to zero. The resources
3426 * offset is one byte after the end of game data.
3427 */
3428 vt_key[0].string = "Globals";
3429 vt_key[1].string = "Embedded";
3430 embedded = prop_get_boolean (bundle, "B<-ss", vt_key);
3431 offset = embedded ? taf_get_game_data_length (taf) + 1 : 0;
3432
3433 /* Add this offset to the properties. */
3434 vt_key[0].string = "ResourceOffset";
3435 vt_value.integer = offset;
3436 prop_put (bundle, "I->s", vt_value, vt_key);
3437 }
3438
3439
3440 /*
3441 * parse_add_version()
3442 *
3443 * Add the TAF version to the properties, both integer and character forms
3444 * for convenience.
3445 */
3446 static void
3447 parse_add_version (sc_prop_setref_t bundle, sc_tafref_t taf)
3448 {
3449 sc_vartype_t vt_key, vt_value;
3450
3451 /* Add the version integer to the properties. */
3452 vt_key.string = "Version";
3453 vt_value.integer = taf_get_version (taf);
3454 prop_put (bundle, "I->s", vt_value, &vt_key);
3455
3456 /* Add the version string to the properties. */
3457 switch (taf_get_version (taf))
3458 {
3459 case TAF_VERSION_400:
3460 vt_value.string = "4.00";
3461 break;
3462 case TAF_VERSION_390:
3463 vt_value.string = "3.90";
3464 break;
3465 case TAF_VERSION_380:
3466 vt_value.string = "3.80";
3467 break;
3468 default:
3469 sc_error ("parse_add_version_string: invalid TAF file version\n");
3470 vt_value.string = "[Unknown version]";
3471 break;
3472 }
3473 vt_key.string = "VersionString";
3474 prop_put (bundle, "S->s", vt_value, &vt_key);
3475 }
3476
3477
3478 /*
3479 * parse_game()
3480 *
3481 * Parse a game into a set properties. Return TRUE on success, FALSE if
3482 * it encountered an error reading the TAF file.
3483 */
3484 sc_bool
3485 parse_game (sc_tafref_t taf, sc_prop_setref_t bundle)
3486 {
3487 assert (taf && bundle);
3488
3489 /* Store the TAF to read from, and the bundle to store into. */
3490 parse_taf = taf;
3491 parse_bundle = bundle;
3492 parse_schema = parse_select_schema (parse_taf);
3493 parse_depth = 0;
3494
3495 /* Try parsing, and catch errors from longjmp. */
3496 if (setjmp (parse_taf_error) == 0)
3497 {
3498 /* Parse a complete game. */
3499 taf_first_line (parse_taf);
3500 parse_tafline = 0;
3501 parse_class ("<_GAME_>");
3502 }
3503 else
3504 {
3505 /* Error with one of the TAF file lines. */
3506 parse_clear_v400_resources_table ();
3507 parse_taf = NULL;
3508 parse_bundle = NULL;
3509 parse_schema = NULL;
3510 parse_depth = 0;
3511 return FALSE;
3512 }
3513
3514 /* Free the accumulated version 4.0 resources details. */
3515 parse_clear_v400_resources_table ();
3516
3517 /* See if we reached the end of the TAF. */
3518 if (taf_more_lines (parse_taf))
3519 sc_error ("parse_game: unexpected trailing data\n");
3520
3521 /* Append post-processing walkalerts and move times. */
3522 parse_add_walkalerts (parse_bundle);
3523 parse_add_movetimes (parse_bundle);
3524
3525 /* Append sorted ALR list and resources offset. */
3526 parse_add_alrs_index (parse_bundle);
3527 parse_add_resources_offset (parse_bundle, parse_taf);
3528
3529 /* Add a note of the TAF file version. */
3530 parse_add_version (parse_bundle, parse_taf);
3531
3532 /* Trim excess allocations from properties. */
3533 prop_solidify (parse_bundle);
3534
3535 /* Return successfully. */
3536 parse_taf = NULL;
3537 parse_bundle = NULL;
3538 parse_schema = NULL;
3539 parse_depth = 0;
3540 return TRUE;
3541 }
3542
3543
3544 /*
3545 * parse_debug_trace()
3546 *
3547 * Set parse tracing on/off.
3548 */
3549 void
3550 parse_debug_trace (sc_bool flag)
3551 {
3552 parse_trace = flag;
3553 }