cabal.c

FlCodebase3.1/src/cabal.c · 224.8 KB

From the archive FlCodebase3.1.tgz

Download the archive   « Back to the archive contents

/***************************************************************************
 *  Original Diku Mud copyright (C) 1990, 1991 by Sebastian Hammer,        *
 *  Michael Seifert, Hans Henrik St{rfeldt, Tom Madsen, and Katja Nyboe.   *
 *                                                                         *
 *  Merc Diku Mud improvments copyright (C) 1992, 1993 by Michael          *
 *  Chastain, Michael Quan, and Mitchell Tse.                              *
 *                                                                         *
 *  In order to use any part of this Envy Diku Mud, you must comply with   *
 *  the original Diku license in 'license.doc', the Merc license in        *
 *  'license.txt', as well as the Envy license in 'license.nvy'.           *
 *  In particular, you may not remove either of these copyright notices.   *
 *                                                                         *
 *  Much time and thought has gone into this software and you are          *
 *  benefitting.  We hope that you share your changes too.  What goes      *
 *  around, comes around.                                                  * 
 *                                                                         *
 *      ROM 2.4 is copyright 1993-1998 Russ Taylor                         *
 *      ROM has been brought to you by the ROM consortium                  *
 *          Russ Taylor (rtaylor@hypercube.org)                            *
 *          Gabrielle Taylor (gtaylor@hypercube.org)                       *
 *          Brian Moore (zump@rom.org)                                     *
 *      By using this code, you have agreed to follow the terms of the     *
 *      ROM license, in the file Rom24/doc/rom.license                     *
 *                                                                         *
 * Code Adapted and Improved by Abandoned Realms Mud                       *
 * and Aabahran: The Forsaken Lands Mud by Virigoth                        *
 *                                                                         *
 * Continued Production of this code is available at www.flcodebase.com    *
 ***************************************************************************/

/* NOTE: If you are looking for old cabal.c its in cabal_old.c */
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <malloc.h>
#include "merc.h"
#include "recycle.h"
#include "cabal.h"
#include "vote.h"
#include "olc.h"
#include "db.h"
#include "save_mud.h"
#include "bounty.h"
#include "armies.h"
#include "clan.h"

/***************************************************************/
/*Following are cabal orientanted routines for Forsaken Lands  */
/*mud created by Virigoth circa 2001.  Copyrighted for Forsaken*/
/*Lands mud Nov 1, 2001.  Do not use or copy without explicit */
/*permisson of author. (Voytek Plawny aka Virigoth)	       */
/***************************************************************/

extern bool format_room_name(ROOM_INDEX_DATA* room, char* room_name);
void do_cstat( CHAR_DATA* ch, char* argument );

/* list of all cabals created */
CABAL_DATA* cabal_list = NULL;
CABAL_INDEX_DATA *	cabal_index_list = NULL;

/* global index for pacts */
PACT_DATA* pact_list = NULL;
int	max_cabal_index = 0;
int	max_cabal = 0;

/* global array with letters that are universal for each rank */
char*	const	rank_letters [RANK_MAX] = 
/* [I]nitiate, [M]ember, [V]eteran, [T]rusted, [E]lder, [L]eader */
{  "I",		"M",	  "V",	     "T",	"E",	 "L"};


/* returns support cost of croom */
int get_croom_cost( int level ){
  return (croom_table[level].cost * CABAL_FULL_SUPPORT * WORLD_SUPPORT_VALUE / 10000);
}

/* returns cp cost of croom to build*/
int get_croom_bcost( int level ){
  int cost = croom_table[level].cost * CABAL_FULL_SUPPORT * WORLD_SUPPORT_VALUE / 3000;
  if (IS_SET(croom_table[level].flags, CABAL_COFFER) && cost > 500)
    cost = 500;
  return (cost);
}

/* shows cabal's rooms and their status to a person */
void show_cabal_rooms(CHAR_DATA* ch, CABAL_DATA* pCabal ){
  CABAL_DATA* pCab = get_parent( pCabal );
  /*
    CVROOM_DATA* pcv;
    int croom_active[CROOM_LEVELS];
    int croom_unactive[CROOM_LEVELS];
    int i, total_cost = 0, tot_rooms = 0;
  */
  bool fCapture = FALSE;

  /* NOT NEEDED
  for (i = 0; i < CROOM_LEVELS; i++){
    croom_active[i] = croom_unactive[i] = 0;
  }

  //run through the rooms and add up active and inactive rooms
  for (pcv = pCab->cvrooms; pcv; pcv = pcv->next){
    if (!pcv->fComplete)
      continue;

    //active
    if (croom_table[pcv->level].flags == 0 || IS_CABAL(pCab, croom_table[pcv->level].flags)){
      croom_active[pcv->level] += 1;
      total_cost += get_croom_cost( pcv->level );
    }
    else
      croom_unactive[pcv->level] += 1;
    tot_rooms++;
  }   

  send_to_char("Active	Sup	Type	      Pow   Name\n\r", ch );
  send_to_char("---------------------------------------------\n\r", ch);
  for (i = 0; i < CROOM_LEVELS; i++){
    char* pow;

    if (IS_SET(croom_table[i].flags, CABAL_ALLOW_2))
      pow = "Vet";
    else if (IS_SET(croom_table[i].flags, CABAL_ALLOW_3))
      pow = "Tru";
    else if (IS_SET(croom_table[i].flags, CABAL_ALLOW_4))
      pow = "Eld";
    else if (IS_SET(croom_table[i].flags, CABAL_ALLOW_5))
      pow = "Lea";
    else
      pow = "   ";

    if (croom_active[i] || croom_unactive[i]){
      sendf(ch, "%2d/%-2d  %-5d %-15.15s  %s %-35.35s\n\r",
	    croom_active[i],
	    croom_active[i] + croom_unactive[i],
	    UMAX(1, croom_active[i]) * get_croom_cost( i ),
	    croom_table[i].name,
	    pow,
	    pCab->pIndexData->crooms[i]->pRoomIndex->name);
    }
    else{
      sendf(ch, " ---   %-5d %-15.15s  %s %-35.35s\n\r",
	    get_croom_cost( i ),
	    croom_table[i].name,
	    pow,
	    pCab->pIndexData->crooms[i]->pRoomIndex->name);
    }
  }
  sendf(ch, "Total of %d rooms costing %d support.\n\r",
	tot_rooms, total_cost);
  */

  if (is_captured( pCab))
    fCapture = TRUE;
  sendf(ch, "Active Powers: %s%s%s%s\n\r",
	IS_CABAL(pCab, CABAL_ALLOW_2) && !fCapture ? "Veteran " : "",
	IS_CABAL(pCab, CABAL_ALLOW_3) && !fCapture ?  "Trusted " : "",
	IS_CABAL(pCab, CABAL_ALLOW_4 )&& !fCapture ?  "Elder " : "",
	IS_CABAL(pCab, CABAL_ALLOW_5) && !fCapture ?  "Leader" : ""
	);
  if (fCapture)
    send_to_char("Your item has been CAPTURED!\n\r", ch);
}

  
/* shows cabal info to a single member */
void show_cabal_info( CHAR_DATA* ch, CABAL_DATA* pCabal ){
  CABAL_DATA* pCab = get_parent( pCabal );
  CMEMBER_DATA* cMem;

  char buf[MSL];
  int cnt = 0;
  bool fEld = FALSE;

/* leader/imm check */
  if (IS_IMMORTAL(ch) && (!str_cmp(ch->name, pCab->immortal) || !str_cmp(ch->name, pCab->immortal)))
    fEld = TRUE;
  else if (IS_ELDER( ch ) && ch->pCabal && get_parent(ch->pCabal) == pCab)
    fEld = TRUE;
  else if (get_trust(ch) > CREATOR)
    fEld = TRUE;

  /* NOT NEEDED
  health_prompt( buf, UMAX(0, ECONOMY_UPDATE - (mud_data.current_time - pCab->room_stamp)), ECONOMY_UPDATE, FALSE );  
  */
  sendf( ch, "[%s]  (%d %ss at %d%%)\n\r", /* RENT: %s\n\r", */
	 pCab->who_name, 
	 GET_CAB_CP( pCab),
	 pCab->currency,
	 pCab->cp_tax);
	 //	 buf);

  sendf( ch, "Cabal Immortal: %s\n\r", pCab->immortal);
  if (IS_CABAL(pCab, CABAL_RAID)){
    if (mud_data.current_time - pCab->raid_stamp < CAB_RAID_WAIT){
      int hour = (CAB_RAID_WAIT + pCab->raid_stamp - mud_data.current_time) / 30;
      int days = hour / MAX_HOURS;
      sendf(ch, "Next Raid: %d %s%s.\n\r", 
	    days > 1 ? days : hour,
	    days > 1 ? "day" : "hour",
	    (days  == 1 || hour == 1) ? "" : "s");
    }
    else
      send_to_char("Next Raid: READY\n\r", ch);
  }
  send_to_char("\n\r", ch);

  sendf( ch, "ECONOMY: Influence   %ss\n\r", capitalize(pCab->currency));
  sendf( ch, " Initial:   %-4d%%     %-4ld\n\r", 100 * pCab->support[ECO_INCOME] / CABAL_FULL_SUPPORT_VAL, pCab->cp_gain[ECO_INCOME] / CPTS);
  sendf( ch, " Bonus  :   %-4d%%        \n\r", 100 * pCab->support[ECO_LAST_BONUS] / CABAL_FULL_SUPPORT_VAL);
  sendf( ch, " Trade  :   %-4d      %-4ld\n\r", pCab->support[ECO_TRADE], pCab->cp_gain[ECO_TRADE] / CPTS);
  if (pCab->support[ECO_LAST_ARMY])
  sendf( ch, " Armies :   %-4d%%\n\r", 100 * pCab->support[ECO_LAST_ARMY] / CABAL_FULL_SUPPORT_VAL);
  //  sendf( ch, " Rooms  :  -%-4d\n\r", pCab->support[ECO_LAST_RENT]);
  //  sendf( ch, " Corru  :  -%-4d\n\r", pCab->support[ECO_LAST_CORR]);
  sendf( ch, "        -------------------\n\r");
  sendf( ch, " Gain   :   %-4d%%     %-4ld\n\r\n\r", 100 * pCab->support[ECO_GAIN] / CABAL_FULL_SUPPORT_VAL, pCab->cp_gain[ECO_GAIN] / CPTS);

  show_cabal_rooms(ch, pCab );

  if (fEld){
    sprintf( buf, "\n\rMembers: [%d/%d] Elders: [%d/%d]  Leaders [%d/%d]\n\r", 
	     pCab->cur_member, pCab->max_member,
	     pCab->cur_elder,  pCab->max_elder,
	     pCab->cur_leader, pCab->max_leader);
    send_to_char( buf, ch );

    /* show members that you can see */
    for ( cnt = 0, cMem = pCab->members; cMem; cMem = cMem->next ){
      if (cMem->level > 50 && !IS_IMMORTAL(ch))
	continue;
      if ( cnt == 0 ){
	send_to_char( "Number     Name       Level   Rank   Days Ago  Cabal\n\r", ch );
	send_to_char( "------ ------------- ------- ------- --------- ------------\n\r", ch );
      }
      sprintf( buf, "[%4d] %-15.15s %-6d   %-5d %-5ld     %-10.10s``\n\r", 
	       cnt, 
	       cMem->name,
	       cMem->level,
	       cMem->rank,
	       (mud_data.current_time - cMem->last_login) / VOTE_DAY,
	       cMem->pCabal->who_name);
      send_to_char( buf, ch );
      cnt++;
    }
  }
  send_to_char("\n\r", ch );

  /*
  sendf( ch, "ROOMS: %d/%d possible costing %d support.\n\r",
	 pCab->cur_room, 
	 pCab->pIndexData->max_room, 
	 pCab->support[ECO_LAST_RENT] );
  */

  if (IS_CABAL(pCab, CABAL_TRADE) || IS_CABAL(pCab, CABAL_NAPACT) 
      || IS_CABAL(pCab, CABAL_ALLIANCE) || IS_CABAL(pCab, CABAL_VENDETTA)){
    send_to_char("PACTS:\n\r", ch );
    show_pacts( ch, pCab );
  }
}

/* shows crooms cabal has */
void show_crooms( CHAR_DATA* ch, CROOM_DATA** crooms ){
  CROOM_DATA* croom;
  int i;
  int count = 0;
  char buf[MSL];

  send_to_char("  Room Type    Sup  Cps   Description\n\r", ch);
  send_to_char("------------------------------------------\n\r", ch );
  for (i = 0; i < CROOM_LEVELS; i++){
    if (crooms[i] != NULL)
      count++;
    croom = crooms[i];

    if (croom){
      sprintf( buf, "%2d. %-10.10s %-4d %-5d %s\n\r",
	       count,
	       croom_table[i].name,
	       get_croom_cost( i ),
	       get_croom_bcost( i ),
	       croom_table[i].desc);
    }
    else{
      sprintf( buf, "--. %-10.10s %-4d %-5d %s\n\r",
	       croom_table[i].name,
	       get_croom_cost( i ),
	       get_croom_bcost( i ),
	       croom_table[i].desc);
    }
    send_to_char( buf, ch );
  }
}

/* creates a new cabal index */
CABAL_DATA* new_cabal(){
  CABAL_DATA *pc = malloc( sizeof( *pc ));
  memset( pc, 0, sizeof( CABAL_DATA ));

  return pc;
}

/* frees existing cabal data */
void free_cabal( CABAL_DATA* pc ){
  CABAL_DATA* ca = pc;
  CVROOM_DATA* cVroom = ca->cvrooms;
  CMEMBER_DATA* cMem = ca->members;

  while ( ca->members ){
    cMem = ca->members;
    ca->members = ca->members->next;

    free_cmember( cMem );
  }

  while ( ca->cvrooms ){
    cVroom = ca->cvrooms;
    ca->cvrooms = ca->cvrooms->next;

    free_cvroom( cVroom );
  }
  
  if (!IS_NULLSTR(pc->name))
    free_string(pc->name );
  if (!IS_NULLSTR(pc->currency))
    free_string(pc->currency);
  if (!IS_NULLSTR(pc->who_name))
    free_string(pc->who_name);
  if (!IS_NULLSTR(pc->immortal))
    free_string(pc->immortal);

  free( pc );
}

/* adds a cabal onto the list of all cabals */
void add_cabal( CABAL_DATA *pc ){
  pc->next = cabal_list;
  cabal_list = pc;
  max_cabal ++;
}

/* removes a cabal from the list */
void rem_cabal( CABAL_DATA *pc ){
  CABAL_DATA* prev = cabal_list;
  
  if (prev == NULL){
   bug("cabal.c>rem_cabal : Cannot remove cabal as non exist.", 0);
   return;
  }
  else if (prev == pc ){
    cabal_list = NULL;
    max_cabal--;
    return;
  }

  for (; prev->next; prev = prev->next){
    if (prev->next == pc){
      prev->next = pc->next;
      max_cabal--;
      return;
    }
  }
  bug("cabal.c>rem_cabal : Cannot remove cabal as non exist.", 0);
  return;
}  

