/*Pirate2 by Dale Swanson May 22, 2007

*/

/*
Things to do:
possibly name the planets, find 20 star's names the use random numbers
add strategies to old man
check every function call, think about what they will do when called there, and what they call, note any global varible, how it changes
buffer overflow, from ship array into world array
pirates can run if slower, but must drop cargo
test boss fleet size, too small
automation stuff
enter 138 to sell everything in every ship
marooned, shipless, with money, enemy civ world, no one will do business, how do I get off?
auto 138 self repair, ship 138, also what about real ship 138, where to cap players fleet, and how
run in cmd, copy into word spell check
pirate fire then run
negative HP
Re do skill, make all constants varibles, to change with skill level, seperate starting money and skill, maybe let each constant/varible be set individuallly (maybe from a list of options, with a custom avaible).
save game, to text, maybe some mild encryption, probably not
pirates, redo ship numbers and class/models.  Something based on skill
0 debt, some savings, no civ so no taxes, just resting forever.
player 100 ship cap, check on shipping out, warn then delete
age of price data
Warn ship HP, display ship HP info, total fleet HP data, something
goal, controll all planets for 10 turns, score based on time taken
*/

using namespace std;
#include <iostream>
#include <fstream>
#include <math.h>

int ships[10][10][50]; //stats on the ships for sale
int fleet[1000][20]; //ships in your fleet, x = index, y = stats 1 - speed, 2 - cargo, 3 - attack, 4 - cost, 5 - sensor range, 6 - hit points, 7 - max HP, 8 - class, 9 - model, 10 - total cargo, 11+ goods
int pfleet[1000][20]; //ships in a pirate fleet
int world[21][20]; //worlds, x = index, y = stats.  
int goods[21][10][3]; //prices of the goods
int upgrade[21][10][3]; //prices of the ship upgrades
int inf[3]; //influence on the worlds
int dis[4]; //distance to other worlds
int cday; //the current day
int debug = 0; //sets mode to debug mode
int curworld = 3; //the world the player is currently on
long long int money; //player money
long long int savings; //players savings
long long int debt; //ammount of debt player has
long long int assets; //players total assets
float worldpen = 1.01; //penalty for being on a world not belonging to your civ
int playerciv; //civ of the player
int play; //do you want to continue to play
int traveltime; //time it will take to travel any given distance
int travelto; //world to travel to
int civ[3]; //total number of worlds each civ owns
float taxper; //percentage of tax you will pay
int skill; //player skill level
int pmoney; //pirates money
bool paggressive; //are the pirates aggressive
bool bossfight; //will determine if it's a boss fight or not
int rebel[21][10]; //1 if attack pending, 2 who will control planet
int playerleader; //civ that player controls
int playertax; //% ammount set by player if a leader (whole number)
int defense; //ammount per plannet user spends on defence
bool gameover; //if the game is over or not
float interest; //debt interest
float badworld; //worldpen for a world with a different civ
float goodworld; //worldpen for a world with same civ
float civpen; //pen for players civ in likelyhood of getting plannets in a flip
int visit[21]; //stores date of last vist to the worlds

/* General notes:
There are 5 goods and there good numbers are:
// 1 - Uranium, 2 - Tritanium, 3 - Silver Oxide, 4, - Platinum, 5 - Carbon Nanotube
note that in fleet[][] they are all +10 to the good numbers, so Uranium is stored at 11, and Nanotubes are 15.
There are 3 civs:
// 0-Neutral, 1-Galactic Star Empire, 2-United Republic of Planets
All the planets start as neutral, except planets 1 and 2, which are the home worlds to civs 1 and 2.  Planets can only go from neutral to 1 or 2, not 1 or 2 to neutral, they can also flip between 1 and 2.  Player starts as neutral.
Any other ship or fleet that isn't you is called pirates, even if friendly.  Generally any varible that deals with ships has a twin that has a p in front and deals with pirates.  Example shipnum is ship index number, pshipnum is pirate ship index number
worlds and planets are used interchangably
goods is used for all the crap you trade, don't get confussed if you see good it dosen't mean nice, positive, or whatever, rather it means tradable stuff.
player and user are used interchangably

Description of the main arrays:
fleet[][] is your fleet, the first index is ship number, the second stores the various stats.  Example fleet[1][3] is first ship, and the 3 is the attack, so this stores the attack value of your first ship.  The 0's in the arrays are used to store totals, so fleet[0][3] will be the total attack for all ships.  fleet[0][0] is the total number of ships you have, and it used quite a bit.  The various stats are:
//ships in your fleet, x = index, y = stats 1 - speed, 2 - cargo, 3 - attack, 4 - cost, 5 - sensor range, 6 - hit points, 7 - max HP, 8 - class, 9 - model, 10 - total cargo, 11-15 goods
They are pretty self explaintory, 2-cargo is the cargo capicity, and 10-total cargo is the ammount of actual cargo you have in that ship, 6-HP is your current hit points for that ship, and 7- max HP is the max HP you can have (100% health).  The other important totals are fleet[0][1] - fleet speed, this isn't a total, but rather the speed of the slowest ship, since the fleet can only move that fast. fleet[0][2] total cargo capacity, fleet[0][5] fleet sensor range, not a total, but the longest sensor range, fleet[0][6] total fleet HP, fleet[0][10] total cargo in your fleet.  fleet[0][11] totals of the different types of goods in your fleet.
pfleet[][] is just pirate fleet, everything is the same as the regular fleet array.
ships[][][] is the ships for sale.  The first two indexes replace the ship number from fleet, the last index is the same stats.  The first two indexes are ship class and model, they both range 1-3.  Each class/model combo (9 total) has it's own set of stats.  You can goto shipsetup to see the stats for all the ships.  Example ship[1][2][3] is class 1 (cargo) model 2 (med), and 3rd stat (attack).
worlds[][] is all the planets.  Like fleet[][] the first is an index and the second is the stats.  All the planets are on a 10x10 grid (actually I think its 11x11 since the grid goes 0-10), the grid cords are stored as stats 1 and 2.  The owner is stat 3.  I don't think any of the other stats are used, and none of the totals (the 0 indexes) are.
goods[][][] is the prices of the goods.  First index is planet number, second is good number, and last is just actual price or last known price.  Example good[3][2][1] stores the actual price (1), of Tritanium (2) on world 3 (3).  Second example good[14][4][2] stores the last known price (2) of Platinum (4) on world 14 (14).
upgrade[][] isn't currently used, but I want to.  It will be upgrades you can buy and store like cargo, but they will actually effect your ships stats.  Things like lasers, sheilds, extra engines, sensors, whatever.

*/

int ran(int min, int max) //returns a random number between a min, and b max, careful with number > 32k
{// custom random number gen, for no reason
	int skip; //stores ammount of times we skip numbers
	int range; //the range from min to max
	int randresult = 0; //this is your real random number that will be used to return
	int temp; //used to switch min and max if they aren't in order
	
	if (max < min)
	{//if max is less than min we switch them
		temp = max; //put max in temp so we can over write max with min
		max = min;
		min = temp;
	}
	
	range = (max - min) + 1;
	if (rand() == 138) srand(rand() + clock()); 
	//1 in 32k odds to reseed the RNG uses both a rand number and the clock (clock isn't seconds, it's system ticks, faster than seconds) 
	if ((rand() + clock()) % 9 == 1) 
	//basicly 1 in 5 chance of looping a wasting some random numbers
	{
		for (skip = ((clock() % 3) + ((rand() + clock()) % 4) + (rand() % 13) + 2); skip > 0; skip--)//comes up with a number to loop from about 10 to 70 times
		{
			rand(); //wastes some random numbers
		}
	}
	//c = a + int(range * rand() / (RAND_MAX + 1));
	randresult = int((rand() % int(max - min + 1)) + min);
   return randresult;
}

void help()
{//outputs help on the screen
	cout<<"\nPirate2 by Dale Swanson";
	cout<<"\nThis game is set in the future.  You have a spaceship and travel between various worlds (planets).";
	cout<<"\nYou will have to buy and sell goods at these worlds, for a profit.  Buy low, sell high.";
	cout<<"\nAlong the way you will come across other ships, they may attack you, or you might attack them.";
	cout<<"\nIf you lose a battle you'll lose your ships, if you win you'll get the cargo, money, and goods of the other fleet.";
	cout<<"\nThe galaxy is at war between two factions (civilizations), \nif you join one you'll only be able to do business on worlds controlled by them.";
	cout<<"\nJoining a civ does offer cheaper goods on the worlds they control and other benefits.";
	cout<<"\nYou can borrow money to get started, but pay it back quickly.  ";
	cout<<"Most the menu's are pretty self explanatory.  \nYour goal is just to get as much money as possible.";
	cout<<"You have options of what kind of ship to get.  \nShips get a small ammount of wear and tear damage per day.";
	cout<<"\nIf you get a cargo ship you'll have lot's of space but it'll be slow and weak against attack.";
	cout<<"\nYou can either make your living as a trader or a pirate, attacking other ships.";
	cout<<"\nWhat choice you make will obviously effect your ship choice.";

	cout<<"\nIt's the future, and you're a trader.  Figure it out.";
	cout<<"\nMake sure that the following line is displayed as one line:";
	cout<<"\n*********************************************************************************************************************************";
	cout<<"\nIf it's not the layout will be messed up. \nTo fix it right click the title bar, Properties, Layout tab, Window Size, Width > 130, OK, Save.";
	money += 100;//cheat to get 100 dollars every time you use help
}

void worldsetup()
{//sets up the initial values for the worlds
	int planetnum; //index number of planets
	int tempplanetnum; //second index, for use in comparing 
	int goodnum; //index number for goods
	bool good; //stores if certain conditions are meet yet
	int lowprice; //the lowest price possible for goods, used for the generating them
	int highprice; //highest possible price
	
	for (planetnum = 1; planetnum <= 20; planetnum++)
	{//loops through each planet
		do 
		{//keep looping until we come up with a location that hasn't been taken yet
			good = 1; //set good to 1, if it isn't set to 0 in the if later then we will assume a good place has been found and exit the loop
			world[planetnum][1] = ran(0, 10); //finds the x cordinate for the world
			world[planetnum][2] = ran(0, 10); //finds the y cordinate for the world
			
			for (tempplanetnum = 1; tempplanetnum < planetnum; tempplanetnum++)
			{//loop through each world that has already been placed and compare the locations to see if this location is unoccupied
				if (world[planetnum][1] == world[tempplanetnum][1] && world[planetnum][2] == world[tempplanetnum][2]) 
				{//compares the x and y cordinates for the canident location with each other world's locations, to see if it's already occupied
					good = 0; //if it is then set good to 0 since it's not good, and we will need to loop through the do loop again
					if (debug == 138) cout<<"\nplanetnum - "<<planetnum<<"\tY - "<<goodnum<<"\tX1 = "<<world[planetnum][1]<<"\tY1 = "<<world[tempplanetnum][1]<<"\tX2 = "<<world[planetnum][2]<<"\tY2 = "<<world[tempplanetnum][2];
				}
			}
		} while (good == 0); //do loop checks to see if good was set to 0, if not we exit
		
		if (debug == 138) cout<<"\n";
		for (goodnum = 1; goodnum <= 5; goodnum++)
		{//sets the initial prices for the various goods
			if (debug == 138) cout<<" Good "<<goodnum<<", Range = "<<int(pow(9, goodnum))<<" - "<<int(pow(11, goodnum));
			//goods[planetnum][goodnum][1] = ran(int(pow(9, goodnum) + 50), int(pow(11, goodnum) + 50 + (goodnum * 10))); //forumla to pick the initial cost of the goods in a certain range
			//=C35+(A35*11)+70-(A35 ^ A35)
			lowprice = pow(8, goodnum) + 50; //lowest possible price for goods
			highprice = pow(9, goodnum) + (goodnum * 11) + 70 - pow(goodnum, goodnum); //highest possible price for goods
			goods[planetnum][goodnum][1] = ran(lowprice, highprice); //forumla to pick the initial cost of the goods in a certain range
			if (debug == 138) cout<<", Result - "<<goods[planetnum][goodnum][1];
		}
	}
	world[1][3] = 1; //world 1 is home world of civ 1
	world[2][3] = 2; //world 2 is home world of civ 2
}

void shipsetup()
{// sets up the intitial prices and stats for ships
	int shipclass; //class of ship
	int shipmodel; //model of ship
	int planetnum; //index number of planet
	
	//ships in your fleet, x = index, y = stats 1 - speed, 2 - cargo, 3 - attack, 4 - cost, 5 - sensor range, 6 - hit points, 7 - max HP, 8 - class, 9 - model, 10 - total cargo, 11+ goods
	ships[1][1][1] = 5; //speed
	ships[1][1][2] = 50; //cargo
	ships[1][1][3] = 5; //attack
	ships[1][1][5] = 1; //sensor range
	ships[1][1][7] = 50; //HP
	
	ships[1][2][1] = 9; //speed
	ships[1][2][2] = 135; //cargo
	ships[1][2][3] = 7; //attack
	ships[1][2][5] = 2; //sensor range
	ships[1][2][7] = 100; //HP
	
	ships[1][3][1] = 14; //speed
	ships[1][3][2] = 255; //cargo
	ships[1][3][3] = 13; //attack
	ships[1][3][5] = 3; //sensor range
	ships[1][3][7] = 175; //HP
	
	ships[2][1][1] = 9; //speed
	ships[2][1][2] = 10; //cargo
	ships[2][1][3] = 17; //attack
	ships[2][1][5] = 2; //sensor range
	ships[2][1][7] = 75; //HP
	
	ships[2][2][1] = 15; //speed
	ships[2][2][2] = 25; //cargo
	ships[2][2][3] = 35; //attack
	ships[2][2][5] = 3; //sensor range
	ships[2][2][7] = 150; //HP
	
	ships[2][3][1] = 18; //speed
	ships[2][3][2] = 45; //cargo
	ships[2][3][3] = 65; //attack
	ships[2][3][5] = 5; //sensor range
	ships[2][3][7] = 250; //HP
	
	ships[3][1][1] = 16; //speed
	ships[3][1][2] = 5; //cargo
	ships[3][1][3] = 5; //attack
	ships[3][1][5] = 2; //sensor range
	ships[3][1][7] = 25; //HP
	
	ships[3][2][1] = 30; //speed
	ships[3][2][2] = 10; //cargo
	ships[3][2][3] = 10; //attack
	ships[3][2][5] = 3; //sensor range
	ships[3][2][7] = 50; //HP
	
	ships[3][3][1] = 45; //speed
	ships[3][3][2] = 25; //cargo
	ships[3][3][3] = 18; //attack
	ships[3][3][5] = 4; //sensor range
	ships[3][3][7] = 75; //HP
	
	
	for (shipclass = 1; shipclass <= 3; shipclass++)
	{//loops through each class
		for (shipmodel = 1; shipmodel <= 3; shipmodel++)
		{//each model			
			for (planetnum = 1; planetnum <= 20; planetnum++)
			{//loop through each planet to set up an initial price for the ships
				ships[shipclass][shipmodel][planetnum+10] = ((ships[shipclass][shipmodel][1] + 5) * 55) + ((ships[shipclass][shipmodel][2] + 5) * 25) + ((ships[shipclass][shipmodel][3] + 5) * 100) + ((ships[shipclass][shipmodel][5] + 5) * 15) + ((ships[shipclass][shipmodel][7] + 5) * 75) + (shipmodel * shipmodel * 1000); //comes up with prices based on the stats of ship
			}
		}
	}
}

void worlddisplay()
{// displays various info
	int planetnum; //planet index number
	int ycord; //y cordinate of planets
	int xcord; //x cordinate of planets
	bool good; //used to store if certain conditions are met (different from it's usual meaning of dealing with tradable stuff)
	
	
	if (debt > 85000) cout<<"\n\n***WARNING: Debt to bank is too high, pay off soon or risk seizure of property!***\n";//warning if you are getting too much debt
	cout<<"\nIndependent planets - "<<civ[0];
	cout<<", The Galactic Star Empire - "<<civ[1];
	cout<<", The United Republic of Planets - "<<civ[2];
	for (planetnum = 1; planetnum <= 20; planetnum++)
	{//loop through each world, display the stats about it
		//cout<<"\nWorld # - "<<x<<", X - "<<world[x][1]<<", Y - "<<world[x][2]<<"\t Owner - "<<world[x][3];
		printf("%s %2d %s %2d %s %2d %s %d ", "\nWorld #", planetnum, "X", world[planetnum][1], "Y", world[planetnum][2], "Owner", world[planetnum][3]);
		if (debug == 138) 
		{
			printf("%s %3d %s %3d %s %3d %s %4d %s %5d %s %3d", " Uranium", goods[planetnum][1][1], " Tritanium", goods[planetnum][2][1], " Silver Oxide", goods[planetnum][3][1], " Platinum", goods[planetnum][4][1], " Carbon Nanotube", goods[planetnum][5][1], " Data Age", 1 - 1);
		}
		
		else if (debug != 138) 
		{
			printf("%s %3d %s %3d %s %3d %s %4d %s %5d %s %3d", " Uranium", goods[planetnum][1][2], " Tritanium", goods[planetnum][2][2], " Silver Oxide", goods[planetnum][3][2], " Platinum", goods[planetnum][4][2], " Carbon Nanotube", goods[planetnum][5][2], " Data Age", cday - visit[planetnum]);
		}
		
		if (curworld == planetnum) cout<<" * ";
	}
	
	for (ycord = 10; ycord >= 0; ycord--)
	{//loop through the y cord
		printf("%s%2d ", "\n", ycord);
		//cout<<"\n"<<y<<"\t"; //displays the y grid numbers
		for (xcord = 0; xcord <= 10; xcord++)
		{//loop through the x cord
			good = 0; //good here will store if a world was in that particular grid cord, if not then we display a dot
			for (planetnum = 1; planetnum <= 20; planetnum++)
			{//loop through each of the planets, to check if they are in the current grid cord
				if (world[planetnum][1] == xcord && world[planetnum][2] == ycord)
				{//checks to see if the planet is in the cord
					cout<<world[planetnum][3]; //if so we display a number to represent its civ
					good = 1; //set good to 1 so that we don't display a dot
				}
			}
			if (good == 0) cout<<"."; //if good is 0 then there was no planet, and we need to display a dot as a placeholder
		}
		printf(" \t %2d ", ycord);
		//cout<<"\t"<<y<<"\t"; //displays the y grid numbers
		for (xcord = 0; xcord <= 10; xcord++)
		{//loop through the x cord
			good = 0; //good here will store if a world was in that particular grid cord, if not then we display a dot
			for (planetnum = 1; planetnum <= 20; planetnum++)
			{//loop through each of the planets, to check if they are in the current grid cord
				if (world[planetnum][1] == xcord && world[planetnum][2] == ycord)
				{//checks to see if the planet is in the cord
					printf("%02d", planetnum);
					good = 1; //set good to 1 so that we don't display a dot
				}
			}
			if (good == 0) cout<<" ."; //if good is 0 then there was no planet, and we need to display a dot as a placeholder
		}
	}
	
	cout<<"\n   ";
	for (xcord = 0; xcord <= 10; xcord++)
	{//display the X grid numbers
		cout<<xcord;
	}
	
	cout<<"\t    ";
	for (xcord = 0; xcord <= 10; xcord++)
	{//display the X grid numbers
		printf("%02d", xcord);
	}
}

