AwakeMUD-0.51Beta/area/
AwakeMUD-0.51Beta/doc/
AwakeMUD-0.51Beta/lib/
AwakeMUD-0.51Beta/lib/etc/
AwakeMUD-0.51Beta/lib/fixer_data/
AwakeMUD-0.51Beta/lib/misc/
AwakeMUD-0.51Beta/lib/plrobjs/
AwakeMUD-0.51Beta/lib/plrobjs/A-E/
AwakeMUD-0.51Beta/lib/plrobjs/K-O/
AwakeMUD-0.51Beta/lib/plrobjs/U-Z/
AwakeMUD-0.51Beta/lib/plrspells/A-E/
AwakeMUD-0.51Beta/lib/plrtext/A-E/
AwakeMUD-0.51Beta/lib/world/
AwakeMUD-0.51Beta/lib/world/mob/
AwakeMUD-0.51Beta/lib/world/obj/
AwakeMUD-0.51Beta/lib/world/qst/
AwakeMUD-0.51Beta/lib/world/shp/
AwakeMUD-0.51Beta/lib/world/wld/
AwakeMUD-0.51Beta/lib/world/zon/
/* ************************************************************************
*   File: db.c                                          Part of CircleMUD *
*  Usage: Loading/saving chars, booting/resetting world, internal funcs   *
*                                                                         *
*  All rights reserved.  See license.doc for complete information.        *
*                                                                         *
*  Copyright (C) 1993, 94 by the Trustees of the Johns Hopkins University *
*  CircleMUD is based on DikuMUD, Copyright (C) 1990, 1991.               *
************************************************************************ */

#define __DB_CC__

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fstream.h>
#include <iomanip.h>
#include <ctype.h>
#include <time.h>
#include <errno.h>
#include <unistd.h>

#include "structs.h"
#include "awake.h"
#include "utils.h"
#include "db.h"
#include "comm.h"
#include "handler.h"
#include "spells.h"
#include "mail.h"
#include "interpreter.h"
#include "house.h"
#include "memory.h"
#include "dblist.h"
#include "quest.h"
#include "shop.h"

void save_etext(struct char_data *ch);
int real_zone(int virt);
extern void read_spells(struct char_data *ch);
extern struct obj_data *find_obj(int num);
extern void obj_to_cyberware(struct obj_data *, struct char_data *);
extern void obj_from_cyberware(struct obj_data *);
extern void obj_to_bioware(struct obj_data *, struct char_data *);
extern void obj_from_bioware(struct obj_data *);
extern char *cleanup(char *dest, const char *src);


/**************************************************************************
*  declarations of most of the 'global' variables                         *
************************************************************************ */

// beginning of mud time
long beginning_of_time = 2157880000U;
long mud_boot_time = 0;

struct room_data *world = NULL; /* array of rooms                */
int top_of_world = 0;           /* ref to top element of world   */
int top_of_world_array = 0;
int world_chunk_size = 100;     /* size of world to add on each reallocation */
int olc_state = 1;              /* current olc state */
int ANTI_LAR  = 0;		/* ANTI-LAR code */
int LAREXCEPT = 0;		/* ANTI-LAR code */


struct char_data *character_list = NULL;        /* global linked list of
                                                 * chars         */
struct index_data *mob_index;   /* index table for mobile file   */
struct char_data *mob_proto;    /* prototypes for mobs           */
int top_of_mobt = 0;            /* top of mobile index table     */
int top_of_mob_array = 0;
int mob_chunk_size = 100;       // default to 100

class objList ObjList;          // contains the global list of perm objects
struct index_data *obj_index;   /* index table for object file   */
struct obj_data *obj_proto;     /* prototypes for objs           */
int top_of_objt = 0;            /* top of object index table     */
int top_of_obj_array = 0;
int obj_chunk_size = 100;       // default to 100

struct quest_data *quest_table = NULL; // array of quests
int top_of_questt = 0;
int top_of_quest_array = 0;
int quest_chunk_size = 25;

struct shop_data *shop_table = NULL;   // array of shops
int top_of_shopt = 0;            // ref to top element of shop_table
int top_of_shop_array = 0;
int shop_chunk_size = 25;

struct zone_data *zone_table;   /* zone table                    */
int top_of_zone_table = 0;      /* top element of zone tab       */
struct message_list fight_messages[MAX_MESSAGES];       /* fighting messages     */

struct player_index_element *player_table = NULL;       /* index to plr file     */
FILE *player_fl = NULL;         /* file desc of player file      */
int top_of_p_table = 0;         /* ref to top of table           */
int top_of_p_file = 0;          /* ref of size of p file         */
long top_idnum = 0;             /* highest idnum in use          */

int no_mail = 0;                /* mail disabled?                */
int mini_mud = 0;               /* mini-mud mode?                */
int no_rent_check = 0;          /* skip rent check on boot?      */
time_t boot_time = 0;           /* time of mud boot              */
int restrict = 0;               /* level of game restriction     */
sh_int r_mortal_start_room;     /* rnum of mortal start room     */
sh_int r_immort_start_room;     /* rnum of immort start room     */
sh_int r_frozen_start_room;     /* rnum of frozen start room     */
sh_int r_newbie_start_room;     /* rnum of newbie start room     */

char *credits = NULL;           /* game credits                  */
char *news = NULL;              /* mud news                      */
char *motd = NULL;              /* message of the day - mortals */
char *imotd = NULL;             /* message of the day - immorts */
char *help = NULL;              /* help screen                   */
char *info = NULL;              /* info page                     */
char *wizlist = NULL;           /* list of higher gods           */
char *immlist = NULL;           /* list of peon gods             */
char *background = NULL;        /* background story              */
char *handbook = NULL;          /* handbook for new immortals    */
char *policies = NULL;          /* policies page                 */
char *hindex = NULL;            /* index of help files           */

FILE *help_fl = NULL;           /* file for help text            */
struct help_index_element *help_index = 0;      /* the help table        */
int top_of_helpt;               /* top of help index table       */

FILE *wizhelp_fl = NULL;          /* file for wizhelp text */
struct help_index_element *wizhelp_index = 0;
int top_of_wizhelpt;

struct time_info_data time_info;/* the infomation about the time    */
struct weather_data weather_info;       /* the infomation about the weather */
struct player_special_data dummy_mob;   /* dummy spec area for mobs      */
struct reset_q_type reset_q;    /* queue of zones to be reset    */

/* local functions */
void setup_dir(FILE * fl, int room, int dir);
void index_boot(int mode);
void discrete_load(FILE * fl, int mode);
void parse_room(FILE * fl, int virtual_nr);
void parse_mobile(FILE * mob_f, int nr);
char *parse_object(FILE * obj_f, int nr);
void parse_shop(FILE * fl, int virtual_nr);
void parse_quest(FILE * fl, int virtual_nr);
void load_zones(FILE * fl, char *zonename);
void assign_mobiles(void);
void assign_objects(void);
void assign_rooms(void);
void assign_the_shopkeepers(void);
void assign_johnsons(void);
void randomize_shop_prices(void);
void build_player_index(void);
void char_to_store(struct char_data * ch, struct char_file_u * st);
void store_to_char(struct char_file_u * st, struct char_data * ch);
int is_empty(int zone_nr);
void reset_zone(int zone, int reboot);
int file_to_string(char *name, char *buf);
int file_to_string_alloc(char *name, char **buf);
void check_start_rooms(void);
void renum_world(void);
void renum_zone_table(void);
void log_zone_error(int zone, int cmd_no, char *message);
void reset_time(void);
void clear_char(struct char_data * ch);

/* external functions */
extern struct descriptor_data *descriptor_list;
void load_messages(void);
void weather_and_time(int mode);
void boot_social_messages(void);
void update_obj_file(void);     /* In objsave.c */
void sort_commands(void);
void sort_spells(void);
void load_banned(void);
void init_speech(void);
void init_quests(void);
void init_elevators(void);
void Read_Invalid_List(void);
struct help_index_element *build_help_index(FILE * fl, int *num);
extern void affect_total(struct char_data * ch);
extern void load_fixer_data(struct char_data *ch);

/* external vars */
extern int no_specials;
extern int rev_dir[];
extern int min_wizlist_lev;


/* memory objects */
extern class memoryClass *Mem;

/*************************************************************************
*  routines for booting the system                                       *
*********************************************************************** */

/* this is necessary for the autowiz system */
void reboot_wizlists(void)
{
  sprintf(buf, "nice ../bin/autowiz %d %s %d %s %d &", min_wizlist_lev,
          WIZLIST_FILE, LVL_LEGEND, IMMLIST_FILE, (int) getpid());
  system(buf);
  file_to_string_alloc(WIZLIST_FILE, &wizlist);
  file_to_string_alloc(IMMLIST_FILE, &immlist);
}

// create the index file here
void create_index()
{
   int i = 0, j = 0;
   char pletter = 'a';
   FILE  *index_f;

   index_f = fopen("text/index", "w+");

   if (!index_f) {
     log("SYSERR: Failed opening lib/index.\n");
     return;
   }

   for (; i < top_of_helpt; ++i) {
     for (; (pletter <= 'z') && (i < top_of_helpt); ++pletter) {
       fprintf(index_f, "\t\t\t\t ^W%c\n", pletter);
       fprintf(index_f, "----------------------------------------------------------------------------^n\n");
       for (j = (strlen(help_index[i].keyword) + 2); (i < top_of_helpt) &&
             (*help_index[(i < top_of_helpt) ? i+1 : i].keyword <= pletter)
             ; ) {
         j += (strlen(help_index[(i < top_of_helpt) ? i+1 : i].keyword) + 2);
         if (j < 70)
            fprintf(index_f, "%s, ", help_index[++i].keyword);
         else {
            j = (strlen(help_index[i].keyword) + 2);
            fprintf(index_f, "%s\n", help_index[++i].keyword);
         }
       }
          fprintf(index_f, "\n\n");
     }
   }
      fclose(index_f);
}

ACMD(do_reboot)
{
  int i;

  one_argument(argument, arg);

  if (!str_cmp(arg, "all") || *arg == '*') {
    sprintf(buf, "nice ../bin/autowiz %d %s %d %s %d &", min_wizlist_lev,
            WIZLIST_FILE, LVL_LEGEND, IMMLIST_FILE, (int) getpid());
    system(buf);
    file_to_string_alloc(NEWS_FILE, &news);
    file_to_string_alloc(CREDITS_FILE, &credits);
    file_to_string_alloc(MOTD_FILE, &motd);
    file_to_string_alloc(IMOTD_FILE, &imotd);
    file_to_string_alloc(HELP_PAGE_FILE, &help);
    file_to_string_alloc(INFO_FILE, &info);
    file_to_string_alloc(WIZLIST_FILE, &wizlist);
    file_to_string_alloc(IMMLIST_FILE, &immlist);
    file_to_string_alloc(POLICIES_FILE, &policies);
    file_to_string_alloc(HANDBOOK_FILE, &handbook);
    file_to_string_alloc(BACKGROUND_FILE, &background);
  } else if (!str_cmp(arg, "wizlist")) {
    sprintf(buf, "nice ../bin/autowiz %d %s %d %s %d &", min_wizlist_lev,
            WIZLIST_FILE, LVL_LEGEND, IMMLIST_FILE, (int) getpid());
    system(buf);
    file_to_string_alloc(WIZLIST_FILE, &wizlist);
  } else if (!str_cmp(arg, "immlist")) {
    sprintf(buf, "nice ../bin/autowiz %d %s %d %s %d &", min_wizlist_lev,
            WIZLIST_FILE, LVL_LEGEND, IMMLIST_FILE, (int) getpid());
    system(buf);
    file_to_string_alloc(IMMLIST_FILE, &immlist);
  } else if (!str_cmp(arg, "news"))
    file_to_string_alloc(NEWS_FILE, &news);
  else if (!str_cmp(arg, "credits"))
    file_to_string_alloc(CREDITS_FILE, &credits);
  else if (!str_cmp(arg, "motd"))
    file_to_string_alloc(MOTD_FILE, &motd);
  else if (!str_cmp(arg, "imotd"))
    file_to_string_alloc(IMOTD_FILE, &imotd);
  else if (!str_cmp(arg, "help"))
    file_to_string_alloc(HELP_PAGE_FILE, &help);
  else if (!str_cmp(arg, "info"))
    file_to_string_alloc(INFO_FILE, &info);
  else if (!str_cmp(arg, "policy"))
    file_to_string_alloc(POLICIES_FILE, &policies);
  else if (!str_cmp(arg, "handbook"))
    file_to_string_alloc(HANDBOOK_FILE, &handbook);
  else if (!str_cmp(arg, "background"))
    file_to_string_alloc(BACKGROUND_FILE, &background);
  else if (!str_cmp(arg, "index")) {
    create_index();
    file_to_string_alloc(HINDEX_FILE, &hindex);
  } else if (!str_cmp(arg, "xhelp")) {
    if (help_fl)
      fclose(help_fl);
    if (!(help_fl = fopen(HELP_KWRD_FILE, "r")))
      return;
    else {
      for (i = 0; i < top_of_helpt; i++)
        delete [] help_index[i].keyword;
      delete [] help_index;
      help_index = build_help_index(help_fl, &top_of_helpt);
      create_index();
    }
  } else if (!str_cmp(arg, "wizhelp")) {
    if (wizhelp_fl)
      fclose(wizhelp_fl);
    if (!(wizhelp_fl = fopen(WIZHELP_FILE, "r")))
      return;
    else {
      for (i = 0; i < top_of_wizhelpt; i++)
        delete [] wizhelp_index[i].keyword;
      delete [] wizhelp_index;
      wizhelp_index = build_help_index(wizhelp_fl, &top_of_wizhelpt);
    }
  } else {
    send_to_char("Unknown reboot option.\r\n", ch);
    return;
  }

  send_to_char(OK, ch);
}

void boot_world(void)
{
  log("Loading zone table.");
  index_boot(DB_BOOT_ZON);

  log("Loading rooms.");
  index_boot(DB_BOOT_WLD);

  log("Renumbering rooms.");
  renum_world();

  log("Checking start rooms.");
  check_start_rooms();

  log("Loading mobs and generating index.");
  index_boot(DB_BOOT_MOB);

  log("Loading objs and generating index.");
  index_boot(DB_BOOT_OBJ);

  log("Loading quests.");
  index_boot(DB_BOOT_QST);

  log("Loading shops.");
  index_boot(DB_BOOT_SHP);

  log("Renumbering zone table.");
  renum_zone_table();
}

/* body of the booting system */
void boot_db(void)
{
  int i;

  log("Boot db -- BEGIN.");

  log("Resetting the game time:");
  reset_time();

  log("Reading news, credits, help, bground, info & motds.");
  file_to_string_alloc(NEWS_FILE, &news);
  file_to_string_alloc(CREDITS_FILE, &credits);
  file_to_string_alloc(MOTD_FILE, &motd);
  file_to_string_alloc(IMOTD_FILE, &imotd);
  file_to_string_alloc(HELP_PAGE_FILE, &help);
  file_to_string_alloc(INFO_FILE, &info);
  file_to_string_alloc(WIZLIST_FILE, &wizlist);
  file_to_string_alloc(IMMLIST_FILE, &immlist);
  file_to_string_alloc(POLICIES_FILE, &policies);
  file_to_string_alloc(HANDBOOK_FILE, &handbook);
  file_to_string_alloc(BACKGROUND_FILE, &background);

  log("Opening help file.");
  if (!(help_fl = fopen(HELP_KWRD_FILE, "r")))
    log("   Could not open help file.");
  else help_index = build_help_index(help_fl, &top_of_helpt);

  log("Creating Help Index.");
  create_index();
  file_to_string_alloc(HINDEX_FILE, &hindex);

  log("Opening wizhelp file.");
  if (!(wizhelp_fl = fopen(WIZHELP_FILE, "r")))
    log("   Could not open wizhelp file.");
  else wizhelp_index = build_help_index(wizhelp_fl, &top_of_wizhelpt);

  log("Booting World.");
  boot_world();

  log("Generating player index.");
  build_player_index();

  log("Loading fight messages.");
  load_messages();

  log("Loading social messages.");
  boot_social_messages();
  log("Assigning function pointers:");

  if (!no_specials) {
    log("   Mobiles.");
    assign_mobiles();
    log("   Johnsons.");
    assign_johnsons();
    log("   Shopkeepers.");
    assign_the_shopkeepers();
    randomize_shop_prices();
    log("   Objects.");
    assign_objects();
    log("   Rooms.");
    assign_rooms();
  }

  log("Sorting command list and spells.");
  sort_commands();
  sort_spells();

  log("Booting mail system.");
  if (!scan_file()) {
    log("    Mail boot failed -- Mail system disabled");
    no_mail = 1;
  }
  log("Reading banned site and invalid-name list.");
  load_banned();
  Read_Invalid_List();

  log("Initializing mob speech system.");
  init_speech();

  log("Initializing elevator system.");
  init_elevators();

  if (!no_rent_check) {
    log("Deleting timed-out crash and rent files:");
    update_obj_file();
    log("Done.");
  }
  for (i = 0; i <= top_of_zone_table; i++) {
    sprintf(buf2, "Resetting %s (rooms %d-%d).", zone_table[i].name,
            (i ? (zone_table[i - 1].top + 1) : 0), zone_table[i].top);
    log(buf2);
    reset_zone(i, 1);
  }

  reset_q.head = reset_q.tail = NULL;

  if (!mini_mud) {
    log("Booting houses.");
    House_boot();
  }
  boot_time = time(0);

  log("Boot db -- DONE.");
}

