//Copyright (C) 2007 SiegeLord
//See ReadMe.txt and License.txt for copying information

#include "neonz.h"
#include "weapon.h"
#include "ship.h"
#include "shipregistry.h"
#include "thruster.h"
#include "camera.h"
#include "orders.h"
#include "explosion.h"
#include "message.h"
#include "flasher.h"
#include "sounds.h"

struct SWeaponDescriptor
{
	int type;
	float reload_rate;
	float min_damage;
	float max_damage;
	float range;

	float cost;

	string description;

	SBitmap* image;
};

struct SShipDescriptor
{
	int num_weapons;
	vector2d slot_positions[3];
	float slot_rotations[3];
	SBitmap* image;
	string image_name;

	SRect bbox;

	float max_hitpoints;
	
	float armour;

	vector2d polar_accel;
	vector2d polar_max_vel;

	string description;

	vector<vector2d> top_thrusters;
	vector<vector2d> left_thrusters;
	vector<vector2d> right_thrusters;
	vector<vector2d> bottom_thrusters;

	vector<vector2d> flasher_positions;

	float cost;
};

//one one of interest here is that the weapon types have their 0 index point to no weapon
//i.e. if our weapon type is 0, that weapon slot is not occupied
struct SLoadOut
{
	SLoadOut()
	{
		ship_type = 0;

		weapon_type[0] = 1;
		weapon_type[1] = 1;
		weapon_type[2] = 1;

		num_in_build_list = 0;
	}
	int ship_type;

	//for now this is 0 - plasma, 1 - rocket
	int weapon_type[3];//3 weapon slots potentially

	int num_in_build_list;//not used for anything but drawing the number in the build screen really...
};

vector<SWeaponDescriptor> g_vecWeaponTypes;
vector<SShipDescriptor> g_vecShipTypes;

int g_nCurLoadOutSelection = 0;
int g_nCurSelectionType = 0;

SGUIItem* g_guiLeftPane;
SGUIItem* g_guiTopPane;
SGUIItem* g_guiBottomPane;
SGUIItem* g_guiBuildPane;

vector<SButton*> g_vecLeftButtons;
vector<SButton*> g_vecTopButtons;
vector<SButton*> g_vecBottomButtons;

int g_nCurMoneySpent[2];
list<int> g_lstBuildList[2];
vector<SLoadOut> g_vecLoadOuts[2];

extern int g_nPlayerCash[2];
extern SShip* g_obMotherShip[2];

extern FONT* g_fntFont;
extern int g_nPlayer;
extern SObjectEx *g_obRootObject;