void flips()
{//determines which planets will flip and to who
	int planetnum; //index number of planets
	int tempplanetnum; //a temp planet index
	
	civ[0] = 0; //total number of plannets the civ controls, set it to 0 for all so we can add it up
	civ[1] = 0;
	civ[2] = 0;
	if (money - defense < defense * 10 && playerleader > 0)
	{//a warning if money is getting too low to keep up defense spending
		cout<<"\n\n***WARNING: Treasury getting low, defense spending could be halted***\n";
	}
	
	for (planetnum = 1; planetnum <= 20; planetnum++)
	{//loops through each planet
		if (money - defense < 10000 && playerleader > 0)
		{//a warning that money is to low to keep up defense spending
			cout<<"\n\n***WARNING: Treasury too low, defense spending halted***\n";
			defense = 0; //set defense spending to 0 so that player dosen't go bankrupt
		}
		if (world[planetnum][3] == playerleader) money -= defense; //if player owns planet he pays defense spending for it
		if (ran(1, 10) == 1)
		{//1 out of 10 odds of flipping
			inf[0] = 0; //the influence of civ 0
			inf[1] = 0; //the influence of civ 1
			inf[2] = 0; //the influence of civ 2
			for (tempplanetnum = 1; tempplanetnum <= 20; tempplanetnum++)
			{//loop through each planter again, to compare the distances, to determine the influence.  Uses A^2 + B^2 = C^2
				dis[1] = abs(world[planetnum][1] - world[tempplanetnum][1]); //finds A
				dis[2] = abs(world[planetnum][2] - world[tempplanetnum][2]); //finds B
				dis[3] = int(sqrt(pow(dis[1], 2) + pow(dis[2], 2))); //finds C using A^2 + B^2 = C^2
				if (dis[3] == 0)
				{//since the influence is 10/distance if distance is 0 we'd get divide by 0, so we just set it to something
					inf[world[tempplanetnum][3]] += 50; //the higher this is the harder it'll be for a planet to flip to another civ
				}
				else
				{//if the distance is anything > 0 then we'll use this formula to determine the influence
					//cout<<"\ndis[3] - "<<dis[3];
					inf[world[tempplanetnum][3]] += ((10 / dis[3]) + 1); //formula for influence is just (10/distance) + 1.
				}
			}
			inf[playerciv] *= civpen; //skill level penelty for player's civ
			
			inf[1]++;
			inf[2]++; //ensures each civ has some chance of gettting the planet, even if they are wiped out
			
			if (playerleader > 0)
			{// if the player is the leader of a civ
				if (debug == 138) cout<<"\nBefore defense spending - "<<inf[playerleader];
				inf[playerleader] += (defense - 10000) / 500;//add players defense spending to inf, 10k is neutral, anything less hurts anything more helps, every 500 either way is 1 inf point
				if (debug == 138) cout<<", After defense spending - "<<inf[playerleader];
			}
			
			
			if (debug == 138) cout<<"\nWorld # "<<planetnum<<", inf 1 - "<<inf[1]<<", inf 2 - "<<inf[2];
			if (ran(1, inf[1] + inf[2]) <= inf[1]) 
			{//randomly decided if plannet will flip.  If the random number in the range of 1 - inf1 + inf2 is <= to inf1 then civ 1 gets it, if not civ 2 does
				if (world[planetnum][3] != playerleader && playerleader == 1) cout<<"\nOur troops have successfully taken planet "<<planetnum<<"!";
				if (world[planetnum][3] == playerleader && playerleader != 1 && playerleader != 0) cout<<"\nWe have lost planet "<<planetnum<<"!";
				world[planetnum][3] = 1; //sets the new owner of the plannet to 1 
				if (debug == 138) cout<<" 1 gets it ";
			}
			else
			{//If the random number in the range of 1 - inf1 + inf2 is not <= to inf1 then civ 2 gets it.
				if (world[planetnum][3] != playerleader && playerleader == 2) cout<<"\nOur troops have successfully taken planet "<<planetnum<<"!";
				if (world[planetnum][3] == playerleader && playerleader != 2 && playerleader != 0) cout<<"\nWe have lost planet "<<planetnum<<"!";
				world[planetnum][3] = 2; //sets the new owner of the plannet to 2
				if (debug == 138) cout<<" 2 gets it ";
			}
			if (debug == 138) worlddisplay(); //displays the current world info
			//system("PAUSE");
		}
		
		if (ran(1, 1000) < playertax && world[planetnum][3] == playerleader && playerleader > 0)
		{//decide if planets will rebel because of taxes.  10% taxes should yeild 1/50 odds of rebellion.  Odds for rebellion are taxes / 500
			cout<<"\n\n***Citizens on world "<<planetnum<<" have rebelled against you, citing high taxes!***\n";
			if (playerleader == 1) world[planetnum][3] = 2; //switch planet control
			if (playerleader == 2) world[planetnum][3] = 1;
		}
		civ[world[planetnum][3]]++; //adds up the number of planets under control of the civ
	}
}

void costs()
{//determines the ammount of change in prices of the various goods
	int planetnum; //planet index number
	int goodnum; //good index number
	
	for (planetnum = 1; planetnum <= 20; planetnum++)
	{//loops through each planet
		for (goodnum = 1; goodnum <= 5; goodnum++)
		{//loops through each good
			goods[planetnum][goodnum][1] += ran((goods[planetnum][goodnum][1] * -0.02) - 1, (goods[planetnum][goodnum][1] * 0.021) + 2); //determines the ammount of change each price will get
			if (goods[planetnum][goodnum][1] < 5) goods[planetnum][goodnum][1] = 5; //checks to see if the price is < 5, if so set it at 5, prevents negative prices
		}
	}
}

void speedship()
{// calculates the speed of your fleet, by finding the slowest ship in it
	int shipnum; //ship index number
	
	fleet[0][1] = fleet[1][1]; //sets the fleet speed to that of your first ship
	for (shipnum = 1; shipnum <= fleet[0][0]; shipnum++)
	{//loop through each ship, check to see if it's slower than the current fleet speed
		if (debug == 138) cout<<"\n fleet[0][1] - "<<fleet[0][1]<<" fleet[shipnum][1] - "<<fleet[shipnum][1];
		if (fleet[shipnum][1] < fleet[0][1]) fleet[0][1] = fleet[shipnum][1]; //if it is slower than it becomes the new fleet speed
	}
}

void pspeedship()
{// calculates the speed of pirate fleet, by finding the slowest ship in it
	int shipnum;//ship index number
	
	pfleet[0][1] = pfleet[1][1]; //sets the fleet speed to that of the first ship
	for (shipnum = 1; shipnum <= pfleet[0][0]; shipnum++)
	{//loop through each ship, check to see if it's slower than the current fleet speed
		if (debug == 138) cout<<"\n pfleet[0][1] - "<<pfleet[0][1]<<" pfleet[shipnum][1] - "<<pfleet[shipnum][1];
		if (pfleet[shipnum][1] < pfleet[0][1]) pfleet[0][1] = pfleet[shipnum][1]; //if it is slower than it becomes the new fleet speed
	}
}

void senseship()
{// calculates the sensor range of your fleet, by finding the longest sensor range ship in it
	int shipnum;//ship index number
	
	fleet[0][5] = fleet[1][5]; //sets the range to that of your first ship
	for (shipnum = 1; shipnum <= fleet[0][0]; shipnum++)
	{//loop through each ship, check to see if it's farther than the current fleet range
		if (debug == 138) cout<<"\n fleet[0][5] - "<<fleet[0][5]<<" fleet[shipnum][5] - "<<fleet[shipnum][5];
		if (fleet[shipnum][5] > fleet[0][5]) fleet[0][5] = fleet[shipnum][5]; //if it is farther than it becomes the new fleet range
	}
}

void psenseship()
{// calculates the sensor range of pirate fleet, by finding the longest sensor range ship in it
	int shipnum; //ship index number
	
	pfleet[0][5] = pfleet[1][5]; //sets the range to that of the first ship
	for (shipnum = 1; shipnum <= pfleet[0][0]; shipnum++)
	{//loop through each ship, check to see if it's farther than the current fleet range
		if (debug == 138) cout<<"\n pfleet[0][5] - "<<pfleet[0][5]<<" pfleet[shipnum][5] - "<<pfleet[shipnum][5];
		if (pfleet[shipnum][5] > pfleet[0][5]) pfleet[0][5] = pfleet[shipnum][5]; //if it is farther than it becomes the new fleet range
	}
}

void loseship(int lostship)
{//takes ship index # passed to it and destorys it, assumes the player has allready been paid for it (if selling).  Any cargo or ship value will be lost
	int shipnum;//ship index number
	int statnum;//stat index number
	
	//the following subtracts the ship we are losing form the fleet totals (stored as ship 0 in the fleet array)
	fleet[0][2] -= fleet[lostship][2]; //cargo
	fleet[0][3] -= fleet[lostship][3]; //attack
	fleet[0][4] -= fleet[lostship][4]; //cost,
	fleet[0][5] -= fleet[lostship][5]; //sensors
	fleet[0][6] -= fleet[lostship][6]; //hit points
	fleet[0][7] -= fleet[lostship][7]; //max hit points
	fleet[0][10] -= fleet[lostship][10]; //cargo used
	fleet[0][11] -= fleet[lostship][11]; //various total cargos
	fleet[0][12] -= fleet[lostship][12];
	fleet[0][13] -= fleet[lostship][13];
	fleet[0][14] -= fleet[lostship][14];
	fleet[0][15] -= fleet[lostship][15];
	
	for (shipnum = lostship; shipnum <= fleet[0][0]; shipnum++)
	{//loops through each ship, starting at the ship you will lose, replaces that ship with ship above it
		for (statnum = 1; statnum <= 20; statnum++)
		{//loops through each stat, to move each one
			fleet[shipnum][statnum] = fleet[shipnum + 1][statnum]; //actually moves the stats
		}
	}
	fleet[0][0]--; //lowers the total count of ships
	speedship(); //calculates the speed of the fleet, by finding the slowest ship
	senseship(); //calculates the sensor range of the fleet
}
	
void listships()
{//lists the ships currently in your fleet
	int shipnum;//ship index number
	
	cout<<"\nCurrently in your fleet you have:";
	for (shipnum = 1; shipnum <= fleet[0][0]; shipnum++)
	{//loop through each ship in your fleet, so that the stats can be shown
		cout<<"\nShip # "<<shipnum<<" - Speed - "<<fleet[shipnum][1]<<", Cargo - "<<fleet[shipnum][2]<<", Attack - "<<fleet[shipnum][3]<<", Value - "<<fleet[shipnum][4]<<", Sensor - "<<fleet[shipnum][5]<<", Hit Points - "<<fleet[shipnum][6]<<", Max HP - "<<fleet[shipnum][7]<<", Class - "<<fleet[shipnum][8]<<", Model - "<<fleet[shipnum][9];
		//ships in your fleet, shipnum = index, y = stats 1 - speed, 2 - cargo, 3 - attack, 4 - cost, 5 - sensor range, 6 - hit points, 7 - max HP, 8 - class, 9 - model, 11+ goods
	}
}

void plistships()
{//lists the ships currently in the pirate fleet
	int shipnum;//ship index number
	
	if (debug == 138) cout<<"\nPirate money - "<<pmoney<<", Aggressive - "<<paggressive;
	cout<<"\nCurrently the enemy fleet has:";
	for (shipnum = 1; shipnum <= pfleet[0][0]; shipnum++)
	{//loop through each ship in pirate fleet, so that the stats can be shown
		cout<<"\nShip # "<<shipnum<<" - Speed - "<<pfleet[shipnum][1]<<", Cargo - "<<pfleet[shipnum][2]<<", Attack - "<<pfleet[shipnum][3]<<", Value - "<<pfleet[shipnum][4]<<", Sensor - "<<pfleet[shipnum][5]<<", Hit Points - "<<pfleet[shipnum][6]<<", Max HP - "<<pfleet[shipnum][7]<<", Class - "<<pfleet[shipnum][8]<<", Model - "<<pfleet[shipnum][9];
		if (debug == 138) cout<<"\nShip # "<<shipnum<<" - Uranium - "<<pfleet[shipnum][11]<<", Tritanium - "<<pfleet[shipnum][12]<<", Silver Oxide - "<<pfleet[shipnum][13]<<", Platinum - "<<pfleet[shipnum][14]<<", Carbon Nanotube - "<<pfleet[shipnum][15];
		//ships in pirate fleet, shipnum = index, y = stats 1 - speed, 2 - cargo, 3 - attack, 4 - cost, 5 - sensor range, 6 - hit points, 7 - max HP, 8 - class, 9 - model, 11+ goods
	}
}

void calcassets()
{//calculates the total assets player has
	int shipnum;//ship index number
	
	assets = (money + savings) - debt; //your assets start as just your money and savings minus your debt
	for (shipnum = 1; shipnum <= fleet[0][0]; shipnum++)
	{//loop through each ship, so that it's value can be added to your assets
		assets += fleet[shipnum][4]; //adds value of ship to your assets
		assets += fleet[shipnum][11] * goods[curworld][1][1]; //adds value of various goods to assets
		assets += fleet[shipnum][12] * goods[curworld][2][1];
		assets += fleet[shipnum][13] * goods[curworld][3][1];
		assets += fleet[shipnum][14] * goods[curworld][4][1];
		assets += fleet[shipnum][15] * goods[curworld][5][1];
	}
}

void groundday()
{//time on the ground, a terrible way of doing it, but neccassary because can't call day() from above it, and need to call it from stuff that it calls (namly pirate())
	long long int taxed; //ammount paid in taxes
	int tempplanetnum; //a temp planet index number
	int planetnum;//planet index number
	
	cday++; //increace the current day counter
	money -= 3; //3 dollars a day to pay for food I guess, ships aren't in space so we don't pay for fuel and stuff
	debt *= interest; //1% per day interest on loans
	savings *= 1.005; //.5% per day interest on savings
	if (cday % 15 == 0 && playerciv > 0)
	{//every 15 days you pay taxes if you are a memeber of a civ
		if (playerleader == 0)
		{//if player isn't the leader of a civ then we will charge them taxes
			cout<<"\nPlayer civ - "<<playerciv;
			calcassets(); //find the assests
			if (assets < 1000) assets = 1000;
			taxper = (civ[playerciv] + 1) * 0.01; //tax % is number of planets civ owns * 1%, so 10 planets is 10%
			cout<<"\nTax time";
			cout<<"\nTaxable assest = "<<assets<<", taxes paid = "<<int(assets * taxper);
			money -= int(assets * taxper);//% taxes on total assets
		}
		else if (playerleader > 0)
		{// if player is the leader of a civ then they get paid the taxes
			cout<<"\nTax time";
			cout<<"\nWelcome leader of ";
			if (playerleader == 0) cout<<"no civilization";
			if (playerleader == 1) cout<<"The Galactic Star Empire";
			if (playerleader == 2) cout<<"The United Republic of Planets";
			cout<<"\nindependent planets - "<<civ[0];
			cout<<"\nPlanets under the control of The Galactic Star Empire - "<<civ[1];
			cout<<"\nPlanets under the control of The United Republic of Planets - "<<civ[2];
			cout<<"\nYour current tax rate is "<<playertax<<"% your Defense spending per planet is "<<defense<<", treasury is "<<money;
			
			taxper = playertax * 0.01; //playertax is a whole number 10% is stored as just 10, so we multiply by .01 to get a fraction of 100 percentage (10% is .10)
			taxed = ran(10, 25) * taxper * (civ[playerleader] + 1) * 100000; //taxes paid are (ran 10-25 * tax % * planets + 1 * 1000,000 Example 15 * .10 * 8 * 100,000 = 1,200,000 (15 ran, 8 planets)
			taxed += ran(0, playertax * 3) * ran(0, playertax * 50); //stupid attempt to make tax ammount non round number, needs work
			money += taxed; //give player the tax money
			if (debug == 138) cout<<"\ntaxper - "<<taxper<<"(civ[playerleader] + 1) - "<<(civ[playerleader] + 1);
			cout<<"\nTaxes paid - "<<taxed<<", new treasury - "<<money;
		}
	}
	flips(); //determines if plannets will flip
	costs(); //determines the rise and fall of prices
	
	if (rebel[0][0] > 0 && ran (1, 10) == 1)
	{//code to see if rebels lauch their paid for attack
		tempplanetnum = ran(1, rebel[0][0]);
		for (planetnum = 1; tempplanetnum > 0; planetnum++)
		{
			if (rebel[planetnum][1] = 1) tempplanetnum--;
		}
		cout<<"\n\n***The mercenaries you payed have launced their rebellion on world "<<planetnum<<"!***\n";
		if (ran(1, 3) == 1)
		{//1 in 3 odds of them winning
			cout<<"\nThey have succefully taken control of world "<<planetnum<<" from the ";
			if (world[rebel[planetnum][2]][3] == 1) cout<<"Galactic Star Empire";
			if (world[rebel[planetnum][2]][3] == 2) cout<<"United Republic of Planets";
			if (world[rebel[planetnum][2]][3] == 0) cout<<"opposing rebel forces";
			cout<<"\nThey have transfered control to the ";
			if (rebel[planetnum][2] == 1) cout<<"Galactic Star Empire";
			if (rebel[planetnum][2] == 2) cout<<"United Republic of Planets";
			if (rebel[planetnum][2] == 0) cout<<"allied rebel forces";
			cout<<"!";
			world[planetnum][3] = rebel[planetnum][2];
		}
		else 
		{//other wise they lose
			cout<<"\nThey were unsuccessful.";
		}
		rebel[0][0]--;
		rebel[planetnum][1] = 0;
		rebel[planetnum][2] = 0;
	}
	
	for (planetnum = 1; planetnum <= 20; planetnum++)
	{
		if (world[planetnum][3] == playerleader && playerleader != 0)
		{
			goods[planetnum][1][2] = goods[planetnum][1][1]; //updates the price list for this world
			goods[planetnum][2][2] = goods[planetnum][2][1];
			goods[planetnum][3][2] = goods[planetnum][3][1];
			goods[planetnum][4][2] = goods[planetnum][4][1];
			goods[planetnum][5][2] = goods[planetnum][5][1];
		}
	}
}