/* reset the time in the game from file 
   weekday is lost on reboot.... implement
   something different if this is mission
   breaking behavior */
void reset_time(void)
{
  extern struct time_info_data mud_time_passed(time_t t2, time_t t1);

  mud_boot_time = time(0);
  time_info = mud_time_passed(beginning_of_time, time(0));

  if (time_info.year < 2050)
    time_info.year = 2050;
  time_info.minute = 0;
  time_info.weekday = 0;

  if (time_info.hours <= 5)
    weather_info.sunlight = SUN_DARK;
  else if (time_info.hours == 6)
    weather_info.sunlight = SUN_RISE;
  else if (time_info.hours <= 18)
    weather_info.sunlight = SUN_LIGHT;
  else if (time_info.hours == 19)
    weather_info.sunlight = SUN_SET;
  else weather_info.sunlight = SUN_DARK;

  sprintf(buf, "   Current Gametime: %d/%d/%d %d:00.", time_info.month,
          time_info.day, time_info.year,
          (time_info.hours % 12) == 0 ? 12 : time_info.hours % 12);
  log(buf);

  weather_info.pressure = 960;
  if ((time_info.month >= 7) && (time_info.month <= 12))
    weather_info.pressure += dice(1, 50);
  else weather_info.pressure += dice(1, 80);

  weather_info.change = 0;

  if (weather_info.pressure <= 980)
    weather_info.sky = SKY_LIGHTNING;
  else if (weather_info.pressure <= 1000)
    weather_info.sky = SKY_RAINING;
  else if (weather_info.pressure <= 1020)
    weather_info.sky = SKY_CLOUDY;
  else weather_info.sky = SKY_CLOUDLESS;
}

/* generate index table for the player file */
void build_player_index(void)
{
  int nr = -1, i;
  long size, recs;
  struct char_file_u dummy;

  if (!(player_fl = fopen(PLAYER_FILE, "r+b"))) {
    if (errno != ENOENT) {
      perror("fatal error opening playerfile");
      exit(1);
    } else {
      log("No playerfile.  Creating a new one.");
      touch(PLAYER_FILE);
      if (!(player_fl = fopen(PLAYER_FILE, "r+b"))) {
        perror("fatal error opening playerfile");
        exit(1);
      }
    }
  }

  fseek(player_fl, 0L, SEEK_END);
  size = ftell(player_fl);
  rewind(player_fl);
  if (size % sizeof(struct char_file_u))
    fprintf(stderr, "\aWARNING:  PLAYERFILE IS PROBABLY CORRUPT!\n");
  recs = size / sizeof(struct char_file_u);
  if (recs) {
    sprintf(buf, "   %ld players in database.", recs);
    log(buf);
    player_table = new struct player_index_element[recs];
    // clear out the table
    for (i = 0; i < recs; ++i) {
      player_table[i].name = NULL;
      player_table[i].id = 0;
    }
  } else {
    player_table = NULL;
    top_of_p_file = top_of_p_table = -1;
    return;
  }

  for (; !feof(player_fl);) {
    fread(&dummy, sizeof(struct char_file_u), 1, player_fl);
    if (!feof(player_fl)) {     /* new record */
      nr++;
      player_table[nr].name = new char[strlen(dummy.name) + 1];
      for (i = 0;
           (*(player_table[nr].name + i) = LOWER(*(dummy.name + i))); i++);
      player_table[nr].id = dummy.char_specials_saved.idnum;
      top_idnum = MAX(top_idnum, dummy.char_specials_saved.idnum);
    }
  }

  top_of_p_file = top_of_p_table = nr;
}

/* function to count how many hash-mark delimited records exist in a file */
int count_hash_records(FILE * fl)
{
  char buf[128];
  int count = 0;

  while (fgets(buf, 128, fl))
    if (*buf == '#')
      count++;

  return count;
}

void index_boot(int mode)
{
  char *index_filename, *prefix;
  FILE *index, *db_file;
  int rec_count = 0;

  switch (mode) {
    case DB_BOOT_WLD:
      prefix = WLD_PREFIX;
      break;
    case DB_BOOT_MOB:
      prefix = MOB_PREFIX;
      break;
    case DB_BOOT_OBJ:
      prefix = OBJ_PREFIX;
      break;
    case DB_BOOT_ZON:
      prefix = ZON_PREFIX;
      break;
    case DB_BOOT_SHP:
      prefix = SHP_PREFIX;
      break;
    case DB_BOOT_QST:
      prefix = QST_PREFIX;
      break;
    default:
      log("SYSERR: Unknown subcommand to index_boot!");
      exit(1);
      break;
  }

  if (mini_mud)
    index_filename = MINDEX_FILE;
  else index_filename = INDEX_FILE;

  sprintf(buf2, "%s/%s", prefix, index_filename);

  if (!(index = fopen(buf2, "r"))) {
    sprintf(buf1, "Error opening index file '%s'", buf2);
    perror(buf1);
    exit(1);
  }
  /* first, count the number of records in the file so we can calloc */
  fscanf(index, "%s\n", buf1);
  while (*buf1 != '$') {
    sprintf(buf2, "%s/%s", prefix, buf1);
    if (!(db_file = fopen(buf2, "r"))) {
      // geez, no need to not boot just cause it's not found...
      //perror(buf2);
      //exit(1);
      sprintf(buf, "Unable to find file %s.", buf2);
      mudlog(buf, NULL, LOG_SYSLOG, TRUE);
    } else {
      if (mode == DB_BOOT_ZON)
        rec_count++;
      else rec_count += count_hash_records(db_file);
      fclose(db_file);
    }

    fscanf(index, "%s\n", buf1);
  }

  if (!rec_count) {
   log("SYSERR: boot error - 0 records counted");
   exit(1);
  }
  rec_count++;

  switch (mode) {
    case DB_BOOT_WLD:
      // here I'm booting with 100 extra slots for creation
      world = new struct room_data[rec_count + world_chunk_size];
      memset((char *) world, 0, (sizeof(struct room_data) *
             (rec_count + world_chunk_size)));
      top_of_world_array = rec_count + world_chunk_size; // assign the real size of the array
      break;
    case DB_BOOT_MOB:
      // here I'm booting with 100 extra slots for creation
      mob_proto = new struct char_data[rec_count + mob_chunk_size];
      memset((char *) mob_proto, 0, (sizeof(struct char_data) *
             (rec_count + mob_chunk_size)));

      mob_index = new struct index_data[rec_count + mob_chunk_size];
      memset((char *) mob_index, 0, (sizeof(struct index_data) *
             (rec_count + mob_chunk_size)));

      top_of_mob_array = rec_count + mob_chunk_size; // assign the real size of the array
      break;
    case DB_BOOT_OBJ:
      // here I'm booting with 100 extra slots for creation
      obj_proto = new struct obj_data[rec_count + obj_chunk_size];
      memset((char *) obj_proto, 0, (sizeof(struct obj_data) *
             (rec_count + obj_chunk_size)));

      obj_index = new struct index_data[rec_count + obj_chunk_size];
      memset((char *) obj_index, 0, (sizeof(struct index_data) *
             (rec_count + obj_chunk_size)));

      top_of_obj_array = rec_count + obj_chunk_size;
      break;
    case DB_BOOT_ZON:
      // the zone table is pretty small, so it's no biggie
      zone_table = new struct zone_data[rec_count];
      memset((char *) zone_table, 0, (sizeof(struct zone_data) * rec_count));
      break;
    case DB_BOOT_SHP:
      shop_table = new struct shop_data[rec_count + shop_chunk_size];
      memset((char *) shop_table, 0, (sizeof(struct shop_data) *
             (rec_count + shop_chunk_size)));
      top_of_shop_array = rec_count + shop_chunk_size;
      break;
    case DB_BOOT_QST:
      quest_table = new struct quest_data[rec_count + quest_chunk_size];
      memset((char *) quest_table, 0, (sizeof(struct quest_data) *
             (rec_count + quest_chunk_size)));
      top_of_quest_array = rec_count + quest_chunk_size;
      break;
  }

  rewind(index);
  fscanf(index, "%s\n", buf1);
  while (*buf1 != '$') {
    sprintf(buf2, "%s/%s", prefix, buf1);
    if (!(db_file = fopen(buf2, "r"))) {
      sprintf(buf, "Unable to find file %s.", buf2);
      mudlog(buf, NULL, LOG_SYSLOG, TRUE);
     // perror(buf2);
     // exit(1);
    } else {
      switch (mode) {
        case DB_BOOT_WLD:
        case DB_BOOT_OBJ:
        case DB_BOOT_MOB:
        case DB_BOOT_SHP:
        case DB_BOOT_QST:
          discrete_load(db_file, mode);
          break;
        case DB_BOOT_ZON:
          load_zones(db_file, buf2);
          break;
      }
      fclose(db_file);
    }
    fscanf(index, "%s\n", buf1);
  }
}

void discrete_load(FILE * fl, int mode)
{
  int nr = -1, last = 0;
  char line[256];

  char *modes[] = {"world", "mob", "obj", "zon", "shop", "quest"};

  for (;;) {
    /*
     * we have to do special processing with the obj files because they have
     * no end-of-record marker :(
     */
    if (mode != DB_BOOT_OBJ || nr < 0)
      if (!get_line(fl, line)) {
        fprintf(stderr, "Format error after %s #%d\n", modes[mode], nr);
        exit(1);
      }
    if (*line == '$')
      return;

    if (*line == '#') {
      last = nr;
      if (sscanf(line, "#%d", &nr) != 1) {
        fprintf(stderr, "Format error after %s #%d\n", modes[mode], last);
        exit(1);
      }
      if (nr >= 99999)
        return;
      else switch (mode) {
        case DB_BOOT_WLD:
          parse_room(fl, nr);
          break;
        case DB_BOOT_MOB:
          parse_mobile(fl, nr);
          break;
        case DB_BOOT_OBJ:
          strcpy(line, parse_object(fl, nr));
          break;
        case DB_BOOT_QST:
          parse_quest(fl, nr);
          break;
        case DB_BOOT_SHP:
          parse_shop(fl, nr);
          break;
      }
    } else {
      fprintf(stderr, "Format error in %s file near %s #%d\n",
              modes[mode], modes[mode], nr);
      fprintf(stderr, "Offending line: '%s'\n", line);
      exit(1);
    }
  }
}

long asciiflag_conv(char *flag)
{
  long flags = 0;
  int is_number = 1;
  register char *p;

  for (p = flag; *p; p++) {
    if (islower(*p))
      flags |= 1 << (*p - 'a');
    else if (isupper(*p))
      flags |= 1 << (26 + (*p - 'A'));

    if (!isdigit(*p))
      is_number = 0;
  }

  if (is_number)
    flags = atol(flag);

  return flags;
}

/* load the rooms */
void parse_room(FILE * fl, int virtual_nr)
{
  static int room_nr = 0, zone = 0;
  int t[10], i;
  char line[256], flags[128];
  struct extra_descr_data *new_descr;
  struct use_descr_data *use_descr;

  sprintf(buf2, "room #%d", virtual_nr);

  if (virtual_nr <= (zone ? zone_table[zone - 1].top : -1)) {
    fprintf(stderr, "Room #%d is below zone %d.\n", virtual_nr, zone);
    exit(1);
  }
  while (virtual_nr > zone_table[zone].top)
    if (++zone > top_of_zone_table) {
      fprintf(stderr, "Room %d is outside of any zone.\n", virtual_nr);
      exit(1);
    }
  world[room_nr].zone = zone;
  world[room_nr].number = virtual_nr;
  world[room_nr].name = fread_string(fl, buf2);
  if (!world[room_nr].name)
    world[room_nr].name = str_dup("An unnamed room");

  world[room_nr].description = fread_string(fl, buf2);
  if (!world[room_nr].description)
    world[room_nr].description = str_dup("You stand in an empty room.");

  // we check for the old version too
  if (!get_line(fl, line) || sscanf(line, " %d %s %d %d ", t, flags, t + 2, t + 3) != 4)
    if (sscanf(line, " %d %s %d ", t, flags, t+2) != 3)
    {
      fprintf(stderr, "Old format error in room #%d\n", virtual_nr);
      exit(1);
    }

  world[room_nr].spec = t[0];
  world[room_nr].room_flags = asciiflag_conv(flags);
  world[room_nr].sector_type = t[2];
  world[room_nr].rating = t[3];

  world[room_nr].func = NULL;
  world[room_nr].contents = NULL;
  world[room_nr].people = NULL;
  world[room_nr].light = 0;     /* Zero light sources */

  for (i = 0; i < NUM_OF_DIRS; i++)
    world[room_nr].dir_option[i] = NULL;

  world[room_nr].ex_description = NULL;

  sprintf(buf, "Format error in room #%d (expecting D/E/S)", virtual_nr);

  for (;;) {
    if (!get_line(fl, line)) {
      fprintf(stderr, "%s\n", buf);
      exit(1);
    }
    switch (*line) {
      case 'D':
        setup_dir(fl, room_nr, atoi(line + 1));
        break;
      case 'E':
        new_descr = new extra_descr_data;
        new_descr->keyword = fread_string(fl, buf2);
        new_descr->description = fread_string(fl, buf2);
        new_descr->next = world[room_nr].ex_description;
        world[room_nr].ex_description = new_descr;
        break;
      case 'M':
        world[room_nr].msp_trigger = fread_string(fl, buf2);
        break;
      case 'S':                 /* end of room */
        top_of_world = room_nr++;
        return;
      default:
        fprintf(stderr, "%s\n", buf);
        exit(1);
        break;
    }
  }
}

/* read direction data */
void setup_dir(FILE * fl, int room, int dir)
{
  int t[7];
  char line[256];
  int retval;

  sprintf(buf2, "room #%d, direction D%d", world[room].number, dir);

  world[room].dir_option[dir] = new room_direction_data;
  world[room].dir_option[dir]->general_description = fread_string(fl, buf2);
  world[room].dir_option[dir]->keyword = fread_string(fl, buf2);

  if (!get_line(fl, line)) {
    fprintf(stderr, "Format error, %s\n", buf2);
    exit(1);
  }
  if ((retval = sscanf(line, " %d %d %d %d %d %d %d", t, t + 1, t + 2, t + 3,
       t + 4, t + 5, t + 6)) < 4) {
    fprintf(stderr, "Format error, %s\n", buf2);
    exit(1);
  }
  if (t[0] == 1)
    world[room].dir_option[dir]->exit_info = EX_ISDOOR;
  else if (t[0] == 2)
    world[room].dir_option[dir]->exit_info = EX_ISDOOR | EX_PICKPROOF;
  else world[room].dir_option[dir]->exit_info = 0;

  world[room].dir_option[dir]->key = t[1];
  world[room].dir_option[dir]->to_room = MAX(0, t[2]);
  world[room].dir_option[dir]->key_level = t[3];

  world[room].dir_option[dir]->material = (retval > 4) ? t[4] : 5;
  world[room].dir_option[dir]->barrier = (retval > 5) ? t[5] : 4;
  world[room].dir_option[dir]->condition = (retval > 5) ? t[5] : 4;
  world[room].dir_option[dir]->hidden = (retval > 6) ? t[6] : 0;
}

