#include<string.h>
#include<stdlib.h>
#include"adogdres.h"
#include"adjust.h"

#define FALSE 0
#define TRUE	1

typedef unsigned char BYTE;
typedef unsigned int HANDLE;
typedef char far LPSTR;
typedef unsigned int WORD;
typedef long LONG;
typedef int BOOL;
typedef HANDLE HWND;
typedef HANDLE HBITMAP;

void cha_adj(void);
void gp(int ganger, int terning);
void familie(void);
void soc_class(void);
void height_weight(void);
void hit_class(void);

extern charinfo Player;
extern BOOL bThief;
extern BOOL bRogue;
extern BOOL bWizard;
extern BOOL bCleric;
extern BOOL bBard;
extern BOOL bSpecialist;


void cha_adj(void)
{
	if(Player.skill[CHA] < 9)
	{
		Player.skill[CML]--;
	}
	if(Player.skill[CHA] < 6)
	{
		Player.skill[CML]--;
	}
	if(Player.skill[CHA] < 4)
	{
		Player.skill[CML]--;
	}
	if(Player.skill[CHA] > 12)
	{
		Player.skill[CML]++;
	}
	if(Player.skill[CHA] > 15)
	{
		Player.skill[CML]++;
	}
	if(Player.skill[CHA] > 17)
	{
		Player.skill[CML]++;
	}
}



/* justerer skills for raser */

void race_adj(void)
{
	switch(Player.race)
	{
		case ELF:
			Player.skill[DEX]++;
			Player.skill[CON]--;
			Player.skill[CML]++;
		break;

		case HALFLING:
			if(Player.skill[WIS]>17)
			{
				Player.skill[WIS]--;
				Player.skill[INT]++;
			}
			Player.skill[DEX]++;
			Player.skill[STR]--;
		break;

		case DWARF:
			if(Player.skill[DEX] > 17)
			{
				if(Player.skill[CON] > Player.skill[STR])
				{
					Player.skill[STR]++;
				}
				else
				{
					Player.skill[CON]++;
				}
			}

			if(Player.skill[CHA] > 17)
			{
				Player.skill[CHA]--;
			}

			Player.skill[CON]++;
			Player.skill[CHA]--;
			Player.skill[CML]--;
		break;

		case GNOME:
			Player.skill[INT]++;
			Player.skill[WIS]--;
			Player.skill[CML]--;
		break;
	}
}

void familie(void)
{
	int mar, birth;

	mar = DICE(100);
	birth = DICE(100);

	switch(Player.race)
	{
		case HALFELF:
			mar -= 10;
			birth -= 10;
		break;

		case ELF:
			mar -= 19;
			birth -= 30;
		break;

		case HALFLING:
			mar -= 10;
			birth -= 10;
		break;

		case DWARF:
			mar -= 14;
			birth -= 14;
		break;

		case GNOME:
			mar -= 18;
			birth -= 20;
		break;
	}

	if(mar < 80)
	{
		strcpy(Player.family,"Married");
	}
	else
	{
		strcpy(Player.family,"Unmarried");
	}

	if(birth < 11)
	{
		Player.siblings = 0;
	}
	else if(birth < 21)
	{
		Player.siblings = 1;
	}
	else if(birth < 36)
	{
		Player.siblings = 2;
	}
	else if(birth < 51)
	{
		Player.siblings = 3;
	}
	else if(birth < 66)
	{
		Player.siblings = 4;
	}
	else if(birth < 81)
	{
		Player.siblings = 5;
	}
	else if(birth < 86)
	{
		Player.siblings = 6;
	}
	else if(birth < 91)
	{
		Player.siblings = 7;
	}
	else if(birth < 95)
	{
		Player.siblings = 8;
	}
	else if(birth < 98)
	{
		Player.siblings = 9;
	}
	else if(birth < 99)
	{
		Player.siblings =10;
	}
	else if(birth >= 100)
	{
		Player.siblings =11;
	}

	Player.birthrank = DICE(Player.siblings+1);

	if(Player.birthrank == 1)
	{
		if(stricmp(Player.sclass,"UUC") == 0)
		{
			strcpy(Player.status,"Crown-prince");
		}
		else if(stricmp(Player.sclass,"MUC") == 0)
		{
			strcpy(Player.status,"A Baron's heir");
		}
		else
		{
			strcpy(Player.status,"Inheritor");
		}
	}
	else
	{
		if(stricmp(Player.sclass,"UUC") == 0)
		{
			strcpy(Player.status,"Prince");
		}
		else if(stricmp(Player.sclass,"MUC") == 0)
		{
			strcpy(Player.status,"Noble");
		}
		else
		{
			strcpy(Player.status,"Adventurer");
		}
	}
}