/* creates a cabal based on passed index */
CABAL_DATA* create_cabal( CABAL_INDEX_DATA* pIndex ){
  int i=0;
  CABAL_DATA* pc;

  if (pIndex == NULL)
    return NULL;

  //get blank cabal data
  pc = new_cabal();

  pc->pIndexData		= pIndex;
  pc->vnum			= pIndex->vnum;
  pc->version			= pIndex->version; 

  if (pIndex->anchor_vnum){
    pc->anchor			= get_room_index( pIndex->anchor_vnum);
    if (pc->anchor == NULL)
      bug("cabal.c>create_cabal: could not load anchor vnum %d.", pIndex->anchor_vnum);
  }
  if (!IS_NULLSTR(pIndex->city_name)){
    pc->city			= get_area_data_str( pIndex->city_name );
    if (pc->city == NULL)
      nlogf("cabal.c>create_cabal: could not load city %s.", pIndex->city_name);
  }
  if (!IS_NULLSTR(pIndex->clan)){
    pc->clan			= GetClanHandle( pIndex->clan );
    if (pc->clan < 1)
      nlogf("cabal.c>create_cabal: could not load clan %s.", pIndex->clan);
  }
  if (pIndex->guard_vnum){
    pc->guard			= get_mob_index( pIndex->guard_vnum );
    if (pc->guard == NULL)
      bug("cabal.c>create_cabal: could not load guard vnum %d.", pIndex->guard_vnum);
  }
  if (pIndex->pit_vnum){
    pc->pitroom			= get_room_index( pIndex->pit_vnum );
    if (pc->pitroom == NULL)
      bug("cabal.c>create_cabal: could not load pitroom vnum %d.", pIndex->pit_vnum);
  }
  pc->name			= str_dup( pIndex->name );
  pc->currency			= str_dup( pIndex->currency );
  pc->who_name			= str_dup( pIndex->who_name );
  pc->immortal			= str_dup( pIndex->immortal );

  pc->max_member		= pIndex->max_member;

  for (i = 0; i < MAX_CLASS; i ++){
    pc->class[i] = pIndex->class[i];
  }
  for (i = 0; i < MAX_PC_RACE; i ++){
    pc->race[i] = pIndex->race[i];
  }
  pc->fAvatar			= pIndex->fAvatar;
  pc->align			= pIndex->align;
  pc->ethos			= pIndex->ethos;
  pc->progress			= pIndex->progress;
  pc->flags			= pIndex->flags;
  pc->flags2			= pIndex->flags2;

  /* reset the report queue */
  init_repq(&pc->report_q);

  //add cabal to list of all cabals
  add_cabal( pc );
  return pc;
}

/* create a new cabal room data */
CROOM_DATA *new_croom(void){

  CROOM_DATA *cr = malloc( sizeof( *cr ));
  
/* reset initial things */
  cr->pRoomIndex	= NULL;
  cr->level		= 0;

  return cr;
};

void free_croom( CROOM_DATA* cr ){
  free( cr );
}

/* create a new cabal virtual room data */
CVROOM_DATA *new_cvroom(void){

  CVROOM_DATA *cvr = malloc( sizeof( *cvr ));
  
/* reset initial things */
  cvr->pRoom		= NULL;
  cvr->next		= NULL;
  cvr->fComplete	= FALSE;
  cvr->vnum		= 0;

  cvr->level		= 0;
  cvr->pos[0] =   cvr->pos[1] =   cvr->pos[2] = 0;
  return cvr;
};

void free_cvroom( CVROOM_DATA* cvr ){
  if (cvr == NULL)
    return;
/* Viri: this seems to cause problems
  if (cvr->pRoom)
    free_room_index( cvr->pRoom );
*/
  free( cvr );
}

void set_cvroom_flags( CVROOM_DATA* pcv, CABAL_DATA* pc, bool fAdd ){
  if (!pcv->fComplete)
    return;
  if (fAdd){
    SET_BIT(pc->flags, croom_table[pcv->level].flags);
    if (IS_SET(croom_table[pcv->level].flags2, CROOM_PITROOM) 
	&& pc->pitroom == NULL){
      pc->pitroom = pcv->pRoom;
    }
  }
  else{
    /* remove the cabal flag */
    if (pcv->fComplete){
      REMOVE_BIT(pc->flags, croom_table[pcv->level].flags);
      if (IS_SET(croom_table[pcv->level].flags2, CROOM_PITROOM) && pc->pitroom != NULL){
	pc->pitroom = NULL;
      }
    }
  }
}


/* attatch a cvroom to a cabal */
void cvroom_to_cabal( CVROOM_DATA* pcv, CABAL_DATA* pc ){
  if (pcv == NULL ){
    bug("cvroom_to_cabal: null pcv passed.", 0);
    return;
  }
  else if (pc == NULL ){
    bug("cvroom_to_cabal: null pc passed.", 0);
    return;
  }
  else{
    pcv->next = pc->cvrooms;
    pc->cvrooms = pcv;
    if (pcv->pRoom)
      pcv->pRoom->pCabal = pc;
  }
  /* add the room flags */
  set_cvroom_flags( pcv, pc, TRUE);
}

void refresh_cvroom_flags( CABAL_DATA* pc ){
  CVROOM_DATA* pcv;

  for (pcv = pc->cvrooms; pcv; pcv = pcv->next ){
    set_cvroom_flags( pcv, pc, TRUE );
  }
}

/* remove a cvroom from cabal */
void cvroom_from_cabal( CVROOM_DATA* pcv, CABAL_DATA* pc ){
  CVROOM_DATA* prev;

  if (pcv == NULL ){
    bug("cvroom_from_cabal: null pcv passed.", 0);
    return;
  }
  else if (pc == NULL ){
    bug("cvroom_from_cabal: null pc passed.", 0);
    return;
  }
  else if (pc->cvrooms == NULL){
    bug("cvroom_from_cabal: cabal had no virtual rooms. cab.vnum: %d", pc->vnum );
    return;
  }
  else{
    prev = pc->cvrooms;
  }

  if (prev == pcv ){
    pc->cvrooms = prev->next;
  }
  else {
    while (prev->next && prev->next != pcv ){
      prev = prev->next;
    }
  }
  if (prev == NULL){
    bug("cvroom_from_cabal: room not found in cabal %d.", pc->vnum);
    return;
  }
  prev->next = pcv->next;
  pcv->next = NULL;
  
  set_cvroom_flags( pcv, pc, FALSE );
  refresh_cvroom_flags( pc );
}


/* save cabal's virtual rooms to file */
void save_cvrooms( CABAL_DATA* pCab ){
  FILE *fp;
  CVROOM_DATA* pcv;
  OBJ_DATA* pObj, *pObj_next;
  char path[MIL], old[MIL];

  fclose( fpReserve );
  sprintf( path, "%s%s", SAVECAB_DIR, TEMPCVR_FILE );
  sprintf( old, "%s%s.ovi", SAVECAB_DIR, capitalize(pCab->pIndexData->file_name) );
  if ( ( fp = fopen( path, "w" ) ) == NULL ){
    perror( path );
    fclose (fp);
    fpReserve = fopen( NULL_FILE, "r" );
    return;
  }

  /* start writting the vrooms to the file */
  for (pcv = pCab->cvrooms; pcv; pcv = pcv->next ){
    if (pcv->pRoom == NULL )
      continue;

    /* cvroom data first */
    fprintf( fp, "#VIRROOM\n%d %d %d\n%d %d\n\r",
	     pcv->pos[P_X], pcv->pos[P_Y], pcv->pos[P_Z],
	     pcv->vnum, pcv->fComplete);
    fprintf( fp, "%d\n", pcv->level );

    /* all the pcv data has been written, we now write the room */
    write_room( fp, pcv->pRoom );

    /* now write this room's resets, since we do not create area resets section. */
    write_resets( fp, pcv->pRoom );
    fprintf( fp, "S\n\n" );

    /* last thing to enter are objects if any */
    for (pObj = pcv->pRoom->contents; pObj; pObj = pObj_next ){
      pObj_next = pObj->next_content;
      
      if (!IS_OBJ_STAT(pObj, ITEM_MELT_DROP)
	  && pObj->in_room != NULL
	  && pObj->carried_by == NULL 
	  && (IS_LIMITED(pObj) 
	      || pObj->homevnum != pObj->in_room->vnum 
	      || pObj->contains != NULL
	      || CAN_WEAR(pObj, ITEM_HAS_OWNER)) ){
	fwrite_obj(NULL, pObj, fp, 0, 0, FALSE);
      }
    }//end for objects
  }//end for cvrooms

/* terminate the file */
  fprintf( fp, "#End\n" );
  fclose (fp);
/* copy current .vir to .ovi, and the temporary file to .vir */
  sprintf( path, "%s%s.vir", SAVECAB_DIR, capitalize(pCab->pIndexData->file_name) );
  rename( path, old );
  sprintf( old, "%s%s", SAVECAB_DIR, TEMPCVR_FILE );
  rename( old, path );
  fpReserve = fopen( NULL_FILE, "r" );
}

/* reads in resets for a single cabal virtual room */
void read_cvresets( FILE* fp, ROOM_INDEX_DATA* pRoomIndex ){
  RESET_DATA *pReset = NULL;
  
  for ( ; ; ){
    char letter = '\0';
      
    letter = fread_letter (fp);
    /* Viri: For some reason compiler thought letter could be uninitilised at this point. */
    if (&letter == NULL)
      letter = '\0';
      
    if (letter == 'S') {
      break;
    }
    else if (letter == '*' ) {
      fread_to_eol( fp );
      continue;
    }
    pReset		= new_reset_data();
    pReset->command	= letter;
    fread_number( fp );
    pReset->arg1	= fread_number( fp );
    pReset->arg2	= fread_number( fp );
    pReset->arg3	= (letter == 'G' || letter == 'R') ? 0 : fread_number( fp );
    pReset->arg4	= (letter == 'P' || letter == 'M' || letter == 'T') ? fread_number(fp) : 0;
    fread_to_eol( fp );

    switch ( letter ){
    default:
      bug( "Load_cvresets: bad command '%c'.", letter );
      exit( 1 );
      break;
    case 'M':
      new_reset( pRoomIndex, pReset );
      break;
    case 'O':
      new_reset( pRoomIndex, pReset );
    case 'P':
      new_reset( pRoomIndex, pReset );
      break;
    case 'G':
    case 'E':
      new_reset( pRoomIndex, pReset );
      break;
    case 'R':
      if ( pReset->arg2 < 0 || pReset->arg2 > MAX_DIR ){
	bug( "Load_cvresets: 'R': bad exit %d.", pReset->arg2 );
	exit( 1 );
      }
      new_reset( pRoomIndex, pReset );
      break;
    case 'T':
      new_reset( pRoomIndex, pReset );
      break;
    }
  }
}
/* read a single cabal vroom from file */
bool read_cvroom( FILE* fp, CVROOM_DATA* pcv ){
  
  pcv->pos[P_X]		= fread_number( fp );
  pcv->pos[P_Y]		= fread_number( fp );
  pcv->pos[P_Z]		= fread_number( fp );

  pcv->vnum		= fread_number( fp );
  pcv->fComplete	= fread_number( fp );
  pcv->level		= fread_number( fp );

  /* get the room data */
  pcv->pRoom = new_room_index();
  read_room( fp, pcv->pRoom, NULL, 0 );

  /* get the reset data */
  read_cvresets( fp, pcv->pRoom );


  return TRUE;
}

/* save a single cabal to a file */
void save_cabal( CABAL_DATA* pCab){
  CMEMBER_DATA* cMem;

  FILE *fp;
  char path[MIL];

  char buf[MIL];
  int i = 0, flag = 0;
  fclose( fpReserve );

  sprintf( path, "%s%s", SAVECAB_DIR, TEMPCAB_FILE );
  if ( ( fp = fopen( path, "w" ) ) == NULL ){
    perror( path );
    fclose (fp);
    fpReserve = fopen( NULL_FILE, "r" );
    return;
  }

// economy info
  fprintf( fp, "Cp %ld ", pCab->cp);
  fprintf( fp, "CpTax %d\n", pCab->cp_tax);
  fprintf( fp, "BonSup %ld ", pCab->bonus_sup);
  fprintf( fp, "Time %ld\n", pCab->time_stamp);
  fprintf( fp, "RTime %ld\n",pCab->room_stamp);
  fprintf( fp, "MTime %ld\n",pCab->member_stamp);
  fprintf( fp, "Armies %d\n",pCab->armies_ready);
  fprintf( fp, "PresentTime %ld\n",pCab->present_stamp);

//  fprintf( fp, "RaTime %ld\n",pCab->raid_stamp);
//  fprintf( fp, "InCp %ld\n", pCab->cp_gain[ECO_INCOME]);
//  fprintf( fp, "InSup %d\n", pCab->support[ECO_INCOME]);

  fprintf( fp, "CurMem %d\n", pCab->cur_member);

  // Before writting the flags, we remove any flags in cabal_flag table that cannot be manualy set. 
  flag = pCab->flags;
  for (i = 0; cabal_flags[i].name; i ++){
    if (cabal_flags[i].settable == FALSE)
      flag &= ~cabal_flags[i].bit;
  }

//  fprintf( fp, "Flags %s\n", fwrite_flag( flag, buf ) );
//  fprintf( fp, "Flags2 %s\n", fwrite_flag( pCab->flags2, buf ) );

  for( cMem = pCab->members; cMem; cMem = cMem->next){
    fprintf( fp, "Member %d %s~ %s~ %s~\n", cMem->pCabal->vnum, cMem->name, cMem->sponsor, cMem->sponsored);
    fprintf( fp, "%d %d %d %d %d %ld %ld\n", cMem->class, cMem->race, cMem->level, cMem->rank, cMem->kills, cMem->cp, cMem->cp_pen);
    fprintf( fp, "%d %d %d %ld %ld %ld\n", cMem->month, cMem->mhours, cMem->hours, cMem->last_update, cMem->time_stamp, cMem->last_login);
    fprintf( fp, "%d ", MAX_TRAIN_QUEUE );
    for (i = 0; i < MAX_TRAIN_QUEUE; i++){
      fprintf( fp, "%d ", cMem->armies[i] );
    }
    fprintf( fp, "\n" );
  }

  fprintf( fp, "End\n#0\n");
  fclose (fp);
  fpReserve = fopen( NULL_FILE, "r" );

//copy the cabal.cab to cabal.old and cabal.tmp to cabal.cab
  sprintf( buf, "%s%s.cab", SAVECAB_DIR, capitalize(pCab->pIndexData->file_name) );
  rename( path, buf);
}

/* save all cabals to a file */
void save_cabals(bool fChanged, CHAR_DATA* ch){
  CABAL_DATA* pCab = cabal_list;

/*  loop through each cabal and save it to its own file */
  for(pCab = cabal_list; pCab; pCab = pCab->next){
    if (fChanged){
      bool fSaved = FALSE;
      if (IS_CABAL(pCab, CABAL_CHANGED)){
	save_cabal( pCab );
	fSaved = TRUE;
      }
      if (IS_CABAL(pCab, CABAL_VRCHANGED)){
	save_cvrooms( pCab );
	fSaved = TRUE;
      }
    }
    else{
      save_cabal( pCab );
      save_cvrooms( pCab );
    }
    REMOVE_BIT(pCab->flags, CABAL_CHANGED);
    REMOVE_BIT(pCab->flags, CABAL_VRCHANGED);
  }
  save_pacts( );
  if (ch)
    send_to_char("Pacts saved.\n\r", ch);
}


