/
roa/
roa/lib/boards/
roa/lib/config/
roa/lib/edits/
roa/lib/help/
roa/lib/misc/
roa/lib/plrobjs/
roa/lib/quests/
roa/lib/socials/
roa/lib/www/
roa/lib/www/LEDSign/
roa/lib/www/LEDSign/fonts/
roa/lib/www/LEDSign/scripts/
roa/src/s_inc/
roa/src/sclient/
roa/src/sclient/binary/
roa/src/sclient/text/
roa/src/util/
/************************************************************************
	Realms of Aurealis 		James Rhone aka Vall of RoA

autowiz.c				Autowiz code from 2.2 moved into
                                        the mud's process since it was
                                        blocking on the system call anyway.

		******** Heavily modified and expanded ********
		*** BE AWARE OF ALL RIGHTS AND RESERVATIONS ***
		******** Heavily modified and expanded ********
		        All rights reserved henceforth. 

    Please note that no guarantees are associated with any code from
Realms of Aurealis.  All code which has been released to the general
public has been done so with an 'as is' pretense.  RoA is based on both
Diku and CircleMUD and ALL licenses from both *MUST* be adhered to as well
as the RoA license.   *** Read, Learn, Understand, Improve ***
*************************************************************************/
#include "conf.h"
#include "sysdep.h"

#include "structures.h"
#include "utils.h"
#include "comm.h"
#include "interpreter.h"
#include "acmd.h"
#include "handler.h"
#include "db.h"
#include "lists.h"
#include "global.h"

#define IMM_LMARG "   "
#define IMM_NSIZE  16
#define LINE_LEN   64
#define MIN_LEVEL LEV_IMM

// max level that should be in columns instead of centered 
#define COL_LEVEL LEV_IMM

// local prototypes...
void	add_name(byte level, char *name);

// local structures and variables...
struct name_rec {
   char	name[NAME_LENGTH+5];
   struct name_rec *next;
};

struct control_rec {
   int level;
   char	*level_name;
};

struct level_rec {
   struct control_rec *params;
   struct level_rec *next;
   struct name_rec *names;
};

struct control_rec level_params[] = {
   { LEV_IMM,  0},
   { LEV_GOD,  0},
   { LEV_AIMP, 0},
   { LEV_CIMP, 0},
   { LEV_IMPL, 0},
   { 0, "" }
};

struct level_rec *levels = NULL;

void wipe_names(struct name_rec *head)
{
  struct name_rec *nm, *temp;

  while((nm = head))
  {
    REMOVE_FROM_LIST(nm, head, next);
    FREENULL(nm);
  }
}

void wipe_levels(void)
{
  struct level_rec *lev, *temp;

  while((lev = levels))
  {
    REMOVE_FROM_LIST(lev, levels, next);
    wipe_names(lev->names);
    FREENULL(lev);
  }
  levels = NULL;
}

void	initialize(void)
{
  struct level_rec *tmp;
  int i = 0;

  // better wipe this if it already exists... 4/9/98 -jtrhone
  if (levels)
    wipe_levels();

  while (level_params[i].level > 0) 
  {
    // must init level names  2/4/98 -jtrhone
    switch (i+LEV_IMM) {
      case LEV_IMPL:
        level_params[i].level_name = impname;
        break;
      case LEV_CIMP:
        level_params[i].level_name = cimpname;
        break;
      case LEV_AIMP:
        level_params[i].level_name = aimpname;
        break;
      case LEV_GOD:
        level_params[i].level_name = godname;
        break;
      case LEV_IMM:
        level_params[i].level_name = avtrname;
        break;
      default: break;
    }

    CREATE(tmp, struct level_rec, 1);
    tmp->names = NULL;
    tmp->params = &(level_params[i++]);
    tmp->next = levels;
    levels = tmp;
  }
}

// parse the player_file adding each as we go along...
void	read_file(void)
{
  struct char_file_u player;

  rewind(player_fl);
  while (!feof(player_fl)) 
  {
    fread(&player, sizeof(struct char_file_u ), 1, player_fl);
    if (!feof(player_fl) && player.level >= MIN_LEVEL && 
        !(IS_SET(player.saved.player_flags, PLR_FROZEN)) && 
        !(IS_SET(player.saved.player_flags, PLR_NOWIZLIST)) && 
        !(IS_SET(player.saved.player_flags, PLR_DELETED)))
      add_name(player.level, player.name);
  }

  // put it back...
  rewind(player_fl);
}

