// Galactic Empire - by AlZe - 08.05.09

bool show_fps = true, play_music = true, show_intro = true;

#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <sstream>
#include <fstream>
#include <cmath>
using namespace std;

#ifdef _DEBUG
#pragma comment(lib, "sfml-audio-d.lib")
#pragma comment(lib, "sfml-graphics-d.lib")
#pragma comment(lib, "sfml-main-d.lib")
#pragma comment(lib, "sfml-system-d.lib")
#pragma comment(lib, "sfml-window-d.lib")
#else
#pragma comment(lib, "sfml-audio.lib")
#pragma comment(lib, "sfml-graphics.lib")
#pragma comment(lib, "sfml-main.lib")
#pragma comment(lib, "sfml-system.lib")
#pragma comment(lib, "sfml-window.lib")
#endif

string game_version = "v1.08";

// Structs -----------------------------------------
struct s_fleet
{
	int target_system, target_planet, target_fleet, status, last_status, last_target_system, last_target_planet, last_target_fleet, owner;
	bool hovered, selected, built, shooting;
	float health, angle, target_angle;
	sf::Vector2f position, speed;
};

struct s_planet
{
	int owner, group, artifact, event_fade, fleets, satellite_angle, turret_target_planet, turret_target_system, satellite_health;
	bool hovered, selected, mine_hovered, auto_send, attacked, has_event, has_mine, has_satellite, has_turret, has_moon, has_disruption, turret_hovered, turret_selected;
	float size, population, max_population, turret_angle, rotation, moon_angle, income, expenses, metal_income, event_radius;
	string name;
	sf::Clock event_clock;
	sf::Vector2f position, position_mine, position_turret, position_satellite;
};

struct s_rock
{
	int number, rotation_direction, rotation, owner;
	bool has_mine, hovered, selected;
	float size;
	sf::Vector2f position;
};

struct s_starsystem
{
	int planets, rocks;
	bool nearby[6];
	s_planet planet[51];
	s_rock rock[101];
};

struct s_research
{
	int time, costs;
	bool researched;
};

struct s_player
{
	int population, planets_conquered, planets_lost, fleets_built, artifacts_discovered, artifact_discovered[6], alignment[5];
	int researching, research_progress, planet_count, researches_done;
	s_research research[20];
	float credits, metal, fleet_speed, fleet_health, fleet_turnrate, fleet_damage, fleet_range;
};

struct s_mission
{
	int starsystems, players, planets, artifacts, difficulty;
};

struct s_explosion
{
	int active, frame;
	sf::Vector2f position;
};

struct s_button
{
	int code, number, hover;
	bool active;
	sf::IntRect IntRect;
};

// Declarations ------------------------------
int artifact_owner[6], planet_selected, research_selected, icon_hover, zoom_counter;
int not_hovered, showPlanetNames, planet_selected_count, system_selected, VideoModeSelected, reset_auto_timer, mouse_still_pressed;
int code, ai_delay, game_speed, fullscreen, oldscreen, total_planets, menuStatus, gameStatus, alpha, v_sync;
int message_code, active_cursor, screen_w, screen_h, disruption_timer, message_alpha, framerate, framecount;
int screen_wh, screen_hh, group, disruption_status, disruption_countdown, old_alignment, zoomCode;
float zoom, total_income, total_expenses, total_metal_income, music_volume;
sf::Clock clock_mouse, clock_button, clock_animation, clock_keyboard, clock_message, clock_fps, clock_zoom, clock_rotation, clock_event, clock_auto;
sf::Clock clock_explosion, clock_yetanother, clock_population, clock_turret, clock_message_alpha, clock_fleet;
sf::Unicode::Text planet_name[129];
int exitGame, endGame, pauseGame, showResearch, showDiplomacy;
int VideoModesCount;
std::stringstream sstr;
const float PI = 3.14159f;

#define CREDITS_FLEET	200
#define METAL_FLEET		75
#define CREDITS_SATELLITE	500
#define METAL_SATELLITE		150
#define CREDITS_TURRET		800
#define METAL_TURRET		300
#define CREDITS_MINE		500
#define METAL_MINE			 50

#define CURSOR_DEFAULT		0
#define CURSOR_SCROLL		1
#define CURSOR_DISRUPTION	2
#define CURSOR_TURRET		3
#define CURSOR_MINE			4
#define CURSOR_ATTACKRED	5
#define CURSOR_ATTACKGREEN	6

#define FLEET_ORBITING		0
#define FLEET_TRAVELLING		1
#define FLEET_DOGFIGHT		2

sf::WindowSettings Settings(24, 8, 2);
sf::VideoMode VideoMode[50];
sf::RenderWindow App;
sf::Font font[6];
sf::String myString[6];
sf::Image image[36];
sf::Sprite graphic[36];
sf::Music music[2];
sf::SoundBuffer soundbuffer[14];
sf::Sound sound[14];
sf::FloatRect viewrect;
sf::View View, ViewDefault;
sf::IntRect subrect;
sf::Event Event;
sf::Vector2f mouse2, box2;
sf::Vector2i mouse, mouse_old, box;
sf::Vector2f event_position, zoomMove;

s_starsystem starsystem[6];
s_player player[5];
s_mission mission[10];
s_explosion explosion[51];
s_button button[21];
s_fleet fleet[200];
s_planet * p1;

// Functions ------------------------------------------
int main();
//void Console();

void Buttons_Reset();
void Buttons_Init();
void Buttons_Draw();
void Buttons_Check();

void Input_Keyboard();
void Input_Mouse();

void Draw_Main();
void Draw_Map();
void Draw_Interface();
void Draw_Menu();
void Draw_Box(int x, int y, int width, int height, int red, int green, int blue);
void Draw_String(short fontnumber, int x, int y, int size, sf::Unicode::Text string, sf::Uint8 red, sf::Uint8 green, sf::Uint8 blue, sf::Uint8 alpha, int alignment);

void Loop_Ingame();
void Loop_Menu();

void Game_Load();
void Game_Init();
void Game_Intro();
void Game_Update();
void Game_AI();

void Reset_Planet(int system, int planet);
void Add_Explosion(sf::Vector2f position);
void Add_Message(int number);
void Send_Fleet(int home_system, int home_planet, int target_system, int target_planet, int play_sound);
void Add_Fleet(int system, int planet);

// Start is here
int main()
{
	//Console();
	Game_Load();
	Buttons_Init();
	if (show_intro)
		Game_Intro();

	gameStatus = 1;
	while (1)
	{
		if (gameStatus == 0)
			break;
		else if (gameStatus == 1)
			Loop_Menu();
		else if (gameStatus == 2)
			Loop_Ingame();
	}

}

//void Console()
//{
//	FILE *f;
//
//	AllocConsole();
//
//	freopen_s(&f, "CONIN$", "r", stdin);
//	freopen_s(&f, "CONOUT$", "w", stdout);
//	freopen_s(&f, "CONOUT$", "w", stderr);
//}

void Game_Load()
{
	sf::VideoMode desktopMode;

	// Load config
	ifstream inFile("data/config.dat");
	if (inFile.is_open())
	{
		inFile >> screen_w;
		inFile >> screen_h;
		inFile >> fullscreen;
		inFile >> v_sync;
		inFile >> music_volume;
	}
	else
	{
		ofstream outFile("data/config.dat");
		if (outFile.is_open())
		{
			desktopMode = sf::VideoMode::GetDesktopMode();
			screen_w = desktopMode.Width;
			screen_h = desktopMode.Height;
			fullscreen = 1;
			v_sync = 0;
			music_volume = 100;
			outFile << screen_w << endl;
			outFile << screen_h << endl;
			outFile << fullscreen << endl;
			outFile << v_sync << endl;
			outFile << music_volume << endl;
			outFile.close();
		}
	}
	inFile.close();

	// Set helper variables with half screen sizes
	screen_wh = screen_w/2 - 1;
	screen_hh = screen_h/2 - 1;

	// Get available screen resolutions
	VideoModesCount = sf::VideoMode::GetModesCount();

	for (int i = 0; i < VideoModesCount; ++i)
	{
		VideoMode[i] = sf::VideoMode::GetMode(i);
		if (VideoMode[i].Width == screen_w && VideoMode[i].Height == screen_h && VideoMode[i].BitsPerPixel == 32)
			VideoModeSelected = i;
		if (VideoMode[i].Width == 1024 && VideoMode[i].Height == 768)
		{
			VideoModesCount = i + 1;
			break;
		}
	}

	// Initialize screen
	oldscreen = fullscreen;

	App.Create(VideoMode[VideoModeSelected], "Galactic Empire", sf::Style::Close | (fullscreen ? sf::Style::Fullscreen : 0), Settings);
	App.ShowMouseCursor(false);
	App.SetCursorPosition(screen_wh, screen_hh);
	App.UseVerticalSync(v_sync ? 1 : 0);

	View.SetFromRect(sf::FloatRect(0, 0, (float)screen_w, (float)screen_h));
	ViewDefault.SetFromRect(sf::FloatRect(0, 0, (float)screen_w, (float)screen_h));

	// Load graphics
	image[0].LoadFromFile("gfx/planets.png"); image[0].SetSmooth(true); graphic[0] = sf::Sprite(image[0]);
	image[1].LoadFromFile("gfx/satellite.png"); image[1].SetSmooth(true); graphic[1] = sf::Sprite(image[1]);
	image[2].LoadFromFile("gfx/research.png"); image[2].SetSmooth(false); graphic[2] = sf::Sprite(image[2]);
	image[3].LoadFromFile("gfx/satellite_icon.png"); image[3].SetSmooth(false); graphic[3] = sf::Sprite(image[3]);
	image[4].LoadFromFile("gfx/research_window.png"); image[4].SetSmooth(false); graphic[4] = sf::Sprite(image[4]);
	if (sf::Randomizer::Random(1, 2) == 0)
	{
		image[5].LoadFromFile("gfx/background.png"); image[5].SetSmooth(true); graphic[5] = sf::Sprite(image[5]);
	}
	else
	{
		image[5].LoadFromFile("gfx/background2.png"); image[5].SetSmooth(true); graphic[5] = sf::Sprite(image[5]);
	}
	image[6].LoadFromFile("gfx/ships.png"); image[6].SetSmooth(true); graphic[6] = sf::Sprite(image[6]);
	image[7].LoadFromFile("gfx/selection.png"); image[7].SetSmooth(false); graphic[7] = sf::Sprite(image[7]);
	image[8].LoadFromFile("gfx/turret_icon.png"); image[8].SetSmooth(false); graphic[8] = sf::Sprite(image[8]);
	image[9].LoadFromFile("gfx/turret.png"); image[9].SetSmooth(true); graphic[9] = sf::Sprite(image[9]);
	image[10].LoadFromFile("gfx/cursor.png"); image[10].SetSmooth(false); graphic[10] = sf::Sprite(image[10]);
	image[11].LoadFromFile("gfx/star.png"); image[11].SetSmooth(false); graphic[11] = sf::Sprite(image[11]);
	image[12].LoadFromFile("gfx/logo.png"); image[12].SetSmooth(true); graphic[12] = sf::Sprite(image[12]);
	image[13].LoadFromFile("gfx/mainmenu.png"); image[13].SetSmooth(true); graphic[13] = sf::Sprite(image[13]);
	image[14].LoadFromFile("gfx/buttons.png"); image[14].SetSmooth(false); graphic[14] = sf::Sprite(image[14]);
	image[15].LoadFromFile("gfx/stats.png"); image[15].SetSmooth(true); graphic[15] = sf::Sprite(image[15]);
	image[16].LoadFromFile("gfx/sun.png"); image[16].SetSmooth(true); graphic[16] = sf::Sprite(image[16]);
	image[17].LoadFromFile("gfx/disruption.png"); image[17].SetSmooth(false); graphic[17] = sf::Sprite(image[17]);
	image[18].LoadFromFile("gfx/title.png"); image[18].SetSmooth(true); graphic[18] = sf::Sprite(image[18]);
	image[19].LoadFromFile("gfx/research_buttons.png"); image[19].SetSmooth(false); graphic[19] = sf::Sprite(image[19]);
	image[20].LoadFromFile("gfx/ui_planets.png"); image[20].SetSmooth(false); graphic[20] = sf::Sprite(image[20]);
	image[21].LoadFromFile("gfx/ui_credits.png"); image[21].SetSmooth(false); graphic[21] = sf::Sprite(image[21]);
	image[22].LoadFromFile("gfx/ui_planetinfo.png"); image[22].SetSmooth(false); graphic[22] = sf::Sprite(image[22]);
	image[23].LoadFromFile("gfx/ui_quit.png"); image[23].SetSmooth(false); graphic[23] = sf::Sprite(image[23]);
	image[24].LoadFromFile("gfx/ui_menu.png"); image[24].SetSmooth(false); graphic[24] = sf::Sprite(image[24]);
	image[25].LoadFromFile("gfx/diplomacy.png"); image[25].SetSmooth(false); graphic[25] = sf::Sprite(image[25]);
	image[26].LoadFromFile("gfx/diplomacy_window.png"); image[26].SetSmooth(false); graphic[26] = sf::Sprite(image[26]);
	image[27].LoadFromFile("gfx/portraits.png"); image[27].SetSmooth(false); graphic[27] = sf::Sprite(image[27]);
	image[28].LoadFromFile("gfx/circles.png"); image[28].SetSmooth(true); graphic[28] = sf::Sprite(image[28]);
	image[29].LoadFromFile("gfx/explosion.png"); image[29].SetSmooth(true); graphic[29] = sf::Sprite(image[29]);
	image[30].LoadFromFile("gfx/moon.png"); image[30].SetSmooth(true); graphic[30] = sf::Sprite(image[30]);
	image[31].LoadFromFile("gfx/rocks.png"); image[31].SetSmooth(true); graphic[31] = sf::Sprite(image[31]);
	image[32].LoadFromFile("gfx/fleet_icon.png"); image[32].SetSmooth(false); graphic[32] = sf::Sprite(image[32]);
	image[33].LoadFromFile("gfx/mine_icon.png"); image[33].SetSmooth(false); graphic[33] = sf::Sprite(image[33]);
	image[34].LoadFromFile("gfx/mine.png"); image[34].SetSmooth(true); graphic[34] = sf::Sprite(image[34]);
	image[35].LoadFromFile("gfx/ui_iconinfo.png"); image[35].SetSmooth(false); graphic[35] = sf::Sprite(image[35]);

	// Position, scale, center images
	// Mine
	graphic[34].SetCenter(graphic[34].GetSize().x/2,graphic[34].GetSize().y/2);
	graphic[34].SetScale(.25f, .25f);

	// Diplomacy window
	graphic[26].SetCenter((float)graphic[26].GetSize().x/2,(float)graphic[26].GetSize().y/2);
	graphic[26].SetPosition((float)screen_wh,(float)screen_hh);

	// Diplomacy button
	graphic[25].SetCenter((float)graphic[25].GetSize().x/2,(float)graphic[25].GetSize().y);
	graphic[25].SetPosition((float)screen_wh,(float)screen_h+7);

	// UI Menu
	graphic[24].SetCenter((float)graphic[24].GetSize().x/2,(float)graphic[24].GetSize().y/2);
	graphic[24].SetPosition((float)screen_wh,(float)screen_hh);

	// UI Quit
	graphic[23].SetCenter((float)graphic[23].GetSize().x/2,(float)graphic[23].GetSize().y/2);
	graphic[23].SetPosition((float)screen_wh,(float)screen_hh);

	// Satellite
	graphic[1].SetCenter((float)graphic[1].GetSize().x/2,(float)graphic[1].GetSize().y/2);
	graphic[1].SetScale(0.25f, 0.25f);

	// Moon
	graphic[30].SetCenter((float)graphic[30].GetSize().x/2,(float)graphic[30].GetSize().y/2);
	graphic[30].SetScale(0.15f, 0.15f);

	// Research button
	graphic[2].SetCenter((float)graphic[2].GetSize().x/2,0.0f);
	graphic[2].SetPosition((float)screen_wh,(float)-8);

	// Research window
	graphic[4].SetCenter((float)graphic[4].GetSize().x/2,(float)graphic[4].GetSize().y/2);
	graphic[4].SetPosition((float)screen_wh,(float)screen_hh);

	// Turret
	graphic[9].SetCenter((float)graphic[9].GetSize().x/2,(float)graphic[9].GetSize().y/2);
	graphic[9].SetScale(0.5f, 0.5f);

	// Star
	graphic[11].SetCenter((float)graphic[11].GetSize().x/2,(float)graphic[11].GetSize().y/2);
	graphic[11].SetScale(0.5f, 0.5f);

	// Logo
	graphic[12].SetCenter(graphic[12].GetSize().x/2,graphic[12].GetSize().y/2);
	graphic[12].SetPosition((float)screen_wh, (float)screen_hh);
	graphic[12].SetScale(.75f, .75f);

	// Main menu background
	graphic[13].SetCenter(graphic[13].GetSize().x/2,graphic[13].GetSize().y/2);
	graphic[13].SetPosition((float)screen_wh,(float)screen_hh);
	graphic[13].SetScale((float)screen_w/graphic[13].GetSize().x,(float)screen_h/graphic[13].GetSize().y);

	// Sun
	graphic[16].SetCenter((float)graphic[16].GetSize().x/2,(float)graphic[16].GetSize().y/2);
	graphic[16].SetPosition(0.0f,0.0f);
	graphic[16].SetScale(0.5f, 0.5f);

	// Phase Lane Disruption icon
	graphic[17].SetCenter(0.0f,(float)graphic[17].GetSize().y/2);
	graphic[17].SetPosition(0.0f,(float)screen_hh);

	// Title
	graphic[18].SetCenter((float)graphic[18].GetSize().x/2,0.0f);
	graphic[18].SetPosition((float)screen_wh,0.0f);

	// Game background
	graphic[5].Scale((float)screen_w/graphic[5].GetSize().x,(float)screen_h/graphic[5].GetSize().y);

	// Victory background
	graphic[15].SetScale((float)screen_w/graphic[15].GetSize().x,(float)screen_h/graphic[15].GetSize().y);

	// Load music
	if (play_music)
	{
		if (sf::Randomizer::Random(0, 1) == 0)
			music[0].OpenFromFile("sfx/ethereal_void.ogg");
		else
			music[0].OpenFromFile("sfx/third_awakening.ogg");
		music[1].OpenFromFile("sfx/mainmusic.ogg");
	}

	// Load sounds
	soundbuffer[0].LoadFromFile("sfx/click.wav");
	soundbuffer[1].LoadFromFile("sfx/takeover.wav");
	soundbuffer[2].LoadFromFile("sfx/impact.wav");
	soundbuffer[3].LoadFromFile("sfx/yes.wav");
	soundbuffer[4].LoadFromFile("sfx/victory.ogg");
	soundbuffer[5].LoadFromFile("sfx/whoosh.wav");
	soundbuffer[6].LoadFromFile("sfx/button.wav");
	soundbuffer[7].LoadFromFile("sfx/defeat.wav");
	soundbuffer[8].LoadFromFile("sfx/laser.wav");
	soundbuffer[9].LoadFromFile("sfx/explosion.wav");
	soundbuffer[10].LoadFromFile("sfx/research_finished.wav");
	soundbuffer[11].LoadFromFile("sfx/artifact_discovered.wav");
	soundbuffer[12].LoadFromFile("sfx/icon_click.wav");
	soundbuffer[13].LoadFromFile("sfx/notice.wav");

	for (int i = 0; i < 13; i++)
		sound[i].SetBuffer(soundbuffer[i]);

	// Load font
	font[0].LoadFromFile("arial.ttf", 15);
	myString[0] = sf::String("empty", font[0], 15);

	font[1].LoadFromFile("arial.ttf", 20);
	myString[1] = sf::String("empty", font[1], 20);

	font[2].LoadFromFile("arial.ttf", 25);
	myString[2] = sf::String("empty", font[2], 25);

	font[3].LoadFromFile("arial.ttf", 35);
	myString[3] = sf::String("empty", font[3], 35);

	font[4].LoadFromFile("arial.ttf", 100);
	myString[4] = sf::String("empty", font[4], 100);

	// Set default values for skirmish game
	mission[0].starsystems = 3;
	mission[0].planets = 25;
	mission[0].players = 3;
	mission[0].difficulty = 2;
	mission[0].artifacts = 1;

	planet_name[1] = "Abante";
	planet_name[2] = "Actor";
	planet_name[3] = "Borges";
	planet_name[4] = "Bootes";
	planet_name[5] = "Caia";
	planet_name[6] = "Cuyo";
	planet_name[7] = "Datura";
	planet_name[8] = "Dedeyn";
	planet_name[9] = "Elatus";
	planet_name[10] = "Eneev";
	planet_name[11] = "Fermat";
	planet_name[12] = "Funke";
	planet_name[13] = "Godel";
	planet_name[14] = "Guisan";
	planet_name[15] = "Haltia";
	planet_name[16] = "Hitomi";
	planet_name[17] = "Immo";
	planet_name[18] = "Isobe";
	planet_name[19] = "Jetta";
	planet_name[20] = "Juewa";
	planet_name[21] = "Kalchas";
	planet_name[22] = "Kavasch";
	planet_name[23] = "Lassell";
	planet_name[24] = "Lavrov";
	planet_name[25] = "Mamiya";
	planet_name[26] = "Malzovia";
	planet_name[27] = "Natrees";
	planet_name[28] = "Nireus";
	planet_name[29] = "Oishi";
	planet_name[30] = "Olmos";
	planet_name[31] = "Parsa";
	planet_name[32] = "Praxedis";
	planet_name[33] = "Quadea";
	planet_name[34] = "Quine";
	planet_name[35] = "Radebeul";
	planet_name[36] = "Rozhen";
	planet_name[37] = "Stuwe";
	planet_name[38] = "Sumava";
	planet_name[39] = "Twarog";
	planet_name[40] = "Tynka";
	planet_name[41] = "Uemura";
	planet_name[42] = "Upice";
	planet_name[43] = "Viget";
	planet_name[44] = "Voss";
	planet_name[45] = "Waland";
	planet_name[46] = "Warell";
	planet_name[47] = "Xanthe";
	planet_name[48] = "Yukar";
	planet_name[49] = "Yurlov";
	planet_name[50] = "Zwaan";
	planet_name[51] = "Abashiri";
	planet_name[52] = "Ables";
	planet_name[53] = "Adeona";
	planet_name[54] = "Bathseba";
	planet_name[55] = "Beadell";
	planet_name[56] = "Braes";
	planet_name[57] = "Cevennes";
	planet_name[58] = "Chryseis";
	planet_name[59] = "Cyane";
	planet_name[60] = "Dolon";
	planet_name[61] = "Durrell";
	planet_name[62] = "Drymas";
	planet_name[63] = "Ehime";
	planet_name[64] = "Eos";
	planet_name[65] = "Euterpe";
	planet_name[66] = "Frobel";
	planet_name[67] = "Furuyama";
	planet_name[68] = "Ferri";
	planet_name[69] = "Gunila";
	planet_name[70] = "Goodhue";
	planet_name[71] = "Gosal";
	planet_name[72] = "Hynek";
	planet_name[73] = "Horne";
	planet_name[74] = "Hoag";
	planet_name[75] = "Iclea";
	planet_name[76] = "Iliya";
	planet_name[77] = "Ino";
	planet_name[78] = "Jagras";
	planet_name[79] = "Jilin";
	planet_name[80] = "Juza";
	planet_name[81] = "Kagawa";
	planet_name[82] = "Klonios";
	planet_name[83] = "Kyte";
	planet_name[84] = "Leroy";
	planet_name[85] = "Liddell";
	planet_name[86] = "Losaka";
	planet_name[87] = "Mak";
	planet_name[88] = "Meges";
	planet_name[89] = "Miho";
	planet_name[90] = "Nezarka";
	planet_name[91] = "Noreen";
	planet_name[92] = "Nysa";
	planet_name[93] = "Ogmios";
	planet_name[94] = "Okyudo";
	planet_name[95] = "Oozora";
	planet_name[96] = "Peitho";
	planet_name[97] = "Phaeo";
	planet_name[98] = "Phocaea";
	planet_name[99] = "Quantz";
	planet_name[100] = "Quadea";
	planet_name[101] = "Quero";
	planet_name[102] = "Raissa";
	planet_name[103] = "Riceia";
	planet_name[104] = "Rugen";
	planet_name[105] = "Satoru";
	planet_name[106] = "Scoon";
	planet_name[107] = "Segre";
	planet_name[108] = "Tauris";
	planet_name[109] = "Teta";
	planet_name[110] = "Thisbe";
	planet_name[111] = "Ursa";
	planet_name[112] = "Ukko";
	planet_name[113] = "Utopia";
	planet_name[114] = "Vaino";
	planet_name[115] = "Veratar";
	planet_name[116] = "Vitek";
	planet_name[117] = "Weigan";
	planet_name[118] = "Wiles";
	planet_name[119] = "Wysner";
	planet_name[120] = "Xerxes";
	planet_name[121] = "Xanthus";
	planet_name[122] = "Xint";
	planet_name[123] = "Yalta";
	planet_name[124] = "Yebes";
	planet_name[125] = "Yoichi";
	planet_name[126] = "Zeglin";
	planet_name[127] = "Zenon";
	planet_name[0] = "Zwin";
}

void Game_Intro()
{

	bool go_back = false, running = true;
	int fade = 0;
	sf::Clock clock_intro;

	sound[5].Play();

	while (running)
	{
		App.GetEvent(Event);

		if (((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape)) || (Event.Type == sf::Event::MouseButtonPressed))
		{
			sound[5].Stop();
			running = false;
		}

		// Increase/decrease transparency of sprite
		if (clock_intro.GetElapsedTime() > .015)
		{
			clock_intro.Reset();
			if (!go_back)
				fade += 2;
			else
				fade -= 2;
			if (fade == 254)
				go_back = true;
			if (fade == 0)
				running = false;

			graphic[12].SetColor(sf::Color(255,255,255,fade));
			graphic[12].Scale(1.001f, 1.001f);
		}

		// Scale and draw sprite
		App.Clear(sf::Color::White);
		App.Draw(graphic[12]);
		App.Display();
	}

}