/* reads a single cabal from file into a structure */
bool fread_cabal( FILE *fp, CABAL_DATA* pCab ){
  char *word = NULL;
  bool fMatch = FALSE;

  for ( ; ; ){
    word   = feof( fp ) ? "End" : fread_word( fp );
    fMatch = FALSE;

    switch ( UPPER(word[0]) ){
    case '*':
      fMatch = TRUE;
      fread_to_eol( fp );
      break;
    case 'A':
      KEY( "Armies",		pCab->armies_ready,	fread_number( fp ) );
      break;
    case 'B':
      KEY( "BonSup",		pCab->bonus_sup,	fread_number(fp)   );
    case 'C':
      KEY( "Cp",		pCab->cp,		fread_number(fp)   );
      KEY( "CpTax",		pCab->cp_tax,		fread_number(fp)   );
      KEY( "CurMem",		pCab->cur_member,	fread_number( fp ) );
      break;
    case 'E':
      if ( !str_cmp( word, "End" ) ){
	return TRUE;
      }
      break;
    case 'F':
      KEY( "Flags",		pCab->flags,		fread_flag(fp)	   );
      KEY( "Flags2",		pCab->flags2,		fread_flag(fp)	   );
      break;
    case 'G':
      break;
    case 'I':
      KEY( "InCp",		pCab->cp_gain[ECO_INCOME],fread_number( fp ) );
      KEY( "InSup",		pCab->support[ECO_INCOME],fread_number( fp ) );
      break;
    case 'M':
      if (!str_cmp( word, "Member" )){
	CMEMBER_DATA* cMem = new_cmember();
	int max, i;

	free_string( cMem->name);
	free_string( cMem->sponsor);
	free_string( cMem->sponsored);

	cMem->vnum      = fread_number( fp );
	cMem->name	= fread_string( fp );
	cMem->sponsor	= fread_string( fp );
	cMem->sponsored	= fread_string( fp );

	cMem->class	= fread_number( fp );
	cMem->race	= fread_number( fp );
	cMem->level	= fread_number( fp );
	cMem->rank	= fread_number( fp );
	cMem->kills	= fread_number( fp );
	cMem->cp	= fread_number( fp );
	cMem->cp_pen	= fread_number( fp );

	cMem->month	= fread_number( fp );
	cMem->mhours	= fread_number( fp );
	cMem->hours	= fread_number( fp );
	cMem->last_update= fread_number( fp );
	cMem->time_stamp= fread_number( fp );
	cMem->last_login= fread_number( fp );

	//army queues
	max		= fread_number( fp );
	max		= UMIN( MAX_TRAIN_QUEUE, max );
	for (i = 0; i < max; i++){
	  cMem->armies[i]	= fread_number( fp );
	}
	cmember_to_cabal( cMem, pCab );

	fMatch = TRUE;
	break;
      }
      KEY( "MTime",		pCab->member_stamp,	fread_number( fp ) );
      break;
    case 'N':
      break;
    case 'O':
      break;
    case 'R':
      KEY( "RTime",		pCab->room_stamp,	fread_number( fp ) );
      KEY( "RaTime",		pCab->raid_stamp,	fread_number( fp ) );
      break;
    case 'P':
      KEY( "PresentTime",	pCab->present_stamp,	fread_number( fp ) );
      break;
    case 'S':
      break;
    case 'T':
      KEY( "Time", pCab->time_stamp, fread_number( fp ) );
      break;
    case 'V':
      break;
    case 'W':
      break;
    }
  // cheack for key match 
    if ( !fMatch ){
      nlogf("Fread_cabal: no match: %s", word);
      fread_to_eol(fp);
    }
  }
  return TRUE;

}


  
/* udpates some odd info after loading all cabals */
void fix_cabals(){
  CABAL_INDEX_DATA* pci;
  CABAL_DATA* pCab = cabal_list;
  CMEMBER_DATA* cMem = pCab->members;
  CVROOM_DATA* pcv = pCab->cvrooms;
  CVROOM_DATA* pcv_next;
  int i;

/* FIX CABAL INDEXES */
  for (pci = cabal_index_list ;pci != NULL; pci = pci->next){
    if (pci->parent_vnum){
      continue;
    }
    /* get room pointers for crooms */
    for (i = 0; i < CROOM_LEVELS; i++){
      if (pci->crooms[i] == NULL){
	/* create default dummy rooms if they do not exist */
	if (i < 2){
	  pci->crooms[i] = new_croom();
	  pci->crooms[i]->level = i;
	}
	else
	  continue;
      }
      /* default level 0 and 1 rooms */
      if (i == 0)
	pci->crooms[i]->vnum	= ROOM_VNUM_CROOM_DUMMY;
      else if (i == 1)
	pci->crooms[i]->vnum	= ROOM_VNUM_CROOM_GARDEN;
      
      if ( pci->crooms[i]->vnum < 1){
	nlogf("cabal.c>fix_cabals:croom with 0 room vnum in cabal %d level %d",
	      pci->vnum,
	      i);
	continue;
      }
      else if ( (pci->crooms[i]->pRoomIndex = get_room_index(pci->crooms[i]->vnum)) == NULL){
	nlogf("cabal.c>fix_cabals: could not find room vnum %d for croom level %d in cabal %d.",
	      pci->crooms[i]->vnum,
	      i,
	      pci->vnum
	      );
	continue;
      }
    }
  }
/* FIX CABALS */
  for (pCab = cabal_list ;pCab != NULL; pCab = pCab->next){

    if (pCab->pIndexData->parent_vnum){
      pCab->parent = get_cabal_vnum( pCab->pIndexData->parent_vnum);
      if (pCab->parent == NULL)
	bug("cabal.c>fix_cabals: could not load parent vnum %d.", pCab->pIndexData->parent_vnum);
    }
    if (pCab->parent){
      SET_BIT(pCab->flags, CABAL_SUBCABAL);
    }
    else
      REMOVE_BIT(pCab->flags, CABAL_SUBCABAL);

    //enemy and enemy vendettas
    if (!IS_NULLSTR(pCab->pIndexData->enemy_name)){
      CABAL_DATA* enemy = get_cabal( pCab->pIndexData->enemy_name );

      if (enemy == NULL){
	bug("cabal.c>fix_cabals: could not load enemy for cabal %d", pCab->pIndexData->vnum );
      }
      else{
	PACT_DATA* pact;
	pCab->enemy = enemy;
	/* check for vendetta between these two */
	if ( pCab->parent == NULL){
	  if ( (pact = get_pact(pCab, enemy, PACT_VENDETTA, FALSE)) == NULL){
	    if ( (pact = create_pact( pCab, enemy, PACT_VENDETTA )) != NULL){
	      pact->complete = PACT_COMPLETE;
	      add_pact(pact);
	    }
	  }
	  else if (pact->complete != PACT_COMPLETE){
	    pact->complete = PACT_COMPLETE;
	  }
	}  
      }
    }
    
    // adjust the max elder / leader
    pCab->max_elder = 1 + pCab->cur_member / 5;
    pCab->max_leader = 2 + pCab->cur_member / 25;

// cabal member data 
    for (cMem = pCab->members; cMem; cMem = cMem->next){
      // cabal 
      if (cMem->vnum){
	CABAL_DATA* pCabal = get_cabal_vnum( cMem->vnum);
	if (pCabal == NULL){
	  bug("fix_cabals (member): could not find cabal vnum: %d.", cMem->vnum);
	  continue;
	}
	else{
	  cMem->pCabal = pCabal;
	}
      }
    }//END for members

//CITY pointer for ROYALS 
    if (IS_CABAL(pCab, CABAL_ROYAL) && pCab->city == NULL){
      bug("fix_cabal: ROYAL cabal with no city! (vnum %d)", pCab->vnum );
    }
    
// ANCHOR room 
    if (pCab->anchor == NULL){
      if (pCab->pIndexData->parent_vnum == 0) 
	bug("fix_cabals: Cabal vnum %d has NULL anchor.", pCab->vnum );
    }
    else if (pCab->parent == NULL){
      area_to_cabal( pCab->anchor->area, pCab, TRUE );
      pCab->anchor->area->bastion_max = 0;
      pCab->anchor->area->age = 31;//force a repop right away
      if (IS_CABAL(pCab, CABAL_JUSTICE))
	SET_BIT(pCab->anchor->area->area_flags, AREA_LAWFUL);
      SET_BIT(pCab->anchor->area->area_flags, AREA_CABAL);
    }

// GUARD 
    if (pCab->guard && pCab->anchor == NULL){
      bug("fix_cabals: WARNING: Cabal guard set with no anchor room in cabal vnum %d.", pCab->vnum );
    }
   
// VCROOM DATA
    if (pCab->cvrooms && pCab->anchor == NULL){
      bug("fix_cabals: Cabal with virtual rooms and no anchor detected! vnum %d", pCab->vnum);
      continue;
    }
    else{
      RESET_DATA* pReset;
      MOB_INDEX_DATA* temp_index_mob;
      OBJ_INDEX_DATA* temp_index;
      
      for (pcv = pCab->cvrooms; pcv; pcv = pcv->next ){
	// we update all vrooms with area
	if (pcv->pRoom == NULL){
	  bug("fix_cabals: vcroom with no room in cabal %d", pCab->vnum );
	  continue;
	}
	pcv->pRoom->area = pCab->anchor->area;
	pcv->pRoom->pCabal = pCab;

	// we only load the room/update resets if this is a complete room
	if (!pcv->fComplete){
	  continue;
	}

	//Load the actual room, this will take care of its vnum and resets
	load_vir_room( pcv->pRoom, pCab->anchor->area );

	// fix its flags now
	set_cvroom_flags( pcv, pCab, TRUE );

	// check over the resets
	for (pReset = pcv->pRoom->reset_first; pReset; pReset = pReset->next){
	  switch ( pReset->command ){
	  default:
	    bug( "fix_cabals: bad command '%c'.", pReset->command );
	    continue;
	    break;
	  case 'M':
	    temp_index_mob = get_mob_index  ( pReset->arg1 );
	    break;
	  case 'O':
	    temp_index = get_obj_index ( pReset->arg1 );
	    temp_index->reset_num++;
	  case 'P':
	    temp_index = get_obj_index ( pReset->arg1 );
	    temp_index->reset_num++;
	    break;
	  case 'G':
	  case 'E':
	    temp_index = get_obj_index  ( pReset->arg1 );
	    temp_index->reset_num++;
	    break;
	  case 'R':
	    if ( pReset->arg2 < 0 || pReset->arg2 > MAX_DIR ){
	      bug( "fix_cabals: 'R': bad exit %d.", pReset->arg2 );
	      continue;
	    }
	    break;
	  case 'T':
	    break;
	  }
	}
      }//end for pcvrooms

      // correct exits for rooms leading to other cvrooms
      for (pcv = pCab->cvrooms; pcv; pcv = pcv_next ){
	pcv_next = pcv->next;
	// we update all vrooms with area
	if (pcv->pRoom == NULL){
	  continue;
	}
	fix_vir_exits( pcv );
      }
    }
  }
}

void refresh_cabal_support(){
  CABAL_DATA* pCab; 
 /* refresh support */
  for (pCab = cabal_list ;pCab != NULL; pCab = pCab->next){
    reinforce_refresh( pCab );
  }

}
/* loads cabal's virtual rooms */
void read_cvrooms( FILE* fp, CABAL_DATA* pCabal ){
  char letter;
  char* word;
  CVROOM_DATA* last_pcv = NULL;
  
  /* begin reading the cabal cvrooms */
  for (;;){
    letter		= fread_letter( fp );
    if ( letter != '#' ){
      bug( "read_cvrooms: # not found.", 0 );
      exit( 1 );
    }
    word   = feof( fp ) ? "End" : fread_word( fp );
    if ( !str_cmp(word, "End"))
      break;
    else if (!str_cmp(word, "O") || !str_cmp(word, "OBJECT")){
      if (last_pcv == NULL){
	bug("read_cvrooms: #O or #OBJECT encontered without previous cvroom.", 0);
	exit( 1 );
      }
      else{
	fread_obj( NULL, fp, last_pcv->pRoom );
	continue;
      }
    }
    else if (!str_cmp(word, "VIRROOM")){
      CVROOM_DATA* pcv = new_cvroom();
      /* read the cabal virtual room data */
      if (read_cvroom( fp, pcv )){
	last_pcv = pcv;
	/* hook up into the cabal */
	pcv->next = pCabal->cvrooms;
	pCabal->cvrooms = pcv;
      }
      /* the rooms are actualy loaded in, and reset in fix_cabals */
    }
    else{
      bug("read_cvrooms: Unknown token when reading for cabal %d", pCabal->vnum );
      exit( 1 );
    }
  }
}

/* loads virtual rooms for parent cabals */
void load_cvrooms(){
  CABAL_DATA* pCab;
  FILE* fp;
  char path[MIL];
  
  fclose( fpReserve );
  for (pCab = cabal_list; pCab; pCab = pCab->next){
    sprintf( path, "%s%s.vir", SAVECAB_DIR, capitalize(pCab->pIndexData->file_name));
    // load this cabals virtual rooms if any
    if ( ( fp = fopen( path, "r" ) ) != NULL ){
      read_cvrooms( fp, pCab );
      fclose( fp );
    }
  }
  fpReserve = fopen( NULL_FILE, "r" );
}

/* create cabals based on indexes, and load their files if any */
void load_cabals(){
  FILE* fpCabal;
  CABAL_INDEX_DATA* pci = cabal_index_list;
  CABAL_DATA* pCab;
  char path[MIL];


  fclose( fpReserve );
  for (; pci; pci = pci->next ){
// load cabal
    pCab = create_cabal( pci );
// get path to its save file
    sprintf( path, "%s%s.cab", SAVECAB_DIR, capitalize(pci->file_name));
// try to open the save file
    if ( ( fpCabal = fopen( path, "r" ) ) == NULL ){
      perror( path );
      continue;
    }
    nlogf("Loading cabal %s.", pci->file_name);
    fread_cabal( fpCabal, pCab );
    fclose( fpCabal );
  }
  fpReserve = fopen( NULL_FILE, "r" );
}

/* CABAL DATA FUNCTIONS (lookup etc) */

/* returns the parent of a cabal */
inline CABAL_DATA* get_parent( CABAL_DATA* pCab ){
  if (pCab == NULL || pCab->parent == NULL)
    return pCab;
  else
    return get_parent( pCab->parent );
}

/* returns pointer to the cabal based on name */
CABAL_DATA* get_cabal(const char *name){
  CABAL_DATA* pCab = cabal_list;
  for (; pCab; pCab = pCab->next){
    if (LOWER(name[0]) == LOWER(pCab->name[0]) && !str_cmp(name, pCab->name))
      return pCab;
  }
  return NULL;
}

/* returns pointer to cabal based on vnum */
CABAL_DATA* get_cabal_vnum(int vnum){
  CABAL_DATA* pCab = cabal_list;
  for (; pCab; pCab = pCab->next){
    if (pCab->vnum == vnum)
      return pCab;
  }
  return NULL;
}

/* low level function of is_cabal checks if pCab is a child of pCabal */
inline bool _is_cabal( CABAL_DATA* pCab, CABAL_DATA* pCabal ){
  if (pCab == NULL || pCabal == NULL)
    return FALSE;
  else if (pCab == pCabal)
    return TRUE;
  else if (pCab->parent)
    return _is_cabal( pCab->parent, pCabal );
  else
    return FALSE;
}
/* checks by vnum if pCab is a child of cabal with vnum */
bool is_cabal_vnum( CABAL_DATA* pCab, int vnum ){
  if (pCab == NULL)
    return FALSE;
  else if (pCab->vnum == vnum)
    return TRUE;
  else if (pCab->parent)
    return _is_cabal( pCab->parent, get_cabal_vnum( vnum ));
  else
    return FALSE;
}

/* checks if the characters is member of cabal (children inherit parent's cabal) */
bool is_cabal(CHAR_DATA* ch, char* const cabal ){
  if (ch->pCabal == NULL)
    return FALSE;
  return _is_cabal( ch->pCabal, get_cabal( cabal ) );
}

/* checks if pCab and pCabal are children of the same cabal */
bool is_same_cabal(CABAL_DATA* pCab, CABAL_DATA* pCabal ){
  if (pCab == NULL || pCabal == NULL)
    return FALSE;
  return (get_parent(pCab) == get_parent(pCabal));
}

/* returns cabal member data if it exists in the cabal */
CMEMBER_DATA* get_cmember(char* name, CABAL_DATA* cp ){
  CMEMBER_DATA* cm;

  if (cp == NULL)
    return NULL;

  for (cm = cp->members; cm; cm = cm->next){
    if (LOWER(name[0]) == LOWER(cm->name[0]) && !str_cmp(name,cm->name))
      return cm;
  }
  return NULL;
}


/* adds a member data to cabal */
void cmember_to_cabal(CMEMBER_DATA* cm, CABAL_DATA* cp ){

  if (cp->members == NULL)
    cp->members = cm;
  else{
    cm->next = cp->members;
    cp->members = cm;
  }
}

/* removes member data from cabal */
void cmember_from_cabal(CMEMBER_DATA* cm, CABAL_DATA* cp ){
  CMEMBER_DATA* pPrev = NULL;

  if (cm == NULL){
    bug("cmember_from_cabal: NULL members.", 0);
    return;
  }
  if (cp == NULL){
    bug("cmember_from_cabal: NULL cabal.", 0);
    return;
  }

  if (cp->members == cm ){
    cp->members = cm->next;
    
  }
  else{
    for (pPrev = cp->members; pPrev; pPrev = pPrev->next){
      if (pPrev->next == cm){
	pPrev->next = cm->next;
	break;
      }
    }
    if (pPrev == NULL){
      bug("cmember_from_cabal: member data not found.", 0);
      return;
    }
  }
  free_cmember( cm );
}