/* make sure the start rooms exist & resolve their vnums to rnums */
void check_start_rooms(void)
{
  extern sh_int mortal_start_room;
  extern sh_int immort_start_room;
  extern sh_int frozen_start_room;
  extern sh_int newbie_start_room;

  if ((r_mortal_start_room = real_room(mortal_start_room)) < 0) {
    log("SYSERR:  Mortal start room does not exist.  Change in config.c.");
    exit(1);
  }
  if ((r_immort_start_room = real_room(immort_start_room)) < 0) {
    if (!mini_mud)
      log("SYSERR:  Warning: Immort start room does not exist.  Change in config.c.");
    r_immort_start_room = r_mortal_start_room;
  }
  if ((r_frozen_start_room = real_room(frozen_start_room)) < 0) {
    if (!mini_mud)
      log("SYSERR:  Warning: Frozen start room does not exist.  Change in config.c.");
    r_frozen_start_room = r_mortal_start_room;
  }
  if ((r_newbie_start_room = real_room(newbie_start_room)) < 0) {
    if (!mini_mud)
      log("SYSERR:  Warning: Newbie start room does not exist.  Change in config.c.");
    r_newbie_start_room = r_mortal_start_room;
  }
}

/* resolve all vnums into rnums in the world */
void renum_world(void)
{
  register int room, door;

  /* before renumbering the exits, copy them to to_room_vnum */
  for (room = 0; room <= top_of_world; room++)
    for (door = 0; door < NUM_OF_DIRS; door++)
      if (world[room].dir_option[door]) {
        /* copy */
        world[room].dir_option[door]->to_room_vnum =
               world[room].dir_option[door]->to_room;
        if (world[room].dir_option[door]->to_room != NOWHERE)
          world[room].dir_option[door]->to_room =
                real_room(world[room].dir_option[door]->to_room);
      }
}

#define ZCMD zone_table[zone].cmd[cmd_no]

/* resulve vnums into rnums in the zone reset tables */
void renum_zone_table(void)
{
  int zone, cmd_no, a, b;

  for (zone = 0; zone <= top_of_zone_table; zone++)
    for (cmd_no = 0; cmd_no < zone_table[zone].num_cmds; cmd_no++) {
      a = b = 0;
      switch (ZCMD.command) {
      case 'M':
        a = ZCMD.arg1 = real_mobile(ZCMD.arg1);
        b = ZCMD.arg3 = real_room(ZCMD.arg3);
        break;
      case 'O':
        a = ZCMD.arg1 = real_object(ZCMD.arg1);
        if (ZCMD.arg3 != NOWHERE)
          b = ZCMD.arg3 = real_room(ZCMD.arg3);
        break;
      case 'G': case 'C':
        a = ZCMD.arg1 = real_object(ZCMD.arg1);
        break;
      case 'E':
        a = ZCMD.arg1 = real_object(ZCMD.arg1);
        break;
      case 'P':
        a = ZCMD.arg1 = real_object(ZCMD.arg1);
        b = ZCMD.arg3 = real_object(ZCMD.arg3);
        break;
      case 'D':
        a = ZCMD.arg1 = real_room(ZCMD.arg1);
        break;
      case 'R': /* rem obj from room */
        a = ZCMD.arg1 = real_room(ZCMD.arg1);
        b = ZCMD.arg2 = real_object(ZCMD.arg2);
        break;
      case 'N':
        a = ZCMD.arg1 = real_object(ZCMD.arg1);
        break;
      }
      if (a < 0 || b < 0) {
        if (!mini_mud)
          log_zone_error(zone, cmd_no, "Invalid vnum, cmd disabled");
        ZCMD.command = '*';
      }
    }
}

void parse_mobile(FILE * mob_f, int nr)
{
  static int i = 0;
  int j, t[10], x;
  char line[256], *tmpptr;
  char f1[128], f2[128];

  mob_index[i].virt = nr;
  mob_index[i].number = 0;
  mob_index[i].func = NULL;

  clear_char(mob_proto + i);

  mob_proto[i].player_specials = &dummy_mob;
  sprintf(buf2, "mob vnum %d", nr);

  /***** String data *** */
  mob_proto[i].player.name = fread_string(mob_f, buf2);
  tmpptr = mob_proto[i].player.short_descr = fread_string(mob_f, buf2);
  if (tmpptr && *tmpptr)
    if (!str_prefix("A ", tmpptr) || !str_prefix("An ", tmpptr) ||
        !str_prefix("The ", tmpptr))
      *tmpptr = LOWER(*tmpptr);
  if (!tmpptr)
    mob_proto[i].player.short_descr = str_dup("unnamed");

  mob_proto[i].player.long_descr = fread_string(mob_f, buf2);
  if (!tmpptr)
    mob_proto[i].player.long_descr = str_dup("An unnamed mob");

  mob_proto[i].player.description = fread_string(mob_f, buf2);
  if (!tmpptr)
    mob_proto[i].player.description = str_dup("You see an unnamed mob.");

  mob_proto[i].player.title = NULL;
  mob_proto[i].mob_specials.arrive = fread_string(mob_f, buf2);
  mob_proto[i].mob_specials.leave = fread_string(mob_f, buf2);

  /* *** Numeric data *** */

  get_line(mob_f, line);
  if (sscanf(line, "%s %s %d %d %d", f1, f2, t + 2, t + 3, t + 4) != 5) {
     fprintf(stderr, "Format error in mob#%d, 1st number line\n"
             "...expecting line of form '# # # # #'\n", nr);
     exit(1);
  }
  MOB_FLAGS(mob_proto + i) = asciiflag_conv(f1);
  SET_BIT(MOB_FLAGS(mob_proto + i), MOB_ISNPC);
  AFF_FLAGS(mob_proto + i) = asciiflag_conv(f2);
  GET_ALIGNMENT(mob_proto + i) = t[2];
  GET_BANK(mob_proto + i) = t[3];
  GET_KARMA(mob_proto + i) = t[4];

  /* scan in mob abilities, yep, all 9 of them - Flynn */
  get_line(mob_f, line);
  if (sscanf(line, "%d %d %d %d %d %d %d %d %d", t, t+1, t+2, t+3, t+4, t+5, t+6,
         t+7, t+8) != 9) {
      fprintf(stderr, "Format error in mob#%d, abilities line (second line)\n"
         "...expecting line of form '# # # # # # # # #'\n", nr);
      exit(1);
  }
  mob_proto[i].real_abils.bod = t[0];
  mob_proto[i].real_abils.qui = t[1];
  mob_proto[i].real_abils.str = t[2];
  mob_proto[i].real_abils.cha = t[3];
  mob_proto[i].real_abils.intel = t[4];
  mob_proto[i].real_abils.wil = t[5];
  mob_proto[i].real_abils.mag = t[7] * 100;
  mob_proto[i].real_abils.rea = (t[1] + t[4]) >> 1;
  mob_proto[i].real_abils.ess = 600;
  mob_proto[i].real_abils.bod_index = t[0] * 100;

  /* scan in mob skills, max 5 for now - Flynn */
  get_line(mob_f, line);
  if (sscanf(line, "%d %d %d %d %d %d %d %d %d %d", t, t+1, t+2, t+3, t+4,
             t+5, t+6, t+7, t+8, t+9) != 10) {
     fprintf(stderr, "Format error in mob#%d, skills line (third line)\n"
         "...expecting line of form '# # # # # # # # # #'\n", nr);
     exit(1);
  }
  if (t[0] > MAX_SPELLS && t[0] < MAX_SKILLS)
    mob_proto[i].char_specials.saved.skills[t[0]] = t[1];
  if (t[2] > MAX_SPELLS && t[2] < MAX_SKILLS)
    mob_proto[i].char_specials.saved.skills[t[2]] = t[3];
  if (t[4] > MAX_SPELLS && t[4] < MAX_SKILLS)
    mob_proto[i].char_specials.saved.skills[t[4]] = t[5];
  if (t[6] > MAX_SPELLS && t[6] < MAX_SKILLS)
    mob_proto[i].char_specials.saved.skills[t[6]] = t[7];
  if (t[8] > MAX_SPELLS && t[8] < MAX_SKILLS)
    mob_proto[i].char_specials.saved.skills[t[8]] = t[9];

  // gotta do this so when the editors write the skills, the values are correct
  mob_proto[i].mob_specials.mob_skills[0] = t[0];
  mob_proto[i].mob_specials.mob_skills[1] = t[1];
  mob_proto[i].mob_specials.mob_skills[2] = t[2];
  mob_proto[i].mob_specials.mob_skills[3] = t[3];
  mob_proto[i].mob_specials.mob_skills[4] = t[4];
  mob_proto[i].mob_specials.mob_skills[5] = t[5];
  mob_proto[i].mob_specials.mob_skills[6] = t[6];
  mob_proto[i].mob_specials.mob_skills[7] = t[7];
  mob_proto[i].mob_specials.mob_skills[8] = t[8];
  mob_proto[i].mob_specials.mob_skills[9] = t[9];

  get_line(mob_f, line);
  if (sscanf(line, " %d %d %d %d %d %d ",
    t, t + 1, t + 2, t + 3, t + 4, t + 5) != 6) {
    fprintf(stderr, "Format error in mob #%d, number line 4.\n"
      "...expecting line of form '# # # # # #'\n", nr);
    exit(1);
  }
  GET_LEVEL(mob_proto + i) = t[0];

  mob_proto[i].points.ballistic = t[1];
  mob_proto[i].points.impact = t[2];
  mob_proto[i].points.max_physical = t[3] * 100;
  mob_proto[i].points.physical = t[3] * 100;
  mob_proto[i].points.max_mental = t[4] * 100;
  mob_proto[i].points.mental = t[4] * 100;
  GET_NUYEN(mob_proto + i) = t[5];
  mob_proto[i].points.move = 0;
  mob_proto[i].points.max_move = 0;
  mob_proto[i].points.sustained = 0;

  /* set pools to 0 initially, affect total will correct them */
  mob_proto[i].real_abils.astral_pool = 0;
  mob_proto[i].real_abils.defense_pool = 0;
  mob_proto[i].real_abils.combat_pool = 0;
  mob_proto[i].real_abils.offense_pool = 0;
  mob_proto[i].real_abils.hacking_pool = 0;
  mob_proto[i].real_abils.magic_pool = 0;

  get_line(mob_f, line);
  if (sscanf(line, " %d %d %d %d %d %d %d", t, t + 1, t + 2, t + 3, t + 4,
     t + 5, t + 6) != 7) {
    fprintf(stderr, "Format error in mob #%d, number line 5.\n"
      "...expecting line of form '# # # # # # #'\n", nr);
    exit(1);
  }

  mob_proto[i].char_specials.position = t[0];
  mob_proto[i].char_specials.saved.idnum = -1;
  mob_proto[i].mob_specials.mood = number(-5, 4);
  mob_proto[i].mob_specials.default_pos = t[1];
  mob_proto[i].player.sex = t[2];
  mob_proto[i].mob_specials.attack_type = (t[3] < TYPE_HIT) ? t[3] + TYPE_HIT : t[3];
  mob_proto[i].player.race = t[4];
  mob_proto[i].player.weight = t[5];
  mob_proto[i].player.height = t[6];

  for (j = 0; j < 3; j++)
    GET_COND(mob_proto + i, j) = -1;

  mob_proto[i].aff_abils = mob_proto[i].real_abils;

  for (j = 0; j < NUM_WEARS; j++)
    mob_proto[i].equipment[j] = NULL;

  mob_proto[i].cyberware = NULL;
  mob_proto[i].bioware = NULL;
  mob_proto[i].nr = i;
  mob_proto[i].desc = NULL;

  top_of_mobt = i++;
}

/* read all objects from obj file; generate index and prototypes */
char *parse_object(FILE * obj_f, int nr)
{
  static int i = 0, retval;
  static char line[256];
  int t[10], j;
  char *tmpptr;
  char f1[256], f2[256];
  struct extra_descr_data *new_descr;
  struct use_descr_data *use_descr;

  obj_index[i].virt = nr;
  obj_index[i].number = 0;
  obj_index[i].func = NULL;

  clear_object(obj_proto + i);
  obj_proto[i].in_room = NOWHERE;
  obj_proto[i].item_number = i;

  sprintf(buf2, "object #%d", nr);

  /* *** string data *** */
  if ((obj_proto[i].name = fread_string(obj_f, buf2)) == NULL) {
    fprintf(stderr, "Null obj name or format error at or near %s\n", buf2);
    obj_proto[i].name = str_dup("unnamed");
  }
  tmpptr = obj_proto[i].short_description = fread_string(obj_f, buf2);
  if (tmpptr && *tmpptr)
    if (!str_prefix("A ", tmpptr) || !str_prefix("An ", tmpptr) ||
        !str_prefix("The ", tmpptr))
      *tmpptr = LOWER(*tmpptr);

  if (!tmpptr)
    obj_proto[i].short_description = str_dup("an unnamed object");

  tmpptr = obj_proto[i].description = fread_string(obj_f, buf2);
  if (tmpptr && *tmpptr)
    *tmpptr = UPPER(*tmpptr);
  if (!tmpptr)
    obj_proto[i].description = str_dup("An unnamed object sits here.");

  obj_proto[i].long_description = fread_string(obj_f, buf2);

  if (!tmpptr)
    obj_proto[i].long_description = str_dup("You see an uncreative object.");

  /* *** numeric data *** */
  if (!get_line(obj_f, line) ||
      (retval = sscanf(line, " %d %s %s %d %d %d ", t, f1, f2, t + 1, t + 2,
                       t + 3)) != 6)
    if ((retval = sscanf(line, " %d %s %s ", t, f1, f2)) != 3)
    {
      fprintf(stderr, "Format error in first numeric line (expecting 3 or 5 args, got %d), %s\n",
              retval, buf2);
      exit(1);
    }

  obj_proto[i].obj_flags.type_flag = t[0];
  obj_proto[i].obj_flags.extra_flags = asciiflag_conv(f1);
  obj_proto[i].obj_flags.wear_flags = asciiflag_conv(f2);
  obj_proto[i].obj_flags.material = (retval > 3) ? t[1] : 5;
  obj_proto[i].obj_flags.barrier = MAX(1, (retval > 4) ? t[2] : 3);
  obj_proto[i].obj_flags.condition = obj_proto[i].obj_flags.barrier;
  obj_proto[i].obj_flags.bitvector = (retval > 5) ? t[3] : 0;
  obj_proto[i].obj_flags.quest_id = 0;

  if (!get_line(obj_f, line) ||
     (retval = sscanf(line, "%d %d %d %d %d %d %d %d %d %d", t, t + 1, t + 2,
                      t + 3, t + 4, t + 5, t + 6, t + 7, t + 8, t + 9)) < 7)
  {
    fprintf(stderr, "Format error in second numeric line (expecting 7 args, got %d), %s\n",
            retval, buf2);
    exit(1);
  }
  obj_proto[i].obj_flags.value[0] = t[0];
  obj_proto[i].obj_flags.value[1] = t[1];
  obj_proto[i].obj_flags.value[2] = t[2];
  // make sure to adapt new values
  if ((obj_proto[i].obj_flags.type_flag == ITEM_WEAPON) && (t[3] < TYPE_HIT))
    obj_proto[i].obj_flags.value[3] = t[3] + TYPE_HIT;
  else
    obj_proto[i].obj_flags.value[3] = t[3];
  obj_proto[i].obj_flags.value[4] = t[4];
  obj_proto[i].obj_flags.value[5] = t[5];
  obj_proto[i].obj_flags.value[6] = t[6];
  obj_proto[i].obj_flags.value[7] = (retval < 10 ? 0 : t[7]);
  obj_proto[i].obj_flags.value[8] = (retval < 10 ? 0 : t[8]);
  obj_proto[i].obj_flags.value[9] = (retval < 10 ? 0 : t[9]);


  if (!get_line(obj_f, line) ||
      (retval = sscanf(line, "%d %d %d", t, t + 1, t + 2)) != 3) {
    fprintf(stderr, "Format error in third numeric line (expecting 3 args, got %d), %s\n", retval, buf2);
    exit(1);
  }
  obj_proto[i].obj_flags.weight = t[0];
  obj_proto[i].obj_flags.cost = t[1];
  obj_proto[i].obj_flags.cost_per_day = t[2];

  /* *** extra descriptions and affect fields *** */

  for (j = 0; j < MAX_OBJ_AFFECT; j++) {
    obj_proto[i].affected[j].location = APPLY_NONE;
    obj_proto[i].affected[j].modifier = 0;
  }

  strcat(buf2, ", after numeric constants (expecting E/A/#xxx)");
  j = 0;

  for (;;) {
    if (!get_line(obj_f, line)) {
      fprintf(stderr, "Format error in %s\n", buf2);
      exit(1);
    }
    switch (*line) {
    case 'E':
      new_descr = new extra_descr_data;
      new_descr->keyword = fread_string(obj_f, buf2);
      new_descr->description = fread_string(obj_f, buf2);
      new_descr->next = obj_proto[i].ex_description;
      obj_proto[i].ex_description = new_descr;
      break;
    case 'U':
      use_descr = new use_descr_data;
      use_descr->keyword = fread_string(obj_f, buf2);
      use_descr->description1 = fread_string(obj_f, buf2);
      use_descr->description2 = fread_string(obj_f, buf2);
      use_descr->next = obj_proto[i].use_description;
      obj_proto[i].use_description = use_descr;
      break;
    case 'A':
      if (j >= MAX_OBJ_AFFECT) {
        fprintf(stderr, "Too many A fields (%d max), %s\n", MAX_OBJ_AFFECT, buf2);
        exit(1);
      }
      get_line(obj_f, line);
      sscanf(line, " %d %d ", t, t + 1);
      obj_proto[i].affected[j].location = t[0];
      obj_proto[i].affected[j].modifier = t[1];
      j++;
      break;
    case '$':
    case '#':
      top_of_objt = i++;
      return line;
      break;
    default:
      fprintf(stderr, "Format error in %s\n", buf2);
      exit(1);
      break;
    }
  }
}

