circlemud_squared_0.5.153/cnf/
circlemud_squared_0.5.153/etc/
circlemud_squared_0.5.153/etc/etc/
circlemud_squared_0.5.153/etc/house/
circlemud_squared_0.5.153/etc/misc/
circlemud_squared_0.5.153/etc/plralias/A-E/
circlemud_squared_0.5.153/etc/plralias/F-J/
circlemud_squared_0.5.153/etc/plralias/K-O/
circlemud_squared_0.5.153/etc/plralias/P-T/
circlemud_squared_0.5.153/etc/plralias/U-Z/
circlemud_squared_0.5.153/etc/plralias/ZZZ/
circlemud_squared_0.5.153/etc/plrobjs/
circlemud_squared_0.5.153/etc/plrobjs/A-E/
circlemud_squared_0.5.153/etc/plrobjs/F-J/
circlemud_squared_0.5.153/etc/plrobjs/K-O/
circlemud_squared_0.5.153/etc/plrobjs/P-T/
circlemud_squared_0.5.153/etc/plrobjs/U-Z/
circlemud_squared_0.5.153/etc/plrobjs/ZZZ/
circlemud_squared_0.5.153/etc/text/
circlemud_squared_0.5.153/etc/text/help/
circlemud_squared_0.5.153/src/util/
circlemud_squared_0.5.153/src/util/worldconv/
/* ************************************************************************
*   File: house.c                                       Part of CircleMUD *
*  Usage: Handling of player houses                                       *
*                                                                         *
*  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.               *
************************************************************************ */

#include "conf.h"
#include "sysdep.h"
#include "structs.h"
#include "comm.h"
#include "command.h"
#include "handler.h"
#include "db.h"
#include "interpreter.h"
#include "utils.h"
#include "house.h"
#include "constants.h"
#include "log.h"
#include "room.h"
#include "item.h"
#include "zone.h"

/* external functions */
itemData_t *Obj_from_store(rentElement_t object, int *location);
int Obj_to_store(itemData_t *obj, FILE *fl, int location);

/* local globals */
houseData_t house_control[MAX_HOUSES];
int num_of_houses = 0;

/* local functions */
int House_get_filename(roomData_t *room, char *filename, size_t maxlen);
int House_load(roomData_t *room);
int House_save(itemData_t *obj, FILE *fp);
void House_restore_weight(itemData_t *obj);
void House_delete_file(roomData_t *room);
int find_house(roomData_t *room);
void House_save_control(void);
void hcontrol_list_houses(charData_t *ch);
void hcontrol_build_house(charData_t *ch, char *arg);
void hcontrol_destroy_house(charData_t *ch, char *arg);
void hcontrol_pay_house(charData_t *ch, char *arg);


/* First, the basics: finding the filename; loading/saving objects */

/* Return a filename given a house vnum */
int House_get_filename(roomData_t *room, char *filename, size_t maxlen)
{
  if (room == NULL)
    return (0);

  snprintf(filename, maxlen, ETC_HOUSE"%s:%d.house", room->zone->keyword, room->number);
  return (1);
}


/* Load all objects for a house */
int House_load(roomData_t *room)
{
  FILE *fl;
  char filename[MAX_STRING_LENGTH];
  rentElement_t object;
  int i;

  if (room == NULL)
    return (0);
  if (!House_get_filename(room, filename, sizeof(filename)))
    return (0);
  if (!(fl = fopen(filename, "r+b")))	/* no file found */
    return (0);
  while (!feof(fl)) {
    fread(&object, sizeof(rentElement_t), 1, fl);
    if (ferror(fl)) {
      perror("SYSERR: Reading house file in House_load");
      fclose(fl);
      return (0);
    }
    if (!feof(fl))
      itemData_toRoom(Obj_from_store(object, &i), room);
  }

  fclose(fl);

  return (1);
}


/* Save all objects for a house (recursive; initial call must be followed
   by a call to House_restore_weight)  Assumes file is open already. */
int House_save(itemData_t *obj, FILE *fp)
{
  itemData_t *tmp;
  int result;

  if (obj) {
    House_save(obj->contains, fp);
    House_save(obj->nextContent, fp);
    result = Obj_to_store(obj, fp, 0);
    if (!result)
      return (0);

    for (tmp = obj->inObj; tmp; tmp = tmp->inObj)
      GET_ITEM_WEIGHT(tmp) -= GET_ITEM_WEIGHT(obj);
  }
  return (1);
}


