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

-- We need to be sure, which scenario file we are loading right now.
dofile("Scenario/Selector.cfg")

if Level == 1 then
	LoadCustomMap("CustomMap1", "scn2psp")
	SCNNumber = "E1"
elseif Level == 2 then
	LoadCustomMap("CustomMap2", "scn2psp")
	SCNNumber = "E2"
elseif Level == 3 then
	LoadCustomMap("CustomMap3", "scn2psp")
	SCNNumber = "E3"
elseif Level == 4 then
	LoadCustomMap("CustomMap4", "scn2psp")
	SCNNumber = "E4"
elseif Level == 5 then
	LoadCustomMap("CustomMap5", "scn2psp")
	SCNNumber = "E5"
elseif Level == 6 then
	LoadCustomMap("CustomMap6", "scn2psp")
	SCNNumber = "E6"
end

Textures("Load")

function CheckWorldTheme()
	if Global.World.Theme == "Grass" then
		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" then
		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" then
		spr.assets.MiniMap = Image.load("Game Resources/Backgrounds/MiniMap/OldDesert_MiniMap.png")
		spr.assets.Background = Image.load("Game Resources/Backgrounds/Ground/Desert2.png")
	end
end

CheckWorldTheme()

local EditorSpr = {
	Panel = {
		GameMenu = Image.load("Game Resources/Backgrounds/Editor/CentralMenu.png"),
		Section = Image.load("Game Resources/Backgrounds/Editor/MenuPanel.png"),
		--Param = Image.load("Game Resources/Backgrounds/Editor/Param.png"),
		--ListBox = Image.load("Game Resources/Backgrounds/Editor/ListBox.png"),
		Back = Image.load("Game Resources/Backgrounds/Editor/BackPanel.png"),
		Left = Image.load("Game Resources/Backgrounds/Editor/LeftPanel.png"),
		MiniPanel = Image.load("Game Resources/Backgrounds/PanelBG.png"),
		Controls = Image.load("Game Resources/Backgrounds/ControlsEditor.png")
	},
	Button = {
		ON = Image.load("Game Resources/Backgrounds/Editor/ButtonON.png"),
		OFF = Image.load("Game Resources/Backgrounds/Editor/ButtonOFF.png"),
	},
	MenuButton = {
		ON = Image.load("Game Resources/Buttons/EditorButtonON.png"),
		OFF = Image.load("Game Resources/Buttons/EditorButtonOFF.png"),
	},
	Check = {
		ON = Image.load("Game Resources/Backgrounds/Editor/CheckBoxON.png"),
		OFF = Image.load("Game Resources/Backgrounds/Editor/CheckBoxOFF.png"),
	},
	Check1 = {
		ON = Image.load("Game Resources/Backgrounds/CheckBoxON.png"),
		OFF = Image.load("Game Resources/Backgrounds/CheckBoxOFF.png"),
	},
	Check2 = {
		ON = Image.load("Game Resources/Backgrounds/CheckBox2ON.png"),
		OFF = Image.load("Game Resources/Backgrounds/CheckBox2OFF.png"),
	},
	ICO = {
		Delete = Image.load("Game Resources/Icons/DeleteICO.png"),
		Add = Image.load("Game Resources/Icons/AddICO.png"),
	}
}

local EditorSprInfo = {
	Panel = {
		Section = {
			Width = 480,
			Height = 272,
		},
	},
	Button = {
		Width = 66,
		Height = 11,
	},
	CheckBox = {
		Widht = 14,
		Height = 14,
	},
}





-- Loading Options.
dofile("Configuration/Options.cfg")

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, SpawnObj = "", Action = "Spawn"}
ActualCursorSpeed = "Normal"

mState = "Edit"
--MenPar = 0

-- "Map" Page Vars:
Map = {
	Index = 1,
	Switch = 1,
	-- Min = 1, Max = 3
}
Object = {
	Index = 1,
	Category = 1,
	Page = 1,
		-- 1
		-- 2
}
Player = {
	Index = 1,
	-- 1 = Color
	-- 2 = Civilization
	
	-- 3 = Wood
	-- 4 = Food
	
	-- 5 = Gold
	-- 6 = Stone
	
	
}
-- end

MenParX = 0 -- Max 5
MenParY = 0 -- Max 2
Old_MenParX = MenParX
Old_MenParY = MenParY
--PressedCircle = true

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

LoadObj()
LoadObj = nil

CurButton = 7
OneTime1 = true
ColorReload = false
AllowOldCB = true
FastResMod = 25 -- Fast Resource Modificator

ButtonColor = {
	Save = Color.new(0, 0, 0),
	MapInfo = Color.new(0, 0, 0),
	Test = Color.new(0, 0, 0),
	Controls = Color.new(0, 0, 0),
	ExitSave = Color.new(0, 0, 0),
	Exit = Color.new(0, 0, 0),
	Cancel = Color.new(255, 255, 0)
}

-- Prototype
-- Units Only
function DeleteObject(X, Y)
	for i = 1, Max.Unit.Villager do
		if X >= Villager[i].X and X <= Villager[i].X + Info.Villager.Width and Y >= Villager[i].Y and Y <= Villager[i].Y + Info.Villager.Height and Villager[i].Exists == true then
			Villager[i].Exists = false
		end
	end
	for i = 1, Max.Unit.Militia do
		if X >= Militia[i].X and X <= Militia[i].X + Info.Militia.Width and Y >= Militia[i].Y and Y <= Militia[i].Y + Info.Militia.Height and Militia[i].Exists == true then
			Militia[i].Exists = false
		end
	end
	for i = 1, Max.Building.House do
		if X >= House[i].X and X <= House[i].X + Info.House.Width and Y >= House[i].Y and Y <= House[i].Y + Info.House.Height and House[i].Builded == true then
			House[i].Builded = false
		end
	end
	for i = 1, Max.Building.TownCenter do
		if X >= TownCenter[i].X and X <= TownCenter[i].X + Info.TC.Width and Y >= TownCenter[i].Y and Y <= TownCenter[i].Y + Info.TC.Height and TownCenter[i].Builded == true then
			TownCenter[i].Builded = false
		end
	end
	for i = 1, Max.Other.Berry do
		if X >= Berry[i].X and X <= Berry[i].X + Info.Berry.Width and Y >= Berry[i].Y and Y <= Berry[i].Y + Info.Berry.Height and Berry[i].Exists == true then
			Berry[i].Exists = false
		end
	end
	for i = 1, Max.Other.Tree do
		if X >= Tree[i].X and X <= Tree[i].X + Info.Tree.Width and Y >= Tree[i].Y and Y <= Tree[i].Y + Info.Tree.Height and Tree[i].Exists == true then
			Tree[i].Exists = false
		end
	end
	for i = 1, Max.Other.Gold do
		if X >= Gold[i].X and X <= Gold[i].X + Info.Gold.Width and Y >= Gold[i].Y and Y <= Gold[i].Y + Info.Gold.Height and Gold[i].Exists == true then
			Gold[i].Exists = false
		end
	end
	for i = 1, Max.Other.Stone do
		if X >= Stone[i].X and X <= Stone[i].X + Info.Stone.Width and Y >= Stone[i].Y and Y <= Stone[i].Y + Info.Stone.Height and Stone[i].Exists == true then
			Stone[i].Exists = false
		end
	end
end

