/
area/city/
area/crypts/
area/guilds/
area/psuedowild/
area/religion/
data/documents/MPDocs/
data/html/
data/mobprogs/
data/quest/
data/world/
data/world/_utilities/
data/world/images/
design/html/
notes/
player/
#include <sys/types.h>
#include <sys/time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "merc.h"
#include "recycle.h"
#include "religion.h"

extern char str_empty[1];

RELIGION_DATA * religion_list[MAX_RELIGIONS];
RELIGION_DATA *religion_free;

const struct religion_type religion_table[] =
{
    {"Agnostic",                "Agnostic",     "agnostic",     117},   /* No Religion      */
    {"The Blue Flame",          "Kuan Yin",     "kuanyin",      9004},  /* Lawful Good      */
    {"The Dogma of Anu",        "Anu",          "anu",          9034},  /* Lawful Neutral   */
    {"The Druaga Sect",         "Druaga",       "druaga",       9054},  /* Lawful Evil      */
    {"The Order of Ishtar",     "Ishtar",       "ishtar",       117},   /* Neutral Good     */
    {"Seekers of Nergal",       "Nergal",       "nergal",       117},   /* True Neutral     */
    {"The Warriors of Dahak",   "Dahak",        "dahak",        117},   /* Neutral Evil     */
    {"The Belivers of Ramman",  "Ramman",       "ramman",       117},   /* Chaotic Good     */
    {"The Followers of Marduk", "Marduk",       "marduk",       117},   /* Chaotic Neutral  */
    {"The Communion of Anshar", "Anshar",       "anshar",       117},   /* Chaotic Evil     */
    {NULL, NULL},
};

const struct religionr_type religionr_table[] =
{
    {"Inititate"},
    {"Acolyte"},
    {"Bishop"},
    {"Archon"},
    {"Chosen One"},
    {NULL},
};


/*
 * new_religion()
 *
 * Allocates a new religion
 */
RELIGION_DATA *new_religion(void)
{
    static RELIGION_DATA religion_zero;
    RELIGION_DATA *religion;

    if (religion_free == NULL)
    {
        religion = alloc_perm(sizeof(*religion));
    }
    else
    {
        religion = religion_free;
        religion_free = religion_free->next;
    }

    *religion = religion_zero;
    VALIDATE(religion);
    religion->name = &str_empty[0];
    return religion;
}


/*
 * free_religion()
 *
 * Frees a religion's data
 */
void free_religion(RELIGION_DATA *religion)
{
    if (!IS_VALID(religion))
        return;

    free_string(religion->name);
    INVALIDATE(religion);

    religion->next = religion_free;
    religion_free = religion;
}


/*
 * save_religion()
 *
 * Saves a specific religion to file
 */