void selfrepair()
{//self repair a ship, less money but takes time
	int shiprepair;//number of ship to repair
	int repairtime;//time to repair
	int confirm;//user confirmation
	int shipnum;//ship index number
	
	do
	{//keep looping until user enters 0 as the ship number to repair
		listships(); //shows ships in players fleet
		cout<<"\nSelf Ship Repair";
		cout<<"\nEnter the ship number you want to repair. Enter 0 to leave. Enter 138 to repair all. ";
		cin>>shiprepair; //player chooses ship he wants to repair
		if ((shiprepair < 1 || shiprepair > fleet[0][0]) && shiprepair != 138)
		{//if the index # for the ship they want to repair is below 1 or above the max in the fleet it's invalid
			cout<<"\nNot a valid ship number.";
			return;//if they enter an invaild ship number exit
		}
		else if (shiprepair == 138)
		{//auto repair all ships
			for (shipnum = 1; shipnum <= fleet[0][0]; shipnum++)
			{//loop through all ships repair each one
				if (money < (fleet[shipnum][7] - fleet[shipnum][6]) * 6) break; //if they don't have the money break
				repairtime = int((fleet[shipnum][7] - fleet[shipnum][6]) / 19) + 1; //calculate repair time
				for (;repairtime > 0; repairtime--)
				{//loop through the repair time until repairs are done, it does most the normal day stuff, excpet stuff that would only be done in space
					groundday();//a day goes by
				}
				cout<<"\nRepairs done!";
				money -= (fleet[shipnum][7] - fleet[shipnum][6]) * 6; //takes money for repairs
				fleet[0][6] += (fleet[shipnum][7] - fleet[shipnum][6]); //adds HP to fleet total
				fleet[shipnum][6] = fleet[shipnum][7]; //maxes out HP
			}
		}
		else if (shiprepair >= 1 && shiprepair <= fleet[0][0])
		{//if the index # is good we repair the ship
			if (fleet[0][0] > 130) cout<<"\nfleet[0][0] - "<<fleet[0][0];
			repairtime = int((fleet[shiprepair][7] - fleet[shiprepair][6]) / 19) + 1; //calculate repair time
			cout<<"\nRepairs on this ship will cost "<<(fleet[shiprepair][7] - fleet[shiprepair][6]) * 6<<", and take "<<repairtime<<" day";
			if (repairtime > 1) cout<<"s"; //proper plurization
			cout<<"\nAre you sure you want to repair this ship:";
			cout<<"\nShip # "<<shiprepair<<" - Speed - "<<fleet[shiprepair][1]<<", Cargo - "<<fleet[shiprepair][2]<<", Attack - "<<fleet[shiprepair][3]<<", Value - "<<int(fleet[shiprepair][4] * (2 - worldpen))<<", Sensor - "<<fleet[shiprepair][5]<<", Hit Points - "<<fleet[shiprepair][6]<<", Max HP - "<<fleet[shiprepair][7]<<", Class - "<<fleet[shiprepair][8]<<", Model - "<<fleet[shiprepair][9];
			cout<<"\nEnter 138 to repair for "<<(fleet[shiprepair][7] - fleet[shiprepair][6]) * 6<<" ";
			cin>>confirm;//get user confirmation
			if (confirm != 138)
			{//if they don't enter 138 as the confirmation we don't repair
				cout<<"\nYou probably don't know how to do it anyway.";
				return;
			}
			else if ((fleet[shiprepair][7] - fleet[shiprepair][6]) * 6 <= money)
			{//if they do enter 138 we do repair
				for (;repairtime > 0; repairtime--)
				{//loop through the repair time until repairs are done, it does most the normal day stuff, excpet stuff that would only be done in space
					groundday();
				}
				cout<<"\nRepairs done!";
				money -= (fleet[shiprepair][7] - fleet[shiprepair][6]) * 6; //takes money for repairs
				fleet[0][6] += (fleet[shiprepair][7] - fleet[shiprepair][6]); //adds HP to fleet total
				fleet[shiprepair][6] = fleet[shiprepair][7]; //maxes out HP
			}
		}
	} while (shiprepair != 0);
}

void buyship()
{//shipyard, all ship functions, buy, sell, repair
	int buysell; //determines if we are in buy or sell mode
	int shipclass; //ship class
	int shipmodel; //ship model
	int shipnum; //ship index number
	int confirm; //user confirmation
	
	if (debug == 138) cout<<" player civ - "<<playerciv<<", cur world - "<<world[curworld][3];
	if (playerciv != world[curworld][3] && world[curworld][3] > 0 && playerciv > 0) 
	{//don't do business with players of other civs
		cout<<"\nWe don't do business with your kind around these parts.";
		return;
	}
	cout<<"\nWelcome to the ship yard";
	cout<<"\nWould you like to 1 - Buy a Ship, 2 - Sell a Ship, 3 - Repair a Ship, 4 - Self Repair a Ship, 5 - Filtered Sale? ";
	cin>>buysell;

	if (buysell == 1)
	{//buying a ship
		cout<<"\nCargo Class - 1, Attack Class - 2, Speed Class - 3";
		for (shipclass = 1; shipclass <= 3; shipclass++)
		{//loop through each class of ship, to display the stats
			for (shipmodel = 1; shipmodel <= 3; shipmodel++)
			{//loop through each model of class to display the stats
				printf("%s %d %s %d %s %2d %s %3d %s %2d %s %d %s %3d %s %5d", "\nClass", shipclass, "Model", shipmodel, "Speed", ships[shipclass][shipmodel][1], "Cargo", ships[shipclass][shipmodel][2], "Attack", ships[shipclass][shipmodel][3], "Sensor", ships[shipclass][shipmodel][5], "HP", ships[shipclass][shipmodel][7], "Cost", int(ships[shipclass][shipmodel][10 + curworld] * worldpen));
			}
		}
		
		cout<<"\nEnter the Class followed by the Model of ship you want, enter 0 for both to leave. ";
		cin>>shipclass>>shipmodel; //the class followed by model of ship
		if (shipclass != 0 && shipmodel != 0 && shipclass < 5 && shipmodel < 5)
		{//if the choice isn't 0 0, and the model and class choices aren't above the limits we will buy a ship
			cout<<"\nAre you sure you want to buy ship Class "<<shipclass<<", Model "<<shipmodel<<", Speed - "<<ships[shipclass][shipmodel][1]<<", Cargo - "<<ships[shipclass][shipmodel][2]<<", Attack - "<<ships[shipclass][shipmodel][3]<<", Cost - "<<int(ships[shipclass][shipmodel][10 + curworld] * worldpen)<<"? Enter 138 to confirm ";
			if (money > ships[shipclass][shipmodel][10 + curworld] * worldpen * 10) cout<<"\nEnter 1138 to auto buy 10 ships at once ";
			cin>>confirm; //confirms they want to buy this ship, type 138 to confirm
			if (confirm != 138 && confirm != 1138)
			{//if they don't want to buy the ship
				cout<<"\nFine don't buy it, I don't care.";
			}
			else if (ships[shipclass][shipmodel][10 + curworld] * worldpen > money)
			{//if they don't have enough money
				cout<<"\nHey! You don't even have enough money for this.  What are you trying to pull?";
			}
			else if (confirm == 138)
			{//if they do want to buy the ship, and have the money
				//ships in your fleet, x = index, y = stats 1 - speed, 2 - cargo, 3 - attack, 4 - cost, 5 - sensor range, 6 - hit points, 7 - max HP, 8 - class, 9 - model, 11+ goods
				cout<<"\nShip bought!  Enjoy it.";
				money -= ships[shipclass][shipmodel][10 + curworld] * worldpen; //takes away the money
				fleet[0][0]++; //increases the count of ships in your fleet
				fleet[fleet[0][0]][1] = ships[shipclass][shipmodel][1]; //speed
				fleet[fleet[0][0]][2] = ships[shipclass][shipmodel][2]; //cargo
				fleet[fleet[0][0]][3] = ships[shipclass][shipmodel][3]; //attack
				fleet[fleet[0][0]][4] = ships[shipclass][shipmodel][10 + curworld] / 3; //cost, is 33% of price since now used (just like a car)
				fleet[fleet[0][0]][5] = ships[shipclass][shipmodel][5]; //sensors
				fleet[fleet[0][0]][6] = ships[shipclass][shipmodel][7]; //hit points
				fleet[fleet[0][0]][7] = ships[shipclass][shipmodel][7]; //max hit points
				fleet[fleet[0][0]][8] = shipclass; //class
				fleet[fleet[0][0]][9] = shipmodel; //model
				
				//These will add the various stats to the fleet totals (stored as ship 0 in the fleet)
				//fleet[0][1] += ships[x][y][1]; //speed
				fleet[0][2] += ships[shipclass][shipmodel][2]; //cargo
				fleet[0][3] += ships[shipclass][shipmodel][3]; //attack
				fleet[0][4] += ships[shipclass][shipmodel][10 + curworld] / 3; //cost, is 33% of price since now used (just like a car)
				fleet[0][5] += ships[shipclass][shipmodel][5]; //sensors
				fleet[0][6] += ships[shipclass][shipmodel][7]; //hit points
				fleet[0][7] += ships[shipclass][shipmodel][7]; //max hit points
				
			}
			else if (confirm == 1138 && money > ships[shipclass][shipmodel][10 + curworld] * worldpen * 10)
			{//auto buy 10 ships at a time
				for (shipnum = 1; shipnum <= 10; shipnum++)
				{//loop through and buy the 10 ships
					cout<<"\nShip bought!  Enjoy it.";
					money -= ships[shipclass][shipmodel][10 + curworld] * worldpen; //takes away the money
					fleet[0][0]++; //increases the count of ships in your fleet
					fleet[fleet[0][0]][1] = ships[shipclass][shipmodel][1]; //speed
					fleet[fleet[0][0]][2] = ships[shipclass][shipmodel][2]; //cargo
					fleet[fleet[0][0]][3] = ships[shipclass][shipmodel][3]; //attack
					fleet[fleet[0][0]][4] = ships[shipclass][shipmodel][10 + curworld] / 3; //cost, is 33% of price since now used (just like a car)
					fleet[fleet[0][0]][5] = ships[shipclass][shipmodel][5]; //sensors
					fleet[fleet[0][0]][6] = ships[shipclass][shipmodel][7]; //hit points
					fleet[fleet[0][0]][7] = ships[shipclass][shipmodel][7]; //max hit points
					fleet[fleet[0][0]][8] = shipclass; //class
					fleet[fleet[0][0]][9] = shipmodel; //model
					
					//These will add the various stats to the fleet totals (stored as ship 0 in the fleet)
					//fleet[0][1] += ships[x][y][1]; //speed
					fleet[0][2] += ships[shipclass][shipmodel][2]; //cargo
					fleet[0][3] += ships[shipclass][shipmodel][3]; //attack
					fleet[0][4] += ships[shipclass][shipmodel][10 + curworld] / 3; //cost, is 33% of price since now used (just like a car)
					fleet[0][5] += ships[shipclass][shipmodel][5]; //sensors
					fleet[0][6] += ships[shipclass][shipmodel][7]; //hit points
					fleet[0][7] += ships[shipclass][shipmodel][7]; //max hit points
				}
			}
			speedship(); //calculates the speed of the fleet, by finding the slowest ship
			senseship();
		}
	}
	else if (buysell == 2)
	{//selling a ship
		listships(); //shows ships in players fleet
		cout<<"\nEnter the ship number you want to sell. ";
		cin>>shipnum;
		if (shipnum < 1 || shipnum > fleet[0][0])
		{//if the index # for the ship they want to sell is below 1 or above the max in the fleet it's invalid
			cout<<"\nInvalid Ship Number.";
			return;
		}
		else
		{//if the index # is good we sell the ship
			cout<<"\nAre you sure you want to sell this ship:";
			cout<<"\nShip # "<<shipnum<<" - Speed - "<<fleet[shipnum][1]<<", Cargo - "<<fleet[shipnum][2]<<", Attack - "<<fleet[shipnum][3]<<", Value - "<<int((fleet[shipnum][4] * (2 - worldpen)) - ((fleet[shipnum][7] - fleet[shipnum][6]) * 18 * worldpen))<<", Sensor - "<<fleet[shipnum][5]<<", Hit Points - "<<fleet[shipnum][6]<<", Max HP - "<<fleet[shipnum][7]<<", Class - "<<fleet[shipnum][8]<<", Model - "<<fleet[shipnum][9];
			if (fleet[shipnum][10] > 0) cout<<"\n\n***WARNING: This ship contains cargo, you lose the cargo and receive no money for it!***\n";
			cout<<"\nEnter 138 to sell for "<<int((fleet[shipnum][4] * (2 - worldpen)) - ((fleet[shipnum][7] - fleet[shipnum][6]) * 18 * worldpen))<<" ";
			cin>>confirm;
			if (confirm != 138)
			{//if they don't enter 138 as the confirmation we don't sell
				cout<<"\nFine I didn't want that ship anyway.";
				return;
			}
			else
			{//if they do enter 138 we do sell
				cout<<"\nShip sold!";
				money += int((fleet[shipnum][4] * (2 - worldpen)) - ((fleet[shipnum][7] - fleet[shipnum][6]) * 18 * worldpen)); //increases their money
				loseship(shipnum); //goes through the process of removing the ship, and adjusting the fleet array
				speedship(); //calculates the speed of the fleet, by finding the slowest ship
				senseship();
			}
		}
	}
	
	else if (buysell == 3)
	{//repair ship, more expensive than self repair, but same day
		listships(); //shows ships in players fleet
		cout<<"\nEnter the ship number you want to repair. ";
		cin>>shipnum; //player choices ship he wants to repair
		if (shipnum < 1 || shipnum > fleet[0][0])
		{//if the index # for the ship they want to sell is below 1 or above the max in the fleet it's invalid
			cout<<"\nInvalid ship number.";
			return;
		}
		else
		{//if the index # is good we repair the ship
			cout<<"\nRepairs on this ship will cost "<<int((fleet[shipnum][7] - fleet[shipnum][6]) * 18 * worldpen); //repairs cost a lot for same day service
			cout<<"\nAre you sure you want to repair this ship:";
			cout<<"\nShip # "<<shipnum<<" - Speed - "<<fleet[shipnum][1]<<", Cargo - "<<fleet[shipnum][2]<<", Attack - "<<fleet[shipnum][3]<<", Value - "<<int(fleet[shipnum][4] * (2 - worldpen))<<", Sensor - "<<fleet[shipnum][5]<<", Hit Points - "<<fleet[shipnum][6]<<", Max HP - "<<fleet[shipnum][7]<<", Class - "<<fleet[shipnum][8]<<", Model - "<<fleet[shipnum][9];
			cout<<"\nEnter 138 to repair for "<<int((fleet[shipnum][7] - fleet[shipnum][6]) * 25 * (2 - worldpen))<<" ";
			cin>>confirm;//confirmatin to repair
			if (confirm != 138)
			{//if they don't enter 138 as the confirmation we don't repair
				cout<<"\nFine, that ship's a piece of shit anyway.";
				return;
			}
			else
			{//if they do enter 138 we do repair
				cout<<"\nRepairs done!";
				money -= int((fleet[shipnum][7] - fleet[shipnum][6]) * 18 * worldpen); //takes money for repairs
				fleet[0][6] += (fleet[shipnum][7] - fleet[shipnum][6]); //add HP to fleet total
				fleet[shipnum][6] = fleet[shipnum][7]; //maxes out HP
			}
		}
		
	}
	
	else if (buysell == 4) selfrepair();
	
	else if (buysell == 5)
	{//filtered selling a ship
		listships(); //shows ships in players fleet
		cout<<"\nEnter ship class followed by model you want to keep, all other ships will be sold. ";
		cin>>shipclass>>shipmodel;
		if ((shipclass < 1 || shipclass > 3) && (shipmodel < 1 || shipmodel > 3))
		{//if the index # for the ship they want to sell is below 1 or above the max in the fleet it's invalid
			cout<<"\nInvalid Ship Number.";
			return;
		}
		else
		{//if the index # is good we sell the ship
			cout<<"\nAre you sure you want to sell all ships other than class "<<shipclass<<", model "<<shipmodel;
			cout<<"\nEnter 138 to sell"<<" ";
			cin>>confirm;
			if (confirm != 138)
			{//if they don't enter 138 as the confirmation we don't sell
				cout<<"\nFine I didn't want those ships anyway.";
				return;
			}
			else
			{//if they do enter 138 we do sell
				for (shipnum = fleet[0][0]; shipnum > 0; shipnum--)
				{
					if (fleet[shipnum][10] == 0 && (fleet[shipnum][8] != shipclass || fleet[shipnum][9] != shipmodel))
					{
						cout<<"\nShip sold!";
						money += int((fleet[shipnum][4] * (2 - worldpen)) - ((fleet[shipnum][7] - fleet[shipnum][6]) * 18 * worldpen)); //increases their money
						loseship(shipnum); //goes through the process of removing the ship, and adjusting the fleet array
					}
				}
			}
		}
		speedship(); //calculates the speed of the fleet, by finding the slowest ship
		senseship();
	}
	
	else if (buysell == 138)
	{//free ships
		shipclass = 2;
		shipmodel = 3;
		for (shipnum = 1; shipnum <= 10; shipnum++)
		{//if they do want to buy the ship, and have the money
			//ships in your fleet, x = index, y = stats 1 - speed, 2 - cargo, 3 - attack, 4 - cost, 5 - sensor range, 6 - hit points, 7 - max HP, 8 - class, 9 - model, 11+ goods
			fleet[0][0]++; //increases the count of ships in your fleet
			fleet[fleet[0][0]][1] = ships[shipclass][shipmodel][1]; //speed
			fleet[fleet[0][0]][2] = ships[shipclass][shipmodel][2]; //cargo
			fleet[fleet[0][0]][3] = ships[shipclass][shipmodel][3]; //attack
			fleet[fleet[0][0]][4] = ships[shipclass][shipmodel][10 + curworld] / 3; //cost, is 33% of price since now used (just like a car)
			fleet[fleet[0][0]][5] = ships[shipclass][shipmodel][5]; //sensors
			fleet[fleet[0][0]][6] = ships[shipclass][shipmodel][7]; //hit points
			fleet[fleet[0][0]][7] = ships[shipclass][shipmodel][7]; //max hit points
			fleet[fleet[0][0]][8] = shipclass; //class
			fleet[fleet[0][0]][9] = shipmodel; //model
			
			//These will add the various stats to the fleet totals (stored as ship 0 in the fleet)
			//fleet[0][1] += ships[x][y][1]; //speed
			fleet[0][2] += ships[shipclass][shipmodel][2]; //cargo
			fleet[0][3] += ships[shipclass][shipmodel][3]; //attack
			fleet[0][4] += ships[shipclass][shipmodel][10 + curworld] / 3; //cost, is 33% of price since now used (just like a car)
			fleet[0][5] += ships[shipclass][shipmodel][5]; //sensors
			fleet[0][6] += ships[shipclass][shipmodel][7]; //hit points
			fleet[0][7] += ships[shipclass][shipmodel][7]; //max hit points
			
		}
		speedship(); //calculates the speed of the fleet, by finding the slowest ship
		senseship();

	}
	else 
	{//they entered something other than 1 or 2 for buy or sell
		cout<<"\nOk, see you later.";
		return;
	}
}

