/* Personality module for MCM 1.1e */

/* Modify the routines in this module to suit your needs, but make */
/* sure that you include all of the listed subroutines.  If you send */
/* me (msawyer@mael.soest.hawaii.edu) your personality files, I will */
/* try to get as many included in the distribution as possible, but */
/* make no promises.  You may redistribute modified versions of this */
/* file. */

#include "mcm.h"

/* This is a personality module for *D&D games, as an example */

 /* Need to initialize some static variables */

int rollmode;
int modif;
void per_init()
{
  modif=0;
  rollmode=0;
}

 /* Define a new signal processor for SIG_USR2.  There really isn't */
 /* that much need for this, but it is here in case you want it. */

void per_sig2()
{
}

 /* Define a routine to be run once per each time step */
 
void per_timestep()
{
}
 
 /* Define a pre-processor for the tell() call.  You may modify the */
 /* system and/or text fields, and should return TRUE if you want the */
 /* tell() to continue, or FALSE to fall out. */

int per_tell(msgfrom, msgto, system, text)
int msgfrom, msgto;
char *system, *text;
{
  return (TRUE);
}

 /* Define a pre-processor for the process() call.  You may call any */
 /* mcm or personality subroutines here and/or modify the command */
 /* variable.  Again, returning TRUE allows the process() to continue */

int per_process(msgfrom, command)
int msgfrom;
char *command;
{
  int id, val;
  char string[80];

  if (command[0]!='*')
    return(TRUE);
  if ((msgfrom!=0)&&(asstleader!=msgfrom))
  {
    tell (-1,msgfrom,"(sys)",
	  "Must be the room coordinator to use * commands");
    return (FALSE);
  }
  if (toupper(command[1])!='U')
    return (TRUE);
  switch (toupper(command[2]))
  {
  case 'I':
    strcap (command);
    sscanf (command,"*UI %d %d",&id, &val);
    if (!testjob(id))
      tell (-1,msgfrom,"(sys)","No such job");
    else
    {
      job[id].userby0=val;
      sprintf (job[id].extra,"*Init: %d%+d, THAC0:%d%+d",
	       job[id].userby0, job[id].userby2,
	       job[id].userby1, job[id].userby3);
      tell (id,msgfrom,"(change)",job[id].extra);
    }
    return (FALSE);
  case 'T':
    strcap (command);
    sscanf (command,"*UT %d %d",&id, &val);
    if (!testjob(id))
      tell (-1,msgfrom,"(sys)","No such job");
    else
    {
      job[id].userby1=val;
      sprintf (job[id].extra,"*Init: %d%+d, THAC0:%d%+d",
	       job[id].userby0, job[id].userby2,
	       job[id].userby1, job[id].userby3);
      tell (id,msgfrom,"(change)",job[id].extra);
    }
    return (FALSE);
  case 'N':
    strcap (command);
    sscanf (command,"*UN %d %d",&id, &val);
    if (!testjob(id))
      tell (-1,msgfrom,"(sys)","No such job");
    else
    {
      job[id].userby2=val;
      sprintf (job[id].extra,"*%d%+d,%d%+d", job[id].userby0,
	       job[id].userby2, job[id].userby1, job[id].userby3);
      tell (-1,msgfrom,"(change)",job[id].extra);
    }
    return (FALSE);
  case 'H':
    strcap (command);
    sscanf (command,"*UH %d %d",&id, &val);
    if (!testjob(id))
      tell (-1,msgfrom,"(sys)","No such job");
    else
    {
      job[id].userby3=val;
      sprintf (job[id].extra,"*%d%+d,%d%+d", job[id].userby0,
	       job[id].userby2, job[id].userby1, job[id].userby3);
      tell (-1,msgfrom,"(change)",job[id].extra);
    }
    return (FALSE);
  }
  return (TRUE);
}

 /* Define a special processor to be run after each successful login */
 /* attempt. Here, read some defaults from the extra field. */