/* updats the cabal member data from character data */
void update_cmember( CHAR_DATA* ch ){

  if (IS_NPC(ch) || ch->pCabal == NULL){
    return;
  }
  else{
    if (ch->pcdata->member == NULL){
      ch->pcdata->member = add_cmember( ch, ch->pCabal );
    }
    else{
      ch->pcdata->member->connected = ch->desc ? ch->desc->connected == CON_PLAYING : FALSE;
      if (ch->pcdata->member->connected)
	ch->pcdata->member->last_login = mud_data.current_time;

      if (ch->pcdata->member->connected && ch->pcdata->member->cp_pen != 0){
	CP_GAIN( ch, ch->pcdata->member->cp_pen, TRUE );
	CHANGE_CABAL(get_parent(ch->pCabal));
	ch->pcdata->member->cp_pen  = 0;
      }
      if (ch->pcdata->member->garrison_changed){
	sendf( ch, "Garrison now at: <%d.%d>\n\r", GARR_NOR( ch),GARR_UPG(ch));
	ch->pcdata->member->garrison_changed = FALSE;
      }
	       
      ch->pcdata->member->pCabal= ch->pCabal;
      ch->pcdata->member->cp	= GET_CP(ch);
      ch->pcdata->member->rank	= ch->pcdata->rank;
      ch->pcdata->member->level = ch->level;
      ch->pcdata->member->class = ch->class;
      ch->pcdata->member->race = ch->race;

      /* update the hours/mhours basedon last update */
      if (ch->timer <= 5  && ch->in_room->vnum != ROOM_VNUM_LIMBO){
	ch->pcdata->member->hours += (int) (mud_data.current_time - ch->pcdata->member->last_update);
	ch->pcdata->member->mhours += (int) (mud_data.current_time - ch->pcdata->member->last_update);
      }
      ch->pcdata->member->last_update = mud_data.current_time;
    }
  }
}

/* adds complete member data to a cabal */
CMEMBER_DATA* add_cmember( CHAR_DATA* ch, CABAL_DATA* pCab ){
  CMEMBER_DATA* cm = get_cmember( ch->name, pCab );

  if (pCab == NULL)
    return NULL;

  /* set the clan to cabal's clan */
  if (HAS_CLAN(ch) && pCab->clan && GET_CLAN(ch) != pCab->clan){
    CharFromClan( ch );
  }
  CharToClan( ch, pCab->clan );

  if (cm != NULL)
    return cm;
  else{
    cm = new_cmember();
    free_string( cm->name );
    cm->pCabal		= pCab;
    cm->name		= str_dup( ch->name );
    cm->class		= ch->class;
    cm->race		= ch->race;
    cm->level		= ch->level;
    cm->hours		= 0;
    cm->mhours		= 0;
    cm->month		= ch->pcdata->month;
    cm->cp		= GET_CP(ch);
    cm->rank		= ch->pcdata->rank;
    cm->connected	= ch->desc ? ch->desc->connected == CON_PLAYING : FALSE;
    
    cmember_to_cabal( cm, get_parent( pCab ) );
    ch->pcdata->member = cm;
    return cm;
  }
}

/* low level functions that adds/removes a character to cabal */
bool _char_to_cabal( CHAR_DATA* ch, CABAL_DATA* pCab ){

  pCab->cur_member  = UMIN(pCab->max_member, pCab->cur_member + 1);
  if (pCab->parent)
    _char_to_cabal( ch, pCab->parent );
  return TRUE;
}


bool char_to_cabal( CHAR_DATA* ch, CABAL_DATA* pCab ){
  if (pCab == NULL){
    bug("char_to_cabal: NULL cabal passed.", 0);
    return FALSE;
  }
  if (IS_NPC(ch)){
    bug("char_to_cabal: NPC passed.", 0);
    return FALSE;
  }
  if (ch->pCabal){
    char_from_cabal( ch );
  }
  
  if (_char_to_cabal( ch, pCab )){
    CABAL_DATA* pPar = get_parent(pCab);
    CMEMBER_DATA* cm = get_cmember( ch->name, pPar );
    
    ch->pcdata->rank = 0;
    ch->pCabal = pCab;
    
    /* either the member data is fresh new, or just updated */
    if (cm == NULL)
      ch->pcdata->member = add_cmember( ch, pCab );
    else{
      cm->pCabal	= pCab;
      cm->class		= ch->class;
      cm->race		= ch->race;
      cm->level		= ch->level;
      cm->month		= ch->pcdata->month;
      cm->cp		= ch->pcdata->cpoints;
      cm->rank		= ch->pcdata->rank;
      cm->connected	= ch->desc ? ch->desc->connected == CON_PLAYING : FALSE;
      ch->pcdata->member = cm;
    }
    
    if (!IS_CABAL(pCab, CABAL_CANHIDE))
      SET_BIT(ch->game, GAME_SHOW_CABAL);
    else
      REMOVE_BIT(ch->game, GAME_SHOW_CABAL);
    
    save_cabals( FALSE, NULL );
  }
  return TRUE;
}

/* low level function */
bool _char_from_cabal( CHAR_DATA* ch, CABAL_DATA* pCabal ){

  pCabal->cur_member  = UMAX(0, pCabal->cur_member - 1);
  if (ch->pcdata->rank == RANK_LEADER)
    pCabal->cur_leader  = UMAX(0, pCabal->cur_leader - 1);
  if (ch->pcdata->rank == RANK_ELDER)
    pCabal->cur_elder  = UMAX(0, pCabal->cur_elder - 1);

  if (pCabal->parent)
    _char_from_cabal( ch, pCabal->parent );
  return TRUE;
}

bool char_from_cabal( CHAR_DATA* ch ){
  CMEMBER_DATA* cm;

  if (IS_NPC(ch)){
    bug("char_From_cabal: NPC passed.", 0);
    return FALSE;
  }

  if (ch->pCabal == NULL){
    bug("char_from_cabal: NULL cabal passed.", 0);
    return FALSE;
  }
  /* clear sponsor if any */
  if (  (cm = sponsor_check(get_parent(ch->pCabal), ch->name )) != NULL){
    char buf[MIL];
    sprintf( buf, "Your obligations as a sponsor of %s have ended.\n\r", ch->name);
    do_hal( cm->name, "End of Sponsorship.", buf, NOTE_NEWS);
    free_string(cm->sponsored);
    cm->sponsored = str_dup( "" );
  }

  if (_char_from_cabal( ch, ch->pCabal ) ){
    CABAL_DATA* pPar = get_parent(ch->pCabal);
    CMEMBER_DATA* cm = get_cmember( ch->name, pPar );

    if (cm){
      cmember_from_cabal( cm, pPar );
      ch->pcdata->member = NULL;
    }
    ch->pcdata->rank = 0;
    ch->pCabal = NULL;
    REMOVE_BIT(ch->game, GAME_SHOW_CABAL);
    save_cabals( FALSE, NULL );
    return TRUE;
  }
  else
    return FALSE;
}

/* checks if person should be removed from cabal after extended leave */
bool purge_cabal(CMEMBER_DATA* cMem){
  struct tm *that_time;
  int month = 0;
  CABAL_DATA* pc = get_parent(cMem->pCabal);

  // Checks for hours played per month, if not enough, purge out of cabal
  that_time = gmtime(&mud_data.current_time);
  month = that_time->tm_mon;
  
  if (cMem->mhours < 0)
    cMem->mhours = 0;
  
  if(cMem->level <= 50 && cMem->month >= 0 && cMem->month != month ){
    DESCRIPTOR_DATA* d;
    CHAR_DATA* ch;
    bool fCon = cMem->connected;
    char buf[MIL];

    if ( cMem->mhours < 36000){	//10h per month minimum
      /* bring the player on */
      if (!fCon){
	if ( (d = bring(get_room_index( ROOM_VNUM_LIMBO ), cMem->name)) == NULL){
	  bug("purge_cabal: Could not find the applicant pfile.", 0);
	  cmember_from_cabal( cMem, pc );
	  return TRUE;
	}
	else{
	  ch = d->character;
	}
      }
      else if ( (ch = get_char( cMem->name )) == NULL)
	return FALSE;
      
      
      /* print the messges */
      sprintf( buf, "Due to lack of presence %s has been removed from %s.", ch->name, pc->who_name );
      log_string( buf );
      wiznet(log_buf,NULL,NULL,WIZ_LOGINS,0,get_trust(ch));
      cabal_echo( pc, buf );
      do_hal( pc->name, "Automatic expulsion.", buf, NOTE_NEWS);
      
      /* we set the messages for login since he was not online */
      SET_BIT(ch->pcdata->messages, MSG_CAB_TEXPEL);
      
      /* remove from cabal */
      ch->pcdata->last_app = mud_data.current_time + VOTE_DAY * 7;
      update_cabal_skills(ch, cMem->pCabal, TRUE, TRUE);
      if (ch->pCabal && ch->pCabal == cMem->pCabal){
	char_from_cabal( ch );      
	save_char_obj( ch );
      }
      else
	cmember_from_cabal( cMem, get_parent( cMem->pCabal ) );
      
      if (!fCon)
	purge( ch );
      cMem->month = month;
      cMem->mhours = 0;
      return TRUE;
    }
    else{
      cMem->month = month;
      cMem->mhours = 0;
      return FALSE;
    }
  }
  return FALSE;
}


/* raises or lowers a rank within cabal as per "gain", returns new rank */
int promote( CHAR_DATA* ch, CABAL_DATA* pc, int gain ){
  bool was_leader = ch->pcdata->rank == RANK_LEADER;
  bool was_elder  = ch->pcdata->rank == RANK_ELDER;

  if (IS_NPC(ch)){
    bug("promote: NPC passed.", 0);
    return FALSE;
  }

  ch->pcdata->rank = URANGE(0, ch->pcdata->rank + gain, RANK_LEADER);

  if (ch->pcdata->member != NULL){
    ch->pcdata->member->rank = ch->pcdata->rank;
  }
  if (was_leader && !IS_LEADER( ch ))
    pc->cur_leader = UMAX( 0, pc->cur_leader - 1);
  else if (!was_leader && IS_LEADER( ch ))
    pc->cur_leader = UMIN( pc->max_leader, pc->cur_leader + 1);
  if (was_elder && !IS_ELDER( ch ))
    pc->cur_elder = UMAX( 0, pc->cur_elder - 1);
  else if (!was_elder && IS_ELDER( ch ))
    pc->cur_elder = UMIN( pc->max_elder, pc->cur_elder + 1);

  /* adjust the hours required for promotion if this is a demotion*/
  if (gain < 0){
    ch->pcdata->member->hours = ch->pcdata->rank * 36000;
  }
  save_cabals( FALSE, NULL );
  return ch->pcdata->rank;
}

/* checks if a player can select a given cabal as a sub calab */
CABAL_DATA* can_select_child( CHAR_DATA* ch, char* child ){
  CABAL_DATA* pChild = get_cabal( child );

  if (pChild == NULL || ch->pCabal == NULL || IS_NPC( ch ))
    return NULL;
  else if (pChild->parent == NULL || !_is_cabal(pChild, ch->pCabal) 
	   || ch->pcdata->rank < 1 
	   || ch->pcdata->rank < pChild->pIndexData->min_rank)
    return NULL;
  else
    return pChild;
}

/* checks if the player can choose any cabal children based on his rank and his cabal */
bool has_child_choice( CABAL_DATA* pCab, int rank ){

  if (pCab == NULL || rank == 0){
    return FALSE;
  }
  else{
    /* we run through all cabals, and look for ones that have pCab as parent */
    CABAL_DATA* pc = cabal_list;

    for (; pc != NULL; pc = pc->next ){
      if (pc->parent && pc->parent == pCab && rank >= pc->pIndexData->min_rank)
	return TRUE;
    }
    return FALSE;
  }
}

/* shows possible child choices to a player */
void show_child_choice( CHAR_DATA* ch, CABAL_DATA* pCab, int rank ){

  if (!has_child_choice( pCab, rank )){
    send_to_char("You have no possible cabal choices.\n\r", ch);
    return;
  }
  else{
    /* we run through all cabals, and look for ones that have pCab as parent */
    CABAL_DATA* pc = cabal_list;
    BUFFER *buffer;
    char buf[MSL];

    
    buffer=new_buf();
    buf[0] = 0;

    for (; pc != NULL; pc = pc->next ){
      if (pc->parent && _is_cabal(pc, pCab) ){
	if (rank >= pc->pIndexData->min_rank)
	  sprintf( buf, "%-15s %-15s [Rank: %s]\n\r", pc->parent->who_name, pc->who_name, clanr_table[pc->pIndexData->min_rank][0]);
	else
	  sprintf( buf, "%-15s %-15s `8[Rank: %s]``\n\r", pc->parent->who_name, pc->who_name, clanr_table[pc->pIndexData->min_rank][0]);
	add_buf(buffer, buf );
      }
    }
    if (IS_NULLSTR(buf)){
      send_to_char("You have no possible cabal choices.\n\r", ch);
    }
    else{
      page_to_char(buf_string(buffer),ch);
    }
    free_buf(buffer);
  }   
}
  

char* get_crank( CHAR_DATA* ch ){
  static char* null = "";
  
  if (IS_NPC(ch) || ch->pCabal == NULL)
    return null;
  else
    return get_crank_str( ch->pCabal, ch->pcdata->true_sex, ch->pcdata->rank );
}

/* returns a proper string for cabal rank */
inline char* get_crank_str( CABAL_DATA* pCabal, int sex, int rank ){
  return sex == SEX_FEMALE ? pCabal->pIndexData->franks[rank] : pCabal->pIndexData->mranks[rank];
}
   
      
/* sets a reward for all cabal membeers, applied when they log on */
void reward( CABAL_DATA* pCab, int amount ){
  CMEMBER_DATA* cMem;
  CABAL_DATA* pPar = get_parent( pCab );
  char buf[MIL];
  
  for (cMem = pPar->members; cMem; cMem = cMem->next ){
    if (_is_cabal(cMem->pCabal, pCab))
      cMem->cp_pen = amount;
  }
  if (pCab == pPar)
    CP_CAB_GAIN( pPar, amount);
  
  sprintf( buf, "A %s of %d %s%s has been applied to your cabal.", 
	   amount < 0 ? "penalty" : "reward",
	   amount,
	   pCab->currency, amount == 1 ? "" : "s");
  cabal_echo( pCab, buf );
}