void soc_class(void)
{
	int sos;

	sos = DICE(100);

	switch(Player.race)
	{
		case HALFELF:
			sos += 2;
		break;

		case ELF:
			sos = sos/10+87;
		break;

		case HALFLING:
			sos = sos/8+10;
		break;

		case DWARF:
			sos = sos/2+50-(100-sos)/4;
		break;

		case GNOME:
			sos = sos/7+15;
		break;
	}

	if(sos < 4)
	{
		strcpy(Player.sclass,"LLC");
	}
	else if(sos < 10)
	{
		strcpy(Player.sclass,"MLC");
	}
	else if(sos < 20)
	{
		strcpy(Player.sclass,"ULC");
	}
	else if(sos < 35)
	{
		strcpy(Player.sclass,"LMC");
	}
	else if(sos < 55)
	{
		strcpy(Player.sclass,"MMC");
	}
	else if(sos < 87)
	{
		strcpy(Player.sclass,"UMC");
	}
	else if(sos < 96)
	{
		strcpy(Player.sclass,"LUC");
	}
	else if(sos < 99)
	{
		strcpy(Player.sclass,"MUC");
	}
	else if(sos == 100)
	{
		strcpy(Player.sclass,"UUC");
	}
	else
	{
		strcpy(Player.sclass,"UMC");
	}
}


void height_weight(void)
{
	switch(Player.race)
	{
		case HUMAN:
			Player.age = 15 + DICE(4);

			if(Player.sex == MALE)
			{
				Player.height = 2.54 * (60 + DICE(10) + DICE(10));
				Player.weight = (50 + DICE(10) + DICE(10) + DICE(10) +
												Player.skill[STR] + Player.skill[CON] / 2) *
												Player.height / 177.8;
			}
			else
			{
				Player.height = 2.54 * (58 + DICE(10) + DICE(10));
				Player.weight = (33.25 + DICE(10) + DICE(10) + DICE(10) +
												Player.skill[STR] + Player.skill[CON] / 2) *
												Player.height / 172.72;
			}
		break;

		case HALFELF:
			Player.age = 15 + DICE(6);

			if(Player.sex == MALE)
			{
				Player.height = 2.54 * (60 + DICE(6) + DICE(6));
				Player.weight = (49.5 + DICE(12) + DICE(6) + Player.skill[STR] / 2) *
												Player.height	/	167.64;
			}
			else
			{
				Player.height = 2.54 * (57 + DICE(6) + DICE(6));
				Player.weight = (37.5 + DICE(12) + DICE(6) + Player.skill[STR] / 2) *
												Player.height / 160;
			}
		break;

		case ELF:
			Player.age = 100 + DICE(6) + DICE(6) + DICE(6) + DICE(6) + DICE(6);

			if(Player.sex == MALE)
			{
				Player.height = 2.54 * (55 + DICE( 10));
				Player.weight = (86.5 + DICE(10) + DICE(10) + DICE(10) +
												Player.skill[STR] / 3) / 2 * Player.height / 153.67;
			}
			else
			{
				Player.height = 2.54 * (50 + DICE(10));
				Player.weight = (34 + DICE(10) + DICE(10) + DICE(10)) *
												Player.height / 141;
			}
		break;

		case HALFLING:
			Player.age = 20 + DICE(4) + DICE(4) + DICE(4);

			if(Player.sex == MALE)
			{
				Player.height = 2.54 * (32 + DICE(8) + DICE(8));
				Player.weight = (41.5 + DICE(4) + DICE(4) + DICE(4) + DICE(4) +
												DICE(4) + Player.skill[STR]) / 2 * Player.height /
												104.14;
			}
			else
			{
				Player.height = 2.54 * (30 + DICE(8) + DICE(8));
				Player.weight = (37.5 + DICE(4) + DICE(4) + DICE(4) + DICE(4) +
												DICE(4) + Player.skill[STR]) / 2 * Player.height /
												99.06;
			}
		break;

		case DWARF:
			Player.age = 40 + DICE(6) + DICE(6) + DICE(6) + DICE(6) + DICE(6);

			if(Player.sex == MALE)
			{
				Player.height = 2.54*(43 + DICE(10));
				Player.weight = (49.25 + DICE(10) + DICE(10) + Player.skill[STR] +
												Player.skill[CON]/2) * Player.height / 123.19;
			}
			else
			{
				Player.height = 2.54 * (41 + DICE(10));
				Player.weight = (44.625 + DICE(10) + DICE(10) + Player.skill[STR] +
												Player.skill[CON]	/	2) *Player.height/118.11;
			}
		break;

		case GNOME:
			Player.age = 60 + DICE(12) + DICE(12) + DICE(12);

			if(Player.sex == MALE)
			{
				Player.height = 2.54 * (38 + DICE(6));
				Player.weight = (66.75 + DICE(4) + DICE(4) + DICE(4) + DICE(4) +
												DICE(4) + Player.skill[STR] / 2) / 2 * Player.height /
												105.41;
			}
			else
			{
				Player.height = 2.54 * (58 + DICE(10) + DICE(10));
				Player.weight = (62.75 + DICE(4) + DICE(4) + DICE(4) + DICE(4) +
												DICE(4) + Player.skill[STR]/2) / 2 * Player.height /
												100.33;
			}
		break;
	}
}