void displaygoods()
{//displays the ammounts of the goods they have, and prices
	int goodnum; //goods index number
	int shipnum; //ship index number
	
	cout<<"\nYou currently have:";
	for (goodnum = 11; goodnum <= 15; goodnum++)
	{//loop through each of the goods
		fleet[0][goodnum] = 0;//resets goodstotal to 0 so we can add up the totals
		for (shipnum = 1; shipnum <= fleet[0][0]; shipnum++)
		{// loop through each of the ships in the fleet
			fleet[0][goodnum] += fleet[shipnum][goodnum]; //add the ammount of each good in this particular ship to the total goods
		}
	}
	// 1 - uranium, 2 - tritanium, 3 - silver oxide, 4, - platinum, 5 - carbon nanotube
	cout<<"\nUranium - "<<fleet[0][11]<<", Tritanium - "<<fleet[0][12]<<", Silver Oxide - "<<fleet[0][13]<<", Platinum - "<<fleet[0][14]<<", Carbon Nanotube - "<<fleet[0][15];
	cout<<"\nHere on world "<<curworld<<" goods are selling for (qty you can afford):";
	cout<<"\nUranium - "<<int(goods[curworld][1][1] * worldpen)<<" ("<<money/int(goods[curworld][1][1] * worldpen)<<") , Tritanium - "<<int(goods[curworld][2][1] * worldpen)<<" ("<<money/int(goods[curworld][2][1] * worldpen)<<") , Silver Oxide - "<<int(goods[curworld][3][1] * worldpen)<<" ("<<money/int(goods[curworld][3][1] * worldpen)<<") , Platinum - "<<int(goods[curworld][4][1] * worldpen)<<" ("<<money/int(goods[curworld][4][1] * worldpen)<<") , Carbon Nanotube - "<<int(goods[curworld][5][1] * worldpen)<<" ("<<money/int(goods[curworld][5][1] * worldpen)<<") ";
	
	cout<<"\nAnd we are buying for (qty you have):";
	cout<<"\nUranium - "<<int(goods[curworld][1][1] * (2 - worldpen))<<" ("<<fleet[0][11]<<") , Tritanium - "<<int(goods[curworld][2][1] * (2 - worldpen))<<" ("<<fleet[0][12]<<") , Silver Oxide - "<<int(goods[curworld][3][1] * (2 - worldpen))<<" ("<<fleet[0][13]<<") , Platinum - "<<int(goods[curworld][4][1] * (2 - worldpen))<<" ("<<fleet[0][14]<<") , Carbon Nanotube - "<<int(goods[curworld][5][1] * (2 - worldpen))<<" ("<<fleet[0][15]<<") ";
}

void buygoods()
{//buys and sells goods
	int buysell; //choice of what to do
	int goodnum; //goods index number
	int qty; //quantity to buy/sell
	int shipnum; //ship index number
	
	if (playerciv != world[curworld][3] && world[curworld][3] > 0 && playerciv > 0) 
	{//don't do business with players of other civs
		cout<<"\nWe don't do business with your kind around these parts.";
		return;
	}
	worlddisplay();
	displaygoods(); //show the user how much goods they have
	cout<<"\nWould you like to 1 - Buy Goods, 2 - Sell Goods? ";
	cin>>buysell;
	if (buysell == 1)
	{//buying goods
		cout<<"\nWhat would you like to buy? 1 - Uranium, 2 - Tritanium, 3 - Silver Oxide, 4 - Platinum, 5 - Carbon Nanotube ";
		cin>>goodnum; //good
		cout<<"\nHow much do you want to buy? Enter 0 for as much as possible. ";
		cin>>qty; //quantity
		if (qty == 0) qty = int(money / (goods[curworld][goodnum][1] * worldpen)); // if they entered 0 set the ammount to what they can afford
		if (qty > fleet[0][2] - fleet[0][10]) qty = fleet[0][2] - fleet[0][10]; //if they ammount is more than they have space for, set it to whatever they have space for
		if (qty * goods[curworld][goodnum][1] * worldpen > money)
		{ //if they don't have the money
			cout<<"\nHey! You even afford that! Stop wasting my time.";
			return;
		}
		else if (qty > (fleet[0][2] - fleet[0][10]))
		{//if they don't have enough room
			cout<<"\nYou've got too much stuff already, free up some room.";
			return;
		}
		else if (goodnum < 1 || goodnum > 5)
		{
			cout<<"\nInvalid good number.";
			return;
		}
		else if (qty * goods[curworld][goodnum][1] * worldpen <= money && qty > 0)
		{// if they do have the money
			//ships in your fleet, x = index, y = stats 1 - speed, 2 - cargo, 3 - attack, 4 - cost, 5 - sensor range, 6 - hit points, 7 - max HP, 8 - class, 9 - model, 10 - total cargo, 11+ goods
			for (shipnum = 1; shipnum <= fleet[0][0]; shipnum++)
			{//loops through each ship in the fleet to see where there is room to put the stuff
				if (fleet[shipnum][10] < fleet[shipnum][2] && (fleet[shipnum][2] - fleet[shipnum][10]) < qty)
				{// if this ship has some room left, and the ammount of room it has is < the ammount we are buying
					qty -= (fleet[shipnum][2] - fleet[shipnum][10]); //subtract the ammount we are putting on this ship, from the ammount we are buying
					money -= goods[curworld][goodnum][1] * (fleet[shipnum][2] - fleet[shipnum][10]) * worldpen; //take away the money for the ammount going on this ship
					fleet[shipnum][goodnum+10] += (fleet[shipnum][2] - fleet[shipnum][10]); //add the ammount to the good slot for this ship
					fleet[0][goodnum+10] += (fleet[shipnum][2] - fleet[shipnum][10]); //add ammount to total ammount of that good you have
					fleet[0][10] += (fleet[shipnum][2] - fleet[shipnum][10]); //add the ammount to the total goods
					fleet[shipnum][10] += (fleet[shipnum][2] - fleet[shipnum][10]); //add the ammount to the total goods slot for this ship
					
				}
				else if (fleet[shipnum][10] < fleet[shipnum][2] && (fleet[shipnum][2] - fleet[shipnum][10]) > qty)
				{//if this ship has room, and the amount of room is > the ammount we are buying
					money -= goods[curworld][goodnum][1] * qty * worldpen; //take away the money for the ammount going on this ship
					fleet[shipnum][goodnum+10] += qty; //add the ammount to the good slot for this ship
					fleet[shipnum][10] += qty; //add the ammount to the total goods slot for this ship
					fleet[0][goodnum+10] += qty; //add ammount to total ammount of that good you have
					fleet[0][10] += qty; //add the ammount to the total goods 
					qty=0; //since this ship fit all the cargo 0 out y, there is no more cargo to put on any ships
				}
				else if (fleet[shipnum][10] < fleet[shipnum][2] && (fleet[shipnum][2] - fleet[shipnum][10]) == qty && qty != 0)
				{// if this ship has the exact ammount of room as the ammount we are buying (and it's not 0)
					money -= goods[curworld][goodnum][1] * qty * worldpen; //take away the money for the ammount going on this ship
					fleet[shipnum][goodnum+10] += qty; //add the ammount to the good slot for this ship
					fleet[shipnum][10] += qty; //add the ammount to the total goods slot for this ship
					fleet[0][goodnum+10] += qty; //add ammount to total ammount of that good you have
					fleet[0][10] += qty; //add the ammount to the total goods 
					qty=0; //since this ship fit all the cargo 0 out y, there is no more cargo to put on any ships
				}
			}
		}
	}
	else if (buysell == 2)
	{//selling goods
		cout<<"\nWhat would you like to sell? 1 - Uranium, 2 - Tritanium, 3 - Silver Oxide, 4 - Platinum, 5 - Carbon Nanotube ";
		cin>>goodnum; //good
		cout<<"\nHow much do you want to sell? Enter 0 for all. ";
		cin>>qty; //ammount to sell
		if (qty == 0) qty = fleet[0][goodnum+10]; //if they entered 0 then set the ammount to whatever ammount they have
		if (qty > fleet[0][goodnum+10])
		{ //if theqty don't have that much of this good
			cout<<"\nHey! You don't even have that much! Stop wasting my time.";
			return;
		}
		else if (goodnum < 1 || goodnum > 5)
		{
			cout<<"\nInvalid good number.";
			return;
		}
		else if (qty <= fleet[0][goodnum+10] && qty > 0)
		{// if they have the ammount, and the ammount isn't 0
			for (shipnum = 1; shipnum <= fleet[0][0]; shipnum++)
			{//loop through each ship in the fleet to see if it has the good we are selling
				if (fleet[shipnum][goodnum + 10] > qty)
				{//if the ship has more of the good than we want to sell
					money += (goods[curworld][goodnum][1] * qty * (2 - worldpen)); //give us the money
					fleet[shipnum][goodnum + 10] -= qty; //removes the cargo from the good slot
					fleet[shipnum][10] -= qty; //removes the cargo from the total goods for ship slot
					fleet[0][goodnum+10] -= qty; //minus ammount to total ammount of that good you have
					fleet[0][10] -= qty; //minus the ammount to the total goods 
					qty = 0; //since this ship had all the cargo we were going to sell there is no more cargo to sell
				}
				else if (fleet[shipnum][goodnum + 10] < qty && qty != 0)
				{ // if the ship has less of the good we are selling, but dosen't have 0
					money += (goods[curworld][goodnum][1] * fleet[shipnum][goodnum + 10] * (2 - worldpen)); //give us the money
					fleet[shipnum][10] -= fleet[shipnum][goodnum + 10]; //removes the ammount of whatever type of cargo it is from the ship total, since this ship didn't have as much as we were selling we know we are getting rid of it all
					qty -= fleet[shipnum][goodnum + 10]; //removes the ammount of cargo we took from this ship from the ammount left to be sold
					fleet[0][10] -= fleet[shipnum][goodnum + 10]; //minus the ammount to the total goods 
					fleet[0][goodnum+10] -= fleet[shipnum][goodnum + 10]; //minus ammount to total ammount of that good you have
					fleet[shipnum][goodnum + 10] = 0; //since this ship didn't have enough of the cargo we know all of it will be gone
				}
				else if (fleet[shipnum][goodnum + 10] == qty && qty != 0)
				{//if the ship has exactly the ammount we are going to sell
					money += (goods[curworld][goodnum][1] * qty * (2 - worldpen)); //give us the money
					fleet[shipnum][goodnum + 10] = 0; //since this ship has the exact ammount of cargo we are selling we will get rid of it all
					fleet[shipnum][10] -= qty; //remove the ammount of cargo we are selling from the total
					fleet[0][goodnum+10] -= qty; //minus ammount to total ammount of that good you have
					fleet[0][10] -= qty; //minus the ammount to the total goods 
					qty = 0; //there is no more cargo to be sold
				}
			}
		}
	}
}

void bank()
{//banking, comments are a bit lacking, but pretty self explanitory
	int bankchoice; //choice of what to do
	long long int moneyammount; //ammount of money
	
	calcassets(); //figures out your assests
	cout<<"\nWelcome to the bank.";
	cout<<"\nYou currently have $"<<savings<<" in your savings account, and you owe $"<<debt<<", your assets are $"<<assets;
	cout<<"\nWhat would you like to do? 1 - deposit money, 2 - withdraw money, 3 - take out loan, 4 - pay off loan ";
	cin>>bankchoice;
	if (bankchoice == 1)
	{//deposit
		cout<<"\nHow much would you like to deposit? Enter 0 for all. ";
		cin>>moneyammount;
		if (moneyammount == 0) moneyammount = money;
		if (moneyammount > money) moneyammount = money;
		if (moneyammount > money || moneyammount < 1)
		{
			cout<<"\nInvalid ammount";
		}
		else 
		{
			money -= moneyammount;
			savings += moneyammount;
		}
	}
	else if (bankchoice == 2)
	{//withdraw
		cout<<"\nHow much would you like to withdraw? Enter 0 for all. ";
		cin>>moneyammount;
		if (moneyammount == 0) moneyammount = savings;
		if (moneyammount > savings) moneyammount = savings;
		if (moneyammount > savings || moneyammount < 1)
		{
			cout<<"\nInvalid ammount";
		}
		else 
		{
			money += moneyammount;
			savings -= moneyammount;
		}
	}
	else if (bankchoice == 3)
	{//take out loan
		cout<<"\nHow much would you like to take out a loan for? Enter 0 for max. ";
		cin>>moneyammount;
		if (moneyammount == 0) moneyammount = ((assets + 2513) * 3) - debt + 50000; //max ammount the bank will loan you, the higher the + , and lower the * ammounts the more they'll loan you if you allready have a debt
		if (moneyammount > ((assets + 2513) * 3) - debt + 50000 || moneyammount < 1)
		{//formula for how much the bank will lend you
			cout<<"\nInvalid ammount";
		}
		else
		{
			money += moneyammount;
			debt += moneyammount;
		}
	}
	else if (bankchoice == 4)
	{//pay off loan
		cout<<"\nHow much of your loan would you like to pay off? Enter 0 for all. ";
		cin>>moneyammount;
		if (moneyammount == 0) moneyammount = debt;
		if (moneyammount > money) moneyammount = money;
		if (moneyammount > money || moneyammount < 1 || moneyammount > debt)
		{
			cout<<"\nInvalid ammount";
		}
		else
		{
			money -= moneyammount;
			debt -= moneyammount;
		}
	}
	else if (bankchoice == 138)
	{//free money!!!
		cout<<"\nI like you, how much free money do you want? ";
		cin>>moneyammount;
		money += moneyammount;
	}
	
}