void InitBuildScreen()
{
	g_nCurMoneySpent[0] = 0;
	g_nCurMoneySpent[1] = 0;

	const int size = 150;
	//init the gui

	g_guiLeftPane = new SGUIItem();
	InitGUIItem(g_guiLeftPane, 0, 0, 0, 0, 0);

	g_guiTopPane = new SGUIItem();
	InitGUIItem(g_guiTopPane, 0, 0, 0, 0, 0);

	g_guiBottomPane = new SGUIItem();
	InitGUIItem(g_guiBottomPane, 0, 0, 0, 0, 0);

	g_guiBuildPane = new SGUIItem();
	InitGUIItem(g_guiBuildPane, 0, 0, 0, 0, 0);

	CreateButton(g_guiBuildPane, 0, SCREEN_H - 30, size, 30, GetMask(1), "Back", g_fntFont);
	CreateButton(g_guiBuildPane, 0, SCREEN_H - 60, size, 30, GetMask(2), "Cancel", g_fntFont);
	CreateButton(g_guiBuildPane, 0, SCREEN_H - 90, size, 30, GetMask(3), "Build", g_fntFont);

	g_vecLeftButtons.push_back(CreateButton(g_guiLeftPane, 0, 0, size, 30, GetMask(1), "Chassis", g_fntFont));
	g_vecLeftButtons.push_back(CreateButton(g_guiLeftPane, 0, 30, size, 30, GetMask(2), "Slot 1", g_fntFont));
	g_vecLeftButtons.push_back(CreateButton(g_guiLeftPane, 0, 60, size, 30, GetMask(3), "Slot 2", g_fntFont));
	g_vecLeftButtons.push_back(CreateButton(g_guiLeftPane, 0, 90, size, 30, GetMask(4), "Slot 3", g_fntFont));

	g_vecTopButtons.push_back(CreateButton(g_guiTopPane, 1 * size, 0, size, size, GetMask(1), 0, g_fntFont));
	g_vecTopButtons.push_back(CreateButton(g_guiTopPane, 2 * size, 0, size, size, GetMask(2), 0, g_fntFont));
	g_vecTopButtons.push_back(CreateButton(g_guiTopPane, 3 * size, 0, size, size, GetMask(3), 0, g_fntFont));
	g_vecTopButtons.push_back(CreateButton(g_guiTopPane, 4 * size, 0, size, size, GetMask(4), 0, g_fntFont));
	g_vecTopButtons.push_back(CreateButton(g_guiTopPane, 5 * size, 0, size, size, GetMask(5), 0, g_fntFont));

	g_vecBottomButtons.push_back(CreateButton(g_guiBottomPane, 1 * size, SCREEN_H - size, size, size, GetMask(1), 0, g_fntFont));
	g_vecBottomButtons.push_back(CreateButton(g_guiBottomPane, 2 * size, SCREEN_H - size, size, size, GetMask(2), 0, g_fntFont));
	g_vecBottomButtons.push_back(CreateButton(g_guiBottomPane, 3 * size, SCREEN_H - size, size, size, GetMask(3), 0, g_fntFont));
	g_vecBottomButtons.push_back(CreateButton(g_guiBottomPane, 4 * size, SCREEN_H - size, size, size, GetMask(4), 0, g_fntFont));
	g_vecBottomButtons.push_back(CreateButton(g_guiBottomPane, 5 * size, SCREEN_H - size, size, size, GetMask(5), 0, g_fntFont));

	//init the choices etc
	SShipDescriptor ship;
	ship.num_weapons = 1;
	ship.slot_positions[0].Set(0, 0);
	ship.slot_rotations[0] = AL_PI / 2;
	ship.image_name = "ship1.tga";
	ship.image = FindBitmap(ship.image_name.c_str());
	ship.max_hitpoints = 100.0f;
	ship.polar_accel.Set(20, 0.05);
	ship.polar_max_vel.Set(20, 0.05);
	ship.cost = 1000;
	ship.armour = 2.0f;
	ship.description = "LUDWIG 300\n\nThis ship is a workhorse of all corporate militia. Sporting a reinforced turrent mount and strong anti-kinetic armor, it gets the job done quickly and with minimal investment.";

	ship.bbox.Set(-40, -40, 40, 40);

	ship.top_thrusters.push_back(vector2d(0, -20));
	ship.left_thrusters.push_back(vector2d(-43, 0));
	ship.right_thrusters.push_back(vector2d(43, 0));
	ship.bottom_thrusters.push_back(vector2d(0, 20));

	ship.flasher_positions.push_back(vector2d(-30, 0));
	ship.flasher_positions.push_back(vector2d(30, 0));

	g_vecShipTypes.push_back(ship);

	SShipDescriptor ship2;
	ship2.num_weapons = 2;
	ship2.slot_positions[0].Set(24, 0);
	ship2.slot_rotations[0] = 0;
	ship2.slot_positions[1].Set(-24, 0);
	ship2.slot_rotations[1] = AL_PI;
	ship2.image_name = "ship2.tga";
	ship2.image = FindBitmap(ship2.image_name.c_str());
	ship2.max_hitpoints = 250.0f;
	ship2.polar_accel.Set(18, 0.03);
	ship2.polar_max_vel.Set(18, 0.03);
	ship2.cost = 2100;
	ship2.armour = 8.0f;
	ship2.description = "HERRING HGF\n\nA powerful cruiser that is becomming a common sight in orbit around many worlds during these war-torn times.";

	ship2.bbox.Set(-50, -50, 50, 50);

	ship2.top_thrusters.push_back(vector2d(0, -24));
	ship2.left_thrusters.push_back(vector2d(-59, 0));
	ship2.right_thrusters.push_back(vector2d(59, 0));
	ship2.bottom_thrusters.push_back(vector2d(0, 24));

	ship2.flasher_positions.push_back(vector2d(11, -20));
	ship2.flasher_positions.push_back(vector2d(-11, 20));
	ship2.flasher_positions.push_back(vector2d(-11, -20));
	ship2.flasher_positions.push_back(vector2d(11, 20));

	g_vecShipTypes.push_back(ship2);

	SWeaponDescriptor weapon;
	weapon.min_damage = 1.0f;
	weapon.max_damage = 15.0f;
	weapon.range = 300.0f;
	weapon.reload_rate = 4.0f;
	weapon.cost = 500.0f;
	weapon.type = plasmaweapon_type;
	weapon.image = FindBitmap("plasma_turret.tga");
	weapon.description = "ZIG 6000 PLASMA LAUNCHER\n\nZig 6000 is a top of the line weapon. It fires superheated bolts of plasma. Most modern armour can scatter this plasma quite well.";

	g_vecWeaponTypes.push_back(weapon);

	weapon.min_damage = 14.0f;
	weapon.max_damage = 15.0f;
	weapon.range = 450.0f;
	weapon.reload_rate = 6.0f;
	weapon.cost = 600.0f;
	weapon.type = rocketweapon_type;
	weapon.image = FindBitmap("rocket_turret.tga");
	weapon.description = "BARF X ROCKET LAUNCHER\n\nThe standard launcher of the PUK 10000K armour-piercing missiles. These missiles are guided and so cheap to reproduce that their supply is limitless.";

	g_vecWeaponTypes.push_back(weapon);
	
	weapon.min_damage = 9.0f;
	weapon.max_damage = 14.0f;
	weapon.range = 350.0f;
	weapon.reload_rate = 5.0f;
	weapon.cost = 550.0f;
	weapon.type = ionbeamweapon_type;
	weapon.image = FindBitmap("ion_turret.tga");
	weapon.description = "GRONIM 2.0 ION BEAM CANNON\n\nThe Gronim Ion Beam Cannon is perhaps the most sophisticated weapon equippable by ships today. It accelerates a plasma stream using high temperature supercondutors.";

	g_vecWeaponTypes.push_back(weapon);

	g_vecLoadOuts[g_nPlayer].push_back(SLoadOut());
	g_vecLoadOuts[g_nPlayer].push_back(SLoadOut());
	g_vecLoadOuts[g_nPlayer].push_back(SLoadOut());
	g_vecLoadOuts[g_nPlayer].push_back(SLoadOut());

	//now create some load outs for the AI
	int player = 1 - g_nPlayer;
	SLoadOut load_out;

	load_out.ship_type = 0;
	load_out.weapon_type[0] = 1;
	g_vecLoadOuts[player].push_back(load_out);

	load_out.ship_type = 0;
	load_out.weapon_type[0] = 2;
	g_vecLoadOuts[player].push_back(load_out);
	
	load_out.ship_type = 0;
	load_out.weapon_type[0] = 3;
	g_vecLoadOuts[player].push_back(load_out);

	load_out.ship_type = 1;
	load_out.weapon_type[0] = 1;
	load_out.weapon_type[1] = 2;
	g_vecLoadOuts[player].push_back(load_out);

	load_out.ship_type = 1;
	load_out.weapon_type[0] = 2;
	load_out.weapon_type[1] = 2;
	g_vecLoadOuts[player].push_back(load_out);
	
	load_out.ship_type = 1;
	load_out.weapon_type[0] = 2;
	load_out.weapon_type[1] = 3;
	g_vecLoadOuts[player].push_back(load_out);
}

