cdirt/ascii/
cdirt/data/BULL/
cdirt/data/ZONES/PENDING/
cdirt/pending/
cdirt/src/utils/
cdirt/utils/
#include <stdlib.h>
#include "kernel.h"
#include "zones.h"
#include "levels.h"
#include "mobile.h"
#include "rooms.h"
#include "objsys.h"
#include "clone.h"
#include "bootstrap.h"
#include "bprintf.h"
#include "log.h"
#include "parse.h"
#include "uaf.h"
#include "locations.h"

/* returns the number of players in a zone, excludes aliased mobs */

int count_players_in_zone(int zone) {
  int i;
  int plrs = 0;

  for (i = 0 ; i < max_players ; i++)
    if (is_in_game(i) && loc2zone(ploc(i)) == zone)
      plrs++;
  return(plrs);
}

/* give this two numbers, LOCMIN & LOCMAX */

int count_players_between_locs(int loca, int locb) {
  int i;
  int plrs = 0;

    for (i = 0 ; i < max_players ; i++)
      if (is_in_game(i) && ploc(i) <= loca && ploc(i) >= locb)
        plrs++;
    return(plrs);
}

/* Return the zone index for a given zone name (abbreviation).
 * -1 if no such zone is found.
 */
int
get_zone_by_name (char *name)
{
  int x;
  int w = strlen (name);

  for (x = 0; x < numzon; x++) {
    if (strncasecmp (name, zname (x), w) == 0)
      return x;
  }

  return -1;
}

/* Return the zone index among the non-permanent zones which matches
 * the given name exactly. If none exists, create one and return that index.
 */

int get_wizzone_by_name(char *zonename){
  int i;
  char name[20];

  strcpy(name, zonename);
  for (i = num_const_zon; i < numzon; i++) {
    if (strcasecmp (name, zname (i)) == 0)
      return i;
  }

  if (numzon == zon_array_len) {
    zon_array_len += 20;

    zoname = resize_array (zoname, sizeof (ZONE),
			   numzon, zon_array_len);
  }
  zname (numzon) = COPY (name);

  init_intset (zlocs (numzon), 5);
  init_intset (zmobs (numzon), 3);
  init_intset (zobjs (numzon), 3);

  return numzon++;
}

/* Return the ZONE entry index for the zone that 'loc' is member of.
 * Return -1 if invalid loc number.
 */
int
loc2zone (int loc)
{
  return exists (loc) ? lzone (loc) : -1;
}

/* Take a 'loc' as argument and return the zone name and the offset within
 * the zone for this loc.
 * If buff = NULL, return the name in a static buffer.
 */
int
findzone (int loc, char *buff)
{
  int z, x;

  if ((z = loc2zone (loc)) == -1) {
    strcpy (buff, "TCHAN");
    return 0;
  }
  strcpy (buff, zname (z));

  if (!(x = find_int (loc, zlocs (z)))) {
    mudlog ("ERROR: Location %d was not in its zone %d.", loc, z);
  }
  return x;
}

/* This function is inverse of findzone in that it
 * from a zone index and offset number finds the loc that it makes up.
 * 0 is returned on error.
 */
int
getlocid (int z, int off)
{
  int a;

  if (z == -1 || z >= numzon)
    return 0;

  if (off == 0)
    off = 1;
  else if (off < 0)
    return 0;

  return (a = find_int_number (off - 1, zlocs (z))) == SET_END ? 0 : a;
}

/* This function is inverse of findzone in that it
 * from a zone name and number finds the loc that it makes up.
 * 0 is returned on error.
 */
int
getlocnum (char *zname, int off)
{
  return getlocid (get_zone_by_name (zname), off);
}

/*
 * Reset a zone. If r_* != NULL, return the number of locs/objs/mobs that
 * were successfully reset ; modified to not destruct wizard locations
 */