void Buttons_Init()
{

	// Campaign
	button[0].IntRect = sf::IntRect(screen_wh - 95, screen_hh - 130, screen_wh - 95 + 191, screen_hh - 130 + 46);
	button[0].code = 1;
	button[0].number = 0;
	// Skirmish
	button[1].IntRect = sf::IntRect(screen_wh - 95, screen_hh - 80, screen_wh - 95 + 191, screen_hh - 80 + 46);
	button[1].code = 2;
	button[1].number = 1;
	// Quit
	button[2].IntRect = sf::IntRect(screen_wh - 95, screen_hh + 70, screen_wh - 95 + 191, screen_hh + 70 + 46);
	button[2].code = 3;
	button[2].number = 2;
	// Yes
	button[3].IntRect = sf::IntRect(screen_wh + 10, screen_hh + 10, screen_wh + 10 + 191, screen_hh + 10 + 46);
	button[3].code = 4;
	button[3].number = 3;
	// No
	button[4].IntRect = sf::IntRect(screen_wh - 201, screen_hh + 10, screen_wh - 201 + 191, screen_hh + 10 + 46);
	button[4].code = 5;
	button[4].number = 4;
	// Stats OK
	button[5].IntRect = sf::IntRect(screen_wh - 95, screen_hh + 15, screen_wh - 95 + 191, screen_hh + 15 + 46);
	button[5].code = 6;
	button[5].number = 5;
	// Skirmish OK
	button[6].IntRect = sf::IntRect(screen_wh + 45, screen_hh + 100, screen_wh + 45 + 191, screen_hh + 100 + 46);
	button[6].code = 7;
	button[6].number = 5;
	// Controls
	button[7].IntRect = sf::IntRect(screen_wh - 95, screen_hh + 20, screen_wh - 95 + 191, screen_hh + 20 + 46);
	button[7].code = 8;
	button[7].number = 6;
	// Settings
	button[8].IntRect = sf::IntRect(screen_wh - 95, screen_hh - 30, screen_wh - 95 + 191, screen_hh - 30 + 46);
	button[8].code = 9;
	button[8].number = 7;
	// Story OK
	button[9].IntRect = sf::IntRect(screen_wh - 95, screen_hh + 100, screen_wh - 95 + 191, screen_hh + 100 + 46);
	button[9].code = 10;
	button[9].number = 5;
	// Campaign OK
	//button[10].IntRect = sf::IntRect(screen_wh - 95, screen_hh + 20, screen_wh - 95 + 191, screen_hh + 20 + 46);
	button[10].code = 11;
	button[10].number = 5;
	// Campaign cancel
	//button[11].IntRect = sf::IntRect(screen_wh - 95, screen_hh + 20, screen_wh - 95 + 191, screen_hh + 20 + 46);
	button[11].code = 12;
	button[11].number = 8;
	// Skirmish cancel
	button[12].IntRect = sf::IntRect(screen_wh - 236, screen_hh + 100, screen_wh - 236 + 191, screen_hh + 100 + 46);
	button[12].code = 13;
	button[12].number = 8;
	// Settings cancel
	button[13].IntRect = sf::IntRect(screen_wh - 236, screen_hh + 100, screen_wh - 236 + 191, screen_hh + 100 + 46);
	button[13].code = 14;
	button[13].number = 8;
	//With Button(14) ' Settings ok
	button[14].IntRect = sf::IntRect(screen_wh + 45, screen_hh + 100, screen_wh + 45 + 191, screen_hh + 100 + 46);
	button[14].code = 15;
	button[14].number = 5;
	// Research ok
	button[15].IntRect = sf::IntRect(screen_wh + 50, screen_hh + 130, screen_wh + 50 + 191, screen_hh + 130 + 46);
	button[15].code = 16;
	button[15].number = 5;
	// Research research
	button[16].IntRect = sf::IntRect(screen_wh + 50, screen_hh, screen_wh + 50 + 191, screen_hh + 46);
	button[16].code = 17;
	button[16].number = 9;
	//With Button(17) ' Diplomacy ok
	button[17].IntRect = sf::IntRect(screen_wh - 95, screen_hh + 170, screen_wh - 95 + 191, screen_hh + 170 + 46);
	button[17].code = 18;
	button[17].number = 5;
	// Send Gift to player 2
	button[18].IntRect = sf::IntRect(screen_wh - 294, screen_hh + 66, screen_wh - 294 + 191, screen_hh + 66 + 46);
	button[18].code = 19;
	button[18].number = 10;
	// Send Gift to player 3
	button[19].IntRect = sf::IntRect(screen_wh - 99, screen_hh + 66, screen_wh - 99 + 191, screen_hh + 66 + 46);
	button[19].code = 20;
	button[19].number = 10;
	// Send Gift to player 4
	button[20].IntRect = sf::IntRect(screen_wh + 96, screen_hh + 66, screen_wh + 96 + 191, screen_hh + 66 + 46);
	button[20].code = 21;
	button[20].number = 10;

}

void Buttons_Reset()
{

	for (int i = 0; i < 21; i++)
		button[i].active = false;

}

void Buttons_Draw()
{

	for (int i = 0; i < 21; i++)
	{
		if (button[i].active)
		{
			graphic[14].SetSubRect(sf::IntRect(191 * button[i].hover, 46 * button[i].number, 190 + 191 * button[i].hover, 46 * button[i].number + 45));
			graphic[14].SetPosition((float)button[i].IntRect.Left, (float)button[i].IntRect.Top);
			App.Draw(graphic[14]);
		}
	}

}

void Loop_Menu()
{
	int changevalue;

	App.SetView(ViewDefault);

	if (play_music)
	{
		music[1].Play();
		music[1].SetLoop(true);
		music[1].SetVolume(music_volume);
	}

	while (gameStatus == 1)
	{
		changevalue = 0;

		// Get events
		const sf::Input& Input = App.GetInput();

		App.GetEvent(Event);
		if (Event.Type == sf::Event::Closed)
		{
			gameStatus = 0;
			break;
		}

		// Get mouse status
		mouse.x = Input.GetMouseX();
		mouse.y = Input.GetMouseY();

		Buttons_Reset();

		// Set active buttons
		if (menuStatus == 0) // Blank main menu
		{
			//button[0].active = 1 // Campaign
			button[1].active = 1; // Skirmish
			button[2].active = 1; // Quit
			button[7].active = 1; // Controls
			button[8].active = 1; // Settings
		}
		//else if (menuStatus == 1) // Campaign menu
		//{
		//	button[10].active = 1; // Campaign ok
		//	button[11].active = 1; // Campaign cancel
		//}
		else if (menuStatus == 2) // Skirmish menu
		{
			button[6].active = 1; // Skirmish ok
			button[12].active = 1; // Skirmish cancel
		}
		else if (menuStatus == 3) // Controls menu
		{
			button[9].active = 1; // Controls ok
		}
		else if (menuStatus == 4) // Settings menu
		{
			button[14].active = 1; // Settings ok
			button[13].active = 1; // Settings cancel
		}

		// Skirmish dialog
		if (menuStatus == 2)
		{

			// Check setting box (galaxies)
			if (mouse.x > screen_wh-100 && mouse.x < screen_wh+125 && mouse.y > screen_hh-67 && mouse.y < screen_hh-47)
			{
				if (Input.IsMouseButtonDown(sf::Mouse::Left))
					changevalue = 1;
				if (Input.IsMouseButtonDown(sf::Mouse::Right))
					changevalue = -1;
				if (clock_mouse.GetElapsedTime() > .1)
				{
					clock_mouse.Reset();
					if (mission[0].starsystems + changevalue <= 5 && mission[0].starsystems + changevalue >= 1)
						mission[0].starsystems += changevalue;
				}
			}

			// Check setting box (planets)
			if (mouse.x > screen_wh-100 && mouse.x < screen_wh+125 && mouse.y > screen_hh-42 && mouse.y < screen_hh-22)
			{
				if (Input.IsMouseButtonDown(sf::Mouse::Left))
					changevalue = 5;
				if (Input.IsMouseButtonDown(sf::Mouse::Right))
					changevalue = -5;
				if (clock_mouse.GetElapsedTime() > .1)
				{
					clock_mouse.Reset();
					if (mission[0].planets + changevalue <= 35 && mission[0].planets + changevalue >= 15)
						mission[0].planets += changevalue;
				}
			}

			// Check setting box (enemies)
			if (mouse.x > screen_wh-100 && mouse.x < screen_wh+125 && mouse.y > screen_hh-17 && mouse.y < screen_hh+3)
			{
				if (Input.IsMouseButtonDown(sf::Mouse::Left))
					changevalue = 1;
				if (Input.IsMouseButtonDown(sf::Mouse::Right))
					changevalue = -1;
				if (clock_mouse.GetElapsedTime() > .1)
				{
					clock_mouse.Reset();
					if (mission[0].players + changevalue < 5 && mission[0].players + changevalue > 1)
						mission[0].players += changevalue;
				}
			}

			// Check setting box (difficulty)
			if (mouse.x > screen_wh-100 && mouse.x < screen_wh+125 && mouse.y > screen_hh+8 && mouse.y < screen_hh+28)
			{
				if (Input.IsMouseButtonDown(sf::Mouse::Left))
					changevalue = 1;
				if (Input.IsMouseButtonDown(sf::Mouse::Right))
					changevalue = -1;
				if (clock_mouse.GetElapsedTime() > .1)
				{
					clock_mouse.Reset();
					if (mission[0].difficulty + changevalue < 4 && mission[0].difficulty + changevalue > 0)
						mission[0].difficulty += changevalue;
				}
			}

			// Draw setting box (artifacts)
			if (mouse.x > screen_wh-100 && mouse.x < screen_wh+125 && mouse.y > screen_hh+33 && mouse.y < screen_hh+53)
			{
				if (Input.IsMouseButtonDown(sf::Mouse::Left))
					changevalue = 1;
				if (Input.IsMouseButtonDown(sf::Mouse::Right))
					changevalue = -1;
				if (clock_mouse.GetElapsedTime() > .1)
				{
					clock_mouse.Reset();
					if (mission[0].artifacts + changevalue < 2 && mission[0].artifacts + changevalue > -1)
						mission[0].artifacts += changevalue;
				}
			}

		}

		// Settings dialog
		if (menuStatus == 4 )
		{

			// Check setting box (resolution)
			if (mouse.x > screen_wh-100 && mouse.x < screen_wh+125 && mouse.y > screen_hh-67 && mouse.y < screen_hh-47)
			{
				if (Input.IsMouseButtonDown(sf::Mouse::Left))
					changevalue = -1;
				if (Input.IsMouseButtonDown(sf::Mouse::Right))
					changevalue = 1;
				if (clock_mouse.GetElapsedTime() > .1)
				{
					clock_mouse.Reset();
					if (VideoModeSelected + changevalue < VideoModesCount && VideoModeSelected + changevalue > -1)
						VideoModeSelected += changevalue;
				}
			}

			// Check setting box (Screen mode)
			if (mouse.x > screen_wh-100 && mouse.x < screen_wh+125 && mouse.y > screen_hh-42 && mouse.y < screen_hh-22)
			{		
				if (Input.IsMouseButtonDown(sf::Mouse::Left))
					changevalue = 1;
				if (Input.IsMouseButtonDown(sf::Mouse::Right))
					changevalue = -1;
				if (clock_mouse.GetElapsedTime() > .1)
				{
					clock_mouse.Reset();
					if (fullscreen + changevalue < 2 && fullscreen + changevalue > -1)
						fullscreen += changevalue;
				}
			}

			// Check setting box (VSync)
			if (mouse.x > screen_wh-100 && mouse.x < screen_wh+125 && mouse.y > screen_hh-17 && mouse.y < screen_hh+3)
			{
				if (Input.IsMouseButtonDown(sf::Mouse::Left))
					changevalue = 1;
				if (Input.IsMouseButtonDown(sf::Mouse::Right))
					changevalue = -1;
				if (clock_mouse.GetElapsedTime() > .1)
				{
					clock_mouse.Reset();
					if (v_sync + changevalue < 2 && v_sync + changevalue > -1)
						v_sync += changevalue;
				}
			}

			// Check setting box (Music volume)
			if (mouse.x > screen_wh-100 && mouse.x < screen_wh+125 && mouse.y > screen_hh+8 && mouse.y < screen_hh+28)
			{
				if (Input.IsMouseButtonDown(sf::Mouse::Left))
					changevalue = 1;
				if (Input.IsMouseButtonDown(sf::Mouse::Right))
					changevalue = -1;
				if (clock_mouse.GetElapsedTime() > .1)
				{
					clock_mouse.Reset();
					if (music_volume + changevalue < 101 && music_volume + changevalue > - 1)
						music_volume += changevalue;
				}
			}
		}

		Buttons_Check();

		Draw_Menu();
	}
}

void Buttons_Check()
{
	int code = 0;

	// Get code
	for (int i = 0; i < 21; i++)
		if (button[i].active)
			if (button[i].IntRect.Contains(mouse.x, mouse.y))
			{
				if (!button[i].hover)
				{
					button[i].hover = 1;
					sound[6].Play();
				}
				if (Event.Type == sf::Event::MouseButtonPressed && clock_button.GetElapsedTime() > .5)
				{
					clock_button.Reset();
					code = button[i].code;
					sound[12].Play();
				}
			}
			else
				button[i].hover = 0;

	// Check code
	if (code > 0)
	{

		// Open Campaign menu
		//if (code == 1)
		//	menuStatus = 1;

		// Open Skirmish menu
		if (code == 2)
			menuStatus = 2;

		// Quit game
		if (code == 3)
		{
			gameStatus = 0;
			return;
		}

		// Quit to mainmenu - yes
		if (code == 4)
		{
			exitGame = -1;
			if (music)
				music[0].Stop();
			gameStatus = 1;
			return;
		}

		// Quit to mainmenu - no
		if (code == 5)
			exitGame = -1;

		// Settings - ok
		if (code == 15)
		{

			// Set new music volume
			if (play_music)
			{
				music[1].SetVolume(music_volume);
				music[0].SetVolume(music_volume);
			}

			// Set vsync
			App.UseVerticalSync(v_sync ? 1 : 0);

			// Set screen modus
			if (fullscreen != oldscreen)
			{
				oldscreen = fullscreen;
				App.Close();
				App.Create(VideoMode[VideoModeSelected], "Galactic Empire", sf::Style::Close | (fullscreen ? sf::Style::Fullscreen : 0), Settings);
				App.ShowMouseCursor(false);
				App.SetCursorPosition(screen_wh, screen_hh);
			}

			// Set new resolution
			if (screen_w != VideoMode[VideoModeSelected].Width || screen_h != VideoMode[VideoModeSelected].Height)
			{
				screen_w = VideoMode[VideoModeSelected].Width;
				screen_h = VideoMode[VideoModeSelected].Height;
				screen_wh = screen_w/2 - 1;
				screen_hh = screen_h/2 - 1;
				App.Close();
				App.Create(VideoMode[VideoModeSelected], "Galactic Empire", sf::Style::Close | (fullscreen ? sf::Style::Fullscreen : 0), Settings);
				App.ShowMouseCursor(false);
				App.SetCursorPosition(screen_wh, screen_hh);
				View.SetFromRect(sf::FloatRect(0, 0, (float)screen_w, (float)screen_h));
				ViewDefault.SetFromRect(sf::FloatRect(0, 0, (float)screen_w, (float)screen_h));
				graphic[2].SetPosition((float)screen_wh,(float)-8); // Research button
				graphic[4].SetPosition((float)screen_wh,(float)screen_hh); // Research window
				graphic[13].SetPosition((float)screen_wh,(float)screen_hh); // Main menu background
				graphic[17].SetPosition(0,(float)screen_hh); // Virus icon
				graphic[18].SetPosition((float)screen_wh,0); // Title
				graphic[23].SetPosition((float)screen_wh,(float)screen_hh); // UI quit
				graphic[24].SetPosition((float)screen_wh,(float)screen_hh); // UI mainmenu
				graphic[26].SetPosition((float)screen_wh,(float)screen_hh); //Diplomacy window
				graphic[25].SetPosition((float)screen_wh,(float)screen_h+7); // Diplomacy button
				graphic[5].Scale((float)screen_w/graphic[5].GetSize().x,(float)screen_h/graphic[5].GetSize().y);
				graphic[13].Scale((float)screen_w/graphic[13].GetSize().x,(float)screen_h/graphic[13].GetSize().y);
				graphic[15].Scale((float)screen_w/graphic[15].GetSize().x,(float)screen_h/graphic[15].GetSize().y);
				Buttons_Init();
			}

			// Save settings to config
			ofstream outFile("data/config.dat");
			if (outFile.is_open())
			{
				outFile << screen_w << endl;
				outFile << screen_h << endl;
				outFile << fullscreen << endl;
				outFile << v_sync << endl;
				outFile << music_volume << endl;
				outFile.close();
			}

			menuStatus = 0;
		}

		// Stats - ok
		if (code == 6)
		{
			endGame = -1;
			if (music)
				music[0].Stop();
			gameStatus = 1;
			return;
		}

		// Skirmish - ok
		if (code == 7)
		{
			Game_Init();
			if (play_music)
			{
				music[1].Stop();
				music[0].Play();
			}
			gameStatus = 2;
			return;
		}

		// Open Story menu
		if (code == 8)
			menuStatus = 3;

		// Open Settings menu
		if (code == 9)
			menuStatus = 4;

		// Story - ok, Campaign/Skirmish/Settings - cancel
		if (code == 10 || code == 12 || code == 13 || code == 14)
			menuStatus = 0;

		// Research - ok
		if (code == 16)
			showResearch = -1;

		// Research - research
		if (code == 17)
		{
			if (player[1].researching == 0)
			{
				if (!player[1].research[research_selected].researched && player[1].credits >= player[1].research[research_selected].costs)
				{
					player[1].researching = research_selected;
					player[1].research_progress = 0;
					player[1].credits -= player[1].research[research_selected].costs;
				}
			}
		}

		// Diplomacy - ok
		if (code == 18)
			showDiplomacy = -1;

		// Send credits
		if (code == 19 || code == 20 || code == 21)
		{
			if (clock_mouse.GetElapsedTime() > .25)
			{
				clock_mouse.Reset();
				if (player[1].credits >= 100)
				{
					player[1].credits -= 100;
					if (code == 19)
					{
						player[2].credits += 100;
						if (player[2].alignment[1] < 100)
							player[2].alignment[1] += 1;
					}
					if (code == 20)
					{
						player[3].credits += 100;
						if (player[3].alignment[1] < 100)
							player[3].alignment[1] += 1;
					}
					if (code == 21)
					{
						player[4].credits += 100;
						if (player[4].alignment[1] < 100)
							player[4].alignment[1] += 1;
					}
				}
			}
		}
	}

}

void Draw_Menu()
{

	App.Clear();
	// Draw background
	if (clock_animation.GetElapsedTime() > .025)
	{
		clock_animation.Reset();
		graphic[13].Scale(1.001f, 1.001f);
		graphic[13].Rotate( .01f);
		if (graphic[13].GetScale().x < 1.4 * screen_w/1024 && alpha > 0)
		{
			alpha -= 2;
			graphic[13].SetColor(sf::Color(255,255,255,255-alpha));
		}
		if (graphic[13].GetScale().x > 1.4 * screen_w/1024 && alpha < 256)
		{
			alpha += 2;
			graphic[13].SetColor(sf::Color(255,255,255,255-alpha));
		}
		if (alpha > 252)
		{
			graphic[13].SetScale((float)screen_w/1024,(float)screen_h/768);
			graphic[13].SetRotation(0.0f);
		}
	}
	App.Draw(graphic[13]);

	// Draw black boxes
	sf::Shape Rect;
	Rect = sf::Shape::Rectangle(0.0f,0.0f,(float)screen_w,(float)210,sf::Color(0,0,0),1,sf::Color(150,150,150));
	App.Draw(Rect);
	Rect = sf::Shape::Rectangle(0.0f,(float)screen_h-210,(float)screen_w,(float)screen_h,sf::Color(0,0,0),1,sf::Color(150,150,150));
	App.Draw(Rect);

	// Draw GalEmp title
	App.Draw(graphic[18]);

	if (menuStatus == 2) // Skirmish
	{
		// Draw box
		App.Draw(graphic[24]);
		// Title
		Draw_String(0,screen_wh, screen_hh-130,25,"Skirmish",0,206,209,255,1);
		// Text

		sstr << "Number of systems: " << mission[0].starsystems;
		if (mouse.x > screen_wh-100 && mouse.x < screen_wh+125 && mouse.y > screen_hh-67 && mouse.y < screen_hh-47)
			Draw_String(0,screen_wh-100, screen_hh-70,20,sstr.str(),0,206,209,255,0);
		else
			Draw_String(0,screen_wh-100, screen_hh-70,20,sstr.str(),255,255,255,255,0);

		sstr << "Number of planets: ~" << mission[0].planets;
		if (mouse.x > screen_wh-100 && mouse.x < screen_wh+125 && mouse.y > screen_hh-42 && mouse.y < screen_hh-22)
			Draw_String(0,screen_wh-100, screen_hh-45,20,sstr.str(),0,206,209,255,0);
		else
			Draw_String(0,screen_wh-100, screen_hh-45,20,sstr.str(),255,255,255,255,0);

		sstr << "Number of enemies: " << mission[0].players-1;
		if (mouse.x > screen_wh-100 && mouse.x < screen_wh+125 && mouse.y > screen_hh-17 && mouse.y < screen_hh+3)
			Draw_String(0,screen_wh-100, screen_hh-20,20,sstr.str(),0,206,209,255,0);
		else
			Draw_String(0,screen_wh-100, screen_hh-20,20,sstr.str(),255,255,255,255,0);

		if (mission[0].difficulty == 1)
			if (mouse.x > screen_wh-100 && mouse.x < screen_wh+125 && mouse.y > screen_hh+8 && mouse.y < screen_hh+28)
				Draw_String(0,screen_wh-100, screen_hh+5,20,"Difficulty: easy",0,206,209,255,0);
			else
				Draw_String(0,screen_wh-100, screen_hh+5,20,"Difficulty: easy",255,255,255,255,0);
		if (mission[0].difficulty == 2)
			if (mouse.x > screen_wh-100 && mouse.x < screen_wh+125 && mouse.y > screen_hh+8 && mouse.y < screen_hh+28)
				Draw_String(0,screen_wh-100, screen_hh+5,20,"Difficulty: normal",0,206,209,255,0);
			else
				Draw_String(0,screen_wh-100, screen_hh+5,20,"Difficulty: normal",255,255,255,255,0);
		if (mission[0].difficulty == 3)
			if (mouse.x > screen_wh-100 && mouse.x < screen_wh+125 && mouse.y > screen_hh+8 && mouse.y < screen_hh+28)
				Draw_String(0,screen_wh-100, screen_hh+5,20,"Difficulty: hard",0,206,209,255,0);
			else
				Draw_String(0,screen_wh-100, screen_hh+5,20,"Difficulty: hard",255,255,255,255,0);
		if (mission[0].artifacts == 0)
			if (mouse.x > screen_wh-100 && mouse.x < screen_wh+125 && mouse.y > screen_hh+33 && mouse.y < screen_hh+53)
				Draw_String(0,screen_wh-100, screen_hh+30,20,"Enable artifacts: no",0,206,209,255,0);
			else
				Draw_String(0,screen_wh-100, screen_hh+30,20,"Enable artifacts: no",255,255,255,255,0);
		if (mission[0].artifacts == 1)
			if (mouse.x > screen_wh-100 && mouse.x < screen_wh+125 && mouse.y > screen_hh+33 && mouse.y < screen_hh+53)
				Draw_String(0,screen_wh-100, screen_hh+30,20,"Enable artifacts: yes",0,206,209,255,0);
			else
				Draw_String(0,screen_wh-100, screen_hh+30,20,"Enable artifacts: yes",255,255,255,255,0);
	}

	if (menuStatus == 3) // Controls
	{
		// Draw box
		App.Draw(graphic[24]);
		// Title
		Draw_String(0,screen_wh, screen_hh-140,25,"The story so far..",0,206,209,255,1);
		// Text
		Draw_String(0,screen_wh-215, screen_hh-90,20,"Space exploration has split mankind into different",255,255,255,255,0);
		Draw_String(0,screen_wh-215, screen_hh-70,20,"factions each venturing out in space and creating",255,255,255,255,0);
		Draw_String(0,screen_wh-215, screen_hh-50,20,"their own colonies.",255,255,255,255,0);
		Draw_String(0,screen_wh-215, screen_hh-30,20,"As the leader of one these factions it is your",255,255,255,255,0);
		Draw_String(0,screen_wh-215, screen_hh-10,20,"destiny to guide your people at exploring and",255,255,255,255,0);
		Draw_String(0,screen_wh-215, screen_hh+10,20,"conquering the known universe.",255,255,255,255,0);
		Draw_String(0,screen_wh-215, screen_hh+30,20,"Find ancient artifacts to aid your goal and",255,255,255,255,0);
		Draw_String(0,screen_wh-215, screen_hh+50,20,"defeat all those who stand in your way.",255,255,255,255,0);
		Draw_String(0,screen_wh-215, screen_hh+70,20,"It's your turn to create a Galactic Empire!",255,255,255,255,0);
	}

	if (menuStatus == 4) // Settings
	{
		// Draw box
		App.Draw(graphic[24]);

		// Title
		Draw_String(0,screen_wh, screen_hh-130,25,"Settings",0,206,209,255,1);
		// Text

		sstr << "Resolution: " << VideoMode[VideoModeSelected].Width << "x" << VideoMode[VideoModeSelected].Height;
		if (mouse.x > screen_wh-100 && mouse.x < screen_wh+125 && mouse.y > screen_hh-67 && mouse.y < screen_hh-47)
			Draw_String(0,screen_wh-100, screen_hh-70,20, sstr.str(),0,206,209,255,0);
		else
			Draw_String(0,screen_wh-100, screen_hh-70,20, sstr.str(),255,255,255,255,0);

		if (!fullscreen)
			if (mouse.x > screen_wh-100 && mouse.x < screen_wh+125 && mouse.y > screen_hh-42 && mouse.y < screen_hh-22)
				Draw_String(0,screen_wh-100, screen_hh-45,20, "Mode: Windowed",0,206,209,255,0);
			else
				Draw_String(0,screen_wh-100, screen_hh-45,20, "Mode: Windowed",255,255,255,255,0);
		else
			if (mouse.x > screen_wh-100 && mouse.x < screen_wh+125 && mouse.y > screen_hh-42 && mouse.y < screen_hh-22)	
				Draw_String(0,screen_wh-100, screen_hh-45,20, "Mode: Fullscreen",0,206,209,255,0);
			else
				Draw_String(0,screen_wh-100, screen_hh-45,20, "Mode: Fullscreen",255,255,255,255,0);
		if (!v_sync)
			if (mouse.x > screen_wh-100 && mouse.x < screen_wh+125 && mouse.y > screen_hh-17 && mouse.y < screen_hh+3)
				Draw_String(0,screen_wh-100, screen_hh-20,20, "Vsync: off",0,206,209,255,0);
			else
				Draw_String(0,screen_wh-100, screen_hh-20,20, "Vsync: off",255,255,255,255,0);
		else
			if (mouse.x > screen_wh-100 && mouse.x < screen_wh+125 && mouse.y > screen_hh-17 && mouse.y < screen_hh+3)
				Draw_String(0,screen_wh-100, screen_hh-20,20, "Vsync: on",0,206,209,255,0);
			else
				Draw_String(0,screen_wh-100, screen_hh-20,20, "Vsync: on",255,255,255,255,0);

		sstr << "Music volume: " << music_volume << "%";
		if (mouse.x > screen_wh-100 && mouse.x < screen_wh+125 && mouse.y > screen_hh+ 8 && mouse.y < screen_hh+28)
			Draw_String(0,screen_wh-100, screen_hh+5,20, sstr.str(),0,206,209,255,0);
		else
			Draw_String(0,screen_wh-100, screen_hh+5,20, sstr.str(),255,255,255,255,0);
	}

	//Print game version
	Draw_String(0,screen_w-10, screen_h-30,20, game_version,255,255,255,255,2);

	Buttons_Draw();

	// Draw cursor
	graphic[10].SetSubRect(sf::IntRect(0, active_cursor * 32, 31, 31 + active_cursor * 32));
	graphic[10].SetPosition((float)mouse.x,(float)mouse.y);
	App.Draw(graphic[10]);

	// Display all
	App.Display();

}

void Draw_String(short fontnumber, int x, int y, int size, sf::Unicode::Text string, sf::Uint8 red, sf::Uint8 green, sf::Uint8 blue, sf::Uint8 alpha, int alignment)
{

	int fontsize;

	if (size == 15)
		fontsize = 0;
	else if (size == 20)
		fontsize = 1;
	else if (size == 25)
		fontsize = 2;
	else if (size = 35)
		fontsize = 3;
	else
		fontsize = 4;

	myString[fontsize].SetText(string);
	if (alignment == 0)
		myString[fontsize].SetPosition((float)x,(float)y);
	else if (alignment == 1)
		myString[fontsize].SetPosition((float)x-myString[fontsize].GetRect().GetWidth()/2,(float)y);
	else
		myString[fontsize].SetPosition((float)x-myString[fontsize].GetRect().GetWidth(),(float)y);
	myString[fontsize].SetColor(sf::Color(red,green,blue,alpha));
	App.Draw(myString[fontsize]);
	sstr.str ("");
}

void Draw_Box(int x, int y, int width, int height, int red, int green, int blue)
{
	sf::Shape Rect = sf::Shape::Rectangle(0.0f,0.0f,(float)width,(float)height,sf::Color(255,255,255,50),1,sf::Color(red,green,blue));
	Rect.SetPosition((float)x,(float)y);
	App.Draw(Rect);
}