/* restore weight of containers after House_save has changed them for saving */
void House_restore_weight(itemData_t *obj)
{
  if (obj) {
    House_restore_weight(obj->contains);
    House_restore_weight(obj->nextContent);
    if (obj->inObj)
      GET_ITEM_WEIGHT(obj->inObj) += GET_ITEM_WEIGHT(obj);
  }
}


/* Save all objects in a house */
void House_crashsave(roomData_t *room)
{
  char buf[MAX_STRING_LENGTH];
  FILE *fp;

  if (room == NULL)
    return;
  if (!House_get_filename(room, buf, sizeof(buf)))
    return;
  if (!(fp = fopen(buf, "wb"))) {
    perror("SYSERR: Error saving house file");
    return;
  }
  if (!House_save(room->contents, fp)) {
    fclose(fp);
    return;
  }
  fclose(fp);
  House_restore_weight(room->contents);
  REMOVE_BIT(ROOM_FLAGS(room), ROOM_HOUSE_CRASH);
}


/* Delete a house save file */
void House_delete_file(roomData_t *room)
{
  char filename[MAX_INPUT_LENGTH];
  FILE *fl;

  if (!House_get_filename(room, filename, sizeof(filename)))
    return;
  if (!(fl = fopen(filename, "rb"))) {
    if (errno != ENOENT)
      log("SYSERR: Error deleting house file %s:%d. (1): %s", room->zone->keyword, room->number, strerror(errno));
    return;
  }
  fclose(fl);
  if (remove(filename) < 0)
    log("SYSERR: Error deleting house file %s:%d. (2): %s", room->zone->keyword, room->number, strerror(errno));
}


/* List all objects in a house file */
void House_listrent(charData_t *ch, roomData_t *room)
{
  FILE *fl;
  char filename[MAX_STRING_LENGTH];
  char buf[MAX_STRING_LENGTH];
  rentElement_t object;
  itemData_t *obj;
  int i;

  if (!House_get_filename(room, filename, sizeof(filename)))
    return;
  if (!(fl = fopen(filename, "rb"))) {
    send_to_char(ch, "No objects on file for house #%s.\r\n", filename);
    return;
  }
  *buf = '\0';
  while (!feof(fl)) {
    fread(&object, sizeof(rentElement_t), 1, fl);
    if (ferror(fl)) {
      fclose(fl);
      return;
    }
    if (!feof(fl) && (obj = Obj_from_store(object, &i)) != NULL) {
      send_to_char(ch, " [%s:%d] (%5dau) %s\r\n", obj->zone->keyword, obj->vnum, GET_ITEM_RENT(obj), obj->shortDescription);
      free_obj(obj);
    }
  }
  fclose(fl);
}




/******************************************************************
 *  Functions for house administration (creation, deletion, etc.  *
 *****************************************************************/

int find_house(roomData_t *room)
{
  int i;

  for (i = 0; i < num_of_houses; i++)
    if ((house_control[i].vnum == room->number) && (strcmp(house_control[i].hZoneKey, room->zone->keyword) == 0))
      return (i);

  return (-1);
}



/* Save the house control information */
void House_save_control(void)
{
  FILE *fl;

  if (!(fl = fopen(HCONTROL_FILE, "wb"))) {
    perror("SYSERR: Unable to open house control file.");
    return;
  }
  /* write all the house control recs in one fell swoop.  Pretty nifty, eh? */
  fwrite(house_control, sizeof(houseData_t), num_of_houses, fl);

  fclose(fl);
}