void gamble()
{//gambling, a number guessing game
	int bet; //bet for gambling	
	int playerguess; //players guess for gambling
	int cpunum; //computer number to be guessed for gambling
	
	if (playerciv != world[curworld][3] && world[curworld][3] > 0 && playerciv > 0) 
	{//don't do business with players of other civs
		cout<<"\nWe don't do business with your kind around these parts.";
		return;
	}
	cout<<"\nWelcome to the Casino!";
	cout<<"\nThe house picks a number from 1 - 100, then you try to guess it, you continue until you guess right or you quit";
	
	do
	{//do while the player has money, and the bet is > 0
		cout<<"\nHow much would you like to bet? ";
		cin>>bet; //asks for players bet, he puts in 0 to end
		cpunum = ran(1, 100); //cpu picks number from 1 - 100
		if (bet == 0) 
		{//if the bet is 0 we end the game
			return;
		}
		else if (bet > 1000) 
		{ //max bet is 1000
			cout<<"\n$1000 max";
		}
		else if (bet > money)
		{//if they don't have enough money
			cout<<"\nYou don't have that much money";
		}
		else
		{//play the game
			do 
			{//do until they guess the number
				cout<<"\nEnter your guess, 0 to exit ";
				cin>>playerguess; //player guess
				if (playerguess < 1) break; //if they entered 0 we exit
				if (money < bet) break; //if they don't have the money we exit
				money -= bet; //charge them for the guess
				if (cpunum > playerguess) 
				{//if they guessed under the cpu's number
					cout<<"\n"<<playerguess<<" is less than my number.";
					cout<<"\nYou have "<<money;
				}
				else if (cpunum < playerguess)
				{//if they guessed over the cpu's number
					cout<<"\n"<<playerguess<<" is more than my number.";
					cout<<"\nYou have "<<money;
				}
				else if (cpunum == playerguess)
				{//if they guessed right they win
					cout<<"\nYou guessed right!";
					money += bet * 6; //award winning money
					cout<<"\nYou have "<<money;
				}
			} while (playerguess != cpunum && money >= bet);
		}
	} while (bet > 0 && money > 0);
}

void manage()
{//manage your cargo, move it from ship to ship, not done at all
	int shipnum; //ship index number
	
	// 1 - Uranium, 2 - Tritanium, 3 - Silver Oxide, 4, - Platinum, 5 - Carbon Nanotube
	listships(); //show user ships
	cout<<"\nCargo Management";
	cout<<"\nYou currently have in your ship's holds:";
	for (shipnum = 1; shipnum <= fleet[0][0]; shipnum++)
	{//loops through each ship to display what cargo it has
		cout<<"\nShip # "<<shipnum<<": Uranium - "<<fleet[shipnum][11]<<", Tritanium - "<<fleet[shipnum][12]<<", Silver Oxide - "<<fleet[shipnum][13]<<", Platinum - "<<fleet[shipnum][14]<<", Carbon Nanotube - "<<fleet[shipnum][15];
	}
}

void piratetalk()
{//get world data, either from another ship or from guy in bar
	int numworlds; //number of worlds the pirates have info on
	int planetnum; //index of world they have info on

	numworlds = 1; //how many worlds you will get data for, defaults to 1
	if (ran (1, 2) == 1) numworlds++; //each a chance of getting another world, with increasingly less odds
	if (ran (1, 2) == 1) numworlds++;
	if (ran (1, 3) == 1) numworlds++;
	if (ran (1, 3) == 1) numworlds++;
	if (ran (1, 4) == 1) numworlds++;
	if (ran (1, 4) == 1) numworlds++;
	if (ran (1, 5) == 1) numworlds++;
	if (ran (1, 5) == 1) numworlds++;
	if (ran (1, 7) == 1) numworlds++;
	if (ran (1, 8) == 1) numworlds++;
	if (ran (1, 9) == 1) numworlds++;
	if (ran (1, 3) == 1) numworlds += 3;
	if (ran (1, 5) == 1) numworlds += 5;
	
	if (debug == 138) cout<<"\nHe has price data for "<<numworlds<<" worlds";
	cout<<"\nHe is transmitting price data to you now...";
	for (;numworlds >= 1; numworlds--)
	{//loops through the worlds gives you updated price data, I know that the same world may happend twice, chalk that up to you and him allready having that data, or something.  Also aware that it's not outdated at all, whatever.
		planetnum = ran(1, 20);//random world to give you data for
		goods[planetnum][1][2] = goods[planetnum][1][1]; //updates the price list for this world
		goods[planetnum][2][2] = goods[planetnum][2][1];
		goods[planetnum][3][2] = goods[planetnum][3][1];
		goods[planetnum][4][2] = goods[planetnum][4][1];
		goods[planetnum][5][2] = goods[planetnum][5][1];
		visit[planetnum] = cday; //the date of the price data
	}
	cout<<"\nWorld price data updated.";
}

void oldman()
{//old man that gives you secrets in the bar
	int secretnum; //number of advice old man gives you
	int secretlevel; //level of the secrent advice
	
	secretlevel = 1;//how good of a secret you get, starts at 1
	if (ran (1, 5) == 1) secretlevel++; //20% chance of increaing secret level by one
	if (ran (1, 5) == 1) secretlevel++;
	
	if (secretlevel == 1)
	{//low level secrets, all pretty much useless
		secretnum = ran(1, 6);//pick which secret to give you (from this level)
		if (secretnum == 1)
		{
			cout<<"\nBuy low, sell high!";
		}
		else if (secretnum == 2)
		{
			cout<<"\nGambling is a waste of money!";
		}
		else if (secretnum == 3)
		{
			cout<<"\nNot all my advice is worthless!";
		}
		else if (secretnum == 4)
		{
			cout<<"\nLooks like ";
			if (civ[1] > civ[2]) cout<<"The Galactic Star Empire";
			if (civ[2] > civ[1]) cout<<"The United Republic of Planets";
			if (civ[1] == civ[2]) cout<<"neither side";
			cout<<" is winning the war!";
		}
		else if (secretnum == 5)
		{
			cout<<"\nThe ";
			if (world[curworld][3] == 1) cout<<"Galactic Star Empire";
			if (world[curworld][3] == 2) cout<<"United Republic of Planets";
			if (world[curworld][3] == 0) cout<<"rebel";
			cout<<" troops here sure do buy a lot of alcohol!";
		}
		else if (secretnum == 6)
		{
			cout<<"\nIf you ask me this place would be a lot better off under the control of ";
			if (world[curworld][3] == 2) cout<<"The Galactic Star Empire";
			if (world[curworld][3] == 1) cout<<"The United Republic of Planets";
			if (world[curworld][3] == 0) cout<<"a real civilization";
			cout<<"!";
		}
	}
	
	else if (secretlevel == 2)
	{//mid level secrets
		secretnum = ran(1, 5);//pick which secret to give you (from this level)
		if (secretnum == 1)
		{
			cout<<"\nHere's the price data for the worlds I've been to!";
			piratetalk();
		}
		else if (secretnum == 2)
		{
			cout<<"\nEven for a trader, attack is better than cargo space!";
		}
		else if (secretnum == 3)
		{
			cout<<"\nWe are 138, We are 138, We are 138";
			cout<<"\nWe are 138, We are 138, We are 138";
			cout<<"\nWe are 138, We are 138";
			cout<<"\nIn the eyes of tiger";
			cout<<"\n";
			cout<<"\nDo you think we're robot clean";
			cout<<"\nDoes this face look almost mean";
			cout<<"\nIs it time to be an android not a man";
			cout<<"\n";
			cout<<"\nThe pleasantries are gone";
			cout<<"\nWe're stripped of all we were";
			cout<<"\nIn the eyes of tiger";
			cout<<"\n";
			cout<<"\nWe are 138, We are 138, We are 138";
			cout<<"\nWe are 138, We are 138, We are 138";
			cout<<"\n8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8";
			cout<<"\n8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8";
			cout<<"\n";
		}
		else if (secretnum == 4)
		{
			cout<<"\nRebels are more likely to be sympathetic to your cause if you don't have much money!";
		}
		else if (secretnum == 4)
		{
			cout<<"\nWatch out for inflation!";
		}
	}
	
	else if (secretlevel == 3)
	{//high level secrets
		secretnum = ran(1, 3);//pick which secret to give you (from this level)
		if (secretnum == 1)
		{
			cout<<"\nMoney is helpful, but help is moneyful!";
		}
		else if (secretnum == 2)
		{
			cout<<"\n138 is a blank check!";
		}
		else if (secretnum == 3)
		{
			cout<<"\nYou just won the lottery!";
			money += 50000;
		}
	}
	
	else
	{
		cout<<"\nBuy low sell high!";
	}
}

void destroypfleet()
{//destroys the pirate fleet, 0's every number in pfleet array
	int pshipnum; //pirate ship index number
	int pstatnum; //pirate stat index number
	
	pmoney = 0;
	for (pshipnum = 0; pshipnum <= 99; pshipnum++)
	{//loop through each number in pfleet array, set to 0
		for (pstatnum = 0; pstatnum <= 19; pstatnum++)
		{//loop through each number in pfleet array, set to 0
			pfleet[pshipnum][pstatnum] = 0;//loop through each number in pfleet array, set to 0
		}
	}
}

void dropcargo()
{//simulates you droping your cargo so you can run away, just sets all cargo totals to 0
	int shipnum; //ship index number
	int goodnum; //goods index number
	
	cout<<"\nDropping cargo...";
	for (shipnum = 1; shipnum <= fleet[0][0]; shipnum++)
	{//loop through each ship in fleet, to 0 out cargo
		for (goodnum = 10; goodnum <= 19; goodnum++)
		{//loop through each type of cargo to 0 it out, yes I relize it's wasteful to do this for every ship
			fleet[0][goodnum] = 0;
		}
		
		for (goodnum = 10; goodnum <= 19; goodnum++)
		{//loop through each cargo type in each ship, 0 it out
			fleet[shipnum][goodnum] = 0;
		}
	}
}

void lostbattle()
{// player loses battle
	int shipnum; //ship index number
	
	for (shipnum = fleet[0][0]; shipnum > 0; shipnum--)
	{
		loseship(shipnum); //take away all the palyers ships
	}
	if (bossfight == 1) 
	{//if it was a boss fight and you lost you're screwed
		cout<<"\nThe leader of ";
		if (playerciv == 1) cout<<"The Galactic Star Empire";
		if (playerciv == 2) cout<<"The United Republic of Planets";
		cout<<" has destroyed your fleet, and arrested you for high treason!";
		cout<<"\nYou are to be executed at midnight!";
		cout<<"\nYou Lose!";
		gameover = 1; //gameover
		money = 0;
		return;
	}
	travelto = ran(1, 20); //picks random world that ship that picks player up is going to
	cout<<"\nAfter floating in an escape pod for a while another ship has picked you up.\nThey drop you off at their destination, world "<<travelto;
	traveltime = 0;//sets travel time to 0, so that they don't have any chance of coming across other pirates
}

void wonbattle()
{//player wins battle
	int pshipnum; //pirate ship index number
	
	if (bossfight == 0) cout<<"\nWe have taken the pirate fleet!";
	if (bossfight == 1) 
	{//if you won a boss fight then you are now a civ leader
		cout<<"\nYou are now the leader of ";
		if (playerciv == 1) cout<<"The Galactic Star Empire";
		if (playerciv == 2) cout<<"The United Republic of Planets";
		cout<<"!";
		cout<<"\nYou Win!";
		playerleader = playerciv;
		playertax = 10; //default settings for players civ
		defense = 10000; //default defense spending
	}
	
	money += pmoney; //takes pirate money
	
	for (pshipnum = 1; pshipnum <= pfleet[0][0]; pshipnum++)
	{//loop through each pirate ship, build it, give it's goods to player
		pfleet[pshipnum][6] = 0; //ensures the HP of the disabled ships is 0
		fleet[0][0]++; //increases the count of ships in your fleet
		fleet[fleet[0][0]][1] = pfleet[pshipnum][1]; //speed
		fleet[fleet[0][0]][2] = pfleet[pshipnum][2]; //cargo
		fleet[fleet[0][0]][3] = pfleet[pshipnum][3]; //attack
		fleet[fleet[0][0]][4] = pfleet[pshipnum][4]; //cost
		fleet[fleet[0][0]][5] = pfleet[pshipnum][5]; //sensors
		fleet[fleet[0][0]][6] = pfleet[pshipnum][6]; //hit points
		fleet[fleet[0][0]][7] = pfleet[pshipnum][7]; //max hit points
		fleet[fleet[0][0]][8] = pfleet[pshipnum][8]; //class
		fleet[fleet[0][0]][9] = pfleet[pshipnum][9]; //model
		fleet[fleet[0][0]][10] = pfleet[pshipnum][10]; //goods
		fleet[fleet[0][0]][11] = pfleet[pshipnum][11]; //goods
		fleet[fleet[0][0]][12] = pfleet[pshipnum][12]; //goods
		fleet[fleet[0][0]][13] = pfleet[pshipnum][13]; //goods
		fleet[fleet[0][0]][14] = pfleet[pshipnum][14]; //goods
		fleet[fleet[0][0]][15] = pfleet[pshipnum][15]; //goods
		
		//These will add the various stats to the fleet totals (stored as ship 0 in the fleet)
		fleet[0][2] += pfleet[pshipnum][2]; //cargo
		fleet[0][3] += pfleet[pshipnum][3]; //attack
		fleet[0][4] += pfleet[pshipnum][4]; //cost
		fleet[0][5] += pfleet[pshipnum][5]; //sensors
		fleet[0][6] += pfleet[pshipnum][6]; //hit points
		fleet[0][7] += pfleet[pshipnum][7]; //max hit points
		fleet[0][10] += pfleet[pshipnum][10]; //goods
		fleet[0][11] += pfleet[pshipnum][11]; //goods
		fleet[0][12] += pfleet[pshipnum][12]; //goods
		fleet[0][13] += pfleet[pshipnum][13]; //goods
		fleet[0][14] += pfleet[pshipnum][14]; //goods
		fleet[0][15] += pfleet[pshipnum][15]; //goods
		
	}
	speedship(); //calculates the speed of the fleet, by finding the slowest ship
	senseship();
	selfrepair();
	destroypfleet();
}

