pdirt/data/
pdirt/data/HELP/
pdirt/data/HELP/0/
pdirt/data/HELP/F/
pdirt/data/HELP/G/
pdirt/data/HELP/H/
pdirt/data/HELP/J/
pdirt/data/HELP/K/
pdirt/data/HELP/O/
pdirt/data/HELP/Q/
pdirt/data/HELP/R/
pdirt/data/HELP/U/
pdirt/data/HELP/V/
pdirt/data/HELP/Y/
pdirt/data/HELP/Z/
pdirt/data/MESSAGES/
pdirt/data/POWERINFO/
pdirt/data/WIZ_ZONES/
pdirt/drv/
pdirt/drv/bin/
pdirt/drv/compiler/converter/
pdirt/drv/compiler/libs/
pdirt/drv/compiler/scripts/
pdirt/drv/include/AberChat/
pdirt/drv/include/InterMud/
pdirt/drv/include/machine/
pdirt/drv/src/InterMud/
pdirt/drv/src/Players/
pdirt/drv/utils/UAFPort/
pdirt/drv/utils/dnsresolv/
pdirt/drv/utils/gdbm/
#include "kernel.h"
#include "hash.h"
#include "memstat.h"

/********************************************************************************
 **                           Gen Global Stuff               
 *******************************************************************************/
#define USE_PIPE
#define FILENAMESIZE 	120    /* the max length of a filename */
#define HASHTABLESIZE 	7211  /* the size of the hash table */
#define GEN_VERSION	"Proto 8.1"

#ifdef GENMAIN_C

int  num_zones = 0;

#else

extern int num_zones;

#endif

#ifdef PARSER_C
int cur_loc = -1;
int cur_mob = 0;
int cur_obj = 0;
char *fn_extension = ".area";
#else

extern int cur_loc;
extern int cur_mob;
extern int cur_obj;
char *fn_extension;
#endif

#ifdef HASH_C
long mem_usage = 0;
#else
extern long mem_usage;
#endif

/*******************************************************************************
 **                           Lexer Stuff                                  
 ******************************************************************************/

#define TOKENSTRLEN   512   /* the max string length of a token string */

/* defines for token types */

#define T_LOCATION    1
#define T_OBJECT      2
#define T_MOBILE      3
#define T_NAME        4
#define T_ALTNAME     5
#define T_LINKED      6
#define T_BASEVALUE   7
#define T_STATE       8
#define T_MAXSTATE    9
#define T_ARMOR       10
#define T_DAMAGE      11
#define T_SIZE        12
#define T_WEIGHT      13
#define T_EXAMINE     14
#define T_LFLAGS      15
#define T_HINT        16
#define T_OFLAGS      17
#define T_TRAP        18
#define T_ENDTRAP     19
#define T_ENDLOCATION 20
#define T_ENDOBJECT   21
#define T_ENDMOBILE   22
#define T_STRENGTH    23
#define T_SPEED       24
#define T_AGGRESSION  25
#define T_WIMPY       26
#define T_MFLAGS      27
#define T_PFLAGS      28
#define T_SFLAGS      29
#define T_NORTH       30
#define T_SOUTH       31
#define T_EAST        32
#define T_WEST        33
#define T_UP          34
#define T_DOWN        35
#define T_NORTHEAST   36
#define T_NORTHWEST   37
#define T_SOUTHEAST   38
#define T_SOUTHWEST   39
#define T_TITLE       40
#define T_TEMP        41
#define T_ZONE        42
#define T_AUTHOR      43
#define T_QUEST       44
#define T_DATA        45
#define T_ENDDATA     46
#define T_ENDZONE     47
#define T_AMPER       48
#define T_LBRAQ       49
#define T_RBRAQ       50
#define T_LPAREN      51
#define T_RPAREN      52
#define T_CARROT      53
#define T_QUOTE       54
#define T_PLUS        55
#define T_MINUS       56
#define T_MULT        57
#define T_DIVIDE      58
#define T_AND         59
#define T_OR          60
#define T_SEMICOLON   61
#define T_EQUALS      62
#define T_POUND       63
#define T_COLON       64
#define T_LSQUIG      65
#define T_RSQUIG      66
#define T_PERCENT     67
#define T_DESC        68
#define T_DESC1       69
#define T_DESC2       70
#define T_DESC3       71
#define T_DESC4       72
#define T_INROOM      73
#define T_INCONTAINER 74
#define T_CARRIEDBY   75
#define T_WORNBY      76
#define T_WIELDEDBY   77
#define T_BOTHBY      78
#define T_PNAME       79
#define T_START       80
#define T_END         81
#define T_CODE        82
#define T_STR         83
#define T_NUMBER      84
#define T_BOOL        85
#define T_IDENT       86
#define T_COMMA       87
#define T_FOR         88
#define T_IF          89
#define T_WHILE       90
#define T_REPEAT      91
#define T_UNTIL       92
#define T_THEN        93
#define T_ELSE        94
#define T_BEGIN       95
#define T_LOOP        96
#define T_TO          97
#define T_TRUE        98
#define T_FALSE       99
#define T_CONSTANT   100
#define T_USETRAP    101
#define T_VISIBILITY 102
#define T_APOST      103
#define T_RIGHTAPOST 104
#define T_LAST       105
#define T_NUMERICAL  106
#define T_STRING     107
#define T_IDENTIFIER 108
#define T_BOOLEAN    109
#define T_OTHER      110
#define T_DOUBLEEQ   111