// add a name to the structures...
void	add_name(byte level, char *name)
{
  struct name_rec *tmp;
  struct level_rec *curr_level;
  char	*ptr;

  if (!*name)
    return;

  for (ptr = name; *ptr; ptr++)
    if (!isalpha(*ptr))
      return;
   
  CREATE(tmp, struct name_rec, 1);
  strcpy(tmp->name, name);
  tmp->next = 0;

  curr_level = levels;
  while (curr_level->params->level > level)
    curr_level = curr_level->next;

  tmp->next = curr_level->names;
  curr_level->names = tmp;
}

// sort em based on level...
void	sort_names(void)
{
  struct level_rec *curr_level;
  struct name_rec *a, *b;
  char	temp[100];

  for (curr_level = levels; curr_level; curr_level = curr_level->next) 
    for (a = curr_level->names; a && a->next; a = a->next) 
      for (b = a->next; b; b = b->next) 
        if (strcmp(a->name, b->name) > 0) 
        {
	  strcpy(temp, a->name);
	  strcpy(a->name, b->name);
	  strcpy(b->name, temp);
	}
}

// ok, write the file out now...
void	write_wizlist(FILE *out, int minlev, int maxlev)
{
  struct level_rec *curr_level;
  struct name_rec *curr_name;
  int	i, j;

  fprintf(out,
      "****************************************************************************\n"
      "  The following people have reached immortality on %s.      \n"
      "  Each should be treated with respect and awe.  Occasional prayers to them  \n"
      "  are advisable and annoying them is not recommended.  Stealing from them   \n"
      "  or lying to them is cause for immediate punishment.                       \n"
      "****************************************************************************\n\n",
      longmudname);

  for (curr_level = levels; curr_level; curr_level = curr_level->next) 
  {
    if (curr_level->params->level < minlev || curr_level->params->level > maxlev)
      continue;

    i = 39 - (strlen(curr_level->params->level_name) >> 1);

    for (j = 1; j <= i; j++) 
      fputc(' ', out);

    fprintf(out, "%s\n", curr_level->params->level_name);

    for (j = 1; j <= i; j++) 
      fputc(' ', out);

    for (j = 1; j <= strlen(curr_level->params->level_name); j++)
      fputc('~', out);

    fprintf(out, "\n");

    strcpy(buf, "");
    curr_name = curr_level->names;
    while (curr_name) 
    {
      strcat(buf, curr_name->name);
      if (strlen(buf) > LINE_LEN) 
      {
        if (curr_level->params->level <= COL_LEVEL)
          fprintf(out, IMM_LMARG);
        else 
        {
          i = 40 - (strlen(buf) >> 1);
          for (j = 1; j <= i; j++) 
	    fputc(' ', out);
        }

        fprintf(out, "%s\n", buf);
        strcpy(buf, "");
      } 
      else 
      {
	if (curr_level->params->level <= COL_LEVEL) 
          for (j = 1; j <= (IMM_NSIZE - strlen(curr_name->name)); j++)
            strcat(buf, " ");

	if (curr_level->params->level > COL_LEVEL)
	  strcat(buf, "   ");
      }
      curr_name = curr_name->next;
    }

    if (*buf) 
    {
      if (curr_level->params->level <= COL_LEVEL)
        fprintf(out, "%s%s\n", IMM_LMARG, buf);
      else 
      {
         i = 40 - (strlen(buf) >> 1);
         for (j = 1; j <= i; j++) 
           fputc(' ', out);
         fprintf(out, "%s\n", buf);
      }
    }

    fprintf(out, "\n");
  }
}

// the actual call....from db.c
BOOL main_wizlist_update(void)
{
  FILE *fl;

  initialize();
  read_file();
  sort_names();

  if (!(fl = fopen(WIZLIST_FILE, "wt")))
  {
    mudlog("SYSERR: Unable to open WIZLIST_FILE for writing.", BRF, LEV_IMM, TRUE);
    return FALSE;
  }

  write_wizlist(fl, min_wizlist_lev, LEV_IMPL);
  fclose(fl);

  if (!(fl = fopen(IMMLIST_FILE, "wt")))
  {
    mudlog("SYSERR: Unable to open IMMLIST_FILE for writing.", BRF, LEV_IMM, TRUE);
    return FALSE;
  }

  write_wizlist(fl, LEV_IMM, min_wizlist_lev - 1);
  fclose(fl);

  // better wipe this now.. no residue... 4/9/98 -jtrhone
  if (levels)
    wipe_levels();

  return TRUE;
}