void per_login(msgfrom)
int msgfrom;
{
  char string[80];
  int by0, by1, by2, by3;

  if (sscanf (job[msgfrom].extra,"*%d,%d,%d,%d", &by0, &by2, &by1, &by3)==2)
  {
    job[msgfrom].userby0=by0;
    job[msgfrom].userby1=by1;
  }
  else
  {
    job[msgfrom].userby0=0;
    job[msgfrom].userby1=20;
  }
  job[msgfrom].userby2=0;
  job[msgfrom].userby3=0;
  sprintf (job[msgfrom].extra,"*Init: %d%+d, THAC0:%d%+d",
	   job[msgfrom].userby0, job[msgfrom].userby2,
	   job[msgfrom].userby1, job[msgfrom].userby3);
}

 /* Define a short routine to document any additional * and / commands */
 /* added by the per_process() call.  TRUE continues with the normal */
 /* help messages */

int per_slashhelp(msgfrom)
int msgfrom;
{
  tell (-1,msgfrom,"","/RI [modifier]  Roll segment init.");
  tell (-1,msgfrom,"","/RH [thac0]     Roll to hit");
  return (TRUE);
}

int per_starhelp(msgfrom)
int msgfrom;
{
  tell (-1,msgfrom,"","*UI job val     Set init base");
  tell (-1,msgfrom,"","*UN job val     Set init modifier");
  tell (-1,msgfrom,"","*UT job val     Set THAC0 base");
  tell (-1,msgfrom,"","*UH job val     Set THAC0 modifier");
  return (TRUE);
}

 /* Now get additional mode help text */

int per_modehelp(msgfrom)
int msgfrom;
{
  return (TRUE);
}

 /* Define a pre-processor for the mode (*M) commands.  Again, you may */
 /* modify the command field, and returning TRUE runs the normal mode */
 /* commands on the modified text */

int per_mode(msgfrom, command)
int msgfrom;
char *command;
{
  return (TRUE);
}

 /* Define a set of processes to be called before and after rolling */
 /* the dice.  You may, in per_preroll() evaluate a modified roll */
 /* command (ie: /RH for roll to hit), modifying the command text to a */
 /* appropriate roll command (/Rd20).  You may then, in per_postroll() */
 /* modify the output string to a more appropriate message (ie: Hit */
 /* AC3).  If either routine returns FALSE, roll() will terminate, */
 /* assuming that the personality mode took care of the rest. */

int per_preroll(msgfrom, command)
int msgfrom;
char *command;
{
  char *textptr;
  char string[80];

  if (toupper(command[0])=='I')
  {
    if ((textptr=strtok(command+1," \n"))==NULL)
      modif=job[msgfrom].userby0;
    else
      modif=atoi(textptr);
    rollmode=1;
    strcpy (command,"Rd10");
    sprintf (string,"Rolling init with modifiers %+d/%+d", modif,
	     job[msgfrom].userby2);
    tell (msgfrom,0,"(roll)",string);
    modif+=job[msgfrom].userby2;
  }
  if (toupper(command[0])=='H')
  {
    if ((textptr=strtok(command+1," \n"))==NULL)
      modif=job[msgfrom].userby1;
    else
      modif=atoi(textptr);
    rollmode=2;
    strcpy (command,"Rd20");
    sprintf (string,"Rolling to hit with THAC0 %d/%+d", modif,
	     job[msgfrom].userby3);
    tell (msgfrom,0,"(roll)",string);
    modif+=job[msgfrom].userby3;
  }
  return (TRUE);
}

int per_postroll(pubroll, job, res_text, res_val, number, sides, rolls)
int pubroll, job, res_val, number, sides;
char *res_text;
int rolls[20];
{
  switch (rollmode)
  {
  case 0:
    return (TRUE);
  case 1:
    res_val+=modif;
    sprintf (res_text,"Inititive roll: %d",res_val);
    rollmode=0;
    return (TRUE);
  case 2:
    res_val=modif-res_val;
    sprintf (res_text,"Hit AC%d",res_val);
    rollmode=0;
    return (TRUE);
  }
}

 /* And finally a special process to be run when a user logs out. */
void per_logout(msgfrom)
int msgfrom;
{
}

