#include "mapobject.h"

MapObject::~MapObject()
{
	if (image) SDL_FreeSurface(image);
}
void MapObject::Init(int sx, int sy, std::string img, std::string ffpk, float zzoom)
{
	x=sx;
	y=sy;
	imgload=img;
	fpk=ffpk;
	zoom=zzoom;
	image=0;
	loaded=false;
	mmr=0;
	mmg=255;
	mmb=0;
}
void MapObject::Display(int sx, int sy)
{
	DrawIMG(image, sx, sy);
}
void MapObject::SetName(std::string n)
{
	name=n;
}
void MapObject::SetX(int sx)
{
	x=sx;
}
void MapObject::SetY(int sy)
{
	y=sy;
}
void MapObject::SetMMR(Uint8 sr)
{
	mmr=sr;
}
void MapObject::SetMMG(Uint8 sg)
{
	mmg=sg;
}
void MapObject::SetMMB(Uint8 sb)
{
	mmb=sb;
}
void MapObject::SetZoom(float szoom)
{
	zoom=szoom;
}
int MapObject::GetWidth()
{
	return image->w;
}
int MapObject::GetHeight()
{
	return image->h;
}
float MapObject::GetZoom()
{
	return zoom;
}
Uint8 MapObject::GetMMR()
{
	return mmr;
}
Uint8 MapObject::GetMMG()
{
	return mmg;
}
Uint8 MapObject::GetMMB()
{
	return mmb;
}
std::string MapObject::GetName()
{
	return name;
}
int MapObject::GetX()
{
	return x;
}
int MapObject::GetY()
{
	return y;
}
int MapObject::GetNegX()
{
	return -x;
}
int MapObject::GetNegY()
{
	return -y;
}
void MapObject::FreeImage()
{
	if (image) SDL_FreeSurface(image);
	image=0;
	loaded=false;
}
void MapObject::LoadImage()
{
	FreeImage();
	SDL_Surface *temp=FPK_LoadIMG(fpk, imgload);
	image=rotozoomSurface(temp, 0, zoom, SMOOTH);
	loaded=true;
	SDL_FreeSurface(temp);
}
int MapObject::GetSectorX()
{
	return (int)x/1000;
}
int MapObject::GetSectorY()
{
	return (int)y/1000;
}
bool MapObject::IsLoaded()
{
	return loaded;
}

