#include <iostream.h>

#include <stdlib.h>
#include <time.h>

void main()
{
	float ,crit_chance [50],xtra_melee_mult [50],base_speed [50], computed_speed [50], digi_level [50], max_energy [50], present_energy [50], melee_stat [50], armor_stat [50], hit_stat [50], dodge_stat [50], digi_team [50];
	float largest;
	int i,j,n,p,t,f,h,a,c,speedtotal;
	char name[50][25];
	float damage_dealt(float digi_level[], float melee_stat [], float armor_stat [], int i, int t, float xtra_melee_mult []);
	float critical_damage(float digi_level[], float melee_stat [], float armor_stat [], int i, int t, float xtra_melee_mult []);
	float accuracy_calc(float hit_stat[], float dodge_stat[], int i, int t);
	
	cout<<"Welcome to Project Sleeper, version 0.20."<<endl;
	cout<<"This is an overall DRPG Battle Simulator; it only supports Melee combat in this release..."<<endl;
	cout<<endl<<"C++ Programming done by Gullwhacker."<<endl;
	cout<<"Original BASIC programming done by Omicron."<<endl;
	cout<<endl<<"The program is freeware. However, the sourcecode may not be copied,"<<endl;
	cout<<"modified or otherwise used without the permission of the author.  So there.  =P"<<endl;
	cout<<endl;
	cout<<endl;
	
	h=100;
	cout<<endl<<"How many Digimon are in this fight?"<<endl;
	cin>>n;
	for (i=0;i<n;++i)
	{
		cout<<endl<<"What is the name of this Digimon?"<<endl;
		cin.get();
		cin.getline (name[i], 25);
		cout<<endl<<"What is the level of "<<name[i]<<"?"<<endl;
		cin>>digi_level[i];
		cout<<endl<<"What is the maximum Energy of "<<name[i]<<"?"<<endl;
		cin>>max_energy[i];
		cout<<endl<<"What is the present Energy of "<<name[i]<<"?"<<endl;
		cin>>present_energy[i];
		cout<<endl<<"What is the Melee Statistic of "<<name[i]<<"?"<<endl;
		cin>>melee_stat[i];
		cout<<endl<<"What is the Armor Statistic of "<<name[i]<<"?"<<endl;
		cin>>armor_stat[i];
		cout<<endl<<"What is the Speed of "<<name[i]<<"?"<<endl;
		cin>>base_speed[i];
		computed_speed[i]=base_speed[i];
		cout<<endl<<"What is the Dodge Statistic of "<<name[i]<<"?"<<endl;
		cin>>dodge_stat[i];
		cout<<endl<<"What is the Hit Statistic of "<<name[i]<<"?"<<endl;
		cin>>hit_stat[i];
		cout<<endl<<"What is the extra damage bonus?  (Standard is 0, omit % signs)"<<endl;
		cin>>xtra_melee_mult[i];
		xtra_melee_mult[i]+=100;
		cout<<endl<<"What is the extra chance for critical hits?  (Standard is 0, omit % signs)"<<endl;
		cin>>crit_chance[i];
		crit_chance[i]+=5;
		cout<<endl<<"What team is "<<name[i]<<" on?"<<endl;
		cin>>digi_team[i];
		if (present_energy[i]>max_energy[i])
		{	
				present_energy[i]=max_energy[i];
		}
		cout<<endl<<"********************"<<endl;
	}
	speedtotal=(1);
	for (a=0;a<n;++a)
		speedtotal*=base_speed[a];
	cout<<endl<<"How many rounds of combat should be calculated?"<<endl;
	cin>>p;

	for(j=0;j<p;++j)
	{
		a=0;
		cout<<"**********\n";
		while (a==0)
		{
			for (i=0; i<n; ++i)
			{
				if (computed_speed[i]>=speedtotal)
					{
					largest=i;
					computed_speed[i]=0;
					a=1;
					}
				else
					computed_speed[i]+=base_speed[i];
			}
		}
		for (i=0;i<n;++i)
		{
			if (i==largest)
				{
					cout<<endl<<name[i]<<" gets to attack!\n";
					for (a=0;a<n;++a)
					{

						cout<<endl<<int (a + 1)<<".  "<<name[a]<<": "<<present_energy[a]<<" of "<<max_energy[a]<<" Energy remaining.  Status:  ";
						
						if (present_energy[a]>(0.1*max_energy[a]))
							cout<<"Normal"<<endl;
						else if (present_energy[a]<=(0.1*max_energy[a]), present_energy[a]>0)
							cout<<"Endangered"<<endl;
						else if (present_energy[a]<=0, present_energy[a]>(-0.1*max_energy[a]))
							cout<<"Unconsious"<<endl;
						else if (present_energy[a]<=(-0.1*max_energy[a]))
							cout<<"Dead"<<endl;
						
					}
					t=0;
					cout<<endl<<"Who will you attack?"<<endl;
					cin>>t;
					--t;
					h=accuracy_calc (hit_stat,dodge_stat,i,t);
					srand( time(NULL) );
					c=rand()%100+1;
					
								if (c<crit_chance[i])
						{
							cout<<endl<<name[i]<<" hit "<<name[t]<<"!\n";
							f=critical_damage(digi_level,melee_stat,armor_stat,i,t,xtra_melee_mult);
							present_energy[t]-=f;
							cout<<"Critical hit!  "<<name[t]<<" took "<<f<<" damage!\n";
							--present_energy[i];
						
							if (present_energy[i]<=0)
							{
								cout<<endl<<name[i]<<" collapsed. . . with good cause!\n"<<endl;
								base_speed[i]=0;
								computed_speed[i]=0;
								dodge_stat[i]=0;
							}
			
							if (present_energy[t]<=0)
							{
								if (i==t)
									cout<<endl<<"Next time, do that kind of damage to the ENEMY!"<<endl;
								else
								{
									cout<<endl<<name[t]<<" was clobbered.\n"<<endl;
									base_speed[t]=0;
									computed_speed[t]=0;
									dodge_stat[t]=0;
								}
							}
						}
						else if (c<h)
						{
							cout<<endl<<name[i]<<" hit "<<name[t]<<"!\n";
							c=1;
							f=damage_dealt (digi_level,melee_stat,armor_stat,i,t,xtra_melee_mult);
							present_energy[t]-=f;
							cout<<name[t]<<" took "<<f<<" damage!\n";
							--present_energy[i];
						
							if (present_energy[i]<=0)
							{
								cout<<endl<<name[i]<<" collapsed.\n";
								base_speed[i]=0;
								computed_speed[i]=0;
								dodge_stat[i]=0;
							}
			
							if (present_energy[t]<=0)
							{
								if (i==t)
									cout<<endl<<"Why did you attack yourself?"<<endl;
								else
								{
									cout<<endl<<name[t]<<" collapsed.\n";
									base_speed[t]=0;
									computed_speed[t]=0;
									dodge_stat[t]=0;
								}
							}
						}
						else
						{
							cout<<endl<<name[i]<<" missed "<<name[t]<<".\n";
							--present_energy[i];
						}

				}

			
		}

	}
	cout<<"Press any key to quit.";
}