int GetBuildListSize(int player)
{
	return g_lstBuildList[player].size();
}

void AddToBuildList(int load_out, int player)
{
	if((unsigned)load_out >= g_vecLoadOuts[player].size())
	{
		return;
	}
	g_lstBuildList[player].push_back(load_out);
	g_vecLoadOuts[player][load_out].num_in_build_list++;
}

//this thing updates button for the selection panes
//activates - deactivates them as appropriate
void UpdateButtons()
{
	const int size1 = g_vecShipTypes[g_vecLoadOuts[g_nPlayer][g_nCurLoadOutSelection].ship_type].num_weapons + 1;
	const int size2 = g_vecLeftButtons.size();
	FOR1(size2)
	{
		g_vecLeftButtons[ii]->active = ii < size1;
		g_vecLeftButtons[ii]->visible = ii < size1;
	}
	const int size3 = g_vecLoadOuts[g_nPlayer].size();
	const int size4 = g_vecBottomButtons.size();
	FOR1(size4)
	{
		g_vecBottomButtons[ii]->active = ii < size3;
		g_vecBottomButtons[ii]->visible = ii < size3;
	}
	const int size5 = g_vecTopButtons.size();
	if(g_nCurSelectionType == 0)//ships
	{
		const int size6 = g_vecShipTypes.size();
		FOR1(size5)
		{
			g_vecTopButtons[ii]->active = ii < size6;
			g_vecTopButtons[ii]->visible = ii < size6;
		}
	}
	else//weapons
	{
		const int size6 = g_vecWeaponTypes.size() + 1;
		FOR1(size5)
		{
			g_vecTopButtons[ii]->active = ii < size6;
			g_vecTopButtons[ii]->visible = ii < size6;
		}
	}
}