void parse_quest(FILE * fl, int virtual_nr)
{
  static int quest_nr = 0;
  int j, t[9];
  char line[256];

  quest_table[quest_nr].virt = virtual_nr;

  get_line(fl, line);
  if (sscanf(line, "%d %d %d %d %d %d %d %d %d", t, t + 1, t + 2, t + 3,
             t + 4, t + 5, t + 6, t + 7, t + 8) != 9) {
    fprintf(stderr, "Format error in quest #%d, expecting # # # # # # #\n",
            quest_nr);
    exit(1);
  }
  quest_table[quest_nr].johnson = t[0];
  quest_table[quest_nr].time = t[1];
  quest_table[quest_nr].min_rep = t[2];
  quest_table[quest_nr].max_rep = t[3];
  quest_table[quest_nr].nuyen = t[4];
  quest_table[quest_nr].karma = t[5];
  quest_table[quest_nr].reward = (real_object(t[6]) > -1 ? t[6] : -1);
  quest_table[quest_nr].num_objs = t[7];
  quest_table[quest_nr].num_mobs = t[8];

  if (quest_table[quest_nr].num_objs > 0) {
    quest_table[quest_nr].obj = new quest_om_data[quest_table[quest_nr].num_objs];
    for (j = 0; j < quest_table[quest_nr].num_objs; j++) {
      get_line(fl, line);
      if (sscanf(line, "%d %d %d %d %d %d %d %d", t, t + 1, t + 2, t + 3,
                 t + 4, t + 5, t + 6, t + 7) != 8) {
        fprintf(stderr, "Format error in quest #%d, obj #%d\n", quest_nr, j);
        exit(1);
      }
      quest_table[quest_nr].obj[j].vnum = t[0];
      quest_table[quest_nr].obj[j].nuyen = t[1];
      quest_table[quest_nr].obj[j].karma = t[2];
      quest_table[quest_nr].obj[j].load = (byte) t[3];
      quest_table[quest_nr].obj[j].objective = (byte) t[4];
      quest_table[quest_nr].obj[j].l_data = t[5];
      quest_table[quest_nr].obj[j].l_data2 = t[6];
      quest_table[quest_nr].obj[j].o_data = t[7];
    }
  } else quest_table[quest_nr].obj = NULL;

  if (quest_table[quest_nr].num_mobs > 0) {
    quest_table[quest_nr].mob = new quest_om_data[quest_table[quest_nr].num_mobs];
    for (j = 0; j < quest_table[quest_nr].num_mobs; j++) {
      get_line(fl, line);
      if (sscanf(line, "%d %d %d %d %d %d %d %d", t, t + 1, t + 2, t + 3,
                 t + 4, t + 5, t + 6, t + 7) != 8) {
        fprintf(stderr, "Format error in quest #%d, mob #%d\n", quest_nr, j);
        exit(1);
      }
      quest_table[quest_nr].mob[j].vnum = t[0];
      quest_table[quest_nr].mob[j].nuyen = t[1];
      quest_table[quest_nr].mob[j].karma = t[2];
      quest_table[quest_nr].mob[j].load = (byte) t[3];
      quest_table[quest_nr].mob[j].objective = (byte) t[4];
      quest_table[quest_nr].mob[j].l_data = t[5];
      quest_table[quest_nr].mob[j].l_data2 = t[6];
      quest_table[quest_nr].mob[j].o_data = t[7];
    }
  } else quest_table[quest_nr].mob = NULL;

  quest_table[quest_nr].intro = fread_string(fl, buf2);
  quest_table[quest_nr].decline = fread_string(fl, buf2);
  quest_table[quest_nr].quit = fread_string(fl, buf2);
  quest_table[quest_nr].finish = fread_string(fl, buf2);
  quest_table[quest_nr].info = fread_string(fl, buf2);

  quest_nr++;
  top_of_questt = quest_nr;
}

void parse_shop(FILE * fl, int virtual_nr)
{
  static int shop_nr = 0;
  char line[256];
  int t[5], i, max;
  float buy, sell;

  shop_table[shop_nr].virt = virtual_nr;

  get_line(fl, line);
  if (sscanf(line, "%d %d %d", t, t + 1, t + 2) != 3) {
    fprintf(stderr, "Format error in shop #%d, expecting # # #\n", shop_nr);
    exit(1);
  }

  shop_table[shop_nr].num_producing = t[0];
  shop_table[shop_nr].num_buy_types = t[1];
  shop_table[shop_nr].num_rooms = t[2];

  max = MAX(t[0], MAX(t[1], t[2]));

  if (shop_table[shop_nr].num_producing > 0)
    shop_table[shop_nr].producing = new int[shop_table[shop_nr].num_producing];
  else shop_table[shop_nr].producing = NULL;

  if (shop_table[shop_nr].num_buy_types > 0)
    shop_table[shop_nr].type = new int[shop_table[shop_nr].num_buy_types];
  else
    shop_table[shop_nr].type = NULL;

  if (shop_table[shop_nr].num_rooms > 0)
    shop_table[shop_nr].in_room = new int[shop_table[shop_nr].num_rooms];
  else shop_table[shop_nr].in_room = NULL;

  for (i = 0; i < max; i++) {
    get_line(fl, line);
    if (sscanf(line, "%d %d %d", t, t + 1, t + 2) != 3) {
      fprintf(stderr, "Format error in shop #%d, p/t/r line #%d", shop_nr, i);
      exit(1);
    }
    if (t[0] > 0 && i < shop_table[shop_nr].num_producing)
      shop_table[shop_nr].producing[i] = t[0];
    if (t[1] > 0 && i < shop_table[shop_nr].num_buy_types)
      shop_table[shop_nr].type[i] = t[1];
    if (t[2] > 0 && i < shop_table[shop_nr].num_rooms)
      shop_table[shop_nr].in_room[i] = t[2];
  }

  get_line(fl, line);
  if (sscanf(line, "%f %f %d %d %d %d %d", &buy, &sell, t, t + 1, t + 2,
             t + 3, t + 4) != 7) {
    fprintf(stderr, "Format error in shop #%d, expecting # # # # # # #", shop_nr);
    exit(1);
  }

  shop_table[shop_nr].profit_buy = buy;
  shop_table[shop_nr].profit_sell = sell;
  shop_table[shop_nr].percentage = t[0],
  shop_table[shop_nr].temper = t[1];
  shop_table[shop_nr].bitvector = t[2];
  shop_table[shop_nr].keeper = t[3];
  shop_table[shop_nr].with_who = t[4];

  get_line(fl, line);
  if (sscanf(line, "%d %d %d %d", t, t + 1, t + 2, t + 3) != 4) {
    fprintf(stderr, "Format error in shop #%d, expecting # # # #", shop_nr);
    exit(1);
  }

  shop_table[shop_nr].open1 = t[0];
  shop_table[shop_nr].close1 = t[1];
  shop_table[shop_nr].open2 = t[2];
  shop_table[shop_nr].close2 = t[3];

  shop_table[shop_nr].no_such_item1 = fread_string(fl, buf2);
  shop_table[shop_nr].no_such_item2 = fread_string(fl, buf2);
  shop_table[shop_nr].do_not_buy = fread_string(fl, buf2);
  shop_table[shop_nr].missing_cash1 = fread_string(fl, buf2);
  shop_table[shop_nr].missing_cash2 = fread_string(fl, buf2);
  shop_table[shop_nr].message_buy = fread_string(fl, buf2);
  shop_table[shop_nr].message_sell = fread_string(fl, buf2);

  SHOP_BANK(shop_nr) = 0;
  SHOP_SORT(shop_nr) = 0;
  SHOP_LASTTIME(shop_nr) = 0;

  shop_nr++;
  top_of_shopt = shop_nr;
}

#define Z       zone_table[zone]

/* load the zone table and command tables */
void load_zones(FILE * fl, char *zonename)
{
  static int zone = 0;
  int cmd_no = 0, line_num = 0, tmp, error;
  char *ptr, buf[256], zname[256];

  strcpy(zname, zonename);

  Z.num_cmds = 0;
  while (get_line(fl, buf))
    Z.num_cmds++;

  // subtract the first 4 lines
  Z.num_cmds -= 4;

  rewind(fl);

  if (Z.num_cmds == 0) {
    fprintf(stderr, "%s is empty.\n", zname);
    Z.cmd = NULL;
    // exit(0);   -- it's okay, we can deal with empty zones
  } else
    Z.cmd = new struct reset_com[Z.num_cmds];

  line_num += get_line(fl, buf);

  if (sscanf(buf, "#%d", &Z.number) != 1) {
    fprintf(stderr, "Format error in %s, line %d\n", zname, line_num);
    exit(0);
  }
  //sprintf(buf2, "beginning of zone #%d", Z.number);

  line_num += get_line(fl, buf);
  if ((ptr = strchr((const char *)buf, '~')) != NULL)   /* take off the '~' if it's there */
    *ptr = '\0';
  Z.name = str_dup(buf);

  line_num += get_line(fl, buf);
  if (sscanf(buf, " %d %d %d %d %d", &Z.top, &Z.lifespan, &Z.reset_mode, &Z.security, &Z.connected) != 5) {
    fprintf(stderr, "Format error in 5-constant line of %s", zname);
    exit(0);
  }

  line_num += get_line(fl, buf);
  // This next section reads in the id nums of the players that can edit
  // this zone.
  if (sscanf(buf, "%d %d %d %d %d", &Z.editor_ids[0], &Z.editor_ids[1],
             &Z.editor_ids[2], &Z.editor_ids[3], &Z.editor_ids[4]) != 5)
  {
    fprintf(stderr, "Format error in editor id list of %s", zname);
    exit(0);
  }

  cmd_no = 0;

  // go ahead and skip this zone from here on if it has no cmds
  for (;Z.num_cmds > 0;) {
    if (!(tmp = get_line(fl, buf))) {
      fprintf(stderr, "Format error in %s - premature end of file\n", zname);
      exit(0);
    }
    line_num += tmp;
    ptr = buf;
    skip_spaces(&ptr);

    if ((ZCMD.command = *ptr) == '*')
      continue;

    ptr++;

    if (ZCMD.command == '$')
      break;

    error = 0;
    if (strchr((const char *)"MOENPD", ZCMD.command) == NULL) { /* a 3-arg command */
      if (sscanf(ptr, " %d %d %d ", &tmp, &ZCMD.arg1, &ZCMD.arg2) != 3)
        error = 1;
    } else {
      if (sscanf(ptr, " %d %d %d %d ", &tmp, &ZCMD.arg1, &ZCMD.arg2,
                 &ZCMD.arg3) != 4)
        error = 1;
    }

    ZCMD.if_flag = tmp;

    if (error) {
      fprintf(stderr, "Format error in %s, line %d: '%s'\n", zname, line_num, buf);
      exit(0);
    }
    ZCMD.line = line_num;
    cmd_no++;
  }

  top_of_zone_table = zone++;

   rewind(fl);
}

#undef Z

/*************************************************************************
*  procedures for resetting, both play-time and boot-time                *
*********************************************************************** */

int vnum_mobile_karma(char *searchname, struct char_data * ch)
{
  SPECIAL(shop_keeper);
  extern int calc_karma(struct char_data *ch, struct char_data *vict);
  int nr, found = 0;
  int karma;
  int i;

  for (i = 1000; i >= 0; i--)
    {
      for (nr = 0; nr <= top_of_mobt; nr++) {
	if (mob_index[nr].func == shop_keeper
	    ||mob_index[nr].sfunc == shop_keeper)
	  continue;
	karma = calc_karma(NULL, &mob_proto[nr]);
	if ( karma < i )
	  continue;
	if ( i != 1000 && karma > i )
	  continue;
	sprintf(buf, "%3d. [%5d] %5.2f %s\r\n", ++found,
		mob_index[nr].virt,
		(float)karma/100.0,
		mob_proto[nr].player.short_descr);
	send_to_char(buf, ch);
      }
    }
  return (found);
}

int vnum_mobile(char *searchname, struct char_data * ch)
{
  int nr, found = 0;

  if (!strcmp(searchname,"karmalist"))
    return vnum_mobile_karma(searchname,ch);
  for (nr = 0; nr <= top_of_mobt; nr++) {
    if (isname(searchname, mob_proto[nr].player.name)
	||isname(searchname, mob_proto[nr].player.short_descr)) {
      sprintf(buf, "%3d. [%5d] %s\r\n", ++found,
              mob_index[nr].virt,
              mob_proto[nr].player.short_descr == NULL ? "(BUG)" :
		mob_proto[nr].player.short_descr );
      send_to_char(buf, ch);
    }
  }

  return (found);
}

int vnum_object_weapons(char *searchname, struct char_data * ch)
{
  extern const char *wound_arr[];
  int nr, found = 0;

  for (nr = 0; nr <= top_of_objt; nr++) {
    if (GET_OBJ_TYPE(&obj_proto[nr]) == ITEM_WEAPON
	&& !from_ip_zone(obj_index[nr].virt)) {
#if 0
      if (GET_OBJ_VAL(&obj_proto[nr], 2) == 0)
	continue;
#endif
      sprintf(buf, "%3d. [%5d] %2d%s +%d %s\r\n", ++found,
              obj_index[nr].virt,
	      GET_OBJ_VAL(&obj_proto[nr], 0),
	      wound_arr[GET_OBJ_VAL(&obj_proto[nr], 1)],
	      GET_OBJ_VAL(&obj_proto[nr], 2),
              obj_proto[nr].short_description);
      send_to_char(buf, ch);
    }
  }
  return (found);
}