#ifdef LEXER_C
/* table for finding or displaying the token type based on the numerical value */
char *TokenNames[] = {"location",   "object",       "mobile",     "name",       "altname", 
      "linked",       "basevalue",  "state",        "maxstate",   "armor",      "damage",     
      "size",         "weight",     "examine",      "lflags",     "hint",       "oflags",    
      "trap",         "endtrap",    "endlocation",  "endobject",  "endmobile",  "strength",  
      "speed",        "aggression", "wimpy",        "mflags",     "pflags",     "sflags",    
      "north",        "south",      "east",         "west",       "up",         "down",       
      "northeast",    "northwest",  "southeast",    "southwest",  "title",      "temp",
      "zone",         "author",     "quest",        "data",       "enddata",    "endzone", 
      "@",            "[",          "]",            "(",          ")",          "^",          
      "\"",           "+",          "-",            "*",          "/",          "and",        
      "or",           ";",          "=",            "#",          ":",          "{",   
      "}",            "%", 
      "desc",         "desc0",      "desc1",        "desc2",      "desc3",      "in_room",
      "in_container", "carried_by", "worn_by",      "wielded_by", "both_by",    "pname",
      "start",        "end",        "code",         "string",     "number",     "boolean",
      "ident",        ",",          
      "for",          "if",         "while",        "repeat",     "until",      "then", 
      "else",         "begin",      "loop",         "to",         "true",       "false",
      "constant",     "usetrap",    "visibility",   "'",          "`"
        };



#else
extern char *TokenNames[];
#endif


/* token record, holds data for the token taken from the file */
typedef struct _token_record {
   int                  token_type;   /* type from def. above, type of token */
   char                 the_string[TOKENSTRLEN]; /* Holds the token string */
   int                  line;         /* Holds the line token was taken from */
   struct _token_record *more;        /* more strings, used for desc and like */
   struct _token_record *next_tok;    /* used for keeping track so we can free
                                         the tokens easily */
} token_record;

/********************************************************************************************
 **                           Parser Non-Mudcode Stuff
 *******************************************************************************/

#define MAXNAMLEN      30  /* the largest size a zone name can be */
#define MAXQUESTNAMLEN 30  /* the largest size a quest name can be */
#define MAXTITLLEN     80  /* the length that a title string can be */
#define MAXDEFSTRLEN  256  /* the max length of strings used for string arithmatic */

#define MAX_ERRORS     15  /* the max number of errors you can get before it stops */

#define DEF_MOB_DAMAGE          4       /* Hit power of a mobile  (0-30) */
#define DEF_MOB_SPEED           5       /* Speed of a mobile (0-99) */
#define DEF_MOB_AGGRESSION      0       /* Aggression of a mobile (0-99) */
#define DEF_MOB_ARMOR           0       /* Armor of a mobile (0-MAX_ARMOR) */
#define DEF_MOB_STRENGTH        80      /* Strength (0-3000) */
#define DEF_MOB_WIMPY           0       /* Wimpy level (0-DEF_MOB_STRENGTH) */
#define DEF_MOB_VIS             0       /* Visibility level */