//creates a ship given the load_out
void CreateShip(SLoadOut* load_out, int player)
{
	SShipDescriptor* ship_descript = &g_vecShipTypes[load_out->ship_type];
	SShip* ship = new SShip();
	InitObject(ship, g_obRootObject);

	ship->dead = false;

	vector2d pos = GetWorldMousePos();

	ship->polar_pos = g_obMotherShip[player]->polar_pos;
	ship->polar_vel.Set(0,0);
	ship->polar_accel = ship_descript->polar_accel;
	ship->polar_max_vel = ship_descript->polar_max_vel;

	//the player who owns this ship
	ship->player = player;

	ship->hitpoints = ship_descript->max_hitpoints;
	ship->max_hitpoints = ship_descript->max_hitpoints;

	ship->image = ship_descript->image;

	ship->bbox = ship_descript->bbox;
	
	ship->armour = ship_descript->armour;

	float max_range = 0.0f;//we need this for the sense range

	FOR1(ship_descript->num_weapons)
	{
		if(load_out->weapon_type[ii] > 0)
		{
			SWeaponDescriptor* weapon_descript = &g_vecWeaponTypes[load_out->weapon_type[ii] - 1];

			if(weapon_descript->range > max_range)
				max_range = weapon_descript->range;

			SWeaponSlot* slot = new SWeaponSlot();

			slot->rotation = ship_descript->slot_rotations[ii];
			slot->position = ship_descript->slot_positions[ii];

			SWeapon* weapon;
			if(weapon_descript->type == rocketweapon_type)
			{
				SRocketLauncher* rocket = CreateRocketLauncher(ship, slot);
				rocket->range = weapon_descript->range;
				rocket->reload_time = weapon_descript->reload_rate;
				rocket->image = weapon_descript->image;
				rocket->min_damage = weapon_descript->min_damage;
				rocket->max_damage = weapon_descript->max_damage;
				weapon = rocket;
			}
			else if(weapon_descript->type == plasmaweapon_type)
			{
				SPlasmaLauncher* plasma = CreatePlasmaLauncher(ship, slot);
				plasma->range = weapon_descript->range;
				plasma->reload_time = weapon_descript->reload_rate;
				plasma->image = weapon_descript->image;
				plasma->min_damage = weapon_descript->min_damage;
				plasma->max_damage = weapon_descript->max_damage;
				weapon = plasma;
			}
			else if(weapon_descript->type == ionbeamweapon_type)
			{
				SIonBeamCannon* beam = CreateIonBeamCannon(ship, slot);
				beam->range = weapon_descript->range;
				beam->reload_time = weapon_descript->reload_rate;
				beam->image = weapon_descript->image;
				beam->min_damage = weapon_descript->min_damage;
				beam->max_damage = weapon_descript->max_damage;
				weapon = beam;
			}
			ship->weapon_slots.push_back(slot);
		}
	}

	//range where we seek enemies to attack
	ship->sense_range = 1.5 * max_range;
	ship->selected = false;

	RegisterShip(ship);//stick the ship into the registry

	ship->flashy_timer = 0.0f;//used when we attack it

	//right, now we just add in all of the thrusters
	SThrusterParticleHolder* thruster;
	SBitmap* thruster_bmp = FindBitmap("part.tga");

	const int size1 = ship_descript->right_thrusters.size();
	FOR1(size1)
	{
		thruster = CreateThrusterParticleHolder(g_obRootObject, ship, thruster_bmp, 0xffff001f);
		thruster->throttle = 0;
		thruster->SetPosition(ship_descript->right_thrusters[ii].x, ship_descript->right_thrusters[ii].y);
		thruster->SetRotation(0);
		ship->right_thrusters.push_back(thruster);
	}

	const int size2 = ship_descript->top_thrusters.size();
	FOR1(size2)
	{
		thruster = CreateThrusterParticleHolder(g_obRootObject, ship, thruster_bmp, 0xffff001f);
		thruster->throttle = 0;
		thruster->SetPosition(ship_descript->top_thrusters[ii].x, ship_descript->top_thrusters[ii].y);
		thruster->SetRotation(-AL_PI / 2);
		ship->top_thrusters.push_back(thruster);
	}

	const int size3 = ship_descript->left_thrusters.size();
	FOR1(size3)
	{
		thruster = CreateThrusterParticleHolder(g_obRootObject, ship, thruster_bmp, 0xffff001f);
		thruster->throttle = 0;
		thruster->SetPosition(ship_descript->left_thrusters[ii].x, ship_descript->left_thrusters[ii].y);
		thruster->SetRotation(AL_PI);
		ship->left_thrusters.push_back(thruster);
	}

	const int size4 = ship_descript->bottom_thrusters.size();
	FOR1(size4)
	{
		thruster = CreateThrusterParticleHolder(g_obRootObject, ship, thruster_bmp, 0xffff001f);
		thruster->throttle = 0;
		thruster->SetPosition(ship_descript->bottom_thrusters[ii].x, ship_descript->bottom_thrusters[ii].y);
		thruster->SetRotation(AL_PI / 2);
		ship->bottom_thrusters.push_back(thruster);
	}
	//add flashers

	const int size5 = ship_descript->flasher_positions.size();
	FOR1(size5)
	{
		vector2d pos = ship_descript->flasher_positions[ii];
		CreateFlasher(ship, 0, 2.0f, 0xffffffff)->SetPosition(pos.x, pos.y);
	}

	//add ship to quad tree
	AddObjectToQuadNode(g_qnRootNode, ship);

	vector2d dest = ship->polar_pos + vector2d(100, 0.0f);

	//give it it's initial orders, stop a bit and then move
	
	SMoveOrder* order = new SMoveOrder(dest);
	order->ship = ship;
	ship->order_list.push_back(order);

	//vector2d cur_pos = ship->GetGlobalPosition();

	CreateExplosion(g_obMotherShip[player]->GetGlobalPosition(), 0.99f, 0xffff00ff);
}