/* IMMortal cabal commands */
void do_imm_cabal( CHAR_DATA *ch, char *argument ){
  CABAL_DATA* pCab;
  CHAR_DATA *victim;
  char arg1[MIL];

  argument = one_argument( argument, arg1 );
  if (IS_NULLSTR(arg1) || IS_NULLSTR(argument)){
    send_to_char( "Syntax:\n\r"\
		  "cabal info <cabal>\n\r"\
		  "cabal rooms <cabal>\n\r"\
		  "cabal penalty <cabal> <amount>\n\r"\
		  "cabal reward <cabal> <amount>\n\r"\
		  "cabal coffers <cabal> <amount>\n\r"\
		  "cabal <char> <cabal name>\n\r"\
		  "cabal <char> <promote/demote>\n\r", ch);
    return;
  }
  /* INFO */
  if (!str_prefix(arg1, "info")){
    if (IS_NULLSTR(argument)){
      send_to_char("Show info for which cabal?\n\r", ch);
      return;
    }
    else if ( (pCab = get_parent(get_cabal( argument ))) == NULL){
      send_to_char("No such cabal.\n\r", ch);
      return;
    }
    else
      show_cabal_info( ch, pCab );
    return;
  }
  /* ROOMS */
  if (!str_prefix(arg1, "rooms")){
    if (IS_NULLSTR(argument)){
      send_to_char("Show rooms for which cabal?\n\r", ch);
      return;
    }
    else if ( (pCab = get_parent(get_cabal( argument ))) == NULL){
      send_to_char("No such cabal.\n\r", ch);
      return;
    }
    else
      show_cabal_rooms( ch, pCab );
    return;
  }
/* PENALTY */
  else if ( !str_prefix( arg1, "penalty")){
    char cabal[MIL];
    int amount;
    argument = one_argument( argument, cabal );

    if ( (pCab = get_cabal( cabal )) == NULL){
      send_to_char("That cabal does not exists.\n\r", ch);
      return;
    }
    else if( (amount = atoi( argument )) < 1){
      send_to_char("Penalty must be non zero, and positive.\n\r", ch );
      return;
    }
    else{
      char buf[MIL];
      reward( pCab, -amount );

      sprintf( buf, "%s has applied a penalty of %d to %s.", ch->name, amount, pCab->who_name );
      do_hal( "Immortal", "Cabal Penalty", buf, NOTE_PENALTY );
      sendf( ch, "You have applied a penalty of %d to all members of %s.\n\r", amount, pCab->who_name );
      return;
    }
  }
/* REWARD */
  else if ( !str_prefix( arg1, "reward")){
    char cabal[MIL];
    int amount;
    argument = one_argument( argument, cabal );

    if ( (pCab = get_cabal( cabal )) == NULL){
      send_to_char("That cabal does not exists.\n\r", ch);
      return;
    }
    else if( (amount = atoi( argument )) < 1){
      send_to_char("Reward must be non zero, and positive.\n\r", ch );
      return;
    }
    else{
      char buf[MIL];
      reward( pCab, amount );

      sprintf( buf, "%s has applied a reward of %d to %s.", ch->name, amount, pCab->who_name );
      do_hal( "Immortal", "Cabal Reward", buf, NOTE_PENALTY );
      sendf( ch, "You have applied a reward of %d to all members of %s.\n\r", amount, pCab->who_name );
      return;
    }
  }
/* MAP */
  else if (!str_prefix(arg1, "map")){
    char cabal[MIL];
    argument = one_argument( argument, cabal );
    if ( (pCab = get_cabal( cabal )) == NULL){
      send_to_char("That cabal does not exists.\n\r", ch);
      return;
    }
    show_cabal_map( ch, pCab, atoi(argument) );
    return;
  }

/* COFFERS */
  else if ( !str_prefix( arg1, "coffers")){
    char cabal[MIL];
    int amount;
    argument = one_argument( argument, cabal );

    if ( (pCab = get_cabal( cabal )) == NULL){
      send_to_char("That cabal does not exists.\n\r", ch);
      return;
    }
    else if( (amount = atoi( argument )) == 0){
      send_to_char("Amount must be non zero.\n\r", ch );
      return;
    }
    else if (get_trust(ch) < CREATOR){
      send_to_char("Only the Implementor can set coffers.\n\r", ch);
      return;
    }
    else{
      CP_CAB_GAIN(pCab, amount);
      return;
    }
  }
  if ( ( victim = get_char_world( ch, arg1 ) ) == NULL ){
    send_to_char( "They aren't playing.\n\r", ch );
    return;
  }
  if ( IS_NPC(victim) ){
    send_to_char( "Why would you want to cabal a mob?\n\r",ch);
    return;
  }
  if (!str_prefix(argument,"none")){
    if (victim->pCabal == NULL){
      send_to_char("They are not in any cabal.\n\r", ch);
      return;
    }
    if (get_trust( ch ) < CREATOR 
	&& str_cmp(get_parent(victim->pCabal)->immortal, ch->name)){
      send_to_char("You are not in charge of that cabal.\n\r", ch);
      return;
    }
    sendf(ch, "%s is now caballess.\n\r", PERS2(victim));
    send_to_char("You are no longer in a cabal!\n\r",victim);
    update_cabal_skills(victim, ch->pCabal, TRUE, TRUE);
    char_from_cabal( victim );
    return;
  }  


  if ( ( victim = get_char_world( ch, arg1 ) ) == NULL ){
    send_to_char( "They aren't playing.\n\r", ch );
    return;
  }
  if ( IS_NPC(victim) ){
    send_to_char( "Why would you want to cabal a mob?\n\r",ch);
    return;
  }
  if (!str_prefix(argument,"none")){
    if (victim->pCabal == NULL){
      send_to_char("They are not in any cabal.\n\r", ch);
      return;
    }
    if (get_trust( ch ) < CREATOR 
	&& str_cmp(get_parent(victim->pCabal)->immortal, ch->name)){
      send_to_char("You are not in charge of that cabal.\n\r", ch);
      return;
    }
    sendf(ch, "%s is now caballess.\n\r", PERS2(victim));
    send_to_char("You are no longer in a cabal!\n\r",victim);
    update_cabal_skills(victim, victim->pCabal, TRUE, TRUE);
    char_from_cabal( victim );
    return;
  }  
  else if (!str_prefix(argument,"promote")){
    CMEMBER_DATA* cm;
    if (victim->pCabal == NULL){
      send_to_char("They are not cabaled.\n\r", ch );
      return;
    }
    if (get_trust( ch ) < CREATOR
	&& str_cmp(get_parent(victim->pCabal)->immortal, ch->name)){
      send_to_char("You are not in charge of that cabal.\n\r", ch);
      return;
    }

    if (victim->pcdata->rank + 1 >= RANK_ELDER
	&& IS_IMMORTAL(victim)
	&& get_trust(ch) < CREATOR){
      send_to_char("Immortals are limited to non leadership positions.\n\r", ch );
      return;
    }
    if (victim->pcdata->rank + 1 == RANK_ELDER
	&& get_trust(ch) < CREATOR
	&& victim->pCabal->cur_elder + 1 > victim->pCabal->max_elder){
      send_to_char("The cabal cannot support another elder.\n\r", ch);
      return;
    }
    if (victim->pcdata->rank + 1 == RANK_LEADER
	&& get_trust(ch) < CREATOR
	&& victim->pCabal->cur_leader + 1 > victim->pCabal->max_leader){
      send_to_char("The cabal cannot support another leader.\n\r", ch);
      return;
    }

    promote( victim, victim->pCabal, 1 );

    /* clear sponsor if any */
    if (  victim->pcdata->rank >= 3
	  && (cm = sponsor_check(get_parent(victim->pCabal), victim->name )) != NULL){
      char buf[MIL];
      sprintf( buf, "Your obligations as a sponsor of %s have ended.\n\r", victim->name);
      do_hal( cm->name, "End of Sponsorship.", buf, NOTE_NEWS);
      free_string(cm->sponsored);
      cm->sponsored = str_dup( "" );
    }
    sendf( ch, "Promoted to rank of %s (%d)\n\r", 
	   get_crank( victim ),
	   victim->pcdata->rank);
    sendf( victim, "You have been promoted to %s of %s.\n\r", 
	   get_crank( victim ),
	   victim->pCabal->name);
    if (has_child_choice( victim->pCabal, victim->pcdata->rank )){
      send_to_char("`@You are now able to choose further within your organization. (\"cabal join\")``\n\r", victim);
    }
    return;
  }
  else if (!str_prefix(argument,"demote")){
    if (victim->pCabal == NULL){
      send_to_char("They are not cabaled.\n\r", ch );
      return;
    }
    if (get_trust( ch ) < CREATOR
	&& str_cmp(get_parent(victim->pCabal)->immortal, ch->name)){
      send_to_char("You are not in charge of that cabal.\n\r", ch);
      return;
    }
    promote( victim, victim->pCabal, -1 );
    sendf( ch, "Demoted to rank of %s (%d)\n\r", 
	   get_crank( victim ),
	   victim->pcdata->rank);
    sendf( victim, "You have been demoted to %s of %s.\n\r", 
	   get_crank( victim ),
	   victim->pCabal->name);
    return;
  }
  else{  
    if ( (pCab = get_cabal(argument)) == NULL){
      send_to_char("No such cabal exists.\n\r",ch);
      return;
    }
    if (get_trust( ch ) < CREATOR ){
      send_to_char("Need Imp acess.\n\r", ch);
      return;
    }
    if (victim->pCabal){
      update_cabal_skills(victim, ch->pCabal, TRUE, TRUE);
      char_from_cabal( victim );
    }
    if (char_to_cabal( victim, pCab ) ){
      sendf(ch,"%s is now a member of cabal %s.\n\r", PERS2(victim), capitalize(victim->pCabal->name));
      sendf(victim,"You are now a member of cabal %s.\n\r", capitalize(victim->pCabal->name));
      update_cabal_skills(victim, victim->pCabal, FALSE, TRUE);

    }
  }

}

/* cabal commands */
void do_cabal( CHAR_DATA *ch, char *argument ){
  char arg1[MIL];


  if (IS_IMMORTAL( ch )){
    do_imm_cabal( ch, argument );
    return;
  }
  if (IS_NPC( ch ) || ch->pCabal == NULL){
    send_to_char("Huh?\n\r", ch);
    return;
  }
/* normal mortal cabal commands */
  if (IS_NULLSTR(argument)){
    send_to_char( "Syntax:\n\r"\
		  "sponsor <character>\n\r"\
		  "cabal info\n\r"\
		  "cabal info <cabal>\n\r"\
		  "cabal rooms\n\r"\
		  "cabal donate\n\r"\
		  "cabal map\n\r"\
		  "cabal promotion\n\r"
		  "cabal <join> <sub-cabal>\n\r"\
		  "cabal leave\n\r", ch);
    return;
  }
  argument = one_argument( argument, arg1 );
  if (!str_cmp(arg1, "join")){
    CABAL_DATA* pCab = NULL;

    if (IS_NULLSTR( argument )){
      show_child_choice( ch, ch->pCabal, ch->pcdata->rank);
      return;
    }
    else if ( (pCab = can_select_child( ch, argument)) == NULL){
      send_to_char("That is not a valid choice.\n\r", ch);
      return;
    }
    else if (!check_cabal_req( ch, pCab )){
      return;
    }
    else{
      char buf[MIL];

      ch->pCabal = pCab;
      update_cmember( ch );
      update_cabal_skills( ch, ch->pCabal, FALSE, TRUE );
      save_char_obj( ch );
      save_cabals( FALSE, NULL );

      sendf( ch, "You have joined the ranks of %s!\n\r", ch->pCabal->who_name);
      sprintf( buf, "%s has chosen to join the ranks of %s.", PERS2( ch ), ch->pCabal->who_name );
      cabal_echo( ch->pCabal, buf );
      
    }
  }
  else if (!str_cmp(arg1, "leave")){
    char buf[MIL];
    if (str_cmp(argument, "confirm")){
      send_to_char("THIS COMMAND WILL REMOVE YOU FROM THE CABAL!\n\rUse \"cabal leave confirm\" to proceed.\n\r", ch);
      return;
    }
    sprintf(buf, "%s has decided to quit our ranks.", PERS2(ch));
    cabal_echo(get_parent(ch->pCabal), buf );
    do_hal(get_parent(ch->pCabal)->name, buf, buf, NOTE_NEWS);

    CHANGE_CABAL( ch->pCabal );
    update_cabal_skills(ch, ch->pCabal, TRUE, TRUE);
    char_from_cabal( ch );
    ch->pcdata->last_app = mud_data.current_time + VOTE_DAY * 14;

    save_char_obj( ch );
    save_cabals( TRUE, NULL );
    return;
  }
  else if (!str_prefix(arg1, "map")){
    show_cabal_map( ch, ch->pCabal, -999 );
    return;
  }
  else if (!str_prefix(arg1, "promotion")){
    if (check_promo_hours( ch->pcdata->member,  ch->pcdata->rank )){
      sendf(ch, "You have been present enough to be promoted.\n\r");
    }
    else{
      int diff = (ch->pcdata->member->rank + 1) * 10 - (ch->pcdata->member->hours / 3600);
      sendf( ch, "%d more hours must pass before your cabal promotion.\n\r", diff);
    }
    check_promo_req( ch, ch->pcdata->member, ch->pcdata->member->rank + 1);
    return;
  }
  else if (!str_prefix(arg1, "donate")){
    CABAL_DATA* pCab = get_parent(ch->pCabal );
    int val = atoi(argument);
    char buf[MIL];

    if (val < 1){
      sendf(ch, "Donate how many %ss to the coffers?\n\r", ch->pCabal->currency);
      return;
    }
    else if (GET_CP(ch) < val ){
      sendf(ch, "You do not have that many %ss.\n\r", ch->pCabal->currency);
      return;
    }
    else if (GET_CAB_CP(pCab) + (val / 2) > 500){
      val = (500 - GET_CAB_CP(pCab)) * 2;
    }
    if (val < 1){
      send_to_char("The coffers cannot accept your donation.\n\r", ch );
      return;
    }
    CP_GAIN(ch, -val, TRUE );
    CP_CAB_GAIN(pCab, val / 2 );
    sprintf(buf, "As a result of %s's donation  the coffers are now at %d %ss.", PERS2(ch), 
	    GET_CAB_CP(pCab), pCab->currency);
    cabal_echo(pCab, buf );
    return;
  }
  else if (!str_prefix(arg1, "sponsor")){
    CABAL_DATA* pCab = get_parent(ch->pCabal );
    CHAR_DATA* victim;

    if (!IS_CABAL(pCab, CABAL_SPONSOR)){
      send_to_char("Your organization does not use the sponsor system.\n\r", ch);
      return;
    }
    else if (IS_NULLSTR(argument)){
      send_to_char("Sponsor who?\n\r", ch );
      return;
    }
    else if (ch->pcdata->member == NULL){
      send_to_char("You are not a member of any cabal.\n\r", ch);
      return;
    }
    else if (!IS_NULLSTR(ch->pcdata->member->sponsored) 
	     && get_cmember(ch->pcdata->member->sponsored, get_parent( ch->pCabal)) != NULL){
      sendf( ch, "You already have %s under your guidance.\n\r", ch->pcdata->member->sponsored);
      return;
    }

/* check for sponsorship and exsiting vote  */

    if (!IS_NULLSTR(ch->pcdata->member->sponsored)){
      VOTE_DATA* pv;
      for (pv = vote_list; pv; pv = pv->next ){
	if (pv->type == VOTE_CAB_APPLY && pv->value[0] == pCab->vnum && !str_cmp(pv->owner, ch->pcdata->member->sponsored)){
	  sendf(ch, "%s's application vote is still undecided!\n\r", ch->pcdata->member->sponsored);
	  return;
	}
      }
    }

    if (GET_CP(ch) < 50){
      sendf( ch, "You must have at least 50 %ss before you can sponsor.\n\r", ch->pCabal->currency);
      return;
    }
    else if (ch->pcdata->rank < 2){
      sendf(ch, "You must hold the rank of %s before you can sponsor.\n\r", 
	    get_crank_str( ch->pCabal, ch->pcdata->true_sex, 2 ));
      return;
    }
    else if ( (victim = get_char_room( ch, NULL, argument)) == NULL){
      send_to_char("They aren't here.\n\r", ch);
      return;
    }
    else if (IS_NPC(victim)){
      send_to_char("Not on mobiles.\n\r", ch );
      return;
    }
    else if (victim == ch ){
      send_to_char("There is no point in sponsoring yourself.\n\r", ch );
      return;
    }
    else{
      if (!IS_NULLSTR(ch->pcdata->member->sponsored))
	free_string( ch->pcdata->member->sponsored );
      ch->pcdata->member->sponsored = str_dup( victim->name );
      sendf(ch, "You are now sponsoring %s.\n\r", PERS2( victim ));
      CP_GAIN(ch, -50, TRUE );
    }
  }
  else if (!str_prefix(arg1, "info")){
    CABAL_DATA* pCab = get_parent( ch->pCabal );
    if (IS_NULLSTR(argument)){
      /* sponsor data if any */
      if (!IS_NPC(ch) && ch->pcdata->member){
	if (!IS_NULLSTR(ch->pcdata->member->sponsor))
	  sendf(ch, "You are currently being sponsored by %s.\n\r", ch->pcdata->member->sponsor);
	if (!IS_NULLSTR(ch->pcdata->member->sponsored))
	  sendf(ch, "You are currently sponsoring %s.\n\r", ch->pcdata->member->sponsored);
      }

      /* show cabal info */
      show_cabal_info( ch, pCab );
    }	    
    else if (!IS_CABAL(pCab, CABAL_SPIES)){
      send_to_char("Your organization has not gained trust of the crime world.\n\r", ch);
      return;
    }
    else if ( (pCab = get_parent(get_cabal( argument ))) == NULL
	      || IS_CABAL(pCab, CABAL_ROUGE)){
      send_to_char("Your contacts lack any information on that subject.\n\r", ch );
      return;
    }
    else
      show_cabal_info( ch, pCab );
  }
  else if (!str_prefix(arg1, "rooms")){
    CABAL_DATA* pCab = get_parent( ch->pCabal );
    /* show cabal info */
    show_cabal_rooms( ch, pCab );
  }	    
  else
    do_cabal(ch, "");
}