function SpawnObject(Entity, x, y, Team)
	-- Team = "Team 1"
	-- Team = "Team 2"
	-- Team = "Team 3"
	if Entity == "Villager Ma" then
		for i = 1, Max.Unit.Villager do
			if Villager[i].Exists == false then
				Villager[i].Exists = true
				Villager[i].X = x
				Villager[i].Y = y
				Villager[i].Type = 1
				Villager[i].IMG = spr.uni.VillagerMale.Down
				PlaySound("VillageCreate")
				break
			end
		end
	end
	if Entity == "Villager Fe" then
		for i = 1, Max.Unit.Villager do
			if Villager[i].Exists == false then
				Villager[i].Exists = true
				Villager[i].X = x
				Villager[i].Y = y
				Villager[i].Type = 2
				Villager[i].IMG = spr.uni.VillagerFemale.Down
				PlaySound("VillageCreate")
				break
			end
		end
	end
	if Entity == "Militia" then
		for i = 1, Max.Unit.Militia do
			if Militia[i].Exists == false then
				Militia[i].Exists = true
				Militia[i].X = x
				Militia[i].Y = y
				PlaySound("MilitaryCreate")
				break
			end
		end
	end
	if Entity == "House A" then
		for i = 1, Max.Building.House do
			if House[i].Builded == false then
				House[i].Builded = true
				House[i].IMG = spr.build.House.DarkTy1
				House[i].X = x
				House[i].Y = y
				PlaySound("House")
				break
			end
		end
	end
	if Entity == "House B" then
		for i = 1, Max.Building.House do
			if House[i].Builded == false then
				House[i].Builded = true
				House[i].IMG = spr.build.House.DarkTy2
				House[i].X = x
				House[i].Y = y
				PlaySound("House")
				break
			end
		end
	end
	if Entity == "House C" then
		for i = 1, Max.Building.House do
			if House[i].Builded == false then
				House[i].Builded = true
				House[i].IMG = spr.build.House.DarkTy3
				House[i].X = x
				House[i].Y = y
				PlaySound("House")
				break
			end
		end
	end
	if Entity == "Town Center" then
		for i = 1, Max.Building.TownCenter do
			if TownCenter[i].Builded == false then
				TownCenter[i].Builded = true
				TownCenter[i].X = x
				TownCenter[i].Y = y
				PlaySound("TownCenter")
				break
			end
		end
	end
	
	if Entity == "Oak A" then
		for i = 1, Max.Other.Tree do
			if Tree[i].Exists == false then
				Tree[i].Exists = true
				Tree[i].X = x
				Tree[i].Y = y
				Tree[i].IMG = spr.res.Tree.Oak1
				break
			end
		end
	end
	if Entity == "Oak B" then
		for i = 1, Max.Other.Tree do
			if Tree[i].Exists == false then
				Tree[i].Exists = true
				Tree[i].X = x
				Tree[i].Y = y
				Tree[i].IMG = spr.res.Tree.Oak2
				break
			end
		end
	end
	if Entity == "Oak C" then
		for i = 1, Max.Other.Tree do
			if Tree[i].Exists == false then
				Tree[i].Exists = true
				Tree[i].X = x
				Tree[i].Y = y
				Tree[i].IMG = spr.res.Tree.Oak3
				break
			end
		end
	end
	if Entity == "Oak D" then
		for i = 1, Max.Other.Tree do
			if Tree[i].Exists == false then
				Tree[i].Exists = true
				Tree[i].X = x
				Tree[i].Y = y
				Tree[i].IMG = spr.res.Tree.Oak4
				break
			end
		end
	end
	if Entity == "Oak E" then
		for i = 1, Max.Other.Tree do
			if Tree[i].Exists == false then
				Tree[i].Exists = true
				Tree[i].X = x
				Tree[i].Y = y
				Tree[i].IMG = spr.res.Tree.Oak5
				break
			end
		end
	end
	
	
	if Entity == "Berry A" then
		for i = 1, Max.Other.Berry do
			if Berry[i].Exists == false then
				Berry[i].Exists = true
				Berry[i].IMG = spr.res.Food.Ty1
				Berry[i].X = x
				Berry[i].Y = y
				break
			end
		end
	end
	if Entity == "Berry B" then
		for i = 1, Max.Other.Berry do
			if Berry[i].Exists == false then
				Berry[i].Exists = true
				Berry[i].IMG = spr.res.Food.Ty2
				Berry[i].X = x
				Berry[i].Y = y
				break
			end
		end
	end
	if Entity == "Berry C" then
		for i = 1, Max.Other.Berry do
			if Berry[i].Exists == false then
				Berry[i].Exists = true
				Berry[i].IMG = spr.res.Food.Ty3
				Berry[i].X = x
				Berry[i].Y = y
				break
			end
		end
	end
	
	
	
	if Entity == "Stone A" then
		for i = 1, Max.Other.Stone do
			if Stone[i].Exists == false then
				Stone[i].Exists = true
				Stone[i].IMG = spr.res.Stone.Ty1
				Stone[i].X = x
				Stone[i].Y = y
				break
			end
		end
	end
	if Entity == "Stone B" then
		for i = 1, Max.Other.Stone do
			if Stone[i].Exists == false then
				Stone[i].Exists = true
				Stone[i].IMG = spr.res.Stone.Ty2
				Stone[i].X = x
				Stone[i].Y = y
				break
			end
		end
	end
	if Entity == "Stone C" then
		for i = 1, Max.Other.Stone do
			if Stone[i].Exists == false then
				Stone[i].Exists = true
				Stone[i].IMG = spr.res.Stone.Ty3
				Stone[i].X = x
				Stone[i].Y = y
				break
			end
		end
	end
	if Entity == "Stone D" then
		for i = 1, Max.Other.Stone do
			if Stone[i].Exists == false then
				Stone[i].Exists = true
				Stone[i].IMG = spr.res.Stone.Ty4
				Stone[i].X = x
				Stone[i].Y = y
				break
			end
		end
	end
	if Entity == "Stone E" then
		for i = 1, Max.Other.Stone do
			if Stone[i].Exists == false then
				Stone[i].Exists = true
				Stone[i].IMG = spr.res.Stone.Ty5
				Stone[i].X = x
				Stone[i].Y = y
				break
			end
		end
	end
	if Entity == "Stone F" then
		for i = 1, Max.Other.Stone do
			if Stone[i].Exists == false then
				Stone[i].Exists = true
				Stone[i].IMG = spr.res.Stone.Ty6
				Stone[i].X = x
				Stone[i].Y = y
				break
			end
		end
	end
	if Entity == "Stone G" then
		for i = 1, Max.Other.Stone do
			if Stone[i].Exists == false then
				Stone[i].Exists = true
				Stone[i].IMG = spr.res.Stone.Ty7
				Stone[i].X = x
				Stone[i].Y = y
				break
			end
		end
	end
	
	
	
	if Entity == "Gold A" then
		for i = 1, Max.Other.Gold do
			if Gold[i].Exists == false then
				Gold[i].Exists = true
				Gold[i].IMG = spr.res.Gold.Ty1
				Gold[i].X = x
				Gold[i].Y = y
				break
			end
		end
	end
	if Entity == "Gold B" then
		for i = 1, Max.Other.Gold do
			if Gold[i].Exists == false then
				Gold[i].Exists = true
				Gold[i].IMG = spr.res.Gold.Ty2
				Gold[i].X = x
				Gold[i].Y = y
				break
			end
		end
	end
	if Entity == "Gold C" then
		for i = 1, Max.Other.Gold do
			if Gold[i].Exists == false then
				Gold[i].Exists = true
				Gold[i].IMG = spr.res.Gold.Ty3
				Gold[i].X = x
				Gold[i].Y = y
				break
			end
		end
	end
	if Entity == "Gold D" then
		for i = 1, Max.Other.Gold do
			if Gold[i].Exists == false then
				Gold[i].Exists = true
				Gold[i].IMG = spr.res.Gold.Ty4
				Gold[i].X = x
				Gold[i].Y = y
				break
			end
		end
	end
	if Entity == "Gold E" then
		for i = 1, Max.Other.Gold do
			if Gold[i].Exists == false then
				Gold[i].Exists = true
				Gold[i].IMG = spr.res.Gold.Ty5
				Gold[i].X = x
				Gold[i].Y = y
				break
			end
		end
	end
	if Entity == "Gold F" then
		for i = 1, Max.Other.Gold do
			if Gold[i].Exists == false then
				Gold[i].Exists = true
				Gold[i].IMG = spr.res.Gold.Ty6
				Gold[i].X = x
				Gold[i].Y = y
				break
			end
		end
	end
	if Entity == "Gold G" then
		for i = 1, Max.Other.Gold do
			if Gold[i].Exists == false then
				Gold[i].Exists = true
				Gold[i].IMG = spr.res.Gold.Ty7
				Gold[i].X = x
				Gold[i].Y = y
				break
			end
		end
	end
end

Total = {
	Villager = 0,
	Militia = 0,
	House = 0,
	TownCenter = 0,
	Berry = 0,
	Gold = 0,
	Stone = 0,
	Tree = 0,
}

