-- Created By CoderX, Team Matrix (2006 Year)
-- GENERAL PUBLIC LICENSE V0.2

-- Game Continue by MasterMen in 2017, 2018, 2019 マスターメン
-- Attempt #2 (cuz first was deleted :/)
-- Start: 2.06.2017, 14:48 (First Screenshot ever)

-- Grid Free Edition

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

mState = "Game"
--"Game"
--"Menu"
--"Options"

TempGlobals()

ScreenCount = 0

MapColor = {
	Map1 = Color.new(0,0,0),
	Map2 = Color.new(0,0,0),
	Map3 = Color.new(0,0,0),
}

--[[
MapChoiseCFG = io.open("Configuration/MapChoise.cfg", "r")
MapChoise = tostring(MapChoiseCFG:read())
MapChoiseCFG:close()
--]]



dofile("Configuration/MapChoise.cfg")

-- load Config, not a world.
function LoadWorld()
	dofile("Configuration/TempWorldValues.cfg")
	
	MapType = Global.World.Theme
	Global.Player.Resources = ResAmount
end

function ResCheck()
	if Global.Player.Resources == "Zero" then
		Global.Player.Wood = 0
		Global.Player.Food = 0
		Global.Player.Stone = 0
		Global.Player.Gold = 0
	end
	if Global.Player.Resources == "Little" then
		Global.Player.Wood = 200
		Global.Player.Food = 200
		Global.Player.Stone = 100
		Global.Player.Gold = 100
	end
	if Global.Player.Resources == "Normal" then
		Global.Player.Wood = 600
		Global.Player.Food = 600
		Global.Player.Stone = 400
		Global.Player.Gold = 400
	end
	if Global.Player.Resources == "Many" then
		Global.Player.Wood = 1200
		Global.Player.Food = 1200
		Global.Player.Stone = 750
		Global.Player.Gold = 750
	end
	-- You don't have to use cheats...
end

--[[
if MapChoise == "C1" then
	LoadCustomMap("CustomMap1", "scn2psp")
elseif MapChoise == "C2" then
	LoadCustomMap("CustomMap2", "scn2psp")
elseif MapChoise == "C3" then
	LoadCustomMap("CustomMap3", "scn2psp")
--]]

--nilSprites()
Textures("Delete")
	
if MapChoise == "E1" then
	LoadCustomMap("CustomMap1", "scn2psp")
elseif MapChoise == "E2" then
	LoadCustomMap("CustomMap2", "scn2psp")
elseif MapChoise == "E3" then
	LoadCustomMap("CustomMap3", "scn2psp")
elseif MapChoise == "E4" then
	LoadCustomMap("CustomMap4", "scn2psp")
elseif MapChoise == "E5" then
	LoadCustomMap("CustomMap5", "scn2psp")
elseif MapChoise == "E6" then
	LoadCustomMap("CustomMap6", "scn2psp")
	
elseif MapChoise == "0" then
	LoadWorld()
	ResCheck()
	LoadWorld = nil
	ResCheck = nil
elseif MapChoise == "1" then
	dofile("Configuration/Map1.cfg")
elseif MapChoise == "2" then
	dofile("Configuration/Map2.cfg")
elseif MapChoise == "3" then
	dofile("Configuration/Map3.cfg")
else
	dofile("MainMenu.lua")
end

if Global.Player.Color == "Blue" then Global.Player.NickCol = Color.new(0,0,255) end
if Global.Player.Color == "Red" then Global.Player.NickCol = Color.new(255,0,0) end

SaveIndex = 1

OptionsBG = Image.load("Game Resources/Backgrounds/OptionsBG.png")
PanelBG = Image.load("Game Resources/Backgrounds/PanelBG.png")

ControlsPicture = Image.load("Game Resources/Backgrounds/ControlsGame.png")

GameMenu = Image.load("Game Resources/Backgrounds/GameMenu.png")
EmptyButton = Image.load("Game Resources/Buttons/EmptyButton.png")

if MapChoise == "0" then
	TempObjects(GenWorld)
else
--------------------------------------------------------------------------------------------------
--[[-----------------------------------]] Textures("Load") --[[-----------------------------------]]
--------------------------------------------------------------------------------------------------
	LoadObj()
	LoadObj = nil
end






if Global.Player.Nick == "" or Global.Player.Nick == " " then
	NickCFG = io.open("Configuration/Nickname.cfg", "r")
	Global.Player.Nick = tostring(NickCFG:read())
	NickCFG:close()
end

CurButton = 1
CurButton_old = CurButton

ButtonColor = {
	ResumeGame = Color.new(0,0,0),
	Options = Color.new(0,0,0),
	Controls = Color.new(0,0,0),
	Save = Color.new(0,0,0),
	ExitSave = Color.new(0,0,0),
	Exit = Color.new(0,0,0)
}

local Back = {X = 0, Y = 0}

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

Cursor = { X = 240, Y = 136, LastX = 0, LastY = 0, IMG_DEF = spr.assets.Cursor.Default, IMG_RES = spr.assets.Cursor.Resources, IMG_ATT =  spr.assets.Cursor.Attack, Speed = Option.Game.CursorSpeed, ScrollSpeed = Option.Game.ScrollSpeed, Under = nil}

--[[
World = {}
for i = 1, 5 do
	World[i][i] = {Ground = "Grass", Object = nil}
end
--]]

--[[ this is the correct example. I will work on it later...
World = {}
for i = 1, 5 do
	for j = 1, 5 do
		World[i] = {}
		World[i][j] = {Ground = "Grass", Object = nil, ObjPart = Left}
	end
end

i wanna make grid on Update 4 or maybe 5, idk.
--]]
--[[
World[25][25] {
	{A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A},
	{A,A,A,A,A,A,A,A,A,A,A,A,B,O,R,E,D,A,A,A,A,A,A,A,A},
	{A,A,A,A,A,A,A,A,A,A,A,A,A,W,I,T,H,A,A,A,A,A,A,A,A},
	{A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,I,T,A,A,A,A,A,A,A,A},
	{A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A},
	{A,A,A,A,A,A,A,A,A,A,A,R,E,A,L,L,Y,A,A,A,A,A,A,A,A},
	{A,2,0,1,1,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A},
	{A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A},
	{A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A},
	{A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A},
	{A,A,A,A,A,A,A,A,A,A,E,E,E,A,A,A,A,A,A,A,A,A,A,A,A},
	{A,A,A,A,A,A,A,A,A,A,E,E,E,A,A,A,A,A,A,A,A,A,A,A,A},
	{A,A,A,A,A,A,A,A,A,A,E,E,E,A,A,A,A,A,A,A,A,A,A,A,A},
	{A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A},
	{A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A},
	{A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A},
	{A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A},
	{A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A},
	{A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A},
	{A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A},
	{A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A},
	{A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A},
	{A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A},
	{A,A,F,A,A,O,A,A,O,A,A,L,A,A,S,A,A,A,A,A,A,A,A,A,A},
	{A,A,D,O,N,',T,A,R,U,N,A,a,W,A,Y,A,A,A,A,A,A,A,A,A},
}
--]]




--[[
if MapChoise ~= "C1" or  MapChoise ~= "C2" or MapChoise ~= "C3" then
	-- Load objects
end
--]]





function UnSelectUnits(Type)
	if Type == "for" then
		for i = 1, Max.Unit.Villager do
			Villager[i].Selected = false
		end
		for i = 1, Max.Unit.Militia do
			Militia[i].Selected = false
		end
	elseif Type == "Classic" then
		Villager[1].Selected = false
		Villager[2].Selected = false
		Villager[3].Selected = false
		Villager[4].Selected = false
		Villager[5].Selected = false
		Villager[6].Selected = false
		Villager[7].Selected = false
		Villager[8].Selected = false
		Villager[9].Selected = false
		Villager[10].Selected = false
		Villager[11].Selected = false
		Villager[12].Selected = false
		Villager[13].Selected = false
		Villager[14].Selected = false
		Villager[15].Selected = false
		Villager[16].Selected = false
		Militia[1].Selected = false
		Militia[2].Selected = false
		Militia[3].Selected = false
		Militia[4].Selected = false
		Militia[5].Selected = false
		Militia[6].Selected = false
		Militia[7].Selected = false
		Militia[8].Selected = false
		Militia[9].Selected = false
		Militia[10].Selected = false
		Militia[11].Selected = false
		Militia[12].Selected = false
		Militia[13].Selected = false
		Militia[14].Selected = false
		Militia[15].Selected = false
		Militia[16].Selected = false
	end
end


function UnSelectBuildings(Type)
	if Type == "for" then
		for i = 1, Max.Building.TownCenter do
			TownCenter[i].Selected = false
		end
		for i = 1, Max.Building.House do
			House[i].Selected = false
		end
	elseif Type == "Classic" then
		House[1].Selected = false
		House[2].Selected = false
		House[3].Selected = false
		House[4].Selected = false
		House[5].Selected = false
		House[6].Selected = false
		House[7].Selected = false
		House[8].Selected = false
		House[9].Selected = false
		House[10].Selected = false
		House[11].Selected = false
		House[12].Selected = false
		House[13].Selected = false
		House[14].Selected = false
		House[15].Selected = false
		House[16].Selected = false
		TownCenter[1].Selected = false
		TownCenter[2].Selected = false
		TownCenter[3].Selected = false
		TownCenter[4].Selected = false
		TownCenter[5].Selected = false
		TownCenter[6].Selected = false
		TownCenter[7].Selected = false
		TownCenter[8].Selected = false
		TownCenter[9].Selected = false
		TownCenter[10].Selected = false
		TownCenter[11].Selected = false
		TownCenter[12].Selected = false
		TownCenter[13].Selected = false
		TownCenter[14].Selected = false
		TownCenter[15].Selected = false
		TownCenter[16].Selected = false
	end
end
-- Hello MasterMen from 2019 or 2020, look at this shit-code xD
-- I hope you will programm better than 2018 me :)