//dun dun dunnn! the function that handles the ship building
//you can pass different arrays if you
void HandleShipBuilding(int player)
{
	if(g_obMotherShip[player] == 0)//can't build if there is no mothership
		return;
	//we can build something!
	if(g_lstBuildList[player].size() > 0)
	{
		SLoadOut* cur_loadout = &g_vecLoadOuts[player][g_lstBuildList[player].front()];
		int delta_money = int(100.0f * g_fDelta) + 1;

		if(delta_money > g_nPlayerCash[player])
			delta_money = g_nPlayerCash[player];

		g_nPlayerCash[player] -= delta_money;

		g_nCurMoneySpent[player] += delta_money;

		//get the cost of our loadout, it can change after all
		float total_cost = 0;

		total_cost += g_vecShipTypes[cur_loadout->ship_type].cost;

		FOR2(g_vecShipTypes[cur_loadout->ship_type].num_weapons)
		{
			int idx = cur_loadout->weapon_type[jj];
			if(idx > 0)
			{
				idx -= 1;
				total_cost += g_vecWeaponTypes[idx].cost;
			}
		}

		if(g_nCurMoneySpent[player] >= int(total_cost))//w00t, done
		{
			CreateShip(cur_loadout, player);

			cur_loadout->num_in_build_list--;
			//if(!
			g_lstBuildList[player].pop_front();

			g_nPlayerCash[player] += g_nCurMoneySpent[player] - (int)total_cost;
			g_nCurMoneySpent[player] = 0;

			if(player == g_nPlayer)
				SendMessage("Ship construction completed!");
		}
	}
}