function CountMax()
	Total.Villager = 0
	Total.Militia = 0
	Total.House = 0
	Total.TownCenter = 0
	Total.Berry = 0
	Total.Gold = 0
	Total.Stone = 0
	Total.Tree = 0
	for i = 1, Max.Unit.Villager do
		if Villager[i].Exists == true then
			Total.Villager = Total.Villager + 1
		end
	end
	for i = 1, Max.Unit.Militia do
		if Militia[i].Exists == true then
			Total.Militia = Total.Militia + 1
		end
	end
	for i = 1, Max.Building.TownCenter do
		if TownCenter[i].Builded == true then
			Total.TownCenter = Total.TownCenter + 1
		end
	end
	for i = 1, Max.Building.House do
		if House[i].Builded == true then
			Total.House = Total.House + 1
		end
	end
	for i = 1, Max.Other.Berry do
		if Berry[i].Exists == true then
			Total.Berry = Total.Berry + 1
		end
	end
	for i = 1, Max.Other.Gold do
		if Gold[i].Exists == true then
			Total.Gold = Total.Gold + 1
		end
	end
	for i = 1, Max.Other.Stone do
		if Stone[i].Exists == true then
			Total.Stone = Total.Stone + 1
		end
	end
	for i = 1, Max.Other.Tree do
		if Tree[i].Exists == true then
			Total.Tree = Total.Tree + 1
		end
	end
	
end

function MakeAction()
	if Cursor.Action == "Spawn" then
		SpawnObject(Cursor.SpawnObj, Cursor.X - Info.Villager.Width/2, Cursor.Y - Info.Villager.Height/2, "Team 1")
		CountMax()
	end
	
	if Cursor.Action == "Delete" then
		DeleteObject(Cursor.X, Cursor.Y)
		CountMax()
	end
end

-- If Panels are open Booleans
Opened = {
	PanelUp = true, -- Always
	PanelLeft = false,
	PanelDown = true,
}

-- Boundaries variables
Place = {
	X1 = 0,
	Y1 = 0,
	X2 = 0,
	Y2 = 0,
}
local Back = {X = 0, Y = 0}

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

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
}

DebTex = {
	X = 0,
	Y = 0,
}

tick = 0

if Global.World.Theme == "Grass" then
	Map.Switch = 1
elseif Global.World.Theme == "Snow" then
	Map.Switch = 2
elseif Global.World.Theme == "Desert" then
	Map.Switch = 3
end

ObjText = {
	X = 5,
	-- +12px every time
	Y1 = 40,
	Y2 = 52,
	Y3 = 64,
	Y4 = 76,
	Y5 = 88,
	Y6 = 100,
	Y7 = 112,
	Y8 = 124,
	Y9 = 136,
	Y10 = 148,
	Y11 = 160,
	Y12 = 172,
	Y13 = 184
}

magic=true
magic2=nil
magic3={x=0,y=0}
function magicUpd()
	if magic and MenParX and MenParY then
		Opened.PanelDown = true
		
		magic2=Image.createEmpty(480,28)
		
		if mState == "ObjectMenu" then
			magic2:fillRect(1,1, 339, 24, Color.new(255,255,0,80))
		end
		magic2:blit(0,0,EditorSpr.Panel.Section)
		--[[
		for i2 = 1,2 do
			for i = 1,5 do
				if(i==MenParX && i2==MenParY)then
					magic2:blit(2+(i-1)*68,2+(i2-1)*12,EditorSpr.Button.ON)
				end
			end
		end]]
		magic2:blit(2+(MenParX-1)*68,2+(MenParY-1)*12,EditorSpr.Button.ON)
		
		magic2:fontPrint(Arial, 20-1, 10-1, "Map", Black)
		magic2:fontPrint(Arial, 81-1, 10-1, "Terrain", Black)
		magic2:fontPrint(Arial, 144-1, 10-1, "Player", Black)
		magic2:fontPrint(Arial, 212-1, 10-1, "Objects", Black)
		magic2:fontPrint(Arial, 277-1, 10-1,"Diplomacy", Black)
		magic2:fontPrint(Arial, 4-1, 22-1, "Global Win", Black)
		magic2:fontPrint(Arial, 80-1, 22-1, "Options", Black)
		magic2:fontPrint(Arial, 142-1, 22-1, "Messages", Black)
		magic2:fontPrint(Arial, 217-1, 22-1, "Intro", Black)
		magic2:fontPrint(Arial, 281-1, 22-1, "Triggers", Black)
		
		magic2:fontPrint(Arial, 20, 10, "Map", White)
		magic2:fontPrint(Arial, 81, 10, "Terrain", White)
		magic2:fontPrint(Arial, 144, 10, "Player", White)
		magic2:fontPrint(Arial, 212, 10, "Objects", White)
		magic2:fontPrint(Arial, 277, 10, "Diplomacy", White)
		magic2:fontPrint(Arial, 4, 22, "Global Win", White)
		magic2:fontPrint(Arial, 80, 22, "Options", White)
		magic2:fontPrint(Arial, 142, 22, "Messages", White)
		magic2:fontPrint(Arial, 217, 22, "Intro", White)
		magic2:fontPrint(Arial, 281, 22, "Triggers", White)
		
		magic=false
	else
		if magic then
			magic=false
			Opened.PanelDown = false
		end
	end
end