#define DEF_OBJ_SIZE            0       /* Size of an object (0-)*/
#define DEF_OBJ_STATE           0       /* Begin state of an object (0-3) */
#define DEF_OBJ_DAMAGE          0       /* Damage of an object (0-30) */
#define DEF_OBJ_ARMOR           0       /* Armor provided by object (0-MAX_ARMOR) */
#define DEF_OBJ_BVALUE          0       /* Base Value of an object (0->) */
#define DEF_OBJ_WEIGHT          0       /* Base weight of an object */
#define DEF_OBJ_VIS             0       /* Visibility level */


/* to hold a list of strings, uses less memory than keeping them in token form */
typedef struct _string_record {
   /*char                  the_string[TOKENSTRLEN];*/
   char			   *the_string;
   struct _string_record *nextstr;
} string_record;

/* to hold a list of locations */
typedef struct _loc_str_record {
   char                   the_loc[(MAXNAMLEN*2)+1];
   int                    loc_type;
   struct _loc_str_record *nextloc;
} loc_str_record;

struct _trap_record;
struct _stmt_list_record;

/* the record that holds information on a location in the zone which it resides below */

typedef struct _location_record {
   char                    *name;              /* the name of the loc */
   char                    *north;             /* loc to the north */
   char                    *south;             /* loc to the south */
   char                    *east;              /* loc to the east */
   char                    *west;              /* loc to the west */
   char                    *up;                /* loc to the up */
   char                    *down;              /* loc to the down */
   char                    *northeast;         /* loc to the ne */
   char                    *northwest;         /* loc to the nw */
   char                    *southeast;         /* loc to the se */
   char                    *southwest;         /* loc to the sw */
   char                    *title;             /* the title of the loc */ 
   string_record           *desc;              /* the room description */
   string_record           *hint;              /* a hint at this loc */
   DoubleLongInt           lflags;             /* the loc flags */  
   struct _trap_record     *loc_code;          /* loc code if any exists */ 
   struct _location_record *next_loc;          /* next loc record */
} location_record;


/* the record that holds information on a mobile in the zone which it resides below */

typedef struct _mobile_record {
   char                  *mname;              /* the mobile name */
   char                  *name;               /* the name of the mobile */
   loc_str_record        *location;           /* the location of the object */
   int                   strength;            /* the mobile's total strength */
   int                   damage;              /* the damage a mobile can do */
   int                   armor;               /* the armor a mobile has */
   int                   speed;               /* how fast a mobile moves */
   int                   aggr;                /* the aggression of a mobile */
   int                   wimpy;               /* the wimpy value of a mobile */
   int                   vis;                 /* mobile visibility */
   string_record         *examine;            /* the examine text */
   string_record         *desc;               /* the description text */
   string_record         *hint;               /* a hint at this mobile */
   MFLAGS                mflags;              /* the mflags of this mobile */    
   DoubleLongInt         pflags;              /* the pflags of this mobile */  
   DoubleLongInt         sflags;              /* the sflags of this mobile */
   struct _trap_record   *mob_code;           /* mobiles code if any exists */
   struct _mobile_record *next_mob;           /* next mobile record */
} mobile_record;


/* the record that holds information on an object in the zone which it resides below */

typedef struct _object_record {
   char                  *oname;              /* the object name */
   char                  *name;               /* the name of the object */
   char                  *altname;            /* an alternate name */
   loc_str_record        *location;           /* the locations of the object */
   char                  *linked;             /* object this obj is linked to */
   int                   basevalue;           /* the base value of the object */
   int                   state;               /* the state of the object */
   int                   maxstate;            /* the max state of the object */  
   int                   armor;               /* the armor value of the object */
   int                   damage;              /* the damage value of the obj. */
   int                   size;                /* the size value of the object */
   int                   weight;              /* the weight value of the obj. */
   int                   vis;                 /* object visibility */
   string_record         *examine;            /* the examine text */
   string_record         *desc[4];            /* array of description texts */
   string_record         *hint;               /* a hint at this object */
   DoubleLongInt         oflags;              /* the oflags of this obj. */ 
   struct _trap_record   *obj_code;           /* obj. code if any exists */
   struct _object_record *next_obj;           /* next object record */
} object_record;