void Reset_Planet(int system, int planet)
{
	p1 = &starsystem[system].planet[planet];
	p1->size = 2;
	p1->owner = 0;
	p1->population = 0;
	p1->max_population = 0;
	p1->selected = 0;
	p1->group = 0;
	p1->auto_send = 0;
	p1->artifact = 0;
	p1->has_satellite = 0;
	p1->satellite_angle = 0;
	p1->has_turret = 0;
	p1->turret_target_planet = 0;
	p1->rotation = 0;
	p1->has_disruption = false;
	p1->name = "Unknown";
	p1->hovered = 0;
	p1->selected = 0;
	p1->attacked = 0;
	p1->has_event = false;
	p1->event_radius = 0;
	p1->position_turret.x = 0;
	p1->position_turret.y = 0;
	p1->turret_target_system = 0;
	p1->event_fade = 255;
	p1->turret_angle = 0;
	p1->has_moon = 0;
	p1->moon_angle = 0;
	p1->fleets = 0;
	p1->satellite_health = 0;
	p1->has_mine = 0;

}
void Input_Keyboard()
{

	const sf::Input& Input = App.GetInput();

	// Delete turrets
	if (Input.IsKeyDown(sf::Key::Delete))
	{
		if (clock_keyboard.GetElapsedTime() > .25)
		{
			clock_keyboard.Reset();
			for (int i = 1; i <= mission[0].starsystems; i++)
				for (int j = 1; j <= starsystem[i].planets; i++)
					if 	(starsystem[i].planet[j].has_turret == 1 && starsystem[i].planet[j].turret_selected == 1 && starsystem[i].planet[j].owner == 1)
					{
						starsystem[i].planet[j].has_turret = 0;
						starsystem[i].planet[j].turret_selected = 0;
						Add_Explosion(starsystem[i].planet[j].position_turret);
					}
		}
	}

	// Build fleet
	if (Input.IsKeyDown(sf::Key::B))
	{
		if (clock_keyboard.GetElapsedTime() > .25)
		{
			clock_keyboard.Reset();
			if (planet_selected > 0 && planet_selected_count == 1)
				if (starsystem[system_selected].planet[planet_selected].fleets <= 10 && player[1].credits >= CREDITS_FLEET && player[1].metal >= METAL_FLEET && starsystem[system_selected].planet[planet_selected].owner == 1)
				{
					player[1].credits -= CREDITS_FLEET;
					player[1].metal -= METAL_FLEET;
					Add_Fleet(system_selected, planet_selected);
				}
		}
	}

	// Build satellite
	if (Input.IsKeyDown(sf::Key::S))
	{
		if (clock_keyboard.GetElapsedTime() > .25)
		{
			clock_keyboard.Reset();
			if (planet_selected > 0 && planet_selected_count == 1 && player[1].research[5].researched)
				if (starsystem[system_selected].planet[planet_selected].has_satellite == 0 && player[1].credits >= CREDITS_SATELLITE && player[1].metal >= METAL_SATELLITE && starsystem[system_selected].planet[planet_selected].owner == 1)
				{
					starsystem[system_selected].planet[planet_selected].has_satellite = 1;
					player[1].credits -= CREDITS_SATELLITE;
					player[1].metal -= METAL_SATELLITE;
					starsystem[system_selected].planet[planet_selected].satellite_health = 1;
					if (player[1].research[6].researched)
						starsystem[system_selected].planet[planet_selected].satellite_health += 1;
					if (player[1].research[7].researched)
						starsystem[system_selected].planet[planet_selected].satellite_health += 1;
				}
		}
	}

	// Build turret
	if (Input.IsKeyDown(sf::Key::T))
	{
		if (clock_keyboard.GetElapsedTime() > .25)
		{
			clock_keyboard.Reset();
			if (planet_selected > 0 && planet_selected_count == 1 && player[1].research[9].researched && player[1].credits >= CREDITS_TURRET && player[1].metal >= METAL_TURRET)
				active_cursor = CURSOR_TURRET;
		}
	}

	// Build mine
	if (Input.IsKeyDown(sf::Key::M))
	{
		if (clock_keyboard.GetElapsedTime() > .25)
		{
			clock_keyboard.Reset();
			if (planet_selected > 0 && planet_selected_count == 1 && player[1].credits >= CREDITS_MINE && player[1].metal >= METAL_MINE)
				active_cursor = CURSOR_MINE;
		}
	}

	// Show diplomacy menu
	if (Input.IsKeyDown(sf::Key::D))
	{
		if (clock_keyboard.GetElapsedTime() > .25)
		{
			clock_keyboard.Reset();
			showDiplomacy *= -1;
			showResearch = -1;
		}
	}

	// Show research menu
	if (Input.IsKeyDown(sf::Key::R))
	{
		if (clock_keyboard.GetElapsedTime() > .25)
		{
			clock_keyboard.Reset();
			showResearch *= -1;
			showDiplomacy = -1;
		}
	}

	// Toggle planet names
	if (Input.IsKeyDown(sf::Key::Tab))
	{
		if (clock_keyboard.GetElapsedTime() > .25)
		{
			clock_keyboard.Reset();
			showPlanetNames *= -1;
		}
	}

	// Set game speed to 1x
	if (Input.IsKeyDown(sf::Key::F1))
	{
		if (clock_keyboard.GetElapsedTime() > .5)
		{
			clock_keyboard.Reset();
			if (game_speed != 1)
			{
				game_speed = 1;
				Add_Message(1);
			}
		}
	}

	// Set game speed to 2x
	if (Input.IsKeyDown(sf::Key::F2))
	{
		if (clock_keyboard.GetElapsedTime() > .5)
		{
			clock_keyboard.Reset();
			if (game_speed != 2)
			{
				game_speed = 2;
				Add_Message(1);
			}
		}
	}

	// Set game speed to 3x
	if (Input.IsKeyDown(sf::Key::F3))
	{
		if (clock_keyboard.GetElapsedTime() > .5)
		{
			clock_keyboard.Reset();
			if (game_speed != 3)
			{
				game_speed = 3;
				Add_Message(1);
			}
		}
	}

	//' Quicksave
	//'If sfInput_IsKeyDown(Inputs,sfKeyF10) Then
	//'	If Timer > (keyboard_timer + .5) Then
	//'		keyboard_timer = Timer
	//'		Open "data/quicksave.sav" For Output As #1
	//'		Print #1,mission(0).starsystems
	//'		Print #1,mission(0).players
	//'		Print #1,mission(0).difficulty
	//'		Print #1,mission(0).artifacts
	//'		Print #1,game_speed
	//'		Print #1,disruption_status
	//'		Print #1,disruption_countdown
	//'		Print #1,disruption_timer
	//'		Print #1,ai_delay
	//'		For i As Integer = 1 To 5
	//'			Print #1,artifact_owner(i)
	//'		Next
	//'		Print #1,research_selected
	//'		Print #1,music_volume
	//'		Print #1,showPlanetNames
	//'		Print #1,system_selected
	//'		Print #1,planet_selected
	//'		Print #1,showResearch
	//'		Print #1,showDiplomacy
	//'		Print #1,total_planets
	//'		Print #1,active_cursor
	//'		For i As Integer = 1 To mission(0).starsystems
	//'			Print #1,starsystem(i).planets
	//'			For j As Integer = 1 To 5
	//'				Print #1,starsystem(i).nearby(j)
	//'			Next
	//'			For j As Integer = 0 To starsystem(i).planets
	//'				With starsystem(i).planet(j)
	//'					Print #1,.position.x
	//'					Print #1,.position.y
	//'					Print #1,.owner
	//'					Print #1,.fleet[1].target_system
	//'					Print #1,.fleet[1].target_planet
	//'					Print #1,.group
	//'					Print #1,.artifact
	//'					Print #1,.fleet[1].has_target
	//'					Print #1,.auto_send
	//'					Print #1,.attacked
	//'					Print #1,.has_event
	//'					Print #1,.event_radius
	//'					Print #1,.disruption
	//'					Print #1,.has_satellite
	//'					Print #1,.satellite_angle
	//'					Print #1,.has_turret
	//'					Print #1,.position_turret.x
	//'					Print #1,.position_turret.y
	//'					Print #1,.turret_target_planet
	//'					Print #1,.turret_target_system
	//'					Print #1,.fleet[1].angle
	//'					Print #1,.size
	//'					Print #1,.population
	//'					Print #1,.max_population
	//'					Print #1,.fleet[1].position.x
	//'					Print #1,.fleet[1].position.y
	//'					Print #1,.fleet[1].speed.x
	//'					Print #1,.fleet[1].speed.y
	//'					Print #1,.turret_angle
	//'					Print #1,.rotation
	//'					Print #1,.fleets
	//'					Print #1,.fleet[1].health
	//'					Print #1,.has_mine
	//'					Print #1,.position_mine.x
	//'					Print #1,.position_mine.y
	//'					Print #1,.satellite_health
	//'					Print #1,.has_moon
	//'					Print #1,.moon_angle
	//'					Dim As String bla
	//'					bla = *sfString_Gettext(.p_name)
	//'					Print #1,bla
	//'				End With
	//'			Next
	//'		Next
	//'		' Save stats
	//'		For i As Integer = 0 To mission(0).players
	//'			Print #1,stats(i).planet_count
	//'			Print #1,stats(i).credits
	//'			Print #1,stats(i).metal
	//'			Print #1,stats(i).fleet_range
	//'			Print #1,stats(i).planets_conquered
	//'			Print #1,stats(i).planets_lost
	//'			Print #1,stats(i).fleets_built
	//'			Print #1,stats(i).artifacts_discovered
	//'			Print #1,stats(i).researching
	//'			Print #1,stats(i).research_progress
	//'			Print #1,stats(i).researches_done
	//'			Print #1,stats(i).population
	//'			Print #1,stats(i).fleet_damage
	//'			For j As Integer = 1 To 5
	//'				Print #1,stats(i).artifact_discovered(j)
	//'			Next
	//'			For j As Integer = 0 To 19
	//'				Print #1,stats(i).researched(j)
	//'			Next
	//'			For j As Integer = 1 To 4
	//'				If j <> i Then Print #1,stats(i).alignment(j)
	//'			Next
	//'		Next
	//'		' Save rocks
	//'		For i As Integer = 1 To mission(0).starsystems
	//'			Print #1,starsystem(i).rocks
	//'			For j As Integer = 0 To starsystem(i).rocks
	//'				With starsystem(i).rock(j)
	//'					Print #1,.position.x
	//'					Print #1,.position.y
	//'					Print #1,.number
	//'					Print #1,.rotation_direction
	//'					Print #1,.rotation
	//'					Print #1,.has_mine
	//'					Print #1,.owner
	//'					Print #1,.hovered
	//'					Print #1,.selected
	//'					Print #1,.size
	//'				End With
	//'			Next
	//'		Next
	//'		Close
	//'		Add_Message(2)
	//'	End If
	//'End If

	//'' Quickload
	//'If sfInput_IsKeyDown(Inputs,sfKeyF11) Then
	//'	If Timer > (keyboard_timer + .5) Then
	//'		keyboard_timer = Timer
	//'		Open "data/quicksave.sav" For Input As #1
	//'		Input #1,mission(0).starsystems
	//'		Input #1,mission(0).players
	//'		Input #1,mission(0).difficulty
	//'		Input #1,mission(0).artifacts
	//'		Input #1,game_speed
	//'		Input #1,disruption_status
	//'		Input #1,disruption_countdown
	//'		Input #1,disruption_timer
	//'		Input #1,ai_delay
	//'		For i As Integer = 1 To 5
	//'			Input #1,artifact_owner(i)
	//'		Next
	//'		Input #1,research_selected
	//'		Input #1,music_volume
	//'		Input #1,showPlanetNames
	//'		Input #1,system_selected
	//'		Input #1,planet_selected
	//'		Input #1,showResearch
	//'		Input #1,showDiplomacy
	//'		Input #1,total_planets
	//'		Input #1,active_cursor
	//'		For i As Integer = 1 To mission(0).starsystems
	//'			Input #1,starsystem(i).planets
	//'			For j As Integer = 1 To 5
	//'				Input #1,starsystem(i).nearby(j)
	//'			Next
	//'			For j As Integer = 0 To starsystem(i).planets
	//'				With starsystem(i).planet(j)
	//'					Input #1,.position.x
	//'					Input #1,.position.y
	//'					Input #1,.owner
	//'					Input #1,.fleet[1].target_system
	//'					Input #1,.fleet[1].target_planet
	//'					Input #1,.group
	//'					Input #1,.artifact
	//'					Input #1,.fleet[1].has_target
	//'					Input #1,.auto_send
	//'					Input #1,.attacked
	//'					Input #1,.has_event
	//'					Input #1,.event_radius
	//'					Input #1,.disruption
	//'					Input #1,.has_satellite
	//'					Input #1,.satellite_angle
	//'					Input #1,.has_turret
	//'					Input #1,.position_turret.x
	//'					Input #1,.position_turret.y
	//'					Input #1,.turret_target_planet
	//'					Input #1,.turret_target_system
	//'					Input #1,.fleet[1].angle
	//'					Input #1,.size
	//'					Input #1,.population
	//'					Input #1,.max_population
	//'					Input #1,.fleet[1].position.x
	//'					Input #1,.fleet[1].position.y
	//'					Input #1,.fleet[1].speed.x
	//'					Input #1,.fleet[1].speed.y
	//'					Input #1,.turret_angle
	//'					Input #1,.rotation
	//'					Input #1,.fleets
	//'					Input #1,.fleet[1].health
	//'					Input #1,.has_mine
	//'					Input #1,.position_mine.x
	//'					Input #1,.position_mine.y
	//'					Input #1,.satellite_health
	//'					Input #1,.has_moon
	//'					Input #1,.moon_angle
	//'					Dim As String bla
	//'					bla = *sfString_Gettext(.p_name)
	//'					Input #1,bla
	//'				End With
	//'			Next
	//'		Next
	//'		' Save stats
	//'		For i As Integer = 0 To mission(0).players
	//'			Input #1,stats(i).planet_count
	//'			Input #1,stats(i).credits
	//'			Input #1,stats(i).metal
	//'			Input #1,stats(i).fleet_range
	//'			Input #1,stats(i).planets_conquered
	//'			Input #1,stats(i).planets_lost
	//'			Input #1,stats(i).fleets_built
	//'			Input #1,stats(i).artifacts_discovered
	//'			Input #1,stats(i).researching
	//'			Input #1,stats(i).research_progress
	//'			Input #1,stats(i).researches_done
	//'			Input #1,stats(i).population
	//'			Input #1,stats(i).fleet_damage
	//'			For j As Integer = 1 To 5
	//'				Input #1,stats(i).artifact_discovered(j)
	//'			Next
	//'			For j As Integer = 0 To 19
	//'				Input #1,stats(i).researched(j)
	//'			Next
	//'			For j As Integer = 1 To 4
	//'				If j <> i Then Input #1,stats(i).alignment(j)
	//'			Next
	//'		Next
	//'		' Save rocks
	//'		For i As Integer = 1 To mission(0).starsystems
	//'			Input #1,starsystem(i).rocks
	//'			For j As Integer = 0 To starsystem(i).rocks
	//'				With starsystem(i).rock(j)
	//'					Input #1,.position.x
	//'					Input #1,.position.y
	//'					Input #1,.number
	//'					Input #1,.rotation_direction
	//'					Input #1,.rotation
	//'					Input #1,.has_mine
	//'					Input #1,.owner
	//'					Input #1,.hovered
	//'					Input #1,.selected
	//'					Input #1,.size
	//'				End With
	//'			Next
	//'		Next
	//'		Close
	//'		Add_Message(3)
	//'		'sfView_Destroy(viewbox)
	//'		viewbox = sfView_CreateFromRect(viewrect)
	//'		zoom = 1
	//'		Open "data/log.dat" For Output As #1
	//'		Print #1, "Game loaded successfully"
	//'		Close
	//'	End If
	//'End If

	// Zoom in
	if (Input.IsKeyDown(sf::Key::N))
	{
		zoomCode = 1;
		zoom_counter = 1;
	}

	// Zoom out
	if (Input.IsKeyDown(sf::Key::M))
	{
		zoomCode = 2;
		zoom_counter = 1;
	}

	// Scroll left
	if (Input.IsKeyDown(sf::Key::Left))
	{
		if (clock_keyboard.GetElapsedTime() > .005)
		{
			clock_keyboard.Reset();
			View.Move((float)-4/zoom, 0.0f);
		}
	}

	// Scroll right
	if (Input.IsKeyDown(sf::Key::Right))
	{
		if (clock_keyboard.GetElapsedTime() > .005)
		{
			clock_keyboard.Reset();
			View.Move((float)4/zoom, 0.0f);
		}
	}

	//' Scroll up
	if (Input.IsKeyDown(sf::Key::Up))
	{
		if (clock_keyboard.GetElapsedTime() > .005)
		{
			clock_keyboard.Reset();
			View.Move(0.0f,(float)-4/zoom);
		}
	}

	//' Scroll down
	if (Input.IsKeyDown(sf::Key::Down))
	{
		if (clock_keyboard.GetElapsedTime() > .005)
		{
			clock_keyboard.Reset();
			View.Move(0.0f,(float)4/zoom);
		}
	}

	// Reset view position
	if (Input.IsKeyDown(sf::Key::Space))
	{
		if (clock_keyboard.GetElapsedTime() > .1)
		{
			clock_keyboard.Reset();
			if (clock_message.GetElapsedTime() < 10)
				View.SetCenter(event_position);
			else
				View.SetCenter(starsystem[1].planet[0].position);
		}
	}

	// Group planet(s)
	if (Input.IsKeyDown(sf::Key::LControl) && Input.IsKeyDown(sf::Key::Num1))
		group = 1;
	if (Input.IsKeyDown(sf::Key::LControl) && Input.IsKeyDown(sf::Key::Num2))
		group = 2;
	if (Input.IsKeyDown(sf::Key::LControl) && Input.IsKeyDown(sf::Key::Num3))
		group = 3;
	if (Input.IsKeyDown(sf::Key::LControl) && Input.IsKeyDown(sf::Key::Num4))
		group = 4;
	if (Input.IsKeyDown(sf::Key::LControl) && Input.IsKeyDown(sf::Key::Num5))
		group = 5;
	if (Input.IsKeyDown(sf::Key::LControl) && Input.IsKeyDown(sf::Key::Num6))
		group = 6;
	if (Input.IsKeyDown(sf::Key::LControl) && Input.IsKeyDown(sf::Key::Num7))
		group = 7;
	if (Input.IsKeyDown(sf::Key::LControl) && Input.IsKeyDown(sf::Key::Num8))
		group = 8;
	if (Input.IsKeyDown(sf::Key::LControl) && Input.IsKeyDown(sf::Key::Num9))
		group = 9;
	if (group > 0)
	{
		if (clock_keyboard.GetElapsedTime() > .1)
		{
			clock_keyboard.Reset();
			for (int i = 1; i <= mission[0].starsystems; i++)
				for (int j = 1; i <= starsystem[i].planets; i++)
					if (starsystem[i].planet[j].selected)
						starsystem[i].planet[j].group = group;
		}
		group = 0;
	}

	// Select grouped planet(s)
	if (Input.IsKeyDown(sf::Key::Num1) && !Input.IsKeyDown(sf::Key::LControl))
		group = 1;
	if (Input.IsKeyDown(sf::Key::Num2) && !Input.IsKeyDown(sf::Key::LControl))
		group = 2;
	if (Input.IsKeyDown(sf::Key::Num3) && !Input.IsKeyDown(sf::Key::LControl))
		group = 3;
	if (Input.IsKeyDown(sf::Key::Num4) && !Input.IsKeyDown(sf::Key::LControl))
		group = 4;
	if (Input.IsKeyDown(sf::Key::Num5) && !Input.IsKeyDown(sf::Key::LControl))
		group = 5;
	if (Input.IsKeyDown(sf::Key::Num6) && !Input.IsKeyDown(sf::Key::LControl))
		group = 6;
	if (Input.IsKeyDown(sf::Key::Num7) && !Input.IsKeyDown(sf::Key::LControl))
		group = 7;
	if (Input.IsKeyDown(sf::Key::Num8) && !Input.IsKeyDown(sf::Key::LControl))
		group = 8;
	if (Input.IsKeyDown(sf::Key::Num9) && !Input.IsKeyDown(sf::Key::LControl))
		group = 9;
	if (group > 0)
	{
		if (clock_keyboard.GetElapsedTime() > .1)
		{
			clock_keyboard.Reset();
			for (int i = 1; i <= mission[0].starsystems; i++)
				for (int j = 1; i <= starsystem[i].planets; i++)
					if (starsystem[i].planet[j].group == group)
						starsystem[i].planet[j].selected = 1;
					else
						starsystem[i].planet[j].selected = 0;
		}
		group = 0;
	}

}

void Add_Explosion(sf::Vector2f position)
{
	for (int i = 0; i < 51; i++)
	{
		if (!explosion[i].active)
		{
			explosion[i].active = true;
			explosion[i].position = position;
			explosion[i].frame = 0;
			if (zoom <= 1)
				sound[9].SetVolume(50*zoom);
			else
				sound[9].SetVolume(50);
			sound[9].Play();
			break;
		}
	}
}

void Add_Message(int eventnumber)
{
	sound[13].Play();
	clock_message.Reset();
	message_code = eventnumber;
	message_alpha = 255;
}

void Loop_Ingame()
{

	const sf::Input& Input = App.GetInput();

	while (gameStatus == 2)
	{

		// Get events
		App.GetEvent(Event);
		if (Event.Type == sf::Event::Closed)
		{
			gameStatus = 0;
			break;
		}

		// Get mouse status
		mouse.x = Input.GetMouseX();
		mouse.y = Input.GetMouseY();

		// Convert from screen coordinates to world coordinates
		mouse2 = App.ConvertCoords(mouse.x, mouse.y, &View);

		Buttons_Reset();

		// Start new music track if the last one is finished
		if (play_music)
		{
			if (music[0].GetStatus() == sf::Sound::Stopped && endGame == -1)
			{
				// sfMusic_Destroy(musics(0))
				if (sf::Randomizer::Random(0,1) == 0)
					music[0].OpenFromFile("sfx/ethereal_void.ogg");
				else
					music[0].OpenFromFile("sfx/third_awakening.ogg");
				music[0].Play();
				music[0].SetVolume(music_volume);
			}
		}

		// Exit game
		if (Input.IsKeyDown(sf::Key::Escape) && endGame == -1)
		{
			if (clock_keyboard.GetElapsedTime() > .2)
			{
				clock_keyboard.Reset();
				active_cursor = CURSOR_DEFAULT;
				exitGame *= -1;
				showResearch = -1;
				showDiplomacy = -1;
			}
		}

		// Pause game
		if (Input.IsKeyDown(sf::Key::P))
		{
			if (clock_keyboard.GetElapsedTime() > .2)
			{
				clock_keyboard.Reset();
				if (exitGame == -1 && endGame == -1)
					pauseGame *= -1;
			}
		}

		if (pauseGame == -1 && exitGame == -1 && endGame == -1)
		{
			// Check Keyboard Input
			Input_Keyboard();

			// Check Mouse Input
			Input_Mouse();

			// Update Miscellaneous
			Game_Update();
		}

		// Game end conditions (victory or defeat)
		if (player[2].planet_count == 0 && player[3].planet_count == 0 && player[4].planet_count == 0 && endGame == -1)
		{
			music[0].Stop();
			sound[4].Play();
			active_cursor = CURSOR_DEFAULT;
			endGame = 1;
		}
		else if (player[1].planet_count == 0 && endGame == -1)
		{
			music[0].Stop();
			sound[7].Play();
			active_cursor = CURSOR_DEFAULT;
			endGame = 1;
		}

		// Check exit box
		if (exitGame == 1)
		{
			button[3].active = 1;
			button[4].active = 1;
		}

		// Check research box
		if (showResearch == 1)
		{
			button[15].active = 1;
			if (player[1].researching == 0 && !player[1].research[research_selected].researched && player[1].research[research_selected-1].researched)
				button[16].active = 1;
		}

		// Check diplomacy box
		if (showDiplomacy == 1)
		{
			button[17].active = 1;
			if (player[2].planet_count > 0 && player[2].alignment[1] < 100)
				button[18].active = 1;
			if (mission[0].players >= 3 && player[3].planet_count > 0 && player[3].alignment[1] < 100)
				button[19].active = 1;
			if (mission[0].players >= 4 && player[4].planet_count > 0 && player[4].alignment[1] < 100)
				button[20].active = 1;
		}

		// Check end box
		if (endGame == 1)
			button[5].active = 1;

		Buttons_Check();

		Draw_Main();

	}

}

void Draw_Main()
{

	const sf::Input& Input = App.GetInput();

	Draw_Map();

	// Game End
	if (endGame == 1)
	{

		App.SetView(ViewDefault);

		App.Draw(graphic[15]);
		App.Draw(graphic[23]);

		// Game end messages
		if (player[2].planet_count == 0 && player[3].planet_count == 0 && player[4].planet_count == 0)
		{
			// Game won
			Draw_String(0,screen_wh,screen_hh-60,35,"VICTORY!",0,255,0,255,1);
			Draw_String(0,screen_wh,screen_hh-25,20,"You have conquered the galaxy!",255,255,255,255,1);
		}
		else if (player[1].planet_count == 0)
		{
			// Game lost
			Draw_String(0,screen_wh,screen_hh-60,35,"DEFEAT!",255,0,0,255,1);
			Draw_String(0,screen_wh,screen_hh-25,20,"You have been eliminated!",255,255,255,255,1);
		}

		// Draw score
		sstr << "Score: " << (int)(((player[1].planets_conquered - player[1].planets_lost)*2 + player[1].artifacts_discovered*10 + player[1].researches_done*5)/mission[0].starsystems + mission[0].difficulty * 100);
		Draw_String(0,screen_wh,screen_hh-5,20,sstr.str(),255,255,255,255,1);

		// Draw stats
		Draw_Box(screen_wh-335,screen_h-200,670,195,0,0,0);
		for (int i = 1; i <= mission[0].players; i++)
		{
			if (i == 1)
				Draw_String(0,screen_wh-300,screen_h-180,20,"Your Empire ",0,255,0,255,0);
			else if (i == 2)
				Draw_String(0,screen_wh-140,screen_h-180,20,"Dazdarians ",255,0,0,255,0);
			else if (i == 3)
				Draw_String(0,screen_wh+ 30,screen_h-180,20,"Valdarians ",0,0,255,255,0);
			else if (i == 4)
				Draw_String(0,screen_wh+190,screen_h-180,20,"Arcturians ",255,255,0,255,0);
			for (int j = 1; j <= 5; j++)
			{
				if (j == 1)
				{
					sstr << "Planets conquered: " << player[i].planets_conquered;
					Draw_String(0,screen_wh+i*160-470,j*20+screen_h-160,15,sstr.str(),255,255,255,255,0);
				}
				else if (j == 2)
				{
					sstr << "Planets lost: " << player[i].planets_lost;
					Draw_String(0,screen_wh+i*160-470,j*20+screen_h-160,15,sstr.str(),255,255,255,255,0);
				}
				else if (j == 3)
				{
					sstr << "Fleets built: " << player[i].fleets_built;
					Draw_String(0,screen_wh+i*160-470,j*20+screen_h-160,15,sstr.str(),255,255,255,255,0);
				}
				else if (j == 4)
				{
					sstr << "Artifacts found: " << player[i].artifacts_discovered;
					Draw_String(0,screen_wh+i*160-470,j*20+screen_h-160,15,sstr.str(),255,255,255,255,0);
				}
				else if (j == 5)
				{
					sstr << "Researches done: " << player[i].researches_done;
					Draw_String(0,screen_wh+i*160-470,j*20+screen_h-160,15,sstr.str(),255,255,255,255,0);
				}
			}
		}

	}

	if (endGame == -1)
		Draw_Interface();

	Buttons_Draw();

	// Draw cursor
	graphic[10].SetSubRect(sf::IntRect(active_cursor*32,0,31 + active_cursor*32,31));
	graphic[10].SetPosition((float)mouse.x, (float)mouse.y);
	App.Draw(graphic[10]);

	// Take screenshot (has to be here or screenshot will be empty)
	if (Input.IsKeyDown(sf::Key::F12))
	{
		if (clock_keyboard.GetElapsedTime() > .25)
		{
			clock_keyboard.Reset();
			sf::Image Screen = App.Capture();
			Screen.SaveToFile("data/screenshot.jpg");
			Add_Message(10);
		}
	}

	// Display all
	App.Display();

}