magic4=true
mPage=nil
function magicUpd2()
	if magic4 then
		mPage=Image.createEmpty(125,169)
		mPage:blit(0, 0, EditorSpr.Panel.Left)
		
		for k in pairs(ObjText) do
			if(k~="X")then
				ObjText[k]=ObjText[k]-28
			end
		end
		
		if Object.Page == 1 then
			mPage:fontPrint(Arial, ObjText.X, ObjText.Y1, "Villager (Male)", White)
			mPage:fontPrint(Arial, ObjText.X, ObjText.Y2, "Villager (Female)", White)
			mPage:fontPrint(Arial, ObjText.X, ObjText.Y3, "Militia", White)
			mPage:fontPrint(Arial, ObjText.X, ObjText.Y4, "House (Type A)", White)
			mPage:fontPrint(Arial, ObjText.X, ObjText.Y5, "House (Type B)", White)
			mPage:fontPrint(Arial, ObjText.X, ObjText.Y6, "House (Type C)", White)
			mPage:fontPrint(Arial, ObjText.X, ObjText.Y7, "Town Center", White)
			mPage:fontPrint(Arial, ObjText.X, ObjText.Y8, "Berry (Type A)", White)
			mPage:fontPrint(Arial, ObjText.X, ObjText.Y9, "Berry (Type B)", White)
			mPage:fontPrint(Arial, ObjText.X, ObjText.Y10,"Berry (Type C)", White)
			mPage:fontPrint(Arial, ObjText.X, ObjText.Y11, "Tree (Oak A)", White)
			mPage:fontPrint(Arial, ObjText.X, ObjText.Y12, "Tree (Oak B)", White)
			mPage:fontPrint(Arial, ObjText.X, ObjText.Y13, "Tree (Oak C)", White)
		elseif Object.Page == 2 then
			mPage:fontPrint(Arial, ObjText.X, ObjText.Y1, "Tree (Oak D)", White)
			mPage:fontPrint(Arial, ObjText.X, ObjText.Y2, "Tree (Oak E)", White)
			mPage:fontPrint(Arial, ObjText.X, ObjText.Y3, "Gold (Type A)", White)
			mPage:fontPrint(Arial, ObjText.X, ObjText.Y4, "Gold (Type B)", White)
			mPage:fontPrint(Arial, ObjText.X, ObjText.Y5, "Gold (Type C)", White)
			mPage:fontPrint(Arial, ObjText.X, ObjText.Y6, "Gold (Type D)", White)
			mPage:fontPrint(Arial, ObjText.X, ObjText.Y7, "Gold (Type E)", White)
			mPage:fontPrint(Arial, ObjText.X, ObjText.Y8, "Gold (Type F)", White)
			mPage:fontPrint(Arial, ObjText.X, ObjText.Y9, "Gold (Type G)", White)
			mPage:fontPrint(Arial, ObjText.X, ObjText.Y10, "Stone (Type A)", White)
			mPage:fontPrint(Arial, ObjText.X, ObjText.Y11, "Stone (Type B)", White)
			mPage:fontPrint(Arial, ObjText.X, ObjText.Y12, "Stone (Type C)", White)
			mPage:fontPrint(Arial, ObjText.X, ObjText.Y13, "Stone (Type D)", White)
		elseif Object.Page == 3 then
			mPage:fontPrint(Arial, ObjText.X, ObjText.Y1, "Stone (Type E)", White)
			mPage:fontPrint(Arial, ObjText.X, ObjText.Y2, "Stone (Type F)", White)
			mPage:fontPrint(Arial, ObjText.X, ObjText.Y3, "Stone (Type G)", White)
		end
		
		if Object.Page == 1 then
			
			if Object.Index == 1 then
				mPage:fontPrint(Arial,ObjText.X,ObjText.Y1,"Villager (Male)",Yellow)
			elseif Object.Index == 2 then
				mPage:fontPrint(Arial,ObjText.X,ObjText.Y2,"Villager (Female)",Yellow)
			elseif Object.Index == 3 then
				mPage:fontPrint(Arial,ObjText.X,ObjText.Y3,"Militia",Yellow)
			elseif Object.Index == 4 then
				mPage:fontPrint(Arial,ObjText.X,ObjText.Y4,"House (Type A)",Yellow)
			elseif Object.Index == 5 then
				mPage:fontPrint(Arial,ObjText.X,ObjText.Y5,"House (Type B)",Yellow)
			elseif Object.Index == 6 then
				mPage:fontPrint(Arial,ObjText.X,ObjText.Y6,"House (Type C)",Yellow)
			elseif Object.Index == 7 then
				mPage:fontPrint(Arial,ObjText.X,ObjText.Y7,"Town Center",Yellow)
			elseif Object.Index == 8 then
				mPage:fontPrint(Arial,ObjText.X,ObjText.Y8,"Berry (Type A)",Yellow)
			elseif Object.Index == 9 then
				mPage:fontPrint(Arial,ObjText.X,ObjText.Y9,"Berry (Type B)",Yellow)
			elseif Object.Index == 10 then
				mPage:fontPrint(Arial,ObjText.X,ObjText.Y10,"Berry (Type C)",Yellow)
			elseif Object.Index == 11 then
				mPage:fontPrint(Arial,ObjText.X,ObjText.Y11,"Tree (Oak A)",Yellow)
			elseif Object.Index == 12 then
				mPage:fontPrint(Arial,ObjText.X,ObjText.Y12,"Tree (Oak B)",Yellow)
			elseif Object.Index == 13 then
				mPage:fontPrint(Arial,ObjText.X,ObjText.Y13,"Tree (Oak C)",Yellow)
			end
		
		elseif Object.Page == 2 then
			
			if Object.Index == 1 then
				mPage:fontPrint(Arial,ObjText.X,ObjText.Y1,"Tree (Oak D)",Yellow)
			elseif Object.Index == 2 then
				mPage:fontPrint(Arial,ObjText.X,ObjText.Y2,"Tree (Oak E)",Yellow)
			elseif Object.Index == 3 then
				mPage:fontPrint(Arial,ObjText.X,ObjText.Y3,"Gold (Type A)",Yellow)
			elseif Object.Index == 4 then
				mPage:fontPrint(Arial,ObjText.X,ObjText.Y4,"Gold (Type B)",Yellow)
			elseif Object.Index == 5 then
				mPage:fontPrint(Arial,ObjText.X,ObjText.Y5,"Gold (Type C)",Yellow)
			elseif Object.Index == 6 then
				mPage:fontPrint(Arial,ObjText.X,ObjText.Y6,"Gold (Type D)",Yellow)
			elseif Object.Index == 7 then
				mPage:fontPrint(Arial,ObjText.X,ObjText.Y7,"Gold (Type E)",Yellow)
			elseif Object.Index == 8 then
				mPage:fontPrint(Arial,ObjText.X,ObjText.Y8,"Gold (Type F)",Yellow)
			elseif Object.Index == 9 then
				mPage:fontPrint(Arial,ObjText.X,ObjText.Y9,"Gold (Type G)",Yellow)
			elseif Object.Index == 10 then
				mPage:fontPrint(Arial,ObjText.X,ObjText.Y10,"Stone (Type A)",Yellow)
			elseif Object.Index == 11 then
				mPage:fontPrint(Arial,ObjText.X,ObjText.Y11,"Stone (Type B)",Yellow)
			elseif Object.Index == 12 then
				mPage:fontPrint(Arial,ObjText.X,ObjText.Y12,"Stone (Type C)",Yellow)
			elseif Object.Index == 13 then
				mPage:fontPrint(Arial,ObjText.X,ObjText.Y13,"Stone (Type D)",Yellow)
			end
		
		elseif Object.Page == 3 then
			
			if Object.Index == 1 then
				mPage:fontPrint(Arial,ObjText.X,ObjText.Y1,"Stone (Type E)",Yellow)
			elseif Object.Index == 2 then
				mPage:fontPrint(Arial,ObjText.X,ObjText.Y2,"Stone (Type F)",Yellow)
			elseif Object.Index == 3 then
				mPage:fontPrint(Arial,ObjText.X,ObjText.Y3,"Stone (Type G)",Yellow)
			end
			
		end
		
		for k in pairs(ObjText) do
			if(k~="X")then
				ObjText[k]=ObjText[k]+28
			end
		end
		
		magic4=false
	end
end

function InitMenPar(Type, IncludeOld)
	if pad:triangle() then
		if Type == "Control" then
		
			if not pad:square() then
				if pad:left() then 
					if pressed.left == false then
						pressed.left = true
						
						if IncludeOld == true then
							if MenParX ~= Old_MenParX and AllowOldCB == true then
								MenParX = Old_MenParX
								AllowOldCB = false
							end
						end
						
						MenParX = MenParX - 1
						
						--PressedCircle = false
						if MenParX < 1 then MenParX = 5 Old_MenParX = 5 end
					end
				else
					pressed.left = false
				end
				
				if pad:right() then 
					if pressed.right == false then
						pressed.right = true
						
						if IncludeOld == true then
							if MenParX ~= Old_MenParX and AllowOldCB == true then
								MenParX = Old_MenParX
								AllowOldCB = false
							end
						end
						
						MenParX = MenParX + 1
						
						
						if MenParX > 5 then MenParX = 1 Old_MenParX = 1 end
					end
				else
					pressed.right = false
				end
				
				if pad:up() then 
					if pressed.up == false then
						pressed.up = true
						
						if IncludeOld == true then
							if MenParY ~= Old_MenParY and AllowOldCB == true then
								MenParY = Old_MenParY
								AllowOldCB = false
							end
						end
						
						MenParY = MenParY - 1
						
						if MenParY < 1 then MenParY = 1 Old_MenParY = 1 end
					end
				else
					pressed.up = false
				end

				if pad:down() then 
					if pressed.down == false then
						pressed.down = true
						
						if IncludeOld == true then
							if MenParY ~= Old_MenParY and AllowOldCB == true then
								MenParY = Old_MenParY
								AllowOldCB = false
							end
						end
						
						MenParY = MenParY + 1
						
						if MenParY > 2 then MenParY = 2 Old_MenParY = 2 end
						end
				else
					pressed.down = false
				end
				
				if (pad:up() or pad:down() or pad:left() or pad:right()) and mState == "Edit" then
					mState = "ObjectMenu"
					MenParX = 1
					MenParY = 1
				end
			end
			
		elseif Type == "Cancel" then
			--if pad:circle() then
				AllowOldCB = false
				Old_MenParX = MenParX
				Old_MenParY = MenParY
				MenParX = 0
				MenParY = 0
				mState = "Edit"
			--end
		elseif Type == "Accept" then
			--if pad:cross() then
				MenParX = Old_MenParX
				MenParY = Old_MenParY
				AllowOldCB = false
				mState = "ObjectMenu"
			--end
		end
	end
end

Place.X1 = 0
Place.X2 = 480
Place.Y1 = 0
Place.Y2 = 272

oldColor = "Blue"
AcceptColorChange = false

