//////////////////////////////////////////////////
//																//
//	By : C.Humphrey										//
// On	: 04/01/2000										//
//																//
//	Holds functions to manage involuntary player //
// actions like vomiting, falling over and stuff//
//															 	//
//////////////////////////////////////////////////

// I know, it could be all made faster with predefined structures!!

#include	<stdio.h>
#include	"config.h"
#include	"utils.h"
#include	"structs.h"
#include	"functions.h"
#include	"actors.h"
#include	"db.h"
#include	"weather.h"

extern char	buf1[SMALL_BUFSIZE];
extern char buf2[SMALL_BUFSIZE];
extern struct descriptor_data *desc_list;
extern struct descriptor_data *desc, *desc_next;

int c;

// This function will check to see if any players are in a POSSITON that
// will cause them to conduct involutary actions.
void	check_actors(void)
{
	// Move through all the players..
	for(desc = desc_list;desc;desc=desc_next)
	{
		// Set the next descriptor.
		desc_next = desc->next_in_list;
		
		// If the player is in game, check them over.
		if(desc->character->CON_STATE == CON_PLYNG)
		{
			// if the players level is below that of GOD, check there level
			// of starvation.
			if(desc->character->LEVEL < GOD)
				ChkNurishment(desc->character);

			// If they arn't asleep, see if they need an actor.
			if(desc->character->POSITION > POS_ASLEEP)
			{
				// See how pissed they are..
				c = 3 - (int)(desc->character->DRUNK / (100/4));
				// If thay have been drinking, employ an actor.
				if(desc->character->DRUNK > 0)
					DrunkActor(desc->character);
				// If they are poisond.
				if(desc->character->STATE == STAT_POISEND)
					PoisondActor(desc->character);
				// If they have VD. :-)
				if(desc->character->STATE == STAT_PLAUGE)
					DiseasedActor(desc->character);

				// If they are drunk and posond.
				if(desc->character->STATE == STAT_DAP)
				{
					DrunkActor(desc->character);
					PoisondActor(desc->character);
				}
				// if they are poisond and diseased.
				if(desc->character->STATE == STAT_PAP)
				{
					PoisondActor(desc->character);
					DiseasedActor(desc->character);
				}
				// if they are drunk and diseased.
				if(desc->character->STATE == STAT_DPL)
				{
					DrunkActor(desc->character);
					DiseasedActor(desc->character);
				}
				// If they are drunk, posond and diseased - NOT good.
				if(desc->character->STATE == STAT_DPP)
				{
					DrunkActor(desc->character);
					PoisondActor(desc->character);
					DiseasedActor(desc->character);
				}
			}
		}
	}	
}
////// Matt, you are going to hate the following functions. Please 
// don't kill me.
void	DrunkActor(struct char_data *ch)
{
//	c = 3 - (int)(ch->DRUNK / (100/4));
	// clean the buffers
	buf1[0] = '\0';
	buf2[0] = '\0';

	// if it is greater than 3 then they must be sobber.
	if(c >= 3)
	{
		switch(ch->STATE)
		{
			case STAT_DAP	:	ch->STATE = STAT_POISEND;
									break;
			case STAT_DPL	:	ch->STATE = STAT_PLAUGE;		
									break;
			case STAT_DPP	:	ch->STATE = STAT_PAP;
									break;
			default			:	ch->STATE = c;
									break;
		}
	}
	else
		ch->STATE = c;	

	if(DEBUG_CHAR)
		printf("%s is %s\n",ch->name,show_stat[c].shrt);

	// Let the actor run wild............!
	switch(c)
	{
		case STAT_STONED: switch(rand() % 5)
								{
									case 0: sprintf(buf2,"dribbles\n\r");
											  sprintf(buf1,"You start dribbling.\n\r");
											  break;
								   case 1: sprintf(buf2,"'s eyes roll back\n\r");
											  sprintf(buf1,"Your eyes roll back.\n\r");
											  break;
								   case 2: 
											  do_say(ch,"says Hnnnghhh!\n\r");
											  sprintf(buf2,"vomits through %s nose\n\r",sex[ch->SEX].b);
											  sprintf(buf1,"You heave and vomit through your nose.\n\r");
											  break;
									case 3: if(ch->POSITION >= POS_STANDING)
											  {
												  ch->POSITION = POS_RESTING;
											  	  sprintf(buf1,"You slump to the floor.\n\r");
											  }
											  else
												  sprintf(buf1,"You try to move but fail.\n\r");
											  sprintf(buf2,"is out cold...\n\r");	
											  break;
									case 4: if(ch->POSITION > POS_INCAP)
											  {
												 ch->POSITION = POS_INCAP;
												 sprintf(buf1,"You go into a comma...\n\r"); 
											  }
											  else
												 sprintf(buf1,"You are completely paralized.\n\r");
											  sprintf(buf2,"is out cold..\n\r");	
											  break;
								}	
								send_2_output(buf1,ch->desc);
								send_2_room(ch,INVIS_CHK,buf2);
								break;
		case STAT_DRUNK : switch(rand() % 5)
								{
								   case 0: sprintf(buf2,"sways from side to side\n\r");
											  sprintf(buf1,"You start to sway..\n\r");
											  break;
									case 1: 
											  do_say(ch,"says BLAGGHH!\n\r");
											  sprintf(buf2,"vomit's down %s own legs.",sex[ch->SEX].b);
											  sprintf(buf1,
										"You throw up down your legs.\n\r");
											  break;
									case 2: sprintf(buf2,"closes %s eyes\n\r",sex[ch->SEX].b);
											  sprintf(buf1,"Your eyes close.\n\r");
											  break;
									case 3: if(ch->POSITION >= POS_STANDING)
											  {
												 do_position(ch,"sleep");
												 sprintf(buf2,"passes out.\n\r");
												 sprintf(buf1,"You pass out.\n\r");
											  }
											  else
												 sprintf(buf1,"You mumbble in your slumber.\n\r");
											  break;
									case 4: do_say(ch,"says BELCH!\n\r");
											  sprintf(buf2,"is so rude..\n\r");
											  sprintf(buf1,"That was a bubbily one...\n\r");
											  break;
								}
								send_2_output(buf1,ch->desc);
								send_2_room(ch,INVIS_CHK,buf2);
								break;
		case STAT_TIPSY : switch(rand() % 5)
								{
									case 0: sprintf(buf2,"giggle's inanely.\n\r");
											  sprintf(buf1,"You giggle..\n\r");
											  break;
									case 1: sprintf(buf2,"stumble's\n\r");
											  sprintf(buf1,"You stumble..\n\r");
											  break;
									case 2: sprintf(buf2,"laughs uncontrollably..\n\r");
											  sprintf(buf1,"You start to laugh.\n\r");
											  break;
									case 3: sprintf(buf2,"sways from side to side\n\r");
											  sprintf(buf1,"You start to sway.\n\r");
											  break;
									case 4: do_say(ch,"says Hic!\n\r");
											  sprintf(buf1,"You hicup!\n\r");
											  sprintf(buf2,"is so rude..\n\r");
											  break;
								}
								send_2_output(buf1,ch->desc);
								send_2_room(ch,INVIS_CHK,buf2);
								break;
		case STAT_OK    :	break;
		default			 :	send_2_output("You have gone completely mad\n\r",ch->desc);
								break;
	}
}
void PoisondActor(struct char_data *ch)
{
	int	level = 0;

	buf1[0] = '\0';
	buf2[0] = '\0';

	// Just how posond are they???
	level = ch->POISEN / 20;	
	if(DEBUG_CHAR)
		printf("%s is %d% Poisoned!\n",ch->name,level);

	switch(level)
	{
		case 0	:	sprintf(buf1,"You feel a little unwell..\n\r");
						sprintf(buf2,"looks a bit under the weather.\n\r");
						break;
		case 1	:	sprintf(buf1,"You feel a little dizzy..\n\r");
						sprintf(buf2,"looks uneasy on %s feet.\n\r",sex[ch->SEX].b);
						break;
		case 2	:	sprintf(buf1,"You have a cold sweat...\n\r");
						sprintf(buf2,"looks ill...\n\r");
						break;
		case 3	:	sprintf(buf1,"Your vision starts to blur..\n\r");
						sprintf(buf2,"keeps blinking..\n\r");
						break;
		case 4	:	sprintf(buf1,"You can't help vomiting \033[1m\033[32mgreen\033[0m goo..\n\r");
						sprintf(buf2,"\033[35mVomits\033[0m \033[32mGREEN\033[0m	goo..\n\r");
						break;
		case 5	:	switch(ch->POISENSTR)
						{
							case 10: sprintf(buf1,"You can't help vomiting \033[1m\033[32mgreen\033[0m goo..\n\r");
										sprintf(buf2,"\033[35mVomits\033[0m \033[32mGREEN\033[0m	goo..\n\r");
										do_say(ch,"says BPLEEEGH!");
							case 9 :	sprintf(buf1,"You dribble \033[1m\033[33mYELLOW\033[0 puss..\n\r");
										sprintf(buf2,"dribbles some \033[1m\033[33myellow\033[0m puss..\n\r");
							case 8 :	sprintf(buf1,"Your eyes start to \033[31mbleed\033[0m.\n\r");
										sprintf(buf2,"eyes start to weep \033[31mblood\033[0m.\n\r");
							case 7 :	sprintf(buf1,"You feel cold, yet you are sweating..\n\r");
										sprintf(buf2,"is sweating profusely..\n\r");
										
							case 6 :	sprintf(buf1,"You are so cold..\n\r");
										sprintf(buf2,"is shivering..\n\r");
							case 5 :	sprintf(buf1,"You look at your hands, they are as white as a sheet.\n\r");
										sprintf(buf2,"looks as white as a sheet..\n\r");
							case 4 : sprintf(buf1,"You seem to be staggering.\n\r");
										sprintf(buf2,"sways and stumbles.\n\r");
							case 3 :	sprintf(buf1,"You feel drowsy..\n\r");
										sprintf(buf2,"looks a little tired.\n\r");
							case 2 :	sprintf(buf1,"Your mouth is watering.\n\r");
										sprintf(buf2,"seems to be dribbling.\n\r");
							case 1 :	sprintf(buf1,"You feel rough..\n\r");
										sprintf(buf2,"looks rough..\n\r");
						}
	}	
	send_2_output(buf1,ch->desc);
	send_2_room(ch,INVIS_CHK,buf2);
	ch->HIT -= ch->POISENSTR / 2;
	if(ch->HIT > ch->MAX_HIT)
		ch->HIT = ch->HIT;
}
void DiseasedActor(struct char_data *ch)
{
	buf1[0] = '\0';
	buf2[0] = '\0';

	if(DEBUG_CHAR)
		printf("%s has a disease\n",ch->name);

	// Just a cold or ebola!!!!?
	if(ch->DISEASE <= ch->CON/4)
	{
		send_2_output("You feel unwell..\n\r",ch->desc);
		switch(rand() % 5)
		{
			case 0: 	sprintf(buf1,"Your vision blurs for a moment.\n\r");
						sprintf(buf2,"blinks %s eyes\n\r",sex[ch->SEX].b);
						break;
			case 1:	sprintf(buf1,"You feel a little sick.\n\r");
						sprintf(buf2,"looks ill.\n\r");
						break;
			case 2:  sprintf(buf1,"You feel very hot..\n\r");
						sprintf(buf2,"is sweating a bit..\n\r");
						break;
			case 3: 	sprintf(buf1,"You have a cold shiver...\n\r");
						sprintf(buf2,"shivers.\n\r");
						break;
			case 4:	sprintf(buf1,"Your nose itches and runs constantly.\n\r");
						sprintf(buf2,"nose is red.\n\r");
						break;
		}
	}
	else
		if(ch->DISEASE <= ch->CON/2)
		{
			switch(rand() % 5)
			{
				case 0:	sprintf(buf1,"You can barely see, you eyes hurt.\n\r");
							sprintf(buf2,"looks ill\n\r");
							break;
				case 1:	sprintf(buf1,"You feel very sick..\n\r");
							do_say(ch,"says HeUeY!\n\r");
							break;
				case 2:	sprintf(buf1,"You have a temprature.\n\r");
							sprintf(buf2,"%s is sweating a lot.\n\r",ch->name);
							break;
				case 3:	sprintf(buf2,"%s shivers uncontrolably\n\r",ch->name);
							break;
				case 4:	sprintf(buf2,"%s nose is bleading.\n\r",ch->name);
							break;
			}
		}
		else
			if(ch->DISEASE <= ch->CON)
			{
				do_position(ch,"sit");
				ch->HIT -= 1;
				sprintf(buf1,"You are so ill, you need to sit down..\n\r");
				sprintf(buf2,"looks very ill.\n\r");
			}
			else
				if(ch->DISEASE > ch->CON)
				{
					do_position(ch,"rest");
					ch->HIT -= ch->DISEASE - ch->CON;
					sprintf(buf1,"You are too ill to go on, you must rest.\n\r");
					sprintf(buf2,"looks very ill.\n\r");
				}
	send_2_output(buf1,ch->desc);
	send_2_room(ch,INVIS_CHK,buf2);
}
void	ChkNurishment(struct char_data *ch)
{
	// How malnurished are they?
	char	thir = ch->THIRST/20;
	char	hung = ch->HUNGER/20;

	buf1[0] = '\0';
	
	// Starve them a bit more...he he he!
	if(ch->THIRST < 100)
		ch->THIRST += 5;

	if(ch->HUNGER < 100)
		ch->HUNGER += 2;

	if(ch->THIRST >= 100)
		ch->HIT -= 10;

	if(ch->HUNGER >= 100)
		ch->HIT -= 5;

	// Let them know if it it bad...
	if(thir > 0)
	{
		sprintf(buf1,drink[thir].thirst);
		send_2_output(buf1,ch->desc);
		if(DEBUG_CHAR)
			printf("%s is %s\n",ch->name,drink[thir].shrt);
		if(thir+1 == 5)
			ch->HIT -= 3;
	}
	if(hung > 0)
	{
		sprintf(buf1,food[hung].hunger);
		send_2_output(buf1,ch->desc);
		if(DEBUG_CHAR)
			printf("%s is %s\n",ch->name,food[thir].shrt);
		if(hung+1 == 5)
			ch->HIT -= 1;
	}
}
// This function is used to scan the rooms in the game and see if a
// room event has to be triggered.
void	check_room_events(void)
{
	for(desc = desc_list;desc;desc=desc_next)
	{
		desc_next = desc->next_in_list;
		
		if(desc->character->CON_STATE == CON_PLYNG)
		{
			// GOD and the like can with stand all room events :-)
			if(desc->character->LEVEL < GOD)
			{
				// What is hapening in there room??
				if(BTST(desc->character->in_room->flags,WATER_ROOM))
				{
					// Drown you bugger!!!
					if(desc->character->POSITION != POS_SWIMMING)
					{
						sprintf(buf1,"You are drowning!!\n\r");
						send_2_output(buf1,desc->character->desc);
						sprintf(buf1,"is drowning!\n\r");
						send_2_room(desc->character,INVIS_CHK,buf1);
						desc->character->HIT -= 5;
					}
					else
					{
						// Swim, swim for your life !!  !! .
						if(desc->character->skills[GetSkillCharIndex(desc->character,"swim")].value[0] > 50 || desc->character->LEVEL >= GOD)
							sprintf(buf1,"is moving through the water like a fish.\n\r");
						else
							sprintf(buf1,"is splashing around in the water.\n\r");
						send_2_room(desc->character,INVIS_CHK,buf1);
					}
				}
			}
		}
	}
}