void hitclass(void)
{
	if(stricmp(Player.class,"Fighter") == 0)
	{
		gp(5,4);
		if(Player.skill[STR] >= 18)
		{
			Player.strbonus = DICE(100);
		}
		Player.hp = 5+DICE(5);
	}
	else if(stricmp(Player.class,"Paladin") == 0)
	{
		gp(5,4);
		if(Player.skill[STR] >= 18)
		{
			Player.strbonus = DICE(100);
		}
		Player.hp = 5+DICE(5);
	}
	else if(stricmp(Player.class,"Ranger") == 0)
	{
		gp(5,4);
		Player.hp = 5+DICE(5);
	}
	else if(stricmp(Player.class,"Wizard") == 0)
	{
		if(Player.race == HUMAN || Player.race == HALFELF || Player.race == ELF)
		{
			bSpecialist = TRUE;
		}
		bWizard = TRUE;

		gp(1,4);
		Player.gp += 10;
		Player.hp = 2+DICE(2);
	}
	else if(stricmp(Player.class,"Illusionist") == 0)
	{
		bWizard = TRUE;
		gp(1,4);
		Player.gp += 10;
		Player.hp = 2+DICE(2);
	}
	else if(stricmp(Player.class,"Cleric") == 0)
	{
		if(Player.race == HUMAN || Player.race == HALFELF)
		{
			bCleric = TRUE;
		}

		gp(3,6);
		Player.hp = 4+DICE(4);
	}
	else if(stricmp(Player.class,"Druid") == 0)
	{
		gp(3,6);
		Player.hp = 4+DICE(4);
	}
	else if(stricmp(Player.class,"Thief") == 0)
	{
		bThief = TRUE;
		gp(2,6);
		Player.hp = 3+DICE(3);
	}
	else if(stricmp(Player.class, "Rogue") == 0)
	{
		gp(2,6);
		Player.hp = 3+DICE(3);
	}
	else if(stricmp(Player.class,"Bard") == 0)
	{
		bBard = TRUE;
		gp(2,6);
		Player.hp = 3+DICE(3);
	}
	else if(stricmp(Player.class,"Fighter/thief") == 0)
	{
		bThief = TRUE;

		if(Player.skill[STR] >= 18)
		{
			Player.strbonus = DICE(100);
		}
		gp(5,4);
		Player.hp = (8+DICE(5)+DICE(3))/2;
	}
	else if(stricmp(Player.class,"Fighter/cleric") == 0)
	{
		if(Player.skill[STR] >= 18)
		{
			Player.strbonus = DICE(100);
		}
		gp(5,4);
		Player.hp = (9+DICE(5)+DICE(4))/2;
	}
	else if(stricmp(Player.class,"Fighter/druid") == 0)
	{
		if(Player.skill[STR] >= 18)
		{
			Player.strbonus = DICE(100);
		}
		gp(5,4);
		Player.hp = (9+DICE(5)+DICE(4))/2;
	}
	else if(stricmp(Player.class,"Fighter/illusionist") == 0)
	{
		bWizard = TRUE;
		if(Player.skill[STR] >= 18)
		{
			Player.strbonus = DICE(100);
		}
		gp(5,4);
		Player.hp = (7+DICE(5)+DICE(2))/2;
	}
	else if(stricmp(Player.class,"Fighter/mage") == 0)
	{
		bWizard = TRUE;

		if(Player.skill[STR] >= 18)
		{
			Player.strbonus = DICE(100);
		}
		gp(5,4);
		Player.hp = (7+DICE(5)+DICE(2))/2;
	}
	else if(stricmp(Player.class,"Ranger/cleric") == 0)
	{
		gp(5,4);
		Player.hp = (8+DICE(5)+DICE(4))/2;
	}
	else if(stricmp(Player.class,"Cleric/mage") == 0)
	{
		bWizard = TRUE;
		gp(3,6);
		Player.hp = (6+DICE(4)+DICE(2))/2;
	}
	else if(stricmp(Player.class,"Mage/thief") == 0)
	{
		bWizard = TRUE;
		bThief = TRUE;
		gp(2,6);
		Player.hp = (5+DICE(3)+DICE(2))/2;
	}
	else if(stricmp(Player.class,"Illusionist/thief") == 0)
	{
		bWizard = TRUE;
		bThief = TRUE;
		gp(2,6);
		Player.hp = (5+DICE(3)+DICE(2))/2;
	}
	else if(stricmp(Player.class,"Fighter/mage/cleric") == 0)
	{
		bWizard = TRUE;

		if(Player.skill[STR] >= 18)
		{
			Player.strbonus = DICE(100);
		}
		gp(5,4);
		Player.hp = (11+DICE(5)+DICE(4)+DICE(2))/3;
	}
	else if(stricmp(Player.class,"Fighter/mage/thief") == 0)
	{
		bWizard = TRUE;
		bThief = TRUE;
		if(Player.skill[STR] >= 18)
		{
			Player.strbonus = DICE(100);
		}
		gp(5,4);
		Player.hp = (10+DICE(5)+DICE(3)+DICE(2))/3;
	}
	else if(stricmp(Player.class,"Psionicist") == 0)
	{
		bRogue = TRUE;
		gp(2,6);
		Player.hp = 3+DICE(3);
	}
	else if(stricmp(Player.class,"Fighter/Psionicist") == 0)
	{
		bRogue = TRUE;
		if(Player.skill[STR] >= 18)
		{
			Player.strbonus = DICE(100);
		}
		gp(5,4);
		Player.hp = (8+DICE(5)+DICE(3))/2;
	}
	else if(stricmp(Player.class,"Thief/Psionicist") == 0)
	{
		bThief = TRUE;
		bRogue = TRUE;
		gp(2,6);
		Player.hp = 3 + DICE(3);
	}
	else if(stricmp(Player.class,"Rogue") == 0)
	{
		bRogue = TRUE;
		Player.gp = 1;
		Player.hp = DICE(4);
	}

	if(Player.skill[CON] == 15)
	{
		 Player.hp++;
	}
	if(Player.skill[CON] > 15)
	{
		 Player.hp += 2;
	}
	if(Player.skill[CON] > 16 && Player.strbonus != 0)
	{
		 Player.hp += Player.skill[CON] - 16;
	}
	if(Player.skill[CON] < 7)
	{
		 Player.hp--;
	}
	if(Player.skill[CON] < 4)
	{
		 Player.hp--;
	}
}


void gp(int ganger, int terning)
{
	int i, temp = 0;

	for(i = 0; i < ganger; i++)
	{
		 temp += DICE(terning);
	}
	Player.gp = temp * 10;
}