/* echos a string to all cabal members with given cabal flag */
void cabal_echo_flag(int flag, char* string){
  DESCRIPTOR_DATA* d;
  char buf[MIL];

  for ( d = descriptor_list; d != NULL; d = d->next ){
    CHAR_DATA* rch = d->character;
    bool fDemon = FALSE;

    if ( d->connected != CON_PLAYING 
	 || rch == NULL
	 || rch->pCabal == NULL
	 || IS_AFFECTED2(rch, AFF_SILENCE)
	 || IS_SET(rch->comm, COMM_NOCABAL) 
	 || IS_SET(rch->comm, COMM_QUIET) ){
      continue;
    }
    fDemon = IS_TELEPATH( rch );

    if (IS_CABAL(get_parent(rch->pCabal), flag) ){
      sprintf(buf, "[%s]: '`0$t``'", rch->pCabal->who_name);
      act_new(buf, rch, string, NULL, TO_CHAR, POS_DEAD);

      if (!IS_NPC(rch) && rch->pcdata->eavesdropped != NULL
	  && is_affected(rch->pcdata->eavesdropped,gsn_eavesdrop)
	  && !fDemon){
	
	send_to_char("A faint message transmits from the parasite.\n\r",rch->pcdata->eavesdropped);
	act_new(buf, rch, string, NULL, TO_CHAR, POS_DEAD);      
      }
    }
  }
}

/* echos a string to all cabal members of pCabal or all cabals that have "Flag" set */
void cabal_echo( CABAL_DATA* pCabal, char* string){
  DESCRIPTOR_DATA* d;
  char buf[MIL];

  sprintf(buf, "[%s]: '`0$t``'", pCabal->who_name);

  for ( d = descriptor_list; d != NULL; d = d->next ){
    CHAR_DATA* rch = d->character;
    bool fDemon = FALSE;

    if ( d->connected != CON_PLAYING 
	 || rch == NULL
	 || rch->pCabal == NULL
	 || IS_AFFECTED2(rch, AFF_SILENCE)
	 || IS_SET(rch->comm, COMM_NOCABAL) 
	 || IS_SET(rch->comm, COMM_QUIET) 
	 || !is_same_cabal(rch->pCabal, pCabal) ){
      continue;
    }

    fDemon = IS_TELEPATH( rch );
    act_new(buf, rch, string, NULL, TO_CHAR, POS_DEAD);

    if (!IS_NPC(rch) && rch->pcdata->eavesdropped != NULL
	&& is_affected(rch->pcdata->eavesdropped,gsn_eavesdrop)
	&& !fDemon){
	
      send_to_char("A faint message transmits from the parasite.\n\r",rch->pcdata->eavesdropped);
      act_new(buf, rch, string, NULL, TO_CHAR, POS_DEAD);      
    }
  }
}


/* sets or clears all cabal's skills and/or its parent's */
void update_cabal_skills( CHAR_DATA* ch, CABAL_DATA* pCabal, bool fRemove, bool fParent ){
  CSKILL_DATA* cSkill;
  CABAL_INDEX_DATA* pIndex;
  
  
  if (IS_NPC( ch ))
    return;

  if (pCabal == NULL)
    return;
  else if ( (pIndex = pCabal->pIndexData) == NULL)
    return;
  else if (pIndex->skills == NULL)
    return;
  else
    cSkill =  pIndex->skills;

  /* run through all cabal skills and set or remove them */
  for( cSkill = pIndex->skills; cSkill; cSkill = cSkill->next){
    if (fRemove 
	&& (skill_table[cSkill->pSkill->sn].skill_level[ch->class] < 1 
	    || skill_table[cSkill->pSkill->sn].skill_level[ch->class] > 50)
	){
      ch->pcdata->learned[cSkill->pSkill->sn] = 0;
    }
    else if ( ch->pcdata->learned[cSkill->pSkill->sn] < cSkill->pSkill->learned)
      ch->pcdata->learned[cSkill->pSkill->sn] = UMAX(2, cSkill->pSkill->learned);
  }
/* check if we move onto parent */
  if (fParent && pCabal->parent)
    update_cabal_skills( ch, pCabal->parent, fRemove, fParent);
}

  
/* finds a cabal skill in cabal data */
inline CSKILL_DATA *cskill_find(CSKILL_DATA *csk, int sn){
    CSKILL_DATA *csk_find;

    if (csk == NULL)
      return FALSE;

    for ( csk_find = csk; csk_find != NULL; csk_find = csk_find->next )
    {
        if ( csk_find->pSkill->sn == sn )
	return csk_find;
    }
    return NULL;
}

/* checks for a cabal skill in cabal's data */
inline CSKILL_DATA* get_cskill
( CABAL_DATA* pCab, int sn ){
  CSKILL_DATA* cSkill = NULL;

  if (pCab == NULL)
    return FALSE;
  if ( (cSkill = cskill_find( pCab->pIndexData->skills, sn )) != NULL){
    return cSkill;
  }
  else
    return get_cskill(pCab->parent, sn );
}

/* checks if someone in the cabal has sponsorted this character */
CMEMBER_DATA* sponsor_check( CABAL_DATA* pCab, char* name ){
  CMEMBER_DATA* cm;

  if (pCab == NULL || name == NULL)
    return NULL;
  
  for (cm = get_parent(pCab)->members; cm; cm = cm->next){
    if (is_name(name, cm->sponsored))
      return cm;
  }
  return NULL;
}

/* checks if a player can join a given cabal */
bool check_cabal_req( CHAR_DATA* ch, CABAL_DATA* pc ){
  int ethos = 0;
  int votes = 0;
  VOTE_DATA* pv;

  if (ch == NULL || pc == NULL )
    return FALSE;

  /* count application votes to this cabal */
  for (pv = vote_list; pv; pv = pv->next ){
    if (pv->type == VOTE_CAB_APPLY && pc->vnum == pv->value[0])
      votes++;
  }
  if ((pc->cur_member + votes) >= pc->max_member){
    sendf(ch, "You cannot join %s as it's at full capacity.\n\r", pc->who_name);
    return FALSE;
  }
  if (IS_CABAL(pc, CABAL_SPONSOR) && pc->cur_member >= 4 && !sponsor_check( pc, ch->name )){
    sendf(ch, "You cannot join %s as you have not been sponsored. (\"help sponsor\")\n\r", pc->who_name);
    return FALSE;
  }
  if (ch->level < pc->pIndexData->min_lvl){
    sendf(ch, "You cannot join %s as your rank is less then the requirement (%d - %d).\n\r", pc->who_name, pc->pIndexData->min_lvl, pc->pIndexData->max_lvl);
    return FALSE;
  }
  if (ch->level > pc->pIndexData->max_lvl){
    sendf(ch, "You cannot join %s as your rank is more then the requirement (%d - %d).\n\r", pc->who_name, pc->pIndexData->min_lvl, pc->pIndexData->max_lvl);
    return FALSE;
  }

  /* check align */
  if (IS_GOOD(ch) && !IS_SET(pc->align, CALIGN_GOOD)){
    sendf(ch, "You cannot join as Lightwalkers are not wanted in %s.\n\r", pc->who_name);
    return FALSE;
  }
  else if (IS_NEUTRAL(ch) && !IS_SET(pc->align, CALIGN_NEUTRAL)){
    sendf(ch, "Your application was rejected as Neutrals are not wanted in %s.\n\r", pc->who_name);
    return FALSE;
  }
  else if (IS_EVIL(ch) && !IS_SET(pc->align, CALIGN_EVIL)){
    sendf(ch, "Your cannot join as Evil is not wanted in %s.\n\r", pc->who_name);
    return FALSE;
  }

  //get ethos.
  switch (ch->pcdata->ethos)
    {
    case 0: ethos = DIETY_ETHOS_LAWFUL;break;
    case 1: ethos = DIETY_ETHOS_NEUTRAL;break;
    case 2: ethos = DIETY_ETHOS_CHAOTIC;break;
    }

  /* check ethos */
  if ( !IS_SET(ethos, pc->ethos)){
    sendf(ch, "You cannot join as your ethos is not wanted in %s.\n\r", pc->who_name);
    return FALSE;
  }
    
  /* check race */
  if (pc->race[0] >= 0
      && pc->race[ch->race] == FALSE){
    sendf(ch, "You cannot join as %ss are not wanted in %s.\n\r", race_table[ch->race].name, pc->who_name);
    return FALSE;
  }
  /* check avatar */
  if (!pc->fAvatar && IS_AVATAR( ch )){
    sendf(ch, "You cannot join as %ss are not wanted in %s.\n\r", "Avatar", pc->who_name);
    return FALSE;
  }

  /* check class */
  if (pc->class[0] >= 0
      && pc->class[ch->class] == FALSE){
    sendf(ch, "You cannot join as %ss are not wanted in %s.\n\r", class_table[ch->class].name, pc->who_name);
    return FALSE;
  }

  return TRUE;
}

/* check if given character has met all promotion requirements for this cabal*/
/* NULL ch for silent check */
bool check_promo_req( CHAR_DATA* ch, CMEMBER_DATA* cm, int rank ){
  CABAL_DATA* pCab; 
  DESCRIPTOR_DATA* d;
  CHAR_DATA* victim = NULL;

  bool fPurge = FALSE;
  int sn = 0;
  int skills = 0;
  int spells = 0;

  if (cm == NULL){
    bug("check_promo_req: null cabal or member data.", 0);
    return FALSE;
  }
  else
    pCab = cm->pCabal;
  if (pCab->progress == 0)
    return TRUE;
  /* check if we need the character for anything */
  if (IS_SET(pCab->progress, PROGRESS_SKILL)
      || IS_SET(pCab->progress, PROGRESS_SPELL)
      || IS_SET(pCab->progress, PROGRESS_QUEST)){
    /* try to load the character */
    if ( (victim = char_file_active (cm->name)) == NULL){
      if ( (d = bring( get_room_index(ROOM_VNUM_LIMBO), cm->name )) != NULL){
	victim = d->character;
	fPurge = TRUE;
	victim->pcdata->roomnum *= -1;
      }
      else{
	bug("check_promo_req: character does not exist!", 0);
	return FALSE;
      }
    }
  }

  /* do the actual checks now */
  if (IS_SET(pCab->progress, PROGRESS_CP)){
    if (cm->cp < rank * 300){
      if (ch)
	sendf( ch, "[%s] requires %s to have %d %ss before being promoted.\n\r",
	       pCab->who_name,
	       cm->name,
	       rank * 300,
	       pCab->currency);
      if (fPurge)
	purge(victim);
      return FALSE;
    }
  }
  if (IS_SET(pCab->progress, PROGRESS_QUEST)){
    if (victim->pcdata->max_quests < (rank * 18 * mud_data.max_quest / 100)){
      if (ch)
	sendf( ch, "[%s] requires %s to have %d quests before being promoted.\n\r",
	       pCab->who_name,
	     cm->name,
	       rank * (18 * mud_data.max_quest / 100));
      if (fPurge)
	purge(victim);
      return FALSE;
    }
  }

  if (IS_SET(pCab->progress, PROGRESS_SKILL) || IS_SET(pCab->progress, PROGRESS_SPELL)){
    /* count skills/spells at this point */
    for ( sn = 0; sn < MAX_SKILL; sn++ ){
      SKILL_DATA* nsk = NULL;
      CSKILL_DATA* csk = NULL;
      sh_int learned = 0;
      
      if ( skill_table[sn].name == NULL )
	break;
      else if (skill_table[sn].skill_level[victim->class] > 50 
	       && (nsk = nskill_find(victim->pcdata->newskills,sn)) == NULL
	       && (csk = get_cskill(get_parent(victim->pCabal), sn)) == NULL)
	continue;
      /* for cabal skills check starting level */
      if (csk && csk->pSkill->learned >= 100)
	continue;
      if (nsk != NULL)
	learned = nsk->learned;
      else
	learned = victim->pcdata->learned[sn];
      if (learned < 100)
	continue;
      if (skill_table[sn].spell_fun == spell_null)
	skills++;
      else
	spells++;
    }
    
    if (IS_SET(pCab->progress, PROGRESS_SKILL)){
      int cnt = IS_SET(pCab->progress, PROGRESS_SPELL) ? skills + spells : skills;
      if (cnt < rank * 3){
	if (ch)
	  sendf( ch, "[%s] requires %s to have %d skills%s mastered before being promoted. (Current: %d)\n\r",
		 pCab->who_name,
		 cm->name,
		 rank * 3,
		 IS_SET(pCab->progress, PROGRESS_SPELL) ? " or spells" : "",
		 cnt);
	if (fPurge)
	  purge(victim);
	return FALSE;
      }
    }
    else if (IS_SET(pCab->progress, PROGRESS_SPELL)){
      int cnt = IS_SET(pCab->progress, PROGRESS_SKILL) ? skills + spells : spells;
      if (cnt < rank * 3){
	if (ch)
	  sendf( ch, "[%s] requires %s to have %d spells%s mastered before being promoted.(Current: %d)\n\r",
		 pCab->who_name,
		 cm->name,
		 rank * 3,
		 IS_SET(pCab->progress, PROGRESS_SKILL) ? " or skills" : "",
		 cnt);
	if (fPurge)
	  purge(victim);
	return FALSE;
      }
    }
  }
  if (IS_SET(pCab->progress, PROGRESS_CHALL)){
    int cnt = cm->kills;
    int req  = 10 * UMIN(2, rank);
    req += 20 * UMAX(0, rank - 2);
    
    if (cnt < req ){
      if (ch)
	sendf(ch, "[%s] requries %s to have %d more challenges.\n\r", 
	      pCab->who_name, 
	      cm->name, 
	      req - cm->kills);
      if (fPurge)
	purge(victim);
      return FALSE;
    }
  }
  
  /* All passed, purge character if we need to */
  if (fPurge)
    purge(victim);
  return TRUE;
}

/* runs through all the areas connected to the cabal and marks their distance from the cabal */
void CalcAreaDistances( AREA_DATA* origin, int start_distance, int cabal ){
  EXIT_DATA* pe, *pe_back;

  //ez case
  if (origin->cabal_distance[cabal] <= start_distance)
    return;
  else
    origin->cabal_distance[cabal] = start_distance;

  for (pe = origin->exits; pe; pe = pe->next_in_area ){
    if (pe->to_room == NULL || pe->to_room->area == NULL || pe->to_room->area == origin)
      continue;
    else if (IS_AREA(pe->to_room->area, AREA_RESTRICTED))
      continue;
    else if (IS_AREA(pe->to_room->area, AREA_NOPATH))
      continue;

    //make sure there is an exit back
    for (pe_back = pe->to_room->area->exits; pe_back; pe_back = pe_back->next_in_area){
      if (pe_back->to_room == NULL || pe_back->to_room->area == NULL)
	continue;
      if (pe_back->to_room->area == origin)
	break;
    }
    if (pe_back == NULL)
      continue;
      
    CalcAreaDistances( pe->to_room->area, start_distance + 1, cabal );
  }
}

void CalcCabalDistances(){
  CABAL_DATA* pCab;

  for (pCab = cabal_list; pCab; pCab = pCab->next){
    if (pCab->parent != NULL)
      continue;
    else if (pCab->anchor == NULL)
      continue;
    CalcAreaDistances( pCab->anchor->area, 1, pCab->vnum );
  }
}