void Game_Init()
{
	int starsystem_size = 3000, galaxy_size = 4500 + mission[0].starsystems * 1500;

	// Reset viewbox (in case the view was zoomed out in the last game)
	View.SetFromRect(sf::FloatRect(0, 0, (float)screen_w, (float)screen_h));

	// Reset all player stats
	for (int i = 0; i <= mission[0].players; i++)
	{
		player[i].credits = 800;
		player[i].metal = 400;
		player[i].fleet_range = 600;

		// Reset statistics
		player[i].planet_count = 0;
		player[i].planets_conquered = 0;
		player[i].planets_lost = 0;
		player[i].fleets_built = 0;

		// Random alignments
		for (int j = 1; j <= 4; j++)
			if (j != i)
				player[i].alignment[j] = sf::Randomizer::Random(0,100);

		// No artifacts discovered
		player[i].artifacts_discovered = 0;
		for (int j = 1; j < 6; j++)
			player[i].artifact_discovered[j] = 0;

		// No researches done
		player[i].researching = 0;
		player[i].research_progress = 0;
		player[i].researches_done = 0;
		for (int j = 0; j < 20; j++)
			player[i].research[j].researched = false;
		player[i].research[0].researched = true;
		player[i].research[4].researched = true;
		player[i].research[8].researched = true;
		player[i].research[12].researched = true;
		player[i].research[16].researched = true;
	}

	// Set own alignments to 0
	player[1].alignment[2] = 0;
	player[1].alignment[3] = 0;
	player[1].alignment[4] = 0;

	// Set fleet parameters for each player
	player[1].fleet_speed = 1.05f;
	player[1].fleet_health = .9f;
	player[1].fleet_turnrate = .95f;
	player[1].fleet_damage = 1.1f;

	player[2].fleet_speed = 1.1f;
	player[2].fleet_health = 1.05f;
	player[2].fleet_turnrate = .9f;
	player[2].fleet_damage = .95f;

	player[3].fleet_speed = .95f;
	player[3].fleet_health = 1.1f;
	player[3].fleet_turnrate = 1.05f;
	player[3].fleet_damage = .9f;

	player[4].fleet_speed = .9f;
	player[4].fleet_health = .95f;
	player[4].fleet_turnrate = 1.1f;
	player[4].fleet_damage = 1.05f;

	//Draw_String(0,10,10,20, "Stats done!",255,0,0,255);
	//App.Display();

	if (menuStatus == 2) // Skirmish - create random game
	{
		// Set empty data for starsystems and stars
		for (int i = 1; i <= mission[0].starsystems; i++)
		{
			for (int j = 1; j < 6; j++)
				starsystem[i].nearby[j] = false;
			Reset_Planet(i,0);
			// Set number of planets
			starsystem[i].planets = mission[0].planets + sf::Randomizer::Random(-5,5);
			// Set number of rocks
			starsystem[i].rocks = sf::Randomizer::Random(74,99);
		}

		// Set stars positions
		starsystem[1].planet[0].position = sf::Vector2f(0,0);

		if (mission[0].starsystems > 1)
		{
			for (int i = 2; i <= mission[0].starsystems; i++)
			{
				int repeat = 0;
				do
				{
					starsystem[i].planet[0].position = sf::Vector2f((float)sf::Randomizer::Random(-galaxy_size / 2,galaxy_size / 2),(float)sf::Randomizer::Random(-galaxy_size / 2,galaxy_size / 2));
					// check if the minimum distance between each star is met
					for (int j = 1; j < i; j++)
						if (sqrt(pow((double)starsystem[i].planet[0].position.x-starsystem[j].planet[0].position.x,2)+pow((double)starsystem[i].planet[0].position.y-starsystem[j].planet[0].position.y,2)) < 3000)
						{
							repeat = 1;
							break;
						}
						else
							repeat = 0;
					// check if the maximum distance between each star is met
					if (repeat == 0)
					{
						int counter = 0, j = 1;
						for (j; j < i - 1; j++)
							if (sqrt(pow((double)starsystem[i].planet[0].position.x-starsystem[j].planet[0].position.x,2)+pow((double)starsystem[i].planet[0].position.y-starsystem[j].planet[0].position.y,2)) > 3750)
								counter += 1;
						if (counter == j)
							repeat = 1;
					}
				} while (repeat == 1);
			}
		}

		// Count total planets
		total_planets = 0;
		for (int i = 1; i <= mission[0].starsystems; i++)
			total_planets += starsystem[i].planets;

		// Check if stars are nearby
		for (int i = 1; i <= mission[0].starsystems; i++)
		{
			for (int j = 1; j <= mission[0].starsystems; j++)
			{
				if (i != j && sqrt(pow((double)starsystem[i].planet[0].position.x-starsystem[j].planet[0].position.x,2)+pow((double)starsystem[i].planet[0].position.y-starsystem[j].planet[0].position.y,2)) < 7500)
					starsystem[i].nearby[j] = true;
				else if (i == j)
					starsystem[i].nearby[j] = true;
				else
					starsystem[i].nearby[j] = false;
			}
		}

		// Create planets and rocks
		int name_count = 0;
		for (int i = 1; i <= mission[0].starsystems; i++)
		{
			// Set rocks
			for (int j = 0; j <= starsystem[i].rocks; j++)
			{
				starsystem[i].rock[j].position.x = sf::Randomizer::Random(-starsystem_size / 2, starsystem_size / 2) + starsystem[i].planet[0].position.x;
				starsystem[i].rock[j].position.y = sf::Randomizer::Random(-starsystem_size / 2, starsystem_size / 2) + starsystem[i].planet[0].position.y;
				starsystem[i].rock[j].rotation = sf::Randomizer::Random(0,359);
				starsystem[i].rock[j].rotation_direction = sf::Randomizer::Random(0,1);
				starsystem[i].rock[j].size = sf::Randomizer::Random(0.5f,1.0f);
				starsystem[i].rock[j].number = sf::Randomizer::Random(0,4);
			}
			// Set planets
			for (int j = 1; j <= starsystem[i].planets; j++)
			{
				Reset_Planet(i, j);
				int repeat;
				do
				{
					repeat = 0;
					for (;;) 
					{
						starsystem[i].planet[j].position.x = sf::Randomizer::Random(-starsystem_size / 2, starsystem_size / 2) + starsystem[i].planet[0].position.x;
						starsystem[i].planet[j].position.y = sf::Randomizer::Random(-starsystem_size / 2, starsystem_size / 2) + starsystem[i].planet[0].position.y;
						if (sqrt(pow((double)starsystem[i].planet[j].position.x-starsystem[i].planet[0].position.x,2)+pow((double)starsystem[i].planet[j].position.y-starsystem[i].planet[0].position.y,2)) > 600)
							break;
					}
					if (j > 1)
					{
						// check for collision with previously placed planets
						for (int k = 1; k < j; k++)
							if (starsystem[i].planet[j].position.x > starsystem[i].planet[k].position.x - 128 && starsystem[i].planet[j].position.x < starsystem[i].planet[k].position.x + 128 && starsystem[i].planet[j].position.y > starsystem[i].planet[k].position.y - 128 && starsystem[i].planet[j].position.y < starsystem[i].planet[k].position.y + 128)
								repeat = 1;
						// check if its in range of one of the previously placed planets
						if (repeat == 0)
							// check for distance
							for (int k = 1; k < j; k++)
								if (sqrt(pow((double)starsystem[i].planet[j].position.x-starsystem[i].planet[k].position.x,2)+pow((double)starsystem[i].planet[j].position.y-starsystem[i].planet[k].position.y,2)) < 600)
								{
									repeat = 0;
									break;
								}
								else
									repeat = 1;
					}
				} while (repeat == 1);

				//printf("%i \n",starsystem[i].rock[j].rotation);
				// placement successfully done, now set the rest of the properties
				starsystem[i].planet[j].size = (float)sf::Randomizer::Random((int)5,(int)10)/10;
				starsystem[i].planet[j].population = starsystem[i].planet[j].size*25 + sf::Randomizer::Random((int)0, (int)starsystem[i].planet[j].size * 25);
				starsystem[i].planet[j].max_population = starsystem[i].planet[j].size * 100;
				starsystem[i].planet[j].rotation = sf::Randomizer::Random((float)0.0, (float)359);
				// Set planet names
				if (name_count < 129)
				{
					starsystem[i].planet[j].name = planet_name[name_count];
					name_count += 1;
				}
				else
					starsystem[i].planet[j].name = "Unknown";
				// Set moons
				if (sf::Randomizer::Random(1, 5) == 1)
				{
					starsystem[i].planet[j].has_moon = 1;
					starsystem[i].planet[j].moon_angle = sf::Randomizer::Random(0.0f, 359.0f);
				}
			}
		}

		// Set player position
		for (;;)
		{
			int j = sf::Randomizer::Random(1,starsystem[1].planets);
			if (starsystem[1].planet[j].owner == 0)
			{
				starsystem[1].planet[j].owner = 1;
				player[1].planet_count = 1;
				starsystem[1].planet[j].size = 1;
				starsystem[1].planet[j].population = 50;
				starsystem[1].planet[j].max_population = starsystem[1].planet[j].size * 100;
				View.SetCenter((float)starsystem[1].planet[j].position.x,(float)starsystem[1].planet[j].position.y);
				break;
			}
		}

		// Distribute enemies
		for (int k = 2; k <= mission[0].players; k++)
			for (;;)
			{
				int i = sf::Randomizer::Random(1,mission[0].starsystems);
				int j = sf::Randomizer::Random(1,starsystem[i].planets);
				if (starsystem[i].planet[j].owner == 0)
				{
					starsystem[i].planet[j].owner = k;
					player[k].planet_count = 1;
					starsystem[i].planet[j].size = 1;
					starsystem[i].planet[j].population = 50;
					starsystem[i].planet[j].max_population = starsystem[i].planet[j].size * 100;
					break;
				}
			}

			// Distribute artifacts
			if (mission[0].artifacts > 0)
			{
				for (int k = 1; k < 6; k++)
					for (;;)
					{
						int i = sf::Randomizer::Random(1, mission[0].starsystems);
						int j = sf::Randomizer::Random(1, starsystem[i].planets);
						if (starsystem[i].planet[j].artifact == 0 && starsystem[i].planet[j].owner == 0)
						{
							starsystem[i].planet[j].artifact = k;
							break;
						}
					}
			}
	}

	// Set misc
	message_alpha = 255;
	game_speed = 1;
	mouse_old.x = -1;
	group = 0;
	disruption_status = 0;
	disruption_countdown = 60;
	disruption_timer = 0;
	zoom = 1;
	research_selected = 1;
	showResearch = -1;
	showDiplomacy = -1;
	showPlanetNames = 1;
	exitGame = -1;
	endGame = -1;
	pauseGame = -1;
	zoomCode = 0;
	planet_selected = 0;
	system_selected = 0;
	// Reset fleets
	for (int i = 0; i <= 199; i++)
		fleet[i].built = false;

	ai_delay = (int)(9000/pow((double)mission[0].difficulty,2)); //9000, 2250, 1000
	for (int i = 0; i <= 19; i++)
		for (int j = 1; j <= 4; j++)
		{
			player[j].research[i].time = 0;
		}
	for (int i = 1; i <= 5; i++)
		artifact_owner[i] = 0;
	for (int i = 1; i <= 4; i++)
	{
		player[i].research[1].time = 60;
		player[i].research[2].time = 120;
		player[i].research[3].time = 180;
		player[i].research[5].time = 60;
		player[i].research[6].time = 120;
		player[i].research[7].time = 180;
		player[i].research[9].time = 60;
		player[i].research[10].time = 120;
		player[i].research[11].time = 180;
		player[i].research[13].time = 60;
		player[i].research[14].time = 120;
		player[i].research[15].time = 180;
		player[i].research[17].time = 60;
		player[i].research[18].time = 120;
		player[i].research[19].time = 180;

		player[i].research[1].costs = 500;
		player[i].research[2].costs = 1500;
		player[i].research[3].costs = 4500;
		player[i].research[5].costs = 600;
		player[i].research[6].costs = 1800;
		player[i].research[7].costs = 5400;
		player[i].research[9].costs = 400;
		player[i].research[10].costs = 1200;
		player[i].research[11].costs = 3600;
		player[i].research[13].costs = 300;
		player[i].research[14].costs = 900;
		player[i].research[15].costs = 2700;
		player[i].research[17].costs = 200;
		player[i].research[18].costs = 600;
		player[i].research[19].costs = 1800;
	}

}

void Game_Update()
{

	// Update framerate
	framecount += 1;
	if (clock_fps.GetElapsedTime() > 1)
	{
		clock_fps.Reset();
		framerate = framecount;
		framecount = 0;
	}

	// Zoom view (OLD CODE)
	if (zoom_counter > 0)
	{
		if (zoomCode == 1)
		{
			if (clock_zoom.GetElapsedTime() > .01)
			{
				clock_zoom.Reset();
				zoom_counter -= 1;
				if (zoom <= 3)
				{
					if (zoom <= 1)
						sound[8].SetVolume(100*zoom);
					View.Zoom(1.03f);
					zoom *= 1.03f;
					View.Move(zoomMove);
				}
			}
		}
		else if (zoomCode == 2)
		{
			if (clock_zoom.GetElapsedTime() > .01)
			{
				clock_zoom.Reset();
				zoom_counter -= 1;
				if (zoom >= .125)
				{
					sound[8].SetVolume(100*zoom);
					View.Zoom(1/1.03f);
					zoom /= 1.03f;
					View.Move(zoomMove);
				}
			}
		}
	}
	if (zoom_counter == 0)
		zoomCode = 0;

	// Update rotation
	if (clock_rotation.GetElapsedTime() > .05 / game_speed)
	{
		clock_rotation.Reset();
		for (int i = 1; i <= mission[0].starsystems; i++)
		{
			// Planets
			for (int j = 0; j <= starsystem[i].planets; j++)
			{
				starsystem[i].planet[j].rotation += (float).25 / starsystem[i].planet[j].size;
				if (starsystem[i].planet[j].rotation >= 360)
					starsystem[i].planet[j].rotation = 0;
			}
			// Rocks
			for (int j = 0; j <= starsystem[i].rocks; j++)
			{
				if (starsystem[i].rock[j].rotation_direction == 0)
				{
					starsystem[i].rock[j].rotation += 1;
					if (starsystem[i].rock[j].rotation == 360)
						starsystem[i].rock[j].rotation = 0;
				}
				if (starsystem[i].rock[j].rotation_direction == 1)
				{
					starsystem[i].rock[j].rotation -= 1;
					if (starsystem[i].rock[j].rotation == -1)
						starsystem[i].rock[j].rotation = 359;
				}
			}
		}
	}

	// Count total population of each player
	for (int i = 1; i <= 4; i++)
		player[i].population = 0;
	for (int i = 1; i <= mission[0].starsystems; i++)
		for (int j = 1; j <= starsystem[i].planets; j++)
			if (starsystem[i].planet[j].owner > 0)
				player[starsystem[i].planet[j].owner].population += (int)starsystem[i].planet[j].population;

	// Change alignment of players
	if (sf::Randomizer::Random(0,ai_delay*10) == 0)
	{
		// Choose a player (only AIs)
		int ai1, ai2;
		ai1 = sf::Randomizer::Random(2,mission[0].players);
		do
		{
			// Choose players whos alignment towards to is changed (included player)
			ai2 = sf::Randomizer::Random(1,mission[0].players);
		} while (ai2 == ai1);
		old_alignment = player[ai1].alignment[ai2];
		int alignment_change = sf::Randomizer::Random(5,75);
		player[ai1].alignment[ai2] -= alignment_change;
		if (player[ai1].alignment[ai2] < 0)
			player[ai1].alignment[ai2] += 100;
		if (ai2 == 1)
		{
			if (ai1 == 2)
				Add_Message(11);
			if (ai1 == 3)
				Add_Message(12);
			if (ai1 == 4)
				Add_Message(13);
		}
	}

	// Planet Events
	if (sf::Randomizer::Random(0,ai_delay*10) == 0)
	{
		int i = sf::Randomizer::Random(1,mission[0].starsystems);
		int j = sf::Randomizer::Random(1,starsystem[i].planets);
		p1 = &starsystem[i].planet[j];
		if (player[p1->owner].planet_count > 1) // do revolt and plague only if you have more than 1 planet
		{
			if (sf::Randomizer::Random(0,1) == 0)
			{
				// Trigger planet revolt
				if (p1->owner > 0)
				{
					int k;
					for (;;)
					{
						k = sf::Randomizer::Random(0, mission[0].players + 1);
						if (k != p1->owner && player[k].planet_count > 0)
							break;
					}
					player[p1->owner].planet_count -= 1; // decrease planet count of old owner
					if (player[p1->owner].planet_count == 0)
						Add_Message(6);
					if (p1->owner == 1) // if player loses a planet notify with message and set event
					{
						Add_Message(4);
						p1->has_event = 1;
						p1->event_clock.Reset();
						p1->event_radius = p1->size * 33;
						clock_message.Reset();
						event_position = p1->position;
					}
					p1->owner = k; // set new owner
					player[p1->owner].planet_count += 1; // increase planet count of new owner
					if (p1->owner == 1) // a revolting planet has joined us
					{
						Add_Message(5);
						p1->has_event = 1;
						p1->event_clock.Reset();
						p1->event_radius = p1->size * 33;
						clock_message.Reset();
						event_position = p1->position;
					}
					if (p1->artifact > 0)
						artifact_owner[p1->artifact] = p1->owner;
					p1->auto_send = 0;
					p1->group = 0;
				}
				else
				{
					// Trigger planet plague
					if (p1->owner > 0)
					{
						if (p1->owner == 1)
						{
							Add_Message(9);
							p1->has_event = 1;
							p1->event_clock.Reset();
							p1->event_radius = p1->size * 33;
							clock_message.Reset();
							event_position = p1->position;
						}
						player[p1->owner].planet_count -= 1;
						if (player[p1->owner].planet_count == 0)
							Add_Message(6);
						p1->owner = 0;
						p1->auto_send = 0;
						p1->group = 0;
						p1->population = 1;
					}
				}
			}
		}
	}

	// Update event radius
	if (clock_event.GetElapsedTime() > .05)
	{
		clock_event.Reset();
		for (int i = 1; i <= mission[0].starsystems; i++)
		{
			for (int j = 1; j <= starsystem[i].planets; j++)
			{
				if (starsystem[i].planet[j].has_event)
				{
					starsystem[i].planet[j].event_radius += .5;
					if (starsystem[i].planet[j].event_fade > 3)
						starsystem[i].planet[j].event_fade -= 4;
					if (starsystem[i].planet[j].event_radius > starsystem[i].planet[j].size * 33 + 40)
					{
						starsystem[i].planet[j].event_radius = starsystem[i].planet[j].size * 33;
						starsystem[i].planet[j].event_fade = 255;
					}
					if (starsystem[i].planet[j].event_clock.GetElapsedTime() > 10)
					{
						starsystem[i].planet[j].has_event = false;
						starsystem[i].planet[j].event_fade = 255;
					}
				}
			}
		}
	}

	// Animate Explosions
	if (clock_explosion.GetElapsedTime() > .05)
	{
		clock_explosion.Reset();
		for (int i = 1; i <= 51; i++)
		{
			if (explosion[i].active == 1)
			{
				explosion[i].frame += 1;
				if (explosion[i].frame == 16)
					explosion[i].active = 0;
			}
		}
	}

	//Decrease disruption timer
	if (clock_yetanother.GetElapsedTime() > 1/game_speed)
	{
		clock_yetanother.Reset();
		// status 0 - countdown
		// status 1 - available
		// status 2 - active
		if (disruption_status == 2)
		{
			if (disruption_timer > 0)
				disruption_timer -= 1;
			if (disruption_timer == 0)
			{
				for (int i = 1; i <= mission[0].starsystems; i++)
				{
					for (int j = 1; j <= starsystem[i].planets; j++)
					{
						if (starsystem[i].planet[j].has_disruption)
							starsystem[i].planet[j].has_disruption = false;
					}
				}
				disruption_status = 0;
				disruption_countdown = 60;
			}
		}
		if (disruption_status == 0)
		{
			if (disruption_countdown > 0 && player[1].research[1].researched)
				disruption_countdown -= 1;
			if (disruption_countdown == 0)
				disruption_status = 1;
		}
		// Update research
		for (int i = 1; i <= mission[0].players; i++)
		{
			if (player[i].researching > 0)
			{
				player[i].research_progress += 1;
				// If research is done..
				if (player[i].research_progress == player[i].research[player[i].researching].time)
				{
					player[i].research[player[i].researching].researched = true;
					player[i].researching = 0;
					player[i].researches_done += 1;
					player[i].research_progress = 0;
					if (i == 1)
					{
						Add_Message(8);
						sound[10].Play();
					}
				}
			}
		}
	}

	// Update planet populations, credits and metal
	if (clock_population.GetElapsedTime() > .25 / game_speed)
	{
		clock_population.Reset();
		total_income = 0;
		total_expenses = 0;
		total_metal_income = 0;
		for (int i = 1; i <= mission[0].starsystems; i++)
		{
			for (int j = 1; j <= starsystem[i].planets; j++)
			{
				p1 = &starsystem[i].planet[j];
				if (p1->owner > 0) // don't update neutral planets
				{
					// Increase population
					if (artifact_owner[3] == p1->owner)
						p1->population += (float)(.25 * p1->size * 1.15);
					else
						p1->population += (float)(.25 * p1->size);
					// Check if pop cap was hit
					if (artifact_owner[1] == p1->owner)
					{
						if (p1->population > p1->max_population * 1.15)
							p1->population = (float)(p1->max_population * 1.15);
					}
					else
						if (p1->population > p1->max_population)
							p1->population = p1->max_population;
					// Increase credits
					p1->income = p1->population / 100;
					if (player[p1->owner].research[15].researched)
						p1->income *= 1.3f;
					else if (player[p1->owner].research[14].researched)
						p1->income *= 1.2f;
					else if (player[p1->owner].research[13].researched)
						p1->income *= 1.1f;
					// Calculate expenses
					p1->expenses = (p1->fleets * .1f);
					player[p1->owner].credits += p1->income - p1->expenses;
					// Lower limit
					if (player[p1->owner].credits < 0)
						player[p1->owner].credits = 0;
					if (p1->owner == 1)
					{
						total_income += p1->income;
						total_expenses += p1->expenses;
					}
					// Increase metal
					p1->metal_income = .2f;
					if (p1->has_mine)
						p1->metal_income += .5f;
					if (player[p1->owner].research[19].researched)
						p1->metal_income *= 1.3f;
					else if (player[p1->owner].research[18].researched)
						p1->metal_income *= 1.2f;
					else if (player[p1->owner].research[17].researched)
						p1->metal_income *= 1.1f;
					player[p1->owner].metal += p1->metal_income;
					if (p1->owner == 1)
						total_metal_income += p1->metal_income;
				}
			}
		}
	}

	// Update planets
	if (clock_animation.GetElapsedTime() > .04 / game_speed)
	{
		clock_animation.Reset();
		for (int i = 1; i <= mission[0].starsystems; i++)
		{
			for (int j = 1; j <= starsystem[i].planets; j++)
			{
				p1 = &starsystem[i].planet[j];
				// Update moon
				if (p1->has_moon)
				{
					p1->moon_angle -= .5;
					if (p1->moon_angle == 0)
						p1->moon_angle = 360;
				}
				if (p1->owner > 0)
				{
					// Update turret
					//if (p1->has_turret == 1)
					//{
					//	if (p1->turret_target_planet > 0)
					//	{
					//		p1->turret_angle = atan2(p1->position_turret.y-starsystem[p1->turret_target_system].planet[p1->turret_target_planet].fleet[1].position.y,p1->position_turret.x-starsystem[p1->turret_target_system].planet[p1->turret_target_planet].fleet[1].position.x);
					//		if (sound[8].GetStatus() == sf::Sound::Stopped)
					//			sound[8].Play();
					//		if (clock_turret.GetElapsedTime() > 1)
					//		{
					//			if (player[p1->owner].researched[9] == 1)
					//				starsystem[p1->turret_target_system].planet[p1->turret_target_planet].fleet[1].health -= 1;
					//			if (player[p1->owner].researched[10] == 1)
					//				starsystem[p1->turret_target_system].planet[p1->turret_target_planet].fleet[1].health -= 1;
					//			if (player[p1->owner].researched[11] == 1)
					//				starsystem[p1->turret_target_system].planet[p1->turret_target_planet].fleet[1].health -= 1;
					//			if (starsystem[p1->turret_target_system].planet[p1->turret_target_planet].fleet[1].health == 0)
					//			{
					//				starsystem[p1->turret_target_system].planet[p1->turret_target_planet].fleet[1].has_target = 0;
					//				Add_Explosion(starsystem[p1->turret_target_system].planet[p1->turret_target_planet].fleet[1].position);
					//			}
					//		}
					//		if (starsystem[p1->turret_target_system].planet[p1->turret_target_planet].fleet[1].has_target == 0 || sqrt(pow((double)p1->position_turret.x-starsystem[p1->turret_target_system].planet[p1->turret_target_planet].fleet[1].position.x,2)+pow((double)p1->position_turret.y-starsystem[p1->turret_target_system].planet[p1->turret_target_planet].fleet[1].position.y,2)) > 100)
					//			p1->turret_target_planet = 0;
					//	}
					//	else
					//	{
					//		for (int k = 1; k <= mission[0].starsystems; k++)
					//		{
					//			for (int l = 1; l <= starsystem[k].planets; l++)
					//			{
					//				if (starsystem[k].planet[l].fleet[1].has_target == 1 && starsystem[k].planet[l].owner != p1->owner && player[p1->owner].alignment[starsystem[k].planet[l].owner] <= 40)
					//				{
					//					if (sqrt(pow((double)p1->position_turret.x-starsystem[k].planet[l].fleet[1].position.x,2)+pow((double)p1->position_turret.y-starsystem[k].planet[l].fleet[1].position.y,2)) <= 100)
					//					{
					//						p1->turret_target_planet = l;
					//						p1->turret_target_system = k;
					//						break;
					//					}
					//				}
					//			}
					//		}
					//	}
					//}
					// Update satellite
					if (p1->has_satellite)
					{
						p1->satellite_angle += 1;
						if (p1->satellite_angle == 360)
							p1->satellite_angle = 0;
						p1->position_satellite.x = (float)(p1->position.x + p1->size * 40 * cos((double)p1->satellite_angle * PI/180));
						p1->position_satellite.y = (float)(p1->position.y + p1->size * 40 * sin((double)p1->satellite_angle * PI/180));
					}
				}
			}
		}
		if (clock_turret.GetElapsedTime() > 1)
			clock_turret.Reset();
	}

	// Update fleets
	if (clock_fleet.GetElapsedTime() > .025)
	{
		clock_fleet.Reset();
		for (int k = 0; k <= 199; k++)
		{
			if (fleet[k].built)
			{
				// Orbiting
				if (fleet[k].status == FLEET_ORBITING)
				{
					// Orbit planet
					fleet[k].angle -= 1;
					if (fleet[k].angle == -1)
						fleet[k].angle = 359;
					fleet[k].speed.x = (float)cos((double)(fleet[k].angle - 90) * PI/180) * player[fleet[k].owner].fleet_speed;
					fleet[k].speed.y = (float)sin((double)(fleet[k].angle - 90) * PI/180) * player[fleet[k].owner].fleet_speed;
					fleet[k].position += fleet[k].speed;
					// Check for dogfight enemies
					for (int n = 0; n <= 199; n++)
					{
						if (fleet[n].built && fleet[n].owner != fleet[k].owner)
						{
							if (sqrt(pow((double)fleet[k].position.x-fleet[n].position.x,2)+pow((double)fleet[k].position.y-fleet[n].position.y,2)) <= 200)
							{
								starsystem[fleet[k].target_system].planet[fleet[k].target_planet].fleets -= 1;
								fleet[k].last_status = FLEET_TRAVELLING;
								fleet[k].last_target_system = fleet[k].target_system;
								fleet[k].last_target_planet = fleet[k].target_planet;
								fleet[k].status = FLEET_DOGFIGHT;
								fleet[k].target_fleet = n;
								fleet[k].angle -= 90;
								// also set enemy fleet
								if (fleet[n].status != FLEET_DOGFIGHT)
								{
									if (fleet[n].status == FLEET_ORBITING)
										starsystem[fleet[n].target_system].planet[fleet[n].target_planet].fleets -= 1;
									fleet[n].last_status = FLEET_TRAVELLING;
									fleet[n].last_target_system = fleet[n].target_system;
									fleet[n].last_target_planet = fleet[n].target_planet;
									fleet[n].status = FLEET_DOGFIGHT;
									fleet[n].target_fleet = k;
								}
								break;
							}
						}
					}
				}
				// Dogfight
				if (fleet[k].status == FLEET_DOGFIGHT)
				{
					fleet[k].target_angle = (float)(180+(180/PI)*(atan2((double)fleet[k].position.y-fleet[fleet[k].target_fleet].position.y,(double)fleet[k].position.x-fleet[fleet[k].target_fleet].position.x)));

					// Update angle
					if (fleet[k].target_angle != fleet[k].angle)
					{
						// Decide which direction to turn
						float turn_rate = player[fleet[k].owner].fleet_turnrate * 2;
						if (abs(fleet[k].target_angle - fleet[k].angle) < player[fleet[k].owner].fleet_turnrate * 2)
							turn_rate = abs(fleet[k].target_angle - fleet[k].angle);
						if (abs(fleet[k].target_angle - fleet[k].angle) <= 180)
							if (fleet[k].target_angle < fleet[k].angle)
								fleet[k].angle -= turn_rate;
							else
								fleet[k].angle += turn_rate;
						else
							if (fleet[k].target_angle < fleet[k].angle)
								fleet[k].angle += turn_rate;
							else
								fleet[k].angle -= turn_rate;

						if (fleet[k].angle > 359)
							fleet[k].angle -= 360;
						if (fleet[k].angle < 0)
							fleet[k].angle += 360;
					}
					// Move
					fleet[k].speed.x = (float)cos((double)(fleet[k].angle) * PI/180) * player[fleet[k].owner].fleet_speed;
					fleet[k].speed.y = (float)sin((double)(fleet[k].angle) * PI/180) * player[fleet[k].owner].fleet_speed;
					fleet[k].position += fleet[k].speed;
					// Check for shooting distance
					if (sqrt(pow((double)fleet[k].position.x-fleet[fleet[k].target_fleet].position.x,2)+pow((double)fleet[k].position.y-fleet[fleet[k].target_fleet].position.y,2)) <= 100)
					{
						if (fleet[k].target_angle == fleet[k].angle)
						{
							fleet[k].shooting = true;
							fleet[fleet[k].target_fleet].health -= player[fleet[k].owner].fleet_damage;
							if (sound[8].GetStatus() == sf::Sound::Stopped)
								sound[8].Play();
						}
						else
							fleet[k].shooting = false;
						// Destroy attacked fleet
						if (fleet[fleet[k].target_fleet].health <= 0)
						{
							fleet[fleet[k].target_fleet].built = false;
							Add_Explosion(fleet[fleet[k].target_fleet].position);
							// Return to old status
							fleet[k].status = fleet[k].last_status;
							fleet[k].target_system = fleet[k].last_target_system;
							fleet[k].target_planet = fleet[k].last_target_planet;
							fleet[k].shooting = false;
							fleet[k].target_angle = (float)(180+(180/PI)*(atan2((double)fleet[k].position.y-starsystem[fleet[k].target_system].planet[fleet[k].target_planet].position.y,(double)fleet[k].position.x-starsystem[fleet[k].target_system].planet[fleet[k].target_planet].position.x)));
						}
					}
				}
				else if (fleet[k].status == FLEET_TRAVELLING)
				{
					// Check if it hit the planet
					if (!((int)(fleet[k].position.x) >= starsystem[fleet[k].target_system].planet[fleet[k].target_planet].position.x-5 &&
						(int)(fleet[k].position.x) <= starsystem[fleet[k].target_system].planet[fleet[k].target_planet].position.x+5 &&
						(int)(fleet[k].position.y) >= starsystem[fleet[k].target_system].planet[fleet[k].target_planet].position.y-5 &&
						(int)(fleet[k].position.y) <= starsystem[fleet[k].target_system].planet[fleet[k].target_planet].position.y+5))
					{
						// Update angle
						if (fleet[k].target_angle != fleet[k].angle)
						{
							fleet[k].target_angle = (float)(180+(180/PI)*(atan2((double)fleet[k].position.y-starsystem[fleet[k].target_system].planet[fleet[k].target_planet].position.y,(double)fleet[k].position.x-starsystem[fleet[k].target_system].planet[fleet[k].target_planet].position.x)));

							// Decide which direction to turn
							float turn_rate = player[fleet[k].owner].fleet_turnrate * 2;
							if (abs(fleet[k].target_angle - fleet[k].angle) < player[fleet[k].owner].fleet_turnrate * 2)
								turn_rate = abs(fleet[k].target_angle - fleet[k].angle);
							if (abs(fleet[k].target_angle - fleet[k].angle) <= 180)
								if (fleet[k].target_angle < fleet[k].angle)
									fleet[k].angle -= turn_rate;
								else
									fleet[k].angle += turn_rate;
							else
								if (fleet[k].target_angle < fleet[k].angle)
									fleet[k].angle += turn_rate;
								else
									fleet[k].angle -= turn_rate;

							if (fleet[k].angle > 359)
								fleet[k].angle -= 360;
							if (fleet[k].angle < 0)
								fleet[k].angle += 360;
						}
						// normal speed for same system
						if (fleet[k].last_target_system == fleet[k].target_system)
						{
							fleet[k].speed.x = (float)cos((double)(fleet[k].angle) * PI/180) * player[fleet[k].owner].fleet_speed;
							fleet[k].speed.y = (float)sin((double)(fleet[k].angle) * PI/180) * player[fleet[k].owner].fleet_speed;
						}
						else // faster speed for different system
						{
							fleet[k].speed.x = (float)cos((double)(fleet[k].angle) * PI/180) * 2 * player[fleet[k].owner].fleet_speed;
							fleet[k].speed.y = (float)sin((double)(fleet[k].angle) * PI/180) * 2 * player[fleet[k].owner].fleet_speed;
						}
						fleet[k].position += fleet[k].speed;
					}
					else // If it has arrived ..
					{
						// .. decrease its population if its population belongs to someone else
						if (starsystem[fleet[k].target_system].planet[fleet[k].target_planet].owner != fleet[k].owner)
						{
							// decrease alignment of player being attacked towards attacking player
							player[starsystem[fleet[k].target_system].planet[fleet[k].target_planet].owner].alignment[fleet[k].owner] -= 10;
							if (player[starsystem[fleet[k].target_system].planet[fleet[k].target_planet].owner].alignment[fleet[k].owner] < 0)
								player[starsystem[fleet[k].target_system].planet[fleet[k].target_planet].owner].alignment[fleet[k].owner] = 0;
							// decrease the attacking fleet size if the target planet has a defense satellite
							if (starsystem[fleet[k].target_system].planet[fleet[k].target_planet].has_satellite == 1)
							{
								starsystem[fleet[k].target_system].planet[fleet[k].target_planet].satellite_health -= 1;
								// Explode fleet
								fleet[k].built = false;
								Add_Explosion(fleet[k].position);
								// Explode satellite
								if (starsystem[fleet[k].target_system].planet[fleet[k].target_planet].satellite_health == 0)
								{
									starsystem[fleet[k].target_system].planet[fleet[k].target_planet].has_satellite = false;
									Add_Explosion(starsystem[fleet[k].target_system].planet[fleet[k].target_planet].position_satellite);
								}
							}
							// destroy attacking fleet if attacking planet has fleet stationed
							//if (p1->fleet[k].built && starsystem[p1->fleet[k].target_system].planet[p1->fleet[k].target_planet].fleets > 0)
							//{
							//	starsystem[p1->fleet[k].target_system].planet[p1->fleet[k].target_planet].fleets -= 1;
							//	for (int l = 1; l <= 10; l++)
							//	{
							//		if (starsystem[p1->fleet[k].target_system].planet[p1->fleet[k].target_planet].fleet[l].built)
							//		{
							//			starsystem[p1->fleet[k].target_system].planet[p1->fleet[k].target_planet].fleet[l].built = false;
							//			Add_Explosion(starsystem[p1->fleet[k].target_system].planet[p1->fleet[k].target_planet].fleet[l].position);
							//			break;
							//			break;
							//		}
							//	}
							//	p1->fleet[k].built = false;
							//	p1->fleets -= 1;
							//	Add_Explosion(p1->fleet[k].position);
							//}
							// if defenses were survived i.e. fleet is still alive then decrease the population
							if (fleet[k].built)
							{
								if (artifact_owner[4] == fleet[k].owner)
									starsystem[fleet[k].target_system].planet[fleet[k].target_planet].population -= player[fleet[k].owner].fleet_damage * 1.15f;
								else
									starsystem[fleet[k].target_system].planet[fleet[k].target_planet].population -= player[fleet[k].owner].fleet_damage * 25;
								sound[2].Play();
								fleet[k].built = false;
								Add_Explosion(fleet[k].position);
							}
							// in case the population is zero take over the planet
							if (starsystem[fleet[k].target_system].planet[fleet[k].target_planet].population <= 0)
							{
								// Deselect planet if selected
								if (starsystem[fleet[k].target_system].planet[fleet[k].target_planet].selected)
								{
									starsystem[fleet[k].target_system].planet[fleet[k].target_planet].selected = false;
									planet_selected_count = 0;
									planet_selected = 0;
									system_selected = 0;
								}
								// Reset population
								starsystem[fleet[k].target_system].planet[fleet[k].target_planet].population = 0;
								// Update stats
								player[starsystem[fleet[k].target_system].planet[fleet[k].target_planet].owner].planets_lost += 1;
								player[starsystem[fleet[k].target_system].planet[fleet[k].target_planet].owner].planet_count -= 1;
								// Message in case an enemy player was destroyed
								if (player[starsystem[fleet[k].target_system].planet[fleet[k].target_planet].owner].planet_count == 0 && starsystem[fleet[k].target_system].planet[fleet[k].target_planet].owner > 1)
									Add_Message(6);
								// Set new owner and update stats
								starsystem[fleet[k].target_system].planet[fleet[k].target_planet].owner = fleet[k].owner;
								player[fleet[k].owner].planet_count += 1;
								player[fleet[k].owner].planets_conquered += 1;
								//  Explode turret
								if (starsystem[fleet[k].target_system].planet[fleet[k].target_planet].has_turret == true)
								{
									starsystem[fleet[k].target_system].planet[fleet[k].target_planet].has_turret = false;
									Add_Explosion(starsystem[fleet[k].target_system].planet[fleet[k].target_planet].position_turret);
								}
								// Explode mine
								if (starsystem[fleet[k].target_system].planet[fleet[k].target_planet].has_mine == true)
								{
									starsystem[fleet[k].target_system].planet[fleet[k].target_planet].has_mine = false;
									Add_Explosion(starsystem[fleet[k].target_system].planet[fleet[k].target_planet].position_mine);
								}
								// Reset other stuff
								//starsystem[fleet[k].target_system].planet[fleet[k].target_planet].auto_send = 0;
								starsystem[fleet[k].target_system].planet[fleet[k].target_planet].group = 0;
								starsystem[fleet[k].target_system].planet[fleet[k].target_planet].attacked = 0; // not by this attacker at least ..
								// but lets check everybody else
								for (int n = 0; n <= 199; n++)
								{
									if (fleet[n].target_system == fleet[k].target_system && fleet[n].target_planet == fleet[k].target_planet && starsystem[fleet[n].target_system].planet[fleet[n].target_planet].owner != starsystem[fleet[k].target_system].planet[fleet[k].target_planet].owner && fleet[n].status == FLEET_TRAVELLING)
									{
										starsystem[fleet[k].target_system].planet[fleet[k].target_planet].attacked = true;
										break;
									}
								}
								// If planet has an artifact it will now be known to the player or AI
								if (player[fleet[k].owner].artifact_discovered[starsystem[fleet[k].target_system].planet[fleet[k].target_planet].artifact] == 0 && starsystem[fleet[k].target_system].planet[fleet[k].target_planet].artifact > 0)
								{
									// set it to discovered
									player[fleet[k].owner].artifact_discovered[starsystem[fleet[k].target_system].planet[fleet[k].target_planet].artifact] = 1;
									player[fleet[k].owner].artifacts_discovered += 1;
									// event
									artifact_owner[starsystem[fleet[k].target_system].planet[fleet[k].target_planet].artifact] = fleet[k].owner;
									// Drop a message if the player found it
									if (fleet[k].owner == 1)
									{
										Add_Message(7);
										sound[11].Play();
										starsystem[fleet[k].target_system].planet[fleet[k].target_planet].has_event = true;
										starsystem[fleet[k].target_system].planet[fleet[k].target_planet].event_clock.Reset();
										starsystem[fleet[k].target_system].planet[fleet[k].target_planet].event_radius = starsystem[fleet[k].target_system].planet[fleet[k].target_planet].size*33;
										event_position = starsystem[fleet[k].target_system].planet[fleet[k].target_planet].position;
									}
									if (starsystem[fleet[k].target_system].planet[fleet[k].target_planet].artifact == 5)
									{
										for (int k = 1; k <= 4; k++)
										{
											player[k].fleet_range = 600;
										}
										player[artifact_owner[5]].fleet_range = 690;
									}
								}
								sound[1].Play();
							}
						}
						else // .. station fleet there
						{
							fleet[k].built = false;
							Add_Fleet(fleet[k].target_system,fleet[k].target_planet);
							sound[2].Play();
						}
						// Set planet to not being attacked
						starsystem[fleet[k].target_system].planet[fleet[k].target_planet].attacked = false;
						// recheck if attacked so the computer AIs know they have to react
						for (int m = 0; m <= 199; m++)
						{
							if (fleet[m].built)
							{
								if (fleet[m].target_system == fleet[k].target_system && fleet[m].target_planet == fleet[k].target_planet && starsystem[fleet[m].target_system].planet[fleet[m].target_planet].owner != starsystem[fleet[k].target_system].planet[fleet[k].target_planet].owner && fleet[m].status == FLEET_TRAVELLING)
									starsystem[fleet[k].target_system].planet[fleet[k].target_planet].attacked = true;
							}
						}
						// Enable fleet when autosend is on
						//if (p1->auto_send == 1)
						//{
						//	if (p1->fleets > 0)
						//	{
						//		p1->fleet[k].position = p1->position;
						//		p1->fleet[k].health = 25;
						//		player[p1->owner].fleets_built += 1;
						//		p1->fleet[k].has_target = 1;
						//		p1->fleets -= 1;
						//	}
						//}
					}
				}
			}
		}
	}

	Game_AI();
}

