/* object.c-- functions to manipulate rooms, nouns, and creatures */ /* 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 AGiliTy */ /* */ /* This module contains most of the routines that implement the */ /* "physics" of the AGT world, including the scope routines. */ #include #include #include #include "agility.h" #include "interp.h" #include "exec.h" /* ------------------------------------------------------------------- */ /* Functions for manipulating parse_recs */ /* ------------------------------------------------------------------- */ /* Make artificial parse record for an object */ parse_rec *make_parserec(int obj,parse_rec *rec) { if (rec==NULL) rec=rmalloc(sizeof(parse_rec)); rec->obj=obj; rec->info=D_NOUN; rec->noun=it_name(obj); rec->adj=it_adj(obj); rec->num=0; return rec; } /* This is used by the parser to initialize a blank parse_rec */ void tmpobj(parse_rec *objrec) { objrec->info=D_NOUN; objrec->num=0; objrec->noun=objrec->adj=0; objrec->obj=0; } parse_rec *copy_parserec(parse_rec *rec) { parse_rec *newrec; if (rec==NULL) return NULL; newrec=rmalloc(sizeof(parse_rec)); memcpy(newrec,rec,sizeof(parse_rec)); return newrec; } void free_all_parserec(void) { rfree(actor_rec);rfree(dobj_rec);rfree(iobj_rec); } /* ------------------------------------------------------------------- */ /* Functions for doing basic manipulation of items and extracting */ /* nformation about these items */ /* (often used to blackbox the difference between nouns and creatures) */ rbool matchclass(int obj, int oclass) { int i; if (oclass==0) return 0; for(i=obj;i!=oclass && i!=0;i=it_class(i)); return i==oclass; } /* Functions for getting bascic information about items */ static const char *it_sdesc(int item) { if (tnoun(item)) return noun[item-first_noun].shortdesc; if (tcreat(item)) return creature[item-first_creat].shortdesc; if (item<0) return dict[-item]; return NULL; } rbool it_possess(int item) { int l; l=it_loc(item); return (l==1 || l==1000); } rbool it_proper(int item) { if (tcreat(item)) return (!PURE_PROPER) || creature[item-first_creat].proper; if (tnoun(item)) return noun[item-first_noun].proper; return 0; } static rbool invischeck(const char *s) { while(rspace(*s)) s++; return strncasecmp(s,"INVISIBLE",9)==0; } static rbool it_invisible(int item, rbool sdesc_flag) { if (sdesc_flag) return invischeck(it_sdesc(item)); else { char *s; rbool tmp; if (it_name(item)==0 && it_adj(item)==0) return 1; s=objname(item); /* Must remember to rfree s before exiting */ tmp=invischeck(s); rfree(s); return tmp; } } static rbool it_appears_empty(int item) { int i; int sdesc_flag; if (item<0) return 1; sdesc_flag=!player_has(item); contloop(i,item) if (!it_invisible(i,sdesc_flag)) return 0; return 1; } /* We classify something as a weapon if it kills something. */ rbool it_isweapon(int objnum) { int i; creatloop(i) if (matchclass(objnum,creature[i].weapon)) return 1; return 0; } /* This used to be a macro like troom, tnoun, and tcreat, but it got too complicated and isn't used in time-critical areas, anyhow */ rbool it_door(int obj,word nword) { if (aver>=AGX00) return 0; /* No doors under Magx */ if (tdoor(obj)) return 1; /* The basic door */ if (it_loc(obj)==loc+first_room) return 0; return (nword==ext_code[wdoor]); } /* ------------------------------------------------------------------- */ /* Routines that manipulate the linked lists representing containment */ /* information */ static void set_contents(int p, int newval) { if (troom(p)) room[p-first_room].contents=newval; else if (p==1) player_contents=newval; else if (p==1000) player_worn=newval; else if (tnoun(p)) noun[p-first_noun].contents=newval; else if (tcreat(p)) creature[p-first_creat].contents=newval; else {writeln("INT ERR: Invalid object heading chain.");return;} } static void set_next(int p, int newval) { if (tnoun(p)) noun[p-first_noun].next=newval; else if (tcreat(p)) creature[p-first_creat].next=newval; else {writeln("INT ERR: Invalid object in chain.");return;} } void add_object(int loc,int item) { int p, q; set_next(item,0); if (loc==0) return; p=it_contents(loc); if (p==0 || p>item) { set_contents(loc,item); set_next(item,p); } else { /* Figure out where to put the item */ do { q=p; p=it_next(p); } while (p!=0 && p=maxroom && i!=1000 && cnt<40000L; cnt++) { i=it_loc(i); if (stop_if_closed && !it_open(i)) break; } if (cnt>=40000L) { /* writeln("GAME ERROR: Loop in object tree.");*/ return 0; } if (i==obj2) return 1; return 0; } int check_fit(int obj1, int obj2) /* Does obj1 fit inside obj2? Return one of the FIT_... values defined in exec.h */ { int size, weight; long net; assert(tnoun(obj1)); /* This should have been checked earlier */ if (obj2==1000) obj2=1; if (obj2==1) size=weight=100; else { assert(tnoun(obj2)); /* check_fit shouldn't be called otherwise */ size=noun[obj2-first_noun].size; weight=noun[obj2-first_noun].weight; } /* Weight */ if (obj2==1 || (aver>AGTME15 && aver=AGX00) net+=contweight(obj1); if (net>weight) return FIT_WEIGHT; if (obj2==1){ if (is_within(obj1,1,0) || is_within(obj1,1000,0)) net=0; net+=contweight(1); if (aver>=AGX00) net+=contweight(1000); if (!PURE_SIZE) net=0; } else { if (is_within(obj1,obj2,0)) net=0; /* Avoid double-counting */ net+=contweight(obj2); /* Net size of contents of obj2 */ } if (net>weight) return FIT_NETWEIGHT; } net=noun[obj1-first_noun].size; if (net>size) return FIT_SIZE; if (obj2==1 && !PURE_SIZE) return FIT_OK; if (obj2==1 || aver>AGTME15) { /* Pre-ME/1.56 interpreters didn't check this except for the player's inventory */ if (it_loc(obj1)==obj2 || (aversize) return FIT_NETSIZE; } return FIT_OK; } /* ------------------------------------------------------------------- */ /* Scope and visibility routines */ integer it_room(int item) { int tmploc; long cnt; cnt=0; while(!troom(item)) { tmploc=item; if (item==0) return 0; if (item==1 || item==1000) item=loc; else item=it_loc(item); if (item==tmploc || ++cnt>=40000L) { /* writeln("GAME ERROR: Loop in object tree."); */ return 0; } } return item; } rbool player_has(int item) { return is_within(item,1,0) || is_within(item,1000,0); } rbool in_scope(int item) /* strictly speaking, visible actually checks scope; this routine determines if an object would be in scope if there were no light problems. */ { int curloc; int tmp; long cnt; if (it_isglobal(item)) return 1; /* Global objects always in scope. */ /* Flag objects in scope if their associated flag is set. */ tmp=it_flagnum(item); if (tmp && (room[loc].flag_noun_bits & (1L<<(tmp-1)))) return 1; curloc=it_loc(item); /* Should work for nouns or creatures */ cnt=0; while (curloc>maxroom && curloc!=1000 && it_open(curloc)) { int tmploc; tmploc=it_loc(curloc); if (tmploc==curloc || ++cnt>=40000L) { /* writeln("GAME ERROR: Loop in the object tree."); */ return 0; } else curloc=tmploc; } if (curloc==1 || curloc==1000 || curloc==loc+first_room) return 1; else return 0; } static int good_light(int obj,int roomlight,rbool active) /* obj is a noun number */ /* If active is false, we don't care if the light is actually turned on is the valid light */ { if (roomlight==1 && !noun[obj].light) return 0; /* obj is not a light source */ if (roomlight>1) { if (!matchclass(first_noun+obj,roomlight)) return 0; /* Not the correct light */ else return 1; /* The correct light _always_ illuminates the room */ } if (!active) return 1; /* Now need to determine if obj is actually providing light */ if (!noun[obj].on) return 0; /* Light source is off or extinguished */ return 1; } int lightcheck(int parent,int roomlight,rbool active) /* This checks to see if anything contained in parent is a valid room light */ /* active=1 means that we only want active lights; active=0 indicates that extinguished light sources are okay. */ { int i; contloop(i,parent) { if (tnoun(i) && good_light(i-first_noun,roomlight,active)) return 1; if (it_open(i) && lightcheck(i,roomlight,active)) return 1; /* Check children */ } return 0; /* nounloop(i) if (good_light(i,room[loc].light) && in_scope(i+first_noun)) return 1; return 0;*/ } rbool islit(void) { if (room[loc].light==0) return 1; if (lightcheck(loc+first_room,room[loc].light,1)) return 1; if (lightcheck(1,room[loc].light,1)) return 1; /* Player carried light */ if (lightcheck(1000,room[loc].light,1)) return 1; /* Worn light */ return 0; } /* Is item visible to player? */ /* visible only works for "normal" items; if the object could be a virtual object (i.e. with negative item number), then use gen_visible() below */ rbool visible(int item) { assert(item>=0); if (islit()) return in_scope(item); else return player_has(item); } rbool genvisible(parse_rec *dobj) { int i; if (dobj->obj>0) return visible(dobj->obj); if (dobj->info==D_INTERN) { if (dobj->obj!=-ext_code[wdoor]) return 1; return islit(); /* If item is a is a door */ } if (dobj->info==D_GLOBAL || dobj->info==D_NUM) return 1; if (dobj->info==D_FLAG) { for(i=0;iobj==-flag_noun[i] && (room[loc].flag_noun_bits & (1L<info==D_PIX) { for(i=0;iobj==-pix_name[i] && (room[loc].PIX_bits & (1L<0) print_descr(dp[i],1); else if (!invischeck(shortdesc)) raw_lineout(shortdesc,1,MSG_DESC,NULL); else sysmsg(msgid,"$You$ see nothing unexpected."); } void it_describe(int dobj) { if (troom(dobj)) print_descr(room_ptr[dobj-first_room],1); else if (tnoun(dobj)) rundesc(dobj-first_noun,noun_ptr,noun[dobj-first_noun].shortdesc,194); else if (tcreat(dobj)) rundesc(dobj-first_creat,creat_ptr, creature[dobj-first_creat].shortdesc,195); else if (dobj==-ext_code[wdoor]) /* i.e. DOOR */ { if (room[loc].locked_door) sysmsg(21,"$You$ see a locked door."); else sysmsg(22,"$You$ see a perfectly normal doorway."); } else sysmsg(194,"$You$ see nothing unexpected."); if (tnoun(dobj) && (noun[dobj-first_noun].open || !noun[dobj-first_noun].closable) && !it_appears_empty(dobj)) { sysmsg(228,"Which contains:"); print_contents(dobj,1); } } static char *build_position(word prep,word name) /* Return the malloc'd string '$prep$ the $name$' */ { int leng; char *s; leng=strlen(dict[prep])+strlen(dict[name])+6; /* includes final '\0' */ s=rmalloc(leng*sizeof(char)); strcpy(s,dict[prep]); strcat(s," the "); strcat(s,dict[name]); assert(strlen(s)+1==leng); return s; } static int print_obj(int obj,int ind_lev) /* Prints out s on a line of its own if obj isn't INVISIBLE */ /* parent_descr is true if the parent has been described, false otherwise (say if the parent is invisible). */ /* ind_lev=indentation level */ /* Return 1 if we actually printed something, 0 if obj is invisible */ { int sdesc_flag; /* True if should print out as sdesc rather than as adjective-noun */ int i, retval, parent; const char *s; char *t, *s0, *posstr; if (tcreat(obj) && creature[obj-first_creat].initdesc!=0) return 0; /* Don't print normal description if printing initdesc */ s0=NULL; sdesc_flag=!player_has(obj); /* This should be tested. */ sdesc_flag=sdesc_flag||(ind_lev>1); /* It seems that AGT uses the sdesc for describing items contained in other items */ /* Some code below relies on this, as well */ if (sdesc_flag) s=it_sdesc(obj); else if (it_name(obj)==0 && it_adj(obj)==0) /* Invisible */ return 0; else { s0=objname(obj); /* Must remember to rfree s before exiting */ if (aver>=AGTME10) { for(t=s0;isspace(*t);t++); /* Skip over initial whitespace... */ *t=toupper(*t); /* ...and upcase the first non-space character */ } s=s0; } retval=0; if (sdesc_flag && tnoun(obj) && noun[obj-first_noun].initdesc!=0) { retval=1; msgout(noun[obj-first_noun].initdesc,1); noun[obj-first_noun].initdesc=0; /* Only show it once */ } else if (!invischeck(s)) { retval=1; /* We're actually going to print something */ for(i=0;i=first_noun && it_invisible(parent,sdesc_flag) && (it_name(parent)!=0 || it_adj(parent)!=0) ) { /* If the parent object *isn't* invisible, we will already have printed it out */ /* This also relies on sdesc_flag being the same for parent and child objects */ if (parent>=first_creat && parent<=maxcreat) sysmsg(221,"(Carried by"); else sysmsg(222," (Inside"); t=objname(parent); writestr(t);rfree(t); sysmsg(223,")"); } if (tnoun(obj) && noun[obj-first_noun].light && noun[obj-first_noun].on && PURE_OBJ_DESC) sysmsg(220," (Providing light)"); writeln(""); } if (!sdesc_flag) rfree(s0); return retval; } int print_contents(int obj,int ind_lev) /* obj=object to list contents of; ind_lev=indentation level */ /* Returns number of objects contained in obj that were listed */ { int i, cnt; cnt=0; contloop(i,obj) { if (print_obj(i,ind_lev)) cnt++; if (it_open(i)) print_contents(i,ind_lev+1); } return cnt; } /* ------------------------------------------------------------------- */ /* Routines for directly getting and setting object properties and */ /* attributes. */ #define NUM_WPROP 6 #define NUM_WATTR 6 static void *compute_addr(int obj, int prop, const prop_struct *ptable) { int ofs; void *base; if (DIAG) rprintf("(Accessing %s->%s)\n",dict[it_name(obj)],ptable[prop].name); if (troom(obj)) { base=(void*)(&room[obj-first_room]); ofs=ptable[prop].room; } else if (tnoun(obj)) { base=(void*)(&noun[obj-first_noun]); ofs=ptable[prop].noun; } else if (tcreat(obj)) { base=(void*)(&creature[obj-first_creat]); ofs=ptable[prop].creature; } else return NULL; if (ofs==-1) /* Field doesn't exist in this type of object */ return NULL; return (void*) ( ((char*)base) + ofs ); } long getprop(int obj, int prop) { integer *paddr; if (prop>=NUM_PROP) return 0; paddr=(integer*)compute_addr(obj,prop,proplist); if (paddr==NULL) return 0; return *paddr; } void setprop(int obj, int prop, long val) { integer *paddr; if (prop>=NUM_WPROP) { writeln("GAME ERROR: Read-only or non-existant property."); return; } paddr=(integer*)compute_addr(obj,prop,proplist); if (paddr==NULL) { writeln("GAME ERROR: Property-object mismatch."); return; } *paddr=val; } rbool getattr(int obj, int prop) { rbool *paddr; if (prop>=NUM_ATTR) return 0; paddr=(rbool*)compute_addr(obj,prop,attrlist); if (paddr==NULL) return 0; return *paddr; } void setattr(int obj, int prop, rbool val) { rbool *paddr; if (prop>=NUM_WATTR && prop!=24) { writeln("GAME ERROR: Read-only or non-existant attribute."); return; } paddr=(rbool*)compute_addr(obj,prop,attrlist); if (paddr==NULL) { writeln("GAME ERROR: Property-object mismatch."); return; } *paddr=val; } /* ------------------------------------------------------------------- */ /* This sets up the creat_fix[] array, which is used to determine the */ /* scan ranges for addressed creatures in cases where there is more */ /* than one creature of the same name */ void init_creat_fix(void) { int i,j; creat_fix=rmalloc(rangefix(maxcreat-first_creat+1)*sizeof(integer)); for(i=0;i i */ } void free_creat_fix(void) { rfree(creat_fix); } /* ------------------------------------------------------------------- */ #ifndef IT_MACRO int it_contents(integer obj) { if (tnoun(obj)) return noun[obj-first_noun].contents; else if (troom(obj)) return room[obj-first_room].contents; else if (tcreat(obj)) return creature[obj-first_creat].contents; else if (obj==1) return player_contents; else if (obj==1000) return player_worn; else return 0; } rbool it_lockable(integer obj, word nword) { if (tnoun(obj)) return noun[obj-first_noun].lockable; else if (it_door(obj,nword)) return 1; else return 0; } rbool it_locked(integer obj, word nword) { if (tnoun(obj)) return noun[obj-first_noun].locked; else if (it_door(obj,nword) && room[loc].locked_door) return 1; else return 0; } #endif