void save_religion(int religion)
{
    FILE *fp;
    char buf[MAX_INPUT_LENGTH];
    RELIGION_DATA * out_list;

    if (religion < 0 || religion >= MAX_RELIGIONS)
        return;
    /* filename */
    sprintf(buf,"../data/religion/%s.data", religion_table[religion].note_name);

    fclose( fpReserve );
    if ( ( fp = fopen( buf, "w" ) ) == NULL )
    {
        perror( buf );
    }

    for ( out_list = religion_list[religion]; out_list != NULL; out_list  = out_list->next)
    {
      fprintf(fp,"%d %s~\n",out_list->rank, out_list->name);
    }

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

/*
 * load_religions()
 *
 * Top-level load function
 */
void load_religions(void)
{
    FILE *fp;
    char buf[MAX_INPUT_LENGTH];
    RELIGION_DATA * in_list;
    int religion, temprank;
    
    //log_string("Loading religions");

    for (religion = 0; religion < MAX_RELIGIONS; religion++)
    {
      religion_list[religion] = NULL;

      //sprintf(buf, "Loading %s", religion_table[religion].note_name);
      //log_string( buf);
      /* filename */
      sprintf(buf,"../data/religion/%s.data", religion_table[religion].note_name);

      fclose( fpReserve );

      if ( ( fp = fopen( buf, "r" ) ) == NULL )
        {
        fpReserve = fopen( NULL_FILE, "r" );
        continue;
        }

      for ( ; ; )
      {
        if ( feof(fp) )
        {
          break;
        }
          temprank = fread_number(fp);
          if (temprank < 999)
          {
            in_list = new_religion();
             in_list->rank = temprank;
             in_list->name = str_dup(fread_string(fp));
            in_list->next = religion_list[religion];
            religion_list[religion] = in_list;
          }
          else
        {
          break;
        }
      }
      fclose( fp );
      fpReserve = fopen( NULL_FILE, "r" );
    }
    return;
}

/*
 * check_religion()
 *
 */
void check_religion(char * name, int religion, int rank)
{
    int i;
    bool changed = FALSE;
    bool found = FALSE;
    RELIGION_DATA * temp1;
    RELIGION_DATA * temp2;

    for ( i = 0 ; i < MAX_RELIGIONS ; i++)
    {
        temp1 = religion_list[i];
        if (temp1 == NULL) /* Need to look-ahead 1 so do this check first */
        {
          if (i == religion)
          {
            temp1 = new_religion();
            temp1->name = str_dup( name );
            temp1->rank = rank;
            temp1->next = religion_list[i];
            religion_list[i] = temp1;
            changed = TRUE;
            found = TRUE;
            save_religion ( religion );
            break;
          }
        }
        else
        if (!str_cmp(temp1->name,name)) /* compare with first on list */
        {
          found = TRUE;
          if (i == religion )
          {
            if (temp1->rank != rank)
            {
              temp1->rank = rank;
              changed = TRUE;
            }
          }
          else
          {
            temp2 = religion_list[i]; /* placeholder */
            religion_list[i] = religion_list[i]->next; /* drop from list */
            free_religion(temp2); /* recycle the memory */
            changed = TRUE;
          }
        }
        else
        for ( ; temp1->next != NULL; temp1 = temp1->next)
        {
          if (i == religion )
          { /* in religion, make sure in list */
            if (!str_cmp(temp1->next->name,name))
            {
              if (temp1->next->rank != rank)
              {
                  temp1->next->rank = rank;
                  changed = TRUE;
              }
                found = TRUE;
              break;
            }
          }
          else
          { /* not in religion, make sure not in list */
            if (!str_cmp(temp1->next->name,name))
            {
                temp2 = temp1->next; /* placeholder */
                temp1->next = temp2->next; /* drop from list */
                free_religion(temp2); /* recycle the memory */
                changed = TRUE;
              break;
            }
          }
        }
        if (!found && (i == religion )) /* need to add */
        {
          temp1 = new_religion();
          temp1->name = str_dup( name );
          temp1->rank = rank;
          temp1->next = religion_list[i];
          religion_list[i] = temp1;
          changed = TRUE;
        }
        if (changed)
          save_religion ( i );
    }

}


/*
 * members_of_religion()
 */
/* returns the first max_members players of rank "rank" in religion "religion"
   and stores in a variable called members */
/* members must be initialized prior to calling this function, it will strcat
   onto the passed variable */

void members_of_religion(char * members, int religion, int rank, int max_members)
{
    RELIGION_DATA  * temp1;
        int count = 0;

    temp1 = religion_list[religion];
    for ( ; temp1 != NULL; temp1 = temp1->next)
        {
          if (strlen(members) > MAX_STRING_LENGTH - 20)
            break;
          if (temp1->rank == rank)
          {
            strcat( members, temp1->name );
            strcat( members, ", ");
            if (++count >= max_members)
              break;
          }
        }
    if (strlen(members) > 2 && members[strlen(members) - 2] == ',')
        members[strlen(members) - 2] = '\0';
}

/*
 * do_roster()
 *
 * Player function to see whos in each religion
 */
void do_roster(CHAR_DATA *ch, char *argument)
{
  static char arg[MAX_INPUT_LENGTH];
  static char buf[2*MAX_STRING_LENGTH];
  int i;

 argument = one_argument(argument,arg);

  if ( (arg[0] == '\0') )
   {
    send_to_char("Syntax:  roster <religion>\n\r", ch );
    return;
   }

 for (i=0;i < MAX_RELIGIONS;i++)
  {
    if ( (!str_prefix(arg, religion_table[i].note_name) ) )
     {
      break;
     }
  }

  if ( i >= MAX_RELIGIONS )
   {
    send_to_char("That's not a religion!\n\r", ch );
    return;
   }

        sprintf(buf,"The following list displays the current membership of %s:\n\r",religion_table[i].name_of_church);
      send_to_char(buf, ch);

        sprintf(buf,"{yOfficers{x\n\r");
      send_to_char(buf, ch);

      sprintf(buf, "{D   Chosen One{x: ");
      members_of_religion(buf, i, 4, 1 );
      send_to_char(buf,ch);
      send_to_char("\n\r", ch);

      sprintf(buf, "{B       Archon{x: ");
      members_of_religion(buf, i, 3, 6 );
      send_to_char(buf,ch);
      send_to_char("\n\r", ch);

      sprintf(buf, "{G       Bishop{x: ");
      members_of_religion(buf, i, 2, 6 );
      send_to_char(buf,ch);
      send_to_char("\n\r", ch);

      sprintf(buf, "{w      Acolyte{x: ");
      members_of_religion(buf, i, 1, 8);
      send_to_char(buf,ch);
      send_to_char("\n\r", ch);

      sprintf(buf, "{yMembers{x\n\r ");
      members_of_religion(buf, i, 0, 999);
      send_to_char(buf,ch);
      send_to_char("\n\r", ch);
}

/*
 * rosterpurge()
 *
 * Admin command to clean up religions
 */
void do_rosterpurge(CHAR_DATA *ch, char *argument)
{
    static char arg[MAX_INPUT_LENGTH];
    static char buf[MAX_INPUT_LENGTH];
    RELIGION_DATA  * temp1;
    RELIGION_DATA  * temp1_next;
    int i;
    FILE *in;

    one_argument(argument,arg);

    if ( (arg[0] == '\0') )
    {
      send_to_char("Syntax:  rosterpurge <religion>\n\r", ch );
      return;
    }

    for (i=0;i < MAX_RELIGIONS;i++)
    {
      if ( (!str_prefix(arg, religion_table[i].name_of_church) ) )
      {
        break;
      }
    }

    if ( i >= MAX_RELIGIONS )
    {
      send_to_char("That's not a religion!\n\r", ch );
      return;
    }

    temp1 = religion_list[i];
    for ( ; temp1 != NULL; temp1 = temp1_next)
    {
      temp1_next = temp1->next;
      sprintf(buf, "../player/%s", capitalize(temp1->name));
      if((in=fopen(buf, "r"))==NULL)
      {
        check_religion(temp1->name,-1,0);
      }
      else
        fclose(in);
    }
    send_to_char("Roster purged of nonexistent players.\n\r", ch );
}