diff options
author | dreammaster | 2019-06-27 04:02:48 +0100 |
---|---|---|
committer | Paul Gilbert | 2019-07-06 15:27:08 -0700 |
commit | dc40211ec5e54d01f7cb822940714ed6e6da36d5 (patch) | |
tree | dedda7f6ce1ab7f581941d9f002ad1926fa3e367 /engines | |
parent | aebf25661076eaa382b3025f0123f6ceb7cf920b (diff) | |
download | scummvm-rg350-dc40211ec5e54d01f7cb822940714ed6e6da36d5.tar.gz scummvm-rg350-dc40211ec5e54d01f7cb822940714ed6e6da36d5.tar.bz2 scummvm-rg350-dc40211ec5e54d01f7cb822940714ed6e6da36d5.zip |
GLK: ALAN3: Proper indentation & formatting
Diffstat (limited to 'engines')
68 files changed, 8085 insertions, 8192 deletions
diff --git a/engines/glk/alan3/acode.h b/engines/glk/alan3/acode.h index 0c404607b7..079ea87878 100644 --- a/engines/glk/alan3/acode.h +++ b/engines/glk/alan3/acode.h @@ -30,19 +30,19 @@ namespace Alan3 { #define ACODEEXTENSION ".a3c" -typedef uint32 Aptr; /* Type for an ACODE memory address used in the structures */ +typedef uint32 Aptr; /* Type for an ACODE memory address used in the structures */ /* TODO: Here's the major 32->64bit problem: Aptrs are 32 bit to fit into the 32-bit structure of the Amachine, but sometimes this is used to store a *real* pointer value, which on 64-bit machines are 64bits. */ -typedef uint32 Aword; /* Type for an ACODE word */ -typedef uint32 Aaddr; /* Type for an ACODE address */ -typedef uint32 Aid; /* Type for an ACODE Instance Id value */ -typedef int32 Abool; /* Type for an ACODE Boolean value */ -typedef int32 Aint; /* Type for an ACODE Integer value */ -typedef int32 Aset; /* Type for an ACODE Set value */ -typedef int CodeValue; /* Definition for the packing process */ +typedef uint32 Aword; /* Type for an ACODE word */ +typedef uint32 Aaddr; /* Type for an ACODE address */ +typedef uint32 Aid; /* Type for an ACODE Instance Id value */ +typedef int32 Abool; /* Type for an ACODE Boolean value */ +typedef int32 Aint; /* Type for an ACODE Integer value */ +typedef int32 Aset; /* Type for an ACODE Set value */ +typedef int CodeValue; /* Definition for the packing process */ #ifndef TRUE #define TRUE (0==0) @@ -63,9 +63,9 @@ typedef int CodeValue; /* Definition for the packing process */ #define TOPVALUE (((CodeValue)1<<VALUEBITS) - 1) /* Highest value possible */ /* Half and quarter points in the code value range */ -#define ONEQUARTER (TOPVALUE/4+1) /* Point after first quarter */ -#define HALF (2*ONEQUARTER) /* Point after first half */ -#define THREEQUARTER (3*ONEQUARTER) /* Point after third quarter */ +#define ONEQUARTER (TOPVALUE/4+1) /* Point after first quarter */ +#define HALF (2*ONEQUARTER) /* Point after first half */ +#define THREEQUARTER (3*ONEQUARTER) /* Point after third quarter */ /* AMACHINE Word Classes, bit positions */ @@ -129,40 +129,40 @@ typedef int WordKind; /* Parameter Classes */ -enum ClaKind { /* NOTE! These must have the same order as */ - CLA_OBJ = 1, /* the name classes in NAM.H */ - CLA_CNT = (int)CLA_OBJ<<1, - CLA_ACT = (int)CLA_CNT<<1, - CLA_NUM = (int)CLA_ACT<<1, - CLA_STR = (int)CLA_NUM<<1, - CLA_COBJ = (int)CLA_STR<<1, - CLA_CACT = (int)CLA_COBJ<<1 +enum ClaKind { /* NOTE! These must have the same order as */ + CLA_OBJ = 1, /* the name classes in NAM.H */ + CLA_CNT = (int)CLA_OBJ << 1, + CLA_ACT = (int)CLA_CNT << 1, + CLA_NUM = (int)CLA_ACT << 1, + CLA_STR = (int)CLA_NUM << 1, + CLA_COBJ = (int)CLA_STR << 1, + CLA_CACT = (int)CLA_COBJ << 1 }; /* Verb Qualifiers */ enum QualClass { - Q_DEFAULT, - Q_AFTER, - Q_BEFORE, - Q_ONLY + Q_DEFAULT, + Q_AFTER, + Q_BEFORE, + Q_ONLY }; /* The AMACHINE Operations */ enum OpClass { - C_CONST, - C_STMOP, - C_CURVAR + C_CONST, + C_STMOP, + C_CURVAR }; /* AMACHINE Text Styles */ enum TextStyle { - NORMAL_STYLE, - EMPHASIZED_STYLE, - PREFORMATTED_STYLE, - ALERT_STYLE, - QUOTE_STYLE + NORMAL_STYLE, + EMPHASIZED_STYLE, + PREFORMATTED_STYLE, + ALERT_STYLE, + QUOTE_STYLE }; @@ -171,138 +171,138 @@ enum TextStyle { #define CURVAR(op) ((((Aword)C_CURVAR)<<28)|((Aword)op)) enum InstClass { - I_LINE, /* Source line debug info */ - I_PRINT, /* Print a string from the text file */ - I_STYLE, /* Set output text style */ - I_QUIT, - I_LOOK, - I_SAVE, - I_RESTORE, - I_LIST, /* List contents of a container */ - I_EMPTY, - I_SCORE, - I_VISITS, - I_SCHEDULE, - I_CANCEL, - I_LOCATE, - I_MAKE, /* Set a boolean attribute to the */ - /* value on top of stack */ - I_SET, /* Set a numeric attribute to the */ - /* value on top of stack */ - I_SETSTR, /* Set a string valued attribute to */ - /* the string on top of stack, */ - /* deallocate current contents first */ - I_SETSET, /* Set a Set valued attribute to */ - /* the Set on top of stack, */ - /* deallocate current contents first */ - I_NEWSET, /* Push a new, empty set at the top of stack */ - I_ATTRIBUTE, /* Push the value of an attribute */ - I_ATTRSTR, /* Push a copy of a string attribute */ - I_ATTRSET, /* Push a copy of a set attribute */ - I_UNION, /* Add a set from the top of stack to a */ - /* set valued attribute */ - I_GETSTR, /* Get a string contents from text + I_LINE, /* Source line debug info */ + I_PRINT, /* Print a string from the text file */ + I_STYLE, /* Set output text style */ + I_QUIT, + I_LOOK, + I_SAVE, + I_RESTORE, + I_LIST, /* List contents of a container */ + I_EMPTY, + I_SCORE, + I_VISITS, + I_SCHEDULE, + I_CANCEL, + I_LOCATE, + I_MAKE, /* Set a boolean attribute to the */ + /* value on top of stack */ + I_SET, /* Set a numeric attribute to the */ + /* value on top of stack */ + I_SETSTR, /* Set a string valued attribute to */ + /* the string on top of stack, */ + /* deallocate current contents first */ + I_SETSET, /* Set a Set valued attribute to */ + /* the Set on top of stack, */ + /* deallocate current contents first */ + I_NEWSET, /* Push a new, empty set at the top of stack */ + I_ATTRIBUTE, /* Push the value of an attribute */ + I_ATTRSTR, /* Push a copy of a string attribute */ + I_ATTRSET, /* Push a copy of a set attribute */ + I_UNION, /* Add a set from the top of stack to a */ + /* set valued attribute */ + I_GETSTR, /* Get a string contents from text file, create a copy and push it on top of stack */ - I_INCR, /* Increase an attribute */ - I_DECR, /* Decrease a numeric attribute */ - I_INCLUDE, /* Include a value in the set on stack top */ - I_EXCLUDE, /* Remove a value from the set on stack top */ - I_SETSIZE, /* Push number of members in a set */ - I_SETMEMB, /* Push the member with index <top>-1 + I_INCR, /* Increase an attribute */ + I_DECR, /* Decrease a numeric attribute */ + I_INCLUDE, /* Include a value in the set on stack top */ + I_EXCLUDE, /* Remove a value from the set on stack top */ + I_SETSIZE, /* Push number of members in a set */ + I_SETMEMB, /* Push the member with index <top>-1 from set <top> */ - I_CONTSIZE, /* Push number of members in a container */ - I_CONTMEMB, /* Push the member with index <top>-1 + I_CONTSIZE, /* Push number of members in a container */ + I_CONTMEMB, /* Push the member with index <top>-1 from container <top> */ - I_USE, - I_STOP, - I_AT, - I_IN, - I_INSET, - I_HERE, - I_NEARBY, - I_NEAR, - I_WHERE, /* Current position of an instance */ - I_LOCATION, /* The *location* an instance is at */ - I_DESCRIBE, - I_SAY, - I_SAYINT, - I_SAYSTR, - I_IF, - I_ELSE, - I_ENDIF, - I_AND, - I_OR, - I_NE, - I_EQ, - I_STREQ, /* String compare */ - I_STREXACT, /* Exact match */ - I_LE, - I_GE, - I_LT, - I_GT, - I_PLUS, - I_MINUS, - I_MULT, - I_DIV, - I_NOT, - I_UMINUS, - I_RND, - I_RETURN, - I_SYSTEM, - I_RESTART, - I_BTW, - I_CONTAINS, - I_DUP, - I_DEPEND, - I_DEPCASE, - I_DEPEXEC, - I_DEPELSE, - I_ENDDEP, - I_ISA, - I_FRAME, - I_SETLOCAL, - I_GETLOCAL, - I_ENDFRAME, - I_LOOP, - I_LOOPNEXT, - I_LOOPEND, - I_SUM, /* Aggregates: */ - I_MAX, - I_MIN, - I_COUNT, /* COUNT aggregate & limit meta-attribute */ - I_SHOW, - I_PLAY, - I_CONCAT, - I_STRIP, - I_POP, + I_USE, + I_STOP, + I_AT, + I_IN, + I_INSET, + I_HERE, + I_NEARBY, + I_NEAR, + I_WHERE, /* Current position of an instance */ + I_LOCATION, /* The *location* an instance is at */ + I_DESCRIBE, + I_SAY, + I_SAYINT, + I_SAYSTR, + I_IF, + I_ELSE, + I_ENDIF, + I_AND, + I_OR, + I_NE, + I_EQ, + I_STREQ, /* String compare */ + I_STREXACT, /* Exact match */ + I_LE, + I_GE, + I_LT, + I_GT, + I_PLUS, + I_MINUS, + I_MULT, + I_DIV, + I_NOT, + I_UMINUS, + I_RND, + I_RETURN, + I_SYSTEM, + I_RESTART, + I_BTW, + I_CONTAINS, + I_DUP, + I_DEPEND, + I_DEPCASE, + I_DEPEXEC, + I_DEPELSE, + I_ENDDEP, + I_ISA, + I_FRAME, + I_SETLOCAL, + I_GETLOCAL, + I_ENDFRAME, + I_LOOP, + I_LOOPNEXT, + I_LOOPEND, + I_SUM, /* Aggregates: */ + I_MAX, + I_MIN, + I_COUNT, /* COUNT aggregate & limit meta-attribute */ + I_SHOW, + I_PLAY, + I_CONCAT, + I_STRIP, + I_POP, I_TRANSCRIPT, - I_DUPSTR /* Duplicate the string on the top of the stack */ + I_DUPSTR /* Duplicate the string on the top of the stack */ }; enum SayForm { - SAY_SIMPLE, - SAY_DEFINITE, - SAY_INDEFINITE, - SAY_NEGATIVE, - SAY_PRONOUN + SAY_SIMPLE, + SAY_DEFINITE, + SAY_INDEFINITE, + SAY_NEGATIVE, + SAY_PRONOUN }; enum VarClass { - V_PARAM, - V_CURLOC, - V_CURACT, - V_CURVRB, - V_SCORE, - V_CURRENT_INSTANCE, - V_MAX_INSTANCE + V_PARAM, + V_CURLOC, + V_CURACT, + V_CURVRB, + V_SCORE, + V_CURRENT_INSTANCE, + V_MAX_INSTANCE }; /* For transitivity in HERE, IN etc. */ enum ATrans { - TRANSITIVE = 0, - DIRECT = 1, - INDIRECT = 2 + TRANSITIVE = 0, + DIRECT = 1, + INDIRECT = 2 }; /* Predefined attributes, one is for containers and the other for locations @@ -320,76 +320,76 @@ enum ATrans { #define AwordSizeOf(x) (sizeof(x)/sizeof(Aword)) struct ArticleEntry { - Aaddr address; /* Address of article code */ - Abool isForm; /* Is the article a complete form? */ -}; - -struct ClassEntry { /* CLASS TABLE */ - Aword code; /* Own code */ - Aaddr id; /* Address to identifier string */ - Aint parent; /* Code for the parent class, 0 if none */ - Aaddr name; /* Address to name printing code */ - Aint pronoun; /* Code for the pronoun word */ - Aaddr initialize; /* Address to initialization statements */ - Aaddr descriptionChecks; /* Address of description checks */ - Aaddr description; /* Address of description code */ - ArticleEntry definite; /* Definite article entry */ - ArticleEntry indefinite; /* Indefinite article entry */ - ArticleEntry negative; /* Negative article entry */ - Aaddr mentioned; /* Address of code for Mentioned clause */ - Aaddr verbs; /* Address of verb table */ - Aaddr entered; /* Address of code for Entered clause */ -}; - -struct InstanceEntry { /* INSTANCE TABLE */ - Aint code; /* Own code */ - Aaddr id; /* Address to identifier string */ - Aint parent; /* Code for the parent class, 0 if none */ - Aaddr name; /* Address to name printing code */ - Aint pronoun; /* Word code for the pronoun */ - Aint initialLocation; /* Code for current location */ - Aaddr initialize; /* Address to initialization statements */ - Aint container; /* Code for a possible container property */ - Aaddr initialAttributes; /* Address of attribute list */ - Aaddr checks; /* Address of description checks */ - Aaddr description; /* Address of description code */ - ArticleEntry definite; /* Definite article entry */ - ArticleEntry indefinite; /* Indefinite article entry */ - ArticleEntry negative; /* Negative article entry */ - Aaddr mentioned; /* Address to short description code */ - Aaddr verbs; /* Address of local verb list */ - Aaddr entered; /* Address of entered code (location only) */ - Aaddr exits; /* Address of exit list */ -}; - -struct AttributeEntry { /* ATTRIBUTE LIST */ - Aint code; /* Its code */ - Aptr value; /* Its value, a string has a dynamic + Aaddr address; /* Address of article code */ + Abool isForm; /* Is the article a complete form? */ +}; + +struct ClassEntry { /* CLASS TABLE */ + Aword code; /* Own code */ + Aaddr id; /* Address to identifier string */ + Aint parent; /* Code for the parent class, 0 if none */ + Aaddr name; /* Address to name printing code */ + Aint pronoun; /* Code for the pronoun word */ + Aaddr initialize; /* Address to initialization statements */ + Aaddr descriptionChecks; /* Address of description checks */ + Aaddr description; /* Address of description code */ + ArticleEntry definite; /* Definite article entry */ + ArticleEntry indefinite; /* Indefinite article entry */ + ArticleEntry negative; /* Negative article entry */ + Aaddr mentioned; /* Address of code for Mentioned clause */ + Aaddr verbs; /* Address of verb table */ + Aaddr entered; /* Address of code for Entered clause */ +}; + +struct InstanceEntry { /* INSTANCE TABLE */ + Aint code; /* Own code */ + Aaddr id; /* Address to identifier string */ + Aint parent; /* Code for the parent class, 0 if none */ + Aaddr name; /* Address to name printing code */ + Aint pronoun; /* Word code for the pronoun */ + Aint initialLocation; /* Code for current location */ + Aaddr initialize; /* Address to initialization statements */ + Aint container; /* Code for a possible container property */ + Aaddr initialAttributes; /* Address of attribute list */ + Aaddr checks; /* Address of description checks */ + Aaddr description; /* Address of description code */ + ArticleEntry definite; /* Definite article entry */ + ArticleEntry indefinite; /* Indefinite article entry */ + ArticleEntry negative; /* Negative article entry */ + Aaddr mentioned; /* Address to short description code */ + Aaddr verbs; /* Address of local verb list */ + Aaddr entered; /* Address of entered code (location only) */ + Aaddr exits; /* Address of exit list */ +}; + +struct AttributeEntry { /* ATTRIBUTE LIST */ + Aint code; /* Its code */ + Aptr value; /* Its value, a string has a dynamic string pointer, a set has a pointer to a dynamically allocated set */ - Aaddr id; /* Address to the name */ + Aaddr id; /* Address to the name */ }; -struct AttributeHeaderEntry { /* ATTRIBUTE LIST in header */ - Aint code; /* Its code */ - Aword value; /* Its value, a string has a dynamic +struct AttributeHeaderEntry { /* ATTRIBUTE LIST in header */ + Aint code; /* Its code */ + Aword value; /* Its value, a string has a dynamic string pointer, a set has a pointer to a dynamically allocated set */ - Aaddr id; /* Address to the name */ + Aaddr id; /* Address to the name */ }; -struct ExitEntry { /* EXIT TABLE structure */ - Aword code; /* Direction code */ - Aaddr checks; /* Address of check table */ - Aaddr action; /* Address of action code */ - Aword target; /* Id for the target location */ +struct ExitEntry { /* EXIT TABLE structure */ + Aword code; /* Direction code */ + Aaddr checks; /* Address of check table */ + Aaddr action; /* Address of action code */ + Aword target; /* Id for the target location */ }; struct RuleEntry { /* RULE TABLE */ - Abool alreadyRun; - Aaddr exp; /* Address to expression code */ - Aaddr stms; /* Address to run */ + Abool alreadyRun; + Aaddr exp; /* Address to expression code */ + Aaddr stms; /* Address to run */ }; @@ -398,102 +398,102 @@ struct RuleEntry { /* RULE TABLE */ #define RESTRICTIONCLASS_STRING (-4) struct RestrictionEntry { /* PARAMETER RESTRICTION TABLE */ - Aint parameterNumber; /* Parameter number */ - Aint _class; /* Parameter class code */ - Aaddr stms; /* Exception statements */ + Aint parameterNumber; /* Parameter number */ + Aint _class; /* Parameter class code */ + Aaddr stms; /* Exception statements */ }; -struct ContainerEntry { /* CONTAINER TABLE */ - Aword owner; /* Owner instance index */ - Aint _class; /* Class to allow in container */ - Aaddr limits; /* Address to limit check code */ - Aaddr header; /* Address to header code */ - Aaddr empty; /* Address to code for header when empty */ - Aaddr extractChecks; /* Address to check before extracting */ - Aaddr extractStatements; /* Address to execute when extracting */ +struct ContainerEntry { /* CONTAINER TABLE */ + Aword owner; /* Owner instance index */ + Aint _class; /* Class to allow in container */ + Aaddr limits; /* Address to limit check code */ + Aaddr header; /* Address to header code */ + Aaddr empty; /* Address to code for header when empty */ + Aaddr extractChecks; /* Address to check before extracting */ + Aaddr extractStatements; /* Address to execute when extracting */ }; -struct ElementEntry { /* SYNTAX ELEMENT TABLES */ - Aint code; /* Code for this element, 0 -> parameter */ - Aword flags; /* Flags for multiple/omni (if parameter), syntax number/verb of EOS */ - Aaddr next; /* Address to next element table ... */ - /* ... or restrictions if code == EOS */ +struct ElementEntry { /* SYNTAX ELEMENT TABLES */ + Aint code; /* Code for this element, 0 -> parameter */ + Aword flags; /* Flags for multiple/omni (if parameter), syntax number/verb of EOS */ + Aaddr next; /* Address to next element table ... */ + /* ... or restrictions if code == EOS */ }; -struct SyntaxEntryPreBeta2 { /* SYNTAX TABLE */ - Aint code; /* Code for verb word */ - Aaddr elms; /* Address to element tables */ +struct SyntaxEntryPreBeta2 { /* SYNTAX TABLE */ + Aint code; /* Code for verb word */ + Aaddr elms; /* Address to element tables */ }; struct SyntaxEntry { /* SYNTAX TABLE */ - Aint code; /* Code for verb word, or 0 if starting with parameter */ - Aaddr elms; /* Address to element tables */ - Aaddr parameterNameTable; /* Address to a table of id-addresses giving the names of the parameters */ + Aint code; /* Code for verb word, or 0 if starting with parameter */ + Aaddr elms; /* Address to element tables */ + Aaddr parameterNameTable; /* Address to a table of id-addresses giving the names of the parameters */ }; -struct ParameterMapEntry { /* PARAMETER MAPPING TABLE */ - Aint syntaxNumber; - Aaddr parameterMapping; - Aint verbCode; +struct ParameterMapEntry { /* PARAMETER MAPPING TABLE */ + Aint syntaxNumber; + Aaddr parameterMapping; + Aint verbCode; }; -struct EventEntry { /* EVENT TABLE */ - Aaddr id; /* Address to name string */ - Aaddr code; +struct EventEntry { /* EVENT TABLE */ + Aaddr id; /* Address to name string */ + Aaddr code; }; -struct ScriptEntry { /* SCRIPT TABLE */ - Aaddr id; /* Address to name string */ - Aint code; /* Script number */ - Aaddr description; /* Optional description statements */ - Aaddr steps; /* Address to steps */ +struct ScriptEntry { /* SCRIPT TABLE */ + Aaddr id; /* Address to name string */ + Aint code; /* Script number */ + Aaddr description; /* Optional description statements */ + Aaddr steps; /* Address to steps */ }; -struct StepEntry { /* STEP TABLE */ - Aaddr after; /* Expression to say after how many ticks? */ - Aaddr exp; /* Expression to condition saying when */ - Aaddr stms; /* Address to the actual code */ +struct StepEntry { /* STEP TABLE */ + Aaddr after; /* Expression to say after how many ticks? */ + Aaddr exp; /* Expression to condition saying when */ + Aaddr stms; /* Address to the actual code */ }; -struct AltEntry { /* VERB ALTERNATIVE TABLE */ - Aword qual; /* Verb execution qualifier */ - Aint param; /* Parameter number */ - Aaddr checks; /* Address of the check table */ - Aaddr action; /* Address of the action code */ +struct AltEntry { /* VERB ALTERNATIVE TABLE */ + Aword qual; /* Verb execution qualifier */ + Aint param; /* Parameter number */ + Aaddr checks; /* Address of the check table */ + Aaddr action; /* Address of the action code */ }; struct SourceFileEntry { /* SOURCE FILE NAME TABLE */ - Aint fpos; - Aint len; + Aint fpos; + Aint len; }; struct SourceLineEntry { /* SOURCE LINE TABLE */ - Aint file; - Aint line; + Aint file; + Aint line; }; struct StringInitEntry { /* STRING INITIALISATION TABLE */ - Aword fpos; /* File position */ - Aword len; /* Length */ - Aint instanceCode; /* Where to store it */ - Aint attributeCode; + Aword fpos; /* File position */ + Aword len; /* Length */ + Aint instanceCode; /* Where to store it */ + Aint attributeCode; }; -struct SetInitEntry { /* SET INITIALISATION TABLE */ - Aint size; /* Size of the initial set */ - Aword setAddress; /* Address to the initial set */ - Aint instanceCode; /* Where to store it */ - Aint attributeCode; +struct SetInitEntry { /* SET INITIALISATION TABLE */ + Aint size; /* Size of the initial set */ + Aword setAddress; /* Address to the initial set */ + Aint instanceCode; /* Where to store it */ + Aint attributeCode; }; struct DictionaryEntry { /* Dictionary */ - Aaddr string; /* ACODE address to string */ - Aword classBits; /* Word class */ - Aword code; - Aaddr adjectiveRefs; /* Address to reference list */ - Aaddr nounRefs; /* Address to reference list */ - Aaddr pronounRefs; /* Address to reference list */ + Aaddr string; /* ACODE address to string */ + Aword classBits; /* Word class */ + Aword code; + Aaddr adjectiveRefs; /* Address to reference list */ + Aaddr nounRefs; /* Address to reference list */ + Aaddr pronounRefs; /* Address to reference list */ }; @@ -501,222 +501,222 @@ struct DictionaryEntry { /* Dictionary */ /* AMACHINE Header */ struct ACodeHeader { - /* Important info */ - char tag[4]; /* "ALAN" */ - char version[4]; /* Version of compiler */ - Aword uid; /* Unique id of the compiled game */ - Aword size; /* Size of ACD-file in Awords */ - /* Options */ - Abool pack; /* Is the text packed and encoded ? */ - Aword stringOffset; /* Offset to string data in game file */ - Aword pageLength; /* Length of a displayed page */ - Aword pageWidth; /* and width */ - Aword debug; /* Option: debug */ - /* Data structures */ - Aaddr classTableAddress; - Aword classMax; - Aword entityClassId; - Aword thingClassId; - Aword objectClassId; - Aword locationClassId; - Aword actorClassId; - Aword literalClassId; - Aword integerClassId; - Aword stringClassId; - Aaddr instanceTableAddress; /* Instance table */ - Aword instanceMax; /* Highest number of an instance */ - Aword theHero; /* The hero instance code (id) */ - Aaddr containerTableAddress; - Aword containerMax; - Aaddr scriptTableAddress; - Aword scriptMax; - Aaddr eventTableAddress; - Aword eventMax; - Aaddr syntaxTableAddress; - Aaddr parameterMapAddress; - Aword syntaxMax; - Aaddr dictionary; - Aaddr verbTableAddress; - Aaddr ruleTableAddress; - Aaddr messageTableAddress; - /* Miscellaneous */ - Aint attributesAreaSize; /* Size of attribute data area in Awords */ - Aint maxParameters; /* Maximum number of parameters in any syntax */ - Aaddr stringInitTable; /* String init table address */ - Aaddr setInitTable; /* Set init table address */ - Aaddr start; /* Address to Start code */ - Aword maximumScore; /* Maximum score */ - Aaddr scores; /* Score table */ - Aint scoreCount; /* Max index into scores table */ - Aaddr sourceFileTable; /* Table of fpos/len for source filenames */ - Aaddr sourceLineTable; /* Table of available source lines to break on */ - Aaddr freq; /* Address to Char freq's for coding */ - Aword acdcrc; /* Checksum for acd code (excl. hdr) */ - Aword txtcrc; /* Checksum for text data file */ - Aaddr ifids; /* Address to IFIDS */ - Aaddr prompt; + /* Important info */ + char tag[4]; /* "ALAN" */ + char version[4]; /* Version of compiler */ + Aword uid; /* Unique id of the compiled game */ + Aword size; /* Size of ACD-file in Awords */ + /* Options */ + Abool pack; /* Is the text packed and encoded ? */ + Aword stringOffset; /* Offset to string data in game file */ + Aword pageLength; /* Length of a displayed page */ + Aword pageWidth; /* and width */ + Aword debug; /* Option: debug */ + /* Data structures */ + Aaddr classTableAddress; + Aword classMax; + Aword entityClassId; + Aword thingClassId; + Aword objectClassId; + Aword locationClassId; + Aword actorClassId; + Aword literalClassId; + Aword integerClassId; + Aword stringClassId; + Aaddr instanceTableAddress; /* Instance table */ + Aword instanceMax; /* Highest number of an instance */ + Aword theHero; /* The hero instance code (id) */ + Aaddr containerTableAddress; + Aword containerMax; + Aaddr scriptTableAddress; + Aword scriptMax; + Aaddr eventTableAddress; + Aword eventMax; + Aaddr syntaxTableAddress; + Aaddr parameterMapAddress; + Aword syntaxMax; + Aaddr dictionary; + Aaddr verbTableAddress; + Aaddr ruleTableAddress; + Aaddr messageTableAddress; + /* Miscellaneous */ + Aint attributesAreaSize; /* Size of attribute data area in Awords */ + Aint maxParameters; /* Maximum number of parameters in any syntax */ + Aaddr stringInitTable; /* String init table address */ + Aaddr setInitTable; /* Set init table address */ + Aaddr start; /* Address to Start code */ + Aword maximumScore; /* Maximum score */ + Aaddr scores; /* Score table */ + Aint scoreCount; /* Max index into scores table */ + Aaddr sourceFileTable; /* Table of fpos/len for source filenames */ + Aaddr sourceLineTable; /* Table of available source lines to break on */ + Aaddr freq; /* Address to Char freq's for coding */ + Aword acdcrc; /* Checksum for acd code (excl. hdr) */ + Aword txtcrc; /* Checksum for text data file */ + Aaddr ifids; /* Address to IFIDS */ + Aaddr prompt; }; struct Pre3_0beta2Header { - /* Important info */ - char tag[4]; /* "ALAN" */ - char version[4]; /* Version of compiler */ - Aword uid; /* Unique id of the compiled game */ - Aword size; /* Size of ACD-file in Awords */ - /* Options */ - Abool pack; /* Is the text packed ? */ - Aword stringOffset; /* Offset to string data in game file */ - Aword pageLength; /* Length of a page */ - Aword pageWidth; /* and width */ - Aword debug; /* Option: debug */ - /* Data structures */ - Aaddr classTableAddress; /* Class table */ - Aword classMax; /* Number of classes */ - Aword entityClassId; - Aword thingClassId; - Aword objectClassId; - Aword locationClassId; - Aword actorClassId; - Aword literalClassId; - Aword integerClassId; - Aword stringClassId; - Aaddr instanceTableAddress; /* Instance table */ - Aword instanceMax; /* Highest number of an instance */ - Aword theHero; /* The hero instance code (id) */ - Aaddr containerTableAddress; - Aword containerMax; - Aaddr scriptTableAddress; - Aword scriptMax; - Aaddr eventTableAddress; - Aword eventMax; - Aaddr syntaxTableAddress; - Aaddr parameterMapAddress; - Aword syntaxMax; - Aaddr dictionary; - Aaddr verbTableAddress; - Aaddr ruleTableAddress; - Aaddr messageTableAddress; - /* Miscellaneous */ - Aint attributesAreaSize; /* Size of attribute data area in Awords */ - Aint maxParameters; /* Maximum number of parameters in any syntax */ - Aaddr stringInitTable; /* String init table address */ - Aaddr setInitTable; /* Set init table address */ - Aaddr start; /* Address to Start code */ - Aword maximumScore; /* Maximum score */ - Aaddr scores; /* Score table */ - Aint scoreCount; /* Max index into scores table */ - Aaddr sourceFileTable; /* Table of fpos/len for source filenames */ - Aaddr sourceLineTable; /* Table of available source lines to break on */ - Aaddr freq; /* Address to Char freq's for coding */ - Aword acdcrc; /* Checksum for acd code (excl. hdr) */ - Aword txtcrc; /* Checksum for text data file */ - Aaddr ifids; /* Address to IFIDS */ + /* Important info */ + char tag[4]; /* "ALAN" */ + char version[4]; /* Version of compiler */ + Aword uid; /* Unique id of the compiled game */ + Aword size; /* Size of ACD-file in Awords */ + /* Options */ + Abool pack; /* Is the text packed ? */ + Aword stringOffset; /* Offset to string data in game file */ + Aword pageLength; /* Length of a page */ + Aword pageWidth; /* and width */ + Aword debug; /* Option: debug */ + /* Data structures */ + Aaddr classTableAddress; /* Class table */ + Aword classMax; /* Number of classes */ + Aword entityClassId; + Aword thingClassId; + Aword objectClassId; + Aword locationClassId; + Aword actorClassId; + Aword literalClassId; + Aword integerClassId; + Aword stringClassId; + Aaddr instanceTableAddress; /* Instance table */ + Aword instanceMax; /* Highest number of an instance */ + Aword theHero; /* The hero instance code (id) */ + Aaddr containerTableAddress; + Aword containerMax; + Aaddr scriptTableAddress; + Aword scriptMax; + Aaddr eventTableAddress; + Aword eventMax; + Aaddr syntaxTableAddress; + Aaddr parameterMapAddress; + Aword syntaxMax; + Aaddr dictionary; + Aaddr verbTableAddress; + Aaddr ruleTableAddress; + Aaddr messageTableAddress; + /* Miscellaneous */ + Aint attributesAreaSize; /* Size of attribute data area in Awords */ + Aint maxParameters; /* Maximum number of parameters in any syntax */ + Aaddr stringInitTable; /* String init table address */ + Aaddr setInitTable; /* Set init table address */ + Aaddr start; /* Address to Start code */ + Aword maximumScore; /* Maximum score */ + Aaddr scores; /* Score table */ + Aint scoreCount; /* Max index into scores table */ + Aaddr sourceFileTable; /* Table of fpos/len for source filenames */ + Aaddr sourceLineTable; /* Table of available source lines to break on */ + Aaddr freq; /* Address to Char freq's for coding */ + Aword acdcrc; /* Checksum for acd code (excl. hdr) */ + Aword txtcrc; /* Checksum for text data file */ + Aaddr ifids; /* Address to IFIDS */ }; struct Pre3_0alpha5Header { - /* Important info */ - char tag[4]; /* "ALAN" */ - char version[4]; /* Version of compiler */ - Aword uid; /* Unique id of the compiled game */ - Aword size; /* Size of ACD-file in Awords */ - /* Options */ - Abool pack; /* Is the text packed ? */ - Aword stringOffset; /* Offset to string data in game file */ - Aword pageLength; /* Length of a page */ - Aword pageWidth; /* and width */ - Aword debug; /* Option: debug */ - /* Data structures */ - Aaddr classTableAddress; /* Class table */ - Aword classMax; /* Number of classes */ - Aword entityClassId; - Aword thingClassId; - Aword objectClassId; - Aword locationClassId; - Aword actorClassId; - Aword literalClassId; - Aword integerClassId; - Aword stringClassId; - Aaddr instanceTableAddress; /* Instance table */ - Aword instanceMax; /* Highest number of an instance */ - Aword theHero; /* The hero instance code (id) */ - Aaddr containerTableAddress; - Aword containerMax; - Aaddr scriptTableAddress; - Aword scriptMax; - Aaddr eventTableAddress; - Aword eventMax; - Aaddr syntaxTableAddress; - Aaddr parameterMapAddress; - Aword syntaxMax; - Aaddr dictionary; - Aaddr verbTableAddress; - Aaddr ruleTableAddress; - Aaddr messageTableAddress; - /* Miscellaneous */ - Aint attributesAreaSize; /* Size of attribute data area in Awords */ - Aint maxParameters; /* Maximum number of parameters in any syntax */ - Aaddr stringInitTable; /* String init table address */ - Aaddr setInitTable; /* Set init table address */ - Aaddr start; /* Address to Start code */ - Aword maximumScore; /* Maximum score */ - Aaddr scores; /* Score table */ - Aint scoreCount; /* Max index into scores table */ - Aaddr sourceFileTable; /* Table of fpos/len for source filenames */ - Aaddr sourceLineTable; /* Table of available source lines to break on */ - Aaddr freq; /* Address to Char freq's for coding */ - Aword acdcrc; /* Checksum for acd code (excl. hdr) */ - Aword txtcrc; /* Checksum for text data file */ + /* Important info */ + char tag[4]; /* "ALAN" */ + char version[4]; /* Version of compiler */ + Aword uid; /* Unique id of the compiled game */ + Aword size; /* Size of ACD-file in Awords */ + /* Options */ + Abool pack; /* Is the text packed ? */ + Aword stringOffset; /* Offset to string data in game file */ + Aword pageLength; /* Length of a page */ + Aword pageWidth; /* and width */ + Aword debug; /* Option: debug */ + /* Data structures */ + Aaddr classTableAddress; /* Class table */ + Aword classMax; /* Number of classes */ + Aword entityClassId; + Aword thingClassId; + Aword objectClassId; + Aword locationClassId; + Aword actorClassId; + Aword literalClassId; + Aword integerClassId; + Aword stringClassId; + Aaddr instanceTableAddress; /* Instance table */ + Aword instanceMax; /* Highest number of an instance */ + Aword theHero; /* The hero instance code (id) */ + Aaddr containerTableAddress; + Aword containerMax; + Aaddr scriptTableAddress; + Aword scriptMax; + Aaddr eventTableAddress; + Aword eventMax; + Aaddr syntaxTableAddress; + Aaddr parameterMapAddress; + Aword syntaxMax; + Aaddr dictionary; + Aaddr verbTableAddress; + Aaddr ruleTableAddress; + Aaddr messageTableAddress; + /* Miscellaneous */ + Aint attributesAreaSize; /* Size of attribute data area in Awords */ + Aint maxParameters; /* Maximum number of parameters in any syntax */ + Aaddr stringInitTable; /* String init table address */ + Aaddr setInitTable; /* Set init table address */ + Aaddr start; /* Address to Start code */ + Aword maximumScore; /* Maximum score */ + Aaddr scores; /* Score table */ + Aint scoreCount; /* Max index into scores table */ + Aaddr sourceFileTable; /* Table of fpos/len for source filenames */ + Aaddr sourceLineTable; /* Table of available source lines to break on */ + Aaddr freq; /* Address to Char freq's for coding */ + Aword acdcrc; /* Checksum for acd code (excl. hdr) */ + Aword txtcrc; /* Checksum for text data file */ }; /* Error message numbers */ enum MsgKind { - M_UNKNOWN_WORD, - M_WHAT, - M_WHAT_WORD, - M_MULTIPLE, - M_NOUN, - M_AFTER_BUT, - M_BUT_ALL, - M_NOT_MUCH, - M_WHICH_ONE_START, - M_WHICH_ONE_COMMA, - M_WHICH_ONE_OR, - M_NO_SUCH, - M_NO_WAY, - M_CANT0, - M_SEE_START, - M_SEE_COMMA, - M_SEE_AND, - M_SEE_END, - M_CONTAINS, - M_CARRIES, - M_CONTAINS_COMMA, - M_CONTAINS_AND, - M_CONTAINS_END, - M_EMPTY, - M_EMPTYHANDED, - M_CANNOTCONTAIN, - M_SCORE, - M_MORE, - M_AGAIN, - M_SAVEWHERE, - M_SAVEOVERWRITE, - M_SAVEFAILED, - M_RESTOREFROM, - M_SAVEMISSING, - M_NOTASAVEFILE, - M_SAVEVERS, - M_SAVENAME, - M_REALLY, - M_QUITACTION, - M_UNDONE, - M_NO_UNDO, - M_WHICH_PRONOUN_START, - M_WHICH_PRONOUN_FIRST, + M_UNKNOWN_WORD, + M_WHAT, + M_WHAT_WORD, + M_MULTIPLE, + M_NOUN, + M_AFTER_BUT, + M_BUT_ALL, + M_NOT_MUCH, + M_WHICH_ONE_START, + M_WHICH_ONE_COMMA, + M_WHICH_ONE_OR, + M_NO_SUCH, + M_NO_WAY, + M_CANT0, + M_SEE_START, + M_SEE_COMMA, + M_SEE_AND, + M_SEE_END, + M_CONTAINS, + M_CARRIES, + M_CONTAINS_COMMA, + M_CONTAINS_AND, + M_CONTAINS_END, + M_EMPTY, + M_EMPTYHANDED, + M_CANNOTCONTAIN, + M_SCORE, + M_MORE, + M_AGAIN, + M_SAVEWHERE, + M_SAVEOVERWRITE, + M_SAVEFAILED, + M_RESTOREFROM, + M_SAVEMISSING, + M_NOTASAVEFILE, + M_SAVEVERS, + M_SAVENAME, + M_REALLY, + M_QUITACTION, + M_UNDONE, + M_NO_UNDO, + M_WHICH_PRONOUN_START, + M_WHICH_PRONOUN_FIRST, M_IMPOSSIBLE_WITH, - M_CONTAINMENT_LOOP, - M_CONTAINMENT_LOOP2, - MSGMAX + M_CONTAINMENT_LOOP, + M_CONTAINMENT_LOOP2, + MSGMAX }; #define NO_MSG MSGMAX diff --git a/engines/glk/alan3/act.cpp b/engines/glk/alan3/act.cpp index cc1fe92fc3..1b92234397 100644 --- a/engines/glk/alan3/act.cpp +++ b/engines/glk/alan3/act.cpp @@ -32,93 +32,91 @@ namespace Glk { namespace Alan3 { /*----------------------------------------------------------------------*/ -static void executeCommand(int verb, Parameter parameters[]) -{ - static AltInfo *altInfos = NULL; /* Need to survive lots of different exits...*/ - int altIndex; - - /* Did we leave anything behind last time... */ - if (altInfos != NULL) - free(altInfos); - - altInfos = findAllAlternatives(verb, parameters); - - if (anyCheckFailed(altInfos, EXECUTE_CHECK_BODY_ON_FAIL)) - return; - - /* Check for anything to execute... */ - if (!anythingToExecute(altInfos)) - error(M_CANT0); - - /* Now perform actions! First try any BEFORE or ONLY from inside out */ - for (altIndex = lastAltInfoIndex(altInfos); altIndex >= 0; altIndex--) { - if (altInfos[altIndex].alt != 0) // TODO Can this ever be NULL? Why? - if (altInfos[altIndex].alt->qual == (Aword)Q_BEFORE - || altInfos[altIndex].alt->qual == (Aword)Q_ONLY) { - if (!executedOk(&altInfos[altIndex])) - abortPlayerCommand(); - if (altInfos[altIndex].alt->qual == (Aword)Q_ONLY) - return; - } - } - - /* Then execute any not declared as AFTER, i.e. the default */ - for (altIndex = 0; !altInfos[altIndex].end; altIndex++) { - if (altInfos[altIndex].alt != 0) - if (altInfos[altIndex].alt->qual != (Aword)Q_AFTER) - if (!executedOk(&altInfos[altIndex])) - abortPlayerCommand(); - } - - /* Finally, the ones declared as AFTER */ - for (altIndex = lastAltInfoIndex(altInfos); altIndex >= 0; altIndex--) { - if (altInfos[altIndex].alt != 0) - if (!executedOk(&altInfos[altIndex])) - abortPlayerCommand(); - } +static void executeCommand(int verb, Parameter parameters[]) { + static AltInfo *altInfos = NULL; /* Need to survive lots of different exits...*/ + int altIndex; + + /* Did we leave anything behind last time... */ + if (altInfos != NULL) + free(altInfos); + + altInfos = findAllAlternatives(verb, parameters); + + if (anyCheckFailed(altInfos, EXECUTE_CHECK_BODY_ON_FAIL)) + return; + + /* Check for anything to execute... */ + if (!anythingToExecute(altInfos)) + error(M_CANT0); + + /* Now perform actions! First try any BEFORE or ONLY from inside out */ + for (altIndex = lastAltInfoIndex(altInfos); altIndex >= 0; altIndex--) { + if (altInfos[altIndex].alt != 0) // TODO Can this ever be NULL? Why? + if (altInfos[altIndex].alt->qual == (Aword)Q_BEFORE + || altInfos[altIndex].alt->qual == (Aword)Q_ONLY) { + if (!executedOk(&altInfos[altIndex])) + abortPlayerCommand(); + if (altInfos[altIndex].alt->qual == (Aword)Q_ONLY) + return; + } + } + + /* Then execute any not declared as AFTER, i.e. the default */ + for (altIndex = 0; !altInfos[altIndex].end; altIndex++) { + if (altInfos[altIndex].alt != 0) + if (altInfos[altIndex].alt->qual != (Aword)Q_AFTER) + if (!executedOk(&altInfos[altIndex])) + abortPlayerCommand(); + } + + /* Finally, the ones declared as AFTER */ + for (altIndex = lastAltInfoIndex(altInfos); altIndex >= 0; altIndex--) { + if (altInfos[altIndex].alt != 0) + if (!executedOk(&altInfos[altIndex])) + abortPlayerCommand(); + } } /*====================================================================== - + action() - + Execute the command. Handles acting on multiple items such as ALL, THEM or lists of objects. - + */ -void action(int verb, Parameter parameters[], Parameter multipleMatches[]) -{ - int multiplePosition; +void action(int verb, Parameter parameters[], Parameter multipleMatches[]) { + int multiplePosition; #ifdef TODO char marker[10]; #endif - multiplePosition = findMultiplePosition(parameters); - if (multiplePosition != -1) { + multiplePosition = findMultiplePosition(parameters); + if (multiplePosition != -1) { #ifdef TODO jmp_buf savedReturnLabel; - memcpy(savedReturnLabel, returnLabel, sizeof(returnLabel)); - sprintf(marker, "($%d)", multiplePosition+1); /* Prepare a printout with $1/2/3 */ - for (int i = 0; !isEndOfArray(&multipleMatches[i]); i++) { - copyParameter(¶meters[multiplePosition], &multipleMatches[i]); - setGlobalParameters(parameters); /* Need to do this here since the marker use them */ - output(marker); - // TODO: if execution for one parameter aborts we should return here, not to top level - if (setjmp(returnLabel) == NO_JUMP_RETURN) - executeCommand(verb, parameters); - if (multipleMatches[i+1].instance != EOD) - para(); - } - memcpy(returnLabel, savedReturnLabel, sizeof(returnLabel)); - parameters[multiplePosition].instance = 0; + memcpy(savedReturnLabel, returnLabel, sizeof(returnLabel)); + sprintf(marker, "($%d)", multiplePosition + 1); /* Prepare a printout with $1/2/3 */ + for (int i = 0; !isEndOfArray(&multipleMatches[i]); i++) { + copyParameter(¶meters[multiplePosition], &multipleMatches[i]); + setGlobalParameters(parameters); /* Need to do this here since the marker use them */ + output(marker); + // TODO: if execution for one parameter aborts we should return here, not to top level + if (setjmp(returnLabel) == NO_JUMP_RETURN) + executeCommand(verb, parameters); + if (multipleMatches[i + 1].instance != EOD) + para(); + } + memcpy(returnLabel, savedReturnLabel, sizeof(returnLabel)); + parameters[multiplePosition].instance = 0; #else ::error("TODO: action"); #endif - } else { - setGlobalParameters(parameters); - executeCommand(verb, parameters); - } + } else { + setGlobalParameters(parameters); + executeCommand(verb, parameters); + } } diff --git a/engines/glk/alan3/actor.cpp b/engines/glk/alan3/actor.cpp index 5152305033..ff4d89f9a1 100644 --- a/engines/glk/alan3/actor.cpp +++ b/engines/glk/alan3/actor.cpp @@ -33,49 +33,48 @@ namespace Alan3 { /*======================================================================*/ ScriptEntry *scriptOf(int actor) { - ScriptEntry *scr; + ScriptEntry *scr; - if (admin[actor].script != 0) { - for (scr = (ScriptEntry *) pointerTo(header->scriptTableAddress); !isEndOfArray(scr); scr++) - if (scr->code == admin[actor].script) - break; - if (!isEndOfArray(scr)) - return scr; - } - return NULL; + if (admin[actor].script != 0) { + for (scr = (ScriptEntry *) pointerTo(header->scriptTableAddress); !isEndOfArray(scr); scr++) + if (scr->code == admin[actor].script) + break; + if (!isEndOfArray(scr)) + return scr; + } + return NULL; } /*======================================================================*/ StepEntry *stepOf(int actor) { - StepEntry *step; - ScriptEntry *scr = scriptOf(actor); + StepEntry *step; + ScriptEntry *scr = scriptOf(actor); - if (scr == NULL) return NULL; + if (scr == NULL) return NULL; - step = (StepEntry*)pointerTo(scr->steps); - step = &step[admin[actor].step]; + step = (StepEntry *)pointerTo(scr->steps); + step = &step[admin[actor].step]; - return step; + return step; } /*======================================================================*/ -void describeActor(int actor) -{ - ScriptEntry *script = scriptOf(actor); +void describeActor(int actor) { + ScriptEntry *script = scriptOf(actor); - if (script != NULL && script->description != 0) - interpret(script->description); - else if (hasDescription(actor)) - describeAnything(actor); - else { - printMessageWithInstanceParameter(M_SEE_START, actor); - printMessage(M_SEE_END); - if (instances[actor].container != 0) - describeContainer(actor); - } - admin[actor].alreadyDescribed = TRUE; + if (script != NULL && script->description != 0) + interpret(script->description); + else if (hasDescription(actor)) + describeAnything(actor); + else { + printMessageWithInstanceParameter(M_SEE_START, actor); + printMessage(M_SEE_END); + if (instances[actor].container != 0) + describeContainer(actor); + } + admin[actor].alreadyDescribed = TRUE; } } // End of namespace Alan3 diff --git a/engines/glk/alan3/alan3.cpp b/engines/glk/alan3/alan3.cpp index c7c4c6c48e..4dd91919ac 100644 --- a/engines/glk/alan3/alan3.cpp +++ b/engines/glk/alan3/alan3.cpp @@ -41,7 +41,7 @@ namespace Alan3 { Alan3 *g_vm = nullptr; Alan3::Alan3(OSystem *syst, const GlkGameDescription &gameDesc) : GlkAPI(syst, gameDesc), - vm_exited_cleanly(false), _restartFlag(false), _saveSlot(-1), _pendingLook(false) { + vm_exited_cleanly(false), _restartFlag(false), _saveSlot(-1), _pendingLook(false) { g_vm = this; // txtfil = nullptr; // logfil = nullptr; @@ -86,42 +86,42 @@ bool Alan3::initialize() { glkStatusWin = g_vm->glk_window_open(glkMainWin, winmethod_Above | winmethod_Fixed, 1, wintype_TextGrid, 0); g_vm->glk_set_window(glkMainWin); -/* - // Set up the code file to point to the already opened game file - codfil = &_gameFile; - strncpy(codfnm, getFilename().c_str(), 255); - codfnm[255] = '\0'; - - if (_gameFile.size() < 8) { - GUIErrorMessage(_("This is too short to be a valid Alan3 file.")); - return false; - } - - if (_gameFile.readUint32BE() != MKTAG(2, 8, 1, 0)) { - GUIErrorMessage(_("This is not a valid Alan3 file.")); - return false; - } - - // Open up the text file - txtfil = new Common::File(); - if (!txtfil->open(Common::String::format("%s.dat", _advName.c_str()))) { - GUIErrorMessage("Could not open adventure text data file"); - delete txtfil; - return false; - } - - // Check for a save being loaded directly from the launcher - _saveSlot = ConfMan.hasKey("save_slot") ? ConfMan.getInt("save_slot") : -1; -*/ + /* + // Set up the code file to point to the already opened game file + codfil = &_gameFile; + strncpy(codfnm, getFilename().c_str(), 255); + codfnm[255] = '\0'; + + if (_gameFile.size() < 8) { + GUIErrorMessage(_("This is too short to be a valid Alan3 file.")); + return false; + } + + if (_gameFile.readUint32BE() != MKTAG(2, 8, 1, 0)) { + GUIErrorMessage(_("This is not a valid Alan3 file.")); + return false; + } + + // Open up the text file + txtfil = new Common::File(); + if (!txtfil->open(Common::String::format("%s.dat", _advName.c_str()))) { + GUIErrorMessage("Could not open adventure text data file"); + delete txtfil; + return false; + } + + // Check for a save being loaded directly from the launcher + _saveSlot = ConfMan.hasKey("save_slot") ? ConfMan.getInt("save_slot") : -1; + */ return true; } void Alan3::deinitialize() { free(memory); -/* - delete txtfil; - delete logfil; -*/ + /* + delete txtfil; + delete logfil; + */ } Common::Error Alan3::readSaveData(Common::SeekableReadStream *rs) { diff --git a/engines/glk/alan3/alan3.h b/engines/glk/alan3/alan3.h index ea8548a080..f4bb7a5ddf 100644 --- a/engines/glk/alan3/alan3.h +++ b/engines/glk/alan3/alan3.h @@ -68,12 +68,16 @@ public: /** * Flag for the game to restart */ - void setRestart(bool flag) { _restartFlag = flag; } + void setRestart(bool flag) { + _restartFlag = flag; + } /** * Returns whether the game should restart */ - bool shouldRestart() const { return _restartFlag; } + bool shouldRestart() const { + return _restartFlag; + } /** * Returns the running interpreter type diff --git a/engines/glk/alan3/alan_version.cpp b/engines/glk/alan3/alan_version.cpp index bb244ec23f..016d5cce79 100644 --- a/engines/glk/alan3/alan_version.cpp +++ b/engines/glk/alan3/alan_version.cpp @@ -26,16 +26,16 @@ namespace Glk { namespace Alan3 { const Product alan = { - "Alan", - "Adventure Language System", - "Alan 3.0beta6", - "Alan 3.0beta6 -- Adventure Language System (2017-09-08 10:18)", - "2017-09-08", - "10:18:25", - "Thomas", - "thoni64", - "cygwin32", - {"3.0beta6", 3, 0, 6, 1504858705, "beta"} + "Alan", + "Adventure Language System", + "Alan 3.0beta6", + "Alan 3.0beta6 -- Adventure Language System (2017-09-08 10:18)", + "2017-09-08", + "10:18:25", + "Thomas", + "thoni64", + "cygwin32", + {"3.0beta6", 3, 0, 6, 1504858705, "beta"} }; } // End of namespace Alan3 diff --git a/engines/glk/alan3/alt_info.cpp b/engines/glk/alan3/alt_info.cpp index 5a87cbb122..d9e8bd2004 100644 --- a/engines/glk/alan3/alt_info.cpp +++ b/engines/glk/alan3/alt_info.cpp @@ -44,337 +44,328 @@ typedef AltInfo *AltInfoFinder(int verb, Parameter parameters[]); /*======================================================================*/ -void primeAltInfo(AltInfo *altInfo, int level, int parameter, int instance, int cls) -{ - altInfo->level = level; - altInfo->parameter = parameter; - altInfo->instance = instance; - altInfo->_class = cls; - altInfo->done = FALSE; - altInfo->end = FALSE; +void primeAltInfo(AltInfo *altInfo, int level, int parameter, int instance, int cls) { + altInfo->level = level; + altInfo->parameter = parameter; + altInfo->instance = instance; + altInfo->_class = cls; + altInfo->done = FALSE; + altInfo->end = FALSE; } /*----------------------------------------------------------------------*/ -static void traceInstanceAndItsClass(Aid instance, Aid cls) -{ - traceSay(instance); - printf("[%d]", instance); - if (cls != NO_CLASS) - printf(", inherited from %s[%d]", idOfClass(cls), cls); +static void traceInstanceAndItsClass(Aid instance, Aid cls) { + traceSay(instance); + printf("[%d]", instance); + if (cls != NO_CLASS) + printf(", inherited from %s[%d]", idOfClass(cls), cls); } /*----------------------------------------------------------------------*/ static void traceAltInfo(AltInfo *alt) { - switch (alt->level) { - case GLOBAL_LEVEL: - printf("GLOBAL"); - break; - case LOCATION_LEVEL: - printf("in (location) "); - traceInstanceAndItsClass(current.location, alt->_class); - break; - case PARAMETER_LEVEL: { + switch (alt->level) { + case GLOBAL_LEVEL: + printf("GLOBAL"); + break; + case LOCATION_LEVEL: + printf("in (location) "); + traceInstanceAndItsClass(current.location, alt->_class); + break; + case PARAMETER_LEVEL: { char *parameterName = parameterNameInSyntax(current.verb, alt->parameter); if (parameterName != NULL) printf("in parameter %s(#%d)=", parameterName, alt->parameter); else printf("in parameter #%d=", alt->parameter); - traceInstanceAndItsClass(globalParameters[alt->parameter-1].instance, alt->_class); - break; + traceInstanceAndItsClass(globalParameters[alt->parameter - 1].instance, alt->_class); + break; + } } - } } /*----------------------------------------------------------------------*/ -static void traceVerbCheck(AltInfo *alt, bool execute) -{ - if (traceSectionOption && execute) { - printf("\n<VERB %d, ", current.verb); - traceAltInfo(alt); - printf(", CHECK:>\n"); - } +static void traceVerbCheck(AltInfo *alt, bool execute) { + if (traceSectionOption && execute) { + printf("\n<VERB %d, ", current.verb); + traceAltInfo(alt); + printf(", CHECK:>\n"); + } } /*======================================================================*/ -bool checkFailed(AltInfo *altInfo, bool execute) -{ - if (altInfo->alt != NULL && altInfo->alt->checks != 0) { - traceVerbCheck(altInfo, execute); - // TODO Why does this not generate a regression error with ! - // Need a new regression case? - fail = FALSE; - if (checksFailed(altInfo->alt->checks, execute)) return TRUE; - if (fail) return TRUE; - } - return FALSE; +bool checkFailed(AltInfo *altInfo, bool execute) { + if (altInfo->alt != NULL && altInfo->alt->checks != 0) { + traceVerbCheck(altInfo, execute); + // TODO Why does this not generate a regression error with ! + // Need a new regression case? + fail = FALSE; + if (checksFailed(altInfo->alt->checks, execute)) return TRUE; + if (fail) return TRUE; + } + return FALSE; } /*----------------------------------------------------------------------*/ -static void traceVerbExecution(AltInfo *alt) -{ - if (traceSectionOption) { - printf("\n<VERB %d, ", current.verb); - traceAltInfo(alt); - printf(", DOES"); - switch (alt->alt->qual) { - case Q_BEFORE: printf(" (BEFORE)"); - break; - case Q_ONLY: printf(" (ONLY)"); - break; - case Q_AFTER: printf(" (AFTER)"); - break; - case Q_DEFAULT: - break; - } - printf(":>\n"); - } +static void traceVerbExecution(AltInfo *alt) { + if (traceSectionOption) { + printf("\n<VERB %d, ", current.verb); + traceAltInfo(alt); + printf(", DOES"); + switch (alt->alt->qual) { + case Q_BEFORE: + printf(" (BEFORE)"); + break; + case Q_ONLY: + printf(" (ONLY)"); + break; + case Q_AFTER: + printf(" (AFTER)"); + break; + case Q_DEFAULT: + break; + } + printf(":>\n"); + } } /*======================================================================*/ -bool executedOk(AltInfo *altInfo) -{ - fail = FALSE; - if (!altInfo->done && altInfo->alt->action != 0) { - traceVerbExecution(altInfo); - current.instance = altInfo->instance; - interpret(altInfo->alt->action); - } - altInfo->done = TRUE; - return !fail; +bool executedOk(AltInfo *altInfo) { + fail = FALSE; + if (!altInfo->done && altInfo->alt->action != 0) { + traceVerbExecution(altInfo); + current.instance = altInfo->instance; + interpret(altInfo->alt->action); + } + altInfo->done = TRUE; + return !fail; } /*======================================================================*/ bool canBeExecuted(AltInfo *altInfo) { - return altInfo->alt != NULL && altInfo->alt->action != 0; + return altInfo->alt != NULL && altInfo->alt->action != 0; } /*======================================================================*/ AltInfo *duplicateAltInfoArray(AltInfo original[]) { - int size; - AltInfo *duplicate; - - for (size = 0; original[size].end != TRUE; size++) - ; - size++; - duplicate = (AltInfo *)allocate(size*sizeof(AltInfo)); - memcpy(duplicate, original, size*sizeof(AltInfo)); - return duplicate; + int size; + AltInfo *duplicate; + + for (size = 0; original[size].end != TRUE; size++) + ; + size++; + duplicate = (AltInfo *)allocate(size * sizeof(AltInfo)); + memcpy(duplicate, original, size * sizeof(AltInfo)); + return duplicate; } /*======================================================================*/ -int lastAltInfoIndex(AltInfo altInfo[]) -{ - int altIndex; - - /* Loop to last alternative */ - for (altIndex = -1; !altInfo[altIndex+1].end; altIndex++) - ; - return altIndex; +int lastAltInfoIndex(AltInfo altInfo[]) { + int altIndex; + + /* Loop to last alternative */ + for (altIndex = -1; !altInfo[altIndex + 1].end; altIndex++) + ; + return altIndex; } /*----------------------------------------------------------------------*/ static AltInfo *nextFreeAltInfo(AltInfoArray altInfos) { - return &altInfos[lastAltInfoIndex(altInfos)+1]; + return &altInfos[lastAltInfoIndex(altInfos) + 1]; } /*----------------------------------------------------------------------*/ static void addAlternative(AltInfoArray altInfos, int verb, int level, Aint parameterNumber, Aint theClass, Aid theInstance, AltEntryFinder finder) { - AltInfo *altInfoP = nextFreeAltInfo(altInfos); - - altInfoP->alt = (*finder)(verb, parameterNumber, theInstance, theClass); - if (altInfoP->alt != NULL) { - primeAltInfo(altInfoP, level, parameterNumber, theInstance, theClass); - altInfoP[1].end = TRUE; - } + AltInfo *altInfoP = nextFreeAltInfo(altInfos); + + altInfoP->alt = (*finder)(verb, parameterNumber, theInstance, theClass); + if (altInfoP->alt != NULL) { + primeAltInfo(altInfoP, level, parameterNumber, theInstance, theClass); + altInfoP[1].end = TRUE; + } } /*----------------------------------------------------------------------*/ -static void addGlobalAlternatives(AltInfoArray altInfos, int verb, AltEntryFinder finder ) { - addAlternative(altInfos, verb, GLOBAL_LEVEL, NO_PARAMETER, NO_CLASS, NO_INSTANCE, finder); +static void addGlobalAlternatives(AltInfoArray altInfos, int verb, AltEntryFinder finder) { + addAlternative(altInfos, verb, GLOBAL_LEVEL, NO_PARAMETER, NO_CLASS, NO_INSTANCE, finder); } /*----------------------------------------------------------------------*/ -static void addAlternativesFromParents(AltInfoArray altInfos, int verb, int level, Aint parameterNumber, Aint theClass, Aid theInstance, AltEntryFinder finder){ - if (classes[theClass].parent != 0) - addAlternativesFromParents(altInfos, verb, level, - parameterNumber, - classes[theClass].parent, - theInstance, - finder); - - addAlternative(altInfos, verb, level, parameterNumber, theClass, theInstance, finder); +static void addAlternativesFromParents(AltInfoArray altInfos, int verb, int level, Aint parameterNumber, Aint theClass, Aid theInstance, AltEntryFinder finder) { + if (classes[theClass].parent != 0) + addAlternativesFromParents(altInfos, verb, level, + parameterNumber, + classes[theClass].parent, + theInstance, + finder); + + addAlternative(altInfos, verb, level, parameterNumber, theClass, theInstance, finder); } /*----------------------------------------------------------------------*/ static void addAlternativesFromLocation(AltInfoArray altInfos, int verb, Aid location, AltEntryFinder finder) { - if (admin[location].location != 0) - addAlternativesFromLocation(altInfos, verb, admin[location].location, finder); - - addAlternativesFromParents(altInfos, verb, - LOCATION_LEVEL, - NO_PARAMETER, - instances[location].parent, - location, - finder); - - addAlternative(altInfos, verb, LOCATION_LEVEL, NO_PARAMETER, NO_CLASS, location, finder); + if (admin[location].location != 0) + addAlternativesFromLocation(altInfos, verb, admin[location].location, finder); + + addAlternativesFromParents(altInfos, verb, + LOCATION_LEVEL, + NO_PARAMETER, + instances[location].parent, + location, + finder); + + addAlternative(altInfos, verb, LOCATION_LEVEL, NO_PARAMETER, NO_CLASS, location, finder); } /*----------------------------------------------------------------------*/ static void addAlternativesFromParameter(AltInfoArray altInfos, int verb, Parameter parameters[], int parameterNumber, AltEntryFinder finder) { - Aid parent; - Aid theInstance = parameters[parameterNumber-1].instance; - - if (isLiteral(theInstance)) - parent = literals[literalFromInstance(theInstance)]._class; - else - parent = instances[theInstance].parent; - addAlternativesFromParents(altInfos, verb, PARAMETER_LEVEL, parameterNumber, parent, theInstance, finder); - - if (!isLiteral(theInstance)) - addAlternative(altInfos, verb, PARAMETER_LEVEL, parameterNumber, NO_CLASS, theInstance, finder); + Aid parent; + Aid theInstance = parameters[parameterNumber - 1].instance; + + if (isLiteral(theInstance)) + parent = literals[literalFromInstance(theInstance)]._class; + else + parent = instances[theInstance].parent; + addAlternativesFromParents(altInfos, verb, PARAMETER_LEVEL, parameterNumber, parent, theInstance, finder); + + if (!isLiteral(theInstance)) + addAlternative(altInfos, verb, PARAMETER_LEVEL, parameterNumber, NO_CLASS, theInstance, finder); } /*======================================================================*/ -bool anyCheckFailed(AltInfoArray altInfo, bool execute) -{ - int altIndex; - - if (altInfo != NULL) - for (altIndex = 0; !altInfo[altIndex].end; altIndex++) { - current.instance = altInfo[altIndex].instance; - if (checkFailed(&altInfo[altIndex], execute)) - return TRUE; - } - return FALSE; +bool anyCheckFailed(AltInfoArray altInfo, bool execute) { + int altIndex; + + if (altInfo != NULL) + for (altIndex = 0; !altInfo[altIndex].end; altIndex++) { + current.instance = altInfo[altIndex].instance; + if (checkFailed(&altInfo[altIndex], execute)) + return TRUE; + } + return FALSE; } /*======================================================================*/ -bool anythingToExecute(AltInfo altInfo[]) -{ - int altIndex; - - /* Check for anything to execute... */ - if (altInfo != NULL) - for (altIndex = 0; !altInfo[altIndex].end; altIndex++) - if (canBeExecuted(&altInfo[altIndex])) - return TRUE; - return FALSE; +bool anythingToExecute(AltInfo altInfo[]) { + int altIndex; + + /* Check for anything to execute... */ + if (altInfo != NULL) + for (altIndex = 0; !altInfo[altIndex].end; altIndex++) + if (canBeExecuted(&altInfo[altIndex])) + return TRUE; + return FALSE; } /*----------------------------------------------------------------------*/ static VerbEntry *findVerbEntry(int verbCode, VerbEntry *entries) { - VerbEntry *verbEntry; - for (verbEntry = entries; !isEndOfArray(verbEntry); verbEntry++) { - if (verbEntry->code < 0) { - /* Verb codes are negative for Meta verbs, if so they are also 1 off to avoid EOD */ - if (abs(verbEntry->code)-1 == verbCode) - return verbEntry; - } else { - if (verbEntry->code == verbCode) - return verbEntry; - } - } - return NULL; + VerbEntry *verbEntry; + for (verbEntry = entries; !isEndOfArray(verbEntry); verbEntry++) { + if (verbEntry->code < 0) { + /* Verb codes are negative for Meta verbs, if so they are also 1 off to avoid EOD */ + if (abs(verbEntry->code) - 1 == verbCode) + return verbEntry; + } else { + if (verbEntry->code == verbCode) + return verbEntry; + } + } + return NULL; } /*----------------------------------------------------------------------*/ -static AltEntry *findAlternative(Aaddr verbTableAddress, int verbCode, int parameterNumber) -{ - AltEntry *alt; - VerbEntry *verbEntry; - - if (verbTableAddress == 0) return NULL; - - verbEntry = findVerbEntry(verbCode, (VerbEntry *) pointerTo(verbTableAddress)); - if (verbEntry != NULL) - for (alt = (AltEntry *) pointerTo(verbEntry->alts); !isEndOfArray(alt); alt++) { - if (alt->param == parameterNumber || alt->param == 0) { - if (verbEntry->code < 0) current.meta = TRUE; - return alt; - } - } - return NULL; +static AltEntry *findAlternative(Aaddr verbTableAddress, int verbCode, int parameterNumber) { + AltEntry *alt; + VerbEntry *verbEntry; + + if (verbTableAddress == 0) return NULL; + + verbEntry = findVerbEntry(verbCode, (VerbEntry *) pointerTo(verbTableAddress)); + if (verbEntry != NULL) + for (alt = (AltEntry *) pointerTo(verbEntry->alts); !isEndOfArray(alt); alt++) { + if (alt->param == parameterNumber || alt->param == 0) { + if (verbEntry->code < 0) current.meta = TRUE; + return alt; + } + } + return NULL; } /*----------------------------------------------------------------------*/ -static AltEntry *alternativeFinder(int verb, int parameterNumber, int theInstance, int theClass) -{ - if (theClass != NO_CLASS) - return findAlternative(classes[theClass].verbs, verb, parameterNumber); - else if (theInstance != NO_INSTANCE) - return findAlternative(instances[theInstance].verbs, verb, parameterNumber); - else - return findAlternative(header->verbTableAddress, verb, parameterNumber); +static AltEntry *alternativeFinder(int verb, int parameterNumber, int theInstance, int theClass) { + if (theClass != NO_CLASS) + return findAlternative(classes[theClass].verbs, verb, parameterNumber); + else if (theInstance != NO_INSTANCE) + return findAlternative(instances[theInstance].verbs, verb, parameterNumber); + else + return findAlternative(header->verbTableAddress, verb, parameterNumber); } /*======================================================================*/ AltInfo *findAllAlternatives(int verb, Parameter parameters[]) { - int parameterNumber; - AltInfo altInfos[1000]; - altInfos[0].end = TRUE; - - addGlobalAlternatives(altInfos, verb, &alternativeFinder); - - addAlternativesFromLocation(altInfos, verb, current.location, &alternativeFinder); - - for (parameterNumber = 1; !isEndOfArray(¶meters[parameterNumber-1]); parameterNumber++) { - addAlternativesFromParameter(altInfos, verb, parameters, parameterNumber, &alternativeFinder); - } - return duplicateAltInfoArray(altInfos); + int parameterNumber; + AltInfo altInfos[1000]; + altInfos[0].end = TRUE; + + addGlobalAlternatives(altInfos, verb, &alternativeFinder); + + addAlternativesFromLocation(altInfos, verb, current.location, &alternativeFinder); + + for (parameterNumber = 1; !isEndOfArray(¶meters[parameterNumber - 1]); parameterNumber++) { + addAlternativesFromParameter(altInfos, verb, parameters, parameterNumber, &alternativeFinder); + } + return duplicateAltInfoArray(altInfos); } /*----------------------------------------------------------------------*/ static bool possibleWithFinder(int verb, Parameter parameters[], AltInfoFinder *finder) { - bool anything; - AltInfo *allAlternatives; + bool anything; + AltInfo *allAlternatives; - allAlternatives = finder(verb, parameters); + allAlternatives = finder(verb, parameters); - // TODO Need to do this since anyCheckFailed() call execute() which assumes the global parameters - setGlobalParameters(parameters); - if (anyCheckFailed(allAlternatives, DONT_EXECUTE_CHECK_BODY_ON_FAIL)) - anything = FALSE; - else - anything = anythingToExecute(allAlternatives); + // TODO Need to do this since anyCheckFailed() call execute() which assumes the global parameters + setGlobalParameters(parameters); + if (anyCheckFailed(allAlternatives, DONT_EXECUTE_CHECK_BODY_ON_FAIL)) + anything = FALSE; + else + anything = anythingToExecute(allAlternatives); - if (allAlternatives != NULL) - deallocate(allAlternatives); + if (allAlternatives != NULL) + deallocate(allAlternatives); - return(anything); + return (anything); } /*======================================================================*/ -bool possible(int verb, Parameter inParameters[], ParameterPosition parameterPositions[]) -{ - // This is a wrapper for possibleWithFinder() which is used in unit tests - // possible() should be used "for real". +bool possible(int verb, Parameter inParameters[], ParameterPosition parameterPositions[]) { + // This is a wrapper for possibleWithFinder() which is used in unit tests + // possible() should be used "for real". - return possibleWithFinder(verb, inParameters, findAllAlternatives); + return possibleWithFinder(verb, inParameters, findAllAlternatives); } } // End of namespace Alan3 diff --git a/engines/glk/alan3/alt_info.h b/engines/glk/alan3/alt_info.h index de99585018..2a7dd0c2f9 100644 --- a/engines/glk/alan3/alt_info.h +++ b/engines/glk/alan3/alt_info.h @@ -51,14 +51,14 @@ namespace Alan3 { /* Types */ struct AltInfo { - bool end; /* Indicator of end in AltInfoArray, first empty has TRUE here */ + bool end; /* Indicator of end in AltInfoArray, first empty has TRUE here */ AltEntry *alt; bool done; - Aint level; /* 0 - Global, 1 - location, 2 - parameter */ - Aid _class; /* In which class, only used for tracing */ - Aid instance; /* In which instance the Alternative was found, - used to set current.instance and tracing */ - Aid parameter; /* In which parameter, only used for tracing */ + Aint level; /* 0 - Global, 1 - location, 2 - parameter */ + Aid _class; /* In which class, only used for tracing */ + Aid instance; /* In which instance the Alternative was found, + used to set current.instance and tracing */ + Aid parameter; /* In which parameter, only used for tracing */ }; typedef AltEntry *(*AltEntryFinder)(int verb, int parameterNumber, int theInstance, int theClass); diff --git a/engines/glk/alan3/args.cpp b/engines/glk/alan3/args.cpp index 12cf1e885c..5ccf0fc134 100644 --- a/engines/glk/alan3/args.cpp +++ b/engines/glk/alan3/args.cpp @@ -40,119 +40,122 @@ char *adventureFileName; #if 0 /*======================================================================*/ char *gameName(char *fullPathName) { - char *foundGameName = ""; + char *foundGameName = ""; - if (fullPathName != NULL) { - foundGameName = strdup(baseNameStart(fullPathName)); - foundGameName[strlen(foundGameName)-4] = '\0'; /* Strip off .A3C */ - } + if (fullPathName != NULL) { + foundGameName = strdup(baseNameStart(fullPathName)); + foundGameName[strlen(foundGameName) - 4] = '\0'; /* Strip off .A3C */ + } - if (foundGameName[0] == '.' && foundGameName[1] == '/') - strcpy(foundGameName, &foundGameName[2]); + if (foundGameName[0] == '.' && foundGameName[1] == '/') + strcpy(foundGameName, &foundGameName[2]); - return foundGameName; + return foundGameName; } /*----------------------------------------------------------------------*/ static char *removeQuotes(char *argument) { - char *str = strdup(&argument[1]); - str[strlen(str)-1] = '\0'; - return str; + char *str = strdup(&argument[1]); + str[strlen(str) - 1] = '\0'; + return str; } /*----------------------------------------------------------------------*/ static bool isQuoted(char *argument) { - return argument[0] == '"' && strlen(argument) > 2; + return argument[0] == '"' && strlen(argument) > 2; } /*----------------------------------------------------------------------*/ static char *addAcodeExtension(char *advFilename) { - if (strlen(advFilename) < strlen(ACODEEXTENSION) - || compareStrings(&advFilename[strlen(advFilename)-4], ACODEEXTENSION) != 0) { - advFilename = (char *)realloc(advFilename, strlen(advFilename)+strlen(ACODEEXTENSION)+1); - strcat(advFilename, ACODEEXTENSION); - } - return advFilename; + if (strlen(advFilename) < strlen(ACODEEXTENSION) + || compareStrings(&advFilename[strlen(advFilename) - 4], ACODEEXTENSION) != 0) { + advFilename = (char *)realloc(advFilename, strlen(advFilename) + strlen(ACODEEXTENSION) + 1); + strcat(advFilename, ACODEEXTENSION); + } + return advFilename; } /*----------------------------------------------------------------------*/ -static void switches(int argc, char *argv[]) -{ - int i; - - for (i = 1; i < argc; i++) { - char *argument = argv[i]; - - if (argument[0] == '-') { - switch (toLower(argument[1])) - { - case 'i': - ignoreErrorOption = TRUE; - break; - case 't': - traceSectionOption = TRUE; - switch (argument[2]) { - case '9': - case '8': - case '7': - case '6': - case '5' : traceStackOption = TRUE; - case '4' : tracePushOption = TRUE; - case '3' : traceInstructionOption = TRUE; - case '2' : traceSourceOption = TRUE; - case '\0': - case '1': traceSectionOption = TRUE; - } - break; - case 'd': - debugOption = TRUE; - break; - case 'l': - transcriptOption = TRUE; - logOption = FALSE; - break; - case 'v': - verboseOption = TRUE; - break; - case 'n': - statusLineOption = FALSE; - break; - case 'c': - logOption = TRUE; - transcriptOption = FALSE; - break; - case 'r': - regressionTestOption = TRUE; - break; - default: - printf("Unrecognized switch, -%c\n", argument[1]); - usage(argv[0]); - terminate(0); - } - } else { - - if (isQuoted(argument)) - adventureFileName = removeQuotes(argument); - else - adventureFileName = strdup(argument); - - adventureFileName = addAcodeExtension(adventureFileName); - - adventureName = gameName(adventureFileName); - - } - } +static void switches(int argc, char *argv[]) { + int i; + + for (i = 1; i < argc; i++) { + char *argument = argv[i]; + + if (argument[0] == '-') { + switch (toLower(argument[1])) { + case 'i': + ignoreErrorOption = TRUE; + break; + case 't': + traceSectionOption = TRUE; + switch (argument[2]) { + case '9': + case '8': + case '7': + case '6': + case '5' : + traceStackOption = TRUE; + case '4' : + tracePushOption = TRUE; + case '3' : + traceInstructionOption = TRUE; + case '2' : + traceSourceOption = TRUE; + case '\0': + case '1': + traceSectionOption = TRUE; + } + break; + case 'd': + debugOption = TRUE; + break; + case 'l': + transcriptOption = TRUE; + logOption = FALSE; + break; + case 'v': + verboseOption = TRUE; + break; + case 'n': + statusLineOption = FALSE; + break; + case 'c': + logOption = TRUE; + transcriptOption = FALSE; + break; + case 'r': + regressionTestOption = TRUE; + break; + default: + printf("Unrecognized switch, -%c\n", argument[1]); + usage(argv[0]); + terminate(0); + } + } else { + + if (isQuoted(argument)) + adventureFileName = removeQuotes(argument); + else + adventureFileName = strdup(argument); + + adventureFileName = addAcodeExtension(adventureFileName); + + adventureName = gameName(adventureFileName); + + } + } } #endif /*----------------------------------------------------------------------*/ bool differentInterpreterName(char *string) { - return strcasecmp(string, PROGNAME) != 0; + return strcasecmp(string, PROGNAME) != 0; } } // End of namespace Alan3 diff --git a/engines/glk/alan3/attribute.cpp b/engines/glk/alan3/attribute.cpp index 1e5a6dd2db..f46758542c 100644 --- a/engines/glk/alan3/attribute.cpp +++ b/engines/glk/alan3/attribute.cpp @@ -29,34 +29,31 @@ namespace Glk { namespace Alan3 { /*----------------------------------------------------------------------*/ -static AttributeEntry *findAttribute(AttributeEntry *attributeTable, int attributeCode) -{ - AttributeEntry *attribute = attributeTable; - while (attribute->code != attributeCode) { - attribute++; - if (isEndOfArray(attribute)) - syserr("Attribute not found."); - } - return attribute; +static AttributeEntry *findAttribute(AttributeEntry *attributeTable, int attributeCode) { + AttributeEntry *attribute = attributeTable; + while (attribute->code != attributeCode) { + attribute++; + if (isEndOfArray(attribute)) + syserr("Attribute not found."); + } + return attribute; } /*======================================================================*/ -Aptr getAttribute(AttributeEntry *attributeTable, int attributeCode) -{ - AttributeEntry *attribute = findAttribute(attributeTable, attributeCode); +Aptr getAttribute(AttributeEntry *attributeTable, int attributeCode) { + AttributeEntry *attribute = findAttribute(attributeTable, attributeCode); - return attribute->value; + return attribute->value; } /*======================================================================*/ -void setAttribute(AttributeEntry *attributeTable, int attributeCode, Aptr newValue) -{ - AttributeEntry *attribute = findAttribute(attributeTable, attributeCode); +void setAttribute(AttributeEntry *attributeTable, int attributeCode, Aptr newValue) { + AttributeEntry *attribute = findAttribute(attributeTable, attributeCode); - attribute->value = newValue; - gameStateChanged = TRUE; + attribute->value = newValue; + gameStateChanged = TRUE; } } // End of namespace Alan3 diff --git a/engines/glk/alan3/checkentry.cpp b/engines/glk/alan3/checkentry.cpp index 51a839cbd6..6a49128d71 100644 --- a/engines/glk/alan3/checkentry.cpp +++ b/engines/glk/alan3/checkentry.cpp @@ -29,24 +29,23 @@ namespace Glk { namespace Alan3 { /*======================================================================*/ -bool checksFailed(Aaddr adr, bool execute) -{ - CheckEntry *chk = (CheckEntry *) pointerTo(adr); - if (chk->exp == 0) { - if (execute == EXECUTE_CHECK_BODY_ON_FAIL) - interpret(chk->stms); - return TRUE; - } else { - while (!isEndOfArray(chk)) { - if (!evaluate(chk->exp)) { - if (execute == EXECUTE_CHECK_BODY_ON_FAIL) - interpret(chk->stms); - return TRUE; - } - chk++; - } - return FALSE; - } +bool checksFailed(Aaddr adr, bool execute) { + CheckEntry *chk = (CheckEntry *) pointerTo(adr); + if (chk->exp == 0) { + if (execute == EXECUTE_CHECK_BODY_ON_FAIL) + interpret(chk->stms); + return TRUE; + } else { + while (!isEndOfArray(chk)) { + if (!evaluate(chk->exp)) { + if (execute == EXECUTE_CHECK_BODY_ON_FAIL) + interpret(chk->stms); + return TRUE; + } + chk++; + } + return FALSE; + } } } // End of namespace Alan3 diff --git a/engines/glk/alan3/checkentry.h b/engines/glk/alan3/checkentry.h index a39558f51a..f5bcff197d 100644 --- a/engines/glk/alan3/checkentry.h +++ b/engines/glk/alan3/checkentry.h @@ -38,8 +38,8 @@ namespace Alan3 { /* TYPES */ struct CheckEntry { /* CHECK TABLE */ - Aaddr exp; /* ACODE address to expression code */ - Aaddr stms; /* ACODE address to statement code */ + Aaddr exp; /* ACODE address to expression code */ + Aaddr stms; /* ACODE address to statement code */ }; diff --git a/engines/glk/alan3/class.cpp b/engines/glk/alan3/class.cpp index b23d8ac3f4..12d7d434f9 100644 --- a/engines/glk/alan3/class.cpp +++ b/engines/glk/alan3/class.cpp @@ -31,7 +31,7 @@ ClassEntry *classes; /* Class table pointer */ /*======================================================================*/ char *idOfClass(int theClass) { - return (char *)pointerTo(classes[theClass].id); + return (char *)pointerTo(classes[theClass].id); } } // End of namespace Alan3 diff --git a/engines/glk/alan3/class.h b/engines/glk/alan3/class.h index a6ff071d9c..9895a1c7ac 100644 --- a/engines/glk/alan3/class.h +++ b/engines/glk/alan3/class.h @@ -29,7 +29,7 @@ namespace Glk { namespace Alan3 { /* DATA */ -extern ClassEntry *classes; /* Class table pointer */ +extern ClassEntry *classes; /* Class table pointer */ /* FUNCTIONS */ extern char *idOfClass(int theClass); diff --git a/engines/glk/alan3/compatibility.cpp b/engines/glk/alan3/compatibility.cpp index a8efb47a09..4668d6ea82 100644 --- a/engines/glk/alan3/compatibility.cpp +++ b/engines/glk/alan3/compatibility.cpp @@ -27,42 +27,42 @@ namespace Alan3 { /*----------------------------------------------------------------------*/ static bool is3_0Alpha(char version[]) { - return version[3] == 3 && version[2] == 0 && version[0] == 'a'; + return version[3] == 3 && version[2] == 0 && version[0] == 'a'; } /*----------------------------------------------------------------------*/ static bool is3_0Beta(char version[]) { - return version[3] == 3 && version[2] == 0 && version[0] == 'b'; + return version[3] == 3 && version[2] == 0 && version[0] == 'b'; } /*----------------------------------------------------------------------*/ static int correction(char version[]) { - return version[1]; + return version[1]; } /*======================================================================*/ bool isPreAlpha5(char version[4]) { - return is3_0Alpha(version) && correction(version) <5; + return is3_0Alpha(version) && correction(version) < 5; } /*======================================================================*/ bool isPreBeta2(char version[4]) { - return is3_0Alpha(version) || (is3_0Beta(version) && correction(version) == 1); + return is3_0Alpha(version) || (is3_0Beta(version) && correction(version) == 1); } /*======================================================================*/ bool isPreBeta3(char version[4]) { - return is3_0Alpha(version) || (is3_0Beta(version) && correction(version) <= 2); + return is3_0Alpha(version) || (is3_0Beta(version) && correction(version) <= 2); } /*======================================================================*/ bool isPreBeta4(char version[4]) { - return is3_0Alpha(version) || (is3_0Beta(version) && correction(version) <= 3); + return is3_0Alpha(version) || (is3_0Beta(version) && correction(version) <= 3); } /*======================================================================*/ bool isPreBeta5(char version[4]) { - return is3_0Alpha(version) || (is3_0Beta(version) && correction(version) <= 4); + return is3_0Alpha(version) || (is3_0Beta(version) && correction(version) <= 4); } } // End of namespace Alan3 diff --git a/engines/glk/alan3/container.cpp b/engines/glk/alan3/container.cpp index 365455c0ce..36469a336c 100644 --- a/engines/glk/alan3/container.cpp +++ b/engines/glk/alan3/container.cpp @@ -38,152 +38,148 @@ ContainerEntry *containers; /* Container table pointer */ /*----------------------------------------------------------------------*/ -static int countInContainer(int containerIndex) /* IN - the container to count in */ -{ - int j = 0; - - for (uint instanceIndex = 1; instanceIndex <= header->instanceMax; instanceIndex++) - if (isIn(instanceIndex, containerIndex, DIRECT)) - /* Then it's in this container also */ - j++; - return(j); +static int countInContainer(int containerIndex) { /* IN - the container to count in */ + int j = 0; + + for (uint instanceIndex = 1; instanceIndex <= header->instanceMax; instanceIndex++) + if (isIn(instanceIndex, containerIndex, DIRECT)) + /* Then it's in this container also */ + j++; + return (j); } /*----------------------------------------------------------------------*/ static int sumAttributeInContainer( - Aint containerIndex, /* IN - the container to sum */ - Aint attributeIndex /* IN - the attribute to sum over */ - ) { - uint instanceIndex; - int sum = 0; - - for (instanceIndex = 1; instanceIndex <= header->instanceMax; instanceIndex++) - if (isIn(instanceIndex, containerIndex, DIRECT)) { /* Then it's directly in this cont */ - if (instances[instanceIndex].container != 0) /* This is also a container! */ - sum = sum + sumAttributeInContainer(instanceIndex, attributeIndex); - sum = sum + getInstanceAttribute(instanceIndex, attributeIndex); - } - return(sum); + Aint containerIndex, /* IN - the container to sum */ + Aint attributeIndex /* IN - the attribute to sum over */ +) { + uint instanceIndex; + int sum = 0; + + for (instanceIndex = 1; instanceIndex <= header->instanceMax; instanceIndex++) + if (isIn(instanceIndex, containerIndex, DIRECT)) { /* Then it's directly in this cont */ + if (instances[instanceIndex].container != 0) /* This is also a container! */ + sum = sum + sumAttributeInContainer(instanceIndex, attributeIndex); + sum = sum + getInstanceAttribute(instanceIndex, attributeIndex); + } + return (sum); } /*----------------------------------------------------------------------*/ -static bool containerIsEmpty(int container) -{ - uint i; - - for (i = 1; i <= header->instanceMax; i++) - if (isDescribable(i) && isIn(i, container, TRANSITIVE)) - return FALSE; - return TRUE; +static bool containerIsEmpty(int container) { + uint i; + + for (i = 1; i <= header->instanceMax; i++) + if (isDescribable(i) && isIn(i, container, TRANSITIVE)) + return FALSE; + return TRUE; } /*======================================================================*/ -void describeContainer(int container) -{ - if (!containerIsEmpty(container) && !isOpaque(container)) - list(container); +void describeContainer(int container) { + if (!containerIsEmpty(container) && !isOpaque(container)) + list(container); } /*======================================================================*/ bool passesContainerLimits(Aint theContainer, Aint theAddedInstance) { - LimitEntry *limit; - Aword props; - - if (!isAContainer(theContainer)) - syserr("Checking limits for a non-container."); - - /* Find the container properties */ - props = instances[theContainer].container; - - if (containers[props].limits != 0) { /* Any limits at all? */ - for (limit = (LimitEntry *) pointerTo(containers[props].limits); !isEndOfArray(limit); limit++) - if ((int)limit->atr == 1-I_COUNT) { /* TODO This is actually some encoding of the attribute number, right? */ - if (countInContainer(theContainer) >= (int)limit->val) { - interpret(limit->stms); - return(FALSE); - } - } else { - if (sumAttributeInContainer(theContainer, limit->atr) + getInstanceAttribute(theAddedInstance, limit->atr) > limit->val) { - interpret(limit->stms); - return(FALSE); - } - } - } - return(TRUE); + LimitEntry *limit; + Aword props; + + if (!isAContainer(theContainer)) + syserr("Checking limits for a non-container."); + + /* Find the container properties */ + props = instances[theContainer].container; + + if (containers[props].limits != 0) { /* Any limits at all? */ + for (limit = (LimitEntry *) pointerTo(containers[props].limits); !isEndOfArray(limit); limit++) + if ((int)limit->atr == 1 - I_COUNT) { /* TODO This is actually some encoding of the attribute number, right? */ + if (countInContainer(theContainer) >= (int)limit->val) { + interpret(limit->stms); + return (FALSE); + } + } else { + if (sumAttributeInContainer(theContainer, limit->atr) + getInstanceAttribute(theAddedInstance, limit->atr) > limit->val) { + interpret(limit->stms); + return (FALSE); + } + } + } + return (TRUE); } /*======================================================================*/ int containerSize(int container, ATrans trans) { - Aword i; - Aint count = 0; - - for (i = 1; i <= header->instanceMax; i++) { - if (isIn(i, container, trans)) - count++; - } - return(count); + Aword i; + Aint count = 0; + + for (i = 1; i <= header->instanceMax; i++) { + if (isIn(i, container, trans)) + count++; + } + return (count); } /*======================================================================*/ -void list(int container) -{ - uint i; - Aword props; - Aword foundInstance[2] = {0,0}; - int found = 0; - Aint previousThis = current.instance; - - current.instance = container; - - /* Find container table entry */ - props = instances[container].container; - if (props == 0) syserr("Trying to list something not a container."); - - for (i = 1; i <= header->instanceMax; i++) { - if (isDescribable(i)) { - /* We can only see objects and actors directly in this container... */ - if (admin[i].location == container) { /* Yes, it's in this container */ - if (found == 0) { - if (containers[props].header != 0) - interpret(containers[props].header); - else { - if (isAActor(containers[props].owner)) - printMessageWithInstanceParameter(M_CARRIES, containers[props].owner); - else - printMessageWithInstanceParameter(M_CONTAINS, containers[props].owner); - } - foundInstance[0] = i; - } else if (found == 1) - foundInstance[1] = i; - else { - printMessageWithInstanceParameter(M_CONTAINS_COMMA, i); - } - found++; - } - } - } - - if (found > 0) { - if (found > 1) - printMessageWithInstanceParameter(M_CONTAINS_AND, foundInstance[1]); - printMessageWithInstanceParameter(M_CONTAINS_END, foundInstance[0]); - } else { - if (containers[props].empty != 0) - interpret(containers[props].empty); - else { - if (isAActor(containers[props].owner)) - printMessageWithInstanceParameter(M_EMPTYHANDED, containers[props].owner); - else - printMessageWithInstanceParameter(M_EMPTY, containers[props].owner); - } - } - needSpace = TRUE; - current.instance = previousThis; +void list(int container) { + uint i; + Aword props; + Aword foundInstance[2] = {0, 0}; + int found = 0; + Aint previousThis = current.instance; + + current.instance = container; + + /* Find container table entry */ + props = instances[container].container; + if (props == 0) syserr("Trying to list something not a container."); + + for (i = 1; i <= header->instanceMax; i++) { + if (isDescribable(i)) { + /* We can only see objects and actors directly in this container... */ + if (admin[i].location == container) { /* Yes, it's in this container */ + if (found == 0) { + if (containers[props].header != 0) + interpret(containers[props].header); + else { + if (isAActor(containers[props].owner)) + printMessageWithInstanceParameter(M_CARRIES, containers[props].owner); + else + printMessageWithInstanceParameter(M_CONTAINS, containers[props].owner); + } + foundInstance[0] = i; + } else if (found == 1) + foundInstance[1] = i; + else { + printMessageWithInstanceParameter(M_CONTAINS_COMMA, i); + } + found++; + } + } + } + + if (found > 0) { + if (found > 1) + printMessageWithInstanceParameter(M_CONTAINS_AND, foundInstance[1]); + printMessageWithInstanceParameter(M_CONTAINS_END, foundInstance[0]); + } else { + if (containers[props].empty != 0) + interpret(containers[props].empty); + else { + if (isAActor(containers[props].owner)) + printMessageWithInstanceParameter(M_EMPTYHANDED, containers[props].owner); + else + printMessageWithInstanceParameter(M_EMPTY, containers[props].owner); + } + } + needSpace = TRUE; + current.instance = previousThis; } } // End of namespace Alan3 diff --git a/engines/glk/alan3/current.h b/engines/glk/alan3/current.h index 3939816ae8..8c986531b2 100644 --- a/engines/glk/alan3/current.h +++ b/engines/glk/alan3/current.h @@ -30,17 +30,17 @@ namespace Alan3 { /* TYPES */ struct CurVars { - int syntax, - verb, - location, - actor, - instance, - tick, - score, - visits, - sourceLine, - sourceFile; - bool meta; + int syntax, + verb, + location, + actor, + instance, + tick, + score, + visits, + sourceLine, + sourceFile; + bool meta; }; /* DATA */ diff --git a/engines/glk/alan3/debug.cpp b/engines/glk/alan3/debug.cpp index 53895f14e7..d090021733 100644 --- a/engines/glk/alan3/debug.cpp +++ b/engines/glk/alan3/debug.cpp @@ -54,367 +54,353 @@ Breakpoint breakpoint[BREAKPOINTMAX]; #define debugPrefix "adbg: " /*----------------------------------------------------------------------*/ -static void showAttributes(AttributeEntry *attrib) -{ - AttributeEntry *at; - int i; - char str[80]; - - if (attrib == 0) - return; - - i = 1; - for (at = attrib; !isEndOfArray(at); at++) { - sprintf(str, "$i$t%s[%d] = %d", (char *) pointerTo(at->id), at->code, (int)at->value); - - output(str); - i++; - } +static void showAttributes(AttributeEntry *attrib) { + AttributeEntry *at; + int i; + char str[80]; + + if (attrib == 0) + return; + + i = 1; + for (at = attrib; !isEndOfArray(at); at++) { + sprintf(str, "$i$t%s[%d] = %d", (char *) pointerTo(at->id), at->code, (int)at->value); + + output(str); + i++; + } } /*----------------------------------------------------------------------*/ -static void showContents(int cnt) -{ - uint i; - char str[80]; - Abool found = FALSE; - - output("$iContains:"); - for (i = 1; i <= header->instanceMax; i++) { - if (isIn(i, cnt, DIRECT)) { /* Yes, it's directly in this container */ - if (!found) - found = TRUE; - output("$i$t"); - say(i); - sprintf(str, "[%d] ", i); - output(str); - } - } - if (!found) - output("nothing"); +static void showContents(int cnt) { + uint i; + char str[80]; + Abool found = FALSE; + + output("$iContains:"); + for (i = 1; i <= header->instanceMax; i++) { + if (isIn(i, cnt, DIRECT)) { /* Yes, it's directly in this container */ + if (!found) + found = TRUE; + output("$i$t"); + say(i); + sprintf(str, "[%d] ", i); + output(str); + } + } + if (!found) + output("nothing"); } /*----------------------------------------------------------------------*/ static char *idOfInstance(int instance) { - int base = header->instanceTableAddress+ - header->instanceMax*sizeof(InstanceEntry)/sizeof(Aword)+1; - return (char *)&memory[memory[base+instance-1]]; + int base = header->instanceTableAddress + + header->instanceMax * sizeof(InstanceEntry) / sizeof(Aword) + 1; + return (char *)&memory[memory[base + instance - 1]]; } /*----------------------------------------------------------------------*/ static void sayInstanceNumberAndName(int ins) { - char buf[1000]; + char buf[1000]; - sprintf(buf, "[%d] %s (\"$$", ins, idOfInstance(ins)); - output(buf); - say(ins); - output("$$\")"); + sprintf(buf, "[%d] %s (\"$$", ins, idOfInstance(ins)); + output(buf); + say(ins); + output("$$\")"); } /*----------------------------------------------------------------------*/ static void sayLocationOfInstance(int ins, const char *prefix) { - if (admin[ins].location == 0) - return; - else { - output(prefix); - if (isALocation(admin[ins].location)) { - output("at"); - sayInstanceNumberAndName(admin[ins].location); - sayLocationOfInstance(admin[ins].location, prefix); - } else if (isAContainer(admin[ins].location)) { - if (isAObject(admin[ins].location)) - output("in"); - else if (isAActor(admin[ins].location)) - output("carried by"); - sayInstanceNumberAndName(admin[ins].location); - sayLocationOfInstance(admin[ins].location, prefix); - } else - output("Illegal location!"); - } + if (admin[ins].location == 0) + return; + else { + output(prefix); + if (isALocation(admin[ins].location)) { + output("at"); + sayInstanceNumberAndName(admin[ins].location); + sayLocationOfInstance(admin[ins].location, prefix); + } else if (isAContainer(admin[ins].location)) { + if (isAObject(admin[ins].location)) + output("in"); + else if (isAActor(admin[ins].location)) + output("carried by"); + sayInstanceNumberAndName(admin[ins].location); + sayLocationOfInstance(admin[ins].location, prefix); + } else + output("Illegal location!"); + } } /*----------------------------------------------------------------------*/ static void listInstance(int ins) { - output("$i"); - sayInstanceNumberAndName(ins); - if (instances[ins].container) - output("(container)"); - sayLocationOfInstance(ins, ", "); + output("$i"); + sayInstanceNumberAndName(ins); + if (instances[ins].container) + output("(container)"); + sayLocationOfInstance(ins, ", "); } /*----------------------------------------------------------------------*/ -static void listInstances(char *pattern) -{ - uint ins; - bool found = FALSE; - - for (ins = 1; ins <= header->instanceMax; ins++) { - if (pattern == NULL || (pattern != NULL && match(pattern, idOfInstance(ins)))) { - if (!found) { - output("Instances:"); - found = TRUE; - } - listInstance(ins); - } - } - if (pattern != NULL && !found) - output("No instances matched the pattern."); +static void listInstances(char *pattern) { + uint ins; + bool found = FALSE; + + for (ins = 1; ins <= header->instanceMax; ins++) { + if (pattern == NULL || (pattern != NULL && match(pattern, idOfInstance(ins)))) { + if (!found) { + output("Instances:"); + found = TRUE; + } + listInstance(ins); + } + } + if (pattern != NULL && !found) + output("No instances matched the pattern."); } /*----------------------------------------------------------------------*/ -static void showInstance(int ins) -{ - char str[80]; - - if (ins > (int)header->instanceMax || ins < 1) { - sprintf(str, "Instance index %d is out of range.", ins); - output(str); - return; - } - - output("The"); - sayInstanceNumberAndName(ins); - if (instances[ins].parent) { - sprintf(str, "Isa %s[%d]", idOfClass(instances[ins].parent), instances[ins].parent); - output(str); - } - - if (!isA(ins, header->locationClassId) || (isA(ins, header->locationClassId) && admin[ins].location != 0)) { - sprintf(str, "$iLocation:"); - output(str); - needSpace = TRUE; - sayLocationOfInstance(ins, ""); - } - - output("$iAttributes:"); - showAttributes(admin[ins].attributes); - - if (instances[ins].container) - showContents(ins); - - if (isA(ins, header->actorClassId)) { - if (admin[ins].script == 0) - output("$iIs idle"); - else { - sprintf(str, "$iExecuting script: %d, Step: %d", admin[ins].script, admin[ins].step); - output(str); - } - } +static void showInstance(int ins) { + char str[80]; + + if (ins > (int)header->instanceMax || ins < 1) { + sprintf(str, "Instance index %d is out of range.", ins); + output(str); + return; + } + + output("The"); + sayInstanceNumberAndName(ins); + if (instances[ins].parent) { + sprintf(str, "Isa %s[%d]", idOfClass(instances[ins].parent), instances[ins].parent); + output(str); + } + + if (!isA(ins, header->locationClassId) || (isA(ins, header->locationClassId) && admin[ins].location != 0)) { + sprintf(str, "$iLocation:"); + output(str); + needSpace = TRUE; + sayLocationOfInstance(ins, ""); + } + + output("$iAttributes:"); + showAttributes(admin[ins].attributes); + + if (instances[ins].container) + showContents(ins); + + if (isA(ins, header->actorClassId)) { + if (admin[ins].script == 0) + output("$iIs idle"); + else { + sprintf(str, "$iExecuting script: %d, Step: %d", admin[ins].script, admin[ins].step); + output(str); + } + } } /*----------------------------------------------------------------------*/ -static void listObjects(void) -{ - uint obj; - - output("Objects:"); - for (obj = 1; obj <= header->instanceMax; obj++) - if (isAObject(obj)) - listInstance(obj); +static void listObjects(void) { + uint obj; + + output("Objects:"); + for (obj = 1; obj <= header->instanceMax; obj++) + if (isAObject(obj)) + listInstance(obj); } /*----------------------------------------------------------------------*/ -static void showObject(int obj) -{ - char str[80]; +static void showObject(int obj) { + char str[80]; - if (!isAObject(obj)) { - sprintf(str, "Instance %d is not an object", obj); - output(str); - return; - } + if (!isAObject(obj)) { + sprintf(str, "Instance %d is not an object", obj); + output(str); + return; + } - showInstance(obj); + showInstance(obj); } /*----------------------------------------------------------------------*/ static int sourceFileNumber(char *fileName) { - SourceFileEntry *entries = (SourceFileEntry *)pointerTo(header->sourceFileTable); - int n; - - for (n = 0; *(Aword*)&entries[n] != EOD; n++) { - char *entryName; - entryName = getStringFromFile(entries[n].fpos, entries[n].len); - if (strcmp(entryName, fileName) == 0) return n; - entryName = baseNameStart(entryName); - if (strcmp(entryName, fileName) == 0) return n; - } - return -1; + SourceFileEntry *entries = (SourceFileEntry *)pointerTo(header->sourceFileTable); + int n; + + for (n = 0; * (Aword *)&entries[n] != EOD; n++) { + char *entryName; + entryName = getStringFromFile(entries[n].fpos, entries[n].len); + if (strcmp(entryName, fileName) == 0) return n; + entryName = baseNameStart(entryName); + if (strcmp(entryName, fileName) == 0) return n; + } + return -1; } /*----------------------------------------------------------------------*/ static void printClassName(int c) { - output(idOfClass(c)); + output(idOfClass(c)); } /*----------------------------------------------------------------------*/ static void showClassInheritance(int c) { - char str[80]; - - if (classes[c].parent != 0) { - output(", Isa"); - printClassName(classes[c].parent); - sprintf(str, "[%d]", classes[c].parent); - output(str); - } + char str[80]; + + if (classes[c].parent != 0) { + output(", Isa"); + printClassName(classes[c].parent); + sprintf(str, "[%d]", classes[c].parent); + output(str); + } } /*----------------------------------------------------------------------*/ -static void showClass(int cla) -{ - char str[80]; - - if (cla < 1) { - sprintf(str, "Class index %d is out of range.", cla); - output(str); - return; - } - - output("$t"); - printClassName(cla); - sprintf(str, "[%d]", cla); - output(str); - showClassInheritance(cla); +static void showClass(int cla) { + char str[80]; + + if (cla < 1) { + sprintf(str, "Class index %d is out of range.", cla); + output(str); + return; + } + + output("$t"); + printClassName(cla); + sprintf(str, "[%d]", cla); + output(str); + showClassInheritance(cla); } /*----------------------------------------------------------------------*/ -static void listClass(int c) -{ - char str[80]; - - sprintf(str, "%3d: ", c); - output(str); - printClassName(c); - showClassInheritance(c); +static void listClass(int c) { + char str[80]; + + sprintf(str, "%3d: ", c); + output(str); + printClassName(c); + showClassInheritance(c); } /*----------------------------------------------------------------------*/ -static void showClassHierarchy(int thisItem, int depth) -{ - int i; - uint child; - - output("$i"); - for (i=0; i < depth; i++) - output("$t"); - - listClass(thisItem); - for (child = 1; child <= header->classMax; child++) { - if (classes[child].parent == thisItem) { - showClassHierarchy(child, depth+1); - } - } +static void showClassHierarchy(int thisItem, int depth) { + int i; + uint child; + + output("$i"); + for (i = 0; i < depth; i++) + output("$t"); + + listClass(thisItem); + for (child = 1; child <= header->classMax; child++) { + if (classes[child].parent == thisItem) { + showClassHierarchy(child, depth + 1); + } + } } /*----------------------------------------------------------------------*/ -static void listLocations(void) -{ - uint loc; - - output("Locations:"); - for (loc = 1; loc <= header->instanceMax; loc++) - if (isALocation(loc)) - listInstance(loc); +static void listLocations(void) { + uint loc; + + output("Locations:"); + for (loc = 1; loc <= header->instanceMax; loc++) + if (isALocation(loc)) + listInstance(loc); } /*----------------------------------------------------------------------*/ -static void showLocation(int loc) -{ - char str[80]; +static void showLocation(int loc) { + char str[80]; - if (!isALocation(loc)) { - sprintf(str, "Instance %d is not a location.", loc); - output(str); - return; - } + if (!isALocation(loc)) { + sprintf(str, "Instance %d is not a location.", loc); + output(str); + return; + } - output("The "); - say(loc); - sprintf(str, "(%d) Isa location :", loc); - output(str); + output("The "); + say(loc); + sprintf(str, "(%d) Isa location :", loc); + output(str); - output("$iAttributes ="); - showAttributes(admin[loc].attributes); + output("$iAttributes ="); + showAttributes(admin[loc].attributes); } /*----------------------------------------------------------------------*/ -static void listActors(void) -{ - uint act; - - output("Actors:"); - for (act = 1; act <= header->instanceMax; act++) - if (isAActor(act)) - listInstance(act); +static void listActors(void) { + uint act; + + output("Actors:"); + for (act = 1; act <= header->instanceMax; act++) + if (isAActor(act)) + listInstance(act); } /*----------------------------------------------------------------------*/ -static void showActor(int act) -{ - char str[80]; +static void showActor(int act) { + char str[80]; - if (!isAActor(act)) { - sprintf(str, "Instance %d is not an actor.", act); - output(str); - return; - } + if (!isAActor(act)) { + sprintf(str, "Instance %d is not an actor.", act); + output(str); + return; + } - showInstance(act); + showInstance(act); } /*----------------------------------------------------------------------*/ -static void showEvents(void) -{ +static void showEvents(void) { uint event; int i; - char str[80]; - bool scheduled; - - output("Events:"); - for (event = 1; event <= header->eventMax; event++) { - sprintf(str, "$i%d [%s]:", event, (char *)pointerTo(events[event].id)); - - output(str); - scheduled = FALSE; - for (i = 0; i < eventQueueTop; i++) - if ((scheduled = (eventQueue[i].event == (int)event))) - break; - if (scheduled) { - sprintf(str, "Scheduled for +%d, at ", eventQueue[i].after); - output(str); - say(eventQueue[i].where); - } else - output("Not scheduled."); - } + char str[80]; + bool scheduled; + + output("Events:"); + for (event = 1; event <= header->eventMax; event++) { + sprintf(str, "$i%d [%s]:", event, (char *)pointerTo(events[event].id)); + + output(str); + scheduled = FALSE; + for (i = 0; i < eventQueueTop; i++) + if ((scheduled = (eventQueue[i].event == (int)event))) + break; + if (scheduled) { + sprintf(str, "Scheduled for +%d, at ", eventQueue[i].after); + output(str); + say(eventQueue[i].where); + } else + output("Not scheduled."); + } } /*======================================================================*/ char *sourceFileName(int fileNumber) { - SourceFileEntry *entries = (SourceFileEntry *)pointerTo(header->sourceFileTable); + SourceFileEntry *entries = (SourceFileEntry *)pointerTo(header->sourceFileTable); - return getStringFromFile(entries[fileNumber].fpos, entries[fileNumber].len); + return getStringFromFile(entries[fileNumber].fpos, entries[fileNumber].len); } @@ -436,14 +422,14 @@ bool readLine(Common::SeekableReadStream *rs, char *line, int maxLen) { /*======================================================================*/ char *readSourceLine(int file, int line) { - int count; + int count; #define SOURCELINELENGTH 1000 - static char buffer[SOURCELINELENGTH]; + static char buffer[SOURCELINELENGTH]; frefid_t sourceFileRef = g_vm->glk_fileref_create_by_name(fileusage_TextMode, sourceFileName(file), 0); strid_t sourceFile = g_vm->glk_stream_open_file(sourceFileRef, filemode_Read, 0); - if (sourceFile != NULL) { + if (sourceFile != NULL) { for (count = 0; count < line; count++) { if (!readLine(*sourceFile, buffer, SOURCELINELENGTH)) return NULL; @@ -456,129 +442,129 @@ char *readSourceLine(int file, int line) { } delete sourceFile; - return buffer; - } + return buffer; + } return NULL; } /*======================================================================*/ void showSourceLine(int fileNumber, int line) { - char *buffer = readSourceLine(fileNumber, line); - if (buffer != NULL) { - if (buffer[strlen(buffer)-1] == '\n') - buffer[strlen(buffer)-1] = '\0'; - printf("<%05d>: %s", line, buffer); - } + char *buffer = readSourceLine(fileNumber, line); + if (buffer != NULL) { + if (buffer[strlen(buffer) - 1] == '\n') + buffer[strlen(buffer) - 1] = '\0'; + printf("<%05d>: %s", line, buffer); + } } /*----------------------------------------------------------------------*/ static void listFiles() { - SourceFileEntry *entry; - int i = 0; - for (entry = (SourceFileEntry *)pointerTo(header->sourceFileTable); *((Aword*)entry) != EOD; entry++) { - printf(" %2d : %s\n", i, sourceFileName(i)); - i++; - } + SourceFileEntry *entry; + int i = 0; + for (entry = (SourceFileEntry *)pointerTo(header->sourceFileTable); * ((Aword *)entry) != EOD; entry++) { + printf(" %2d : %s\n", i, sourceFileName(i)); + i++; + } } /*----------------------------------------------------------------------*/ static int findSourceLineIndex(SourceLineEntry *entry, int file, int line) { - /* Will return index to the closest line available */ - int i = 0; - - while (!isEndOfArray(&entry[i]) && entry[i].file != file) - i++; - while (!isEndOfArray(&entry[i]) && entry[i].file == file && entry[i].line < line) - i++; - if (isEndOfArray(entry) || entry[i].file != file) - return i-1; - else - return i; + /* Will return index to the closest line available */ + int i = 0; + + while (!isEndOfArray(&entry[i]) && entry[i].file != file) + i++; + while (!isEndOfArray(&entry[i]) && entry[i].file == file && entry[i].line < line) + i++; + if (isEndOfArray(entry) || entry[i].file != file) + return i - 1; + else + return i; } /*----------------------------------------------------------------------*/ static void listBreakpoints() { - int i; - bool found = FALSE; - - for (i = 0; i < BREAKPOINTMAX; i++) - if (breakpoint[i].line != 0) { - if (!found) - printf("Breakpoints set:\n"); - found = TRUE; - printf(" %s:%d\n", sourceFileName(breakpoint[i].file), breakpoint[i].line); - } - if (!found) - printf("No breakpoints set\n"); + int i; + bool found = FALSE; + + for (i = 0; i < BREAKPOINTMAX; i++) + if (breakpoint[i].line != 0) { + if (!found) + printf("Breakpoints set:\n"); + found = TRUE; + printf(" %s:%d\n", sourceFileName(breakpoint[i].file), breakpoint[i].line); + } + if (!found) + printf("No breakpoints set\n"); } /*======================================================================*/ int breakpointIndex(int file, int line) { - int i; + int i; - for (i = 0; i < BREAKPOINTMAX; i++) - if (breakpoint[i].line == line && breakpoint[i].file == file) - return i; - return -1; + for (i = 0; i < BREAKPOINTMAX; i++) + if (breakpoint[i].line == line && breakpoint[i].file == file) + return i; + return -1; } /*----------------------------------------------------------------------*/ static int availableBreakpointSlot() { - int i; + int i; - for (i = 0; i < BREAKPOINTMAX; i++) - if (breakpoint[i].line == 0) - return i; - return -1; + for (i = 0; i < BREAKPOINTMAX; i++) + if (breakpoint[i].line == 0) + return i; + return -1; } /*----------------------------------------------------------------------*/ static void setBreakpoint(int file, int line) { - int i = breakpointIndex(file, line); - - if (i != -1) - printf("Breakpoint already set at %s:%d\n", sourceFileName(file), line); - else { - i = availableBreakpointSlot(); - if (i == -1) - printf("No room for more breakpoints. Delete one first.\n"); - else { - int lineIndex = findSourceLineIndex((SourceLineEntry *)pointerTo(header->sourceLineTable), file, line); - SourceLineEntry *entry = (SourceLineEntry *)pointerTo(header->sourceLineTable); - char leadingText[100] = "Breakpoint"; - if (entry[lineIndex].file == (Aint)EOD) { - printf("Line %d not available\n", line); - } else { - if (entry[lineIndex].line != line) - sprintf(leadingText, "Line %d not available, breakpoint instead", line); - breakpoint[i].file = entry[lineIndex].file; - breakpoint[i].line = entry[lineIndex].line; - printf("%s set at %s:%d\n", leadingText, sourceFileName(entry[lineIndex].file), entry[lineIndex].line); - showSourceLine(entry[lineIndex].file, entry[lineIndex].line); - printf("\n"); - } - } - } + int i = breakpointIndex(file, line); + + if (i != -1) + printf("Breakpoint already set at %s:%d\n", sourceFileName(file), line); + else { + i = availableBreakpointSlot(); + if (i == -1) + printf("No room for more breakpoints. Delete one first.\n"); + else { + int lineIndex = findSourceLineIndex((SourceLineEntry *)pointerTo(header->sourceLineTable), file, line); + SourceLineEntry *entry = (SourceLineEntry *)pointerTo(header->sourceLineTable); + char leadingText[100] = "Breakpoint"; + if (entry[lineIndex].file == (Aint)EOD) { + printf("Line %d not available\n", line); + } else { + if (entry[lineIndex].line != line) + sprintf(leadingText, "Line %d not available, breakpoint instead", line); + breakpoint[i].file = entry[lineIndex].file; + breakpoint[i].line = entry[lineIndex].line; + printf("%s set at %s:%d\n", leadingText, sourceFileName(entry[lineIndex].file), entry[lineIndex].line); + showSourceLine(entry[lineIndex].file, entry[lineIndex].line); + printf("\n"); + } + } + } } /*----------------------------------------------------------------------*/ static void deleteBreakpoint(int line, int file) { - int i = breakpointIndex(file, line); - - if (i == -1) - printf("No breakpoint set at %s:%d\n", sourceFileName(file), line); - else { - breakpoint[i].line = 0; - printf("Breakpoint at %s:%d deleted\n", sourceFileName(file), line); - } + int i = breakpointIndex(file, line); + + if (i == -1) + printf("No breakpoint set at %s:%d\n", sourceFileName(file), line); + else { + breakpoint[i].line = 0; + printf("Breakpoint at %s:%d deleted\n", sourceFileName(file), line); + } } @@ -587,30 +573,35 @@ static bool saved_traceSection, saved_traceInstruction, saved_capitilize, saved_ static int loc; /*======================================================================*/ -void saveInfo(void) -{ - /* Save some important things */ - saved_capitilize = capitalize; capitalize = FALSE; - saved_traceSection = traceSectionOption; traceSectionOption = FALSE; - saved_traceSource = traceSourceOption; traceSourceOption = FALSE; - saved_traceInstruction = traceInstructionOption; traceInstructionOption = FALSE; - saved_tracePush = tracePushOption; tracePushOption = FALSE; - saved_traceStack = traceStackOption; traceStackOption = FALSE; - loc = current.location; current.location = where(HERO, DIRECT); +void saveInfo(void) { + /* Save some important things */ + saved_capitilize = capitalize; + capitalize = FALSE; + saved_traceSection = traceSectionOption; + traceSectionOption = FALSE; + saved_traceSource = traceSourceOption; + traceSourceOption = FALSE; + saved_traceInstruction = traceInstructionOption; + traceInstructionOption = FALSE; + saved_tracePush = tracePushOption; + tracePushOption = FALSE; + saved_traceStack = traceStackOption; + traceStackOption = FALSE; + loc = current.location; + current.location = where(HERO, DIRECT); } /*======================================================================*/ -void restoreInfo(void) -{ - /* Restore! */ - capitalize = saved_capitilize; - traceSectionOption = saved_traceSection; - traceInstructionOption = saved_traceInstruction; - traceSourceOption = saved_traceSource; - tracePushOption = saved_tracePush; - traceStackOption = saved_traceStack; - current.location = loc; +void restoreInfo(void) { + /* Restore! */ + capitalize = saved_capitilize; + traceSectionOption = saved_traceSection; + traceInstructionOption = saved_traceInstruction; + traceSourceOption = saved_traceSource; + tracePushOption = saved_tracePush; + traceStackOption = saved_traceStack; + current.location = loc; } #define HELP_COMMAND 'H' @@ -639,428 +630,478 @@ void restoreInfo(void) #define TRACE_STACK_COMMAND 't' typedef struct DebugParseEntry { - const char *command; + const char *command; const char *parameter; - char code; + char code; const char *helpText; } DebugParseEntry; static const DebugParseEntry commandEntries[] = { - {"help", "", HELP_COMMAND, "this help"}, - {"?", "", HELP_COMMAND, "d:o"}, - {"break", "[[file:]n]", BREAK_COMMAND, "set breakpoint at source line [n] (optionally in [file])"}, - {"delete", "[[file:]n]", DELETE_COMMAND, "delete breakpoint at source line [n] (optionally in [file])"}, - {"files", "", FILES_COMMAND, "list source files"}, - {"events", "", EVENTS_COMMAND, "list events"}, - {"classes", "", CLASSES_COMMAND, "list class hierarchy"}, - {"instances", "[n]", INSTANCES_COMMAND, "list instance(s), all, wildcard, number or name"}, - {"objects", "[n]", OBJECTS_COMMAND, "list instance(s) that are objects"}, - {"actors", "[n]", ACTORS_COMMAND, "list instance(s) that are actors"}, - {"locations", "[n]", LOCATIONS_COMMAND, "list instances that are locations"}, - {"trace", "('source'|'section'|'instruction'|'push'|'stack')", TRACE_COMMAND, "toggle various traces"}, - {"next", "", NEXT_COMMAND, "run game and stop at next source line"}, - {"go", "", GO_COMMAND, "go another player turn"}, - {"exit", "", EXIT_COMMAND, "exit to game, enter 'debug' to get back"}, - {"x", "", EXIT_COMMAND, "d:o"}, - {"quit", "", QUIT_COMMAND, "quit game"}, - {NULL, NULL, '\0', NULL} + {"help", "", HELP_COMMAND, "this help"}, + {"?", "", HELP_COMMAND, "d:o"}, + {"break", "[[file:]n]", BREAK_COMMAND, "set breakpoint at source line [n] (optionally in [file])"}, + {"delete", "[[file:]n]", DELETE_COMMAND, "delete breakpoint at source line [n] (optionally in [file])"}, + {"files", "", FILES_COMMAND, "list source files"}, + {"events", "", EVENTS_COMMAND, "list events"}, + {"classes", "", CLASSES_COMMAND, "list class hierarchy"}, + {"instances", "[n]", INSTANCES_COMMAND, "list instance(s), all, wildcard, number or name"}, + {"objects", "[n]", OBJECTS_COMMAND, "list instance(s) that are objects"}, + {"actors", "[n]", ACTORS_COMMAND, "list instance(s) that are actors"}, + {"locations", "[n]", LOCATIONS_COMMAND, "list instances that are locations"}, + {"trace", "('source'|'section'|'instruction'|'push'|'stack')", TRACE_COMMAND, "toggle various traces"}, + {"next", "", NEXT_COMMAND, "run game and stop at next source line"}, + {"go", "", GO_COMMAND, "go another player turn"}, + {"exit", "", EXIT_COMMAND, "exit to game, enter 'debug' to get back"}, + {"x", "", EXIT_COMMAND, "d:o"}, + {"quit", "", QUIT_COMMAND, "quit game"}, + {NULL, NULL, '\0', NULL} }; static const DebugParseEntry traceSubcommand[] = { - {"source", "", TRACE_SOURCE_COMMAND, ""}, - {"section", "", TRACE_SECTION_COMMAND, ""}, - {"instructions", "", TRACE_INSTRUCTION_COMMAND, ""}, - {"pushs", "", TRACE_PUSH_COMMAND, ""}, - {"stacks", "", TRACE_STACK_COMMAND, ""}, - {NULL, NULL, '\0', NULL} + {"source", "", TRACE_SOURCE_COMMAND, ""}, + {"section", "", TRACE_SECTION_COMMAND, ""}, + {"instructions", "", TRACE_INSTRUCTION_COMMAND, ""}, + {"pushs", "", TRACE_PUSH_COMMAND, ""}, + {"stacks", "", TRACE_STACK_COMMAND, ""}, + {NULL, NULL, '\0', NULL} }; static char *spaces(int length) { - static char buf[200]; - int i; + static char buf[200]; + int i; - for (i = 0; i<length; i++) - buf[i] = ' '; - buf[i] = '\0'; - return buf; + for (i = 0; i < length; i++) + buf[i] = ' '; + buf[i] = '\0'; + return buf; } /*----------------------------------------------------------------------*/ static char *padding(const DebugParseEntry *entry, int maxLength) { - return spaces(maxLength-strlen(entry->command)-strlen(entry->parameter)); + return spaces(maxLength - strlen(entry->command) - strlen(entry->parameter)); } /*----------------------------------------------------------------------*/ static void handleHelpCommand() { - if (!regressionTestOption) - output(alan.longHeader); - - const DebugParseEntry *entry = commandEntries; - - int maxLength = 0; - for (entry = commandEntries; entry->command != NULL; entry++) { - if (strlen(entry->command)+strlen(entry->parameter) > (uint)maxLength) - maxLength = strlen(entry->command)+strlen(entry->parameter); - } - - output("$nADBG Commands (can be abbreviated):"); - for (entry = commandEntries; entry->command != NULL; entry++) { - char buf[200]; - sprintf(buf, "$i%s %s %s$n$t$t-- %s", entry->command, entry->parameter, padding(entry, maxLength), entry->helpText); - output(buf); - } + if (!regressionTestOption) + output(alan.longHeader); + + const DebugParseEntry *entry = commandEntries; + + int maxLength = 0; + for (entry = commandEntries; entry->command != NULL; entry++) { + if (strlen(entry->command) + strlen(entry->parameter) > (uint)maxLength) + maxLength = strlen(entry->command) + strlen(entry->parameter); + } + + output("$nADBG Commands (can be abbreviated):"); + for (entry = commandEntries; entry->command != NULL; entry++) { + char buf[200]; + sprintf(buf, "$i%s %s %s$n$t$t-- %s", entry->command, entry->parameter, padding(entry, maxLength), entry->helpText); + output(buf); + } } /*----------------------------------------------------------------------*/ static const DebugParseEntry *findEntry(char *command, const DebugParseEntry *entry) { - while (entry->command != NULL) { - if (strncasecmp(command, entry->command, strlen(command)) == 0) - return entry; - entry++; - } - return NULL; + while (entry->command != NULL) { + if (strncasecmp(command, entry->command, strlen(command)) == 0) + return entry; + entry++; + } + return NULL; } /*----------------------------------------------------------------------*/ static char parseDebugCommand(char *command) { - const DebugParseEntry *entry = findEntry(command, commandEntries); - if (entry != NULL) { - if (strlen(command) < strlen(entry->command)) { - /* See if there are any more partial matches */ - if (findEntry(command, entry+1) != NULL) - /* TODO: we should list the possible matches somehow */ - return AMBIGUOUS_COMMAND; - } - return entry->code; - } else - return UNKNOWN_COMMAND; + const DebugParseEntry *entry = findEntry(command, commandEntries); + if (entry != NULL) { + if (strlen(command) < strlen(entry->command)) { + /* See if there are any more partial matches */ + if (findEntry(command, entry + 1) != NULL) + /* TODO: we should list the possible matches somehow */ + return AMBIGUOUS_COMMAND; + } + return entry->code; + } else + return UNKNOWN_COMMAND; } /*----------------------------------------------------------------------*/ static void readCommand(char buf[]) { - char c; - - capitalize = FALSE; - if (anyOutput) newline(); - do { - output("adbg> "); - - if (!readline(buf)) { - newline(); - quitGame(); - } - lin = 1; - c = buf[0]; - } while (c == '\0'); + char c; + + capitalize = FALSE; + if (anyOutput) newline(); + do { + output("adbg> "); + + if (!readline(buf)) { + newline(); + quitGame(); + } + lin = 1; + c = buf[0]; + } while (c == '\0'); } /*----------------------------------------------------------------------*/ static void displaySourceLocation(int line, int fileNumber) { - const char *cause; - if (anyOutput) newline(); - if (breakpointIndex(fileNumber, line) != -1) - cause = "Breakpoint hit at"; - else - cause = "Stepping to"; - printf("%s %s %s:%d\n", debugPrefix, cause, sourceFileName(fileNumber), line); - showSourceLine(fileNumber, line); - printf("\n"); - anyOutput = FALSE; + const char *cause; + if (anyOutput) newline(); + if (breakpointIndex(fileNumber, line) != -1) + cause = "Breakpoint hit at"; + else + cause = "Stepping to"; + printf("%s %s %s:%d\n", debugPrefix, cause, sourceFileName(fileNumber), line); + showSourceLine(fileNumber, line); + printf("\n"); + anyOutput = FALSE; } /*----------------------------------------------------------------------*/ static void toggleSectionTrace() { - if ((saved_traceSection = !saved_traceSection)) - printf("Section trace on."); - else - printf("Section trace off."); + if ((saved_traceSection = !saved_traceSection)) + printf("Section trace on."); + else + printf("Section trace off."); } /*----------------------------------------------------------------------*/ static void toggleInstructionTrace() { - if ((saved_traceInstruction = !saved_traceInstruction)) - printf("Single instruction trace on."); - else - printf("Single instruction trace off."); + if ((saved_traceInstruction = !saved_traceInstruction)) + printf("Single instruction trace on."); + else + printf("Single instruction trace off."); } /*----------------------------------------------------------------------*/ static void toggleSourceTrace() { - if ((saved_traceSource = !saved_traceSource)) - printf("Source code trace on."); - else - printf("Source code trace off."); + if ((saved_traceSource = !saved_traceSource)) + printf("Source code trace on."); + else + printf("Source code trace off."); } /*----------------------------------------------------------------------*/ static void togglePushTrace() { - if ((saved_tracePush = !saved_tracePush)) - printf("Stack Push trace on."); - else - printf("Stack Push trace off."); + if ((saved_tracePush = !saved_tracePush)) + printf("Stack Push trace on."); + else + printf("Stack Push trace off."); } /*----------------------------------------------------------------------*/ static void toggleStackTrace() { - if ((saved_traceStack = !saved_traceStack)) - printf("Full stack trace on."); - else - printf("Full stack trace off."); + if ((saved_traceStack = !saved_traceStack)) + printf("Full stack trace on."); + else + printf("Full stack trace off."); } /*----------------------------------------------------------------------*/ static int parseTraceCommand() { - char *subcommand = strtok(NULL, ""); - const DebugParseEntry *entry; - if (subcommand == 0) - return UNKNOWN_COMMAND; - else { - entry = findEntry(subcommand, traceSubcommand); - if (entry != NULL) { - if (strlen(subcommand) < strlen(entry->command)) { - if (findEntry(subcommand, entry+1) != NULL) - return AMBIGUOUS_COMMAND; - } - return entry->code; - } else - return UNKNOWN_COMMAND; - } + char *subcommand = strtok(NULL, ""); + const DebugParseEntry *entry; + if (subcommand == 0) + return UNKNOWN_COMMAND; + else { + entry = findEntry(subcommand, traceSubcommand); + if (entry != NULL) { + if (strlen(subcommand) < strlen(entry->command)) { + if (findEntry(subcommand, entry + 1) != NULL) + return AMBIGUOUS_COMMAND; + } + return entry->code; + } else + return UNKNOWN_COMMAND; + } } /*----------------------------------------------------------------------*/ static const char *printTraceState(bool state) { - if (state) - return "on - Traces"; - else - return "off - Doesn't trace"; + if (state) + return "on - Traces"; + else + return "off - Doesn't trace"; } /*----------------------------------------------------------------------*/ static void printTrace(void) { - printf("Trace section : %s entry to every section (check, description, event, actor, ...)\n", printTraceState(saved_traceSection)); - printf("Trace source : %s every source line executed\n", printTraceState(saved_traceSource)); - printf("Trace instruction : %s every Amachine instruction executed\n", printTraceState(saved_traceInstruction)); - printf("Trace push : %s every push onto the Amachine stack\n", printTraceState(saved_tracePush)); - printf("Trace stack : %s the complete stack every time\n", printTraceState(saved_traceStack)); + printf("Trace section : %s entry to every section (check, description, event, actor, ...)\n", printTraceState(saved_traceSection)); + printf("Trace source : %s every source line executed\n", printTraceState(saved_traceSource)); + printf("Trace instruction : %s every Amachine instruction executed\n", printTraceState(saved_traceInstruction)); + printf("Trace push : %s every push onto the Amachine stack\n", printTraceState(saved_tracePush)); + printf("Trace stack : %s the complete stack every time\n", printTraceState(saved_traceStack)); } /*----------------------------------------------------------------------*/ static void handleTraceCommand() { - char subcommand = parseTraceCommand(); - - switch (subcommand) { - case TRACE_SECTION_COMMAND: toggleSectionTrace(); break; - case TRACE_SOURCE_COMMAND: toggleSourceTrace(); break; - case TRACE_INSTRUCTION_COMMAND: toggleInstructionTrace(); break; - case TRACE_PUSH_COMMAND: togglePushTrace(); break; - case TRACE_STACK_COMMAND: toggleStackTrace(); break; - case AMBIGUOUS_COMMAND: output("Ambiguous Trace subcommand abbreviation. ? for help."); break; - default: printTrace(); - } + char subcommand = parseTraceCommand(); + + switch (subcommand) { + case TRACE_SECTION_COMMAND: + toggleSectionTrace(); + break; + case TRACE_SOURCE_COMMAND: + toggleSourceTrace(); + break; + case TRACE_INSTRUCTION_COMMAND: + toggleInstructionTrace(); + break; + case TRACE_PUSH_COMMAND: + togglePushTrace(); + break; + case TRACE_STACK_COMMAND: + toggleStackTrace(); + break; + case AMBIGUOUS_COMMAND: + output("Ambiguous Trace subcommand abbreviation. ? for help."); + break; + default: + printTrace(); + } } /*----------------------------------------------------------------------*/ static void handleBreakCommand(int fileNumber) { - char *parameter = strtok(NULL, ":"); - if (parameter != NULL && isalpha((int)parameter[0])) { - fileNumber = sourceFileNumber(parameter); - if (fileNumber == -1) { - printf("No such file: '%s'\n", parameter); - return; - } - parameter = strtok(NULL, ""); - } - if (parameter == NULL) - listBreakpoints(); - else - setBreakpoint(fileNumber, atoi(parameter)); + char *parameter = strtok(NULL, ":"); + if (parameter != NULL && isalpha((int)parameter[0])) { + fileNumber = sourceFileNumber(parameter); + if (fileNumber == -1) { + printf("No such file: '%s'\n", parameter); + return; + } + parameter = strtok(NULL, ""); + } + if (parameter == NULL) + listBreakpoints(); + else + setBreakpoint(fileNumber, atoi(parameter)); } /*----------------------------------------------------------------------*/ static void handleDeleteCommand(bool calledFromBreakpoint, int line, int fileNumber) { - char *parameter = strtok(NULL, ""); - if (parameter == NULL) { - if (calledFromBreakpoint) - deleteBreakpoint(line, fileNumber); - else - printf("No current breakpoint to delete\n"); - } else - deleteBreakpoint(atoi(parameter), fileNumber); + char *parameter = strtok(NULL, ""); + if (parameter == NULL) { + if (calledFromBreakpoint) + deleteBreakpoint(line, fileNumber); + else + printf("No current breakpoint to delete\n"); + } else + deleteBreakpoint(atoi(parameter), fileNumber); } /*----------------------------------------------------------------------*/ static void handleNextCommand(bool calledFromBreakpoint) { - stopAtNextLine = TRUE; - debugOption = FALSE; - if (!calledFromBreakpoint) - current.sourceLine = 0; - restoreInfo(); + stopAtNextLine = TRUE; + debugOption = FALSE; + if (!calledFromBreakpoint) + current.sourceLine = 0; + restoreInfo(); } /*----------------------------------------------------------------------*/ static void handleLocationsCommand() { - char *parameter = strtok(NULL, ""); - if (parameter == 0) - listLocations(); - else - showLocation(atoi(parameter)); + char *parameter = strtok(NULL, ""); + if (parameter == 0) + listLocations(); + else + showLocation(atoi(parameter)); } /*----------------------------------------------------------------------*/ static void handleActorsCommand() { - char *parameter = strtok(NULL, ""); - if (parameter == NULL) - listActors(); - else - showActor(atoi(parameter)); + char *parameter = strtok(NULL, ""); + if (parameter == NULL) + listActors(); + else + showActor(atoi(parameter)); } /*----------------------------------------------------------------------*/ static void handleClassesCommand() { - char *parameter = strtok(NULL, ""); - if (parameter == NULL || strchr(parameter, '*') != 0) { - output("Classes:"); - showClassHierarchy(1, 0); - listInstances(parameter); - } else if (isdigit((int)parameter[0])) - showClass(atoi(parameter)); - else { - printf("You have to give a class index to display. You can't use names (yet)."); - } + char *parameter = strtok(NULL, ""); + if (parameter == NULL || strchr(parameter, '*') != 0) { + output("Classes:"); + showClassHierarchy(1, 0); + listInstances(parameter); + } else if (isdigit((int)parameter[0])) + showClass(atoi(parameter)); + else { + printf("You have to give a class index to display. You can't use names (yet)."); + } } /*----------------------------------------------------------------------*/ static void handleObjectsCommand() { - char *parameter = strtok(NULL, ""); - if (parameter == NULL) - listObjects(); - else - showObject(atoi(parameter)); + char *parameter = strtok(NULL, ""); + if (parameter == NULL) + listObjects(); + else + showObject(atoi(parameter)); } /*----------------------------------------------------------------------*/ static void handleInstancesCommand() { - char *parameter = strtok(NULL, ""); - uint i; - - if (parameter == NULL || strchr(parameter, '*') != 0) - listInstances(parameter); - else if (isdigit((int)parameter[0])) - showInstance(atoi(parameter)); - else { - for (i = 1; i<header->instanceMax; i++) - if (strcmp(parameter, idOfInstance(i)) == 0) { - showInstance(i); - return; - } - printf("No instance named '%s'.", parameter); - } + char *parameter = strtok(NULL, ""); + uint i; + + if (parameter == NULL || strchr(parameter, '*') != 0) + listInstances(parameter); + else if (isdigit((int)parameter[0])) + showInstance(atoi(parameter)); + else { + for (i = 1; i < header->instanceMax; i++) + if (strcmp(parameter, idOfInstance(i)) == 0) { + showInstance(i); + return; + } + printf("No instance named '%s'.", parameter); + } } /*----------------------------------------------------------------------*/ static bool exactSameVersion() { - return header->version[3] == alan.version.version - && header->version[2] == alan.version.revision - && header->version[1] == alan.version.correction - && header->version[0] == alan.version.state[0]; + return header->version[3] == alan.version.version + && header->version[2] == alan.version.revision + && header->version[1] == alan.version.correction + && header->version[0] == alan.version.state[0]; } /*======================================================================*/ -void debug(bool calledFromBreakpoint, int line, int fileNumber) -{ - static bool warned = FALSE; - - saveInfo(); - g_vm->glk_set_style(style_Preformatted); - - if (calledFromBreakpoint) - displaySourceLocation(line, fileNumber); - else { - if (!exactSameVersion() && !warned && !regressionTestOption) { - printf("<WARNING: You are debugging a game which has version %s.>\n", - decodedGameVersion(header->version)); - printf("<That is not exactly the same as this interpreter (%s).>\n", alan.version.string); - printf("<This might cause a lot of trouble. Cross your fingers...>\n"); - warned = TRUE; - } - } - - while (TRUE) { - - char commandLine[200]; - readCommand(commandLine); - char *command = strtok(commandLine, " "); - char commandCode = parseDebugCommand(command); - - switch (commandCode) { - case AMBIGUOUS_COMMAND: output("Ambiguous ADBG command abbreviation. ? for help."); break; - case ACTORS_COMMAND: handleActorsCommand(); break; - case BREAK_COMMAND: handleBreakCommand(fileNumber); break; - case CLASSES_COMMAND: handleClassesCommand(); break; - case DELETE_COMMAND: handleDeleteCommand(calledFromBreakpoint, line, fileNumber); break; - case EVENTS_COMMAND: showEvents(); break; - case EXIT_COMMAND: debugOption = FALSE; restoreInfo(); goto exit_debug; - case FILES_COMMAND: listFiles(); break; - case GO_COMMAND: restoreInfo(); goto exit_debug; - case HELP_COMMAND: handleHelpCommand(); break; - case INSTANCES_COMMAND: handleInstancesCommand(); break; - case TRACE_COMMAND: handleTraceCommand(); break; - case INSTRUCTION_TRACE_COMMAND: toggleInstructionTrace(); break; - case LOCATIONS_COMMAND: handleLocationsCommand(); break; - case NEXT_COMMAND: handleNextCommand(calledFromBreakpoint); goto exit_debug; - case OBJECTS_COMMAND: handleObjectsCommand(); break; - case QUIT_COMMAND: terminate(0); break; - case SECTION_TRACE_COMMAND: toggleSectionTrace(); break; - default: output("Unknown ADBG command. ? for help."); break; - } - } - - exit_debug: +void debug(bool calledFromBreakpoint, int line, int fileNumber) { + static bool warned = FALSE; + + saveInfo(); + g_vm->glk_set_style(style_Preformatted); + + if (calledFromBreakpoint) + displaySourceLocation(line, fileNumber); + else { + if (!exactSameVersion() && !warned && !regressionTestOption) { + printf("<WARNING: You are debugging a game which has version %s.>\n", + decodedGameVersion(header->version)); + printf("<That is not exactly the same as this interpreter (%s).>\n", alan.version.string); + printf("<This might cause a lot of trouble. Cross your fingers...>\n"); + warned = TRUE; + } + } + + while (TRUE) { + + char commandLine[200]; + readCommand(commandLine); + char *command = strtok(commandLine, " "); + char commandCode = parseDebugCommand(command); + + switch (commandCode) { + case AMBIGUOUS_COMMAND: + output("Ambiguous ADBG command abbreviation. ? for help."); + break; + case ACTORS_COMMAND: + handleActorsCommand(); + break; + case BREAK_COMMAND: + handleBreakCommand(fileNumber); + break; + case CLASSES_COMMAND: + handleClassesCommand(); + break; + case DELETE_COMMAND: + handleDeleteCommand(calledFromBreakpoint, line, fileNumber); + break; + case EVENTS_COMMAND: + showEvents(); + break; + case EXIT_COMMAND: + debugOption = FALSE; + restoreInfo(); + goto exit_debug; + case FILES_COMMAND: + listFiles(); + break; + case GO_COMMAND: + restoreInfo(); + goto exit_debug; + case HELP_COMMAND: + handleHelpCommand(); + break; + case INSTANCES_COMMAND: + handleInstancesCommand(); + break; + case TRACE_COMMAND: + handleTraceCommand(); + break; + case INSTRUCTION_TRACE_COMMAND: + toggleInstructionTrace(); + break; + case LOCATIONS_COMMAND: + handleLocationsCommand(); + break; + case NEXT_COMMAND: + handleNextCommand(calledFromBreakpoint); + goto exit_debug; + case OBJECTS_COMMAND: + handleObjectsCommand(); + break; + case QUIT_COMMAND: + terminate(0); + break; + case SECTION_TRACE_COMMAND: + toggleSectionTrace(); + break; + default: + output("Unknown ADBG command. ? for help."); + break; + } + } + +exit_debug: g_vm->glk_set_style(style_Normal); } /*======================================================================*/ -void traceSay(int item) -{ - /* - Say something, but make sure we don't disturb anything and that it is - shown to the player. Needed for tracing. During debugging things are - set up to avoid this problem. - */ - - saveInfo(); - needSpace = FALSE; - col = 1; - if (item == 0) - printf("$null$"); - else - say(item); - needSpace = FALSE; - col = 1; - restoreInfo(); +void traceSay(int item) { + /* + Say something, but make sure we don't disturb anything and that it is + shown to the player. Needed for tracing. During debugging things are + set up to avoid this problem. + */ + + saveInfo(); + needSpace = FALSE; + col = 1; + if (item == 0) + printf("$null$"); + else + say(item); + needSpace = FALSE; + col = 1; + restoreInfo(); } } // End of namespace Alan3 diff --git a/engines/glk/alan3/debug.h b/engines/glk/alan3/debug.h index 3bebe27d29..3bfa32ee6c 100644 --- a/engines/glk/alan3/debug.h +++ b/engines/glk/alan3/debug.h @@ -33,8 +33,8 @@ namespace Alan3 { /* Types: */ struct Breakpoint { - int line; - int file; + int line; + int file; }; diff --git a/engines/glk/alan3/decode.cpp b/engines/glk/alan3/decode.cpp index 41afbddca6..286223ced0 100644 --- a/engines/glk/alan3/decode.cpp +++ b/engines/glk/alan3/decode.cpp @@ -35,101 +35,98 @@ Aword *freq; /* Cumulative character frequencies */ /* PRIVATE DATA */ /* Bit output */ -static int decodeBuffer; /* Bits to be input */ -static int bitsToGo; /* Bits still in buffer */ -static int garbageBits; /* Bits past EOD */ - - -static int inputBit(void) -{ - int bit; - - /* More bits available ? */ - if (!bitsToGo) { - /* No, so get more */ - decodeBuffer = (textFile->pos() >= textFile->size()) ? EOD : textFile->readByte(); - if (decodeBuffer == (int)EOD) { - garbageBits++; - if (garbageBits > VALUEBITS-2) - syserr("Error in encoded data file."); - } else - bitsToGo = 8; /* Another Char, 8 new bits */ - } - bit = decodeBuffer&1; /* Get next bit */ - decodeBuffer = decodeBuffer>>1; /* and remove it */ - bitsToGo--; - return bit; +static int decodeBuffer; /* Bits to be input */ +static int bitsToGo; /* Bits still in buffer */ +static int garbageBits; /* Bits past EOD */ + + +static int inputBit(void) { + int bit; + + /* More bits available ? */ + if (!bitsToGo) { + /* No, so get more */ + decodeBuffer = (textFile->pos() >= textFile->size()) ? EOD : textFile->readByte(); + if (decodeBuffer == (int)EOD) { + garbageBits++; + if (garbageBits > VALUEBITS - 2) + syserr("Error in encoded data file."); + } else + bitsToGo = 8; /* Another Char, 8 new bits */ + } + bit = decodeBuffer & 1; /* Get next bit */ + decodeBuffer = decodeBuffer >> 1; /* and remove it */ + bitsToGo--; + return bit; } /* Current state of decoding */ -static CodeValue value; /* Currently seen code value */ -static CodeValue low, high; /* Current code region */ +static CodeValue value; /* Currently seen code value */ +static CodeValue low, high; /* Current code region */ -void startDecoding(void) -{ - int i; +void startDecoding(void) { + int i; - bitsToGo = 0; - garbageBits = 0; + bitsToGo = 0; + garbageBits = 0; - value = 0; - for (i = 0; i < VALUEBITS; i++) - value = 2*value + inputBit(); - low = 0; - high = TOPVALUE; + value = 0; + for (i = 0; i < VALUEBITS; i++) + value = 2 * value + inputBit(); + low = 0; + high = TOPVALUE; } -int decodeChar(void) -{ - long range; - int f; - int symbol; - - range = (long)(high-low) + 1; - f = (((long)(value-low)+1)*freq[0]-1)/range; - - /* Find the symbol */ - for (symbol = 1; (int)freq[symbol] > f; ++symbol) {} - - high = low + range*freq[symbol-1]/freq[0]-1; - low = low + range*freq[symbol]/freq[0]; - - for (;;) { - if (high < HALF) - ; - else if (low >= HALF) { - value = value - HALF; - low = low - HALF; - high = high - HALF; - } else if (low >= ONEQUARTER && high < THREEQUARTER) { - value = value - ONEQUARTER; - low = low - ONEQUARTER; - high = high - ONEQUARTER; - } else - break; - - /* Scale up the range */ - low = 2*low; - high = 2*high+1; - value = 2*value + inputBit(); - } - return symbol-1; +int decodeChar(void) { + long range; + int f; + int symbol; + + range = (long)(high - low) + 1; + f = (((long)(value - low) + 1) * freq[0] - 1) / range; + + /* Find the symbol */ + for (symbol = 1; (int)freq[symbol] > f; ++symbol) {} + + high = low + range * freq[symbol - 1] / freq[0] - 1; + low = low + range * freq[symbol] / freq[0]; + + for (;;) { + if (high < HALF) + ; + else if (low >= HALF) { + value = value - HALF; + low = low - HALF; + high = high - HALF; + } else if (low >= ONEQUARTER && high < THREEQUARTER) { + value = value - ONEQUARTER; + low = low - ONEQUARTER; + high = high - ONEQUARTER; + } else + break; + + /* Scale up the range */ + low = 2 * low; + high = 2 * high + 1; + value = 2 * value + inputBit(); + } + return symbol - 1; } /* Structure for saved decode info */ struct DecodeInfo { - long fpos; - int buffer; - int bits; - CodeValue value; - CodeValue high; - CodeValue low; + long fpos; + int buffer; + int bits; + CodeValue value; + CodeValue high; + CodeValue low; }; @@ -141,18 +138,17 @@ struct DecodeInfo { restore and continue later. */ -void *pushDecode(void) -{ - DecodeInfo *info; - - info = (DecodeInfo *)allocate(sizeof(DecodeInfo)); - info->fpos = textFile->pos(); - info->buffer = decodeBuffer; - info->bits = bitsToGo; - info->value = value; - info->high = high; - info->low = low; - return(info); +void *pushDecode(void) { + DecodeInfo *info; + + info = (DecodeInfo *)allocate(sizeof(DecodeInfo)); + info->fpos = textFile->pos(); + info->buffer = decodeBuffer; + info->bits = bitsToGo; + info->value = value; + info->high = high; + info->low = low; + return (info); } @@ -164,18 +160,17 @@ void *pushDecode(void) continue after having decoded something else. */ -void popDecode(void *i) -{ - DecodeInfo *info = (DecodeInfo *) i; - - textFile->seek(info->fpos); - decodeBuffer = info->buffer; - bitsToGo = info->bits; - value = info->value; - high = info->high; - low = info->low; - - free(info); +void popDecode(void *i) { + DecodeInfo *info = (DecodeInfo *) i; + + textFile->seek(info->fpos); + decodeBuffer = info->buffer; + bitsToGo = info->bits; + value = info->value; + high = info->high; + low = info->low; + + free(info); } } // End of namespace Alan3 diff --git a/engines/glk/alan3/dictionary.cpp b/engines/glk/alan3/dictionary.cpp index 375b7f72ca..de9d40ce05 100644 --- a/engines/glk/alan3/dictionary.cpp +++ b/engines/glk/alan3/dictionary.cpp @@ -36,99 +36,99 @@ int conjWord; /* First conjunction in dictionary, for ',' */ /* Word class query methods, move to Word.c */ /* Word classes are numbers but in the dictionary they are generated as bits */ static bool isVerb(int wordCode) { - return wordCode < dictionarySize && (dictionary[wordCode].classBits&VERB_BIT)!=0; + return wordCode < dictionarySize && (dictionary[wordCode].classBits & VERB_BIT) != 0; } bool isVerbWord(int wordIndex) { - return isVerb(playerWords[wordIndex].code); + return isVerb(playerWords[wordIndex].code); } bool isConjunction(int wordCode) { - return wordCode < dictionarySize && (dictionary[wordCode].classBits&CONJUNCTION_BIT)!=0; + return wordCode < dictionarySize && (dictionary[wordCode].classBits & CONJUNCTION_BIT) != 0; } bool isConjunctionWord(int wordIndex) { - return isConjunction(playerWords[wordIndex].code); + return isConjunction(playerWords[wordIndex].code); } static bool isExcept(int wordCode) { - return wordCode < dictionarySize && (dictionary[wordCode].classBits&EXCEPT_BIT)!=0; + return wordCode < dictionarySize && (dictionary[wordCode].classBits & EXCEPT_BIT) != 0; } bool isExceptWord(int wordIndex) { - return isExcept(playerWords[wordIndex].code); + return isExcept(playerWords[wordIndex].code); } static bool isThem(int wordCode) { - return wordCode < dictionarySize && (dictionary[wordCode].classBits&THEM_BIT)!=0; + return wordCode < dictionarySize && (dictionary[wordCode].classBits & THEM_BIT) != 0; } bool isThemWord(int wordIndex) { - return isThem(playerWords[wordIndex].code); + return isThem(playerWords[wordIndex].code); } static bool isIt(int wordCode) { - return wordCode < dictionarySize && (dictionary[wordCode].classBits&IT_BIT)!=0; + return wordCode < dictionarySize && (dictionary[wordCode].classBits & IT_BIT) != 0; } bool isItWord(int wordIndex) { - return isIt(playerWords[wordIndex].code); + return isIt(playerWords[wordIndex].code); } static bool isNoun(int wordCode) { - return wordCode < dictionarySize && (dictionary[wordCode].classBits&NOUN_BIT)!=0; + return wordCode < dictionarySize && (dictionary[wordCode].classBits & NOUN_BIT) != 0; } bool isNounWord(int wordIndex) { - return isNoun(playerWords[wordIndex].code); + return isNoun(playerWords[wordIndex].code); } static bool isAdjective(int wordCode) { - return wordCode < dictionarySize && (dictionary[wordCode].classBits&ADJECTIVE_BIT)!=0; + return wordCode < dictionarySize && (dictionary[wordCode].classBits & ADJECTIVE_BIT) != 0; } bool isAdjectiveWord(int wordIndex) { - return isAdjective(playerWords[wordIndex].code); + return isAdjective(playerWords[wordIndex].code); } static bool isPreposition(int wordCode) { - return wordCode < dictionarySize && (dictionary[wordCode].classBits&PREPOSITION_BIT)!=0; + return wordCode < dictionarySize && (dictionary[wordCode].classBits & PREPOSITION_BIT) != 0; } bool isPrepositionWord(int wordIndex) { - return isPreposition(playerWords[wordIndex].code); + return isPreposition(playerWords[wordIndex].code); } bool isAll(int wordCode) { - return wordCode < dictionarySize && (dictionary[wordCode].classBits&ALL_BIT)!=0; + return wordCode < dictionarySize && (dictionary[wordCode].classBits & ALL_BIT) != 0; } bool isAllWord(int wordIndex) { - return isAll(playerWords[wordIndex].code); + return isAll(playerWords[wordIndex].code); } static bool isDir(int wordCode) { - return wordCode < dictionarySize && (dictionary[wordCode].classBits&DIRECTION_BIT)!=0; + return wordCode < dictionarySize && (dictionary[wordCode].classBits & DIRECTION_BIT) != 0; } bool isDirectionWord(int wordIndex) { - return isDir(playerWords[wordIndex].code); + return isDir(playerWords[wordIndex].code); } bool isNoise(int wordCode) { - return wordCode < dictionarySize && (dictionary[wordCode].classBits&NOISE_BIT)!=0; + return wordCode < dictionarySize && (dictionary[wordCode].classBits & NOISE_BIT) != 0; } bool isPronoun(int wordCode) { - return wordCode < dictionarySize && (dictionary[wordCode].classBits&PRONOUN_BIT)!=0; + return wordCode < dictionarySize && (dictionary[wordCode].classBits & PRONOUN_BIT) != 0; } bool isPronounWord(int wordIndex) { - return isPronoun(playerWords[wordIndex].code); + return isPronoun(playerWords[wordIndex].code); } bool isLiteralWord(int wordIndex) { - return playerWords[wordIndex].code >= dictionarySize; + return playerWords[wordIndex].code >= dictionarySize; } } // End of namespace Alan3 diff --git a/engines/glk/alan3/event.h b/engines/glk/alan3/event.h index 4855548f1b..0a76d3c8c2 100644 --- a/engines/glk/alan3/event.h +++ b/engines/glk/alan3/event.h @@ -31,9 +31,9 @@ namespace Alan3 { /* TYPES */ typedef struct EventQueueEntry { /* EVENT QUEUE ENTRIES */ - int after; - int event; - int where; + int after; + int event; + int where; } EventQueueEntry; diff --git a/engines/glk/alan3/exe.cpp b/engines/glk/alan3/exe.cpp index 445b00dc6d..e3e0e0c60f 100644 --- a/engines/glk/alan3/exe.cpp +++ b/engines/glk/alan3/exe.cpp @@ -69,50 +69,59 @@ Common::SeekableReadStream *textFile; /*======================================================================*/ void setStyle(int style) { - switch (style) { - case NORMAL_STYLE: g_vm->glk_set_style(style_Normal); break; - case EMPHASIZED_STYLE: g_vm->glk_set_style(style_Emphasized); break; - case PREFORMATTED_STYLE: g_vm->glk_set_style(style_Preformatted); break; - case ALERT_STYLE: g_vm->glk_set_style(style_Alert); break; - case QUOTE_STYLE: g_vm->glk_set_style(style_BlockQuote); break; - } + switch (style) { + case NORMAL_STYLE: + g_vm->glk_set_style(style_Normal); + break; + case EMPHASIZED_STYLE: + g_vm->glk_set_style(style_Emphasized); + break; + case PREFORMATTED_STYLE: + g_vm->glk_set_style(style_Preformatted); + break; + case ALERT_STYLE: + g_vm->glk_set_style(style_Alert); + break; + case QUOTE_STYLE: + g_vm->glk_set_style(style_BlockQuote); + break; + } } /*======================================================================*/ -void print(Aword fpos, Aword len) -{ - char str[2*WIDTH]; /* String buffer */ - uint outlen = 0; /* Current output length */ - int ch = 0; - int i; - long savfp = 0; /* Temporary saved text file position */ - static bool printFlag = FALSE; /* Printing already? */ - bool savedPrintFlag = printFlag; - void *info = NULL; /* Saved decoding info */ - - - if (len == 0) return; - - if (isHere(HERO, /*TRUE*/ DIRECT)) { /* Check if the player will see it */ - if (printFlag) { /* Already printing? */ - /* Save current text file position and/or decoding info */ - if (header->pack) - info = pushDecode(); - else - savfp = textFile->pos(); - } - printFlag = TRUE; /* We're printing now! */ - - /* Position to start of text */ - textFile->seek(fpos+header->stringOffset); - - if (header->pack) - startDecoding(); - for (outlen = 0; outlen != len; outlen = outlen + strlen(str)) { - /* Fill the buffer from the beginning */ - for (i = 0; i <= WIDTH || (i > WIDTH && ch != ' '); i++) { - if (outlen + i == len) /* No more characters? */ - break; +void print(Aword fpos, Aword len) { + char str[2 * WIDTH]; /* String buffer */ + uint outlen = 0; /* Current output length */ + int ch = 0; + int i; + long savfp = 0; /* Temporary saved text file position */ + static bool printFlag = FALSE; /* Printing already? */ + bool savedPrintFlag = printFlag; + void *info = NULL; /* Saved decoding info */ + + + if (len == 0) return; + + if (isHere(HERO, /*TRUE*/ DIRECT)) { /* Check if the player will see it */ + if (printFlag) { /* Already printing? */ + /* Save current text file position and/or decoding info */ + if (header->pack) + info = pushDecode(); + else + savfp = textFile->pos(); + } + printFlag = TRUE; /* We're printing now! */ + + /* Position to start of text */ + textFile->seek(fpos + header->stringOffset); + + if (header->pack) + startDecoding(); + for (outlen = 0; outlen != len; outlen = outlen + strlen(str)) { + /* Fill the buffer from the beginning */ + for (i = 0; i <= WIDTH || (i > WIDTH && ch != ' '); i++) { + if (outlen + i == len) /* No more characters? */ + break; if (header->pack) ch = decodeChar(); else @@ -120,104 +129,100 @@ void print(Aword fpos, Aword len) str[i] = ch; if (textFile->pos() >= textFile->size()) /* Or end of text? */ - break; - } - str[i] = '\0'; + break; + } + str[i] = '\0'; - output(str); - } + output(str); + } - /* And restore */ - printFlag = savedPrintFlag; - if (printFlag) { - if (header->pack) - popDecode(info); - else - textFile->seek(savfp); - } - } + /* And restore */ + printFlag = savedPrintFlag; + if (printFlag) { + if (header->pack) + popDecode(info); + else + textFile->seek(savfp); + } + } } /*======================================================================*/ -void sys(Aword fpos, Aword len) -{ +void sys(Aword fpos, Aword len) { ::error("sys calls are unsupported"); } /*======================================================================*/ -char *getStringFromFile(Aword fpos, Aword len) -{ - char *buf = (char *)allocate(len+1); - char *bufp = buf; +char *getStringFromFile(Aword fpos, Aword len) { + char *buf = (char *)allocate(len + 1); + char *bufp = buf; - /* Position to start of text */ - textFile->seek(fpos+header->stringOffset); + /* Position to start of text */ + textFile->seek(fpos + header->stringOffset); - if (header->pack) - startDecoding(); - while (len--) - if (header->pack) - *(bufp++) = decodeChar(); - else - *(bufp++) = textFile->readByte(); + if (header->pack) + startDecoding(); + while (len--) + if (header->pack) + *(bufp++) = decodeChar(); + else + *(bufp++) = textFile->readByte(); - /* Terminate string with zero */ - *bufp = '\0'; + /* Terminate string with zero */ + *bufp = '\0'; - return buf; + return buf; } /*======================================================================*/ -void score(Aword sc) -{ - if (sc == 0) { - ParameterArray messageParameters = newParameterArray(); - addParameterForInteger(messageParameters, current.score); - addParameterForInteger(messageParameters, header->maximumScore); - addParameterForInteger(messageParameters, current.tick); - printMessageWithParameters(M_SCORE, messageParameters); - freeParameterArray(messageParameters); - } else { - current.score += scores[sc-1]; - scores[sc-1] = 0; - gameStateChanged = TRUE; - } +void score(Aword sc) { + if (sc == 0) { + ParameterArray messageParameters = newParameterArray(); + addParameterForInteger(messageParameters, current.score); + addParameterForInteger(messageParameters, header->maximumScore); + addParameterForInteger(messageParameters, current.tick); + printMessageWithParameters(M_SCORE, messageParameters); + freeParameterArray(messageParameters); + } else { + current.score += scores[sc - 1]; + scores[sc - 1] = 0; + gameStateChanged = TRUE; + } } /*======================================================================*/ -void visits(Aword v) -{ - current.visits = v; +void visits(Aword v) { + current.visits = v; } /*----------------------------------------------------------------------*/ static void sayUndoneCommand(char *words) { - static Parameter *messageParameters = NULL; - messageParameters = (Parameter *)ensureParameterArrayAllocated(messageParameters); + static Parameter *messageParameters = NULL; + messageParameters = (Parameter *)ensureParameterArrayAllocated(messageParameters); - current.location = where(HERO, DIRECT); - clearParameterArray(messageParameters); - addParameterForString(&messageParameters[0], words); - setEndOfArray(&messageParameters[1]); - printMessageWithParameters(M_UNDONE, messageParameters); + current.location = where(HERO, DIRECT); + clearParameterArray(messageParameters); + addParameterForString(&messageParameters[0], words); + setEndOfArray(&messageParameters[1]); + printMessageWithParameters(M_UNDONE, messageParameters); } /*======================================================================*/ void undo(void) { - forgetGameState(); - if (anySavedState()) { - recallGameState(); - sayUndoneCommand(recreatePlayerCommand()); - } else { - printMessage(M_NO_UNDO); - } + forgetGameState(); + if (anySavedState()) { + recallGameState(); + sayUndoneCommand(recreatePlayerCommand()); + } else { + printMessage(M_NO_UNDO); + } #ifdef TODO longjmp(returnLabel, UNDO_RETURN); #else @@ -227,41 +232,40 @@ void undo(void) { /*======================================================================*/ -void quitGame(void) -{ +void quitGame(void) { #ifdef TODO char buf[80]; - current.location = where(HERO, DIRECT); - para(); - while (TRUE) { - col = 1; - statusline(); - printMessage(M_QUITACTION); - - if (!readline(buf)) terminate(0); - if (strcasecmp(buf, "restart") == 0) - longjmp(restartLabel, TRUE); - else if (strcasecmp(buf, "restore") == 0) { - restore(); - return; - } else if (strcasecmp(buf, "quit") == 0) { - terminate(0); - } else if (strcasecmp(buf, "undo") == 0) { - if (gameStateChanged) { - rememberCommands(); - rememberGameState(); - undo(); - } else { - if (anySavedState()) { - recallGameState(); - sayUndoneCommand(playerWordsAsCommandString()); - } else - printMessage(M_NO_UNDO); - longjmp(returnLabel, UNDO_RETURN); - } - } - } + current.location = where(HERO, DIRECT); + para(); + while (TRUE) { + col = 1; + statusline(); + printMessage(M_QUITACTION); + + if (!readline(buf)) terminate(0); + if (strcasecmp(buf, "restart") == 0) + longjmp(restartLabel, TRUE); + else if (strcasecmp(buf, "restore") == 0) { + restore(); + return; + } else if (strcasecmp(buf, "quit") == 0) { + terminate(0); + } else if (strcasecmp(buf, "undo") == 0) { + if (gameStateChanged) { + rememberCommands(); + rememberGameState(); + undo(); + } else { + if (anySavedState()) { + recallGameState(); + sayUndoneCommand(playerWordsAsCommandString()); + } else + printMessage(M_NO_UNDO); + longjmp(returnLabel, UNDO_RETURN); + } + } + } #endif syserr("Fallthrough in QUIT"); } @@ -269,16 +273,15 @@ void quitGame(void) /*======================================================================*/ -void restartGame(void) -{ +void restartGame(void) { #ifdef TODO Aint previousLocation = current.location; current.location = where(HERO, DIRECT); - para(); - if (confirm(M_REALLY)) { - longjmp(restartLabel, TRUE); - } - current.location = previousLocation; + para(); + if (confirm(M_REALLY)) { + longjmp(restartLabel, TRUE); + } + current.location = previousLocation; #else ::error("TODO: restartGame"); #endif @@ -287,258 +290,250 @@ void restartGame(void) /*======================================================================*/ -void cancelEvent(Aword theEvent) -{ - int i; - - for (i = eventQueueTop-1; i>=0; i--) - if (eventQueue[i].event == (int)theEvent) { - while (i < eventQueueTop-1) { - eventQueue[i].event = eventQueue[i+1].event; - eventQueue[i].after = eventQueue[i+1].after; - eventQueue[i].where = eventQueue[i+1].where; - i++; - } - eventQueueTop--; - return; - } +void cancelEvent(Aword theEvent) { + int i; + + for (i = eventQueueTop - 1; i >= 0; i--) + if (eventQueue[i].event == (int)theEvent) { + while (i < eventQueueTop - 1) { + eventQueue[i].event = eventQueue[i + 1].event; + eventQueue[i].after = eventQueue[i + 1].after; + eventQueue[i].where = eventQueue[i + 1].where; + i++; + } + eventQueueTop--; + return; + } } /*----------------------------------------------------------------------*/ -static void increaseEventQueue(void) -{ - eventQueue = (EventQueueEntry *)realloc(eventQueue, (eventQueueTop+2)*sizeof(EventQueueEntry)); - if (eventQueue == NULL) syserr("Out of memory in increaseEventQueue()"); +static void increaseEventQueue(void) { + eventQueue = (EventQueueEntry *)realloc(eventQueue, (eventQueueTop + 2) * sizeof(EventQueueEntry)); + if (eventQueue == NULL) syserr("Out of memory in increaseEventQueue()"); - eventQueueSize = eventQueueTop + 2; + eventQueueSize = eventQueueTop + 2; } /*----------------------------------------------------------------------*/ static void moveEvent(int to, int from) { - eventQueue[to].event = eventQueue[from].event; - eventQueue[to].after = eventQueue[from].after; - eventQueue[to].where = eventQueue[from].where; + eventQueue[to].event = eventQueue[from].event; + eventQueue[to].after = eventQueue[from].after; + eventQueue[to].where = eventQueue[from].where; } /*======================================================================*/ -void schedule(Aword event, Aword where, Aword after) -{ - uint i; +void schedule(Aword event, Aword where, Aword after) { + uint i; - if (event == 0) syserr("NULL event"); + if (event == 0) syserr("NULL event"); - cancelEvent(event); - /* Check for overflow */ - if (eventQueue == nullptr || eventQueueTop == eventQueueSize) { - increaseEventQueue(); + cancelEvent(event); + /* Check for overflow */ + if (eventQueue == nullptr || eventQueueTop == eventQueueSize) { + increaseEventQueue(); assert(eventQueue); } - /* Bubble this event down */ - for (i = eventQueueTop; i >= 1 && eventQueue[i-1].after <= (int)after; i--) { - moveEvent(i, i-1); - } + /* Bubble this event down */ + for (i = eventQueueTop; i >= 1 && eventQueue[i - 1].after <= (int)after; i--) { + moveEvent(i, i - 1); + } - eventQueue[i].after = after; - eventQueue[i].where = where; - eventQueue[i].event = event; - eventQueueTop++; + eventQueue[i].after = after; + eventQueue[i].where = where; + eventQueue[i].event = event; + eventQueueTop++; } // TODO Move to string.c? /*======================================================================*/ -Aptr concat(Aptr as1, Aptr as2) -{ - char *s1 = (char *)fromAptr(as1); - char *s2 = (char *)fromAptr(as2); - char *result = (char *)allocate(strlen((char*)s1)+strlen((char*)s2)+1); - strcpy(result, s1); - strcat(result, s2); - return toAptr(result); +Aptr concat(Aptr as1, Aptr as2) { + char *s1 = (char *)fromAptr(as1); + char *s2 = (char *)fromAptr(as2); + char *result = (char *)allocate(strlen((char *)s1) + strlen((char *)s2) + 1); + strcpy(result, s1); + strcat(result, s2); + return toAptr(result); } /*----------------------------------------------------------------------*/ -static char *stripCharsFromStringForwards(int count, char *initialString, char **theRest) -{ - int stripPosition; - char *strippedString; - char *rest; - - if (count > (int)strlen(initialString)) - stripPosition = strlen(initialString); - else - stripPosition = count; - rest = strdup(&initialString[stripPosition]); - strippedString = strdup(initialString); - strippedString[stripPosition] = '\0'; - *theRest = rest; - return strippedString; +static char *stripCharsFromStringForwards(int count, char *initialString, char **theRest) { + int stripPosition; + char *strippedString; + char *rest; + + if (count > (int)strlen(initialString)) + stripPosition = strlen(initialString); + else + stripPosition = count; + rest = strdup(&initialString[stripPosition]); + strippedString = strdup(initialString); + strippedString[stripPosition] = '\0'; + *theRest = rest; + return strippedString; } /*----------------------------------------------------------------------*/ static char *stripCharsFromStringBackwards(Aint count, char *initialString, char **theRest) { - int stripPosition; - char *strippedString; - char *rest; + int stripPosition; + char *strippedString; + char *rest; - if (count > (int)strlen(initialString)) - stripPosition = 0; - else - stripPosition = strlen(initialString)-count; - strippedString = strdup(&initialString[stripPosition]); - rest = strdup(initialString); - rest[stripPosition] = '\0'; - *theRest = rest; - return strippedString; + if (count > (int)strlen(initialString)) + stripPosition = 0; + else + stripPosition = strlen(initialString) - count; + strippedString = strdup(&initialString[stripPosition]); + rest = strdup(initialString); + rest[stripPosition] = '\0'; + *theRest = rest; + return strippedString; } /*----------------------------------------------------------------------*/ static int countLeadingBlanks(char *string, int position) { - static char blanks[] = " "; - return strspn(&string[position], blanks); + static char blanks[] = " "; + return strspn(&string[position], blanks); } /*----------------------------------------------------------------------*/ -static int skipWordForwards(char *string, int position) -{ - char separators[] = " .,?"; +static int skipWordForwards(char *string, int position) { + char separators[] = " .,?"; - uint i; + uint i; - for (i = position; i<=strlen(string) && strchr(separators, string[i]) == NULL; i++) - ; - return i; + for (i = position; i <= strlen(string) && strchr(separators, string[i]) == NULL; i++) + ; + return i; } /*----------------------------------------------------------------------*/ static char *stripWordsFromStringForwards(Aint count, char *initialString, char **theRest) { - int skippedChars; - int position = 0; - char *stripped; - int i; - - for (i = count; i>0; i--) { - /* Ignore any initial blanks */ - skippedChars = countLeadingBlanks(initialString, position); - position += skippedChars; - position = skipWordForwards(initialString, position); - } + int skippedChars; + int position = 0; + char *stripped; + int i; + + for (i = count; i > 0; i--) { + /* Ignore any initial blanks */ + skippedChars = countLeadingBlanks(initialString, position); + position += skippedChars; + position = skipWordForwards(initialString, position); + } - stripped = (char *)allocate(position+1); - strncpy(stripped, initialString, position); - stripped[position] = '\0'; + stripped = (char *)allocate(position + 1); + strncpy(stripped, initialString, position); + stripped[position] = '\0'; - skippedChars = countLeadingBlanks(initialString, position); - *theRest = strdup(&initialString[position+skippedChars]); + skippedChars = countLeadingBlanks(initialString, position); + *theRest = strdup(&initialString[position + skippedChars]); - return(stripped); + return (stripped); } /*----------------------------------------------------------------------*/ -static int skipWordBackwards(char *string, int position) -{ - char separators[] = " .,?"; - int i; +static int skipWordBackwards(char *string, int position) { + char separators[] = " .,?"; + int i; - for (i = position; i>0 && strchr(separators, string[i-1]) == NULL; i--) - ; - return i; + for (i = position; i > 0 && strchr(separators, string[i - 1]) == NULL; i--) + ; + return i; } /*----------------------------------------------------------------------*/ static int countTrailingBlanks(char *string, int position) { - int skippedChars, i; - skippedChars = 0; + int skippedChars, i; + skippedChars = 0; - if (position > (int)strlen(string)-1) - syserr("position > length in countTrailingBlanks"); - for (i = position; i >= 0 && string[i] == ' '; i--) - skippedChars++; - return(skippedChars); + if (position > (int)strlen(string) - 1) + syserr("position > length in countTrailingBlanks"); + for (i = position; i >= 0 && string[i] == ' '; i--) + skippedChars++; + return (skippedChars); } /*----------------------------------------------------------------------*/ static char *stripWordsFromStringBackwards(Aint count, char *initialString, char **theRest) { - int skippedChars; - char *stripped; - int strippedLength; - int position = strlen(initialString); - int i; - - for (i = count; i>0 && position>0; i--) { - position -= 1; - /* Ignore trailing blanks */ - skippedChars = countTrailingBlanks(initialString, position); - if (position - skippedChars < 0) break; /* No more words to strip */ - position -= skippedChars; - position = skipWordBackwards(initialString, position); - } - - skippedChars = countLeadingBlanks(initialString, 0); - strippedLength = strlen(initialString)-position-skippedChars; - stripped = (char *)allocate(strippedLength+1); - strncpy(stripped, &initialString[position+skippedChars], strippedLength); - stripped[strippedLength] = '\0'; - - if (position > 0) { - skippedChars = countTrailingBlanks(initialString, position-1); - position -= skippedChars; - } - *theRest = strdup(initialString); - (*theRest)[position] = '\0'; - return(stripped); + int skippedChars; + char *stripped; + int strippedLength; + int position = strlen(initialString); + int i; + + for (i = count; i > 0 && position > 0; i--) { + position -= 1; + /* Ignore trailing blanks */ + skippedChars = countTrailingBlanks(initialString, position); + if (position - skippedChars < 0) break; /* No more words to strip */ + position -= skippedChars; + position = skipWordBackwards(initialString, position); + } + + skippedChars = countLeadingBlanks(initialString, 0); + strippedLength = strlen(initialString) - position - skippedChars; + stripped = (char *)allocate(strippedLength + 1); + strncpy(stripped, &initialString[position + skippedChars], strippedLength); + stripped[strippedLength] = '\0'; + + if (position > 0) { + skippedChars = countTrailingBlanks(initialString, position - 1); + position -= skippedChars; + } + *theRest = strdup(initialString); + (*theRest)[position] = '\0'; + return (stripped); } /*======================================================================*/ -Aptr strip(bool stripFromBeginningNotEnd, int count, bool stripWordsNotChars, int id, int atr) -{ - char *initialString = (char *)fromAptr(getInstanceAttribute(id, atr)); - char *theStripped; - char *theRest; - - if (stripFromBeginningNotEnd) { - if (stripWordsNotChars) - theStripped = stripWordsFromStringForwards(count, initialString, &theRest); - else - theStripped = stripCharsFromStringForwards(count, initialString, &theRest); - } else { - if (stripWordsNotChars) - theStripped = stripWordsFromStringBackwards(count, initialString, &theRest); - else - theStripped = stripCharsFromStringBackwards(count, initialString, &theRest); - } - setInstanceStringAttribute(id, atr, theRest); - return toAptr(theStripped); +Aptr strip(bool stripFromBeginningNotEnd, int count, bool stripWordsNotChars, int id, int atr) { + char *initialString = (char *)fromAptr(getInstanceAttribute(id, atr)); + char *theStripped; + char *theRest; + + if (stripFromBeginningNotEnd) { + if (stripWordsNotChars) + theStripped = stripWordsFromStringForwards(count, initialString, &theRest); + else + theStripped = stripCharsFromStringForwards(count, initialString, &theRest); + } else { + if (stripWordsNotChars) + theStripped = stripWordsFromStringBackwards(count, initialString, &theRest); + else + theStripped = stripCharsFromStringBackwards(count, initialString, &theRest); + } + setInstanceStringAttribute(id, atr, theRest); + return toAptr(theStripped); } /*======================================================================*/ int getContainerMember(int container, int index, bool directly) { - uint i; - Aint count = 0; - - for (i = 1; i <= header->instanceMax; i++) { - if (isIn(i, container, DIRECT)) { - count++; - if (count == index) - return i; - } - } - apperr("Index not in container in 'containerMember()'"); - return 0; + uint i; + Aint count = 0; + + for (i = 1; i <= header->instanceMax; i++) { + if (isIn(i, container, DIRECT)) { + count++; + if (count == index) + return i; + } + } + apperr("Index not in container in 'containerMember()'"); + return 0; } @@ -551,157 +546,149 @@ int getContainerMember(int container, int index, bool directly) { /*======================================================================*/ void showImage(int image, int align) { - uint ecode; + uint ecode; - if ((g_vm->glk_gestalt(gestalt_Graphics, 0) == 1) && - (g_vm->glk_gestalt(gestalt_DrawImage, wintype_TextBuffer) == 1)) { + if ((g_vm->glk_gestalt(gestalt_Graphics, 0) == 1) && + (g_vm->glk_gestalt(gestalt_DrawImage, wintype_TextBuffer) == 1)) { g_vm->glk_window_flow_break(glkMainWin); - printf("\n"); - ecode = g_vm->glk_image_draw(glkMainWin, image, imagealign_MarginLeft, 0); - (void)ecode; - } + printf("\n"); + ecode = g_vm->glk_image_draw(glkMainWin, image, imagealign_MarginLeft, 0); + (void)ecode; + } } /*======================================================================*/ -void playSound(int sound) -{ +void playSound(int sound) { #ifdef GLK_MODULE_SOUND - static schanid_t soundChannel = NULL; + static schanid_t soundChannel = NULL; - if (g_vm->glk_gestalt(gestalt_Sound, 0) == 1) { - if (soundChannel == NULL) - soundChannel = g_vm->glk_schannel_create(0); - if (soundChannel != NULL) { + if (g_vm->glk_gestalt(gestalt_Sound, 0) == 1) { + if (soundChannel == NULL) + soundChannel = g_vm->glk_schannel_create(0); + if (soundChannel != NULL) { g_vm->glk_schannel_stop(soundChannel); - (void)g_vm->glk_schannel_play(soundChannel, sound); - } - } + (void)g_vm->glk_schannel_play(soundChannel, sound); + } + } #endif } /*======================================================================*/ -void empty(int cnt, int whr) -{ - uint i; +void empty(int cnt, int whr) { + uint i; - for (i = 1; i <= header->instanceMax; i++) - if (isIn(i, cnt, DIRECT)) - locate(i, whr); + for (i = 1; i <= header->instanceMax; i++) + if (isIn(i, cnt, DIRECT)) + locate(i, whr); } /*======================================================================*/ -void use(int actor, int script) -{ - char str[80]; - StepEntry *step; +void use(int actor, int script) { + char str[80]; + StepEntry *step; - if (!isAActor(actor)) { - sprintf(str, "Instance is not an Actor (%d).", actor); - syserr(str); - } + if (!isAActor(actor)) { + sprintf(str, "Instance is not an Actor (%d).", actor); + syserr(str); + } - admin[actor].script = script; - admin[actor].step = 0; - step = stepOf(actor); - if (step != NULL && step->after != 0) { - admin[actor].waitCount = evaluate(step->after); - } + admin[actor].script = script; + admin[actor].step = 0; + step = stepOf(actor); + if (step != NULL && step->after != 0) { + admin[actor].waitCount = evaluate(step->after); + } - gameStateChanged = TRUE; + gameStateChanged = TRUE; } /*======================================================================*/ -void stop(int act) -{ - char str[80]; +void stop(int act) { + char str[80]; - if (!isAActor(act)) { - sprintf(str, "Instance is not an Actor (%d).", act); - syserr(str); - } + if (!isAActor(act)) { + sprintf(str, "Instance is not an Actor (%d).", act); + syserr(str); + } - admin[act].script = 0; - admin[act].step = 0; + admin[act].script = 0; + admin[act].step = 0; - gameStateChanged = TRUE; + gameStateChanged = TRUE; } static int randomValue = 0; /*----------------------------------------------------------------------*/ -int randomInteger(int from, int to) -{ - if (regressionTestOption) { - int ret = from + randomValue; - /* Generate them in sequence */ - if (ret > to) { - ret = from; - randomValue = 1; - } else if (ret == to) - randomValue = 0; - else - randomValue++; - return ret; - } else { - if (to == from) - return to; - else if (to > from) - return (rand()/10)%(to-from+1)+from; - else - return (rand()/10)%(from-to+1)+to; - } +int randomInteger(int from, int to) { + if (regressionTestOption) { + int ret = from + randomValue; + /* Generate them in sequence */ + if (ret > to) { + ret = from; + randomValue = 1; + } else if (ret == to) + randomValue = 0; + else + randomValue++; + return ret; + } else { + if (to == from) + return to; + else if (to > from) + return (rand() / 10) % (to - from + 1) + from; + else + return (rand() / 10) % (from - to + 1) + to; + } } /*----------------------------------------------------------------------*/ -bool between(int val, int low, int high) -{ - if (high > low) - return low <= val && val <= high; - else - return high <= val && val <= low; +bool between(int val, int low, int high) { + if (high > low) + return low <= val && val <= high; + else + return high <= val && val <= low; } /*======================================================================*/ -bool contains(Aptr string, Aptr substring) -{ - bool found; +bool contains(Aptr string, Aptr substring) { + bool found; - strlow((char *)fromAptr(string)); - strlow((char *)fromAptr(substring)); + strlow((char *)fromAptr(string)); + strlow((char *)fromAptr(substring)); - found = (strstr((char *)fromAptr(string), (char *)fromAptr(substring)) != 0); + found = (strstr((char *)fromAptr(string), (char *)fromAptr(substring)) != 0); - return found; + return found; } /*======================================================================*/ -bool streq(char a[], char b[]) -{ - bool eq; +bool streq(char a[], char b[]) { + bool eq; - strlow(a); - strlow(b); + strlow(a); + strlow(b); - eq = (strcmp(a, b) == 0); + eq = (strcmp(a, b) == 0); - return eq; + return eq; } /*======================================================================*/ void startTranscript(void) { - if (logFile == NULL) { + if (logFile == NULL) { Common::String filename = g_vm->getTargetName() + ".log"; uint fileUsage = transcriptOption ? fileusage_Transcript : fileusage_InputRecord; @@ -721,7 +708,7 @@ void startTranscript(void) { /*======================================================================*/ void stopTranscript(void) { if (logFile != NULL) { - if (transcriptOption|| logOption) + if (transcriptOption || logOption) delete logFile; logFile = NULL; diff --git a/engines/glk/alan3/fnmatch.cpp b/engines/glk/alan3/fnmatch.cpp index 203a7bf4b3..970567261e 100644 --- a/engines/glk/alan3/fnmatch.cpp +++ b/engines/glk/alan3/fnmatch.cpp @@ -71,163 +71,162 @@ namespace Alan3 { static int rangematch(const char *, char, int, char **); int fnmatch(const char *pattern, const char *string, int flags) { - const char *stringstart; - char *newp; - char c, test; - - for (stringstart = string;;) - switch (c = *pattern++) { - case EOS: - if ((flags & FNM_LEADING_DIR) && *string == '/') - return (0); - return (*string == EOS ? 0 : FNM_NOMATCH); - case '?': - if (*string == EOS) - return (FNM_NOMATCH); - if (*string == '/' && (flags & FNM_PATHNAME)) - return (FNM_NOMATCH); - if (*string == '.' && (flags & FNM_PERIOD) && - (string == stringstart || - ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) - return (FNM_NOMATCH); - ++string; - break; - case '*': - c = *pattern; - /* Collapse multiple stars. */ - while (c == '*') - c = *++pattern; - - if (*string == '.' && (flags & FNM_PERIOD) && - (string == stringstart || - ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) - return (FNM_NOMATCH); - - /* Optimize for pattern with * at end or before /. */ - if (c == EOS) - if (flags & FNM_PATHNAME) - return ((flags & FNM_LEADING_DIR) || - strchr(string, '/') == NULL ? - 0 : FNM_NOMATCH); - else - return (0); - else if (c == '/' && flags & FNM_PATHNAME) { - if ((string = strchr(string, '/')) == NULL) - return (FNM_NOMATCH); - break; - } - - /* General case, use recursion. */ - while ((test = *string) != EOS) { - if (!fnmatch(pattern, string, flags & ~FNM_PERIOD)) - return (0); - if (test == '/' && flags & FNM_PATHNAME) - break; - ++string; - } - return (FNM_NOMATCH); - case '[': - if (*string == EOS) - return (FNM_NOMATCH); - if (*string == '/' && (flags & FNM_PATHNAME)) - return (FNM_NOMATCH); - if (*string == '.' && (flags & FNM_PERIOD) && - (string == stringstart || - ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) - return (FNM_NOMATCH); - - switch (rangematch(pattern, *string, flags, &newp)) { - case RANGE_ERROR: - goto norm; - case RANGE_MATCH: - pattern = newp; - break; - case RANGE_NOMATCH: - return (FNM_NOMATCH); - } - ++string; - break; - case '\\': - if (!(flags & FNM_NOESCAPE)) { - if ((c = *pattern++) == EOS) { - c = '\\'; - --pattern; - } - } - /* FALLTHROUGH */ - default: - norm: - if (c == *string) - ; - else if ((flags & FNM_CASEFOLD) && - (tolower((unsigned char)c) == - tolower((unsigned char)*string))) - ; - else - return (FNM_NOMATCH); - string++; - break; - } - /* NOTREACHED */ + const char *stringstart; + char *newp; + char c, test; + + for (stringstart = string;;) + switch (c = *pattern++) { + case EOS: + if ((flags & FNM_LEADING_DIR) && *string == '/') + return (0); + return (*string == EOS ? 0 : FNM_NOMATCH); + case '?': + if (*string == EOS) + return (FNM_NOMATCH); + if (*string == '/' && (flags & FNM_PATHNAME)) + return (FNM_NOMATCH); + if (*string == '.' && (flags & FNM_PERIOD) && + (string == stringstart || + ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) + return (FNM_NOMATCH); + ++string; + break; + case '*': + c = *pattern; + /* Collapse multiple stars. */ + while (c == '*') + c = *++pattern; + + if (*string == '.' && (flags & FNM_PERIOD) && + (string == stringstart || + ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) + return (FNM_NOMATCH); + + /* Optimize for pattern with * at end or before /. */ + if (c == EOS) + if (flags & FNM_PATHNAME) + return ((flags & FNM_LEADING_DIR) || + strchr(string, '/') == NULL ? + 0 : FNM_NOMATCH); + else + return (0); + else if (c == '/' && flags & FNM_PATHNAME) { + if ((string = strchr(string, '/')) == NULL) + return (FNM_NOMATCH); + break; + } + + /* General case, use recursion. */ + while ((test = *string) != EOS) { + if (!fnmatch(pattern, string, flags & ~FNM_PERIOD)) + return (0); + if (test == '/' && flags & FNM_PATHNAME) + break; + ++string; + } + return (FNM_NOMATCH); + case '[': + if (*string == EOS) + return (FNM_NOMATCH); + if (*string == '/' && (flags & FNM_PATHNAME)) + return (FNM_NOMATCH); + if (*string == '.' && (flags & FNM_PERIOD) && + (string == stringstart || + ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) + return (FNM_NOMATCH); + + switch (rangematch(pattern, *string, flags, &newp)) { + case RANGE_ERROR: + goto norm; + case RANGE_MATCH: + pattern = newp; + break; + case RANGE_NOMATCH: + return (FNM_NOMATCH); + } + ++string; + break; + case '\\': + if (!(flags & FNM_NOESCAPE)) { + if ((c = *pattern++) == EOS) { + c = '\\'; + --pattern; + } + } + /* FALLTHROUGH */ + default: +norm: + if (c == *string) + ; + else if ((flags & FNM_CASEFOLD) && + (tolower((unsigned char)c) == + tolower((unsigned char)*string))) + ; + else + return (FNM_NOMATCH); + string++; + break; + } + /* NOTREACHED */ } static int -rangematch(const char *pattern, char test, int flags, char **newp) -{ - int negate, ok; - char c, c2; - - /* - * A bracket expression starting with an unquoted circumflex - * character produces unspecified results (IEEE 1003.2-1992, - * 3.13.2). This implementation treats it like '!', for - * consistency with the regular expression syntax. - * J.T. Conklin (conklin@ngai.kaleida.com) - */ - if ( (negate = (*pattern == '!' || *pattern == '^')) ) - ++pattern; - - if (flags & FNM_CASEFOLD) - test = tolower((unsigned char)test); - - /* - * A right bracket shall lose its special meaning and represent - * itself in a bracket expression if it occurs first in the list. - * -- POSIX.2 2.8.3.2 - */ - ok = 0; - c = *pattern++; - do { - if (c == '\\' && !(flags & FNM_NOESCAPE)) - c = *pattern++; - if (c == EOS) - return (RANGE_ERROR); - - if (c == '/' && (flags & FNM_PATHNAME)) - return (RANGE_NOMATCH); - - if (flags & FNM_CASEFOLD) - c = tolower((unsigned char)c); - - if (*pattern == '-' - && (c2 = *(pattern+1)) != EOS && c2 != ']') { - pattern += 2; - if (c2 == '\\' && !(flags & FNM_NOESCAPE)) - c2 = *pattern++; - if (c2 == EOS) - return (RANGE_ERROR); - - if (flags & FNM_CASEFOLD) - c2 = tolower((unsigned char)c2); - - if (c <= test && test <= c2) - ok = 1; - } else if (c == test) - ok = 1; - } while ((c = *pattern++) != ']'); - - *newp = const_cast<char *>(pattern); - return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH); +rangematch(const char *pattern, char test, int flags, char **newp) { + int negate, ok; + char c, c2; + + /* + * A bracket expression starting with an unquoted circumflex + * character produces unspecified results (IEEE 1003.2-1992, + * 3.13.2). This implementation treats it like '!', for + * consistency with the regular expression syntax. + * J.T. Conklin (conklin@ngai.kaleida.com) + */ + if ((negate = (*pattern == '!' || *pattern == '^'))) + ++pattern; + + if (flags & FNM_CASEFOLD) + test = tolower((unsigned char)test); + + /* + * A right bracket shall lose its special meaning and represent + * itself in a bracket expression if it occurs first in the list. + * -- POSIX.2 2.8.3.2 + */ + ok = 0; + c = *pattern++; + do { + if (c == '\\' && !(flags & FNM_NOESCAPE)) + c = *pattern++; + if (c == EOS) + return (RANGE_ERROR); + + if (c == '/' && (flags & FNM_PATHNAME)) + return (RANGE_NOMATCH); + + if (flags & FNM_CASEFOLD) + c = tolower((unsigned char)c); + + if (*pattern == '-' + && (c2 = *(pattern + 1)) != EOS && c2 != ']') { + pattern += 2; + if (c2 == '\\' && !(flags & FNM_NOESCAPE)) + c2 = *pattern++; + if (c2 == EOS) + return (RANGE_ERROR); + + if (flags & FNM_CASEFOLD) + c2 = tolower((unsigned char)c2); + + if (c <= test && test <= c2) + ok = 1; + } else if (c == test) + ok = 1; + } while ((c = *pattern++) != ']'); + + *newp = const_cast<char *>(pattern); + return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH); } } // End of namespace Alan3 diff --git a/engines/glk/alan3/glkstart.cpp b/engines/glk/alan3/glkstart.cpp index d4f7dcb88d..f5f91a5de2 100644 --- a/engines/glk/alan3/glkstart.cpp +++ b/engines/glk/alan3/glkstart.cpp @@ -34,15 +34,15 @@ namespace Glk { namespace Alan3 { const glkunix_argumentlist_t glkunix_arguments[] = { - { "-l", glkunix_arg_NoValue, "-l: log player command and game output" }, - { "-c", glkunix_arg_NoValue, "-c: log player commands to a file" }, - { "-n", glkunix_arg_NoValue, "-n: no status line" }, - { "-i", glkunix_arg_NoValue, "-i: ignore version and checksum errors" }, - { "-d", glkunix_arg_NoValue, "-d: enter debug mode" }, - { "-t", glkunix_arg_NoValue, "-t [<n>]: trace game execution, higher <n> gives more trace" }, - { "-r", glkunix_arg_NoValue, "-r: refrain from printing timestamps and paging (making regression testing easier)" }, - { "", glkunix_arg_ValueFollows, "filename: The game file to load." }, - { NULL, glkunix_arg_End, NULL } + { "-l", glkunix_arg_NoValue, "-l: log player command and game output" }, + { "-c", glkunix_arg_NoValue, "-c: log player commands to a file" }, + { "-n", glkunix_arg_NoValue, "-n: no status line" }, + { "-i", glkunix_arg_NoValue, "-i: ignore version and checksum errors" }, + { "-d", glkunix_arg_NoValue, "-d: enter debug mode" }, + { "-t", glkunix_arg_NoValue, "-t [<n>]: trace game execution, higher <n> gives more trace" }, + { "-r", glkunix_arg_NoValue, "-r: refrain from printing timestamps and paging (making regression testing easier)" }, + { "", glkunix_arg_ValueFollows, "filename: The game file to load." }, + { NULL, glkunix_arg_End, NULL } }; /* Resources */ @@ -50,14 +50,14 @@ static strid_t resourceFile; /*----------------------------------------------------------------------*/ static void openGlkWindows() { - glkMainWin = g_vm->glk_window_open(0, 0, 0, wintype_TextBuffer, 0); - if (glkMainWin == NULL) { - printf("FATAL ERROR: Cannot open initial window"); - g_vm->glk_exit(); - } - - glkStatusWin = g_vm->glk_window_open(glkMainWin, winmethod_Above | - winmethod_Fixed, 1, wintype_TextGrid, 0); + glkMainWin = g_vm->glk_window_open(0, 0, 0, wintype_TextBuffer, 0); + if (glkMainWin == NULL) { + printf("FATAL ERROR: Cannot open initial window"); + g_vm->glk_exit(); + } + + glkStatusWin = g_vm->glk_window_open(glkMainWin, winmethod_Above | + winmethod_Fixed, 1, wintype_TextGrid, 0); g_vm->glk_set_window(glkStatusWin); g_vm->glk_set_style(style_Preformatted); g_vm->glk_set_window(glkMainWin); @@ -66,49 +66,49 @@ static void openGlkWindows() { /*----------------------------------------------------------------------*/ static void openResourceFile() { #ifdef TODO - char *originalFileName = strdup(adventureFileName); + char *originalFileName = strdup(adventureFileName); char *resourceFileName = originalFileName; - char *extension = strrchr(resourceFileName, '.'); - frefid_t resourceFileRef; + char *extension = strrchr(resourceFileName, '.'); + frefid_t resourceFileRef; // giblorb_err_t ecode; - strcpy(extension, ".a3r"); + strcpy(extension, ".a3r"); - resourceFileRef = winglk_fileref_create_by_name(fileusage_BinaryMode, - resourceFileName, 0, FALSE); + resourceFileRef = winglk_fileref_create_by_name(fileusage_BinaryMode, + resourceFileName, 0, FALSE); if (glk_fileref_does_file_exist(resourceFileRef)) { - resourceFile = glk_stream_open_file(resourceFileRef, filemode_Read, 0); - ecode = giblorb_set_resource_map(resourceFile); - (void)ecode; - } - free(originalFileName); + resourceFile = glk_stream_open_file(resourceFileRef, filemode_Read, 0); + ecode = giblorb_set_resource_map(resourceFile); + (void)ecode; + } + free(originalFileName); #endif } /*======================================================================*/ int glkunix_startup_code(glkunix_startup_t *data) { - g_vm->glk_stylehint_set(wintype_AllTypes, style_Emphasized, stylehint_Weight, 0); + g_vm->glk_stylehint_set(wintype_AllTypes, style_Emphasized, stylehint_Weight, 0); g_vm->glk_stylehint_set(wintype_AllTypes, style_Emphasized, stylehint_Oblique, 1); g_vm->glk_stylehint_set(wintype_AllTypes, style_BlockQuote, stylehint_Indentation, 10); - /* first, open a window for error output */ - openGlkWindows(); + /* first, open a window for error output */ + openGlkWindows(); - /* now process the command line arguments */ - //args(data->argc, data->argv); + /* now process the command line arguments */ + //args(data->argc, data->argv); - if (adventureFileName == NULL || strcmp(adventureFileName, "") == 0) { - printf("You should supply a game file to play.\n"); - usage("arun"); // TODO Find real programname from arguments - terminate(1); - } + if (adventureFileName == NULL || strcmp(adventureFileName, "") == 0) { + printf("You should supply a game file to play.\n"); + usage("arun"); // TODO Find real programname from arguments + terminate(1); + } - /* Open any possible blorb resource file */ - openResourceFile(); + /* Open any possible blorb resource file */ + openResourceFile(); - return TRUE; + return TRUE; } } // End of namespace Alan3 diff --git a/engines/glk/alan3/glkstart.h b/engines/glk/alan3/glkstart.h index 2bc2e9c9dc..0d91732d78 100644 --- a/engines/glk/alan3/glkstart.h +++ b/engines/glk/alan3/glkstart.h @@ -33,7 +33,7 @@ with the various Unix Glk libraries -- at least, the three I wrote. (I encourage anyone writing a Unix Glk library to use this interface, but it's not part of the Glk spec.) - + Because Glk is *almost* perfectly portable, this interface *almost* doesn't have to exist. In practice, it's small. */ @@ -60,14 +60,14 @@ namespace Alan3 { #define glkunix_arg_NumberValue (4) struct glkunix_argumentlist_t { - const char *name; - int argtype; + const char *name; + int argtype; const char *desc; }; struct glkunix_startup_t { - int argc; - char **argv; + int argc; + char **argv; }; /* The list of command-line arguments; this should be defined in your code. */ diff --git a/engines/glk/alan3/instance.cpp b/engines/glk/alan3/instance.cpp index d2670f3b46..89e3659c93 100644 --- a/engines/glk/alan3/instance.cpp +++ b/engines/glk/alan3/instance.cpp @@ -55,320 +55,300 @@ AttributeEntry *attributes; /* Dynamic attribute values */ /* Instance query methods */ /*======================================================================*/ -bool isA(int instance, int ancestor) -{ - int parent; +bool isA(int instance, int ancestor) { + int parent; - if (isLiteral(instance)) - parent = literals[instance-header->instanceMax]._class; - else - parent = instances[instance].parent; - while (parent != 0 && parent != ancestor) - parent = classes[parent].parent; + if (isLiteral(instance)) + parent = literals[instance - header->instanceMax]._class; + else + parent = instances[instance].parent; + while (parent != 0 && parent != ancestor) + parent = classes[parent].parent; - return (parent != 0); + return (parent != 0); } -bool isAObject(int instance) -{ - return isA(instance, OBJECT); +bool isAObject(int instance) { + return isA(instance, OBJECT); } -bool isAContainer(int instance) -{ - return instance != 0 && !isLiteral(instance) && instances[instance].container != 0; +bool isAContainer(int instance) { + return instance != 0 && !isLiteral(instance) && instances[instance].container != 0; } -bool isAActor(int instance) -{ - return isA(instance, ACTOR); +bool isAActor(int instance) { + return isA(instance, ACTOR); } -bool isALocation(int instance) -{ - return isA(instance, LOCATION); +bool isALocation(int instance) { + return isA(instance, LOCATION); } -bool isLiteral(int instance) -{ - return instance > (int)header->instanceMax; +bool isLiteral(int instance) { + return instance > (int)header->instanceMax; } -bool isANumeric(int instance) -{ - return isLiteral(instance) && literals[literalFromInstance(instance)].type == NUMERIC_LITERAL; +bool isANumeric(int instance) { + return isLiteral(instance) && literals[literalFromInstance(instance)].type == NUMERIC_LITERAL; } -bool isAString(int instance) -{ - return isLiteral(instance) && literals[literalFromInstance(instance)].type == STRING_LITERAL; +bool isAString(int instance) { + return isLiteral(instance) && literals[literalFromInstance(instance)].type == STRING_LITERAL; } /*======================================================================*/ bool isOpaque(int container) { - return getInstanceAttribute(container, OPAQUEATTRIBUTE); + return getInstanceAttribute(container, OPAQUEATTRIBUTE); } /*======================================================================*/ -void setInstanceAttribute(int instance, int attribute, Aptr value) -{ - char str[80]; +void setInstanceAttribute(int instance, int attribute, Aptr value) { + char str[80]; - if (instance > 0 && instance <= (int)header->instanceMax) { - setAttribute(admin[instance].attributes, attribute, value); - if (isALocation(instance) && attribute != VISITSATTRIBUTE) - /* If it wasn't the VISITSATTRIBUTE the location may have - changed so describe next time */ - admin[instance].visitsCount = 0; - } else { - sprintf(str, "Can't SET/MAKE instance (%d).", instance); - syserr(str); - } + if (instance > 0 && instance <= (int)header->instanceMax) { + setAttribute(admin[instance].attributes, attribute, value); + if (isALocation(instance) && attribute != VISITSATTRIBUTE) + /* If it wasn't the VISITSATTRIBUTE the location may have + changed so describe next time */ + admin[instance].visitsCount = 0; + } else { + sprintf(str, "Can't SET/MAKE instance (%d).", instance); + syserr(str); + } } /*======================================================================*/ -void setInstanceStringAttribute(int instance, int attribute, char *string) -{ - deallocate(fromAptr(getInstanceAttribute(instance, attribute))); - setInstanceAttribute(instance, attribute, toAptr(string)); +void setInstanceStringAttribute(int instance, int attribute, char *string) { + deallocate(fromAptr(getInstanceAttribute(instance, attribute))); + setInstanceAttribute(instance, attribute, toAptr(string)); } /*======================================================================*/ -void setInstanceSetAttribute(int instance, int attribute, Aptr set) -{ - freeSet((Set *)fromAptr(getInstanceAttribute(instance, attribute))); - setInstanceAttribute(instance, attribute, set); +void setInstanceSetAttribute(int instance, int attribute, Aptr set) { + freeSet((Set *)fromAptr(getInstanceAttribute(instance, attribute))); + setInstanceAttribute(instance, attribute, set); } /*----------------------------------------------------------------------*/ -static Aptr literalAttribute(int literal, int attribute) -{ - if (isPreBeta3(header->version)) { - if (attribute == 1) - return literals[literalFromInstance(literal)].value; - else - return 0; - } else { - if (attribute == 0) - return literals[literalFromInstance(literal)].value; - else - return getAttribute(admin[header->instanceMax].attributes, attribute); - } - return(EOD); +static Aptr literalAttribute(int literal, int attribute) { + if (isPreBeta3(header->version)) { + if (attribute == 1) + return literals[literalFromInstance(literal)].value; + else + return 0; + } else { + if (attribute == 0) + return literals[literalFromInstance(literal)].value; + else + return getAttribute(admin[header->instanceMax].attributes, attribute); + } + return (EOD); } /*======================================================================*/ -Aptr getInstanceAttribute(int instance, int attribute) -{ - char str[80]; - - if (isLiteral(instance)) - return literalAttribute(instance, attribute); - else { - if (instance > 0 && instance <= (int)header->instanceMax) { - if (attribute == -1) - return locationOf(instance); - else - return getAttribute(admin[instance].attributes, attribute); - } else { - sprintf(str, "Can't ATTRIBUTE item (%d).", instance); - syserr(str); - } - } - return(EOD); +Aptr getInstanceAttribute(int instance, int attribute) { + char str[80]; + + if (isLiteral(instance)) + return literalAttribute(instance, attribute); + else { + if (instance > 0 && instance <= (int)header->instanceMax) { + if (attribute == -1) + return locationOf(instance); + else + return getAttribute(admin[instance].attributes, attribute); + } else { + sprintf(str, "Can't ATTRIBUTE item (%d).", instance); + syserr(str); + } + } + return (EOD); } /*======================================================================*/ -char *getInstanceStringAttribute(int instance, int attribute) -{ - return strdup((char *)fromAptr(getInstanceAttribute(instance, attribute))); +char *getInstanceStringAttribute(int instance, int attribute) { + return strdup((char *)fromAptr(getInstanceAttribute(instance, attribute))); } /*======================================================================*/ -Set *getInstanceSetAttribute(int instance, int attribute) -{ - return copySet((Set *)fromAptr(getInstanceAttribute(instance, attribute))); +Set *getInstanceSetAttribute(int instance, int attribute) { + return copySet((Set *)fromAptr(getInstanceAttribute(instance, attribute))); } /*----------------------------------------------------------------------*/ static void verifyInstance(int instance, const char *action) { - char message[200]; + char message[200]; - if (instance == 0) { - sprintf(message, "Can't %s instance (%d).", action, instance); - syserr(message); - } else if (instance > (int)header->instanceMax) { - sprintf(message, "Can't %s instance (%d > instanceMax).", action, instance); - syserr(message); - } + if (instance == 0) { + sprintf(message, "Can't %s instance (%d).", action, instance); + syserr(message); + } else if (instance > (int)header->instanceMax) { + sprintf(message, "Can't %s instance (%d > instanceMax).", action, instance); + syserr(message); + } } /*======================================================================*/ -bool isHere(int id, ATrans transitivity) -{ - verifyInstance(id, "HERE"); +bool isHere(int id, ATrans transitivity) { + verifyInstance(id, "HERE"); - return isAt(id, current.location, transitivity); + return isAt(id, current.location, transitivity); } /*======================================================================*/ -bool isNearby(int instance, ATrans transitivity) -{ - verifyInstance(instance, "NEARBY"); +bool isNearby(int instance, ATrans transitivity) { + verifyInstance(instance, "NEARBY"); - if (isALocation(instance)) - return exitto(current.location, instance); - else - return exitto(current.location, where(instance, transitivity)); + if (isALocation(instance)) + return exitto(current.location, instance); + else + return exitto(current.location, where(instance, transitivity)); } /*======================================================================*/ -bool isNear(int instance, int other, ATrans trans) -{ - Aint l1, l2; +bool isNear(int instance, int other, ATrans trans) { + Aint l1, l2; - verifyInstance(instance, "NEAR"); + verifyInstance(instance, "NEAR"); - if (isALocation(instance)) - l1 = instance; - else - l1 = where(instance, trans); - if (isALocation(other)) - l2 = other; - else - l2 = where(other, trans); - return exitto(l2, l1); + if (isALocation(instance)) + l1 = instance; + else + l1 = where(instance, trans); + if (isALocation(other)) + l2 = other; + else + l2 = where(other, trans); + return exitto(l2, l1); } /*======================================================================*/ /* Look in a container to see if the instance is in it. */ -bool isIn(int instance, int container, ATrans trans) -{ - int loc; +bool isIn(int instance, int container, ATrans trans) { + int loc; - if (!isAContainer(container)) - syserr("IN in a non-container."); + if (!isAContainer(container)) + syserr("IN in a non-container."); - if (trans == DIRECT) - return admin[instance].location == container; - else { - loc = admin[instance].location; - if (trans == INDIRECT && loc != 0 && !isA(loc, LOCATION)) - loc = admin[loc].location; - while (loc != 0 && !isA(loc, LOCATION)) - if (loc == container) - return TRUE; - else - loc = admin[loc].location; - return FALSE; - } + if (trans == DIRECT) + return admin[instance].location == container; + else { + loc = admin[instance].location; + if (trans == INDIRECT && loc != 0 && !isA(loc, LOCATION)) + loc = admin[loc].location; + while (loc != 0 && !isA(loc, LOCATION)) + if (loc == container) + return TRUE; + else + loc = admin[loc].location; + return FALSE; + } } /*======================================================================*/ /* Look see if an instance is AT another. */ -bool isAt(int instance, int other, ATrans trans) -{ - if (instance == 0 || other == 0) return FALSE; - - if (isALocation(instance)) { - /* Nested locations */ - /* TODO - What if the other is not a location? */ - int curr = admin[instance].location; - switch (trans) { - case DIRECT: - return admin[instance].location == other; - case INDIRECT: - if (curr == other) - return FALSE; - curr = admin[curr].location; - case TRANSITIVE: - while (curr != 0) { - if (curr == other) - return TRUE; - else - curr = admin[curr].location; - } - return FALSE; - } - syserr("Unexpected value in switch in isAt() for location"); - return FALSE; - } else if (isALocation(other)) { - /* Instance is not a location but other is */ - switch (trans) { - case DIRECT: - return admin[instance].location == other; - case INDIRECT: { - if (admin[instance].location == other) - return FALSE; /* Directly, so not Indirectly */ - /* Fall through to transitive handling of the location */ - } - case TRANSITIVE: { - int location = locationOf(instance); - int curr = other; - while (curr != 0) { - if (curr == location) - return TRUE; - else - curr = admin[curr].location; - } - return FALSE; - } - } - syserr("Unexpected value in switch in isAt() for non-location"); - return FALSE; - } else { - /* Other is also not a location */ - switch (trans) { - case DIRECT: - return positionOf(instance) == admin[other].location; - case INDIRECT: { - int location = locationOf(instance); - int curr = other; - if (location == curr) - return FALSE; - else - curr = admin[curr].location; - while (curr != 0) { - if (curr == location) - return TRUE; - else - curr = admin[curr].location; - } - return FALSE; - } - case TRANSITIVE: { - int location = locationOf(other); - int curr = locationOf(instance); - bool ok = FALSE; - while (curr != 0 && !ok) { - if (curr == location) - ok = TRUE; - else - curr = admin[curr].location; - } - return ok; - } - } - syserr("Unexpected value in switch in isAt() for non-location"); - return FALSE; - } +bool isAt(int instance, int other, ATrans trans) { + if (instance == 0 || other == 0) return FALSE; + + if (isALocation(instance)) { + /* Nested locations */ + /* TODO - What if the other is not a location? */ + int curr = admin[instance].location; + switch (trans) { + case DIRECT: + return admin[instance].location == other; + case INDIRECT: + if (curr == other) + return FALSE; + curr = admin[curr].location; + case TRANSITIVE: + while (curr != 0) { + if (curr == other) + return TRUE; + else + curr = admin[curr].location; + } + return FALSE; + } + syserr("Unexpected value in switch in isAt() for location"); + return FALSE; + } else if (isALocation(other)) { + /* Instance is not a location but other is */ + switch (trans) { + case DIRECT: + return admin[instance].location == other; + case INDIRECT: { + if (admin[instance].location == other) + return FALSE; /* Directly, so not Indirectly */ + /* Fall through to transitive handling of the location */ + } + case TRANSITIVE: { + int location = locationOf(instance); + int curr = other; + while (curr != 0) { + if (curr == location) + return TRUE; + else + curr = admin[curr].location; + } + return FALSE; + } + } + syserr("Unexpected value in switch in isAt() for non-location"); + return FALSE; + } else { + /* Other is also not a location */ + switch (trans) { + case DIRECT: + return positionOf(instance) == admin[other].location; + case INDIRECT: { + int location = locationOf(instance); + int curr = other; + if (location == curr) + return FALSE; + else + curr = admin[curr].location; + while (curr != 0) { + if (curr == location) + return TRUE; + else + curr = admin[curr].location; + } + return FALSE; + } + case TRANSITIVE: { + int location = locationOf(other); + int curr = locationOf(instance); + bool ok = FALSE; + while (curr != 0 && !ok) { + if (curr == location) + ok = TRUE; + else + curr = admin[curr].location; + } + return ok; + } + } + syserr("Unexpected value in switch in isAt() for non-location"); + return FALSE; + } } @@ -379,773 +359,747 @@ bool isAt(int instance, int other, ATrans trans) container that is somewhere, or a THING that is nowhere. It might also be an ENTITY which is always everywhere so take that to mean where the hero is. */ -int locationOf(int instance) -{ - int position; - int container = 0; - - verifyInstance(instance, "get LOCATION of"); - - position = admin[instance].location; - while (position != 0 && !isALocation(position)) { - container = position; /* Remember innermost container */ - position = admin[position].location; - } - if (position > NOWHERE) /* It was a location so return that */ - return position; - else { - /* If we did not find a location then it might be in a container */ - if (container != 0) - instance = container; - /* If the instance or the container it was in is a THING then its nowhere. */ - if (isA(instance, THING)) - return NOWHERE; /* #nowhere */ - else if (isALocation(instance)) - return NO_LOCATION; /* No location */ - else - return locationOf(HERO); - } +int locationOf(int instance) { + int position; + int container = 0; + + verifyInstance(instance, "get LOCATION of"); + + position = admin[instance].location; + while (position != 0 && !isALocation(position)) { + container = position; /* Remember innermost container */ + position = admin[position].location; + } + if (position > NOWHERE) /* It was a location so return that */ + return position; + else { + /* If we did not find a location then it might be in a container */ + if (container != 0) + instance = container; + /* If the instance or the container it was in is a THING then its nowhere. */ + if (isA(instance, THING)) + return NOWHERE; /* #nowhere */ + else if (isALocation(instance)) + return NO_LOCATION; /* No location */ + else + return locationOf(HERO); + } } /*======================================================================*/ /* Return the current position of an instance, directly or not */ /* TODO: this will be a possible duplicate of where() */ -int positionOf(int instance) -{ - return admin[instance].location; +int positionOf(int instance) { + return admin[instance].location; } /*======================================================================*/ /* Return the current position of an instance, directly or not */ -int where(int instance, ATrans trans) -{ - verifyInstance(instance, "WHERE"); +int where(int instance, ATrans trans) { + verifyInstance(instance, "WHERE"); - if (isALocation(instance)) - return 0; - else if (trans == DIRECT) - return admin[instance].location; - else - return locationOf(instance); + if (isALocation(instance)) + return 0; + else if (trans == DIRECT) + return admin[instance].location; + else + return locationOf(instance); } /*----------------------------------------------------------------------*/ static bool executeInheritedMentioned(int cls) { - if (cls == 0) return FALSE; + if (cls == 0) return FALSE; - if (classes[cls].mentioned) { - interpret(classes[cls].mentioned); - return TRUE; - } else - return executeInheritedMentioned(classes[cls].parent); + if (classes[cls].mentioned) { + interpret(classes[cls].mentioned); + return TRUE; + } else + return executeInheritedMentioned(classes[cls].parent); } /*----------------------------------------------------------------------*/ static bool mention(int instance) { - if (instances[instance].mentioned) { - interpret(instances[instance].mentioned); - return TRUE; - } else - return executeInheritedMentioned(instances[instance].parent); + if (instances[instance].mentioned) { + interpret(instances[instance].mentioned); + return TRUE; + } else + return executeInheritedMentioned(instances[instance].parent); } /*======================================================================*/ -void sayInstance(int instance) -{ +void sayInstance(int instance) { #ifdef SAY_INSTANCE_WITH_PLAYER_WORDS_IF_PARAMETER - int p, i; - - /* Find the id in the parameters... */ - if (params != NULL) - for (p = 0; params[p].code != EOD; p++) - if (params[p].code == instance) { - /* Found it so.. */ - if (params[p].firstWord == EOD) /* Any words he used? */ - break; /* No... */ - else { /* Yes, so use them... */ - char *capitalized; - /* Assuming the noun is the last word we can simply output the adjectives... */ - for (i = params[p].firstWord; i <= params[p].lastWord-1; i++) - output((char *)pointerTo(dict[wrds[i]].wrd)); - /* ... and then the noun, capitalized if necessary */ - if (header->capitalizeNouns) { - capitalized = strdup((char *)pointerTo(dict[wrds[params[p].lastWord]].wrd)); - capitalized[0] = IsoToUpperCase(capitalized[0]); - output(capitalized); - deallocate(capitalized); - } else - output((char *)pointerTo(dict[wrds[params[p].lastWord]].wrd)); - } - return; - } + int p, i; + + /* Find the id in the parameters... */ + if (params != NULL) + for (p = 0; params[p].code != EOD; p++) + if (params[p].code == instance) { + /* Found it so.. */ + if (params[p].firstWord == EOD) /* Any words he used? */ + break; /* No... */ + else { /* Yes, so use them... */ + char *capitalized; + /* Assuming the noun is the last word we can simply output the adjectives... */ + for (i = params[p].firstWord; i <= params[p].lastWord - 1; i++) + output((char *)pointerTo(dict[wrds[i]].wrd)); + /* ... and then the noun, capitalized if necessary */ + if (header->capitalizeNouns) { + capitalized = strdup((char *)pointerTo(dict[wrds[params[p].lastWord]].wrd)); + capitalized[0] = IsoToUpperCase(capitalized[0]); + output(capitalized); + deallocate(capitalized); + } else + output((char *)pointerTo(dict[wrds[params[p].lastWord]].wrd)); + } + return; + } #endif - if (!mention(instance)) - interpret(instances[instance].name); + if (!mention(instance)) + interpret(instances[instance].name); } /*======================================================================*/ -void sayInteger(int value) -{ - char buf[25]; +void sayInteger(int value) { + char buf[25]; - if (isHere(HERO, /*FALSE*/ TRANSITIVE)) { - sprintf(buf, "%d", value); - output(buf); - } + if (isHere(HERO, /*FALSE*/ TRANSITIVE)) { + sprintf(buf, "%d", value); + output(buf); + } } /*======================================================================*/ -void sayString(char *string) -{ - if (isHere(HERO, /*FALSE*/ TRANSITIVE)) - output(string); - deallocate(string); +void sayString(char *string) { + if (isHere(HERO, /*FALSE*/ TRANSITIVE)) + output(string); + deallocate(string); } /*----------------------------------------------------------------------*/ -static void sayLiteral(int literal) -{ - char *str; +static void sayLiteral(int literal) { + char *str; - if (isANumeric(literal)) - sayInteger(literals[literal-header->instanceMax].value); - else { - str = (char *)strdup((char *)fromAptr(literals[literal-header->instanceMax].value)); - sayString(str); - } + if (isANumeric(literal)) + sayInteger(literals[literal - header->instanceMax].value); + else { + str = (char *)strdup((char *)fromAptr(literals[literal - header->instanceMax].value)); + sayString(str); + } } /*----------------------------------------------------------------------*/ static char *wordWithCode(int classBit, int code) { - int w; - char str[50]; + int w; + char str[50]; - for (w = 0; w < dictionarySize; w++) - if (dictionary[w].code == (Aword)code && ((classBit&dictionary[w].classBits) != 0)) - return (char *)pointerTo(dictionary[w].string); - sprintf(str, "Could not find word of class %d with code %d.", classBit, code); - syserr(str); - return NULL; + for (w = 0; w < dictionarySize; w++) + if (dictionary[w].code == (Aword)code && ((classBit & dictionary[w].classBits) != 0)) + return (char *)pointerTo(dictionary[w].string); + sprintf(str, "Could not find word of class %d with code %d.", classBit, code); + syserr(str); + return NULL; } /*----------------------------------------------------------------------*/ static bool sayInheritedDefiniteForm(int cls) { - if (cls == 0) { - syserr("No default definite article"); - return FALSE; - } else { - if (classes[cls].definite.address) { - interpret(classes[cls].definite.address); - return classes[cls].definite.isForm; - } else - return sayInheritedDefiniteForm(classes[cls].parent); - } + if (cls == 0) { + syserr("No default definite article"); + return FALSE; + } else { + if (classes[cls].definite.address) { + interpret(classes[cls].definite.address); + return classes[cls].definite.isForm; + } else + return sayInheritedDefiniteForm(classes[cls].parent); + } } /*----------------------------------------------------------------------*/ static void sayDefinite(int instance) { - if (instances[instance].definite.address) { - interpret(instances[instance].definite.address); - if (!instances[instance].definite.isForm) - sayInstance(instance); - } else - if (!sayInheritedDefiniteForm(instances[instance].parent)) - sayInstance(instance); + if (instances[instance].definite.address) { + interpret(instances[instance].definite.address); + if (!instances[instance].definite.isForm) + sayInstance(instance); + } else if (!sayInheritedDefiniteForm(instances[instance].parent)) + sayInstance(instance); } /*----------------------------------------------------------------------*/ static bool sayInheritedIndefiniteForm(int cls) { - if (cls == 0) { - syserr("No default indefinite article"); - return FALSE; - } else { - if (classes[cls].indefinite.address) { - interpret(classes[cls].indefinite.address); - return classes[cls].indefinite.isForm; - } else - return sayInheritedIndefiniteForm(classes[cls].parent); - } + if (cls == 0) { + syserr("No default indefinite article"); + return FALSE; + } else { + if (classes[cls].indefinite.address) { + interpret(classes[cls].indefinite.address); + return classes[cls].indefinite.isForm; + } else + return sayInheritedIndefiniteForm(classes[cls].parent); + } } /*----------------------------------------------------------------------*/ static void sayIndefinite(int instance) { - if (instances[instance].indefinite.address) { - interpret(instances[instance].indefinite.address); - if (!instances[instance].indefinite.isForm) - sayInstance(instance); - } else - if (!sayInheritedIndefiniteForm(instances[instance].parent)) - sayInstance(instance); + if (instances[instance].indefinite.address) { + interpret(instances[instance].indefinite.address); + if (!instances[instance].indefinite.isForm) + sayInstance(instance); + } else if (!sayInheritedIndefiniteForm(instances[instance].parent)) + sayInstance(instance); } /*----------------------------------------------------------------------*/ static bool sayInheritedNegativeForm(int cls) { - if (cls == 0) { - syserr("No default negative form"); - return FALSE; - } else { - if (classes[cls].negative.address) { - interpret(classes[cls].negative.address); - return classes[cls].negative.isForm; - } else - return sayInheritedNegativeForm(classes[cls].parent); - } + if (cls == 0) { + syserr("No default negative form"); + return FALSE; + } else { + if (classes[cls].negative.address) { + interpret(classes[cls].negative.address); + return classes[cls].negative.isForm; + } else + return sayInheritedNegativeForm(classes[cls].parent); + } } /*----------------------------------------------------------------------*/ static void sayNegative(int instance) { - if (instances[instance].negative.address) { - interpret(instances[instance].negative.address); - if (!instances[instance].negative.isForm) - sayInstance(instance); - } else - if (!sayInheritedNegativeForm(instances[instance].parent)) - sayInstance(instance); + if (instances[instance].negative.address) { + interpret(instances[instance].negative.address); + if (!instances[instance].negative.isForm) + sayInstance(instance); + } else if (!sayInheritedNegativeForm(instances[instance].parent)) + sayInstance(instance); } /*----------------------------------------------------------------------*/ static void sayInheritedPronoun(int instance) { - if (instance == 0) - syserr("No default pronoun"); - else { - if (classes[instance].pronoun != 0) - output(wordWithCode(PRONOUN_BIT, classes[instance].pronoun)); - else - sayInheritedPronoun(classes[instance].parent); - } + if (instance == 0) + syserr("No default pronoun"); + else { + if (classes[instance].pronoun != 0) + output(wordWithCode(PRONOUN_BIT, classes[instance].pronoun)); + else + sayInheritedPronoun(classes[instance].parent); + } } /*----------------------------------------------------------------------*/ static void sayPronoun(int instance) { - if (instances[instance].pronoun != 0) - output(wordWithCode(PRONOUN_BIT, instances[instance].pronoun)); - else - sayInheritedPronoun(instances[instance].parent); + if (instances[instance].pronoun != 0) + output(wordWithCode(PRONOUN_BIT, instances[instance].pronoun)); + else + sayInheritedPronoun(instances[instance].parent); } /*----------------------------------------------------------------------*/ -static void sayArticleOrForm(int id, SayForm form) -{ - if (!isLiteral(id)) - switch (form) { - case SAY_DEFINITE: - sayDefinite(id); - break; - case SAY_INDEFINITE: - sayIndefinite(id); - break; - case SAY_NEGATIVE: - sayNegative(id); - break; - case SAY_PRONOUN: - sayPronoun(id); - break; - case SAY_SIMPLE: - say(id); - break; - default: - syserr("Unexpected form in 'sayArticleOrForm()'"); - } - else - say(id); +static void sayArticleOrForm(int id, SayForm form) { + if (!isLiteral(id)) + switch (form) { + case SAY_DEFINITE: + sayDefinite(id); + break; + case SAY_INDEFINITE: + sayIndefinite(id); + break; + case SAY_NEGATIVE: + sayNegative(id); + break; + case SAY_PRONOUN: + sayPronoun(id); + break; + case SAY_SIMPLE: + say(id); + break; + default: + syserr("Unexpected form in 'sayArticleOrForm()'"); + } + else + say(id); } /*======================================================================*/ -void say(int instance) -{ - Aword previousInstance = current.instance; - current.instance = instance; +void say(int instance) { + Aword previousInstance = current.instance; + current.instance = instance; - if (isHere(HERO, /*FALSE*/ TRANSITIVE)) { - if (isLiteral(instance)) - sayLiteral(instance); - else { - verifyInstance(instance, "SAY"); - sayInstance(instance); - } - } - current.instance = previousInstance; + if (isHere(HERO, /*FALSE*/ TRANSITIVE)) { + if (isLiteral(instance)) + sayLiteral(instance); + else { + verifyInstance(instance, "SAY"); + sayInstance(instance); + } + } + current.instance = previousInstance; } /*======================================================================*/ -void sayForm(int instance, SayForm form) -{ - Aword previousInstance = current.instance; - current.instance = instance; +void sayForm(int instance, SayForm form) { + Aword previousInstance = current.instance; + current.instance = instance; - sayArticleOrForm(instance, form); + sayArticleOrForm(instance, form); - current.instance = previousInstance; + current.instance = previousInstance; } /*======================================================================*/ bool isDescribable(int instance) { - return isAObject(instance) || isAActor(instance); + return isAObject(instance) || isAActor(instance); } /*----------------------------------------------------------------------*/ -static bool inheritsDescriptionFrom(int cls) -{ - if (classes[cls].description != 0) - return TRUE; - else if (classes[cls].parent != 0) - return inheritsDescriptionFrom(classes[cls].parent); - else - return FALSE; +static bool inheritsDescriptionFrom(int cls) { + if (classes[cls].description != 0) + return TRUE; + else if (classes[cls].parent != 0) + return inheritsDescriptionFrom(classes[cls].parent); + else + return FALSE; } /*======================================================================*/ -bool hasDescription(int instance) -{ - if (instances[instance].description != 0) - return TRUE; - else if (instances[instance].parent != 0) - return inheritsDescriptionFrom(instances[instance].parent); - else - return FALSE; +bool hasDescription(int instance) { + if (instances[instance].description != 0) + return TRUE; + else if (instances[instance].parent != 0) + return inheritsDescriptionFrom(instances[instance].parent); + else + return FALSE; } /*----------------------------------------------------------------------*/ -static void describeClass(int instance) -{ - if (classes[instance].description != 0) { - /* This class has a description, run it */ - interpret(classes[instance].description); - } else { - /* Search up the inheritance tree, if any, to find a description */ - if (classes[instance].parent != 0) - describeClass(classes[instance].parent); - } +static void describeClass(int instance) { + if (classes[instance].description != 0) { + /* This class has a description, run it */ + interpret(classes[instance].description); + } else { + /* Search up the inheritance tree, if any, to find a description */ + if (classes[instance].parent != 0) + describeClass(classes[instance].parent); + } } /*======================================================================*/ -void describeAnything(int instance) -{ - if (instances[instance].description != 0) { - /* This instance has its own description, run it */ - interpret(instances[instance].description); - } else { - /* Search up the inheritance tree to find a description */ - if (instances[instance].parent != 0) - describeClass(instances[instance].parent); - } - admin[instance].alreadyDescribed = TRUE; +void describeAnything(int instance) { + if (instances[instance].description != 0) { + /* This instance has its own description, run it */ + interpret(instances[instance].description); + } else { + /* Search up the inheritance tree to find a description */ + if (instances[instance].parent != 0) + describeClass(instances[instance].parent); + } + admin[instance].alreadyDescribed = TRUE; } /*----------------------------------------------------------------------*/ -static void describeObject(int object) -{ - if (hasDescription(object)) - describeAnything(object); - else { - printMessageWithInstanceParameter(M_SEE_START, object); - printMessage(M_SEE_END); - if (instances[object].container != 0) - describeContainer(object); - } - admin[object].alreadyDescribed = TRUE; +static void describeObject(int object) { + if (hasDescription(object)) + describeAnything(object); + else { + printMessageWithInstanceParameter(M_SEE_START, object); + printMessage(M_SEE_END); + if (instances[object].container != 0) + describeContainer(object); + } + admin[object].alreadyDescribed = TRUE; } /*----------------------------------------------------------------------*/ -static bool inheritedDescriptionCheck(int cls) -{ - if (cls == 0) return TRUE; - if (!inheritedDescriptionCheck(classes[cls].parent)) return FALSE; - if (classes[cls].descriptionChecks == 0) return TRUE; - return !checksFailed(classes[cls].descriptionChecks, TRUE); +static bool inheritedDescriptionCheck(int cls) { + if (cls == 0) return TRUE; + if (!inheritedDescriptionCheck(classes[cls].parent)) return FALSE; + if (classes[cls].descriptionChecks == 0) return TRUE; + return !checksFailed(classes[cls].descriptionChecks, TRUE); } /*----------------------------------------------------------------------*/ -static bool descriptionCheck(int instance) -{ - int previousInstance = current.instance; - bool r; +static bool descriptionCheck(int instance) { + int previousInstance = current.instance; + bool r; - current.instance = instance; - if (inheritedDescriptionCheck(instances[instance].parent)) { - if (instances[instance].checks == 0) - r = TRUE; - else - r = !checksFailed(instances[instance].checks, TRUE); - } else - r = FALSE; - current.instance = previousInstance; - return r; + current.instance = instance; + if (inheritedDescriptionCheck(instances[instance].parent)) { + if (instances[instance].checks == 0) + r = TRUE; + else + r = !checksFailed(instances[instance].checks, TRUE); + } else + r = FALSE; + current.instance = previousInstance; + return r; } /*======================================================================*/ -void describeInstances(void) -{ - uint i; - int lastInstanceFound = 0; - int found = 0; - - /* First describe every object here with its own description */ - for (i = 1; i <= header->instanceMax; i++) - if (admin[i].location == current.location && isAObject(i) && - !admin[i].alreadyDescribed && hasDescription(i)) - describe(i); - - /* Then list all things without a description */ - for (i = 1; i <= header->instanceMax; i++) - if (admin[i].location == current.location - && !admin[i].alreadyDescribed - && isAObject(i) - && descriptionCheck(i)) { - if (found == 0) - printMessageWithInstanceParameter(M_SEE_START, i); - else if (found > 1) - printMessageWithInstanceParameter(M_SEE_COMMA, lastInstanceFound); - admin[i].alreadyDescribed = TRUE; - - // TODO : isOpaque() - if (instances[i].container && containerSize(i, DIRECT) > 0 && !getInstanceAttribute(i, OPAQUEATTRIBUTE)) { - if (found > 0) - printMessageWithInstanceParameter(M_SEE_AND, i); - printMessage(M_SEE_END); - describeContainer(i); - found = 0; - continue; /* Actually start another list. */ - } - found++; - lastInstanceFound = i; - } - - if (found > 0) { - if (found > 1) { - printMessageWithInstanceParameter(M_SEE_AND, lastInstanceFound); - } - printMessage(M_SEE_END); - } - - /* Finally all actors with a separate description */ - for (i = 1; i <= header->instanceMax; i++) - if (admin[i].location == current.location && i != HERO && isAActor(i) - && !admin[i].alreadyDescribed) - describe(i); - - /* Clear the describe flag for all instances */ - for (i = 1; i <= header->instanceMax; i++) - admin[i].alreadyDescribed = FALSE; +void describeInstances(void) { + uint i; + int lastInstanceFound = 0; + int found = 0; + + /* First describe every object here with its own description */ + for (i = 1; i <= header->instanceMax; i++) + if (admin[i].location == current.location && isAObject(i) && + !admin[i].alreadyDescribed && hasDescription(i)) + describe(i); + + /* Then list all things without a description */ + for (i = 1; i <= header->instanceMax; i++) + if (admin[i].location == current.location + && !admin[i].alreadyDescribed + && isAObject(i) + && descriptionCheck(i)) { + if (found == 0) + printMessageWithInstanceParameter(M_SEE_START, i); + else if (found > 1) + printMessageWithInstanceParameter(M_SEE_COMMA, lastInstanceFound); + admin[i].alreadyDescribed = TRUE; + + // TODO : isOpaque() + if (instances[i].container && containerSize(i, DIRECT) > 0 && !getInstanceAttribute(i, OPAQUEATTRIBUTE)) { + if (found > 0) + printMessageWithInstanceParameter(M_SEE_AND, i); + printMessage(M_SEE_END); + describeContainer(i); + found = 0; + continue; /* Actually start another list. */ + } + found++; + lastInstanceFound = i; + } + + if (found > 0) { + if (found > 1) { + printMessageWithInstanceParameter(M_SEE_AND, lastInstanceFound); + } + printMessage(M_SEE_END); + } + + /* Finally all actors with a separate description */ + for (i = 1; i <= header->instanceMax; i++) + if (admin[i].location == current.location && i != HERO && isAActor(i) + && !admin[i].alreadyDescribed) + describe(i); + + /* Clear the describe flag for all instances */ + for (i = 1; i <= header->instanceMax; i++) + admin[i].alreadyDescribed = FALSE; } /*======================================================================*/ -bool describe(int instance) -{ - bool descriptionOk; - int previousInstance = current.instance; - - current.instance = instance; - verifyInstance(instance, "DESCRIBE"); - if (descriptionCheck(instance)) { - descriptionOk = TRUE; - if (isAObject(instance)) { - describeObject(instance); - } else if (isAActor(instance)) { - describeActor(instance); - } else - describeAnything(instance); - } else - descriptionOk = FALSE; - current.instance = previousInstance; - return descriptionOk; +bool describe(int instance) { + bool descriptionOk; + int previousInstance = current.instance; + + current.instance = instance; + verifyInstance(instance, "DESCRIBE"); + if (descriptionCheck(instance)) { + descriptionOk = TRUE; + if (isAObject(instance)) { + describeObject(instance); + } else if (isAActor(instance)) { + describeActor(instance); + } else + describeAnything(instance); + } else + descriptionOk = FALSE; + current.instance = previousInstance; + return descriptionOk; } /*----------------------------------------------------------------------*/ static void locateIntoContainer(Aword theInstance, Aword theContainer) { - if (!isA(theInstance, containers[instances[theContainer].container]._class)) - printMessageUsing2InstanceParameters(M_CANNOTCONTAIN, theContainer, theInstance); - else if (passesContainerLimits(theContainer, theInstance)) - admin[theInstance].location = theContainer; - else - abortPlayerCommand(); + if (!isA(theInstance, containers[instances[theContainer].container]._class)) + printMessageUsing2InstanceParameters(M_CANNOTCONTAIN, theContainer, theInstance); + else if (passesContainerLimits(theContainer, theInstance)) + admin[theInstance].location = theContainer; + else + abortPlayerCommand(); } /*----------------------------------------------------------------------*/ -static void locateLocation(Aword loc, Aword whr) -{ - Aint l = whr; +static void locateLocation(Aword loc, Aword whr) { + Aint l = whr; - /* Ensure this does not create a recursive location chain */ - while (l != 0) { - if (admin[l].location == (int)loc) - apperr("Locating a location that would create a recursive loop of locations containing each other."); - else - l = admin[l].location; - } - admin[loc].location = whr; + /* Ensure this does not create a recursive location chain */ + while (l != 0) { + if (admin[l].location == (int)loc) + apperr("Locating a location that would create a recursive loop of locations containing each other."); + else + l = admin[l].location; + } + admin[loc].location = whr; } /*----------------------------------------------------------------------*/ -static void locateObject(Aword obj, Aword whr) -{ - if (isAContainer(whr)) { /* Into a container */ - locateIntoContainer(obj, whr); - } else { - admin[obj].location = whr; - /* Make sure the location is described since it's changed */ - admin[whr].visitsCount = 0; - } +static void locateObject(Aword obj, Aword whr) { + if (isAContainer(whr)) { /* Into a container */ + locateIntoContainer(obj, whr); + } else { + admin[obj].location = whr; + /* Make sure the location is described since it's changed */ + admin[whr].visitsCount = 0; + } } /*----------------------------------------------------------------------*/ static void traceEnteredClass(Aint theClass, bool empty) { - printf("\n<ENTERED in class "); - printf("%s", idOfClass(theClass)); - printf("[%d]%s>\n", theClass, empty?" is empty":":"); + printf("\n<ENTERED in class "); + printf("%s", idOfClass(theClass)); + printf("[%d]%s>\n", theClass, empty ? " is empty" : ":"); } /*----------------------------------------------------------------------*/ static void traceEnteredInstance(Aint instance, bool empty) { - printf("\n<ENTERED in instance "); - traceSay(instance); - printf("[%d]%s>\n", instance, empty?" is empty":""); + printf("\n<ENTERED in instance "); + traceSay(instance); + printf("[%d]%s>\n", instance, empty ? " is empty" : ""); } /*----------------------------------------------------------------------*/ static void executeInheritedEntered(Aint theClass) { - if (theClass == 0) return; - executeInheritedEntered(classes[theClass].parent); - if (traceSectionOption) - traceEnteredClass(theClass, classes[theClass].entered == 0); - if (classes[theClass].entered) { - interpret(classes[theClass].entered); - } + if (theClass == 0) return; + executeInheritedEntered(classes[theClass].parent); + if (traceSectionOption) + traceEnteredClass(theClass, classes[theClass].entered == 0); + if (classes[theClass].entered) { + interpret(classes[theClass].entered); + } } /*----------------------------------------------------------------------*/ static void executeEntered(Aint instance) { - int currentInstance = current.instance; - current.instance = instance; - if (admin[instance].location != 0) - executeEntered(admin[instance].location); - executeInheritedEntered(instances[instance].parent); - if (traceSectionOption) - traceEnteredInstance(instance, instances[instance].entered == 0); - if (instances[instance].entered != 0) { - interpret(instances[instance].entered); - } - current.instance = currentInstance; + int currentInstance = current.instance; + current.instance = instance; + if (admin[instance].location != 0) + executeEntered(admin[instance].location); + executeInheritedEntered(instances[instance].parent); + if (traceSectionOption) + traceEnteredInstance(instance, instances[instance].entered == 0); + if (instances[instance].entered != 0) { + interpret(instances[instance].entered); + } + current.instance = currentInstance; } /*----------------------------------------------------------------------*/ static int getVisits(int location) { - return getInstanceAttribute(location, VISITSATTRIBUTE); + return getInstanceAttribute(location, VISITSATTRIBUTE); } /*----------------------------------------------------------------------*/ static void incrementVisits(int location) { - setInstanceAttribute(location, VISITSATTRIBUTE, getVisits(location)+1); - if (admin[location].location != 0) - /* Nested location, so increment that too */ - incrementVisits(admin[location].location); + setInstanceAttribute(location, VISITSATTRIBUTE, getVisits(location) + 1); + if (admin[location].location != 0) + /* Nested location, so increment that too */ + incrementVisits(admin[location].location); } /*----------------------------------------------------------------------*/ static void revisited(void) { - if (anyOutput) - para(); - say(where(HERO, DIRECT)); - printMessage(M_AGAIN); - newline(); - describeInstances(); + if (anyOutput) + para(); + say(where(HERO, DIRECT)); + printMessage(M_AGAIN); + newline(); + describeInstances(); } /*----------------------------------------------------------------------*/ static bool shouldBeDescribed(void) { - if (!isPreBeta5(header->version)) - return getVisits(admin[HERO].location) % (current.visits+1) == 0 - || admin[admin[HERO].location].visitsCount == 0; - else - return admin[admin[HERO].location].visitsCount % (current.visits+1) == 0; + if (!isPreBeta5(header->version)) + return getVisits(admin[HERO].location) % (current.visits + 1) == 0 + || admin[admin[HERO].location].visitsCount == 0; + else + return admin[admin[HERO].location].visitsCount % (current.visits + 1) == 0; } /*----------------------------------------------------------------------*/ -static void locateActor(Aint movingActor, Aint whr) -{ - Aint previousCurrentLocation = current.location; - Aint previousActorLocation = admin[movingActor].location; - Aint previousActor = current.actor; - Aint previousInstance = current.instance; - - /* Before leaving, remember that we visited the location */ - if (!isPreBeta5(header->version)) - if (movingActor == (int)HERO) - incrementVisits(where(HERO, DIRECT)); - - /* TODO Actors locating into containers is dubious, anyway as it - is now it allows the hero to be located into a container. And what - happens with current location if so... */ - if (isAContainer(whr)) - locateIntoContainer(movingActor, whr); - else { - current.location = whr; - admin[movingActor].location = whr; - } - - /* Now we have moved, so show what is needed... */ - current.instance = current.location; - - /* Execute possible entered */ - current.actor = movingActor; - if (previousActorLocation != current.location) { - executeEntered(current.location); - } - current.instance = previousInstance; - current.actor = previousActor; - - if (movingActor == (int)HERO) { - if (shouldBeDescribed()) - look(); - else - revisited(); - admin[where(HERO, DIRECT)].visitsCount++; - } else - /* Ensure that the location will be described to the hero next time */ - admin[whr].visitsCount = 0; - - if (current.actor != movingActor) - current.location = previousCurrentLocation; - - current.instance = previousInstance; +static void locateActor(Aint movingActor, Aint whr) { + Aint previousCurrentLocation = current.location; + Aint previousActorLocation = admin[movingActor].location; + Aint previousActor = current.actor; + Aint previousInstance = current.instance; + + /* Before leaving, remember that we visited the location */ + if (!isPreBeta5(header->version)) + if (movingActor == (int)HERO) + incrementVisits(where(HERO, DIRECT)); + + /* TODO Actors locating into containers is dubious, anyway as it + is now it allows the hero to be located into a container. And what + happens with current location if so... */ + if (isAContainer(whr)) + locateIntoContainer(movingActor, whr); + else { + current.location = whr; + admin[movingActor].location = whr; + } + + /* Now we have moved, so show what is needed... */ + current.instance = current.location; + + /* Execute possible entered */ + current.actor = movingActor; + if (previousActorLocation != current.location) { + executeEntered(current.location); + } + current.instance = previousInstance; + current.actor = previousActor; + + if (movingActor == (int)HERO) { + if (shouldBeDescribed()) + look(); + else + revisited(); + admin[where(HERO, DIRECT)].visitsCount++; + } else + /* Ensure that the location will be described to the hero next time */ + admin[whr].visitsCount = 0; + + if (current.actor != movingActor) + current.location = previousCurrentLocation; + + current.instance = previousInstance; } /*----------------------------------------------------------------------*/ static void traceExtract(int instance, int containerId, const char *what) { - if (traceSectionOption) { - printf("\n<EXTRACT from "); - traceSay(instance); - printf("[%d, container %d], %s:>\n", instance, containerId, what); - } + if (traceSectionOption) { + printf("\n<EXTRACT from "); + traceSay(instance); + printf("[%d, container %d], %s:>\n", instance, containerId, what); + } } /*----------------------------------------------------------------------*/ static void containmentLoopError(int instance, int whr) { - ParameterArray parameters = newParameterArray(); - if (isPreBeta4(header->version)) - output("That would be to put something inside itself."); - else if (whr == instance) { - addParameterForInstance(parameters, instance); - printMessageWithParameters(M_CONTAINMENT_LOOP, parameters); - } else { - addParameterForInstance(parameters, instance); - addParameterForInstance(parameters, whr); - printMessageWithParameters(M_CONTAINMENT_LOOP2, parameters); - } - free(parameters); - error(NO_MSG); + ParameterArray parameters = newParameterArray(); + if (isPreBeta4(header->version)) + output("That would be to put something inside itself."); + else if (whr == instance) { + addParameterForInstance(parameters, instance); + printMessageWithParameters(M_CONTAINMENT_LOOP, parameters); + } else { + addParameterForInstance(parameters, instance); + addParameterForInstance(parameters, whr); + printMessageWithParameters(M_CONTAINMENT_LOOP2, parameters); + } + free(parameters); + error(NO_MSG); } /*----------------------------------------------------------------------*/ static void runExtractStatements(int instance, int containerId) { - ContainerEntry *theContainer = &containers[containerId]; + ContainerEntry *theContainer = &containers[containerId]; - if (theContainer->extractStatements != 0) { - traceExtract(instance, containerId, "Executing"); - interpret(theContainer->extractStatements); - } + if (theContainer->extractStatements != 0) { + traceExtract(instance, containerId, "Executing"); + interpret(theContainer->extractStatements); + } } /*----------------------------------------------------------------------*/ static bool runExtractChecks(int instance, int containerId) { - ContainerEntry *theContainer = &containers[containerId]; + ContainerEntry *theContainer = &containers[containerId]; - if (theContainer->extractChecks != 0) { - traceExtract(instance, containerId, "Checking"); - if (checksFailed(theContainer->extractChecks, EXECUTE_CHECK_BODY_ON_FAIL)) { - fail = TRUE; - return FALSE; /* Failed! */ - } - } - return TRUE; /* Passed! */ + if (theContainer->extractChecks != 0) { + traceExtract(instance, containerId, "Checking"); + if (checksFailed(theContainer->extractChecks, EXECUTE_CHECK_BODY_ON_FAIL)) { + fail = TRUE; + return FALSE; /* Failed! */ + } + } + return TRUE; /* Passed! */ } /*======================================================================*/ -void locate(int instance, int whr) -{ - int containerId; - int previousInstance = current.instance; - - verifyInstance(instance, "LOCATE"); - verifyInstance(whr, "LOCATE AT"); - - /* Will this create a containment loop? */ - if (whr == instance || (isAContainer(instance) && isIn(whr, instance, TRANSITIVE))) - containmentLoopError(instance, whr); - - /* First check if the instance is in a container, if so run extract checks */ - if (isAContainer(admin[instance].location)) { /* In something? */ - int loc = admin[instance].location; - - /* Run all nested extraction checks */ - while (isAContainer(loc)) { - current.instance = loc; - containerId = instances[loc].container; - - if (!runExtractChecks(instance, containerId)) { - current.instance = previousInstance; - return; - } - runExtractStatements(instance, containerId); - loc = admin[loc].location; - } - current.instance = previousInstance; - } - - if (isAActor(instance)) - locateActor(instance, whr); - else if (isALocation(instance)) - locateLocation(instance, whr); - else - locateObject(instance, whr); - - gameStateChanged = TRUE; +void locate(int instance, int whr) { + int containerId; + int previousInstance = current.instance; + + verifyInstance(instance, "LOCATE"); + verifyInstance(whr, "LOCATE AT"); + + /* Will this create a containment loop? */ + if (whr == instance || (isAContainer(instance) && isIn(whr, instance, TRANSITIVE))) + containmentLoopError(instance, whr); + + /* First check if the instance is in a container, if so run extract checks */ + if (isAContainer(admin[instance].location)) { /* In something? */ + int loc = admin[instance].location; + + /* Run all nested extraction checks */ + while (isAContainer(loc)) { + current.instance = loc; + containerId = instances[loc].container; + + if (!runExtractChecks(instance, containerId)) { + current.instance = previousInstance; + return; + } + runExtractStatements(instance, containerId); + loc = admin[loc].location; + } + current.instance = previousInstance; + } + + if (isAActor(instance)) + locateActor(instance, whr); + else if (isALocation(instance)) + locateLocation(instance, whr); + else + locateObject(instance, whr); + + gameStateChanged = TRUE; } } // End of namespace Alan3 diff --git a/engines/glk/alan3/instance.h b/engines/glk/alan3/instance.h index 2b5adb3229..6c62bb56d7 100644 --- a/engines/glk/alan3/instance.h +++ b/engines/glk/alan3/instance.h @@ -32,13 +32,13 @@ namespace Alan3 { /* Types: */ struct AdminEntry { /* Administrative data about instances */ - Aint location; - AttributeEntry *attributes; - Abool alreadyDescribed; - Aint visitsCount; - Aint script; - Aint step; - Aint waitCount; + Aint location; + AttributeEntry *attributes; + Abool alreadyDescribed; + Aint visitsCount; + Aint script; + Aint step; + Aint waitCount; }; diff --git a/engines/glk/alan3/inter.cpp b/engines/glk/alan3/inter.cpp index 457f75dc71..042144204f 100644 --- a/engines/glk/alan3/inter.cpp +++ b/engines/glk/alan3/inter.cpp @@ -59,335 +59,332 @@ static void (*interpreterMock)(Aaddr adr) = NULL; /*======================================================================*/ void setInterpreterMock(void (*mock)(Aaddr adr)) { - interpreterMock = mock; + interpreterMock = mock; } /*======================================================================*/ -void setInterpreterStack(Stack theStack) -{ - stack = theStack; +void setInterpreterStack(Stack theStack) { + stack = theStack; } /*----------------------------------------------------------------------*/ static void traceInstruction(const char *str, ...) { - va_list args; + va_list args; - if (traceInstructionOption) { - va_start(args, str); + if (traceInstructionOption) { + va_start(args, str); Common::String msg = Common::String::format(str, args); va_end(args); printf("%s", msg.c_str()); - } + } } /*----------------------------------------------------------------------*/ static void traceSkip() { - printf("\n : \t\t\t\t\t\t\t"); + printf("\n : \t\t\t\t\t\t\t"); } /*----------------------------------------------------------------------*/ -static void interpretIf(Aword v) -{ - int lev = 1; - Aword i; - - if (!v) { - /* Skip to next ELSE or ENDIF on same level */ - if (traceInstructionOption) traceSkip(); - while (TRUE) { - i = memory[pc++]; - if (I_CLASS(i) == (Aword)C_STMOP) - switch (I_OP(i)) { - case I_ELSE: - if (lev == 1) { - if (traceInstructionOption) - printf("\n%4x: ELSE\t\t\t\t\t\t", pc); - return; - } - break; - case I_IF: - lev++; - break; - case I_ENDIF: - lev--; - if (lev == 0) { - if (traceInstructionOption) - printf("\n%4x: ENDIF\t\t\t\t\t\t", pc); - return; - } - break; - } - } - } +static void interpretIf(Aword v) { + int lev = 1; + Aword i; + + if (!v) { + /* Skip to next ELSE or ENDIF on same level */ + if (traceInstructionOption) traceSkip(); + while (TRUE) { + i = memory[pc++]; + if (I_CLASS(i) == (Aword)C_STMOP) + switch (I_OP(i)) { + case I_ELSE: + if (lev == 1) { + if (traceInstructionOption) + printf("\n%4x: ELSE\t\t\t\t\t\t", pc); + return; + } + break; + case I_IF: + lev++; + break; + case I_ENDIF: + lev--; + if (lev == 0) { + if (traceInstructionOption) + printf("\n%4x: ENDIF\t\t\t\t\t\t", pc); + return; + } + break; + } + } + } } /*----------------------------------------------------------------------*/ -static void interpretElse(void) -{ - int lev = 1; - Aword i; - - if (traceInstructionOption) traceSkip(); - while (TRUE) { - /* Skip to ENDIF on the same level */ - i = memory[pc++]; - if (I_CLASS(i) == (Aword)C_STMOP) - switch (I_OP(i)) { - case I_ENDIF: - lev--; - if (lev == 0) return; - break; - case I_IF: - lev++; - break; - } - } +static void interpretElse(void) { + int lev = 1; + Aword i; + + if (traceInstructionOption) traceSkip(); + while (TRUE) { + /* Skip to ENDIF on the same level */ + i = memory[pc++]; + if (I_CLASS(i) == (Aword)C_STMOP) + switch (I_OP(i)) { + case I_ENDIF: + lev--; + if (lev == 0) return; + break; + case I_IF: + lev++; + break; + } + } } /*----------------------------------------------------------------------*/ static void goToLOOPEND(void) { - int level = 1; - int i; - - if (traceInstructionOption) traceSkip(); - while (TRUE) { - /* Skip past LOOPEND on the same level */ - i = memory[pc]; - if (I_CLASS(i) == (Aword)C_STMOP) - switch (I_OP(i)) { - case I_LOOPEND: - level--; - if (level == 0) - return; - break; - case I_LOOP: - level++; - break; - } - pc++; - } + int level = 1; + int i; + + if (traceInstructionOption) traceSkip(); + while (TRUE) { + /* Skip past LOOPEND on the same level */ + i = memory[pc]; + if (I_CLASS(i) == (Aword)C_STMOP) + switch (I_OP(i)) { + case I_LOOPEND: + level--; + if (level == 0) + return; + break; + case I_LOOP: + level++; + break; + } + pc++; + } } /*----------------------------------------------------------------------*/ static void jumpBackToStartOfMatchingLOOP(void) { - int level = 1; - int i; - - if (traceInstructionOption) traceSkip(); - pc--; /* Ignore the instruction we're on */ - while (TRUE) { - /* Skip back past LOOP on the same level */ - i = memory[--pc]; - if (I_CLASS(i) == (Aword)C_STMOP) - switch (I_OP(i)) { - case I_LOOPEND: - level++; - break; - case I_LOOP: - level--; - if (level == 0) { - return; - } - break; - } - } + int level = 1; + int i; + + if (traceInstructionOption) traceSkip(); + pc--; /* Ignore the instruction we're on */ + while (TRUE) { + /* Skip back past LOOP on the same level */ + i = memory[--pc]; + if (I_CLASS(i) == (Aword)C_STMOP) + switch (I_OP(i)) { + case I_LOOPEND: + level++; + break; + case I_LOOP: + level--; + if (level == 0) { + return; + } + break; + } + } } /*----------------------------------------------------------------------*/ -static void nextLoop(void) -{ - goToLOOPEND(); +static void nextLoop(void) { + goToLOOPEND(); } /*----------------------------------------------------------------------*/ -static void endLoop(Aint index, Aint limit) -{ - if (index < limit) { - index++; - push(stack, limit); - push(stack, index); - jumpBackToStartOfMatchingLOOP(); - if (traceInstructionOption) - printf("\n%4x: LOOP\t\t\t\t\t\t", pc); - pc++; - } +static void endLoop(Aint index, Aint limit) { + if (index < limit) { + index++; + push(stack, limit); + push(stack, index); + jumpBackToStartOfMatchingLOOP(); + if (traceInstructionOption) + printf("\n%4x: LOOP\t\t\t\t\t\t", pc); + pc++; + } } /*----------------------------------------------------------------------*/ -static void stackDup(void) -{ - push(stack, top(stack)); +static void stackDup(void) { + push(stack, top(stack)); } /*----------------------------------------------------------------------*/ -static void depexec(Aword v) -{ - int lev = 1; - Aword i; - const char *instructionString = "DEPELSE"; - - if (!v) { - /* The expression was not true, skip to next CASE on the same - level which could be a DEPCASE or DEPELSE */ - if (traceInstructionOption) printf("\n : "); - while (TRUE) { - i = memory[pc++]; - if (I_CLASS(i) == (Aword)C_STMOP) - switch (I_OP(i)) { - case I_DEPEND: - lev++; - break; - case I_ENDDEP: - if (lev == 1) { - pc--; - if (traceInstructionOption) - printf("\n%4x: ENDDEP", pc); - return; - } else - lev--; - break; - case I_DEPCASE: - instructionString = "DEPCASE"; - case I_DEPELSE: - if (lev == 1) { - if (traceInstructionOption) - printf("\n%4x: %s", pc, instructionString); - return; - } - break; - } - } - } +static void depexec(Aword v) { + int lev = 1; + Aword i; + const char *instructionString = "DEPELSE"; + + if (!v) { + /* The expression was not true, skip to next CASE on the same + level which could be a DEPCASE or DEPELSE */ + if (traceInstructionOption) printf("\n : "); + while (TRUE) { + i = memory[pc++]; + if (I_CLASS(i) == (Aword)C_STMOP) + switch (I_OP(i)) { + case I_DEPEND: + lev++; + break; + case I_ENDDEP: + if (lev == 1) { + pc--; + if (traceInstructionOption) + printf("\n%4x: ENDDEP", pc); + return; + } else + lev--; + break; + case I_DEPCASE: + instructionString = "DEPCASE"; + case I_DEPELSE: + if (lev == 1) { + if (traceInstructionOption) + printf("\n%4x: %s", pc, instructionString); + return; + } + break; + } + } + } } /*----------------------------------------------------------------------*/ -static void depcase(void) -{ - int lev = 1; - Aword i; - - /* - We have just executed a DEPCASE/DEPELSE clause as a result of a - DEPCASE catching so skip to end of DEPENDING block (next DEPEND - on same level) then return. - */ - - if (traceInstructionOption) printf("\n : "); - while (TRUE) { - i = memory[pc++]; - if (I_CLASS(i) == (Aword)C_STMOP) - switch (I_OP(i)) { - case I_DEPEND: - lev++; - break; - case I_ENDDEP: - lev--; - if (lev == 0) { - pc--; - return; - } - break; - } - } +static void depcase(void) { + int lev = 1; + Aword i; + + /* + We have just executed a DEPCASE/DEPELSE clause as a result of a + DEPCASE catching so skip to end of DEPENDING block (next DEPEND + on same level) then return. + */ + + if (traceInstructionOption) printf("\n : "); + while (TRUE) { + i = memory[pc++]; + if (I_CLASS(i) == (Aword)C_STMOP) + switch (I_OP(i)) { + case I_DEPEND: + lev++; + break; + case I_ENDDEP: + lev--; + if (lev == 0) { + pc--; + return; + } + break; + } + } } /*----------------------------------------------------------------------*/ static const char *booleanValue(Abool value) { - if (value) return " TRUE"; - else return " FALSE"; + if (value) return " TRUE"; + else return " FALSE"; } /*----------------------------------------------------------------------*/ static const char *stringValue(Aptr address) { - static char string[1000]; + static char string[1000]; - sprintf(string, "0x%lx (\"%s\")\t\t", (unsigned long) address, (char *)fromAptr(address)); - return string; + sprintf(string, "0x%lx (\"%s\")\t\t", (unsigned long) address, (char *)fromAptr(address)); + return string; } /*----------------------------------------------------------------------*/ static const char *pointerValue(Aptr address) { - static char string[100]; + static char string[100]; - sprintf(string, "@%6lx",(unsigned long) address); - return string; + sprintf(string, "@%6lx", (unsigned long) address); + return string; } /*----------------------------------------------------------------------*/ static void traceStringTopValue() { - if (traceInstructionOption) - printf("\t=%s", stringValue(top(stack))); + if (traceInstructionOption) + printf("\t=%s", stringValue(top(stack))); } /*----------------------------------------------------------------------*/ static void tracebooleanTopValue() { - if (traceInstructionOption) { - if (top(stack)) printf("\t=TRUE\t"); - else printf("\t=FALSE\t"); - } + if (traceInstructionOption) { + if (top(stack)) printf("\t=TRUE\t"); + else printf("\t=FALSE\t"); + } } /*----------------------------------------------------------------------*/ static void traceIntegerTopValue() { - if (traceInstructionOption) - printf("\t=%ld\t", (long)top(stack)); + if (traceInstructionOption) + printf("\t=%ld\t", (long)top(stack)); } /*----------------------------------------------------------------------*/ static void tracePointerTopValue() { - if (traceInstructionOption) - printf("\t=%s\t", pointerValue(top(stack))); + if (traceInstructionOption) + printf("\t=%s\t", pointerValue(top(stack))); } /*----------------------------------------------------------------------*/ static void traceInstanceTopValue() { - if (traceInstructionOption) { - printf("\t=%ld ('", (long)top(stack)); - traceSay(top(stack)); - printf("')"); - if (traceStackOption) - printf("\n\t\t\t\t\t\t\t"); - } + if (traceInstructionOption) { + printf("\t=%ld ('", (long)top(stack)); + traceSay(top(stack)); + printf("')"); + if (traceStackOption) + printf("\n\t\t\t\t\t\t\t"); + } } /*----------------------------------------------------------------------*/ static const char *transitivityFlag(ATrans value) { - switch (value) { - case TRANSITIVE: - return "Transitive"; - case DIRECT: - return "Direct"; - case INDIRECT: - return "Indirect"; - } - syserr("Unexpected transitivity"); - return "ERROR"; + switch (value) { + case TRANSITIVE: + return "Transitive"; + case DIRECT: + return "Direct"; + case INDIRECT: + return "Indirect"; + } + syserr("Unexpected transitivity"); + return "ERROR"; } /*----------------------------------------------------------------------*/ static const char *printForm(SayForm form) { - switch (form) { - case SAY_SIMPLE: return "-"; - case SAY_INDEFINITE: return "An"; - case SAY_DEFINITE: return "The"; - case SAY_NEGATIVE: return "No"; - case SAY_PRONOUN: return "It"; - } - return "**Unknown!!***"; + switch (form) { + case SAY_SIMPLE: + return "-"; + case SAY_INDEFINITE: + return "An"; + case SAY_DEFINITE: + return "The"; + case SAY_NEGATIVE: + return "No"; + case SAY_PRONOUN: + return "It"; + } + return "**Unknown!!***"; } @@ -396,14 +393,14 @@ int recursionDepth = 0; /*----------------------------------------------------------------------*/ static void checkForRecursion(Aaddr adr) { - int i; - - for (i = 0; i < recursionDepth; i++) - if (invocation[i] == adr) - apperr("Interpreter recursion."); - invocation[recursionDepth++] = adr; - if (recursionDepth > 1000) - syserr("Interpreter call stack too deep."); + int i; + + for (i = 0; i < recursionDepth; i++) + if (invocation[i] == adr) + apperr("Interpreter recursion."); + invocation[recursionDepth++] = adr; + if (recursionDepth > 1000) + syserr("Interpreter call stack too deep."); } @@ -417,995 +414,995 @@ static bool stillOnSameLine(Aint line, Aint file) { /*======================================================================*/ -void interpret(Aaddr adr) -{ - Aaddr oldpc; - Aword i; - - /* Check for mock implementation */ - if (interpreterMock != NULL) { - interpreterMock(adr); - return; - } - - /* Sanity checks: */ - if (adr == 0) syserr("Interpreting at address 0."); - checkForRecursion(adr); - - if (traceInstructionOption) - printf("\n++++++++++++++++++++++++++++++++++++++++++++++++++"); - - oldpc = pc; - pc = adr; - while(TRUE) { - if (pc > memTop) - syserr("Interpreting outside program."); - - i = memory[pc++]; - - switch (I_CLASS(i)) { - case C_CONST: - if (tracePushOption) printf("\n%4x: PUSH \t%7ld\t\t\t\t\t", pc-1, (long)I_OP(i)); - push(stack, I_OP(i)); - if (tracePushOption && traceStackOption) - dumpStack(stack); - break; - case C_CURVAR: - if (traceInstructionOption) printf("\n%4x: ", pc-1); - switch (I_OP(i)) { - case V_PARAM: - if (traceInstructionOption) printf("PARAM \t%7ld\t\t\t\t=%ld\t", (long)top(stack), - (long)globalParameters[top(stack)-1].instance); - push(stack, globalParameters[pop(stack)-1].instance); - break; - case V_CURLOC: - if (traceInstructionOption) printf("CURLOC \t\t\t\t\t=%d\t", current.location); - push(stack, current.location); - break; - case V_CURACT: - if (traceInstructionOption) printf("CURACT \t\t\t\t\t=%d\t", current.actor); - push(stack, current.actor); - break; - case V_CURVRB: - if (traceInstructionOption) printf("CURVRB \t\t\t\t\t=%d\t", current.verb); - push(stack, current.verb); - break; - case V_CURRENT_INSTANCE: - if (traceInstructionOption) printf("CURINS \t\t\t\t\t=%d\t", current.instance); - push(stack, current.instance); - break; - case V_SCORE: - if (traceInstructionOption) printf("CURSCORE \t\t\t\t\t=%d\t", current.score); - push(stack, current.score); - break; - case V_MAX_INSTANCE: { - int instanceMax = isPreBeta3(header->version)?header->instanceMax:header->instanceMax-1; - if (traceInstructionOption) printf("MAXINSTANCE \t\t\t\t=%d\t", instanceMax); - push(stack, instanceMax); - break; - } - default: - syserr("Unknown CURVAR instruction."); - break; - } - if (traceStackOption) - dumpStack(stack); - break; - - case C_STMOP: - if (traceInstructionOption) printf("\n%4x: ", pc-1); - switch (I_OP(i)) { - - case I_DUP: - if (traceInstructionOption) - printf("DUP\t\t\t\t\t\t"); - stackDup(); - break; - - case I_DUPSTR: - if (traceInstructionOption) - printf("DUPSTR\t\t\t\t\t\t"); - push(stack, toAptr(strdup((char*)fromAptr(top(stack))))); - break; - - case I_POP: { - Aptr top = pop(stack); - if (traceInstructionOption) - printf("POP\t%7ld", (long)top); - break; - } - - case I_LINE: { - Aint line = pop(stack); - Aint file = pop(stack); - traceInstruction("LINE\t%7ld, %7ld\t\t\t", (long)file, (long)line); - if (traceStackOption) - dumpStack(stack); - skipStackDump = TRUE; - if (line != 0) { - bool atNext = stopAtNextLine && line != current.sourceLine; - bool atBreakpoint = breakpointIndex(file, line) != -1; - if (traceSourceOption && stillOnSameLine(line, file)) { - if (col != 1 || traceInstructionOption) - printf("\n"); - showSourceLine(file, line); - if (!traceInstructionOption) - printf("\n"); - } - current.sourceLine = line; - current.sourceFile = file; - if (atNext || atBreakpoint) { - stopAtNextLine = FALSE; - debug(TRUE, line, file); - } - } - break; - } - - case I_PRINT: { - Aint fpos = pop(stack); - Aint len = pop(stack); - if (traceInstructionOption) { - printf("PRINT \t%7ld, %7ld\t\"", (long)fpos, (long)len); - col = 41; /* To break lines better! */ - } - print(fpos, len); - if (traceInstructionOption) { - printf("\""); - if (traceStackOption) - printf("\n\t\t\t\t\t\t\t"); - } - break; - } - - case I_STYLE: { - Aint style = pop(stack); - if (traceInstructionOption) { - printf("STYLE \t%7ld\t\t\"", (long)style); - } - setStyle(style); - break; - } - - case I_SYSTEM: { - Aint fpos = pop(stack); - Aint len = pop(stack); - if (traceInstructionOption) { - printf("SYSTEM \t%7ld, %7ld\t\"", (long)fpos, (long)len); - col = 34; /* To format it better! */ - } - sys(fpos, len); - if (traceInstructionOption) - printf("\"\t\t\t\t\t\t"); - break; - } - - case I_GETSTR: { - Aint fpos = pop(stack); - Aint len = pop(stack); - if (traceInstructionOption) - printf("GETSTR\t%7ld, %7ld", (long)fpos, (long)len); - push(stack, toAptr(getStringFromFile(fpos, len))); - traceStringTopValue(); - break; - } - - case I_QUIT: { - if (traceInstructionOption) - printf("QUIT\t\t\t\t\t\t"); - quitGame(); - break; - } - case I_LOOK: { - if (traceInstructionOption) - printf("LOOK\t\t\t\t\t\t"); - look(); - break; - } - case I_SAVE: { - if (traceInstructionOption) - printf("SAVE\t\t\t\t\t\t"); - save(); - break; - } - case I_RESTORE: { - if (traceInstructionOption) - printf("RESTORE\t\t\t\t\t\t"); - restore(); - break; - } - case I_RESTART: { - if (traceInstructionOption) - printf("RESTART\t\t\t\t\t\t"); - restartGame(); - break; - } - - case I_SCORE: { - Aint sc = pop(stack); - if (traceInstructionOption) - printf("SCORE \t%7ld\t\t=%ld\t\t\t", (long)sc, (long)scores[sc-1]); - score(sc); - break; - } - case I_VISITS: { - Aint v = pop(stack); - if (traceInstructionOption) - printf("VISITS \t%7ld\t\t\t\t\t", (long)v); - visits(v); - break; - } - - case I_LIST: { - Aint cnt = pop(stack); - if (traceInstructionOption) - printf("LIST \t%7ld\t\t\t\t\t", (long)cnt); - list(cnt); - break; - } - case I_EMPTY: { - Aint cnt = pop(stack); - Aint whr = pop(stack); - if (traceInstructionOption) - printf("EMPTY \t%7ld, %7ld\t\t\t\t", (long)cnt, (long)whr); - empty(cnt, whr); - break; - } - case I_SCHEDULE: { - Aint event = pop(stack); - Aint where = pop(stack); - Aint after = pop(stack); - if (traceInstructionOption) - printf("SCHEDULE \t%7ld, %7ld, %7ld\t\t\t\t", (long)event, (long)where, (long)after); - schedule(event, where, after); - break; - } - case I_CANCEL: { - Aint event = pop(stack); - if (traceInstructionOption) - printf("CANCEL \t%7ld\t\t\t\t", (long)event); - cancelEvent(event); - break; - } - case I_MAKE: { - Aint atr = pop(stack); - Aid id = pop(stack); - Abool val = pop(stack); - if (traceInstructionOption) - printf("MAKE \t%7ld, %7ld, %s\t\t\t", (long)id, (long)atr, booleanValue(val)); - setInstanceAttribute(id, atr, val); - break; - } - case I_SET: { - Aint atr = pop(stack); - Aid id = pop(stack); - Aptr val = pop(stack); - if (traceInstructionOption) { - printf("SET \t%7ld, %7ld, %7ld\t\t\t\t", (long)id, (long)atr, (long)val); - } - setInstanceAttribute(id, atr, val); - break; - } - case I_SETSTR: { - Aint atr = pop(stack); - Aid id = pop(stack); - Aptr str = pop(stack); - if (traceInstructionOption) { - printf("SETSTR\t%7ld, %7ld, %s\t\t\t\t", (long)id, (long)atr, stringValue(str)); - } - setInstanceStringAttribute(id, atr, (char *)fromAptr(str)); - break; - } - case I_SETSET: { - Aint atr = pop(stack); - Aid id = pop(stack); - Aptr set = pop(stack); - if (traceInstructionOption) { - printf("SETSET\t%7ld, %7ld, %7s\t\t", (long)id, (long)atr, pointerValue(set)); - } - setInstanceSetAttribute(id, atr, set); - break; - } - case I_NEWSET: { - Set *set = newSet(0); - if (traceInstructionOption) { - printf("NEWSET\t\t\t"); - } - push(stack, toAptr(set)); - tracePointerTopValue(); - break; - } - case I_UNION: { - Aptr set2 = pop(stack); - Aptr set1 = pop(stack); - if (traceInstructionOption) { - printf("UNION\t%7ld, %7ld\t\t\t\t", (long)set1, (long)set2); - } - push(stack, toAptr(setUnion((Set *)fromAptr(set1), (Set *)fromAptr(set2)))); - tracePointerTopValue(); - freeSet((Set *)fromAptr(set1)); - freeSet((Set *)fromAptr(set2)); - break; - } - case I_INCR: { - Aint step = pop(stack); - if (traceInstructionOption) { - printf("INCR\t%7ld", (long)step); - } - push(stack, pop(stack) + step); - traceIntegerTopValue(); - break; - } - case I_DECR: { - Aint step = pop(stack); - if (traceInstructionOption) { - printf("DECR\t%7ld\t\t\t\t\t", (long)step); - } - push(stack, pop(stack) - step); - traceIntegerTopValue(); - break; - } - case I_INCLUDE: { - Aint member = pop(stack); - if (traceInstructionOption) { - printf("INCLUDE\t%7ld\t\t\t\t\t", (long)member); - } - addToSet((Set *)fromAptr(top(stack)), member); - break; - } - case I_EXCLUDE: { - Aint member = pop(stack); - if (traceInstructionOption) { - printf("EXCLUDE\t%7ld", (long)member); - } - removeFromSet((Set *)fromAptr(top(stack)), member); - break; - } - case I_SETSIZE: { - Set *set = (Set *)fromAptr(pop(stack)); - if (traceInstructionOption) - printf("SETSIZE\t%7ld\t\t", (long)set); - push(stack, setSize(set)); - if (traceInstructionOption) - traceIntegerTopValue(); - break; - } - case I_SETMEMB: { - Set *set = (Set *)fromAptr(pop(stack)); - Aint index = pop(stack); - if (traceInstructionOption) - printf("SETMEMB\t%7ld, %7ld", (long)set, (long)index); - push(stack, getSetMember(set, index)); - if (traceInstructionOption) - traceIntegerTopValue(); - break; - } - case I_CONTSIZE: { - Abool transitivity = pop(stack); - Aint container = pop(stack); - if (traceInstructionOption) - printf("CONTSIZE\t%7ld, %7s\t", (long)container, transitivityFlag((ATrans)transitivity)); - push(stack, containerSize(container, (ATrans)transitivity)); - if (traceInstructionOption) - traceIntegerTopValue(); - break; - } - case I_CONTMEMB: { - Abool transitivity = pop(stack); - Aint container = pop(stack); - Aint index = pop(stack); - if (traceInstructionOption) - printf("CONTMEMB\t%7ld, %7ld, %7s", (long)container, (long)index, transitivityFlag((ATrans)transitivity)); - push(stack, getContainerMember(container, index, transitivity)); - if (traceInstructionOption) - traceIntegerTopValue(); - break; - } - case I_ATTRIBUTE: { - Aint atr = pop(stack); - Aid id = pop(stack); - if (traceInstructionOption) - printf("ATTRIBUTE %7ld, %7ld\t", (long)id, (long)atr); - push(stack, getInstanceAttribute(id, atr)); - traceIntegerTopValue(); - break; - } - case I_ATTRSTR: { - Aint atr = pop(stack); - Aid id = pop(stack); - if (traceInstructionOption) - printf("ATTRSTR \t%7ld, %7ld", (long)id, (long)atr); - push(stack, toAptr(getInstanceStringAttribute(id, atr))); - traceStringTopValue(); - break; - } - case I_ATTRSET: { - Aint atr = pop(stack); - Aid id = pop(stack); - if (traceInstructionOption) - printf("ATTRSET \t%7ld, %7ld", (long)id, (long)atr); - push(stack, toAptr(getInstanceSetAttribute(id, atr))); - tracePointerTopValue(); - break; - } - case I_SHOW: { - Aint image = pop(stack); - Aint align = pop(stack); - if (traceInstructionOption) - printf("SHOW \t%7ld, %7ld\t\t\t\t", (long)image, (long)align); - showImage(image, align); - break; - } - case I_PLAY: { - Aint sound = pop(stack); - if (traceInstructionOption) - printf("PLAY \t%7ld\t\t\t\t", (long)sound); - playSound(sound); - break; - } - case I_LOCATE: { - Aid id = pop(stack); - Aint whr = pop(stack); - if (traceInstructionOption) - printf("LOCATE \t%7ld, %7ld\t\t\t", (long)id, (long)whr); - locate(id, whr); - break; - } - case I_WHERE: { - Abool transitivity = pop(stack); - Aid id = pop(stack); - if (traceInstructionOption) - printf("WHERE \t%7ld, %7s", (long)id, transitivityFlag((ATrans)transitivity)); - push(stack, where(id, (ATrans)transitivity)); - traceInstanceTopValue(); - break; - } - case I_LOCATION: { - Aid id = pop(stack); - if (traceInstructionOption) - printf("LOCATION \t%7ld\t\t", (long)id); - push(stack, locationOf(id)); - traceInstanceTopValue(); - break; - } - case I_HERE: { - Abool transitivity = pop(stack); - Aid id = pop(stack); - if (traceInstructionOption) - printf("HERE \t%7ld, %s\t\t\t", (long)id, transitivityFlag((ATrans)transitivity)); - push(stack, isHere(id, (ATrans)transitivity)); - tracebooleanTopValue(); - break; - } - case I_NEARBY: { - Abool transitivity = pop(stack); - Aid id = pop(stack); - if (traceInstructionOption) - printf("NEARBY \t%7ld, %s\t\t\t", (long)id, transitivityFlag((ATrans)transitivity)); - push(stack, isNearby(id, (ATrans)transitivity)); - tracebooleanTopValue(); - break; - } - case I_NEAR: { - Abool transitivity = pop(stack); - Aint other = pop(stack); - Aid id = pop(stack); - if (traceInstructionOption) - printf("NEAR \t%7ld, %7ld, %s\t\t\t", (long)id, (long)other, transitivityFlag((ATrans)transitivity)); - push(stack, isNear(id, other, (ATrans)transitivity)); - tracebooleanTopValue(); - break; - } - case I_AT: { - Abool transitivity = pop(stack); - Aint other = pop(stack); - Aint instance = pop(stack); - if (traceInstructionOption) - printf("AT \t%7ld, %7ld, %s", (long)instance, (long)other, transitivityFlag((ATrans)transitivity)); - push(stack, isAt(instance, other, (ATrans)transitivity)); - tracebooleanTopValue(); - break; - } - case I_IN: { - Abool transitivity = pop(stack); - Aint cnt = pop(stack); - Aint obj = pop(stack); - if (traceInstructionOption) - printf("IN \t%7ld, %7ld, %s", (long)obj, (long)cnt, transitivityFlag((ATrans)transitivity)); - push(stack, isIn(obj, cnt, (ATrans)transitivity)); - tracebooleanTopValue(); - break; - } - case I_INSET: { - Aptr set = pop(stack); - Aword element = pop(stack); - if (traceInstructionOption) - printf("INSET \t%7ld, %7ld", (long)element, (long)set); - push(stack, inSet((Set*)fromAptr(set), element)); - freeSet((Set *)fromAptr(set)); - tracebooleanTopValue(); - break; - } - case I_USE: { - Aid act = pop(stack); - Aint scr = pop(stack); - if (traceInstructionOption) - printf("USE \t%7ld, %7ld\t\t\t\t", (long)act, (long)scr); - use(act, scr); - break; - } - case I_STOP: { - Aid actor = pop(stack); - if (traceInstructionOption) - printf("STOP \t%7ld\t\t\t\t\t", (long)actor); - stop(actor); - break; - } - case I_DESCRIBE: { - Aid id = pop(stack); - if (traceInstructionOption) { - printf("DESCRIBE \t%7ld\t\t\t", (long)id); - col = 41; /* To format it better! */ - } - describe(id); - if (traceInstructionOption) - printf("\n\t\t\t\t\t\t"); - break; - } - case I_SAY: { - Aint form = pop(stack); - Aid id = pop(stack); - if (traceInstructionOption) - printf("SAY\t%7s, %7ld\t\t\t", printForm((SayForm)form), (long)id); - if (form == SAY_SIMPLE) - say(id); - else - sayForm(id, (SayForm)form); - if (traceInstructionOption) - printf("\t\t\t\t\t\t\t"); - break; - } - case I_SAYINT: { - Aword val = pop(stack); - if (traceInstructionOption) - printf("SAYINT\t%7ld\t\t\t\"", (long)val); - sayInteger(val); - if (traceInstructionOption) - printf("\"\n\t\t\t\t\t\t\t"); - break; - } - case I_SAYSTR: { - Aptr sayAddr = pop(stack); - if (traceInstructionOption) - printf("SAYSTR\t%7ld\t\ty\t", (long)sayAddr); - sayString((char *)fromAptr(sayAddr)); - if (traceInstructionOption) - printf("\n\t\t\t\t\t\t"); - break; - } - case I_IF: { - Aword v = pop(stack); - if (traceInstructionOption) - printf("IF \t%s\t\t\t\t\t", booleanValue(v)); - interpretIf(v); - break; - } - case I_ELSE: { - if (traceInstructionOption) - printf("ELSE\t\t\t\t\t\t"); - interpretElse(); - break; - } - case I_ENDIF: { - if (traceInstructionOption) - printf("ENDIF\t\t\t\t\t\t"); - break; - } - case I_AND: { - Aword rh = pop(stack); - Aword lh = pop(stack); - if (traceInstructionOption) - printf("AND \t%s, %s", booleanValue(lh), booleanValue(rh)); - push(stack, lh && rh); - tracebooleanTopValue(); - break; - } - case I_OR: { - Aword rh = pop(stack); - Aword lh = pop(stack); - if (traceInstructionOption) - printf("OR \t%s, %s", booleanValue(lh), booleanValue(rh)); - push(stack, lh || rh); - tracebooleanTopValue(); - break; - } - case I_NE: { - Aword rh = pop(stack); - Aword lh = pop(stack); - if (traceInstructionOption) - printf("NE \t%7ld, %7ld", (long)lh, (long)rh); - push(stack, lh != rh); - tracebooleanTopValue(); - break; - } - case I_EQ: { - Aword rh = pop(stack); - Aword lh = pop(stack); - if (traceInstructionOption) - printf("EQ \t%7ld, %7ld", (long)lh, (long)rh); - push(stack, lh == rh); - tracebooleanTopValue(); - break; - } - case I_STREQ: { - Aptr rh = pop(stack); - Aptr lh = pop(stack); - if (traceInstructionOption) - printf("STREQ \t0x%lx, 0x%lx", (long)lh, (long)rh); - push(stack, streq((char *)fromAptr(lh), (char *)fromAptr(rh))); - tracebooleanTopValue(); - if (traceInstructionOption) - printf("\t"); - deallocate(fromAptr(lh)); - deallocate(fromAptr(rh)); - break; - } - case I_STREXACT: { - Aptr rh = pop(stack); - Aptr lh = pop(stack); - if (traceInstructionOption) - printf("STREXACT \t0x%lx, 0x%lx", (long)lh, (long)rh); - push(stack, strcmp((char *)fromAptr(lh), (char *)fromAptr(rh)) == 0); - tracebooleanTopValue(); - deallocate(fromAptr(lh)); - deallocate(fromAptr(rh)); - break; - } - case I_LE: { - Aint rh = pop(stack); - Aint lh = pop(stack); - if (traceInstructionOption) - printf("LE \t%7ld, %7ld", (long)lh, (long)rh); - push(stack, lh <= rh); - tracebooleanTopValue(); - break; - } - case I_GE: { - Aint rh = pop(stack); - Aint lh = pop(stack); - if (traceInstructionOption) - printf("GE \t%7ld, %7ld", (long)lh, (long)rh); - push(stack, lh >= rh); - tracebooleanTopValue(); - break; - } - case I_LT: { - Aint rh = pop(stack); - Aint lh = pop(stack); - if (traceInstructionOption) - printf("LT \t%7ld, %7ld", (long)lh, (long)rh); - push(stack, lh < rh); - tracebooleanTopValue(); - break; - } - case I_GT: { - Aint rh = pop(stack); - Aint lh = pop(stack); - if (traceInstructionOption) - printf("GT \t%7ld, %7ld", (long)lh, (long)rh); - push(stack, lh > rh); - tracebooleanTopValue(); - break; - } - case I_PLUS: { - Aint rh = pop(stack); - Aint lh = pop(stack); - if (traceInstructionOption) - printf("PLUS \t%7ld, %7ld", (long)lh, (long)rh); - push(stack, lh + rh); - traceIntegerTopValue(); - break; - } - case I_MINUS: { - Aint rh = pop(stack); - Aint lh = pop(stack); - if (traceInstructionOption) - printf("MINUS \t%7ld, %7ld", (long)lh, (long)rh); - push(stack, lh - rh); - traceIntegerTopValue(); - break; - } - case I_MULT: { - Aint rh = pop(stack); - Aint lh = pop(stack); - if (traceInstructionOption) - printf("MULT \t%7ld, %7ld", (long)lh, (long)rh); - push(stack, lh * rh); - traceIntegerTopValue(); - break; - } - case I_DIV: { - Aint rh = pop(stack); - Aint lh = pop(stack); - if (traceInstructionOption) - printf("DIV \t%7ld, %7ld", (long)lh, (long)rh); - push(stack, lh / rh); - traceIntegerTopValue(); - break; - } - case I_NOT: { - Aword val = pop(stack); - if (traceInstructionOption) - printf("NOT \t%s\t\t\t", booleanValue(val)); - push(stack, !val); - tracebooleanTopValue(); - break; - } - case I_RND: { - Aint from = pop(stack); - Aint to = pop(stack); - if (traceInstructionOption) - printf("RANDOM \t%7ld, %7ld", (long)from, (long)to); - push(stack, randomInteger(from, to)); - traceIntegerTopValue(); - break; - } - case I_BTW: { - Aint high = pop(stack); - Aint low = pop(stack); - Aint val = pop(stack); - if (traceInstructionOption) - printf("BETWEEN \t%7ld, %7ld, %7ld", (long)val, (long)low, (long)high); - push(stack, between(val, low, high)); - traceIntegerTopValue(); - break; - } - - /*------------------------------------------------------------* \ - String functions - \*------------------------------------------------------------*/ - case I_CONCAT: { - Aptr s2 = pop(stack); - Aptr s1 = pop(stack); - if (traceInstructionOption) - printf("CONCAT \t%s, %s", pointerValue(s1), pointerValue(s2)); - push(stack, concat(s1, s2)); - traceStringTopValue(); - deallocate(fromAptr(s1)); - deallocate(fromAptr(s2)); - break; - } - - case I_CONTAINS: { - Aptr substring = pop(stack); - Aptr string = pop(stack); - if (traceInstructionOption) - printf("CONTAINS \t%s, %s", pointerValue(string), pointerValue(substring)); - push(stack, contains(string, substring)); - traceIntegerTopValue(); - deallocate(fromAptr(string)); - deallocate(fromAptr(substring)); - break; - } - - case I_STRIP: { - Aint atr = pop(stack); - Aid id = pop(stack); - Aint words = pop(stack); - Aint count = pop(stack); - Aint first = pop(stack); - if (traceInstructionOption) - printf("STRIP \t%7ld, %7ld, %7ld, %7ld, %7ld", (long)first, (long)count, (long)words, (long)id, (long)atr); - push(stack, strip(first, count, words, id, atr)); - traceStringTopValue(); - break; - } - - /*------------------------------------------------------------ - Aggregation - ------------------------------------------------------------*/ - case I_MIN: - case I_SUM: - case I_MAX: { - Aint attribute = pop(stack); - Aint loopIndex = pop(stack); - Aint limit = pop(stack); - Aint aggregate = pop(stack); - switch (I_OP(i)) { - case I_MAX: - if (traceInstructionOption) - printf("MAX \t%7ld\t\t\t", (long)attribute); - if (aggregate < attribute) - push(stack, attribute); - else - push(stack, aggregate); - break; - case I_MIN: - if (traceInstructionOption) - printf("MIN \t%7ld\t\t\t", (long)attribute); - if (aggregate > attribute) - push(stack, attribute); - else - push(stack, aggregate); - break; - case I_SUM: - if (traceInstructionOption) - printf("SUM \t%7ld\t\t\t", (long)attribute); - push(stack, aggregate + attribute); - break; - } - traceIntegerTopValue(); - push(stack, limit); - push(stack, loopIndex); - break; - } - case I_COUNT: { - Aint loopIndex = pop(stack); - Aint limit = pop(stack); - if (traceInstructionOption) - printf("COUNT\t\t\t"); - push(stack, pop(stack) + 1); - traceIntegerTopValue(); - push(stack, limit); - push(stack, loopIndex); - break; - } - case I_TRANSCRIPT: { - Aint on_or_off = pop(stack); - if (traceInstructionOption) - printf("TRANSCRIPT\t\t\t"); - if (on_or_off) - startTranscript(); - else - stopTranscript(); - break; - } - - /*------------------------------------------------------------ - Depending On - ------------------------------------------------------------*/ - case I_DEPEND: - if (traceInstructionOption) - printf("DEPEND\t\t\t\t\t\t"); - break; - - case I_DEPCASE: - if (traceInstructionOption) - printf("DEPCASE\t\t\t\t\t\t"); - depcase(); - break; - - case I_DEPEXEC: { - Aword v = pop(stack); - if (traceInstructionOption) { - printf("DEPEXEC \t\t\t"); - if (v) printf(" TRUE"); else printf("FALSE"); - printf("\t\t\t\t\t"); - } - depexec(v); - break; - } - - case I_DEPELSE: - if (traceInstructionOption) - printf("DEPELSE\t\t\t\t\t\t"); - depcase(); - break; - - case I_ENDDEP: - if (traceInstructionOption) - printf("ENDDEP\t\t\t\t\t\t"); - pop(stack); - break; - - case I_ISA: { - Aid rh = pop(stack); - Aid lh = pop(stack); - if (traceInstructionOption) - printf("ISA \t%7ld, %7ld\t", (long)lh, (long)rh); - push(stack, isA(lh, rh)); - tracebooleanTopValue(); - break; - } - - case I_FRAME: { - Aint size = pop(stack); - if (traceInstructionOption) - printf("FRAME \t%7ld\t\t\t\t\t", (long)size); - newFrame(stack, size); - break; - } - - case I_GETLOCAL: { - Aint framesBelow = pop(stack); - Aint variableNumber = pop(stack); - if (traceInstructionOption) - printf("GETLOCAL \t%7ld, %7ld\t", (long)framesBelow, (long)variableNumber); - push(stack, getLocal(stack, framesBelow, variableNumber)); - traceIntegerTopValue(); - break; - } - - case I_SETLOCAL: { - Aint framesBelow = pop(stack); - Aint variableNumber = pop(stack); - Aint value = pop(stack); - if (traceInstructionOption) - printf("SETLOCAL \t%7ld, %7ld, %7ld\t\t", (long)framesBelow, (long)variableNumber, (long)value); - setLocal(stack, framesBelow, variableNumber, value); - break; - } - - case I_ENDFRAME: { - if (traceInstructionOption) - printf("ENDFRAME\t\t\t\t\t\t"); - endFrame(stack); - break; - } - - case I_LOOP: { - Aint index = pop(stack); - Aint limit = pop(stack); - if (traceInstructionOption) - printf("LOOP \t\t\t\t\t\t"); - push(stack, limit); - push(stack, index); - if (index > limit) - goToLOOPEND(); - break; - } - - case I_LOOPNEXT: { - if (traceInstructionOption) - printf("LOOPNEXT\t\t\t\t\t\t"); - nextLoop(); - break; - } - - case I_LOOPEND: { - Aint index = pop(stack); - Aint limit = pop(stack); - if (traceInstructionOption) - printf("LOOPEND\t\t\t\t\t\t"); - endLoop(index, limit); - break; - } - - case I_RETURN: - if (traceInstructionOption) - printf("RETURN\n--------------------------------------------------\n"); - pc = oldpc; - goto exitInterpreter; - - default: - syserr("Unknown STMOP instruction."); - break; - } - if (fail) { - pc = oldpc; - goto exitInterpreter; - } - if (traceStackOption) { - if (!skipStackDump) - dumpStack(stack); - skipStackDump = FALSE; - } - break; - - default: - syserr("Unknown instruction class."); - break; - } - } - exitInterpreter: - recursionDepth--; +void interpret(Aaddr adr) { + Aaddr oldpc; + Aword i; + + /* Check for mock implementation */ + if (interpreterMock != NULL) { + interpreterMock(adr); + return; + } + + /* Sanity checks: */ + if (adr == 0) syserr("Interpreting at address 0."); + checkForRecursion(adr); + + if (traceInstructionOption) + printf("\n++++++++++++++++++++++++++++++++++++++++++++++++++"); + + oldpc = pc; + pc = adr; + while (TRUE) { + if (pc > memTop) + syserr("Interpreting outside program."); + + i = memory[pc++]; + + switch (I_CLASS(i)) { + case C_CONST: + if (tracePushOption) printf("\n%4x: PUSH \t%7ld\t\t\t\t\t", pc - 1, (long)I_OP(i)); + push(stack, I_OP(i)); + if (tracePushOption && traceStackOption) + dumpStack(stack); + break; + case C_CURVAR: + if (traceInstructionOption) printf("\n%4x: ", pc - 1); + switch (I_OP(i)) { + case V_PARAM: + if (traceInstructionOption) printf("PARAM \t%7ld\t\t\t\t=%ld\t", (long)top(stack), + (long)globalParameters[top(stack) - 1].instance); + push(stack, globalParameters[pop(stack) - 1].instance); + break; + case V_CURLOC: + if (traceInstructionOption) printf("CURLOC \t\t\t\t\t=%d\t", current.location); + push(stack, current.location); + break; + case V_CURACT: + if (traceInstructionOption) printf("CURACT \t\t\t\t\t=%d\t", current.actor); + push(stack, current.actor); + break; + case V_CURVRB: + if (traceInstructionOption) printf("CURVRB \t\t\t\t\t=%d\t", current.verb); + push(stack, current.verb); + break; + case V_CURRENT_INSTANCE: + if (traceInstructionOption) printf("CURINS \t\t\t\t\t=%d\t", current.instance); + push(stack, current.instance); + break; + case V_SCORE: + if (traceInstructionOption) printf("CURSCORE \t\t\t\t\t=%d\t", current.score); + push(stack, current.score); + break; + case V_MAX_INSTANCE: { + int instanceMax = isPreBeta3(header->version) ? header->instanceMax : header->instanceMax - 1; + if (traceInstructionOption) printf("MAXINSTANCE \t\t\t\t=%d\t", instanceMax); + push(stack, instanceMax); + break; + } + default: + syserr("Unknown CURVAR instruction."); + break; + } + if (traceStackOption) + dumpStack(stack); + break; + + case C_STMOP: + if (traceInstructionOption) printf("\n%4x: ", pc - 1); + switch (I_OP(i)) { + + case I_DUP: + if (traceInstructionOption) + printf("DUP\t\t\t\t\t\t"); + stackDup(); + break; + + case I_DUPSTR: + if (traceInstructionOption) + printf("DUPSTR\t\t\t\t\t\t"); + push(stack, toAptr(strdup((char *)fromAptr(top(stack))))); + break; + + case I_POP: { + Aptr top = pop(stack); + if (traceInstructionOption) + printf("POP\t%7ld", (long)top); + break; + } + + case I_LINE: { + Aint line = pop(stack); + Aint file = pop(stack); + traceInstruction("LINE\t%7ld, %7ld\t\t\t", (long)file, (long)line); + if (traceStackOption) + dumpStack(stack); + skipStackDump = TRUE; + if (line != 0) { + bool atNext = stopAtNextLine && line != current.sourceLine; + bool atBreakpoint = breakpointIndex(file, line) != -1; + if (traceSourceOption && stillOnSameLine(line, file)) { + if (col != 1 || traceInstructionOption) + printf("\n"); + showSourceLine(file, line); + if (!traceInstructionOption) + printf("\n"); + } + current.sourceLine = line; + current.sourceFile = file; + if (atNext || atBreakpoint) { + stopAtNextLine = FALSE; + debug(TRUE, line, file); + } + } + break; + } + + case I_PRINT: { + Aint fpos = pop(stack); + Aint len = pop(stack); + if (traceInstructionOption) { + printf("PRINT \t%7ld, %7ld\t\"", (long)fpos, (long)len); + col = 41; /* To break lines better! */ + } + print(fpos, len); + if (traceInstructionOption) { + printf("\""); + if (traceStackOption) + printf("\n\t\t\t\t\t\t\t"); + } + break; + } + + case I_STYLE: { + Aint style = pop(stack); + if (traceInstructionOption) { + printf("STYLE \t%7ld\t\t\"", (long)style); + } + setStyle(style); + break; + } + + case I_SYSTEM: { + Aint fpos = pop(stack); + Aint len = pop(stack); + if (traceInstructionOption) { + printf("SYSTEM \t%7ld, %7ld\t\"", (long)fpos, (long)len); + col = 34; /* To format it better! */ + } + sys(fpos, len); + if (traceInstructionOption) + printf("\"\t\t\t\t\t\t"); + break; + } + + case I_GETSTR: { + Aint fpos = pop(stack); + Aint len = pop(stack); + if (traceInstructionOption) + printf("GETSTR\t%7ld, %7ld", (long)fpos, (long)len); + push(stack, toAptr(getStringFromFile(fpos, len))); + traceStringTopValue(); + break; + } + + case I_QUIT: { + if (traceInstructionOption) + printf("QUIT\t\t\t\t\t\t"); + quitGame(); + break; + } + case I_LOOK: { + if (traceInstructionOption) + printf("LOOK\t\t\t\t\t\t"); + look(); + break; + } + case I_SAVE: { + if (traceInstructionOption) + printf("SAVE\t\t\t\t\t\t"); + save(); + break; + } + case I_RESTORE: { + if (traceInstructionOption) + printf("RESTORE\t\t\t\t\t\t"); + restore(); + break; + } + case I_RESTART: { + if (traceInstructionOption) + printf("RESTART\t\t\t\t\t\t"); + restartGame(); + break; + } + + case I_SCORE: { + Aint sc = pop(stack); + if (traceInstructionOption) + printf("SCORE \t%7ld\t\t=%ld\t\t\t", (long)sc, (long)scores[sc - 1]); + score(sc); + break; + } + case I_VISITS: { + Aint v = pop(stack); + if (traceInstructionOption) + printf("VISITS \t%7ld\t\t\t\t\t", (long)v); + visits(v); + break; + } + + case I_LIST: { + Aint cnt = pop(stack); + if (traceInstructionOption) + printf("LIST \t%7ld\t\t\t\t\t", (long)cnt); + list(cnt); + break; + } + case I_EMPTY: { + Aint cnt = pop(stack); + Aint whr = pop(stack); + if (traceInstructionOption) + printf("EMPTY \t%7ld, %7ld\t\t\t\t", (long)cnt, (long)whr); + empty(cnt, whr); + break; + } + case I_SCHEDULE: { + Aint event = pop(stack); + Aint where = pop(stack); + Aint after = pop(stack); + if (traceInstructionOption) + printf("SCHEDULE \t%7ld, %7ld, %7ld\t\t\t\t", (long)event, (long)where, (long)after); + schedule(event, where, after); + break; + } + case I_CANCEL: { + Aint event = pop(stack); + if (traceInstructionOption) + printf("CANCEL \t%7ld\t\t\t\t", (long)event); + cancelEvent(event); + break; + } + case I_MAKE: { + Aint atr = pop(stack); + Aid id = pop(stack); + Abool val = pop(stack); + if (traceInstructionOption) + printf("MAKE \t%7ld, %7ld, %s\t\t\t", (long)id, (long)atr, booleanValue(val)); + setInstanceAttribute(id, atr, val); + break; + } + case I_SET: { + Aint atr = pop(stack); + Aid id = pop(stack); + Aptr val = pop(stack); + if (traceInstructionOption) { + printf("SET \t%7ld, %7ld, %7ld\t\t\t\t", (long)id, (long)atr, (long)val); + } + setInstanceAttribute(id, atr, val); + break; + } + case I_SETSTR: { + Aint atr = pop(stack); + Aid id = pop(stack); + Aptr str = pop(stack); + if (traceInstructionOption) { + printf("SETSTR\t%7ld, %7ld, %s\t\t\t\t", (long)id, (long)atr, stringValue(str)); + } + setInstanceStringAttribute(id, atr, (char *)fromAptr(str)); + break; + } + case I_SETSET: { + Aint atr = pop(stack); + Aid id = pop(stack); + Aptr set = pop(stack); + if (traceInstructionOption) { + printf("SETSET\t%7ld, %7ld, %7s\t\t", (long)id, (long)atr, pointerValue(set)); + } + setInstanceSetAttribute(id, atr, set); + break; + } + case I_NEWSET: { + Set *set = newSet(0); + if (traceInstructionOption) { + printf("NEWSET\t\t\t"); + } + push(stack, toAptr(set)); + tracePointerTopValue(); + break; + } + case I_UNION: { + Aptr set2 = pop(stack); + Aptr set1 = pop(stack); + if (traceInstructionOption) { + printf("UNION\t%7ld, %7ld\t\t\t\t", (long)set1, (long)set2); + } + push(stack, toAptr(setUnion((Set *)fromAptr(set1), (Set *)fromAptr(set2)))); + tracePointerTopValue(); + freeSet((Set *)fromAptr(set1)); + freeSet((Set *)fromAptr(set2)); + break; + } + case I_INCR: { + Aint step = pop(stack); + if (traceInstructionOption) { + printf("INCR\t%7ld", (long)step); + } + push(stack, pop(stack) + step); + traceIntegerTopValue(); + break; + } + case I_DECR: { + Aint step = pop(stack); + if (traceInstructionOption) { + printf("DECR\t%7ld\t\t\t\t\t", (long)step); + } + push(stack, pop(stack) - step); + traceIntegerTopValue(); + break; + } + case I_INCLUDE: { + Aint member = pop(stack); + if (traceInstructionOption) { + printf("INCLUDE\t%7ld\t\t\t\t\t", (long)member); + } + addToSet((Set *)fromAptr(top(stack)), member); + break; + } + case I_EXCLUDE: { + Aint member = pop(stack); + if (traceInstructionOption) { + printf("EXCLUDE\t%7ld", (long)member); + } + removeFromSet((Set *)fromAptr(top(stack)), member); + break; + } + case I_SETSIZE: { + Set *set = (Set *)fromAptr(pop(stack)); + if (traceInstructionOption) + printf("SETSIZE\t%7ld\t\t", (long)set); + push(stack, setSize(set)); + if (traceInstructionOption) + traceIntegerTopValue(); + break; + } + case I_SETMEMB: { + Set *set = (Set *)fromAptr(pop(stack)); + Aint index = pop(stack); + if (traceInstructionOption) + printf("SETMEMB\t%7ld, %7ld", (long)set, (long)index); + push(stack, getSetMember(set, index)); + if (traceInstructionOption) + traceIntegerTopValue(); + break; + } + case I_CONTSIZE: { + Abool transitivity = pop(stack); + Aint container = pop(stack); + if (traceInstructionOption) + printf("CONTSIZE\t%7ld, %7s\t", (long)container, transitivityFlag((ATrans)transitivity)); + push(stack, containerSize(container, (ATrans)transitivity)); + if (traceInstructionOption) + traceIntegerTopValue(); + break; + } + case I_CONTMEMB: { + Abool transitivity = pop(stack); + Aint container = pop(stack); + Aint index = pop(stack); + if (traceInstructionOption) + printf("CONTMEMB\t%7ld, %7ld, %7s", (long)container, (long)index, transitivityFlag((ATrans)transitivity)); + push(stack, getContainerMember(container, index, transitivity)); + if (traceInstructionOption) + traceIntegerTopValue(); + break; + } + case I_ATTRIBUTE: { + Aint atr = pop(stack); + Aid id = pop(stack); + if (traceInstructionOption) + printf("ATTRIBUTE %7ld, %7ld\t", (long)id, (long)atr); + push(stack, getInstanceAttribute(id, atr)); + traceIntegerTopValue(); + break; + } + case I_ATTRSTR: { + Aint atr = pop(stack); + Aid id = pop(stack); + if (traceInstructionOption) + printf("ATTRSTR \t%7ld, %7ld", (long)id, (long)atr); + push(stack, toAptr(getInstanceStringAttribute(id, atr))); + traceStringTopValue(); + break; + } + case I_ATTRSET: { + Aint atr = pop(stack); + Aid id = pop(stack); + if (traceInstructionOption) + printf("ATTRSET \t%7ld, %7ld", (long)id, (long)atr); + push(stack, toAptr(getInstanceSetAttribute(id, atr))); + tracePointerTopValue(); + break; + } + case I_SHOW: { + Aint image = pop(stack); + Aint align = pop(stack); + if (traceInstructionOption) + printf("SHOW \t%7ld, %7ld\t\t\t\t", (long)image, (long)align); + showImage(image, align); + break; + } + case I_PLAY: { + Aint sound = pop(stack); + if (traceInstructionOption) + printf("PLAY \t%7ld\t\t\t\t", (long)sound); + playSound(sound); + break; + } + case I_LOCATE: { + Aid id = pop(stack); + Aint whr = pop(stack); + if (traceInstructionOption) + printf("LOCATE \t%7ld, %7ld\t\t\t", (long)id, (long)whr); + locate(id, whr); + break; + } + case I_WHERE: { + Abool transitivity = pop(stack); + Aid id = pop(stack); + if (traceInstructionOption) + printf("WHERE \t%7ld, %7s", (long)id, transitivityFlag((ATrans)transitivity)); + push(stack, where(id, (ATrans)transitivity)); + traceInstanceTopValue(); + break; + } + case I_LOCATION: { + Aid id = pop(stack); + if (traceInstructionOption) + printf("LOCATION \t%7ld\t\t", (long)id); + push(stack, locationOf(id)); + traceInstanceTopValue(); + break; + } + case I_HERE: { + Abool transitivity = pop(stack); + Aid id = pop(stack); + if (traceInstructionOption) + printf("HERE \t%7ld, %s\t\t\t", (long)id, transitivityFlag((ATrans)transitivity)); + push(stack, isHere(id, (ATrans)transitivity)); + tracebooleanTopValue(); + break; + } + case I_NEARBY: { + Abool transitivity = pop(stack); + Aid id = pop(stack); + if (traceInstructionOption) + printf("NEARBY \t%7ld, %s\t\t\t", (long)id, transitivityFlag((ATrans)transitivity)); + push(stack, isNearby(id, (ATrans)transitivity)); + tracebooleanTopValue(); + break; + } + case I_NEAR: { + Abool transitivity = pop(stack); + Aint other = pop(stack); + Aid id = pop(stack); + if (traceInstructionOption) + printf("NEAR \t%7ld, %7ld, %s\t\t\t", (long)id, (long)other, transitivityFlag((ATrans)transitivity)); + push(stack, isNear(id, other, (ATrans)transitivity)); + tracebooleanTopValue(); + break; + } + case I_AT: { + Abool transitivity = pop(stack); + Aint other = pop(stack); + Aint instance = pop(stack); + if (traceInstructionOption) + printf("AT \t%7ld, %7ld, %s", (long)instance, (long)other, transitivityFlag((ATrans)transitivity)); + push(stack, isAt(instance, other, (ATrans)transitivity)); + tracebooleanTopValue(); + break; + } + case I_IN: { + Abool transitivity = pop(stack); + Aint cnt = pop(stack); + Aint obj = pop(stack); + if (traceInstructionOption) + printf("IN \t%7ld, %7ld, %s", (long)obj, (long)cnt, transitivityFlag((ATrans)transitivity)); + push(stack, isIn(obj, cnt, (ATrans)transitivity)); + tracebooleanTopValue(); + break; + } + case I_INSET: { + Aptr set = pop(stack); + Aword element = pop(stack); + if (traceInstructionOption) + printf("INSET \t%7ld, %7ld", (long)element, (long)set); + push(stack, inSet((Set *)fromAptr(set), element)); + freeSet((Set *)fromAptr(set)); + tracebooleanTopValue(); + break; + } + case I_USE: { + Aid act = pop(stack); + Aint scr = pop(stack); + if (traceInstructionOption) + printf("USE \t%7ld, %7ld\t\t\t\t", (long)act, (long)scr); + use(act, scr); + break; + } + case I_STOP: { + Aid actor = pop(stack); + if (traceInstructionOption) + printf("STOP \t%7ld\t\t\t\t\t", (long)actor); + stop(actor); + break; + } + case I_DESCRIBE: { + Aid id = pop(stack); + if (traceInstructionOption) { + printf("DESCRIBE \t%7ld\t\t\t", (long)id); + col = 41; /* To format it better! */ + } + describe(id); + if (traceInstructionOption) + printf("\n\t\t\t\t\t\t"); + break; + } + case I_SAY: { + Aint form = pop(stack); + Aid id = pop(stack); + if (traceInstructionOption) + printf("SAY\t%7s, %7ld\t\t\t", printForm((SayForm)form), (long)id); + if (form == SAY_SIMPLE) + say(id); + else + sayForm(id, (SayForm)form); + if (traceInstructionOption) + printf("\t\t\t\t\t\t\t"); + break; + } + case I_SAYINT: { + Aword val = pop(stack); + if (traceInstructionOption) + printf("SAYINT\t%7ld\t\t\t\"", (long)val); + sayInteger(val); + if (traceInstructionOption) + printf("\"\n\t\t\t\t\t\t\t"); + break; + } + case I_SAYSTR: { + Aptr sayAddr = pop(stack); + if (traceInstructionOption) + printf("SAYSTR\t%7ld\t\ty\t", (long)sayAddr); + sayString((char *)fromAptr(sayAddr)); + if (traceInstructionOption) + printf("\n\t\t\t\t\t\t"); + break; + } + case I_IF: { + Aword v = pop(stack); + if (traceInstructionOption) + printf("IF \t%s\t\t\t\t\t", booleanValue(v)); + interpretIf(v); + break; + } + case I_ELSE: { + if (traceInstructionOption) + printf("ELSE\t\t\t\t\t\t"); + interpretElse(); + break; + } + case I_ENDIF: { + if (traceInstructionOption) + printf("ENDIF\t\t\t\t\t\t"); + break; + } + case I_AND: { + Aword rh = pop(stack); + Aword lh = pop(stack); + if (traceInstructionOption) + printf("AND \t%s, %s", booleanValue(lh), booleanValue(rh)); + push(stack, lh && rh); + tracebooleanTopValue(); + break; + } + case I_OR: { + Aword rh = pop(stack); + Aword lh = pop(stack); + if (traceInstructionOption) + printf("OR \t%s, %s", booleanValue(lh), booleanValue(rh)); + push(stack, lh || rh); + tracebooleanTopValue(); + break; + } + case I_NE: { + Aword rh = pop(stack); + Aword lh = pop(stack); + if (traceInstructionOption) + printf("NE \t%7ld, %7ld", (long)lh, (long)rh); + push(stack, lh != rh); + tracebooleanTopValue(); + break; + } + case I_EQ: { + Aword rh = pop(stack); + Aword lh = pop(stack); + if (traceInstructionOption) + printf("EQ \t%7ld, %7ld", (long)lh, (long)rh); + push(stack, lh == rh); + tracebooleanTopValue(); + break; + } + case I_STREQ: { + Aptr rh = pop(stack); + Aptr lh = pop(stack); + if (traceInstructionOption) + printf("STREQ \t0x%lx, 0x%lx", (long)lh, (long)rh); + push(stack, streq((char *)fromAptr(lh), (char *)fromAptr(rh))); + tracebooleanTopValue(); + if (traceInstructionOption) + printf("\t"); + deallocate(fromAptr(lh)); + deallocate(fromAptr(rh)); + break; + } + case I_STREXACT: { + Aptr rh = pop(stack); + Aptr lh = pop(stack); + if (traceInstructionOption) + printf("STREXACT \t0x%lx, 0x%lx", (long)lh, (long)rh); + push(stack, strcmp((char *)fromAptr(lh), (char *)fromAptr(rh)) == 0); + tracebooleanTopValue(); + deallocate(fromAptr(lh)); + deallocate(fromAptr(rh)); + break; + } + case I_LE: { + Aint rh = pop(stack); + Aint lh = pop(stack); + if (traceInstructionOption) + printf("LE \t%7ld, %7ld", (long)lh, (long)rh); + push(stack, lh <= rh); + tracebooleanTopValue(); + break; + } + case I_GE: { + Aint rh = pop(stack); + Aint lh = pop(stack); + if (traceInstructionOption) + printf("GE \t%7ld, %7ld", (long)lh, (long)rh); + push(stack, lh >= rh); + tracebooleanTopValue(); + break; + } + case I_LT: { + Aint rh = pop(stack); + Aint lh = pop(stack); + if (traceInstructionOption) + printf("LT \t%7ld, %7ld", (long)lh, (long)rh); + push(stack, lh < rh); + tracebooleanTopValue(); + break; + } + case I_GT: { + Aint rh = pop(stack); + Aint lh = pop(stack); + if (traceInstructionOption) + printf("GT \t%7ld, %7ld", (long)lh, (long)rh); + push(stack, lh > rh); + tracebooleanTopValue(); + break; + } + case I_PLUS: { + Aint rh = pop(stack); + Aint lh = pop(stack); + if (traceInstructionOption) + printf("PLUS \t%7ld, %7ld", (long)lh, (long)rh); + push(stack, lh + rh); + traceIntegerTopValue(); + break; + } + case I_MINUS: { + Aint rh = pop(stack); + Aint lh = pop(stack); + if (traceInstructionOption) + printf("MINUS \t%7ld, %7ld", (long)lh, (long)rh); + push(stack, lh - rh); + traceIntegerTopValue(); + break; + } + case I_MULT: { + Aint rh = pop(stack); + Aint lh = pop(stack); + if (traceInstructionOption) + printf("MULT \t%7ld, %7ld", (long)lh, (long)rh); + push(stack, lh * rh); + traceIntegerTopValue(); + break; + } + case I_DIV: { + Aint rh = pop(stack); + Aint lh = pop(stack); + if (traceInstructionOption) + printf("DIV \t%7ld, %7ld", (long)lh, (long)rh); + push(stack, lh / rh); + traceIntegerTopValue(); + break; + } + case I_NOT: { + Aword val = pop(stack); + if (traceInstructionOption) + printf("NOT \t%s\t\t\t", booleanValue(val)); + push(stack, !val); + tracebooleanTopValue(); + break; + } + case I_RND: { + Aint from = pop(stack); + Aint to = pop(stack); + if (traceInstructionOption) + printf("RANDOM \t%7ld, %7ld", (long)from, (long)to); + push(stack, randomInteger(from, to)); + traceIntegerTopValue(); + break; + } + case I_BTW: { + Aint high = pop(stack); + Aint low = pop(stack); + Aint val = pop(stack); + if (traceInstructionOption) + printf("BETWEEN \t%7ld, %7ld, %7ld", (long)val, (long)low, (long)high); + push(stack, between(val, low, high)); + traceIntegerTopValue(); + break; + } + + /*------------------------------------------------------------* \ + String functions + \*------------------------------------------------------------*/ + case I_CONCAT: { + Aptr s2 = pop(stack); + Aptr s1 = pop(stack); + if (traceInstructionOption) + printf("CONCAT \t%s, %s", pointerValue(s1), pointerValue(s2)); + push(stack, concat(s1, s2)); + traceStringTopValue(); + deallocate(fromAptr(s1)); + deallocate(fromAptr(s2)); + break; + } + + case I_CONTAINS: { + Aptr substring = pop(stack); + Aptr string = pop(stack); + if (traceInstructionOption) + printf("CONTAINS \t%s, %s", pointerValue(string), pointerValue(substring)); + push(stack, contains(string, substring)); + traceIntegerTopValue(); + deallocate(fromAptr(string)); + deallocate(fromAptr(substring)); + break; + } + + case I_STRIP: { + Aint atr = pop(stack); + Aid id = pop(stack); + Aint words = pop(stack); + Aint count = pop(stack); + Aint first = pop(stack); + if (traceInstructionOption) + printf("STRIP \t%7ld, %7ld, %7ld, %7ld, %7ld", (long)first, (long)count, (long)words, (long)id, (long)atr); + push(stack, strip(first, count, words, id, atr)); + traceStringTopValue(); + break; + } + + /*------------------------------------------------------------ + Aggregation + ------------------------------------------------------------*/ + case I_MIN: + case I_SUM: + case I_MAX: { + Aint attribute = pop(stack); + Aint loopIndex = pop(stack); + Aint limit = pop(stack); + Aint aggregate = pop(stack); + switch (I_OP(i)) { + case I_MAX: + if (traceInstructionOption) + printf("MAX \t%7ld\t\t\t", (long)attribute); + if (aggregate < attribute) + push(stack, attribute); + else + push(stack, aggregate); + break; + case I_MIN: + if (traceInstructionOption) + printf("MIN \t%7ld\t\t\t", (long)attribute); + if (aggregate > attribute) + push(stack, attribute); + else + push(stack, aggregate); + break; + case I_SUM: + if (traceInstructionOption) + printf("SUM \t%7ld\t\t\t", (long)attribute); + push(stack, aggregate + attribute); + break; + } + traceIntegerTopValue(); + push(stack, limit); + push(stack, loopIndex); + break; + } + case I_COUNT: { + Aint loopIndex = pop(stack); + Aint limit = pop(stack); + if (traceInstructionOption) + printf("COUNT\t\t\t"); + push(stack, pop(stack) + 1); + traceIntegerTopValue(); + push(stack, limit); + push(stack, loopIndex); + break; + } + case I_TRANSCRIPT: { + Aint on_or_off = pop(stack); + if (traceInstructionOption) + printf("TRANSCRIPT\t\t\t"); + if (on_or_off) + startTranscript(); + else + stopTranscript(); + break; + } + + /*------------------------------------------------------------ + Depending On + ------------------------------------------------------------*/ + case I_DEPEND: + if (traceInstructionOption) + printf("DEPEND\t\t\t\t\t\t"); + break; + + case I_DEPCASE: + if (traceInstructionOption) + printf("DEPCASE\t\t\t\t\t\t"); + depcase(); + break; + + case I_DEPEXEC: { + Aword v = pop(stack); + if (traceInstructionOption) { + printf("DEPEXEC \t\t\t"); + if (v) printf(" TRUE"); + else printf("FALSE"); + printf("\t\t\t\t\t"); + } + depexec(v); + break; + } + + case I_DEPELSE: + if (traceInstructionOption) + printf("DEPELSE\t\t\t\t\t\t"); + depcase(); + break; + + case I_ENDDEP: + if (traceInstructionOption) + printf("ENDDEP\t\t\t\t\t\t"); + pop(stack); + break; + + case I_ISA: { + Aid rh = pop(stack); + Aid lh = pop(stack); + if (traceInstructionOption) + printf("ISA \t%7ld, %7ld\t", (long)lh, (long)rh); + push(stack, isA(lh, rh)); + tracebooleanTopValue(); + break; + } + + case I_FRAME: { + Aint size = pop(stack); + if (traceInstructionOption) + printf("FRAME \t%7ld\t\t\t\t\t", (long)size); + newFrame(stack, size); + break; + } + + case I_GETLOCAL: { + Aint framesBelow = pop(stack); + Aint variableNumber = pop(stack); + if (traceInstructionOption) + printf("GETLOCAL \t%7ld, %7ld\t", (long)framesBelow, (long)variableNumber); + push(stack, getLocal(stack, framesBelow, variableNumber)); + traceIntegerTopValue(); + break; + } + + case I_SETLOCAL: { + Aint framesBelow = pop(stack); + Aint variableNumber = pop(stack); + Aint value = pop(stack); + if (traceInstructionOption) + printf("SETLOCAL \t%7ld, %7ld, %7ld\t\t", (long)framesBelow, (long)variableNumber, (long)value); + setLocal(stack, framesBelow, variableNumber, value); + break; + } + + case I_ENDFRAME: { + if (traceInstructionOption) + printf("ENDFRAME\t\t\t\t\t\t"); + endFrame(stack); + break; + } + + case I_LOOP: { + Aint index = pop(stack); + Aint limit = pop(stack); + if (traceInstructionOption) + printf("LOOP \t\t\t\t\t\t"); + push(stack, limit); + push(stack, index); + if (index > limit) + goToLOOPEND(); + break; + } + + case I_LOOPNEXT: { + if (traceInstructionOption) + printf("LOOPNEXT\t\t\t\t\t\t"); + nextLoop(); + break; + } + + case I_LOOPEND: { + Aint index = pop(stack); + Aint limit = pop(stack); + if (traceInstructionOption) + printf("LOOPEND\t\t\t\t\t\t"); + endLoop(index, limit); + break; + } + + case I_RETURN: + if (traceInstructionOption) + printf("RETURN\n--------------------------------------------------\n"); + pc = oldpc; + goto exitInterpreter; + + default: + syserr("Unknown STMOP instruction."); + break; + } + if (fail) { + pc = oldpc; + goto exitInterpreter; + } + if (traceStackOption) { + if (!skipStackDump) + dumpStack(stack); + skipStackDump = FALSE; + } + break; + + default: + syserr("Unknown instruction class."); + break; + } + } +exitInterpreter: + recursionDepth--; } /*======================================================================*/ Aword evaluate(Aaddr adr) { - interpret(adr); - return pop(stack); + interpret(adr); + return pop(stack); } } // End of namespace Alan3 diff --git a/engines/glk/alan3/lists.cpp b/engines/glk/alan3/lists.cpp index c42e90e84d..1b50896cb6 100644 --- a/engines/glk/alan3/lists.cpp +++ b/engines/glk/alan3/lists.cpp @@ -31,34 +31,32 @@ void initArray(void *array) { } /* How to know we are at end of a table or array, first Aword == EOD */ -void implementationOfSetEndOfArray(Aword *adr) -{ +void implementationOfSetEndOfArray(Aword *adr) { *adr = EOD; } -bool implementationOfIsEndOfList(Aword *adr) -{ +bool implementationOfIsEndOfList(Aword *adr) { return *adr == EOD; } int lengthOfArrayImplementation(void *array_of_any_type, int element_size_in_bytes) { - int length; - int element_size = element_size_in_bytes/sizeof(Aword); - Aword *array = (Aword *)array_of_any_type; - if (array == NULL) - syserr("Taking length of NULL array"); - for (length = 0; !isEndOfArray(&array[length*element_size]); length++) - ; - return length; + int length; + int element_size = element_size_in_bytes / sizeof(Aword); + Aword *array = (Aword *)array_of_any_type; + if (array == NULL) + syserr("Taking length of NULL array"); + for (length = 0; !isEndOfArray(&array[length * element_size]); length++) + ; + return length; } void addElementImplementation(void *array_of_any_type, void *element, int element_size_in_bytes) { Aword *array = (Aword *)array_of_any_type; int length = lengthOfArray(array); - int element_size_in_words = element_size_in_bytes/sizeof(Aword); - memcpy(&array[length*element_size_in_words], element, element_size_in_bytes); - setEndOfArray(&array[(length+1)*element_size_in_words]); + int element_size_in_words = element_size_in_bytes / sizeof(Aword); + memcpy(&array[length * element_size_in_words], element, element_size_in_bytes); + setEndOfArray(&array[(length + 1)*element_size_in_words]); } } // End of namespace Alan3 diff --git a/engines/glk/alan3/literal.cpp b/engines/glk/alan3/literal.cpp index ad49aa8ed5..6040074c78 100644 --- a/engines/glk/alan3/literal.cpp +++ b/engines/glk/alan3/literal.cpp @@ -40,40 +40,41 @@ LiteralEntry *literals = literalTable; /*======================================================================*/ int instanceFromLiteral(int literalIndex) { - return literalIndex + header->instanceMax; + return literalIndex + header->instanceMax; } /*----------------------------------------------------------------------*/ void createIntegerLiteral(int integerValue) { - litCount++; - literals[litCount]._class = header->integerClassId; - literals[litCount].type = NUMERIC_LITERAL; - literals[litCount].value = integerValue; + litCount++; + literals[litCount]._class = header->integerClassId; + literals[litCount].type = NUMERIC_LITERAL; + literals[litCount].value = integerValue; } /*----------------------------------------------------------------------*/ void createStringLiteral(char *unquotedString) { - litCount++; - literals[litCount]._class = header->stringClassId; - literals[litCount].type = STRING_LITERAL; - literals[litCount].value = toAptr(strdup(unquotedString)); + litCount++; + literals[litCount]._class = header->stringClassId; + literals[litCount].type = STRING_LITERAL; + literals[litCount].value = toAptr(strdup(unquotedString)); } /*----------------------------------------------------------------------*/ void freeLiterals() { - int i; + int i; - for (i = 0; i <= litCount; i++) - if (literals[i].type == STRING_LITERAL && literals[i].value != 0) { - deallocate((void *)fromAptr(literals[i].value)); - } - litCount = 0;} + for (i = 0; i <= litCount; i++) + if (literals[i].type == STRING_LITERAL && literals[i].value != 0) { + deallocate((void *)fromAptr(literals[i].value)); + } + litCount = 0; +} /*======================================================================*/ int literalFromInstance(int instance) { - return instance - header->instanceMax; + return instance - header->instanceMax; } } // End of namespace Alan3 diff --git a/engines/glk/alan3/literal.h b/engines/glk/alan3/literal.h index 5afcd37b63..e16f190ba0 100644 --- a/engines/glk/alan3/literal.h +++ b/engines/glk/alan3/literal.h @@ -30,13 +30,13 @@ namespace Alan3 { /* TYPES */ enum LiteralType { - NO_LITERAL, NUMERIC_LITERAL, STRING_LITERAL + NO_LITERAL, NUMERIC_LITERAL, STRING_LITERAL }; struct LiteralEntry { /* LITERAL */ - Aint _class; /* Class id of the literal type */ - LiteralType type; - Aptr value; + Aint _class; /* Class id of the literal type */ + LiteralType type; + Aptr value; }; diff --git a/engines/glk/alan3/location.cpp b/engines/glk/alan3/location.cpp index eca958052d..2199b7a653 100644 --- a/engines/glk/alan3/location.cpp +++ b/engines/glk/alan3/location.cpp @@ -40,90 +40,87 @@ namespace Alan3 { /*----------------------------------------------------------------------*/ static void traceExit(int location, int dir, char *what) { - printf("\n<EXIT %s[%d] from ", - (char *)pointerTo(dictionary[playerWords[currentWordIndex-1].code].string), dir); - traceSay(location); - printf("[%d], %s:>\n", location, what); + printf("\n<EXIT %s[%d] from ", + (char *)pointerTo(dictionary[playerWords[currentWordIndex - 1].code].string), dir); + traceSay(location); + printf("[%d], %s:>\n", location, what); } /*======================================================================*/ -void go(int location, int dir) -{ - ExitEntry *theExit; - bool ok; - Aword oldloc; - - theExit = (ExitEntry *) pointerTo(instances[location].exits); - if (instances[location].exits != 0) - while (!isEndOfArray(theExit)) { - if (theExit->code == (uint)dir) { - ok = TRUE; - if (theExit->checks != 0) { - if (traceSectionOption) - traceExit(location, dir, "Checking"); - ok = !checksFailed(theExit->checks, EXECUTE_CHECK_BODY_ON_FAIL); - } - if (ok) { - oldloc = location; - if (theExit->action != 0) { - if (traceSectionOption) - traceExit(location, dir, "Executing"); - interpret(theExit->action); - } - /* Still at the same place? */ - if (where(HERO, TRANSITIVE) == (int)oldloc) { - if (traceSectionOption) - traceExit(location, dir, "Moving"); - locate(HERO, theExit->target); - } - return; - } else - error(NO_MSG); - } - theExit++; - } - error(M_NO_WAY); +void go(int location, int dir) { + ExitEntry *theExit; + bool ok; + Aword oldloc; + + theExit = (ExitEntry *) pointerTo(instances[location].exits); + if (instances[location].exits != 0) + while (!isEndOfArray(theExit)) { + if (theExit->code == (uint)dir) { + ok = TRUE; + if (theExit->checks != 0) { + if (traceSectionOption) + traceExit(location, dir, "Checking"); + ok = !checksFailed(theExit->checks, EXECUTE_CHECK_BODY_ON_FAIL); + } + if (ok) { + oldloc = location; + if (theExit->action != 0) { + if (traceSectionOption) + traceExit(location, dir, "Executing"); + interpret(theExit->action); + } + /* Still at the same place? */ + if (where(HERO, TRANSITIVE) == (int)oldloc) { + if (traceSectionOption) + traceExit(location, dir, "Moving"); + locate(HERO, theExit->target); + } + return; + } else + error(NO_MSG); + } + theExit++; + } + error(M_NO_WAY); } /*======================================================================*/ -bool exitto(int to, int from) -{ - ExitEntry *theExit; - - if (instances[from].exits == 0) - return FALSE; /* No exits */ - - for (theExit = (ExitEntry *) pointerTo(instances[from].exits); !isEndOfArray(theExit); theExit++) - if (theExit->target == (uint)to) - return TRUE; - - return FALSE; +bool exitto(int to, int from) { + ExitEntry *theExit; + + if (instances[from].exits == 0) + return FALSE; /* No exits */ + + for (theExit = (ExitEntry *) pointerTo(instances[from].exits); !isEndOfArray(theExit); theExit++) + if (theExit->target == (uint)to) + return TRUE; + + return FALSE; } /*======================================================================*/ -void look(void) -{ - uint i; +void look(void) { + uint i; - /* Set describe flag for all objects and actors */ - for (i = 1; i <= header->instanceMax; i++) - admin[i].alreadyDescribed = FALSE; + /* Set describe flag for all objects and actors */ + for (i = 1; i <= header->instanceMax; i++) + admin[i].alreadyDescribed = FALSE; - if (anyOutput) - para(); + if (anyOutput) + para(); - setSubHeaderStyle(); - sayInstance(current.location); - setNormalStyle(); + setSubHeaderStyle(); + sayInstance(current.location); + setNormalStyle(); - newline(); - capitalize = TRUE; - if (describe(current.location)) - describeInstances(); + newline(); + capitalize = TRUE; + if (describe(current.location)) + describeInstances(); } } // End of namespace Alan3 diff --git a/engines/glk/alan3/main.cpp b/engines/glk/alan3/main.cpp index e22c5be942..c9612db412 100644 --- a/engines/glk/alan3/main.cpp +++ b/engines/glk/alan3/main.cpp @@ -60,7 +60,7 @@ namespace Alan3 { /* PUBLIC DATA */ /* Amachine structures - Static */ -VerbEntry *vrbs; /* Verb table pointer */ +VerbEntry *vrbs; /* Verb table pointer */ /* PRIVATE DATA */ @@ -74,34 +74,33 @@ VerbEntry *vrbs; /* Verb table pointer */ /*----------------------------------------------------------------------*/ static char *eventName(int event) { - return stringAt(events[event].id); + return stringAt(events[event].id); } /*----------------------------------------------------------------------*/ -static void runPendingEvents(void) -{ - int i; - - resetRules(); - while (eventQueueTop != 0 && eventQueue[eventQueueTop-1].after == 0) { - eventQueueTop--; - if (isALocation(eventQueue[eventQueueTop].where)) - current.location = eventQueue[eventQueueTop].where; - else - current.location = where(eventQueue[eventQueueTop].where, TRANSITIVE); - if (traceSectionOption) { - printf("\n<EVENT %s[%d] (at ", eventName(eventQueue[eventQueueTop].event), - eventQueue[eventQueueTop].event); - traceSay(current.location); - printf(" [%d]):>\n", current.location); - } - interpret(events[eventQueue[eventQueueTop].event].code); - evaluateRules(rules); - } - - for (i = 0; i<eventQueueTop; i++) - eventQueue[i].after--; +static void runPendingEvents(void) { + int i; + + resetRules(); + while (eventQueueTop != 0 && eventQueue[eventQueueTop - 1].after == 0) { + eventQueueTop--; + if (isALocation(eventQueue[eventQueueTop].where)) + current.location = eventQueue[eventQueueTop].where; + else + current.location = where(eventQueue[eventQueueTop].where, TRANSITIVE); + if (traceSectionOption) { + printf("\n<EVENT %s[%d] (at ", eventName(eventQueue[eventQueueTop].event), + eventQueue[eventQueueTop].event); + traceSay(current.location); + printf(" [%d]):>\n", current.location); + } + interpret(events[eventQueue[eventQueueTop].event].code); + evaluateRules(rules); + } + + for (i = 0; i < eventQueueTop; i++) + eventQueue[i].after--; } @@ -122,13 +121,13 @@ Common::SeekableReadStream *codfil; header size for later versions. */ static int crcStart(char version[4]) { - /* Some earlier versions had a shorter header */ - if (isPreAlpha5(version)) - return sizeof(Pre3_0alpha5Header)/sizeof(Aword); - else if (isPreBeta2(version)) - return sizeof(Pre3_0beta2Header)/sizeof(Aword); - else - return sizeof(ACodeHeader)/sizeof(Aword); + /* Some earlier versions had a shorter header */ + if (isPreAlpha5(version)) + return sizeof(Pre3_0alpha5Header) / sizeof(Aword); + else if (isPreBeta2(version)) + return sizeof(Pre3_0beta2Header) / sizeof(Aword); + else + return sizeof(ACodeHeader) / sizeof(Aword); } @@ -137,47 +136,47 @@ static void readTemporaryHeader(ACodeHeader *tmphdr) { codfil->seek(0); codfil->read(tmphdr, sizeof(*tmphdr)); codfil->seek(0); - + if (strncmp((char *)tmphdr, "ALAN", 4) != 0) - playererr("Not an Alan game file, does not start with \"ALAN\""); + playererr("Not an Alan game file, does not start with \"ALAN\""); } /*----------------------------------------------------------------------*/ static void reverseMemory() { - if (littleEndian()) { - if (debugOption||traceSectionOption||traceInstructionOption) - output("<Hmm, this is a little-endian machine, fixing byte ordering...."); - reverseACD(); /* Reverse content of the ACD file */ - if (debugOption||traceSectionOption||traceInstructionOption) - output("OK.>$n"); - } + if (littleEndian()) { + if (debugOption || traceSectionOption || traceInstructionOption) + output("<Hmm, this is a little-endian machine, fixing byte ordering...."); + reverseACD(); /* Reverse content of the ACD file */ + if (debugOption || traceSectionOption || traceInstructionOption) + output("OK.>$n"); + } } /*----------------------------------------------------------------------*/ static void setupHeader(ACodeHeader tmphdr) { - if (isPreBeta2(tmphdr.version)) { - header = (ACodeHeader *)duplicate(&memory[0], sizeof(ACodeHeader)); - if (isPreAlpha5(tmphdr.version)) { - header->ifids = 0; - } - header->prompt = 0; - } else if (isPreBeta3(tmphdr.version)) { - header = (ACodeHeader *)pointerTo(0); - } else { - header = (ACodeHeader *)pointerTo(0); - } + if (isPreBeta2(tmphdr.version)) { + header = (ACodeHeader *)duplicate(&memory[0], sizeof(ACodeHeader)); + if (isPreAlpha5(tmphdr.version)) { + header->ifids = 0; + } + header->prompt = 0; + } else if (isPreBeta3(tmphdr.version)) { + header = (ACodeHeader *)pointerTo(0); + } else { + header = (ACodeHeader *)pointerTo(0); + } } /*----------------------------------------------------------------------*/ static void loadAndCheckMemory(ACodeHeader tmphdr, Aword crc, char err[]) { int i; - /* No memory allocated yet? */ - if (memory == NULL) { - memory = (Aword *)allocate(tmphdr.size * sizeof(Aword)); - } + /* No memory allocated yet? */ + if (memory == NULL) { + memory = (Aword *)allocate(tmphdr.size * sizeof(Aword)); + } memTop = tmphdr.size; if ((int)(sizeof(Aword) * tmphdr.size) > codfil->size()) @@ -186,560 +185,550 @@ static void loadAndCheckMemory(ACodeHeader tmphdr, Aword crc, char err[]) { for (i = 0; i < (int)tmphdr.size; ++i) memory[i] = codfil->readUint32LE(); - /* Calculate checksum */ - for (i = crcStart(tmphdr.version); i < memTop; i++) { - crc += memory[i]&0xff; - crc += (memory[i]>>8)&0xff; - crc += (memory[i]>>16)&0xff; - crc += (memory[i]>>24)&0xff; - } - if (crc != tmphdr.acdcrc) { - sprintf(err, "Checksum error in Acode (.a3c) file (0x%lx instead of 0x%lx).", - (unsigned long) crc, (unsigned long) tmphdr.acdcrc); - if (!ignoreErrorOption) - syserr(err); - else { - output("<WARNING! $$"); - output(err); - output("$$ Ignored, proceed at your own risk.>$n"); - } - } + /* Calculate checksum */ + for (i = crcStart(tmphdr.version); i < memTop; i++) { + crc += memory[i] & 0xff; + crc += (memory[i] >> 8) & 0xff; + crc += (memory[i] >> 16) & 0xff; + crc += (memory[i] >> 24) & 0xff; + } + if (crc != tmphdr.acdcrc) { + sprintf(err, "Checksum error in Acode (.a3c) file (0x%lx instead of 0x%lx).", + (unsigned long) crc, (unsigned long) tmphdr.acdcrc); + if (!ignoreErrorOption) + syserr(err); + else { + output("<WARNING! $$"); + output(err); + output("$$ Ignored, proceed at your own risk.>$n"); + } + } } /*----------------------------------------------------------------------*/ static const char *decodeState(int c) { - static char state[3] = "\0\0"; - switch (c) { - case 0: return "."; - case 'd': return "dev"; - case 'a': return "alpha"; - case 'b': return "beta"; - default: - state[0] = header->version[3]; - return state; - } + static char state[3] = "\0\0"; + switch (c) { + case 0: + return "."; + case 'd': + return "dev"; + case 'a': + return "alpha"; + case 'b': + return "beta"; + default: + state[0] = header->version[3]; + return state; + } } /*======================================================================*/ char *decodedGameVersion(char version[]) { - static char str[100]; - sprintf(str, "%d.%d%s%d", - (int)version[3], - (int)version[2], - decodeState(version[0]), - (int)version[1]); - return str; + static char str[100]; + sprintf(str, "%d.%d%s%d", + (int)version[3], + (int)version[2], + decodeState(version[0]), + (int)version[1]); + return str; } /*----------------------------------------------------------------------*/ static void incompatibleDevelopmentVersion(ACodeHeader *hdr) { - char str[80]; - sprintf(str, "Incompatible version of ACODE program. Development versions always require exact match. Game is %ld.%ld%s%ld, interpreter %ld.%ld%s%ld!", - (long)(hdr->version[0]), - (long)(hdr->version[1]), - decodeState(hdr->version[3]), - (long)(hdr->version[2]), - (long)alan.version.version, - (long)alan.version.revision, - alan.version.state, - (long)alan.version.correction); - apperr(str); + char str[80]; + sprintf(str, "Incompatible version of ACODE program. Development versions always require exact match. Game is %ld.%ld%s%ld, interpreter %ld.%ld%s%ld!", + (long)(hdr->version[0]), + (long)(hdr->version[1]), + decodeState(hdr->version[3]), + (long)(hdr->version[2]), + (long)alan.version.version, + (long)alan.version.revision, + alan.version.state, + (long)alan.version.correction); + apperr(str); } /*----------------------------------------------------------------------*/ static void incompatibleVersion(ACodeHeader *hdr) { - char str[80]; - sprintf(str, "Incompatible version of ACODE program. Game is %ld.%ld, interpreter %ld.%ld.", - (long)(hdr->version[0]), - (long)(hdr->version[1]), - (long)alan.version.version, - (long)alan.version.revision); - apperr(str); + char str[80]; + sprintf(str, "Incompatible version of ACODE program. Game is %ld.%ld, interpreter %ld.%ld.", + (long)(hdr->version[0]), + (long)(hdr->version[1]), + (long)alan.version.version, + (long)alan.version.revision); + apperr(str); } /*----------------------------------------------------------------------*/ static void alphaRunningLaterGame(char gameState) { - output("<WARNING! You are running an alpha interpreter, but the game is generated by a"); - if (gameState == 'b') - output("beta"); - else - output("release"); - output("state compiler which was released later. This might cause the game to not work fully as intended. Look for an upgraded game file.>\n"); + output("<WARNING! You are running an alpha interpreter, but the game is generated by a"); + if (gameState == 'b') + output("beta"); + else + output("release"); + output("state compiler which was released later. This might cause the game to not work fully as intended. Look for an upgraded game file.>\n"); } /*----------------------------------------------------------------------*/ static void nonDevelopmentRunningDevelopmentStateGame(char version[]) { - char errorMessage[200]; - char versionString[100]; - - strcpy(errorMessage, "Games generated by a development state compiler"); - sprintf(versionString, "(this game is v%d.%d.%d%s)", version[0], version[1], - version[2], decodeState(version[3])); - strcat(errorMessage, versionString); - strcat(errorMessage, "can only be run with a matching interpreter. Look for a game file generated with an alpha, beta or release state compiler.>\n"); - apperr(errorMessage); + char errorMessage[200]; + char versionString[100]; + + strcpy(errorMessage, "Games generated by a development state compiler"); + sprintf(versionString, "(this game is v%d.%d.%d%s)", version[0], version[1], + version[2], decodeState(version[3])); + strcat(errorMessage, versionString); + strcat(errorMessage, "can only be run with a matching interpreter. Look for a game file generated with an alpha, beta or release state compiler.>\n"); + apperr(errorMessage); } /*======================================================================*/ -void checkVersion(ACodeHeader *hdr) -{ - /* Strategy for version matching is: - 1) Development interpreters/games require exact match - 2) Alpha, Beta and Release interpreters will not run development games - 3) Alpha interpreters must warn if they run beta or release games - 4) Beta interpreters may introduce changes which are not alpha compatible, - if the change is a strict addition (i.e. if not used will not affect - alpha interpreters, example is introduction of a new opcode if it is - done at the end of the list) - 5) Release interpreters should run alpha and beta games without problems - - NOTE that we are working with a non-reversed version string/word here. - */ - - char interpreterVersion[4]; - bool developmentVersion; - bool alphaVersion; - int compareLength; - char gameState = hdr->version[3]; - - /* Construct our own version */ - interpreterVersion[0] = alan.version.version; - interpreterVersion[1] = alan.version.revision; - interpreterVersion[2] = alan.version.correction; - interpreterVersion[3] = alan.version.state[0]; - - /* Check version of .ACD file */ - if (debugOption && !regressionTestOption) { - printf("<Version of '%s' is %d.%d%s%d!>\n", - adventureFileName, - (int)hdr->version[0], - (int)hdr->version[1], - decodeState(hdr->version[3]), - (int)hdr->version[2]); - newline(); - } - - /* Development version require exact match, else only 2 digit match */ - developmentVersion = (strcmp(alan.version.state, "dev") == 0); - alphaVersion = (strcmp(alan.version.state, "alpha") == 0); - compareLength = (developmentVersion? 3 : 2); - - if (gameState == 'd' && !developmentVersion) - /* Development state game requires development state interpreter... */ - nonDevelopmentRunningDevelopmentStateGame(hdr->version); - else { - /* Compatible if version, revision (and correction if dev state) match... */ - if (memcmp(hdr->version, interpreterVersion, compareLength) != 0) { - /* Mismatch! */ - if (!ignoreErrorOption) { - if (developmentVersion) - incompatibleDevelopmentVersion(hdr); - else - incompatibleVersion(hdr); - } else - output("<WARNING! Incompatible version of ACODE program.>\n"); - } else if (developmentVersion && gameState != 'd') - /* ... unless interpreter is development and game not */ - incompatibleDevelopmentVersion(hdr); - else if (alphaVersion && gameState != 'a') { - /* If interpreter is alpha version and the game is later, warn! */ - alphaRunningLaterGame(gameState); - } - } +void checkVersion(ACodeHeader *hdr) { + /* Strategy for version matching is: + 1) Development interpreters/games require exact match + 2) Alpha, Beta and Release interpreters will not run development games + 3) Alpha interpreters must warn if they run beta or release games + 4) Beta interpreters may introduce changes which are not alpha compatible, + if the change is a strict addition (i.e. if not used will not affect + alpha interpreters, example is introduction of a new opcode if it is + done at the end of the list) + 5) Release interpreters should run alpha and beta games without problems + + NOTE that we are working with a non-reversed version string/word here. + */ + + char interpreterVersion[4]; + bool developmentVersion; + bool alphaVersion; + int compareLength; + char gameState = hdr->version[3]; + + /* Construct our own version */ + interpreterVersion[0] = alan.version.version; + interpreterVersion[1] = alan.version.revision; + interpreterVersion[2] = alan.version.correction; + interpreterVersion[3] = alan.version.state[0]; + + /* Check version of .ACD file */ + if (debugOption && !regressionTestOption) { + printf("<Version of '%s' is %d.%d%s%d!>\n", + adventureFileName, + (int)hdr->version[0], + (int)hdr->version[1], + decodeState(hdr->version[3]), + (int)hdr->version[2]); + newline(); + } + + /* Development version require exact match, else only 2 digit match */ + developmentVersion = (strcmp(alan.version.state, "dev") == 0); + alphaVersion = (strcmp(alan.version.state, "alpha") == 0); + compareLength = (developmentVersion ? 3 : 2); + + if (gameState == 'd' && !developmentVersion) + /* Development state game requires development state interpreter... */ + nonDevelopmentRunningDevelopmentStateGame(hdr->version); + else { + /* Compatible if version, revision (and correction if dev state) match... */ + if (memcmp(hdr->version, interpreterVersion, compareLength) != 0) { + /* Mismatch! */ + if (!ignoreErrorOption) { + if (developmentVersion) + incompatibleDevelopmentVersion(hdr); + else + incompatibleVersion(hdr); + } else + output("<WARNING! Incompatible version of ACODE program.>\n"); + } else if (developmentVersion && gameState != 'd') + /* ... unless interpreter is development and game not */ + incompatibleDevelopmentVersion(hdr); + else if (alphaVersion && gameState != 'a') { + /* If interpreter is alpha version and the game is later, warn! */ + alphaRunningLaterGame(gameState); + } + } } /*----------------------------------------------------------------------*/ -static void load(void) -{ - ACodeHeader tmphdr; - Aword crc = 0; - char err[100]; +static void load(void) { + ACodeHeader tmphdr; + Aword crc = 0; + char err[100]; - readTemporaryHeader(&tmphdr); - checkVersion(&tmphdr); + readTemporaryHeader(&tmphdr); + checkVersion(&tmphdr); - /* Allocate and load memory */ + /* Allocate and load memory */ - if (littleEndian()) - reverseHdr(&tmphdr); + if (littleEndian()) + reverseHdr(&tmphdr); - if (tmphdr.size <= sizeof(ACodeHeader)/sizeof(Aword)) - syserr("Malformed game file. Too small."); + if (tmphdr.size <= sizeof(ACodeHeader) / sizeof(Aword)) + syserr("Malformed game file. Too small."); - loadAndCheckMemory(tmphdr, crc, err); + loadAndCheckMemory(tmphdr, crc, err); - reverseMemory(); - setupHeader(tmphdr); + reverseMemory(); + setupHeader(tmphdr); } /*----------------------------------------------------------------------*/ -static void checkDebug(void) -{ - /* Make sure he can't debug if not allowed! */ - if (!header->debug) { - if (debugOption|traceSectionOption|traceInstructionOption) { - printf("<Sorry, '%s' is not compiled for debug! Exiting.>\n", adventureFileName); - terminate(0); - } - para(); - debugOption = FALSE; - traceSectionOption = FALSE; - traceInstructionOption = FALSE; - tracePushOption = FALSE; - } +static void checkDebug(void) { + /* Make sure he can't debug if not allowed! */ + if (!header->debug) { + if (debugOption | traceSectionOption | traceInstructionOption) { + printf("<Sorry, '%s' is not compiled for debug! Exiting.>\n", adventureFileName); + terminate(0); + } + para(); + debugOption = FALSE; + traceSectionOption = FALSE; + traceInstructionOption = FALSE; + tracePushOption = FALSE; + } #ifdef TODO - if (debugOption || regressionTestOption) /* If debugging... */ - srand(1); /* ... use no randomization */ - else - srand(time(0)); /* Else seed random generator */ + if (debugOption || regressionTestOption) /* If debugging... */ + srand(1); /* ... use no randomization */ + else + srand(time(0)); /* Else seed random generator */ #endif } /*----------------------------------------------------------------------*/ -static void initStaticData(void) -{ - /* Dictionary */ - dictionary = (DictionaryEntry *) pointerTo(header->dictionary); - /* Find out number of entries in dictionary */ - for (dictionarySize = 0; !isEndOfArray(&dictionary[dictionarySize]); dictionarySize++); - - /* Scores */ - - - /* All addresses to tables indexed by ids are converted to pointers, - then adjusted to point to the (imaginary) element before the - actual table so that [0] does not exist. Instead indices goes - from 1 and we can use [1]. */ - - if (header->instanceTableAddress == 0) - syserr("Instance table pointer == 0"); - instances = (InstanceEntry *) pointerTo(header->instanceTableAddress); - instances--; /* Back up one so that first is no. 1 */ - - - if (header->classTableAddress == 0) - syserr("Class table pointer == 0"); - classes = (ClassEntry *) pointerTo(header->classTableAddress); - classes--; /* Back up one so that first is no. 1 */ - - if (header->containerTableAddress != 0) { - containers = (ContainerEntry *) pointerTo(header->containerTableAddress); - containers--; - } - - if (header->eventTableAddress != 0) { - events = (EventEntry *) pointerTo(header->eventTableAddress); - events--; - } - - /* Scores, if already allocated, copy initial data */ - if (scores == NULL) - scores = (Aword *)duplicate((Aword *) pointerTo(header->scores), header->scoreCount*sizeof(Aword)); - else - memcpy(scores, pointerTo(header->scores), header->scoreCount*sizeof(Aword)); - - if (literals == NULL) - literals = (LiteralEntry *)allocate(sizeof(Aword)*(MAXPARAMS+1)); - - stxs = (SyntaxEntry *) pointerTo(header->syntaxTableAddress); - vrbs = (VerbEntry *) pointerTo(header->verbTableAddress); - msgs = (MessageEntry *) pointerTo(header->messageTableAddress); - initRules(header->ruleTableAddress); - - if (header->pack) - freq = (Aword *) pointerTo(header->freq); +static void initStaticData(void) { + /* Dictionary */ + dictionary = (DictionaryEntry *) pointerTo(header->dictionary); + /* Find out number of entries in dictionary */ + for (dictionarySize = 0; !isEndOfArray(&dictionary[dictionarySize]); dictionarySize++); + + /* Scores */ + + + /* All addresses to tables indexed by ids are converted to pointers, + then adjusted to point to the (imaginary) element before the + actual table so that [0] does not exist. Instead indices goes + from 1 and we can use [1]. */ + + if (header->instanceTableAddress == 0) + syserr("Instance table pointer == 0"); + instances = (InstanceEntry *) pointerTo(header->instanceTableAddress); + instances--; /* Back up one so that first is no. 1 */ + + + if (header->classTableAddress == 0) + syserr("Class table pointer == 0"); + classes = (ClassEntry *) pointerTo(header->classTableAddress); + classes--; /* Back up one so that first is no. 1 */ + + if (header->containerTableAddress != 0) { + containers = (ContainerEntry *) pointerTo(header->containerTableAddress); + containers--; + } + + if (header->eventTableAddress != 0) { + events = (EventEntry *) pointerTo(header->eventTableAddress); + events--; + } + + /* Scores, if already allocated, copy initial data */ + if (scores == NULL) + scores = (Aword *)duplicate((Aword *) pointerTo(header->scores), header->scoreCount * sizeof(Aword)); + else + memcpy(scores, pointerTo(header->scores), header->scoreCount * sizeof(Aword)); + + if (literals == NULL) + literals = (LiteralEntry *)allocate(sizeof(Aword) * (MAXPARAMS + 1)); + + stxs = (SyntaxEntry *) pointerTo(header->syntaxTableAddress); + vrbs = (VerbEntry *) pointerTo(header->verbTableAddress); + msgs = (MessageEntry *) pointerTo(header->messageTableAddress); + initRules(header->ruleTableAddress); + + if (header->pack) + freq = (Aword *) pointerTo(header->freq); } /*----------------------------------------------------------------------*/ -static void initStrings(void) -{ - StringInitEntry *init; +static void initStrings(void) { + StringInitEntry *init; - for (init = (StringInitEntry *) pointerTo(header->stringInitTable); !isEndOfArray(init); init++) - setInstanceAttribute(init->instanceCode, init->attributeCode, toAptr(getStringFromFile(init->fpos, init->len))); + for (init = (StringInitEntry *) pointerTo(header->stringInitTable); !isEndOfArray(init); init++) + setInstanceAttribute(init->instanceCode, init->attributeCode, toAptr(getStringFromFile(init->fpos, init->len))); } /*----------------------------------------------------------------------*/ -static Aint sizeOfAttributeData(void) -{ - uint i; - int size = 0; - - for (i=1; i<=header->instanceMax; i++) { - AttributeEntry *attribute = (AttributeEntry *)pointerTo(instances[i].initialAttributes); - while (!isEndOfArray(attribute)) { - size += AwordSizeOf(AttributeEntry); - attribute++; - } - size += 1; /* For EOD */ - } - - if (size != header->attributesAreaSize - && (sizeof(AttributeHeaderEntry) == sizeof(AttributeEntry))) - syserr("Attribute area size calculated wrong."); - return size; +static Aint sizeOfAttributeData(void) { + uint i; + int size = 0; + + for (i = 1; i <= header->instanceMax; i++) { + AttributeEntry *attribute = (AttributeEntry *)pointerTo(instances[i].initialAttributes); + while (!isEndOfArray(attribute)) { + size += AwordSizeOf(AttributeEntry); + attribute++; + } + size += 1; /* For EOD */ + } + + if (size != header->attributesAreaSize + && (sizeof(AttributeHeaderEntry) == sizeof(AttributeEntry))) + syserr("Attribute area size calculated wrong."); + return size; } /*----------------------------------------------------------------------*/ -static AttributeEntry *initializeAttributes(int awordSize) -{ - Aword *attributeArea = (Aword *)allocate(awordSize*sizeof(Aword)); - Aword *currentAttributeArea = attributeArea; - uint i; - - for (i=1; i<=header->instanceMax; i++) { - AttributeHeaderEntry *originalAttribute = (AttributeHeaderEntry *)pointerTo(instances[i].initialAttributes); - admin[i].attributes = (AttributeEntry *)currentAttributeArea; - while (!isEndOfArray(originalAttribute)) { - ((AttributeEntry *)currentAttributeArea)->code = originalAttribute->code; - ((AttributeEntry *)currentAttributeArea)->value = originalAttribute->value; - ((AttributeEntry *)currentAttributeArea)->id = originalAttribute->id; - currentAttributeArea += AwordSizeOf(AttributeEntry); - originalAttribute++; - } - *((Aword*)currentAttributeArea) = EOD; - currentAttributeArea += 1; - } - - return (AttributeEntry *)attributeArea; +static AttributeEntry *initializeAttributes(int awordSize) { + Aword *attributeArea = (Aword *)allocate(awordSize * sizeof(Aword)); + Aword *currentAttributeArea = attributeArea; + uint i; + + for (i = 1; i <= header->instanceMax; i++) { + AttributeHeaderEntry *originalAttribute = (AttributeHeaderEntry *)pointerTo(instances[i].initialAttributes); + admin[i].attributes = (AttributeEntry *)currentAttributeArea; + while (!isEndOfArray(originalAttribute)) { + ((AttributeEntry *)currentAttributeArea)->code = originalAttribute->code; + ((AttributeEntry *)currentAttributeArea)->value = originalAttribute->value; + ((AttributeEntry *)currentAttributeArea)->id = originalAttribute->id; + currentAttributeArea += AwordSizeOf(AttributeEntry); + originalAttribute++; + } + *((Aword *)currentAttributeArea) = EOD; + currentAttributeArea += 1; + } + + return (AttributeEntry *)attributeArea; } /*----------------------------------------------------------------------*/ -static void initDynamicData(void) -{ - uint instanceId; +static void initDynamicData(void) { + uint instanceId; - /* Allocate for administrative table */ - admin = (AdminEntry *)allocate((header->instanceMax+1)*sizeof(AdminEntry)); + /* Allocate for administrative table */ + admin = (AdminEntry *)allocate((header->instanceMax + 1) * sizeof(AdminEntry)); - /* Create game state copy of attributes */ - attributes = initializeAttributes(sizeOfAttributeData()); + /* Create game state copy of attributes */ + attributes = initializeAttributes(sizeOfAttributeData()); - /* Initialise string & set attributes */ - initStrings(); - initSets((SetInitEntry*)pointerTo(header->setInitTable)); + /* Initialise string & set attributes */ + initStrings(); + initSets((SetInitEntry *)pointerTo(header->setInitTable)); - /* Set initial locations */ - for (instanceId = 1; instanceId <= header->instanceMax; instanceId++) - admin[instanceId].location = instances[instanceId].initialLocation; + /* Set initial locations */ + for (instanceId = 1; instanceId <= header->instanceMax; instanceId++) + admin[instanceId].location = instances[instanceId].initialLocation; } /*----------------------------------------------------------------------*/ static void runInheritedInitialize(Aint theClass) { - if (theClass == 0) return; - runInheritedInitialize(classes[theClass].parent); - if (classes[theClass].initialize) - interpret(classes[theClass].initialize); + if (theClass == 0) return; + runInheritedInitialize(classes[theClass].parent); + if (classes[theClass].initialize) + interpret(classes[theClass].initialize); } /*----------------------------------------------------------------------*/ static void runInitialize(Aint theInstance) { - runInheritedInitialize(instances[theInstance].parent); - if (instances[theInstance].initialize != 0) - interpret(instances[theInstance].initialize); + runInheritedInitialize(instances[theInstance].parent); + if (instances[theInstance].initialize != 0) + interpret(instances[theInstance].initialize); } /*----------------------------------------------------------------------*/ static void initializeInstances() { - uint instanceId; + uint instanceId; - /* Set initial locations */ - for (instanceId = 1; instanceId <= header->instanceMax; instanceId++) { - current.instance = instanceId; - runInitialize(instanceId); - } + /* Set initial locations */ + for (instanceId = 1; instanceId <= header->instanceMax; instanceId++) { + current.instance = instanceId; + runInitialize(instanceId); + } } /*----------------------------------------------------------------------*/ -static void start(void) -{ - int startloc; - - current.tick = 0; - current.location = startloc = where(HERO, TRANSITIVE); - current.actor = HERO; - current.score = 0; - - initializeInstances(); - - if (traceSectionOption) - printf("\n<START:>\n"); - interpret(header->start); - para(); - - if (where(HERO, TRANSITIVE) == startloc) { - if (traceSectionOption) - printf("<CURRENT LOCATION:>"); - look(); - } - resetAndEvaluateRules(rules, header->version); +static void start(void) { + int startloc; + + current.tick = 0; + current.location = startloc = where(HERO, TRANSITIVE); + current.actor = HERO; + current.score = 0; + + initializeInstances(); + + if (traceSectionOption) + printf("\n<START:>\n"); + interpret(header->start); + para(); + + if (where(HERO, TRANSITIVE) == startloc) { + if (traceSectionOption) + printf("<CURRENT LOCATION:>"); + look(); + } + resetAndEvaluateRules(rules, header->version); } /*----------------------------------------------------------------------*/ -static void openFiles(void) -{ - /* If logging open log file */ - if (transcriptOption || logOption) { - startTranscript(); - } +static void openFiles(void) { + /* If logging open log file */ + if (transcriptOption || logOption) { + startTranscript(); + } } /*----------------------------------------------------------------------*/ -static void init(void) -{ - int i; - - /* Initialise some status */ - eventQueueTop = 0; /* No pending events */ - initStaticData(); - initDynamicData(); - initParsing(); - checkDebug(); - - getPageSize(); - - /* Find first conjunction and use that for ',' handling */ - for (i = 0; i < dictionarySize; i++) - if (isConjunction(i)) { - conjWord = i; - break; - } - - /* Start the adventure */ - if (debugOption) - debug(FALSE, 0, 0); - else - clear(); - - start(); +static void init(void) { + int i; + + /* Initialise some status */ + eventQueueTop = 0; /* No pending events */ + initStaticData(); + initDynamicData(); + initParsing(); + checkDebug(); + + getPageSize(); + + /* Find first conjunction and use that for ',' handling */ + for (i = 0; i < dictionarySize; i++) + if (isConjunction(i)) { + conjWord = i; + break; + } + + /* Start the adventure */ + if (debugOption) + debug(FALSE, 0, 0); + else + clear(); + + start(); } /*----------------------------------------------------------------------*/ -static bool traceActor(int theActor) -{ - if (traceSectionOption) { - printf("\n<ACTOR "); - traceSay(theActor); - printf("[%d]", theActor); - if (current.location != 0) { - printf(" (at "); - traceSay(current.location); - } else - printf(" (nowhere"); - printf("[%d])", current.location); - } - return traceSectionOption; +static bool traceActor(int theActor) { + if (traceSectionOption) { + printf("\n<ACTOR "); + traceSay(theActor); + printf("[%d]", theActor); + if (current.location != 0) { + printf(" (at "); + traceSay(current.location); + } else + printf(" (nowhere"); + printf("[%d])", current.location); + } + return traceSectionOption; } /*----------------------------------------------------------------------*/ -static char *scriptName(int theActor, int theScript) -{ - ScriptEntry *scriptEntry = (ScriptEntry *)pointerTo(header->scriptTableAddress); - - while (theScript > 1) { - scriptEntry++; - theScript--; - } - return (char *)pointerTo(scriptEntry->id); +static char *scriptName(int theActor, int theScript) { + ScriptEntry *scriptEntry = (ScriptEntry *)pointerTo(header->scriptTableAddress); + + while (theScript > 1) { + scriptEntry++; + theScript--; + } + return (char *)pointerTo(scriptEntry->id); } /*----------------------------------------------------------------------*/ -static void moveActor(int theActor) -{ - ScriptEntry *scr; - StepEntry *step; - Aint previousInstance = current.instance; - - current.actor = theActor; - current.instance = theActor; - current.location = where(theActor, TRANSITIVE); - if (theActor == (int)HERO) { +static void moveActor(int theActor) { + ScriptEntry *scr; + StepEntry *step; + Aint previousInstance = current.instance; + + current.actor = theActor; + current.instance = theActor; + current.location = where(theActor, TRANSITIVE); + if (theActor == (int)HERO) { #ifdef TODO /* Ask him! */ - if (setjmp(forfeitLabel) == 0) { - parse(); - capitalize = TRUE; - fail = FALSE; /* fail only aborts one actor */ - } + if (setjmp(forfeitLabel) == 0) { + parse(); + capitalize = TRUE; + fail = FALSE; /* fail only aborts one actor */ + } #else ::error("TODO: moveActor setjmp"); #endif - } else if (admin[theActor].script != 0) { - for (scr = (ScriptEntry *) pointerTo(header->scriptTableAddress); !isEndOfArray(scr); scr++) { - if (scr->code == admin[theActor].script) { - /* Find correct step in the list by indexing */ - step = (StepEntry *) pointerTo(scr->steps); - step = (StepEntry *) &step[admin[theActor].step]; - /* Now execute it, maybe. First check wait count */ - if (admin[theActor].waitCount > 0) { /* Wait some more ? */ - if (traceActor(theActor)) - printf(", SCRIPT %s[%ld], STEP %ld, Waiting another %ld turns>\n", - scriptName(theActor, admin[theActor].script), - (long)admin[theActor].script, (long)admin[theActor].step+1, - (long)admin[theActor].waitCount); - admin[theActor].waitCount--; - break; - } - /* Then check possible expression to wait for */ - if (step->exp != 0) { - if (traceActor(theActor)) - printf(", SCRIPT %s[%ld], STEP %ld, Evaluating:>\n", - scriptName(theActor, admin[theActor].script), - (long)admin[theActor].script, (long)admin[theActor].step+1); - if (!evaluate(step->exp)) - break; /* Break loop, don't execute step*/ - } - /* OK, so finally let him do his thing */ - admin[theActor].step++; /* Increment step number before executing... */ - if (!isEndOfArray(step+1) && (step+1)->after != 0) { - admin[theActor].waitCount = evaluate((step+1)->after); - } - if (traceActor(theActor)) - printf(", SCRIPT %s[%ld], STEP %ld, Executing:>\n", - scriptName(theActor, admin[theActor].script), - (long)admin[theActor].script, - (long)admin[theActor].step); - interpret(step->stms); - step++; - /* ... so that we can see if he failed or is USEing another script now */ - if (fail || (admin[theActor].step != 0 && isEndOfArray(step))) - /* No more steps in this script, so stop him */ - admin[theActor].script = 0; - fail = FALSE; /* fail only aborts one actor */ - break; /* We have executed a script so leave loop */ - } - } - if (isEndOfArray(scr)) - syserr("Unknown actor script."); - } else { - if (traceActor(theActor)) { - printf(", Idle>\n"); - } - } - - current.instance = previousInstance; + } else if (admin[theActor].script != 0) { + for (scr = (ScriptEntry *) pointerTo(header->scriptTableAddress); !isEndOfArray(scr); scr++) { + if (scr->code == admin[theActor].script) { + /* Find correct step in the list by indexing */ + step = (StepEntry *) pointerTo(scr->steps); + step = (StepEntry *) &step[admin[theActor].step]; + /* Now execute it, maybe. First check wait count */ + if (admin[theActor].waitCount > 0) { /* Wait some more ? */ + if (traceActor(theActor)) + printf(", SCRIPT %s[%ld], STEP %ld, Waiting another %ld turns>\n", + scriptName(theActor, admin[theActor].script), + (long)admin[theActor].script, (long)admin[theActor].step + 1, + (long)admin[theActor].waitCount); + admin[theActor].waitCount--; + break; + } + /* Then check possible expression to wait for */ + if (step->exp != 0) { + if (traceActor(theActor)) + printf(", SCRIPT %s[%ld], STEP %ld, Evaluating:>\n", + scriptName(theActor, admin[theActor].script), + (long)admin[theActor].script, (long)admin[theActor].step + 1); + if (!evaluate(step->exp)) + break; /* Break loop, don't execute step*/ + } + /* OK, so finally let him do his thing */ + admin[theActor].step++; /* Increment step number before executing... */ + if (!isEndOfArray(step + 1) && (step + 1)->after != 0) { + admin[theActor].waitCount = evaluate((step + 1)->after); + } + if (traceActor(theActor)) + printf(", SCRIPT %s[%ld], STEP %ld, Executing:>\n", + scriptName(theActor, admin[theActor].script), + (long)admin[theActor].script, + (long)admin[theActor].step); + interpret(step->stms); + step++; + /* ... so that we can see if he failed or is USEing another script now */ + if (fail || (admin[theActor].step != 0 && isEndOfArray(step))) + /* No more steps in this script, so stop him */ + admin[theActor].script = 0; + fail = FALSE; /* fail only aborts one actor */ + break; /* We have executed a script so leave loop */ + } + } + if (isEndOfArray(scr)) + syserr("Unknown actor script."); + } else { + if (traceActor(theActor)) { + printf(", Idle>\n"); + } + } + + current.instance = previousInstance; } #define RESTARTED (setjmp(restartLabel) != NO_JUMP_RETURN) @@ -747,74 +736,74 @@ static void moveActor(int theActor) /*======================================================================*/ void run(void) { - static Stack theStack = NULL; /* Needs to survive longjmp() */ + static Stack theStack = NULL; /* Needs to survive longjmp() */ - openFiles(); - load(); /* Load program */ + openFiles(); + load(); /* Load program */ #ifdef TODO - if (RESTARTED) { - deleteStack(theStack); - } - - theStack = createStack(STACKSIZE); - setInterpreterStack(theStack); - - initStateStack(); - - if (!ERROR_RETURNED) /* Can happen in start section too... */ - init(); /* Initialise and start the adventure */ - - while (TRUE) { - if (debugOption) - debug(FALSE, 0, 0); - - if (stackDepth(theStack) != 0) - syserr("Stack is not empty in main loop"); - - if (!current.meta) - runPendingEvents(); - - /* Return here if error during execution */ - switch (setjmp(returnLabel)) { - case NO_JUMP_RETURN: - break; - case ERROR_RETURN: - forgetGameState(); - forceNewPlayerInput(); - break; - case UNDO_RETURN: - forceNewPlayerInput(); - break; - default: - syserr("Unexpected longjmp() return value"); - } - - recursionDepth = 0; - - /* Move all characters, hero first */ - rememberGameState(); - current.meta = FALSE; - moveActor(header->theHero); - - if (gameStateChanged) - rememberCommands(); - else - forgetGameState(); - - if (!current.meta) { - current.tick++; - - /* Remove this call? Since Eval is done up there after each event... */ - resetAndEvaluateRules(rules, header->version); - - /* Then all the other actors... */ - for (uint i = 1; i <= header->instanceMax; i++) - if (i != header->theHero && isAActor(i)) { - moveActor(i); - resetAndEvaluateRules(rules, header->version); - } - } - } + if (RESTARTED) { + deleteStack(theStack); + } + + theStack = createStack(STACKSIZE); + setInterpreterStack(theStack); + + initStateStack(); + + if (!ERROR_RETURNED) /* Can happen in start section too... */ + init(); /* Initialise and start the adventure */ + + while (TRUE) { + if (debugOption) + debug(FALSE, 0, 0); + + if (stackDepth(theStack) != 0) + syserr("Stack is not empty in main loop"); + + if (!current.meta) + runPendingEvents(); + + /* Return here if error during execution */ + switch (setjmp(returnLabel)) { + case NO_JUMP_RETURN: + break; + case ERROR_RETURN: + forgetGameState(); + forceNewPlayerInput(); + break; + case UNDO_RETURN: + forceNewPlayerInput(); + break; + default: + syserr("Unexpected longjmp() return value"); + } + + recursionDepth = 0; + + /* Move all characters, hero first */ + rememberGameState(); + current.meta = FALSE; + moveActor(header->theHero); + + if (gameStateChanged) + rememberCommands(); + else + forgetGameState(); + + if (!current.meta) { + current.tick++; + + /* Remove this call? Since Eval is done up there after each event... */ + resetAndEvaluateRules(rules, header->version); + + /* Then all the other actors... */ + for (uint i = 1; i <= header->instanceMax; i++) + if (i != header->theHero && isAActor(i)) { + moveActor(i); + resetAndEvaluateRules(rules, header->version); + } + } + } #endif } diff --git a/engines/glk/alan3/main.h b/engines/glk/alan3/main.h index 127cc111ae..60e3d0527d 100644 --- a/engines/glk/alan3/main.h +++ b/engines/glk/alan3/main.h @@ -31,7 +31,7 @@ namespace Glk { namespace Alan3 { -extern VerbEntry *vrbs; // Verb table pointer +extern VerbEntry *vrbs; // Verb table pointer extern void run(); extern void usage(); diff --git a/engines/glk/alan3/memory.cpp b/engines/glk/alan3/memory.cpp index 600e9b6c9a..030e8cfb83 100644 --- a/engines/glk/alan3/memory.cpp +++ b/engines/glk/alan3/memory.cpp @@ -36,36 +36,34 @@ int memTop = 0; /* Top of load memory */ /*======================================================================*/ -void *allocate(unsigned long lengthInBytes) -{ - void *p = (void *)calloc((size_t)lengthInBytes, 1); +void *allocate(unsigned long lengthInBytes) { + void *p = (void *)calloc((size_t)lengthInBytes, 1); - if (p == NULL) - syserr("Out of memory."); + if (p == NULL) + syserr("Out of memory."); - return p; + return p; } /*======================================================================*/ void deallocate(void *ptr) { - free(ptr); + free(ptr); } /*======================================================================*/ -void *duplicate(void *original, unsigned long len) -{ - void *p = allocate(len+1); +void *duplicate(void *original, unsigned long len) { + void *p = allocate(len + 1); - memcpy(p, original, len); - return p; + memcpy(p, original, len); + return p; } typedef struct { - Aptr aptr; - void *voidp; + Aptr aptr; + void *voidp; } PointerMapEntry; static PointerMapEntry *pointerMap = NULL; @@ -74,44 +72,44 @@ static int nextAptr = 1; /*======================================================================*/ void resetPointerMap(void) { - if (pointerMap != NULL) free(pointerMap); - pointerMap = NULL; - pointerMapSize = 0; + if (pointerMap != NULL) free(pointerMap); + pointerMap = NULL; + pointerMapSize = 0; } /*======================================================================*/ void *fromAptr(Aptr aptr) { - int index; + int index; - for (index=0; index < pointerMapSize && pointerMap[index].aptr != aptr; index++) - ; + for (index = 0; index < pointerMapSize && pointerMap[index].aptr != aptr; index++) + ; - if (index == pointerMapSize) - syserr("No pointerMap entry for Aptr"); + if (index == pointerMapSize) + syserr("No pointerMap entry for Aptr"); - return pointerMap[index].voidp; + return pointerMap[index].voidp; } /*======================================================================*/ Aptr toAptr(void *ptr) { - int index; - - if (pointerMap == NULL) { - pointerMap = (PointerMapEntry *)allocate(sizeof(PointerMapEntry)); - pointerMapSize = 1; - } - - for (index=0; index < pointerMapSize && pointerMap[index].voidp != NULL; index++) - ; - if (index == pointerMapSize) { - pointerMap = (PointerMapEntry *)realloc(pointerMap, (index+1)*sizeof(PointerMapEntry)); - pointerMapSize++; - } - - pointerMap[index].voidp = ptr; - pointerMap[index].aptr = nextAptr++; - return pointerMap[index].aptr; + int index; + + if (pointerMap == NULL) { + pointerMap = (PointerMapEntry *)allocate(sizeof(PointerMapEntry)); + pointerMapSize = 1; + } + + for (index = 0; index < pointerMapSize && pointerMap[index].voidp != NULL; index++) + ; + if (index == pointerMapSize) { + pointerMap = (PointerMapEntry *)realloc(pointerMap, (index + 1) * sizeof(PointerMapEntry)); + pointerMapSize++; + } + + pointerMap[index].voidp = ptr; + pointerMap[index].aptr = nextAptr++; + return pointerMap[index].aptr; } } // End of namespace Alan3 diff --git a/engines/glk/alan3/msg.cpp b/engines/glk/alan3/msg.cpp index d2d4956b85..f748f26820 100644 --- a/engines/glk/alan3/msg.cpp +++ b/engines/glk/alan3/msg.cpp @@ -33,74 +33,69 @@ namespace Alan3 { MessageEntry *msgs; /* Message table pointer */ /*======================================================================*/ -void printMessage(MsgKind msg) /* IN - message number */ -{ - interpret(msgs[msg].stms); +void printMessage(MsgKind msg) { /* IN - message number */ + interpret(msgs[msg].stms); } static void (*errorHandler)(MsgKind msg) = NULL; /*======================================================================*/ -void setErrorHandler(void (*handler)(MsgKind msg)) /* IN - The error message number */ -{ - // N.B. The error handler must not return because the standard handler does not... - errorHandler = handler; +void setErrorHandler(void (*handler)(MsgKind msg)) { /* IN - The error message number */ + // N.B. The error handler must not return because the standard handler does not... + errorHandler = handler; } /*======================================================================*/ -void error(MsgKind msgno) /* IN - The error message number */ -{ - if (errorHandler != NULL) - errorHandler(msgno); - else { - /* Print an error message and longjmp to main loop. */ - if (msgno != NO_MSG) - printMessage(msgno); - //longjmp(returnLabel, ERROR_RETURN); - } +void error(MsgKind msgno) { /* IN - The error message number */ + if (errorHandler != NULL) + errorHandler(msgno); + else { + /* Print an error message and longjmp to main loop. */ + if (msgno != NO_MSG) + printMessage(msgno); + //longjmp(returnLabel, ERROR_RETURN); + } } /*======================================================================*/ -void abortPlayerCommand(void) -{ - error(NO_MSG); +void abortPlayerCommand(void) { + error(NO_MSG); } /*======================================================================*/ void printMessageWithInstanceParameter(MsgKind message, int instanceId) { - ParameterArray parameters = newParameterArray(); - addParameterForInstance(parameters, instanceId); - printMessageWithParameters(message, parameters); - freeParameterArray(parameters); + ParameterArray parameters = newParameterArray(); + addParameterForInstance(parameters, instanceId); + printMessageWithParameters(message, parameters); + freeParameterArray(parameters); } /*======================================================================*/ void printMessageUsing2InstanceParameters(MsgKind message, int instance1, int instance2) { - ParameterArray parameters = newParameterArray(); - addParameterForInstance(parameters, instance1); - addParameterForInstance(parameters, instance2); - printMessageWithParameters(message, parameters); - freeParameterArray(parameters); + ParameterArray parameters = newParameterArray(); + addParameterForInstance(parameters, instance1); + addParameterForInstance(parameters, instance2); + printMessageWithParameters(message, parameters); + freeParameterArray(parameters); } /*======================================================================*/ -void printMessageWithParameters(MsgKind msg, Parameter *messageParameters) -{ - Parameter *savedParameters = newParameterArray(); +void printMessageWithParameters(MsgKind msg, Parameter *messageParameters) { + Parameter *savedParameters = newParameterArray(); - copyParameterArray(savedParameters, globalParameters); - copyParameterArray(globalParameters, messageParameters); + copyParameterArray(savedParameters, globalParameters); + copyParameterArray(globalParameters, messageParameters); - interpret(msgs[msg].stms); + interpret(msgs[msg].stms); - copyParameterArray(globalParameters, savedParameters); - freeParameterArray(savedParameters); + copyParameterArray(globalParameters, savedParameters); + freeParameterArray(savedParameters); } } // End of namespace Alan3 diff --git a/engines/glk/alan3/msg.h b/engines/glk/alan3/msg.h index 9f445329ca..5b2f8d6912 100644 --- a/engines/glk/alan3/msg.h +++ b/engines/glk/alan3/msg.h @@ -32,7 +32,7 @@ namespace Alan3 { /* TYPES */ typedef struct MessageEntry { /* MESSAGE TABLE */ - Aaddr stms; /* Address to statements*/ + Aaddr stms; /* Address to statements*/ } MessageEntry; diff --git a/engines/glk/alan3/output.cpp b/engines/glk/alan3/output.cpp index 16f1c1e65b..97a3a35574 100644 --- a/engines/glk/alan3/output.cpp +++ b/engines/glk/alan3/output.cpp @@ -58,17 +58,17 @@ strid_t logFile; /*----------------------------------------------------------------------*/ static int updateColumn(int currentColumn, const char *string) { - const char *newlinePosition = strrchr(string, '\n'); - if (newlinePosition != NULL) - return &string[strlen(string)] - newlinePosition; - else - return currentColumn + strlen(string); + const char *newlinePosition = strrchr(string, '\n'); + if (newlinePosition != NULL) + return &string[strlen(string)] - newlinePosition; + else + return currentColumn + strlen(string); } /*======================================================================*/ void setSubHeaderStyle(void) { - g_vm->glk_set_style(style_Subheader); + g_vm->glk_set_style(style_Subheader); } @@ -79,23 +79,22 @@ void setNormalStyle(void) { /*======================================================================*/ void newline(void) { - printAndLog("\n"); - col = 1; - needSpace = FALSE; + printAndLog("\n"); + col = 1; + needSpace = FALSE; } /*======================================================================*/ -void para(void) -{ - /* Make a new paragraph, i.e one empty line (one or two newlines). */ - if (g_vm->glk_gestalt(gestalt_Graphics, 0) == 1) +void para(void) { + /* Make a new paragraph, i.e one empty line (one or two newlines). */ + if (g_vm->glk_gestalt(gestalt_Graphics, 0) == 1) g_vm->glk_window_flow_break(glkMainWin); - if (col != 1) - newline(); - newline(); - capitalize = TRUE; + if (col != 1) + newline(); + newline(); + capitalize = TRUE; } @@ -107,47 +106,47 @@ void clear(void) { /*----------------------------------------------------------------------*/ static void capitalizeFirst(Common::String &str) { - uint i = 0; + uint i = 0; - // Skip over space... - while (i < str.size() && isSpace(str[i])) i++; + // Skip over space... + while (i < str.size() && isSpace(str[i])) i++; if (i < str.size()) { str.setChar(toUpper(str[i]), i); - capitalize = false; - } + capitalize = false; + } } /*======================================================================*/ void printAndLog(const char *string) { - static int column = 0; - char *stringCopy; - char *stringPart; - - printf("%s", string); - if (!onStatusLine && transcriptOption) { - // TODO Is this assuming only 70-char wide windows for GLK? - if ((int)strlen(string) > 70-column) { - stringCopy = strdup(string); /* Make sure we can write NULLs */ - stringPart = stringCopy; - while ((int)strlen(stringPart) > 70-column) { - int p; - for (p = 70-column; p>0 && !isspace((int)stringPart[p]); p--); - stringPart[p] = '\0'; + static int column = 0; + char *stringCopy; + char *stringPart; + + printf("%s", string); + if (!onStatusLine && transcriptOption) { + // TODO Is this assuming only 70-char wide windows for GLK? + if ((int)strlen(string) > 70 - column) { + stringCopy = strdup(string); /* Make sure we can write NULLs */ + stringPart = stringCopy; + while ((int)strlen(stringPart) > 70 - column) { + int p; + for (p = 70 - column; p > 0 && !isspace((int)stringPart[p]); p--); + stringPart[p] = '\0'; g_vm->glk_put_string_stream(logFile, stringPart); g_vm->glk_put_char_stream(logFile, '\n'); - column = 0; - stringPart = &stringPart[p+1]; - } + column = 0; + stringPart = &stringPart[p + 1]; + } g_vm->glk_put_string_stream(logFile, stringPart); - column = updateColumn(column, stringPart); - free(stringCopy); - } else { + column = updateColumn(column, stringPart); + free(stringCopy); + } else { g_vm->glk_put_string_stream(logFile, string); - column = updateColumn(column, string); - } - } + column = updateColumn(column, string); + } + } } @@ -155,62 +154,60 @@ void printAndLog(const char *string) { static void justify(const char *str) { Common::String tempStr(str); - if (capitalize) - capitalizeFirst(tempStr); + if (capitalize) + capitalizeFirst(tempStr); - printAndLog(tempStr.c_str()); - col = col + tempStr.size(); // Update column + printAndLog(tempStr.c_str()); + col = col + tempStr.size(); // Update column } /*----------------------------------------------------------------------*/ -static void space(void) -{ - if (skipSpace) - skipSpace = FALSE; - else { - if (needSpace) { - printAndLog(" "); - col++; - } - } - needSpace = FALSE; +static void space(void) { + if (skipSpace) + skipSpace = FALSE; + else { + if (needSpace) { + printAndLog(" "); + col++; + } + } + needSpace = FALSE; } /*----------------------------------------------------------------------*/ static void sayPlayerWordsForParameter(int p) { - int i; + int i; - for (i = globalParameters[p].firstWord; i <= globalParameters[p].lastWord; i++) { - justify((char *)pointerTo(dictionary[playerWords[i].code].string)); - if (i < globalParameters[p].lastWord) - justify(" "); - } + for (i = globalParameters[p].firstWord; i <= globalParameters[p].lastWord; i++) { + justify((char *)pointerTo(dictionary[playerWords[i].code].string)); + if (i < globalParameters[p].lastWord) + justify(" "); + } } /*----------------------------------------------------------------------*/ -static void sayParameter(int p, int form) -{ - int i; +static void sayParameter(int p, int form) { + int i; - for (i = 0; i <= p; i++) - if (isEndOfArray(&globalParameters[i])) - apperr("Nonexistent parameter referenced."); + for (i = 0; i <= p; i++) + if (isEndOfArray(&globalParameters[i])) + apperr("Nonexistent parameter referenced."); #ifdef ALWAYS_SAY_PARAMETERS_USING_PLAYER_WORDS - if (params[p].firstWord != EOD) /* Any words he used? */ - /* Yes, so use them... */ - sayPlayerWordsForParameter(p); - else - sayForm(params[p].code, form); + if (params[p].firstWord != EOD) /* Any words he used? */ + /* Yes, so use them... */ + sayPlayerWordsForParameter(p); + else + sayForm(params[p].code, form); #else - if (globalParameters[p].useWords) { - /* Ambiguous instance referenced, so use the words he used */ - sayPlayerWordsForParameter(p); - } else - sayForm(globalParameters[p].instance, (SayForm)form); + if (globalParameters[p].useWords) { + /* Ambiguous instance referenced, so use the words he used */ + sayPlayerWordsForParameter(p); + } else + sayForm(globalParameters[p].instance, (SayForm)form); #endif } @@ -234,200 +231,207 @@ static void sayParameter(int p, int form) $ = no space needed after this, and don't capitalize _ = interpret this as a single dollar, if in doubt or conflict with other symbols */ -static char *printSymbol(char str[]) /* IN - The string starting with '$' */ -{ - int advance = 2; - - if (*str == '\0') printAndLog("$"); - else switch (toLower(str[1])) { - case 'n': - newline(); - needSpace = FALSE; - break; - case 'i': - newline(); - printAndLog(" "); - col = 5; - needSpace = FALSE; - break; - case 'o': - space(); - sayParameter(0, 0); - needSpace = TRUE; /* We did print something non-white */ - break; - case '+': - case '0': - case '-': - case '!': - space(); - if (isdigit((int)str[2])) { - int form; - switch (str[1]) { - case '+': form = SAY_DEFINITE; break; - case '0': form = SAY_INDEFINITE; break; - case '-': form = SAY_NEGATIVE; break; - case '!': form = SAY_PRONOUN; break; - default: form = SAY_SIMPLE; break; - } - sayParameter(str[2]-'1', form); - needSpace = TRUE; - } - advance = 3; - break; - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - space(); - sayParameter(str[1]-'1', SAY_SIMPLE); - needSpace = TRUE; /* We did print something non-white */ - break; - case 'l': - space(); - say(current.location); - needSpace = TRUE; /* We did print something non-white */ - break; - case 'a': - space(); - say(current.actor); - needSpace = TRUE; /* We did print something non-white */ - break; - case 'v': - space(); - justify((const char *)pointerTo(dictionary[verbWord].string)); - needSpace = TRUE; /* We did print something non-white */ - break; - case 'p': - para(); - needSpace = FALSE; - break; - case 't': { - int i; - int spaces = 4-(col-1)%4; - - for (i = 0; i<spaces; i++) printAndLog(" "); - col = col + spaces; - needSpace = FALSE; - break; - } - case '$': - skipSpace = TRUE; - capitalize = FALSE; - break; - case '_': - advance = 2; - printAndLog("$"); - break; - default: - advance = 1; - printAndLog("$"); - break; - } - - return &str[advance]; +static char *printSymbol(char str[]) { /* IN - The string starting with '$' */ + int advance = 2; + + if (*str == '\0') printAndLog("$"); + else switch (toLower(str[1])) { + case 'n': + newline(); + needSpace = FALSE; + break; + case 'i': + newline(); + printAndLog(" "); + col = 5; + needSpace = FALSE; + break; + case 'o': + space(); + sayParameter(0, 0); + needSpace = TRUE; /* We did print something non-white */ + break; + case '+': + case '0': + case '-': + case '!': + space(); + if (isdigit((int)str[2])) { + int form; + switch (str[1]) { + case '+': + form = SAY_DEFINITE; + break; + case '0': + form = SAY_INDEFINITE; + break; + case '-': + form = SAY_NEGATIVE; + break; + case '!': + form = SAY_PRONOUN; + break; + default: + form = SAY_SIMPLE; + break; + } + sayParameter(str[2] - '1', form); + needSpace = TRUE; + } + advance = 3; + break; + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + space(); + sayParameter(str[1] - '1', SAY_SIMPLE); + needSpace = TRUE; /* We did print something non-white */ + break; + case 'l': + space(); + say(current.location); + needSpace = TRUE; /* We did print something non-white */ + break; + case 'a': + space(); + say(current.actor); + needSpace = TRUE; /* We did print something non-white */ + break; + case 'v': + space(); + justify((const char *)pointerTo(dictionary[verbWord].string)); + needSpace = TRUE; /* We did print something non-white */ + break; + case 'p': + para(); + needSpace = FALSE; + break; + case 't': { + int i; + int spaces = 4 - (col - 1) % 4; + + for (i = 0; i < spaces; i++) printAndLog(" "); + col = col + spaces; + needSpace = FALSE; + break; + } + case '$': + skipSpace = TRUE; + capitalize = FALSE; + break; + case '_': + advance = 2; + printAndLog("$"); + break; + default: + advance = 1; + printAndLog("$"); + break; + } + + return &str[advance]; } /*----------------------------------------------------------------------*/ static bool inhibitSpace(char *str) { - return str[0] != '\0' && str[0] == '$' && str[1] == '$'; + return str[0] != '\0' && str[0] == '$' && str[1] == '$'; } /*----------------------------------------------------------------------*/ static bool isSpaceEquivalent(char str[]) { - if (str[0] == ' ') - return TRUE; - else - return strncmp(str, "$p", 2) == 0 - || strncmp(str, "$n", 2) == 0 - || strncmp(str, "$i", 2) == 0 - || strncmp(str, "$t", 2) == 0; + if (str[0] == ' ') + return TRUE; + else + return strncmp(str, "$p", 2) == 0 + || strncmp(str, "$n", 2) == 0 + || strncmp(str, "$i", 2) == 0 + || strncmp(str, "$t", 2) == 0; } /*----------------------------------------------------------------------*/ static bool punctuationNext(char *str) { - const char *punctuation = strchr(".,!?", str[0]); - bool end = str[1] == '\0'; - bool space = isSpaceEquivalent(&str[1]); - return (punctuation != NULL && (end || space)); + const char *punctuation = strchr(".,!?", str[0]); + bool end = str[1] == '\0'; + bool space = isSpaceEquivalent(&str[1]); + return (punctuation != NULL && (end || space)); } /*----------------------------------------------------------------------*/ static char lastCharOf(char *str) { - return str[strlen(str)-1]; + return str[strlen(str) - 1]; } /*======================================================================*/ -void output(const char *original) -{ - char ch; - char *str, *copy; - char *symptr; - - copy = strdup(original); - str = copy; - - if (inhibitSpace(str) || punctuationNext(str)) - needSpace = FALSE; - else - space(); /* Output space if needed (& not inhibited) */ - - /* Output string up to symbol and handle the symbol */ - while ((symptr = strchr(str, '$')) != (char *) NULL) { - ch = *symptr; /* Terminate before symbol */ - *symptr = '\0'; - if (strlen(str) > 0) { - skipSpace = FALSE; /* Only let skipSpace through if it is +void output(const char *original) { + char ch; + char *str, *copy; + char *symptr; + + copy = strdup(original); + str = copy; + + if (inhibitSpace(str) || punctuationNext(str)) + needSpace = FALSE; + else + space(); /* Output space if needed (& not inhibited) */ + + /* Output string up to symbol and handle the symbol */ + while ((symptr = strchr(str, '$')) != (char *) NULL) { + ch = *symptr; /* Terminate before symbol */ + *symptr = '\0'; + if (strlen(str) > 0) { + skipSpace = FALSE; /* Only let skipSpace through if it is last in the string */ - if (lastCharOf(str) == ' ') { - str[strlen(str)-1] = '\0'; /* Truncate space character */ - justify(str); /* Output part before '$' */ - needSpace = TRUE; - } else { - justify(str); /* Output part before '$' */ - needSpace = FALSE; - } - } - *symptr = ch; /* restore '$' */ - str = printSymbol(symptr); /* Print the symbolic reference and advance */ - } - - if (str[0] != 0) { - justify(str); /* Output trailing part */ - skipSpace = FALSE; - if (lastCharOf(str) != ' ') - needSpace = TRUE; - } - if (needSpace) - capitalize = strchr("!?.", str[strlen(str)-1]) != 0; - anyOutput = TRUE; - free(copy); + if (lastCharOf(str) == ' ') { + str[strlen(str) - 1] = '\0'; /* Truncate space character */ + justify(str); /* Output part before '$' */ + needSpace = TRUE; + } else { + justify(str); /* Output part before '$' */ + needSpace = FALSE; + } + } + *symptr = ch; /* restore '$' */ + str = printSymbol(symptr); /* Print the symbolic reference and advance */ + } + + if (str[0] != 0) { + justify(str); /* Output trailing part */ + skipSpace = FALSE; + if (lastCharOf(str) != ' ') + needSpace = TRUE; + } + if (needSpace) + capitalize = strchr("!?.", str[strlen(str) - 1]) != 0; + anyOutput = TRUE; + free(copy); } /*======================================================================*/ -bool confirm(MsgKind msgno) -{ - char buf[80]; +bool confirm(MsgKind msgno) { + char buf[80]; - /* This is a bit of a hack since we really want to compare the input, - it could be affirmative, but for now any input is NOT! */ - printMessage(msgno); + /* This is a bit of a hack since we really want to compare the input, + it could be affirmative, but for now any input is NOT! */ + printMessage(msgno); - if (!readline(buf)) + if (!readline(buf)) return TRUE; - col = 1; + col = 1; - return (buf[0] == '\0'); + return (buf[0] == '\0'); } } // End of namespace Alan3 diff --git a/engines/glk/alan3/parameter_position.cpp b/engines/glk/alan3/parameter_position.cpp index 185b6e633b..1c2aaed548 100644 --- a/engines/glk/alan3/parameter_position.cpp +++ b/engines/glk/alan3/parameter_position.cpp @@ -29,74 +29,74 @@ namespace Alan3 { /*======================================================================*/ void deallocateParameterPositions(ParameterPosition *parameterPositions) { - int i; - for (i = 0; !parameterPositions[i].endOfList; i++) { - ParameterPosition *position = ¶meterPositions[i]; - freeParameterArray(position->parameters); - if (position->exceptions) - freeParameterArray(position->exceptions); - } - deallocate(parameterPositions); + int i; + for (i = 0; !parameterPositions[i].endOfList; i++) { + ParameterPosition *position = ¶meterPositions[i]; + freeParameterArray(position->parameters); + if (position->exceptions) + freeParameterArray(position->exceptions); + } + deallocate(parameterPositions); } /*======================================================================*/ void uncheckAllParameterPositions(ParameterPosition parameterPositions[]) { - int position; - for (position = 0; position < MAXPARAMS; position++) { - parameterPositions[position].checked = FALSE; - } + int position; + for (position = 0; position < MAXPARAMS; position++) { + parameterPositions[position].checked = FALSE; + } } /*======================================================================*/ void copyParameterPositions(ParameterPosition originalParameterPositions[], ParameterPosition parameterPositions[]) { - int i; - for (i = 0; !originalParameterPositions[i].endOfList; i++) - parameterPositions[i] = originalParameterPositions[i]; - parameterPositions[i].endOfList = TRUE; + int i; + for (i = 0; !originalParameterPositions[i].endOfList; i++) + parameterPositions[i] = originalParameterPositions[i]; + parameterPositions[i].endOfList = TRUE; } /*======================================================================*/ bool equalParameterPositions(ParameterPosition parameterPositions1[], ParameterPosition parameterPositions2[]) { - int i; - for (i = 0; !parameterPositions1[i].endOfList; i++) { - if (parameterPositions2[i].endOfList) - return FALSE; - if (!equalParameterArrays(parameterPositions1[i].parameters, parameterPositions2[i].parameters)) - return FALSE; - } - return parameterPositions2[i].endOfList; + int i; + for (i = 0; !parameterPositions1[i].endOfList; i++) { + if (parameterPositions2[i].endOfList) + return FALSE; + if (!equalParameterArrays(parameterPositions1[i].parameters, parameterPositions2[i].parameters)) + return FALSE; + } + return parameterPositions2[i].endOfList; } /*======================================================================*/ int findMultipleParameterPosition(ParameterPosition parameterPositions[]) { - Aint parameterNumber; - for (parameterNumber = 0; !parameterPositions[parameterNumber].endOfList; parameterNumber++) - if (parameterPositions[parameterNumber].explicitMultiple) - return parameterNumber; - return -1; + Aint parameterNumber; + for (parameterNumber = 0; !parameterPositions[parameterNumber].endOfList; parameterNumber++) + if (parameterPositions[parameterNumber].explicitMultiple) + return parameterNumber; + return -1; } /*======================================================================*/ void markExplicitMultiple(ParameterPosition parameterPositions[], Parameter parameters[]) { - int parameterCount; - for (parameterCount = 0; !parameterPositions[parameterCount].endOfList; parameterCount++) - if (parameterPositions[parameterCount].explicitMultiple) - parameters[parameterCount].instance = 0; + int parameterCount; + for (parameterCount = 0; !parameterPositions[parameterCount].endOfList; parameterCount++) + if (parameterPositions[parameterCount].explicitMultiple) + parameters[parameterCount].instance = 0; } /*======================================================================*/ void convertPositionsToParameters(ParameterPosition parameterPositions[], Parameter parameters[]) { - ParameterPosition *position = parameterPositions; + ParameterPosition *position = parameterPositions; - clearParameterArray(parameters); - while (!position->endOfList) { - addParameterToParameterArray(parameters, &position->parameters[0]); - position++; - } + clearParameterArray(parameters); + while (!position->endOfList) { + addParameterToParameterArray(parameters, &position->parameters[0]); + position++; + } } } // End of namespace Alan3 diff --git a/engines/glk/alan3/parameter_position.h b/engines/glk/alan3/parameter_position.h index a2072011a0..e5f12b5893 100644 --- a/engines/glk/alan3/parameter_position.h +++ b/engines/glk/alan3/parameter_position.h @@ -52,14 +52,14 @@ namespace Alan3 { /* Types: */ struct ParameterPosition { - bool endOfList; - bool explicitMultiple; - bool all; - bool them; - bool checked; - Aword flags; - Parameter *parameters; - Parameter *exceptions; + bool endOfList; + bool explicitMultiple; + bool all; + bool them; + bool checked; + Aword flags; + Parameter *parameters; + Parameter *exceptions; }; /* Functions: */ diff --git a/engines/glk/alan3/params.cpp b/engines/glk/alan3/params.cpp index d1d97a7ba7..6fe9ae771d 100644 --- a/engines/glk/alan3/params.cpp +++ b/engines/glk/alan3/params.cpp @@ -35,87 +35,87 @@ Parameter *globalParameters = NULL; /*======================================================================*/ Parameter *newParameter(int id) { - Parameter *parameter = NEW(Parameter); - parameter->instance = id; - parameter->candidates = NULL; + Parameter *parameter = NEW(Parameter); + parameter->instance = id; + parameter->candidates = NULL; - return parameter; + return parameter; } /*======================================================================*/ Parameter *newParameterArray(void) { - Parameter *newArray = (Parameter *)allocate((MAXINSTANCE+1)*sizeof(Parameter)); - setEndOfArray(newArray); - return newArray; + Parameter *newArray = (Parameter *)allocate((MAXINSTANCE + 1) * sizeof(Parameter)); + setEndOfArray(newArray); + return newArray; } /*======================================================================*/ void freeParameterArray(ParameterArray arrayPointer) { - Parameter *p; + Parameter *p; - for (p = arrayPointer; !isEndOfArray(p); p++) - if (p->candidates != NULL) - freeParameterArray(p->candidates); - deallocate(arrayPointer); + for (p = arrayPointer; !isEndOfArray(p); p++) + if (p->candidates != NULL) + freeParameterArray(p->candidates); + deallocate(arrayPointer); } /*======================================================================*/ Parameter *ensureParameterArrayAllocated(ParameterArray currentArray) { - if (currentArray == NULL) - return newParameterArray(); - else { - clearParameterArray(currentArray); - return currentArray; - } + if (currentArray == NULL) + return newParameterArray(); + else { + clearParameterArray(currentArray); + return currentArray; + } } /*======================================================================*/ bool parameterArrayIsEmpty(ParameterArray array) { - return array == NULL || lengthOfParameterArray(array) == 0; + return array == NULL || lengthOfParameterArray(array) == 0; } /*======================================================================*/ void clearParameter(Parameter *parameter) { - Parameter *candidates = parameter->candidates; - memset(parameter, 0, sizeof(Parameter)); - parameter->candidates = candidates; - if (parameter->candidates != NULL) - clearParameterArray(parameter->candidates); + Parameter *candidates = parameter->candidates; + memset(parameter, 0, sizeof(Parameter)); + parameter->candidates = candidates; + if (parameter->candidates != NULL) + clearParameterArray(parameter->candidates); } /*======================================================================*/ void setGlobalParameters(ParameterArray newParameters) { - if (globalParameters == NULL) - globalParameters = newParameterArray(); - copyParameterArray(globalParameters, newParameters); + if (globalParameters == NULL) + globalParameters = newParameterArray(); + copyParameterArray(globalParameters, newParameters); } /*======================================================================*/ Parameter *getGlobalParameters(void) { - if (globalParameters == NULL) - globalParameters = newParameterArray(); - return globalParameters; + if (globalParameters == NULL) + globalParameters = newParameterArray(); + return globalParameters; } /*======================================================================*/ Parameter *getGlobalParameter(int parameterIndex) { - return &globalParameters[parameterIndex]; + return &globalParameters[parameterIndex]; } /*======================================================================*/ Parameter *findEndOfParameterArray(Parameter *parameters) { - Parameter *parameter; - for (parameter = parameters; !isEndOfArray(parameter); parameter++); - return parameter; + Parameter *parameter; + for (parameter = parameters; !isEndOfArray(parameter); parameter++); + return parameter; } @@ -125,208 +125,199 @@ Parameter *findEndOfParameterArray(Parameter *parameters) { * matched list) */ int findMultiplePosition(Parameter parameters[]) { - // TODO: this should look at the isAll and isExplicitMultiple flags instead - int multiplePosition; - for (multiplePosition = 0; !isEndOfArray(¶meters[multiplePosition]); multiplePosition++) - if (parameters[multiplePosition].instance == 0) - return multiplePosition; - return -1; + // TODO: this should look at the isAll and isExplicitMultiple flags instead + int multiplePosition; + for (multiplePosition = 0; !isEndOfArray(¶meters[multiplePosition]); multiplePosition++) + if (parameters[multiplePosition].instance == 0) + return multiplePosition; + return -1; } /*======================================================================*/ -void compressParameterArray(Parameter theArray[]) -{ - int i, j; +void compressParameterArray(Parameter theArray[]) { + int i, j; - for (i = 0, j = 0; !isEndOfArray(&theArray[j]); j++) + for (i = 0, j = 0; !isEndOfArray(&theArray[j]); j++) if (theArray[j].instance != 0) theArray[i++] = theArray[j]; - setEndOfArray(&theArray[i]); + setEndOfArray(&theArray[i]); } /*======================================================================*/ -int lengthOfParameterArray(Parameter theArray[]) -{ - int i = 0; +int lengthOfParameterArray(Parameter theArray[]) { + int i = 0; - if (theArray == NULL) return 0; + if (theArray == NULL) return 0; - while (!isEndOfArray(&theArray[i])) - i++; - return i; + while (!isEndOfArray(&theArray[i])) + i++; + return i; } /*======================================================================*/ -bool equalParameterArrays(Parameter parameters1[], Parameter parameters2[]) -{ - int i; - - if ((parameters1 == NULL) != (parameters2 == NULL)) - return FALSE; - if (parameters1 == NULL) // Because then parameter2 is also NULL - return TRUE; - for (i = 0; !isEndOfArray(¶meters1[i]); i++) { - if (isEndOfArray(¶meters2[i])) return FALSE; - if (parameters1[i].instance != parameters2[i].instance) return FALSE; - } - return isEndOfArray(¶meters2[i]); +bool equalParameterArrays(Parameter parameters1[], Parameter parameters2[]) { + int i; + + if ((parameters1 == NULL) != (parameters2 == NULL)) + return FALSE; + if (parameters1 == NULL) // Because then parameter2 is also NULL + return TRUE; + for (i = 0; !isEndOfArray(¶meters1[i]); i++) { + if (isEndOfArray(¶meters2[i])) return FALSE; + if (parameters1[i].instance != parameters2[i].instance) return FALSE; + } + return isEndOfArray(¶meters2[i]); } /*======================================================================*/ -bool inParameterArray(Parameter theArray[], Aword theCode) -{ - int i; +bool inParameterArray(Parameter theArray[], Aword theCode) { + int i; - for (i = 0; !isEndOfArray(&theArray[i]) && theArray[i].instance != theCode; i++); - return (theArray[i].instance == theCode); + for (i = 0; !isEndOfArray(&theArray[i]) && theArray[i].instance != theCode; i++); + return (theArray[i].instance == theCode); } /*======================================================================*/ void copyParameter(Parameter *to, Parameter *from) { - Parameter *toCandidates = to->candidates; - - *to = *from; - if (from->candidates != NULL) { - if (toCandidates == NULL) - to->candidates = newParameterArray(); - else - to->candidates = toCandidates; - copyParameterArray(to->candidates, from->candidates); - } else if (toCandidates != NULL) - freeParameterArray(toCandidates); + Parameter *toCandidates = to->candidates; + + *to = *from; + if (from->candidates != NULL) { + if (toCandidates == NULL) + to->candidates = newParameterArray(); + else + to->candidates = toCandidates; + copyParameterArray(to->candidates, from->candidates); + } else if (toCandidates != NULL) + freeParameterArray(toCandidates); } /*======================================================================*/ -void addParameterToParameterArray(ParameterArray theArray, Parameter *theParameter) -{ - if (theArray == NULL) syserr("Adding to null parameter array"); - - uint i; - - for (i = 0; !isEndOfArray(&theArray[i]) && i < MAXINSTANCE; i++) - ; - if (isEndOfArray(&theArray[i])) { - copyParameter(&theArray[i], theParameter); - setEndOfArray(&theArray[i+1]); - } else - syserr("Couldn't find end of ParameterArray"); +void addParameterToParameterArray(ParameterArray theArray, Parameter *theParameter) { + if (theArray == NULL) syserr("Adding to null parameter array"); + + uint i; + + for (i = 0; !isEndOfArray(&theArray[i]) && i < MAXINSTANCE; i++) + ; + if (isEndOfArray(&theArray[i])) { + copyParameter(&theArray[i], theParameter); + setEndOfArray(&theArray[i + 1]); + } else + syserr("Couldn't find end of ParameterArray"); } /*======================================================================*/ -void copyParameterArray(ParameterArray to, ParameterArray from) -{ - int i; - - if (to == NULL && from == NULL) return; - - if (to == NULL) - syserr("Copying to null parameter array"); - else { - clearParameterArray(to); - for (i = 0; !isEndOfArray(&from[i]); i++) - addParameterToParameterArray(to, &from[i]); - } +void copyParameterArray(ParameterArray to, ParameterArray from) { + int i; + + if (to == NULL && from == NULL) return; + + if (to == NULL) + syserr("Copying to null parameter array"); + else { + clearParameterArray(to); + for (i = 0; !isEndOfArray(&from[i]); i++) + addParameterToParameterArray(to, &from[i]); + } } /*======================================================================*/ -void subtractParameterArrays(Parameter theArray[], Parameter remove[]) -{ - int i; - - if (remove == NULL) return; - - for (i = 0; !isEndOfArray(&theArray[i]); i++) - if (inParameterArray(remove, theArray[i].instance)) - theArray[i].instance = 0; /* Mark empty */ - compressParameterArray(theArray); +void subtractParameterArrays(Parameter theArray[], Parameter remove[]) { + int i; + + if (remove == NULL) return; + + for (i = 0; !isEndOfArray(&theArray[i]); i++) + if (inParameterArray(remove, theArray[i].instance)) + theArray[i].instance = 0; /* Mark empty */ + compressParameterArray(theArray); } /*======================================================================*/ void clearParameterArray(Parameter theArray[]) { - Parameter *p = &theArray[0]; + Parameter *p = &theArray[0]; - for (p = &theArray[0]; !isEndOfArray(p); p++) - clearParameter(p); - setEndOfArray(theArray); + for (p = &theArray[0]; !isEndOfArray(p); p++) + clearParameter(p); + setEndOfArray(theArray); } /*======================================================================*/ -void intersectParameterArrays(Parameter one[], Parameter other[]) -{ - int i, last = 0; +void intersectParameterArrays(Parameter one[], Parameter other[]) { + int i, last = 0; - for (i = 0; !isEndOfArray(&one[i]); i++) + for (i = 0; !isEndOfArray(&one[i]); i++) if (inParameterArray(other, one[i].instance)) one[last++] = one[i]; - setEndOfArray(&one[last]); + setEndOfArray(&one[last]); } /*======================================================================*/ -void copyReferencesToParameterArray(Aint references[], Parameter parameterArray[]) -{ - int i; - - for (i = 0; !isEndOfArray(&references[i]); i++) { - parameterArray[i].instance = references[i]; - parameterArray[i].firstWord = EOD; /* Ensure that there is no word that can be used */ - } - setEndOfArray(¶meterArray[i]); +void copyReferencesToParameterArray(Aint references[], Parameter parameterArray[]) { + int i; + + for (i = 0; !isEndOfArray(&references[i]); i++) { + parameterArray[i].instance = references[i]; + parameterArray[i].firstWord = EOD; /* Ensure that there is no word that can be used */ + } + setEndOfArray(¶meterArray[i]); } /*======================================================================*/ void addParameterForInstance(Parameter *parameters, int instance) { - Parameter *parameter = findEndOfParameterArray(parameters); + Parameter *parameter = findEndOfParameterArray(parameters); - parameter->instance = instance; - parameter->useWords = FALSE; + parameter->instance = instance; + parameter->useWords = FALSE; - setEndOfArray(parameter+1); + setEndOfArray(parameter + 1); } /*======================================================================*/ void addParameterForInteger(ParameterArray parameters, int value) { - Parameter *parameter = findEndOfParameterArray(parameters); + Parameter *parameter = findEndOfParameterArray(parameters); - createIntegerLiteral(value); - parameter->instance = instanceFromLiteral(litCount); - parameter->useWords = FALSE; + createIntegerLiteral(value); + parameter->instance = instanceFromLiteral(litCount); + parameter->useWords = FALSE; - setEndOfArray(parameter+1); + setEndOfArray(parameter + 1); } /*======================================================================*/ void addParameterForString(Parameter *parameters, char *value) { - Parameter *parameter = findEndOfParameterArray(parameters); + Parameter *parameter = findEndOfParameterArray(parameters); - createStringLiteral(value); - parameter->instance = instanceFromLiteral(litCount); - parameter->useWords = FALSE; + createStringLiteral(value); + parameter->instance = instanceFromLiteral(litCount); + parameter->useWords = FALSE; - setEndOfArray(parameter+1); + setEndOfArray(parameter + 1); } /*======================================================================*/ void printParameterArray(Parameter parameters[]) { - int i; - printf("["); - for (i = 0; !isEndOfArray(¶meters[i]); i++) { - printf("%d ", (int)parameters[i].instance); - } - printf("]\n"); + int i; + printf("["); + for (i = 0; !isEndOfArray(¶meters[i]); i++) { + printf("%d ", (int)parameters[i].instance); + } + printf("]\n"); } } // End of namespace Alan3 diff --git a/engines/glk/alan3/params.h b/engines/glk/alan3/params.h index 4b266db16e..a0c460ca40 100644 --- a/engines/glk/alan3/params.h +++ b/engines/glk/alan3/params.h @@ -32,14 +32,14 @@ namespace Alan3 { /* TYPES */ struct Parameter { /* PARAMETER */ - Aid instance; /* Instance code for the parameter (0=multiple) */ - bool isLiteral; - bool isPronoun; - bool isThem; - bool useWords; /* Indicate to use words instead of instance code when saying */ - int firstWord; /* Index to first word used by player */ - int lastWord; /* d:o to last */ - struct Parameter *candidates; /* Array of instances possibly matching this parameter depending on player input */ + Aid instance; /* Instance code for the parameter (0=multiple) */ + bool isLiteral; + bool isPronoun; + bool isThem; + bool useWords; /* Indicate to use words instead of instance code when saying */ + int firstWord; /* Index to first word used by player */ + int lastWord; /* d:o to last */ + struct Parameter *candidates; /* Array of instances possibly matching this parameter depending on player input */ }; typedef Parameter *ParameterArray; diff --git a/engines/glk/alan3/parse.cpp b/engines/glk/alan3/parse.cpp index a1967e6a60..f04b91040c 100644 --- a/engines/glk/alan3/parse.cpp +++ b/engines/glk/alan3/parse.cpp @@ -52,13 +52,13 @@ namespace Alan3 { /* PRIVATE TYPES */ /* To remember parameter/pronoun relations */ struct Pronoun { - int pronoun; - int instance; + int pronoun; + int instance; }; /*----------------------------------------------------------------------*/ static void clearPronounList(Pronoun list[]) { - implementationOfSetEndOfArray((Aword *)list); + implementationOfSetEndOfArray((Aword *)list); } @@ -82,284 +82,284 @@ static Parameter *previousMultipleParameters; /* Previous multiple list */ /*----------------------------------------------------------------------*/ static void addParameterForWord(Parameter *parameters, int wordIndex) { - Parameter *parameter = findEndOfParameterArray(parameters); + Parameter *parameter = findEndOfParameterArray(parameters); - createStringLiteral((char *)pointerTo(dictionary[playerWords[wordIndex].code].string)); - parameter->instance = instanceFromLiteral(litCount); /* A faked literal */ - parameter->useWords = TRUE; - parameter->firstWord = parameter->lastWord = wordIndex; - setEndOfArray(parameter+1); + createStringLiteral((char *)pointerTo(dictionary[playerWords[wordIndex].code].string)); + parameter->instance = instanceFromLiteral(litCount); /* A faked literal */ + parameter->useWords = TRUE; + parameter->firstWord = parameter->lastWord = wordIndex; + setEndOfArray(parameter + 1); } /*----------------------------------------------------------------------*/ static Pronoun *allocatePronounArray(Pronoun *currentList) { - if (currentList == NULL) - currentList = (Pronoun *)allocate(sizeof(Pronoun)*(MAXPARAMS+1)); - clearPronounList(currentList); - return currentList; + if (currentList == NULL) + currentList = (Pronoun *)allocate(sizeof(Pronoun) * (MAXPARAMS + 1)); + clearPronounList(currentList); + return currentList; } /*----------------------------------------------------------------------*/ static bool endOfWords(int wordIndex) { - return isEndOfArray(&playerWords[wordIndex]); + return isEndOfArray(&playerWords[wordIndex]); } /*----------------------------------------------------------------------*/ static void handleDirectionalCommand() { - currentWordIndex++; - if (!endOfWords(currentWordIndex) && !isConjunctionWord(currentWordIndex)) - error(M_WHAT); - else - go(current.location, dictionary[playerWords[currentWordIndex-1].code].code); - if (!endOfWords(currentWordIndex)) - currentWordIndex++; + currentWordIndex++; + if (!endOfWords(currentWordIndex) && !isConjunctionWord(currentWordIndex)) + error(M_WHAT); + else + go(current.location, dictionary[playerWords[currentWordIndex - 1].code].code); + if (!endOfWords(currentWordIndex)) + currentWordIndex++; } /*----------------------------------------------------------------------*/ static void errorWhichOne(Parameter alternative[]) { - int p; /* Index into the list of alternatives */ - ParameterArray parameters = newParameterArray(); - - parameters[0] = alternative[0]; - setEndOfArray(¶meters[1]); - printMessageWithParameters(M_WHICH_ONE_START, parameters); - for (p = 1; !isEndOfArray(&alternative[p+1]); p++) { - clearParameterArray(parameters); - addParameterToParameterArray(parameters, &alternative[p]); - printMessageWithParameters(M_WHICH_ONE_COMMA, parameters); - } - clearParameterArray(parameters); - addParameterToParameterArray(parameters, &alternative[p]); - printMessageWithParameters(M_WHICH_ONE_OR, parameters); - freeParameterArray(parameters); - abortPlayerCommand(); /* Return with empty error message */ + int p; /* Index into the list of alternatives */ + ParameterArray parameters = newParameterArray(); + + parameters[0] = alternative[0]; + setEndOfArray(¶meters[1]); + printMessageWithParameters(M_WHICH_ONE_START, parameters); + for (p = 1; !isEndOfArray(&alternative[p + 1]); p++) { + clearParameterArray(parameters); + addParameterToParameterArray(parameters, &alternative[p]); + printMessageWithParameters(M_WHICH_ONE_COMMA, parameters); + } + clearParameterArray(parameters); + addParameterToParameterArray(parameters, &alternative[p]); + printMessageWithParameters(M_WHICH_ONE_OR, parameters); + freeParameterArray(parameters); + abortPlayerCommand(); /* Return with empty error message */ } /*----------------------------------------------------------------------*/ static void errorWhichPronoun(int pronounWordIndex, Parameter alternatives[]) { - int p; /* Index into the list of alternatives */ - Parameter *messageParameters = newParameterArray(); + int p; /* Index into the list of alternatives */ + Parameter *messageParameters = newParameterArray(); - addParameterForWord(messageParameters, pronounWordIndex); - printMessageWithParameters(M_WHICH_PRONOUN_START, messageParameters); + addParameterForWord(messageParameters, pronounWordIndex); + printMessageWithParameters(M_WHICH_PRONOUN_START, messageParameters); - clearParameterArray(messageParameters); - addParameterToParameterArray(messageParameters, &alternatives[0]); + clearParameterArray(messageParameters); + addParameterToParameterArray(messageParameters, &alternatives[0]); - printMessageWithParameters(M_WHICH_PRONOUN_FIRST, messageParameters); + printMessageWithParameters(M_WHICH_PRONOUN_FIRST, messageParameters); - for (p = 1; !isEndOfArray(&alternatives[p+1]); p++) { - clearParameterArray(messageParameters); - addParameterToParameterArray(messageParameters, &alternatives[p]); - printMessageWithParameters(M_WHICH_ONE_COMMA, messageParameters); - } - clearParameterArray(messageParameters); - addParameterToParameterArray(messageParameters, &alternatives[p]); - printMessageWithParameters(M_WHICH_ONE_OR, messageParameters); - freeParameterArray(messageParameters); - abortPlayerCommand(); + for (p = 1; !isEndOfArray(&alternatives[p + 1]); p++) { + clearParameterArray(messageParameters); + addParameterToParameterArray(messageParameters, &alternatives[p]); + printMessageWithParameters(M_WHICH_ONE_COMMA, messageParameters); + } + clearParameterArray(messageParameters); + addParameterToParameterArray(messageParameters, &alternatives[p]); + printMessageWithParameters(M_WHICH_ONE_OR, messageParameters); + freeParameterArray(messageParameters); + abortPlayerCommand(); } /*----------------------------------------------------------------------*/ static void errorWhat(int playerWordIndex) { - Parameter *messageParameters = newParameterArray(); + Parameter *messageParameters = newParameterArray(); - addParameterForWord(messageParameters, playerWordIndex); - printMessageWithParameters(M_WHAT_WORD, messageParameters); - freeParameterArray(messageParameters); - abortPlayerCommand(); + addParameterForWord(messageParameters, playerWordIndex); + printMessageWithParameters(M_WHAT_WORD, messageParameters); + freeParameterArray(messageParameters); + abortPlayerCommand(); } /*----------------------------------------------------------------------*/ static void errorAfterExcept(int butWordIndex) { - Parameter *messageParameters = newParameterArray(); - addParameterForWord(messageParameters, butWordIndex); - printMessageWithParameters(M_AFTER_BUT, messageParameters); - freeParameterArray(messageParameters); - abortPlayerCommand(); + Parameter *messageParameters = newParameterArray(); + addParameterForWord(messageParameters, butWordIndex); + printMessageWithParameters(M_AFTER_BUT, messageParameters); + freeParameterArray(messageParameters); + abortPlayerCommand(); } /*----------------------------------------------------------------------*/ static int fakePlayerWordForAll() { - /* Look through the dictionary and find any ALL_WORD, then add a - player word so that it can be used in the message */ - int p, d; + /* Look through the dictionary and find any ALL_WORD, then add a + player word so that it can be used in the message */ + int p, d; - for (p = 0; !isEndOfArray(&playerWords[p]); p++) - ; - setEndOfArray(&playerWords[p+1]); /* Make room for one more word */ - for (d = 0; d < dictionarySize; d++) - if (isAll(d)) { - playerWords[p].code = d; - return p; - } - syserr("No ALLWORD found"); - return 0; + for (p = 0; !isEndOfArray(&playerWords[p]); p++) + ; + setEndOfArray(&playerWords[p + 1]); /* Make room for one more word */ + for (d = 0; d < dictionarySize; d++) + if (isAll(d)) { + playerWords[p].code = d; + return p; + } + syserr("No ALLWORD found"); + return 0; } /*----------------------------------------------------------------------*/ static void errorButAfterAll(int butWordIndex) { - Parameter *messageParameters = newParameterArray(); - addParameterForWord(messageParameters, butWordIndex); - addParameterForWord(messageParameters, fakePlayerWordForAll()); - printMessageWithParameters(M_BUT_ALL, messageParameters); - freeParameterArray(messageParameters); - abortPlayerCommand(); + Parameter *messageParameters = newParameterArray(); + addParameterForWord(messageParameters, butWordIndex); + addParameterForWord(messageParameters, fakePlayerWordForAll()); + printMessageWithParameters(M_BUT_ALL, messageParameters); + freeParameterArray(messageParameters); + abortPlayerCommand(); } /*----------------------------------------------------------------------*/ static Aint findInstanceForNoun(int wordIndex) { - DictionaryEntry *d = &dictionary[wordIndex]; - if (d->nounRefs == 0 || d->nounRefs == EOD) - syserr("No references for noun"); - return *(Aint*) pointerTo(d->nounRefs); + DictionaryEntry *d = &dictionary[wordIndex]; + if (d->nounRefs == 0 || d->nounRefs == EOD) + syserr("No references for noun"); + return *(Aint *) pointerTo(d->nounRefs); } /*----------------------------------------------------------------------*/ static void errorNoSuch(Parameter parameter) { - /* If there was no instance, assume the last word used is the noun, - * then find any instance with the noun he used */ - if (parameter.instance == (Aid)-1) - parameter.instance = 0; - if (parameter.instance == 0) - parameter.instance = findInstanceForNoun(playerWords[parameter.lastWord].code); - parameter.useWords = TRUE; /* Indicate to use words and not names */ + /* If there was no instance, assume the last word used is the noun, + * then find any instance with the noun he used */ + if (parameter.instance == (Aid) - 1) + parameter.instance = 0; + if (parameter.instance == 0) + parameter.instance = findInstanceForNoun(playerWords[parameter.lastWord].code); + parameter.useWords = TRUE; /* Indicate to use words and not names */ - clearParameterArray(globalParameters); - addParameterToParameterArray(globalParameters, ¶meter); - error(M_NO_SUCH); + clearParameterArray(globalParameters); + addParameterToParameterArray(globalParameters, ¶meter); + error(M_NO_SUCH); } /*----------------------------------------------------------------------*/ static void buildAllHere(Parameter list[]) { - uint instance; - bool found = FALSE; - int word = list[0].firstWord; + uint instance; + bool found = FALSE; + int word = list[0].firstWord; - for (instance = 1; instance <= header->instanceMax; instance++) - if (isHere(instance, /*FALSE*/ TRANSITIVE)) { - Parameter *parameter = newParameter(instance); - addParameterToParameterArray(list, parameter); - deallocate(parameter); - found = TRUE; - } - if (!found) - errorWhat(word); + for (instance = 1; instance <= header->instanceMax; instance++) + if (isHere(instance, /*FALSE*/ TRANSITIVE)) { + Parameter *parameter = newParameter(instance); + addParameterToParameterArray(list, parameter); + deallocate(parameter); + found = TRUE; + } + if (!found) + errorWhat(word); } /*----------------------------------------------------------------------*/ static bool endOfPronouns(int pronounIndex) { - return isEndOfArray(&pronouns[pronounIndex]); + return isEndOfArray(&pronouns[pronounIndex]); } /*----------------------------------------------------------------------*/ static int getPronounInstances(int word, Parameter instanceParameters[]) { - /* Find the instance that the pronoun word could refer to, return 0 - if none or multiple */ - int p; - int instanceCount = 0; - - clearParameterArray(instanceParameters); - for (p = 0; !endOfPronouns(p); p++) - if (pronouns[p].instance != 0 && dictionary[word].code == (Aword)pronouns[p].pronoun) { - instanceParameters[instanceCount].instance = pronouns[p].instance; - instanceParameters[instanceCount].useWords = FALSE; /* Can't use words since they are gone, pronouns + /* Find the instance that the pronoun word could refer to, return 0 + if none or multiple */ + int p; + int instanceCount = 0; + + clearParameterArray(instanceParameters); + for (p = 0; !endOfPronouns(p); p++) + if (pronouns[p].instance != 0 && dictionary[word].code == (Aword)pronouns[p].pronoun) { + instanceParameters[instanceCount].instance = pronouns[p].instance; + instanceParameters[instanceCount].useWords = FALSE; /* Can't use words since they are gone, pronouns refer to parameters in previous command */ - setEndOfArray(&instanceParameters[++instanceCount]); - } - return instanceCount; + setEndOfArray(&instanceParameters[++instanceCount]); + } + return instanceCount; } /*----------------------------------------------------------------------*/ static bool inOpaqueContainer(int originalInstance) { - int instance = admin[originalInstance].location; + int instance = admin[originalInstance].location; - while (isAContainer(instance)) { - // TODO : isOpaque() - if (getInstanceAttribute(instance, OPAQUEATTRIBUTE)) - return TRUE; - instance = admin[instance].location; - } - return FALSE; + while (isAContainer(instance)) { + // TODO : isOpaque() + if (getInstanceAttribute(instance, OPAQUEATTRIBUTE)) + return TRUE; + instance = admin[instance].location; + } + return FALSE; } /*----------------------------------------------------------------------*/ static bool reachable(int instance) { - if (isA(instance, THING) || isA(instance, LOCATION)) - return isHere(instance, TRANSITIVE) && !inOpaqueContainer(instance); - else - return TRUE; + if (isA(instance, THING) || isA(instance, LOCATION)) + return isHere(instance, TRANSITIVE) && !inOpaqueContainer(instance); + else + return TRUE; } /*----------------------------------------------------------------------*/ static Aint *nounReferencesForWord(int wordIndex) { - return (Aint *) pointerTo(dictionary[playerWords[wordIndex].code].nounRefs); + return (Aint *) pointerTo(dictionary[playerWords[wordIndex].code].nounRefs); } /*----------------------------------------------------------------------*/ static Aint *adjectiveReferencesForWord(int wordIndex) { - return (Aint *) pointerTo(dictionary[playerWords[wordIndex].code].adjectiveRefs); + return (Aint *) pointerTo(dictionary[playerWords[wordIndex].code].adjectiveRefs); } /*----------------------------------------------------------------------*/ static void parseLiteral(Parameter parameters[]) { - parameters[0].firstWord = parameters[0].lastWord = currentWordIndex++; - parameters[0].instance = 0; - parameters[0].isLiteral = TRUE; - setEndOfArray(¶meters[1]); + parameters[0].firstWord = parameters[0].lastWord = currentWordIndex++; + parameters[0].instance = 0; + parameters[0].isLiteral = TRUE; + setEndOfArray(¶meters[1]); } /*----------------------------------------------------------------------*/ static void parsePronoun(Parameter parameters[]) { - parameters[0].firstWord = parameters[0].lastWord = currentWordIndex++; - parameters[0].instance = 0; - parameters[0].isPronoun = TRUE; - setEndOfArray(¶meters[1]); + parameters[0].firstWord = parameters[0].lastWord = currentWordIndex++; + parameters[0].instance = 0; + parameters[0].isPronoun = TRUE; + setEndOfArray(¶meters[1]); } /*----------------------------------------------------------------------*/ static bool anotherAdjective(int wordIndex) { - return !endOfWords(wordIndex) && isAdjectiveWord(wordIndex); + return !endOfWords(wordIndex) && isAdjectiveWord(wordIndex); } /*----------------------------------------------------------------------*/ static bool lastPossibleNoun(int wordIndex) { - return isNounWord(wordIndex) && (endOfWords(wordIndex+1) || !isNounWord(wordIndex+1)); + return isNounWord(wordIndex) && (endOfWords(wordIndex + 1) || !isNounWord(wordIndex + 1)); } /*----------------------------------------------------------------------*/ -static void updateWithReferences(Parameter result[], int wordIndex, Aint *(*referenceFinder)(int wordIndex)) { - static Parameter *references = NULL; /* Instances referenced by a word */ - references = ensureParameterArrayAllocated(references); +static void updateWithReferences(Parameter result[], int wordIndex, Aint * (*referenceFinder)(int wordIndex)) { + static Parameter *references = NULL; /* Instances referenced by a word */ + references = ensureParameterArrayAllocated(references); - copyReferencesToParameterArray(referenceFinder(wordIndex), references); - if (lengthOfParameterArray(result) == 0) - copyParameterArray(result, references); - else - intersectParameterArrays(result, references); + copyReferencesToParameterArray(referenceFinder(wordIndex), references); + if (lengthOfParameterArray(result) == 0) + copyParameterArray(result, references); + else + intersectParameterArrays(result, references); } /*----------------------------------------------------------------------*/ static void filterOutNonReachable(Parameter filteredCandidates[], bool (*reachable)(int)) { - int i; - for (i = 0; !isEndOfArray(&filteredCandidates[i]); i++) - if (!reachable(filteredCandidates[i].instance)) - filteredCandidates[i].instance = 0; - compressParameterArray(filteredCandidates); + int i; + for (i = 0; !isEndOfArray(&filteredCandidates[i]); i++) + if (!reachable(filteredCandidates[i].instance)) + filteredCandidates[i].instance = 0; + compressParameterArray(filteredCandidates); } @@ -376,33 +376,33 @@ static void filterOutNonReachable(Parameter filteredCandidates[], bool (*reachab /*----------------------------------------------------------------------*/ static void disambiguateCandidatesForPosition(ParameterPosition parameterPositions[], int position, Parameter candidates[]) { - int i; - Parameter *parameters = newParameterArray(); - - convertPositionsToParameters(parameterPositions, parameters); - for (i = 0; !isEndOfArray(&candidates[i]); i++) { - if (candidates[i].instance != 0) { /* Already empty? */ - copyParameter(¶meters[position], &candidates[i]); - // DISAMBIGUATION!! - if (!reachable(candidates[i].instance) || !possible(current.verb, parameters, parameterPositions)) - candidates[i].instance = 0; /* Then remove this candidate from list */ - } - } - compressParameterArray(candidates); - freeParameterArray(parameters); + int i; + Parameter *parameters = newParameterArray(); + + convertPositionsToParameters(parameterPositions, parameters); + for (i = 0; !isEndOfArray(&candidates[i]); i++) { + if (candidates[i].instance != 0) { /* Already empty? */ + copyParameter(¶meters[position], &candidates[i]); + // DISAMBIGUATION!! + if (!reachable(candidates[i].instance) || !possible(current.verb, parameters, parameterPositions)) + candidates[i].instance = 0; /* Then remove this candidate from list */ + } + } + compressParameterArray(candidates); + freeParameterArray(parameters); } /*----------------------------------------------------------------------*/ static bool parseAnyAdjectives(Parameter parameters[]) { - bool adjectiveOrNounFound = FALSE; - while (anotherAdjective(currentWordIndex)) { - if (lastPossibleNoun(currentWordIndex)) - break; - adjectiveOrNounFound = TRUE; - currentWordIndex++; - } - return adjectiveOrNounFound; + bool adjectiveOrNounFound = FALSE; + while (anotherAdjective(currentWordIndex)) { + if (lastPossibleNoun(currentWordIndex)) + break; + adjectiveOrNounFound = TRUE; + currentWordIndex++; + } + return adjectiveOrNounFound; } @@ -410,102 +410,102 @@ static bool parseAnyAdjectives(Parameter parameters[]) { /* Parse the input and note the word indices in the parameters, matching will be done by the match* functions */ static void parseAdjectivesAndNoun(Parameter parameters[]) { - int wordFirst, wordLast; - bool adjectiveOrNounFound = FALSE; - - wordFirst = currentWordIndex; - - adjectiveOrNounFound = parseAnyAdjectives(parameters); - - if (!endOfWords(currentWordIndex)) { - if (isNounWord(currentWordIndex)) { - adjectiveOrNounFound = TRUE; - currentWordIndex++; - } else - error(M_NOUN); - } else if (adjectiveOrNounFound) { - /* Perhaps the last word could also be interpreted as a noun? */ - if (isNounWord(currentWordIndex - 1)) { - // TODO When does this get executed? Maybe if conjunctions can be nouns? Or nouns be adjectives? - printf("DEBUG: When does this get executed? Found adjective or Noun and the previous word could also be a noun...\n"); - } else - error(M_NOUN); - } - - if (adjectiveOrNounFound) { + int wordFirst, wordLast; + bool adjectiveOrNounFound = FALSE; + + wordFirst = currentWordIndex; + + adjectiveOrNounFound = parseAnyAdjectives(parameters); + + if (!endOfWords(currentWordIndex)) { + if (isNounWord(currentWordIndex)) { + adjectiveOrNounFound = TRUE; + currentWordIndex++; + } else + error(M_NOUN); + } else if (adjectiveOrNounFound) { + /* Perhaps the last word could also be interpreted as a noun? */ + if (isNounWord(currentWordIndex - 1)) { + // TODO When does this get executed? Maybe if conjunctions can be nouns? Or nouns be adjectives? + printf("DEBUG: When does this get executed? Found adjective or Noun and the previous word could also be a noun...\n"); + } else + error(M_NOUN); + } + + if (adjectiveOrNounFound) { wordLast = currentWordIndex - 1; - parameters[0].firstWord = wordFirst; - parameters[0].lastWord = wordLast; - parameters[0].instance = 0; /* No instance yet! */ - setEndOfArray(¶meters[1]); - } else - setEndOfArray(¶meters[0]); + parameters[0].firstWord = wordFirst; + parameters[0].lastWord = wordLast; + parameters[0].instance = 0; /* No instance yet! */ + setEndOfArray(¶meters[1]); + } else + setEndOfArray(¶meters[0]); } /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ static void parseReference(Parameter parameters[]) { - clearParameterArray(parameters); + clearParameterArray(parameters); - if (isLiteralWord(currentWordIndex)) { - parseLiteral(parameters); - } else if (isPronounWord(currentWordIndex)) { - parsePronoun(parameters); - } else { - parseAdjectivesAndNoun(parameters); - } + if (isLiteralWord(currentWordIndex)) { + parseLiteral(parameters); + } else if (isPronounWord(currentWordIndex)) { + parsePronoun(parameters); + } else { + parseAdjectivesAndNoun(parameters); + } } /*----------------------------------------------------------------------*/ static void getPreviousMultipleParameters(Parameter parameters[]) { - int i; - for (i = 0; !isEndOfArray(&previousMultipleParameters[i]); i++) { - parameters[i].candidates = ensureParameterArrayAllocated(parameters[i].candidates); - setEndOfArray(¶meters[i].candidates[0]); /* No candidates */ - if (!reachable(previousMultipleParameters[i].instance)) - parameters[i].instance = 0; - else - parameters[i].instance = previousMultipleParameters[i].instance; - } - setEndOfArray(¶meters[i]); - compressParameterArray(parameters); + int i; + for (i = 0; !isEndOfArray(&previousMultipleParameters[i]); i++) { + parameters[i].candidates = ensureParameterArrayAllocated(parameters[i].candidates); + setEndOfArray(¶meters[i].candidates[0]); /* No candidates */ + if (!reachable(previousMultipleParameters[i].instance)) + parameters[i].instance = 0; + else + parameters[i].instance = previousMultipleParameters[i].instance; + } + setEndOfArray(¶meters[i]); + compressParameterArray(parameters); } /*----------------------------------------------------------------------*/ static void parseReferenceToPreviousMultipleParameters(Parameter parameters[]) { - parameters[0].firstWord = parameters[0].lastWord = currentWordIndex++; - parameters[0].instance = 0; - parameters[0].isThem = TRUE; - setEndOfArray(¶meters[1]); + parameters[0].firstWord = parameters[0].lastWord = currentWordIndex++; + parameters[0].instance = 0; + parameters[0].isThem = TRUE; + setEndOfArray(¶meters[1]); } /*----------------------------------------------------------------------*/ static bool parseOneParameter(Parameter parameters[], int parameterIndex) { - Parameter *parameter = newParameterArray(); - - // TODO Maybe this should go in the complex()? - if (isThemWord(currentWordIndex) && (!isPronounWord(currentWordIndex) || - (isPronounWord(currentWordIndex) && lengthOfParameterArray(previousMultipleParameters) > 0))) { - // "them" is also a common pronoun for some instances, but if there - // are previous multiple parameters we give precedence to those - parseReferenceToPreviousMultipleParameters(parameter); - } else { - parseReference(parameter); - if (lengthOfParameterArray(parameter) == 0) { /* Failed to find any exceptions! */ - freeParameterArray(parameter); - return FALSE; - } - } - - /* Add the one we found to the parameters */ - parameters[parameterIndex] = parameter[0]; - setEndOfArray(¶meters[parameterIndex+1]); - freeParameterArray(parameter); - return TRUE; + Parameter *parameter = newParameterArray(); + + // TODO Maybe this should go in the complex()? + if (isThemWord(currentWordIndex) && (!isPronounWord(currentWordIndex) || + (isPronounWord(currentWordIndex) && lengthOfParameterArray(previousMultipleParameters) > 0))) { + // "them" is also a common pronoun for some instances, but if there + // are previous multiple parameters we give precedence to those + parseReferenceToPreviousMultipleParameters(parameter); + } else { + parseReference(parameter); + if (lengthOfParameterArray(parameter) == 0) { /* Failed to find any exceptions! */ + freeParameterArray(parameter); + return FALSE; + } + } + + /* Add the one we found to the parameters */ + parameters[parameterIndex] = parameter[0]; + setEndOfArray(¶meters[parameterIndex + 1]); + freeParameterArray(parameter); + return TRUE; } @@ -530,34 +530,34 @@ static bool parseOneParameter(Parameter parameters[], int parameterIndex) { /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ static void simpleParameterParser(Parameter parameters[]) { - /* This will loop until all references are collected (typically "a and b and c") */ - int parameterIndex; - for (parameterIndex = 0;; parameterIndex++) { - - if(!parseOneParameter(parameters, parameterIndex)) - return; - - if (!endOfWords(currentWordIndex) - && (isConjunctionWord(currentWordIndex) && (isAdjectiveWord(currentWordIndex+1) - || isNounWord(currentWordIndex+1)))) { - /* Since this is a conjunction and the next seems to be another instance reference, - let's continue with that by eating the conjunction */ - currentWordIndex++; - } else { - return; - } - } + /* This will loop until all references are collected (typically "a and b and c") */ + int parameterIndex; + for (parameterIndex = 0;; parameterIndex++) { + + if (!parseOneParameter(parameters, parameterIndex)) + return; + + if (!endOfWords(currentWordIndex) + && (isConjunctionWord(currentWordIndex) && (isAdjectiveWord(currentWordIndex + 1) + || isNounWord(currentWordIndex + 1)))) { + /* Since this is a conjunction and the next seems to be another instance reference, + let's continue with that by eating the conjunction */ + currentWordIndex++; + } else { + return; + } + } } /*----------------------------------------------------------------------*/ static void parseExceptions(ParameterPosition *parameterPosition, ParameterParser simpleParameterParser) { - int exceptWordIndex = currentWordIndex; - currentWordIndex++; - parameterPosition->exceptions = ensureParameterArrayAllocated(parameterPosition->exceptions); - simpleParameterParser(parameterPosition->exceptions); - if (lengthOfParameterArray(parameterPosition->exceptions) == 0) - errorAfterExcept(exceptWordIndex); + int exceptWordIndex = currentWordIndex; + currentWordIndex++; + parameterPosition->exceptions = ensureParameterArrayAllocated(parameterPosition->exceptions); + simpleParameterParser(parameterPosition->exceptions); + if (lengthOfParameterArray(parameterPosition->exceptions) == 0) + errorAfterExcept(exceptWordIndex); } @@ -573,130 +573,130 @@ static void parseExceptions(ParameterPosition *parameterPosition, ParameterParse /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ static void complexParameterParserDelegate(ParameterPosition *parameterPosition, ParameterParser simpleParameterParser) { - parameterPosition->parameters = ensureParameterArrayAllocated(parameterPosition->parameters); - - parameterPosition->all = FALSE; - parameterPosition->them = FALSE; - parameterPosition->explicitMultiple = FALSE; - - if (isAllWord(currentWordIndex)) { - parameterPosition->all = TRUE; - parameterPosition->explicitMultiple = TRUE; - parameterPosition->parameters[0].firstWord = currentWordIndex; - parameterPosition->parameters[0].lastWord = currentWordIndex; - currentWordIndex++; - parameterPosition->parameters[0].instance = 0; - setEndOfArray(¶meterPosition->parameters[1]); - if (!endOfWords(currentWordIndex) && isExceptWord(currentWordIndex)) - parseExceptions(parameterPosition, simpleParameterParser); - } else { - simpleParameterParser(parameterPosition->parameters); - if (lengthOfParameterArray(parameterPosition->parameters) > 1) - parameterPosition->explicitMultiple = TRUE; - } + parameterPosition->parameters = ensureParameterArrayAllocated(parameterPosition->parameters); + + parameterPosition->all = FALSE; + parameterPosition->them = FALSE; + parameterPosition->explicitMultiple = FALSE; + + if (isAllWord(currentWordIndex)) { + parameterPosition->all = TRUE; + parameterPosition->explicitMultiple = TRUE; + parameterPosition->parameters[0].firstWord = currentWordIndex; + parameterPosition->parameters[0].lastWord = currentWordIndex; + currentWordIndex++; + parameterPosition->parameters[0].instance = 0; + setEndOfArray(¶meterPosition->parameters[1]); + if (!endOfWords(currentWordIndex) && isExceptWord(currentWordIndex)) + parseExceptions(parameterPosition, simpleParameterParser); + } else { + simpleParameterParser(parameterPosition->parameters); + if (lengthOfParameterArray(parameterPosition->parameters) > 1) + parameterPosition->explicitMultiple = TRUE; + } } /*----------------------------------------------------------------------*/ static void complexReferencesParser(ParameterPosition *parameterPosition) { - complexParameterParserDelegate(parameterPosition, simpleParameterParser); + complexParameterParserDelegate(parameterPosition, simpleParameterParser); } /*----------------------------------------------------------------------*/ static char *classNameAndId(int classId) { - static char buffer[1000] = ""; + static char buffer[1000] = ""; - if (classId != -1) - sprintf(buffer, "%s[%d]", idOfClass(classId), classId); - else - sprintf(buffer, "Container"); - return buffer; + if (classId != -1) + sprintf(buffer, "%s[%d]", idOfClass(classId), classId); + else + sprintf(buffer, "Container"); + return buffer; } /*----------------------------------------------------------------------*/ static char *parameterNumberAndName(int parameterNumber) { - static char buffer[1000] = ""; - /* HERE SHOULD BE current.syntax */ + static char buffer[1000] = ""; + /* HERE SHOULD BE current.syntax */ char *parameterName = parameterNameInSyntax(current.syntax, parameterNumber); - if (parameterName != NULL) - sprintf(buffer, "%s(#%d)", parameterName, parameterNumber); - else - sprintf(buffer, "#%d", parameterNumber); - return buffer; + if (parameterName != NULL) + sprintf(buffer, "%s(#%d)", parameterName, parameterNumber); + else + sprintf(buffer, "#%d", parameterNumber); + return buffer; } /*----------------------------------------------------------------------*/ static void traceRestriction(RestrictionEntry *restriction, int classId, bool condition) { - printf("\n<SYNTAX RESTRICTION WHERE parameter %s Isa %s, %s>\n", - parameterNumberAndName(restriction->parameterNumber), - classNameAndId(classId), condition?"PASSED":"FAILED:"); + printf("\n<SYNTAX RESTRICTION WHERE parameter %s Isa %s, %s>\n", + parameterNumberAndName(restriction->parameterNumber), + classNameAndId(classId), condition ? "PASSED" : "FAILED:"); } /*----------------------------------------------------------------------*/ static bool restrictionCheck(RestrictionEntry *restriction, int instance) { - if (restriction->_class == RESTRICTIONCLASS_CONTAINER) { - if (traceSectionOption) - traceRestriction(restriction, -1, isAContainer(instance)); - return isAContainer(instance); - } else { - if (traceSectionOption) - traceRestriction(restriction, restriction->_class, isA(instance, restriction->_class)); - return isA(instance, restriction->_class); - } + if (restriction->_class == RESTRICTIONCLASS_CONTAINER) { + if (traceSectionOption) + traceRestriction(restriction, -1, isAContainer(instance)); + return isAContainer(instance); + } else { + if (traceSectionOption) + traceRestriction(restriction, restriction->_class, isA(instance, restriction->_class)); + return isA(instance, restriction->_class); + } } /*----------------------------------------------------------------------*/ static void runRestriction(RestrictionEntry *restriction, Parameter parameters[]) { - if (restriction->stms) { - setGlobalParameters(parameters); - interpret(restriction->stms); - } else - error(M_CANT0); + if (restriction->stms) { + setGlobalParameters(parameters); + interpret(restriction->stms); + } else + error(M_CANT0); } /*----------------------------------------------------------------------*/ static int remapParameterOrder(int syntaxNumber, ParameterPosition parameterPositions[]) { - /* Find the syntax map, use the verb code from it and remap the parameters */ - ParameterMapEntry *parameterMapTable; - Aword *parameterMap; - Aint parameterNumber; + /* Find the syntax map, use the verb code from it and remap the parameters */ + ParameterMapEntry *parameterMapTable; + Aword *parameterMap; + Aint parameterNumber; Common::Array<ParameterPosition> savedParameterPositions; savedParameterPositions.resize(MAXPARAMS + 1); - for (parameterMapTable = (ParameterMapEntry *)pointerTo(header->parameterMapAddress); !isEndOfArray(parameterMapTable); parameterMapTable++) - if (parameterMapTable->syntaxNumber == syntaxNumber) - break; - if (isEndOfArray(parameterMapTable)) - syserr("Could not find syntax in mapping table."); + for (parameterMapTable = (ParameterMapEntry *)pointerTo(header->parameterMapAddress); !isEndOfArray(parameterMapTable); parameterMapTable++) + if (parameterMapTable->syntaxNumber == syntaxNumber) + break; + if (isEndOfArray(parameterMapTable)) + syserr("Could not find syntax in mapping table."); - parameterMap = (Aword *)pointerTo(parameterMapTable->parameterMapping); + parameterMap = (Aword *)pointerTo(parameterMapTable->parameterMapping); - copyParameterPositions(parameterPositions, &savedParameterPositions[0]); + copyParameterPositions(parameterPositions, &savedParameterPositions[0]); - for (parameterNumber = 1; !savedParameterPositions[parameterNumber-1].endOfList; parameterNumber++) { - parameterPositions[parameterNumber-1] = savedParameterPositions[parameterMap[parameterNumber-1]-1]; - } + for (parameterNumber = 1; !savedParameterPositions[parameterNumber - 1].endOfList; parameterNumber++) { + parameterPositions[parameterNumber - 1] = savedParameterPositions[parameterMap[parameterNumber - 1] - 1]; + } - return parameterMapTable->verbCode; + return parameterMapTable->verbCode; } /*----------------------------------------------------------------------*/ static bool hasBit(Aword flags, Aint bit) { - return (flags & bit) != 0; + return (flags & bit) != 0; } /*----------------------------------------------------------------------*/ static bool multipleAllowed(Aword flags) { - return hasBit(flags, MULTIPLEBIT); + return hasBit(flags, MULTIPLEBIT); } @@ -723,179 +723,179 @@ static bool multipleAllowed(Aword flags) { /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ static void parseParameterPosition(ParameterPosition *parameterPosition, Aword flags, void (*complexReferencesParser)(ParameterPosition *parameterPosition)) { - parameterPosition->parameters = ensureParameterArrayAllocated(parameterPosition->parameters); + parameterPosition->parameters = ensureParameterArrayAllocated(parameterPosition->parameters); - complexReferencesParser(parameterPosition); - if (lengthOfParameterArray(parameterPosition->parameters) == 0) /* No object!? */ - error(M_WHAT); + complexReferencesParser(parameterPosition); + if (lengthOfParameterArray(parameterPosition->parameters) == 0) /* No object!? */ + error(M_WHAT); - if (parameterPosition->explicitMultiple && !multipleAllowed(flags)) - error(M_MULTIPLE); + if (parameterPosition->explicitMultiple && !multipleAllowed(flags)) + error(M_MULTIPLE); } /*----------------------------------------------------------------------*/ static ElementEntry *elementForParameter(ElementEntry *elms) { - /* Require a parameter if elms->code == 0! */ - while (!isEndOfArray(elms) && elms->code != 0) - elms++; - if (isEndOfArray(elms)) - return NULL; - return elms; + /* Require a parameter if elms->code == 0! */ + while (!isEndOfArray(elms) && elms->code != 0) + elms++; + if (isEndOfArray(elms)) + return NULL; + return elms; } /*----------------------------------------------------------------------*/ static ElementEntry *elementForEndOfSyntax(ElementEntry *elms) { - while (!isEndOfArray(elms) && (Aword)elms->code != EOS) - elms++; - if (isEndOfArray(elms)) /* No match for EOS! */ - return NULL; - return elms; + while (!isEndOfArray(elms) && (Aword)elms->code != EOS) + elms++; + if (isEndOfArray(elms)) /* No match for EOS! */ + return NULL; + return elms; } /*----------------------------------------------------------------------*/ static ElementEntry *elementForWord(ElementEntry *elms, Aint wordCode) { - while (!isEndOfArray(elms) && elms->code != wordCode) - elms++; - if (isEndOfArray(elms)) - return NULL; - return elms; + while (!isEndOfArray(elms) && elms->code != wordCode) + elms++; + if (isEndOfArray(elms)) + return NULL; + return elms; } /*----------------------------------------------------------------------*/ static bool isInstanceReferenceWord(int wordIndex) { - return isNounWord(wordIndex) || isAdjectiveWord(wordIndex) || isAllWord(wordIndex) - || isLiteralWord(wordIndex) || isItWord(wordIndex) || isThemWord(wordIndex) || isPronounWord(wordIndex); + return isNounWord(wordIndex) || isAdjectiveWord(wordIndex) || isAllWord(wordIndex) + || isLiteralWord(wordIndex) || isItWord(wordIndex) || isThemWord(wordIndex) || isPronounWord(wordIndex); } /*----------------------------------------------------------------------*/ static bool endOfPlayerCommand(int wordIndex) { - return endOfWords(wordIndex) || isConjunctionWord(wordIndex); + return endOfWords(wordIndex) || isConjunctionWord(wordIndex); } /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ static ElementEntry *parseInputAccordingToSyntax(SyntaxEntry *syntax, ParameterPosition parameterPositions[]) { - ElementEntry *currentElement = elementTreeOf(syntax); - ElementEntry *nextElement = currentElement; - - int parameterCount = 0; - while (nextElement != NULL) { - /* Traverse the possible branches of currentElement to find a match, let the actual input control what we look for */ - parameterPositions[parameterCount].endOfList = TRUE; - - if (endOfPlayerCommand(currentWordIndex)) { - // TODO If a conjunction word is also some other type of word, like noun? What happens? - return elementForEndOfSyntax(currentElement); - } - - /* Or an instance reference ? */ - if (isInstanceReferenceWord(currentWordIndex)) { - /* If so, save word info for this parameterPosition */ - nextElement = elementForParameter(currentElement); - if (nextElement != NULL) { - // Create parameter structure for the parameter position based on player words - // but without resolving them - ParameterPosition *parameterPosition = ¶meterPositions[parameterCount]; - parseParameterPosition(parameterPosition, nextElement->flags, complexReferencesParser); - parameterPosition->flags = nextElement->flags; - parameterPosition->endOfList = FALSE; - - currentElement = (ElementEntry *) pointerTo(nextElement->next); - parameterCount++; - continue; - } - } - - /* Or maybe preposition? */ - if (isPrepositionWord(currentWordIndex) || isVerbWord(currentWordIndex)) { - /* A preposition? Or rather, an intermediate word? */ - nextElement = elementForWord(currentElement, dictionary[playerWords[currentWordIndex].code].code); - if (nextElement != NULL) { - currentWordIndex++; /* Word matched, go to next */ - currentElement = (ElementEntry *) pointerTo(nextElement->next); - continue; - } - } - - /* Anything else is an error, but we'll handle 'but' specially here */ - if (isExceptWord(currentWordIndex)) - errorButAfterAll(currentWordIndex); - - /* If we get here we couldn't match anything... */ - nextElement = NULL; - } - return NULL; + ElementEntry *currentElement = elementTreeOf(syntax); + ElementEntry *nextElement = currentElement; + + int parameterCount = 0; + while (nextElement != NULL) { + /* Traverse the possible branches of currentElement to find a match, let the actual input control what we look for */ + parameterPositions[parameterCount].endOfList = TRUE; + + if (endOfPlayerCommand(currentWordIndex)) { + // TODO If a conjunction word is also some other type of word, like noun? What happens? + return elementForEndOfSyntax(currentElement); + } + + /* Or an instance reference ? */ + if (isInstanceReferenceWord(currentWordIndex)) { + /* If so, save word info for this parameterPosition */ + nextElement = elementForParameter(currentElement); + if (nextElement != NULL) { + // Create parameter structure for the parameter position based on player words + // but without resolving them + ParameterPosition *parameterPosition = ¶meterPositions[parameterCount]; + parseParameterPosition(parameterPosition, nextElement->flags, complexReferencesParser); + parameterPosition->flags = nextElement->flags; + parameterPosition->endOfList = FALSE; + + currentElement = (ElementEntry *) pointerTo(nextElement->next); + parameterCount++; + continue; + } + } + + /* Or maybe preposition? */ + if (isPrepositionWord(currentWordIndex) || isVerbWord(currentWordIndex)) { + /* A preposition? Or rather, an intermediate word? */ + nextElement = elementForWord(currentElement, dictionary[playerWords[currentWordIndex].code].code); + if (nextElement != NULL) { + currentWordIndex++; /* Word matched, go to next */ + currentElement = (ElementEntry *) pointerTo(nextElement->next); + continue; + } + } + + /* Anything else is an error, but we'll handle 'but' specially here */ + if (isExceptWord(currentWordIndex)) + errorButAfterAll(currentWordIndex); + + /* If we get here we couldn't match anything... */ + nextElement = NULL; + } + return NULL; } /*----------------------------------------------------------------------*/ static bool anyExplicitMultiple(ParameterPosition parameterPositions[]) { - int i; + int i; - for (i = 0; !parameterPositions[i].endOfList; i++) - if (parameterPositions[i].explicitMultiple) - return TRUE; - return FALSE; + for (i = 0; !parameterPositions[i].endOfList; i++) + if (parameterPositions[i].explicitMultiple) + return TRUE; + return FALSE; } /*----------------------------------------------------------------------*/ static bool anyAll(ParameterPosition parameterPositions[]) { - int i; + int i; - for (i = 0; !parameterPositions[i].endOfList; i++) - if (parameterPositions[i].all) - return TRUE; - return FALSE; + for (i = 0; !parameterPositions[i].endOfList; i++) + if (parameterPositions[i].all) + return TRUE; + return FALSE; } /*----------------------------------------------------------------------*/ static void checkRestrictedParameters(ParameterPosition parameterPositions[], ElementEntry elms[]) { - RestrictionEntry *restriction; - static Parameter *localParameters = NULL; - int i; - - localParameters = ensureParameterArrayAllocated(localParameters); - clearParameterArray(localParameters); - - for (i=0; !parameterPositions[i].endOfList; i++) - addParameterToParameterArray(localParameters, ¶meterPositions[i].parameters[0]); - - for (restriction = (RestrictionEntry *) pointerTo(elms->next); !isEndOfArray(restriction); restriction++) { - ParameterPosition *parameterPosition = ¶meterPositions[restriction->parameterNumber-1]; - if (parameterPosition->explicitMultiple) { - /* This was a multiple parameter position, so check all multipleCandidates */ - for (i = 0; !isEndOfArray(¶meterPosition->parameters[i]); i++) { - copyParameter(&localParameters[restriction->parameterNumber-1], ¶meterPosition->parameters[i]); - if (!restrictionCheck(restriction, parameterPosition->parameters[i].instance)) { - /* Multiple could be both an explicit list of instance references and an expansion of ALL */ - if (!parameterPosition->all) { - char marker[80]; - /* It wasn't ALL, we need to say something about it, so - * prepare a printout with $1/2/3 - */ - sprintf(marker, "($%ld)", (unsigned long) restriction->parameterNumber); - setGlobalParameters(localParameters); - output(marker); - runRestriction(restriction, localParameters); - para(); - } - parameterPosition->parameters[i].instance = 0; /* In any case remove it from the list */ - } - } - } else { - if (!restrictionCheck(restriction, parameterPosition->parameters[0].instance)) { - runRestriction(restriction, localParameters); - abortPlayerCommand(); - } - } - parameterPositions[restriction->parameterNumber - 1].checked = TRUE; - } - freeParameterArray(localParameters); - localParameters = NULL; + RestrictionEntry *restriction; + static Parameter *localParameters = NULL; + int i; + + localParameters = ensureParameterArrayAllocated(localParameters); + clearParameterArray(localParameters); + + for (i = 0; !parameterPositions[i].endOfList; i++) + addParameterToParameterArray(localParameters, ¶meterPositions[i].parameters[0]); + + for (restriction = (RestrictionEntry *) pointerTo(elms->next); !isEndOfArray(restriction); restriction++) { + ParameterPosition *parameterPosition = ¶meterPositions[restriction->parameterNumber - 1]; + if (parameterPosition->explicitMultiple) { + /* This was a multiple parameter position, so check all multipleCandidates */ + for (i = 0; !isEndOfArray(¶meterPosition->parameters[i]); i++) { + copyParameter(&localParameters[restriction->parameterNumber - 1], ¶meterPosition->parameters[i]); + if (!restrictionCheck(restriction, parameterPosition->parameters[i].instance)) { + /* Multiple could be both an explicit list of instance references and an expansion of ALL */ + if (!parameterPosition->all) { + char marker[80]; + /* It wasn't ALL, we need to say something about it, so + * prepare a printout with $1/2/3 + */ + sprintf(marker, "($%ld)", (unsigned long) restriction->parameterNumber); + setGlobalParameters(localParameters); + output(marker); + runRestriction(restriction, localParameters); + para(); + } + parameterPosition->parameters[i].instance = 0; /* In any case remove it from the list */ + } + } + } else { + if (!restrictionCheck(restriction, parameterPosition->parameters[0].instance)) { + runRestriction(restriction, localParameters); + abortPlayerCommand(); + } + } + parameterPositions[restriction->parameterNumber - 1].checked = TRUE; + } + freeParameterArray(localParameters); + localParameters = NULL; } @@ -913,173 +913,172 @@ static void impossibleWith(ParameterPosition parameterPositions[], int positionI /*----------------------------------------------------------------------*/ static void checkNonRestrictedParameters(ParameterPosition parameterPositions[]) { - int positionIndex; - for (positionIndex = 0; !parameterPositions[positionIndex].endOfList; positionIndex++) - if (!parameterPositions[positionIndex].checked) { - if (parameterPositions[positionIndex].explicitMultiple) { - /* This was a multiple parameter position, check all multiple candidates and remove failing */ - int i; - for (i = 0; !isEndOfArray(¶meterPositions[positionIndex].parameters[i]); i++) - if (parameterPositions[positionIndex].parameters[i].instance != 0) /* Skip any empty slots */ - if (!isAObject(parameterPositions[positionIndex].parameters[i].instance)) - parameterPositions[positionIndex].parameters[i].instance = 0; - } else if (!isAObject(parameterPositions[positionIndex].parameters[0].instance)) + int positionIndex; + for (positionIndex = 0; !parameterPositions[positionIndex].endOfList; positionIndex++) + if (!parameterPositions[positionIndex].checked) { + if (parameterPositions[positionIndex].explicitMultiple) { + /* This was a multiple parameter position, check all multiple candidates and remove failing */ + int i; + for (i = 0; !isEndOfArray(¶meterPositions[positionIndex].parameters[i]); i++) + if (parameterPositions[positionIndex].parameters[i].instance != 0) /* Skip any empty slots */ + if (!isAObject(parameterPositions[positionIndex].parameters[i].instance)) + parameterPositions[positionIndex].parameters[i].instance = 0; + } else if (!isAObject(parameterPositions[positionIndex].parameters[0].instance)) impossibleWith(parameterPositions, positionIndex); - } + } } /*----------------------------------------------------------------------*/ static void restrictParametersAccordingToSyntax(ParameterPosition parameterPositions[], ElementEntry *elms) { - uncheckAllParameterPositions(parameterPositions); - checkRestrictedParameters(parameterPositions, elms); - checkNonRestrictedParameters(parameterPositions); - int positionIndex; - for (positionIndex = 0; !parameterPositions[positionIndex].endOfList; positionIndex++) - compressParameterArray(parameterPositions[positionIndex].parameters); + uncheckAllParameterPositions(parameterPositions); + checkRestrictedParameters(parameterPositions, elms); + checkNonRestrictedParameters(parameterPositions); + int positionIndex; + for (positionIndex = 0; !parameterPositions[positionIndex].endOfList; positionIndex++) + compressParameterArray(parameterPositions[positionIndex].parameters); } /*----------------------------------------------------------------------*/ static void matchPronoun(Parameter *parameter) { - static Parameter *pronounInstances = NULL; - pronounInstances = ensureParameterArrayAllocated(pronounInstances); - - int pronounCandidateCount = getPronounInstances(playerWords[parameter->firstWord].code, pronounInstances); - if (pronounCandidateCount == 0) - errorWhat(parameter->firstWord); - else if (pronounCandidateCount > 1) - errorWhichPronoun(parameter->firstWord, pronounInstances); - else { - parameter->candidates[0] = pronounInstances[0]; - setEndOfArray(¶meter->candidates[1]); - } + static Parameter *pronounInstances = NULL; + pronounInstances = ensureParameterArrayAllocated(pronounInstances); + + int pronounCandidateCount = getPronounInstances(playerWords[parameter->firstWord].code, pronounInstances); + if (pronounCandidateCount == 0) + errorWhat(parameter->firstWord); + else if (pronounCandidateCount > 1) + errorWhichPronoun(parameter->firstWord, pronounInstances); + else { + parameter->candidates[0] = pronounInstances[0]; + setEndOfArray(¶meter->candidates[1]); + } } /*----------------------------------------------------------------------*/ static void matchNounPhrase(Parameter *parameter, ReferencesFinder adjectiveReferencesFinder, ReferencesFinder nounReferencesFinder) { - int i; + int i; - for (i = parameter->firstWord; i < parameter->lastWord; i++) - updateWithReferences(parameter->candidates, i, adjectiveReferencesFinder); - updateWithReferences(parameter->candidates, parameter->lastWord, nounReferencesFinder); + for (i = parameter->firstWord; i < parameter->lastWord; i++) + updateWithReferences(parameter->candidates, i, adjectiveReferencesFinder); + updateWithReferences(parameter->candidates, parameter->lastWord, nounReferencesFinder); } /*----------------------------------------------------------------------*/ static void instanceMatcher(Parameter *parameter) { - Parameter *candidates = parameter->candidates; - int i; - - if (parameter->isLiteral) { - candidates[0].instance = instanceFromLiteral(playerWords[parameter->firstWord].code - dictionarySize); - setEndOfArray(&candidates[1]); - } else if (parameter->isPronoun) { - matchPronoun(parameter); - } else - matchNounPhrase(parameter, adjectiveReferencesForWord, nounReferencesForWord); - - // Ensure that every candidate have the words, even if there where no candidates - candidates[0].firstWord = parameter->firstWord; - candidates[0].lastWord = parameter->lastWord; - for (i = 0; i < lengthOfParameterArray(candidates); i++) { - candidates[i].firstWord = parameter->firstWord; - candidates[i].lastWord = parameter->lastWord; - } + Parameter *candidates = parameter->candidates; + int i; + + if (parameter->isLiteral) { + candidates[0].instance = instanceFromLiteral(playerWords[parameter->firstWord].code - dictionarySize); + setEndOfArray(&candidates[1]); + } else if (parameter->isPronoun) { + matchPronoun(parameter); + } else + matchNounPhrase(parameter, adjectiveReferencesForWord, nounReferencesForWord); + + // Ensure that every candidate have the words, even if there where no candidates + candidates[0].firstWord = parameter->firstWord; + candidates[0].lastWord = parameter->lastWord; + for (i = 0; i < lengthOfParameterArray(candidates); i++) { + candidates[i].firstWord = parameter->firstWord; + candidates[i].lastWord = parameter->lastWord; + } } /*----------------------------------------------------------------------*/ -static void findCandidates(Parameter parameters[], void (*instanceMatcher)(Parameter *parameter)) -{ - int i; +static void findCandidates(Parameter parameters[], void (*instanceMatcher)(Parameter *parameter)) { + int i; - for (i = 0; i < lengthOfParameterArray(parameters); i++) { - parameters[i].candidates = ensureParameterArrayAllocated(parameters[i].candidates); - instanceMatcher(¶meters[i]); - parameters[i].candidates[0].isPronoun = parameters[i].isPronoun; - } + for (i = 0; i < lengthOfParameterArray(parameters); i++) { + parameters[i].candidates = ensureParameterArrayAllocated(parameters[i].candidates); + instanceMatcher(¶meters[i]); + parameters[i].candidates[0].isPronoun = parameters[i].isPronoun; + } } /*----------------------------------------------------------------------*/ static void handleFailedParse(ElementEntry *elms) { - if (elms == NULL) - error(M_WHAT); - if (elms->next == 0) { /* No verb code, verb not declared! */ - /* TODO Does this ever happen? */ - error(M_CANT0); - } + if (elms == NULL) + error(M_WHAT); + if (elms->next == 0) { /* No verb code, verb not declared! */ + /* TODO Does this ever happen? */ + error(M_CANT0); + } } /*----------------------------------------------------------------------*/ static void convertMultipleCandidatesToMultipleParameters(ParameterPosition parameterPositions[], Parameter multipleParameters[]) { - int parameterIndex; - for (parameterIndex=0; !parameterPositions[parameterIndex].endOfList; parameterIndex++) - if (parameterPositions[parameterIndex].explicitMultiple) { - compressParameterArray(parameterPositions[parameterIndex].parameters); - copyParameterArray(multipleParameters, parameterPositions[parameterIndex].parameters); - } + int parameterIndex; + for (parameterIndex = 0; !parameterPositions[parameterIndex].endOfList; parameterIndex++) + if (parameterPositions[parameterIndex].explicitMultiple) { + compressParameterArray(parameterPositions[parameterIndex].parameters); + copyParameterArray(multipleParameters, parameterPositions[parameterIndex].parameters); + } } /*----------------------------------------------------------------------*/ static ElementEntry *parseInput(ParameterPosition *parameterPositions) { - ElementEntry *element; - SyntaxEntry *stx; + ElementEntry *element; + SyntaxEntry *stx; - stx = findSyntaxTreeForVerb(verbWordCode); - element = parseInputAccordingToSyntax(stx, parameterPositions); - handleFailedParse(element); - current.syntax = element->flags; - current.verb = remapParameterOrder(current.syntax, parameterPositions); - return element; + stx = findSyntaxTreeForVerb(verbWordCode); + element = parseInputAccordingToSyntax(stx, parameterPositions); + handleFailedParse(element); + current.syntax = element->flags; + current.verb = remapParameterOrder(current.syntax, parameterPositions); + return element; } /*----------------------------------------------------------------------*/ static void findCandidatesForPlayerWords(ParameterPosition *parameterPosition) { - ParameterArray parameters = parameterPosition->parameters; - - if (!parameterArrayIsEmpty(parameters)) { - if (parameters[0].isThem) { - parameterPosition->them = TRUE; - getPreviousMultipleParameters(parameters); - if (lengthOfParameterArray(parameters) == 0) - errorWhat(parameters[0].firstWord); - if (lengthOfParameterArray(parameters) > 1) - parameterPosition->explicitMultiple = TRUE; - } else if (parameterPosition->all) { - buildAllHere(parameters); - if (!parameterArrayIsEmpty(parameterPosition->exceptions)) - findCandidates(parameterPosition->exceptions, instanceMatcher); - } else - findCandidates(parameters, instanceMatcher); - } + ParameterArray parameters = parameterPosition->parameters; + + if (!parameterArrayIsEmpty(parameters)) { + if (parameters[0].isThem) { + parameterPosition->them = TRUE; + getPreviousMultipleParameters(parameters); + if (lengthOfParameterArray(parameters) == 0) + errorWhat(parameters[0].firstWord); + if (lengthOfParameterArray(parameters) > 1) + parameterPosition->explicitMultiple = TRUE; + } else if (parameterPosition->all) { + buildAllHere(parameters); + if (!parameterArrayIsEmpty(parameterPosition->exceptions)) + findCandidates(parameterPosition->exceptions, instanceMatcher); + } else + findCandidates(parameters, instanceMatcher); + } } /*----------------------------------------------------------------------*/ static void handleMultiplePosition(ParameterPosition parameterPositions[]) { - int multiplePosition = findMultipleParameterPosition(parameterPositions); - if (anyAll(parameterPositions)) { - /* If the player used ALL, try to find out what was applicable */ - disambiguateCandidatesForPosition(parameterPositions, multiplePosition, parameterPositions[multiplePosition].parameters); - if (lengthOfParameterArray(parameterPositions[multiplePosition].parameters) == 0) - errorWhat(parameterPositions[multiplePosition].parameters[0].firstWord); - subtractParameterArrays(parameterPositions[multiplePosition].parameters, parameterPositions[multiplePosition].exceptions); - if (lengthOfParameterArray(parameterPositions[multiplePosition].parameters) == 0) - error(M_NOT_MUCH); - } else if (anyExplicitMultiple(parameterPositions)) { - compressParameterArray(parameterPositions[multiplePosition].parameters); - if (lengthOfParameterArray(parameterPositions[multiplePosition].parameters) == 0) { - /* If there where multiple parameters but non left, exit without a */ - /* word, assuming we have already said enough */ - abortPlayerCommand(); - } - } + int multiplePosition = findMultipleParameterPosition(parameterPositions); + if (anyAll(parameterPositions)) { + /* If the player used ALL, try to find out what was applicable */ + disambiguateCandidatesForPosition(parameterPositions, multiplePosition, parameterPositions[multiplePosition].parameters); + if (lengthOfParameterArray(parameterPositions[multiplePosition].parameters) == 0) + errorWhat(parameterPositions[multiplePosition].parameters[0].firstWord); + subtractParameterArrays(parameterPositions[multiplePosition].parameters, parameterPositions[multiplePosition].exceptions); + if (lengthOfParameterArray(parameterPositions[multiplePosition].parameters) == 0) + error(M_NOT_MUCH); + } else if (anyExplicitMultiple(parameterPositions)) { + compressParameterArray(parameterPositions[multiplePosition].parameters); + if (lengthOfParameterArray(parameterPositions[multiplePosition].parameters) == 0) { + /* If there where multiple parameters but non left, exit without a */ + /* word, assuming we have already said enough */ + abortPlayerCommand(); + } + } } @@ -1120,345 +1119,368 @@ typedef Parameter *DisambiguationHandler(Parameter allCandidates[], Parameter pr typedef DisambiguationHandler *DisambiguationHandlerTable[3][3][2]; static Parameter *disambiguate00N(Parameter allCandidates[], Parameter presentCandidates[]) { - if (allCandidates[0].isPronoun) - errorWhat(allCandidates[0].firstWord); - else - errorNoSuch(allCandidates[0]); - return NULL; + if (allCandidates[0].isPronoun) + errorWhat(allCandidates[0].firstWord); + else + errorNoSuch(allCandidates[0]); + return NULL; } static Parameter *disambiguate01N(Parameter allCandidates[], Parameter presentCandidates[]) { - if (allCandidates[0].isPronoun) - errorWhat(allCandidates[0].firstWord); - else - errorNoSuch(allCandidates[0]); - return NULL; + if (allCandidates[0].isPronoun) + errorWhat(allCandidates[0].firstWord); + else + errorNoSuch(allCandidates[0]); + return NULL; } static Parameter *disambiguate0MN(Parameter allCandidates[], Parameter presentCandidates[]) { - if (allCandidates[0].isPronoun) - errorWhat(allCandidates[0].firstWord); - else - errorNoSuch(allCandidates[0]); - return NULL; + if (allCandidates[0].isPronoun) + errorWhat(allCandidates[0].firstWord); + else + errorNoSuch(allCandidates[0]); + return NULL; } static Parameter *disambiguate10N(Parameter allCandidates[], Parameter presentCandidates[]) { - return presentCandidates; + return presentCandidates; } static Parameter *disambiguate11N(Parameter allCandidates[], Parameter presentCandidates[]) { - return presentCandidates; + return presentCandidates; } static Parameter *disambiguate1MN(Parameter allCandidates[], Parameter presentCandidates[]) { - return presentCandidates; + return presentCandidates; } static Parameter *disambiguateM0N(Parameter allCandidates[], Parameter presentCandidates[]) { - errorWhichOne(presentCandidates); - return NULL; + errorWhichOne(presentCandidates); + return NULL; } static Parameter *disambiguateM1N(Parameter allCandidates[], Parameter presentCandidates[]) { - errorWhichOne(presentCandidates); - return NULL; + errorWhichOne(presentCandidates); + return NULL; } static Parameter *disambiguateMMN(Parameter allCandidates[], Parameter presentCandidates[]) { - errorWhichOne(presentCandidates); - return NULL; + errorWhichOne(presentCandidates); + return NULL; } static Parameter *disambiguate00Y(Parameter allCandidates[], Parameter presentCandidates[]) { - errorNoSuch(allCandidates[0]); - return NULL; + errorNoSuch(allCandidates[0]); + return NULL; } static Parameter *disambiguate01Y(Parameter allCandidates[], Parameter presentCandidates[]) { - return allCandidates; + return allCandidates; } static Parameter *disambiguate0MY(Parameter allCandidates[], Parameter presentCandidates[]) { - errorWhichOne(allCandidates); - return NULL; + errorWhichOne(allCandidates); + return NULL; } static Parameter *disambiguate10Y(Parameter allCandidates[], Parameter presentCandidates[]) { - return presentCandidates; + return presentCandidates; } static Parameter *disambiguate11Y(Parameter allCandidates[], Parameter presentCandidates[]) { - return presentCandidates; + return presentCandidates; } static Parameter *disambiguate1MY(Parameter allCandidates[], Parameter presentCandidates[]) { - return presentCandidates; + return presentCandidates; } static Parameter *disambiguateM0Y(Parameter allCandidates[], Parameter presentCandidates[]) { - errorWhichOne(presentCandidates); - return NULL; + errorWhichOne(presentCandidates); + return NULL; } static Parameter *disambiguateM1Y(Parameter allCandidates[], Parameter presentCandidates[]) { - errorWhichOne(presentCandidates); - return NULL; + errorWhichOne(presentCandidates); + return NULL; } static Parameter *disambiguateMMY(Parameter allCandidates[], Parameter presentCandidates[]) { - errorWhichOne(presentCandidates);return NULL; -} - -static DisambiguationHandlerTable disambiguationHandlerTable = - { - { // Present == 0 - { // Distant == 0 - disambiguate00N, disambiguate00Y}, - { // Distant == 1 - disambiguate01N, disambiguate01Y}, - { // Distant == M - disambiguate0MN, disambiguate0MY}}, - { // Present == 1 - { // Distant == 0 - disambiguate10N, disambiguate10Y}, - { // Distant == 1 - disambiguate11N, disambiguate11Y}, - { // Distant == M - disambiguate1MN, disambiguate1MY}}, - { // Present == M - { // Distant == 0 - disambiguateM0N, disambiguateM0Y}, - { // Distant == 1 - disambiguateM1N, disambiguateM1Y}, - { // Distant == M - disambiguateMMN, disambiguateMMY}} - }; + errorWhichOne(presentCandidates); + return NULL; +} + +static DisambiguationHandlerTable disambiguationHandlerTable = { + { + // Present == 0 + { + // Distant == 0 + disambiguate00N, disambiguate00Y + }, + { + // Distant == 1 + disambiguate01N, disambiguate01Y + }, + { + // Distant == M + disambiguate0MN, disambiguate0MY + } + }, + { + // Present == 1 + { + // Distant == 0 + disambiguate10N, disambiguate10Y + }, + { + // Distant == 1 + disambiguate11N, disambiguate11Y + }, + { + // Distant == M + disambiguate1MN, disambiguate1MY + } + }, + { + // Present == M + { + // Distant == 0 + disambiguateM0N, disambiguateM0Y + }, + { + // Distant == 1 + disambiguateM1N, disambiguateM1Y + }, + { + // Distant == M + disambiguateMMN, disambiguateMMY + } + } +}; /*----------------------------------------------------------------------*/ static void disambiguateCandidates(Parameter *allCandidates, bool omnipotent, bool (*reachable)(int), DisambiguationHandlerTable handler) { - static Parameter *presentCandidates = NULL; - int present; - int distant; - Parameter *result; + static Parameter *presentCandidates = NULL; + int present; + int distant; + Parameter *result; - presentCandidates = ensureParameterArrayAllocated(presentCandidates); + presentCandidates = ensureParameterArrayAllocated(presentCandidates); - copyParameterArray(presentCandidates, allCandidates); - filterOutNonReachable(presentCandidates, reachable); + copyParameterArray(presentCandidates, allCandidates); + filterOutNonReachable(presentCandidates, reachable); - present = lengthOfParameterArray(presentCandidates); - if (present > 1) present = 2; /* 2 = M */ + present = lengthOfParameterArray(presentCandidates); + if (present > 1) present = 2; /* 2 = M */ - distant = lengthOfParameterArray(allCandidates) - present; - if (distant > 1) distant = 2; /* 2 = M */ + distant = lengthOfParameterArray(allCandidates) - present; + if (distant > 1) distant = 2; /* 2 = M */ - result = handler[present][distant][omnipotent](allCandidates, presentCandidates); + result = handler[present][distant][omnipotent](allCandidates, presentCandidates); - // If we returned then it's ok, use the single candidate found - allCandidates[0] = result[0]; - setEndOfArray(&allCandidates[1]); + // If we returned then it's ok, use the single candidate found + allCandidates[0] = result[0]; + setEndOfArray(&allCandidates[1]); } /*----------------------------------------------------------------------*/ static void disambiguate(ParameterPosition parameterPositions[], ElementEntry *element) { - /* The New Strategy! Parsing has only collected word indications, - not built anything, so we need to match parameters to instances here */ - - int position; - for (position = 0; !parameterPositions[position].endOfList; position++) { - findCandidatesForPlayerWords(¶meterPositions[position]); - } - - /* Now we have candidates for everything the player said, except - if he used ALL or THEM, then we have built those as parameters, or he - referred to the multiple parameters of the previous command - using 'them, if so, they too are stored as parameters */ - - for (position = 0; !parameterPositions[position].endOfList; position++) { - ParameterPosition *parameterPosition = ¶meterPositions[position]; - bool omni = hasBit(parameterPosition->flags, OMNIBIT); - if (!parameterPosition->all && !parameterPosition->them) { - Parameter *parameters = parameterPosition->parameters; - int p; - for (p = 0; p < lengthOfParameterArray(parameters); p++) { - Parameter *parameter = ¶meters[p]; - Parameter *candidates = parameter->candidates; - disambiguateCandidates(candidates, omni, reachable, disambiguationHandlerTable); - parameter->instance = candidates[0].instance; - } - } - if (parameterPosition->all) { - Parameter *exceptions = parameterPosition->exceptions; - int p; - for (p = 0; p < lengthOfParameterArray(exceptions); p++) { - Parameter *parameter = &exceptions[p]; - Parameter *candidates = parameter->candidates; - disambiguateCandidates(candidates, omni, reachable, disambiguationHandlerTable); - parameter->instance = candidates[0].instance; - } - } - } - - handleMultiplePosition(parameterPositions); - - /* Now perform restriction checks */ - restrictParametersAccordingToSyntax(parameterPositions, element); + /* The New Strategy! Parsing has only collected word indications, + not built anything, so we need to match parameters to instances here */ + + int position; + for (position = 0; !parameterPositions[position].endOfList; position++) { + findCandidatesForPlayerWords(¶meterPositions[position]); + } + + /* Now we have candidates for everything the player said, except + if he used ALL or THEM, then we have built those as parameters, or he + referred to the multiple parameters of the previous command + using 'them, if so, they too are stored as parameters */ + + for (position = 0; !parameterPositions[position].endOfList; position++) { + ParameterPosition *parameterPosition = ¶meterPositions[position]; + bool omni = hasBit(parameterPosition->flags, OMNIBIT); + if (!parameterPosition->all && !parameterPosition->them) { + Parameter *parameters = parameterPosition->parameters; + int p; + for (p = 0; p < lengthOfParameterArray(parameters); p++) { + Parameter *parameter = ¶meters[p]; + Parameter *candidates = parameter->candidates; + disambiguateCandidates(candidates, omni, reachable, disambiguationHandlerTable); + parameter->instance = candidates[0].instance; + } + } + if (parameterPosition->all) { + Parameter *exceptions = parameterPosition->exceptions; + int p; + for (p = 0; p < lengthOfParameterArray(exceptions); p++) { + Parameter *parameter = &exceptions[p]; + Parameter *candidates = parameter->candidates; + disambiguateCandidates(candidates, omni, reachable, disambiguationHandlerTable); + parameter->instance = candidates[0].instance; + } + } + } + + handleMultiplePosition(parameterPositions); + + /* Now perform restriction checks */ + restrictParametersAccordingToSyntax(parameterPositions, element); } /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ static void tryParam(Parameter parameters[], Parameter multipleParameters[]) { - ElementEntry *element; /* Pointer to element list */ - static ParameterPosition *parameterPositions = NULL; - if (parameterPositions != NULL) - deallocateParameterPositions(parameterPositions); + ElementEntry *element; /* Pointer to element list */ + static ParameterPosition *parameterPositions = NULL; + if (parameterPositions != NULL) + deallocateParameterPositions(parameterPositions); - // TODO newParameterPositionArray()!!!! Or even reallocatePP.. or cleanPP.. - parameterPositions = (ParameterPosition *)allocate(sizeof(ParameterPosition)*(MAXPARAMS+1)); - parameterPositions[0].endOfList = TRUE; + // TODO newParameterPositionArray()!!!! Or even reallocatePP.. or cleanPP.. + parameterPositions = (ParameterPosition *)allocate(sizeof(ParameterPosition) * (MAXPARAMS + 1)); + parameterPositions[0].endOfList = TRUE; - element = parseInput(parameterPositions); + element = parseInput(parameterPositions); - disambiguate(parameterPositions, element); + disambiguate(parameterPositions, element); - // TODO: Now we need to convert back to legacy parameter and multipleParameter format - convertPositionsToParameters(parameterPositions, parameters); - markExplicitMultiple(parameterPositions, parameters); - convertMultipleCandidatesToMultipleParameters(parameterPositions, multipleParameters); + // TODO: Now we need to convert back to legacy parameter and multipleParameter format + convertPositionsToParameters(parameterPositions, parameters); + markExplicitMultiple(parameterPositions, parameters); + convertMultipleCandidatesToMultipleParameters(parameterPositions, multipleParameters); - deallocateParameterPositions(parameterPositions); - parameterPositions = NULL; + deallocateParameterPositions(parameterPositions); + parameterPositions = NULL; } /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ -static void parseOneCommand(Parameter parameters[], Parameter multipleParameters[]) -{ - tryParam(parameters, multipleParameters); /* ... to understand what he said */ - - /* More on this line? */ - if (!endOfWords(currentWordIndex)) { - if (isConjunctionWord(currentWordIndex)) - currentWordIndex++; /* If so skip the conjunction */ - else - error(M_WHAT); - } +static void parseOneCommand(Parameter parameters[], Parameter multipleParameters[]) { + tryParam(parameters, multipleParameters); /* ... to understand what he said */ + + /* More on this line? */ + if (!endOfWords(currentWordIndex)) { + if (isConjunctionWord(currentWordIndex)) + currentWordIndex++; /* If so skip the conjunction */ + else + error(M_WHAT); + } } /*======================================================================*/ void initParsing(void) { - currentWordIndex = 0; - continued = FALSE; - ensureSpaceForPlayerWords(0); - clearWordList(playerWords); + currentWordIndex = 0; + continued = FALSE; + ensureSpaceForPlayerWords(0); + clearWordList(playerWords); - pronouns = allocatePronounArray(pronouns); - globalParameters = ensureParameterArrayAllocated(globalParameters); - previousMultipleParameters = ensureParameterArrayAllocated(previousMultipleParameters); + pronouns = allocatePronounArray(pronouns); + globalParameters = ensureParameterArrayAllocated(globalParameters); + previousMultipleParameters = ensureParameterArrayAllocated(previousMultipleParameters); } /*----------------------------------------------------------------------*/ static int pronounWordForInstance(int instance) { - /* Scan through the dictionary to find any pronouns that can be used - for this instance */ - int w; + /* Scan through the dictionary to find any pronouns that can be used + for this instance */ + int w; - for (w = 0; w < dictionarySize; w++) - if (isPronoun(w)) { - Aword *reference = (Aword *)pointerTo(dictionary[w].pronounRefs); - while (*reference != EOD) { - if (*reference == (Aword)instance) - return dictionary[w].code; - reference++; - } - } - return 0; + for (w = 0; w < dictionarySize; w++) + if (isPronoun(w)) { + Aword *reference = (Aword *)pointerTo(dictionary[w].pronounRefs); + while (*reference != EOD) { + if (*reference == (Aword)instance) + return dictionary[w].code; + reference++; + } + } + return 0; } /*----------------------------------------------------------------------*/ static void addPronounForInstance(int thePronoun, int instanceCode) { - int i; + int i; - for (i = 0; !endOfPronouns(i); i++) - if (pronouns[i].pronoun == thePronoun && pronouns[i].instance == instanceCode) - // Don't add the same instance twice for the same pronoun - return; - pronouns[i].pronoun = thePronoun; - pronouns[i].instance = instanceCode; - setEndOfArray(&pronouns[i + 1]); + for (i = 0; !endOfPronouns(i); i++) + if (pronouns[i].pronoun == thePronoun && pronouns[i].instance == instanceCode) + // Don't add the same instance twice for the same pronoun + return; + pronouns[i].pronoun = thePronoun; + pronouns[i].instance = instanceCode; + setEndOfArray(&pronouns[i + 1]); } /*----------------------------------------------------------------------*/ static void notePronounsForParameters(Parameter parameters[]) { - /* For all parameters note which ones can be referred to by a pronoun */ - Parameter *p; - - clearPronounList(pronouns); - for (p = parameters; !isEndOfArray(p); p++) { - int pronoun = pronounWordForInstance(p->instance); - if (pronoun > 0) - addPronounForInstance(pronoun, p->instance); - } + /* For all parameters note which ones can be referred to by a pronoun */ + Parameter *p; + + clearPronounList(pronouns); + for (p = parameters; !isEndOfArray(p); p++) { + int pronoun = pronounWordForInstance(p->instance); + if (pronoun > 0) + addPronounForInstance(pronoun, p->instance); + } } /*----------------------------------------------------------------------*/ static void parseVerbCommand(Parameter parameters[], Parameter multipleParameters[]) { - verbWord = playerWords[currentWordIndex].code; - verbWordCode = dictionary[verbWord].code; - if (isPreBeta2(header->version)) - /* Pre-beta2 didn't generate syntax elements for verb words, - need to skip first word which should be the verb */ - currentWordIndex++; - parseOneCommand(parameters, multipleParameters); - notePronounsForParameters(parameters); - fail = FALSE; + verbWord = playerWords[currentWordIndex].code; + verbWordCode = dictionary[verbWord].code; + if (isPreBeta2(header->version)) + /* Pre-beta2 didn't generate syntax elements for verb words, + need to skip first word which should be the verb */ + currentWordIndex++; + parseOneCommand(parameters, multipleParameters); + notePronounsForParameters(parameters); + fail = FALSE; } /*----------------------------------------------------------------------*/ static void parseInstanceCommand(Parameter parameters[], Parameter multipleParameters[]) { - /* Pick up the parse tree for the syntaxes that start with an - instance reference and parse according to that. The - verbWord code is set to 0 to indicate that it is not a verb - but an instance that starts the command. */ - verbWordCode = 0; - parseOneCommand(parameters, multipleParameters); - notePronounsForParameters(parameters); - fail = FALSE; + /* Pick up the parse tree for the syntaxes that start with an + instance reference and parse according to that. The + verbWord code is set to 0 to indicate that it is not a verb + but an instance that starts the command. */ + verbWordCode = 0; + parseOneCommand(parameters, multipleParameters); + notePronounsForParameters(parameters); + fail = FALSE; } /*======================================================================*/ void parse(void) { - /* longjmp's ahead so these need to survive to not leak memory */ - static Parameter *parameters = NULL; - static Parameter *multipleParameters = NULL; - parameters = ensureParameterArrayAllocated(parameters); - multipleParameters = ensureParameterArrayAllocated(multipleParameters); - - if (endOfWords(currentWordIndex)) { - currentWordIndex = 0; - scan(); - } else if (anyOutput) - para(); - - capitalize = TRUE; - - firstWord = currentWordIndex; - if (isVerbWord(currentWordIndex)) { - parseVerbCommand(parameters, multipleParameters); - action(current.verb, parameters, multipleParameters); - } else if (isDirectionWord(currentWordIndex)) { - clearParameterArray(previousMultipleParameters); - clearPronounList(pronouns); - handleDirectionalCommand(); - } else if (isInstanceReferenceWord(currentWordIndex)) { - parseInstanceCommand(parameters, multipleParameters); - action(current.verb, parameters, multipleParameters); - } else - error(M_WHAT); - - if (fail) error(NO_MSG); - - lastWord = currentWordIndex - 1; - if (isConjunctionWord(lastWord)) - lastWord--; - - if (lengthOfParameterArray(parameters) > 0) - copyParameterArray(previousMultipleParameters, multipleParameters); - else - clearParameterArray(previousMultipleParameters); - - freeParameterArray(parameters); - parameters = NULL; - freeParameterArray(multipleParameters); - multipleParameters = NULL; + /* longjmp's ahead so these need to survive to not leak memory */ + static Parameter *parameters = NULL; + static Parameter *multipleParameters = NULL; + parameters = ensureParameterArrayAllocated(parameters); + multipleParameters = ensureParameterArrayAllocated(multipleParameters); + + if (endOfWords(currentWordIndex)) { + currentWordIndex = 0; + scan(); + } else if (anyOutput) + para(); + + capitalize = TRUE; + + firstWord = currentWordIndex; + if (isVerbWord(currentWordIndex)) { + parseVerbCommand(parameters, multipleParameters); + action(current.verb, parameters, multipleParameters); + } else if (isDirectionWord(currentWordIndex)) { + clearParameterArray(previousMultipleParameters); + clearPronounList(pronouns); + handleDirectionalCommand(); + } else if (isInstanceReferenceWord(currentWordIndex)) { + parseInstanceCommand(parameters, multipleParameters); + action(current.verb, parameters, multipleParameters); + } else + error(M_WHAT); + + if (fail) error(NO_MSG); + + lastWord = currentWordIndex - 1; + if (isConjunctionWord(lastWord)) + lastWord--; + + if (lengthOfParameterArray(parameters) > 0) + copyParameterArray(previousMultipleParameters, multipleParameters); + else + clearParameterArray(previousMultipleParameters); + + freeParameterArray(parameters); + parameters = NULL; + freeParameterArray(multipleParameters); + multipleParameters = NULL; } } // End of namespace Alan3 diff --git a/engines/glk/alan3/readline.cpp b/engines/glk/alan3/readline.cpp index 4fb7a8926a..2bd408f7cb 100644 --- a/engines/glk/alan3/readline.cpp +++ b/engines/glk/alan3/readline.cpp @@ -48,54 +48,52 @@ namespace Alan3 { */ /* TODO - length of user buffer should be used */ -bool readline(char buffer[]) -{ - event_t event; - static bool readingCommands = FALSE; - static frefid_t commandFileRef; - static strid_t commandFile; +bool readline(char buffer[]) { + event_t event; + static bool readingCommands = FALSE; + static frefid_t commandFileRef; + static strid_t commandFile; - if (readingCommands) { - if (g_vm->glk_get_line_stream(commandFile, buffer, 255) == 0) { + if (readingCommands) { + if (g_vm->glk_get_line_stream(commandFile, buffer, 255) == 0) { g_vm->glk_stream_close(commandFile, NULL); - readingCommands = FALSE; - } else { + readingCommands = FALSE; + } else { g_vm->glk_set_style(style_Input); - printf(buffer); + printf(buffer); g_vm->glk_set_style(style_Normal); - } - } else { + } + } else { g_vm->glk_request_line_event(glkMainWin, buffer, 255, 0); - /* FIXME: buffer size should be infallible: all existing calls use 256 or - 80 character buffers, except parse which uses LISTLEN (currently 100) - */ - do - { - g_vm->glk_select(&event); - switch (event.type) { - case evtype_Arrange: - statusline(); - break; + /* FIXME: buffer size should be infallible: all existing calls use 256 or + 80 character buffers, except parse which uses LISTLEN (currently 100) + */ + do { + g_vm->glk_select(&event); + switch (event.type) { + case evtype_Arrange: + statusline(); + break; - default: - break; - } - } while (event.type != evtype_LineInput); - if (buffer[0] == '@') { - buffer[event.val1] = 0; - commandFileRef = g_vm->glk_fileref_create_by_name(fileusage_InputRecord+fileusage_TextMode, &buffer[1], 0); - commandFile = g_vm->glk_stream_open_file(commandFileRef, filemode_Read, 0); - if (commandFile != NULL) - if (g_vm->glk_get_line_stream(commandFile, buffer, 255) != 0) { - readingCommands = TRUE; + default: + break; + } + } while (event.type != evtype_LineInput); + if (buffer[0] == '@') { + buffer[event.val1] = 0; + commandFileRef = g_vm->glk_fileref_create_by_name(fileusage_InputRecord + fileusage_TextMode, &buffer[1], 0); + commandFile = g_vm->glk_stream_open_file(commandFileRef, filemode_Read, 0); + if (commandFile != NULL) + if (g_vm->glk_get_line_stream(commandFile, buffer, 255) != 0) { + readingCommands = TRUE; g_vm->glk_set_style(style_Input); - printf(buffer); + printf(buffer); g_vm->glk_set_style(style_Normal); - } - } else - buffer[event.val1] = 0; - } - return TRUE; + } + } else + buffer[event.val1] = 0; + } + return TRUE; } } // End of namespace Alan3 diff --git a/engines/glk/alan3/resources.h b/engines/glk/alan3/resources.h index 837c137ac1..6977befc25 100644 --- a/engines/glk/alan3/resources.h +++ b/engines/glk/alan3/resources.h @@ -29,12 +29,12 @@ #define IDR_ABOUT 27000 #define IDD_ABOUT 26000 -#define ID_MENU_RESTART 26001 -#define ID_MENU_SAVE 26002 -#define ID_MENU_RECORD 26003 -#define ID_MENU_PLAYBACK 26004 -#define ID_MENU_TRANSCRIPT 26005 -#define ID_MENU_RESTORE 26006 +#define ID_MENU_RESTART 26001 +#define ID_MENU_SAVE 26002 +#define ID_MENU_RECORD 26003 +#define ID_MENU_PLAYBACK 26004 +#define ID_MENU_TRANSCRIPT 26005 +#define ID_MENU_RESTORE 26006 #define ID_MENU_ABOUT 26007 #endif diff --git a/engines/glk/alan3/reverse.cpp b/engines/glk/alan3/reverse.cpp index 2a0cd427ad..ca6db949af 100644 --- a/engines/glk/alan3/reverse.cpp +++ b/engines/glk/alan3/reverse.cpp @@ -41,613 +41,585 @@ static Aword *addressesDone = NULL; static int numberDone = 0; static int doneSize = 0; -static bool alreadyDone(Aaddr address) -{ - int i; +static bool alreadyDone(Aaddr address) { + int i; - if (address == 0) return TRUE; + if (address == 0) return TRUE; - /* Have we already done it? */ - for (i = 0; i < numberDone; i++) - if (addressesDone[i] == address) - return TRUE; + /* Have we already done it? */ + for (i = 0; i < numberDone; i++) + if (addressesDone[i] == address) + return TRUE; - if (doneSize == numberDone) { - doneSize += 100; - addressesDone = (Aword *)realloc(addressesDone, doneSize*sizeof(Aword)); - } - addressesDone[numberDone] = address; - numberDone++; + if (doneSize == numberDone) { + doneSize += 100; + addressesDone = (Aword *)realloc(addressesDone, doneSize * sizeof(Aword)); + } + addressesDone[numberDone] = address; + numberDone++; - return FALSE; + return FALSE; } #define NATIVE(w) \ - ( (((Aword)((w)[3]) ) & 0x000000ff) \ - | (((Aword)((w)[2]) << 8) & 0x0000ff00) \ - | (((Aword)((w)[1]) << 16) & 0x00ff0000) \ - | (((Aword)((w)[0]) << 24) & 0xff000000)) + ( (((Aword)((w)[3]) ) & 0x000000ff) \ + | (((Aword)((w)[2]) << 8) & 0x0000ff00) \ + | (((Aword)((w)[1]) << 16) & 0x00ff0000) \ + | (((Aword)((w)[0]) << 24) & 0xff000000)) /*----------------------------------------------------------------------*/ -Aword reversed(Aword w) /* IN - The ACODE word to swap bytes of */ -{ +Aword reversed(Aword w) { /* IN - The ACODE word to swap bytes of */ #ifdef TRYNATIVE - return NATIVE(&w); + return NATIVE(&w); #else - Aword s; /* The swapped ACODE word */ - char *wp, *sp; - uint i; + Aword s; /* The swapped ACODE word */ + char *wp, *sp; + uint i; - wp = (char *) &w; - sp = (char *) &s; + wp = (char *) &w; + sp = (char *) &s; - for (i = 0; i < sizeof(Aword); i++) - sp[sizeof(Aword)-1 - i] = wp[i]; + for (i = 0; i < sizeof(Aword); i++) + sp[sizeof(Aword) - 1 - i] = wp[i]; - return s; + return s; #endif } -void reverseWord(Aword *w) /* IN - The ACODE word to reverse bytes in */ -{ - *w = reversed(*w); +void reverseWord(Aword *w) { /* IN - The ACODE word to reverse bytes in */ + *w = reversed(*w); } -void reverse(Aword *w) /* IN - The ACODE word to reverse bytes in */ -{ - if (w < &memory[0] || w > &memory[memorySize]) - syserr("Reversing address outside of memory"); - reverseWord(w); +void reverse(Aword *w) { /* IN - The ACODE word to reverse bytes in */ + if (w < &memory[0] || w > &memory[memorySize]) + syserr("Reversing address outside of memory"); + reverseWord(w); } -static void reverseTable(Aword adr, int elementSize) -{ - Aword *e = &memory[adr]; - uint i; +static void reverseTable(Aword adr, int elementSize) { + Aword *e = &memory[adr]; + uint i; - if (elementSize < (int)sizeof(Aword) || elementSize % (int)sizeof(Aword) != 0) - syserr("***Wrong size in 'reverseTable()' ***"); + if (elementSize < (int)sizeof(Aword) || elementSize % (int)sizeof(Aword) != 0) + syserr("***Wrong size in 'reverseTable()' ***"); - if (adr == 0) return; + if (adr == 0) return; - while (!isEndOfArray(e)) { - for (i = 0; i < elementSize/sizeof(Aword); i++) { - reverse(e); - e++; - } - } + while (!isEndOfArray(e)) { + for (i = 0; i < elementSize / sizeof(Aword); i++) { + reverse(e); + e++; + } + } } -static void reverseStms(Aword adr) -{ - Aword *e = &memory[adr]; +static void reverseStms(Aword adr) { + Aword *e = &memory[adr]; - if (!adr || alreadyDone(adr)) return; + if (!adr || alreadyDone(adr)) return; - while (TRUE) { - reverse(e); - if (*e == ((Aword)C_STMOP<<28|(Aword)I_RETURN)) break; - e++; - } + while (TRUE) { + reverse(e); + if (*e == ((Aword)C_STMOP << 28 | (Aword)I_RETURN)) break; + e++; + } } -static void reverseMsgs(Aword adr) -{ - MessageEntry *e = (MessageEntry *) &memory[adr]; +static void reverseMsgs(Aword adr) { + MessageEntry *e = (MessageEntry *) &memory[adr]; - if (!adr || alreadyDone(adr)) return; + if (!adr || alreadyDone(adr)) return; - if (!isEndOfArray(e)) { - reverseTable(adr, sizeof(MessageEntry)); - while (!isEndOfArray(e)) { - reverseStms(e->stms); - e++; - } - } + if (!isEndOfArray(e)) { + reverseTable(adr, sizeof(MessageEntry)); + while (!isEndOfArray(e)) { + reverseStms(e->stms); + e++; + } + } } -static void reverseDictionary(Aword adr) -{ - DictionaryEntry *e = (DictionaryEntry *) &memory[adr]; +static void reverseDictionary(Aword adr) { + DictionaryEntry *e = (DictionaryEntry *) &memory[adr]; - if (!adr || alreadyDone(adr)) return; + if (!adr || alreadyDone(adr)) return; - if (!isEndOfArray(e)) { - reverseTable(adr, sizeof(DictionaryEntry)); - while (!isEndOfArray(e)) { - if ((e->classBits & SYNONYM_BIT) == 0) { /* Do not do this for synonyms */ - reverseTable(e->adjectiveRefs, sizeof(Aword)); - reverseTable(e->nounRefs, sizeof(Aword)); - reverseTable(e->pronounRefs, sizeof(Aword)); - } - e++; - } - } + if (!isEndOfArray(e)) { + reverseTable(adr, sizeof(DictionaryEntry)); + while (!isEndOfArray(e)) { + if ((e->classBits & SYNONYM_BIT) == 0) { /* Do not do this for synonyms */ + reverseTable(e->adjectiveRefs, sizeof(Aword)); + reverseTable(e->nounRefs, sizeof(Aword)); + reverseTable(e->pronounRefs, sizeof(Aword)); + } + e++; + } + } } -static void reverseChks(Aword adr) -{ - CheckEntry *e = (CheckEntry *) &memory[adr]; +static void reverseChks(Aword adr) { + CheckEntry *e = (CheckEntry *) &memory[adr]; - if (!adr || alreadyDone(adr)) return; + if (!adr || alreadyDone(adr)) return; - if (!isEndOfArray(e)) { - reverseTable(adr, sizeof(CheckEntry)); - while (!isEndOfArray(e)) { - reverseStms(e->exp); - reverseStms(e->stms); - e++; - } - } + if (!isEndOfArray(e)) { + reverseTable(adr, sizeof(CheckEntry)); + while (!isEndOfArray(e)) { + reverseStms(e->exp); + reverseStms(e->stms); + e++; + } + } } -static void reverseAlts(Aword adr) -{ - AltEntry *e = (AltEntry *)&memory[adr]; +static void reverseAlts(Aword adr) { + AltEntry *e = (AltEntry *)&memory[adr]; - if (!adr || alreadyDone(adr)) return; + if (!adr || alreadyDone(adr)) return; - if (!isEndOfArray(e)) { - reverseTable(adr, sizeof(AltEntry)); - while (!isEndOfArray(e)) { - reverseChks(e->checks); - reverseStms(e->action); - e++; - } - } + if (!isEndOfArray(e)) { + reverseTable(adr, sizeof(AltEntry)); + while (!isEndOfArray(e)) { + reverseChks(e->checks); + reverseStms(e->action); + e++; + } + } } -static void reverseVerbs(Aword adr) -{ - VerbEntry *e = (VerbEntry *)&memory[adr]; +static void reverseVerbs(Aword adr) { + VerbEntry *e = (VerbEntry *)&memory[adr]; - if (!adr || alreadyDone(adr)) return; + if (!adr || alreadyDone(adr)) return; - if (!isEndOfArray(e)) { - reverseTable(adr, sizeof(VerbEntry)); - while (!isEndOfArray(e)) { - reverseAlts(e->alts); - e++; - } - } + if (!isEndOfArray(e)) { + reverseTable(adr, sizeof(VerbEntry)); + while (!isEndOfArray(e)) { + reverseAlts(e->alts); + e++; + } + } } -static void reverseSteps(Aword adr) -{ - StepEntry *e = (StepEntry *) &memory[adr]; +static void reverseSteps(Aword adr) { + StepEntry *e = (StepEntry *) &memory[adr]; - if (!adr || alreadyDone(adr)) return; + if (!adr || alreadyDone(adr)) return; - if (!isEndOfArray(e)) { - reverseTable(adr, sizeof(StepEntry)); - while (!isEndOfArray(e)) { - reverseStms(e->after); - reverseStms(e->exp); - reverseStms(e->stms); - e++; - } - } + if (!isEndOfArray(e)) { + reverseTable(adr, sizeof(StepEntry)); + while (!isEndOfArray(e)) { + reverseStms(e->after); + reverseStms(e->exp); + reverseStms(e->stms); + e++; + } + } } -static void reverseScrs(Aword adr) -{ - ScriptEntry *e = (ScriptEntry *) &memory[adr]; +static void reverseScrs(Aword adr) { + ScriptEntry *e = (ScriptEntry *) &memory[adr]; - if (!adr || alreadyDone(adr)) return; + if (!adr || alreadyDone(adr)) return; - if (!isEndOfArray(e)) { - reverseTable(adr, sizeof(ScriptEntry)); - while (!isEndOfArray(e)) { - reverseStms(e->description); - reverseSteps(e->steps); - e++; - } - } + if (!isEndOfArray(e)) { + reverseTable(adr, sizeof(ScriptEntry)); + while (!isEndOfArray(e)) { + reverseStms(e->description); + reverseSteps(e->steps); + e++; + } + } } -static void reverseExits(Aword adr) -{ - ExitEntry *e = (ExitEntry *) &memory[adr]; +static void reverseExits(Aword adr) { + ExitEntry *e = (ExitEntry *) &memory[adr]; - if (!adr || alreadyDone(adr)) return; + if (!adr || alreadyDone(adr)) return; - if (!isEndOfArray(e)) { - reverseTable(adr, sizeof(ExitEntry)); - while (!isEndOfArray(e)) { - reverseChks(e->checks); - reverseStms(e->action); - e++; - } - } + if (!isEndOfArray(e)) { + reverseTable(adr, sizeof(ExitEntry)); + while (!isEndOfArray(e)) { + reverseChks(e->checks); + reverseStms(e->action); + e++; + } + } } -static void reverseClasses(Aword adr) -{ - ClassEntry *e = (ClassEntry *) &memory[adr]; +static void reverseClasses(Aword adr) { + ClassEntry *e = (ClassEntry *) &memory[adr]; - if (!adr || alreadyDone(adr)) return; + if (!adr || alreadyDone(adr)) return; - if (!isEndOfArray(e)) { - reverseTable(adr, sizeof(ClassEntry)); - while (!isEndOfArray(e)) { - reverseStms(e->name); - reverseStms(e->initialize); - reverseChks(e->descriptionChecks); - reverseStms(e->description); - reverseStms(e->entered); - reverseStms(e->definite.address); - reverseStms(e->indefinite.address); - reverseStms(e->negative.address); - reverseStms(e->mentioned); - reverseVerbs(e->verbs); - e++; - } - } + if (!isEndOfArray(e)) { + reverseTable(adr, sizeof(ClassEntry)); + while (!isEndOfArray(e)) { + reverseStms(e->name); + reverseStms(e->initialize); + reverseChks(e->descriptionChecks); + reverseStms(e->description); + reverseStms(e->entered); + reverseStms(e->definite.address); + reverseStms(e->indefinite.address); + reverseStms(e->negative.address); + reverseStms(e->mentioned); + reverseVerbs(e->verbs); + e++; + } + } } -static void reverseInstances(Aword adr) -{ - InstanceEntry *e = (InstanceEntry *) &memory[adr]; +static void reverseInstances(Aword adr) { + InstanceEntry *e = (InstanceEntry *) &memory[adr]; - if (!adr || alreadyDone(adr)) return; + if (!adr || alreadyDone(adr)) return; - if (!isEndOfArray(e)) { - reverseTable(adr, sizeof(InstanceEntry)); - while (!isEndOfArray(e)) { - reverseStms(e->name); - reverseTable(e->initialAttributes, sizeof(AttributeHeaderEntry)); - reverseStms(e->initialize); - reverseStms(e->definite.address); - reverseStms(e->indefinite.address); - reverseStms(e->negative.address); - reverseStms(e->mentioned); - reverseChks(e->checks); - reverseStms(e->description); - reverseVerbs(e->verbs); - reverseStms(e->entered); - reverseExits(e->exits); - e++; - } - } + if (!isEndOfArray(e)) { + reverseTable(adr, sizeof(InstanceEntry)); + while (!isEndOfArray(e)) { + reverseStms(e->name); + reverseTable(e->initialAttributes, sizeof(AttributeHeaderEntry)); + reverseStms(e->initialize); + reverseStms(e->definite.address); + reverseStms(e->indefinite.address); + reverseStms(e->negative.address); + reverseStms(e->mentioned); + reverseChks(e->checks); + reverseStms(e->description); + reverseVerbs(e->verbs); + reverseStms(e->entered); + reverseExits(e->exits); + e++; + } + } } -static void reverseRestrictions(Aword adr) -{ - RestrictionEntry *e = (RestrictionEntry *) &memory[adr]; +static void reverseRestrictions(Aword adr) { + RestrictionEntry *e = (RestrictionEntry *) &memory[adr]; - if (!adr || alreadyDone(adr)) return; - if (!isEndOfArray(e)) { - reverseTable(adr, sizeof(RestrictionEntry)); - while (!isEndOfArray(e)) { - reverseStms(e->stms); - e++; - } - } + if (!adr || alreadyDone(adr)) return; + if (!isEndOfArray(e)) { + reverseTable(adr, sizeof(RestrictionEntry)); + while (!isEndOfArray(e)) { + reverseStms(e->stms); + e++; + } + } } -static void reverseElms(Aword adr) -{ - ElementEntry *e = (ElementEntry *) &memory[adr]; +static void reverseElms(Aword adr) { + ElementEntry *e = (ElementEntry *) &memory[adr]; - if (!adr || alreadyDone(adr)) return; - - if (!isEndOfArray(e)) { - reverseTable(adr, sizeof(ElementEntry)); - while (!isEndOfArray(e)) { - if ((uint)e->code == EOS) reverseRestrictions(e->next); - else reverseElms(e->next); - e++; - } - } + if (!adr || alreadyDone(adr)) return; + + if (!isEndOfArray(e)) { + reverseTable(adr, sizeof(ElementEntry)); + while (!isEndOfArray(e)) { + if ((uint)e->code == EOS) reverseRestrictions(e->next); + else reverseElms(e->next); + e++; + } + } } static void reverseSyntaxTableCurrent(Aword adr) { - SyntaxEntry *e = (SyntaxEntry *) &memory[adr]; + SyntaxEntry *e = (SyntaxEntry *) &memory[adr]; - if (!isEndOfArray(e)) { - reverseTable(adr, sizeof(SyntaxEntry)); - while (!isEndOfArray(e)) { - reverseElms(e->elms); - reverseTable(e->parameterNameTable, sizeof(Aaddr)); - e++; - } - } + if (!isEndOfArray(e)) { + reverseTable(adr, sizeof(SyntaxEntry)); + while (!isEndOfArray(e)) { + reverseElms(e->elms); + reverseTable(e->parameterNameTable, sizeof(Aaddr)); + e++; + } + } } static void reverseSyntaxTablePreBeta2(Aword adr) { - SyntaxEntryPreBeta2 *e = (SyntaxEntryPreBeta2 *) &memory[adr]; + SyntaxEntryPreBeta2 *e = (SyntaxEntryPreBeta2 *) &memory[adr]; - if (!isEndOfArray(e)) { - reverseTable(adr, sizeof(SyntaxEntryPreBeta2)); - while (!isEndOfArray(e)) { - reverseElms(e->elms); - e++; - } - } + if (!isEndOfArray(e)) { + reverseTable(adr, sizeof(SyntaxEntryPreBeta2)); + while (!isEndOfArray(e)) { + reverseElms(e->elms); + e++; + } + } } -static void reverseSyntaxTable(Aword adr, char version[]) -{ - if (!adr || alreadyDone(adr)) return; +static void reverseSyntaxTable(Aword adr, char version[]) { + if (!adr || alreadyDone(adr)) return; - if (isPreBeta2(version)) - reverseSyntaxTablePreBeta2(adr); - else - reverseSyntaxTableCurrent(adr); + if (isPreBeta2(version)) + reverseSyntaxTablePreBeta2(adr); + else + reverseSyntaxTableCurrent(adr); } static void reverseParameterNames(Aaddr parameterMapAddress) { - Aaddr *e; - Aaddr adr; - - adr = addressAfterTable(parameterMapAddress, sizeof(ParameterMapEntry)); - reverse(&memory[adr]); - adr = memory[adr]; - - reverseTable(adr, sizeof(Aaddr)); - - e = (Aaddr*) &memory[adr]; - while (!isEndOfArray(e)) { - reverseTable(*e, sizeof(Aaddr)); - e++; - } + Aaddr *e; + Aaddr adr; + + adr = addressAfterTable(parameterMapAddress, sizeof(ParameterMapEntry)); + reverse(&memory[adr]); + adr = memory[adr]; + + reverseTable(adr, sizeof(Aaddr)); + + e = (Aaddr *) &memory[adr]; + while (!isEndOfArray(e)) { + reverseTable(*e, sizeof(Aaddr)); + e++; + } } -static void reverseParameterTable(Aword adr) -{ - ParameterMapEntry *e = (ParameterMapEntry *) &memory[adr]; +static void reverseParameterTable(Aword adr) { + ParameterMapEntry *e = (ParameterMapEntry *) &memory[adr]; - if (!adr || alreadyDone(adr)) return; + if (!adr || alreadyDone(adr)) return; - if (!isEndOfArray(e)) { - reverseTable(adr, sizeof(ParameterMapEntry)); - while (!isEndOfArray(e)) { - reverseTable(e->parameterMapping, sizeof(Aword)); - e++; - } - } + if (!isEndOfArray(e)) { + reverseTable(adr, sizeof(ParameterMapEntry)); + while (!isEndOfArray(e)) { + reverseTable(e->parameterMapping, sizeof(Aword)); + e++; + } + } } -static void reverseEvts(Aword adr) -{ - EventEntry *e = (EventEntry *) &memory[adr]; +static void reverseEvts(Aword adr) { + EventEntry *e = (EventEntry *) &memory[adr]; - if (!adr || alreadyDone(adr)) return; + if (!adr || alreadyDone(adr)) return; - if (!isEndOfArray(e)) { - reverseTable(adr, sizeof(EventEntry)); - while (!isEndOfArray(e)) { - reverseStms(e->code); - e++; - } - } + if (!isEndOfArray(e)) { + reverseTable(adr, sizeof(EventEntry)); + while (!isEndOfArray(e)) { + reverseStms(e->code); + e++; + } + } } -static void reverseLims(Aword adr) -{ - LimitEntry *e = (LimitEntry *) &memory[adr]; +static void reverseLims(Aword adr) { + LimitEntry *e = (LimitEntry *) &memory[adr]; - if (!adr || alreadyDone(adr)) return; + if (!adr || alreadyDone(adr)) return; - if (!isEndOfArray(e)) { - reverseTable(adr, sizeof(LimitEntry)); - while (!isEndOfArray(e)) { - reverseStms(e->stms); - e++; - } - } + if (!isEndOfArray(e)) { + reverseTable(adr, sizeof(LimitEntry)); + while (!isEndOfArray(e)) { + reverseStms(e->stms); + e++; + } + } } -static void reverseContainers(Aword adr) -{ - ContainerEntry *e = (ContainerEntry *) &memory[adr]; +static void reverseContainers(Aword adr) { + ContainerEntry *e = (ContainerEntry *) &memory[adr]; - if (!adr || alreadyDone(adr)) return; + if (!adr || alreadyDone(adr)) return; - if (!isEndOfArray(e)) { - reverseTable(adr, sizeof(ContainerEntry)); - while (!isEndOfArray(e)) { - reverseLims(e->limits); - reverseStms(e->header); - reverseStms(e->empty); - reverseChks(e->extractChecks); - reverseStms(e->extractStatements); - e++; - } - } + if (!isEndOfArray(e)) { + reverseTable(adr, sizeof(ContainerEntry)); + while (!isEndOfArray(e)) { + reverseLims(e->limits); + reverseStms(e->header); + reverseStms(e->empty); + reverseChks(e->extractChecks); + reverseStms(e->extractStatements); + e++; + } + } } -static void reverseRuls(Aword adr) -{ - RuleEntry *e = (RuleEntry *) &memory[adr]; +static void reverseRuls(Aword adr) { + RuleEntry *e = (RuleEntry *) &memory[adr]; - if (!adr || alreadyDone(adr)) return; + if (!adr || alreadyDone(adr)) return; - if (!isEndOfArray(e)) { - reverseTable(adr, sizeof(RuleEntry)); - while (!isEndOfArray(e)) { - reverseStms(e->exp); - reverseStms(e->stms); - e++; - } - } + if (!isEndOfArray(e)) { + reverseTable(adr, sizeof(RuleEntry)); + while (!isEndOfArray(e)) { + reverseStms(e->exp); + reverseStms(e->stms); + e++; + } + } } -static void reverseSetInitTable(Aaddr adr) -{ - SetInitEntry *e = (SetInitEntry *)&memory[adr]; +static void reverseSetInitTable(Aaddr adr) { + SetInitEntry *e = (SetInitEntry *)&memory[adr]; - if (!adr || alreadyDone(adr)) return; + if (!adr || alreadyDone(adr)) return; - if (!isEndOfArray(e)) { - reverseTable(adr, sizeof(SetInitEntry)); - while (!isEndOfArray(e)) { - reverseTable(e->setAddress, sizeof(Aword)); - e++; - } - } + if (!isEndOfArray(e)) { + reverseTable(adr, sizeof(SetInitEntry)); + while (!isEndOfArray(e)) { + reverseTable(e->setAddress, sizeof(Aword)); + e++; + } + } } /*----------------------------------------------------------------------*/ -static void reversePreAlpha5Header(Pre3_0alpha5Header *hdr) -{ - uint i; +static void reversePreAlpha5Header(Pre3_0alpha5Header *hdr) { + uint i; - /* Reverse all words in the header except the tag */ - for (i = 1; i < sizeof(*hdr)/sizeof(Aword); i++) - reverseWord(&((Aword *)hdr)[i]); + /* Reverse all words in the header except the tag */ + for (i = 1; i < sizeof(*hdr) / sizeof(Aword); i++) + reverseWord(&((Aword *)hdr)[i]); } /*----------------------------------------------------------------------*/ static void reversePreAlpha5() { - /* NOTE that the reversePreXXX() have different header definitions */ - Pre3_0alpha5Header *hdr = (Pre3_0alpha5Header *)memory; - - reversePreAlpha5Header(hdr); - memorySize = hdr->size; - - reverseDictionary(hdr->dictionary); - reverseSyntaxTable(hdr->syntaxTableAddress, hdr->version); - reverseParameterTable(hdr->parameterMapAddress); - reverseVerbs(hdr->verbTableAddress); - reverseClasses(hdr->classTableAddress); - reverseInstances(hdr->instanceTableAddress); - reverseScrs(hdr->scriptTableAddress); - reverseContainers(hdr->containerTableAddress); - reverseEvts(hdr->eventTableAddress); - reverseRuls(hdr->ruleTableAddress); - reverseTable(hdr->stringInitTable, sizeof(StringInitEntry)); - reverseSetInitTable(hdr->setInitTable); - reverseTable(hdr->sourceFileTable, sizeof(SourceFileEntry)); - reverseTable(hdr->sourceLineTable, sizeof(SourceLineEntry)); - reverseStms(hdr->start); - reverseMsgs(hdr->messageTableAddress); - - reverseTable(hdr->scores, sizeof(Aword)); - reverseTable(hdr->freq, sizeof(Aword)); + /* NOTE that the reversePreXXX() have different header definitions */ + Pre3_0alpha5Header *hdr = (Pre3_0alpha5Header *)memory; + + reversePreAlpha5Header(hdr); + memorySize = hdr->size; + + reverseDictionary(hdr->dictionary); + reverseSyntaxTable(hdr->syntaxTableAddress, hdr->version); + reverseParameterTable(hdr->parameterMapAddress); + reverseVerbs(hdr->verbTableAddress); + reverseClasses(hdr->classTableAddress); + reverseInstances(hdr->instanceTableAddress); + reverseScrs(hdr->scriptTableAddress); + reverseContainers(hdr->containerTableAddress); + reverseEvts(hdr->eventTableAddress); + reverseRuls(hdr->ruleTableAddress); + reverseTable(hdr->stringInitTable, sizeof(StringInitEntry)); + reverseSetInitTable(hdr->setInitTable); + reverseTable(hdr->sourceFileTable, sizeof(SourceFileEntry)); + reverseTable(hdr->sourceLineTable, sizeof(SourceLineEntry)); + reverseStms(hdr->start); + reverseMsgs(hdr->messageTableAddress); + + reverseTable(hdr->scores, sizeof(Aword)); + reverseTable(hdr->freq, sizeof(Aword)); } /*----------------------------------------------------------------------*/ -static void reversePreBeta2Header(Pre3_0beta2Header *hdr) -{ - uint i; +static void reversePreBeta2Header(Pre3_0beta2Header *hdr) { + uint i; - /* Reverse all words in the header except the tag */ - for (i = 1; i < sizeof(*hdr)/sizeof(Aword); i++) - reverseWord(&((Aword *)hdr)[i]); + /* Reverse all words in the header except the tag */ + for (i = 1; i < sizeof(*hdr) / sizeof(Aword); i++) + reverseWord(&((Aword *)hdr)[i]); } /*----------------------------------------------------------------------*/ static void reversePreBeta2() { - /* NOTE that the reversePreXXX() have different header definitions */ - Pre3_0beta2Header *hdr = (Pre3_0beta2Header *)memory; - - reversePreBeta2Header(hdr); - memorySize = hdr->size; - - reverseDictionary(hdr->dictionary); - reverseSyntaxTable(hdr->syntaxTableAddress, hdr->version); - reverseParameterTable(hdr->parameterMapAddress); - reverseVerbs(hdr->verbTableAddress); - reverseClasses(hdr->classTableAddress); - reverseInstances(hdr->instanceTableAddress); - reverseScrs(hdr->scriptTableAddress); - reverseContainers(hdr->containerTableAddress); - reverseEvts(hdr->eventTableAddress); - reverseRuls(hdr->ruleTableAddress); - reverseTable(hdr->stringInitTable, sizeof(StringInitEntry)); - reverseSetInitTable(hdr->setInitTable); - reverseTable(hdr->sourceFileTable, sizeof(SourceFileEntry)); - reverseTable(hdr->sourceLineTable, sizeof(SourceLineEntry)); - reverseStms(hdr->start); - reverseMsgs(hdr->messageTableAddress); - - reverseTable(hdr->scores, sizeof(Aword)); - reverseTable(hdr->freq, sizeof(Aword)); + /* NOTE that the reversePreXXX() have different header definitions */ + Pre3_0beta2Header *hdr = (Pre3_0beta2Header *)memory; + + reversePreBeta2Header(hdr); + memorySize = hdr->size; + + reverseDictionary(hdr->dictionary); + reverseSyntaxTable(hdr->syntaxTableAddress, hdr->version); + reverseParameterTable(hdr->parameterMapAddress); + reverseVerbs(hdr->verbTableAddress); + reverseClasses(hdr->classTableAddress); + reverseInstances(hdr->instanceTableAddress); + reverseScrs(hdr->scriptTableAddress); + reverseContainers(hdr->containerTableAddress); + reverseEvts(hdr->eventTableAddress); + reverseRuls(hdr->ruleTableAddress); + reverseTable(hdr->stringInitTable, sizeof(StringInitEntry)); + reverseSetInitTable(hdr->setInitTable); + reverseTable(hdr->sourceFileTable, sizeof(SourceFileEntry)); + reverseTable(hdr->sourceLineTable, sizeof(SourceLineEntry)); + reverseStms(hdr->start); + reverseMsgs(hdr->messageTableAddress); + + reverseTable(hdr->scores, sizeof(Aword)); + reverseTable(hdr->freq, sizeof(Aword)); } /*======================================================================*/ -void reverseHdr(ACodeHeader *hdr) -{ - uint i; +void reverseHdr(ACodeHeader *hdr) { + uint i; - /* Reverse all words in the header except the tag and the version marking */ - for (i = 1; i < sizeof(*hdr)/sizeof(Aword); i++) - reverseWord(&((Aword *)hdr)[i]); + /* Reverse all words in the header except the tag and the version marking */ + for (i = 1; i < sizeof(*hdr) / sizeof(Aword); i++) + reverseWord(&((Aword *)hdr)[i]); } /*----------------------------------------------------------------------*/ static void reverseInstanceIdTable(ACodeHeader *hdr) { - reverseTable(hdr->instanceTableAddress+hdr->instanceMax*sizeof(InstanceEntry)/sizeof(Aword)+1, sizeof(Aword)); + reverseTable(hdr->instanceTableAddress + hdr->instanceMax * sizeof(InstanceEntry) / sizeof(Aword) + 1, sizeof(Aword)); } /*----------------------------------------------------------------------*/ static void reverseNative() { - /* NOTE that the reversePreXXX() have different hdr definitions */ - ACodeHeader *hdr = (ACodeHeader *)memory; - - reverseHdr(hdr); - memorySize = hdr->size; - - reverseDictionary(hdr->dictionary); - reverseSyntaxTable(hdr->syntaxTableAddress, hdr->version); - if (hdr->debug && !isPreBeta3(hdr->version)) - reverseParameterNames(hdr->parameterMapAddress); - reverseParameterTable(hdr->parameterMapAddress); - reverseVerbs(hdr->verbTableAddress); - reverseClasses(hdr->classTableAddress); - reverseInstances(hdr->instanceTableAddress); - if (hdr->debug && !isPreBeta3(hdr->version)) - reverseInstanceIdTable(hdr); - reverseScrs(hdr->scriptTableAddress); - reverseContainers(hdr->containerTableAddress); - reverseEvts(hdr->eventTableAddress); - reverseRuls(hdr->ruleTableAddress); - reverseTable(hdr->stringInitTable, sizeof(StringInitEntry)); - reverseSetInitTable(hdr->setInitTable); - reverseTable(hdr->sourceFileTable, sizeof(SourceFileEntry)); - reverseTable(hdr->sourceLineTable, sizeof(SourceLineEntry)); - reverseStms(hdr->prompt); - reverseStms(hdr->start); - reverseMsgs(hdr->messageTableAddress); - - reverseTable(hdr->scores, sizeof(Aword)); - reverseTable(hdr->freq, sizeof(Aword)); + /* NOTE that the reversePreXXX() have different hdr definitions */ + ACodeHeader *hdr = (ACodeHeader *)memory; + + reverseHdr(hdr); + memorySize = hdr->size; + + reverseDictionary(hdr->dictionary); + reverseSyntaxTable(hdr->syntaxTableAddress, hdr->version); + if (hdr->debug && !isPreBeta3(hdr->version)) + reverseParameterNames(hdr->parameterMapAddress); + reverseParameterTable(hdr->parameterMapAddress); + reverseVerbs(hdr->verbTableAddress); + reverseClasses(hdr->classTableAddress); + reverseInstances(hdr->instanceTableAddress); + if (hdr->debug && !isPreBeta3(hdr->version)) + reverseInstanceIdTable(hdr); + reverseScrs(hdr->scriptTableAddress); + reverseContainers(hdr->containerTableAddress); + reverseEvts(hdr->eventTableAddress); + reverseRuls(hdr->ruleTableAddress); + reverseTable(hdr->stringInitTable, sizeof(StringInitEntry)); + reverseSetInitTable(hdr->setInitTable); + reverseTable(hdr->sourceFileTable, sizeof(SourceFileEntry)); + reverseTable(hdr->sourceLineTable, sizeof(SourceLineEntry)); + reverseStms(hdr->prompt); + reverseStms(hdr->start); + reverseMsgs(hdr->messageTableAddress); + + reverseTable(hdr->scores, sizeof(Aword)); + reverseTable(hdr->freq, sizeof(Aword)); } @@ -660,25 +632,24 @@ static void reverseNative() { makes the .ACD files fully compatible across architectures */ -void reverseACD(void) -{ - ACodeHeader *hdr = (ACodeHeader *)memory; - char version[4]; - int i; - - /* Make a copy of the version marking to reverse */ - for (i = 0; i <= 3; i++) - version[i] = hdr->version[i]; - reverseWord((Aword*)&version); - - if (isPreAlpha5(version)) - reversePreAlpha5(); - else if (isPreBeta2(version)) - reversePreBeta2(); - else - reverseNative(); - - free(addressesDone); +void reverseACD(void) { + ACodeHeader *hdr = (ACodeHeader *)memory; + char version[4]; + int i; + + /* Make a copy of the version marking to reverse */ + for (i = 0; i <= 3; i++) + version[i] = hdr->version[i]; + reverseWord((Aword *)&version); + + if (isPreAlpha5(version)) + reversePreAlpha5(); + else if (isPreBeta2(version)) + reversePreBeta2(); + else + reverseNative(); + + free(addressesDone); } } // End of namespace Alan3 diff --git a/engines/glk/alan3/rules.cpp b/engines/glk/alan3/rules.cpp index 96dd33daf0..53b922061c 100644 --- a/engines/glk/alan3/rules.cpp +++ b/engines/glk/alan3/rules.cpp @@ -43,8 +43,8 @@ bool anyRuleRun; /* PRIVATE TYPES: */ typedef struct RulesAdmin { - bool lastEval; - bool alreadyRun; + bool lastEval; + bool alreadyRun; } RulesAdmin; /* PRIVATE DATA: */ @@ -53,207 +53,205 @@ static RulesAdmin *rulesAdmin; /* Table for administration of the rules */ /*----------------------------------------------------------------------*/ static void clearRulesAdmin(int numRules) { - int r; - for (r = 0; r < numRules; r++) { - rulesAdmin[r].lastEval = FALSE; - rulesAdmin[r].alreadyRun = FALSE; - } + int r; + for (r = 0; r < numRules; r++) { + rulesAdmin[r].lastEval = FALSE; + rulesAdmin[r].alreadyRun = FALSE; + } } /*----------------------------------------------------------------------*/ static void initRulesAdmin(int numRules) { - int r; + int r; - rulesAdmin = (RulesAdmin *)allocate(numRules*sizeof(RulesAdmin)+sizeof(EOD)); - for (r = 0; r < numRules; r++) - ; - setEndOfArray(&rulesAdmin[r]); + rulesAdmin = (RulesAdmin *)allocate(numRules * sizeof(RulesAdmin) + sizeof(EOD)); + for (r = 0; r < numRules; r++) + ; + setEndOfArray(&rulesAdmin[r]); } /*======================================================================*/ void initRules(Aaddr ruleTableAddress) { - rules = (RuleEntry *) pointerTo(ruleTableAddress); + rules = (RuleEntry *) pointerTo(ruleTableAddress); - if (ruleCount == 0) { /* Not initiated */ - for (ruleCount = 0; !isEndOfArray(&rules[ruleCount]); ruleCount++) - ; - initRulesAdmin(ruleCount); - } - clearRulesAdmin(ruleCount); + if (ruleCount == 0) { /* Not initiated */ + for (ruleCount = 0; !isEndOfArray(&rules[ruleCount]); ruleCount++) + ; + initRulesAdmin(ruleCount); + } + clearRulesAdmin(ruleCount); } /*----------------------------------------------------------------------*/ static void traceRuleStart(int rule, char *what) { - printf("\n<RULE %d", rule); - if (current.location != 0) { - printf(" (at "); - traceSay(current.location); - } else - printf(" (nowhere"); - printf("[%d]), %s", current.location, what); + printf("\n<RULE %d", rule); + if (current.location != 0) { + printf(" (at "); + traceSay(current.location); + } else + printf(" (nowhere"); + printf("[%d]), %s", current.location, what); } static bool detailedTraceOn() { - return traceInstructionOption || traceSourceOption || tracePushOption || traceStackOption; + return traceInstructionOption || traceSourceOption || tracePushOption || traceStackOption; } /*----------------------------------------------------------------------*/ static void traceRuleEvaluation(int rule) { - if (traceSectionOption) { - if (detailedTraceOn()) { - traceRuleStart(rule, "Evaluating:>"); - if (!traceInstructionOption) - printf("\n"); - } else { - traceRuleStart(rule, "Evaluating to "); - } - } + if (traceSectionOption) { + if (detailedTraceOn()) { + traceRuleStart(rule, "Evaluating:>"); + if (!traceInstructionOption) + printf("\n"); + } else { + traceRuleStart(rule, "Evaluating to "); + } + } } /*----------------------------------------------------------------------*/ static void traceRuleResult(int rule, bool result) { - if (traceSectionOption) { - if (detailedTraceOn()) - printf("<RULE %d %s%s", rule, "Evaluated to ", result?": true>\n":": false>\n"); - else - printf(result?"true":"false"); - } + if (traceSectionOption) { + if (detailedTraceOn()) + printf("<RULE %d %s%s", rule, "Evaluated to ", result ? ": true>\n" : ": false>\n"); + else + printf(result ? "true" : "false"); + } } /*----------------------------------------------------------------------*/ static void traceRuleExecution(int rule) { - if (traceSectionOption) { - if (!traceInstructionOption && !traceSourceOption) - printf(", Executing:>\n"); - else { - traceRuleStart(rule, "Executing:>"); - if (!traceInstructionOption) - printf("\n"); - } - } + if (traceSectionOption) { + if (!traceInstructionOption && !traceSourceOption) + printf(", Executing:>\n"); + else { + traceRuleStart(rule, "Executing:>"); + if (!traceInstructionOption) + printf("\n"); + } + } } /*----------------------------------------------------------------------*/ -static void evaluateRulesPreBeta2(void) -{ - bool change = TRUE; - int i; - - for (i = 1; !isEndOfArray(&rules[i-1]); i++) - rules[i-1].alreadyRun = FALSE; - - while (change) { - change = FALSE; - for (i = 1; !isEndOfArray(&rules[i-1]); i++) - if (!rules[i-1].alreadyRun) { - traceRuleEvaluation(i); - if (evaluate(rules[i-1].exp)) { - change = TRUE; - rules[i-1].alreadyRun = TRUE; - traceRuleExecution(i); - interpret(rules[i-1].stms); - } else if (traceSectionOption && !traceInstructionOption) - printf(":>\n"); - } - } +static void evaluateRulesPreBeta2(void) { + bool change = TRUE; + int i; + + for (i = 1; !isEndOfArray(&rules[i - 1]); i++) + rules[i - 1].alreadyRun = FALSE; + + while (change) { + change = FALSE; + for (i = 1; !isEndOfArray(&rules[i - 1]); i++) + if (!rules[i - 1].alreadyRun) { + traceRuleEvaluation(i); + if (evaluate(rules[i - 1].exp)) { + change = TRUE; + rules[i - 1].alreadyRun = TRUE; + traceRuleExecution(i); + interpret(rules[i - 1].stms); + } else if (traceSectionOption && !traceInstructionOption) + printf(":>\n"); + } + } } /*----------------------------------------------------------------------*/ /* This is how beta2 thought rules should be evaluated: */ -static void evaluateRulesBeta2(void) -{ - bool change = TRUE; - int i; - - for (i = 1; !isEndOfArray(&rules[i-1]); i++) - rules[i-1].alreadyRun = FALSE; - - current.location = NOWHERE; - current.actor = 0; - - while (change) { - change = FALSE; - for (i = 1; !isEndOfArray(&rules[i-1]); i++) - if (!rules[i-1].alreadyRun) { - traceRuleEvaluation(i); - bool triggered = evaluate(rules[i-1].exp); - if (triggered) { - if (rulesAdmin[i-1].lastEval == false) { - change = TRUE; - rules[i-1].alreadyRun = TRUE; - traceRuleExecution(i); - interpret(rules[i-1].stms); - } - rulesAdmin[i-1].lastEval = triggered; - } else { - rulesAdmin[i-1].lastEval = false; - if (traceSectionOption && !traceInstructionOption) - printf(":>\n"); - } - } - } +static void evaluateRulesBeta2(void) { + bool change = TRUE; + int i; + + for (i = 1; !isEndOfArray(&rules[i - 1]); i++) + rules[i - 1].alreadyRun = FALSE; + + current.location = NOWHERE; + current.actor = 0; + + while (change) { + change = FALSE; + for (i = 1; !isEndOfArray(&rules[i - 1]); i++) + if (!rules[i - 1].alreadyRun) { + traceRuleEvaluation(i); + bool triggered = evaluate(rules[i - 1].exp); + if (triggered) { + if (rulesAdmin[i - 1].lastEval == false) { + change = TRUE; + rules[i - 1].alreadyRun = TRUE; + traceRuleExecution(i); + interpret(rules[i - 1].stms); + } + rulesAdmin[i - 1].lastEval = triggered; + } else { + rulesAdmin[i - 1].lastEval = false; + if (traceSectionOption && !traceInstructionOption) + printf(":>\n"); + } + } + } } /*======================================================================*/ void resetRules() { - int i; - for (i = 1; !isEndOfArray(&rules[i-1]); i++) { - rulesAdmin[i-1].alreadyRun = FALSE; - } + int i; + for (i = 1; !isEndOfArray(&rules[i - 1]); i++) { + rulesAdmin[i - 1].alreadyRun = FALSE; + } } /*======================================================================*/ void evaluateRules(RuleEntry ruleList[]) { - bool change = TRUE; - int rule; - - current.location = NOWHERE; - current.actor = 0; - - while (change) { - change = FALSE; - for (rule = 1; !isEndOfArray(&ruleList[rule-1]); rule++) { - traceRuleEvaluation(rule); - bool evaluated_value = evaluate(ruleList[rule-1].exp); - traceRuleResult(rule, evaluated_value); - if (evaluated_value == true && rulesAdmin[rule-1].lastEval == false - && !rulesAdmin[rule-1].alreadyRun) { - change = TRUE; - traceRuleExecution(rule); - interpret(ruleList[rule-1].stms); - rulesAdmin[rule-1].alreadyRun = TRUE; - anyRuleRun = TRUE; - } else { - if (traceSectionOption && !(traceInstructionOption || traceSourceOption)) - printf(":>\n"); - } - rulesAdmin[rule-1].lastEval = evaluated_value; - } - } + bool change = TRUE; + int rule; + + current.location = NOWHERE; + current.actor = 0; + + while (change) { + change = FALSE; + for (rule = 1; !isEndOfArray(&ruleList[rule - 1]); rule++) { + traceRuleEvaluation(rule); + bool evaluated_value = evaluate(ruleList[rule - 1].exp); + traceRuleResult(rule, evaluated_value); + if (evaluated_value == true && rulesAdmin[rule - 1].lastEval == false + && !rulesAdmin[rule - 1].alreadyRun) { + change = TRUE; + traceRuleExecution(rule); + interpret(ruleList[rule - 1].stms); + rulesAdmin[rule - 1].alreadyRun = TRUE; + anyRuleRun = TRUE; + } else { + if (traceSectionOption && !(traceInstructionOption || traceSourceOption)) + printf(":>\n"); + } + rulesAdmin[rule - 1].lastEval = evaluated_value; + } + } } /*=======================================================================*/ void resetAndEvaluateRules(RuleEntry ruleList[], char *version) { - if (isPreBeta2(version)) - evaluateRulesPreBeta2(); - else if (isPreBeta3(version)) - evaluateRulesBeta2(); - else { - resetRules(); - evaluateRules(ruleList); - } + if (isPreBeta2(version)) + evaluateRulesPreBeta2(); + else if (isPreBeta3(version)) + evaluateRulesBeta2(); + else { + resetRules(); + evaluateRules(ruleList); + } } } // End of namespace Alan3 diff --git a/engines/glk/alan3/rules.h b/engines/glk/alan3/rules.h index 5b0e449f56..849fc506ae 100644 --- a/engines/glk/alan3/rules.h +++ b/engines/glk/alan3/rules.h @@ -32,7 +32,7 @@ namespace Alan3 { /* DATA */ extern RuleEntry *rules; /* Rule table pointer */ -extern bool anyRuleRun; /* Did any rule run? */ +extern bool anyRuleRun; /* Did any rule run? */ /* FUNCTIONS */ extern void initRules(Aaddr rulesTableAddress); diff --git a/engines/glk/alan3/save.cpp b/engines/glk/alan3/save.cpp index 170cfd4a7d..f1b7d0df4c 100644 --- a/engines/glk/alan3/save.cpp +++ b/engines/glk/alan3/save.cpp @@ -25,335 +25,332 @@ /*----------------------------------------------------------------------*/ static void saveStrings(AFILE saveFile) { - StringInitEntry *initEntry; - - if (header->stringInitTable != 0) - for (initEntry = (StringInitEntry *)pointerTo(header->stringInitTable); - !isEndOfArray(initEntry); initEntry++) { - char *attr = (char *)getInstanceStringAttribute(initEntry->instanceCode, initEntry->attributeCode); - Aint length = strlen(attr) + 1; - fwrite((void *)&length, sizeof(length), 1, saveFile); - fwrite((void *)attr, 1, length, saveFile); - } + StringInitEntry *initEntry; + + if (header->stringInitTable != 0) + for (initEntry = (StringInitEntry *)pointerTo(header->stringInitTable); + !isEndOfArray(initEntry); initEntry++) { + char *attr = (char *)getInstanceStringAttribute(initEntry->instanceCode, initEntry->attributeCode); + Aint length = strlen(attr) + 1; + fwrite((void *)&length, sizeof(length), 1, saveFile); + fwrite((void *)attr, 1, length, saveFile); + } } /*----------------------------------------------------------------------*/ static void saveSets(AFILE saveFile) { - SetInitEntry *initEntry; - - if (header->setInitTable != 0) - for (initEntry = (SetInitEntry *)pointerTo(header->setInitTable); - !isEndOfArray(initEntry); initEntry++) { - Set *attr = (Set *)getInstanceSetAttribute(initEntry->instanceCode, initEntry->attributeCode); - fwrite((void *)&attr->size, sizeof(attr->size), 1, saveFile); - fwrite((void *)attr->members, sizeof(attr->members[0]), attr->size, saveFile); - } + SetInitEntry *initEntry; + + if (header->setInitTable != 0) + for (initEntry = (SetInitEntry *)pointerTo(header->setInitTable); + !isEndOfArray(initEntry); initEntry++) { + Set *attr = (Set *)getInstanceSetAttribute(initEntry->instanceCode, initEntry->attributeCode); + fwrite((void *)&attr->size, sizeof(attr->size), 1, saveFile); + fwrite((void *)attr->members, sizeof(attr->members[0]), attr->size, saveFile); + } } /*----------------------------------------------------------------------*/ static void saveGameInfo(AFILE saveFile) { - fwrite((void *)"ASAV", 1, 4, saveFile); - fwrite((void *)&header->version, 1, sizeof(Aword), saveFile); - fwrite((void *)adventureName, 1, strlen(adventureName)+1, saveFile); - fwrite((void *)&header->uid, 1, sizeof(Aword), saveFile); + fwrite((void *)"ASAV", 1, 4, saveFile); + fwrite((void *)&header->version, 1, sizeof(Aword), saveFile); + fwrite((void *)adventureName, 1, strlen(adventureName) + 1, saveFile); + fwrite((void *)&header->uid, 1, sizeof(Aword), saveFile); } /*----------------------------------------------------------------------*/ static void saveAdmin(AFILE saveFile) { - fwrite((void *)&admin[1], sizeof(AdminEntry), header->instanceMax, saveFile); + fwrite((void *)&admin[1], sizeof(AdminEntry), header->instanceMax, saveFile); } /*----------------------------------------------------------------------*/ static void saveAttributeArea(AFILE saveFile) { - fwrite((void*)attributes, header->attributesAreaSize, sizeof(Aword), saveFile); + fwrite((void *)attributes, header->attributesAreaSize, sizeof(Aword), saveFile); } /*----------------------------------------------------------------------*/ static void saveEventQueue(AFILE saveFile) { - fwrite((void *)&eventQueueTop, sizeof(eventQueueTop), 1, saveFile); - fwrite((void *)&eventQueue[0], sizeof(eventQueue[0]), eventQueueTop, saveFile); + fwrite((void *)&eventQueueTop, sizeof(eventQueueTop), 1, saveFile); + fwrite((void *)&eventQueue[0], sizeof(eventQueue[0]), eventQueueTop, saveFile); } /*----------------------------------------------------------------------*/ static void saveCurrentValues(AFILE saveFile) { - fwrite((void *)¤t, sizeof(current), 1, saveFile); + fwrite((void *)¤t, sizeof(current), 1, saveFile); } /*----------------------------------------------------------------------*/ static void saveScores(AFILE saveFile) { - fwrite((void *)scores, sizeof(Aword), header->scoreCount, saveFile); + fwrite((void *)scores, sizeof(Aword), header->scoreCount, saveFile); } /*----------------------------------------------------------------------*/ static void saveGame(AFILE saveFile) { - /* Save tag, version of interpreter, name and uid of game */ - saveGameInfo(saveFile); + /* Save tag, version of interpreter, name and uid of game */ + saveGameInfo(saveFile); - /* Save current values */ - saveCurrentValues(saveFile); + /* Save current values */ + saveCurrentValues(saveFile); - saveAttributeArea(saveFile); - saveAdmin(saveFile); + saveAttributeArea(saveFile); + saveAdmin(saveFile); - saveEventQueue(saveFile); + saveEventQueue(saveFile); - saveScores(saveFile); + saveScores(saveFile); - saveStrings(saveFile); - saveSets(saveFile); + saveStrings(saveFile); + saveSets(saveFile); } /*======================================================================*/ -void save(void) -{ +void save(void) { #ifdef HAVE_GLK - frefid_t saveFileRef; - strid_t saveFile; - saveFileRef = glk_fileref_create_by_prompt(fileusage_SavedGame, filemode_Write, 0); - if (saveFileRef == NULL) - error(M_SAVEFAILED); - saveFile = glk_stream_open_file(saveFileRef, filemode_Write, 0); + frefid_t saveFileRef; + strid_t saveFile; + saveFileRef = glk_fileref_create_by_prompt(fileusage_SavedGame, filemode_Write, 0); + if (saveFileRef == NULL) + error(M_SAVEFAILED); + saveFile = glk_stream_open_file(saveFileRef, filemode_Write, 0); #else - FILE *saveFile; - char str[256]; - - current.location = where(HERO, DIRECT); - /* First save ? */ - if (saveFileName[0] == '\0') { - strcpy(saveFileName, adventureName); - strcat(saveFileName, ".sav"); - } - printMessage(M_SAVEWHERE); - sprintf(str, "(%s) : ", saveFileName); - output(str); + FILE *saveFile; + char str[256]; + + current.location = where(HERO, DIRECT); + /* First save ? */ + if (saveFileName[0] == '\0') { + strcpy(saveFileName, adventureName); + strcat(saveFileName, ".sav"); + } + printMessage(M_SAVEWHERE); + sprintf(str, "(%s) : ", saveFileName); + output(str); #ifdef USE_READLINE - readline(str); + readline(str); #else - gets(str); + gets(str); #endif - if (str[0] == '\0') - strcpy(str, saveFileName); - col = 1; - if ((saveFile = fopen(str, READ_MODE)) != NULL) - /* It already existed */ - if (!regressionTestOption) { - /* Ask for overwrite confirmation */ - if (!confirm(M_SAVEOVERWRITE)) - abortPlayerCommand(); /* Return to player without saying anything */ - } - strcpy(saveFileName, str); - if ((saveFile = fopen(saveFileName, WRITE_MODE)) == NULL) - error(M_SAVEFAILED); + if (str[0] == '\0') + strcpy(str, saveFileName); + col = 1; + if ((saveFile = fopen(str, READ_MODE)) != NULL) + /* It already existed */ + if (!regressionTestOption) { + /* Ask for overwrite confirmation */ + if (!confirm(M_SAVEOVERWRITE)) + abortPlayerCommand(); /* Return to player without saying anything */ + } + strcpy(saveFileName, str); + if ((saveFile = fopen(saveFileName, WRITE_MODE)) == NULL) + error(M_SAVEFAILED); #endif - saveGame(saveFile); + saveGame(saveFile); - fclose(saveFile); + fclose(saveFile); } /*----------------------------------------------------------------------*/ static void restoreStrings(AFILE saveFile) { - StringInitEntry *initEntry; - - if (header->stringInitTable != 0) - for (initEntry = (StringInitEntry *)pointerTo(header->stringInitTable); - !isEndOfArray(initEntry); initEntry++) { - Aint length; - char *string; - fread((void *)&length, sizeof(Aint), 1, saveFile); - string = allocate(length+1); - fread((void *)string, 1, length, saveFile); - setInstanceAttribute(initEntry->instanceCode, initEntry->attributeCode, toAptr(string)); - } + StringInitEntry *initEntry; + + if (header->stringInitTable != 0) + for (initEntry = (StringInitEntry *)pointerTo(header->stringInitTable); + !isEndOfArray(initEntry); initEntry++) { + Aint length; + char *string; + fread((void *)&length, sizeof(Aint), 1, saveFile); + string = allocate(length + 1); + fread((void *)string, 1, length, saveFile); + setInstanceAttribute(initEntry->instanceCode, initEntry->attributeCode, toAptr(string)); + } } /*----------------------------------------------------------------------*/ static void restoreSets(AFILE saveFile) { - SetInitEntry *initEntry; - - if (header->setInitTable != 0) - for (initEntry = (SetInitEntry *)pointerTo(header->setInitTable); - !isEndOfArray(initEntry); initEntry++) { - Aint setSize; - Set *set; - int i; - - fread((void *)&setSize, sizeof(setSize), 1, saveFile); - set = newSet(setSize); - for (i = 0; i < setSize; i++) { - Aword member; - fread((void *)&member, sizeof(member), 1, saveFile); - addToSet(set, member); - } - setInstanceAttribute(initEntry->instanceCode, initEntry->attributeCode, toAptr(set)); - } + SetInitEntry *initEntry; + + if (header->setInitTable != 0) + for (initEntry = (SetInitEntry *)pointerTo(header->setInitTable); + !isEndOfArray(initEntry); initEntry++) { + Aint setSize; + Set *set; + int i; + + fread((void *)&setSize, sizeof(setSize), 1, saveFile); + set = newSet(setSize); + for (i = 0; i < setSize; i++) { + Aword member; + fread((void *)&member, sizeof(member), 1, saveFile); + addToSet(set, member); + } + setInstanceAttribute(initEntry->instanceCode, initEntry->attributeCode, toAptr(set)); + } } /*----------------------------------------------------------------------*/ static void restoreScores(AFILE saveFile) { - fread((void *)scores, sizeof(Aword), header->scoreCount, saveFile); + fread((void *)scores, sizeof(Aword), header->scoreCount, saveFile); } /*----------------------------------------------------------------------*/ static void restoreEventQueue(AFILE saveFile) { - fread((void *)&eventQueueTop, sizeof(eventQueueTop), 1, saveFile); - if (eventQueueTop > eventQueueSize) { - deallocate(eventQueue); - eventQueue = allocate(eventQueueTop*sizeof(eventQueue[0])); - } - fread((void *)&eventQueue[0], sizeof(eventQueue[0]), eventQueueTop, saveFile); + fread((void *)&eventQueueTop, sizeof(eventQueueTop), 1, saveFile); + if (eventQueueTop > eventQueueSize) { + deallocate(eventQueue); + eventQueue = allocate(eventQueueTop * sizeof(eventQueue[0])); + } + fread((void *)&eventQueue[0], sizeof(eventQueue[0]), eventQueueTop, saveFile); } /*----------------------------------------------------------------------*/ static void restoreAdmin(AFILE saveFile) { - /* Restore admin for instances, remember to reset attribute area pointer */ - int i; - for (i = 1; i <= header->instanceMax; i++) { - AttributeEntry *currentAttributesArea = admin[i].attributes; - fread((void *)&admin[i], sizeof(AdminEntry), 1, saveFile); - admin[i].attributes = currentAttributesArea; - } + /* Restore admin for instances, remember to reset attribute area pointer */ + int i; + for (i = 1; i <= header->instanceMax; i++) { + AttributeEntry *currentAttributesArea = admin[i].attributes; + fread((void *)&admin[i], sizeof(AdminEntry), 1, saveFile); + admin[i].attributes = currentAttributesArea; + } } /*----------------------------------------------------------------------*/ static void restoreAttributeArea(AFILE saveFile) { - fread((void *)attributes, header->attributesAreaSize, sizeof(Aword), saveFile); + fread((void *)attributes, header->attributesAreaSize, sizeof(Aword), saveFile); } /*----------------------------------------------------------------------*/ static void restoreCurrentValues(AFILE saveFile) { - fread((void *)¤t, sizeof(current), 1, saveFile); + fread((void *)¤t, sizeof(current), 1, saveFile); } /*----------------------------------------------------------------------*/ static void verifyGameId(AFILE saveFile) { - Aword savedUid; + Aword savedUid; - fread((void *)&savedUid, sizeof(Aword), 1, saveFile); - if (!ignoreErrorOption && savedUid != header->uid) - error(M_SAVEVERS); + fread((void *)&savedUid, sizeof(Aword), 1, saveFile); + if (!ignoreErrorOption && savedUid != header->uid) + error(M_SAVEVERS); } /*----------------------------------------------------------------------*/ static void verifyGameName(AFILE saveFile) { - char savedName[256]; - int i = 0; + char savedName[256]; + int i = 0; - while ((savedName[i++] = fgetc(saveFile)) != '\0'); - if (strcmp(savedName, adventureName) != 0) - error(M_SAVENAME); + while ((savedName[i++] = fgetc(saveFile)) != '\0'); + if (strcmp(savedName, adventureName) != 0) + error(M_SAVENAME); } /*----------------------------------------------------------------------*/ static void verifyCompilerVersion(AFILE saveFile) { - char savedVersion[4]; + char savedVersion[4]; - fread((void *)&savedVersion, sizeof(Aword), 1, saveFile); - if (!ignoreErrorOption && strncmp(savedVersion, header->version, 4)) - error(M_SAVEVERS); + fread((void *)&savedVersion, sizeof(Aword), 1, saveFile); + if (!ignoreErrorOption && strncmp(savedVersion, header->version, 4)) + error(M_SAVEVERS); } /*----------------------------------------------------------------------*/ static void verifySaveFile(AFILE saveFile) { - char string[256]; + char string[256]; - fread((void *)&string, 1, 4, saveFile); - string[4] = '\0'; - if (strcmp(string, "ASAV") != 0) - error(M_NOTASAVEFILE); + fread((void *)&string, 1, 4, saveFile); + string[4] = '\0'; + if (strcmp(string, "ASAV") != 0) + error(M_NOTASAVEFILE); } /*----------------------------------------------------------------------*/ -static void restoreGame(AFILE saveFile) -{ - if (saveFile == NULL) syserr("'restoreGame()' from a null fileref"); +static void restoreGame(AFILE saveFile) { + if (saveFile == NULL) syserr("'restoreGame()' from a null fileref"); - verifySaveFile(saveFile); + verifySaveFile(saveFile); - /* Verify version of compiler/interpreter of saved game with us */ - verifyCompilerVersion(saveFile); + /* Verify version of compiler/interpreter of saved game with us */ + verifyCompilerVersion(saveFile); - /* Verify name of game */ - verifyGameName(saveFile); + /* Verify name of game */ + verifyGameName(saveFile); - /* Verify unique id of game */ - verifyGameId(saveFile); + /* Verify unique id of game */ + verifyGameId(saveFile); - restoreCurrentValues(saveFile); - restoreAttributeArea(saveFile); - restoreAdmin(saveFile); - restoreEventQueue(saveFile); - restoreScores(saveFile); - restoreStrings(saveFile); - restoreSets(saveFile); + restoreCurrentValues(saveFile); + restoreAttributeArea(saveFile); + restoreAdmin(saveFile); + restoreEventQueue(saveFile); + restoreScores(saveFile); + restoreStrings(saveFile); + restoreSets(saveFile); } /*======================================================================*/ -void restore(void) -{ +void restore(void) { #ifdef HAVE_GLK - frefid_t saveFileRef; - strid_t saveFile; - saveFileRef = glk_fileref_create_by_prompt(fileusage_SavedGame, filemode_Read, 0); - if (saveFileRef == NULL) return; - saveFile = glk_stream_open_file(saveFileRef, filemode_Read, 0); - if (saveFile == NULL) return; + frefid_t saveFileRef; + strid_t saveFile; + saveFileRef = glk_fileref_create_by_prompt(fileusage_SavedGame, filemode_Read, 0); + if (saveFileRef == NULL) return; + saveFile = glk_stream_open_file(saveFileRef, filemode_Read, 0); + if (saveFile == NULL) return; #else - char str[1000]; - FILE *saveFile; - - current.location = where(HERO, DIRECT); - /* First save ? */ - if (saveFileName[0] == '\0') { - strcpy(saveFileName, adventureName); - strcat(saveFileName, ".sav"); - } - printMessage(M_RESTOREFROM); - sprintf(str, "(%s) : ", saveFileName); - output(str); + char str[1000]; + FILE *saveFile; + + current.location = where(HERO, DIRECT); + /* First save ? */ + if (saveFileName[0] == '\0') { + strcpy(saveFileName, adventureName); + strcat(saveFileName, ".sav"); + } + printMessage(M_RESTOREFROM); + sprintf(str, "(%s) : ", saveFileName); + output(str); #ifdef USE_READLINE - readline(str); + readline(str); #else - gets(str); + gets(str); #endif - col = 1; - if (str[0] == '\0') { - strcpy(str, saveFileName); - } - if ((saveFile = fopen(str, READ_MODE)) == NULL) - error(M_SAVEMISSING); - strcpy(saveFileName, str); /* Save it for future use */ + col = 1; + if (str[0] == '\0') { + strcpy(str, saveFileName); + } + if ((saveFile = fopen(str, READ_MODE)) == NULL) + error(M_SAVEMISSING); + strcpy(saveFileName, str); /* Save it for future use */ #endif - restoreGame(saveFile); + restoreGame(saveFile); - fclose(saveFile); + fclose(saveFile); } #endif diff --git a/engines/glk/alan3/scan.cpp b/engines/glk/alan3/scan.cpp index 9eb40d99a6..8f823def98 100644 --- a/engines/glk/alan3/scan.cpp +++ b/engines/glk/alan3/scan.cpp @@ -54,189 +54,189 @@ static char *token = NULL; /*======================================================================*/ void forceNewPlayerInput() { - setEndOfArray(&playerWords[currentWordIndex]); + setEndOfArray(&playerWords[currentWordIndex]); } /*----------------------------------------------------------------------*/ static void unknown(char tok[]) { - char *str = strdup(tok); - Parameter *messageParameters = newParameterArray(); + char *str = strdup(tok); + Parameter *messageParameters = newParameterArray(); - addParameterForString(messageParameters, str); - printMessageWithParameters(M_UNKNOWN_WORD, messageParameters); + addParameterForString(messageParameters, str); + printMessageWithParameters(M_UNKNOWN_WORD, messageParameters); deallocate(messageParameters); - free(str); - abortPlayerCommand(); + free(str); + abortPlayerCommand(); } /*----------------------------------------------------------------------*/ static int number(char tok[]) { - int i; - - (void)sscanf(tok, "%d", &i); - return i; + int i; + + (void)sscanf(tok, "%d", &i); + return i; } /*----------------------------------------------------------------------*/ static int lookup(char wrd[]) { - int i; - - for (i = 0; !isEndOfArray(&dictionary[i]); i++) { - if (compareStrings(wrd, (char *) pointerTo(dictionary[i].string)) == 0) { - return (i); - } - } - unknown(wrd); - return (int)EOD; + int i; + + for (i = 0; !isEndOfArray(&dictionary[i]); i++) { + if (compareStrings(wrd, (char *) pointerTo(dictionary[i].string)) == 0) { + return (i); + } + } + unknown(wrd); + return (int)EOD; } /*----------------------------------------------------------------------*/ static bool isWordCharacter(int ch) { - return isISOLetter(ch) || isdigit(ch) || ch == '\'' || ch == '-' || ch == '_'; + return isISOLetter(ch) || isdigit(ch) || ch == '\'' || ch == '-' || ch == '_'; } /*----------------------------------------------------------------------*/ static char *gettoken(char *txtBuf) { - static char *marker; - static char oldch; - - if (txtBuf == NULL) - *marker = oldch; - else - marker = txtBuf; - while (*marker != '\0' && isSpace(*marker) && *marker != '\n') - marker++; - txtBuf = marker; - if (isISOLetter(*marker)) - while (*marker && isWordCharacter(*marker)) - marker++; - else if (isdigit((int)*marker)) - while (isdigit((int)*marker)) - marker++; - else if (*marker == '\"') { - marker++; - while (*marker != '\"') - marker++; - marker++; - } else if (*marker == '\0' || *marker == '\n' || *marker == ';') - return NULL; - else - marker++; - oldch = *marker; - *marker = '\0'; - return txtBuf; + static char *marker; + static char oldch; + + if (txtBuf == NULL) + *marker = oldch; + else + marker = txtBuf; + while (*marker != '\0' && isSpace(*marker) && *marker != '\n') + marker++; + txtBuf = marker; + if (isISOLetter(*marker)) + while (*marker && isWordCharacter(*marker)) + marker++; + else if (isdigit((int)*marker)) + while (isdigit((int)*marker)) + marker++; + else if (*marker == '\"') { + marker++; + while (*marker != '\"') + marker++; + marker++; + } else if (*marker == '\0' || *marker == '\n' || *marker == ';') + return NULL; + else + marker++; + oldch = *marker; + *marker = '\0'; + return txtBuf; } /*----------------------------------------------------------------------*/ // TODO replace dependency to exe.c with injection of quitGame() and undo() static void getLine(void) { - para(); - do { - statusline(); - if (header->prompt) { - anyOutput = FALSE; - interpret(header->prompt); - if (anyOutput) - printAndLog(" "); - needSpace = FALSE; - } else - printAndLog("> "); - - if (!readline(buf)) { - newline(); - quitGame(); - } - - getPageSize(); - anyOutput = FALSE; - if (transcriptOption || logOption) { - // TODO: Refactor out the logging to log.c? + para(); + do { + statusline(); + if (header->prompt) { + anyOutput = FALSE; + interpret(header->prompt); + if (anyOutput) + printAndLog(" "); + needSpace = FALSE; + } else + printAndLog("> "); + + if (!readline(buf)) { + newline(); + quitGame(); + } + + getPageSize(); + anyOutput = FALSE; + if (transcriptOption || logOption) { + // TODO: Refactor out the logging to log.c? g_vm->glk_put_string_stream(logFile, buf); g_vm->glk_put_char_stream(logFile, '\n'); - } - /* If the player input an empty command he forfeited his command */ + } + /* If the player input an empty command he forfeited his command */ #ifdef TODO if (strlen(buf) == 0) { - clearWordList(playerWords); - longjmp(forfeitLabel, 0); - } + clearWordList(playerWords); + longjmp(forfeitLabel, 0); + } #else ::error("TODO: empty command"); #endif - - strcpy(isobuf, buf); - token = gettoken(isobuf); - if (token != NULL) { - if (strcmp("debug", token) == 0 && header->debug) { - debugOption = TRUE; - debug(FALSE, 0, 0); - token = NULL; - } else if (strcmp("undo", token) == 0) { - token = gettoken(NULL); - if (token != NULL) /* More tokens? */ - error(M_WHAT); - undo(); - } - } - } while (token == NULL); - eol = FALSE; + + strcpy(isobuf, buf); + token = gettoken(isobuf); + if (token != NULL) { + if (strcmp("debug", token) == 0 && header->debug) { + debugOption = TRUE; + debug(FALSE, 0, 0); + token = NULL; + } else if (strcmp("undo", token) == 0) { + token = gettoken(NULL); + if (token != NULL) /* More tokens? */ + error(M_WHAT); + undo(); + } + } + } while (token == NULL); + eol = FALSE; } /*======================================================================*/ void scan(void) { - int i; - int w; - - if (continued) { - /* Player used '.' to separate commands. Read next */ - para(); - token = gettoken(NULL); /* Or did he just finish the command with a full stop? */ - if (token == NULL) - getLine(); - continued = FALSE; - } else - getLine(); - - freeLiterals(); - playerWords[0].code = 0; // TODO This means what? - i = 0; - do { - ensureSpaceForPlayerWords(i+1); - playerWords[i].start = token; - playerWords[i].end = strchr(token, '\0'); - if (isISOLetter(token[0])) { - w = lookup(token); - if (!isNoise(w)) - playerWords[i++].code = w; - } else if (isdigit((int)token[0]) || token[0] == '\"') { - if (isdigit((int)token[0])) { - createIntegerLiteral(number(token)); - } else { - char *unquotedString = strdup(token); - unquotedString[strlen(token) - 1] = '\0'; - createStringLiteral(&unquotedString[1]); - free(unquotedString); - } - playerWords[i++].code = dictionarySize + litCount; /* Word outside dictionary = literal */ - } else if (token[0] == ',') { - playerWords[i++].code = conjWord; - } else if (token[0] == '.') { - continued = TRUE; - setEndOfArray(&playerWords[i]); - eol = TRUE; - break; - } else - unknown(token); - setEndOfArray(&playerWords[i]); - eol = (token = gettoken(NULL)) == NULL; - } while (!eol); + int i; + int w; + + if (continued) { + /* Player used '.' to separate commands. Read next */ + para(); + token = gettoken(NULL); /* Or did he just finish the command with a full stop? */ + if (token == NULL) + getLine(); + continued = FALSE; + } else + getLine(); + + freeLiterals(); + playerWords[0].code = 0; // TODO This means what? + i = 0; + do { + ensureSpaceForPlayerWords(i + 1); + playerWords[i].start = token; + playerWords[i].end = strchr(token, '\0'); + if (isISOLetter(token[0])) { + w = lookup(token); + if (!isNoise(w)) + playerWords[i++].code = w; + } else if (isdigit((int)token[0]) || token[0] == '\"') { + if (isdigit((int)token[0])) { + createIntegerLiteral(number(token)); + } else { + char *unquotedString = strdup(token); + unquotedString[strlen(token) - 1] = '\0'; + createStringLiteral(&unquotedString[1]); + free(unquotedString); + } + playerWords[i++].code = dictionarySize + litCount; /* Word outside dictionary = literal */ + } else if (token[0] == ',') { + playerWords[i++].code = conjWord; + } else if (token[0] == '.') { + continued = TRUE; + setEndOfArray(&playerWords[i]); + eol = TRUE; + break; + } else + unknown(token); + setEndOfArray(&playerWords[i]); + eol = (token = gettoken(NULL)) == NULL; + } while (!eol); } } // End of namespace Alan3 diff --git a/engines/glk/alan3/set.cpp b/engines/glk/alan3/set.cpp index 1b4253abef..c5750c455c 100644 --- a/engines/glk/alan3/set.cpp +++ b/engines/glk/alan3/set.cpp @@ -33,143 +33,137 @@ namespace Alan3 { /*======================================================================*/ Set *newSet(int allocation) { - Set *theSet = NEW(Set); - - if (allocation) { - theSet->members = (Aword *)allocate(allocation*sizeof(theSet->members[0])); - theSet->size = 0; - theSet->allocated = allocation; - } - return theSet; + Set *theSet = NEW(Set); + + if (allocation) { + theSet->members = (Aword *)allocate(allocation * sizeof(theSet->members[0])); + theSet->size = 0; + theSet->allocated = allocation; + } + return theSet; } /*======================================================================*/ -void initSets(SetInitEntry *initTable) -{ - SetInitEntry *init; - int i; - - for (init = initTable; !isEndOfArray(init); init++) { - Set *set = newSet(init->size); - Aword *member = (Aword *)pointerTo(init->setAddress); - for (i = 0; i < init->size; i++, member++) - addToSet(set, *member); - setInstanceAttribute(init->instanceCode, init->attributeCode, toAptr(set)); - } +void initSets(SetInitEntry *initTable) { + SetInitEntry *init; + int i; + + for (init = initTable; !isEndOfArray(init); init++) { + Set *set = newSet(init->size); + Aword *member = (Aword *)pointerTo(init->setAddress); + for (i = 0; i < init->size; i++, member++) + addToSet(set, *member); + setInstanceAttribute(init->instanceCode, init->attributeCode, toAptr(set)); + } } /*======================================================================*/ int setSize(Set *theSet) { - return theSet->size; + return theSet->size; } /*======================================================================*/ void clearSet(Set *theSet) { - theSet->size = 0; + theSet->size = 0; } /*======================================================================*/ Set *copySet(Set *theSet) { - Set *nset = newSet(theSet->size); - int i; + Set *nset = newSet(theSet->size); + int i; - for (i = 1; i <= theSet->size; i++) - addToSet(nset, getSetMember(theSet, i)); - return nset; + for (i = 1; i <= theSet->size; i++) + addToSet(nset, getSetMember(theSet, i)); + return nset; } /*======================================================================*/ Aword getSetMember(Set *theSet, Aint theMember) { - if (theMember > theSet->size || theMember < 1) - apperr("Accessing nonexisting member in a set"); - return theSet->members[theMember-1]; + if (theMember > theSet->size || theMember < 1) + apperr("Accessing nonexisting member in a set"); + return theSet->members[theMember - 1]; } /*======================================================================*/ -bool inSet(Set *theSet, Aword member) -{ - int i; - - for (i = 1; i <= theSet->size; i++) - if (getSetMember(theSet, i) == member) - return TRUE; - return FALSE; +bool inSet(Set *theSet, Aword member) { + int i; + + for (i = 1; i <= theSet->size; i++) + if (getSetMember(theSet, i) == member) + return TRUE; + return FALSE; } /*=======================================================================*/ -Set *setUnion(Set *set1, Set *set2) -{ - Set *theUnion = newSet(set1->size+set2->size); - int i; - - for (i = 0; i < set1->size; i++) - addToSet(theUnion, set1->members[i]); - for (i = 0; i < set2->size; i++) - addToSet(theUnion, set2->members[i]); - return theUnion; +Set *setUnion(Set *set1, Set *set2) { + Set *theUnion = newSet(set1->size + set2->size); + int i; + + for (i = 0; i < set1->size; i++) + addToSet(theUnion, set1->members[i]); + for (i = 0; i < set2->size; i++) + addToSet(theUnion, set2->members[i]); + return theUnion; } /*=======================================================================*/ -void addToSet(Set *theSet, Aword newMember) -{ - if (inSet(theSet, newMember)) return; - if (theSet->size == theSet->allocated) { - theSet->allocated += EXTENT; - theSet->members = (Aword *)realloc(theSet->members, theSet->allocated*sizeof(theSet->members[0])); - } - theSet->members[theSet->size] = newMember; - theSet->size++; +void addToSet(Set *theSet, Aword newMember) { + if (inSet(theSet, newMember)) return; + if (theSet->size == theSet->allocated) { + theSet->allocated += EXTENT; + theSet->members = (Aword *)realloc(theSet->members, theSet->allocated * sizeof(theSet->members[0])); + } + theSet->members[theSet->size] = newMember; + theSet->size++; } /*=======================================================================*/ -void removeFromSet(Set *theSet, Aword member) -{ - int i, j; - - if (!inSet(theSet, member)) return; - - for (i = 0; i < theSet->size; i++) { - if ((Aword)theSet->members[i] == member) { - for (j = i; j < theSet->size-1; j++) - theSet->members[j] = theSet->members[j+1]; - theSet->size--; - break; - } - } +void removeFromSet(Set *theSet, Aword member) { + int i, j; + + if (!inSet(theSet, member)) return; + + for (i = 0; i < theSet->size; i++) { + if ((Aword)theSet->members[i] == member) { + for (j = i; j < theSet->size - 1; j++) + theSet->members[j] = theSet->members[j + 1]; + theSet->size--; + break; + } + } } /*=======================================================================*/ -bool equalSets(Set *set1, Set *set2) -{ - int i; +bool equalSets(Set *set1, Set *set2) { + int i; - if (set1->size != set2->size) return FALSE; + if (set1->size != set2->size) return FALSE; - for (i = 0; i < set1->size; i++) { - if (!inSet(set2, set1->members[i])) - return FALSE; - } - return TRUE; + for (i = 0; i < set1->size; i++) { + if (!inSet(set2, set1->members[i])) + return FALSE; + } + return TRUE; } /*======================================================================*/ void freeSet(Set *theSet) { - if (theSet != NULL) { - if (theSet->members != NULL) - deallocate(theSet->members); - deallocate(theSet); - } + if (theSet != NULL) { + if (theSet->members != NULL) + deallocate(theSet->members); + deallocate(theSet); + } } } // End of namespace Alan3 diff --git a/engines/glk/alan3/set.h b/engines/glk/alan3/set.h index 3fb751c418..60b992a903 100644 --- a/engines/glk/alan3/set.h +++ b/engines/glk/alan3/set.h @@ -25,12 +25,12 @@ /* Abstract datatype Set for Alan interpreter - A set is implemented as a struct holding a size and a - dynamically allocated array of members. Members can be - integers or instance numbers. Attributes of Set type is - allocated and the pointer to it is used as the attribute - value. As members are only references, clearing a set can - simply be done by setting the size to zero. + A set is implemented as a struct holding a size and a + dynamically allocated array of members. Members can be + integers or instance numbers. Attributes of Set type is + allocated and the pointer to it is used as the attribute + value. As members are only references, clearing a set can + simply be done by setting the size to zero. */ #include "glk/alan3/acode.h" @@ -40,9 +40,9 @@ namespace Glk { namespace Alan3 { struct Set { - int size; - int allocated; - Aword *members; + int size; + int allocated; + Aword *members; }; extern Set *newSet(int size); diff --git a/engines/glk/alan3/stack.cpp b/engines/glk/alan3/stack.cpp index a845afff53..a2a7989fa0 100644 --- a/engines/glk/alan3/stack.cpp +++ b/engines/glk/alan3/stack.cpp @@ -31,154 +31,144 @@ namespace Glk { namespace Alan3 { /*======================================================================*/ -Stack createStack(int size) -{ - StackStructure *theStack = NEW(StackStructure); +Stack createStack(int size) { + StackStructure *theStack = NEW(StackStructure); - theStack->stack = (Aword *)allocate(size*sizeof(Aptr)); - theStack->stackSize = size; - theStack->framePointer = -1; + theStack->stack = (Aword *)allocate(size * sizeof(Aptr)); + theStack->stackSize = size; + theStack->framePointer = -1; - return theStack; + return theStack; } /*======================================================================*/ -void deleteStack(Stack theStack) -{ - if (theStack == NULL) - syserr("deleting a NULL stack"); +void deleteStack(Stack theStack) { + if (theStack == NULL) + syserr("deleting a NULL stack"); - deallocate(theStack->stack); - deallocate(theStack); + deallocate(theStack->stack); + deallocate(theStack); } /*======================================================================*/ int stackDepth(Stack theStack) { - return theStack->stackp; + return theStack->stackp; } /*======================================================================*/ -void dumpStack(Stack theStack) -{ - int i; - - if (theStack == NULL) - syserr("NULL stack not supported anymore"); - - printf("["); - for (i = 0; i < theStack->stackp; i++) - printf("%ld ", (unsigned long) theStack->stack[i]); - printf("]"); - if (!traceInstructionOption && !tracePushOption) - printf("\n"); +void dumpStack(Stack theStack) { + int i; + + if (theStack == NULL) + syserr("NULL stack not supported anymore"); + + printf("["); + for (i = 0; i < theStack->stackp; i++) + printf("%ld ", (unsigned long) theStack->stack[i]); + printf("]"); + if (!traceInstructionOption && !tracePushOption) + printf("\n"); } /*======================================================================*/ -void push(Stack theStack, Aptr i) -{ - if (theStack == NULL) - syserr("NULL stack not supported anymore"); - - if (theStack->stackp == theStack->stackSize) - syserr("Out of stack space."); - theStack->stack[(theStack->stackp)++] = i; +void push(Stack theStack, Aptr i) { + if (theStack == NULL) + syserr("NULL stack not supported anymore"); + + if (theStack->stackp == theStack->stackSize) + syserr("Out of stack space."); + theStack->stack[(theStack->stackp)++] = i; } /*======================================================================*/ -Aptr pop(Stack theStack) -{ - if (theStack == NULL) - syserr("NULL stack not supported anymore"); - - if (theStack->stackp == 0) - syserr("Stack underflow."); - return theStack->stack[--(theStack->stackp)]; +Aptr pop(Stack theStack) { + if (theStack == NULL) + syserr("NULL stack not supported anymore"); + + if (theStack->stackp == 0) + syserr("Stack underflow."); + return theStack->stack[--(theStack->stackp)]; } /*======================================================================*/ -Aptr top(Stack theStack) -{ - if (theStack == NULL) - syserr("NULL stack not supported anymore"); +Aptr top(Stack theStack) { + if (theStack == NULL) + syserr("NULL stack not supported anymore"); - return theStack->stack[theStack->stackp-1]; + return theStack->stack[theStack->stackp - 1]; } /* The AMACHINE Block Frames */ /*======================================================================*/ -void newFrame(Stack theStack, Aint noOfLocals) -{ - int n; +void newFrame(Stack theStack, Aint noOfLocals) { + int n; - if (theStack == NULL) - syserr("NULL stack not supported anymore"); + if (theStack == NULL) + syserr("NULL stack not supported anymore"); - push(theStack, theStack->framePointer); - theStack->framePointer = theStack->stackp; - for (n = 0; n < noOfLocals; n++) - push(theStack, 0); + push(theStack, theStack->framePointer); + theStack->framePointer = theStack->stackp; + for (n = 0; n < noOfLocals; n++) + push(theStack, 0); } /*======================================================================*/ /* Local variables are numbered 1 and up and stored on their index-1 */ -Aptr getLocal(Stack theStack, Aint framesBelow, Aint variableNumber) -{ - int frame; - int frameCount; +Aptr getLocal(Stack theStack, Aint framesBelow, Aint variableNumber) { + int frame; + int frameCount; - if (variableNumber < 1) - syserr("Reading a non-existing block-local variable."); + if (variableNumber < 1) + syserr("Reading a non-existing block-local variable."); - if (theStack == NULL) - syserr("NULL stack not supported anymore"); + if (theStack == NULL) + syserr("NULL stack not supported anymore"); - frame = theStack->framePointer; + frame = theStack->framePointer; - if (framesBelow != 0) - for (frameCount = framesBelow; frameCount != 0; frameCount--) - frame = theStack->stack[frame-1]; + if (framesBelow != 0) + for (frameCount = framesBelow; frameCount != 0; frameCount--) + frame = theStack->stack[frame - 1]; - return theStack->stack[frame + variableNumber-1]; + return theStack->stack[frame + variableNumber - 1]; } /*======================================================================*/ -void setLocal(Stack theStack, Aint framesBelow, Aint variableNumber, Aptr value) -{ - int frame; - int frameCount; +void setLocal(Stack theStack, Aint framesBelow, Aint variableNumber, Aptr value) { + int frame; + int frameCount; - if (variableNumber < 1) - syserr("Writing a non-existing block-local variable."); + if (variableNumber < 1) + syserr("Writing a non-existing block-local variable."); - if (theStack == NULL) - syserr("NULL stack not supported anymore"); + if (theStack == NULL) + syserr("NULL stack not supported anymore"); - frame = theStack->framePointer; - if (framesBelow != 0) - for (frameCount = framesBelow; frameCount != 0; frameCount--) - frame = theStack->stack[frame-1]; + frame = theStack->framePointer; + if (framesBelow != 0) + for (frameCount = framesBelow; frameCount != 0; frameCount--) + frame = theStack->stack[frame - 1]; - theStack->stack[frame + variableNumber-1] = value; + theStack->stack[frame + variableNumber - 1] = value; } /*======================================================================*/ -void endFrame(Stack theStack) -{ - if (theStack == NULL) - syserr("NULL stack not supported anymore"); +void endFrame(Stack theStack) { + if (theStack == NULL) + syserr("NULL stack not supported anymore"); - theStack->stackp = theStack->framePointer; - theStack->framePointer = pop(theStack); + theStack->stackp = theStack->framePointer; + theStack->framePointer = pop(theStack); } } // End of namespace Alan3 diff --git a/engines/glk/alan3/stack.h b/engines/glk/alan3/stack.h index 88e589f4c3..8c7f22d303 100644 --- a/engines/glk/alan3/stack.h +++ b/engines/glk/alan3/stack.h @@ -31,7 +31,7 @@ namespace Glk { namespace Alan3 { struct StackStructure { - Aword *stack; // Array that can take Awords + Aword *stack; // Array that can take Awords int stackSize; int stackp; int framePointer; diff --git a/engines/glk/alan3/state.cpp b/engines/glk/alan3/state.cpp index f7fc2eb0ee..0ebf52f3a4 100644 --- a/engines/glk/alan3/state.cpp +++ b/engines/glk/alan3/state.cpp @@ -39,22 +39,22 @@ namespace Alan3 { /* Implementation of the abstract type typedef struct game_state GameState */ struct game_state { - /* Event queue */ - EventQueueEntry *eventQueue; - int eventQueueTop; /* Event queue top pointer */ - - /* Scores */ - int score; - Aword *scores; /* Score table pointer */ - - /* Instance data */ - AdminEntry *admin; /* Administrative data about instances */ - AttributeEntry *attributes; /* Attributes data area */ - /* Sets and strings are dynamically allocated areas for which the - attribute is just a pointer to. So they are not catched by the - saving of attributes, instead they require special storage */ - Set **sets; /* Array of set pointers */ - char **strings; /* Array of string pointers */ + /* Event queue */ + EventQueueEntry *eventQueue; + int eventQueueTop; /* Event queue top pointer */ + + /* Scores */ + int score; + Aword *scores; /* Score table pointer */ + + /* Instance data */ + AdminEntry *admin; /* Administrative data about instances */ + AttributeEntry *attributes; /* Attributes data area */ + /* Sets and strings are dynamically allocated areas for which the + attribute is just a pointer to. So they are not catched by the + saving of attributes, instead they require special storage */ + Set **sets; /* Array of set pointers */ + char **strings; /* Array of string pointers */ }; /* PRIVATE DATA */ @@ -66,292 +66,292 @@ static char *playerCommand; /*----------------------------------------------------------------------*/ static int countStrings(void) { - StringInitEntry *entry; - int count = 0; + StringInitEntry *entry; + int count = 0; - if (header->stringInitTable != 0) - for (entry = (StringInitEntry *)pointerTo(header->stringInitTable); *(Aword *)entry != EOD; entry++) - count++; - return(count); + if (header->stringInitTable != 0) + for (entry = (StringInitEntry *)pointerTo(header->stringInitTable); * (Aword *)entry != EOD; entry++) + count++; + return (count); } /*----------------------------------------------------------------------*/ static void deallocateStrings(GameState *gState) { - int count = countStrings(); - int i; + int count = countStrings(); + int i; - for (i = 0; i < count; i++) - deallocate(gState->strings[i]); - deallocate(gState->strings); + for (i = 0; i < count; i++) + deallocate(gState->strings[i]); + deallocate(gState->strings); } /*----------------------------------------------------------------------*/ static int countSets(void) { - SetInitEntry *entry; - int count = 0; + SetInitEntry *entry; + int count = 0; - if (header->setInitTable != 0) - for (entry = (SetInitEntry *)pointerTo(header->setInitTable); *(Aword *)entry != EOD; entry++) - count++; - return(count); + if (header->setInitTable != 0) + for (entry = (SetInitEntry *)pointerTo(header->setInitTable); * (Aword *)entry != EOD; entry++) + count++; + return (count); } /*----------------------------------------------------------------------*/ static void deallocateSets(GameState *gState) { - int count = countSets(); - int i; + int count = countSets(); + int i; - for (i = 0; i < count; i++) - freeSet(gState->sets[i]); - deallocate(gState->sets); + for (i = 0; i < count; i++) + freeSet(gState->sets[i]); + deallocate(gState->sets); } /*======================================================================*/ void deallocateGameState(GameState *gState) { - deallocate(gState->admin); - deallocate(gState->attributes); + deallocate(gState->admin); + deallocate(gState->attributes); - if (gState->eventQueueTop > 0) { - deallocate(gState->eventQueue); - gState->eventQueue = NULL; - } - if (gState->scores) - deallocate(gState->scores); + if (gState->eventQueueTop > 0) { + deallocate(gState->eventQueue); + gState->eventQueue = NULL; + } + if (gState->scores) + deallocate(gState->scores); - deallocateStrings(gState); - deallocateSets(gState); + deallocateStrings(gState); + deallocateSets(gState); - memset(gState, 0, sizeof(GameState)); + memset(gState, 0, sizeof(GameState)); } /*======================================================================*/ void forgetGameState(void) { - char *playerCmd; - popGameState(stateStack, &gameState, &playerCmd); - deallocateGameState(&gameState); - if (playerCmd != NULL) - deallocate(playerCmd); + char *playerCmd; + popGameState(stateStack, &gameState, &playerCmd); + deallocateGameState(&gameState); + if (playerCmd != NULL) + deallocate(playerCmd); } /*======================================================================*/ void initStateStack(void) { - if (stateStack != NULL) - deleteStateStack(stateStack); - stateStack = createStateStack(sizeof(GameState)); + if (stateStack != NULL) + deleteStateStack(stateStack); + stateStack = createStateStack(sizeof(GameState)); } /*======================================================================*/ void terminateStateStack(void) { - deleteStateStack(stateStack); - stateStack = NULL; + deleteStateStack(stateStack); + stateStack = NULL; } /*======================================================================*/ bool anySavedState(void) { - return !stateStackIsEmpty(stateStack); + return !stateStackIsEmpty(stateStack); } /*----------------------------------------------------------------------*/ static Set **collectSets(void) { - SetInitEntry *entry; - int count = countSets(); - Set **sets; - int i; + SetInitEntry *entry; + int count = countSets(); + Set **sets; + int i; - if (count == 0) return NULL; + if (count == 0) return NULL; - sets = (Set **)allocate(count*sizeof(Set)); + sets = (Set **)allocate(count * sizeof(Set)); - entry = (SetInitEntry *)pointerTo(header->setInitTable); - for (i = 0; i < count; i++) - sets[i] = getInstanceSetAttribute(entry[i].instanceCode, entry[i].attributeCode); + entry = (SetInitEntry *)pointerTo(header->setInitTable); + for (i = 0; i < count; i++) + sets[i] = getInstanceSetAttribute(entry[i].instanceCode, entry[i].attributeCode); - return sets; + return sets; } /*----------------------------------------------------------------------*/ static char **collectStrings(void) { - StringInitEntry *entry; - int count = countStrings(); - char **strings; - int i; + StringInitEntry *entry; + int count = countStrings(); + char **strings; + int i; - if (count == 0) return NULL; + if (count == 0) return NULL; - strings = (char **)allocate(count*sizeof(char *)); + strings = (char **)allocate(count * sizeof(char *)); - entry = (StringInitEntry *)pointerTo(header->stringInitTable); - for (i = 0; i < count; i++) - strings[i] = getInstanceStringAttribute(entry[i].instanceCode, entry[i].attributeCode); + entry = (StringInitEntry *)pointerTo(header->stringInitTable); + for (i = 0; i < count; i++) + strings[i] = getInstanceStringAttribute(entry[i].instanceCode, entry[i].attributeCode); - return strings; + return strings; } /*======================================================================*/ void rememberCommands(void) { - char *command = playerWordsAsCommandString(); - attachPlayerCommandsToLastState(stateStack, command); - deallocate(command); + char *command = playerWordsAsCommandString(); + attachPlayerCommandsToLastState(stateStack, command); + deallocate(command); } /*----------------------------------------------------------------------*/ static void collectEvents(void) { - gameState.eventQueueTop = eventQueueTop; - if (eventQueueTop > 0) - gameState.eventQueue = (EventQueueEntry *)duplicate(eventQueue, eventQueueTop*sizeof(EventQueueEntry)); + gameState.eventQueueTop = eventQueueTop; + if (eventQueueTop > 0) + gameState.eventQueue = (EventQueueEntry *)duplicate(eventQueue, eventQueueTop * sizeof(EventQueueEntry)); } /*----------------------------------------------------------------------*/ static void collectInstanceData(void) { - gameState.admin = (AdminEntry *)duplicate(admin, (header->instanceMax+1)*sizeof(AdminEntry)); - gameState.attributes = (AttributeEntry *)duplicate(attributes, header->attributesAreaSize*sizeof(Aword)); - gameState.sets = collectSets(); - gameState.strings = collectStrings(); + gameState.admin = (AdminEntry *)duplicate(admin, (header->instanceMax + 1) * sizeof(AdminEntry)); + gameState.attributes = (AttributeEntry *)duplicate(attributes, header->attributesAreaSize * sizeof(Aword)); + gameState.sets = collectSets(); + gameState.strings = collectStrings(); } /*----------------------------------------------------------------------*/ static void collectScores(void) { - gameState.score = current.score; - if (scores == NULL) - gameState.scores = NULL; - else - gameState.scores = (Aword *)duplicate(scores, header->scoreCount*sizeof(Aword)); + gameState.score = current.score; + if (scores == NULL) + gameState.scores = NULL; + else + gameState.scores = (Aword *)duplicate(scores, header->scoreCount * sizeof(Aword)); } /*======================================================================*/ void rememberGameState(void) { - collectEvents(); - collectInstanceData(); - collectScores(); + collectEvents(); + collectInstanceData(); + collectScores(); - if (stateStack == NULL) - initStateStack(); + if (stateStack == NULL) + initStateStack(); - pushGameState(stateStack, &gameState); - gameStateChanged = FALSE; + pushGameState(stateStack, &gameState); + gameStateChanged = FALSE; } /*----------------------------------------------------------------------*/ static void freeCurrentSetAttributes(void) { - SetInitEntry *entry; + SetInitEntry *entry; - if (header->setInitTable == 0) return; - for (entry = (SetInitEntry *)pointerTo(header->setInitTable); *(Aword *)entry != EOD; entry++) { - Aptr attributeValue = getAttribute(admin[entry->instanceCode].attributes, entry->attributeCode); - freeSet((Set*)fromAptr(attributeValue)); - } + if (header->setInitTable == 0) return; + for (entry = (SetInitEntry *)pointerTo(header->setInitTable); * (Aword *)entry != EOD; entry++) { + Aptr attributeValue = getAttribute(admin[entry->instanceCode].attributes, entry->attributeCode); + freeSet((Set *)fromAptr(attributeValue)); + } } /*----------------------------------------------------------------------*/ static void recallSets(Set **sets) { - SetInitEntry *entry; - int count = countSets(); - int i; + SetInitEntry *entry; + int count = countSets(); + int i; - if (header->setInitTable == 0) return; + if (header->setInitTable == 0) return; - entry = (SetInitEntry *)pointerTo(header->setInitTable); - for (i = 0; i < count; i++) { - setAttribute(admin[entry[i].instanceCode].attributes, entry[i].attributeCode, toAptr(sets[i])); - sets[i] = NULL; /* Since we reuse the saved set, we need to clear the pointer */ - } + entry = (SetInitEntry *)pointerTo(header->setInitTable); + for (i = 0; i < count; i++) { + setAttribute(admin[entry[i].instanceCode].attributes, entry[i].attributeCode, toAptr(sets[i])); + sets[i] = NULL; /* Since we reuse the saved set, we need to clear the pointer */ + } } /*----------------------------------------------------------------------*/ static void freeCurrentStringAttributes(void) { - StringInitEntry *entry; + StringInitEntry *entry; - if (header->stringInitTable == 0) return; - for (entry = (StringInitEntry *)pointerTo(header->stringInitTable); *(Aword *)entry != EOD; entry++) { - Aptr attributeValue = getAttribute(admin[entry->instanceCode].attributes, entry->attributeCode); - deallocate(fromAptr(attributeValue)); - } + if (header->stringInitTable == 0) return; + for (entry = (StringInitEntry *)pointerTo(header->stringInitTable); * (Aword *)entry != EOD; entry++) { + Aptr attributeValue = getAttribute(admin[entry->instanceCode].attributes, entry->attributeCode); + deallocate(fromAptr(attributeValue)); + } } /*----------------------------------------------------------------------*/ static void recallStrings(char **strings) { - StringInitEntry *entry; - int count = countStrings(); - int i; + StringInitEntry *entry; + int count = countStrings(); + int i; - if (header->stringInitTable == 0) return; + if (header->stringInitTable == 0) return; - entry = (StringInitEntry *)pointerTo(header->stringInitTable); - for (i = 0; i < count; i++) { - setAttribute(admin[entry[i].instanceCode].attributes, entry[i].attributeCode, toAptr(strings[i])); - strings[i] = NULL; /* Since we reuse the saved, we need to clear the state */ - } + entry = (StringInitEntry *)pointerTo(header->stringInitTable); + for (i = 0; i < count; i++) { + setAttribute(admin[entry[i].instanceCode].attributes, entry[i].attributeCode, toAptr(strings[i])); + strings[i] = NULL; /* Since we reuse the saved, we need to clear the state */ + } } /*----------------------------------------------------------------------*/ static void recallEvents(void) { - eventQueueTop = gameState.eventQueueTop; - if (eventQueueTop > 0) { - memcpy(eventQueue, gameState.eventQueue, - (eventQueueTop+1)*sizeof(EventQueueEntry)); - } + eventQueueTop = gameState.eventQueueTop; + if (eventQueueTop > 0) { + memcpy(eventQueue, gameState.eventQueue, + (eventQueueTop + 1)*sizeof(EventQueueEntry)); + } } /*----------------------------------------------------------------------*/ static void recallInstances(void) { - if (admin == NULL) - syserr("admin[] == NULL in recallInstances()"); + if (admin == NULL) + syserr("admin[] == NULL in recallInstances()"); - memcpy(admin, gameState.admin, - (header->instanceMax+1)*sizeof(AdminEntry)); + memcpy(admin, gameState.admin, + (header->instanceMax + 1)*sizeof(AdminEntry)); - freeCurrentSetAttributes(); /* Need to free previous set values */ - freeCurrentStringAttributes(); /* Need to free previous string values */ + freeCurrentSetAttributes(); /* Need to free previous set values */ + freeCurrentStringAttributes(); /* Need to free previous string values */ - memcpy(attributes, gameState.attributes, - header->attributesAreaSize*sizeof(Aword)); + memcpy(attributes, gameState.attributes, + header->attributesAreaSize * sizeof(Aword)); - recallSets(gameState.sets); - recallStrings(gameState.strings); + recallSets(gameState.sets); + recallStrings(gameState.strings); } /*----------------------------------------------------------------------*/ static void recallScores(void) { - current.score = gameState.score; - memcpy(scores, gameState.scores, header->scoreCount*sizeof(Aword)); + current.score = gameState.score; + memcpy(scores, gameState.scores, header->scoreCount * sizeof(Aword)); } /*======================================================================*/ void recallGameState(void) { - popGameState(stateStack, &gameState, &playerCommand); - recallEvents(); - recallInstances(); - recallScores(); - deallocateGameState(&gameState); + popGameState(stateStack, &gameState, &playerCommand); + recallEvents(); + recallInstances(); + recallScores(); + deallocateGameState(&gameState); } /*======================================================================*/ char *recreatePlayerCommand(void) { - return playerCommand; + return playerCommand; } } // End of namespace Alan3 diff --git a/engines/glk/alan3/state_stack.cpp b/engines/glk/alan3/state_stack.cpp index 38e8bb66ec..fa5f9c683e 100644 --- a/engines/glk/alan3/state_stack.cpp +++ b/engines/glk/alan3/state_stack.cpp @@ -37,45 +37,44 @@ struct StateStackStructure { void **states; char **commands; int stackSize; - int stackPointer; /* Points above used stack, 0 initially */ - int elementSize; /* Size of elements in the stack */ + int stackPointer; /* Points above used stack, 0 initially */ + int elementSize; /* Size of elements in the stack */ }; /*----------------------------------------------------------------------*/ -static void *reallocate(void *from, int newSize) -{ - void *newArea = realloc(from, newSize*sizeof(void*)); - if (newArea == NULL) - syserr("Out of memory in 'reallocateStack()'"); - return newArea; +static void *reallocate(void *from, int newSize) { + void *newArea = realloc(from, newSize * sizeof(void *)); + if (newArea == NULL) + syserr("Out of memory in 'reallocateStack()'"); + return newArea; } /*======================================================================*/ StateStackP createStateStack(int elementSize) { - StateStackP stack = NEW(StateStackStructure); - stack->stackSize = 0; - stack->stackPointer = 0; - stack->elementSize = elementSize; - return stack; + StateStackP stack = NEW(StateStackStructure); + stack->stackSize = 0; + stack->stackPointer = 0; + stack->elementSize = elementSize; + return stack; } /*======================================================================*/ void deleteStateStack(StateStackP stateStack) { - if (stateStack != NULL) { - while (stateStack->stackPointer > 0) { - stateStack->stackPointer--; - deallocateGameState((GameState *)stateStack->states[stateStack->stackPointer]); - deallocate(stateStack->states[stateStack->stackPointer]); - deallocate(stateStack->commands[stateStack->stackPointer]); - } - if (stateStack->stackSize > 0) { - deallocate(stateStack->states); - deallocate(stateStack->commands); - } - deallocate(stateStack); - } + if (stateStack != NULL) { + while (stateStack->stackPointer > 0) { + stateStack->stackPointer--; + deallocateGameState((GameState *)stateStack->states[stateStack->stackPointer]); + deallocate(stateStack->states[stateStack->stackPointer]); + deallocate(stateStack->commands[stateStack->stackPointer]); + } + if (stateStack->stackSize > 0) { + deallocate(stateStack->states); + deallocate(stateStack->commands); + } + deallocate(stateStack); + } } @@ -86,13 +85,12 @@ bool stateStackIsEmpty(StateStackP stateStack) { /*----------------------------------------------------------------------*/ -static void ensureSpaceForGameState(StateStackP stack) -{ - if (stack->stackPointer == stack->stackSize) { - stack->states = (void **)reallocate(stack->states, stack->stackSize+EXTENT); - stack->commands = (char **)reallocate(stack->commands, stack->stackSize+EXTENT); - stack->stackSize += EXTENT; - } +static void ensureSpaceForGameState(StateStackP stack) { + if (stack->stackPointer == stack->stackSize) { + stack->states = (void **)reallocate(stack->states, stack->stackSize + EXTENT); + stack->commands = (char **)reallocate(stack->commands, stack->stackSize + EXTENT); + stack->stackSize += EXTENT; + } } @@ -100,28 +98,28 @@ static void ensureSpaceForGameState(StateStackP stack) void pushGameState(StateStackP stateStack, void *gameState) { void *element = allocate(stateStack->elementSize); memcpy(element, gameState, stateStack->elementSize); - ensureSpaceForGameState(stateStack); - stateStack->commands[stateStack->stackPointer] = NULL; - stateStack->states[stateStack->stackPointer++] = element; + ensureSpaceForGameState(stateStack); + stateStack->commands[stateStack->stackPointer] = NULL; + stateStack->states[stateStack->stackPointer++] = element; } /*======================================================================*/ void attachPlayerCommandsToLastState(StateStackP stateStack, char *playerCommands) { - stateStack->commands[stateStack->stackPointer-1] = strdup(playerCommands); + stateStack->commands[stateStack->stackPointer - 1] = strdup(playerCommands); } /*======================================================================*/ -void popGameState(StateStackP stateStack, void *gameState, char** playerCommand) { - if (stateStack->stackPointer == 0) - syserr("Popping GameState from empty stack"); - else { - stateStack->stackPointer--; - memcpy(gameState, stateStack->states[stateStack->stackPointer], stateStack->elementSize); - deallocate(stateStack->states[stateStack->stackPointer]); - *playerCommand = stateStack->commands[stateStack->stackPointer]; - } +void popGameState(StateStackP stateStack, void *gameState, char **playerCommand) { + if (stateStack->stackPointer == 0) + syserr("Popping GameState from empty stack"); + else { + stateStack->stackPointer--; + memcpy(gameState, stateStack->states[stateStack->stackPointer], stateStack->elementSize); + deallocate(stateStack->states[stateStack->stackPointer]); + *playerCommand = stateStack->commands[stateStack->stackPointer]; + } } } // End of namespace Alan3 diff --git a/engines/glk/alan3/syntax.cpp b/engines/glk/alan3/syntax.cpp index 6edb1cecd3..940c1ffe6e 100644 --- a/engines/glk/alan3/syntax.cpp +++ b/engines/glk/alan3/syntax.cpp @@ -40,55 +40,55 @@ SyntaxEntry *stxs; /* Syntax table pointer */ /*======================================================================*/ ElementEntry *elementTreeOf(SyntaxEntry *stx) { - return (ElementEntry *) pointerTo(stx->elms); + return (ElementEntry *) pointerTo(stx->elms); } /*----------------------------------------------------------------------*/ static SyntaxEntry *findSyntaxEntryForPreBeta2(int verbCode, SyntaxEntry *foundStx) { - SyntaxEntryPreBeta2 *stx; - for (stx = (SyntaxEntryPreBeta2*)stxs; !isEndOfArray(stx); stx++) - if (stx->code == verbCode) { - foundStx = (SyntaxEntry *)stx; - break; - } - return(foundStx); + SyntaxEntryPreBeta2 *stx; + for (stx = (SyntaxEntryPreBeta2 *)stxs; !isEndOfArray(stx); stx++) + if (stx->code == verbCode) { + foundStx = (SyntaxEntry *)stx; + break; + } + return (foundStx); } /*----------------------------------------------------------------------*/ static SyntaxEntry *findSyntaxEntry(int verbCode) { - SyntaxEntry *stx; - for (stx = stxs; !isEndOfArray(stx); stx++) - if (stx->code == verbCode) { - return stx; - break; - } - return NULL; + SyntaxEntry *stx; + for (stx = stxs; !isEndOfArray(stx); stx++) + if (stx->code == verbCode) { + return stx; + break; + } + return NULL; } /*======================================================================*/ SyntaxEntry *findSyntaxTreeForVerb(int verbCode) { - SyntaxEntry *foundStx = NULL; - if (isPreBeta2(header->version)) { - foundStx = findSyntaxEntryForPreBeta2(verbCode, foundStx); - } else { - foundStx = findSyntaxEntry(verbCode); - } - if (foundStx == NULL) - /* No matching syntax */ - error(M_WHAT); - return foundStx; + SyntaxEntry *foundStx = NULL; + if (isPreBeta2(header->version)) { + foundStx = findSyntaxEntryForPreBeta2(verbCode, foundStx); + } else { + foundStx = findSyntaxEntry(verbCode); + } + if (foundStx == NULL) + /* No matching syntax */ + error(M_WHAT); + return foundStx; } /*======================================================================*/ char *parameterNameInSyntax(int syntaxNumber, int parameterNumber) { - Aaddr adr = addressAfterTable(header->parameterMapAddress, sizeof(ParameterMapEntry)); - Aaddr *syntaxParameterNameTable = (Aaddr *)pointerTo(memory[adr]); - Aaddr *parameterNameTable = (Aaddr *)pointerTo(syntaxParameterNameTable[syntaxNumber-1]); - return stringAt(parameterNameTable[parameterNumber-1]); + Aaddr adr = addressAfterTable(header->parameterMapAddress, sizeof(ParameterMapEntry)); + Aaddr *syntaxParameterNameTable = (Aaddr *)pointerTo(memory[adr]); + Aaddr *parameterNameTable = (Aaddr *)pointerTo(syntaxParameterNameTable[syntaxNumber - 1]); + return stringAt(parameterNameTable[parameterNumber - 1]); } } // End of namespace Alan3 diff --git a/engines/glk/alan3/sysdep.cpp b/engines/glk/alan3/sysdep.cpp index 0966d54d7b..58f0fa0098 100644 --- a/engines/glk/alan3/sysdep.cpp +++ b/engines/glk/alan3/sysdep.cpp @@ -36,180 +36,176 @@ namespace Alan3 { move to Unicode). */ -static const byte spaceCharacters[] = -{ - 0x0A, /* linefeed */ - 0x20, /* space */ - 0xA0, /* non-breaking space */ - 0x00 +static const byte spaceCharacters[] = { + 0x0A, /* linefeed */ + 0x20, /* space */ + 0xA0, /* non-breaking space */ + 0x00 }; -static const byte lowerCaseCharacters[] = -{ - 0x61, /* a */ 0x62, /* b */ 0x63, /* c */ 0x64, /* d */ - 0x65, /* e */ 0x66, /* f */ 0x67, /* g */ 0x68, /* h */ - 0x69, /* i */ 0x6A, /* j */ 0x6B, /* k */ 0x6C, /* l */ - 0x6D, /* m */ 0x6E, /* n */ 0x6F, /* o */ 0x70, /* p */ - 0x71, /* q */ 0x72, /* r */ 0x73, /* s */ 0x74, /* t */ - 0x75, /* u */ 0x76, /* v */ 0x77, /* w */ 0x78, /* x */ - 0x79, /* y */ 0x7A, /* z */ 0xDF, /* ss <small sharp s> */ - 0xE0, /* a grave */ 0xE1, /* a acute */ - 0xE2, /* a circumflex */ 0xE3, /* a tilde */ - 0xE4, /* a diaeresis */ 0xE5, /* a ring */ - 0xE6, /* ae */ 0xE7, /* c cedilla */ - 0xE8, /* e grave */ 0xE9, /* e acute */ - 0xEA, /* e circumflex */ 0xEB, /* e diaeresis */ - 0xEC, /* i grave */ 0xED, /* i acute */ - 0xEE, /* i circumflex */ 0xEF, /* i diaeresis */ - 0xF0, /* <small eth> */ 0xF1, /* n tilde */ - 0xF2, /* o grave */ 0xF3, /* o acute */ - 0xF4, /* o circumflex */ 0xF5, /* o tilde */ - 0xF6, /* o diaeresis */ 0xF8, /* o slash */ - 0xF9, /* u grave */ 0xFA, /* u acute */ - 0xFB, /* u circumflex */ 0xFC, /* u diaeresis */ - 0xFD, /* y acute */ 0xFE, /* <small thorn> */ - 0xFF, /* y diaeresis */ 0x00 +static const byte lowerCaseCharacters[] = { + 0x61, /* a */ 0x62, /* b */ 0x63, /* c */ 0x64, /* d */ + 0x65, /* e */ 0x66, /* f */ 0x67, /* g */ 0x68, /* h */ + 0x69, /* i */ 0x6A, /* j */ 0x6B, /* k */ 0x6C, /* l */ + 0x6D, /* m */ 0x6E, /* n */ 0x6F, /* o */ 0x70, /* p */ + 0x71, /* q */ 0x72, /* r */ 0x73, /* s */ 0x74, /* t */ + 0x75, /* u */ 0x76, /* v */ 0x77, /* w */ 0x78, /* x */ + 0x79, /* y */ 0x7A, /* z */ 0xDF, /* ss <small sharp s> */ + 0xE0, /* a grave */ 0xE1, /* a acute */ + 0xE2, /* a circumflex */ 0xE3, /* a tilde */ + 0xE4, /* a diaeresis */ 0xE5, /* a ring */ + 0xE6, /* ae */ 0xE7, /* c cedilla */ + 0xE8, /* e grave */ 0xE9, /* e acute */ + 0xEA, /* e circumflex */ 0xEB, /* e diaeresis */ + 0xEC, /* i grave */ 0xED, /* i acute */ + 0xEE, /* i circumflex */ 0xEF, /* i diaeresis */ + 0xF0, /* <small eth> */ 0xF1, /* n tilde */ + 0xF2, /* o grave */ 0xF3, /* o acute */ + 0xF4, /* o circumflex */ 0xF5, /* o tilde */ + 0xF6, /* o diaeresis */ 0xF8, /* o slash */ + 0xF9, /* u grave */ 0xFA, /* u acute */ + 0xFB, /* u circumflex */ 0xFC, /* u diaeresis */ + 0xFD, /* y acute */ 0xFE, /* <small thorn> */ + 0xFF, /* y diaeresis */ 0x00 }; /* FIXME: ss <small sharp s> and y diaeresis have no UC analogues Are they really considered LC? */ -static const byte upperCaseCharacters[] = -{ - 0x41, /* A */ 0x42, /* B */ 0x43, /* C */ 0x44, /* D */ - 0x45, /* E */ 0x46, /* F */ 0x47, /* G */ 0x48, /* H */ - 0x49, /* I */ 0x4A, /* J */ 0x4B, /* K */ 0x4C, /* L */ - 0x4D, /* M */ 0x4E, /* N */ 0x4F, /* O */ 0x50, /* P */ - 0x51, /* Q */ 0x52, /* R */ 0x53, /* S */ 0x54, /* T */ - 0x55, /* U */ 0x56, /* V */ 0x57, /* W */ 0x58, /* X */ - 0x59, /* Y */ 0x5A, /* Z */ - 0xC0, /* A grave */ 0xC1, /* A acute */ - 0xC2, /* A circumflex */ 0xC3, /* A tilde */ - 0xC4, /* A diaeresis */ 0xC5, /* A ring */ - 0xC6, /* AE */ 0xC7, /* C cedilla */ - 0xC8, /* E grave */ 0xC9, /* E acute */ - 0xCA, /* E circumflex */ 0xCB, /* E diaeresis */ - 0xCC, /* I grave */ 0xCD, /* I acute */ - 0xCE, /* I circumflex */ 0xCF, /* I diaeresis */ - 0xD0, /* <capital eth> */ 0xD1, /* N tilde */ - 0xD2, /* O grave */ 0xD3, /* O acute */ - 0xD4, /* O circumflex */ 0xD5, /* O tilde */ - 0xD6, /* O diaeresis */ 0xD8, /* O slash */ - 0xD9, /* U grave */ 0xDA, /* U acute */ - 0xDB, /* U circumflex */ 0xDC, /* U diaeresis */ - 0xDD, /* Y acute */ 0xDE, /* <capital thorn> */ - 0x00 +static const byte upperCaseCharacters[] = { + 0x41, /* A */ 0x42, /* B */ 0x43, /* C */ 0x44, /* D */ + 0x45, /* E */ 0x46, /* F */ 0x47, /* G */ 0x48, /* H */ + 0x49, /* I */ 0x4A, /* J */ 0x4B, /* K */ 0x4C, /* L */ + 0x4D, /* M */ 0x4E, /* N */ 0x4F, /* O */ 0x50, /* P */ + 0x51, /* Q */ 0x52, /* R */ 0x53, /* S */ 0x54, /* T */ + 0x55, /* U */ 0x56, /* V */ 0x57, /* W */ 0x58, /* X */ + 0x59, /* Y */ 0x5A, /* Z */ + 0xC0, /* A grave */ 0xC1, /* A acute */ + 0xC2, /* A circumflex */ 0xC3, /* A tilde */ + 0xC4, /* A diaeresis */ 0xC5, /* A ring */ + 0xC6, /* AE */ 0xC7, /* C cedilla */ + 0xC8, /* E grave */ 0xC9, /* E acute */ + 0xCA, /* E circumflex */ 0xCB, /* E diaeresis */ + 0xCC, /* I grave */ 0xCD, /* I acute */ + 0xCE, /* I circumflex */ 0xCF, /* I diaeresis */ + 0xD0, /* <capital eth> */ 0xD1, /* N tilde */ + 0xD2, /* O grave */ 0xD3, /* O acute */ + 0xD4, /* O circumflex */ 0xD5, /* O tilde */ + 0xD6, /* O diaeresis */ 0xD8, /* O slash */ + 0xD9, /* U grave */ 0xDA, /* U acute */ + 0xDB, /* U circumflex */ 0xDC, /* U diaeresis */ + 0xDD, /* Y acute */ 0xDE, /* <capital thorn> */ + 0x00 }; int isSpace(unsigned int c) { - return (c != '\0' && strchr((const char *)spaceCharacters, c) != 0); + return (c != '\0' && strchr((const char *)spaceCharacters, c) != 0); } int isLower(unsigned int c) { - return (c != '\0' && strchr((const char *)lowerCaseCharacters, c) != 0); + return (c != '\0' && strchr((const char *)lowerCaseCharacters, c) != 0); } int isUpper(unsigned int c) { - return (c != '\0' && strchr((const char *)upperCaseCharacters, c) != 0); + return (c != '\0' && strchr((const char *)upperCaseCharacters, c) != 0); } int isLetter(unsigned int c) { - return(c != '\0' && (isLower(c)? !0: isUpper(c))); + return (c != '\0' && (isLower(c) ? !0 : isUpper(c))); } int toLower(unsigned int c) { - return g_vm->glk_char_to_lower(c); + return g_vm->glk_char_to_lower(c); } int toUpper(unsigned int c) { - return g_vm->glk_char_to_upper(c); + return g_vm->glk_char_to_upper(c); } char *strlow(char str[]) { - char *s; + char *s; - for (s = str; *s; s++) - *s = toLower(*s); - return(str); + for (s = str; *s; s++) + *s = toLower(*s); + return (str); } char *strupp(char str[]) { - char *s; + char *s; - for (s = str; *s; s++) - *s = toUpper(*s); - return(str); + for (s = str; *s; s++) + *s = toUpper(*s); + return (str); } /* The following work on ISO characters */ int isLowerCase(unsigned int c) { - uint i; - for (i = 0; i < strlen((const char *)lowerCaseCharacters); i++) - if (((unsigned int)lowerCaseCharacters[i]) == c) return 1; - return 0; + uint i; + for (i = 0; i < strlen((const char *)lowerCaseCharacters); i++) + if (((unsigned int)lowerCaseCharacters[i]) == c) return 1; + return 0; } int isUpperCase(unsigned int c) { - uint i; - for (i = 0; i < strlen((const char *)upperCaseCharacters); i++) - if (upperCaseCharacters[i] == c) return 1; - return 0; + uint i; + for (i = 0; i < strlen((const char *)upperCaseCharacters); i++) + if (upperCaseCharacters[i] == c) return 1; + return 0; } int isISOLetter(int c) { - return (isLowerCase(c) || isUpperCase(c)); + return (isLowerCase(c) || isUpperCase(c)); } char IsoToLowerCase(int c) { - return (isUpperCase(c)? c + ('a' - 'A'): c); + return (isUpperCase(c) ? c + ('a' - 'A') : c); } char IsoToUpperCase(int c) { - return (isLowerCase(c)? c - ('a' - 'A'): c); + return (isLowerCase(c) ? c - ('a' - 'A') : c); } char *stringLower(char str[]) { - char *s; + char *s; - for (s = str; *s; s++) - *s = IsoToLowerCase(*s); - return(str); + for (s = str; *s; s++) + *s = IsoToLowerCase(*s); + return (str); } char *stringUpper(char str[]) { - char *s; + char *s; - for (s = str; *s; s++) - *s = IsoToUpperCase(*s); - return(str); + for (s = str; *s; s++) + *s = IsoToUpperCase(*s); + return (str); } /*----------------------------------------------------------------------*/ -int compareStrings(char *str1, char *str2) -{ - char *s1 = str1, *s2 = str2; - - while (*s1 != '\0' && *s2 != '\0') { - if (IsoToLowerCase(*s1) < IsoToLowerCase(*s2)) return -1; - if (IsoToLowerCase(*s1) > IsoToLowerCase(*s2)) return 1; - s1++; - s2++; - } - return IsoToLowerCase(*s2) - IsoToLowerCase(*s1); +int compareStrings(char *str1, char *str2) { + char *s1 = str1, *s2 = str2; + + while (*s1 != '\0' && *s2 != '\0') { + if (IsoToLowerCase(*s1) < IsoToLowerCase(*s2)) return -1; + if (IsoToLowerCase(*s1) > IsoToLowerCase(*s2)) return 1; + s1++; + s2++; + } + return IsoToLowerCase(*s2) - IsoToLowerCase(*s1); } @@ -223,65 +219,66 @@ int compareStrings(char *str1, char *str2) */ void toIso(char copy[], /* OUT - Mapped string */ - char original[], /* IN - string to convert */ - int charset) /* IN - the current character set */ -{ -static const unsigned char macMap[256] -= { -0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0A,0x0E,0x0F, -0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F, -0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F, -0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F, -0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F, -0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x5B,0x5C,0x5D,0x5E,0x5F, -0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F, -0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A,0x7B,0x7C,0x7D,0x7E,0x7F, -0xC4,0xC5,0xC7,0xC9,0xD1,0xD6,0xDC,0xE1,0xE0,0xE2,0xE4,0xE3,0xE5,0xE7,0xE9,0xE8, -0xEA,0xEB,0xED,0xEC,0xEE,0xEF,0xF1,0xF3,0xF2,0xF4,0xF6,0xF5,0xFA,0xF9,0xFB,0xFC, -0xB9,0xB0,0xA2,0xA3,0xA7,0xB7,0xB6,0xDF,0xAE,0xA9,0xB2,0xB4,0xA8,0xD7,0xC6,0xD8, -0xA4,0xB1,0xCD,0xCC,0xA5,0xB5,0xF0,0xCA,0xDE,0xFE,0xA6,0xAA,0xBA,0xD4,0xE6,0xF8, -0xBF,0xA1,0xAC,0xCE,0xCF,0xC8,0xD0,0xAB,0xBB,0xCB,0xA0,0xC0,0xC3,0xD5,0xDD,0xFD, -0xAD,0xAF,0xDA,0xD9,0xB8,0xB3,0xF7,0xC2,0xFF,0xBC,0xBD,0xBE,0xC1,0xD2,0xD3,0xDB, -0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, -0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F}; - -static const unsigned char dosMap[256] -= { -0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0A,0x0E,0x0F, -0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F, -0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F, -0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F, -0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F, -0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x5B,0x5C,0x5D,0x5E,0x5F, -0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F, -0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A,0x7B,0x7C,0x7D,0x7E,0x7F, -0xC7,0xFC,0xE9,0xE2,0xE4,0xE0,0xE5,0xE7,0xEA,0xEB,0xE8,0xEF,0xEE,0xEC,0xC4,0xC5, -0xC9,0xE6,0xC6,0xF4,0xF6,0xF2,0xFB,0xF9,0xFF,0xD6,0xDC,0xA2,0xA3,0xA5,0xDE,0xA6, -0xE1,0xED,0xF3,0xFA,0xF1,0xD1,0xAA,0xBA,0xBF,0xC0,0xC1,0xBD,0xBC,0xCF,0xAB,0xBB, -0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, -0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, -0xA1,0xA7,0xAD,0xB3,0xB8,0xB9,0xC3,0xCE,0xD2,0xD3,0xDB,0xDD,0xE3,0xF5,0xF8,0xFD, -0xA9,0xDF,0xC8,0xB6,0xCA,0xA4,0xB5,0xAE,0xD5,0xD0,0xD4,0xF0,0xD7,0xD8,0xCB,0xC2, -0xBE,0xB1,0xD9,0xDA,0xCD,0xCC,0xF7,0xA8,0xB0,0xB7,0xAF,0xAC,0xFE,0xB2,0xB4,0xA0}; - unsigned char *o, *c; - - switch (charset) { - case 0: /* ISO */ - if (copy != original) - (void)strcpy(copy, original); - break; - case 1: /* Mac */ - for (o = (unsigned char *)original, c = (unsigned char *)copy; *o; o++, c++) - *c = macMap[*o]; - *c = '\0'; - break; - - case 2: /* Dos */ - for (o = (unsigned char *)original, c = (unsigned char *)copy; *o; o++, c++) - *c = dosMap[*o]; - *c = '\0'; - break; - } + char original[], /* IN - string to convert */ + int charset) { /* IN - the current character set */ + static const unsigned char macMap[256] + = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0A, 0x0E, 0x0F, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, + 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, + 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, + 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, + 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, + 0xC4, 0xC5, 0xC7, 0xC9, 0xD1, 0xD6, 0xDC, 0xE1, 0xE0, 0xE2, 0xE4, 0xE3, 0xE5, 0xE7, 0xE9, 0xE8, + 0xEA, 0xEB, 0xED, 0xEC, 0xEE, 0xEF, 0xF1, 0xF3, 0xF2, 0xF4, 0xF6, 0xF5, 0xFA, 0xF9, 0xFB, 0xFC, + 0xB9, 0xB0, 0xA2, 0xA3, 0xA7, 0xB7, 0xB6, 0xDF, 0xAE, 0xA9, 0xB2, 0xB4, 0xA8, 0xD7, 0xC6, 0xD8, + 0xA4, 0xB1, 0xCD, 0xCC, 0xA5, 0xB5, 0xF0, 0xCA, 0xDE, 0xFE, 0xA6, 0xAA, 0xBA, 0xD4, 0xE6, 0xF8, + 0xBF, 0xA1, 0xAC, 0xCE, 0xCF, 0xC8, 0xD0, 0xAB, 0xBB, 0xCB, 0xA0, 0xC0, 0xC3, 0xD5, 0xDD, 0xFD, + 0xAD, 0xAF, 0xDA, 0xD9, 0xB8, 0xB3, 0xF7, 0xC2, 0xFF, 0xBC, 0xBD, 0xBE, 0xC1, 0xD2, 0xD3, 0xDB, + 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, + 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F + }; + + static const unsigned char dosMap[256] + = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0A, 0x0E, 0x0F, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, + 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, + 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, + 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, + 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, + 0xC7, 0xFC, 0xE9, 0xE2, 0xE4, 0xE0, 0xE5, 0xE7, 0xEA, 0xEB, 0xE8, 0xEF, 0xEE, 0xEC, 0xC4, 0xC5, + 0xC9, 0xE6, 0xC6, 0xF4, 0xF6, 0xF2, 0xFB, 0xF9, 0xFF, 0xD6, 0xDC, 0xA2, 0xA3, 0xA5, 0xDE, 0xA6, + 0xE1, 0xED, 0xF3, 0xFA, 0xF1, 0xD1, 0xAA, 0xBA, 0xBF, 0xC0, 0xC1, 0xBD, 0xBC, 0xCF, 0xAB, 0xBB, + 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, + 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F, + 0xA1, 0xA7, 0xAD, 0xB3, 0xB8, 0xB9, 0xC3, 0xCE, 0xD2, 0xD3, 0xDB, 0xDD, 0xE3, 0xF5, 0xF8, 0xFD, + 0xA9, 0xDF, 0xC8, 0xB6, 0xCA, 0xA4, 0xB5, 0xAE, 0xD5, 0xD0, 0xD4, 0xF0, 0xD7, 0xD8, 0xCB, 0xC2, + 0xBE, 0xB1, 0xD9, 0xDA, 0xCD, 0xCC, 0xF7, 0xA8, 0xB0, 0xB7, 0xAF, 0xAC, 0xFE, 0xB2, 0xB4, 0xA0 + }; + unsigned char *o, *c; + + switch (charset) { + case 0: /* ISO */ + if (copy != original) + (void)strcpy(copy, original); + break; + case 1: /* Mac */ + for (o = (unsigned char *)original, c = (unsigned char *)copy; *o; o++, c++) + *c = macMap[*o]; + *c = '\0'; + break; + + case 2: /* Dos */ + for (o = (unsigned char *)original, c = (unsigned char *)copy; *o; o++, c++) + *c = dosMap[*o]; + *c = '\0'; + break; + } } /*---------------------------------------------------------------------- @@ -293,8 +290,8 @@ static const unsigned char dosMap[256] */ void fromIso(char copy[], char original[]) { - if (copy != original) - (void)strcpy(copy, original); + if (copy != original) + (void)strcpy(copy, original); } @@ -309,28 +306,28 @@ void fromIso(char copy[], char original[]) { charset - the current character set */ void toNative(char copy[], char original[], int charset) { - toIso(copy, original, charset); - if (NATIVECHARSET != 0) - fromIso(copy, copy); + toIso(copy, original, charset); + if (NATIVECHARSET != 0) + fromIso(copy, copy); } /*======================================================================*/ int littleEndian() { - int x = 1; - return (*(char *)&x == 1); + int x = 1; + return (*(char *)&x == 1); } /*======================================================================*/ char *baseNameStart(char *fullPathName) { - const char *delimiters = "\\>]/:"; - int i; + const char *delimiters = "\\>]/:"; + int i; - for (i = strlen(fullPathName)-1; i > 0; i--) - if (strchr(delimiters, fullPathName[i]) != NULL) - return &fullPathName[i+1]; - return(fullPathName); + for (i = strlen(fullPathName) - 1; i > 0; i--) + if (strchr(delimiters, fullPathName[i]) != NULL) + return &fullPathName[i + 1]; + return (fullPathName); } } // End of namespace Alan3 diff --git a/engines/glk/alan3/sysdep.h b/engines/glk/alan3/sysdep.h index a0c2f7ad4f..87b95fc03d 100644 --- a/engines/glk/alan3/sysdep.h +++ b/engines/glk/alan3/sysdep.h @@ -120,15 +120,15 @@ extern int compareStrings(char str1[], char str2[]); /* Case-insensitive compare /* ISO string conversion functions */ extern void toIso(char copy[], /* OUT - Mapped string */ - char original[], /* IN - string to convert */ - int charset); /* IN - The current character set */ + char original[], /* IN - string to convert */ + int charset); /* IN - The current character set */ extern void fromIso(char copy[], /* OUT - Mapped string */ - char original[]); /* IN - string to convert */ + char original[]); /* IN - string to convert */ extern void toNative(char copy[], /* OUT - Mapped string */ - char original[], /* IN - string to convert */ - int charset); /* IN - current character set */ + char original[], /* IN - string to convert */ + int charset); /* IN - current character set */ extern int littleEndian(void); diff --git a/engines/glk/alan3/syserr.cpp b/engines/glk/alan3/syserr.cpp index 9ebd397380..31aa981e7e 100644 --- a/engines/glk/alan3/syserr.cpp +++ b/engines/glk/alan3/syserr.cpp @@ -34,69 +34,65 @@ static void (*handler)(const char *); /*----------------------------------------------------------------------*/ static void runtimeError(const char *errorClassification, const char *errorDescription, const char *blurb) { - output("$n$nAs you enter the twilight zone of Adventures, you stumble \ + output("$n$nAs you enter the twilight zone of Adventures, you stumble \ and fall to your knees. In front of you, you can vaguely see the outlines \ of an Adventure that never was.$n$n"); - output(errorClassification); - output(errorDescription); - newline(); + output(errorClassification); + output(errorDescription); + newline(); - if (current.sourceLine != 0) { - printf("At source line %d in '%s':\n", current.sourceLine, sourceFileName(current.sourceFile)); - printf("%s", readSourceLine(current.sourceFile, current.sourceLine)); - } + if (current.sourceLine != 0) { + printf("At source line %d in '%s':\n", current.sourceLine, sourceFileName(current.sourceFile)); + printf("%s", readSourceLine(current.sourceFile, current.sourceLine)); + } - newline(); - output(blurb); + newline(); + output(blurb); - terminate(2); + terminate(2); } /*======================================================================*/ -void setSyserrHandler(void (*f)(const char *)) -{ - handler = f; +void setSyserrHandler(void (*f)(const char *)) { + handler = f; } /*======================================================================*/ // TODO Make syserr() use ... as printf() -void syserr(const char *description) -{ - lin = 0; - if (handler == NULL) { +void syserr(const char *description) { + lin = 0; + if (handler == NULL) { const char *blurb = "<If you are the creator of this piece of Interactive Fiction, \ please help debug this Alan system error. Collect *all* the sources, and, if possible, an \ exact transcript of the commands that led to this error, in a zip-file and send \ it to support@alanif.se. Thank you!>"; - runtimeError("SYSTEM ERROR: ", description, blurb); - } else - handler(description); + runtimeError("SYSTEM ERROR: ", description, blurb); + } else + handler(description); } /*======================================================================*/ -void apperr(const char *description) -{ - if (handler == NULL) { +void apperr(const char *description) { + if (handler == NULL) { const char *blurb = "<If you are playing this piece of Interactive Fiction, \ please help the author to debug this programming error. Send an exact \ transcript of the commands that led to this error to the author. Thank you! \ If you *are* the author, then you have to figure this out before releasing the game.>"; - runtimeError("APPLICATION ERROR: ", description, blurb); - } else - handler(description); + runtimeError("APPLICATION ERROR: ", description, blurb); + } else + handler(description); } /*======================================================================*/ -void playererr(const char *description) -{ - if (handler == NULL) { - const char *blurb = "<You have probably done something that is not exactly right.>"; - runtimeError("PLAYER ERROR: ", description, blurb); - } else - handler(description); +void playererr(const char *description) { + if (handler == NULL) { + const char *blurb = "<You have probably done something that is not exactly right.>"; + runtimeError("PLAYER ERROR: ", description, blurb); + } else + handler(description); } } // End of namespace Alan3 diff --git a/engines/glk/alan3/term.cpp b/engines/glk/alan3/term.cpp index 24e9a882ac..86c6036cf6 100644 --- a/engines/glk/alan3/term.cpp +++ b/engines/glk/alan3/term.cpp @@ -43,42 +43,42 @@ bool onStatusLine = FALSE; /* To know if where printing the status line or not * */ void getPageSize(void) { - pageLength = 0; - pageWidth = 0; + pageLength = 0; + pageWidth = 0; } /*======================================================================*/ void statusline(void) { - uint32 glkWidth; - char line[100]; - int pcol = col; + uint32 glkWidth; + char line[100]; + int pcol = col; - if (!statusLineOption) return; - if (glkStatusWin == NULL) - return; + if (!statusLineOption) return; + if (glkStatusWin == NULL) + return; - g_vm->glk_set_window(glkStatusWin); - g_vm->glk_window_clear(glkStatusWin); - g_vm->glk_window_get_size(glkStatusWin, &glkWidth, NULL); + g_vm->glk_set_window(glkStatusWin); + g_vm->glk_window_clear(glkStatusWin); + g_vm->glk_window_get_size(glkStatusWin, &glkWidth, NULL); - onStatusLine = TRUE; - col = 1; - g_vm->glk_window_move_cursor(glkStatusWin, 1, 0); - sayInstance(where(HERO, /*TRUE*/ TRANSITIVE)); + onStatusLine = TRUE; + col = 1; + g_vm->glk_window_move_cursor(glkStatusWin, 1, 0); + sayInstance(where(HERO, /*TRUE*/ TRANSITIVE)); - // TODO Add status message1 & 2 as author customizable messages - if (header->maximumScore > 0) - sprintf(line, "Score %d(%d)/%d moves", current.score, (int)header->maximumScore, current.tick); - else - sprintf(line, "%d moves", current.tick); - g_vm->glk_window_move_cursor(glkStatusWin, glkWidth-strlen(line)-1, 0); - g_vm->glk_put_string(line); - needSpace = FALSE; + // TODO Add status message1 & 2 as author customizable messages + if (header->maximumScore > 0) + sprintf(line, "Score %d(%d)/%d moves", current.score, (int)header->maximumScore, current.tick); + else + sprintf(line, "%d moves", current.tick); + g_vm->glk_window_move_cursor(glkStatusWin, glkWidth - strlen(line) - 1, 0); + g_vm->glk_put_string(line); + needSpace = FALSE; - col = pcol; - onStatusLine = FALSE; + col = pcol; + onStatusLine = FALSE; - g_vm->glk_set_window(glkMainWin); + g_vm->glk_set_window(glkMainWin); } } // End of namespace Alan3 diff --git a/engines/glk/alan3/types.cpp b/engines/glk/alan3/types.cpp index 962a9f422b..7635d36910 100644 --- a/engines/glk/alan3/types.cpp +++ b/engines/glk/alan3/types.cpp @@ -28,10 +28,10 @@ namespace Alan3 { /*======================================================================*/ Aaddr addressAfterTable(Aaddr adr, int size) { - while (!isEndOfArray(&memory[adr])) { - adr += size/sizeof(Aword); - } - return adr+1; + while (!isEndOfArray(&memory[adr])) { + adr += size / sizeof(Aword); + } + return adr + 1; } } // End of namespace Alan3 diff --git a/engines/glk/alan3/types.h b/engines/glk/alan3/types.h index 278cfa2c8c..1055203b63 100644 --- a/engines/glk/alan3/types.h +++ b/engines/glk/alan3/types.h @@ -56,15 +56,15 @@ namespace Alan3 { #define ASIZE(x) (sizeof(x)/sizeof(Aword)) /* The various tables */ -struct VerbEntry { /* VERB TABLE */ - Aint code; /* Code for the verb */ - Aaddr alts; /* Address to alternatives */ +struct VerbEntry { /* VERB TABLE */ + Aint code; /* Code for the verb */ + Aaddr alts; /* Address to alternatives */ }; -struct LimitEntry { /* LIMIT Type */ - Aword atr; /* Attribute that limits */ - Aword val; /* And the limiting value */ - Aaddr stms; /* Statements if fail */ +struct LimitEntry { /* LIMIT Type */ + Aword atr; /* Attribute that limits */ + Aword val; /* And the limiting value */ + Aaddr stms; /* Statements if fail */ }; /* Functions: */ diff --git a/engines/glk/alan3/utils.cpp b/engines/glk/alan3/utils.cpp index 4b01b46d68..402cbe6a21 100644 --- a/engines/glk/alan3/utils.cpp +++ b/engines/glk/alan3/utils.cpp @@ -43,48 +43,47 @@ namespace Alan3 { */ void terminate(int code) { - newline(); + newline(); - terminateStateStack(); + terminateStateStack(); - stopTranscript(); + stopTranscript(); - if (memory) - deallocate(memory); + if (memory) + deallocate(memory); - g_vm->glk_exit(); + g_vm->glk_exit(); } /*======================================================================*/ void printVersion(int buildNumber) { - printf("Arun - Adventure Language Interpreter version %s", alan.version.string); - if (buildNumber != 0) printf("-%d", buildNumber); - printf(" (%s %s)", alan.date, alan.time); + printf("Arun - Adventure Language Interpreter version %s", alan.version.string); + if (buildNumber != 0) printf("-%d", buildNumber); + printf(" (%s %s)", alan.date, alan.time); } /*======================================================================*/ -void usage(const char *programName) -{ +void usage(const char *programName) { #if (BUILD+0) != 0 - printVersion(BUILD); + printVersion(BUILD); #else - printVersion(0); + printVersion(0); #endif - printf("\n\nUsage:\n\n"); - printf(" %s [<switches>] <adventure>\n\n", programName); - printf("where the possible optional switches are:\n"); - - g_vm->glk_set_style(style_Preformatted); - printf(" -v verbose mode\n"); - printf(" -l log transcript to a file\n"); - printf(" -c log player commands to a file\n"); - printf(" -n no Status Line\n"); - printf(" -d enter debug mode\n"); - printf(" -t[<n>] trace game execution, higher <n> gives more trace\n"); - printf(" -i ignore version and checksum errors\n"); - printf(" -r make regression test easier (don't timestamp, page break, randomize...)\n"); - g_vm->glk_set_style(style_Normal); + printf("\n\nUsage:\n\n"); + printf(" %s [<switches>] <adventure>\n\n", programName); + printf("where the possible optional switches are:\n"); + + g_vm->glk_set_style(style_Preformatted); + printf(" -v verbose mode\n"); + printf(" -l log transcript to a file\n"); + printf(" -c log player commands to a file\n"); + printf(" -n no Status Line\n"); + printf(" -d enter debug mode\n"); + printf(" -t[<n>] trace game execution, higher <n> gives more trace\n"); + printf(" -i ignore version and checksum errors\n"); + printf(" -r make regression test easier (don't timestamp, page break, randomize...)\n"); + g_vm->glk_set_style(style_Normal); } @@ -93,7 +92,7 @@ void usage(const char *programName) #endif /*======================================================================*/ bool match(const char *pattern, char *input) { - return fnmatch(pattern, input, FNM_CASEFOLD) == 0; + return fnmatch(pattern, input, FNM_CASEFOLD) == 0; } } // End of namespace Alan3 diff --git a/engines/glk/alan3/version.h b/engines/glk/alan3/version.h index 82036b682b..e2056ab52a 100644 --- a/engines/glk/alan3/version.h +++ b/engines/glk/alan3/version.h @@ -31,25 +31,25 @@ namespace Alan3 { typedef int64 Time; struct Version { - char* string; - int version; - int revision; - int correction; - Time time; - const char * state; + char *string; + int version; + int revision; + int correction; + Time time; + const char *state; }; struct Product { - const char* name; - const char* slogan; - const char* shortHeader; - const char* longHeader; - const char* date; - const char* time; - const char* user; - const char* host; - const char* ostype; - Version version; + const char *name; + const char *slogan; + const char *shortHeader; + const char *longHeader; + const char *date; + const char *time; + const char *user; + const char *host; + const char *ostype; + Version version; }; } // End of namespace Alan3 diff --git a/engines/glk/alan3/word.cpp b/engines/glk/alan3/word.cpp index 450e684308..4df82099e7 100644 --- a/engines/glk/alan3/word.cpp +++ b/engines/glk/alan3/word.cpp @@ -51,31 +51,31 @@ int verbWordCode; /* The code for that verb */ /*+++++++++++++++++++++++++++++++++++++++++++++++++++*/ void ensureSpaceForPlayerWords(int size) { - int newLength = playerWordsLength+PLAYER_WORDS_EXTENT; - - if (playerWordsLength < size+1) { - playerWords = (Word *)realloc(playerWords, newLength*sizeof(Word)); - if (playerWords == NULL) - syserr("Out of memory in 'ensureSpaceForPlayerWords()'"); - playerWordsLength = newLength; - } + int newLength = playerWordsLength + PLAYER_WORDS_EXTENT; + + if (playerWordsLength < size + 1) { + playerWords = (Word *)realloc(playerWords, newLength * sizeof(Word)); + if (playerWords == NULL) + syserr("Out of memory in 'ensureSpaceForPlayerWords()'"); + playerWordsLength = newLength; + } } /*======================================================================*/ char *playerWordsAsCommandString(void) { - char *commandString; - int size = playerWords[lastWord].end - playerWords[firstWord].start; - commandString = (char *)allocate(size + 1); - strncpy(commandString, playerWords[firstWord].start, size); - commandString[size] = '\0'; - return commandString; + char *commandString; + int size = playerWords[lastWord].end - playerWords[firstWord].start; + commandString = (char *)allocate(size + 1); + strncpy(commandString, playerWords[firstWord].start, size); + commandString[size] = '\0'; + return commandString; } /*======================================================================*/ void clearWordList(Word list[]) { - implementationOfSetEndOfArray((Aword *)list); + implementationOfSetEndOfArray((Aword *)list); } } // End of namespace Alan3 diff --git a/engines/glk/alan3/word.h b/engines/glk/alan3/word.h index 3232092b2e..d5d0603f71 100644 --- a/engines/glk/alan3/word.h +++ b/engines/glk/alan3/word.h @@ -27,16 +27,16 @@ namespace Glk { namespace Alan3 { typedef struct WordEntry { - int code; /* The dictionary index for that word */ - char *start; /* Where does it start */ - char *end; /* .. and end */ + int code; /* The dictionary index for that word */ + char *start; /* Where does it start */ + char *end; /* .. and end */ } Word; /* DATA */ -extern Word *playerWords; /* List of Parsed Word */ -extern int currentWordIndex; /* and an index into it */ +extern Word *playerWords; /* List of Parsed Word */ +extern int currentWordIndex; /* and an index into it */ extern int firstWord; extern int lastWord; |