/* gamedata.c-- Miscellaneous utilities and data structures */ /* Copyright (C) 1996-1999,2001 Robert Masenten */ /* This program may be redistributed under the terms of the GNU General Public License, version 2; see agility.h for details. */ /* */ /* This is part of the source for both AGiliTy: the (Mostly) Universal */ /* AGT Interpreter and for the Magx adventure game compiler. */ /* This is a mishmash of utilities and preinitialized arrays, including the verblist, the metacommand token list, and the dictionary routines. */ #include "agility.h" #include #include #include /* ------------------------------------------------------------------- */ /* Preinitialized data structures */ /* Most of the preinitialized data structures used by all of the */ /* AGT-related programs go here . */ /* ------------------------------------------------------------------- */ /* ------------------------------------------------------------ */ /* The PC --> ASCII conversion table. This converts the 8th-bit */ /* PC characters to their nearest ASCII equivalent. */ /* ------------------------------------------------------------ */ const char trans_ibm[]= "CueaaaaceeeiiiAA" /* 80 */ "E@@ooouuyOUc$$pf" /* 90 */ "aiounNao?....!<>" /* A0 */ "###|++|+++|\\/++\\" /* B0 */ "\\+++-+||\\/+++=+=" /* C0 */ "+=+++++++//@@@@@" /* D0 */ "abGpSsmtFTOd.fe^" /* E0 */ "=+> n-55 105,122 Dummy_verb1...Dummy_Verb50 */ /* Possible extension to verb definitons (not implemented): If it _requires_ a prep, use : ? If it takes a prep and no dobj, use | ? */ /* These are alternative (that is, non-canonical) forms of verbs that were present in the original AGT interpreters. They have the property that they have no effect if used in a dummy_verb declaration. */ /* Their dictionary indices are stored in old_agt_verb, which is initialized by reinit_dict. */ /* PICK, GO */ char *old_agt_verb_str[]={ "n","s","e","w","ne","nw","se","sw","u","d","in","inside","leave", "cast","dump","shut","l","ex","inspect","check","kill","fight","hit", "shout","scream","place","q","talk","i","take","touch","ext", "shoot","h","ins",NULL}; /* ------------------------------------------------------------------- */ /* Dictionary primitives: the basic functions for manipulating the */ /* dictionary data structures. */ /* ------------------------------------------------------------------- */ #define HASHSIZE (1<>HASHBITS))&HASHMASK; } return (n & HASHMASK); } static word search0_dict(const char *s) { int i; #ifdef DOHASH for(i=hashfunc(s); hash[i]!=-1 && strcmp(s,dict[hash[i]])!=0; i=(i+1)&HASHMASK); return hash[i]; #else for(i=0; strcmp(s,dict[i])!=0 && idictstrsize) { /* Enlarge dictstr */ if (dictstrsize==0) dictstrsize=DICT_INIT; while(newptr>dictstrsize) dictstrsize+=DICT_GRAN; newstr=rrealloc(dictstr,dictstrsize); /* Now need to update all of our pointers */ for(i=0;iHASHSIZE) fatal("Hash table overflow"); for(i=hashfunc(s);hash[i]!=-1;i=(i+1)&HASHMASK); hash[i]=dp; #endif return dp++; } #ifdef DOHASH static void init_hash(void) { int i; for(i=0;iHASHSIZE) fatal("Hash table overflow"); init_hash(); for(i=0;i0) { /* word just ended: need to add it to dictionary etc */ curr[n]=0;n=0; addsyn(add0_dict(curr)); /* Add to syn list or prep list, depending */ } if (!isspace(*p)) addsyn(-1); /* Mark the end of the list */ if (*p==';' || *p=='.' || *p=='!') { if (*p==';') verbflag[vp]|=VERB_TAKEOBJ; if (*p=='!') verbflag[vp]|=VERB_META; if (have_multiword) verb_multiword(vp); have_multiword=0; vp++; if (vp>=TOTAL_VERB) break; auxsyn[vp]=synptr; /* The following words will be the syn list */ } else if (*p==',') preplist[vp]=synptr; /* The following words will be the prep list */ } else if (*p=='&') { curr[n++]=' '; have_multiword=1; } else curr[n++]=*p; } void init_dict(void) { dict=NULL;verblist=NULL;syntbl=NULL;no_syn=0; auxsyn=NULL;preplist=NULL;verbflag=NULL; auxcomb=NULL; old_agt_verb=NULL; num_auxcomb=0; } /* This is called by agttest.c */ void build_verblist(void) { int i; verblist=rmalloc(sizeof(words)*TOTAL_VERB); for(i=0;i "); for(j=preplist[i];syntbl[j]!=0;j++) rprintf(" %s",dict[ syntbl[preplist[i]]] ); writeln(""); } } #endif } void set_verbflag(void) { verbflag[14]|=VERB_MULTI; /* throw */ verbflag[29]|=VERB_MULTI; /* put */ verbflag[33]|=VERB_MULTI; /* get */ verbflag[41]|=VERB_MULTI; /* drop */ verbflag[51]|=VERB_MULTI; /* wear */ verbflag[52]|=VERB_MULTI; /* remove */ } void reinit_dict(void) /* reinit_dict initializes verblist and sets up aux_syn as well as loading the initial vocabulary into the dictionary. */ { char buff[16]; /* Needs to be big enough to hold dummy_verbNNN\0 or subroutineNNN\0 */ int i; no_syn=no_auxsyn; auxsyn=rmalloc(sizeof(slist)*TOTAL_VERB); auxcomb=NULL; num_auxcomb=0; preplist=rmalloc(sizeof(slist)*TOTAL_VERB); verbflag=rmalloc(sizeof(uchar)*TOTAL_VERB); if (!agx_file) init0_dict(); #ifdef DOHASH else rebuild_hash(); #endif for(i=0;i0; j--); s[j+1]=0; /* Okay, now make sure it isn't 'none' */ if (strcmp(s,"none")==0) return 0; /* Finally, add it to the dictionary if it isn't already there */ return add0_dict(s); } /* Adds w to dynamically grown synonym list */ /* If no_syn is set, then *don't* add a synonym: return immediatly */ /* (This is done by agt2agx to avoid creating the auxsyn lists, */ /* since those should be created when the interpreter loads the */ /* game file and not before) */ void addsyn(word w) { if (no_syn) return; if (w==0) return; if (w==-1) w=0; if (synptr>=syntbl_size) { syntbl_size+=SYN_GRAIN; if (syntbl_size>0x7FFF) fatal("Too many synonyms."); syntbl=rrealloc(syntbl,((long)syntbl_size)*sizeof(word)); } syntbl[synptr++]=w; } /* Returns the given dictionary word with some checking for -1 */ const char *gdict(word w) { assert(w>=-1 && w=0;i--) for(j=synlist[BASE_VERB+DVERB+i];syntbl[j]!=0;j++) if (w==syntbl[j]) return BASE_VERB+DVERB+i; /* In AGT the dummy verbs are laid out in memory in a non-obvious order: DUMMY_VERB1 DUMMY_VERB26 DUMMY_VERB2 DUMMY_VERB27 ... For a few games this is relevant (e.g. SIGNAL in Shades of Gray), as the same synonym occurs in multiple dummy verbs, so we scan the dummy verb synonyms here in the same order as original AGT. */ for(i=DVERB-1;i>=0;i--) { int ii=((i%2)==0) ? i/2 : (i+DVERB-1)/2; for(j=synlist[BASE_VERB+ii];syntbl[j]!=0;j++) if (w==syntbl[j]) return BASE_VERB+ii; } for(i=BASE_VERB-1;i>0;i--) for(j=synlist[i];syntbl[j]!=0;j++) if (w==syntbl[j]) return i; } else { for(i=TOTAL_VERB-1;i>0;i--) for(j=synlist[i];syntbl[j]!=0;j++) if (w==syntbl[j]) return i; } return 0; } int verb_code(word w) /* Given a word w, searches auxsyn and returns the verb id */ { int canon, tmp; /* Expand author-defined synonyms */ tmp=verb_authorsyn(w); if (tmp!=0) return tmp; /* Expand built-in synonyms */ canon=verb_builtin(w); if (canon!=0) { /* Allow built-in verbs to be overridden */ tmp=verb_authorsyn(syntbl[auxsyn[canon]]); if (tmp!=0) return tmp; } return canon; /* No new synonyms; return canonical match if it exists */ } /* This is a faster version of the above for use in the special case of command headers where the verb word is much more restricted; it should be the first auxsyn entry and it should never by a synlist entry. */ static int cmdverb_code(word w) { int i,j; for(i=0;i=first_room && i<=maxroom) return rstrdup(room[i-first_room].name); if ( (i>=first_noun && i<=maxnoun) || (i>=first_creat && i<=maxcreat) ) { word adjw,nounw; if (i>=first_noun && i<=maxnoun) { adjw=noun[i-first_noun].adj; nounw=noun[i-first_noun].name; } else { adjw=creature[i-first_creat].adj; nounw=creature[i-first_creat].name; } if (adjw==0 || !strcmp(dict[adjw],"no_adjective")) return rstrdup(dict[nounw]); return concdup(dict[adjw],dict[nounw]); } /* At this point we can't get a name: return ILLn. */ s=rmalloc(3+1+(5*sizeof(int))/2+1); /* Make sure we have enough space in case i is big */ sprintf(s,"ILL%d",i); return s; } /* ------------------------------------------------------------------- */ /* Routines to sort the command array and construct verbptr */ /* ------------------------------------------------------------------- */ #define SORT_META #ifdef SORT_META #define ch1 ((const cmd_rec*)cmd1) #define ch2 ((const cmd_rec*)cmd2) /* See notes below before trying to decipher this routine; during the sort, many of the fields are being used for nonstandard purposes */ #define s_verb(cmd) ( (cmd)->actor<0 ? (cmd)->data[0] : (cmd)->verbcmd) static int cmp_cmd(const void *cmd1, const void *cmd2) { word v1,v2; /* We are sorting on command[].verbcmd, but if one of the headers is really the object of a redirect command then we need to use its parent's verbcmd */ /* For commands with actors, we need to avoid sorting them at all. */ v1=s_verb(ch1); v2=s_verb(ch2); if (v1v2) return +1; /* v1==v2, so leave them in the same order as before */ /* We have to take absolute values here because we are using negatives to indicate redirection objects */ if ( abs(ch1->actor) < abs(ch2->actor) ) return -1; else if (abs(ch1->actor) == abs(ch2->actor) ) return 0; else return 1; /* Equality should be impossible */ } #undef ch1 #undef ch2 /* This sets things up for qsort */ /* We need a sort that is i) Stable and ii) Keeps "redirection headers" attached to the correct command */ /* We steal the field actor for this purpose */ /* actor will equal the index of the header in the original list. */ /* (or negative the header if the command is a redirection) */ /* For redirected commands, we steal the data pointer since it shouldn't be being used anyhow. */ /* In a field pointed to by data we store the verb word */ /* NOTE: this routine requires that the data type of *data (namely integer) is big enough to hold a value of type word. */ static void rsort(void) { long i; integer *save_actor; word *save_verb; save_actor=rmalloc(last_cmd*sizeof(integer)); save_verb=rmalloc(last_cmd*sizeof(word)); /* The following loop does three things: i) Copies command[].actor to save_actor[] ii) Sets command[].actor to the commands index in the array iii) For actor commands, sets the verb to .... after saving it in save_verb. iv) For redirection commands, stores the verb of the owning header in a block pointed to by data */ for(i=0;i1) /* i.e. there _is_ an actor */ command[i].verbcmd=syntbl[auxsyn[DIR_ADDR_CODE]]; save_actor[i]=command[i].actor; command[i].actor=i; if (save_actor[i]<0) { /* Redirected command */ int j; command[i].actor=-i; rfree(command[i].data); /* data should be NULL, anyhow */ command[i].data=rmalloc(sizeof(integer)); for(j=i;j>0 && save_actor[j]<0;j--); if (save_actor[j]>0) command[i].data[0]=command[j].verbcmd; else { command[i].data[0]=0; agtwarn("First command header is REDIRECT object!",0); } } } /* Now do the sort... */ qsort(command,last_cmd,sizeof(cmd_rec),cmp_cmd); #if 0 /* This is code to test the integrity of the sort */ for(i=0;i1) curr_vb=DIR_ADDR_CODE; else curr_vb=cmdverb_code(command[i].verbcmd); if (iverbend[curr_vb]) verbend[curr_vb]=i; } for(i=0;i0 && command[i].nouncmd>0 && command[i].prep==0 && command[i].objcmd==0) { for(j=0;j0) { vb = verb_builtin(w); if(vb>0) { command[i].verbcmd=syntbl[auxsyn[vb]]; command[i].nouncmd=0; } } } } } } /* ------------------------------------------------------------------- */ /* Functions for getting opcode information */ /* ------------------------------------------------------------------- */ /* Returns the opdef structure associated with an opcode */ const opdef *get_opdef(integer op) { op=op%2048; /* Strip operand information */ if (op<0 || (op>MAX_COND && opPREWIN_ACT && opMAX_ACT) ) { return &illegal_def; } if (op>=2000) return &end_def[op-2000]; if (op>=1000) return &act_def[op-1000]; return &cond_def[op]; } /* ------------------------------------------------------------------- */ /* Functions for processing strings */ /* ------------------------------------------------------------------- */ long new_str(char *buff,int max_leng,rbool pasc) /* Stores the (up to leng) characters of a string into our master string space (enlarging it if neccessary) and returns the offset into the array. pasc=1 ==> pascal-style string pasc=0 ==> C-style string; ignore max_leng and NONE strings */ { int leng, i; long p; if (pasc) { leng=buff[0]; if (leng>max_leng) leng=max_leng; } else leng=strlen(buff); if (ss_size empty string */ if (ss_end!=0) return (ss_end-1); /* Points to last \0 */ else { /* Very first string */ static_str[0]=0;ss_end=1; return 0; } } p=ss_end; /* Remember begining of string */ for(i=0;i=oflag_cnt) return -1; switch (t) { case 0: *ofs=attrtable[id].rbit; return attrtable[id].r; case 1: *ofs=attrtable[id].nbit; return attrtable[id].n; case 2: *ofs=attrtable[id].cbit; return attrtable[id].c; default: rprintf("INT ERROR: Invalid object type.\n"); return -1; } } long lookup_objprop(int id, int t) { if (id<0 || id>=oprop_cnt) return -1; switch (t) { case 0: return proptable[id].r; case 1: return proptable[id].n; case 2: return proptable[id].c; default: rprintf("INT ERROR: Invalid object type.\n"); return -1; } } int num_oattrs(int t, rbool isflag) { switch(t) { case 0: return isflag ? num_rflags : num_rprops; case 1: return isflag ? num_nflags : num_nprops; case 2: return isflag ? num_cflags : num_cprops; default: rprintf("INT ERROR: Invalid object type.\n"); return 0; } } rbool op_simpflag(uchar *pf, char ofs, int op) /* op: 0=clear, 1=set, 2=nop, 3=toggle two bits: */ { unsigned char mask, amask, bmask; mask=1<>1) << ofs ); bmask=(op&1)< %d\n",base,obj,ocnt,rval); */ return rval; } rbool have_objattr(rbool prop,integer obj,int id) { int t; char ofs; if (troom(obj)) t=0; else if (tnoun(obj)) t=1; else if (tcreat(obj)) t=2; else return 0; if (prop) return (lookup_objprop(id,t)>=0); else return (lookup_objflag(id,t,&ofs)>=0); } rbool op_objflag(int op, integer obj, int id) { /* op: 0=clear, 1=set, 2=nop, 3=toggle two bits: */ /* = (&)^ ) */ int index; int t, firstobj; char ofs; if (troom(obj)) {t=0;firstobj=first_room;} else if (tnoun(obj)) {t=1;firstobj=first_noun;} else if (tcreat(obj)) {t=2;firstobj=first_creat;} else return 0; index=calcindex(obj,firstobj,num_oattrs(t,1),lookup_objflag(id,t,&ofs)); if (index==-1) return 0; return op_simpflag(&objflag[index],ofs,op); } long op_objprop(int op, int obj, int id, long val) { /* op: 2=get, 1=set */ int index, t, firstobj; if (troom(obj)) {t=0;firstobj=first_room;} else if (tnoun(obj)) {t=1;firstobj=first_noun;} else if (tcreat(obj)) {t=2;firstobj=first_creat;} else return 0; index=calcindex(obj,firstobj,num_oattrs(t,0),lookup_objprop(id,t)); if (index==-1) return 0; if (op==2) return objprop[index]; else objprop[index]=val; return val; } const char *get_objattr_str(int dtype,int id,long val) { int max_val; if (dtype==AGT_OBJPROP) { if (!proptable || !propstr || id<0 || id>=oprop_cnt) return ""; max_val=proptable[id].str_cnt; if (val<0) val=0; if (val>=max_val) val=max_val-1; if (max_val>0) return propstr[ proptable[id].str_list + val ]; return ""; } else if (dtype==AGT_VAR) { if (!vartable || !propstr || id<0 || id>VAR_NUM) return ""; max_val=vartable[id].str_cnt; if (val<0) val=0; if (val>=max_val) val=max_val-1; if (max_val>0) return propstr[ vartable[id].str_list + val ]; return ""; } else if (dtype==AGT_OBJFLAG) { if (attrtable && id>=0 && id=0 && id<=FLAG_NUM) return val ? flagtable[id].ystr : flagtable[id].nstr; else return val ? "on" : "off"; } else rprintf("INTERNAL ERROR: Invalid data type for get_objattr_str()."); return ""; } /* ------------------------------------------------------------------- */ /* Warning and error functions */ /* ------------------------------------------------------------------- */ void agtwarn(const char *s,int elev) { if (ERR_LEVEL>=elev) rprintf("Warning: %s\n",s); } void agtnwarn(const char *s,int n,int elev) { if (ERR_LEVEL>=elev) rprintf("Warning: %s%d.\n",s,n); } void fatal(const char *s) { /* Should really change this so it uses only low-level primitives, instead of rprintf (which depends on writestr, etc.) */ rprintf("Fatal error: %s\n",s); close_interface(); exit(EXIT_FAILURE); } void init_flags(void) { rm_trap=1; DIAG=def_DIAG; interp_arg=def_interp_arg; debug_da1=def_debug_da1; RAW_CMD_OUT=def_RAW_CMD_OUT; ERR_LEVEL=def_ERR_LEVEL; irun_mode=0; fix_ascii_flag=fix_ascii; descr_maxmem=DESCR_BUFFSIZE; bold_mode=0; dbg_nomsg=0; /* Print out MSG arguments to metacommands */ debug_mode=0; dbgflagptr=NULL; dbgvarptr=NULL; dbgcntptr=NULL; no_auxsyn=0; text_file=0; #ifdef PATH_SEP gamepath=NULL; #endif BATCH_MODE=make_test=0; font_status=0; #ifdef OPEN_AS_TEXT open_as_binary=0; #endif }