if Option.Debug.GetInfo == true then UserInfo.AddVal("Create", 1) end
GameOn = true

White = Color.new(255,255,255)
Black = Color.new(0,0,0)

local t1 = 0
local tbool = false
GenWorldCG = 1
-- 1, 2, 3

--CreateBG1 = Image.load("Game Resources/Backgrounds/CreateBG1.png")
--CreateBG2 = Image.load("Game Resources/Backgrounds/CreateBG2.png")
local Panel1 = Image.load("Game Resources/Backgrounds/Panel3.png")
local SelectionPanel = Image.load("Game Resources/Backgrounds/SelectionPanel.png")

local GameCreateBG = Image.load("Game Resources/Backgrounds/GameCreateBG.png")

-- Height = 13, Width = 15
local ChooseButton = Image.load("Game Resources/Buttons/Buttons/ChooseButtonOff.png")

--114, 277 Cancel Button

--Panel2 = Image.load("Game Resources/Backgrounds/Panel4.png")
local SelectionPanel = Image.load("Game Resources/Backgrounds/SelectionPanel.png")
 
local TeamButtons = {
	Team1 = Image.load("Game Resources/Buttons/Buttons/TeamButton1.png"),
	Team2 = Image.load("Game Resources/Buttons/Buttons/TeamButton2.png"),
	TeamNone = Image.load("Game Resources/Buttons/Buttons/TeamButton-.png")
}

local Switch = {
	Yes = Image.load("Game Resources/Buttons/Buttons/SwitchYes.png"),
	No = Image.load("Game Resources/Buttons/Buttons/SwitchNo.png")
}

------ MapType:
---- Grass
---- Snow
---- Desert
local MapType = ""
local Global = {
	Player = {
		Color = "",
		Civ = "",
		Resources = "",
	},
}
local Selection1 = 1
local GameCreated = "No"
 
local CurX = 0
local CurY = 0

---- Font

-- Arial
Arial = Font.load("Font/Arial.TTF")
Arial:setPixelSizes(11,11)

local DT1 = ""
local DT2 = ""
local DT3 = ""

local function TempWorld()
	TempCFG = io.open("Configuration/TempWorldValues.cfg", "w")
	TempCFG:write("Global.World.Theme = "..string.format('%q',MapType).."\n")
	TempCFG:write("Global.Player.Color = "..string.format('%q',Global.Player.Color).."\n")
	TempCFG:write("ResAmount = "..string.format('%q',Global.Player.Resources).."\n")
	TempCFG:write("Global.Player.Civ = "..string.format('%q',Global.Player.Civ).."\n")
	TempCFG:write("GenWorld = "..GenWorldCG.."\n")
	TempCFG:write("GameVersion = "..string.format('%q',MV.." "..DevState.." Update "..Update.." "..Prefix))
	TempCFG:close()
end

local Main = {
	
	Switch = 1,
	
	Map = {
		Main = Color.new(0,0,0),
		
		GrassCol = Color.new(0,0,0),
		SnowCol = Color.new(0,0,0),
		DesertCol = Color.new(0,0,0),
		Select = 1,
	},
	
	
	Team = {
		Main = Color.new(0,0,0),
		
		BlueCol = Color.new(0,0,0),
		RedCol = Color.new(0,0,0),
		Select = 1,
	},
	
	
	Res = {
		Main = Color.new(0,0,0),
		
		ZeroCol = Color.new(0,0,0),
		LittleCol = Color.new(0,0,0),
		NormalCol = Color.new(0,0,0),
		ManyCol = Color.new(0,0,0),
		Select = 1,
	},
	
	Civ = {
		Main = Color.new(0,0,0),
		
		CeltsColor = Color.new(0,0,0),
		JapaneseColor = Color.new(0,0,0),
		ByzantiesColor = Color.new(0,0,0),
		PersiansColor = Color.new(0,0,0),
		BritonsColor = Color.new(0,0,0),
		MongolsColor = Color.new(0,0,0),
		SaracensColor = Color.new(0,0,0),
		VikingsColor = Color.new(0,0,0),
		--FranksColor = Color.new(0,0,0),
		--AztequesColor = Color.new(0,0,0),
		--SlavsColor = Color.new(0,0,0),
		--ItaliansColor = Color.new(0,0,0),
		--SpanishColor = Color.new(0,0,0),
		--TurksColor = Color.new(0,0,0),
		--ChineseColor = Color.new(0,0,0),
		--KoreansColor = Color.new(0,0,0),
		--GothsColor = Color.new(0,0,0),
		--TeutonsColor = Color.new(0,0,0),
		--HunsColor = Color.new(0,0,0),
		--MayansColor = Color.new(0,0,0),
		--IndiansColor = Color.new(0,0,0),
		--IncasColor = Color.new(0,0,0),
		--MagyarsColor = Color.new(0,0,0),
		--BerbersColor = Color.new(0,0,0),
		--MaliansColor = Color.new(0,0,0),
		--PortugueseColor = Color.new(0,0,0),
		--EthiopiansColor = Color.new(0,0,0),
		
		Select = 1,
	},
}