void Game_AI()
{

	// AI Research
	if (sf::Randomizer::Random(0, ai_delay) == 0)
	{
		for (int i = 2; i <= mission[0].players; i++)
		{
			if (player[i].researching == 0)
			{
				if (!player[i].research[1].researched || !player[i].research[5].researched || !player[i].research[9].researched || !player[i].research[13].researched || !player[i].research[17].researched)
				{
					if (!player[i].research[1].researched && player[i].credits >= player[i].research[1].costs)
					{
						player[i].researching = 1;
						player[i].credits -= player[i].research[1].costs;
					}
					if (!player[i].research[5].researched && player[i].credits >= player[i].research[5].costs)
					{
						player[i].researching = 5;
						player[i].credits -= player[i].research[5].costs;
					}
					if (!player[i].research[9].researched && player[i].credits >= player[i].research[9].costs)
					{
						player[i].researching = 9;
						player[i].credits -= player[i].research[9].costs;
					}
					if (!player[i].research[13].researched && player[i].credits >= player[i].research[13].costs)
					{
						player[i].researching = 13;
						player[i].credits -= player[i].research[13].costs;
					}
					if (!player[i].research[17].researched && player[i].credits >= player[i].research[17].costs)
					{
						player[i].researching = 17;
						player[i].credits -= player[i].research[17].costs;
					}
				}
				else if (!player[i].research[2].researched || !player[i].research[6].researched || !player[i].research[10].researched || !player[i].research[14].researched || !player[i].research[18].researched)
				{
					if (!player[i].research[2].researched && player[i].credits >= player[i].research[2].costs)
					{
						player[i].researching = 2;
						player[i].credits -= player[i].research[2].costs;
					}
					if (!player[i].research[6].researched && player[i].credits >= player[i].research[6].costs)
					{
						player[i].researching = 6;
						player[i].credits -= player[i].research[6].costs;
					}
					if (!player[i].research[10].researched && player[i].credits >= player[i].research[10].costs)
					{
						player[i].researching = 10;
						player[i].credits -= player[i].research[10].costs;
					}
					if (!player[i].research[14].researched && player[i].credits >= player[i].research[14].costs)
					{
						player[i].researching = 14;
						player[i].credits -= player[i].research[14].costs;
					}
					if (!player[i].research[18].researched && player[i].credits >= player[i].research[18].costs)
					{
						player[i].researching = 18;
						player[i].credits -= player[i].research[18].costs;
					}
				}
				else if (!player[i].research[3].researched || !player[i].research[7].researched || !player[i].research[11].researched || !player[i].research[15].researched || !player[i].research[19].researched)
				{
					if (!player[i].research[3].researched && player[i].credits >= player[i].research[3].costs)
					{
						player[i].researching = 3;
						player[i].credits -= player[i].research[3].costs;
					}
					if (!player[i].research[7].researched && player[i].credits >= player[i].research[7].costs)
					{
						player[i].researching = 7;
						player[i].credits -= player[i].research[7].costs;
					}
					if (!player[i].research[11].researched && player[i].credits >= player[i].research[11].costs)
					{
						player[i].researching = 11;
						player[i].credits -= player[i].research[11].costs;
					}
					if (!player[i].research[15].researched && player[i].credits >= player[i].research[15].costs)
					{
						player[i].researching = 15;
						player[i].credits -= player[i].research[15].costs;
					}
					if (!player[i].research[19].researched && player[i].credits >= player[i].research[19].costs)
					{
						player[i].researching = 19;
						player[i].credits -= player[i].research[19].costs;
					}
				}
			}
		}
	}

	int temp_distance, temp_system, temp_planet;

	for (int i = 1; i <= mission[0].starsystems; i++)
	{
		for (int j = 1; j <= starsystem[i].planets; j++)
		{
			if (starsystem[i].planet[j].owner > 1) // only check AI controlled planets
			{
				p1 = &starsystem[i].planet[j];

				// Build mine
				if (player[p1->owner].credits >= CREDITS_MINE && player[p1->owner].metal >= METAL_MINE && !p1->has_mine)
				{
					if (sf::Randomizer::Random(0, ai_delay) == 0)
					{
						for (int k = 0; k < starsystem[i].rocks; k++)
						{
							if (sqrt(pow((double)p1->position.x-starsystem[i].rock[k].position.x,2)+pow((double)p1->position.y-starsystem[i].rock[k].position.y,2)) <= player[p1->owner].fleet_range && !starsystem[i].rock[k].has_mine)
							{
								p1->has_mine = 1;
								p1->position_mine = starsystem[i].rock[k].position;
								player[p1->owner].credits -= CREDITS_MINE;
								player[p1->owner].metal -= METAL_MINE;
								break;
							}
						}
					}
				}

				// Build satellite
				if (player[p1->owner].credits >= CREDITS_SATELLITE && player[p1->owner].metal >= METAL_SATELLITE && p1->attacked && player[p1->owner].research[5].researched && !p1->has_satellite)
				{
					if (sf::Randomizer::Random(0, ai_delay) == 0)
					{
						p1->has_satellite = 1;
						player[p1->owner].credits -= CREDITS_SATELLITE;
						player[p1->owner].metal -= METAL_SATELLITE;
						p1->satellite_health = 1;
						if (player[p1->owner].research[6].researched == 1)
							p1->satellite_health += 1;
						if (player[p1->owner].research[7].researched == 1)
							p1->satellite_health += 1;
					}
				}

				// Build turret
				//if (player[p1->owner].credits >= CREDITS_TURRET && player[p1->owner].metal >= METAL_TURRET && p1->attacked && player[p1->owner].research[9].researched && !p1->has_turret)
				//{
				//	if (sf::Randomizer::Random(0, ai_delay) == 0)
				//	{
				//		for (int k = 1; k <= starsystem[i].planets; k++)
				//		{
				//			if (starsystem[i].planet[k].owner != p1->owner)
				//			{
				//				for (int l = 1; l <= 10; l++)
				//				{
				//					if (starsystem[i].planet[k].fleet[l].target_planet == j)
				//					{
				//						p1->position_turret.x = (p1->position.x + starsystem[i].planet[k].position.x)/2 + sf::Randomizer::Random(-50,50);
				//						p1->position_turret.y = (p1->position.y + starsystem[i].planet[k].position.y)/2 + sf::Randomizer::Random(-50,50);
				//						p1->has_turret = true;
				//						player[p1->owner].credits -= CREDITS_TURRET;
				//						player[p1->owner].metal -= METAL_TURRET;
				//						break;
				//					}
				//				}
				//			}
				//		}
				//	}
				//}

				// Build fleet
				if (player[p1->owner].credits >= CREDITS_FLEET && player[p1->owner].metal >= METAL_FLEET && p1->fleets <= 10)
				{
					if (sf::Randomizer::Random(0, ai_delay) == 0)
					{
						player[p1->owner].credits -= CREDITS_FLEET;
						player[p1->owner].metal -= METAL_FLEET;
						Add_Fleet(i,j);
					}
				}

				// Send fleet
				if (p1->fleets > 0)
				{
					if (sf::Randomizer::Random(0, ai_delay) == 0)
					{
						temp_distance = 20000;
						temp_system = 0;
						temp_planet = 0;
						// first target is the closest planet in range that doesn't belong to the AI
						for (int k = 1; k <= mission[0].starsystems; k++)
						{
							for (int l = 1; l <= starsystem[k].planets; l++)
							{
								if (i != k || j != l)
								{
									if (sqrt(pow((double)p1->position.x-starsystem[k].planet[l].position.x,2)+pow((double)p1->position.y-starsystem[k].planet[l].position.y,2)) < temp_distance && starsystem[k].planet[l].owner != p1->owner && (sqrt(pow((double)p1->position.x-starsystem[k].planet[l].position.x,2)+pow((double)p1->position.y-starsystem[k].planet[l].position.y,2)) <= player[p1->owner].fleet_range || k != i))
									{
										temp_system = k;
										temp_planet = l;
										temp_distance = (int)(sqrt(pow((double)p1->position.x-starsystem[k].planet[l].position.x,2)+pow((double)p1->position.y-starsystem[k].planet[l].position.y,2)));
									}
								}
							}
						}
						// now check if one of the planets in range has a known artifact and prioritize
						for (int k = 1; k <= mission[0].starsystems; k++)
						{
							for (int l = 1; l <= starsystem[k].planets; l++)
							{
								if (i != k || j != l)
								{
									if (starsystem[k].planet[l].artifact > 0)
									{
										if ((sqrt(pow((double)p1->position.x-starsystem[k].planet[l].position.x,2)+pow((double)p1->position.y-starsystem[k].planet[l].position.y,2)) <= player[p1->owner].fleet_range || k != i) && starsystem[k].planet[l].owner != p1->owner && player[p1->owner].artifact_discovered[starsystem[k].planet[l].artifact])
										{
											temp_system = k;
											temp_planet = l;
										}
									}
								}
							}
						}
						// now check if one of the planets in range belongs to us already, has a population of less than 50%, and is being attacked (only in normal and hard difficulty)
						if (mission[0].difficulty > 1) 
						{
							for (int k = 1; k <= mission[0].starsystems; k++)
							{
								for (int l = 1; l <= starsystem[k].planets; l++)
								{
									//if (!(i == k && j == l))
									if (i != k || j != l)
									{
										if (starsystem[k].planet[l].owner == p1->owner && (sqrt(pow((double)p1->position.x-starsystem[k].planet[l].position.x,2)+pow((double)p1->position.y-starsystem[k].planet[l].position.y,2)) <= player[p1->owner].fleet_range || k != i) && starsystem[k].planet[l].attacked && starsystem[k].planet[l].population < starsystem[k].planet[l].max_population * .50)
										{
											temp_system = k;
											temp_planet = l;
										}
									}
								}
							}
						}
						// check for phase lane disruption or peace condition with targeted player
						if (!starsystem[temp_system].planet[temp_planet].has_disruption && player[p1->owner].alignment[starsystem[temp_system].planet[temp_planet].owner] <= 40 && starsystem[i].nearby[temp_system])
							// Send!
							Send_Fleet(i,j,temp_system,temp_planet,0);
					}
				}
			}
		}
	}

}