/* the record that holds all zone information, plus a pointer to all zone data and a pointer to
   the next zone record */

typedef struct _zone_record {
   char                zonefile[FILENAMESIZE]; /* file the zone is assoc. with */
   char                *zonename;              /* the zone name */
   char                *author;                /* the author of the zone */
   char                *quest;                 /* quest that is in the zone */
   location_record     *location_data;         /* pointer to all loc. records */
   mobile_record       *mobile_data;           /* pointer to all mob. records */
   object_record       *object_data;           /* pointer to all obj. records */
   struct _stmt_list_record *code;
   struct _zone_record *next_zone;             /* pointer to the next zone record */   
} zone_record;

/*********************************************************************************
 **                           Parser Mudcode Loading Stuff
 *********************************************************************************/

#define MAXCODENAMLEN 60   /* the max length the name of a mudcode entry can be */


/* holds all code up to the next parameter, w/ a pointer to code after parameter */
typedef struct _thecode_record {
   string_record           *code_str;   /* holds a list of lines of code */
   int                     param_type;  /* param type - comes after code string */
   int                     param_num;   /* param number-comes after code string */
   struct _thecode_record  *next_code;  /* pointer to the next code record */
} thecode_record;

/* stores the parameters in order for the mudcode function */
typedef struct _param_record {
   int                   param_type;
   int                   param_num;
   struct _param_record  *next_param;
} param_record;


/* the record that holds all def. info that is defined in the file mudcode.lib */
typedef struct _mudcode_record {
   char                    code_name[MAXCODENAMLEN]; /* the name of this mudcode */
   param_record            *parameters;              /* a list of param. allowed */
   thecode_record          *the_code;      /* list of code that it converts to */
   struct _mudcode_record  *next_mudcode;  /* the next mudcode entry */
} mudcode_record;

/********************************************************************************
 **                           Actual Mudcode Parsing
 ********************************************************************************/

#define MAXBOOLLEN 120

#define STMT_VAR_DECL      0  /* a variable declaration */
#define STMT_CONST_DECL    1  /* a constant declaration */
#define STMT_FOR_LOOP      2  /* a for loop */
#define STMT_WHILE_LOOP    3  /* a while loop */
#define STMT_REPEAT_UNTIL  4  /* a repeat until loop */
#define STMT_IF_THEN       5  /* an if then segment */
#define STMT_BLOCK         6  /* a begin end block */
#define STMT_CASE          7  /* a case statement */
#define STMT_RAW_CODE      8  /* actual raw C code */ 
#define STMT_MUD_DEFINED   9  /* mudcode defined in the mudcode.lib file */
#define STMT_ARITH_STR    10  /* an arithmetic string */
#define STMT_ARITH_NUM    11  /* an arithmetic number */
#define STMT_IDENT        12  /* if it is an actual identifier */
#define STMT_ASSIGNMT     13  /* if it is an assignment */

/******************** global mudcode structs *************************************/

/* holds the type, used for typecasting to the correct mudcode record */
struct _stmt_record;


/* holds a linked list of all the statements */
typedef struct _stmt_list_record {
   struct _stmt_record       *the_statement;
   struct _stmt_list_record  *more_statements;
} stmt_list_record;


typedef struct _trap_desc {
   int                  trap_verb;
   char                 trap_location[(MAXNAMLEN*2)+5];
   struct _trap_desc    *next_desc;   
} trap_desc;

/* holds a trap record, with all code attached to it */
typedef struct _trap_record {
   trap_desc            *trap_verb;
   stmt_list_record     *the_statements;
   struct _trap_record  *more_traps;
} trap_record;

/********************* assignments ***************************/

typedef struct _assignmt {
   char                  var_name[MAXNAMLEN];
   struct _stmt_record   *the_stmt;
} assignmt;

/********************* arithmetic declarations ***************************/

typedef struct _arith_number {
   char                  arith_str[MAXBOOLLEN];
   struct _stmt_record   *numberfunct;
   struct _arith_number  *next_number;
} arith_number;

typedef struct _arith_string {
   string_record         *string1;
   struct _stmt_record   *stringfunct1;
   int                   the_op;
   struct _stmt_record   *string2;
} arith_string;

/********************* mud defined declarations ***************************/