/* call from boot_db - will load control recs, load objs, set atrium bits */
/* should do sanity checks on vnums & remove invalid records */
void House_boot(void)
{
  houseData_t temp_house;
  roomData_t *house, *atrium;
  zoneData_t *hZone = NULL, *aZone = NULL;
  FILE *fl;

  memset((char *)house_control,0,sizeof(houseData_t)*MAX_HOUSES);

  if (!(fl = fopen(HCONTROL_FILE, "rb"))) {
    if (errno == ENOENT)
      log("   House control file '%s' does not exist.", HCONTROL_FILE);
    else
      perror("SYSERR: " HCONTROL_FILE);
    return;
  }
  while (!feof(fl) && num_of_houses < MAX_HOUSES) {
    fread(&temp_house, sizeof(houseData_t), 1, fl);

    if (feof(fl))
      break;

    if (get_name_by_id(temp_house.owner) == NULL)
      continue;			/* owner no longer exists -- skip */

    if ((hZone = zoneData_find(temp_house.hZoneKey)) == NULL)
      continue;                 /* Zone doesn't exist */

    if ((aZone = zoneData_find(temp_house.aZoneKey)) == NULL)
      continue;                 /* Zone doesn't exist */

    if ((house = roomData_findInZone(hZone, temp_house.vnum)) == NULL)
      continue;			/* this vnum doesn't exist -- skip */

    if (find_house(house) != -1)
      continue;			/* this vnum is already a house -- skip */

    if ((atrium = roomData_findInZone(aZone, temp_house.atrium)) == NULL)
      continue;			/* house doesn't have an atrium -- skip */

    if (temp_house.exit_num < 0 || temp_house.exit_num >= NUM_OF_DIRS)
      continue;			/* invalid exit num -- skip */

    if (TOROOM(house, temp_house.exit_num) != atrium)
      continue;			/* exit num mismatch -- skip */

    house_control[num_of_houses++] = temp_house;

    SET_BIT(ROOM_FLAGS(house), ROOM_HOUSE | ROOM_PRIVATE);
    SET_BIT(ROOM_FLAGS(atrium), ROOM_ATRIUM);
    House_load(house);
  }

  fclose(fl);
  House_save_control();
}



/* "House Control" functions */

const char *HCONTROL_FORMAT =
"Usage: hcontrol build <house vnum> <exit direction> <player name>\r\n"
"       hcontrol destroy <house vnum>\r\n"
"       hcontrol pay <house vnum>\r\n"
"       hcontrol show\r\n";

/**
 * @todo Recode this display output show it shows the zone part
 */
void hcontrol_list_houses(charData_t *ch)
{
  int i;
  char *timestr, *temp;
  char built_on[128], last_pay[128], own_name[MAX_NAME_LENGTH + 1];

  if (!num_of_houses) {
    send_to_char(ch, "No houses have been defined.\r\n");
    return;
  }
  send_to_char(ch,
	"Address  Atrium  Build Date  Guests  Owner        Last Paymt\r\n"
	"-------  ------  ----------  ------  ------------ ----------\r\n");

  for (i = 0; i < num_of_houses; i++) {
    /* Avoid seeing <UNDEF> entries from self-deleted people. -gg 6/21/98 */
    if ((temp = get_name_by_id(house_control[i].owner)) == NULL)
      continue;

    if (house_control[i].built_on) {
      timestr = asctime(localtime(&(house_control[i].built_on)));
      *(timestr + 10) = '\0';
      strlcpy(built_on, timestr, sizeof(built_on));
    } else
      strcpy(built_on, "Unknown");	/* strcpy: OK (for 'strlen("Unknown") < 128') */

    if (house_control[i].last_payment) {
      timestr = asctime(localtime(&(house_control[i].last_payment)));
      *(timestr + 10) = '\0';
      strlcpy(last_pay, timestr, sizeof(built_on));
    } else
      strcpy(last_pay, "None");	/* strcpy: OK (for 'strlen("None") < 128') */

    /* Now we need a copy of the owner's name to capitalize. -gg 6/21/98 */
    strcpy(own_name, temp);	/* strcpy: OK (names guaranteed <= MAX_NAME_LENGTH+1) */
    send_to_char(ch, "%7d %7d  %-10s    %2d    %-12s %s\r\n",
	    house_control[i].vnum, house_control[i].atrium, built_on,
	    house_control[i].num_of_guests, CAP(own_name), last_pay);

    House_list_guests(ch, i, TRUE);
  }
}