void AreaInfluenceUpdate( AREA_DATA* area ){
  CABAL_DATA* pCab;
  EXIT_DATA* pe, *pe_back;
  int i, delta;
  int count = 0, tot_inf = 0;

  int high = 0, sec_high = 0, high_cab = 0, sec_high_cab = 0;

  //ez cases for areas not meant to be taken over
  if (IS_AREA(area, AREA_RESTRICTED))
    return;
  else if (IS_AREA(area, AREA_NOPATH))
    return;
  else if (IS_AREA(area, AREA_CABAL))
    return;

  /* PASS 0:
     - Count number of non zero cabal influences
     - Count total influence/dist of all cabals

     PASS 1:
     - Run through area exits that have cabal owners and are unmarked
     - Add influence according to formula
     - Mark the exit's area

     if not held by armies
     PASS 2:
     - Find highest and second highest inluence
     - Change alliegence if highest is 20% > then second highest
     
  */

  /* PASS 0 */
  for (pe = area->exits; pe; pe = pe->next_in_area ){
    if (pe->to_room == NULL || pe->to_room->area == NULL || pe->to_room->area == area)
      continue;
    else if (IS_AREA(pe->to_room->area, AREA_RESTRICTED))
      continue;
    else if (IS_AREA(pe->to_room->area, AREA_NOPATH))
      continue;
    else if ( (pCab = pe->to_room->area->pCabal) == NULL)
      continue;
    else if (IS_AREA(pe->to_room->area, AREA_MARKED))
      continue;
    else if (area->cabal_distance[pCab->vnum] == 999)
      continue;

    //make sure there is an exit back
    for (pe_back = pe->to_room->area->exits; pe_back; pe_back = pe_back->next_in_area){
      if (pe_back->to_room == NULL || pe_back->to_room->area == NULL)
	continue;
      if (pe_back->to_room->area == area)
	break;
    }
    if (pe_back == NULL)
      continue;
    //make sure to not count the same area twice
    SET_BIT(pe->to_room->area->area_flags, AREA_MARKED);

    count++;
    tot_inf += UMAX(0, (pCab->support[ECO_GAIN] - CABAL_FULL_SUPPORT_VAL)) / UMAX(1, area->cabal_distance[pCab->vnum]);
  }

  if (count < 1)
    return;
  else
    tot_inf /= count;

  //PASS 1
  for (pe = area->exits; pe; pe = pe->next_in_area ){
    bool fMark = FALSE;

    if (pe->to_room == NULL || pe->to_room->area == NULL || pe->to_room->area == area)
      continue;

    //fault toleran MARK remove
    if (!IS_AREA(pe->to_room->area, AREA_MARKED))
      continue;
    else{
      fMark = TRUE;
      REMOVE_BIT(pe->to_room->area->area_flags, AREA_MARKED);      
    }

    if (IS_AREA(pe->to_room->area, AREA_RESTRICTED))
      continue;
    else if (IS_AREA(pe->to_room->area, AREA_NOPATH))
      continue;
    else if ( (pCab = pe->to_room->area->pCabal) == NULL)
      continue;

    if (fMark){
      i = (pCab->support[ECO_GAIN] - CABAL_FULL_SUPPORT_VAL) / UMAX(1, area->cabal_distance[pCab->vnum]);
      delta = i - tot_inf + (i / count);
      area->cabal_influence[pCab->vnum] = URANGE(0, area->cabal_influence[pCab->vnum] + delta, 32767);
      
    }
  }

  /* check if the current cabal owner holds the area with armies */
  if (area->pCabal){
    CABAL_DATA* pc = get_parent( area->pCabal );
    TOWER_DATA* ptow;

/* check if we own majority */
    if ( (ptow = get_tower_data(pc, area)) != NULL
	 && (area->bastion_max - ptow->towers) < ptow->towers ){
      return;
    }
  }

 
  //Area not held by force, we check for influence 
  /* PASS 2 */
  for (i = 0; i < MAX_CABAL; i++){
    if (area->cabal_influence[i] > high){
      sec_high = high;
      sec_high_cab = high_cab;
      high = area->cabal_influence[i];
      high_cab = i;
    }
  }

  //No positive influence at all
  if (high_cab < 1){
    area->pCabal = NULL;
  }
  //Enough influence change
  else if (high > (2 * CABAL_FULL_SUPPORT_VAL) && high > 12 * sec_high / 10 && (area->pCabal == NULL || area->pCabal->vnum != high_cab)){
    CABAL_DATA* pCab = get_cabal_vnum( high_cab );
    /* check if this cabal already gained an area this cabal tick */
    if (pCab && !IS_CABAL2(pCab, CABAL2_AREAGAIN)){
      area_to_cabal(area, pCab, FALSE );
      SET_BIT(pCab->flags2, CABAL2_AREAGAIN);
    }
    return;
  }

  return;
}
/* --------------================CABAL POINT RELATED ROUTINES=======----------- */


/* IMPORTANT */
/* IN ORDER TO SCALE THE CABAL POINTS, THE AMOUNT OF CURRENCY		*/
/* A CHAR HAS (CP's) IS OBTAINED BY DIVIDING CPOINTS BY CPTS		*/
/* FROM HERE ON, CP's ARE ACUAL CURRENCY, CPOINTS IS THE LONG INT USED	*/
/* TO STORE THE UNDIVIDED TOTAL						*/

/* Written by: Virigoth							*
 * Returns: amount of cp's						*
 * Used: cabal.c, act_obj.c						*
 * Comment: Ratio of cp's to currency returned is controled by CP_RATIO */

inline int GET_CP(CHAR_DATA* ch){

  if (!ch){
    bug("GET_CP: argument NULL.", 0);
    return 0;
  }
  if (IS_NPC(ch) || !ch->pCabal)
    return 0;
  
  return ch->pcdata->cpoints / CPTS;
}

/* Written by: Virigoth							*
 * Returns: amount of cp's for cabals					*
 * Used: cabal.c, act_obj.c						*
 * Comment: Ratio of cp's to currency returned is controled by CP_RATIO */

inline int GET_CAB_CP(CABAL_DATA* pc){
  CABAL_DATA* pCab = get_parent( pc );
  if (!pCab){
    bug("GET_CAB_CP: argument NULL.", 0);
    return 0;
  }
  return pCab->cp / CPTS;
}

/* adds or removes support from cabal, returns current income */
int SUPPORT_GAIN( CABAL_DATA* pCabal, int eco_sector, int gain ){
  CABAL_DATA* pCab = get_parent( pCabal );

  if (pCab == NULL)
    return 0;
  
  pCab->support[eco_sector] += gain;
  if (eco_sector != ECO_GAIN)
    pCab->support[ECO_GAIN] += gain;

  return     pCab->support[ECO_GAIN];
}
    
  
/* Written by: Virigoth							*
 * Returns: amount of cpoints's char has after update.			*
 * Used: cabal.c, act_obj.c						*
 * Comment: amount returned is curent amount of CP's, if fshow the gain	*
 * is signaled to player. CP_GAIN takes amount in currency, CPS_GAIN in *
 * cpoints								*/

int CPS_GAIN(CHAR_DATA* ch, int gain, bool fshow){

  if (!ch){
    bug("CPS_GAIN: ch argument NULL.", 0);
    return 0;
  }
  if (IS_NPC(ch) || !ch->pCabal || gain == 0)
    return 0;

  if (fshow){
    if (abs(gain)  < CPTS){//if not a whole point
      if (gain > 0)
	sendf(ch, "You have gained some worth in service of %s.\n\r", 
	      ch->pCabal->who_name);
      else if (gain < 0)
	sendf(ch, "You have lost some worth in service of %s.\n\r", 
	      ch->pCabal->who_name);
    }
    else{
      if (gain > 0)
	sendf(ch, "You have gained %d %s%s in service of %s.\n\r", 
	      gain / CPTS,
	      ch->pCabal->currency,
	      (abs(gain) >= 2 * CPTS ? "s" : ""),
	      ch->pCabal->who_name);
      else if (gain < 0)
	sendf(ch, "You have lost %d %s%s in service of %s.\n\r", 
	      abs(gain / CPTS),
	      ch->pCabal->currency,
	      (abs(gain) >= 2 * CPTS ? "s" : ""),
	      ch->pCabal->who_name);

    }
  }
  ch->pcdata->cpoints = UMAX(0, ch->pcdata->cpoints + gain);
  if (ch->pcdata->member)
    ch->pcdata->member->cp = GET_CP(ch);
  return GET_CP(ch);
}

int CP_GAIN(CHAR_DATA* ch, int gain, bool fshow){
  return CPS_GAIN(ch, gain * CPTS, fshow);
}

/* adds gain in cp */
int CP_CAB_GAIN(CABAL_DATA* pc, int gain ){
  return CPS_CAB_GAIN( pc, gain * CPTS );
}

/* adds gain in cps, returns in cp */
int CPS_CAB_GAIN(CABAL_DATA* pc, int gain ){
  CABAL_DATA* pCab = get_parent(pc );

  pCab->cp +=  gain;

/* set ceiling based on coffer status */
  if (IS_CABAL(pCab, CABAL_COFFER) && pCab->cp > ECONOMY_COFF_MAX)
    pCab->cp = ECONOMY_COFF_MAX;
  else if (!IS_CABAL(pCab, CABAL_COFFER) && pCab->cp > ECONOMY_NOCOFF_MAX){
    pCab->cp -= gain;
    return (pCab->cp);
  }
  else if (pCab->cp < ECONOMY_COFF_MIN )
    pCab->cp = ECONOMY_COFF_MIN;
  
  return GET_CAB_CP( pCab );
}


/* returns cp cost of a skill when used in this room */
inline int get_cskill_cost( CHAR_DATA* ch, ROOM_INDEX_DATA* room, CSKILL_DATA* cSkill ){
  
  if (room == NULL || cSkill == NULL)
    return 1000;
  /* check here if the area belongs to the cabal of the player */
  /*  normal cost for non-owner areas, 1/2 cost for friendly/allied */
  if (room->area->pCabal && is_friendly(ch->pCabal, room->area->pCabal) == CABAL_FRIEND)
    return cSkill->cost / 2;
  else
    return cSkill->cost;
}
  

/* this function handles subtraction of cps when power is used */
void handle_skill_cost( CHAR_DATA* ch, CABAL_DATA* pCabal, int sn ){
  CSKILL_DATA* pSkill = get_cskill( pCabal, sn );

  if (pSkill == NULL)
    return;
  else
    CP_GAIN( ch, -get_cskill_cost(ch, ch->in_room, pSkill), FALSE );
}

/* this function handles subtraction of cps but with %cent multiplier of
cost, 100 = normal
*/
void handle_skill_cost_percent( CHAR_DATA* ch, CABAL_DATA* pCabal, int sn, int percent ){
  CSKILL_DATA* pSkill = get_cskill( pCabal, sn );

  if (pSkill == NULL)
    return;
  else
    CP_GAIN( ch, percent * -get_cskill_cost(ch, ch->in_room, pSkill) / 100, FALSE );
}


/* Written by: Virigoth							*
 * Returns: VOID							*
 * Used: cabal.c,							*
 * comment: Distributes "gain" CPS to members of "cabal". If "fScale"	*
 * "ecabal" presence is used to scale the points.			*/

void distribute_cps(CHAR_DATA* ch, CABAL_DATA* pCab, int init_gain, bool fScale){
  CHAR_DATA* vch;
//  CMEMBER_DATA* cm;
  CABAL_DATA* pCabal = pCab;
  int tax = init_gain > 0 ? pCab->cp_tax * init_gain / 100 : init_gain;	//amount of cps moved to coffers
  int gain = tax > 0 ? init_gain - tax : init_gain;			//amount of gain given to char
  int scaled_gain = gain;						//amount of gained distributed to members

  if (pCab == NULL)
    return;
  else if (init_gain == 0 )
    return;
  else{
    pCabal = get_parent( pCab );
  }

  if (fScale){
    scaled_gain /= UMAX(1, pCabal->present);
  }

/* add tax to the cabal coffers */    
  CPS_CAB_GAIN( pCabal, tax );

/* if ch is passed, then ch recives full gain, and rest players according to fScale */
  for ( vch = player_list; vch != NULL; vch = vch->next_player ){
    /* if link dead omit, not in cabal, or same ch omit */
    if (!IS_NPC(vch) && vch->desc 
	&& vch->pCabal 
	&& !IS_IMMORTAL(vch)
	&& is_same_cabal(pCabal, vch->pCabal)){
      if (ch && ch == vch)
	CPS_GAIN(vch, gain, TRUE);
      else
	CPS_GAIN(vch, scaled_gain, TRUE);
    }
  }
 /* now run through unpresent members and reward them with scaled_gain / total members */
  /* disabled Viri.
  for (cm = pCabal->members; cm; cm = cm->next){
    if (cm->connected == FALSE)
      cm->cp_pen += (scaled_gain / UMAX(1, pCabal->cur_member)) / CPTS;
  }
  */
}


/* Written by: Virigoth							*
 * Returns: amount of cps char recives on update.			*
 * Used: cabal.c, update.c						*
 * Comment: amount returned is in cps and used for scaling based on	*
 * number of cabal members on.						*/

int cp_calculate_timegain(CHAR_DATA* ch){
  int gain_mod = 100;   //base gain is 100% of basegain
  CABAL_DATA* pc = get_parent( ch->pCabal );

  /* safety */
  if (ch == NULL){
    bug("cp_calculate_gain: NULL argument passed.", 0);
    return 0;
  }
  /* safety checks */
  else if (IS_NPC(ch) || !ch->pCabal || !ch->desc || !ch->in_room || IS_IMMORTAL(ch))
    return 0;
  /* captured item results in 0 gain */
  if (is_captured(ch->pCabal))
    return 0;
  /* stance cancels time gain */
  else if (is_affected(ch, gsn_stance))
    return 0;
  /* abuse checks */
  else if (ch->timer >= 5  || ch->in_room->vnum == ROOM_VNUM_LIMBO)
    return 0;
  /* return the gain values in CPS */
  if (IS_CABAL(pc, CABAL_VENDETTA))
    gain_mod += 50;			//VENDETTA gives 50% boost to gain
  if (IS_CABAL(pc, CABAL_HIGHGAIN))	//Rooms boost gain by another 50%
    gain_mod += 50;
  //boost the cp gain by influence
  gain_mod = gain_mod * pc->support[ECO_GAIN] / CABAL_FULL_SUPPORT_VAL;

  return gain_mod * CP_BASEGAIN / 100;
}


/* cp rewards but for armies *
 * returns amount of cps used for reward
 */
int cp_event_army( ARMY_DATA* pa, CABAL_DATA* pEn, void* vo, int event ){
  CABAL_DATA* pc = get_parent(pa->pCabal);
  CMEMBER_DATA* cm;
  char buf[MSL];
  int gain;
  bool fTalk = FALSE;
  bool fScale = TRUE;

  if (pa == NULL){
    bug("cp_event_army: NULL army passed.", 0);
    return 0;
  }
  else if (event < CP_EVENT_MILITARY){
    bug("cp_event_army: Non-Military event (%d) passed.", event );
    return 0;
  }

  switch ( event ){
  default:
    bug("cp_Event_army: Unknown event. (%d)", event );
    return 0;
/* DESTRUCTION OF UNIT */
  case CP_EVENT_VICTORY:
    gain = CP_EVENT_VICTORY_COST;
    fScale = FALSE;
    fTalk = FALSE;
    if ( pEn )
      affect_cabal_relations(pc, pEn, -gain / 10, TRUE );
    break;
/* DESTRUCTION OF BASTION */
  case CP_EVENT_SIEGE:
    gain = CP_EVENT_SIEGE_COST;
    fScale = TRUE;
    fTalk = FALSE;
    if ( pEn )
      affect_cabal_relations(pc, pEn,  -gain / 10, TRUE );
    break;
/* CAPTURING NEUTRAL AREA */
  case CP_EVENT_AREA:
    gain = CP_EVENT_AREA_COST;
    fScale = TRUE;
    fTalk = TRUE;

    if (vo){
      AREA_DATA* area = (AREA_DATA*) vo;
      sprintf(buf, "Area secured: %s.", area->name );
    }
    else
      sprintf(buf, "Area secured.");
    break;
  case CP_EVENT_CONQUER:
    gain = CP_EVENT_CONQUER_COST;
    fScale = TRUE;
    fTalk = TRUE;

    if (vo && pEn){
      AREA_DATA* area = (AREA_DATA*) vo;
      /* Loser report */
      sprintf(buf, "Area Lost: %s taken by [%s].", area->name, pc->who_name );
      cabal_report( pEn, buf, REPORT_BAD, TRUE );

      /* winner report */
      sprintf(buf, "Area gained: %s taken from [%s].", area->name, pEn->who_name );      
    }
    else
      fTalk = FALSE;
    affect_cabal_relations(pc, pEn, -gain / 10, TRUE );
    break;
  }

/* check if pEn and ch->pCabal has a vendetta, if so give +50% */
  if (gain > 0 && pEn 
      && IS_CABAL(pc, CABAL_VENDETTA) 
      && get_pact(pEn, pc, PACT_VENDETTA, FALSE) != NULL)
    gain = 3 * gain / 2;

  /* if there is army data, then we put the reward in owner's cp_pen stash */
  if (pa && (cm = get_cmember(pa->commander, pc)) != NULL
      && !cm->connected){
    cm->cp_pen += gain / CPTS;
  }

/* we announce this to the cabal of ch */
  if (fTalk && !IS_NULLSTR( buf )){
    cabal_report( pc, buf, REPORT_GOOD, TRUE );
  }
  /* scaled distribution if fscale */
  if (gain != 0)
    distribute_cps(NULL, pc, gain, fScale );

  return gain;
}