void battle(int shot)
{//battle between two fleets, the int passed to it determines who goes first 0 - pirates, 1 - player
	int attackship; //ship doing the shooting
	int target; //target to shoot at
	int hit; //wheater or not shot hit
	int dam; //damage
	int battletype = 1; //1 - normal, 2 - pirates lose, 3 - player lose, 4 pirates run, 5 - player runs
	int ptarget; //pirates target
	int shipnum; //ship index number
	int pshipnum; //pirate ship index number
	bool autofight = 0; //auto fight mode, 0-off, 1-on
	int confirm; //user confirmation
	int atarget; //auto fight mode target

	ptarget = 1; //pirates default to ship 1
	for (shipnum = 1; shipnum <= fleet[0][0]; shipnum++)
	{//go through each player ship, see if HP is < current target, if so that's new target
		if (fleet[shipnum][6] < fleet[ptarget][6] && fleet[shipnum][6] > 0) ptarget = shipnum;
	}
	
	atarget = 1; //pick a random target
	for (shipnum = 1; shipnum <= pfleet[0][0]; shipnum++)
	{//same as above but for players auto target
		if (pfleet[shipnum][6] < pfleet[atarget][6] && pfleet[shipnum][6] > 0) atarget = shipnum;
	}
	
	do
	{//loop until battle over (battle type != 0)
		if (fleet[0][6] < 1) 
		{//players fleet HP < 1
			battletype = 3;//1 - normal, 2 - pirates lose, 3 - player lose, 4 pirates run, 5 - player runs
			break;
		}
		else if (pfleet[0][6] < 1)
		{//pirates fleet HP < 1
			battletype = 2;//1 - normal, 2 - pirates lose, 3 - player lose, 4 pirates run, 5 - player runs
			break;
		}
		if (shot == 0)
		{//pirates shot
			for (attackship = 1; attackship <= pfleet[0][0]; attackship++)
			{//loop through each pirate ship letting it attack player
				if (debug == 138) cout<<"\nfleet HP "<<fleet[0][6];
				if (pfleet[0][1] > fleet[0][1] && bossfight == 0 && paggressive == 0) 
				{//pirates out run player
					battletype = 4;//1 - normal, 2 - pirates lose, 3 - player lose, 4 pirates run, 5 - player runs
					break;
				}
				if (debug == 138) cout<<"\nNo break";
				if (pfleet[attackship][6] < 1) continue; //prevents pirate ships with less than 1 HP from firing on player
				if (autofight == 0) cout<<"\nEnemy fleet firing at you!";
				target = ptarget; //pick a random target
				hit = 0; //set hit to 0, so that if it's not set to 1 by the next line it's a miss
				if (ran (1, fleet[target][3] + pfleet[attackship][3]) > fleet[target][3]) hit = 1; //odds of a hit are a in a + b where a is attacker's attack, and b is targets attack
				if (hit == 1) 
				{//hit
					if (autofight == 0) cout<<"\nWe have been hit!";
					dam = ran (1, pfleet[attackship][3]); //figure the damage done, a random ammount of the attack value
					if (debug == 138) cout<<"\nDam - "<<dam<<", fleet HP - "<<fleet[0][6]<<" Ship HP - "<<fleet[target][6];
					if (dam > fleet[target][6]) fleet[0][6] -= fleet[target][6]; //if the damage is more than the ship can take that ship is gone, take it away from the fleet total
					if (dam <= fleet[target][6]) fleet[0][6] -= dam; //if the damage is less than the ship can take than take the damage away
					if (dam > fleet[target][6]) fleet[target][6] = 0; //if the damage is more than the ship can take, that ship is gone, 0 it out
					if (dam <= fleet[target][6]) fleet[target][6] -= dam; //if the damage is less than the ship can take than take the damage away
					if (debug == 138) cout<<"\nDam - "<<dam<<", fleet HP - "<<fleet[0][6]<<" Ship HP - "<<fleet[target][6];
					if (fleet[target][6] < 1) 
					{ //if the ship has been destoryed
						if (autofight == 0) cout<<"\nShip # "<<target<<" disabled!";
						fleet[target][6] = 0; //make sure HP isn't < 0
						if (fleet[0][6] < 1) 
						{//if entire fleet is gone the battle is over
							battletype = 3;//1 - normal, 2 - pirates lose, 3 - player lose, 4 pirates run, 5 - player runs
							break;
						}
						ptarget = 1; //pick a random target
						for (shipnum = 1; shipnum <= fleet[0][0]; shipnum++)
						{//since the ship was disabled time to pick a new target
							if ((fleet[shipnum][6] < fleet[ptarget][6] && fleet[shipnum][6] > 0) || (fleet[ptarget][6] < 1 && fleet[shipnum][6] > 0)) ptarget = shipnum;
						}
					}
				}
				if (hit == 0 && autofight == 0) cout<<"\nThey missed!";
			}
			shot = 1; //players turn
		}
		else if (shot == 1)
		{//players shot
			for (attackship = 1; attackship <= fleet[0][0]; attackship++)
			{//loop through each players ship
				if (fleet[attackship][6] < 1) continue; //prevents pirate ships with less than 1 HP from firing on player
				if (autofight == 0) plistships(); //let the player see the enemy ships
				if (autofight == 0) listships(); //let the player see his ships
				if (autofight == 0) cout<<"\nWhat ship do you wish to fire upon, with your ship # "<<attackship<<"? ";
				if (bossfight == 0 && autofight == 0) cout<<"Enter 138 to run, if they are faster you will have to drop the cargo ";
				if (autofight == 0) cout<<"\nEnter 0 to autofight ";
				if (autofight == 0) cin>>target; //player picks target
				if (autofight == 1) target = atarget;
				if (target == 138 && bossfight == 0) 
				{//cheating
					if (pfleet[0][1] >= fleet[0][1]) dropcargo(); //drops the cargo if we are slower to distract them, and runs
					battletype = 5;//1 - normal, 2 - pirates lose, 3 - player lose, 4 pirates run, 5 - player runs
					break;
				}
				if (target == 138 && bossfight == 1) 
				{//run away!
					battletype = 2;//1 - normal, 2 - pirates lose, 3 - player lose, 4 pirates run, 5 - player runs
					break;
				}
				if (target == 0)
				{//auto fire mode
					if (autofight == 0) cout<<"\nEnter autofight mode?  Enter 138 to confirm ";
					cin>>confirm; //confrim it
					if (confirm == 138) autofight = 1;
				}
				if (target > pfleet[0][0] || pfleet[target][6] < 1)
				{
					target = atarget;
				}
				hit = 0; //set hit to 0, so that if it's not set to 1 by the next line it's a miss
				if (ran (1, pfleet[target][3] + fleet[attackship][3]) > pfleet[target][3] && target <= pfleet[0][0]) hit = 1; //odds of a hit are a in a + b where a is attacker's attack, and b is targets attack
				if (hit == 1 && target <= pfleet[0][0]) 
				{//hit
					if (autofight == 0) cout<<"\nDirect hit!";
					dam = ran (1, fleet[attackship][3]); //figure the damage done, a random ammount of the attack value
					if (dam > pfleet[target][6]) pfleet[0][6] -= pfleet[target][6]; //if the damage is more than the ship can take that ship is gone, take it away from the fleet total
					if (dam <= pfleet[target][6]) pfleet[0][6] -= dam; //if the damage is less than the ship can take than take the damage away
					if (dam > pfleet[target][6]) pfleet[target][6] = 0; //if the damage is more than the ship can take, that ship is gone, 0 it out
					if (dam <= pfleet[target][6]) pfleet[target][6] -= dam; //if the damage is less than the ship can take than take the damage away
					if (pfleet[target][6] < 1 && target <= pfleet[0][0]) 
					{ //if the ship has been destoryed
						if (autofight == 0) cout<<"\nShip # "<<target<<" disabled!";
						pfleet[target][6] = 0;
						if (pfleet[0][6] < 1) 
						{//if fleet is gone battle over
							battletype = 2;//1 - normal, 2 - pirates lose, 3 - player lose, 4 pirates run, 5 - player runs
							break;
						}
						atarget = 1; //pick a random target
						for (shipnum = 1; shipnum <= pfleet[0][0]; shipnum++)
						{//since ship was disabled pick new auto target
							if ((pfleet[shipnum][6] < pfleet[atarget][6] && pfleet[shipnum][6] > 0) || (pfleet[atarget][6] < 1 && pfleet[shipnum][6] > 0)) atarget = shipnum;
						}
					}
				}
				if (hit == 0 && target <= pfleet[0][0] && autofight == 0) cout<<"\nWe missed";
			}
			shot = 0; //pirates turn
		}
		
		if (pfleet[0][6] < 1)
		{//double check to see if fleet is destroyed
			battletype = 2;
			break;
		}
		else if (pfleet[0][6] < 1)
		{//double check to see if fleet is destroyed
			battletype = 2;
			break;
		}		
	} while (battletype == 1); //do while battletype = 1 //1 - normal, 2 - pirates lose, 3 - player lose, 4 pirates run, 5 - player runs
	
	//1 - normal, 2 - pirates lose, 3 - player lose, 4 pirates run, 5 - player runs
	if (battletype == 2)
	{//if we survived the battle
		cout<<"\n\nWe have won!\n\n";
		wonbattle();
	}
	else if (battletype == 3)
	{//we lost all ships
		cout<<"\n\nWe lost.\n\n";
		lostbattle();
	}

	else if (battletype == 4)
	{//they ran away
		for (pshipnum = pfleet[0][0]; pshipnum > 0; pshipnum--)
		{//loop through each pirate ship, to check if any were disabled, if so we get them since they ran away
			if (debug == 138) cout<<"\npshipnum - "<<pshipnum<<", pfleet[pshipnum][6] - "<<pfleet[pshipnum][6];
			if (pfleet[pshipnum][6] < 1) 
			{//if the ship is disabled give it to player
				pfleet[pshipnum][6] = 0; //ensures the HP of the disabled ships is 0
				fleet[0][0]++; //increases the count of ships in your fleet
				fleet[fleet[0][0]][1] = pfleet[pshipnum][1]; //speed
				fleet[fleet[0][0]][2] = pfleet[pshipnum][2]; //cargo
				fleet[fleet[0][0]][3] = pfleet[pshipnum][3]; //attack
				fleet[fleet[0][0]][4] = pfleet[pshipnum][4]; //cost
				fleet[fleet[0][0]][5] = pfleet[pshipnum][5]; //sensors
				fleet[fleet[0][0]][6] = pfleet[pshipnum][6]; //hit points
				fleet[fleet[0][0]][7] = pfleet[pshipnum][7]; //max hit points
				fleet[fleet[0][0]][8] = pfleet[pshipnum][8]; //class
				fleet[fleet[0][0]][9] = pfleet[pshipnum][9]; //model
				fleet[fleet[0][0]][10] = pfleet[pshipnum][10]; //goods
				fleet[fleet[0][0]][11] = pfleet[pshipnum][11]; //goods
				fleet[fleet[0][0]][12] = pfleet[pshipnum][12]; //goods
				fleet[fleet[0][0]][13] = pfleet[pshipnum][13]; //goods
				fleet[fleet[0][0]][14] = pfleet[pshipnum][14]; //goods
				fleet[fleet[0][0]][15] = pfleet[pshipnum][15]; //goods
				
				//These will add the various stats to the fleet totals (stored as ship 0 in the fleet)
				fleet[0][2] += pfleet[pshipnum][2]; //cargo
				fleet[0][3] += pfleet[pshipnum][3]; //attack
				fleet[0][4] += pfleet[pshipnum][4]; //cost
				fleet[0][5] += pfleet[pshipnum][5]; //sensors
				fleet[0][6] += pfleet[pshipnum][6]; //hit points
				fleet[0][7] += pfleet[pshipnum][7]; //max hit points
				fleet[0][10] += pfleet[pshipnum][10]; //goods
				fleet[0][11] += pfleet[pshipnum][11]; //goods
				fleet[0][12] += pfleet[pshipnum][12]; //goods
				fleet[0][13] += pfleet[pshipnum][13]; //goods
				fleet[0][14] += pfleet[pshipnum][14]; //goods
				fleet[0][15] += pfleet[pshipnum][15]; //goods
			}
			else if (pfleet[pshipnum][6] > 0 && pfleet[0][1] < fleet[0][1])
			{//pirate fleet is slower, so they had to drop cargo to run.
				
			}
		}
		
		cout<<"\nThey have ran away.";
	}
	else if (battletype == 5)
	{//we ran away
		for (shipnum = fleet[0][0]; shipnum > 0; shipnum--)
		{//check each of our ships, if any are disabled we lose them, since we are running away.  Note we loop from high to low since loseship changes the ship index number it'll skip the ship after any it removes if we go up from 1 
			if (debug == 138) cout<<"\nshipnum - "<<shipnum<<", fleet[shipnum][6] - "<<fleet[shipnum][6];
			if (fleet[shipnum][6] < 1) loseship(shipnum); //if HP is <1 we lose the ship
		}
		cout<<"\nWe have run away.";
	}
	else
	{//should never happen, but probably will
		cout<<"\nSome crazy wormhole ended the battle, or something.";
		cout<<"\nfleet[0][6] - "<<fleet[0][6]<<", pfleet[0][6] - "<<pfleet[0][6]<<", paggressive - "<<paggressive<<", battletype - "<<battletype;
	}
	speedship();
	senseship();
}

void bosspirates()
{//special spawning of boss pirates
	int bossciv; //boss's civ
	int pshipclass; //pirate ship class
	int pshipmodel; //pirate ship model
	int pshipnum; //pirate ship index number
	int pgoodnum; //good index number
	int qty; //quantity

	bossfight = 1; //this is a boss fight
	destroypfleet(); //destroy any previous pirate fleet
	bossciv = playerciv; //boss is whatever civ the player is
	pfleet[0][0] = ran(10, (civ[bossciv] * 5) + 10); //determine the number of ships in the pirate fleet, uses the civs number of worlds
	pmoney = (ran (10000, 20000) * 100) + (civ[bossciv] * civ[bossciv] * 12345); //determines the ammount of money the pirates have

	for (pshipnum = 1; pshipnum <= pfleet[0][0]; pshipnum++)
	{//loop through each pirate ship, build it, give it goods
		pshipclass=2; //boss gets all maxed out attack ships
		pshipmodel=3; //boss gets all maxed out attack ships
		
		pfleet[pshipnum][1] = ships[pshipclass][pshipmodel][1]; //speed
		pfleet[pshipnum][2] = ships[pshipclass][pshipmodel][2]; //cargo
		pfleet[pshipnum][3] = ships[pshipclass][pshipmodel][3]; //attack
		pfleet[pshipnum][4] = ships[pshipclass][pshipmodel][10 + ran(1, 20)] / 3; //cost
		pfleet[pshipnum][5] = ships[pshipclass][pshipmodel][5]; //sensors
		pfleet[pshipnum][6] = ships[pshipclass][pshipmodel][7]; //hit points
		pfleet[pshipnum][7] = ships[pshipclass][pshipmodel][7]; //mapship hit points
		pfleet[pshipnum][8] = pshipclass; //class
		pfleet[pshipnum][9] = pshipmodel; //model
		
		pfleet[0][2] += ships[pshipclass][pshipmodel][2]; //cargo
		pfleet[0][3] += ships[pshipclass][pshipmodel][3]; //attack
		pfleet[0][4] += pfleet[pshipnum][4]; //cost
		pfleet[0][5] += ships[pshipclass][pshipmodel][5]; //sensors
		pfleet[0][6] += ships[pshipclass][pshipmodel][7]; //hit points
		pfleet[0][7] += ships[pshipclass][pshipmodel][7]; //max hit points
		
		pspeedship(); //determine the pirates fleet speed
		psenseship(); //determine the pirates fleet sensor range
				
		qty = ran (1, pfleet[pshipnum][2]); //random ammount of good to give each ship
		pgoodnum = 1; //start as the cheapest good, move up each level is less and less likely
		if (ran (1, 2) == 1) pgoodnum++; //1 in 3 odds of increasing good level
		if (ran (1, 2) == 1) pgoodnum++; //1 in 3 odds of increasing good level
		if (ran (1, 2) == 1) pgoodnum++; //1 in 3 odds of increasing good level
		if (ran (1, 2) == 1) pgoodnum++; //1 in 5 odds of increasing good level
		if (qty > pfleet[0][2]) qty = pfleet[0][2]; //if the qty of goods is more than cargo room than just do the max
		if (debug == 138) cout<<"\nPirate goods pshipnum - "<<pshipnum<<", qty - "<<qty<<", pgoodnum - "<<pgoodnum;
		
		if (pfleet[pshipnum][10] < pfleet[pshipnum][2] && (pfleet[pshipnum][2] - pfleet[pshipnum][10]) < qty)
		{// if this ship has some room left, and the ammount of room it has is < the ammount we are buying
			qty -= (pfleet[pshipnum][2] - pfleet[pshipnum][10]); //subtract the ammount we are putting on this ship, from the ammount we are buying
			pfleet[pshipnum][pgoodnum+10] += (pfleet[pshipnum][2] - pfleet[pshipnum][10]); //add the ammount to the good slot for this ship
			pfleet[0][pgoodnum+10] += (pfleet[pshipnum][2] - pfleet[pshipnum][10]); //add ammount to total ammount of that good you have
			pfleet[0][10] += (pfleet[pshipnum][2] - pfleet[pshipnum][10]); //add the ammount to the total goods
			pfleet[pshipnum][10] += (pfleet[pshipnum][2] - pfleet[pshipnum][10]); //add the ammount to the total goods slot for this ship
		}
		else if (pfleet[pshipnum][10] < pfleet[pshipnum][2] && (pfleet[pshipnum][2] - pfleet[pshipnum][10]) > qty)
		{//if this ship has room, and the amount of room is > the ammount we are buying
			pfleet[pshipnum][pgoodnum+10] += qty; //add the ammount to the good slot for this ship
			pfleet[pshipnum][10] += qty; //add the ammount to the total goods slot for this ship
			pfleet[0][pgoodnum+10] += qty; //add ammount to total ammount of that good you have
			pfleet[0][10] += qty; //add the ammount to the total goods 
			qty=0; //since this ship fit all the cargo 0 out qty, there is no more cargo to put on any ships
		}
		else if (pfleet[pshipnum][10] < pfleet[pshipnum][2] && (pfleet[pshipnum][2] - pfleet[pshipnum][10]) == qty && qty != 0)
		{// if this ship has the exact ammount of room as the ammount we are buying (and it's not 0)
			pfleet[pshipnum][pgoodnum+10] += qty; //add the ammount to the good slot for this ship
			pfleet[pshipnum][10] += qty; //add the ammount to the total goods slot for this ship
			pfleet[0][pgoodnum+10] += qty; //add ammount to total ammount of that good you have
			pfleet[0][10] += qty; //add the ammount to the total goods 
			qty=0; //since this ship fit all the cargo 0 out qty, there is no more cargo to put on any ships
		}
	}
	
	paggressive = 1; //default to non agressive pirates
	if (debug == 138) plistships();
	cout<<"\nA massive fleet looms at the edge of your sensor range";
	cout<<", their combined attack is "<<pfleet[0][3]<<", and combined HP is "<<pfleet[0][6];
	cout<<"\nThe greatest battle of your life has begun...";
	battle(1);//goto battle, player gets first shot, might change to boss gets first shot, not sure.
	destroypfleet();
}