int HandleBuildScreen()
{
	UpdateButtons();

	int ret = DoGUILogic(g_guiLeftPane, 0, 0);
	const int size1 = g_vecShipTypes[g_vecLoadOuts[g_nPlayer][g_nCurLoadOutSelection].ship_type].num_weapons + 1;
	FOR1(size1)
	{
		if(ret & GetMask(ii + 1))
		{
			g_nCurSelectionType = ii;
			PlayButtonSound();
			break;
		}
	}

	SLoadOut* load_out = &g_vecLoadOuts[g_nPlayer][g_nCurLoadOutSelection];

	ret = DoGUILogic(g_guiTopPane, 0, 0);
	if(g_nCurSelectionType == 0)//ships
	{
		const int size2 = g_vecShipTypes.size();
		FOR1(size2)
		{
			if(ret & GetMask(ii + 1))
			{
				load_out->ship_type = ii;
				PlayButtonSound();
				break;
			}
		}
	}
	else
	{
		const int size2 = g_vecWeaponTypes.size() + 1;
		FOR1(size2)
		{
			if(ret & GetMask(ii + 1))
			{
				load_out->weapon_type[g_nCurSelectionType - 1] = ii;
				PlayButtonSound();
				break;
			}
		}
	}

	ret = DoGUILogic(g_guiBottomPane, 0, 0);
	const int size2 = g_vecLoadOuts[g_nPlayer].size();
	FOR1(size2)
	{
		if(ret & GetMask(ii + 1))
		{
			g_nCurLoadOutSelection = ii;
			PlayButtonSound();
			break;
		}
	}

	ret = DoGUILogic(g_guiBuildPane, 0, 0);
	if(ret & GetMask(1))
	{
		PlayButtonSound();
		return 1;
	}
	else if(ret & GetMask(3))//build
	{
		PlayButtonSound();
		AddToBuildList(g_nCurLoadOutSelection, g_nPlayer);
	}
	else if(ret & GetMask(2))//cancel
	{
		PlayButtonSound();
		if(g_lstBuildList[g_nPlayer].size() > 0)
		{
			g_vecLoadOuts[g_nPlayer][g_lstBuildList[g_nPlayer].front()].num_in_build_list--;
			g_nPlayerCash[g_nPlayer] += g_nCurMoneySpent[g_nPlayer];//refund!
			g_nCurMoneySpent[g_nPlayer] = 0;
			g_lstBuildList[g_nPlayer].pop_front();
		}
	}
	
	int n = g_vecShipTypes[g_vecLoadOuts[g_nPlayer][g_nCurLoadOutSelection].ship_type].num_weapons;
	if(g_nCurSelectionType > n)
		g_nCurSelectionType = n;

	if(IsKeyDown(KEY_ESC))
	{
		PlayButtonSound();
		return 1;
	}

	return 0;
}

void DeInitBuildScreen()
{
	g_vecWeaponTypes.clear();
	g_vecShipTypes.clear();

	g_nCurLoadOutSelection = 0;
	g_nCurSelectionType = 0;

	g_vecLeftButtons.clear();
	g_vecTopButtons.clear();
	g_vecBottomButtons.clear();

	g_nCurMoneySpent[0] = 0;
	g_nCurMoneySpent[1] = 0;
	g_lstBuildList[0].clear();
	g_lstBuildList[1].clear();
	g_vecLoadOuts[0].clear();
	g_vecLoadOuts[1].clear();

	DestroyGUI(g_guiLeftPane);
	DestroyGUI(g_guiTopPane);
	DestroyGUI(g_guiBottomPane);
	DestroyGUI(g_guiBuildPane);
}