local function Check(Param)
	if Param == "Map" then
		if Main.Map.Select == 1 then
			MapType = "Grass"
			Main.Map.GrassCol = Color.new(255,0,0)
			Main.Map.SnowCol = Color.new(0,0,0)
			Main.Map.DesertCol = Color.new(0,0,0)
		end
		
		if Main.Map.Select == 2 then
			MapType = "Snow"
			Main.Map.GrassCol = Color.new(0,0,0)
			Main.Map.SnowCol = Color.new(255,0,0)
			Main.Map.DesertCol = Color.new(0,0,0)
		end
		
		if Main.Map.Select == 3 then
			MapType = "Desert"
			Main.Map.GrassCol = Color.new(0,0,0)
			Main.Map.SnowCol = Color.new(0,0,0)
			Main.Map.DesertCol = Color.new(255,0,0)
		end
	elseif Param == "Team" then
		
		if Main.Team.Select == 1 then
			Global.Player.Color = "Blue"
			Main.Team.BlueCol = Color.new(255,0,0)
			Main.Team.RedCol = Color.new(0,0,0)
		end
		
		if Main.Team.Select == 2 then
			Global.Player.Color = "Red"
			Main.Team.BlueCol = Color.new(0,0,0)
			Main.Team.RedCol = Color.new(255,0,0)
		end
	elseif Param == "Res" then
		if Main.Res.Select == 0 then
			Player1_Wood = 0
			Player1_Food = 0
			Player1_Gold = 0
			Player1_Stone = 0
			Global.Player.Resources = "Zero"
			Main.Res.ZeroCol = Color.new(255,0,0)
			Main.Res.LittleCol = Color.new(0,0,0)
			Main.Res.NormalCol = Color.new(0,0,0)
			Main.Res.ManyCol = Color.new(0,0,0)
		end
		
		if Main.Res.Select == 1 then
			Player1_Wood = 200
			Player1_Food = 200
			Player1_Gold = 100
			Player1_Stone = 100
			Global.Player.Resources = "Little"
			Main.Res.ZeroCol = Color.new(0,0,0)
			Main.Res.LittleCol = Color.new(255,0,0)
			Main.Res.NormalCol = Color.new(0,0,0)
			Main.Res.ManyCol = Color.new(0,0,0)
		end
		
		if Main.Res.Select == 2 then
			Player1_Wood = 600
			Player1_Food = 600
			Player1_Gold = 400
			Player1_Stone = 400
			Global.Player.Resources = "Normal"
			Main.Res.ZeroCol = Color.new(0,0,0)
			Main.Res.LittleCol = Color.new(0,0,0)
			Main.Res.NormalCol = Color.new(255,0,0)
			Main.Res.ManyCol = Color.new(0,0,0)
		end
		
		if Main.Res.Select == 3 then
			Player1_Wood = 1200
			Player1_Food = 1200
			Player1_Gold = 750
			Player1_Stone = 750
			Global.Player.Resources = "Many"
			Main.Res.ZeroCol = Color.new(0,0,0)
			Main.Res.LittleCol = Color.new(0,0,0)
			Main.Res.NormalCol = Color.new(0,0,0)
			Main.Res.ManyCol = Color.new(255,0,0)
		end
	elseif Param == "Civ" then
		if Main.Civ.Select == 1 then
			Global.Player.Civ = "Celt"
			Main.Civ.CeltsColor = Color.new(255,0,0)
			Main.Civ.JapaneseColor = Color.new(0,0,0)
			Main.Civ.ByzantiesColor = Color.new(0,0,0)
			Main.Civ.PersiansColor = Color.new(0,0,0)
			Main.Civ.BritonsColor = Color.new(0,0,0)
			Main.Civ.MongolsColor = Color.new(0,0,0)
			Main.Civ.SaracensColor = Color.new(0,0,0)
			Main.Civ.VikingsColor = Color.new(0,0,0)
		elseif Main.Civ.Select == 2 then
			Global.Player.Civ = "Japanese"
			Main.Civ.CeltsColor = Color.new(0,0,0)
			Main.Civ.JapaneseColor = Color.new(255,0,0)
			Main.Civ.ByzantiesColor = Color.new(0,0,0)
			Main.Civ.PersiansColor = Color.new(0,0,0)
			Main.Civ.BritonsColor = Color.new(0,0,0)
			Main.Civ.MongolsColor = Color.new(0,0,0)
			Main.Civ.SaracensColor = Color.new(0,0,0)
			Main.Civ.VikingsColor = Color.new(0,0,0)
		elseif Main.Civ.Select == 3 then
			Global.Player.Civ = "Byzanties"
			Main.Civ.CeltsColor = Color.new(0,0,0)
			Main.Civ.JapaneseColor = Color.new(0,0,0)
			Main.Civ.ByzantiesColor = Color.new(255,0,0)
			Main.Civ.PersiansColor = Color.new(0,0,0)
			Main.Civ.BritonsColor = Color.new(0,0,0)
			Main.Civ.MongolsColor = Color.new(0,0,0)
			Main.Civ.SaracensColor = Color.new(0,0,0)
			Main.Civ.VikingsColor = Color.new(0,0,0)
		elseif Main.Civ.Select == 4 then
			Global.Player.Civ = "Persians"
			Main.Civ.CeltsColor = Color.new(0,0,0)
			Main.Civ.JapaneseColor = Color.new(0,0,0)
			Main.Civ.ByzantiesColor = Color.new(0,0,0)
			Main.Civ.PersiansColor = Color.new(255,0,0)
			Main.Civ.BritonsColor = Color.new(0,0,0)
			Main.Civ.MongolsColor = Color.new(0,0,0)
			Main.Civ.SaracensColor = Color.new(0,0,0)
			Main.Civ.VikingsColor = Color.new(0,0,0)
		elseif Main.Civ.Select == 5 then
			Global.Player.Civ = "Britons"
			Main.Civ.CeltsColor = Color.new(0,0,0)
			Main.Civ.JapaneseColor = Color.new(0,0,0)
			Main.Civ.ByzantiesColor = Color.new(0,0,0)
			Main.Civ.PersiansColor = Color.new(0,0,0)
			Main.Civ.BritonsColor = Color.new(255,0,0)
			Main.Civ.MongolsColor = Color.new(0,0,0)
			Main.Civ.SaracensColor = Color.new(0,0,0)
			Main.Civ.VikingsColor = Color.new(0,0,0)
		elseif Main.Civ.Select == 6 then
			Global.Player.Civ = "Mongols"
			Main.Civ.CeltsColor = Color.new(0,0,0)
			Main.Civ.JapaneseColor = Color.new(0,0,0)
			Main.Civ.ByzantiesColor = Color.new(0,0,0)
			Main.Civ.PersiansColor = Color.new(0,0,0)
			Main.Civ.BritonsColor = Color.new(0,0,0)
			Main.Civ.MongolsColor = Color.new(255,0,0)
			Main.Civ.SaracensColor = Color.new(0,0,0)
			Main.Civ.VikingsColor = Color.new(0,0,0)
		elseif Main.Civ.Select == 7 then
			Global.Player.Civ = "Saracens"
			Main.Civ.CeltsColor = Color.new(0,0,0)
			Main.Civ.JapaneseColor = Color.new(0,0,0)
			Main.Civ.ByzantiesColor = Color.new(0,0,0)
			Main.Civ.PersiansColor = Color.new(0,0,0)
			Main.Civ.BritonsColor = Color.new(0,0,0)
			Main.Civ.MongolsColor = Color.new(0,0,0)
			Main.Civ.SaracensColor = Color.new(255,0,0)
			Main.Civ.VikingsColor = Color.new(0,0,0)
		elseif Main.Civ.Select == 8 then
			Global.Player.Civ = "Vikings"
			Main.Civ.CeltsColor = Color.new(0,0,0)
			Main.Civ.JapaneseColor = Color.new(0,0,0)
			Main.Civ.ByzantiesColor = Color.new(0,0,0)
			Main.Civ.PersiansColor = Color.new(0,0,0)
			Main.Civ.BritonsColor = Color.new(0,0,0)
			Main.Civ.MongolsColor = Color.new(0,0,0)
			Main.Civ.SaracensColor = Color.new(0,0,0)
			Main.Civ.VikingsColor = Color.new(255,0,0)
		end
	elseif Param == "Main" then
		if Main.Switch == 1 then
			Main.Map.Main = Color.new(255,0,0)
			Main.Team.Main = Color.new(0,0,0)
			Main.Res.Main = Color.new(0,0,0)
			Main.Civ.Main = Color.new(0,0,0)
			
			DT1 = "Select ground"
			DT2 = ""
			DT3 = ""
		elseif Main.Switch == 2 then
			Main.Map.Main = Color.new(0,0,0)
			Main.Team.Main = Color.new(255,0,0)
			Main.Res.Main = Color.new(0,0,0)
			Main.Civ.Main = Color.new(0,0,0)
			
			DT1 = "Select the color of your team"
			DT2 = ""
			DT3 = ""
		elseif Main.Switch == 3 then
			Main.Map.Main = Color.new(0,0,0)
			Main.Team.Main = Color.new(0,0,0)
			Main.Res.Main = Color.new(255,0,0)
			Main.Civ.Main = Color.new(0,0,0)
			
			DT1 = "Select the amount of your resources"
			DT2 = ""
			DT3 = ""
		elseif Main.Switch == 4 then
			Main.Map.Main = Color.new(0,0,0)
			Main.Team.Main = Color.new(0,0,0)
			Main.Res.Main = Color.new(0,0,0)
			Main.Civ.Main = Color.new(255,0,0)
			
			DT1 = "Select your civilization"
			DT2 = ""
			DT3 = ""
		end
	end