void hcontrol_build_house(charData_t *ch, char *arg)
{
  char arg1[MAX_INPUT_LENGTH];
  houseData_t temp_house;
  roomData_t *house = NULL, *atrium = NULL;
  sh_int exit_num;
  long owner;

  if (num_of_houses >= MAX_HOUSES) {
    send_to_char(ch, "Max houses already defined.\r\n");
    return;
  }

  /* first arg: house's vnum */
  arg = one_argument(arg, arg1);
  if (!*arg1) {
    send_to_char(ch, "%s", HCONTROL_FORMAT);
    return;
  }
  if ((house = roomData_find(arg1)) == NULL) {
    send_to_char(ch, "No such room exists.\r\n");
    return;
  }
  if ((find_house(house)) != -1) {
    send_to_char(ch, "House already exists.\r\n");
    return;
  }

  /* second arg: direction of house's exit */
  arg = one_argument(arg, arg1);
  if (!*arg1) {
    send_to_char(ch, "%s", HCONTROL_FORMAT);
    return;
  }
  if ((exit_num = search_block(arg1, dirs, FALSE)) < 0) {
    send_to_char(ch, "'%s' is not a valid direction.\r\n", arg1);
    return;
  }
  if (TOROOM(house, exit_num) == NULL) {
    send_to_char(ch, "There is no exit %s from room %s:%d.\r\n", dirs[exit_num], house->zone->keyword, house->number);
    return;
  }

  atrium = TOROOM(house, exit_num);

  if (TOROOM(atrium, rev_dir[exit_num]) != house) {
    send_to_char(ch, "A house's exit must be a two-way door.\r\n");
    return;
  }

  /* third arg: player's name */
  one_argument(arg, arg1);
  if (!*arg1) {
    send_to_char(ch, "%s", HCONTROL_FORMAT);
    return;
  }
  if ((owner = get_id_by_name(arg1)) < 0) {
    send_to_char(ch, "Unknown player '%s'.\r\n", arg1);
    return;
  }

  temp_house.mode = HOUSE_PRIVATE;
  temp_house.vnum = house->number;
  temp_house.atrium = atrium->number;
  temp_house.hZoneKey = strdup(house->zone->keyword);
  temp_house.aZoneKey = strdup(atrium->zone->keyword);
  temp_house.exit_num = exit_num;
  temp_house.built_on = time(0);
  temp_house.last_payment = 0;
  temp_house.owner = owner;
  temp_house.num_of_guests = 0;

  house_control[num_of_houses++] = temp_house;

  SET_BIT(ROOM_FLAGS(house), ROOM_HOUSE | ROOM_PRIVATE);
  SET_BIT(ROOM_FLAGS(atrium), ROOM_ATRIUM);
  House_crashsave(house);

  send_to_char(ch, "House built.  Mazel tov!\r\n");
  House_save_control();
}



void hcontrol_destroy_house(charData_t *ch, char *arg)
{
  int i, j;
  roomData_t *atrium = NULL, *house = NULL;

  if (!*arg) {
    send_to_char(ch, "%s", HCONTROL_FORMAT);
    return;
  }
  if ((house = roomData_find(arg)) == NULL) {
    send_to_char(ch, "Unknown room.\r\n");
    return;
  }
  if ((i = find_house(house)) == -1) {
    send_to_char(ch, "Unknown house.\r\n");
    return;
  }
  if ((atrium = roomData_findInZone(zoneData_find(house_control[i].aZoneKey), house_control[i].atrium)) == NULL) 
    log("SYSERR: House %d had invalid atrium %s:%d!", atoi(arg), house_control[i].aZoneKey, house_control[i].atrium);
  else
    REMOVE_BIT(ROOM_FLAGS(atrium), ROOM_ATRIUM);

  if ((house = roomData_findInZone(zoneData_find(house_control[i].hZoneKey), house_control[i].vnum)) == NULL) 
    log("SYSERR: House %d had invalid vnum %s:%d!", atoi(arg), house_control[i].hZoneKey, house_control[i].vnum);
  else
    REMOVE_BIT(ROOM_FLAGS(house), ROOM_HOUSE | ROOM_PRIVATE | ROOM_HOUSE_CRASH);

  if (house_control[i].hZoneKey)
    free(house_control[i].hZoneKey);

  if (house_control[i].aZoneKey)
    free(house_control[i].aZoneKey);  

  House_delete_file(house);

  for (j = i; j < num_of_houses - 1; j++)
    house_control[j] = house_control[j + 1];

  num_of_houses--;

  send_to_char(ch, "House deleted.\r\n");
  House_save_control();

  /*
   * Now, reset the ROOM_ATRIUM flag on all existing houses' atriums,
   * just in case the house we just deleted shared an atrium with another
   * house.  --JE 9/19/94
   */
  for (i = 0; i < num_of_houses; i++)
    if ((atrium = roomData_findInZone(zoneData_find(house_control[i].aZoneKey), house_control[i].atrium)) != NULL) 
      SET_BIT(ROOM_FLAGS(atrium), ROOM_ATRIUM);
}


void hcontrol_pay_house(charData_t *ch, char *arg)
{
  roomData_t *house = NULL;
  int i = -1;

  if (!*arg)
    send_to_char(ch, "%s", HCONTROL_FORMAT);
  else if ((house = roomData_find(arg)) == NULL)
    send_to_char(ch, "Unknown room.\r\n");
  else if ((i = find_house(house)) == -1)
    send_to_char(ch, "Unknown house.\r\n");
  else {
    mudlog(NRM, MAX(AUTH_WIZARD, GET_INVIS_AUTH(ch)), TRUE, "Payment for house %s collected by %s.", arg, GET_NAME(ch));

    house_control[i].last_payment = time(0);
    House_save_control();
    send_to_char(ch, "Payment recorded.\r\n");
  }
}