function UnSelectResources(Type)
	if Type == "for" then
		for i = 1, Max.Other.Berry do
			Berry[i].Selected = false
		end
		for i = 1, Max.Other.Tree do
			Tree[i].Selected = false
		end
		for i = 1, Max.Other.Gold do
			Gold[i].Selected = false
		end
		for i = 1, Max.Other.Stone do
			Stone[i].Selected = false
		end
	elseif Type == "Classic" then
		Berry[1].Selected = false
		Berry[2].Selected = false
		Berry[3].Selected = false
		Berry[4].Selected = false
		Berry[5].Selected = false
		Berry[6].Selected = false
		Berry[7].Selected = false
		Berry[8].Selected = false
		Berry[9].Selected = false
		Berry[10].Selected = false
		Berry[11].Selected = false
		Berry[12].Selected = false
		Berry[13].Selected = false
		Berry[14].Selected = false
		Berry[15].Selected = false
		Berry[16].Selected = false
		
		Tree[1].Selected = false
		Tree[2].Selected = false
		Tree[3].Selected = false
		Tree[4].Selected = false
		Tree[5].Selected = false
		Tree[6].Selected = false
		Tree[7].Selected = false
		Tree[8].Selected = false
		Tree[9].Selected = false
		Tree[10].Selected = false
		Tree[11].Selected = false
		Tree[12].Selected = false
		Tree[13].Selected = false
		Tree[14].Selected = false
		Tree[15].Selected = false
		Tree[16].Selected = false
		Tree[17].Selected = false
		Tree[18].Selected = false
		Tree[19].Selected = false
		Tree[20].Selected = false
		Tree[21].Selected = false
		Tree[22].Selected = false
		Tree[23].Selected = false
		Tree[24].Selected = false
		
		Gold[1].Selected = false
		Gold[2].Selected = false
		Gold[3].Selected = false
		Gold[4].Selected = false
		Gold[5].Selected = false
		Gold[6].Selected = false
		Gold[7].Selected = false
		Gold[8].Selected = false
		Gold[9].Selected = false
		Gold[10].Selected = false
		Gold[11].Selected = false
		Gold[12].Selected = false
		Gold[13].Selected = false
		Gold[14].Selected = false
		Gold[15].Selected = false
		Gold[16].Selected = false
		
		Stone[1].Selected = false
		Stone[2].Selected = false
		Stone[3].Selected = false
		Stone[4].Selected = false
		Stone[5].Selected = false
		Stone[6].Selected = false
		Stone[7].Selected = false
		Stone[8].Selected = false
		Stone[9].Selected = false
		Stone[10].Selected = false
		Stone[11].Selected = false
		Stone[12].Selected = false
		Stone[13].Selected = false
		Stone[14].Selected = false
		Stone[15].Selected = false
		Stone[16].Selected = false
	end
end

--[[
bonkSound = Sound.load("bonk.wav",true)

cfm = Sound.load("game/Cits.wav")
--]]

MapTypeBool = true


function UpdateTheme()
	if MapChoise == "0" then
		Global.World.Theme = MapType
		-- Loading Map by file's information
		if MapType == "Grass" and MapTypeBool == true then
			MapTypeBool = false
			spr.assets.MiniMap = Image.load("Game Resources/Backgrounds/MiniMap/OldGrass_MiniMap.png")
			spr.assets.Background = Image.load("Game Resources/Backgrounds/Ground/Grass2.png")
		elseif MapType == "Snow" and MapTypeBool == true then
			MapTypeBool = false
			spr.assets.MiniMap = Image.load("Game Resources/Backgrounds/MiniMap/OldSnow_MiniMap.png")
			spr.assets.Background = Image.load("Game Resources/Backgrounds/Ground/Snow1.png")
		elseif MapType == "Desert" and MapTypeBool == true then
			MapTypeBool = false
			spr.assets.MiniMap = Image.load("Game Resources/Backgrounds/MiniMap/OldDesert_MiniMap.png")
			spr.assets.Background = Image.load("Game Resources/Backgrounds/Ground/Desert2.png")
		end
	else
		if Global.World.Theme == "Grass" and MapTypeBool == true then
			MapTypeBool = false
			spr.assets.MiniMap = Image.load("Game Resources/Backgrounds/MiniMap/OldGrass_MiniMap.png")
			spr.assets.Background = Image.load("Game Resources/Backgrounds/Ground/Grass2.png")
		elseif Global.World.Theme == "Snow" and MapTypeBool == true then
			MapTypeBool = false
			spr.assets.MiniMap = Image.load("Game Resources/Backgrounds/MiniMap/OldSnow_MiniMap.png")
			spr.assets.Background = Image.load("Game Resources/Backgrounds/Ground/Snow1.png")
		elseif Global.World.Theme == "Desert" and MapTypeBool == true then
			MapTypeBool = false
			spr.assets.MiniMap = Image.load("Game Resources/Backgrounds/MiniMap/OldDesert_MiniMap.png")
			spr.assets.Background = Image.load("Game Resources/Backgrounds/Ground/Desert2.png")
		end
		MapType = Global.World.Theme
	end
end

UpdateTheme()

	
-- Debug Only
tick = 0
lim = 4

MenuTarget = nil

--[[
function OpenMenu(Target)
	MenuTarget = Target
	mState = "AddMenu"
end
--]]



WindTimer = 0

-- Loads civilization sounds
--LoadSoundsPack()

OneTime1 = true
OneTime2 = true


-- Population Count
function CountPopulation()
	TotalVillager = 0
	TotalMilitia = 0
	TotalTownCenter = 0
	TotalHouse = 0
	Global.Player.Pop = 0
	Global.Player.MaxPop = 0
	
	for i = 1, Max.Unit.Villager do
		if Villager[i].Exists == true then
			TotalVillager = TotalVillager + 1
		end
	end
	for i = 1, Max.Unit.Militia do
		if Militia[i].Exists == true then
			TotalMilitia = TotalMilitia + 1
		end
	end
	
	Global.Player.Pop = Global.Player.Pop + (TotalMilitia + TotalVillager)
	
	for i = 1, Max.Building.TownCenter do
		if TownCenter[i].Builded == true then
			TotalTownCenter = TotalTownCenter + 5
		end
	end
	
	for i = 1, Max.Building.House do
		if House[i].Builded == true then
			TotalHouse = TotalHouse + 5
		end
	end
	
	Global.Player.MaxPop = Global.Player.MaxPop + (TotalTownCenter + TotalHouse)
end

CountPopulation()

spr.assets.GamePanel = nil
if Option.Graphics.AlphaPanel == true then
	spr.assets.GamePanel = Image.load("Game Resources/Panel/AlphaPanel.png")
else
	if Global.Player.Civ == "Celt" then
		spr.assets.GamePanel = Image.load("Game Resources/Panel/CeltPanel.png")
	elseif Global.Player.Civ == "Japanese" then
		spr.assets.GamePanel = Image.load("Game Resources/Panel/JapanesePanel.png")
	elseif Global.Player.Civ == "Byzanties" then
		spr.assets.GamePanel = Image.load("Game Resources/Panel/ByzantiesPanel.png")
	elseif Global.Player.Civ == "Britons" then
		spr.assets.GamePanel = Image.load("Game Resources/Panel/BritonsPanel.png")
	elseif Global.Player.Civ == "Persians" then
		spr.assets.GamePanel = Image.load("Game Resources/Panel/PersiansPanel.png")
	elseif Global.Player.Civ == "Mongols" then
		spr.assets.GamePanel = Image.load("Game Resources/Panel/MongolsPanel.png")
	elseif Global.Player.Civ == "Saracens" then
		spr.assets.GamePanel = Image.load("Game Resources/Panel/SaracensPanel.png")
	elseif Global.Player.Civ == "Vikings" then
		spr.assets.GamePanel = Image.load("Game Resources/Panel/VikingsPanel.png")
	else
		spr.assets.GamePanel = Image.load("Game Resources/Panel/BritonsPanel.png")
	end
end



Debug = {
	TotalObj = 0,
	RenderObj = 0,
	MaxObj = Max.Unit.Villager + Max.Unit.Militia + Max.Building.TownCenter + Max.Building.House + Max.Other.Berry + Max.Other.Tree + Max.Other.Gold + Max.Other.Stone
}


OneTime = true

----------------------------

LastTime = 0 -- Reset Last Time
Loop = 0 -- Обнуляем счётчик частоты смены integer'а FPS
FPST = Timer.new() -- Initialize FPS Timer
FPST:reset(0) -- Reset FPS Timer
FPST:start() -- Running FPS Timer
CurTime = 0 -- Reset Current Time
FPS = 0 -- Reset FPS
FinalFPS = 0 -- Final FPS Value (MATH.FLOORed FPS)

crsC = 0

dofile('CanvasUpdater.lua') -- there all Static-Changing GUI
	
magic4 = true
mPage = nil



function magicUpd3()
	if magic4 then
		
		local Coords1 = {
			X = -137,
			Y = -10,
		}
		
		mPage = Image.createEmpty(204,258)
		mPage:blit(0,0,GameMenu)
		
		-- Menu Parts Blit
		mPage:blit(160 + Coords1.X,42 + Coords1.Y,EmptyButton) -- Resume Game
		
		mPage:blit(160 + Coords1.X,74 + Coords1.Y,EmptyButton) -- Options
		
		mPage:blit(160 + Coords1.X,106 + Coords1.Y,EmptyButton) -- Controls
		
		mPage:blit(160 + Coords1.X,138 + Coords1.Y,EmptyButton) -- Save
		
		mPage:blit(160 + Coords1.X,170 + Coords1.Y,EmptyButton) -- Exit and Save
		
		mPage:blit(160 + Coords1.X,203 + Coords1.Y,EmptyButton) -- Exit
		
		
		mPage:fontPrint(Arial,202 + Coords1.X,58 + Coords1.Y,"Resume Game",ButtonColor.ResumeGame)
		
		mPage:fontPrint(Arial,218 + Coords1.X,90 + Coords1.Y,"Options",ButtonColor.Options)
		
		mPage:fontPrint(Arial,216 + Coords1.X,122 + Coords1.Y,"Controls",ButtonColor.Controls)
		
		mPage:fontPrint(Arial,224 + Coords1.X,153 + Coords1.Y,"Save",ButtonColor.Save)
		
		mPage:fontPrint(Arial,202 + Coords1.X,186 + Coords1.Y,"Exit And Save",ButtonColor.ExitSave)
		
		mPage:fontPrint(Arial,227 + Coords1.X,220 + Coords1.Y,"Exit",ButtonColor.Exit)
		
		magic4 = false
	end