void Draw_Map()
{

	// Draw background
	App.SetView(ViewDefault);
	int fade = (int)(511 / (1/zoom));
	if (fade > 255)
		fade = 255;
	if (fade < 0)
		fade = 0;
	graphic[5].SetColor(sf::Color(255,255,255,fade));
	App.Draw(graphic[5]);

	App.SetView(View);

	// Draw stars
	for (int i = 1; i <= mission[0].starsystems; i++)
	{
		if (zoom > .25)
		{
			graphic[16].SetPosition(starsystem[i].planet[0].position);
			graphic[16].SetRotation(starsystem[i].planet[0].rotation);
			App.Draw(graphic[16]);
		}
		else
		{
			App.Draw(sf::Shape::Circle(starsystem[i].planet[0].position, 50 * starsystem[i].planet[0].size, sf::Color(255,127,0),2,sf::Color(255,255,255)));
		}
	}

	// Draw rocks
	if (zoom > .25)
	{
		for (int k = 1; k <= mission[0].starsystems; k++)
		{
			for (int l = 0; l <= starsystem[k].rocks; l++)
			{
				graphic[31].SetSubRect(sf::IntRect(96 * starsystem[k].rock[l].number, 0, 96 + 96 * starsystem[k].rock[l].number, 93));
				graphic[31].SetPosition(starsystem[k].rock[l].position);
				graphic[31].SetRotation((float)starsystem[k].rock[l].rotation);
				graphic[31].SetScale((float)(starsystem[k].rock[l].size * .2), (float)(starsystem[k].rock[l].size * .2));
				graphic[31].SetCenter(48, 48);
				App.Draw(graphic[31]);
				if (starsystem[k].rock[l].hovered)
					App.Draw(sf::Shape::Circle(starsystem[k].rock[l].position, 15, sf::Color(255,255,255,0), 1, sf::Color(150,150,150)));
			}
		}
	}

	// Draw star connection lines (when a star is hovered)
	for (int k = 1; k <= mission[0].starsystems; k++)
	{
		if (starsystem[k].planet[0].hovered == 1)
		{
			for (int i = 1; i <= mission[0].starsystems; i++)
			{
				for (int j = 1; j <= mission[0].starsystems; j++)
				{
					if (i != j && starsystem[i].nearby[j])
						App.Draw(sf::Shape::Line(starsystem[i].planet[0].position,starsystem[j].planet[0].position, (float)(zoom > .25 ? 5 : 10), sf::Color(255,255,255,100),0,sf::Color(0,0,0)));
				}
			}
		}
	}

	// Draw planet connection lines
	for (int k = 1; k <= mission[0].starsystems; k++)
	{
		for (int i = 1; i <= starsystem[k].planets; i++)
		{
			for (int j = 1; j <= starsystem[k].planets; j++)
			{
				for (int l = 1; l <= mission[0].players; l++)
				{
					if (i != j && sqrt(pow((double)starsystem[k].planet[i].position.x-starsystem[k].planet[j].position.x,2)+pow((double)starsystem[k].planet[i].position.y-starsystem[k].planet[j].position.y,2)) <= player[l].fleet_range)
					{
						if (zoom > .25)
						{
							if (starsystem[k].planet[i].owner == 1)
								App.Draw(sf::Shape::Line(starsystem[k].planet[i].position,starsystem[k].planet[j].position,2,sf::Color(0,125,0,100),1,sf::Color(0,75,0,100)));
							//else if (starsystem[k].planet[i].owner == 2)
							//	App.Draw(sf::Shape::Line(starsystem[k].planet[i].position,starsystem[k].planet[j].position,2,sf::Color(125,0,0,100),1,sf::Color(75,0,0,100)));
							//else if (starsystem[k].planet[i].owner == 3)
							//	App.Draw(sf::Shape::Line(starsystem[k].planet[i].position,starsystem[k].planet[j].position,2,sf::Color(0,0,125,100),1,sf::Color(0,0,75,100)));
							//else if (starsystem[k].planet[i].owner == 4)
							//	App.Draw(sf::Shape::Line(starsystem[k].planet[i].position,starsystem[k].planet[j].position,2,sf::Color(125,125,0,100),1,sf::Color(75,75,0,100)));
						}
						else
						{
							if (starsystem[k].planet[i].owner == 1)
								App.Draw(sf::Shape::Line(starsystem[k].planet[i].position,starsystem[k].planet[j].position,6,sf::Color(0,200,0,100),1,sf::Color(0,150,0,100)));
							//else if (starsystem[k].planet[i].owner == 2)
							//	App.Draw(sf::Shape::Line(starsystem[k].planet[i].position,starsystem[k].planet[j].position,6,sf::Color(200,0,0,100),1,sf::Color(150,0,0,100)));
							//else if (starsystem[k].planet[i].owner == 3)
							//	App.Draw(sf::Shape::Line(starsystem[k].planet[i].position,starsystem[k].planet[j].position,6,sf::Color(0,0,200,100),1,sf::Color(0,0,150,100)));
							//else if (starsystem[k].planet[i].owner == 4)
							//	App.Draw(sf::Shape::Line(starsystem[k].planet[i].position,starsystem[k].planet[j].position,6,sf::Color(200,200,0,100),1,sf::Color(150,150,0,100)));
						}
					}
				}
			}
		}
	}

	// First layer (phase lanes and event circles)
	for (int i = 1; i <= mission[0].starsystems; i++)
	{
		for (int j = 1; j <= starsystem[i].planets; j++)
		{
			p1 = &starsystem[i].planet[j];
			if (zoom > .25)
			{
				// Event circle
				if (p1->has_event)
					App.Draw(sf::Shape::Circle(p1->position,(float)p1->event_radius, sf::Color(255,255,255,0),3,sf::Color(255,0,0,p1->event_fade)));
				// Show turret circle and line on hover
				if (p1->turret_hovered == 1 && p1->has_turret == 1) // && p1->owner == 1
				{
					App.Draw(sf::Shape::Circle(p1->position_turret, 15, sf::Color(255,255,255,0),1,sf::Color(150,150,150)));
					App.Draw(sf::Shape::Circle(p1->position_turret, 100, sf::Color(255,255,255,0),1,sf::Color(255,0,0)));
					App.Draw(sf::Shape::Line(p1->position_turret,p1->position,1,sf::Color(150,150,150),1,sf::Color(0,0,0)));
				}
				// Show mine circle and line on hover
				if (p1->has_mine == 1 && p1->owner == 1)
				{
					if (p1->mine_hovered == 1)
					{
						App.Draw(sf::Shape::Circle(p1->position_mine, 15, sf::Color(255,255,255,0),1,sf::Color(150,150,150)));
						App.Draw(sf::Shape::Line(p1->position_mine,p1->position,1,sf::Color(200,200,200),1,sf::Color(0,0,0)));
					}
					else
						App.Draw(sf::Shape::Line(p1->position_mine,p1->position,1,sf::Color(200,200,0),1,sf::Color(0,0,0)));
				}
				// Hover circle
				if ((p1->hovered && showResearch == -1 && showDiplomacy == -1) || p1->selected)
				{
					graphic[28].SetSubRect(sf::IntRect(0,0,155,155));
					graphic[28].SetPosition(p1->position);
					graphic[28].SetScale((float)(p1->size*.5),(float)(p1->size*.5));
					graphic[28].SetCenter(78, 78);
					App.Draw(graphic[28]);
					// Turret circle
					if (p1->has_turret)
					{
						App.Draw(sf::Shape::Line(p1->position_turret,p1->position,1,sf::Color(150,150,150),1,sf::Color(0,0,0)));
						App.Draw(sf::Shape::Circle(p1->position_turret, 15, sf::Color(255,255,255,0),1,sf::Color(150,150,150)));
					}
					// Mine circle
					if (p1->has_mine)
					{
						App.Draw(sf::Shape::Line(p1->position_mine,p1->position,1,sf::Color(150,150,150),1,sf::Color(0,0,0)));
						App.Draw(sf::Shape::Circle(p1->position_mine, 15, sf::Color(255,255,255,0),1,sf::Color(150,150,150)));
					}
				}
				else
				{
					if (p1->owner > 0)
					{
						graphic[28].SetSubRect(sf::IntRect(156*p1->owner,0,156*p1->owner+155,155));
						graphic[28].SetPosition(p1->position);
						graphic[28].SetScale((float)(p1->size*.5),(float)(p1->size*.5));
						graphic[28].SetCenter(78, 78);
						App.Draw(graphic[28]);
					}
				}
			}
			else
			{
				// Simple event circle
				if (p1->has_event)
					App.Draw(sf::Shape::Circle(p1->position,(float)p1->event_radius, sf::Color(255,255,255,0),6,sf::Color(255,0,0,p1->event_fade)));
			}
		}
	}

	// Second layer (planets, satellites, turrets, selection/hover circle, group, population bar)
	for (int i = 1; i <= mission[0].starsystems; i++)
	{
		for (int j = 1; j <= starsystem[i].planets; j++)
		{
			p1 = &starsystem[i].planet[j];
			if (zoom > .25)
			{
				// Draw planet
				graphic[0].SetSubRect(sf::IntRect(p1->owner * 144, 0, 143 + p1->owner * 144, 143));
				graphic[0].SetPosition(p1->position);
				graphic[0].SetScale((float)(p1->size * .5),(float)(p1->size * .5));
				graphic[0].SetRotation(p1->rotation);
				graphic[0].SetCenter(72,72);
				App.Draw(graphic[0]);
				// Draw satellite
				if (p1->has_satellite == 1)
				{
					graphic[1].SetPosition(p1->position_satellite);
					App.Draw(graphic[1]);
				}
				// Draw moon
				if (p1->has_moon == 1)
				{
					graphic[30].SetPosition((float)(p1->position.x + p1->size * 60 * cos((double)p1->moon_angle * PI/180)),(float)(p1->position.y + p1->size * 60 * sin((double)p1->moon_angle * PI/180)));
					App.Draw(graphic[30]);
				}
				// Draw turret
				//if (p1->has_turret == 1)
				//{
				//	graphic[9].SetRotation((float)((-p1->turret_angle+PI) * (180/PI)));
				//	graphic[9].SetPosition(p1->position_turret);
				//	App.Draw(graphic[9]);
				//	if (p1->turret_target_planet > 0)
				//	{
				//		if (p1->owner == 1)
				//			App.Draw(sf::Shape::Line(p1->position_turret,starsystem[p1->turret_target_system].planet[p1->turret_target_planet].fleet[1].position,1,sf::Color(0,150,0),1,sf::Color(0,0,0,0)));
				//		if (p1->owner == 2)
				//			App.Draw(sf::Shape::Line(p1->position_turret,starsystem[p1->turret_target_system].planet[p1->turret_target_planet].fleet[1].position,1,sf::Color(150,0,0),1,sf::Color(0,0,0,0)));
				//		if (p1->owner == 3)
				//			App.Draw(sf::Shape::Line(p1->position_turret,starsystem[p1->turret_target_system].planet[p1->turret_target_planet].fleet[1].position,1,sf::Color(0,0,150),1,sf::Color(0,0,0,0)));
				//		if (p1->owner == 4)
				//			App.Draw(sf::Shape::Line(p1->position_turret,starsystem[p1->turret_target_system].planet[p1->turret_target_planet].fleet[1].position,1,sf::Color(150,150,0),1,sf::Color(0,0,0,0)));
				//	}
				//	if (p1->turret_selected == 1)
				//		App.Draw(sf::Shape::Circle(p1->position_turret, 15, sf::Color(255,255,255,0),1,sf::Color(0,255,0)));
				//}
				// Draw mine
				if (p1->has_mine == 1)
				{
					graphic[34].SetPosition(p1->position_mine);
					App.Draw(graphic[34]);
				}
				// Draw ship range circle
				if (p1->selected)
					App.Draw(sf::Shape::Circle(p1->position,(float)player[p1->owner].fleet_range, sf::Color(255,255,255,0),2,sf::Color(0,255,0)));
				// Draw group number
				if (p1->group > 0)
				{
					sstr << p1->group;
					Draw_String(0,(int)(p1->position.x),(int)(p1->position.y+p1->size*40),15,sstr.str(),255,255,255,255,1);
				}
				// Draw disruption icon
				if (p1->has_disruption)
				{
					graphic[10].SetSubRect(sf::IntRect(64,0,95,31));
					graphic[10].SetPosition((float)p1->position.x - 40,(float)p1->position.y - 40);
					graphic[10].SetColor(sf::Color(255,255,255,150));
					graphic[10].SetScale(2.5f, 2.5f);
					App.Draw(graphic[10]);
					// revert
					graphic[10].SetColor(sf::Color(255,255,255));
					graphic[10].SetScale(1, 1);
					sstr << disruption_timer;
					Draw_String(0,(int)((int)p1->position.x+p1->size*40),(int)(p1->position.y-8),15,sstr.str(),255,255,255,255,0);
				}
				// Draw planet population and number of fleets
				if (p1->owner == 1)
				{
					App.Draw(sf::Shape::Line((float)p1->position.x-15,(float)p1->position.y,(float)p1->position.x+15,(float)p1->position.y,1,sf::Color(255,255,255),1,sf::Color(0,0,0,0)));
					sstr << (int)p1->population;
					Draw_String(0,(int)p1->position.x,(int)p1->position.y-23,20,sstr.str(),255,255,255,255,1);
					if (p1->fleets == 0)
					{
						sstr << p1->fleets;
						Draw_String(0,(int)p1->position.x,(int)p1->position.y-2,20,sstr.str(),255,0,0,255,1);
					}
					else
					{
						sstr << p1->fleets;
						Draw_String(0,(int)p1->position.x,(int)p1->position.y-2,20,sstr.str(),0,255,0,255,1);
					}
				}
				// Draw population bar
				//if ((p1->hovered || p1->selected) && showResearch == -1 && showDiplomacy == -1)
				//{
				//	if (p1->owner == 1)
				//	{
				//		Draw_Box((int)p1->position.x-10,(int)p1->position.y+10,20,4,0,0,150);
				//		App.Draw(sf::Shape::Rectangle((float)p1->position.x-10,(float)p1->position.y+10,(float)p1->position.x-9+((19/p1->max_population)*p1->population),(float)p1->position.y+14,sf::Color(0,206,209),0,sf::Color(0,206,209)));
				//	}
				//}
			}
			else
			{
				// Planet simple shape
				if (p1->owner == 0)
					App.Draw(sf::Shape::Circle(p1->position, p1->size*50, sf::Color(150,150,150,255),4,sf::Color(255,255,255)));
				if (p1->owner == 1)
					App.Draw(sf::Shape::Circle(p1->position, 50*p1->size, sf::Color(0,255,0,255),4,sf::Color(255,255,255)));
				if (p1->owner == 2)
					App.Draw(sf::Shape::Circle(p1->position, 50*p1->size, sf::Color(255,0,0,255),4,sf::Color(255,255,255)));
				if (p1->owner == 3)
					App.Draw(sf::Shape::Circle(p1->position, 50*p1->size, sf::Color(0,0,255,255),4,sf::Color(255,255,255)));
				if (p1->owner == 4)
					App.Draw(sf::Shape::Circle(p1->position, 50*p1->size, sf::Color(255,255,0,255),4,sf::Color(255,255,255)));
				// Selected simple shape
				if (p1->selected)
				{
					// white selection circle
					App.Draw(sf::Shape::Circle(p1->position, 80*p1->size, sf::Color(255,255,255,0),6,sf::Color(255,255,255)));
					// red ship range circle
					App.Draw(sf::Shape::Circle(p1->position,(float)player[p1->owner].fleet_range, sf::Color(255,255,255,0),6,sf::Color(0,255,0)));
				}
				// Hovered simple shape
				if (p1->hovered)
					// grey hover circle
					App.Draw(sf::Shape::Circle(p1->position,(float)80*p1->size, sf::Color(255,255,255,0),4,sf::Color(150,150,150)));
			}
		}
	}

	// Third layer (fleets, fleet size, turret circle, artifact star, population bar)
	for (int i = 1; i <= mission[0].starsystems; i++)
	{
		for (int j = 1; j <= starsystem[i].planets; j++)
		{
			p1 = &starsystem[i].planet[j];
			if (zoom > .25)
			{
				// Draw artifact star
				if (p1->artifact > 0 && player[1].artifact_discovered[p1->artifact] == 1)
				{
					graphic[11].SetPosition(p1->position.x,(float)p1->position.y-p1->size*33-25);
					App.Draw(graphic[11]);
				}
				// Draw planet name
				if (showPlanetNames == 1)
					Draw_String(0,(int)p1->position.x,(int)(p1->position.y-p1->size*40-20),20,p1->name,255,255,255,255,1);
			}
		}
	}

	// Phase lines
	for (int k = 0; k <= 199; k++)
	{
		if (fleet[k].built && fleet[k].status == FLEET_TRAVELLING)
		{
			if (zoom > .25)
			{
				if (fleet[k].owner == 1)
					App.Draw(sf::Shape::Line(fleet[k].position,starsystem[fleet[k].target_system].planet[fleet[k].target_planet].position,2,sf::Color(0,255,0),1,sf::Color(0,0,0)));
				if (fleet[k].owner == 2)
					App.Draw(sf::Shape::Line(fleet[k].position,starsystem[fleet[k].target_system].planet[fleet[k].target_planet].position,2,sf::Color(255,0,0),1,sf::Color(0,0,0)));
				if (fleet[k].owner == 3)
					App.Draw(sf::Shape::Line(fleet[k].position,starsystem[fleet[k].target_system].planet[fleet[k].target_planet].position,2,sf::Color(0,0,255),1,sf::Color(0,0,0)));
				if (fleet[k].owner == 4)
					App.Draw(sf::Shape::Line(fleet[k].position,starsystem[fleet[k].target_system].planet[fleet[k].target_planet].position,2,sf::Color(255,255,0),1,sf::Color(0,0,0)));
			}
			else
			{
				// Simple phase line
				if (fleet[k].owner == 1)
					App.Draw(sf::Shape::Line(fleet[k].position,starsystem[fleet[k].target_system].planet[fleet[k].target_planet].position,6,sf::Color(255,255,255),1,sf::Color(0,0,0)));
				if (fleet[k].owner == 2)
					App.Draw(sf::Shape::Line(fleet[k].position,starsystem[fleet[k].target_system].planet[fleet[k].target_planet].position,6,sf::Color(255,0,0),1,sf::Color(0,0,0)));
				if (fleet[k].owner == 3)
					App.Draw(sf::Shape::Line(fleet[k].position,starsystem[fleet[k].target_system].planet[fleet[k].target_planet].position,6,sf::Color(0,0,255),1,sf::Color(0,0,0)));
				if (fleet[k].owner == 4)
					App.Draw(sf::Shape::Line(fleet[k].position,starsystem[fleet[k].target_system].planet[fleet[k].target_planet].position,6,sf::Color(255,255,0),1,sf::Color(0,0,0)));
			}
		}
	}

	// Attack laser
	for (int k = 0; k <= 199; k++)
	{
		if (fleet[k].built && fleet[k].shooting)
		{
			if (fleet[k].owner == 1)
				App.Draw(sf::Shape::Line(fleet[k].position,fleet[fleet[k].target_fleet].position,1,sf::Color(0,255,0),1,sf::Color(0,0,0,0)));
			if (fleet[k].owner == 2)
				App.Draw(sf::Shape::Line(fleet[k].position,fleet[fleet[k].target_fleet].position,1,sf::Color(255,0,0),1,sf::Color(0,0,0,0)));
			if (fleet[k].owner == 31)
				App.Draw(sf::Shape::Line(fleet[k].position,fleet[fleet[k].target_fleet].position,1,sf::Color(0,0,255),1,sf::Color(0,0,0,0)));
			if (fleet[k].owner == 4)
				App.Draw(sf::Shape::Line(fleet[k].position,fleet[fleet[k].target_fleet].position,1,sf::Color(255,255,0),1,sf::Color(0,0,0,0)));
		}
	}
	// Fleets
	for (int k = 0; k <= 199; k++)
	{
		if (fleet[k].built)
		{
			if (zoom > .25)
			{
				if (fleet[k].status == FLEET_TRAVELLING || fleet[k].status == FLEET_DOGFIGHT)
				{
					graphic[6].SetSubRect(sf::IntRect(fleet[k].owner*44 - 44,0,43 + fleet[k].owner*44 - 44,48));
					graphic[6].SetPosition(fleet[k].position);
					graphic[6].SetScale(.25,.25);
					graphic[6].SetCenter(22,24);
					graphic[6].SetRotation((float)-fleet[k].angle);
					App.Draw(graphic[6]);
				}
				else if (fleet[k].status == FLEET_ORBITING)

				{
					graphic[6].SetSubRect(sf::IntRect(fleet[k].owner*44 - 44,0,43 + fleet[k].owner*44 - 44,48));
					graphic[6].SetPosition(fleet[k].position);
					graphic[6].SetScale(.25,.25);
					graphic[6].SetCenter(22,24);
					graphic[6].SetRotation((float)90-fleet[k].angle);
					App.Draw(graphic[6]);
				}
			}
			else
			{
				// Draw simple fleets
				if (fleet[k].owner == 1)
					App.Draw(sf::Shape::Circle(fleet[k].position,20,sf::Color(0,150,0),4,sf::Color(255,255,255)));
				if (fleet[k].owner == 2)
					App.Draw(sf::Shape::Circle(fleet[k].position,20,sf::Color(150,0,0),4,sf::Color(255,255,255)));
				if (fleet[k].owner == 3)
					App.Draw(sf::Shape::Circle(fleet[k].position,20,sf::Color(0,0,150),4,sf::Color(255,255,255)));
				if (fleet[k].owner == 4)
					App.Draw(sf::Shape::Circle(fleet[k].position,20,sf::Color(150,150,0),4,sf::Color(255,255,255)));
			}

			// Show fleet health on hover
			if (fleet[k].hovered) // && p1->owner == 1
			{
				App.Draw(sf::Shape::Circle(fleet[k].position, 15, sf::Color(255,255,255,0),1,sf::Color(150,150,150)));
				sstr << (int)fleet[k].health;
				Draw_String(0,(int)fleet[k].position.x,(int)fleet[k].position.y-20,15,sstr.str(),255,255,255,255,1);
			}
			else if (fleet[k].selected)
			{
				App.Draw(sf::Shape::Circle(fleet[k].position, 15, sf::Color(255,255,255,0),1,sf::Color(255,255,255)));
				sstr << (int)fleet[k].health;
				Draw_String(0,(int)fleet[k].position.x,(int)fleet[k].position.y-20,15,sstr.str(),255,255,255,255,1);
			}
		}
	}

	// Draw Explosions
	for (int i = 1; i < 51; i++)
	{
		if (explosion[i].active == 1)
		{
			graphic[29].SetSubRect(sf::IntRect((int)(explosion[i].frame/4) * -256 + explosion[i].frame * 64,
				(int)(explosion[i].frame/4) * 64,
				(int)(explosion[i].frame/4) * -256 + explosion[i].frame * 64 + 63,
				(int)(explosion[i].frame/4) * 64 + 63));
			graphic[29].SetCenter(31,31);
			graphic[29].SetPosition(explosion[i].position);
			App.Draw(graphic[29]);
		}
	}

	// Draw range circle when build turret cursor is selected
	if (active_cursor == CURSOR_TURRET)
		App.Draw(sf::Shape::Circle(mouse2.x,mouse2.y,100,sf::Color(255,255,255,0),1,sf::Color(255,0,0)));

}