int vnum_object_foci(char *searchname, struct char_data * ch)
{
  extern const char *wound_arr[];
  int nr, found = 0;

  for (nr = 0; nr <= top_of_objt; nr++) {
    if (GET_OBJ_TYPE(&obj_proto[nr]) == ITEM_FOCUS
	&& !from_ip_zone(obj_index[nr].virt)) {
      sprintf(buf, "%3d. [%5d] %d +%2d %s\r\n", ++found,
              obj_index[nr].virt,
	      GET_OBJ_VAL(&obj_proto[nr], VALUE_FOCUS_TYPE),
	      GET_OBJ_VAL(&obj_proto[nr], VALUE_FOCUS_RATING),
              obj_proto[nr].short_description);
      send_to_char(buf, ch);
    }
  }
  return (found);
}

int vnum_object_type(int type, struct char_data * ch)
{
  int nr, found = 0;

  for (nr = 0; nr <= top_of_objt; nr++) {
    if (GET_OBJ_TYPE(&obj_proto[nr]) == type)
      {
	sprintf(buf, "%3d. [%5d] %s %s\r\n", ++found,
		obj_index[nr].virt,
		from_ip_zone(obj_index[nr].virt) ? " " : "*",
		obj_proto[nr].short_description);
	send_to_char(buf, ch);
      }
  }
  return (found);
}

int vnum_object(char *searchname, struct char_data * ch)
{
  int nr, found = 0;
  char arg1[MAX_STRING_LENGTH];
  char arg2[MAX_STRING_LENGTH];

  two_arguments(searchname, arg1, arg2);

  if (!strcmp(searchname,"weaponslist"))
    return vnum_object_weapons(searchname,ch);
  if (!strcmp(searchname,"focilist"))
    return vnum_object_foci(searchname,ch);
  if (!strcmp(arg1,"objtype"))
    return vnum_object_type(atoi(arg2),ch);
  for (nr = 0; nr <= top_of_objt; nr++) {
    if (isname(searchname, obj_proto[nr].name)
	||isname(searchname, obj_proto[nr].short_description)) {
      sprintf(buf, "%3d. [%5d] %s %s\r\n", ++found,
              obj_index[nr].virt,
	      from_ip_zone(obj_index[nr].virt) ? " " : "*",
              obj_proto[nr].short_description);
      send_to_char(buf, ch);
    }
  }
  return (found);
}

/* create a character, and add it to the char list */
struct char_data *create_char(void)
{
  struct char_data *ch;

  ch = Mem->GetCh();
  ch->next = character_list;
  character_list = ch;

  return ch;
}

/* create a new mobile from a prototype */
struct char_data *read_mobile(int nr, int type)
{
  int i;
  struct char_data *mob;

  if (type == VIRTUAL) {
    if ((i = real_mobile(nr)) < 0) {
      sprintf(buf, "Mobile (V) %d does not exist in database.", nr);
      return (0);
    }
  } else i = nr;

  mob = Mem->GetCh();
  *mob = mob_proto[i];
  mob->next = character_list;
  character_list = mob;

  mob->points.physical = mob->points.max_physical;
  mob->points.mental = mob->points.max_mental;
  mob->points.move = mob->points.max_move;

  mob->player.time.birth = time(0);
  mob->player.time.played = 0;
  mob->player.time.logon = time(0);
  mob->char_specials.saved.left_handed = (!number(0, 9) ? 1 : 0);
  GET_WIELDED(mob, 0) = 0;
  GET_WIELDED(mob, 1) = 0;

  mob_index[i].number++;

  affect_total(mob);

  if (GET_MOB_SPEC(mob) && !MOB_FLAGGED(mob, MOB_SPEC))
    SET_BIT(MOB_FLAGS(mob), MOB_SPEC);

  return mob;
}

/* create an object, and add it to the object list */
struct obj_data *create_obj(void)
{
  struct obj_data *obj;

  //obj = new obj_data;
  // the objects are cleared already
  obj = Mem->GetObject();
  //clear_object(obj);
  //obj->next = object_list;
  //object_list = obj;

  ObjList.ADD(obj);

  return obj;
}

/* create a new object from a prototype */
struct obj_data *read_object(int nr, int type)
{
  struct obj_data *obj;
  int i;

  if (nr < 0) {
    log("SYSERR: trying to create obj with negative num!");
    return NULL;
  }
  if (type == VIRTUAL) {
    if ((i = real_object(nr)) < 0) {
      sprintf(buf, "Object (V) %d does not exist in database.", nr);
      return NULL;
    }
  } else
    i = nr;

  //obj = new obj_data;
  obj = Mem->GetObject();
  //clear_object(obj);
  *obj = obj_proto[i];
  //obj->next = object_list;
  //object_list = obj;

  ObjList.ADD(obj);

  obj_index[i].number++;

  return obj;
}

void spec_update(void)
{
  int i;
  struct obj_data *obj;

  for (i = 0; i < top_of_world; i++)
    if (world[i].func != NULL)
      world[i].func (NULL, world + i, 0, "");

  ObjList.CallSpec();
}

#define ZO_DEAD  999

/* update zone ages, queue for reset if necessary, and dequeue when possible */
void zone_update(void)
{
  int i;
  struct reset_q_element *update_u, *temp;
  static int timer = 0;
  char buf[128];

  /* jelson 10/22/92 */
  if (((++timer * PULSE_ZONE) / PASSES_PER_SEC) >= 60) {
    /* one minute has passed */
    /*
     * NOT accurate unless PULSE_ZONE is a multiple of PASSES_PER_SEC or a
     * factor of 60
     */

    timer = 0;

    /* since one minute has passed, increment zone ages */
    for (i = 0; i <= top_of_zone_table; i++) {
      if (zone_table[i].age < zone_table[i].lifespan &&
          zone_table[i].reset_mode)
        (zone_table[i].age)++;

      if (zone_table[i].age >= zone_table[i].lifespan &&
          zone_table[i].age < ZO_DEAD && zone_table[i].reset_mode) {
        /* enqueue zone */
        update_u = new reset_q_element;

        update_u->zone_to_reset = i;
        update_u->next = 0;

        if (!reset_q.head)
          reset_q.head = reset_q.tail = update_u;
        else {
          reset_q.tail->next = update_u;
          reset_q.tail = update_u;
        }

        zone_table[i].age = ZO_DEAD;
      }
    }
  }                             /* end - one minute has passed */
  /* dequeue zones (if possible) and reset */
  /* this code is executed every 10 seconds (i.e. PULSE_ZONE) */
  for (update_u = reset_q.head; update_u; update_u = update_u->next)
    if (zone_table[update_u->zone_to_reset].reset_mode == 2 ||
        is_empty(update_u->zone_to_reset)) {
      reset_zone(update_u->zone_to_reset, 0);
      sprintf(buf, "Auto zone reset: %s",
              zone_table[update_u->zone_to_reset].name);
      mudlog(buf, NULL, LOG_ZONELOG, FALSE);
      /* dequeue */
      if (update_u == reset_q.head)
        reset_q.head = reset_q.head->next;
      else {
        for (temp = reset_q.head; temp->next != update_u; temp = temp->next);

        if (!update_u->next)
          reset_q.tail = temp;

        temp->next = update_u->next;
      }

      delete update_u;
      break;
    }
}

void log_zone_error(int zone, int cmd_no, char *message)
{
  char buf[256];

  sprintf(buf, "error in zone file: %s", message);
  mudlog(buf, NULL, LOG_ZONELOG, TRUE);

  sprintf(buf, " ...offending cmd: '%c' cmd in zone #%d, line %d, cmd %d",
          ZCMD.command, zone_table[zone].number, ZCMD.line, cmd_no);
  mudlog(buf, NULL, LOG_ZONELOG, TRUE);
}

#define ZONE_ERROR(message) {log_zone_error(zone, cmd_no, message); last_cmd = 0;}

/* execute the reset command table of a given zone */
void reset_zone(int zone, int reboot)
{
  SPECIAL(fixer);
  int cmd_no, j, last_cmd = 0, found = 0, no_mob = 0;
  static int i;
  struct char_data *mob = NULL;
  struct obj_data *obj, *obj_to, *check;

  for (cmd_no = 0; cmd_no < zone_table[zone].num_cmds; cmd_no++) {
    if (ZCMD.if_flag && !last_cmd)
      continue;
    found = 0;
    switch (ZCMD.command) {
      case '$':
        last_cmd = 0;
        break;
      case '*':                 /* ignore command */
        last_cmd = 0;
        break;
      case 'M':                 /* read a mobile */
        if ((mob_index[ZCMD.arg1].number < ZCMD.arg2) || (ZCMD.arg2 == -1) ||
            (ZCMD.arg2 == 0 && reboot)) {
          mob = read_mobile(ZCMD.arg1, REAL);
          char_to_room(mob, ZCMD.arg3);
          if (GET_MOB_SPEC(mob) && GET_MOB_SPEC(mob) == fixer &&
              mob_index[mob->nr].number == 1)
            load_fixer_data(mob);
          last_cmd = 1;
        } else {
          if (ZCMD.arg2 == 0 && !reboot)
            no_mob = 1;
          else no_mob = 0;
          last_cmd = 0;
          mob = NULL;
        }
        break;
      case 'O':                 /* read an object */
        if ((obj_index[ZCMD.arg1].number < ZCMD.arg2) || (ZCMD.arg2 == -1) ||
            (ZCMD.arg2 == 0 && reboot)) {
          obj = read_object(ZCMD.arg1, REAL);
          obj_to_room(obj, ZCMD.arg3);
          if (!from_ip_zone(GET_OBJ_VNUM(obj)) && !zone_table[zone].connected)
            SET_BIT(GET_OBJ_EXTRA(obj), ITEM_VOLATILE);
          last_cmd = 1;
        } else last_cmd = 0;
        break;
      case 'P':                 /* object to object */
        if ((obj_index[ZCMD.arg1].number < ZCMD.arg2) || (ZCMD.arg2 == -1) ||
            (ZCMD.arg2 == 0 && reboot)) {
          obj = read_object(ZCMD.arg1, REAL);
          if (!(obj_to = ObjList.FindObj(ZCMD.arg3))) {
            ZONE_ERROR("target obj not found");
            break;
          }
          obj_to_obj(obj, obj_to);
          if (!from_ip_zone(GET_OBJ_VNUM(obj)) && !zone_table[zone].connected)
            SET_BIT(GET_OBJ_EXTRA(obj), ITEM_VOLATILE);
          last_cmd = 1;
        } else last_cmd = 0;
        break;
      case 'G':                 /* obj_to_char */
        if (!mob) {
          if (!no_mob)
            ZONE_ERROR("attempt to give obj to non-existant mob");
          break;
        }
        if ((obj_index[ZCMD.arg1].number < ZCMD.arg2) || (ZCMD.arg2 == -1) ||
            (ZCMD.arg2 == 0 && reboot)) {
          obj = read_object(ZCMD.arg1, REAL);
          obj_to_char(obj, mob);
          if (!from_ip_zone(GET_OBJ_VNUM(obj)) && !zone_table[zone].connected)
            SET_BIT(GET_OBJ_EXTRA(obj), ITEM_VOLATILE);
          last_cmd = 1;
        } else last_cmd = 0;
        break;
      case 'E':                 /* object to equipment list */
        if (!mob) {
          if (!no_mob)
            ZONE_ERROR("trying to equip non-existant mob");
          break;
        }
        if ((obj_index[ZCMD.arg1].number < ZCMD.arg2) || (ZCMD.arg2 == -1) ||
            (ZCMD.arg2 == 0 && reboot)) {
          if (ZCMD.arg3 < 0 || ZCMD.arg3 >= NUM_WEARS) {
            ZONE_ERROR("invalid equipment pos number");
          } else {
            obj = read_object(ZCMD.arg1, REAL);
            equip_char(mob, obj, ZCMD.arg3);
            if (!from_ip_zone(GET_OBJ_VNUM(obj)) && !zone_table[zone].connected)
              SET_BIT(GET_OBJ_EXTRA(obj), ITEM_VOLATILE);
            last_cmd = 1;
          }
        } else last_cmd = 0;
        break;
      case 'N':  // give x number of items to a mob
        if (!mob) {
          if (!no_mob)
            ZONE_ERROR("attempt to give obj to non-existant mob");
          break;
        }
        last_cmd = 0;
        for (i = 0; (i < ZCMD.arg3) && ((obj_index[ZCMD.arg1].number < ZCMD.arg2) ||
            (ZCMD.arg2 == -1) || (ZCMD.arg2 == 0 && reboot)); ++i) {
          obj = read_object(ZCMD.arg1, REAL);
          obj_to_char(obj, mob);
          if (!from_ip_zone(GET_OBJ_VNUM(obj)) && !zone_table[zone].connected)
            SET_BIT(GET_OBJ_EXTRA(obj), ITEM_VOLATILE);
          last_cmd = 1;
        }
        break;
      case 'C': // give mob bio/cyberware
        if (!mob) {
          if (!no_mob)
            ZONE_ERROR("attempt to give obj to non-existant mob");
          break;
        }
        obj = read_object(ZCMD.arg1, REAL);
        if (!ZCMD.arg2) {
          if (GET_OBJ_TYPE(obj) != ITEM_CYBERWARE) {
            ZONE_ERROR("attempt to install non-cyberware to mob");
            break;
          }
          if (GET_ESS(mob) < GET_OBJ_VAL(obj, 1))
            break;
          for (check = mob->cyberware; check && !found; check = check->next_content) {
            if ((GET_OBJ_VNUM(check) == GET_OBJ_VNUM(obj)))
              found = 1;
            if (GET_OBJ_VAL(check, 2) == GET_OBJ_VAL(obj, 2))
              found = 1;
          }
          if (GET_OBJ_VAL(obj, 2) == 23 || GET_OBJ_VAL(obj, 2) == 30 ||
              GET_OBJ_VAL(obj, 2) == 20)
            for (check = mob->bioware; check && !found; check = check->next_content) {
              if (GET_OBJ_VAL(check, 2) == 2 && GET_OBJ_VAL(obj, 2) == 23)
                found = 1;
              if (GET_OBJ_VAL(check, 2) == 8 && GET_OBJ_VAL(obj, 2) == 30)
                found = 1;
              if (GET_OBJ_VAL(check, 2) == 10 && GET_OBJ_VAL(obj, 2) == 20)
                found = 1;
            }
          if (found)
            break;
          obj_to_cyberware(obj, mob);
        } else {
          if (GET_OBJ_TYPE(obj) != ITEM_BIOWARE) {
            ZONE_ERROR("attempt to install non-bioware to mob");
            break;
          }
          if (GET_INDEX(mob) < GET_OBJ_VAL(obj, 1))
            break;
          for (check = mob->bioware; check && !found; check = check->next_content) {
            if ((GET_OBJ_VNUM(check) == GET_OBJ_VNUM(obj)))
                found = 1;
            if (GET_OBJ_VAL(check, 2) == GET_OBJ_VAL(obj, 2))
                found = 1;
          }
          if (GET_OBJ_VAL(obj, 2) == 2 || GET_OBJ_VAL(obj, 2) == 8 || GET_OBJ_VAL(obj, 2) == 10)
            for (check = mob->cyberware; check; check = check->next_content) {
              if (GET_OBJ_VAL(check, 2) == 23 && GET_OBJ_VAL(obj, 2) == 2)
                found = 1;
              if (GET_OBJ_VAL(check, 2) == 30 && GET_OBJ_VAL(obj, 2) == 8)
                found = 1;
              if (GET_OBJ_VAL(check, 2) == 20 && GET_OBJ_VAL(obj, 2) == 10)
                found = 1;
            }
          if (found)
            break;
          if (GET_OBJ_VAL(obj, 2) == 0)
            GET_OBJ_VAL(obj, 5) = 24;
          obj_to_bioware(obj, mob);
        }
        last_cmd = 1;
        break;
      case 'R': /* rem obj from room */
        if ((obj = get_obj_in_list_num(ZCMD.arg2, world[ZCMD.arg1].contents)) != NULL) {
          obj_from_room(obj);
          extract_obj(obj);
        }
        last_cmd = 1;
        break;
      case 'D':                 /* set state of door */
        if (ZCMD.arg2 < 0 || ZCMD.arg2 >= NUM_OF_DIRS ||
            (world[ZCMD.arg1].dir_option[ZCMD.arg2] == NULL)) {
          ZONE_ERROR("door does not exist");
        } else {
          bool ok = FALSE;
          int opposite = MAX(0, world[ZCMD.arg1].dir_option[ZCMD.arg2]->to_room);
          if (!world[opposite].dir_option[rev_dir[ZCMD.arg2]] || (ZCMD.arg1 !=
              world[opposite].dir_option[rev_dir[ZCMD.arg2]]->to_room)) {
            sprintf(buf, "Note: Exits from %d to %d do not coincide (zone %d, line %d, cmd %d)",
                    world[opposite].number, world[ZCMD.arg1].number, zone_table[zone].number,
                    ZCMD.line, cmd_no);
            mudlog(buf, NULL, LOG_ZONELOG, FALSE);
          } else ok = TRUE;
          // here I set the hidden flag for the door if hidden > 0
          if (world[ZCMD.arg1].dir_option[ZCMD.arg2]->hidden)
            SET_BIT(world[ZCMD.arg1].dir_option[ZCMD.arg2]->exit_info, EX_HIDDEN);
          if (ok && world[opposite].dir_option[rev_dir[ZCMD.arg2]]->hidden)
            SET_BIT(world[opposite].dir_option[rev_dir[ZCMD.arg2]]->exit_info, EX_HIDDEN);
          // repair all damage
          REMOVE_BIT(world[ZCMD.arg1].dir_option[ZCMD.arg2]->exit_info, EX_DESTROYED);
          world[ZCMD.arg1].dir_option[ZCMD.arg2]->condition =
               world[ZCMD.arg1].dir_option[ZCMD.arg2]->barrier;
          if (ok) {
            world[opposite].dir_option[rev_dir[ZCMD.arg2]]->material =
                 world[ZCMD.arg1].dir_option[ZCMD.arg2]->material;
            world[opposite].dir_option[rev_dir[ZCMD.arg2]]->barrier =
                 world[ZCMD.arg1].dir_option[ZCMD.arg2]->barrier;
            world[opposite].dir_option[rev_dir[ZCMD.arg2]]->condition =
                 world[ZCMD.arg1].dir_option[ZCMD.arg2]->condition;
            REMOVE_BIT(world[opposite].dir_option[rev_dir[ZCMD.arg2]]->exit_info, EX_DESTROYED);
          }
          switch (ZCMD.arg3) {
          // you now only have to set one side of a door
            case 0:
              REMOVE_BIT(world[ZCMD.arg1].dir_option[ZCMD.arg2]->exit_info, EX_LOCKED);
              REMOVE_BIT(world[ZCMD.arg1].dir_option[ZCMD.arg2]->exit_info, EX_CLOSED);
              if (ok) {
                REMOVE_BIT(world[opposite].dir_option[rev_dir[ZCMD.arg2]]->exit_info, EX_LOCKED);
                REMOVE_BIT(world[opposite].dir_option[rev_dir[ZCMD.arg2]]->exit_info, EX_CLOSED);
              }
              break;
            case 1:
              SET_BIT(world[ZCMD.arg1].dir_option[ZCMD.arg2]->exit_info, EX_CLOSED);
              REMOVE_BIT(world[ZCMD.arg1].dir_option[ZCMD.arg2]->exit_info, EX_LOCKED);
              if (ok) {
                SET_BIT(world[opposite].dir_option[rev_dir[ZCMD.arg2]]->exit_info, EX_CLOSED);
                REMOVE_BIT(world[opposite].dir_option[rev_dir[ZCMD.arg2]]->exit_info, EX_LOCKED);
              }
              break;
            case 2:
              SET_BIT(world[ZCMD.arg1].dir_option[ZCMD.arg2]->exit_info, EX_LOCKED);
              SET_BIT(world[ZCMD.arg1].dir_option[ZCMD.arg2]->exit_info, EX_CLOSED);
              if (ok) {
                SET_BIT(world[opposite].dir_option[rev_dir[ZCMD.arg2]]->exit_info, EX_LOCKED);
                SET_BIT(world[opposite].dir_option[rev_dir[ZCMD.arg2]]->exit_info, EX_CLOSED);
              }
              break;
          }
        }
        last_cmd = 1;
        break;
      default:
        ZONE_ERROR("unknown cmd in reset table; cmd disabled");
        ZCMD.command = '*';
        break;
    }
  }

  zone_table[zone].age = 0;
  if (zone_table[zone].alert != 4)
    zone_table[zone].alert = 0;
  if (zone == real_zone(31))
    for (j = 0; j <= top_of_zone_table; j++)
      zone_table[j].alert = 0;
}

