/*******************************************************************************
*  I always interested in having a random name generator for the game, both    *
*  as a tool for builders for naming mobs and for new characters who might     *
*  need a helping hand on coming up with a new name.                           *
*  The code was written originally by plamzi of Bedlam bedlam.gotdns.com:9000  *
*  as a means for mercenary random personal names.  The code was shared and I, *
*  Diablos of End of Time eotmud.com:4000 modified it into its current form    *
*  as an output rather than mob naming utility, along with the various options *
*  to specify gender, type, and count.  No credit is necessary (but always     *
*  appreciated) for use of this snippet, but please keep the commented credits *
*  in place below.  Other than as is provided, another suggested use would be  *
*  to integrate this into character creation.                                  *
*  This code was originally released on Mudbytes at http://www.mudbytes.net    *
********************************************************************************/


//Be sure to add the proper command entries in interp.c and interp.h for "do_genname" or whatever else you may wish to call the command

//The nanny function in comm.c would be the appropriate place to try to integrate this into character creation

/* Examples of some of the names generated by the snippet:
genname male standard 20
Random Male Names: Tevaf Amaiuil Fatal Ilalam Amairip Elarag Kelaz Tisap Ilalal Etodat Amairam Nokat Okasaf Afarat Irodar Sulaz Tipal Kodal Arcazir Enazin

genname female fantasy 20
Random Female Names: Delina Xora Xola Lasina Faxola Larara Wala Waraera Nalaera Mowala Salaelle Saelle Sasa Welaelle Roro Monina Wenala Wemora Mola Dewala

genname male norse 20
Random Male Names: Todid Ivaturd Odinard Rodig Alateyr Inalat Rodih Alival Anadard Tivag Saev Odaran Eyrodid Haes Ralit Aralan Ralih Ivaheyr Alavar Hivad

genname female celtic 20
Random Female Names: Fiwela Fiwama Yigio Kayama Yora Gala Regela Rekora Kora Lirela Weka Rachio Wemala Naeroka Liwala Gama Lemela Dorama Naelela Kala

genname male dwarven 20
Random Male Names: Ekefak Ronor Uriket Rakay Uganek Asugak Refat Ugakas Asugay Eturin Uritas Urinet Urugak Urirek Nefak Kurin Konot Urarar Urarak Akarur
*/

/*  Suggested helpfile:
GENNAME 'GENERATE NAME' GENERATE_NAME 'RANDOM NAME' RANDOM_NAME
SYNTAX: genname <gender> <type> <count>
 
This will generate a random name based on various conditions.  The gender,
type and count are all optional, and when not supplied it will pick a
random gender and type.
 
Gender may be supplied using male, female, 1 for male, or 2 for female.
 
Type may be supplied using standard (1), fantasy (2), norse (3), celtic (4),
or dwarven (5).
 
Count is how many names it will generate.  The maximum is 20.

genname coded by Diablos of End of Time and plamzi of Bedlam  (This line does not have to appear in the MUD, but would be appreciated)
*/

//The following code can be placed really anywhere.  If you leave it as its own file (namegen.c), be sure to add namegen.c to your makefile


int size_of(char *arr[])
{
	int size = 0;
	while (arr[size] != NULL) size++;
	return size;
}

char *one_of(char *arr[])
{
	return arr[number_range(0, size_of(arr)-2)];
}