end

local function CheckALL()
	Check("Map")
	Check("Team")
	Check("Res")
	Check("Civ")
	Check("Main")
end

local OneTime = true

while GameOn do
	pad = Controls.read()
	
	GenWorldCG = GenWorldCG + 1
	if GenWorldCG >= 3 then GenWorldCG = 1 end
	
	screen:blit(0,0,GameCreateBG)
	--screen:blit(0,0,CreateBG1)
	--screen:blit(265,11,Panel2)
	--screen:blit(0,0,CreateBG2)
	screen:blit(200,181,Panel1)
	
	screen:fontPrint(Arial,5,5,"Gen: "..GenWorldCG,Black)
	
	
	screen:fontPrint(Arial,85,28,"Standart Play",Black)
	screen:fontPrint(Arial,330,28,"Game Settings",Black)
	
	screen:fontPrint(Arial,38,45,"Map Theme",Main.Map.Main)
	screen:fontPrint(Arial,38,65,"Grass",Main.Map.GrassCol)
	screen:fontPrint(Arial,38,75,"Snow",Main.Map.SnowCol)
	screen:fontPrint(Arial,38,85,"Desert",Main.Map.DesertCol)
	
	screen:fontPrint(Arial,115,45,"Team Color",Main.Team.Main)
	screen:fontPrint(Arial,115,65,"Blue",Main.Team.BlueCol)
	screen:fontPrint(Arial,115,75,"Red",Main.Team.RedCol)
	
	screen:fontPrint(Arial,185,45,"Resources",Main.Res.Main)
	screen:fontPrint(Arial,185,65,"Zero",Main.Res.ZeroCol)
	screen:fontPrint(Arial,185,75,"Little",Main.Res.LittleCol)
	screen:fontPrint(Arial,185,85,"Normal",Main.Res.NormalCol)
	screen:fontPrint(Arial,185,95,"Many",Main.Res.ManyCol)
	
	screen:fontPrint(Arial,38,110,"Civilization",Main.Civ.Main)
	screen:fontPrint(Arial,38,130,"Celts",Main.Civ.CeltsColor)
	screen:fontPrint(Arial,38,140,"Japanese",Main.Civ.JapaneseColor)
	screen:fontPrint(Arial,38,150,"Byzanties",Main.Civ.ByzantiesColor)
	screen:fontPrint(Arial,38,160,"Persians",Main.Civ.PersiansColor)
	screen:fontPrint(Arial,38,170,"Britons",Main.Civ.BritonsColor)
	screen:fontPrint(Arial,38,180,"Mongols",Main.Civ.MongolsColor)
	screen:fontPrint(Arial,38,190,"Saracens",Main.Civ.SaracensColor)
	screen:fontPrint(Arial,38,200,"Vikings",Main.Civ.VikingsColor)
	
	screen:fontPrint(Arial,210,210,DT1,White)
	screen:fontPrint(Arial,210,225,DT2,White)
	screen:fontPrint(Arial,210,230,DT3,White)
	
	-- R.I.P. Cursor (28.08.2019)
	--screen:blit(CurX, CurY, IMG_Cursor)
	
	
	-- One Time Checking Everything
	if OneTime == true then
		CheckALL()
		OneTime = false
	end
	
	if tbool == true then
		if t1 >= 0 then t1 = t1 + 1 end
		if t1 == 1 then
			Mp3me.stop()
			Mp3me.load("Sounds/select2.mp3")
			Mp3me.play()
		end
		if t1 == 15 then
			if Global.Player.Civ == "Celt" then
				Mp3me.stop()
				Mp3me.load("Sounds/stream/Celt.mp3")
				Mp3me.play()
			elseif Global.Player.Civ == "Japanese" then
				Mp3me.stop()
				Mp3me.load("Sounds/stream/Japanese.mp3")
				Mp3me.play()
			elseif Global.Player.Civ == "Byzanties" then
				Mp3me.stop()
				Mp3me.load("Sounds/stream/Byzantin.mp3")
				Mp3me.play()
			elseif Global.Player.Civ == "Persians" then
				Mp3me.stop()
				Mp3me.load("Sounds/stream/Persian.mp3")
				Mp3me.play()
			elseif Global.Player.Civ == "Britons" then
				Mp3me.stop()
				Mp3me.load("Sounds/stream/British.mp3")
				Mp3me.play()
			elseif Global.Player.Civ == "Mongols" then
				Mp3me.stop()
				Mp3me.load("Sounds/stream/Mongol.mp3")
				Mp3me.play()
			elseif Global.Player.Civ == "Saracens" then
				Mp3me.stop()
				Mp3me.load("Sounds/stream/Saracen.mp3")
				Mp3me.play()
			elseif Global.Player.Civ == "Vikings" then
				Mp3me.stop()
				Mp3me.load("Sounds/stream/Viking.mp3")
				Mp3me.play()
			end
		end
		if t1 >= 50 then
			t1 = nil
			tbool = nil
			Fade(25)
			SavePath("0")
			dofile("GameScript.lua")
		end
	end
	
	
	
	
	
	
	
	
	
	--Selection1
	--if Selection1 == 1 then MapType = "Grass" end
	--if Selection1 == 2 then MapType = "Snow" end
	--if Selection1 == 3 then MapType = "Desert" end
	
	--Selection2
	
	
	
	--------------------------------------------------------------------------
                                  --Controls--
	--------------------------------------------------------------------------
	
	if pad:cross() then
		tbool = true
		GameCreated = "Yes"
		TempWorld()
		--MapChoise = 0 -- Temp
		SavePath("0")
	end
	
	if pad:down() then
		if pressed.down == false then
			pressed.down = true
			if Main.Switch == 1 then
				Main.Map.Select = Main.Map.Select + 1
				if Main.Map.Select >= 4 then Main.Map.Select = 3 end
				Check("Map")
			elseif Main.Switch == 2 then
				Main.Team.Select = Main.Team.Select + 1
				if Main.Team.Select >= 3 then Main.Team.Select = 2 end
				Check("Team")
			elseif Main.Switch == 3 then
				Main.Res.Select = Main.Res.Select + 1
				if Main.Res.Select >= 4 then Main.Res.Select = 3 end
				Check("Res")
			elseif Main.Switch == 4 then
				Main.Civ.Select = Main.Civ.Select + 1
				if Main.Civ.Select > 8 then Main.Civ.Select = 8 end
				Check("Civ")
			end
		end
	else
		pressed.down = false
	end
	
	
	
	
	
	
	if pad:up() then
		if pressed.up == false then
			pressed.up = true
			if Main.Switch == 1 then
				Main.Map.Select = Main.Map.Select - 1
				if Main.Map.Select <= 0 then Main.Map.Select = 1 end
				Check("Map")
			elseif Main.Switch == 2 then
				Main.Team.Select = Main.Team.Select - 1
				if Main.Team.Select <= 0 then Main.Team.Select = 1 end
				Check("Team")
			elseif Main.Switch == 3 then
				Main.Res.Select = Main.Res.Select - 1
				if Main.Res.Select <= -1 then Main.Res.Select = 0 end
				Check("Res")
			elseif Main.Switch == 4 then
				Main.Civ.Select = Main.Civ.Select - 1
				if Main.Civ.Select < 1 then Main.Civ.Select = 1 end
				Check("Civ")
			end
		end
	else
		pressed.up = false
	end
	
	
	---------------------------
	
	
	if pad:l() then
		if pressed.l == false then
			pressed.l = true
			Main.Switch = Main.Switch - 1
			if Main.Switch <= 0 then Main.Switch = 1 end
			Check("Main")
		end
	else
		pressed.l = false
	end
	
	if pad:r() then
		if pressed.r == false then
			pressed.r = true
			Main.Switch = Main.Switch + 1
			if Main.Switch >= 5 then Main.Switch = 4 end
			Check("Main")
		end
	else
		pressed.r = false
	end
	
	if pad:circle() then dofile("MainMenu.lua") end
	
	if pad:select() then
		Screenshot()
	end
	
	
	
	screen.waitVblankStart(1)
	screen.flip()
end