/* for use in reset_zone; return TRUE if zone 'nr' is Free of PC's  */
int is_empty(int zone_nr)
{
  struct descriptor_data *i;

  for (i = descriptor_list; i; i = i->next)
    if (!i->connected)
      if (world[i->character->in_room].zone == zone_nr)
        return 0;

  return 1;
}

/*************************************************************************
*  stuff related to the save/load player system                          *
*********************************************************************** */

void html_stats(void)
{
  extern const char *pc_race_types[];

  FILE *html;

  if (!(html = fopen("../../public_html/plrstats.html", "w+"))) {
    log("Error opening plrstats.html.");
    return;
  }

  struct char_file_u tch;
  int total = top_of_p_table + 1;
  int humans = 0, elves = 0, dwarves = 0, trolls = 0, orks = 0;
  int actives[6], mages[6], shamans[6], adepts[6];
  int height[6], weight[6], righties[6];
  sh_int i, num = 0;
  char *tmstr;
  time_t thetime = time(0);

  tmstr = (char *) asctime(localtime(&thetime));
  *(tmstr + strlen(tmstr) - 6) = '\0';

  for (i = 0; i < 6; i++) {
    mages[i] = 0;
    shamans[i] = 0;
    adepts[i] = 0;
    height[i] = 0;
    weight[i] = 0;
    righties[i] = 0;
  }

  for (i = 0; i <= top_of_p_table; i++) {
    if ((load_char((player_table + i)->name, &tch)) < 0)
      continue;

    num = tch.race;
    switch (num) {
      case RACE_HUMAN: humans++;  break;
      case RACE_DWARF: dwarves++; break;
      case RACE_ELF:   elves++;   break;
      case RACE_ORK:   orks++;    break;
      case RACE_TROLL: trolls++;  break;
    }
    switch (tch.tradition) {
      case TRAD_HERMETIC: mages[num]++;    break;
      case TRAD_SHAMANIC: shamans[num]++;  break;
      case TRAD_ADEPT:    adepts[num]++;   break;
    }
    if (!(tch.char_specials_saved.left_handed))
      righties[num]++;
    height[num] += tch.height;
    weight[num] += tch.weight;
  }

  for (i = 0; i < 6; i++)
    actives[i] = mages[i] + shamans[i] + adepts[i];

  for (i = 0; i < 5; i++) {
    height[5] += height[i];
    weight[5] += weight[i];
    righties[5] += righties[i];
    actives[5] += actives[i];
    mages[5] += mages[i];
    shamans[5] += shamans[i];
    adepts[5] += adepts[i];
  }

  fprintf(html, "<html>\n<head>\n<title>Player statistics</title></head>\n");
  fprintf(html, "<body text=\"#808080\" bgcolor=\"#000000\" link=\"#0000FF\" "
                "vlink=\"#000080\" alink=\"#0000A0\">\n\n");
  fprintf(html, "<h2 align=center><font color=\"#0000F0\">Player statistics</font></h2>\n");
  fprintf(html, "<h5 align=center>Last updated: %s</h5>\n", tmstr + 4);
  fprintf(html, "<hr size=1 align=center width=75%%>\n<tt>\n");

  for (i = 0; i <= 5; i++) {
    switch (i) {
      case 0: num = humans;  break;
      case 1: num = dwarves; break;
      case 2: num = elves;   break;
      case 3: num = orks;    break;
      case 4: num = trolls;  break;
      case 5: num = total;   break;
    }
    if (i < 5)
      fprintf(html, "<h4>%s: %d (%0.2f%%)</h4>\n", pc_race_types[i], num,
              ((float)num * 100 / total));
    else fprintf(html, "<h3>Total: %d</h3>\n", num);
    fprintf(html, "<pre>  Average height: %0.2f m, average weight: %0.2f kg.\n",
                  ((float)height[i] / (100 * num)), ((float)weight[i] / num));
    fprintf(html, "  %d (%0.2f%%) are right-handed.\n",
                  righties[i], ((float)righties[i] * 100 / num));
    fprintf(html, "  %d (%0.2f%%) are magically active, of which:\n",
                  actives[i], ((float)actives[i] * 100 / num));
    fprintf(html, "    %d (%0.2f%%) follow the hermetic tradition,\n",
                  mages[i], ((float)mages[i] * 100 / num));
    fprintf(html, "    %d (%0.2f%%) follow the shamanic tradition, and\n",
                  shamans[i], ((float)shamans[i] * 100 / num));
    fprintf(html, "    %d (%0.2f%%) are physical adepts.</pre>\n\n",
                  adepts[i], ((float)adepts[i] * 100 / num));
  }

  fprintf(html, "</p>\n</body>\n</html>\n");
  fclose(html);
}

long get_id_by_name(char *name)
{
  int i;

  one_argument(name, arg);
  for (i = 0; i <= top_of_p_table; i++)
    if (!strcmp((player_table + i)->name, arg))
      return ((player_table + i)->id);

  return -1;
}

char *get_name_by_id(long id)
{
  int i;

  if (id < 1 || id > top_idnum)
    return NULL;

  for (i = 0; i <= top_of_p_table; i++)
    if ((player_table + i)->id == id)
      return ((player_table + i)->name);

  return NULL;
}

/* Load a char, TRUE if loaded, FALSE if not */
int load_char(char *name, struct char_file_u * char_element)
{
  int player_i;

  int find_name(char *name);

  if ((player_i = find_name(name)) >= 0) {
    fseek(player_fl, (long) (player_i * sizeof(struct char_file_u)), SEEK_SET);
    fread(char_element, sizeof(struct char_file_u), 1, player_fl);
    return (player_i);
  } else return (-1);
}

/* write the vital data of a player to the player file */
void save_char(struct char_data * ch, sh_int load_room)
{
  struct char_file_u st;

  if (IS_NPC(ch) || !ch->desc || GET_PFILEPOS(ch) < 0)
    return;
  char_to_store(ch, &st);

  strncpy(st.host, ch->desc->host, HOST_LENGTH);
  st.host[HOST_LENGTH] = '\0';

  if (load_room == NOWHERE)
    load_room = GET_LOADROOM(ch);

  if (!PLR_FLAGGED(ch, PLR_LOADROOM))
    st.player_specials_saved.load_room = load_room;

  strcpy(st.pwd, GET_PASSWD(ch));

  fseek(player_fl, GET_PFILEPOS(ch) * sizeof(struct char_file_u), SEEK_SET);
  fwrite(&st, sizeof(struct char_file_u), 1, player_fl);
}

/* copy data from the file structure to a char struct */
void store_to_char(struct char_file_u * st, struct char_data * ch)
{
  /* to save memory, only PC's -- not MOB's -- have player_specials */
  if (ch->player_specials == NULL)
    ch->player_specials = new player_special_data;

  GET_SEX(ch) = st->sex;
  GET_RACE(ch) = st->race;
  GET_LEVEL(ch) = st->level;
  GET_TRADITION(ch) = st->tradition;
  if (GET_TRADITION(ch) != TRAD_HERMETIC && GET_TRADITION(ch) != TRAD_SHAMANIC &&
      GET_TRADITION(ch) != TRAD_ADEPT && GET_TRADITION(ch) != TRAD_MUNDANE)
    GET_TRADITION(ch) = TRAD_MUNDANE;

  ch->player.name = new char[strlen(st->name) + 1];
  strcpy(ch->player.name, st->name);
  strcpy(ch->player.passwd, st->pwd);

  ch->player.short_descr = NULL;
  ch->player.long_descr = NULL;
  ch->player.title = str_dup(st->title);
  ch->player.pretitle = str_dup(st->pretitle);
  ch->player.whotitle = str_dup(st->whotitle);
  ch->player.description = str_dup(st->description);
  ch->player.prompt = str_dup(st->prompt);
  ch->player.poofin = str_dup(st->poofin);
  ch->player.poofout = str_dup(st->poofout);
  ch->player.mname = str_dup(st->mname);
  ch->player.msdesc = str_dup(st->msdesc);
  ch->player.mdesc = str_dup(st->mdesc);
  ch->player.mldesc = str_dup(st->mldesc);
  ch->player.aname = str_dup(st->aname);
  ch->player.asdesc = str_dup(st->asdesc);
  ch->player.adesc = str_dup(st->adesc);
  ch->player.aldesc = str_dup(st->aldesc);

  ch->player.last_room = st->hometown;
  ch->player.time.birth = st->birth;
  ch->player.time.played = st->played;
  ch->player.time.logon = time(0);

  ch->player.weight = st->weight;
  ch->player.height = st->height;

  ch->real_abils = st->abilities;
  ch->aff_abils = st->abilities;
  ch->points = st->points;
  ch->char_specials.saved = st->char_specials_saved;
  ch->player_specials->saved = st->player_specials_saved;

  if (ch->points.max_physical < 1000)
    ch->points.max_physical = 1000;

  if (ch->points.max_mental < 1000)
    ch->points.max_mental = 1000;

  ch->char_specials.carry_weight = 0;
  ch->char_specials.carry_items = 0;
  ch->points.ballistic = 0;
  ch->points.init_dice = 0;
  ch->points.init_roll = 0;
  ch->points.sustained = 0;

  /* Do not add spell effects since spells are sustained
  for (i = 0; i < MAX_AFFECT; i++) {
    if (st->affected[i].type)
      affect_to_char(ch, &st->affected[i]);
  }
  */

  ch->in_room = real_room(GET_LOADROOM(ch));
  GET_LAST_TELL(ch) = NOBODY;

  affect_total(ch);

  /*
   * If you're not poisioned and you've been away for more than an hour,
   * we'll set your HMV back to full
   */

  if (!IS_AFFECTED(ch, AFF_POISON) &&
      (((long) (time(0) - st->last_logon)) >= SECS_PER_REAL_HOUR)) {
    GET_PHYSICAL(ch) = GET_MAX_PHYSICAL(ch);
    GET_MOVE(ch) = GET_MAX_MOVE(ch);
    GET_MENTAL(ch) = GET_MAX_MENTAL(ch);
  }
}                               /* store_to_char */

