load_constants("properties\\bonuses.xml")

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

function init(app, widget_agent, lua)

	g_Globals = {}
	g_Globals.value_chaged_timer = 0

	g_WidgetAgent:RemoveWidget("loading")

	g_Lua:LoadScript("scripts/mainmenu/mainmenu.lua")
	g_Lua:LoadScript("scripts/mainmenu/effects.lua")

	g_Lua:LoadScript("scripts/game/gameplay.lua")
	g_Lua:LoadScript("scripts/game/game.lua")
	g_Lua:LoadScript("scripts/game/gui.lua")
	g_Lua:LoadScript("scripts/game/progress.lua")
	g_Lua:LoadScript("scripts/game/particle.lua")
	g_Lua:LoadScript("scripts/game/events.lua")
	g_Lua:LoadScript("scripts/game/finaleffect.lua")

	g_Lua:LoadScript("scripts/other.lua")
	g_Lua:LoadScript("scripts/music.lua")
	g_Lua:LoadScript("scripts/dialogs.lua")

	g_Loader:LoadFile("properties/templates.xml")
	g_Loader:LoadFile("properties/game.xml")
	g_Loader:LoadFile("properties/mainmenu.xml")
	g_Loader:LoadFile("properties/gui.xml")
	g_Loader:LoadFile("properties/dialogs.xml")

	start_mainmenu_state()
end

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

function shutdown()
	return
--[[	g_Lua = nil
	g_WidgetAgent = nil
	g_App = nil
	g_Loader = nil
	g_State = nil
	g_Game = nil
	collectgarbage(0)]]
end

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

function lua_info()
--[[	local mem, thr = gcinfo()
	log("-----------------------------------")
	log("Lua info:")
	log("   Memory used: "..mem)
	log("   Threshold : "..thr)
	log("-----------------------------------")]]
end

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

function lua_load(file, widget_agent)
	log("lua load")
	g_WidgetAgent = widget_agent

	local infile, err = io.open(file, "rb")
	if infile == nil then
		error("While opening: " .. (err or "no error"))
	end

	local buf, err = infile:read("*a")
	if buf == nil then
		error("While reading: " .. (err or "no error"))
	end

	infile:close() 

	local loaded = pluto.unpersist({}, buf)
	g_GameVars = {}
	g_Globals = {}
	g_GameVars = loaded[1]
	g_Globals = loaded[2]

	local cursor_pointer = 0
	SetCursor(cursor_pointer, false)
	
	after_load_lua_change_game_music()
end

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

function lua_save(file)
	log("lua save")
	outfile = io.open(file, "wb")
	local buff = pluto.persist({}, {g_GameVars, g_Globals} )
	outfile:write(buff)
	outfile:close() 
end

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

function parse_vector_from_string(str)
	local vec = {}

	local number = ""
	local i = 1
	local len = string.len(str)
	while i<=len do
		if string.byte(str, i)>=48 and string.byte(str, i)<=57 then
			number = number..string.char(string.byte(str, i))
		else
			if number~="" then
				vec[table.getn(vec) + 1] = tonumber(number)
			end
			number = ""
		end
		i = i + 1
	end
	if number~="" then
		vec[table.getn(vec) + 1] = tonumber(number)
	end

	return vec
end

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

function start_game_state()
	g_State = CGame(g_Loader:GetTemplate("gamestate"), g_Lua)
	g_App:ChangeState(g_State)
	g_MainMenu = nil
end

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

function start_mainmenu_state()
	g_State = CMainMenu(g_Loader:GetTemplate("mainmenu_state"), g_Lua)
	g_App:ChangeState(g_State)
	g_Game = nil
end

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

function show_help(parent)
	if g_Game then g_Game:Hide(true) end
	pause_game(true)
	local help  = CLuaWindow(g_Loader:GetTemplate("help_window"), g_Lua)
	parent:AddWidget(help)
end

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


function quit_game()
	g_App:Shutdown()
end

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

function log(message)
	print(message)
	io.flush()
end