void reset_zone (int z, time_t * now, int *r_locs, int *r_mobs, int *r_objs) {
  int i, ct, loc;
  int xr_locs = 0, xr_mobs = 0, xr_objs = 0;

#ifdef LOCMIN_VOLCANO
    if(!strcmp(zname(z), "volcano")) { 
      room_data[convroom(LOC_VOLCANO_BANDILBAR23)].r_exit[2] = LOC_VOLCANO_BANDILBAR24;
      room_data[convroom(LOC_VOLCANO_BANDILBAR28)].r_exit[1] = LOC_VOLCANO_BANDILBAR26;
    }
#endif

    /* reset data for each room/obj/mob, if it moves from the intset,   *
     * then decrement ct so that when ct++ happens we are at the proper *
     * place in the link list (where the old one was deleted            */

    for (i = zfirst_mob(z), ct = 0; ct < znumchars(z) ; i = zmob_nr(++ct, z)) {
      loc = pzone(i);
      xr_mobs += reset_mobile(i);
      if (loc != pzone(i)) ct--;
    }

    for (i = zfirst_obj(z), ct = 0; ct < znumobs(z) ; i = zobj_nr(++ct, z)) {
      loc = ozone(i);
      xr_objs += reset_object(i);
      if (loc != ozone(i)) ct--;
    }

    for (i = zfirst_loc(z), ct = 0; ct < znumloc(z) ; i = zloc_nr(++ct, z)) { 
      loc = lzone(i);
      xr_locs += reset_location(i);
      if (loc != lzone(i)) ct--;
    }

  if (r_locs != NULL)
    *r_locs = xr_locs;          /* locs reset     */
  if (r_mobs != NULL)
    *r_mobs = xr_mobs;          /* mobs reset     */
  if (r_objs != NULL)
    *r_objs = xr_objs;          /* objs reset     */
}

/* Load a zone from disk into the game.
 * Name, is its owner, which also identifies the files it's stored on.
 * Place the number of items found and the number actually loaded in the
 * positions pointed to by the arguments.
 * Return memory allocated as our function value, -1 on error.
 */
int
load_zone (char *name, int *nlocs, int *nlocs_f, int *nmobs, int *nmobs_f,
	   int *nobjs, int *nobjs_f)
{
  Boolean locs_exist, mobs_exist, objs_exist;
  char filename[128];
  FILE *locfile, *mobfile, *objfile;
  int loc_mem = 0, mob_mem = 0, obj_mem = 0;
  int z;

  if (nlocs != NULL)
    *nlocs = 0;
  if (nlocs_f != NULL)
    *nlocs_f = 0;
  if (nmobs != NULL)
    *nmobs = 0;
  if (nmobs_f != NULL)
    *nmobs_f = 0;
  if (nobjs != NULL)
    *nobjs = 0;
  if (nobjs_f != NULL)
    *nobjs_f = 0;

  wiz_loc_filename (filename, name);
  locs_exist = (locfile = FOPEN(filename, "r")) != NULL;

  wiz_mob_filename (filename, name);
  mobs_exist = (mobfile = FOPEN(filename, "r")) != NULL;

  wiz_obj_filename (filename, name);
  objs_exist = (objfile = FOPEN(filename, "r")) != NULL;

  if (!(locs_exist || mobs_exist || objs_exist))
    return 0;

  z = get_wizzone_by_name (name);

  if (locs_exist) {
    loc_mem = load_locations (z, locfile, nlocs, nlocs_f);
    FCLOSE(locfile);
  }
  if (mobs_exist) {
    mob_mem = load_mobiles (z, mobfile, nmobs, nmobs_f);
    FCLOSE(mobfile);
  }
  if (objs_exist) {
    obj_mem = load_objects (z, objfile, nobjs, nobjs_f);
    FCLOSE(objfile);
  }
  reset_zone (z, NULL, NULL, NULL, NULL);

  return (loc_mem < 0 || mob_mem < 0 || obj_mem < 0) ? -1
    : loc_mem + mob_mem + obj_mem;
}

/* From a character name, get the filenames that will contain his zone. */
static char *
wiz_zone_filename (char *buff, char *name, char *contents)
{
  sprintf (buff, "%s/%s.%s", WIZ_ZONES, name, contents);
  return buff;
}

char *
wiz_mob_filename (char *buff, char *name)
{
  return wiz_zone_filename (buff, name, "mobiles");
}

