/*
 * This is a command to display a breakdown of sex, class, and race for your mobiles. Functions to run mud wide or same
 * area only. Easily pluggable into any stock Ack, and probably other Diku-derived games.
 *
 * Feel free to modify this code to suit your needs, and if you have a great improvement I wouldn't mind if you
 * shared it with me either, if you're so inclined :D. All I ask is to give me credit for the code if you do
 * choose to use it.
 *
 * --Kline (Matt Goff)
 */

void do_census( CHAR_DATA *ch, char *argument )
{
 CHAR_DATA *vch;
 char buf[MSL];
 int rcnt[MAX_RACE];
 int ccnt[MAX_CLASS];
 int scnt[3];
 sh_int i = 0;
 float tf0, tf1, tf2, tf3 = 0;
 int ti1 = 0;

 for( i = 0; i < MAX_RACE; i++ )
  rcnt[i] = 0;
 for( i = 0; i < MAX_CLASS; i++ )
  ccnt[i] = 0;
 for( i = 0; i < 3; i++ )
  scnt[i] = 0;


 if( argument[0] == '\0' )
 {
  send_to_char("Syntax: census <world/area>\n\r",ch);
  return;
 }

 if( !str_prefix(argument,"world") )
 {
  sprintf(buf,"Census For: %s",mudnamecolor);
  send_to_char(center_text(buf,132),ch);
  send_to_char("\n\r------------------------------------------------------------------------------------------------------------------------------------\n\r",ch);
  for( vch = first_char; vch != NULL; vch = vch->next )
  {
   if( !IS_NPC(vch) )
    continue;
   rcnt[vch->race]++;
   ccnt[vch->class]++;
   scnt[vch->sex]++;
  }
 }
 else if( !str_prefix(argument,"area") )
 {
  sprintf(buf,"Census For: %s",ch->in_room->area->name);
  send_to_char(center_text(buf,132),ch);
  send_to_char("\n\r------------------------------------------------------------------------------------------------------------------------------------\n\r",ch);
  for( vch = first_char; vch != NULL; vch = vch->next )
  {
   if( !IS_NPC(vch) )
    continue;
   if( vch->in_room->area != ch->in_room->area )
    continue;
   rcnt[vch->race]++;
   ccnt[vch->class]++;
   scnt[vch->sex]++;
  }
 }
 else
 {
  do_census(ch,"");
  return;
 }

 /* Tally the sexes! */
 tf0 = (scnt[SEX_NEUTRAL] + scnt[SEX_MALE] + scnt[SEX_FEMALE]);
 tf1 = (scnt[SEX_NEUTRAL] / tf0) * 100;
 tf2 = (scnt[SEX_MALE] / tf0) * 100;
 tf3 = (scnt[SEX_FEMALE] / tf0) * 100;
 sprintf(buf,"[SEX  ] ");
 send_to_char(buf,ch);
 sprintf(buf,"%9s: %4d (%05.2f%%) %9s: %4d (%05.2f%%) %9s: %4d (%05.2f%%)\n\r","Neutral",scnt[SEX_NEUTRAL],tf1,"Male",scnt[SEX_MALE],tf2,"Female",scnt[SEX_FEMALE],tf3);
 send_to_char(buf,ch);

 /* Tally the classes! */
 tf0 = 0;
 for( i = 0; i < MAX_CLASS; i++ )
  tf0 += ccnt[i];
 sprintf(buf,"[CLASS] ");
 for( i = 0; i < MAX_CLASS; i++ )
  strcat(buf,"%9s: %4d (%05.2f%%) ",class_table[i].who_name,ccnt[i],((ccnt[i] / tf0) * 100));
 strcat(buf,"\n\r");
 send_to_char(buf,ch);

 /* Tally the races! */
 tf0 = 0;
 ti1 = 0;
 for( i = 0; i < MAX_RACE; i++ )
  tf0 += rcnt[i];
 sprintf(buf,"[RACE ] ");
 for( i = 0; i < MAX_RACE; i++ )
 {
  strcat(buf,"%9s: %4d (%05.2f%%) ",race_table[i].race_title,rcnt[i],((rcnt[i] / tf0) * 100));
  if( ++ti1 % 5 == 0 && i < (MAX_RACE -1) )
   strcat(buf,"\n\r[RACE ] ");
 }

 strcat(buf,"\n\r\n\rFound %0.0f total mobs.\n\r",tf0);
 send_to_char(buf,ch);

 return;
}