void Draw_Interface()
{

	const sf::Input& Input = App.GetInput();

	App.SetView(ViewDefault);

	// Draw UI
	// Planets
	graphic[21].SetPosition((float)-8, (float)-8);
	App.Draw(graphic[21]);

	// Population
	graphic[21].SetPosition((float)210, (float)-8);
	App.Draw(graphic[21]);

	// Metal
	graphic[21].SetPosition((float)screen_w-433, (float)-8);
	App.Draw(graphic[21]);

	// Credits
	graphic[21].SetPosition((float)screen_w-213, (float)-8);
	App.Draw(graphic[21]);

	// Draw diplomacy button
	if (showDiplomacy == -1)
	{
		App.Draw(graphic[25]);
		if (mouse.x >= screen_wh-77 && mouse.x <= screen_wh+77 && mouse.y >= screen_h-34)
		{
			Draw_String(0,screen_wh,screen_h-65,20,"Open diplomacy screen",0,100,255,255,1);
			if (Input.IsMouseButtonDown(sf::Mouse::Left) && showDiplomacy == -1)
			{
				showDiplomacy = 1;
				showResearch = -1;
				sound[12].Play();
			}
		}
	}

	// Draw research button
	if (showResearch == -1)
	{
		App.Draw(graphic[2]);

		subrect.Left = (int)(graphic[2].GetPosition().x - graphic[2].GetSize().x/2);
		subrect.Top = (int)graphic[2].GetPosition().y;
		subrect.Right = (int)(graphic[2].GetPosition().x + graphic[2].GetSize().x/2);
		subrect.Bottom = (int)(graphic[2].GetPosition().y + graphic[2].GetSize().y);

		if (subrect.Contains(mouse.x, mouse.y))
		{
			if (player[1].researching == 0)
				Draw_String(0,screen_wh,40,20,"Open research screen",255,0,0,255,1);
			else
				Draw_String(0,screen_wh,65,20,"Open research screen",255,0,0,255,1);
			if (Input.IsMouseButtonDown(sf::Mouse::Left))
			{
				showResearch = 1;
				showDiplomacy = -1;
				sound[12].Play();
			}
		}
	}

	// Print framerate, planet count, metal and credits
	if (show_fps)
	{
		sstr << "FPS: " << framerate;
		Draw_String(0,4,screen_h-25, 20, sstr.str(),255,255,255,255,0);
	}
	sstr << "Planets: " << player[1].planet_count;
	Draw_String(0,12,4,20,sstr.str(),255,255,255,255,0);
	sstr << "Population: " << player[1].population;
	Draw_String(0,230,4,20,sstr.str(),255,255,255,255,0);
	sstr << "Metal: " << (int)(player[1].metal);
	Draw_String(0,screen_w-413,4,20,sstr.str(),255,255,255,255,0);
	sstr << "Credits: " << (int)(player[1].credits);
	Draw_String(0,screen_w-193,4,20,sstr.str(),255,255,255,255,0);

	// Draw credits changerate
	float bla = (float)(int)(total_income*10 - total_expenses*10);
	if ((int)(total_income*10 - total_expenses*10)/10 >= 0)
	{
		sstr << "+" << bla/10;
		Draw_String(0,screen_w-63,4,20,sstr.str(),0,255,0,255,0);
	}
	else
	{
		sstr << "-" << bla/10;
		Draw_String(0,screen_w-63,4,20,sstr.str(),255,0,0,255,0);
	}

	// Draw metal changerate
	bla = (float)(int)(total_metal_income*10);
	if ((int)(total_metal_income*10)/10 >= 0)
	{
		sstr << "+" << bla/10;
		Draw_String(0,screen_w-283,4,20,sstr.str(),0,255,0,255,0);
	}
	else
	{
		sstr << "-" << bla/10;
		Draw_String(0,screen_w-283,4,20,sstr.str(),255,0,0,255,0);
	}

	// Print all planet incomes
	if (mouse.x > screen_w - 208 && mouse.y < 37)
	{
		int z = 0;
		for (int i = 1; i <= mission[0].starsystems; i++)
		{
			for (int j = 1; j <= starsystem[i].planets; j++)
			{
				if (starsystem[i].planet[j].owner == 1)
				{
					//starsystem[i].planet[j].name.SetPosition((float)screen_w-195,(float)45+z*20);
					//App.Draw(starsystem[i].planet[j].name);
					sstr << starsystem[i].planet[j].name;
					Draw_String(0,(int)screen_w-195,(int)45+z*20, 20,sstr.str(),0,255,0,255,0);
					bla = (float)(int)(starsystem[i].planet[j].income*10 - starsystem[i].planet[j].expenses*10);
					if ((int)(starsystem[i].planet[j].income*10 - starsystem[i].planet[j].expenses*10)/10 >= 0)
					{
						sstr << "+" << bla/10;
						Draw_String(0,screen_w-90,45+z*20, 20,sstr.str(),0,255,0,255,0);
					}
					else
					{
						sstr << "-" << bla/10;
						Draw_String(0,screen_w-90,45+z*20, 20,sstr.str(),255,0,0,255,0);
					}
					z += 1;
				}
			}
		}
	}

	// Print all planet metal incomes
	if (mouse.x > screen_w - 438 && mouse.x < screen_w - 233 && mouse.y < 37)
	{
		int z = 0;
		for (int i = 1; i <= mission[0].starsystems; i++)
		{
			for (int j = 1; j <= starsystem[i].planets; j++)
			{
				if (starsystem[i].planet[j].owner == 1)
				{
					sstr << starsystem[i].planet[j].name;
					Draw_String(0,(int)screen_w-425,(int)45+z*20, 20,sstr.str(),0,255,0,255,0);
					bla = (float)(int)(starsystem[i].planet[j].metal_income*10);
					if ((int)(starsystem[i].planet[j].metal_income * 10)/10 >= 0)
					{
						sstr << "+" << bla/10;
						Draw_String(0,screen_w-320,45+z*20, 20,sstr.str(),0,255,0,255,0);
					}
					else
					{
						sstr << "-" << bla/10;
						Draw_String(0,screen_w-320,45+z*20, 20,sstr.str(),255,0,0,255,0);
					}
					z += 1;
				}
			}
		}
	}

	// Draw information box
	for (int i = 1; i <= mission[0].starsystems; i++)
	{
		for (int j = 1; j <= starsystem[i].planets; j++)
		{
			if (starsystem[i].planet[j].hovered /*&& starsystem[i].planet[j].owner == 1*/ && showResearch == -1 && showDiplomacy == -1)
			{
				sstr << "System: " << i << " / Planet: " << j;
				Draw_String(0,10,30,15,sstr.str(),255,255,255,255,0);
				// Box
				graphic[22].SetPosition((float)screen_w-214,(float)screen_h-144);
				App.Draw(graphic[22]);
				// Name
				sstr << "Name: " << starsystem[i].planet[j].name;
				Draw_String(0,screen_w-195,screen_h-130,15,sstr.str(),255,255,255,255,0);
				// Population
				if (artifact_owner[1] == 1)
				{
					sstr << "Population: " << (int)(starsystem[i].planet[j].population) << "/" << starsystem[i].planet[j].max_population << " +" << (int)(starsystem[i].planet[j].max_population * .15);
					Draw_String(0,screen_w-195,screen_h-115,15,sstr.str(),255,255,255,255,0);
				}
				else
				{
					sstr << "Population: " << (int)(starsystem[i].planet[j].population) << "/" << starsystem[i].planet[j].max_population;
					Draw_String(0,screen_w-195,screen_h-115,15,sstr.str(),255,255,255,255,0);
				}
				// Growth Rate
				if (artifact_owner[3] == 1)
				{
					sstr << "Growth rate: " << (int)(starsystem[i].planet[j].size * 20) / 10 << "/sec +" << (int)(starsystem[i].planet[j].size * .15 * 20) / 10;
					Draw_String(0,screen_w-195,screen_h-100,15,sstr.str(),255,255,255,255,0);
				}
				else
				{
					sstr << "Growth rate: " << (int)(starsystem[i].planet[j].size * 20) / 10 << "/sec";
					Draw_String(0,screen_w-195,screen_h-100,15,sstr.str(),255,255,255,255,0);
				}
				// Credits rate
				sstr << "Credits rate: " << (int)(starsystem[i].planet[j].income*10 - starsystem[i].planet[j].expenses*10)/10 << "/sec";
				Draw_String(0,screen_w-195,screen_h-85,15,sstr.str(),255,255,255,255,0);
				// Size
				sstr << "Size: " << starsystem[i].planet[j].size*10;
				Draw_String(0,screen_w-195, screen_h-70,15,sstr.str(),255,255,255,255,0);
				// Artifact
				if (starsystem[i].planet[j].artifact == 0)
					Draw_String(0,screen_w-195,screen_h-55, 15,"Artifact: none",255,255,255,255,0);
				else if (starsystem[i].planet[j].artifact == 1)
					Draw_String(0,screen_w-195,screen_h-55, 15,"Artifact: +15% Pop. cap",255,255,255,255,0);
				else if (starsystem[i].planet[j].artifact == 2)
					Draw_String(0,screen_w-195,screen_h-55, 15,"Artifact: +15% Fleet speed",255,255,255,255,0);
				else if (starsystem[i].planet[j].artifact == 3)
					Draw_String(0,screen_w-195,screen_h-55, 15,"Artifact: +15% Pop. grow",255,255,255,255,0);
				else if (starsystem[i].planet[j].artifact == 4)
					Draw_String(0,screen_w-195,screen_h-55, 15,"Artifact: +15% Fleet damage",255,255,255,255,0);
				else if (starsystem[i].planet[j].artifact == 5)
					Draw_String(0,screen_w-195,screen_h-55, 15,"Artifact: +15% Fleet range",255,255,255,255,0);

				// Satellite
				Draw_String(0,screen_w-195,screen_h-40, 15,"Satellite:",255,255,255,255,0);
				if (starsystem[i].planet[j].has_satellite)
					Draw_String(0,screen_w-125,screen_h-40, 15,"yes",0,255,0,255,0);
				else
					Draw_String(0,screen_w-125,screen_h-40, 15,"no",255,0,0,255,0);
				// Turret
				Draw_String(0,screen_w-195,screen_h-25, 15,"Turret:",255,255,255,255,0);
				if (starsystem[i].planet[j].has_turret)
					Draw_String(0,screen_w-140,screen_h-25, 15,"yes",0,255,0,255,0);
				else
					Draw_String(0,screen_w-140,screen_h-25, 15,"no",255,0,0,255,0);
			}
		}
	}

	// Draw message
	if (message_code > 0)
	{

		if (message_code == 1)
			sstr << "Game speed changed to " << game_speed << "x!";
		if (message_code == 2)
			sstr << "Game saved!";
		if (message_code == 3)
			sstr << "Game loaded!";
		if (message_code == 4)
			sstr << "The population on one of your planets has revolted and joined enemy forces!";
		if (message_code == 5)
			sstr << "An enemy planet has revolted and joined our forces!";
		if (message_code == 6)
			sstr << "Another civilization has been annihilated!";
		if (message_code == 7)
			sstr << "You have discovered an artifact!";
		if (message_code == 8)
			sstr << "You have finished a research!";
		if (message_code == 9)
			sstr << "You have lost a planet to a plague!";
		if (message_code == 10)
			sstr << "Screenshot saved!";
		if (message_code == 11)
			sstr << "The Dazdarians alignment towards you has changed from " << old_alignment << "% to " << player[2].alignment[1] << "%!";
		if (message_code == 12)
			sstr << "The Valdarians alignment towards you has changed from " << old_alignment << "% to " << player[3].alignment[1] << "%!";
		if (message_code == 13)
			sstr << "The Arcturians alignment towards you has changed from " << old_alignment << "% to " << player[4].alignment[1] << "%!";
		Draw_String(0,5,60,25,sstr.str(),255,0,0,message_alpha,0);

		if (message_code == 4 || message_code == 5 || message_code == 7 || message_code == 9)
			Draw_String(0,5,85,20,"- Press space to go to event -",255,255,255,message_alpha,0);

		// Fade out message
		if (clock_message.GetElapsedTime() > 3)
		{
			if (clock_message_alpha.GetElapsedTime() > .0125)
			{
				clock_message_alpha.Reset();
				message_alpha -= 2;
				if (message_alpha == -1)
					message_code = 0;
			}
		}
	}

	// Draw diplomacy window
	if (showDiplomacy == 1)
	{
		App.Draw(graphic[26]);

		// Names, Portraits, Planets, Population
		if (player[2].planet_count > 0)
		{
			Draw_String(0,screen_wh-245,screen_hh-205,20, "Dazdarians ",255,0,0,255,0);
			sstr << "Planets: " << player[2].planet_count;
			Draw_String(0,screen_wh-280,screen_hh-60,20, sstr.str(),255,255,255,255,0);
			sstr << "Population: " << player[2].population;
			Draw_String(0,screen_wh-280,screen_hh-40,20, sstr.str(),255,255,255,255,0);
			sstr << "Alignment: " << player[2].alignment[1] << "%";
			Draw_String(0,screen_wh-280,screen_hh-20,20, sstr.str(),255,255,255,255,0);
			graphic[27].SetSubRect(sf::IntRect(0,0,201,133));
			graphic[27].SetPosition((float)screen_wh-296,(float)screen_hh-185);
			App.Draw(graphic[27]);
		}
		if (mission[0].players >= 3 && player[3].planet_count > 0)
		{
			Draw_String(0,screen_wh-45 ,screen_hh-205,20, "Valdarians ",0,0,255,255,0);
			sstr << "Planets: " << player[3].planet_count;
			Draw_String(0,screen_wh-85 ,screen_hh-60,20, sstr.str(),255,255,255,255,0);
			sstr << "Population: " << player[3].population;
			Draw_String(0,screen_wh-85 ,screen_hh-40,20, sstr.str(),255,255,255,255,0);
			sstr << "Alignment: " << player[3].alignment[1] << "%";
			Draw_String(0,screen_wh-85 ,screen_hh-20,20, sstr.str(),255,255,255,255,0);
			graphic[27].SetSubRect(sf::IntRect(202,0,403,133));
			graphic[27].SetPosition((float)screen_wh-101,(float)screen_hh-185);
			App.Draw(graphic[27]);
		}
		if (mission[0].players >= 4 && player[4].planet_count > 0)
		{
			Draw_String(0,screen_wh+150,screen_hh-205,20, "Arcturians ",255,255,0,255,0);
			sstr << "Planets: " << player[4].planet_count;
			Draw_String(0,screen_wh+110,screen_hh-60,20, sstr.str(),255,255,255,255,0);
			sstr << "Population: " << player[4].population;
			Draw_String(0,screen_wh+110,screen_hh-40,20, sstr.str(),255,255,255,255,0);
			sstr << "Alignment: " << player[4].alignment[1] << "%";
			Draw_String(0,screen_wh+110,screen_hh-20,20, sstr.str(),255,255,255,255,0);
			graphic[27].SetSubRect(sf::IntRect(404,0,605,133));
			graphic[27].SetPosition((float)screen_wh+94,(float)screen_hh-185);
			App.Draw(graphic[27]);
		}

		// Alignment bars
		if (player[2].planet_count > 0)
		{
			Draw_Box(screen_wh-280,screen_hh+5,100,20,0,0,0);
			App.Draw(sf::Shape::Rectangle((float)screen_wh-279,(float)screen_hh+6,(float)screen_wh-279+player[2].alignment[1],(float)screen_hh+24,sf::Color(255-player[2].alignment[1]*255/100,player[2].alignment[1]*255/100,0),0,sf::Color(0,0,0)));
		}
		if (mission[0].players >= 3 && player[3].planet_count > 0)
		{
			Draw_Box(screen_wh-85,screen_hh+5,100,20,0,0,0);
			App.Draw(sf::Shape::Rectangle((float)screen_wh-84,(float)screen_hh+6,(float)screen_wh-84+player[3].alignment[1],(float)screen_hh+24,sf::Color(255-player[3].alignment[1]*255/100,player[3].alignment[1]*255/100,0),0,sf::Color(0,0,0)));
		}
		if (mission[0].players >= 4 && player[4].planet_count > 0)
		{
			Draw_Box(screen_wh+110,screen_hh+5,100,20,0,0,0);
			App.Draw(sf::Shape::Rectangle((float)screen_wh+111,(float)screen_hh+6,(float)screen_wh+111+player[4].alignment[1],(float)screen_hh+24,sf::Color(255-player[4].alignment[1]*255/100,player[4].alignment[1]*255/100,0),0,sf::Color(0,0,0)));
		}

		// Alignment text
		for (int i = 2; i <= 4; i++)
		{
			if (i <= mission[0].players && player[i].planet_count > 0)
			{
				if (player[i].alignment[1] <= 20)
					Draw_String(0,screen_wh-279+195*(i-2),screen_hh+31,20,"Hostile",255,255,255,255,0);
				if (player[i].alignment[1] >= 21 && player[i].alignment[1] <= 40)
					Draw_String(0,screen_wh-279+195*(i-2),screen_hh+31,20,"Distrustful",255,255,255,255,0);
				if (player[i].alignment[1] >= 41 && player[i].alignment[1] <= 60)
					Draw_String(0,screen_wh-279+195*(i-2),screen_hh+31,20,"Neutral",255,255,255,255,0);
				if (player[i].alignment[1] >= 61 && player[i].alignment[1] <= 80)
					Draw_String(0,screen_wh-279+195*(i-2),screen_hh+31,20,"Trustful",255,255,255,255,0);
				if (player[i].alignment[1] >= 81)
					Draw_String(0,screen_wh-279+195*(i-2),screen_hh+31,20,"Peaceful",255,255,255,255,0);
			}
		}

		// Draw send gift text
		if (button[18].hover == 1 || button[19].hover == 1 || button[20].hover == 1)
		{
			Draw_String(0,screen_wh-280,screen_hh+120,20, "Send 100 credits to raise the alignment of this",255,255,255,255,0);
			Draw_String(0,screen_wh-280,screen_hh+140,20, "player towards you about 1%.",255,255,255,255,0);
		}

	}

	// Draw research window
	if (showResearch == 1)
	{
		App.Draw(graphic[4]);
		if (player[1].researching > 0) // && research_selected = player[1].researching
			Draw_String(0,(int)graphic[4].GetPosition().x+50,(int)graphic[4].GetPosition().y,25,"Researching...",255,0,0,255,0);

		// Draw research icons
		for (int i = 1; i <= 3; i++)
		{
			graphic[19].SetSubRect(sf::IntRect(44*i - 44 + 132 * player[1].research[i].researched,0,44*i + 132 * player[1].research[i].researched,43));
			graphic[19].SetPosition((float)screen_wh-272,(float)screen_hh-322 + i*150);
			App.Draw(graphic[19]);
		}

		for (int i = 1; i <= 3; i++)
		{
			graphic[19].SetSubRect(sf::IntRect(44*i - 44 + 132 * player[1].research[i+4].researched,44,44*i + 132 * player[1].research[i+4].researched,87));
			graphic[19].SetPosition((float)screen_wh-217,(float)screen_hh-322 + i*150);
			App.Draw(graphic[19]);
		}

		for (int i = 1; i <= 3; i++)
		{
			graphic[19].SetSubRect(sf::IntRect(44*i - 44 + 132 * player[1].research[i+8].researched,88,44*i + 132 * player[1].research[i+8].researched,131));
			graphic[19].SetPosition((float)screen_wh-162,(float)screen_hh-322 + i*150);
			App.Draw(graphic[19]);
		}

		for (int i = 1; i <= 3; i++)
		{
			graphic[19].SetSubRect(sf::IntRect(44*i - 44 + 132 * player[1].research[i+12].researched,132,44*i + 132 * player[1].research[i+12].researched,175));
			graphic[19].SetPosition((float)screen_wh-107,(float)screen_hh-322 + i*150);
			App.Draw(graphic[19]);
		}

		for (int i = 1; i <= 3; i++)
		{
			graphic[19].SetSubRect(sf::IntRect(44*i - 44 + 132 * player[1].research[i+16].researched,176,44*i + 132 * player[1].research[i+16].researched,219));
			graphic[19].SetPosition((float)screen_wh-52,(float)screen_hh-322 + i*150);
			App.Draw(graphic[19]);
		}

		// Phase Lane Disruption
		if (research_selected == 1)
		{
			graphic[7].SetPosition((float)screen_wh-280,(float)screen_hh-180);
			App.Draw(graphic[7]);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-210,25,"Phase Lane Disruption I",0,206,209,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-170,20,"Destabilizes all phase lanes to",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-150,20,"a planet. Attacking fleets will",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-130,20,"be destroyed.",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 90,20,"Research Time: 60 seconds",255,255,255,255,0);
			sstr << "Research Cost: " << player[1].research[1].costs;
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 70,20,sstr.str(),255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 50,20,"Duration: 30 seconds",255,255,255,255,0);
		}

		if (research_selected == 2)
		{
			graphic[7].SetPosition((float)screen_wh-280,(float)screen_hh-30);
			App.Draw(graphic[7]);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-210,25,"Phase Lane Disruption I",0,206,209,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-170,20,"Destabilizes all phase lanes to",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-150,20,"a planet. Attacking fleets will",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-130,20,"be destroyed.",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 90,20,"Research Time: 120 seconds",255,255,255,255,0);
			sstr << "Research Cost: " << player[1].research[2].costs;
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 70,20,sstr.str(),255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 50,20,"Duration: 60 seconds",255,255,255,255,0);
		}

		if (research_selected == 3)
		{
			graphic[7].SetPosition((float)screen_wh-280,(float)screen_hh+120);
			App.Draw(graphic[7]);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-210,25,"Phase Lane Disruption I",0,206,209,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-170,20,"Destabilizes all phase lanes to",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-150,20,"a planet. Attacking fleets will",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-130,20,"be destroyed.",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 90,20,"Research Time: 180 seconds",255,255,255,255,0);
			sstr << "Research Cost: " << player[1].research[3].costs;
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 70,20,sstr.str(),255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 50,20,"Duration: 90 seconds",255,255,255,255,0);
		}

		// Satellite Defense
		if (research_selected == 5)
		{
			graphic[7].SetPosition((float)screen_wh-225,(float)screen_hh-180);
			App.Draw(graphic[7]);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-210,25,"Satellite Defense I",0,206,209,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-170,20,"Defend a planet with this",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-150,20,"orbiting station. It will be",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-130,20,"destroyed when attacking fleet",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-110,20,"is larger than its Defense.",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 70,20,"Research Time: 60 seconds",255,255,255,255,0);
			sstr << "Research Cost: " << player[1].research[5].costs;
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 50,20,sstr.str(),255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 30,20,"Defense: 1 fleet",255,255,255,255,0);
		}

		if (research_selected == 6)
		{
			graphic[7].SetPosition((float)screen_wh-225,(float)screen_hh-30);
			App.Draw(graphic[7]);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-210,25,"Satellite Defense I",0,206,209,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-170,20,"Defend a planet with this",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-150,20,"orbiting station. It will be",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-130,20,"destroyed when attacking fleet",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-110,20,"is larger than its Defense.",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 70,20,"Research Time: 120 seconds",255,255,255,255,0);
			sstr << "Research Cost: " << player[1].research[6].costs;
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 50,20,sstr.str(),255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 30,20,"Defense: 2 fleets",255,255,255,255,0);
		}

		if (research_selected == 7)
		{
			graphic[7].SetPosition((float)screen_wh-225,(float)screen_hh+120);
			App.Draw(graphic[7]);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-210,25,"Satellite Defense I",0,206,209,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-170,20,"Defend a planet with this",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-150,20,"orbiting station. It will be",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-130,20,"destroyed when attacking fleet",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-110,20,"is larger than its Defense.",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 70,20,"Research Time: 180 seconds",255,255,255,255,0);
			sstr << "Research Cost: " << player[1].research[7].costs;
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 50,20,sstr.str(),255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 30,20,"Defense: 3 fleets",255,255,255,255,0);
		}

		// Laser Turret
		if (research_selected == 9)
		{
			graphic[7].SetPosition((float)screen_wh-170,(float)screen_hh-180);
			App.Draw(graphic[7]);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-210,25,"Laser Turret I",0,206,209,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-170,20,"Place this turret to attack",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-150,20,"travelling fleets and weaken",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-130,20,"their strength.",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 90,20,"Research Time: 60 seconds",255,255,255,255,0);
			sstr << "Research Cost: " << player[1].research[9].costs;
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 70,20,sstr.str(),255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 50,20,"Damage: 1 point",255,255,255,255,0);
		}

		if (research_selected == 10)
		{
			graphic[7].SetPosition((float)screen_wh-170,(float)screen_hh-30);
			App.Draw(graphic[7]);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-210,25,"Laser Turret I",0,206,209,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-170,20,"Place this turret to attack",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-150,20,"travelling fleets and weaken",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-130,20,"their strength.",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 90,20,"Research Time: 120 seconds",255,255,255,255,0);
			sstr << "Research Cost: " << player[1].research[10].costs;
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 70,20,sstr.str(),255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 50,20,"Damage: 2 points",255,255,255,255,0);
		}

		if (research_selected == 11)
		{
			graphic[7].SetPosition((float)screen_wh-170,(float)screen_hh+120);
			App.Draw(graphic[7]);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-210,25,"Laser Turret I",0,206,209,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-170,20,"Place this turret to attack",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-150,20,"travelling fleets and weaken",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-130,20,"their strength.",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 90,20,"Research Time: 180 seconds",255,255,255,255,0);
			sstr << "Research Cost: " << player[1].research[11].costs;
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 70,20,sstr.str(),255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 50,20,"Damage: 3 points",255,255,255,255,0);
		}

		// Economy Boost
		if (research_selected == 13)
		{
			graphic[7].SetPosition((float)screen_wh-115,(float)screen_hh-180);
			App.Draw(graphic[7]);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-210,25,"Economy Boost I",0,206,209,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-170,20,"Strengthens your economy",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-150,20,"output with 10% more credits",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-130,20,"per level.",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 90,20,"Research Time: 60 seconds",255,255,255,255,0);
			sstr << "Research Cost: " << player[1].research[13].costs;
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 70,20,sstr.str(),255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 50,20,"Credits Bonus: 10%",255,255,255,255,0);
		}

		if (research_selected == 14)
		{
			graphic[7].SetPosition((float)screen_wh-115,(float)screen_hh-30);
			App.Draw(graphic[7]);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-210,25,"Economy Boost I",0,206,209,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-170,20,"Strengthens your economy",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-150,20,"output with 10% more credits",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-130,20,"per level.",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 90,20,"Research Time: 120 seconds",255,255,255,255,0);
			sstr << "Research Cost: " << player[1].research[14].costs;
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 70,20,sstr.str(),255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 50,20,"Credits Bonus: 20%",255,255,255,255,0);
		}

		if (research_selected == 15)
		{
			graphic[7].SetPosition((float)screen_wh-115,(float)screen_hh+120);
			App.Draw(graphic[7]);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-210,25,"Economy Boost I",0,206,209,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-170,20,"Strengthens your economy",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-150,20,"output with 10% more credits",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-130,20,"per level.",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 90,20,"Research Time: 180 seconds",255,255,255,255,0);
			sstr << "Research Cost: " << player[1].research[15].costs;
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 70,20,sstr.str(),255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 50,20,"Credits Bonus: 30%",255,255,255,255,0);
		}

		// Metal Extraction
		if (research_selected == 17)
		{
			graphic[7].SetPosition((float)screen_wh-60,(float)screen_hh-180);
			App.Draw(graphic[7]);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-210,25,"Metal Extraction I",0,206,209,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-170,20,"Allows your planets and metal",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-150,20,"mines to produce 10% more",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-130,20,"metal per level.",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 90,20,"Research Time: 60 seconds",255,255,255,255,0);
			sstr << "Research Cost: " << player[1].research[17].costs;
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 70,20,sstr.str(),255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 50,20,"Metal Bonus: 10%",255,255,255,255,0);
		}

		if (research_selected == 18)
		{
			graphic[7].SetPosition((float)screen_wh-60,(float)screen_hh-30);
			App.Draw(graphic[7]);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-210,25,"Metal Extraction I",0,206,209,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-170,20,"Allows your planets and metal",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-150,20,"mines to produce 10% more",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-130,20,"metal per level.",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 90,20,"Research Time: 120 seconds",255,255,255,255,0);
			sstr << "Research Cost: " << player[1].research[18].costs;
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 70,20,sstr.str(),255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 50,20,"Metal Bonus: 20%",255,255,255,255,0);
		}

		if (research_selected == 19)
		{
			graphic[7].SetPosition((float)screen_wh-60,(float)screen_hh+120);
			App.Draw(graphic[7]);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-210,25,"Metal Extraction I",0,206,209,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-170,20,"Allows your planets and metal",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-150,20,"mines to produce 10% more",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y-130,20,"metal per level.",255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 90,20,"Research Time: 180 seconds",255,255,255,255,0);
			sstr << "Research Cost: " << player[1].research[19].costs;
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 70,20,sstr.str(),255,255,255,255,0);
			Draw_String(0,(int)graphic[4].GetPosition().x+10,(int)graphic[4].GetPosition().y- 50,20,"Metal Bonus: 30%",255,255,255,255,0);
		}
	}

	// Draw research progress bar
	if (player[1].researching > 0)
	{
		if (showResearch == -1)
		{
			Draw_Box(screen_wh-75,40,150,25,0,0,0);
			App.Draw(sf::Shape::Rectangle((float)screen_wh-74,(float)41,(float)screen_wh-74+((148/player[1].research[player[1].researching].time)*player[1].research_progress),(float)64,sf::Color(255,0,0),0,sf::Color(0,0,0)));
			if (mouse.x >= screen_wh-75 && mouse.x <= screen_wh+75 && mouse.y >= 40 && mouse.y <= 65)
			{
				sstr << (int)(100/player[1].research[player[1].researching].time)*player[1].research_progress << "% done";
				Draw_String(0,screen_wh,65,20,sstr.str(),255,255,255,255,1);
			}
		}
		else
		{
			Draw_Box(screen_wh+49,screen_hh+30,150,25,0,0,0);
			App.Draw(sf::Shape::Rectangle((float)screen_wh+50,(float)screen_hh+31,(float)screen_wh+50+((148/player[1].research[player[1].researching].time)*player[1].research_progress),(float)screen_hh+55,sf::Color(255,0,0),0,sf::Color(0,0,0)));
		}
	}

	// Draw icons
	if (planet_selected > 0 && planet_selected_count == 1)
	{

		// Draw build mine icon
		if (player[1].credits < CREDITS_MINE || player[1].metal < METAL_MINE || starsystem[system_selected].planet[planet_selected].has_mine == 1)
			graphic[33].SetSubRect(sf::IntRect(45,0,88,44));
		else
			graphic[33].SetSubRect(sf::IntRect(0,0,44,44));
		graphic[33].SetCenter(graphic[33].GetSize().x, graphic[33].GetSize().y/2);
		graphic[33].SetPosition((float)screen_w,(float)screen_hh + 88);
		App.Draw(graphic[33]);

		// Draw build fleet icon
		if (player[1].credits < CREDITS_FLEET || player[1].metal < METAL_FLEET)
			graphic[32].SetSubRect(sf::IntRect(45,0,88,44));
		else
			graphic[32].SetSubRect(sf::IntRect(0,0,44,44));
		graphic[32].SetCenter(graphic[32].GetSize().x, graphic[32].GetSize().y/2);
		graphic[32].SetPosition((float)screen_w,(float)screen_hh - 44);
		App.Draw(graphic[32]);

		// Draw build satellite icon
		if (player[1].research[5].researched)
		{
			if (player[1].credits < CREDITS_SATELLITE || player[1].metal < METAL_SATELLITE || starsystem[system_selected].planet[planet_selected].has_satellite == 1)
				graphic[3].SetSubRect(sf::IntRect(45,0,88,44));
			else
				graphic[3].SetSubRect(sf::IntRect(0,0,44,44));
			graphic[3].SetCenter(graphic[3].GetSize().x, graphic[3].GetSize().y/2);
			graphic[3].SetPosition((float)screen_w,(float)screen_hh);
			App.Draw(graphic[3]);
		}

		// Draw build turret icon
		if (player[1].research[9].researched)
		{
			if (player[1].credits < CREDITS_TURRET || player[1].metal < METAL_TURRET || starsystem[system_selected].planet[planet_selected].has_turret == 1)
				graphic[8].SetSubRect(sf::IntRect(45,0,88,44));
			else
				graphic[8].SetSubRect(sf::IntRect(0,0,44,44));
			graphic[8].SetCenter(graphic[8].GetSize().x, graphic[8].GetSize().y/2);
			graphic[8].SetPosition((float)screen_w,(float)screen_hh + 44);
			App.Draw(graphic[8]);
		}

		// Draw hover descriptions

		// Satellite
		if (icon_hover == 1)
		{
			graphic[35].SetPosition((float)screen_w-159,(float)screen_hh-24);
			App.Draw(graphic[35]);
			if (starsystem[system_selected].planet[planet_selected].has_satellite == 0)
			{
				Draw_String(0,screen_w-140,screen_hh-11,15,"Build satellite",255,255,255,255,0);
				Draw_String(0,screen_w-140,screen_hh+9,15,"Credits:",255,255,255,255,0);
				sstr << CREDITS_SATELLITE;
				if (player[1].credits < CREDITS_SATELLITE)
					Draw_String(0,screen_w-82,screen_hh+9,15,sstr.str(),255,0,0,255,0);
				else
					Draw_String(0,screen_w-82,screen_hh+9,15,sstr.str(),0,255,0,255,0);
				Draw_String(0,screen_w-140,screen_hh+29,15,"Metal:",255,255,255,255,0);
				sstr << METAL_SATELLITE;
				if (player[1].metal < METAL_SATELLITE)
					Draw_String(0,screen_w-82,screen_hh+29,15,sstr.str(),255,0,0,255,0);
				else
					Draw_String(0,screen_w-82,screen_hh+29,15,sstr.str(),0,255,0,255,0);
			}
			else
				Draw_String(0,screen_w-140,screen_hh-11,15,"Already built!",255,0,0,255,0);
		}

		// Turret
		if (icon_hover == 2)
		{
			graphic[35].SetPosition((float)screen_w-159,(float)screen_hh+20);
			App.Draw(graphic[35]);
			if (starsystem[system_selected].planet[planet_selected].has_turret == 0)
			{	
				Draw_String(0,screen_w-140,screen_hh+33,15,"Build turret",255,255,255,255,0);
				Draw_String(0,screen_w-140,screen_hh+53,15,"Credits:",255,255,255,255,0);
				sstr << CREDITS_TURRET;
				if (player[1].credits < CREDITS_TURRET)
					Draw_String(0,screen_w-82,screen_hh+53,15,sstr.str(),255,0,0,255,0);
				else
					Draw_String(0,screen_w-82,screen_hh+53,15,sstr.str(),0,255,0,255,0);
				Draw_String(0,screen_w-140,screen_hh+73,15,"Metal:",255,255,255,255,0);
				sstr << METAL_TURRET;
				if (player[1].metal < METAL_TURRET)
					Draw_String(0,screen_w-82,screen_hh+73,15,sstr.str(),255,0,0,255,0);
				else
					Draw_String(0,screen_w-82,screen_hh+73,15,sstr.str(),0,255,0,255,0);
			}
			else
				Draw_String(0,screen_w-140,screen_hh+33,15,"Already built!",255,0,0,255,0);
		}

		// Fleet
		if (icon_hover == 3)
		{
			graphic[35].SetPosition((float)screen_w-159,(float)screen_hh-68);
			App.Draw(graphic[35]);
			Draw_String(0,screen_w-140,screen_hh-55,15,"Build fleet",255,255,255,255,0);
			Draw_String(0,screen_w-140,screen_hh-35,15,"Credits:",255,255,255,255,0);
			sstr << CREDITS_FLEET;
			if (player[1].credits < CREDITS_FLEET)
				Draw_String(0,screen_w-82,screen_hh-35,15,sstr.str(),255,0,0,255,0);
			else
				Draw_String(0,screen_w-82,screen_hh-35,15,sstr.str(),0,255,0,255,0);
			Draw_String(0,screen_w-140,screen_hh-15,15,"Metal:",255,255,255,255,0);
			sstr << METAL_FLEET;
			if (player[1].metal < METAL_FLEET)
				Draw_String(0,screen_w-82,screen_hh-15,15,sstr.str(),255,0,0,255,0);
			else
				Draw_String(0,screen_w-82,screen_hh-15,15,sstr.str(),0,255,0,255,0);
		}

		// Mine
		if (icon_hover == 4)
		{
			graphic[35].SetPosition((float)screen_w-159,(float)screen_hh+64);
			App.Draw(graphic[35]);
			if (starsystem[system_selected].planet[planet_selected].has_mine == 0)
			{
				Draw_String(0,screen_w-140,screen_hh+77,15,"Build mine",255,255,255,255,0);
				Draw_String(0,screen_w-140,screen_hh+97,15,"Credits:",255,255,255,255,0);
				sstr << CREDITS_MINE;
				if (player[1].credits < CREDITS_MINE)
					Draw_String(0,screen_w-82,screen_hh+97,15,sstr.str(),255,0,0,255,0);
				else
					Draw_String(0,screen_w-82,screen_hh+97,15,sstr.str(),0,255,0,255,0);
				Draw_String(0,screen_w-140,screen_hh+117,15,"Metal:",255,255,255,255,0);
				sstr << METAL_MINE;
				if (player[1].metal < METAL_MINE)
					Draw_String(0,screen_w-82,screen_hh+117,15,sstr.str(),255,0,0,255,0);
				else
					Draw_String(0,screen_w-82,screen_hh+117,15,sstr.str(),0,255,0,255,0);
			}
			else
				Draw_String(0,screen_w-140,screen_hh+77,15,"Already built!",255,0,0,255,0);
		}
	}

	// Draw phase lane disruption icon
	if (player[1].research[1].researched)
	{
		graphic[17].SetSubRect(sf::IntRect(disruption_status * 44, 0, 43 + disruption_status * 44, 43));
		App.Draw(graphic[17]);
		if (disruption_status == 0)
		{
			if (disruption_countdown % 60 > 9)
			{
				sstr << disruption_countdown/60 << ":" << disruption_countdown % 60;
				Draw_String(0,8,screen_hh-9,15, sstr.str(),255,255,255,255,0);
			}
			if (disruption_countdown % 60 < 10)
			{
				sstr << disruption_countdown/60 << ":0" << disruption_countdown % 60;
				Draw_String(0,8,screen_hh-9,15, sstr.str(),255,255,255,255,0);
			}
		}
		if (icon_hover == 3)
			Draw_String(0,50,screen_hh-9,15,"Phase Lane Disruption",255,255,255,255,0);
	}
	// status 0 - countdown
	// status 1 - available
	// status 2 - active

	// Draw selection box
	if (mouse_still_pressed == 1 && pauseGame == -1 && exitGame == -1 && showResearch == -1)
		App.Draw(sf::Shape::Rectangle((float)box.x,(float)box.y,(float)mouse.x,(float)mouse.y,sf::Color(255,255,255,25),1,sf::Color(255,255,255)));

	// Draw pause box
	if (pauseGame == 1)
	{
		App.Draw(graphic[23]);
		Draw_String(0,screen_wh,screen_hh-12,25,"Game paused!",255,0,0,255,1);
	}

	// Draw exit game box
	if (exitGame == 1)
	{
		App.Draw(graphic[23]);
		Draw_String(0,screen_wh,screen_hh-40,25,"Quit to mainmenu?",255,255,255,255,1);
	}

}