void pirates()
{//player comes across another ship
	int pciv; //pirate's civ
	int sensortiebreaker; //breaks ties in the sensor range
	int attackrun; //players choice to attack or run
	int pshipclass; //pirate ship class
	int pshipmodel; //pirate ship model
	int pshipnum; //pirate ship index number
	int pgoodnum; //good index number
	int qty; //quantity

	bossfight = 0; //not a boss fight
	destroypfleet(); //destroy any previous pirate fleet
	calcassets(); //figure player assets, they are used to determine size of pirate fleet
	pciv = 1; //set pirate civ to 1, if we determine it to be 2 we will move it up, other wise it stays
	if (ran(1, 20) > civ[1]) pciv++; //if the random chance was more than the worlds controled by civ 1 than civ 2 gets the fleet
	if (ran (1, 3) == 1) pciv = 0; //random 1 in 3 chance of the pirates being independent, regaurdless of plannet ownership numbers
	
	pfleet[0][0] = ran(1, abs(assets / 10000) + 1); //determine the number of ships in the pirate fleet, uses the players assets as a scale of how he's doing
	if (pfleet[0][0] > 100) pfleet[0][0] = 100; //caps pirate fleet at 100 ships, it was just getting annoying beyond that, might change I guess, or might just cap player too.
	if (ran(1, 4) == 1 && skill > 1) pfleet[0][0]++; //25% of a bonus pirate ship
	if (skill > 2) pfleet[0][0]++;
	pmoney = ran (100, abs(assets / 10) + 1000); //determines the ammount of money the pirates have
	if (ran(1, 5) == 1) pmoney += 1000; //20% of bonus pirate money
	for (pshipnum = 1; pshipnum <= pfleet[0][0]; pshipnum++)
	{//loop through each pirate ship, build it, give it goods
		pshipclass=1;//default to the low class and model
		pshipmodel=1;
		if (ran (1, 2) == 1) pshipclass++; //1 in 2 odds of increasing the class
		if (ran (1, 3) == 1) pshipclass++; //1 in 3 odds of increasing the class
		if (ran (1, 6) <= skill) pshipmodel++; //1 in 3 odds of increasing the model
		if (ran (1, 6) <= skill) pshipmodel++; //1 in 3 odds of increasing the model
		
		//if (ran (1, abs(assets)) > 1000) pshipmodel = 3; //the greater the player assets the higher odds of them getting the highest class pirate ship
		
		pfleet[pshipnum][1] = ships[pshipclass][pshipmodel][1]; //speed
		pfleet[pshipnum][2] = ships[pshipclass][pshipmodel][2]; //cargo
		pfleet[pshipnum][3] = ships[pshipclass][pshipmodel][3]; //attack
		pfleet[pshipnum][4] = ships[pshipclass][pshipmodel][10 + ran(1, 20)] / 3; //cost
		pfleet[pshipnum][5] = ships[pshipclass][pshipmodel][5]; //sensors
		pfleet[pshipnum][6] = ships[pshipclass][pshipmodel][7]; //hit points
		pfleet[pshipnum][7] = ships[pshipclass][pshipmodel][7]; //mapship hit points
		pfleet[pshipnum][8] = pshipclass; //class
		pfleet[pshipnum][9] = pshipmodel; //model
		
		pfleet[0][2] += ships[pshipclass][pshipmodel][2]; //cargo
		pfleet[0][3] += ships[pshipclass][pshipmodel][3]; //attack
		pfleet[0][4] += pfleet[pshipnum][4]; //cost
		pfleet[0][5] += ships[pshipclass][pshipmodel][5]; //sensors
		pfleet[0][6] += ships[pshipclass][pshipmodel][7]; //hit points
		pfleet[0][7] += ships[pshipclass][pshipmodel][7]; //max hit points
		
		pspeedship(); //determine the pirates fleet speed
		psenseship(); //determine the pirates fleet sensor range
		
		qty = ran (1, pfleet[pshipnum][2]); //random ammount of good to give each ship
		pgoodnum = 1; //start as the cheapest good, move up each level is less and less likely
		if (ran (1, 3) == 1) pgoodnum++; //1 in 3 odds of increasing good level
		if (ran (1, 5) == 1) pgoodnum++; //1 in 5 odds of increasing good level
		if (ran (1, 6) >= skill * 2) pgoodnum++; //1 in 2 odds of increasing good level
		if (ran (1, 6) >= skill * 2) pgoodnum++; //1 in 2 odds of increasing good level
		
		if (qty > pfleet[0][2]) qty = pfleet[0][2]; //if the qty of goods is more than cargo room than just do the max
		
		if (debug == 138) cout<<"\nPirate goods pshipnum - "<<pshipnum<<", qty - "<<qty<<", pgoodnum - "<<pgoodnum;
		
		if (pfleet[pshipnum][10] < pfleet[pshipnum][2] && (pfleet[pshipnum][2] - pfleet[pshipnum][10]) < qty)
		{// if this ship has some room left, and the ammount of room it has is < the ammount we are buying
			qty -= (pfleet[pshipnum][2] - pfleet[pshipnum][10]); //subtract the ammount we are putting on this ship, from the ammount we are buying
			pfleet[pshipnum][pgoodnum+10] += (pfleet[pshipnum][2] - pfleet[pshipnum][10]); //add the ammount to the good slot for this ship
			pfleet[0][pgoodnum+10] += (pfleet[pshipnum][2] - pfleet[pshipnum][10]); //add ammount to total ammount of that good you have
			pfleet[0][10] += (pfleet[pshipnum][2] - pfleet[pshipnum][10]); //add the ammount to the total goods
			pfleet[pshipnum][10] += (pfleet[pshipnum][2] - pfleet[pshipnum][10]); //add the ammount to the total goods slot for this ship
		}
		else if (pfleet[pshipnum][10] < pfleet[pshipnum][2] && (pfleet[pshipnum][2] - pfleet[pshipnum][10]) > qty)
		{//if this ship has room, and the amount of room is > the ammount we are buying
			pfleet[pshipnum][pgoodnum+10] += qty; //add the ammount to the good slot for this ship
			pfleet[pshipnum][10] += qty; //add the ammount to the total goods slot for this ship
			pfleet[0][pgoodnum+10] += qty; //add ammount to total ammount of that good you have
			pfleet[0][10] += qty; //add the ammount to the total goods 
			qty=0; //since this ship fit all the cargo 0 out qty, there is no more cargo to put on any ships
		}
		else if (pfleet[pshipnum][10] < pfleet[pshipnum][2] && (pfleet[pshipnum][2] - pfleet[pshipnum][10]) == qty && qty != 0)
		{// if this ship has the exact ammount of room as the ammount we are buying (and it's not 0)
			pfleet[pshipnum][pgoodnum+10] += qty; //add the ammount to the good slot for this ship
			pfleet[pshipnum][10] += qty; //add the ammount to the total goods slot for this ship
			pfleet[0][pgoodnum+10] += qty; //add ammount to total ammount of that good you have
			pfleet[0][10] += qty; //add the ammount to the total goods 
			qty=0; //since this ship fit all the cargo 0 out qty, there is no more cargo to put on any ships
		}
	}
	
	paggressive = 0; //default to non agressive pirates
	//just adds fleet attack to 25% of fleet HP, adds the two fleets togethor and if a random number in that range is greater than players attack + 25% HP than the pirates will attack
	if (ran(1, (pfleet[0][3] + (pfleet[0][6] / 4)) + (fleet[0][3] + (fleet[0][6] / 4))) > (fleet[0][3] + (fleet[0][6] / 4))) paggressive = 1;
	
	if (pciv == playerciv && playerciv != 0) paggressive = 0; //if the player and pirates both belong to a civ (not neutral) than they will not attack
	if (pciv > 0 && playerciv > 0 && playerciv != pciv) paggressive = 1; //if the pirates and the player belong to opposite civs (neither neutral) than they will attack
	if (debug == 138) plistships();
	
	sensortiebreaker = 0; //tie breaker if sensor ranges are same
	if (pfleet[0][5] == fleet[0][5]) sensortiebreaker = ran(0, 1); //if it's 1 we get the tie breaker, if 0 they do
	
	if (pfleet[0][5] >= fleet[0][5] + sensortiebreaker)
	{//if the pirates sensor range is better than players, they have the first choice of what to do
		if (paggressive == 1) battle(0); //battle, pirates get first shot
		if (paggressive == 0 && pciv == playerciv && playerciv != 0) piratetalk(); //pirates talk
	}
	else if (pfleet[0][5] <= fleet[0][5] + sensortiebreaker)
	{//if players sensor range is better, the player has first choice
		cout<<"\nYou have come across another fleet, it is ";
		if (pciv == 0) cout<<"neutral"; //a bunch of complicated nonsense to display the correct civ for player and pirates
		if (pciv == 1) cout<<"a member of The Galactic Star Empire";
		if (pciv == 2) cout<<"a member of The United Republic of Planets";
		cout<<", and you are ";
		if (playerciv == 0) cout<<"neutral";
		if (playerciv == 1) cout<<"a member of The Galactic Star Empire";
		if (playerciv == 2) cout<<"a member of The United Republic of Planets";
		cout<<".";
		cout<<"\nThere ";
		if (pfleet[0][0] > 1) cout<<"are"; //correct pluralization depending on > 1 ships
		if (pfleet[0][0] == 1) cout<<"is";
		cout<<" "<<pfleet[0][0]<<" ship";
		if (pfleet[0][0] > 1) cout<<"s";
		cout<<", their combined attack is "<<pfleet[0][3]<<", and combined HP is "<<pfleet[0][6];
		cout<<"\nYou have ";
		cout<<fleet[0][0]<<" ship";
		if (fleet[0][0] > 1) cout<<"s";
		cout<<", their combined attack is "<<fleet[0][3]<<", and combined HP is "<<fleet[0][6];
		if (pciv > 0 && playerciv > 0 && playerciv != pciv) cout<<"\nWhat do you want to do? 1 - attack, 2 - run "; //if the pirates and the player belong to opposite civs (neither neutral) they attack
		if (pciv == 0 || playerciv == 0) cout<<"\nWhat do you want to do? 1 - attack, 2 - run, 3 - talk ";
		
		if (pciv != playerciv || playerciv == 0) cin>>attackrun; //player decided to attack or run, if player and pirates are same civ then it defaults to talk
		if (pciv == playerciv && playerciv != 0) attackrun = 3; //talk to same civ pirates
		if (attackrun > 3 || attackrun < 1) attackrun = 2; //attack rival civ pirates
		
		if (pciv == playerciv && playerciv != 0 && attackrun == 1) attackrun = 2; //if the player and pirates both belong to a civ (not neutral) than they will not attack
		if (pciv > 0 && playerciv > 0 && playerciv != pciv && attackrun == 3) attackrun = 2; //if the pirates and the player belong to opposite civs (neither neutral) they attack
		
		if (attackrun == 1) 
		{//attack
			battle(1); //if you attack than we battle, player gets first shot
		}
		else if (attackrun == 2 && pfleet[0][1] > fleet[0][1] && paggressive == 1)
		{// if you try to run but they are faster and want to attack then we battle
			cout<<"\nThe enemy fleet is faster, they have fired upon you!";
			battle(0); //goto battle, pirates get first shot
		}
		else if (attackrun == 2)
		{// run away
			cout<<"\nYou have out run them.";
			return;
		}
		else if (attackrun == 3 && paggressive == 1)
		{// try to talk, but they attack
			cout<<"\nYou attempted to contact the enemy fleet, but they have fired upon you!";
			battle(0); //goto battle, pirates get first shot
		}
		else if (attackrun == 3 && paggressive == 0)
		{// talk
			cout<<"\nContacting fleet...";
			piratetalk(); //goto battle, pirates get first shot
		}
		else
		{//shouldn't happen, but it did a few times, added the output, even without the debug to help when it does happen.
			cout<<"\nSome crazy wormhole bullshit happened";
			cout<<"\nattackrun - "<<attackrun<<", pfleet[0][1] - "<<pfleet[0][1]<<", fleet[0][1]"<<fleet[0][1]<<", paggressive - "<<paggressive;
		}
	}
	destroypfleet();
}

void forclose()
{//player debt gets too high, bank starts taking stuff
	cout<<"\n\n***You have defaulted on your loan, the bank is now seizing your property!***\n";
	money += savings;//put any money you have in savings into your main money pile
	savings = 0;
	if (debt > money) 
	{//if your debt is greater than your money bank will take money and ships
		debt -= money;//take away as much money as you have from the debt
		money = 0;//you don't have any more money after you pay off the debt
		if (fleet[0][0] > 0)
		{//since you still owe more money the bank will take your first ship, and cargo
			cout<<"\nThe bank has seized ship# 1, and all goods on it, and sold it at auction.";
			debt -= (fleet[1][4] / 2); //gives you half cost for ship, no money for goods
			loseship(1); //take away the ship
		}
	}
	else if (debt <= money)
	{//if you have enough money to pay back debt than bank will just take the money
		money -= debt; //pay off debt
		debt = 0;
	}
	
	if (debt > money && fleet[0][0] < 1)
	{//if after paying as much as you can you don't have any money, or ships you lose
		cout<<"\nYou have no more money or property, but you still owe the bank money!";
		cout<<"\nYou'll have to go into hiding as a bum in the Flordidia System";
		cout<<"\nYou Lose!";
		gameover = 1; //gameover
	}
}

void day(int space)
{//the flow of time
	int shipnum; //ship index number
	int planetnum; //index number of planets
	int tempplanetnum; //second index, for use in comparing 
	int taxed; //ammount taxed
	
	cday++; //increace the current day counter
	if (space > 0) money -= (fleet[0][0] * 3 * skill) + 3; //cost per day
	if (space < 1) money -= 3;
	debt *= interest; //1% per day interest on loans
	savings *= 1.005; //.5% per day interest on savings
	if (cday % 15 == 0 && playerciv > 0)
	{//every 15 days you pay taxes if you are a memeber of a civ
		if (playerleader == 0)
		{//if the player isn't a civ leader time to pay taxes
			cout<<"\nPlayer civ - "<<playerciv;
			calcassets(); //find the assests
			if (assets < 1000) assets = 1000;
			taxper = (civ[playerciv] + 1) * 0.005 * skill;
			cout<<"\nTax time";
			cout<<"\nTaxable assest = "<<assets<<", taxes paid = "<<int(assets * taxper);
			money -= int(assets * taxper);//% taxes on total assets
		}
		else if (playerleader > 0)
		{//if player is civ leader he gets taxes
			cout<<"\nTax time";
			cout<<"\nWelcome leader of ";
			if (playerleader == 0) cout<<"no civilization";
			if (playerleader == 1) cout<<"The Galactic Star Empire";
			if (playerleader == 2) cout<<"The United Republic of Planets";
			cout<<"\nindependent planets - "<<civ[0];
			cout<<"\nPlanets under the control of The Galactic Star Empire - "<<civ[1];
			cout<<"\nPlanets under the control of The United Republic of Planets - "<<civ[2];
			cout<<"\nYour current tax rate is "<<playertax<<"% your Defense spending per planet is "<<defense<<", treasury is "<<money;
			
			taxper = playertax * 0.01; //playertax is a whole number 10% is stored as just 10, so we multiply by .01 to get a fraction of 100 percentage (10% is .10)
			taxed = ran(10, 25) * taxper * (civ[playerleader] + 1) * 100000; //taxes paid are (ran 10-25 * tax % * planets + 1 * 1000,000 Example 15 * .10 * 8 * 100,000 = 1,200,000 (15 ran, 8 planets)
			taxed += ran(0, playertax * 3) * ran(0, playertax * 50); //stupid attempt to make tax ammount non round number, needs work
			money += taxed; //give player the tax money
			if (debug == 138) cout<<"\ntaxper - "<<taxper<<"(civ[playerleader] + 1) - "<<(civ[playerleader] + 1);
			cout<<"\nTaxes paid - "<<taxed<<", new treasury - "<<money;
		}
	}
	for (shipnum = 1; shipnum <= fleet[0][0]; shipnum++)
	{//loop through each ship, for the purpose of slow damage
		if (space > 0 && ran(1, 4) <= skill) 
		{//25% of losing a HP per day, simulates wear and tear
			fleet[shipnum][6]--; //takes HP from ship
			fleet[0][6]--; //takes HP from fleet
		}
		if (fleet[0][6] < 0) fleet[0][6] = 0; //think about doing lose ship here, since the ship has 0 HP and it takes damage, for now just keep it at 0, if we do loseship display a message about losing the ship
	}
	if (space > 0 && ran (1, 10) == 1) pirates(); //1 in 10 odds of coming across another fleet
	flips(); //determines if plannets will flip
	costs(); //determines the rise and fall of prices
	
	if (rebel[0][0] > 0 && ran (1, 10) == 1)
	{//code to see if rebels lauch their paid for attack
		tempplanetnum = ran(1, rebel[0][0]); //rebel[0][0] stores total number of pending attacks, pick a number at random
		for (planetnum = 1; tempplanetnum > 0; planetnum++)
		{//code to see which planet gets attacked, count down from the number we picked, stop when we reach 0, we'll be on the planet to attack.  I did a poor job of explaining it, but it isn't a lot of code, just look at it.
			if (rebel[planetnum][1] = 1) tempplanetnum--;
		}
		cout<<"\n\n***The mercenaries you payed have launced their rebellion on world "<<planetnum<<"!***\n";
		if (ran(1, 4) <= skill)
		{//1 in 3 odds of them winning
			cout<<"\nThey have succefully taken control of world "<<planetnum<<" from the ";
			if (world[rebel[planetnum][2]][3] == 1) cout<<"Galactic Star Empire";
			if (world[rebel[planetnum][2]][3] == 2) cout<<"United Republic of Planets";
			if (world[rebel[planetnum][2]][3] == 0) cout<<"opposing rebel forces";
			cout<<"\nThey have transfered control to the ";
			if (rebel[planetnum][2] == 1) cout<<"Galactic Star Empire";
			if (rebel[planetnum][2] == 2) cout<<"United Republic of Planets";
			if (rebel[planetnum][2] == 0) cout<<"allied rebel forces";
			cout<<"!";
			world[planetnum][3] = rebel[planetnum][2];
		}
		else 
		{//other wise they lose
			cout<<"\nThey were unsuccessful.";
		}
		rebel[0][0]--;
		rebel[planetnum][1] = 0;
		rebel[planetnum][2] = 0;
	}
	
	for (planetnum = 1; planetnum <= 20; planetnum++)
	{//if player is a leader he gets up to date price info for al his worlds
		if (world[planetnum][3] == playerleader && playerleader != 0)
		{
			goods[planetnum][1][2] = goods[planetnum][1][1]; //updates the price list for this world
			goods[planetnum][2][2] = goods[planetnum][2][1];
			goods[planetnum][3][2] = goods[planetnum][3][1];
			goods[planetnum][4][2] = goods[planetnum][4][1];
			goods[planetnum][5][2] = goods[planetnum][5][1];
		}
	}
	
}

void travel()
{//calculates travel
	if (fleet[0][0] < 1)
	{//no ship
		cout<<"\nHow do you plan to leave without a ship?";
		return;
	}
	else if (fleet[0][0] > 100)
	{//no ship
		cout<<"\nFleet too large, please reduce to under 100 ships.";
		return;
	}
	worlddisplay();
	speedship();
	senseship();
	cout<<"\nWhere do you want to go? ";
	cin>>travelto;
	if (travelto > 0 && travelto <= 20 && travelto != curworld && fleet[0][0] > 0 && money > 100)
	{//if it's a valid world to travel to we will do the calculations and travel there
		dis[1] = abs(world[curworld][1] - world[travelto][1]); //finds A
		dis[2] = abs(world[curworld][2] - world[travelto][2]); //finds B
		dis[3] = int(sqrt(pow(dis[1], 2) + pow(dis[2], 2))); //finds C using A^2 + B^2 = C^2
		dis[3] *= 10; //multiplies distance by 10, just to make the distances farther
		if (debug == 138) cout<<"\ndis[3] - "<<dis[3]<<", fleet[0][1] - "<<fleet[0][1];
		traveltime = dis[3] / fleet[0][1]; //travel time is just the distance divided by the fleet speed
		//traveltime *= 10;
		traveltime++; //ensure traveltime is never 0
		cout<<"\nDistance is "<<dis[3]<<", speed of the fleet is "<<fleet[0][1]<<", travel time will be "<<traveltime;
		
		for (;traveltime > 0; traveltime--)
		{//loop through the travel time, letting one day pass for each unit of travel time
			day(1);
		}
		
		curworld = travelto; //sets the curworld to the travel to world
		if (playerciv != world[curworld][3]) 
		{//if the player isn't this world's civ then he gets a 5% pelanty on any purchases
			worldpen = badworld;
		}
		else if (playerciv == world[curworld][3])
		{//if the player is the world's civ he gets a 1% pelanty on any purchases
			worldpen = goodworld;
		}
		goods[curworld][1][2] = goods[curworld][1][1]; //updates the price list for this world
		goods[curworld][2][2] = goods[curworld][2][1];
		goods[curworld][3][2] = goods[curworld][3][1];
		goods[curworld][4][2] = goods[curworld][4][1];
		goods[curworld][5][2] = goods[curworld][5][1];
		visit[curworld] = cday; //the date of the price data
		//worlddisplay();
		if (debt > 125000) forclose();//max debt you can have is 125,000 after that bank starts taking our stuff
	}
	else if (money <= 100)
	{
		cout<<"\nYou don't have enough money";
	}
	else if (travelto < 1 || travelto > 20)
	{
		cout<<"\nI don't know where that is";
	}
	else if (travelto == curworld)
	{
		cout<<"\nUm, that's where we are now, maybe you need to get some rest";
	}
	else if (fleet[0][0] < 1)
	{
		cout<<"\nFirst we need a ship";
	}
	else 
	{
		cout<<"\nCan't let you do that";
	}
	
}