/* Written by: Virigoth							*
 * Returns: cp's gained							*
 * Used: cabal.c, act_obj.c, fight.c					*
 * comment: ch is player who performed event, victim/obj used as	*
 * neccesary, event is one of events in cabal.h				*
 * The cabal relations are affected roughly by 1/10 the reward for event */

int cp_event(CHAR_DATA* ch, CHAR_DATA* victim, OBJ_DATA* obj, int event){
  /* calclate events based on even pased */
  CABAL_DATA* pc = get_parent( ch->pCabal );
  CABAL_DATA* pEn;	//pointer to enemy cabal, used at the end to check for vendetta bonus
  char buf[MIL];

  int gain = 0;
  bool fTalk = FALSE;
  bool fScale = TRUE;

  buf[0] = '\0';

  /* Safety checks */
  if ((event >= CP_EVENT_OBJ) && (event < CP_EVENT_CHAR) && (!obj)){
    bug("cp_event: obj NULL on object event.", 0);
    return 0;
  }
  else if (event >= CP_EVENT_CHAR && event < CP_EVENT_MILITARY && !victim){
    bug("cp_event: victim NULL on victim event.", 0);
    return 0;
  }
  else if (IS_NPC(ch) && event < CP_EVENT_MILITARY){
    bug("cp_event: NPC passed.", 0);
    return 0;
  }
  else if (event >= CP_EVENT_MILITARY){
    bug("cp_event: Military event passed.", 0);
    return 0;
  }
  else if (!pc){
    bug("cp_event: Character not cabaled.", 0);
    return 0;
  }
  /* no bonus for killing self etc. */
  else if (ch == victim)
    return 0;
  else if (IS_IMMORTAL(ch) || IS_IMMORTAL(victim))
    return 0;

  /* assign the enemy cabal based on obj or victim */
  pEn = victim ? get_parent(victim->pCabal) : obj ? get_parent(obj->pCabal) : NULL;

  /* start case by case process */
  switch (event){
/* BOUNTY REWARD */
  case CP_EVENT_BOUNTY:
    gain = CP_EVENT_BOUNTY_COST;
    if (victim->pCabal && IS_LEADER(victim))
      gain += CP_EVENT_LEADER_COST;
    else if (victim->pCabal && IS_ELDER(victim))
      gain += CP_EVENT_ELDER_COST;
    else if (victim->pCabal)
      gain += CP_EVENT_PC_COST;
    sprintf(buf, "%s has claimed a bounty!", PERS2(ch));
    fScale = TRUE;
    fTalk = TRUE;
    break;
/* KILL PC */
  case CP_EVENT_PC:
    sprintf(buf, "%s has triumphed over %s!", PERS2(ch), PERS2(victim));
    if (IS_LEADER(victim))
      gain = CP_EVENT_LEADER_COST;
    else if (IS_ELDER(victim))
      gain = CP_EVENT_ELDER_COST;
    else 
      gain = CP_EVENT_PC_COST;
    fTalk = TRUE;
    fScale = TRUE;

    /* if this is self defence of bountied person vs. rouge we do not do this. */
    if (victim->pcdata->pbounty != NULL && IS_CABAL(ch->pCabal, CABAL_ROUGE))
      break;
    if (ch->pcdata->pbounty != NULL && IS_CABAL(victim->pCabal, CABAL_ROUGE))
      break;

    if ( pEn ){
      bool fLaw = FALSE;
      if ((IS_WANTED(victim) || IS_OUTLAW(victim)) 
	  && (ch->pCabal && IS_CABAL(ch->pCabal, CABAL_JUSTICE)) ){
	fLaw = TRUE;
      }
      /* check if we have a NA-PACT or an ALLIANCE */
      if (!fLaw && IS_CABAL( pc, CABAL_NAPACT) && get_pact(pEn, pc, PACT_NAPACT, FALSE) != NULL){
	char buf[MIL];
	sprintf( buf, "%s has violated the Non-Aggression Pact between %s and %s!", ch->name, pc->who_name, pEn->who_name );
	cabal_echo( pc, buf );
	cabal_echo( pEn, buf );
	reward( pc, -gain / CPTS );
	affect_cabal_relations(pc, pEn, -gain / CPTS, TRUE );
	fTalk = FALSE;

	/* if person is out of cps we demote them */
	/*we subtract the gain since reward takes effect on tick, not instantenously */
	if ((GET_CP( ch ) - gain/CPTS)  < 1 && ch->pcdata->rank > RANK_NEWBIE ){
	  char sub[MIL];
	  sprintf( buf, "Due to disregard for peace treaties, %s has been demoted.", PERS2(ch) );
	  cabal_echo( pc, buf );
	  strcat( buf, "\n\r");
	  sprintf( sub, "%s's demotion.", PERS2(ch));
	  do_hal( pc->name, sub, buf, NOTE_NEWS );
	  promote( ch, ch->pCabal, -1 );
	  if (ch->pcdata->member)
	    ch->pcdata->member->time_stamp = mud_data.current_time;
	}
	gain = 0;
      }
      else if (!fLaw && IS_CABAL( pc, CABAL_ALLIANCE) && get_pact(pEn, pc, PACT_ALLIANCE, FALSE) != NULL){
	char buf[MIL];
	sprintf( buf, "%s has violated the Alliance betwen %s and %s!", ch->name, pEn->who_name, pc->who_name );
	cabal_echo( pc, buf );
	cabal_echo( pEn, buf );
	reward( pc, -2* gain / CPTS );
	affect_cabal_relations(pc, pEn, -2 * gain / CPTS, TRUE );
	fTalk = FALSE;

	/* if person is out of cps we demote them */
	/*we subtract the gain since reward takes effect on tick, not instantenously */
	if ( (GET_CP( ch ) - gain/CPTS)  < 1 && ch->pcdata->rank > RANK_NEWBIE ){
	  char sub[MIL];
	  sprintf( buf, "Due to disregard for peace treaties, %s has been demoted.", PERS2(ch) );
	  cabal_echo( pc, buf );
	  strcat( buf, "\n\r");
	  sprintf( sub, "%s's demotion.", PERS2(ch));
	  do_hal( pc->name, sub, buf, NOTE_NEWS );
	  promote( ch, ch->pCabal, -1 );
	  if (ch->pcdata->member)
	    ch->pcdata->member->time_stamp = mud_data.current_time;
	}
	gain = 0;
      }
      else{
	affect_cabal_relations(pc, pEn, -4 * gain / CPTS, !fLaw );
      }
    }
    /* check of 1/2 gain for ROUGE killing cabal pcs since they also get bounties */
    if (IS_CABAL(pc, CABAL_ROUGE) && gain > 0)
      gain /= 3;
    break;
/* WANTED APPREHENSION */
  case CP_EVENT_CAPTURE:
    sprintf(buf, "%s has captured %s!", PERS2(ch), PERS2(victim));
    gain = (URANGE(1, victim->pcdata->flagged, 20)) * CP_EVENT_CAPTURE_COST / 10;
    fScale = TRUE;
    fTalk = TRUE;
    break;
/* WANTED FLAG CORRECT */
  case CP_EVENT_WANTED:
    gain = CP_EVENT_WANTED_COST;
    fScale = TRUE;
    fTalk = FALSE;
    if ( pEn )
      affect_cabal_relations(pc, pEn, -gain / (10 * CPTS), TRUE );
    break;
/* WANTED FLAG PENALTY (not correct) */
  case CP_EVENT_WANTED_FALSE:
    gain = (1 + ch->pcdata->rank) * CP_EVENT_WAN_PEN_COST;	//this is a negative value
    fScale = TRUE;
    fTalk = FALSE;
    break;
    if ( pEn )
      affect_cabal_relations(pc, pEn, gain / (10 * CPTS), TRUE );
    break;
  case CP_EVENT_STANDARD:
    gain = CP_EVENT_STANDARD_COST;
    fScale = TRUE;
    fTalk = FALSE;
    if ( pEn )
      affect_cabal_relations(pc, pEn, -gain / (10 * CPTS), TRUE );
    break;
  case CP_EVENT_RETURN:
    gain = CP_EVENT_RETURN_COST;
    fScale = TRUE;
    fTalk = FALSE;
    break;
  default:
    break;
  }


/* check if pEn and ch->pCabal has a vendetta, if so give +50% to the gains
*/
  if (gain > 0 && pEn && IS_CABAL(ch->pCabal, CABAL_VENDETTA) && get_pact(pEn, ch->pCabal, PACT_VENDETTA, FALSE) != NULL)
    gain = 3 * gain / 2;

  //give bonus penalty etc.
  pc->bonus_sup += gain / 60;
  if (pEn)
    pEn->bonus_sup -= gain / 60;

/* we announce this to the cabal of ch */
  if (fTalk && !IS_NULLSTR( buf )){
    cabal_echo(  ch->pCabal, buf );
  }
  /* scaled distribution if fscale */
  if (gain != 0)
    distribute_cps(ch, get_parent(ch->pCabal), gain, fScale );
  return gain;
}

/* ---------======== PACT ORIENTATED STUFF =======--------- */
/* write a pact to a file */
void save_pact( FILE* fp, PACT_DATA* pp ){
  char buf[MIL];
/* type, complete, rating, cre_vnu, bene_vnum, c_cp, c_sup, b_cp, b_sup, time_stamp */
  fprintf( fp, "#%d %s ", PACT_MARKER, fwrite_flag( pp->type, buf ) );
  fprintf( fp, "%s %d %d %d %d %d %d %d %d %ld\n",
	   fwrite_flag( pp->Adv, buf ),
	   pp->complete,
	   pp->rating,
	   pp->creator->vnum,
	   pp->benefactor->vnum,
	   pp->c_cp, pp->c_sup,
	   pp->b_cp, pp->b_sup,
	   pp->time_stamp );
}

bool read_pact( FILE* fp, PACT_DATA* pp ){
  int vnum = 0;
  int fSuccess = TRUE;

  pp->type		= fread_flag ( fp );
  pp->Adv		= fread_flag ( fp );
  pp->complete		= fread_number ( fp );
  pp->rating		= fread_number ( fp );

  vnum			= fread_number ( fp );
  if ( (pp->creator = get_cabal_vnum ( vnum )) == NULL){
    bug("read_pact: could not get creator cabal vnum %d", vnum );
    fSuccess = FALSE;
  }
  vnum			= fread_number ( fp );
  if ( (pp->benefactor = get_cabal_vnum ( vnum )) == NULL){
    bug("read_pact: could not get benefactor cabal vnum %d", vnum );
    fSuccess = FALSE;
  }

  pp->c_cp		= fread_number ( fp );
  pp->c_sup		= fread_number ( fp );
  pp->b_cp		= fread_number ( fp );
  pp->b_sup		= fread_number ( fp );
  pp->time_stamp	= fread_number ( fp );

  return fSuccess;
}

/* saves all pacts this MUST be written AFTER cabals */
void save_pacts( ){
  PACT_DATA* pp = pact_list;
  FILE* fp;
  char path[MIL];

  /* save the cabal pacts */
  fclose (fpReserve);
  sprintf( path, "%s%s", SAVECAB_DIR, PACT_FILE );
  if ( ( fp = fopen( path, "w" ) ) == NULL ){
    perror( path );
    fpReserve = fopen( NULL_FILE, "r" );
    fclose (fp);
  }

  for (; pp; pp = pp->next ){
    save_pact( fp, pp );
  }
/* terminate the file */
  fprintf( fp, "#0" );


  fclose (fp);
  fpReserve = fopen( NULL_FILE, "r" );

}

PACT_DATA *new_pact(void){

  PACT_DATA *pp = malloc( sizeof( *pp ));
  
/* reset initial things */
  pp->next		= NULL;
  pp->type		= PACT_TRADE;
  pp->complete		= PACT_NONE;
  pp->Adv		= 0;

  pp->creator		= NULL;
  pp->benefactor	= NULL;

  return pp;
};

void free_pact( PACT_DATA* pp ){

  free( pp );
}


/* creates a pact given a type, and two cabal vnums */
PACT_DATA* create_pact(CABAL_DATA* pCre, CABAL_DATA* pBen, int type ){
  PACT_DATA* pp = NULL;

  if ( pCre == NULL ){
    bug("create_pact: null creator", 0);
    return pp;
  }
  else if ( pBen == NULL){
    bug("create_pact: null benefactor.", 0);
    return pp;
  }
  else if ( (pp = new_pact()) == NULL){
    bug("create_pact: could not get a new pact.", 0);
    return pp;
  }

  pp->type		= type;
  pp->creator		= pCre;
  pp->benefactor	= pBen;
  pp->c_cp		= 0;
  pp->b_cp		= 0;
  pp->c_sup		= 0;
  pp->b_sup		= 0;
  pp->rating		= PACT_START_RATING;
  pp->complete		= PACT_NONE;
  pp->time_stamp	= mud_data.current_time;

  return pp;
}


PACT_DATA* create_pact_vnum( int cre, int ben, int type ){
  CABAL_DATA* pCre, *pBen;

  if ( (pCre = get_cabal_vnum( cre )) == NULL){
    bug("create_pact_vnum: could not get creator vnum %d.", cre );
    return NULL;
  }
  else if ( (pBen = get_cabal_vnum( ben )) == NULL){
    bug("create_pact_vnum: could not get benefactor vnum %d.", cre );
    return NULL;
  }
  else
    return create_pact( pCre, pBen, type );
}

/* sets proper pact flags based on pact type, if the vote is complete */
void set_pact_flags( PACT_DATA* pp, bool fRemove ){
  int flags = 0;
  if (pp->complete != PACT_COMPLETE)
    return;
  else if (IS_SET(pp->type, PACT_TRADE))
    flags |= CABAL_TRADE;
  else if (IS_SET(pp->type, PACT_NAPACT)){
    flags |= CABAL_NAPACT;
    /* check if one of the cabals was a rouge cabal, then we add the spy flag */
    if (pp->creator 
	&& pp->benefactor 
	&& (IS_CABAL(pp->creator, CABAL_ROUGE) || IS_CABAL(pp->benefactor, CABAL_ROUGE)) )
      flags |= CABAL_SPIES;
  }
  else if (IS_SET(pp->type, PACT_ALLIANCE)){
    flags |= CABAL_ALLIANCE;
    /* check if one of the cabals was a rouge cabal, then we add the spy flag */
    if (pp->creator 
	&& pp->benefactor 
	&& (IS_CABAL(pp->creator, CABAL_ROUGE) || IS_CABAL(pp->benefactor, CABAL_ROUGE)) )
      flags |= CABAL_SPIES;
  }
  else if (IS_SET(pp->type, PACT_VENDETTA))
    flags |= CABAL_VENDETTA;

  if (pp->creator)
    fRemove ? REMOVE_BIT(pp->creator->flags, flags) : SET_BIT(pp->creator->flags, flags);
  if (pp->benefactor)
    fRemove ? REMOVE_BIT(pp->benefactor->flags, flags) : SET_BIT(pp->benefactor->flags, flags);

  /* AREA flags */
  if (IS_SET(flags, CABAL_TRADE) || IS_SET(flags, CABAL_NAPACT) || IS_SET(flags, CABAL_ALLIANCE) ){
    if ( IS_CABAL(pp->creator, CABAL_ROYAL) && IS_CABAL(pp->benefactor, CABAL_JUSTICE) && pp->creator->city){
      fRemove ? REMOVE_BIT(pp->creator->city->area_flags, AREA_LAWFUL) : SET_BIT(pp->creator->cit

Showing the first 100 KB of 224.8 KB. Download the archive for the full file.

Site Stats

Snippets: 1791
Codebases: 571