void Input_Mouse()
{

	const sf::Input& Input = App.GetInput();

	// Check rock hover
	for (int i = 1; i <= mission[0].starsystems; i++)
	{
		for (int j = 0; j <= starsystem[i].rocks; j++)
		{
			if (mouse2.x > starsystem[i].rock[j].position.x - 5 && mouse2.x < starsystem[i].rock[j].position.x + 5 && mouse2.y > starsystem[i].rock[j].position.y - 5 && mouse2.y < starsystem[i].rock[j].position.y + 5)
				starsystem[i].rock[j].hovered = 1;
			else
				starsystem[i].rock[j].hovered = 0;
		}
	}

	// Check icon hover
	icon_hover = 0;
	if (planet_selected > 0 && planet_selected_count == 1)
	{
		// satellite icon
		if (mouse.x > screen_w-44 && mouse.y > screen_hh-22 && mouse.y < screen_hh + 22 && player[1].research[5].researched)
		{
			icon_hover = 1;
			if (Input.IsMouseButtonDown(sf::Mouse::Left) && starsystem[system_selected].planet[planet_selected].has_satellite == 0)
			{
				sound[12].Play();
				if (starsystem[system_selected].planet[planet_selected].has_satellite == 0 && player[1].credits >= CREDITS_SATELLITE && player[1].metal >= METAL_SATELLITE && starsystem[system_selected].planet[planet_selected].owner == 1)
				{
					starsystem[system_selected].planet[planet_selected].has_satellite = 1;
					starsystem[system_selected].planet[planet_selected].satellite_health = 1;
					if (player[1].research[6].researched)
						starsystem[system_selected].planet[planet_selected].satellite_health += 1;
					if (player[1].research[7].researched)
						starsystem[system_selected].planet[planet_selected].satellite_health += 1;
					player[1].credits -= CREDITS_SATELLITE;
					player[1].metal -= METAL_SATELLITE;
				}
			}
		}
		// turret icon
		if (mouse.x > screen_w-44 && mouse.y > screen_hh+22 && mouse.y < screen_hh+66 && player[1].research[9].researched)
		{
			icon_hover = 2;
			if (Input.IsMouseButtonDown(sf::Mouse::Left) && player[1].credits >= CREDITS_TURRET && player[1].metal >= METAL_TURRET && starsystem[system_selected].planet[planet_selected].has_turret == 0 && active_cursor != CURSOR_TURRET)
			{
				sound[12].Play();
				active_cursor = CURSOR_TURRET;
			}
		}
		// fleet icon
		if (mouse.x > screen_w-44 && mouse.y > screen_hh-66 && mouse.y < screen_hh-22)
		{
			icon_hover = 3;
			if (Input.IsMouseButtonDown(sf::Mouse::Left) && player[1].credits >= CREDITS_FLEET && player[1].metal >= METAL_FLEET && starsystem[system_selected].planet[planet_selected].fleets <= 10)
			{
				if (clock_mouse.GetElapsedTime() > .25)
				{
					clock_mouse.Reset();
					sound[12].Play();
					Add_Fleet(system_selected,planet_selected);
					player[1].credits -= CREDITS_FLEET;
					player[1].metal -= METAL_FLEET;
				}
			}
		}
		// mine icon
		if (mouse.x > screen_w-44 && mouse.y > screen_hh+66 && mouse.y < screen_hh+110)
		{
			icon_hover = 4;
			if (Input.IsMouseButtonDown(sf::Mouse::Left) && player[1].credits >= CREDITS_MINE && player[1].metal >= METAL_MINE && starsystem[system_selected].planet[planet_selected].has_mine == 0 && active_cursor != CURSOR_MINE)
			{
				sound[12].Play();
				active_cursor = CURSOR_MINE;
			}
		}
	}

	// Phase lane disruption icon
	if (mouse.x > 0 && mouse.x < 33 && mouse.y > screen_hh - 17 && mouse.y < screen_hh + 17 && player[1].research[1].researched)
	{
		icon_hover = 3;
		if (Input.IsMouseButtonDown(sf::Mouse::Left))
		{
			if (disruption_status == 1)
			{
				sound[12].Play();
				active_cursor = CURSOR_DISRUPTION;
			}
		}
	}

	// Select research
	if (showResearch == 1)
	{
		if (mouse.x > screen_wh - 268 && mouse.x < screen_wh - 232 && mouse.y > screen_hh - 168 && mouse.y < screen_hh - 132 && Input.IsMouseButtonDown(sf::Mouse::Left))
			research_selected = 1;
		if (mouse.x > screen_wh - 268 && mouse.x < screen_wh - 232 && mouse.y > screen_hh - 18 && mouse.y < screen_hh + 18 && Input.IsMouseButtonDown(sf::Mouse::Left))
			research_selected = 2;
		if (mouse.x > screen_wh - 268 && mouse.x < screen_wh - 232 && mouse.y > screen_hh + 132 && mouse.y < screen_hh + 168 && Input.IsMouseButtonDown(sf::Mouse::Left))
			research_selected = 3;
		if (mouse.x > screen_wh - 213 && mouse.x < screen_wh - 177 && mouse.y > screen_hh - 168 && mouse.y < screen_hh - 132 && Input.IsMouseButtonDown(sf::Mouse::Left))
			research_selected = 5;
		if (mouse.x > screen_wh - 213 && mouse.x < screen_wh - 177 && mouse.y > screen_hh - 18 && mouse.y < screen_hh + 18 && Input.IsMouseButtonDown(sf::Mouse::Left))
			research_selected = 6;
		if (mouse.x > screen_wh - 213 && mouse.x < screen_wh - 177 && mouse.y > screen_hh + 132 && mouse.y < screen_hh + 168 && Input.IsMouseButtonDown(sf::Mouse::Left))
			research_selected = 7;
		if (mouse.x > screen_wh - 158 && mouse.x < screen_wh - 122 && mouse.y > screen_hh - 168 && mouse.y < screen_hh - 132 && Input.IsMouseButtonDown(sf::Mouse::Left))
			research_selected = 9;
		if (mouse.x > screen_wh - 158 && mouse.x < screen_wh - 122 && mouse.y > screen_hh - 18 && mouse.y < screen_hh + 18 && Input.IsMouseButtonDown(sf::Mouse::Left))
			research_selected = 10;
		if (mouse.x > screen_wh - 158 && mouse.x < screen_wh - 122 && mouse.y > screen_hh + 132 && mouse.y < screen_hh + 168 && Input.IsMouseButtonDown(sf::Mouse::Left))
			research_selected = 11;
		if (mouse.x > screen_wh - 103 && mouse.x < screen_wh - 67 && mouse.y > screen_hh - 168 && mouse.y < screen_hh - 132 && Input.IsMouseButtonDown(sf::Mouse::Left))
			research_selected = 13;
		if (mouse.x > screen_wh - 103 && mouse.x < screen_wh - 67 && mouse.y > screen_hh - 18 && mouse.y < screen_hh + 18 && Input.IsMouseButtonDown(sf::Mouse::Left))
			research_selected = 14;
		if (mouse.x > screen_wh - 103 && mouse.x < screen_wh - 67 && mouse.y > screen_hh + 132 && mouse.y < screen_hh + 168 && Input.IsMouseButtonDown(sf::Mouse::Left))
			research_selected = 15;
		if (mouse.x > screen_wh - 48 && mouse.x < screen_wh - 12 && mouse.y > screen_hh - 168 && mouse.y < screen_hh - 132 && Input.IsMouseButtonDown(sf::Mouse::Left))
			research_selected = 17;
		if (mouse.x > screen_wh - 48 && mouse.x < screen_wh - 12 && mouse.y > screen_hh - 18 && mouse.y < screen_hh + 18 && Input.IsMouseButtonDown(sf::Mouse::Left))
			research_selected = 18;
		if (mouse.x > screen_wh - 48 && mouse.x < screen_wh - 12 && mouse.y > screen_hh + 132 && mouse.y < screen_hh + 168 && Input.IsMouseButtonDown(sf::Mouse::Left))
			research_selected = 19;
	}

	// Scroll view
	if ((mouse.x == 0 || mouse.x == screen_w-1 || mouse.y == 0 || mouse.y == screen_h-1) && mouse_still_pressed == 0)
	{
		if (clock_mouse.GetElapsedTime() > 0.01)
		{
			clock_mouse.Reset();
			if (mouse.x == 0)
				View.Move(-4/zoom, 0);
			if (mouse.x == screen_w-1)
				View.Move(4/zoom, 0);
			if (mouse.y == 0)
				View.Move(0, -4/zoom);
			if (mouse.y == screen_h-1)
				View.Move(0, 4/zoom);
		}
	}

	// Check star hover
	for (int i = 1; i <= mission[0].starsystems; i++)
	{
		if (mouse2.x > starsystem[i].planet[0].position.x - 65 && mouse2.x < starsystem[i].planet[0].position.x + 65 && mouse2.y > starsystem[i].planet[0].position.y - 65 && mouse2.y < starsystem[i].planet[0].position.y + 65 && mouse_still_pressed == 0)
			starsystem[i].planet[0].hovered = 1;
		else
			starsystem[i].planet[0].hovered = 0;
	}

	// Check fleet hover
	for (int k = 0; k <= 199; k++)
	{
		if (mouse2.x > fleet[k].position.x-10 && mouse2.x < fleet[k].position.x+10 && mouse2.y > fleet[k].position.y-10 && mouse2.y < fleet[k].position.y+10 && mouse_still_pressed == 0)
		{
			fleet[k].hovered = true;
			if (Input.IsMouseButtonDown(sf::Mouse::Left))
				fleet[k].selected = true;
		}
		else
			fleet[k].hovered = false;
	}

	// Main interaction
	not_hovered = 0;
	reset_auto_timer = 0;
	for (int i = 1; i <= mission[0].starsystems; i++)
	{
		for (int j = 1; j <= starsystem[i].planets; j++)
		{
			// hovering over mine?
			if (mouse2.x > starsystem[i].planet[j].position_mine.x-10 && mouse2.x < starsystem[i].planet[j].position_mine.x+10 && mouse2.y > starsystem[i].planet[j].position_mine.y-10 && mouse2.y < starsystem[i].planet[j].position_mine.y+10 && mouse_still_pressed == 0)
				starsystem[i].planet[j].mine_hovered = 1;
			else
				starsystem[i].planet[j].mine_hovered = 0;
			//  hovering over turret?
			if (mouse2.x > starsystem[i].planet[j].position_turret.x-10 && mouse2.x < starsystem[i].planet[j].position_turret.x+10 && mouse2.y > starsystem[i].planet[j].position_turret.y-10 && mouse2.y < starsystem[i].planet[j].position_turret.y+10 && mouse_still_pressed == 0)
			{
				starsystem[i].planet[j].turret_hovered = 1;
				if (Input.IsMouseButtonDown(sf::Mouse::Left))
				{
					for (int k = 1; k <= mission[0].starsystems; k++)
					{
						for (int l = 1; l <= starsystem[k].planets; l++)
						{
							starsystem[k].planet[l].selected = 0;
						}
					}
					planet_selected_count = 0;
					starsystem[i].planet[j].turret_selected = 1;
					sound[0].Play();
				}
			}
			else
				starsystem[i].planet[j].turret_hovered = 0;
			// hovering over planet?
			if (mouse2.x > starsystem[i].planet[j].position.x-33 && mouse2.x < starsystem[i].planet[j].position.x+33 && mouse2.y > starsystem[i].planet[j].position.y-33 && mouse2.y < starsystem[i].planet[j].position.y+33 && mouse_still_pressed == 0)
			{
				starsystem[i].planet[j].hovered = 1;
				if (planet_selected_count > 0)
				{
					if (starsystem[i].planet[j].owner == 1 && planet_selected != j && active_cursor == CURSOR_DEFAULT)
						active_cursor = CURSOR_ATTACKGREEN;
					else
						active_cursor = CURSOR_ATTACKRED;
				}
				// left click on own planet
				if (Input.IsMouseButtonDown(sf::Mouse::Left) && starsystem[i].planet[j].owner == 1 && showResearch == -1 && showDiplomacy == -1)
				{
					// deselect all other planets
					for (int k = 1; k <= mission[0].starsystems; k++)
					{
						for (int l = 1; l <= starsystem[k].planets; l++)
						{
							starsystem[k].planet[l].selected = 0;
						}
					}
					planet_selected_count = 0;
					starsystem[i].planet[j].selected = 1;
					planet_selected = j;
					system_selected = i;
					planet_selected_count += 1;
					sound[0].Play();
					// Apply phase lane disruption
					if (active_cursor == CURSOR_DISRUPTION)
					{
						for (int k = 0; k <= 199; k++)
						{
							if (fleet[k].status == FLEET_TRAVELLING && fleet[k].owner != 1 && fleet[k].target_system == i && fleet[k].target_planet == j)
							{
								fleet[k].built = false;
								Add_Explosion(fleet[k].position);
							}
						}
						active_cursor = CURSOR_DEFAULT;
						starsystem[i].planet[j].has_disruption = true;
						disruption_status = 2;
						if (player[1].research[1].researched)
							disruption_timer = 30;
						if (player[1].research[2].researched)
							disruption_timer += 30;
						if (player[1].research[3].researched)
							disruption_timer += 30;
					}
				}
				// Target planet
				if (Input.IsMouseButtonDown(sf::Mouse::Right) && clock_mouse.GetElapsedTime() > .25)
				{
					clock_mouse.Reset();
					// check all selected planets
					for (int k = 1; k <= mission[0].starsystems; k++)
					{
						for (int l = 1; l <= starsystem[k].planets; l++)
						{
							if (starsystem[k].planet[l].selected)
							{
								// flip auto send variable
								//if (Input.IsKeyDown(sf::Key::LControl))
								//{
								//	if (clock_auto.GetElapsedTime() > .5)
								//	{
								//		reset_auto_timer = 1;
								//		if (starsystem[k].planet[l].auto_send == 0)
								//			starsystem[k].planet[l].auto_send = 1;
								//		else
								//			starsystem[k].planet[l].auto_send = 0;
								//	}
								//}
								// send fleet if conditions are ok
								if (starsystem[k].planet[l].fleets > 0 && (sqrt(pow((double)starsystem[k].planet[l].position.x-starsystem[i].planet[j].position.x,2)+ pow((double)starsystem[k].planet[l].position.y-starsystem[i].planet[j].position.y,2)) <= player[1].fleet_range || k != i) && !(i==k && j==l) && starsystem[k].planet[l].fleets > 0 && starsystem[i].planet[j].fleets < 10 && starsystem[system_selected].nearby[i])
									Send_Fleet(k,l,i,j,1);
							}
						}
					}
					// check all selected ships
					for (int k = 0; k <= 199; k++)
					{
						if (fleet[k].selected)
						{
							fleet[k].target_system = i;
							fleet[k].target_planet = j;
							fleet[k].target_angle = (float)(180+(180/PI)*(atan2((double)fleet[k].position.y-starsystem[fleet[k].target_system].planet[fleet[k].target_planet].position.y,(double)fleet[k].position.x-starsystem[fleet[k].target_system].planet[fleet[k].target_planet].position.x)));
						}
					}
				}
			}
			else
			{
				// increase the "not_hovered" counter, if we are not hovering over any planet ..
				starsystem[i].planet[j].hovered = 0;
				not_hovered += 1;
				if (not_hovered == total_planets)
				{
					if (active_cursor != CURSOR_DISRUPTION && active_cursor != CURSOR_TURRET && active_cursor != CURSOR_MINE)
						active_cursor = CURSOR_DEFAULT;
					if (Input.IsMouseButtonDown(sf::Mouse::Left))
					{
						// build turret
						if (planet_selected_count == 1 && active_cursor == CURSOR_TURRET && player[1].credits >= CREDITS_TURRET && player[1].credits >= METAL_TURRET && sqrt(pow((double)starsystem[system_selected].planet[planet_selected].position.x-mouse2.x,2)+pow((double)starsystem[system_selected].planet[planet_selected].position.y-mouse2.y,2)) <= player[1].fleet_range && icon_hover == 0)
						{
							starsystem[system_selected].planet[planet_selected].has_turret = 1;
							starsystem[system_selected].planet[planet_selected].position_turret = mouse2;
							starsystem[system_selected].planet[planet_selected].turret_angle = (float)sf::Randomizer::Random(0, 359);
							active_cursor = CURSOR_DEFAULT;
							player[1].credits -= CREDITS_TURRET;
							player[1].metal -= METAL_TURRET;
						}
						// build mine
						if (planet_selected_count == 1 && active_cursor == CURSOR_MINE && player[1].credits >= CREDITS_MINE && player[1].metal >= METAL_MINE && icon_hover == 0)
						{
							for (int k = 1; k <= mission[0].starsystems; k++)
							{
								for (int l = 1; l <= starsystem[k].rocks; l++)
								{
									if (mouse2.x > starsystem[k].rock[l].position.x - 10 && mouse2.x < starsystem[k].rock[l].position.x + 10 && mouse2.y > starsystem[k].rock[l].position.y - 10 && mouse2.y < starsystem[k].rock[l].position.y + 10 && sqrt(pow((double)starsystem[system_selected].planet[planet_selected].position.x-mouse2.x,2)+pow((double)starsystem[system_selected].planet[planet_selected].position.y-mouse2.y,2)) <= player[1].fleet_range && starsystem[system_selected].planet[planet_selected].has_mine == 0 && starsystem[k].rock[l].has_mine == 0)
									{
										starsystem[system_selected].planet[planet_selected].has_mine = 1;
										starsystem[system_selected].planet[planet_selected].position_mine = starsystem[k].rock[l].position;
										active_cursor = CURSOR_DEFAULT;
										player[1].credits -= CREDITS_MINE;
										player[1].metal -= METAL_MINE;
									}
								}
							}
						}
					}
					// deselect all planets, turrets and ships
					if (Input.IsMouseButtonDown(sf::Mouse::Right))
					{
						for (int k = 1; k <= mission[0].starsystems; k++)
						{
							for (int l = 1; l <= starsystem[k].planets; l++)
							{
								starsystem[k].planet[l].selected = 0;
								starsystem[k].planet[l].turret_selected = 0;
							}
						}
						for (int k = 0; k <= 199; k++)
							fleet[k].selected = false;

						planet_selected_count = 0;
						planet_selected = 0;
						system_selected = 0;
						if (active_cursor == CURSOR_DISRUPTION || active_cursor == CURSOR_TURRET || active_cursor == CURSOR_MINE)
							active_cursor = CURSOR_DEFAULT;
					}
				}
			}
		}
	}

	// Reset auto_send timer
	if (reset_auto_timer == 1)
		clock_auto.Reset();

	// Mousewheel zoom
	if (Event.Type == sf::Event::MouseWheelMoved)
	{
		sf::Vector2f ViewCenter = View.GetCenter();

		if (Event.MouseWheel.Delta > 0)
		{
			zoomCode = 1;
			zoom_counter = 20;
			Event.MouseWheel.Delta = 0;
			zoomMove.x = (mouse2.x - ViewCenter.x) / 20;
			zoomMove.y = (mouse2.y - ViewCenter.y) / 20;
		}
		else if (Event.MouseWheel.Delta < 0)
		{
			zoomCode = 2;
			zoom_counter = 20;
			Event.MouseWheel.Delta = 0;
			zoomMove.x = (mouse2.x - ViewCenter.x) / 20;
			zoomMove.y = (mouse2.y - ViewCenter.y) / 20;
		}
	}

	// Pan view
	if (Input.IsMouseButtonDown(sf::Mouse::Middle))
	{
		if (mouse_old.x == -1)
		{
			mouse_old = mouse;
			active_cursor = CURSOR_SCROLL;
		}
		if (clock_mouse.GetElapsedTime() > .01)
		{
			clock_mouse.Reset();
			if (mouse.x > mouse_old.x)
				View.Move((float)(mouse.x - mouse_old.x)/3, 0);
			if (mouse.x < mouse_old.x)
				View.Move((float)(mouse.x - mouse_old.x)/3, 0);
			if (mouse.y > mouse_old.y)
				View.Move(0, (float)(mouse.y - mouse_old.y)/3);
			if (mouse.y < mouse_old.y)
				View.Move(0, (float)(mouse.y - mouse_old.y)/3);
		}
	}
	else
	{
		// reset mouse after panning
		if (mouse_old.x > -1)
		{
			mouse_old.x = -1;
			active_cursor = CURSOR_DEFAULT;
		}
	}

	// Get starting point for selection box
	if (Input.IsMouseButtonDown(sf::Mouse::Left) && active_cursor == CURSOR_DEFAULT)
	{
		if (mouse_still_pressed == 0)
		{
			mouse_still_pressed = 1;
			box = mouse;
			box2 = App.ConvertCoords(box.x, box.y, &View);
		}
	}
	else
	{
		if (mouse_still_pressed == 1)
		{
			mouse_still_pressed = 0;
			// select all planets within the box
			for (int i = 1; i <= mission[0].starsystems; i++)
			{
				for (int j = 1; j <= starsystem[i].planets; j++)
				{
					if (starsystem[i].planet[j].owner == 1) // do this only for the player planets
					{
						if (mouse2.x != box2.x || mouse2.y != box2.y)
							starsystem[i].planet[j].selected = 0;
						if (mouse2.x > box2.x && mouse2.y > box2.y)
						{
							if (starsystem[i].planet[j].position.x > box2.x && starsystem[i].planet[j].position.x < mouse2.x && starsystem[i].planet[j].position.y > box2.y && starsystem[i].planet[j].position.y < mouse2.y) // center to bottom right
							{
								starsystem[i].planet[j].selected = 1;
								if (sound[0].GetStatus() == sf::Sound::Stopped)
								{
									sound[0].Play();
									planet_selected_count += 1;
								}
							}
						}
						else if (mouse2.x > box2.x && mouse2.y < box2.y)
						{
							if (starsystem[i].planet[j].position.x > box2.x && starsystem[i].planet[j].position.x < mouse2.x && starsystem[i].planet[j].position.y < box2.y && starsystem[i].planet[j].position.y > mouse2.y) // center to top right
							{
								starsystem[i].planet[j].selected = 1;
								if (sound[0].GetStatus() == sf::Sound::Stopped)
								{
									sound[0].Play();
									planet_selected_count += 1;
								}
							}
						}
						else if (mouse2.x < box2.x && mouse2.y > box2.y)
						{
							if (starsystem[i].planet[j].position.x < box2.x && starsystem[i].planet[j].position.x > mouse2.x && starsystem[i].planet[j].position.y > box2.y && starsystem[i].planet[j].position.y < mouse2.y) // center to bottom left
							{
								starsystem[i].planet[j].selected = 1;
								if (sound[0].GetStatus() == sf::Sound::Stopped)
								{
									sound[0].Play();
									planet_selected_count += 1;
								}
							}
						}
						else if (mouse2.x < box2.x && mouse2.y < box2.y)
						{
							if (starsystem[i].planet[j].position.x < box2.x && starsystem[i].planet[j].position.x > mouse2.x && starsystem[i].planet[j].position.y < box2.y && starsystem[i].planet[j].position.y > mouse2.y) // center to top left
							{
								starsystem[i].planet[j].selected = 1;
								if (sound[0].GetStatus() == sf::Sound::Stopped)
								{
									sound[0].Play();
									planet_selected_count += 1;
								}
							}
						}
					}
				}
			}
		}
	}

}

void Send_Fleet(int home_system, int home_planet, int target_system, int target_planet, int play_sound)
{
	for (int i = 0; i <= 199; i++)
	{
		if (fleet[i].built && fleet[i].status == FLEET_ORBITING && fleet[i].target_system == home_system && fleet[i].target_planet == home_planet)
		{
			starsystem[home_system].planet[home_planet].fleets -= 1;
			fleet[i].status = FLEET_TRAVELLING;
			fleet[i].angle -= 90;
			if (fleet[i].angle < 0)
				fleet[i].angle += 360;
			fleet[i].last_target_system = fleet[i].target_system;
			fleet[i].last_target_planet = fleet[i].target_planet;
			fleet[i].target_system = target_system;
			fleet[i].target_planet = target_planet;
			fleet[i].target_angle = (float)(180+(180/PI)*(atan2((double)fleet[i].position.y-starsystem[target_system].planet[target_planet].position.y,(double)fleet[i].position.x-starsystem[target_system].planet[target_planet].position.x)));
			if (starsystem[target_system].planet[target_planet].owner != starsystem[home_system].planet[home_planet].owner)
				starsystem[target_system].planet[target_planet].attacked = true;
			if (play_sound)
				sound[3].Play();
			break;
		}
	}
}

void Add_Fleet(int system, int planet)
{
	for (int i = 0; i <= 199; i++)
	{
		if (!fleet[i].built)
		{
			fleet[i].built = true;
			fleet[i].owner = starsystem[system].planet[planet].owner;
			fleet[i].health = 100 * player[starsystem[system].planet[planet].owner].fleet_health;
			fleet[i].target_system = system;
			fleet[i].target_planet = planet;
			fleet[i].target_fleet = 0;
			fleet[i].angle = sf::Randomizer::Random(.0f,359.0f);
			fleet[i].position.x = (float)(starsystem[system].planet[planet].position.x + cos((double)fleet[i].angle* PI/180)*50);
			fleet[i].position.y = (float)(starsystem[system].planet[planet].position.y + sin((double)fleet[i].angle* PI/180)*50);
			fleet[i].status = FLEET_ORBITING;
			fleet[i].shooting = false;
			starsystem[system].planet[planet].fleets += 1;
			player[fleet[i].owner].fleets_built += 1;
			break;
		}
	}
}