/* The hcontrol command itself, used by imms to create/destroy houses */
ACMD(do_hcontrol)
{
  char arg1[MAX_INPUT_LENGTH], arg2[MAX_INPUT_LENGTH];

  half_chop(argument, arg1, arg2);

  if (is_abbrev(arg1, "build"))
    hcontrol_build_house(ch, arg2);
  else if (is_abbrev(arg1, "destroy"))
    hcontrol_destroy_house(ch, arg2);
  else if (is_abbrev(arg1, "pay"))
    hcontrol_pay_house(ch, arg2);
  else if (is_abbrev(arg1, "show"))
    hcontrol_list_houses(ch);
  else
    send_to_char(ch, "%s", HCONTROL_FORMAT);
}


/* The house command, used by mortal house owners to assign guests */
ACMD(do_house)
{
  char arg[MAX_INPUT_LENGTH];
  int i, j, id;

  one_argument(argument, arg);

  if (!ROOM_FLAGGED(IN_ROOM(ch), ROOM_HOUSE))
    send_to_char(ch, "You must be in your house to set guests.\r\n");
  else if ((i = find_house(ch->room)) == -1)
    send_to_char(ch, "Um.. this house seems to be screwed up.\r\n");
  else if (GET_IDNUM(ch) != house_control[i].owner)
    send_to_char(ch, "Only the primary owner can set guests.\r\n");
  else if (!*arg)
    House_list_guests(ch, i, FALSE);
  else if ((id = get_id_by_name(arg)) < 0)
    send_to_char(ch, "No such player.\r\n");
  else if (id == GET_IDNUM(ch))
    send_to_char(ch, "It's your house!\r\n");
  else {
    for (j = 0; j < house_control[i].num_of_guests; j++)
      if (house_control[i].guests[j] == id) {
	for (; j < house_control[i].num_of_guests; j++)
	  house_control[i].guests[j] = house_control[i].guests[j + 1];
	house_control[i].num_of_guests--;
	House_save_control();
	send_to_char(ch, "Guest deleted.\r\n");
	return;
      }
    if (house_control[i].num_of_guests == MAX_GUESTS) {
      send_to_char(ch, "You have too many guests.\r\n");
      return;
    }
    j = house_control[i].num_of_guests++;
    house_control[i].guests[j] = id;
    House_save_control();
    send_to_char(ch, "Guest added.\r\n");
  }
}



/* Misc. administrative functions */


/* crash-save all the houses */
void House_save_all(void)
{
  int i;
  roomData_t *house = NULL;

  for (i = 0; i < num_of_houses; i++)
    if ((house = roomData_findInZone(zoneData_find(house_control[i].hZoneKey), house_control[i].vnum)) != NULL)
      if (ROOM_FLAGGED(house, ROOM_HOUSE_CRASH))
	House_crashsave(house);
}


/* note: arg passed must be house vnum, so there. */
int House_can_enter(charData_t *ch, roomData_t *house)
{
  int i, j;

  if (GET_AUTH(ch) >= AUTH_WIZARD || (i = find_house(house)) == -1)
    return (1);

  switch (house_control[i].mode) {
  case HOUSE_PRIVATE:
    if (GET_IDNUM(ch) == house_control[i].owner)
      return (1);
    for (j = 0; j < house_control[i].num_of_guests; j++)
      if (GET_IDNUM(ch) == house_control[i].guests[j])
	return (1);
  }

  return (0);
}

void House_list_guests(charData_t *ch, int i, int quiet)
{
  int j, num_printed;
  char *temp;

  if (house_control[i].num_of_guests == 0) {
    if (!quiet)
      send_to_char(ch, "  Guests: None\r\n");
    return;
  }

  send_to_char(ch, "  Guests: ");

  for (num_printed = j = 0; j < house_control[i].num_of_guests; j++) {
    /* Avoid <UNDEF>. -gg 6/21/98 */
    if ((temp = get_name_by_id(house_control[i].guests[j])) == NULL)
      continue;

    num_printed++;
    send_to_char(ch, "%c%s ", UPPER(*temp), temp + 1);
  }

  if (num_printed == 0)
    send_to_char(ch, "all dead");

  send_to_char(ch, "\r\n");
}