/* Code derived from Bedlam's (bedlam.gotdns.com:9000) mercenary random personal names by Diablos of/for End of Time (eotmud.com:4000) */
char *generate_name(int gender, int type)
{
	static char *standard_syl_closed[] = { "gob", "gup", "gad", "got", "dan", "sil", "ban", "vig", "zom", "rak", "dob", "mag", "tog", "vat", "gam",
	"bok", "jok", "rok", "mek", "dim", "ris", "tok", "tan", "tor", "mar", "mor", "vig", "rem",
	"sal", "sel", "les", "rit", "vel", "vir", "lin", "don", "dar", "dir", "mir", "lir",
	"ties", "mael", "fein", "man", "fin", "dyr", "lyr", "nyr", "vyr", NULL };
	static char *standard_syl_open[] = { "ara", "arca", "anka", "eva", "ena", "ega", "oma", "ik", "era", "ida",
	  "oda", "ami", "oka", "ipa", "ina", "osa", "abi", "ea", "ai", "amai",
	  "ata", "isa", "ira", "ara", "ala", "ela", "ula", "asa", "esa", "eta", NULL };
 	static char *standard_syl_edge[] = { "p", "n", "m", "k", "s", "t", "u", "z", "r", "b", "g", "l", "t", "f", "x", NULL };
	static char *standard_syl_opening[] = { "sa", "mi", "fa", "li", "fe", "si", "bri", "ty", "ly", NULL };
	static char *standard_syl_closing[] = { "af", "ip", "es", "il", "am", "in", "an", "ir", "el", "et", "yl", NULL };

	static char *fantasy_syl_closed[] = { "ban", "bar", "cut", "dan", "dar", "del", "der", "fin", "jed", "kan", "kar", "ker", "mar", "mer", "van", "var", "tyr",
		 "bet", "cut", "tor", "ris", "tok", "vel", "lin", "lyr", "vyr", "beth", "bett", "dell", "kurr", "tsal", "tser", "tsir", NULL };
	static char *fantasy_syl_open[] = { "ina", "ala", "aelle", "oro", "ara", "ona", "ola", "asa", "ora", "aera", "ana", NULL };
 	static char *fantasy_syl_edge[] = { "l", "n", "r", "s", "m", "w", "x", "d", NULL };
	static char *fantasy_syl_opening[] = { "ly", "we", "la", "fa", "ja", "jo", "mo", "lo", "de", "da", NULL };
	static char *fantasy_syl_closing[] = { "an", "ar", "or", "al", "en", "eo", "el", "ol", "ess", "in", "il", NULL };

	static char *norse_syl_closed[] = { "nid", "heim", "far", "mir", "nar", "nir", "rir", "sil", "sir", "ttir", "gud", "hag", "jot", "jut", "tun", "var", "tys", "vol", "ygg",
		 "nar", "din", "tal", "gar", NULL };
	static char *norse_syl_open[] = { "ala", "ae", "ali", "iva", "aea", "ana", "oda", "odi", "olu", NULL };
 	static char *norse_syl_edge[] = { "h", "g", "v", "t", "r", "d", "n", "s", "n", NULL };
	static char *norse_syl_opening[] = { "bi", "fe", "ha", "jo", "ma", "mu", "nu", "ta", "va", "vo", "da", NULL };
	static char *norse_syl_closing[] = { "ad", "ald", "agr", "ar", "ard", "eyr", "far", "heim", "ar", "ir", "urd", "an", "yr", "al", "in", "agg", NULL };

	static char *celtic_syl_closed[] = { "dan", "del", "mor", "nem", "rig", "ren", "fig", "nan", "lun", "leg", "fyn", "lyn", "myn", "tal", "wyn", "del", "ker", "wyr",
		 "kyn", "kel", "kyl", "cil", "cal", NULL };
	static char *celtic_syl_open[] = { "ae", "ela", "ora", "io", "ama", "eka", "oka", "ala", "ola", NULL };
 	static char *celtic_syl_edge[] = { "r", "g", "k", "l", "w", "y", "f", NULL };
	static char *celtic_syl_opening[] = { "ra", "ka", "la", "fi", "so", "re", "do", "li", "io", "nae", "fae", "mae", "tae", NULL };
	static char *celtic_syl_closing[] = { "ig", "ach", "eth", "ed", "em", "og", "or", "orn", "yn", "gus", "aid", "ain", "an", NULL };

	static char *dwarven_syl_closed[] = { "bom", "dar", "gar", "gim", "jor", "kil", "mar", "nal", "ras", "ren", "tar", "tel", "wer", "von", "wer", "yur",
		 "rok", "nok", "lok", "fon", "fin", "nik", "rit", "tin", "vad", "fun", "tal", "vad", NULL };
	static char *dwarven_syl_open[] = { "aka", "ara", "ono", "efa", "uga", "uri", "uda", NULL };
 	static char *dwarven_syl_edge[] = { "k", "r", "t", "n", "y", "s", NULL };
	static char *dwarven_syl_opening[] = { "ga", "da", "ra", "ka", "ty", "ry", "gu", NULL };
	static char *dwarven_syl_closing[] = { "et", "ur", "ek", "al", "as", "ar", NULL };


	char *name;
	int number = 0;

	number = number_range(1,3);

	name = strdup("");

	if (gender == 1)
	{
		if (number == 3)
		{
			if(type == 1) // standard
			{
				strcat(name, one_of(standard_syl_open));
				strcat(name, one_of(standard_syl_edge));
				strcat(name, one_of(standard_syl_closing));
			}
			else if(type == 2) // fantasy
			{
				strcat(name, one_of(fantasy_syl_open));
				strcat(name, one_of(fantasy_syl_edge));
				strcat(name, one_of(fantasy_syl_closing));
			}
			else if(type == 3) // norse
			{
				strcat(name, one_of(norse_syl_open));
				strcat(name, one_of(norse_syl_edge));
				strcat(name, one_of(norse_syl_closing));
			}
			else if(type == 4) // celtic
			{
				strcat(name, one_of(celtic_syl_open));
				strcat(name, one_of(celtic_syl_edge));
				strcat(name, one_of(celtic_syl_closing));
			}
			else if(type == 5) // dwarven
			{
				strcat(name, one_of(dwarven_syl_open));
				strcat(name, one_of(dwarven_syl_edge));
				strcat(name, one_of(dwarven_syl_closing));
			}
		}
		else if (number == 2)
		{
			if(type == 1) // standard
			{
				strcat(name, one_of(standard_syl_closing));
				strcat(name, one_of(standard_syl_open));
				strcat(name, one_of(standard_syl_edge));
			}
			else if(type == 2) // fantasy
			{
				strcat(name, one_of(fantasy_syl_closing));
				strcat(name, one_of(fantasy_syl_open));
				strcat(name, one_of(fantasy_syl_edge));
			}
			else if(type == 3) // norse
			{
				strcat(name, one_of(norse_syl_closing));
				strcat(name, one_of(norse_syl_open));
				strcat(name, one_of(norse_syl_edge));
			}
			else if(type == 4) // celtic
			{
				strcat(name, one_of(celtic_syl_closing));
				strcat(name, one_of(celtic_syl_open));
				strcat(name, one_of(celtic_syl_edge));
			}
			else if(type == 5) // dwarven
			{
				strcat(name, one_of(dwarven_syl_closing));
				strcat(name, one_of(dwarven_syl_open));
				strcat(name, one_of(dwarven_syl_edge));
			}
		}
		else
		{
			if(type == 1) // standard
			{
				if (number == 1) strcat(name, one_of(standard_syl_edge));
					strcat(name, one_of(standard_syl_open));
				if (number == 3)
					strcat(name, one_of(standard_syl_closed));
				else
					strcat(name, one_of(standard_syl_edge));
			}
			else if(type == 2) // fantasy
			{
				if (number == 1) strcat(name, one_of(fantasy_syl_edge));
					strcat(name, one_of(fantasy_syl_open));
				if (number == 3)
					strcat(name, one_of(fantasy_syl_closed));
				else
					strcat(name, one_of(fantasy_syl_edge));

			}
			else if(type == 3) // norse
			{
				if (number == 1) strcat(name, one_of(norse_syl_edge));
					strcat(name, one_of(norse_syl_open));
				if (number == 3)
					strcat(name, one_of(norse_syl_closed));
				else
					strcat(name, one_of(norse_syl_edge));

			}
			else if(type == 4) // celtic
			{
				if (number == 1) strcat(name, one_of(celtic_syl_edge));
					strcat(name, one_of(celtic_syl_open));
				if (number == 3)
					strcat(name, one_of(celtic_syl_closed));
				else
					strcat(name, one_of(celtic_syl_edge));

			}
			else if(type == 5) // dwarven
			{
				if (number == 1) strcat(name, one_of(dwarven_syl_edge));
					strcat(name, one_of(dwarven_syl_open));
				if (number == 3)
					strcat(name, one_of(dwarven_syl_closed));
				else
					strcat(name, one_of(dwarven_syl_edge));

			}
		}
	}
	else
	{
		if (number == 3)
		{
			if(type == 1) // standard
			{
				strcat(name, one_of(number == 1?standard_syl_closed:standard_syl_edge));
				strcat(name, one_of(standard_syl_open));
			}
			else if(type == 2) // fantasy
			{
				strcat(name, one_of(number == 1?fantasy_syl_closed:fantasy_syl_edge));
				strcat(name, one_of(fantasy_syl_open));
			}
			else if(type == 3) // norse
			{
				strcat(name, one_of(number == 1?norse_syl_closed:norse_syl_edge));
				strcat(name, one_of(norse_syl_open));
			}
			else if(type == 4) // celtic
			{
				strcat(name, one_of(number == 1?celtic_syl_closed:celtic_syl_edge));
				strcat(name, one_of(celtic_syl_open));
			}
			else if(type == 5) // dwarven
			{
				strcat(name, one_of(number == 1?dwarven_syl_closed:dwarven_syl_edge));
				strcat(name, one_of(dwarven_syl_open));
			}
		}
		else if (number == 2)
		{
			if(type == 1) // standard
			{
				strcat(name, one_of(standard_syl_opening));
				strcat(name, one_of(standard_syl_edge));
				strcat(name, one_of(standard_syl_open));
			}
			else if(type == 2) // fantasy
			{
				strcat(name, one_of(fantasy_syl_opening));
				strcat(name, one_of(fantasy_syl_edge));
				strcat(name, one_of(fantasy_syl_open));
			}
			else if(type == 3) // norse
			{
				strcat(name, one_of(norse_syl_opening));
				strcat(name, one_of(norse_syl_edge));
				strcat(name, one_of(norse_syl_open));
			}
			else if(type == 4) // celtic
			{
				strcat(name, one_of(celtic_syl_opening));
				strcat(name, one_of(celtic_syl_edge));
				strcat(name, one_of(celtic_syl_open));
			}
			else if(type == 5) // dwarven
			{
				strcat(name, one_of(dwarven_syl_opening));
				strcat(name, one_of(dwarven_syl_edge));
				strcat(name, one_of(dwarven_syl_open));
			}
		}
		else if (number == 1)
		{
			if(type == 1) // standard
			{
				strcat(name, one_of(standard_syl_edge));
				strcat(name, one_of(standard_syl_closing));
				strcat(name, one_of(standard_syl_open));
			}
			else if(type == 2) // fantasy
			{
				strcat(name, one_of(fantasy_syl_edge));
				strcat(name, one_of(fantasy_syl_closing));
				strcat(name, one_of(fantasy_syl_open));
			}
			else if(type == 3) // norse
			{
				strcat(name, one_of(norse_syl_edge));
				strcat(name, one_of(norse_syl_closing));
				strcat(name, one_of(norse_syl_open));
			}
			else if(type == 4) // celtic
			{
				strcat(name, one_of(celtic_syl_edge));
				strcat(name, one_of(celtic_syl_closing));
				strcat(name, one_of(celtic_syl_open));
			}
			else if(type == 5) // dwarven
			{
				strcat(name, one_of(dwarven_syl_edge));
				strcat(name, one_of(dwarven_syl_closing));
				strcat(name, one_of(dwarven_syl_open));
			}
		}
		else
		{
			if(type == 1) // standard
			{
				strcat(name, one_of(standard_syl_opening));
				strcat(name, one_of(standard_syl_open));
			}
			else if(type == 2) // fantasy
			{
				strcat(name, one_of(fantasy_syl_opening));
				strcat(name, one_of(fantasy_syl_open));
			}
			else if(type == 3) // norse
			{
				strcat(name, one_of(norse_syl_opening));
				strcat(name, one_of(norse_syl_open));
			}
			else if(type == 4) // celtic
			{
				strcat(name, one_of(celtic_syl_opening));
				strcat(name, one_of(celtic_syl_open));
			}
			else if(type == 5) // dwarven
			{
				strcat(name, one_of(dwarven_syl_opening));
				strcat(name, one_of(dwarven_syl_open));
			}
		}
	}
	return capitalize(name);
}