typedef struct _mudfunct_param {
   Boolean                has_quotes;
   string_record          *the_param;
   struct _stmt_record    *funct_param;  /* if we have a function as a parameter */
   struct _mudfunct_param *more_param; 
} mudfunct_param;


typedef struct _mudfunct_decl {
   mudcode_record    *the_funct;
   mudfunct_param    *parameters;   
} mudfunct_decl;

typedef struct _ident_decl {
   char   the_ident[MAXNAMLEN];  /* The identifier string */
} ident_decl;

/********************************** raw code *******************************/

typedef struct _raw_decl {
   string_record     *the_code;  /* pointer to the code strings */
} raw_decl;


/***************************** case declarations ***************************/

typedef struct _case_decl {
   int               stmt_type;          /* mandatory for type conversions */
} case_decl;


/***************************** block declarations ***************************/

typedef struct _block_decl {
   stmt_list_record  *block_statements;  /* the statements in the block */
} block_decl;


/***************************** boolean statement ***************************/

typedef struct _bool_stmt {
   char                bool_string[MAXBOOLLEN];
   struct _stmt_record *the_stmt;
   struct _bool_stmt   *next_bool;
} bool_stmt;

/***************************** if then declarations ***************************/

typedef struct _if_decl {
   bool_stmt         *bool_exp;            /* the bool. exp. in string form */
   stmt_list_record  *if_statements;       /* stmts bet. the if and else */
   stmt_list_record  *else_statements;     /* stmts bet. else and end if */
} if_decl;



/****************** while loop/repeat until declarations **********************/

typedef struct _while_decl {
   bool_stmt         *bool_exp;            /* the bool. exp. in string form */
   stmt_list_record  *while_statements;      
} while_decl;

/******************** for loop declarations *************************************/

typedef struct _for_decl {
   int               from;              /* go from this number */
   int               to;                /* to this number */
   stmt_list_record  *for_statements;      
} for_decl;

/******************** constant declarations *************************************/

/* used for declaring constants */
typedef struct _const_decl {
   char  const_name[MAXNAMLEN];  /* the name of the variable */
   char  const_type;             /* the variable type, either string or number */
   char  *assign;                /* holds the constant assignment */
} const_decl;


/******************** variable declarations *************************************/

/* used for declaring variables */
typedef struct _var_decl {
   char  var_name[MAXNAMLEN];  /* the name of the variable */
   int              var_type;   /* the variable type, either string or number */
   int              str_len;   /* if its a string, the length of that string */
   struct _var_decl *more_var;
} var_decl;

typedef union _the_stmt {
   var_decl       *var_d;
   const_decl     *const_d;
   for_decl       *for_d;
   while_decl     *while_d;
   if_decl        *if_d;
   block_decl     *block_d;
   raw_decl       *raw_d;
   mudfunct_decl  *mudfunct_d;
   arith_string   *arith_str_d;
   arith_number   *arith_num_d;
   ident_decl     *ident_d;
   assignmt       *assignmt_d;
} the_stmt;

typedef struct _stmt_record {
   int      stmt_type;
   the_stmt stmt;
} stmt_record;

/********************************************************************************
 **                           Max Value Defines
 ********************************************************************************/

#define MAX_OBJ_BASEVALUE 7000
#define MAX_OBJ_MAXSTATE   100
#define MAX_OBJ_ARMOR       35
#define MAX_OBJ_DAMAGE      45
#define MAX_OBJ_SIZE     10000  
#define MAX_OBJ_WEIGHT   10000
#define MAX_OBJ_VIS      10000

#define MAX_MOB_STRENGTH 10000
#define MAX_MOB_DAMAGE      70
#define MAX_MOB_ARMOR       40
#define MAX_MOB_VIS      10000

char *xlowercase (char *str, int thesize);
FILE *xfopen(char *filename);
char check_next_char(FILE *thefile);
int goto_next_char(FILE *thefile);
string_record *get_str_until(FILE *thefile, char until1, char until2);
int xend_of_line(FILE *thefile);
int next_line(FILE *the_file);
token_record *get_token(FILE *thefile, char until, Boolean get_event);
mudcode_record *load_mudcode(void);
zone_record *get_zone(char *zonename, hashTable *hasht, mudcode_record *mudcode);
FILE *safe_open(char *the_filename,char *the_directory,char *mode);
int generate_verb_files(char *fn,char *dir);