while true do
	pad = Controls.read()
	screen:clear()
	
	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 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, чтобы не так уж сильно мельтешил экран.
	------------------------------------------------------------------------------------
	------------------------------------------------------------------------------------
	------------------------------------------------------------------------------------
	
	if ColorReload == true and AcceptColorChange == true then
		Textures("ColorLoad")
	end
	
	for i = 1, Max.Building.TownCenter do
		if TownCenter[i].Builded == true and TownCenter[i].X + Info.TC.Width >= Place.X1 and TownCenter[i].X <= Place.X2 and TownCenter[i].Y + Info.TC.Height >= Place.Y1 and TownCenter[i].Y <= Place.Y2 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 >= Place.X1 and House[i].X <= Place.X2 and House[i].Y + Info.House.Height >= Place.Y1 and House[i].Y <= Place.Y2 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 >= Place.X1 and Berry[i].X <= Place.X2 and Berry[i].Y + Info.Berry.Height >= Place.Y1 and Berry[i].Y <= Place.Y2 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
	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 >= Place.X1 and Tree[i].X <= Place.X2 and Tree[i].Y + Info.Tree.Height >= Place.Y1 and Tree[i].Y <= Place.Y2 then
			if Tree[i].Selected == true then
				screen:blit(Tree[i].X + 8, Tree[i].Y + Info.Tree.Height / 2 + 15, 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 >= Place.X1 and Gold[i].X <= Place.X2 and Gold[i].Y + Info.Gold.Height >= Place.Y1 and Gold[i].Y <= Place.Y2 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 >= Place.X1 and Stone[i].X <= Place.X2 and Stone[i].Y + Info.Stone.Height >= Place.Y1 and Stone[i].Y <= Place.Y2 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 >= Place.X1 and Villager[i].X <= Place.X2 and Villager[i].Y + Info.Villager.Height >= Place.Y1 and Villager[i].Y <= Place.Y2 then
			if Villager[i].Selected == true then
				screen:blit(Villager[i].X - 7, Villager[i].Y + 30, spr.uni.Select.Small) -- Bound
				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, 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 >= Place.X1 and Militia[i].X <= Place.X2 and Militia[i].Y + Info.Militia.Height >= Place.Y1 and Militia[i].Y <= Place.Y2 then
			if Militia[i].Selected == true then
				screen:blit(Militia[i].X - 7, Militia[i].Y + 30, spr.uni.Select.Small) -- Bound
				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, 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
	
	------------------------------------------------------------------------------------
	------------------------------------------------------------------------------------
	------------------------------------------------------------------------------------
	
	if tick >= 100 then tick = 0 end
	tick = tick + 1
	
	if Opened.PanelLeft == true then
		DebTex.X = 132
		DebTex.Y = 0
	else
		DebTex.X = 0
		DebTex.Y = 0
	end
	
	
	if Option.Debug.DebugText then
		if Option.Debug.ShowFPS then
			DrawShadowString(Arial, DebTex.X + 10, DebTex.Y + 60, "Tick: "..tick, White)
			DrawShadowString(Arial, DebTex.X + 10, DebTex.Y + 78, "Total Obj: "..Debug.TotalObj.." / "..Debug.MaxObj, White)
			DrawShadowString(Arial, DebTex.X + 10, DebTex.Y + 96, "Render Obj: "..Debug.RenderObj.." / "..Debug.MaxObj, White)
		else
			DrawShadowString(Arial, DebTex.X + 10, DebTex.Y + 45, "Tick: "..tick, White)
			DrawShadowString(Arial, DebTex.X + 10, DebTex.Y + 63, "Total Obj: "..Debug.TotalObj.." / "..Debug.MaxObj, White)
			DrawShadowString(Arial, DebTex.X + 10, DebTex.Y + 81, "Render Obj: "..Debug.RenderObj.." / "..Debug.MaxObj, White)
		end
	end
	if Option.Debug.ShowFPS == true then
		DrawShadowString(Arial, DebTex.X + 10, DebTex.Y + 45, "FPS: "..math.floor(FPS), White)
		--DrawShadowString(Arial, DebTex.X + 10, DebTex.Y + 63, "MenParX = "..MenParX, White)
		--DrawShadowString(Arial, DebTex.X + 10, DebTex.Y + 81, "MenParY = "..MenParY, White)
	end
	
	Debug.TotalObj = 0
	Debug.RenderObj = 0
	
	if(magic3.x~=MenParX or magic3.y~=MenParY or magic) then
		magic3.x=MenParX
		magic3.y=MenParY
		magic=true
		magicUpd()
	end
	
	if(magic2)then screen:blit(0,0,magic2) end
	
	-------------------- X Part --------------------


	--DrawShadowString(Arial, 144, 10, "Player", White)
	
	---- Игрок - Player
	-- 00100
	-- 00000
	if MenParX == 3 and MenParY == 1 then
		Opened.PanelDown = true
	else
		Opened.PanelDown = false
	end
	
	---- Единицы - Objects (Units + Buildings)
	-- 00010
	-- 00000
	if MenParX == 4 and MenParY == 1 then
		Opened.PanelLeft = true
	else
		Opened.PanelLeft = false
	end

	--DrawShadowString(Arial, 277, 10, "Diplomacy", White)
	
	-------------------- Y Part --------------------
	--DrawShadowString(Arial, 281, 22, "Triggers", White)
	
	
	
	--------------------------------------------------------------------------------------------------------------------------------------------------
	--------------------------------------------------------------------------------------------------------------------------------------------------
	--------------------------------------------------------------------------------------------------------------------------------------------------
	--------------------------------------------------------------------------------------------------------------------------------------------------
	--------------------------------------------------------------------------------------------------------------------------------------------------
	
	if mState == "ObjectMenu" then
	
	if MenParX ~= 0 and MenParY ~= 0 then
		screen:blit(0, 197, EditorSpr.Panel.Back)
	end
	
	if MenParX == 1 and MenParY == 2 then
		screen:blit(338, 200, spr.assets.MiniMap)
	end
	
	
	
	
	if (MenParX == 4 and MenParY == 1) or (magic4) then
		
		if not pad:triangle() and not pad:square() then
			if pad:up() then
				Object.Index = Object.Index - 1
				magic4=true
				if Object.Page == 1 then
					if Object.Index < 1 then
						Object.Index = 3
						Object.Page = 3
					end
				elseif Object.Page == 2 then
					if Object.Index < 1 then
						Object.Index = 13
						Object.Page = 1
					end
				elseif Object.Page == 3 then
					if Object.Index < 1 then
						Object.Index = 13
						Object.Page = 2
					end
				end
			end
			
			if pad:down() then
				Object.Index = Object.Index + 1
				magic4=true
				if Object.Page == 1 then
					if Object.Index > 13 then
						Object.Index = 1
						Object.Page = 2
					end
				elseif Object.Page == 2 then
					if Object.Index > 13 then
						Object.Index = 1
						Object.Page = 3
					end
				elseif Object.Page == 3 then
					if Object.Index > 3 then
						Object.Index = 1
						Object.Page = 1
					end
				end
			end
		
		
		if pad:cross() then
			if Object.Page == 1 then
				if Object.Index == 1 then
					Cursor.SpawnObj = "Villager Ma"
				elseif Object.Index == 2 then
					Cursor.SpawnObj = "Villager Fe"
				elseif Object.Index == 3 then
					Cursor.SpawnObj = "Militia"
				elseif Object.Index == 4 then
					Cursor.SpawnObj = "House A"
				elseif Object.Index == 5 then
					Cursor.SpawnObj = "House B"
				elseif Object.Index == 6 then
					Cursor.SpawnObj = "House C"
				elseif Object.Index == 7 then
					Cursor.SpawnObj = "Town Center"
				elseif Object.Index == 8 then
					Cursor.SpawnObj = "Berry A"
				elseif Object.Index == 9 then
					Cursor.SpawnObj = "Berry B"
				elseif Object.Index == 10 then
					Cursor.SpawnObj = "Berry C"
				elseif Object.Index == 11 then
					Cursor.SpawnObj = "Oak A"
				elseif Object.Index == 12 then
					Cursor.SpawnObj = "Oak B"
				elseif Object.Index == 13 then
					Cursor.SpawnObj = "Oak C"
				end
			elseif Object.Page == 2 then
				if Object.Index == 1 then
					Cursor.SpawnObj = "Oak D"
				elseif Object.Index == 2 then
					Cursor.SpawnObj = "Oak E"
				elseif Object.Index == 3 then
					Cursor.SpawnObj = "Gold A"
				elseif Object.Index == 4 then
					Cursor.SpawnObj = "Gold B"
				elseif Object.Index == 5 then
					Cursor.SpawnObj = "Gold C"
				elseif Object.Index == 6 then
					Cursor.SpawnObj = "Gold D"
				elseif Object.Index == 7 then
					Cursor.SpawnObj = "Gold E"
				elseif Object.Index == 8 then
					Cursor.SpawnObj = "Gold F"
				elseif Object.Index == 9 then
					Cursor.SpawnObj = "Gold G"
				elseif Object.Index == 10 then
					Cursor.SpawnObj = "Stone A"
				elseif Object.Index == 11 then
					Cursor.SpawnObj = "Stone B"
				elseif Object.Index == 12 then
					Cursor.SpawnObj = "Stone C"
				elseif Object.Index == 13 then
					Cursor.SpawnObj = "Stone D"
				end
			elseif Object.Page == 3 then
				if Object.Index == 1 then
					Cursor.SpawnObj = "Stone E"
				elseif Object.Index == 2 then
					Cursor.SpawnObj = "Stone F"
				elseif Object.Index == 3 then
					Cursor.SpawnObj = "Stone G"
				end
			end
		end -- pad:cross() If
		
		end
		
		magicUpd2()
		screen:blit(0, 28, mPage)
	end -- Object Menu If
	
	if MenParX == 1 and MenParY == 1 then
		
		-- 1 = 8,206
		-- 2 = 8,228
		-- 3 = 8,250
		
		-- Text X = 31
		
		if not pad:triangle() and not pad:square() then
			if pad:up() then
				if pressed.up == false then
					pressed.up = true
					Map.Index = Map.Index - 1
					if Map.Index < 1 then Map.Index = 1 end
				end
			else
				pressed.up = false
			end
			
			if pad:down() then 
				if pressed.down == false then
					pressed.down = true
					Map.Index = Map.Index + 1
					if Map.Index > 3 then Map.Index = 3 end
				end
			else
				pressed.down = false
			end
		
		
			if pad:cross() then
				if pressed.cross == false then
					pressed.cross = true
					if Map.Index == 1 then
						Map.Switch = 1
						Global.World.Theme = "Grass"
					elseif Map.Index == 2 then
						Map.Switch = 2
						Global.World.Theme = "Snow"
					elseif Map.Index == 3 then
						Map.Switch = 3
						Global.World.Theme = "Desert"
					end
					CheckWorldTheme()
				end
			else
				pressed.cross = false
			end
		
		end
		
		screen:fontPrint(Arial,31,218,"Grass",Black)
		screen:fontPrint(Arial,31,241,"Snow",Black)
		screen:fontPrint(Arial,31,263,"Desert",Black)
		
		screen:fontPrint(Arial,31+1,218+1,"Grass",White)
		screen:fontPrint(Arial,31+1,241+1,"Snow",White)
		screen:fontPrint(Arial,31+1,263+1,"Desert",White)
		
		if Map.Index == 1 then
			screen:fontPrint(Arial,31+1,218+1,"Grass",Yellow)
		elseif Map.Index == 2 then
			screen:fontPrint(Arial,31+1,241+1,"Snow",Yellow)
		elseif Map.Index == 3 then
			screen:fontPrint(Arial,31+1,263+1,"Desert",Yellow)
		end
		
		if Map.Switch == 1 then
			screen:blit(8, 206, EditorSpr.Check1.ON)
			screen:blit(8, 228, EditorSpr.Check2.OFF)
			screen:blit(8, 250, EditorSpr.Check2.OFF)
		elseif Map.Switch == 2 then
			screen:blit(8, 206, EditorSpr.Check2.OFF)
			screen:blit(8, 228, EditorSpr.Check1.ON)
			screen:blit(8, 250, EditorSpr.Check2.OFF)
		elseif Map.Switch == 3 then
			screen:blit(8, 206, EditorSpr.Check2.OFF)
			screen:blit(8, 228, EditorSpr.Check2.OFF)
			screen:blit(8, 250, EditorSpr.Check1.ON)
		end
	end
	
	-- Player (Color, Civilization and Resources Manager)
	if MenParX == 3 and MenParY == 1 then
		
		
		if not pad:triangle() and not pad:square() then
		if pad:up() then
			if pressed.up == false then
				pressed.up = true
				Player.Index = Player.Index - 1
				if Player.Index < 1 then Player.Index = 6 end
			end
		else
			pressed.up = false
		end
		
		if pad:down() then
			if pressed.down == false then
				pressed.down = true
				Player.Index = Player.Index + 1
				if Player.Index > 6 then Player.Index = 1 end
			end
		else
			pressed.down = false
		end
		
		if pad:left() then
			if Player.Index == 1 then
				if Global.Player.Color == "Blue" then Global.Player.Color = "Red"
				elseif Global.Player.Color == "Red" then Global.Player.Color = "Blue" end
				
			elseif Player.Index == 2 then
				if Global.Player.Civ == "Celt" then
					Global.Player.Civ = "Vikings"
				elseif Global.Player.Civ == "Vikings" then
					Global.Player.Civ = "Saracens"
				elseif Global.Player.Civ == "Saracens" then
					Global.Player.Civ = "Mongols"
				elseif Global.Player.Civ == "Mongols" then
					Global.Player.Civ = "Britons"
				elseif Global.Player.Civ == "Britons" then
					Global.Player.Civ = "Persians"
				elseif Global.Player.Civ == "Persians" then
					Global.Player.Civ = "Byzanties"
				elseif Global.Player.Civ == "Byzanties" then
					Global.Player.Civ = "Japanese"
				elseif Global.Player.Civ == "Japanese" then
					Global.Player.Civ = "Celt"
				end
				
			elseif Player.Index == 3 then
				if not pad:l() then Global.Player.Wood = Global.Player.Wood - 1 end
				if pad:l() then Global.Player.Wood = Global.Player.Wood - FastResMod end
				if Global.Player.Wood < 0 then Global.Player.Wood = 0 end
			elseif Player.Index == 4 then
				if not pad:l() then Global.Player.Food = Global.Player.Food - 1 end
				if pad:l() then Global.Player.Food = Global.Player.Food - FastResMod end
				if Global.Player.Food < 0 then Global.Player.Food = 0 end
			elseif Player.Index == 5 then
				if not pad:l() then Global.Player.Gold = Global.Player.Gold - 1 end
				if pad:l() then Global.Player.Gold = Global.Player.Gold - FastResMod end
				if Global.Player.Gold < 0 then Global.Player.Gold = 0 end
			elseif Player.Index == 6 then
				if not pad:l() then Global.Player.Stone = Global.Player.Stone - 1 end
				if pad:l() then Global.Player.Stone = Global.Player.Stone - FastResMod end
				if Global.Player.Stone < 0 then Global.Player.Stone = 0 end
			end
		
		end
		
		if pad:right() then
			if Player.Index == 1 then
				if Global.Player.Color == "Blue" then Global.Player.Color = "Red"
				elseif Global.Player.Color == "Red" then Global.Player.Color = "Blue" end
				
			elseif Player.Index == 2 then
				if Global.Player.Civ == "Celt" then Global.Player.Civ = "Japanese"
				elseif Global.Player.Civ == "Japanese" then Global.Player.Civ = "Byzanties"
				elseif Global.Player.Civ == "Byzanties" then Global.Player.Civ = "Persians"
				elseif Global.Player.Civ == "Persians" then Global.Player.Civ = "Britons"
				elseif Global.Player.Civ == "Britons" then Global.Player.Civ = "Mongols"
				elseif Global.Player.Civ == "Mongols" then Global.Player.Civ = "Saracens"
				elseif Global.Player.Civ == "Saracens" then Global.Player.Civ = "Vikings"
				elseif Global.Player.Civ == "Vikings" then Global.Player.Civ = "Celt" end
				
			elseif Player.Index == 3 then
				if not pad:l() then Global.Player.Wood = Global.Player.Wood + 1 end
				if pad:l() then Global.Player.Wood = Global.Player.Wood + FastResMod end
				if Global.Player.Wood > 100000 then Global.Player.Wood = 100000 end
			elseif Player.Index == 4 then
				if not pad:l() then Global.Player.Food = Global.Player.Food + 1 end
				if pad:l() then Global.Player.Food = Global.Player.Food + FastResMod end
				if Global.Player.Food > 100000 then Global.Player.Food = 100000 end
			elseif Player.Index == 5 then
				if not pad:l() then Global.Player.Gold = Global.Player.Gold + 1 end
				if pad:l() then Global.Player.Gold = Global.Player.Gold + FastResMod end
				if Global.Player.Gold > 100000 then Global.Player.Gold = 100000 end
			elseif Player.Index == 6 then
				if not pad:l() then Global.Player.Stone = Global.Player.Stone + 1 end
				if pad:l() then Global.Player.Stone = Global.Player.Stone + FastResMod end
				if Global.Player.Stone > 100000 then Global.Player.Stone = 100000 end
			end
		end
		
		end
		
		screen:fillRect(54, 207, 75, 14, Black)
		screen:fillRect(51, 230, 78, 14, Black)
		
		screen:fillRect(186, 207, 80, 14, Black)
		screen:fillRect(182, 230, 80, 14, Black)
		
		screen:fillRect(332, 207, 80, 14, Black)
		screen:fillRect(336, 230, 80, 14, Black)
		
		-- Black
		screen:fontPrint(Arial,21-1,218-1,"Color: "..Global.Player.Color, Black)
		screen:fontPrint(Arial,21-1,241-1,"Civil.: "..Global.Player.Civ, Black)
		
		screen:fontPrint(Arial,150-1,218-1,"Wood: "..Global.Player.Wood, Black)
		screen:fontPrint(Arial,150-1,241-1,"Food: "..Global.Player.Food, Black)
		
		screen:fontPrint(Arial,300-1,218-1,"Gold: "..Global.Player.Gold, Black)
		screen:fontPrint(Arial,300-1,241-1,"Stone: "..Global.Player.Stone, Black)
		
		-- White
		screen:fontPrint(Arial,21,218,"Color: "..Global.Player.Color, White)
		screen:fontPrint(Arial,21,241,"Civil.: "..Global.Player.Civ, White)
		
		screen:fontPrint(Arial,150,218,"Wood: "..Global.Player.Wood, White)
		screen:fontPrint(Arial,150,241,"Food: "..Global.Player.Food, White)
		
		screen:fontPrint(Arial,300,218,"Gold: "..Global.Player.Gold, White)
		screen:fontPrint(Arial,300,241,"Stone: "..Global.Player.Stone, White)
		
		if Player.Index == 1 then
			screen:fontPrint(Arial,21,218,"Color: "..Global.Player.Color, Yellow)
		elseif Player.Index == 2 then
			screen:fontPrint(Arial,21,241,"Civil.: "..Global.Player.Civ, Yellow)
		elseif Player.Index == 3 then
			screen:fontPrint(Arial,150,218,"Wood: "..Global.Player.Wood, Yellow)
		elseif Player.Index == 4 then
			screen:fontPrint(Arial,150,241,"Food: "..Global.Player.Food, Yellow)
		elseif Player.Index == 5 then
			screen:fontPrint(Arial,300,218,"Gold: "..Global.Player.Gold, Yellow)
		elseif Player.Index == 6 then
			screen:fontPrint(Arial,300,241,"Stone: "..Global.Player.Stone, Yellow)
		end
		
	end
	
	end
	-- End of the mState = "ObjectMenu"
	
	
	
	-- Editing
	
	
	if pad:l() then 
		-- Modifed Speed (Slow/Fast)
		ActualCursorSpeed = "Mod"
		
		if Cursor.Speed == 1 then crsC = 10 end
		if Cursor.Speed == 2 then crsC = 5  end
		if Cursor.Speed == 3 then crsC = 10 end
		if Cursor.Speed == 4 then crsC = 20 end
		if Cursor.Speed == 5 then crsC = 25 end
	else
		-- Normal Speed
		ActualCursorSpeed = "Normal"
		if Cursor.Speed == 1 then crsC = 23 end -- Slow
		if Cursor.Speed == 2 then crsC = 17 end -- Slow
		if Cursor.Speed == 3 then crsC = 15 end -- Normal
		if Cursor.Speed == 4 then crsC = 10 end -- Fast
		if Cursor.Speed == 5 then crsC = 5  end -- Fast
	end
	
	
	if mState == "Edit" then
		--ANALOG-X
		dx = pad:analogX()
		if math.abs(dx) > 32 then
			Cursor.X = Cursor.X + dx / crsC
		end
		--ANALOG-Y
		dy = pad:analogY()
		if math.abs(dy) > 32 then
			Cursor.Y = Cursor.Y + dy / crsC
		end
		--DPAD
		
		if pad:r() then 
			if pressed.r == false then
				pressed.r = true
				if Cursor.Action == "Delete" then
					Cursor.Action = "Spawn"
				elseif Cursor.Action == "Spawn" then
					Cursor.Action = "Delete"
				end
			end
		else
			pressed.r = false
		end
		
		
		------------------ Spawning
		if pad:cross() then
			if pressed.cross == false then
				pressed.cross = true
				
				
				if Cursor.Y >= 29 then
					if MenParX == 0 and MenParY == 0 then
						MakeAction()
					end
				end
				
				
			end
		else
			pressed.cross = false
		end -------------------------
		
		
		
		if pad:square() then
		
			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
			end
			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
			end
			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
			end
			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
			end
			
		end
		
		if pad:circle() then
			
			DrawShadowString(Arial, 240, 43, "Moving Everything Mode", White)
			
			if pad:up() then
				
				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
			end
			if pad:down() then
				
				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
			end
			if pad:left() then
				
				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
			end
			if pad:right() then
				
				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
			end
		
		end
		
		if pad:start() then 
			if pressed.start == false then
				pressed.start = true
				mState = "Menu"
			end
		else
			pressed.start = false
		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
		
	end
	
	---------------------------------------------------------------------------------------------------------------------
	---------------------------------------------------------------------------------------------------------------------
	---------------------------------------------------------------------------------------------------------------------
	
	-- Object Menu (Triangle)
	--if mState == "ObjectMenu" then
		--screen:fontPrint(Arial,10-1,53-1,"Object Menu - Press Triangle to Exit",Black)
		--screen:fontPrint(Arial,10,53 ,   "Object Menu - Press Triangle to Exit",White)
		
		
		
	--end ------------------------------- END OBJECT MENU -----------------------------
	
	DrawShadowString(Arial, 375, 13, Cursor.SpawnObj, White)
	
	if ActualCursorSpeed == "Mod" then
		if Cursor.Speed == 1 or Cursor.Speed == 2 then
			DrawShadowString(Arial, 375, 23, "Slow", White)
		elseif Cursor.Speed == 4 or Cursor.Speed == 5 or Cursor.Speed == 3 then
			DrawShadowString(Arial, 375, 23, "Fast", White)
		end
	elseif ActualCursorSpeed == "Normal" then
		DrawShadowString(Arial, 375, 23, "Normal", White)
	end
	
	--screen:fontPrint(Arial,320-1,42+15-1,"Cursor Action = "..Cursor.Action,Black)
	--screen:fontPrint(Arial,320,42+15,    "Cursor Action = "..Cursor.Action,White)
	
	-- 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
	
	if Cursor.Action == "Spawn" then
		screen:blit(345,1,EditorSpr.ICO.Add)
	elseif Cursor.Action == "Delete" then
		screen:blit(345,1,EditorSpr.ICO.Delete)
	end
	
	if Cursor.SpawnObj == "Villager Ma" or Cursor.SpawnObj == "Villager Fe" then
		DrawShadowString(Arial, 456, 13,  Total.Villager.."/"..Max.Unit.Villager, White)
	elseif Cursor.SpawnObj == "Militia" then
		DrawShadowString(Arial, 456, 13,  Total.Militia.."/"..Max.Unit.Militia, White)
	elseif Cursor.SpawnObj == "House A" or Cursor.SpawnObj == "House B" or Cursor.SpawnObj == "House C" then
		DrawShadowString(Arial, 456, 13,  Total.House.."/"..Max.Building.House, White)
	elseif Cursor.SpawnObj == "Town Center" then
		DrawShadowString(Arial, 456, 13,  Total.TownCenter.."/"..Max.Building.TownCenter, White)
	elseif Cursor.SpawnObj == "Berry A" or Cursor.SpawnObj == "Berry B" or Cursor.SpawnObj == "Berry C" then
		DrawShadowString(Arial, 456, 13,  Total.Berry.."/"..Max.Other.Berry, White)
	elseif Cursor.SpawnObj == "Gold A" or Cursor.SpawnObj == "Gold B" or Cursor.SpawnObj == "Gold C" or Cursor.SpawnObj == "Gold D" or Cursor.SpawnObj == "Gold E" or Cursor.SpawnObj == "Gold F" or Cursor.SpawnObj == "Gold G" then
		DrawShadowString(Arial, 456, 13,  Total.Gold.."/"..Max.Other.Gold, White)
	elseif Cursor.SpawnObj == "Stone A" or Cursor.SpawnObj == "Stone B" or Cursor.SpawnObj == "Stone C" or Cursor.SpawnObj == "Stone D" or Cursor.SpawnObj == "Stone E" or Cursor.SpawnObj == "Stone F" or Cursor.SpawnObj == "Stone G" then
		DrawShadowString(Arial, 456, 13,  Total.Stone.."/"..Max.Other.Stone, White)
	elseif Cursor.SpawnObj == "Oak A" or Cursor.SpawnObj == "Oak B" or Cursor.SpawnObj == "Oak C" or Cursor.SpawnObj == "Oak D" or Cursor.SpawnObj == "Oak E" then
		DrawShadowString(Arial, 456, 13,  Total.Tree.."/"..Max.Other.Tree, White)
	end
	
	-- mState = "ObjectMenu" Controls
	if mState ~= "Menu" then
		InitMenPar("Control", false)
		
		if pad:circle() then
			InitMenPar("Cancel")
		elseif pad:cross() then
			InitMenPar("Accept")
		end
		
		
		
	end
	
	screen:blit(Cursor.X, Cursor.Y, Cursor.IMG_DEF)
	
	-- Editing Menu (Start)
	if mState == "Menu" then
		MenParX = 0
		MenParY = 0
		
		screen:blit(136,7,EditorSpr.Panel.GameMenu)
		
		-- Menu Parts Blit
		
		screen:blit(163,34,EditorSpr.MenuButton.OFF) -- Exit
		screen:fontPrint(Arial,224,51,"Save",ButtonColor.Save)
		
		screen:blit(163,63,EditorSpr.MenuButton.OFF) -- Map Info
		screen:fontPrint(Arial,216,80,"Map Info",ButtonColor.MapInfo)
		
		screen:blit(163,92,EditorSpr.MenuButton.OFF) -- Test
		screen:fontPrint(Arial,224,109,"Test",ButtonColor.Test)
		
		screen:blit(163,121,EditorSpr.MenuButton.OFF) -- Save
		screen:fontPrint(Arial,218,138,"Controls",ButtonColor.Controls)
		
		screen:blit(163,150,EditorSpr.MenuButton.OFF) -- Exit and Save
		screen:fontPrint(Arial,203,167,"Exit and Save",ButtonColor.ExitSave)
		
		screen:blit(163,179,EditorSpr.MenuButton.OFF) -- Exit
		screen:fontPrint(Arial,224,196,"Exit",ButtonColor.Exit)
		
		screen:blit(163,224,EditorSpr.MenuButton.OFF) -- Cancel
		screen:fontPrint(Arial,218,241,"Cancel",ButtonColor.Cancel)
		
		-- 34 63 92 121 150 179 224
		
		--DrawShadowString(Arial, 10, 43, "Circle  | Cancel", White)
		
		--DrawShadowString(Arial, 10, 58, "Start   | Save and Exit", White)
		
		--DrawShadowString(Arial, 10, 73, "Cross   | Exit", White)
		
		-- Moving
		if pad:down() then
			if pressed.down == false then
				pressed.down = true
				CurButton = CurButton + 1
				if CurButton > 7 then CurButton = 1 end
			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 = 7 end
			end
		else
			pressed.up = false
		end
		
		if OneTime1 == true then
			ButtonColor.Cancel = Color.new(255,255,0)
			OneTime1 = false
		end
		
		if pad:up() or pad:down() then
			if CurButton == 1 then
				ButtonColor.Save = Color.new(255,255,0)
				ButtonColor.MapInfo = Color.new(0,0,0)
				ButtonColor.Test = Color.new(0,0,0)
				ButtonColor.Controls = Color.new(0,0,0)
				ButtonColor.ExitSave = Color.new(0,0,0)
				ButtonColor.Exit = Color.new(0,0,0)
				ButtonColor.Cancel = Color.new(0,0,0)
			elseif CurButton == 2 then
				ButtonColor.Save = Color.new(0,0,0)
				ButtonColor.MapInfo = Color.new(255,255,0)
				ButtonColor.Test = Color.new(0,0,0)
				ButtonColor.Controls = Color.new(0,0,0)
				ButtonColor.ExitSave = Color.new(0,0,0)
				ButtonColor.Exit = Color.new(0,0,0)
				ButtonColor.Cancel = Color.new(0,0,0)
			elseif CurButton == 3 then
				ButtonColor.Save = Color.new(0,0,0)
				ButtonColor.MapInfo = Color.new(0,0,0)
				ButtonColor.Test = Color.new(255,255,0)
				ButtonColor.Controls = Color.new(0,0,0)
				ButtonColor.ExitSave = Color.new(0,0,0)
				ButtonColor.Exit = Color.new(0,0,0)
				ButtonColor.Cancel = Color.new(0,0,0)
			elseif CurButton == 4 then
				ButtonColor.Save = Color.new(0,0,0)
				ButtonColor.MapInfo = Color.new(0,0,0)
				ButtonColor.Test = Color.new(0,0,0)
				ButtonColor.Controls = Color.new(255,255,0)
				ButtonColor.ExitSave = Color.new(0,0,0)
				ButtonColor.Exit = Color.new(0,0,0)
				ButtonColor.Cancel = Color.new(0,0,0)
			elseif CurButton == 5 then
				ButtonColor.Save = Color.new(0,0,0)
				ButtonColor.MapInfo = Color.new(0,0,0)
				ButtonColor.Test = Color.new(0,0,0)
				ButtonColor.Controls = Color.new(0,0,0)
				ButtonColor.ExitSave = Color.new(255,255,0)
				ButtonColor.Exit = Color.new(0,0,0)
				ButtonColor.Cancel = Color.new(0,0,0)
			elseif CurButton == 6 then
				ButtonColor.Save = Color.new(0,0,0)
				ButtonColor.MapInfo = Color.new(0,0,0)
				ButtonColor.Test = Color.new(0,0,0)
				ButtonColor.Controls = Color.new(0,0,0)
				ButtonColor.ExitSave = Color.new(0,0,0)
				ButtonColor.Exit = Color.new(255,255,0)
				ButtonColor.Cancel = Color.new(0,0,0)
			elseif CurButton == 7 then
				ButtonColor.Save = Color.new(0,0,0)
				ButtonColor.MapInfo = Color.new(0,0,0)
				ButtonColor.Test = Color.new(0,0,0)
				ButtonColor.Controls = Color.new(0,0,0)
				ButtonColor.ExitSave = Color.new(0,0,0)
				ButtonColor.Exit = Color.new(0,0,0)
				ButtonColor.Cancel = Color.new(255,255,0)
			end
		end
		
		if pad:cross() then
			if CurButton == 1 then
				SaveGame("SCN", "My Scenario", "", "", "Normal", "None", MV, Update, "[F]", "Scenario Save", Level, false)
				OneTime2 = true -- for a sign "Scenario Saved!"
			end -- Save
			
			if CurButton == 2 then mState = "MapInfo" end -- MapInfo
			if CurButton == 3 then
				SaveGame("SCN", "My Scenario", "", "", "Normal", "None", MV, Update, "[F]", "Scenario Save", Level, false)
				ClearRAM("Editor")
				Textures("Delete")
				SavePath(SCNNumber)
				dofile("GameScript.lua")
			end -- Test
			
			if CurButton == 4 then mState = "Controls" end -- MapInfo
			
			if CurButton == 5 then
				SaveGame("SCN", "My Scenario", "", "", "Normal", "None", MV, Update, "[F]", "Scenario Save", Level, false)
				ClearRAM("Editor")
				dofile("EditorMenu.lua")
			end -- Exit and Save
			
			if CurButton == 6 then
				ClearRAM("Editor")
				dofile("EditorMenu.lua")
			end -- Exit
			if CurButton == 7 then mState = "Edit" end if pad:start() then mState = "Edit" end -- Resume Game
		end
		
		if pad:cross() then
		
			if CurButton == 1 then
				screen:blit(163,34,EditorSpr.MenuButton.ON) -- Save
			elseif CurButton == 2 then
				screen:blit(163,63,EditorSpr.MenuButton.ON) -- Map Info
			elseif CurButton == 3 then
				screen:blit(163,92,EditorSpr.MenuButton.ON) -- Test
			elseif CurButton == 4 then
				screen:blit(163,121,EditorSpr.MenuButton.ON) -- Controls
			elseif CurButton == 5 then
				screen:blit(163,150,EditorSpr.MenuButton.ON) -- Exit and Save
			elseif CurButton == 6 then
				screen:blit(163,179,EditorSpr.MenuButton.ON) -- Exit
			elseif CurButton == 7 then
				screen:blit(163,224,EditorSpr.MenuButton.ON) -- Cancel
			end
			-- 34 63 92 121 150 179 224
		
		end
		
	end
	
	if mState == "MapInfo" then
		
		screen:blit(51, 22, EditorSpr.Panel.MiniPanel)
		
		if pad:circle() then mState = "Menu" end
		
	end
	
	if mState == "Controls" then
		
		screen:blit(51, 22, EditorSpr.Panel.MiniPanel)
		screen:blit(51, 22, EditorSpr.Panel.Controls)
		
		
		if pad:circle() then mState = "Menu" end
		
	end
	
	
	
	if pad:select() then
		Screenshot()
	end
	
	screen.waitVblankStart(1)
	screen.flip()
end