Station::Station()
{
	int i;
	for (i=0; i<30; i++)
	{
		shop.buyitems[i].price=0;
		shop.buyitems[i].baseprice=0;
		shop.buyitems[i].baseimport=0;
		shop.buyitems[i].dispimage=0;
		shop.buyitems[i].bayw=0;
		shop.buyitems[i].bayl=0;
		shop.buyitems[i].bayd=0;
		shop.buyitems[i].amount=0;
		shop.buyitems[i].holdamount=0;
		shop.stock[i]=0;
	}
	for (i=0; i<100; i++)
	{
		ships[i]=0;
	}
	shop.buynext=0;
	fpkshipnext=0;
	cargonext=0;
	HRepairsAllowed=false;
	SRepairsAllowed=false;
	hasShipyard=false;
	pm=2;
	ticks=0;
	buy=false;
	sell=false;
	shipyardshop=0;
}
Station::~Station()
{
	int i;
	for (i=0; i<30; i++)
	{
		if (shop.buyitems[i].dispimage)
		{
			SDL_FreeSurface(shop.buyitems[i].dispimage);
			shop.buyitems[i].dispimage=0;
		}
	}
	if (shipyardshop) delete shipyardshop;
}
void Station::AddItemToBuyShop(__shopitem item)
{
	if (shop.buynext<30)
	{
		shop.buyitems[shop.buynext]=item;
		if (item.baseimport) shop.stock[shop.buynext]=item.baseimport;
		else shop.stock[shop.buynext]=-1;
		shop.buynext++;
	}
	buy=true;
}
void Station::AddSellableCargo(__cargo cargo)
{
	if(cargonext<30)
	{
		cargosell[cargonext].type=cargo.type;
		cargosell[cargonext].priceBought=cargo.priceBought;
		cargonext++;
	}
	sell=true;
}
void Station::AllowHullRepairs(unsigned int hullRepairCost)
{
	HRepairsAllowed=true;
	hrepairCost=hullRepairCost;
}
void Station::AllowShieldRepairs(unsigned int shieldRepairCost)
{
	SRepairsAllowed=true;
	srepairCost=shieldRepairCost;
}
void Station::AddShipToShipyard(FPKShip *ship)
{
	hasShipyard=true;
	if (fpkshipnext<100)
	{
		ships[fpkshipnext]=ship;
		fpkshipnext++;
	}
}
void Station::InitShipyardShop()
{
	SDL_Surface *screen=SDL_GetVideoSurface();
	int w=screen->w;
	if (!shipyardshop)
	{
		shipyardshop=new ShipyardShop(80, 40, w-160, 400, ships);
	}
}
void Station::Dock(Unit *ship)
{
	if (!ticks) ticks=SDL_GetTicks();
	int done=0;
	SDL_Event event;
	SDL_Surface *screen=SDL_GetVideoSurface();
	int w=screen->w;
	int h=screen->h;
	SDL_Surface *bg = SDL_CreateRGBSurface(0, screen->w, screen->h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
	SDL_Rect dest;
	dest.x = 0;
	dest.y = 0;
	SDL_BlitSurface(screen, NULL, bg, &dest);
	SDL_Color white = { 0xFF, 0xFF, 0xFF, 0 };
	TTF_Font *font=TTF_OpenFont("fonts/arial.ttf", 14);
	TTF_Font *font12=TTF_OpenFont("fonts/arial.ttf", 12);

	int choice=0;
	int buycur=0;
	int getx, gety;
	int click=-1;
	int click2=-1;
	int click3=-1;
	int sPercentNeeded, hPercentNeeded;

	GUI *tabs=new GUI;
	tabs->Init();
	tabs->MakeWindow(60, 40, w-120, 400);
	int sBuy=tabs->MakeBigButton(80,90,60,60, "Buy",14);
	int sSell=tabs->MakeBigButton(80,152,60,60, "Sell",14);
	int sRepairs=tabs->MakeBigButton(80,214,60,60, "Repairs",14);
	int sShipyard=tabs->MakeBigButton(80,276,60,60, "Shipyard",14);
	int sLeaveStation=tabs->MakeButton((w/2)-50,410,100,"Leave Station");

	GUI *buyGUI=new GUI;
	buyGUI->Init();
	GUI *buy2GUI=new GUI;
	buy2GUI->Init();
	GUI *sellGUI=new GUI;
	sellGUI->Init();
	GUI *sell2GUI=new GUI;
	sell2GUI->Init();
	GUI *bayGUI=new GUI;
	bayGUI->Init();
	GUI *repairGUI=new GUI;
	repairGUI->Init();

	int sbPrev=buyGUI->MakeButton(180,380,50,"< Prev");
	int sbBuy=buyGUI->MakeButton(260,380,90,"Buy this");
	int sbNext=buyGUI->MakeButton(380,380,50,"Next >");
	//int sbAllTypes=buyGUI->MakeButton(160,100,70,"All");

	int sbUp=buy2GUI->MakeDirButton(400,220,0);
	int sbDown=buy2GUI->MakeDirButton(400,234,1);
	int sbLeft=buy2GUI->MakeDirButton(376,228,2);
	int sbRight=buy2GUI->MakeDirButton(424,228,3);

	int sbayPrev=bayGUI->MakeButton(180,380,50,"< Prev");
	int sbaySelect=bayGUI->MakeButton(260,380,90,"Select");
	int sbayNext=bayGUI->MakeButton(380,380,50,"Next >");
	int sbayCancel=bayGUI->MakeButton(460,380,50,"Cancel");

	int ssPrev=sellGUI->MakeButton(180,380,50,"< Prev");
	int ssSell=sellGUI->MakeButton(260,380,90,"Sell");
	int ssNext=sellGUI->MakeButton(380,380,50,"Next >");
	//int ssCargo=sellGUI->MakeButton(160,100,70,"Cargo");

	int ssUp=sell2GUI->MakeDirButton(300,200,0);
	int ssDown=sell2GUI->MakeDirButton(300,214,1);
	int ssLeft=sell2GUI->MakeDirButton(276,208,2);
	int ssRight=sell2GUI->MakeDirButton(324,208,3);

	int rHull=-1;
	int rShield=-1;
	if (HRepairsAllowed && SRepairsAllowed)
	{
		rHull=repairGUI->MakeButton(w/2-95,h/2,90, "Repair Hull");
		rShield=repairGUI->MakeButton(w/2+5,h/2,90, "Recharge Shields");
	}
	else
	{
		if (HRepairsAllowed) rHull=repairGUI->MakeButton(w/2-45,h/2,90, "Repair Hull");
		if (SRepairsAllowed) rShield=repairGUI->MakeButton(w/2-45,h/2,90, "Recharge Shields");
	}
	hPercentNeeded=(int)(((float)ship->GetHullHits()/(float)ship->GetMaxHullHits())*100);
	sPercentNeeded=(int)(((float)ship->GetShieldHits()/(float)ship->GetMaxShieldHits())*100);

	__ship_slot *slots;
	slots=ship->GetSlots();
	int numslots=ship->GetNumSlots();
	int slotcur=0;

	__cargo *cargohold;
	cargohold=ship->GetCargo();
	int cargofill=ship->GetCargoFill();
	std::string cargotypecur=cargohold[0].type;
	int cargocur=0;
	int curcargocount=0;
	int cargosellcur=0;

	int buying=0;
	int allowbuyquant=0;
	int buyquant=1;

	int allowsellquant=0;
	int sellquant=1;

	char disp[300];

	while(!done)
	{
		Slock();
		while (SDL_PollEvent(&event))
		{
			switch (event.type) {
			// close button clicked
			case SDL_QUIT:
				exit(0);
				break;

			// handle the keyboard
			case SDL_KEYDOWN:
				switch (event.key.keysym.sym) {
				case SDLK_ESCAPE:
					done = 1;
					break;
				default:
					break;
				}
				break;
			case SDL_MOUSEMOTION:
				SDL_GetMouseState(&getx, &gety);
				tabs->SendMouseMotion(getx,gety);
				if (choice==0 && buying==0) buyGUI->SendMouseMotion(getx, gety);
				if (choice==0 && buying==1) bayGUI->SendMouseMotion(getx, gety);
				if (choice==1) sellGUI->SendMouseMotion(getx, gety);
				if (choice==2) repairGUI->SendMouseMotion(getx, gety);
				if (allowbuyquant) click3 = buy2GUI->SendMouseMotion(getx, gety);
				if (allowsellquant) click3 = sell2GUI->SendMouseMotion(getx, gety);
				break;
			case SDL_MOUSEBUTTONDOWN:
				SDL_GetMouseState(&getx, &gety);
				click = tabs->SendMouseClick(getx, gety);
				if (choice==0 && buying==0) click2 = buyGUI->SendMouseClick(getx, gety);
				if (choice==0 && buying==1) click2 = bayGUI->SendMouseClick(getx, gety);
				if (choice==1) click2 = sellGUI->SendMouseClick(getx, gety);
				if (choice==2) click2 = repairGUI->SendMouseClick(getx, gety);
				if (allowbuyquant) click3 = buy2GUI->SendMouseClick(getx, gety);
				if (allowsellquant) click3 = sell2GUI->SendMouseClick(getx, gety);
				break;
			case SDL_MOUSEBUTTONUP:
				SDL_GetMouseState(&getx, &gety);
				if (click!=-1 && click==tabs->SendMouseClick(getx, gety))
				{
					DrawIMG(bg,0,0);
					tabs->Draw();
					if (choice==2) repairGUI->Draw();
					centerString(font,w/2,54,this->GetName(),white);
					sprintf(disp, "Credits: %d",ship->GetCredits());
					drawString(font12, w-160, 70, disp, white);
					Sulock();
					SDL_Flip(screen);
					if (click==sBuy)
					{
						choice=0;
						buying=0;
					}
					if (click==sSell)
					{
						choice=1;
						if (sell)
						{
							sell2GUI->UpdateButtons();
							sellquant=1;
							cargofill=ship->GetCargoFill();
							cargotypecur=cargohold[0].type;
							cargocur=0;
							curcargocount=0;
							int i;
							for(i=0; i<cargofill; i++)
							{
								if(!strcmp(cargotypecur.c_str(),cargohold[i].type.c_str())) curcargocount++;
							}
							for(i=0;i<cargonext;i++)
							{
								if(!strcmp(cargotypecur.c_str(),cargosell[i].type.c_str())) cargosellcur=i;
							}
						}
					}
					if (click==sRepairs)
					{
						choice=2;
					}
					if (click==sShipyard)
					{
						choice=3;
					}
					if (click==sLeaveStation)
					{
						done=1;
					}
				}
				if (click2!=-1 && (click2==buyGUI->SendMouseClick(getx, gety) || click2==sellGUI->SendMouseClick(getx, gety) || click2==bayGUI->SendMouseClick(getx, gety) || click2==repairGUI->SendMouseClick(getx, gety)))
				{
					DrawIMG(bg,0,0);
					tabs->Draw();
					if (choice==0 && buying==0)
					{
						if (buy)
						{
							if (shop.buyitems[buycur].dispimage) DrawIMG(shop.buyitems[buycur].dispimage,150,140);
							if (!strcmp(shop.buyitems[buycur].type.c_str(),"cargo"))
							{
								sprintf(disp, "Cargo Space: %d/%d",cargofill,ship->GetCargoUnits());
								drawString(font12, 150, 340, disp, white);
								allowbuyquant=1;
							}
							else if (!strcmp(shop.buyitems[buycur].type.c_str(),"ammo") && !strcmp(shop.buyitems[buycur].holdweapon.c_str(),"heatseeking"))
							{
								sprintf(disp, "Ammo Space: %d/%d",ship->GetHeatSeekingAmmoFill(),ship->GetHeatSeekingAmmoSpace());
								drawString(font12, 150, 340, disp, white);
								allowbuyquant=0;
							}
							else allowbuyquant=0;
							sprintf(disp, "Name: %s",shop.buyitems[buycur].name.c_str());
							drawString(font12, 260, 140, disp, white);
							sprintf(disp, "Price: %d credits",shop.buyitems[buycur].price*buyquant);
							drawString(font12, 260, 160, disp, white);
							sprintf(disp, "Description: %s",shop.buyitems[buycur].desc.c_str());
							drawString(font12, 260, 180, disp, white);
							if (shop.stock[buycur]>=0)
							{
								sprintf(disp, "Stock: %d",shop.stock[buycur]);
								drawString(font12, 260, 200, disp, white);
								sprintf(disp, "Quantity to Buy: %d",buyquant);
								drawString(font12, 260, 220, disp, white);
							}
							else buyquant=1;
							buyGUI->Draw();
							if (allowbuyquant) buy2GUI->Draw();
							if (click2==sbPrev)
							{
								if (buycur-1<0) buycur=shop.buynext-1;
								else buycur-=1;
								buyquant=1;
							}
							else if (click2==sbNext)
							{
								if (buycur+1>=shop.buynext) buycur=0;
								else buycur+=1;
								buyquant=1;
							}
							else if (click2==sbBuy)
							{
								if (!strcmp("bay",shop.buyitems[buycur].type.c_str()))
								{
									if (ship->GetCredits()>=(unsigned int)shop.buyitems[buycur].price)
									{
										slotcur=0;
										buying=1;
									}
								}
								else if (!strcmp("cargo",shop.buyitems[buycur].type.c_str()))
								{
									if (shop.stock[buycur]>0 && ship->GetCredits()>=((unsigned int)shop.buyitems[buycur].price)*buyquant && ship->GetCargoFill()<ship->GetCargoUnits())
									{
										int i;
										for(i=0; i<buyquant; i++)
										{
											__cargo newcargo;
											newcargo.type=shop.buyitems[buycur].name;
											newcargo.priceBought=shop.buyitems[buycur].price;
											ship->AddToCargo(newcargo);
										}
										ship->SetCredits(ship->GetCredits()-(shop.buyitems[buycur].price)*buyquant);
										shop.stock[buycur]-=buyquant;
									}
									//else the cargo bays are full
								}
								else if (!strcmp(shop.buyitems[buycur].type.c_str(),"ammo") && !strcmp(shop.buyitems[buycur].holdweapon.c_str(),"heatseeking"))
								{
									int bayid=ship->GetHeatSeekingBay();
									if (ship->GetCredits()>=(unsigned int)shop.buyitems[buycur].price && bayid)
									{
										ship->SetCredits(ship->GetCredits()-shop.buyitems[buycur].price);
										ship->AddWeaponToSlot(bayid,shop.buyitems[buycur].fpk,shop.buyitems[buycur].imagefn,shop.buyitems[buycur].amount);
										Log::WriteVerbose("Inserted %d of ammo into bay id %d\n",shop.buyitems[buycur].amount,bayid);
									}
								}
								else
								{
									if (shop.stock[buycur]>0 && ship->GetCredits()>=(unsigned int)shop.buyitems[buycur].price)
									{
										ship->SetCredits(ship->GetCredits()-shop.buyitems[buycur].price);
										shop.stock[buycur]-=1;
									}
									if (shop.stock[buycur]==-1 && ship->GetCredits()>=(unsigned int)shop.buyitems[buycur].price)
									{
										ship->SetCredits(ship->GetCredits()-shop.buyitems[buycur].price);
										//give it
									}
								}
							}
						}
						else centerString(font,w/2,h/2,"No Buying at this station",white);
					}
					else if (choice==0 && buying==1)
					{
						sprintf(disp, "Selected Bay: %s",shop.buyitems[buycur].name.c_str());
						drawString(font12, 160, 90, disp, white);
						sprintf(disp, "Bay Width: %d  Bay Length: %d  Bay Depth: %d",shop.buyitems[buycur].bayw,shop.buyitems[buycur].bayl,shop.buyitems[buycur].bayd);
						drawString(font12, 160, 110, disp, white);
						if (!slots[slotcur].full)
						{
							if (!strcmp(slots[slotcur].type.c_str(),"") ||
							(!strcmp(slots[slotcur].type.c_str(),shop.buyitems[buycur].baytype.c_str())))
							{
								sprintf(disp, "Place in your ship slot %d?",slots[slotcur].id);
								drawString(font12, 160, 140, disp, white);
								sprintf(disp, "Bay Width: %d  Bay Length: %d  Bay Depth: %d",slots[slotcur].w,slots[slotcur].l,slots[slotcur].d);
								drawString(font12, 160, 160, disp, white);
							}
							else
							{
								sprintf(disp, "Ship slot %d does not take %s bays",slots[slotcur].id,shop.buyitems[buycur].baytype.c_str());
								drawString(font12, 160, 140, disp, white);
							}
						}
						else
						{
							sprintf(disp, "Ship slot %d is full",slots[slotcur].id);
							drawString(font12, 160, 140, disp, white);
						}
						bayGUI->Draw();
						if (click2==sbayPrev)
						{
							if (slotcur-1>=0) slotcur--;
							else slotcur=numslots-1;
						}
						else if (click2==sbayNext)
						{
							if (slotcur+1<numslots) slotcur++;
							else slotcur=0;
						}
						else if (click2==sbaySelect)
						{
							if (!slots[slotcur].full && slots[slotcur].w>=shop.buyitems[buycur].bayw && slots[slotcur].d>=shop.buyitems[buycur].bayd && slots[slotcur].l>=shop.buyitems[buycur].bayl)
							{
								if ((!strcmp(slots[slotcur].type.c_str(),"") || !strcmp(slots[slotcur].type.c_str(),"weapon")) && !strcmp(shop.buyitems[buycur].baytype.c_str(),"weapon"))
								{
									ship->AddWeaponBayToSlot(slots[slotcur].id, shop.buyitems[buycur].holdweapon, shop.buyitems[buycur].holdamount);
									if (!shop.buyitems[buycur].holdamount) ship->AddWeaponToSlot(slots[slotcur].id,shop.buyitems[buycur].fpk,shop.buyitems[buycur].imagefn,0);
									ship->SetCredits(ship->GetCredits()-shop.buyitems[buycur].price);
									buying=0;
								}
								if ((!strcmp(slots[slotcur].type.c_str(),"") || !strcmp(slots[slotcur].type.c_str(),"cargo")) && !strcmp(shop.buyitems[buycur].baytype.c_str(),"cargo"))
								{
									ship->AddCargoBayToSlot(slots[slotcur].id,shop.buyitems[buycur].cargounits);
									ship->SetCredits(ship->GetCredits()-shop.buyitems[buycur].price);
									buying=0;
								}
							}
						}
						else if (click2==sbayCancel)
						{
							buying=0;
						}
					}
					else if (choice==1)
					{
						if (sell)
						{
							if (cargofill)
							{
								allowsellquant=1;
								sprintf(disp, "Sell Cargo: %s",cargotypecur.c_str());
								drawString(font12, 160, 140, disp, white);
								sprintf(disp, "In Cargo Hold: %d",curcargocount);
								drawString(font12, 160, 160, disp, white);
								sprintf(disp, "Price: %d",cargosell[cargosellcur].priceBought*sellquant);
								drawString(font12, 160, 180, disp, white);
								if (allowsellquant)
								{
									sprintf(disp, "Sell how many: %d",sellquant);
									drawString(font12, 160, 200, disp, white);
								}
							}
							else
							{
								allowsellquant=0;
								drawString(font12, 160, 140, "No Cargo to Sell", white);
							}
							sprintf(disp, "Cargo Space: %d/%d",cargofill,ship->GetCargoUnits());
							drawString(font12, 150, 340, disp, white);
							sellGUI->Draw();
							if (click2==ssPrev)
							{
								int i=0;
								bool cycled=false;
								for(i=cargocur; i>=-1; i--)
								{
									if(strcmp(cargotypecur.c_str(),cargohold[i].type.c_str()))
									{
										cargocur=i;
										cargotypecur=cargohold[i].type;
										break;
									}
									if(i==0)
									{
										i=cargofill-1;
										cycled=true;
									}
									if(cycled && i==cargocur) break;
								}
								curcargocount=0;
								for(i=0; i<cargofill; i++)
								{
									if(!strcmp(cargotypecur.c_str(),cargohold[i].type.c_str())) curcargocount++;
								}
								for(i=0;i<cargonext;i++)
								{
									if(!strcmp(cargotypecur.c_str(),cargosell[i].type.c_str())) cargosellcur=i;
								}
							}
							else if (click2==ssNext)
							{
								int i=0;
								bool cycled=false;
								for(i=cargocur; i<cargofill+1; i++)
								{
									if(strcmp(cargotypecur.c_str(),cargohold[i].type.c_str()))
									{
										cargocur=i;
										cargotypecur=cargohold[i].type;
										break;
									}
									if(i==cargofill-1)
									{
										i=0;
										cycled=true;
									}
									if(cycled && i==cargocur) break;
								}
								curcargocount=0;
								for(i=0; i<cargofill; i++)
								{
									if(!strcmp(cargotypecur.c_str(),cargohold[i].type.c_str())) curcargocount++;
								}
								for(i=0;i<cargonext;i++)
								{
									if(!strcmp(cargotypecur.c_str(),cargosell[i].type.c_str())) cargosellcur=i;
								}
							}
							else if (click2==ssSell && cargofill)
							{
								int i;
								unsigned int price=0;
								for (i=0; i<cargonext; i++)
								{
									if(!strcmp(cargohold[cargocur].type.c_str(),cargosell[i].type.c_str()))
									{
										price=cargosell[i].priceBought;
									}
								}
								for (i=0; i<shop.buynext; i++)
								{
									if(!strcmp(cargohold[cargocur].type.c_str(),shop.buyitems[i].name.c_str()))
									{
										shop.stock[i]+=sellquant;
									}
								}
								ship->SetCredits(ship->GetCredits()+(price*sellquant));
								ship->RemoveAmountFromCargo(cargotypecur,sellquant);
								cargofill=ship->GetCargoFill();
								if (cargocur>=cargofill) cargocur=0;
								curcargocount-=sellquant;
								if (sellquant>curcargocount) sellquant=curcargocount;
								if (sellquant<=0) sellquant=1;
								cargotypecur=cargohold[cargocur].type;
								if (cargofill && curcargocount<=0)
								{
									curcargocount=0;
									for(i=0; i<cargofill; i++)
									{
										if(!strcmp(cargotypecur.c_str(),cargohold[i].type.c_str())) curcargocount++;
									}
								}
								for(i=0;i<cargonext;i++)
								{
									if(!strcmp(cargotypecur.c_str(),cargosell[i].type.c_str())) cargosellcur=i;
								}
							}
							if (allowsellquant) sell2GUI->Draw();
						}
						else centerString(font,w/2,h/2,"No Selling at this station",white);
					}
					else if (choice==2)
					{
						repairGUI->Draw();
						int inc=100;
						if (HRepairsAllowed)
						{
							sprintf(disp, "Hull Damage: %d%%",hPercentNeeded);
							drawString(font12, 160, inc, disp, white);
							inc+=16;
							sprintf(disp, "Price to Fully Repair Hull: %d",hPercentNeeded*hrepairCost);
							drawString(font12, 160, inc, disp, white);
							inc+=16;
						}
						if (SRepairsAllowed)
						{
							sprintf(disp, "Shield Damage: %d%%",sPercentNeeded);
							drawString(font12, 160, inc, disp, white);
							inc+=16;
							sprintf(disp, "Price to Fully Recharge Shields: %d",sPercentNeeded*srepairCost);
							drawString(font12, 160, inc, disp, white);
							inc+=16;
						}
						if (HRepairsAllowed || SRepairsAllowed)
						{
							inc+=4;
							drawString(font12, 160, inc, "Partial Repairs can be made", white);
						}
						else drawString(font12, 160, inc, "No Repairs can be made here", white);
					}
					centerString(font,w/2,54,this->GetName(),white);
					sprintf(disp, "Credits: %d",ship->GetCredits());
					drawString(font12, w-160, 70, disp, white);
					Sulock();
					SDL_Flip(screen);
					if (click2==rHull && choice==2)
					{
						float increment=((float)1/(float)ship->GetMaxHullHits())*100;
						if (ship->GetCredits()<(hPercentNeeded*hrepairCost))
						{
							float take=0;
							while ((take+increment)*hrepairCost<=(float)ship->GetCredits())
							{
								take+=increment;
							}
							ship->SetCredits(ship->GetCredits()-(((int)take)*hrepairCost));
							ship->SetHullHits((int)(ship->GetHullHits()-((float)((int)take)/(float)hrepairCost)));
						}
						else
						{
							ship->SetCredits(ship->GetCredits()-(hPercentNeeded*hrepairCost));
							ship->SetHullHits(0);
						}
						hPercentNeeded=(int)(((float)ship->GetHullHits()/(float)ship->GetMaxHullHits())*100);
					}
					if (click2==rShield && choice==2)
					{
						float increment=((float)1/(float)ship->GetMaxShieldHits())*100;
						if (ship->GetCredits()<(sPercentNeeded*srepairCost))
						{
							float take=0;
							while ((take+increment)*srepairCost<=(float)ship->GetCredits())
							{
								take+=increment;
							}
							ship->SetCredits(ship->GetCredits()-(((int)take)*srepairCost));
							ship->SetShieldHits((int)(ship->GetShieldHits()-((float)((int)take)/(float)srepairCost)));
						}
						else
						{
							ship->SetCredits(ship->GetCredits()-(sPercentNeeded*srepairCost));
							ship->SetShieldHits(0);
						}
						sPercentNeeded=(int)(((float)ship->GetShieldHits()/(float)ship->GetMaxShieldHits())*100);
					}
					cargofill=ship->GetCargoFill();
				}
				if (click3!=-1 && (click3==buy2GUI->SendMouseClick(getx, gety)))
				{
					if (click3==sbUp && buyquant+1<=shop.stock[buycur] && buyquant+1<=ship->GetCargoUnits()-cargofill)
					{
						buyquant++;
					}
					else if (click3==sbDown && buyquant-1>0)
					{
						buyquant--;
					}
					else if (click3==sbRight && buyquant+10<=shop.stock[buycur] && buyquant+10<=ship->GetCargoUnits()-cargofill)
					{
						buyquant+=10;
					}
					else if (click3==sbRight && buyquant+10>shop.stock[buycur])
					{
						buyquant=shop.stock[buycur];
					}
					else if (click3==sbRight && buyquant+10>ship->GetCargoUnits()-cargofill)
					{
						buyquant=ship->GetCargoUnits()-cargofill;
						if (!buyquant) buyquant=1;
					}
					else if (click3==sbLeft && buyquant-10>0)
					{
						buyquant-=10;
					}
					else if (click3==sbLeft && buyquant-10<=0)
					{
						buyquant=1;
					}
				}
				if (click3!=-1 && (click3==sell2GUI->SendMouseClick(getx, gety)))
				{
					if (click3==ssUp && sellquant+1<=curcargocount)
					{
						sellquant++;
					}
					else if (click3==ssDown && sellquant-1>0)
					{
						sellquant--;
					}
					else if (click3==ssRight && sellquant+10<=curcargocount)
					{
						sellquant+=10;
					}
					else if (click3==ssRight && sellquant+10>curcargocount)
					{
						sellquant=curcargocount;
					}
					else if (click3==ssLeft && sellquant-10>0)
					{
						sellquant-=10;
					}
					else if (click3==ssLeft && sellquant-10<=0)
					{
						sellquant=1;
					}
				}
				tabs->UpdateButtons();
				if (choice==0 && buying==0) buyGUI->UpdateButtons();
				else if (choice==0 && buying==1) bayGUI->UpdateButtons();
				else if (choice==1) sellGUI->UpdateButtons();
				else if (choice==2) repairGUI->UpdateButtons();
				if (allowbuyquant) buy2GUI->UpdateButtons();
				if (allowsellquant) sell2GUI->UpdateButtons();
				break;
			default:
				break;
			}
			if (choice==3 && hasShipyard)
			{
				int tempdone=shipyardshop->PollEvent(event);
				if (tempdone==-1) done=-1;
			}
		}
		DrawIMG(bg,0,0);
		tabs->Draw();
		centerString(font,w/2,54,this->GetName(),white);
		sprintf(disp, "Credits: %d",ship->GetCredits());
		drawString(font12, w-160, 70, disp, white);
		switch (choice)
		{
			int inc;
			case 0:
				if (buy)
				{
					if (buying==0)
					{
						if (shop.buyitems[buycur].dispimage) DrawIMG(shop.buyitems[buycur].dispimage,150,140);
						if (!strcmp(shop.buyitems[buycur].type.c_str(),"cargo"))
						{
							sprintf(disp, "Cargo Space: %d/%d",cargofill,ship->GetCargoUnits());
							drawString(font12, 150, 340, disp, white);
							allowbuyquant=1;
						}
						else if (!strcmp(shop.buyitems[buycur].type.c_str(),"ammo"))
						{
							sprintf(disp, "Ammo Space: %d/%d",ship->GetHeatSeekingAmmoFill(),ship->GetHeatSeekingAmmoSpace());
							drawString(font12, 150, 340, disp, white);
							allowbuyquant=0;
						}
						else allowbuyquant=0;
						sprintf(disp, "Name: %s",shop.buyitems[buycur].name.c_str());
						drawString(font12, 260, 140, disp, white);
						sprintf(disp, "Price: %d credits",shop.buyitems[buycur].price*buyquant);
						drawString(font12, 260, 160, disp, white);
						sprintf(disp, "Description: %s",shop.buyitems[buycur].desc.c_str());
						drawString(font12, 260, 180, disp, white);
						if (shop.stock[buycur]>=0)
						{
							sprintf(disp, "Stock: %d",shop.stock[buycur]);
							drawString(font12, 260, 200, disp, white);
							sprintf(disp, "Quantity to Buy: %d",buyquant);
							drawString(font12, 260, 220, disp, white);
						}
						else buyquant=1;
						buyGUI->Draw();
						if (allowbuyquant) buy2GUI->Draw();
					}
					if (buying==1)
					{
						sprintf(disp, "Selected Bay: %s",shop.buyitems[buycur].name.c_str());
						drawString(font12, 160, 90, disp, white);
						sprintf(disp, "Bay Width: %d  Bay Length: %d  Bay Depth: %d",shop.buyitems[buycur].bayw,shop.buyitems[buycur].bayl,shop.buyitems[buycur].bayd);
						drawString(font12, 160, 110, disp, white);
						if (!slots[slotcur].full)
						{
							if (!strcmp(slots[slotcur].type.c_str(),"") ||
							(!strcmp(slots[slotcur].type.c_str(),shop.buyitems[buycur].baytype.c_str())))
							{
								sprintf(disp, "Place in your ship slot %d?",slots[slotcur].id);
								drawString(font12, 160, 140, disp, white);
								sprintf(disp, "Bay Width: %d  Bay Length: %d  Bay Depth: %d",slots[slotcur].w,slots[slotcur].l,slots[slotcur].d);
								drawString(font12, 160, 160, disp, white);
							}
							else
							{
								sprintf(disp, "Ship slot %d does not take %s bays",slots[slotcur].id,shop.buyitems[buycur].baytype.c_str());
								drawString(font12, 160, 140, disp, white);
							}
						}
						else
						{
							sprintf(disp, "Ship slot %d is full",slots[slotcur].id);
							drawString(font12, 160, 140, disp, white);
						}
						bayGUI->Draw();
					}
				}
				else centerString(font,w/2,h/2,"No Buying at this station",white);
				break;
			case 1:
				if (sell)
				{
					if (cargofill)
					{
						allowsellquant=1;
						sprintf(disp, "Sell Cargo: %s",cargotypecur.c_str());
						drawString(font12, 160, 140, disp, white);
						sprintf(disp, "In Cargo Hold: %d",curcargocount);
						drawString(font12, 160, 160, disp, white);
						sprintf(disp, "Price: %d",cargosell[cargosellcur].priceBought*sellquant);
						drawString(font12, 160, 180, disp, white);
						if (allowsellquant)
						{
							sprintf(disp, "Sell how many: %d",sellquant);
							drawString(font12, 160, 200, disp, white);
						}
					}
					else
					{
						allowsellquant=0;
						drawString(font12, 160, 140, "No Cargo to Sell", white);
					}
					sprintf(disp, "Cargo Space: %d/%d",cargofill,ship->GetCargoUnits());
					drawString(font12, 150, 340, disp, white);
					sellGUI->Draw();
					if (allowsellquant) sell2GUI->Draw();
				}
				else centerString(font,w/2,h/2,"No Selling at this station",white);
				break;
			case 2:
				inc=100;
				if (HRepairsAllowed)
				{
					sprintf(disp, "Hull Damage: %d%%",hPercentNeeded);
					drawString(font12, 160, inc, disp, white);
					inc+=16;
					sprintf(disp, "Price to Fully Repair Hull: %d",hPercentNeeded*hrepairCost);
					drawString(font12, 160, inc, disp, white);
					inc+=16;
				}
				if (SRepairsAllowed)
				{
					sprintf(disp, "Shield Damage: %d%%",sPercentNeeded);
					drawString(font12, 160, inc, disp, white);
					inc+=16;
					sprintf(disp, "Price to Fully Recharge Shields: %d",sPercentNeeded*srepairCost);
					drawString(font12, 160, inc, disp, white);
					inc+=16;
				}
				if (HRepairsAllowed || SRepairsAllowed)
				{
					inc+=4;
					drawString(font12, 160, inc, "Partial Repairs can be made", white);
				}
				else drawString(font12, 80, inc, "No Repairs can be made here", white);
				repairGUI->Draw();
				break;
			case 3:
				if (hasShipyard)
				{
					shipyardshop->Display();
				}
				else centerString(font,w/2,h/2,"No Shipyard at this station",white);
				break;
			default:
				break;
		}
		Sulock();
		SDL_Flip(screen);
	}
	TTF_CloseFont(font);
	TTF_CloseFont(font12);
	delete tabs;
	delete buyGUI;
	delete sellGUI;
	delete buy2GUI;
	delete sell2GUI;
	delete bayGUI;
	delete repairGUI;
}
void Station::ChangeSupply()
{
	int i;
	Uint32 import_ticks=60000;
	if(SDL_GetTicks()>=ticks+import_ticks)
	{
		for(i=0; i<30; i++)
		{
			if (shop.buyitems[i].baseprice)
			{
				int difference=shop.buyitems[i].baseimport-shop.stock[i];
				if (shop.stock[i]<shop.buyitems[i].baseimport)
				{
					shop.stock[i]+=difference;
				}
				//change price
				//int price=(int)(shop.buyitems[i].baseprice*((float)pm/(float)10));  //This sets the price to the basic price * profit
				//int perc_difference=(int)((float)100/(float)shop.buyitems[i].baseimport*(float)difference);
				//int mod_price=(int)((100-perc_difference)*(5*((float)pm/(float)10)));
				int perc_difference=(int)((float)100/(float)shop.buyitems[i].baseimport*(float)difference);
				int mod_price=shop.buyitems[i].baseprice+(int)(perc_difference*((float)pm/(float)10));
				shop.buyitems[i].price=mod_price;
			}
		}
		ticks=SDL_GetTicks();
	}
}
void Station::AddToWaitTime(Uint32 time)
{
	ticks+=time;
}
Uint32 Station::GetTicks()
{
	return ticks;
}