float damage_dealt (float digi_level[], float melee_stat[], float armor_stat[], int i, int t, float xtra_melee_mult[])
{
	float damaga, damagb, damagc, randomdamage;
	int f, d;
	damaga=(((digi_level [i] * melee_stat [i]) / 9) + 1);
	damagb=(((damaga - (0.3 * damaga)) / 100) * xtra_melee_mult [i]);
	damagc=(((damaga + (0.3 * damaga)) / 100) * xtra_melee_mult [i]);
	d=(damagc-damagb);
	srand( time(NULL) );
	randomdamage=((rand()%100+1)*d);
	f=((0.01*randomdamage) + damagb - armor_stat[t]);
	
		if (f<1)
		{
			f=1;
		}
	
	
		if (f>9999)
		{
			f=9999;
		}
	return f;
}

float accuracy_calc(float hit_stat[], float dodge_stat[], int i, int t)
{
	float total;
	int h;
	total=(hit_stat[i]-dodge_stat[t]);
	if (total <= -280)
		h=10;
	else if (total > -280, total < -210)
		h=15;
	else if (total > -211, total < -150)
		h=20;
	else if (total > -151, total < -100)
		h=25;
	else if (total > -101, total < -60)
		h=30;
	else if (total > -61, total < -30)
		h=35;
	else if (total > -31, total < -10)
		h=40;
	else if (total > -11, total < 0)
		h=45;
	else if (total == 0)
		h=50;
	else if (total > 0, total < 11)
		h=55;
	else if (total > 10, total < 31)
		h=60;
	else if (total > 30, total < 61)
		h=65;
	else if (total > 60, total < 101)
		h=70;
	else if (total > 100, total < 151)
		h=75;
	else if (total > 150, total < 211)
		h=80;
	else if (total > 210, total < 280)
		h=85;
	else if (total <= 280)
		h=90;
	
	return h;
}

float critical_damage(float digi_level[], float melee_stat[], float armor_stat[], int i, int t, float xtra_melee_mult[])
{
	float damaga, damagb, damagc, randomdamage;
	int f, d;
	damaga=(((digi_level [i] * melee_stat [i]) / 9) + 1);
	damagb=(((damaga - (0.3 * damaga)) / 100) * xtra_melee_mult [i]);
	damagc=(((damaga + (0.3 * damaga)) / 100) * xtra_melee_mult [i]);
	d=(damagc-damagb);
	srand( time(NULL) );
	randomdamage = ((rand()%100)*d);
	f=((((randomdamage*0.01) + damagb) * 2) - (armor_stat[t]/3));
	
		if (f<1)
		{
			f=1;
		}
	
	
		if (f>15000)
		{
			f=15000;
		}
	return f;
}