void playersetup()
{//player picks his civ
	int newciv; //the civ player is switching to
	int confirm; //user confirmation
	
	/*
	Neutral
	Galactic Star Empire
	United Republic of Planets
	*/
	if (playerleader == 0)
	{//not a civ leader, gets the standard player civ change stuff
		cout<<"\nWelcome to player set up";
		cout<<"\nYou are currently a member of ";
		if (playerciv == 0) cout<<"no civilization";
		if (playerciv == 1) cout<<"The Galactic Star Empire";
		if (playerciv == 2) cout<<"The United Republic of Planets";
		cout<<"\nindependent planets - "<<civ[0];
		cout<<"\nPlanets under the control of The Galactic Star Empire - "<<civ[1];
		cout<<"\nPlanets under the control of The United Republic of Planets - "<<civ[2];
		cout<<"\nPick your alignment, 0 - Neutral, 1 - Galactic Star Empire, Cost - "<<(civ[1] * 50) + 100<<", 2 - United Republic of Planets, Cost - "<<(civ[2] * 50) + 100<<" ";
		cin>>newciv; //player picks civ
		if (newciv != playerciv && newciv == 0)
		{//player chooses neutral
			cout<<"\nEnter 138 to confirm Neutral, this is free ";
			cin>>confirm;
			if (confirm == 138) playerciv = 0; //if the confirmation was 138 than we switch to neutral
		}
		else if (newciv != playerciv && newciv == 1  && money >= civ[1] * 50)
		{//player chooses GSE
			cout<<"\nEnter 138 to confirm Galactic Star Empire, this will cost "<<(civ[1] * 50) + 100<<" ";
			cin>>confirm;
			if (confirm == 138 && money >= civ[1] * 50) 
			{//if the confirmation was 138 and the player has the money we switch to neutral
				playerciv = 1;
				money -= (civ[1] * 50) + 100; //take the money for the membership dues
			}
		}
		else if (newciv != playerciv && newciv == 2  && money >= civ[2] * 50)
		{//player chooses URP
			cout<<"\nEnter 138 to confirm United Republic of Planets, this will cost "<<(civ[2] * 50) + 100<<" ";
			cin>>confirm;
			if (confirm == 138 && money >= civ[2] * 50) 
			{//if the confirmation is 138 and player has the money we switch
				playerciv = 2;
				money -= (civ[2] * 50) + 100; //take the money for the membership dues
			}
		}
	}
	else if (playerleader > 0)
	{//player is a civ leader this is the civ management
		cout<<"\nWelcome leader of ";
		if (playerleader == 0) cout<<"no civilization";
		if (playerleader == 1) cout<<"The Galactic Star Empire";
		if (playerleader == 2) cout<<"The United Republic of Planets";
		cout<<"\nindependent planets - "<<civ[0];
		cout<<"\nPlanets under the control of The Galactic Star Empire - "<<civ[1];
		cout<<"\nPlanets under the control of The United Republic of Planets - "<<civ[2];
		cout<<"\nYour current tax rate is "<<playertax<<"% your Defense spending per planet is "<<defense<<", treasury is "<<money;
		cout<<"\nEnter 138 to change them ";
		cin>>confirm; //change tax and defense spending
		if (confirm == 138)
		{
			cout<<"\nEnter new tax rate as a percentage, followed by new defense per planet ";
			cin>>playertax; //new tax
			cin>>defense; //new defense spending
			//you can goto flips to see how tax and defence work, I took these two comments from there to help explain here
			//add players defense spending to inf, 10k is neutral, anything less hurts anything more helps, every 500 either way is 1 inf point
			//decide if planets will rebel because of taxes.  10% taxes should yeild 1/50 odds of rebellion.  Odds for rebellion are taxes / 500
			if (playertax < 0 || playertax > 100)
			{//make sure it's a real percentage
				cout<<"\nInvalid ammount, default to 10%";
				playertax = 10; 
			}
			if (defense < 0) defense = 0; //user input checking
		}
	}
}

void bar()
{//bar where various underworld transactions take place, gambling, secrets, mercenaries, and for some reason challenging civ leader
/*
Ideas for bar:
some kind of illegal stolen goods
multiple gambling games
some non gambling games
buy something high cost high value, maybe something that gives you updated prices on all worlds
some kind of influence on the war
some kind of updated info on war

*/
	int barchoice; //players choice in bar
	int datachoice; //players choice to buy would data
	int dataprice; //the price of the world data
	int rebelchoice; //players choice for the rebels
	int rebelcontrol; //civ to control plannet if rebeled succed
	int rebelworld; //world that rebellion will attack on
	
	calcassets();
	cout<<"\nWelcome to the world "<<curworld<<" bar.";
	cout<<"\nWhat would you like to do? 1 - Gamble, 2 - Buy updated world data, 3 - Talk to old man, 4 - Mercenaries ";
	if (assets > 100000 && fleet[0][0] > 10 && playerciv > 0 && playerleader == 0) 
	{//if player meets requirements let him challenge leader, reqs are over 100,000 in assets, more than 10 ships, member of civ, not current leader
		cout<<"\n5 - Challenge leader of ";
		if (playerciv == 1) cout<<"The Galactic Star Empire";
		if (playerciv == 2) cout<<"The United Republic of Planets";
		cout<<" ";
	}
	cin>>barchoice;//user decided what he wants
	if (barchoice == 1)
	{//gambling goto the function allready made for it
		gamble();
	}
	else if (barchoice == 2)
	{// buy updated price data, same as talk to other ships, but you pay 1000 here
		dataprice = 1000; //set to 1000, used to have worldpen, but took it off here
		cout<<"\nA man offers to sell you updated world data for an unknown number of worlds, for "<<dataprice;
		cout<<"\nIs it a deal? 1 - Yes, 2 - No ";
		cin>>datachoice;//decide if they want to buy it or not
		if (datachoice == 1 && money > dataprice)
		{//yes they buy it
			money -= dataprice;//takes price for data
			piratetalk();//give the actual data
		}
	}
	else if (barchoice == 3)
	{//secrets
		dataprice = 1000;//no world pen
		cout<<"\nAn old man offers to give you secret advice for "<<dataprice;
		cout<<"\nIs it a deal? 1 - Yes, 2 - No ";
		cin>>datachoice;// buy secret yes or no
		if (datachoice == 1 && money > dataprice)
		{//yes they buy a secret
			money -= dataprice;
			oldman();
		}
	}
	else if (barchoice == 4)
	{//hire mercinaries to try to take over control of planet, and give it to whatever faction you choose.
		cout<<"\nThe local rebel leader would be willing to attempt a coup against the ";
		if (world[curworld][3] == 1) cout<<"Galactic Star Empire";
		if (world[curworld][3] == 2) cout<<"United Republic of Planets";
		if (world[curworld][3] == 0) cout<<"opposing rebel forces";
		cout<<" that currently control this place.";
		cout<<"\nThat is, if the price was right.";
		cout<<"\nHow does 50000 sound?";
		cout<<"\n1 - It sounds right!, 2 - No way, 3 - How about 25000 ";
		cin>>rebelchoice;//decide yes or no, or bargin
		if (rebelchoice == 1 && money >= 50000)
		{//pay the 50k for a rebillion
			cout<<"\nOk, and if we are victorious who do you want put in control?";
			cout<<"\n1 - Galactic Star Empire, 2 - United Republic of Planets, 0 - Neutral ";
			cin>>rebelcontrol;//decide who will get the planet
			if (rebelcontrol >= 0 && rebelcontrol < 3)
			{//if you pick a valid choice then it goes ahead, if not nothing
				cout<<"Ok, we'll start planning our attack immediately!";
				rebel[curworld][1] = 1; //set first index to 1, as a flag that rebellion is pending
				rebel[curworld][2] = rebelcontrol; //set second index to whoever player wants to control planet
				rebel[0][0]++; //add one more pending rebellion to total
				money -= 50000;
			}
		}
		else if (rebelchoice == 2)
		{//choice not to pay, nothing
			cout<<"\nFine, our services aren't cheap";
		}
		else if (rebelchoice == 3 && money >= 25000)
		{//try to bargin, I don't think this ever works but I guess it may, I leave it in here so people can waste time trying
			if (ran (1, assets + 2000) > 1000)
			{//if a rand num from 1 to your assets + 2000 is over 1000 no deal.  Since you need 25k just to get here your assets are at least 25k (unless you have lots of debt I guess)
				cout<<"\nNo way!  Our services aren't cheap ";
				return;
			}
			else
			{//they go for the bargin
				cout<<"Ok fine, but only because we support your cause.  If we are victorious who do you want put in control?";
				cout<<"\n1 - Galactic Star Empire, 2 - United Republic of Planets, 0 - Neutral ";
				cin>>rebelcontrol;
				if (rebelcontrol >= 0 && rebelcontrol < 3)
				{//choose the new owner after rebillion
					cout<<"Ok, we'll start planning our attack immediately!";
					rebel[curworld][1] = 1; //set first index to 1, as a flag that rebellion is pending
					rebel[curworld][2] = rebelcontrol; //set second index to whoever player wants to control planet
					rebel[0][0]++; //add one more pending rebellion to total
					money -= 25000;
				}
			}
		}
	}
	else if (barchoice == 5 && assets > 100000 && fleet[0][0] > 10 && playerciv > 0 && playerleader == 0)
	{//challenge the civ leader
		cout<<"\nIf you fail you will surely be caught and punished, the leader will have the entire fleet of his civilization at his disposal.";
		cout<<"\nAre you sure?  Enter 138 to confirm ";
		cin>>rebelchoice;//reused rebelchoice here, don't be confused
		if (rebelchoice == 138) bosspirates();
	}
}

void debugoutput()
{//displays debug info
/*
int ships[10][10][50]; //stats on the ships for sale
int fleet[100][20]; //ships in your fleet, x = index, y = stats 1 - speed, 2 - cargo, 3 - attack, 4 - cost, 5 - sensor range, 6 - hit points, 7 - max HP, 8 - class, 9 - model, 10 - total cargo, 11+ goods
int pfleet[100][20]; //ships in a pirate fleet
int world[21][20]; //worlds, x = index, y = stats
int goods[21][10][3]; //prices of the goods
int goodschange[21][10]; //the ammount change in prices
int upgrade[21][10][3]; //prices of the ship upgrades
int inf[3]; //influence on the worlds
int dis[4]; //distance to other worlds
int cday; //the current day
int debug = 0; //sets mode to debug mode
int curworld = 3; //the world the player is currently on
int money; //player money
int savings; //players savings
int debt; //ammount of debt player has
int assets; //players total assets
float worldpen = 1.01; //penalty for being on a world not belonging to your civ
int playerciv; //civ of the player
int play; //do you want to continue to play
int traveltime; //time it will take to travel any given distance
int travelto; //world to travel to
int civ[3]; //total number of worlds each civ owns
float taxper; //percentage of tax you will pay
int skill; //player skill level
int pmoney; //pirates money
bool paggressive; //are the pirates aggressive
bool bossfight; //will determine if it's a boss fight or not
int rebel[21][10] //1 if attack pending, 2 who will control planet
*/
	int x, y, z;
	debug=138;
	cout<<"\nDebug Info:\n";
	cout<<"\nMoney - "<<money<<", Day - "<<cday;
	for (x = 0; x <= fleet[0][0] + 3; x++)
	{
		cout<<"\n\nShip # "<<x;
		for(y = 0; y <= 19; y++)
		{
			cout<<" Item # "<<y<<" - "<<fleet[x][y];
		}
	}
	for (x = 0; x <= 20; x++)
	{
		cout<<"\n\nWorld # "<<x;
		for(y = 0; y <= 19; y++)
		{
			cout<<" Item # "<<y<<" - "<<world[x][y];
		}
	}
	for (x = 0; x <= 20; x++)
	{
		cout<<"\n\nGood # "<<x;
		for(y = 0; y <= 9; y++)
		{
			for (z = 0; z <= 2; z++)
			{
				cout<<" Item # "<<y<<" - "<<goods[x][y][z];
			}
		}
	}
	for (x = 0; x <= 20; x++)
	{
		cout<<"\n\nRebel # "<<x;
		for(y = 0; y <= 9; y++)
		{
			cout<<" Item # "<<y<<" - "<<rebel[x][y];
		}
	}
}

void start()
{//start up screen, gets skill level, and displays help
	/*
	float interest; //debt interest
	float badworld; //worldpen for a world with a different civ
	float goodworld; //worldpen for a world with same civ
	float civpen; //pen for players civ in likelyhood of getting plannets in a flip
	*/
	cout<<"\nPirate2 by Dale Swanson!";
	do
	{//loop until play makes valid choice
		cout<<"\nEnter skill level 1 - Easy, 2 - Normal, 3 - Hard, 0 - Help ";
		cin>>skill; //get skill level from player
		if (skill == 1) 
		{//easy
			cout<<"\nOk, baby...";
			money = 10000;
			debt = 0;
			interest = 1.006;
			badworld = 1.03;
			goodworld = 1.01;
			civpen = 1.1;
		}
		else if (skill == 2) 
		{//normal
			cout<<"\nNow we're talking.";
			money = 0;
			debt = 0;
			interest = 1.01;
			badworld = 1.05;
			goodworld = 1.01;
			civpen = 1;
		}
		else if (skill == 3) 
		{//hard
			cout<<"\nYou're a braver man that I!";
			money = 0;
			debt = 10000;
			interest = 1.02;
			badworld = 1.1;
			goodworld = 1.03;
			civpen = 0.9;
		}
		else if (skill == 138)
		{//special
			cout<<"\nEnter starting money: ";
			cin>>money;
			debt = 0;
			interest = 1.01;
			badworld = 1.05;
			goodworld = 1.01;
			civpen = 1;
			skill = 2;
		}
		else
		{//help
			cout<<"\nHelp!";
			help();
		}
	}
	while (skill != 1 && skill != 2 && skill != 3 && skill != 138); //continue looping until player picks a skill
}

int main()
{//main loop, displays main menu, calls all the above functions, exits game
    time_t sseconds; //start seconds, will be used to seed the rng
    time(&sseconds); //get our seed for rng
    srand((unsigned int) sseconds); //seed rng
	int confirm;
	int days;
	start(); //get user input on skill
	money += 3; //3 free dollars since first day is automatic, and you'll lose 3 dollars before you begin
	worldsetup(); //setup the planets
	shipsetup(); //setup ships
	day(0); //run the first day
	goods[curworld][1][2] = goods[curworld][1][1]; //give you good prices for current world
	goods[curworld][2][2] = goods[curworld][2][1];
	goods[curworld][3][2] = goods[curworld][3][1];
	goods[curworld][4][2] = goods[curworld][4][1];
	goods[curworld][5][2] = goods[curworld][5][1];
	visit[curworld] = cday; //the date of the price data
	
	do
	{//main loop of the game, will continue until play equals 0
		listships();
		worlddisplay();
		cout<<"\nWelcome to world "<<curworld<<"!  What would you like to do?";
		cout<<"\nDay - "<<cday<<", Money - "<<money<<", Savings - "<<savings<<", Debt - "<<debt<<", Cargo Space - "<<fleet[0][2]<<", Cargo Used - "<<fleet[0][10]; 
		cout<<"\n1-Buy/Sell Goods, 2-Visit Ship Yard, 3-Bank, 4-Ship Out, 5-Manage Cargo, 6-Bar, 7-Player Setup, 8-Rest, 0-Exit ";
		cin>>play;
		if (gameover == 1) play = 0;
		if (play == 1)
		{//Buy/Sell Goods
			buygoods();
		}
		else if (play == 2)
		{//Visit Ship Yard
			buyship();
		}
		else if (play == 3)
		{//Bank
			bank();
		}
		else if (play == 4)
		{//Ship Out
			travel();
		}
		else if (play == 5)
		{//Manage Cargo
			manage();
		}
		else if (play == 6)
		{//Gamble
			bar();
		}
		else if (play == 7)
		{//player setup
			playersetup();
		}
		else if (play == 8)
		{//rest
			cout<<"\nHow many days do you want to rest for? ";
			cin>>days;
			for(; days > 0; days--)
			{
				groundday();
			}
		}
		else if (play == 9)
		{//testing stuff
			bosspirates();
		}
		
		else if (play == 138)
		{//debug
			debugoutput();
		}
		
		else if (play == 0)
		{//end
			cout<<"\nAre you sure you want to leave?  Enter 138 to confirm ";
			cin>>confirm;
			if (confirm == 138) gameover = 1;//user ends game
		}
	} while (gameover == 0);
	
	if (ran(1, 4) == 1) oldman(); //25% of a free secret on exit.

	cout<<"\nGood bye ";
	
	cout<<"\nDone...\n";
    system("PAUSE");
    return EXIT_SUCCESS;
}