void do_genname( CHAR_DATA *ch,  char *argument )
{
    char arg1[MAX_INPUT_LENGTH];
    char arg2[MAX_INPUT_LENGTH];
    char arg3[MAX_INPUT_LENGTH];
	char buf[MAX_STRING_LENGTH];
	char buf2[MAX_STRING_LENGTH];
	char output[MAX_STRING_LENGTH];
	int gender = 0;
	int type = 0;
	int count = 0;
	int number = 0;

	argument = one_argument( argument, arg1 );
	argument = one_argument( argument, arg2 );
	argument = one_argument( argument, arg3 );

	if ( arg1[0] == '\0')
    {
		gender = number_range(1,2);
    }
	else
	{
		if(!is_number(arg1))
		{
			if(!str_cmp(arg1,"male"))
				gender = 1;
			else if(!str_cmp(arg1,"female"))
				gender = 2;
			else
			{
				send_to_char("That is not a valid gender.\r\n1 = male, 2 = female\r\n",ch);
				return;
			}
		}
		else
			gender = atoi(arg1);
		if(gender < 1 || gender > 2)
		{
			send_to_char("That is not a valid gender.\r\n1 = male, 2 = female\r\n",ch);
			return;
		}
	}
	if ( arg2[0] == '\0')
    {
		type = number_range(1,5);
	}
	else
	{
		if(is_number(arg2))
			type = atoi(arg2);
		else
		{
			if(!str_prefix(arg2,"standard"))
				type = 1;
			else if(!str_prefix(arg2,"fantasy"))
				type = 2;
			else if(!str_prefix(arg2,"norse"))
				type = 3;
			else if(!str_prefix(arg2,"celtic"))
				type = 4;
			else if(!str_prefix(arg2,"dwarven") || !str_prefix(arg2,"dwarf") )
				type = 5;
			else
			{
				send_to_char("That is not a valid type.\r\n1 = standard, 2 = fantasy, 3 = norse, 4 = celtic, 5 = dwarven\r\n",ch);
				return;
			}
		}
		if(type < 1 || type > 5)
		{
			send_to_char("That is not a valid type.\r\n1 = standard, 2 = fantasy, 3 = norse, 4 = celtic, 5 = dwarven\r\n",ch);
			return;
		}
	}

	buf2[0] = '\0';
	output[0] = '\0';
	if ( arg3[0] != '\0' && is_number(arg3))
	{
		number = atoi(arg3);
		if(number > 20)
		{
			send_to_char("That number is too high, defaulting to 20 names.\r\n",ch);
			number = 20;
		}
		if(number < 1)
			number = 1;
		for (count = 0; count < number; count++)
		{
			sprintf(output,"%s%s",generate_name(gender, type), number > 1 ? " " : "");
			strcat(buf2,output);
		}
		sprintf(buf,"Random %s Name%s: %s\r\n",gender == 1 ? "Male" : "Female",number > 1 ? "s" : "",buf2);
	}
	else
		sprintf(buf,"Random %s Name: %s\r\n",gender == 1 ? "Male" : "Female",generate_name(gender, type));
	send_to_char(buf,ch);
	return;
}