char *
wiz_loc_filename (char *buff, char *name)
{
  return wiz_zone_filename (buff, name, "locations");
}

char *
wiz_obj_filename (char *buff, char *name)
{
  return wiz_zone_filename (buff, name, "objects");
}

/* The ZONES command.
 */
void
zonescom (void)
{
  int a;

  if (plev (mynum) < LVL_WIZARD) {
    erreval ();
    return;
  }
  bprintf ("&+C     Zone-Name Rooms  Mobiles  Objects  "
	   "Zone-Name  Rooms  Mobiles  Objects\n\n");

  for (a = 0; a < num_const_zon; a++) {
    bprintf ("&*%14s&+W%5d %7d %7d", zname (a), znumloc (a), znumchars (a), znumobs (a));

    if (a % 2 == 1 || a == num_const_zon - 1)
      bprintf ("\n");
  }

  bprintf ("\n&*A total of &+W%d &*permanent zones containing "
	   "&+W%d &*rooms, &+W%d &*mobiles, &+W%d &*objects.\n\n",
	   num_const_zon, num_const_locs, num_const_chars - max_players,
	   num_const_obs);

  if (numzon != num_const_zon) {
    int i = 0;

    bprintf ("&+C     Zone-Name Rooms  Mobiles  Objects  "
	     "Zone-Name  Rooms  Mobiles  Objects\n\n");

    for (a = num_const_zon; a < numzon; a++) {
      int locs = znumloc (a);
      int mobs = znumchars (a);
      int objs = znumobs (a);

      if (locs > 0 || mobs > 0 || objs > 0) {
	bprintf ("&*%14s&+W%5d %7d %7d", zname (a), locs, mobs, objs);

	if (i++ % 2 == 1)
	  bprintf ("\n");
      }
    }

    bprintf ("\n&*A total of &+W%d &*Wizard's zones containing "
	     "&+W%d &*rooms, &+W%d &*mobiles, &+W%d &*objects.\n",
	     i,
	     numloc - num_const_locs,
	     numchars - num_const_chars,
	     numobs - num_const_obs);
  }
}

/* The LOCATIONS command
 */
void
locationscom ()
{
  int a, b;

  b = 0;

  if (plev (mynum) < LVL_WIZARD) {
    erreval ();
    return;
  }
  bprintf ("&+CZone           Locations  Zone           Locations  Zone           Locations\n");
  bprintf ("&+c------------------------  ------------------------  ------------------------\n");

  for (a = 0; a < num_const_zon; a++) {
    ++b;
    bprintf ("&*%-15s&+W%9d", zname (a), znumloc (a));
    if (b % 3 == 0 || a == num_const_zon - 1) {
      b = 0;
      bprintf ("\n");
    } else
      bprintf ("  ");
  }

  if (b)
    bprintf ("\n");
  bprintf ("&+c------------------------  ------------------------  ------------------------\n");
  bprintf ("&+wA total of &+W%d &+wpermanent zones, with &+W%d &+wlocations.\n",
	   num_const_zon, num_const_locs);

  if (numzon != num_const_zon) {
    int i = 0;

    b = 0;
    bprintf ("&+c------------------------  ------------------------  ------------------------\n");
    bprintf ("&+CZone           Locations  Zone           Locations  Zone           Locations\n");
    bprintf ("&+c------------------------  ------------------------  ------------------------\n");

    for (a = num_const_zon; a < numzon; a++) {
      int locs = znumloc (a);
      int mobs = znumchars (a);
      int objs = znumobs (a);

      if (locs > 0 || mobs > 0 || objs > 0) {
	bprintf ("&*%-15s&+W%9d", zname (a), znumloc (a));
	++b;
	++i;
	if (b % 3 == 0 || a == num_const_zon - 1) {
	  b = 0;
	  bprintf ("\n");
	} else
	  bprintf ("  ");
      }
    }

    if (b)
      bprintf ("\n");
    bprintf ("&+c------------------------  ------------------------  ------------------------\n");
    bprintf ("&+wA total of &+W%d &+wWizard zones, with &+W%d &+wlocations.\n",
	     i, numloc - num_const_locs);
  }
}