/* copy vital data from a players char-structure to the file structure */
void char_to_store(struct char_data * ch, struct char_file_u * st)
{
  int i, wield[2];
  struct affected_type *af;
  struct obj_data *char_eq[NUM_WEARS];
  struct obj_data *temp, *next_obj;

  wield[0] = GET_WIELDED(ch, 0);
  wield[1] = GET_WIELDED(ch, 1);

  /* Unaffect everything a character can be affected by */

  // worn eq
  for (i = 0; i < NUM_WEARS; i++) {
    if (ch->equipment[i])
      char_eq[i] = unequip_char(ch, i);
    else char_eq[i] = NULL;
  }

  GET_WIELDED(ch, 0) = wield[0];
  GET_WIELDED(ch, 1) = wield[1];

  // cyberware
  for (temp = ch->cyberware; temp; temp = next_obj) {
    next_obj = temp->next_content;
    obj_from_cyberware(temp);
    obj_to_char(temp, ch);
  }

  /* bioware */
  for (temp = ch->bioware; temp; temp = next_obj) {
    next_obj = temp->next_content;
    obj_from_bioware(temp);
    obj_to_char(temp, ch);
  }

  for (af = ch->affected, i = 0; i < MAX_AFFECT; i++) {
    if (af) {
      st->affected[i] = *af;
      st->affected[i].next = NULL;
      af = af->next;
    } else {
      st->affected[i].type = 0; /* Zero signifies not used */
      st->affected[i].duration = 0;
      st->affected[i].modifier = 0;
      st->affected[i].location = 0;
      st->affected[i].bitvector = 0;
      st->affected[i].next = NULL;
    }
  }


  /*
   * remove the affections so that the raw values are stored; otherwise the
   * effects are doubled when the char logs back in.
   */

  while (ch->affected)
    affect_remove(ch, ch->affected, 0);

  if ((i >= MAX_AFFECT) && af && af->next)
    log("SYSERR: WARNING: OUT OF STORE ROOM FOR AFFECTED TYPES!!!");

  ch->aff_abils = ch->real_abils;

  st->birth = ch->player.time.birth;
  st->played = ch->player.time.played;
  st->played += (long) (time(0) - ch->player.time.logon);
  st->last_logon = time(0);

  ch->player.time.played = st->played;
  ch->player.time.logon = time(0);

  st->hometown = ch->player.last_room;
  st->weight = GET_WEIGHT(ch);
  st->height = GET_HEIGHT(ch);
  st->race = GET_RACE(ch);
  st->level = GET_LEVEL(ch);
  st->sex = GET_SEX(ch);
  st->tradition = GET_TRADITION(ch);
  st->abilities = ch->real_abils;
  st->points = ch->points;
  st->char_specials_saved = ch->char_specials.saved;
  st->player_specials_saved = ch->player_specials->saved;

  st->points.ballistic = 0;
  st->points.impact = 0;
  st->points.init_dice = 0;
  st->points.init_roll = 0;
  st->points.sustained = 0;

  strcpy(st->name, GET_NAME(ch));

  if (ch->player.description)
    strcpy(st->description, ch->player.description);
  else *st->description = '\0';

  if (GET_TITLE(ch))
    strcpy(st->title, GET_TITLE(ch));
  else *st->title = '\0';

  if (GET_PRETITLE(ch))
    strcpy(st->pretitle, GET_PRETITLE(ch));
  else *st->pretitle = '\0';

  if (GET_WHOTITLE(ch))
    strcpy(st->whotitle, GET_WHOTITLE(ch));
  else *st->whotitle = '\0';

  if (ch->player.prompt)
    strcpy(st->prompt, ch->player.prompt);
  else *st->prompt = '\0';

  if (ch->player.poofin)
    strcpy(st->poofin, ch->player.poofin);
  else sprintf(st->poofin, "%s appears in a cloud of smoke.", GET_NAME(ch));

  if (ch->player.poofout)
    strcpy(st->poofout, ch->player.poofout);
  else sprintf(st->poofout, "%s disappears in a cloud of smoke.", GET_NAME(ch));

  if (ch->player.mname)
    strcpy(st->mname, ch->player.mname);
  else strcpy(st->mname, "persona");

  if (ch->player.msdesc)
    strcpy(st->msdesc, ch->player.msdesc);
  else strcpy(st->msdesc, "a persona");

  if (ch->player.mdesc)
    strcpy(st->mdesc, ch->player.mdesc);
  else strcpy(st->mdesc, "A nondescript persona stands idly here.\r\n");

  if (ch->player.mldesc)
    strcpy(st->mldesc, ch->player.mldesc);
  else *st->mldesc = '\0';

  if (ch->player.aname)
    strcpy(st->aname, ch->player.aname);
  else strcpy(st->aname, "reflection");

  if (ch->player.asdesc)
    strcpy(st->asdesc, ch->player.asdesc);
  else strcpy(st->asdesc, "a reflection");

  if (ch->player.adesc)
    strcpy(st->adesc, ch->player.adesc);
  else strcpy(st->adesc, "The reflection of some physical being stands here.\r\n");

  if (ch->player.aldesc)
    strcpy(st->aldesc, ch->player.aldesc);
  else *st->aldesc = '\0';

  /* add spell and eq affections back in now */
  // do cyberware first
  for (temp = ch->carrying; temp; temp = next_obj) {
    next_obj = temp->next_content;
    if ((GET_OBJ_TYPE(temp) == ITEM_CYBERWARE) && (GET_OBJ_VAL(temp, 4) == 1)) {
      obj_from_char(temp);
      obj_to_cyberware(temp, ch);
    }
    /* bioware next */
    if (GET_OBJ_TYPE(temp) == ITEM_BIOWARE && GET_OBJ_VAL(temp, 4) == 1) {
      obj_from_char(temp);
      obj_to_bioware(temp, ch);
    }
  }

  for (i = 0; i < MAX_AFFECT; i++) {
    if (st->affected[i].type)
      affect_to_char(ch, &st->affected[i]);
  }

  // then worn eq
  for (i = 0; i < NUM_WEARS; i++) {
    if (char_eq[i])
      equip_char(ch, char_eq[i], i);
  }

  affect_total(ch);
}                               /* Char to store */

void save_etext(struct char_data * ch)
{
  if (!get_filename(GET_NAME(ch), buf2, ETEXT_FILE) || !GET_ALIASES(ch))
    return;

  FILE *outFile;

  if (!(outFile = fopen(buf2, "w+"))) {
    sprintf(buf, "Unable to write %s's alias file.\n", GET_NAME(ch));
    mudlog(buf, ch, LOG_SYSLOG, TRUE);
    return;
  }

  for (struct alias *a = GET_ALIASES(ch); a; a = a->next)
    fprintf(outFile, "%s %s\n", a->command, a->replacement);

  fclose(outFile);
}

void read_etext(struct char_data *ch)
{
  if (GET_ALIASES(ch) || !get_filename(GET_NAME(ch), buf2, ETEXT_FILE))
    return;

  FILE *inFile;

  if (!(inFile = fopen(buf2, "r"))) {
    if (errno != ENOENT) {
      sprintf(buf, "Unable to open %s's alias file.\n", GET_NAME(ch));
      mudlog(buf, ch, LOG_SYSLOG, TRUE);
    }
    return;
  }

  char *repl;
  char line[256];
  while (get_line(inFile, line)) {
    repl = any_one_arg(line, arg);
    struct alias *a = new alias;
    a->command = str_dup(arg);
    skip_spaces(&repl);
    a->replacement = str_dup(repl);
    if (strchr((const char *)repl, ALIAS_SEP_CHAR) ||
        strchr((const char *)repl, ALIAS_VAR_CHAR))
      a->type = ALIAS_COMPLEX;
    else a->type = ALIAS_SIMPLE;
    a->next = GET_ALIASES(ch);
    GET_ALIASES(ch) = a;
  }

  fclose(inFile);
}

/* create a new entry in the in-memory index table for the player file */
int create_entry(char *name)
{
  int i;

  if (top_of_p_table == -1) {
    player_table = new player_index_element;
    top_of_p_table = 0;
  }
  else
  {
    //if (!(player_table = (struct player_index_element *)
//             realloc(player_table, sizeof(struct player_index_element) *
//                     (++top_of_p_table + 1)))) {
    player_index_element *temp = new player_index_element[++top_of_p_table + 1];
    if (!temp)
    {
      perror("create entry");
      exit(1);
    }
    for (register int iterator = 0; iterator < (top_of_p_table + 1); ++iterator)
    {
      temp[iterator].name = player_table[iterator].name;
      temp[iterator].id = player_table[iterator].id;
    }
    delete [] player_table;
    player_table = temp;
  }
  player_table[top_of_p_table].name = new char[strlen(name) + 1];

  /* copy lowercase equivalent of name to table field */
  for (i = 0; (*(player_table[top_of_p_table].name + i) = LOWER(*(name + i)));
       i++);

  return (top_of_p_table);
}



/************************************************************************
*  funcs of a (more or less) general utility nature                     *
********************************************************************** */

// These were added for OLC purposes.  They'll allocate a new array of
// the old size + 100 elements, copy over, and Free up the old one.
// They return TRUE if it worked, FALSE if it didn't.  I could add in some
// checks and report to folks using OLC that currently there's not enough
// room to allocate.  Obviously these belong in an object once I convert
// completely over to C++.
bool resize_world_array() {
  int counter, result;
  struct room_data *new_world;

  new_world = new struct room_data[top_of_world_array + world_chunk_size];

  if (!new_world) {
    mudlog("Unable to allocate new world array.", NULL, LOG_SYSLOG, TRUE);
    mudlog("OLC temporarily unavailable.", NULL, LOG_SYSLOG, TRUE);
    olc_state = 0;  // disallow any olc from here on
    return FALSE;
  }

  // remember that top_of_world is the actual rnum in the array
  for (counter = 0; counter <= top_of_world; counter++)
     new_world[counter] = world[counter];

  top_of_world_array += world_chunk_size;

  delete [] world;
  world = new_world;

  sprintf(buf, "World array resized to %d.", top_of_world_array);
  mudlog(buf, NULL, LOG_SYSLOG, TRUE);
  return TRUE;
}

bool resize_mob_array() {
  int counter;
  struct char_data *new_mob;
  struct index_data *new_mob_index;

  new_mob = new struct char_data[top_of_mob_array + mob_chunk_size];

  if (!new_mob) {
    mudlog("Unable to allocate new mob array.", NULL, LOG_SYSLOG, TRUE);
    mudlog("OLC temporarily unavailable.", NULL, LOG_SYSLOG, TRUE);
    olc_state = 0;
    return FALSE;
  }

  new_mob_index = new struct index_data[top_of_mob_array + mob_chunk_size];

  if (!new_mob_index) {
    mudlog("Unable to allocate new mob index array.", NULL, LOG_SYSLOG, TRUE);
    mudlog("OLC temporarily unavailable.", NULL, LOG_SYSLOG, TRUE);
    olc_state = 0;
    return FALSE;
  }

  // do both mob_proto and mob_index
  for (counter = 0; counter <= top_of_mobt; counter++) {
     new_mob[counter] = mob_proto[counter];
     new_mob_index[counter] = mob_index[counter];
  }

  top_of_mob_array += mob_chunk_size;

  delete [] mob_proto;
  delete [] mob_index;
  mob_proto = new_mob;
  mob_index = new_mob_index;

  sprintf(buf, "Mob and Mob Index arrays resized to %d", top_of_mob_array);
  mudlog(buf, NULL, LOG_SYSLOG, TRUE);
  return TRUE;
}

bool resize_obj_array() {
  int counter;
  struct obj_data *new_obj;
  struct index_data *new_obj_index;

  new_obj = new struct obj_data[top_of_obj_array + obj_chunk_size];

  if (!new_obj) {
    mudlog("Unable to allocate new obj array.", NULL, LOG_SYSLOG, TRUE);
    mudlog("OLC temporarily unavailable.", NULL, LOG_SYSLOG, TRUE);
    olc_state = 0;
    return FALSE;
  }

  new_obj_index = new struct index_data[top_of_obj_array + obj_chunk_size];

  if (!new_obj_index) {
    mudlog("Unable to allocate new obj index array.", NULL, LOG_SYSLOG, TRUE);
    mudlog("OLC temporarily unavailable.", NULL, LOG_SYSLOG, TRUE);
    olc_state = 0;
    return FALSE;
  }

  // do for both obj_proto and obj_index
  for (counter = 0; counter <= top_of_objt; counter++) {
     new_obj[counter] = obj_proto[counter];
     new_obj_index[counter] = obj_index[counter];
  }

  top_of_obj_array += obj_chunk_size;

  delete [] obj_proto;
  delete [] obj_index;
  obj_proto = new_obj;
  obj_index = new_obj_index;

  sprintf(buf, "Obj and Obj Index arrays resized to %d", top_of_obj_array);
  mudlog(buf, NULL, LOG_SYSLOG, TRUE);
  return TRUE;
}

bool resize_qst_array(void)
{
  int counter;
  struct quest_data *new_qst;

  new_qst = new struct quest_data[top_of_quest_array + quest_chunk_size];

  if (!new_qst) {
    mudlog("Unable to allocate new quest array.", NULL, LOG_SYSLOG, TRUE);
    mudlog("OLC temporarily unavailable.", NULL, LOG_SYSLOG, TRUE);
    olc_state = 0;
    return FALSE;
  }

  for (counter = 0; counter <= top_of_questt; counter++)
    new_qst[counter] = quest_table[counter];

  top_of_quest_array += quest_chunk_size;

  delete [] quest_table;
  quest_table = new_qst;

  sprintf(buf, "Quest array resized to %d", top_of_quest_array);
  mudlog(buf, NULL, LOG_SYSLOG, TRUE);
  return TRUE;
}

bool resize_shp_array(void)
{
  int counter;
  struct shop_data *new_shp;

  new_shp = new struct shop_data[top_of_shop_array + shop_chunk_size];

  if (!new_shp) {
    mudlog("Unable to allocate new shop array.", NULL, LOG_SYSLOG, TRUE);
    mudlog("OLC temporarily unavailable.", NULL, LOG_SYSLOG, TRUE);
    olc_state = 0;
    return FALSE;
  }

  for (counter = 0; counter <= top_of_shopt; counter++)
    new_shp[counter] = shop_table[counter];

  top_of_shop_array += shop_chunk_size;

  delete [] shop_table;
  shop_table = new_shp;

  sprintf(buf, "Shop array resized to %d", top_of_shop_array);
  mudlog(buf, NULL, LOG_SYSLOG, TRUE);
  return TRUE;
}

/* read and allocate space for a '~'-terminated string from a given file */
char *fread_string(FILE * fl, char *error)
{
  char buf[MAX_STRING_LENGTH], tmp[512], *rslt;
  register char *point;
  int done = 0, length = 0, templength = 0;

  *buf = '\0';

  do {
    if (!fgets(tmp, 512, fl)) {
      fprintf(stderr, "SYSERR: fread_string: format error at or near %s\n", error);
      exit(1);
    }
    /* If there is a '~', end the string; else put an "\r\n" over the '\n'. */
    if ((point = strchr((const char *)tmp, '~')) != NULL) {
      *point = '\0';
      done = 1;
    } else {
      point = tmp + strlen(tmp) - 1;
      *(point++) = '\r';
      *(point++) = '\n';
      *point = '\0';
    }

    templength = strlen(tmp);

    if (length + templength >= MAX_STRING_LENGTH) {
      log("SYSERR: fread_string: string too large (db.c)");
      exit(1);
    } else {
      strcat(buf + length, tmp);
      length += templength;
    }
  } while (!done);

  /* allocate space for the new string and copy it */
  if (strlen(buf) > 0) {
    rslt = new char[length + 1];
    strcpy(rslt, buf);
  } else
    rslt = NULL;

  return rslt;
}

