/* agil.c-- initialization and the main execution loop */ /* 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: the (Mostly) */ /* Universal AGT Interpreter */ /* This file contains the initialization and clean-up code as well as the interpreter's main loop. */ /* Overview of the source for AGiliTy: agil.c --initialization --tokeniser parser.c: --parser --disambiguation --menuing system exec.c --verb execution wrapper --standard utilities for running actions runverb.c: --standard verbs and the main routine for executing the player's action. object.c --Utility functions for manipulating game objects (nouns, creatures, etc.) including scope routines. metacommand.c --metacommand execution: scan the metacommands and execute them in the proper order. The actual execution of each individual command is handled in token.c. token.c --this module contains the actual code to run each of the (several hundered) different metacommands; called from metacommand.c. debugcmd.c --Routines to execute debugging verb savegame.c --save and restore --undo and restart util.c --general utilities --memory management --buffered file i/o interface.c --high level input/output --general utilities disassemble.c --Metacommand debugging output os_.c --low level input/output --other platform dependent code */ #include #include "agility.h" #include "interp.h" /* The following are AGT 'purity' flags; they turn off features of */ /* my interpreter that are not fully consistent with the original AGT */ /* and so could break some games. Some of these are trivial improvements; */ /* some are more radical and should be used with caution. Several are */ /* only useful if a game was designed with them in mind. */ /* In all cases, setting the flag to 1 more closely follows the */ /* behavior of the original interpreters */ /* WARNING: Most of these haven't been tested in the non-default state. */ /* Most of these will eventually become variables settable by the user */ /* or from a (possibly game-specific) configuration file */ rbool PURE_INPUT=1; /* Is the input line bold? */ rbool PURE_TONE=0; /* Is low level sound enabled? */ /*-------------------------------------------------------------------*/ /* Misc. things to support the tokenizer and the dictionry. */ /*-------------------------------------------------------------------*/ /* The following should not be changed without also changing the wtype enum statement in interp.h to match */ static const char* ext_voc[]= {"the","my","a","an", /* These 4 are ignored in canonical AGT */ "then",".",";","and", "," , "its", "all","undo", "look","g", "pick","go","exits","talk","take","door","again","but","except", "scene","everything","listexit","listexits","close", "verb","noun","adjective","prep","object","name","step", " any","either","both","everyone","everybody", "he","she","it","they","him","her","them","is","are","oops", "was","were","scream", /* Everything between 'in' and 'about' should be a preposition */ "in","out", "into","at","to","across","inside","with","near","for", "of","behind", "beside", "on", "off", "under", "from","through", "toward", "towards", "between", "around", "upon", "thru", "by", "over", "up", "down", "about"}; /* 'about' must be the last element of this list */ /*-------------------------------------------------------------------*/ /* Routines to read in and use various auxilary files. */ /* (.TTL, .INS, .VOC, .CFG) */ /*-------------------------------------------------------------------*/ static rbool emptyline(unsigned char *s) /* Check if s consists only of white space and control characters */ { unsigned char *p; for(p=s;*p!=0;p++) if (!rspace(*p) && *p>26) return 0; return 1; } static void print_title(fc_type fc) { int height; signed char center_mode; /* Center title? */ descr_line *buff; char *s; rbool skip_line; /* Skip first line: it has COLORS */ if (agx_file) buff=read_descr(title_ptr.start,title_ptr.size); else buff=read_ttl(fc); if (buff==NULL) { char *s; writeln("");writeln(""); s=formal_name(fc,fNONE); if (s!=NULL) { s[0]=toupper(s[0]); agt_center(1); agt_textcolor(-1); writeln(s); agt_textcolor(-2); agt_center(0); rfree(s); } writeln("");writeln(""); if (avervm_width) vm_width=n; } } /* .CFG reading routines -------------------------------------------- */ #define opt(s) (strcasecmp(optstr[0],s)==0) /* These are the interpreter specific options; this is called from cfg_option in agtdata.c. */ void agil_option(int optnum,char *optstr[], rbool setflag, rbool lastpass) { if (opt("ibm_char")) fix_ascii_flag=!setflag; else if (!lastpass) return; /* On the first pass through the game specific file, we ignore all but the above options */ else if (opt("tone")) PURE_TONE=setflag; else if (opt("input_bold")) PURE_INPUT=setflag; else if (opt("force_load")) FORCE_VERSION=setflag; else if (opt("stable_random")) stable_random=setflag; else if (!agt_option(optnum,optstr,setflag)) /* Platform specific options */ rprintf("Invalid option %s\n",optstr[0]); } /*-------------------------------------------------------------------*/ /* Tokeniser: Split input into words and look them up in dictionary */ /*-------------------------------------------------------------------*/ static rbool noise_word(word w) { if (w==ext_code[wthe] || w==ext_code[wa] || w==ext_code[wan]) return 1; if (w==ext_code[wmy]) return 1; if (aver>=AGT18 && aver<=AGT18MAX && w==ext_code[wis]) return 1; return 0; } static rbool check_dot(char *prevtext, int prevcnt, char *lookahead) /* This routine is devoted to trying to figure out whether we should treat '.' as punctuation or as a letter. */ /* It returns true if '.' should be treated as punctuation. */ /* prevtext=the current word, as far as it has been parsed. prevcnt=the number of letters in prevtext [which is *not* 0 terminated] */ /* lookahead=the rest of the current input line *after* the period. */ { int i, endword, restcnt; if (!PURE_DOT) return 1; /* No words with periods in them, so it must be punctuation. */ /* We just start scanning the dictionary to see if any of them are possible matches, looking ahead as neccessary. */ /* Find the next unambiguous word end. This ignores possible word ends caused by periods. */ for(endword=0;lookahead[endword]!=0;endword++) if (isspace(lookahead[endword]) || lookahead[endword]==',' || lookahead[endword]==';') break; for(i=0;iendword) continue; /* Dictionary entry is longer than following text */ /* Check to see if the dictionary entry can be found in the lookahead buffer */ if (strncasecmp(lookahead,dict[i]+prevcnt+1,restcnt)!=0) continue; if (restcnt==endword) return 0; /* We have a match */ /* At this point, we know that restcnt0) /* End of word: add it to input */ { in_text[ip][j]=0; input[ip]=search_dict(in_text[ip]); if (input[ip]==-1) input[ip]=0; else if (input[ip]==0) input[ip]=ext_code[w_any]; /* _Real_ 'ANY' */ if (!noise_word(input[ip])) ip+=1; /* i.e. if not one of the four ignored words, advance */ j=0; } /* If j=0 and not punct, then no new word; just skip the whitespace */ if (punctuation) { in_text[ip][0]=buff[k];in_text[ip][1]=0; input[ip]=search_dict(in_text[ip]); if (input[ip]==-1) input[ip]=0; j=0;ip++; } if (ip>=MAXINPUT-1) { writeln("Too many words in input; ignoring rest of line."); break; } if (buff[k]==0) break; } input[ip]=-1; in_text[ip][0]=0; } /*-------------------------------------------------------------------*/ /* Main game loop: Get player input and call the parser. */ /*-------------------------------------------------------------------*/ static void game_end(void) { rbool done_flag; char *s; if (winflag || deadflag) { writeln("");writeln("");agt_center(1); if (winflag) gen_sysmsg(148,"***** $You$ have won! *****",MSG_MAIN,NULL); if (deadflag) gen_sysmsg(147,"***** $You$ have died! *****",MSG_MAIN,NULL); writeln("");writeln("");agt_center(0); } if (deadflag && !endflag) { if (curr_lives>1) { /* Resurrection code */ if (curr_lives==max_lives) gen_sysmsg(151,"Hmmm.... so $you$'ve gotten $your$self killed. " "Would you like me to try a resurrection?",MSG_MAIN,NULL); else gen_sysmsg(152," $You$'ve died *again*. " "Would you like me to try another resurrection?", MSG_MAIN,NULL); if (yesno("? ")) { /* Now do resurrection */ curr_lives--; quitflag=deadflag=0; gen_sysmsg(154, "$You$ emerge coughing from a cloud of dark green smoke.", MSG_MAIN,NULL); writeln(""); loc=resurrect_room-first_room; newlife_flag=1; set_statline(); do_look=do_autoverb=1; newroom(); return; } else writeln("As you wish..."); } else if (max_lives>1) gen_sysmsg(153,"$You$'ve used up all of $your$ lives.",MSG_MAIN,NULL); } writeln(""); print_score(); writeln(""); done_flag=quitflag; /* If player has QUIT, don't ask again */ while (!done_flag && !quitflag) { writestr("Would you like to "); if (restart_state!=NULL) writestr("restart, "); writestr("restore"); if (undo_state!=NULL && can_undo) writestr(", undo,"); else if (restart_state!=NULL) writestr(","); writestr(" or quit? "); s=agt_readline(5); if (strncasecmp(s,"RESTART",7)==0) if (restart_state!=NULL) {restart_game();done_flag=1;} else writeln("Sorry, I'm unable to do that because of limited memory."); else if (strncasecmp(s,"RESTORE",7)==0) if (loadgame()) {done_flag=1;} else writeln("(RESTORE failed)"); else if (strncasecmp(s,"UNDO",4)==0) if (can_undo && undo_state!=NULL) {putstate(undo_state);done_flag=1;} else writeln("Insufficiant memory to support UNDO"); else if (toupper(s[0])=='Q') {quitflag=1;done_flag=1;} } set_statline(); } static void parse_loop(void) /* This exists to deal with THEN lists; parse() handles the indiviudual commands */ { for(ip=0;ip>=0 && ip=0 && ip --> EXAMINE */ for(i=0;i0 || command[i].noun_adj>0 || command[i].objcmd>0 || command[i].obj_adj>0 || command[i].prep>0) ) smart_look=0; } pictable=rmalloc(sizeof(int)*maxpict); for(i=0;i 'dummy verb5' */ dict[ syntbl[auxsyn[21]] ][6]=' '; /* change_locations */ dict[ syntbl[auxsyn[55]] ][5]=' '; /* magic_word */ } if (!PURE_SUBNAME) /* Replace the 'e' by a space */ for(i=0;i=AGT18 && aver<=AGT18MAX) PURE_DOT=0; else { PURE_DOT=FORCE_PURE_DOT; for(i=0;i Views an illustration. (Not supported on all " "platforms.)"); writeln(" BRIEF Don't print room descriptions for rooms you've seen."); writeln(" VERBOSE Print room descriptions even for rooms you've already " "seen."); writeln(" LIST EXITS List the exits from a room."); writeln(" LISTEXIT ON/OFF Turn on/off automatic listing of exits."); writeln(" SCRIPT Start sending a transcript to a file."); writeln(" UNSCRIPT Stop creating a transcript."); writeln(" SOUND ON, OFF Turn sound on/off."); writeln(" LOG Start sending all of your commands to a file."); writeln(" REPLAY Replay your commands from a file, " "executing one every seconds."); writeln(" REPLAY FAST Replay your commands from a file without waiting " "for you to read the scrolling text."); writeln(" REPLAY STEP Replay your commands from a file, " "one for every keypress."); writeln(" AGILDEBUG Access debugging commands."); writeln(" MENU Toggle menu mode on or off."); writeln(" OOPS Correct a word you just mistyped; must be the first " "command on a line."); writeln(" UNDO Undo your last move; must be the first command on a line."); writeln(" SAVE Save the game."); writeln(" RESTORE Restore the game."); writeln(" RESTART Restart the game."); writeln(" QUIT Quit."); writeln(""); } /* This is a hack to get rid of the "What Now?" prompt. */ static void fix_prompt(void) { descr_line *d; if (err_ptr==NULL) return; d=read_descr(err_ptr[0].start,err_ptr[0].size); if (d==NULL) return; if (strncasecmp(d[0],"What Now?",9)==0) err_ptr[0].size=err_ptr[0].start=0; free_descr(d); } void close_game(void); /* Called by setup_game, and so needs to be defined here. */ static fc_type setup_game(fc_type fc) /* game_name is the common filename of the AGT game files */ { int can_save; char choice; rbool have_ins; bold_mode=0; rm_acct=1;rm_trap=1; rm_size=ralloc_cnt=rfree_cnt=0; mars_fix=0; no_auxsyn=0; debug_disambig=0;debug_any=1; dbg_nomsg=1; /* Supress output of MSG arguments to metacommands */ textbold=0;debug_mode=0;aver=0; verboseflag=1;notify_flag=0;logflag=0;menu_mode=0; fast_replay=0; stable_random=BATCH_MODE || make_test; if (make_test) BATCH_MODE=0; hold_fc=fc; set_default_filenames(fc); init_stack(); read_config(agt_globalfile(0),1); /* Global configuration file */ /* Now that we *have* PATH information, go looking for the games */ /* At the very least, it creates an rmalloc'd copy of game_name */ read_config(openfile(fc,fCFG,NULL,0),0); text_file=0; /* First pass through game specific config file */ build_trans_ascii(); #ifdef PROFILE resetwatch(); #endif writeln("Loading game..."); if (!read_agx(fc,0) && !readagt(fc,0)) fatal("Unable to load game."); #ifdef PROFILE writeln(stopwatch()); agt_waitkey(); #endif if (have_opt) menu_mode=opt_data[5]; /* See agtread.c for discussion of OPT file format */ text_file=1; read_config(openfile(fc,fCFG,NULL,0),1);/*Game specific config file*/ text_file=0; if (min_ver>AGIL_VERID) { if (FORCE_VERSION) agtwarn("This game requires a later version of AGiliTy.",0); else fatal("This game requires a later version of AGiliTy."); } sort_cmd(); ext_dict(); build_verbmenu(); fix_dummy(); /* Prevent player from calling dummy verbs or subroutines by typing 'Subroutine n' on the command line */ can_save=init(); if (!agx_file) open_descr(fc); fix_prompt(); /* Kill off 'What Now?' prompt. */ if (BATCH_MODE || make_test) set_test_mode(fc); start_interface(fc); fontcmd(2,0); /* Set initial font */ if (intro_first && intro_ptr.size>0) { agt_clrscr(); print_descr(intro_ptr,1); wait_return(); } if (aver>=AGTME10) pictcmd(3,0); /* Show title image, if there is one */ print_title(fc); have_ins=open_ins_file(fc,0); do { if (have_ins) writestr("Choose nstructions, GiliTy Information, " "or to start the game"); else writestr("Choose GiliTy Information or to start the game"); choice=tolower(agt_getchar()); /* Wait for keypress */ agt_clrscr(); if (have_ins && choice=='i') print_instructions(fc); else if (choice=='a') print_license(); } while ( (choice=='i' && have_ins) || choice=='a'); close_ins_file(); if (!intro_first && intro_ptr.size>0) { print_descr(intro_ptr,1); wait_return(); agt_clrscr(); } if (maxroom