//I sincerely apologize for the mess that follows
void DrawBuildScreen()
{
	int midy = SCREEN_H / 2;
	DrawModeAlpha();

	const int size = 150;
	int position = 0;

	SLoadOut* load_out = &g_vecLoadOuts[g_nPlayer][g_nCurLoadOutSelection];

	if(g_nCurSelectionType == 0)//ships
	{
		position = load_out->ship_type;
	}
	else
	{
		position = load_out->weapon_type[g_nCurSelectionType - 1];
	}

	//draw current selections

	DrawFilledRect(size + position * size, 0, 2 * size + position * size, size, -10, 0xf880600c);

	DrawFilledRect(size + g_nCurLoadOutSelection * size, SCREEN_H - size, 2 * size + g_nCurLoadOutSelection * size, SCREEN_H, -10, 0xf880600c);

	DrawFilledRect(0, g_nCurSelectionType * 30, size, g_nCurSelectionType * 30 + 30, -10, 0xf880600c);

	DrawModeMask();
	//draw choices
	if(g_nCurSelectionType == 0)//ships
	{
		const int sz1 = g_vecShipTypes.size();

		FOR1(sz1)
		{
			DrawBitmap(g_vecShipTypes[ii].image,
			size +  size * ii + size / 2 - g_vecShipTypes[ii].image->w / 2,
			size / 2 - g_vecShipTypes[ii].image->h / 2, -10);
		}
	}
	else//weapons
	{
		const int sz1 = g_vecWeaponTypes.size();
		FOR1(sz1)
		{
			//note the offset below, the first position is a null position -> if we have no weapon
			DrawBitmap(g_vecWeaponTypes[ii].image,
				2 * size +  size * ii + size / 2 - g_vecWeaponTypes[ii].image->w / 2,
				size / 2 - g_vecWeaponTypes[ii].image->h / 2, -10);
		}

	}

	//draw selected thing
	if(g_nCurSelectionType == 0)//ships
	{
		SShipDescriptor* ship = &g_vecShipTypes[load_out->ship_type];
		DrawBitmap(ship->image,
					2 * size - ship->image->w / 2,
					midy - ship->image->h / 2, -10);

		char buf[512];
		DrawModeAlpha();

		sprintf(buf, "Cost: $%d", int(ship->cost));
		DrawTextAlign(buf, size * 3 + 10, SCREEN_H / 2 - size + 10, -1, -1, 0xffccffcc, g_fntFont);

		sprintf(buf, "Hitpoints: %d", int(ship->max_hitpoints));
		DrawTextAlign(buf, size * 3 + 10, SCREEN_H / 2 - size + 30, -1, -1, 0xffccffcc, g_fntFont);
		
		sprintf(buf, "Armour: %d", int(ship->armour));
		DrawTextAlign(buf, size * 3 + 10, SCREEN_H / 2 - size + 50, -1, -1, 0xffccffcc, g_fntFont);

		sprintf(buf, "Acceleration: %.1f / %.3f", ship->polar_accel.x, ship->polar_accel.y);
		DrawTextAlign(buf, size * 3 + 10, SCREEN_H / 2 - size + 70, -1, -1, 0xffccffcc, g_fntFont);

		sprintf(buf, "Max Speed: %.1f / %.3f", ship->polar_max_vel.x, ship->polar_max_vel.y);
		DrawTextAlign(buf, size * 3 + 10, SCREEN_H / 2 - size + 90, -1, -1, 0xffccffcc, g_fntFont);

		//litle variables for draw text lim
		int start = 0;
		int color = 0xffccffcc;
		int width = 0;
		int counter = 0;
		while(start != -1)
		{
			start = DrawTextLim(&ship->description, size * 3 + 10, SCREEN_H / 2 - size + 150 + counter * 20, start, SCREEN_W - size * 3 - 20,
				color, g_fntFont, &width, &color, false, true);
			counter++;
		}
		//DrawTextAlign(ship->description.c_str(), size * 3 + 10, size + 110, -1, -1, 0xffccffcc, g_fntFont);
	}
	else//weapons
	{
		if(load_out->weapon_type[g_nCurSelectionType - 1] > 0)
		{
			int idx = load_out->weapon_type[g_nCurSelectionType - 1] - 1;
			SWeaponDescriptor* weapon = &g_vecWeaponTypes[idx];
			DrawBitmap(weapon->image,
			2 * size - weapon->image->w / 2,
			midy - weapon->image->h / 2, -10);

			char buf[512];
			DrawModeAlpha();

			sprintf(buf, "Cost: $%d", int(weapon->cost));
			DrawTextAlign(buf, size * 3 + 10, SCREEN_H / 2 - size + 10, -1, -1, 0xffccffcc, g_fntFont);

			sprintf(buf, "Damage: %d-%d", (int)weapon->min_damage, (int)weapon->max_damage);
			DrawTextAlign(buf, size * 3 + 10, SCREEN_H / 2 - size + 30, -1, -1, 0xffccffcc, g_fntFont);

			sprintf(buf, "Range: %d", int(weapon->range));
			DrawTextAlign(buf, size * 3 + 10, SCREEN_H / 2 - size + 50, -1, -1, 0xffccffcc, g_fntFont);

			sprintf(buf, "Reload Time: %.2f", weapon->reload_rate);
			DrawTextAlign(buf, size * 3 + 10, SCREEN_H / 2 - size + 70, -1, -1, 0xffccffcc, g_fntFont);

			//litle variables for draw text lim
			int start = 0;
			int color = 0xffccffcc;
			int width = 0;
			int counter = 0;
			while(start != -1)
			{
				start = DrawTextLim(&weapon->description, size * 3 + 10, SCREEN_H / 2 - size + 130 + counter * 20, start, SCREEN_W - size * 3 - 20,
					color, g_fntFont, &width, &color, false, true);
				counter++;
			}
		}
	}

	DrawModeMask();

	//draw the load_outs
	const int sz1 = g_vecLoadOuts[g_nPlayer].size();

	FOR1(sz1)
	{
		int cost = 0;
		int sidx = g_vecLoadOuts[g_nPlayer][ii].ship_type;

		cost += (int)g_vecShipTypes[sidx].cost;

		DrawBitmap(g_vecShipTypes[sidx].image,
			size + size * ii + size / 2 - g_vecShipTypes[sidx].image->w / 2,
			SCREEN_H - size / 2 - g_vecShipTypes[sidx].image->h / 2, -10);
		FOR2(g_vecShipTypes[sidx].num_weapons)
		{
			int idx = g_vecLoadOuts[g_nPlayer][ii].weapon_type[jj];
			if(idx > 0)
			{
				idx -= 1;
				cost += (int)g_vecWeaponTypes[idx].cost;
				DrawBitmap(g_vecWeaponTypes[idx].image,
					g_vecShipTypes[sidx].slot_positions[jj].x + size + size * ii + size / 2 - g_vecWeaponTypes[idx].image->w / 2,
					SCREEN_H - g_vecShipTypes[sidx].slot_positions[jj].y - size / 2 - g_vecWeaponTypes[idx].image->h / 2, -10);
			}
		}
		char buf[512];
		DrawModeAlpha();
		sprintf(buf, "$%d", cost);
		DrawTextAlign(buf, size + size * ii + size / 2, SCREEN_H - size + 10, -1, 0, 0xffccffcc, g_fntFont);
		if(g_vecLoadOuts[g_nPlayer][ii].num_in_build_list > 0)
		{
			sprintf(buf, "x%d", g_vecLoadOuts[g_nPlayer][ii].num_in_build_list);
			DrawTextAlign(buf, 2 * size + size * ii - 15, SCREEN_H - size + 10, -1, 0, 0xffccffcc, g_fntFont);
		}

		DrawModeMask();
	}

	//draw the thing we are building right now

	if(g_lstBuildList[g_nPlayer].size() > 0)
	{
		float total_cost = 0;

		int sidx = g_vecLoadOuts[g_nPlayer][g_lstBuildList[g_nPlayer].front()].ship_type;

		total_cost += g_vecShipTypes[sidx].cost;

		DrawBitmap(g_vecShipTypes[sidx].image,
			size / 2 - g_vecShipTypes[sidx].image->w / 2,
			SCREEN_H - size - size / 2 - g_vecShipTypes[sidx].image->h / 2, -10);
		FOR2(g_vecShipTypes[sidx].num_weapons)
		{
			int idx = g_vecLoadOuts[g_nPlayer][g_lstBuildList[g_nPlayer].front()].weapon_type[jj];
			if(idx > 0)
			{
				idx -= 1;
				total_cost += g_vecWeaponTypes[idx].cost;
				DrawBitmap(g_vecWeaponTypes[idx].image,
					g_vecShipTypes[sidx].slot_positions[jj].x + size / 2 - g_vecWeaponTypes[idx].image->w / 2,
					SCREEN_H - g_vecShipTypes[sidx].slot_positions[jj].y - size - size / 2 - g_vecWeaponTypes[idx].image->h / 2, -10);
			}
		}
		char buf[512];
		DrawModeAlpha();
		sprintf(buf, "%d %s", int(float(g_nCurMoneySpent[g_nPlayer]) / float(total_cost) * 100.0f), "%%");
		DrawTextAlign(buf, size / 2, SCREEN_H - 2 * size + 10, -1, 0, 0xffccffcc, g_fntFont);
		DrawModeMask();
	}

	DrawModeAlpha();

	//draw how much moneys we have
	char buf[256];
	sprintf(buf, "Money: $%d", g_nPlayerCash[g_nPlayer]);
	DrawTextAlign(buf, size / 2, SCREEN_H - 2.5 * size, -1, 0, 0xffccffcc, g_fntFont);

	//draw the lines

	DrawLine(size, 0, size, SCREEN_H, -10, 0xfff0c030);
	DrawLine(size * 3, size, size * 3, SCREEN_H - size, -10, 0xfff0c030);
	DrawLine(size, size, SCREEN_W, size, -10, 0xfff0c030);
	DrawLine(size, midy - size, SCREEN_W, midy - size, -10, 0xfff0c030);
	DrawLine(size, midy + size, SCREEN_W, midy + size, -10, 0xfff0c030);
	DrawLine(size, SCREEN_H - size, SCREEN_W, SCREEN_H - size, -10, 0xfff0c030);

	DrawLine(0, SCREEN_H - 2 * size, size, SCREEN_H - 2 * size, -10, 0xfff0c030);
	DrawLine(0, SCREEN_H - size, size, SCREEN_H - size, -10, 0xfff0c030);

	DrawModeSolid();

	DrawGUI(g_guiLeftPane);
	DrawGUI(g_guiTopPane);
	DrawGUI(g_guiBottomPane);
	DrawGUI(g_guiBuildPane);
}