/* release memory allocated for a char struct */
void free_char(struct char_data * ch)
{
  int i;
  struct alias *a, *temp, *next;
  extern void free_alias(struct alias * a);

  if (ch->player_specials != NULL && ch->player_specials != &dummy_mob) {
    // we have to delete these here before we delete the struct
    for (a = GET_ALIASES(ch); a; a = next) {
      next = a->next;
      REMOVE_FROM_LIST(a, GET_ALIASES(ch), next);
      free_alias(a);
    }

    if (ch->player_specials->mob_complete)
      delete [] ch->player_specials->mob_complete;
    if (ch->player_specials->obj_complete)
      delete [] ch->player_specials->obj_complete;

    delete ch->player_specials;
    if (IS_NPC(ch))
      log("SYSERR: Mob had player_specials allocated!");
  }
  if (!IS_NPC(ch) || (IS_NPC(ch) && GET_MOB_RNUM(ch) == -1)) {
    /* if this is a player, or a non-prototyped non-player, Free all */
    if (GET_NAME(ch))
      delete [] GET_NAME(ch);
    if (ch->player.title)
      delete [] ch->player.title;
    if (ch->player.pretitle)
      delete [] ch->player.pretitle;
    if (ch->player.whotitle)
      delete [] ch->player.whotitle;
    if (ch->player.short_descr)
      delete [] ch->player.short_descr;
    if (ch->player.long_descr)
      delete [] ch->player.long_descr;
    if (ch->player.description)
      delete [] ch->player.description;
    if (ch->player.prompt)
      delete [] ch->player.prompt;
    if (ch->player.poofin)
      delete [] ch->player.poofin;
    if (ch->player.poofout)
      delete [] ch->player.poofout;
    if (ch->player.mname)
      delete [] ch->player.mname;
    if (ch->player.msdesc)
      delete [] ch->player.msdesc;
    if (ch->player.mdesc)
      delete [] ch->player.mdesc;
    if (ch->player.mldesc)
      delete [] ch->player.mldesc;
    if (ch->player.aname)
      delete [] ch->player.aname;
    if (ch->player.asdesc)
      delete [] ch->player.asdesc;
    if (ch->player.adesc)
      delete [] ch->player.adesc;
    if (ch->player.aldesc)
      delete [] ch->player.aldesc;
    if (ch->player_specials->gname)
      delete [] ch->player_specials->gname;
  } else if ((i = GET_MOB_RNUM(ch)) > -1) {
    /* otherwise, Free strings only if the string is not pointing at proto */
    if (ch->player.name && ch->player.name != mob_proto[i].player.name &&
        GET_MOB_VNUM(ch) != 20 && GET_MOB_VNUM(ch) != 22)
      delete [] ch->player.name;
    if (ch->player.title && ch->player.title != mob_proto[i].player.title)
      delete [] ch->player.title;
    if (ch->player.short_descr && ch->player.short_descr != mob_proto[i].player.short_descr &&
        GET_MOB_VNUM(ch) != 20 && GET_MOB_VNUM(ch) != 22)
      delete [] ch->player.short_descr;
    if (ch->player.long_descr && ch->player.long_descr != mob_proto[i].player.long_descr &&
        GET_MOB_VNUM(ch) != 20 && GET_MOB_VNUM(ch) != 22)
      delete [] ch->player.long_descr;
    if (ch->player.description && ch->player.description != mob_proto[i].player.description &&
        GET_MOB_VNUM(ch) != 20 && GET_MOB_VNUM(ch) != 22)
      delete [] ch->player.description;
  }
  while (ch->affected)
    affect_remove(ch, ch->affected, 0);

  clear_char(ch);
}

void free_room(struct room_data *room)
{
  struct extra_descr_data *This, *next_one;
//  struct use_descr_data *This2, *next_one2;

  // first free up the strings
  if (room->name)
    delete [] room->name;
  if (room->description)
    delete [] room->description;
  if (room->msp_trigger)
    delete [] room->msp_trigger;

  // then free up the exits
  for (int counter = 0; counter < NUM_OF_DIRS; counter++)
    {
      if (room->dir_option[counter]) {
        if (room->dir_option[counter]->general_description)
          delete [] room->dir_option[counter]->general_description;
        if (room->dir_option[counter]->keyword)
          delete [] room->dir_option[counter]->keyword;
        delete room->dir_option[counter];
      }
    }
  // now the extra descriptions
  if (room->ex_description)
    for (This = room->ex_description; This; This = next_one) {
      next_one = This->next;
      if (This->keyword)
        delete [] This->keyword;
      if (This->description)
        delete [] This->description;
      delete This;
    }

  clear_room(room);
}

/* release memory allocated for an obj struct */
void free_obj(struct obj_data * obj)
{
  int nr;
  struct extra_descr_data *this1, *next_one;
  struct use_descr_data *this2, *next2;

  if ((nr = GET_OBJ_RNUM(obj)) == -1)
  {
    if (obj->name)
      delete [] obj->name;
    if (obj->description)
      delete [] obj->description;
    if (obj->short_description)
      delete [] obj->short_description;
    if (obj->long_description)
      delete [] obj->long_description;
    if (obj->ex_description)
      for (this1 = obj->ex_description; this1; this1 = next_one) {
        next_one = this1->next;
          if (this1->keyword)
            delete [] this1->keyword;
          if (this1->description)
            delete [] this1->description;
          delete this1;
      }
    if (obj->use_description)
      for (this2 = obj->use_description; this2; this2 = next2) {
        next2 = this2->next;
        if (this2->keyword)
          delete [] this2->keyword;
        if (this2->description1)
          delete [] this2->description1;
        if (this2->description2)
          delete [] this2->description2;
        delete this2;
      }
  } else {
    if (obj->name && obj->name != obj_proto[nr].name)
      delete [] obj->name;
    if (obj->description && obj->description != obj_proto[nr].description)
      delete [] obj->description;
    if (obj->short_description && obj->short_description != obj_proto[nr].short_description)
      delete [] obj->short_description;
    if (obj->long_description && obj->long_description != obj_proto[nr].long_description)
      delete [] obj->long_description;
    if (obj->ex_description && obj->ex_description != obj_proto[nr].ex_description)
      for (this1 = obj->ex_description; this1; this1 = next_one) {
        next_one = this1->next;
          if (this1->keyword)
            delete [] this1->keyword;
          if (this1->description)
            delete [] this1->description;
          delete this1;
      }
    if (obj->use_description)
      for (this2 = obj->use_description; this2; this2 = next2) {
        next2 = this2->next;
        if (this2->keyword)
          delete [] this2->keyword;
        if (this2->description1)
          delete [] this2->description1;
        if (this2->description2)
          delete [] this2->description2;
        delete this2;
      }
  }
  clear_object(obj);
}

void free_quest(struct quest_data *quest)
{
  if (quest->obj)
    delete [] quest->obj;
  if (quest->mob)
    delete [] quest->mob;
  if (quest->intro)
    delete [] quest->intro;
  if (quest->decline)
    delete [] quest->decline;
  if (quest->quit)
    delete [] quest->quit;
  if (quest->finish)
    delete [] quest->finish;
  if (quest->info)
    delete [] quest->info;
}

void free_shop(struct shop_data *shop)
{
  if (shop->no_such_item1)
    delete [] shop->no_such_item1;
  if (shop->no_such_item2)
    delete [] shop->no_such_item2;
  if (shop->missing_cash1)
    delete [] shop->missing_cash1;
  if (shop->missing_cash2)
    delete [] shop->missing_cash2;
  if (shop->do_not_buy)
    delete [] shop->do_not_buy;
  if (shop->message_buy)
    delete [] shop->message_buy;
  if (shop->message_sell)
    delete [] shop->message_sell;
  if (shop->producing)
    delete [] shop->producing;
  if (shop->type)
    delete [] shop->type;
  if (shop->in_room)
    delete [] shop->in_room;
}

/* read contets of a text file, alloc space, point buf to it */
int file_to_string_alloc(char *name, char **buf)
{
  char temp[MAX_STRING_LENGTH];

  if (file_to_string(name, temp) < 0)
    return -1;

  if (*buf)
    delete [] *buf;

  *buf = str_dup(temp);

  return 0;
}

/* read contents of a text file, and place in buf */
int file_to_string(char *name, char *buf)
{
  FILE *fl;
  char tmp[128];

  *buf = '\0';

  if (!(fl = fopen(name, "r"))) {
    sprintf(tmp, "Error reading %s", name);
    perror(tmp);
    return (-1);
  }
  do {
    fgets(tmp, 128, fl);
    tmp[strlen(tmp) - 1] = '\0';/* take off the trailing \n */
    strcat(tmp, "\r\n");

    if (!feof(fl)) {
      if (strlen(buf) + strlen(tmp) + 1 > MAX_STRING_LENGTH) {
        log("SYSERR: fl->strng: string too big (db.c, file_to_string)");
        *buf = '\0';
        return (-1);
      }
      strcat(buf, tmp);
    }
  } while (!feof(fl));

  fclose(fl);

  return (0);
}

/* clear some of the the working variables of a char */
void reset_char(struct char_data * ch)
{
  int i;

  for (i = 0; i < NUM_WEARS; i++)
    ch->equipment[i] = NULL;

  ch->followers = NULL;
  ch->master = NULL;
  ch->cyberware = NULL;
  ch->bioware = NULL;
  /* ch->in_room = NOWHERE; Used for start in room */
  ch->carrying = NULL;
  ch->next = NULL;
  ch->next_fighting = NULL;
  ch->next_in_room = NULL;
  FIGHTING(ch) = NULL;
  ch->char_specials.position = POS_STANDING;
  ch->mob_specials.default_pos = POS_STANDING;
  ch->char_specials.carry_weight = 0;
  ch->char_specials.carry_items = 0;

  if (GET_PHYSICAL(ch) < 100)
    GET_PHYSICAL(ch) = 100;
  if (GET_MOVE(ch) <= 0)
    GET_MOVE(ch) = 1;
  if (GET_MENTAL(ch) < 100)
    GET_MENTAL(ch) = 100;
}

/* clear ALL the working variables of a char; do NOT free any space alloc'ed */
void clear_char(struct char_data * ch)
{
  memset((char *) ch, 0, sizeof(struct char_data));

  ch->in_room = NOWHERE;
  GET_PFILEPOS(ch) = -1;
  GET_WAS_IN(ch) = NOWHERE;
  GET_POS(ch) = POS_STANDING;
  ch->mob_specials.default_pos = POS_STANDING;

  GET_BALLISTIC(ch) = 0;                /* Basic Armor */
  GET_IMPACT(ch) = 0;
  if (ch->points.max_mental < 1000)
    ch->points.max_mental = 1000;
}

/* Clear ALL the vars of an object; don't free up space though */
// we do this because generally, objects which are created are off of
// prototypes, and we don't want to delete the prototype strings
void clear_object(struct obj_data * obj)
{
  memset((char *) obj, 0, sizeof(struct obj_data));

  obj->item_number = NOTHING;
  obj->in_room = NOWHERE;
}

void clear_room(struct room_data *room)
{
  memset((char *) room, 0, sizeof(struct room_data));
}

/* initialize a new character only if class is set */
void init_char(struct char_data * ch)
{
  int i;

  /* create a player_special structure */
  if (ch->player_specials == NULL) {
    ch->player_specials = new player_special_data;
    memset((char *) ch->player_specials, 0, sizeof(struct player_special_data));
  }

  /* *** if this is our first player --- he be God *** */

  if (top_of_p_table == 0) {
    GET_KARMA(ch) = 0;
    GET_LEVEL(ch) = LVL_OWNER;
    GET_REP(ch) = 0;

    ch->points.max_physical = 10000;
    ch->points.max_mental = 10000;
    ch->points.max_move = 100;
  }
  set_title(ch, NULL);

  ch->player.short_descr = NULL;
  ch->player.long_descr = NULL;
  ch->player.description = NULL;

  ch->player.last_room = 0;

  ch->player.time.birth = time(0);
  ch->player.time.played = 0;
  ch->player.time.logon = time(0);

  /* make favors for sex */
  switch (GET_RACE(ch)) {
    case RACE_DWARF:
      if (ch->player.sex == SEX_MALE) {
        ch->player.weight = number(50, 62);
        ch->player.height = number(115, 133);
      } else {
        ch->player.weight = number(45, 56);
        ch->player.height = number(105, 124);
      }
      break;
    case RACE_ELF:
      if (ch->player.sex == SEX_MALE) {
        ch->player.weight = number(70, 82);
        ch->player.height = number(180, 205);
      } else {
        ch->player.weight = number(60, 75);
        ch->player.height = number(175, 195);
      }
      break;
    case RACE_HUMAN:
      if (ch->player.sex == SEX_MALE) {
        ch->player.weight = number(65, 77);
        ch->player.height = number(160, 187);
      } else {
        ch->player.weight = number(56, 69);
        ch->player.height = number(145, 175);
      }
      break;
    case RACE_ORK:
      if (ch->player.sex == SEX_MALE) {
        ch->player.weight = number(90, 105);
        ch->player.height = number(185, 210);
      } else {
        ch->player.weight = number(85, 95);
        ch->player.height = number(178, 195);
      }
      break;
    case RACE_TROLL:
      if (ch->player.sex == SEX_MALE) {
        ch->player.weight = number(215, 245);
        ch->player.height = number(270, 295);
      } else {
        ch->player.weight = number(200, 230);
        ch->player.height = number(255, 280);
      }
      break;
    default:
      if (ch->player.sex == SEX_MALE) {
        ch->player.weight = number(65, 77);
        ch->player.height = number(160, 187);
      } else {
        ch->player.weight = number(56, 69);
        ch->player.height = number(145, 175);
      }
      break;
  }

  ch->points.max_mental = 1000;
  ch->points.mental = GET_MAX_MENTAL(ch);
  ch->points.physical = GET_MAX_PHYSICAL(ch);
  ch->points.max_move = 100;
  ch->points.move = GET_MAX_MOVE(ch);
  ch->points.ballistic = 0;
  ch->points.impact = 0;
  ch->points.sustained = 0;

  player_table[top_of_p_table].id = GET_IDNUM(ch) = ++top_idnum;

  if (!access_level(ch, LVL_OWNER))
    for (i = 1; i <= MAX_SKILLS; i++) {
      SET_SKILL(ch, i, 0)
    }
  else
    for (i = 1; i <= MAX_SKILLS; i++) {
      SET_SKILL(ch, i, 100);
    }

  ch->char_specials.saved.affected_by = 0;

  for (i = 0; i < 3; i++)
    GET_COND(ch, i) = (access_level(ch, LVL_OWNER) ? -1 : 24);
}

/* returns the real number of the room with given virtual number */
int real_room(int virt)
{
  int bot, top, mid;

  bot = 0;
  top = top_of_world;

  /* perform binary search on world-table */
  for (;;) {
    mid = (bot + top) >> 1;

    if ((world + mid)->number == virt)
      return mid;
    if (bot >= top)
      return -1;
    if ((world + mid)->number > virt)
      top = mid - 1;
    else
      bot = mid + 1;
  }
}

/* returns the real number of the monster with given virtual number */
int real_mobile(int virt)
{
  int bot, top, mid;

  bot = 0;
  top = top_of_mobt;

  /* perform binary search on mob-table */
  for (;;) {
    mid = (bot + top) / 2;

    if ((mob_index + mid)->virt == virt)
      return (mid);
    if (bot >= top)
      return (-1);
    if ((mob_index + mid)->virt > virt)
      top = mid - 1;
    else
      bot = mid + 1;
  }
}

int real_quest(int virt)
{
  int bot, top, mid;

  bot = 0;
  top = top_of_questt;

  for (;;) {
    mid = (bot + top) >> 1;

    if ((quest_table + mid)->virt == virt)
      return mid;
    if (bot >= top)
      return -1;
    if ((quest_table + mid)->virt > virt)
      top = mid - 1;
    else
      bot = mid + 1;
  }
}

int real_shop(int virt)
{
  int bot, top, mid;

  bot = 0;
  top = (top_of_shopt - 1);

  for (;;) {
    mid = (bot + top) / 2;

    if ((shop_table + mid)->virt == virt)
      return (mid);
    if (bot >= top)
      return (-1);
    if ((shop_table + mid)->virt > virt)
      top = mid - 1;
    else bot = mid + 1;
  }
}

int real_zone(int virt)
{
  int bot, top, mid;

  bot = 0;
  top = top_of_zone_table;

  /* perform binary search on zone-table */
  for (;;) {
    mid = (bot + top) / 2;

    if ((zone_table + mid)->number == virt)
      return (mid);
    if (bot >= top)
      return (-1);
    if ((zone_table + mid)->number > virt)
      top = mid - 1;
    else
      bot = mid + 1;
  }
}

/* returns the real number of the object with given virtual number */
int real_object(int virt)
{
  int bot, top, mid;

  bot = 0;
  top = top_of_objt;

  /* perform binary search on obj-table */
  for (;;) {
    mid = (bot + top) / 2;

    if ((obj_index + mid)->virt == virt)
      return (mid);
    if (bot >= top)
      return (-1);
    if ((obj_index + mid)->virt > virt)
      top = mid - 1;
    else
      bot = mid + 1;
  }
}

char *short_object(int virt, int what)
{
  int bot, top, mid;

  bot = 0;
  top = top_of_objt;

  /* perform binary search on obj-table */
  for (;;) {
    mid = (bot + top) / 2;

    if ((obj_index + mid)->virt == virt)
         /* 1-namelist, 2-shortdescription */
      if (what == 1)
        return (obj_proto[mid].name);
      else if (what == 2)
        return (obj_proto[mid].short_description);
    if (bot >= top)
      return (NULL);
    if ((obj_index + mid)->virt > virt)
      top = mid - 1;
    else
      bot = mid + 1;
  }
}