end

magicPop = true
mPage2 = nil
function magicUpdPop()
	if magicPop then
		mPage2 = CanvasIMG.a480x272
			
		mPage2:fontPrint(Arial,288,13,Global.Player.Pop.."/"..Global.Player.MaxPop,White)
		
		magicPop = false
	end
end



magicNick = true
mPage3 = nil
function magicUpdNick()
	if magicNick then
		--mPage3 = Image.createEmpty(155,272)
		--mPage3:blit(0,0,CanvasIMG.a155x272)
		mPage3 = CanvasIMG.a155x272
			
		mPage3:fontPrint(Arial,10-1,190-1,Global.Player.Nick.." ("..Global.Player.Score.."/"..Global.Player.TeamScore..")",Black)
		mPage3:fontPrint(Arial,10,190,    Global.Player.Nick.." ("..Global.Player.Score.."/"..Global.Player.TeamScore..")",Global.Player.NickCol)
		--mPage3:fontPrint(Arial,10,50,"TEST",Global.Player.NickCol)
		
		magicNick = false
	end
end

if magicPop == true then
	magicUpdPop()
end
if magicNick == true then
	magicUpdNick()
end

while true do
	screen:clear()
	pad = Controls.read()
	
	for i = 0,1 do
		for i2 = 0,1 do
			screen:blit(i*480+Back.X, i2*272+Back.Y, spr.assets.Background, true)
		end
	end
	
	if OneTime == true then
		--CountPopulation()
		magicPop = true
		magicNick = true
		mPage3:fontPrint(Arial,10-1,190-1,Global.Player.Nick.." ("..Global.Player.Score.."/"..Global.Player.TeamScore..")",Black)
		mPage3:fontPrint(Arial,10,190,    Global.Player.Nick.." ("..Global.Player.Score.."/"..Global.Player.TeamScore..")",Global.Player.NickCol)
		
		OneTime = false
	end
	
	
	
	if Loop < 2 then -- Частота Обновления Экрана
		LastTime = CurTime
		CurTime = FPST:time()
		FPS = 1000 / (CurTime - LastTime) -- Это Математическое вычисление FPS путем деления 1000мс на разность времен текущего и предыдущего, т.е. определение провисания процессора при обработке кода.
	end
	
	if Loop > 50 then Loop = 0 else Loop = Loop + 1 end -- Накручиваем счётчик обновления экрана до 50, чтобы не так уж сильно мельтешил экран.
	FinalFPS = math.floor(FPS)
	--[[
	for i = 1, 50 do
		for j = 1, 50 do
			screen:blit(i * Info.Tile.Width, j * Info.Tile.Height, spr.assets.Tile.Grass1)
			screen:blit(i * Info.Tile.Width + Info.Tile.Width / 2, j * Info.Tile.Height - Info.Tile.Height / 2, spr.assets.Tile.Grass1)
		end
	end
	--]]
	
	Debug.RenderObj = 0
	Debug.TotalObj = 0
	
	-- Everything's Optimized
	for i = 1, Max.Building.TownCenter do
		if TownCenter[i].Builded == true and TownCenter[i].X + Info.TC.Width >= 0 and TownCenter[i].X <= 480 and TownCenter[i].Y + Info.TC.Height >= 0 and TownCenter[i].Y <= 272 then
			if TownCenter[i].Selected == true then
				screen:blit(TownCenter[i].X - 26, TownCenter[i].Y + 25, spr.build.Select.S3x3)
				drawfilledRect(TownCenter[i].X + 20, TownCenter[i].Y - 10, TownCenter[i].MaxHP / 16, 2, Red)
				drawfilledRect(TownCenter[i].X + 20, TownCenter[i].Y - 10, TownCenter[i].HP / 16, 2, Green)
			end
			screen:blit(TownCenter[i].X, TownCenter[i].Y, TownCenter[i].IMG, true)
			Debug.RenderObj = Debug.RenderObj + 1
			Debug.TotalObj = Debug.TotalObj + 1
		elseif TownCenter[i].Builded == true then
			Debug.TotalObj = Debug.TotalObj + 1
		end
	end
	
	for i = 1, Max.Building.House do
		if House[i].Builded == true and House[i].X + Info.House.Width >= 0 and House[i].X <= 480 and House[i].Y + Info.House.Height >= 0 and House[i].Y <= 272 then
			if House[i].Selected == true then
				screen:blit(House[i].X - 22, House[i].Y + 8, spr.build.Select.S2x2)
				drawfilledRect(House[i].X + 20, House[i].Y - 10, House[i].MaxHP / 10, 2, Red)
				drawfilledRect(House[i].X + 20, House[i].Y - 10, House[i].HP / 10, 2, Green)
			end
			screen:blit(House[i].X, House[i].Y, House[i].IMG, true)
			Debug.RenderObj = Debug.RenderObj + 1
			Debug.TotalObj = Debug.TotalObj + 1
		elseif House[i].Builded == true then
			Debug.TotalObj = Debug.TotalObj + 1
		end
	end
	
	for i = 1, Max.Other.Berry do
		if Berry[i].Exists == true and Berry[i].Food ~= 0 and Berry[i].X + Info.Berry.Width >= 0 and Berry[i].X <= 480 and Berry[i].Y + Info.Berry.Height >= 0 and Berry[i].Y <= 272 then
			if Berry[i].Selected == true then
				screen:blit(Berry[i].X - 10, Berry[i].Y - Info.Berry.Height / 2 + 22, spr.build.Select.S1x1)
			end
			screen:blit(Berry[i].X, Berry[i].Y, Berry[i].IMG, true)
			Debug.RenderObj = Debug.RenderObj + 1
			Debug.TotalObj = Debug.TotalObj + 1
		elseif Berry[i].Exists == true then
			Debug.TotalObj = Debug.TotalObj + 1
		end
		--if Berry[i].Food <= 0 then
		--	Berry[i].Exists = false
		--end
	end
	for i = 1, Max.Other.Tree do
		if Tree[i].Exists == true and Tree[i].Wood ~= 0 and Tree[i].X + Info.Tree.Width >= 0 and Tree[i].X <= 480 and Tree[i].Y + Info.Tree.Height >= 0 and Tree[i].Y <= 272 then
			if Tree[i].Selected == true then
				screen:blit(Tree[i].X + 8-5, Tree[i].Y + Info.Tree.Height / 2 + 20, spr.build.Select.S1x1)
			end
			screen:blit(Tree[i].X, Tree[i].Y, Tree[i].IMG)
			Debug.RenderObj = Debug.RenderObj + 1
			Debug.TotalObj = Debug.TotalObj + 1
		elseif Tree[i].Exists == true then
			Debug.TotalObj = Debug.TotalObj + 1
		end
	end
	for i = 1, Max.Other.Gold do
		if Gold[i].Exists == true and Gold[i].Gold ~= 0 and Gold[i].X + Info.Gold.Width >= 0 and Gold[i].X <= 480 and Gold[i].Y + Info.Gold.Height >= 0 and Gold[i].Y <= 272 then
			if Gold[i].Selected == true then
				screen:blit(Gold[i].X - 8, Gold[i].Y - Info.Gold.Height / 2 + 18, spr.build.Select.S1x1)
			end
			screen:blit(Gold[i].X, Gold[i].Y, Gold[i].IMG, true)
			Debug.RenderObj = Debug.RenderObj + 1
			Debug.TotalObj = Debug.TotalObj + 1
		elseif Gold[i].Exists == true then
			Debug.TotalObj = Debug.TotalObj + 1
		end
	end
	for i = 1, Max.Other.Stone do
		if Stone[i].Exists == true and Stone[i].Stone ~= 0 and Stone[i].X + Info.Stone.Width >= 0 and Stone[i].X <= 480 and Stone[i].Y + Info.Stone.Height >= 0 and Stone[i].Y <= 272 then
			if Stone[i].Selected == true then
				screen:blit(Stone[i].X - 10, Stone[i].Y - Info.Stone.Height / 2 + 12, spr.build.Select.S1x1)
			end
			screen:blit(Stone[i].X, Stone[i].Y, Stone[i].IMG, true)
			Debug.RenderObj = Debug.RenderObj + 1
			Debug.TotalObj = Debug.TotalObj + 1
		elseif Stone[i].Exists == true then
			Debug.TotalObj = Debug.TotalObj + 1
		end
	end
	
	for i = 1, Max.Unit.Villager do
		if Villager[i].Exists == true and Villager[i].X + Info.Villager.Width >= 0 and Villager[i].X <= 480 and Villager[i].Y + Info.Villager.Height >= 0 and Villager[i].Y <= 272 then
			if Villager[i].Selected == true then
				screen:blit(Villager[i].X - 7, Villager[i].Y + 30, spr.uni.Select.Small)
				drawfilledRect(Villager[i].X - 2, Villager[i].Y - 10, Villager[i].MaxHP / 2, 2, Red)
				drawfilledRect(Villager[i].X - 2, Villager[i].Y - 10, Villager[i].HP / 2, 2, Green)
			end
			--screen:blit(Villager[i].X - Info.Villager.Width / 2, Villager[i].Y - Info.Villager.Height / 2, Villager[i].IMG)
			screen:blit(Villager[i].X, Villager[i].Y, Villager[i].IMG)
			Debug.RenderObj = Debug.RenderObj + 1
			Debug.TotalObj = Debug.TotalObj + 1
		elseif Villager[i].Exists == true then
			Debug.TotalObj = Debug.TotalObj + 1
		end
	end
	for i = 1, Max.Unit.Militia do
		if Militia[i].Exists == true and Militia[i].X + Info.Militia.Width >= 0 and Militia[i].X <= 480 and Militia[i].Y + Info.Militia.Height >= 0 and Militia[i].Y <= 272 then
			if Militia[i].Selected == true then
				screen:blit(Militia[i].X - 7, Militia[i].Y + 30, spr.uni.Select.Small)
				drawfilledRect(Militia[i].X - 2, Militia[i].Y - 10, Militia[i].MaxHP / 2, 2, Red)
				drawfilledRect(Militia[i].X - 2, Militia[i].Y - 10, Militia[i].HP / 2, 2, Green)
			end
			--screen:blit(Militia[i].X - Info.Militia.Width / 2, Militia[i].Y - Info.Militia.Height / 2, Militia[i].IMG)
			screen:blit(Militia[i].X, Militia[i].Y, Militia[i].IMG)
			Debug.RenderObj = Debug.RenderObj + 1
			Debug.TotalObj = Debug.TotalObj + 1
		elseif Militia[i].Exists == true then
			Debug.TotalObj = Debug.TotalObj + 1
		end
	end

	--PopulationCheck()
	
	--[[
	-- GUI Rendering
	 -------------------------------------------------------------------------------------------------------
	
	screen:blit(338,200,spr.assets.MiniMap) -- 330, 197  or  338, 200
	screen:fontPrint(Arial,288,13,Global.Player.Pop.."/"..Global.Player.MaxPop,white)
	Global.Player.Pop = 0
	Global.Player.MaxPop = 0
	screen:fontPrint(Arial,27,13,Global.Player.Wood,white)
	screen:fontPrint(Arial,94,13,Global.Player.Food,white)
	screen:fontPrint(Arial,160,13,Global.Player.Gold,white)
	screen:fontPrint(Arial,220,13,Global.Player.Stone,white)
	
	screen:fontPrint(Arial,380-1,13-1,Global.Player.Age,black)
	screen:fontPrint(Arial,380,13,Global.Player.Age,white)
	--]]
	
	local Score = 0
	local TeamScore = 0
	Score = Score + Global.Player.Wood + Global.Player.Food + Global.Player.Stone + Global.Player.Gold
	TeamScore = TeamScore + Global.Player.Wood + Global.Player.Food + Global.Player.Stone + Global.Player.Gold
	if Score ~= Global.Player.Score or TeamScore ~= Global.Player.TeamScore then
		Canvas.Update.Numbers.Score = true
		Global.Player.Score = Score
		Global.Player.TeamScore = TeamScore
	end
	
	--screen:blit(0, 0,spr.assets.GamePanel, true)
	
	Canvas.Rebuild()
	screen:blit(0,0,Canvas.Result)
	
	screen:blit(0, 0, mPage2)
	screen:blit(325, 0, mPage3)
	
	
	--screen:blit(0, 0, mPage2) -- Population
	--screen:blit(325, 0, mPage3) -- Nickname
	
	
	
	--[[
	if magicNick == true then
		screen:fontPrint(Arial,385,40,"magicNick = true",black)
	elseif magicNick == false then
		screen:fontPrint(Arial,385,40,"magicNick = false",black)
	end
	--]]
	
	--screen:fontPrint(Arial,385,190,Global.Player.Nick.." (0/0)",Global.Player.NickCol)
	
	
	
			
	-- Debug Mode
	
	tick = tick + 1
	
	--[[
	if FPST:time() > 1 then
		FPST:reset(0)
		tick = FPS
		tick = 0
	end
	--]]
	
	--[[
	local Test = {
		FPS = 0,
		Tick = 0,
		TotalObj = 0,
		RenderObj = 0,
	}
	
	if FPS ~= Test.FPS or tick ~= Test.Tick or Debug.TotalObj ~= Test.TotalObj or Debug.RenderObj ~= Test.RenderObj then
		Canvas.Update.Debug = true
		Test.FPS = FPS
		Test.Tick = tick
		Test.TotalObj = Debug.DebugObj
		Test.RenderObj = Debug.RenderObj
	end
	--]]
	
	--[[
	local MaxPop = 0
	local Pop = 0
	
	for i = 1, Max.Unit.Villager do
		if Villager[i].Exists == true then
			Pop = Pop + 1
		end
	end
	for i = 1, Max.Unit.Militia do
		if Militia[i].Exists == true then
			Pop = Pop + 1
		end
	end
	
	for i = 1, Max.Building.TownCenter do
		if TownCenter[i].Builded == true then
			MaxPop = MaxPop + 5
		end
	end
	for i = 1, Max.Building.House do
		if House[i].Builded == true then
			MaxPop = MaxPop + 5
		end
	end
	
	if MaxPop ~= Global.Player.MaxPop or Pop ~= Global.Player.Pop then
		Canvas.Update.Numbers.Pop = true
		MaxPop = Global.Player.MaxPop
		Pop = Global.Player.Pop
	end
	--]]
	
	
	-- Debug Mode
	
	if tick > 100 then
		tick = 0
	end
	
	
	
	-- screen:fontPrint(Arial,200-1,35-1,"Tick: "..tick,Black)
	
	if Option.Sounds.Nature == true then
		if WindTimer >= 0 then
			if WindTimer >= 1000 or WindTimer <= -1 then WindTimer = 0 end
			WindTimer = WindTimer + 1
		end
		
		--screen:fontPrint(Arial,10-1,45-1,"Timer: "..WindTimer,Black)
		--screen:fontPrint(Arial,10,45,"Timer: "..WindTimer,White)
		
		if WindTimer == 200 then
			PlaySound("Wind1")
		elseif WindTimer == 700 then
			PlaySound("Wind2")
		elseif WindTimer == 1000 then
			PlaySound("Wind3")
		end
		
	end
	
	--screen:fontPrint(Arial,380,175,null[1].X.." ".. null[1].Y,white)
	
	for i = 1, Max.Unit.Villager do
		if Villager[i].Selected == true then
			if Option.Debug.ShowObjID  == true then
				screen:fontPrint(Arial,140,218,"Villager ["..i.."]",black)
			else
				screen:fontPrint(Arial,140,218,"Villager",black)
			end
			
			screen:fontPrint(Arial,200,235,Global.Player.Nick,black)
			screen:fontPrint(Arial,200,245,Global.Player.Civ,black)
			screen:fontPrint(Arial,140,210 + 30 + 15 + 10,Villager[i].HP.."/"..Villager[i].MaxHP,black)
			if Villager[i].Type == 1 then
				screen:blit(140,220,spr.uni.VillagerMale.Icon)
			elseif Villager[i].Type == 2 then
				screen:blit(140,220,spr.uni.VillagerFemale.Icon)
			end
			drawfilledRect(140, 250, Villager[i].MaxHP / 2, 2, Red)
			drawfilledRect(140, 250, Villager[i].HP / 2, 2, Green)
			if Villager[i].Food ~= 0 then
				screen:blit(260,220,spr.assets.ico.Food) -- 25x15
				screen:fontPrint(Arial,250,220+15+15,Villager[i].Food.."/"..Global.Player.Villager.MaxInv,black)
				Villager[i].Wood = 0
				Villager[i].Gold = 0
				Villager[i].Stone = 0
			end
			--screen:blit(40, 220, spr.ico.More) -- 32x32 Pic
			if Cursor.X >= 40 and Cursor.X <= 62 and Cursor.Y >= 220 and Cursor.Y <= 252 and pad:cross() then
				--OpenMenu(Villager[i])
			end
		end
		--[[
		if Villager[i].Target == Berry[i] or Villager[i].Target == Tree[i] or Villager[i].Target == Gold[i] or Villager[i].Target == Stone[i] then
			-- i need to do something here, but i forgot...
			if pad:triangle() then
				Villager[i].Target = nil
				Villager[i].LastTarget = nil
			end
		end
		if Villager[i].Target == Berry[i] and Villager[i].Food <= Global.Player.Villager.MaxInv then
			Villager[i].LastTarget = Berry[i]
			Villager[i].Food = Villager[i].Food + 1
			Berry[i].Food = Berry[i].Food - 1
		end
		if Villager[i].Target == Tree[i] and Villager[i].Tree <= Global.Player.Villager.MaxInv then
			Villager[i].LastTarget = Tree[i]
			Villager[i].Wood = Villager[i].Wood + 1
			Tree[i].Wood = Tree[i].Wood - 1
		end
		--]]
	end
	for i = 1, Max.Unit.Militia do
		if Militia[i].Selected == true then
			if Option.Debug.ShowObjID  == true then
				screen:fontPrint(Arial,140,218,"Militia ["..i.."]",black)
			else
				screen:fontPrint(Arial,140,218,"Militia",black)
			end
			
			screen:fontPrint(Arial,200,235,Global.Player.Nick,black)
			screen:fontPrint(Arial,200,245,Global.Player.Civ,black)
			screen:fontPrint(Arial,140,210 + 30 + 15 + 10,Militia[i].HP.."/"..Militia[i].MaxHP,black)
			screen:blit(140,220,spr.uni.Militia.Icon)
			drawfilledRect(140, 250, Militia[i].MaxHP / 2, 2, Red)
			drawfilledRect(140, 250, Militia[i].HP / 2, 2, Green)
		end
	end
	
	for i = 1, Max.Other.Berry do
		if Berry[i].Selected == true then
			screen:blit(140,220,spr.res.Food.Icon)
			if Option.Debug.ShowObjID  == true then
				screen:fontPrint(Arial,140,218,"Forage Bush ["..i.."]",black)
			else
				screen:fontPrint(Arial,140,218,"Forage Bush",black)
			end
			
			screen:blit(270,220,spr.assets.ico.Food) -- 25x15
			screen:fontPrint(Arial,260,220+15+5+5,Berry[i].Food.."/"..Berry[i].MaxFood,black)
		end
	end
	for i = 1, Max.Other.Tree do
		if Tree[i].Selected == true then
			screen:blit(140,220,spr.res.Tree.Icon)
			if Option.Debug.ShowObjID  == true then
				screen:fontPrint(Arial,140,218,"Tree ["..i.."]",black)
			else
				screen:fontPrint(Arial,140,218,"Tree",black)
			end
			
			screen:blit(270,220,spr.assets.ico.Wood)
			screen:fontPrint(Arial,260,220+15+5+5,Tree[i].Wood.."/"..Tree[i].MaxWood,black)
		end
	end
	for i = 1, Max.Other.Gold do
		if Gold[i].Selected == true then
			screen:blit(140,220,spr.res.Gold.Icon)
			if Option.Debug.ShowObjID  == true then
				screen:fontPrint(Arial,140,218,"Gold Mine ["..i.."]",black)
			else
				screen:fontPrint(Arial,140,218,"Gold Mine",black)
			end
			
			screen:blit(270,220,spr.assets.ico.Gold)
			screen:fontPrint(Arial,260,220+15+5+5,Gold[i].Gold.."/"..Gold[i].MaxGold,black)
		end
	end
	for i = 1, Max.Other.Stone do
		if Stone[i].Selected == true then
			screen:blit(140,220,spr.res.Stone.Icon)
			if Option.Debug.ShowObjID  == true then
				screen:fontPrint(Arial,140,218,"Stone Mine ["..i.."]",black)
			else
				screen:fontPrint(Arial,140,218,"Stone Mine",black)
			end
			
			screen:blit(270,220,spr.assets.ico.Stone)
			screen:fontPrint(Arial,260,220+15+5+5,Stone[i].Stone.."/"..Stone[i].MaxStone,black)
		end
	end
	
	for i = 1, Max.Building.TownCenter do
		if TownCenter[i].Builded == true then
			if TownCenter[i].Selected == true then
				if Option.Debug.ShowObjID  == true then
					screen:fontPrint(Arial,185,225,"Town Center ["..i.."]",black)
				else
					screen:fontPrint(Arial,185,225,"Town Center",black)
				end
				
				screen:fontPrint(Arial,185,235,Global.Player.Nick,black)
				screen:fontPrint(Arial,185,245,Global.Player.Civ,black)
				screen:blit(140,215,spr.build.TC.Icon)
			end
		end
	end
	
	for i = 1, Max.Building.House do
		if House[i].Builded == true then
			if House[i].Selected == true then
				if Option.Debug.ShowObjID  == true then
					screen:fontPrint(Arial,185,225,"House ["..i.."]",black)
				else
					screen:fontPrint(Arial,185,225,"House",black)
				end
				
				screen:fontPrint(Arial,185,235,Global.Player.Nick,black)
				screen:fontPrint(Arial,185,245,Global.Player.Civ,black)
				screen:blit(140,215,spr.build.House.Icon)
			end
		end
	end
	
	for i = 1, Max.Unit.Villager do
		if Villager[i].Selected == true and Cursor.Under == Berry[i] or Cursor.Under == Tree[i] or Cursor.Under == Gold[i] or Cursor.Under == Stone[i] then
			screen:blit(Cursor.X, Cursor.Y, Cursor.IMG_RES)
		else
			screen:blit(Cursor.X, Cursor.Y, Cursor.IMG_DEF)
		end
	end
	
	
	
	if Option.Debug.ShowFPS == true then
		screen:fontPrint(Arial,10-1,35-1,"FPS: "..FinalFPS,Black)
		screen:fontPrint(Arial,10,35,"FPS: "..FinalFPS,White)
	end
	
	if Option.Debug.DebugText == true and Option.Debug.ShowFPS == true then
		screen:fontPrint(Arial,10-1,50-1,"Tick: "..tick,Black)
		screen:fontPrint(Arial,10,50,"Tick: "..tick,White)
		
		screen:fontPrint(Arial,10-1,68-1,"Total Obj: "..Debug.TotalObj.." / "..Debug.MaxObj,Black)
		screen:fontPrint(Arial,10,68,"Total Obj: "..Debug.TotalObj.." / "..Debug.MaxObj,White)
		
		screen:fontPrint(Arial,10-1,86-1,"Render Obj: "..Debug.RenderObj.." / "..Debug.MaxObj,Black)
		screen:fontPrint(Arial,10,86,"Render Obj: "..Debug.RenderObj.." / "..Debug.MaxObj,White)
		
		-- 50 68 86 104 122 140
	end
	if Option.Debug.DebugText == true and Option.Debug.ShowFPS == false then
		screen:fontPrint(Arial,10-1,35-1,"Tick: "..tick,Black)
		screen:fontPrint(Arial,10,35,"Tick: "..tick,White)
		
		screen:fontPrint(Arial,10-1,53-1,"Total Obj: "..Debug.TotalObj.." / "..Debug.MaxObj,Black)
		screen:fontPrint(Arial,10,53,"Total Obj: "..Debug.TotalObj.." / "..Debug.MaxObj,White)
		
		screen:fontPrint(Arial,10-1,71-1,"Render Obj: "..Debug.RenderObj.." / "..Debug.MaxObj,Black)
		screen:fontPrint(Arial,10,71,"Render Obj: "..Debug.RenderObj.." / "..Debug.MaxObj,White)
	end
	
	
	
	if mState == "Save" then -----------------------------------------------------------------------------------------
		
		screen:blit(51,22,PanelBG)
		
		screen:fontPrint(Arial,210,50,"Save Menu",Black)
		
		screen:fontPrint(Arial,185-1,80+1,"Save Game in Slot #1",Black)
		screen:fontPrint(Arial,185-1,110+1,"Save Game in Slot #2",Black)
		screen:fontPrint(Arial,185-1,140+1,"Save Game in Slot #3",Black)
		
		screen:fontPrint(Arial,185,80,"Save Game in Slot #1",MapColor.Map1)
		screen:fontPrint(Arial,185,110,"Save Game in Slot #2",MapColor.Map2)
		screen:fontPrint(Arial,185,140,"Save Game in Slot #3",MapColor.Map3)
		
		if OneTime2 == true then
			MapColor.Map1 = Color.new(255,255,0)
			OneTime2 = false
		end
		
		if pad:down() then
			if pressed.down == false then
				pressed.down = true
				SaveIndex = SaveIndex + 1
				if SaveIndex >= 4 then SaveIndex = 3 end
			end
		else
			pressed.down = false
		end
		
		if pad:up() then
			if pressed.up == false then
				pressed.up = true
				SaveIndex = SaveIndex - 1
				if SaveIndex <= 0 then SaveIndex = 1 end
			end
		else
			pressed.up = false
		end
		
		if pad:down() or pad:up() then
			if SaveIndex == 1 then
				MapColor.Map1 = Color.new(255,255,0)
				MapColor.Map2 = Color.new(0,0,0)
				MapColor.Map3 = Color.new(0,0,0)
			elseif SaveIndex == 2 then
				MapColor.Map1 = Color.new(0,0,0)
				MapColor.Map2 = Color.new(255,255,0)
				MapColor.Map3 = Color.new(0,0,0)
			elseif SaveIndex == 3 then
				MapColor.Map1 = Color.new(0,0,0)
				MapColor.Map2 = Color.new(0,0,0)
				MapColor.Map3 = Color.new(255,255,0)
			end
		end
		
		if pad:cross() then
			if SaveIndex == 1 then
				SaveGame("Map1", "Saved Game", "", "", "Normal", "None", MV, Update, Pref, "Saved Game Save", SaveIndex, false)
			end
			if SaveIndex == 2 then
				SaveGame("Map2", "Saved Game", "", "", "Normal", "None", MV, Update, Pref, "Saved Game Save", SaveIndex, false)
			end
			if SaveIndex == 3 then
				SaveGame("Map3", "Saved Game", "", "", "Normal", "None", MV, Update, Pref, "Saved Game Save", SaveIndex, false)
			end
		end
		
		if pad:circle() then
			if pressed.circle == false then
				pressed.circle = true
				mState = "Menu"
			end
		else
			pressed.circle = false
		end
		
	end -----------------------------------------------------------------------------------------
	
	if mState == "Options" then -----------------------------------------------------------------------------------------
		
		screen:blit(51,22,OptionsBG)

		if pad:circle() then
			if pressed.circle == false then
				pressed.circle = true
				mState = "Menu"
			end
		else
			pressed.circle = false
		end
		
	end -- mState Options -----------------------------------------------------------------------------------------
	
	if mState == "Controls" then -----------------------------------------------------------------------------------------
		
		screen:blit(51,22,PanelBG)
		screen:blit(51,22,ControlsPicture)

		if pad:circle() then
			if pressed.circle == false then
				pressed.circle = true
				mState = "Menu"
			end
		else
			pressed.circle = false
		end
		
	end -- mState Options -----------------------------------------------------------------------------------------
	
	if mState == "Menu" then
		
		
		
		
		
		magicUpd3()
		
		if OneTime1 == true then
			ButtonColor.ResumeGame = Color.new(255,255,0)
			OneTime1 = false
		end
		
		screen:blit(136, 9, mPage)
		
		screen:fontPrint(Arial,203,32,"Game Paused",black)
		
		-- Moving
		if pad:down() then
			if pressed.down == false then
				pressed.down = true
				CurButton = CurButton + 1
				if CurButton > 6 then CurButton = 1 end
				magic4 = true
			end
		else
			pressed.down = false
		end
		
		if pad:up() then
			if pressed.up == false then
				pressed.up = true
				CurButton = CurButton - 1
				if CurButton < 1 then CurButton = 6 end
				magic4 = true
			end
		else
			pressed.up = false
		end
		
		
		
		if pad:cross() then
			if CurButton == 1 then mState = "Game" end if pad:start() then mState = "Game" end -- Resume Game
			if CurButton == 2 then mState = "Options" end -- Options
			if CurButton == 3 then mState = "Controls" end -- Controls
			if CurButton == 4 then mState = "Save" end -- Save
			if CurButton == 5 then  end -- Exit and Save
			if CurButton == 6 then
				ClearRAM("Game")
				dofile("MainMenu.lua")
			end -- Exit
		end
		
		
		
		if pad:up() or pad:down() then
			if CurButton == 1 then
				ButtonColor.ResumeGame = Color.new(255,255,0)
				ButtonColor.Options = Color.new(0,0,0)
				ButtonColor.Controls = Color.new(0,0,0)
				ButtonColor.Save = Color.new(0,0,0)
				ButtonColor.ExitSave = Color.new(0,0,0)
				ButtonColor.Exit = Color.new(0,0,0)
			elseif CurButton == 2 then
				ButtonColor.ResumeGame = Color.new(0,0,0)
				ButtonColor.Options = Color.new(255,255,0)
				ButtonColor.Controls = Color.new(0,0,0)
				ButtonColor.Save = Color.new(0,0,0)
				ButtonColor.ExitSave = Color.new(0,0,0)
				ButtonColor.Exit = Color.new(0,0,0)
			elseif CurButton == 3 then
				ButtonColor.ResumeGame = Color.new(0,0,0)
				ButtonColor.Options = Color.new(0,0,0)
				ButtonColor.Controls = Color.new(255,255,0)
				ButtonColor.Save = Color.new(0,0,0)
				ButtonColor.ExitSave = Color.new(0,0,0)
				ButtonColor.Exit = Color.new(0,0,0)
			elseif CurButton == 4 then
				ButtonColor.ResumeGame = Color.new(0,0,0)
				ButtonColor.Options = Color.new(0,0,0)
				ButtonColor.Controls = Color.new(0,0,0)
				ButtonColor.Save = Color.new(255,255,0)
				ButtonColor.ExitSave = Color.new(0,0,0)
				ButtonColor.Exit = Color.new(0,0,0)
			elseif CurButton == 5 then
				ButtonColor.ResumeGame = Color.new(0,0,0)
				ButtonColor.Options = Color.new(0,0,0)
				ButtonColor.Controls = Color.new(0,0,0)
				ButtonColor.Save = Color.new(0,0,0)
				ButtonColor.ExitSave = Color.new(255,255,0)
				ButtonColor.Exit = Color.new(0,0,0)
			elseif CurButton == 6 then
				ButtonColor.ResumeGame = Color.new(0,0,0)
				ButtonColor.Options = Color.new(0,0,0)
				ButtonColor.Controls = Color.new(0,0,0)
				ButtonColor.Save = Color.new(0,0,0)
				ButtonColor.ExitSave = Color.new(0,0,0)
				ButtonColor.Exit = Color.new(255,255,0)
			end
		end

	end
	
	if mState == "Game" then ---------------------------------------------------------------------------------------------------------
	
		if Cursor.Speed == 1 then crsC = 23 end -- Slow
		if Cursor.Speed == 2 then crsC = 17 end -- Slow
		if Cursor.Speed == 3 then crsC = 12 end -- Normal
		if Cursor.Speed == 4 then crsC = 10 end -- Fast
		if Cursor.Speed == 5 then crsC = 5  end -- Fast
		
		-- Cursor.Speed = Option.Game.CursorSpeed
		-- Cursor.ScrollSpeed = Option.Game.ScrollSpeed
	
		--ANALOG-X
		dx = pad:analogX()
		if math.abs(dx) > 32 then
			Cursor.X = Cursor.X + dx / crsC -- crsC = 20
		end
		--ANALOG-Y
		dy = pad:analogY()
		if math.abs(dy) > 32 then
			Cursor.Y = Cursor.Y + dy / crsC -- crsC = 20
		end
		--DPAD
		
		-- Moving all items --
		
		if pad:square() then
			-- Y + -------------------------------------------------
			if pad:up() then
				Back.Y = Back.Y + Cursor.ScrollSpeed
				
				for i = 1, Max.Building.TownCenter do if TownCenter[i].Builded == true then TownCenter[i].Y = TownCenter[i].Y + Cursor.ScrollSpeed end end
				for i = 1, Max.Building.House do if House[i].Builded == true then House[i].Y = House[i].Y + Cursor.ScrollSpeed end end
				for i = 1, Max.Unit.Villager do 
					if Villager[i].Exists == true then
						Villager[i].Y = Villager[i].Y + Cursor.ScrollSpeed
						Villager[i].TarY = Villager[i].TarY + Cursor.ScrollSpeed
					end
				end
				for i = 1, Max.Unit.Militia do 
					if Militia[i].Exists == true then
						Militia[i].Y = Militia[i].Y + Cursor.ScrollSpeed
						Militia[i].TarY = Militia[i].TarY + Cursor.ScrollSpeed
					end
				end
				for i = 1, Max.Other.Berry do if Berry[i].Exists == true then Berry[i].Y = Berry[i].Y + Cursor.ScrollSpeed end end
				for i = 1, Max.Other.Tree do if Tree[i].Exists == true then Tree[i].Y = Tree[i].Y + Cursor.ScrollSpeed end end
				for i = 1, Max.Other.Gold do if Gold[i].Exists == true then Gold[i].Y = Gold[i].Y + Cursor.ScrollSpeed end end
				for i = 1, Max.Other.Stone do if Stone[i].Exists == true then Stone[i].Y = Stone[i].Y + Cursor.ScrollSpeed end end
				
				Mineral[1].Y = Mineral[1].Y + Cursor.ScrollSpeed
				Cursor.LastY = Cursor.LastY + Cursor.ScrollSpeed
			end
			
			-- Y - --------------------------------------------------
			if pad:down() then
				Back.Y = Back.Y - Cursor.ScrollSpeed
				
				for i = 1, Max.Building.TownCenter do if TownCenter[i].Builded == true then TownCenter[i].Y = TownCenter[i].Y - Cursor.ScrollSpeed end end
				for i = 1, Max.Building.House do if House[i].Builded == true then House[i].Y = House[i].Y - Cursor.ScrollSpeed end end
				for i = 1, Max.Unit.Villager do 
					if Villager[i].Exists == true then
						Villager[i].Y = Villager[i].Y - Cursor.ScrollSpeed
						Villager[i].TarY = Villager[i].TarY - Cursor.ScrollSpeed
					end
				end
				for i = 1, Max.Unit.Militia do 
					if Militia[i].Exists == true then
						Militia[i].Y = Militia[i].Y - Cursor.ScrollSpeed
						Militia[i].TarY = Militia[i].TarY - Cursor.ScrollSpeed
					end
				end
				for i = 1, Max.Other.Berry do if Berry[i].Exists == true then Berry[i].Y = Berry[i].Y - Cursor.ScrollSpeed end end
				for i = 1, Max.Other.Tree do if Tree[i].Exists == true then Tree[i].Y = Tree[i].Y - Cursor.ScrollSpeed end end
				for i = 1, Max.Other.Gold do if Gold[i].Exists == true then Gold[i].Y = Gold[i].Y - Cursor.ScrollSpeed end end
				for i = 1, Max.Other.Stone do if Stone[i].Exists == true then Stone[i].Y = Stone[i].Y - Cursor.ScrollSpeed end end
				
				Mineral[1].Y = Mineral[1].Y - Cursor.ScrollSpeed
				Cursor.LastY = Cursor.LastY - Cursor.ScrollSpeed
			end
			 
			-- X + -----------------------------------------------------
			if pad:left() then
				Back.X = Back.X + Cursor.ScrollSpeed
				
				for i = 1, Max.Building.TownCenter do if TownCenter[i].Builded == true then TownCenter[i].X = TownCenter[i].X + Cursor.ScrollSpeed end end
				for i = 1, Max.Building.House do if House[i].Builded == true then House[i].X = House[i].X + Cursor.ScrollSpeed end end
				for i = 1, Max.Unit.Villager do 
					if Villager[i].Exists == true then
						Villager[i].X = Villager[i].X + Cursor.ScrollSpeed
						Villager[i].TarX = Villager[i].TarX + Cursor.ScrollSpeed
					end
				end
				for i = 1, Max.Unit.Militia do 
					if Militia[i].Exists == true then
						Militia[i].X = Militia[i].X + Cursor.ScrollSpeed
						Militia[i].TarX = Militia[i].TarX + Cursor.ScrollSpeed
					end
				end
				for i = 1, Max.Other.Berry do if Berry[i].Exists == true then Berry[i].X = Berry[i].X + Cursor.ScrollSpeed end end
				for i = 1, Max.Other.Tree do if Tree[i].Exists == true then Tree[i].X = Tree[i].X + Cursor.ScrollSpeed end end
				for i = 1, Max.Other.Gold do if Gold[i].Exists == true then Gold[i].X = Gold[i].X + Cursor.ScrollSpeed end end
				for i = 1, Max.Other.Stone do if Stone[i].Exists == true then Stone[i].X = Stone[i].X + Cursor.ScrollSpeed end end
				
				Mineral[1].X = Mineral[1].X + Cursor.ScrollSpeed
				Cursor.LastX = Cursor.LastX + Cursor.ScrollSpeed
			end
			
			-- X - -------------------------------------------------------
			if pad:right() then
				Back.X = Back.X - Cursor.ScrollSpeed
				
				for i = 1, Max.Building.TownCenter do if TownCenter[i].Builded == true then TownCenter[i].X = TownCenter[i].X - Cursor.ScrollSpeed end end
				for i = 1, Max.Building.House do if House[i].Builded == true then House[i].X = House[i].X - Cursor.ScrollSpeed end end
				for i = 1, Max.Unit.Villager do 
					if Villager[i].Exists == true then
						Villager[i].X = Villager[i].X - Cursor.ScrollSpeed
						Villager[i].TarX = Villager[i].TarX - Cursor.ScrollSpeed
					end
				end
				for i = 1, Max.Unit.Militia do 
					if Militia[i].Exists == true then
						Militia[i].X = Militia[i].X - Cursor.ScrollSpeed
						Militia[i].TarX = Militia[i].TarX - Cursor.ScrollSpeed
					end
				end
				for i = 1, Max.Other.Berry do if Berry[i].Exists == true then Berry[i].X = Berry[i].X - Cursor.ScrollSpeed end end
				for i = 1, Max.Other.Tree do if Tree[i].Exists == true then Tree[i].X = Tree[i].X - Cursor.ScrollSpeed end end
				for i = 1, Max.Other.Gold do if Gold[i].Exists == true then Gold[i].X = Gold[i].X - Cursor.ScrollSpeed end end
				for i = 1, Max.Other.Stone do if Stone[i].Exists == true then Stone[i].X = Stone[i].X - Cursor.ScrollSpeed end end
				
				Mineral[1].X = Mineral[1].X - Cursor.ScrollSpeed
				Cursor.LastX = Cursor.LastX - Cursor.ScrollSpeed
			end
		end
	
	if pad:circle() then
		UnSelectBuildings("for")
		UnSelectUnits("for")
		UnSelectResources("for")
	end
	
	
	
	
	
	
	-- this comments are not mine!
	-- and also, it's not working.
	-- what a magic Lua engine was he using that can't even run this piece of code?!?!
	
	--[[
	if start >= 1 then
		bonkSound:play()
		Music.volume(20)
		start = 0
	end
	--]]
	
	-- Control Background motion
	--for i = 1,4 do
	--	if BackY[i] >= 272 then BackY[i] = -272 end
	--	if BackX[i] > 480 then BackX[i] = -480 end
	--	if BackY[i] <= -273 then BackY[i] = 272 end
	--	if BackX[i] < -480 then BackX[i] = 480 end
	--end
	
	-- Control Background motion
	if Back.Y > 0 then 
		Back.Y = Back.Y - 272
	elseif Back.Y < -272 then 
		Back.Y = Back.Y + 272 
	end
	if Back.X > 0 then 
		Back.X = Back.X - 480 
	elseif Back.X < -480 then 
		Back.X = Back.X + 480  
	end
	
	-- Boundaries
	if Cursor.X >= 480 then Cursor.X = 480 end
	if Cursor.X <= 0 then Cursor.X = 0 end
	if Cursor.Y <= 0 then Cursor.Y = 0 end
	if Cursor.Y >= 252 then Cursor.Y = 252 end
	
	
	
	
	-- Selecting Objects
	if Cursor.Y > 20 and Cursor.Y < 200 and pad:cross() then
		if pressed.cross == true then
			pressed.cross = false
			Cursor.LastX = Cursor.X
			Cursor.LastY = Cursor.Y
			
			-- Select
			for i = 1, Max.Building.TownCenter do
				if TownCenter[i].Builded == true then
					if Cursor.LastX >= TownCenter[i].X and Cursor.LastX <= TownCenter[i].X + Info.TC.Width and Cursor.LastY >= TownCenter[i].Y and Cursor.LastY <= TownCenter[i].Y + Info.TC.Height and TownCenter[i].Selected == false then
						PlaySound("TownCenter")
						UnSelectResources("Classic")
						UnSelectBuildings("Classic")
						UnSelectUnits("Classic")
						TownCenter[i].Selected = true
					end
				end
			end
			for i = 1, Max.Building.House do
				if House[i].Builded == true then
					if Cursor.LastX >= House[i].X and Cursor.LastX <= House[i].X + Info.House.Width and Cursor.LastY >= House[i].Y and Cursor.LastY <= House[i].Y + Info.House.Height and House[i].Selected == false then
						PlaySound("House")
						UnSelectResources("Classic")
						UnSelectBuildings("Classic")
						UnSelectUnits("Classic")
						House[i].Selected = true
					end
				end
			end
			for i = 1, Max.Unit.Villager do
				if Villager[i].Exists == true then
					if Cursor.LastX >= Villager[i].X and Cursor.LastX <= Villager[i].X + Info.Villager.Width and Cursor.LastY >= Villager[i].Y and Cursor.LastY <= Villager[i].Y + Info.Villager.Height and Villager[i].Selected == false then
						if Villager[i].Selected == true then
							Villager[i].Selected = false
						else
							UnSelectResources("Classic")
							UnSelectBuildings("Classic")
							UnSelectUnits("Classic")
							Villager[i].Selected = true
						end
					end
				end
			end
			for i = 1, Max.Unit.Militia do
				if Militia[i].Exists == true then
					if Cursor.LastX >= Militia[i].X and Cursor.LastX <= Militia[i].X + Info.Militia.Width and Cursor.LastY >= Militia[i].Y and Cursor.LastY <= Militia[i].Y + Info.Militia.Height and Militia[i].Selected == false then
						if Militia[i].Selected == true then
							Militia[i].Selected = false
						else
							UnSelectResources("Classic")
							UnSelectBuildings("Classic")
							UnSelectUnits("Classic")
							Militia[i].Selected = true
						end
					end
				end
			end
			for i = 1, Max.Other.Berry do
				if Berry[i].Exists == true then
					if Cursor.LastX >= Berry[i].X and Cursor.LastX <= Berry[i].X + Info.Berry.Width and Cursor.LastY >= Berry[i].Y and Cursor.LastY <= Berry[i].Y + Info.Berry.Height and Berry[i].Selected == false then
						--Mp3me.stop()
						--Mp3me.load("Sounds/forage1.mp3")
						--Mp3me.play()
						UnSelectResources("Classic")
						UnSelectBuildings("Classic")
						UnSelectUnits("Classic")
						Berry[i].Selected = true
					end
					--if Cursor.X >= Berry[i].X and Cursor.X <= Berry[i].X + Info.Berry.Width and Cursor.Y >= Berry[i].Y and Cursor.Y <= Berry[i].Y + Info.Berry.Height and Villager[i].Selected == true then
					--	Cursor.Under = Berry[i]
					--end
				end
			end
			for i = 1, Max.Other.Tree do
				if Tree[i].Exists == true then
					if Cursor.LastX >= Tree[i].X and Cursor.LastX <= Tree[i].X + Info.Tree.Width and Cursor.LastY >= Tree[i].Y and Cursor.LastY <= Tree[i].Y + Info.Tree.Height and Tree[i].Selected == false then
						UnSelectResources("Classic")
						UnSelectBuildings("Classic")
						UnSelectUnits("Classic")
						Tree[i].Selected = true
					end
					--if Cursor.X >= Tree[i].X and Cursor.X <= Tree[i].X + Info.Tree.Width and Cursor.Y >= Tree[i].Y and Cursor.Y <= Tree[i].Y + Info.Tree.Height and Villager[i].Selected == true then
					--	Cursor.Under = Tree[i]
					--end
				end
			end
			for i = 1, Max.Other.Gold do
				if Gold[i].Exists == true then
					if Cursor.LastX >= Gold[i].X and Cursor.LastX <= Gold[i].X + Info.Gold.Width and Cursor.LastY >= Gold[i].Y and Cursor.LastY <= Gold[i].Y + Info.Gold.Height and Gold[i].Selected == false then
						UnSelectResources("Classic")
						UnSelectBuildings("Classic")
						UnSelectUnits("Classic")
						Gold[i].Selected = true
					end
					--if Cursor.X >= Gold[i].X and Cursor.X <= Gold[i].X + Info.Gold.Width and Cursor.Y >= Tree[i].Y and Cursor.Y <= Gold[i].Y + Info.Gold.Height and Villager[i].Selected == true then
					--	Cursor.Under = Gold[i]
					--end
				end
			end
			for i = 1, Max.Other.Stone do
				if Stone[i].Exists == true then
					if Cursor.LastX >= Stone[i].X and Cursor.LastX <= Stone[i].X + Info.Stone.Width and Cursor.LastY >= Stone[i].Y and Cursor.LastY <= Stone[i].Y + Info.Stone.Height and Stone[i].Selected == false then
						UnSelectResources("Classic")
						UnSelectBuildings("Classic")
						UnSelectUnits("Classic")
						Stone[i].Selected = true
					end
					--if Cursor.X >= Stone[i].X and Cursor.X <= Stone[i].X + Info.Stone.Width and Cursor.Y >= Stone[i].Y and Cursor.Y <= Stone[i].Y + Info.Stone.Height and Villager[i].Selected == true then
					--	Cursor.Under = Stone[i]
					--end
				end
			end 
			
			for i = 1, Max.Unit.Villager do
				if Villager[i].Selected == true then
					Villager[i].Follow = true
					Villager[i].TarX = Cursor.LastX
					Villager[i].TarY = Cursor.LastY
				end
			end
			for i = 1, Max.Unit.Militia do
				if Militia[i].Selected == true then
					Militia[i].Follow = true
					Militia[i].TarX = Cursor.LastX
					Militia[i].TarY = Cursor.LastY
				end
			end
			
		else
			pressed.cross = true
		end
	end -- End of selecting objects
	
	-- Animation and Walking
	for i = 1, Max.Unit.Militia do
		
		if Militia[i].Exists == true then
			if Militia[i].X == Militia[i].TarX and Militia[i].Y == Militia[i].TarY then
				Militia[i].Follow = false
			end
			if Militia[i].Follow == true then
				if Militia[i].TarX ~= Militia[i].X and Militia[i].TarY ~= Militia[i].Y then
				
				
				
					--if Militia[i].X + Info.Militia.Width >= TownCenter[i].X or House[i].X then -- Collision to Right
					--	Militia[i].X = Militia[i].X - 1
					--else
						if Militia[i].TarX >= Militia[i].X then
							Militia[i].X = Militia[i].X + Militia[i].Speed
						end
					--end
					--if Militia[i].X <= TownCenter[i].X + Info.TC.Width or House[i].X + Info.House.Width then -- Collision to Left
					--	Militia[i].X = Militia[i].X + 1
					--else
						if Militia[i].TarX <= Militia[i].X then
							Militia[i].X = Militia[i].X - Militia[i].Speed
						end
					--end
					--if Militia[i].Y + Info.Militia.Height >= TownCenter[i].Y or House[i].Y then -- Collision to Down
					--	Militia[i].Y = Militia[i].Y - 1
					--else
						if Militia[i].TarY >= Militia[i].Y then
							Militia[i].Y = Militia[i].Y + Militia[i].Speed
						end
					--end
					--if Militia[i].Y <= TownCenter[i].Y + Info.TC.Height or House[i].Y + Info.House.Height then -- Collision to Up
					--	Militia[i].Y = Militia[i].Y + 1
					--else
						if Militia[i].TarY <= Militia[i].Y then
							Militia[i].Y = Militia[i].Y - Militia[i].Speed
						end
					--end
					
					
					
					
				end
				
			end
			
			if Militia[i].TarY < Militia[i].Y and Militia[i].TarX == Militia[i].X then
				Militia[i].IMG = spr.uni.Militia.Up
			elseif Militia[i].TarY > Militia[i].Y and Militia[i].TarX == Militia[i].X then
				Militia[i].IMG = spr.uni.Militia.Down
			elseif Militia[i].TarX < Militia[i].X and Militia[i].TarY == Militia[i].Y then
				Militia[i].IMG = spr.uni.Militia.Left
			elseif Militia[i].TarX > Militia[i].X and Militia[i].TarY == Militia[i].Y then
				Militia[i].IMG = spr.uni.Militia.Right
			elseif Militia[i].TarY < Militia[i].Y and Militia[i].TarX < Militia[i].X then
				Militia[i].IMG = spr.uni.Militia.UpLeft
			elseif Militia[i].TarY > Militia[i].Y and Militia[i].TarX > Militia[i].X then
				Militia[i].IMG = spr.uni.Militia.DownRight
			elseif Militia[i].TarY > Militia[i].Y and Militia[i].TarX < Militia[i].X then
				Militia[i].IMG = spr.uni.Militia.DownLeft
			elseif Militia[i].TarY < Militia[i].Y and Militia[i].TarX> Militia[i].X then
				Militia[i].IMG = spr.uni.Militia.UpRight
			end
		end
		
	end
	for i = 1, Max.Unit.Villager do
		
		if Villager[i].Exists == true then
			if Villager[i].X == Villager[i].TarX and Villager[i].Y == Villager[i].TarY then
				Villager[i].Follow = false
			end
			if Villager[i].Follow == true then
				if Villager[i].TarX ~= Villager[i].X and Villager[i].TarY ~= Villager[i].Y then
				
				
				
					--if Villager[i].X + Info.Villager.Width >= TownCenter[i].X or House[i].X then -- Collision to Right
					--	Villager[i].X = Villager[i].X - 1
					--else
						if Villager[i].TarX >= Villager[i].X then
							Villager[i].X = Villager[i].X + Villager[i].Speed
							--cfm:play()
						end
					--end
					--if Villager[i].X <= TownCenter[i].X + Info.TC.Width or House[i].X + Info.House.Width then -- Collision to Left
					--	Villager[i].X = Villager[i].X + 1
					--else
						if Villager[i].TarX <= Villager[i].X then
							Villager[i].X = Villager[i].X - Villager[i].Speed
							--cfm:play()
						end
					--end
					--if Villager[i].Y + Info.Villager.Height >= TownCenter[i].Y or House[i].Y then -- Collision to Down
					--	Villager[i].Y = Villager[i].Y - 1
					--else
						if Villager[i].TarY >= Villager[i].Y then
							Villager[i].Y = Villager[i].Y + Villager[i].Speed
							--cfm:play()
						end
					--end
					--if Villager[i].Y <= TownCenter[i].Y + Info.TC.Height or House[i].Y + Info.House.Height then -- Collision to Up
					--	Villager[i].Y = Villager[i].Y + 1
					--else
						if Villager[i].TarY <= Villager[i].Y then
							Villager[i].Y = Villager[i].Y - Villager[i].Speed
							--cfm:play()
						end
					--end
					
					
					
					
				end
				
			end
			
			--[[
			if Villager[i].Target == Berry[i] then
				if Berry[i].X >= Villager[i].X then
					Villager[i].X = Villager[i].X + Villager[i].Speed
				end
				if Berry[i].X <= Villager[i].X then
					Villager[i].X = Villager[i].X - Villager[i].Speed
				end
				if Berry[i].Y >= Villager[i].Y then
					Villager[i].Y = Villager[i].Y + Villager[i].Speed
				end
				if Berry[i].Y <= Villager[i].Y then
					Villager[i].Y = Villager[i].Y - Villager[i].Speed
				end
			end
			if Villager[i].Target == Tree[i] then
				if Tree[i].X >= Villager[i].X then
					Villager[i].X = Villager[i].X + Villager[i].Speed
				end
				if Tree[i].X <= Villager[i].X then
					Villager[i].X = Villager[i].X - Villager[i].Speed
				end
				if Tree[i].Y >= Villager[i].Y then
					Villager[i].Y = Villager[i].Y + Villager[i].Speed
				end
				if Tree[i].Y <= Villager[i].Y then
					Villager[i].Y = Villager[i].Y - Villager[i].Speed
				end
			end
			if Villager[i].Food >= Global.Player.Villager.MaxInv or Villager[i].Wood >= Global.Player.Villager.MaxInv or Villager[i].Gold >= Global.Player.Villager.MaxInv or Villager[i].Stone >= Global.Player.Villager.MaxInv then
				if TownCenter[i].X >= Villager[i].X then
					Villager[i].X = Villager[i].X + Villager[i].Speed
				end
				if TownCenter[i].X <= Villager[i].X then
					Villager[i].X = Villager[i].X - Villager[i].Speed
				end
				if TownCenter[i].Y >= Villager[i].Y then
					Villager[i].Y = Villager[i].Y + Villager[i].Speed
				end
				if TownCenter[i].Y <= Villager[i].Y then
					Villager[i].Y = Villager[i].Y - Villager[i].Speed
				end
				if Villager[i].X - Info.Villager.Width >= TownCenter[i].X and Villager[i].X + Info.Villager.Width <= TownCenter[i].X + Info.TC.Width and Villager[i].Y + Info.Villager.Height >= TownCenter[i].Y and Villager[i].Y + Info.Villager.Height <= TownCenter[i].Y + Info.TC.Height and Villager[i].Food ~= 0 or Villager[i].Wood ~= 0 or Villager[i].Gold ~= 0 or Villager[i].Stone ~= 0 then
					Global.Player.Food = Global.Player.Food + Villager[i].Food
					Global.Player.Food = Global.Player.Food + Villager[i].Wood
					Global.Player.Food = Global.Player.Food + Villager[i].Stone
					Global.Player.Food = Global.Player.Food + Villager[i].Gold
					Villager[i].Food = 0
					Villager[i].Wood = 0
					Villager[i].Stone = 0
					Villager[i].Gold = 0
					Villager[i].Target = Villager[i].LastTarget
				end
			end
			--]]
			
			-- Visuals / Animations
			
			-- Male
			if Villager[i].Type == 1 then
				if Villager[i].TarY < Villager[i].Y and Villager[i].TarX == Villager[i].X then
					Villager[i].IMG = spr.uni.VillagerMale.Up
				elseif Villager[i].TarY > Villager[i].Y and Villager[i].TarX == Villager[i].X then
					Villager[i].IMG = spr.uni.VillagerMale.Down
				elseif Villager[i].TarX < Villager[i].X and Villager[i].TarY == Villager[i].Y then
					Villager[i].IMG = spr.uni.VillagerMale.Left
				elseif Villager[i].TarX > Villager[i].X and Villager[i].TarY == Villager[i].Y then
					Villager[i].IMG = spr.uni.VillagerMale.Right
				elseif Villager[i].TarY < Villager[i].Y and Villager[i].TarX < Villager[i].X then
					Villager[i].IMG = spr.uni.VillagerMale.UpLeft
				elseif Villager[i].TarY > Villager[i].Y and Villager[i].TarX > Villager[i].X then
					Villager[i].IMG = spr.uni.VillagerMale.DownRight
				elseif Villager[i].TarY > Villager[i].Y and Villager[i].TarX < Villager[i].X then
					Villager[i].IMG = spr.uni.VillagerMale.DownLeft
				elseif Villager[i].TarY < Villager[i].Y and Villager[i].TarX> Villager[i].X then
					Villager[i].IMG = spr.uni.VillagerMale.UpRight
				end
				
				if Villager[i].Target == Berry[i] then
					-- Insert your "Collecting" animation here (-____-)
				end
			end
			
			-- Female
			if Villager[i].Type == 2 then
				if Villager[i].TarY < Villager[i].Y and Villager[i].TarX== Villager[i].X then
					Villager[i].IMG = spr.uni.VillagerFemale.Up
				elseif Villager[i].TarY > Villager[i].Y and Villager[i].TarX == Villager[i].X then
					Villager[i].IMG = spr.uni.VillagerFemale.Down
				elseif Villager[i].TarX < Villager[i].X and Villager[i].TarY == Villager[i].Y then
					Villager[i].IMG = spr.uni.VillagerFemale.Left
				elseif Villager[i].TarX > Villager[i].X and Villager[i].TarY == Villager[i].Y then
					Villager[i].IMG = spr.uni.VillagerFemale.Right
				elseif Villager[i].TarY < Villager[i].Y and Villager[i].TarX < Villager[i].X then
					Villager[i].IMG = spr.uni.VillagerFemale.UpLeft
				elseif Villager[i].TarY > Villager[i].Y and Villager[i].TarX > Villager[i].X then
					Villager[i].IMG = spr.uni.VillagerFemale.DownRight
				elseif Villager[i].TarY> Villager[i].Y and Villager[i].TarX < Villager[i].X then
					Villager[i].IMG = spr.uni.VillagerFemale.DownLeft
				elseif Villager[i].TarY < Villager[i].Y and Villager[i].TarX > Villager[i].X then
					Villager[i].IMG = spr.uni.VillagerFemale.UpRight
				end
			end
			--[[
			
			Animation
			
			We have a var "Anim.Villager.Type" and more conditions:
			
			
			if Cursor.LastY < Villager[1].Y and Cursor.LastX == Villager[1].X then
				Anim.Villager.Type = "Up"
			end
			if Cursor.LastY > Villager[1].Y and Cursor.LastX == Villager[1].X then
				Anim.Villager.Type = "Down"
			end
			
			
			and then this will check in function "Animation()"
			
			function Animation()
				if Anim.Villager.Type == "Up"
					Row = 2
					screen:blit(X,Y,PIC,x0,y0, x1,y1)
					
					Row 1 - Stand in the all positions
					Row 2 - Walk Up
					Row 3 - Walk Down
					ect...
					
				end
			end
			
			ah, my bad english...
			
			
			yep yep yep
			--]]
		end
	end
		
	if pad:select() then
		Screenshot()
	end
	
	if pad:start() then
		mState = "Menu"
		magic4 = true
	end
	
	--screen:fontPrint(Arial,360-1,190-1,Global.Player.Nick.." ("..Global.Player.Score.."/"..Global.Player.TeamScore..")",Black)
	--screen:fontPrint(Arial,360,190,    Global.Player.Nick.." ("..Global.Player.Score.."/"..Global.Player.TeamScore..")",Global.Player.NickCol)
	
	
	
	end -- mState "Game"'s End
	
	screen.waitVblankStart(1)
	screen.flip()
end
