print "hello from lua"

require("seppo")

gotomode = nil
gamemode = 4
musicvol = 64
sfxvol = 64
transitionLen = 54
transition = -transitionLen

dofile("camelback.lua")
game_camelback_init = init
game_camelback_frame = frame
game_camelback_board = boardname

dofile("skiland.lua")
game_init = init
game_frame = frame
game_board = boardname

dofile("megaslope.lua")
game_megaslope_init = init
game_megaslope_frame = frame
game_megaslope_board = boardname


dofile("markusland.lua")
game_markusland_init = init
game_markusland_frame = frame
game_markusland_board = boardname

dofile("menu.lua")
menu_init = init
menu_frame = frame
menu_board = boardname

dofile("intro.lua")
intro_init = init
intro_frame = frame
intro_board = boardname

dofile("admire.lua")
admire_init = init
admire_frame = frame
admire_board = boardname

dofile("members.lua")
members_init = init
members_frame = frame
members_board = boardname

dofile("shareware.lua")
shareware_init = init
shareware_frame = frame
shareware_board = boardname

--print("boards: "..menu_board.." and "..game_board)

quit(false)


function init(newinitpos, hotload)
	print(string.format("top init called! %f,%f,%f", newinitpos.x, newinitpos.y, newinitpos.z ) )
	print("gamemode init "..gamemode)

	if gamemode == 0 or gamemode == 1 then
		menu_init(newinitpos, hotload)
		gamemode = 1
	elseif gamemode == 2 or gamemode == 3 then
		game_init(newinitpos, hotload)
		gamemode = 3
	elseif gamemode == 4 or gamemode == 5 then
		intro_init(newinitpos, hotload)
		gamemode = 5
	elseif gamemode == 6 or gamemode == 7 then
		game_megaslope_init(newinitpos, hotload)
		gamemode = 7
	elseif gamemode == 8 or gamemode == 9 then
		game_camelback_init(newinitpos, hotload)
		gamemode = 9
	elseif gamemode == 10 or gamemode == 11 then
		game_markusland_init(newinitpos, hotload)
		gamemode = 11
	elseif gamemode == 12 or gamemode == 13 then
		admire_init(newinitpos, hotload)
		gamemode = 13
	elseif gamemode == 14 or gamemode == 15 then
		members_init(newinitpos, hotload)
		gamemode = 15
	elseif gamemode == 16 or gamemode == 17 then
		shareware_init(newinitpos, hotload)
		gamemode = 17
	end
end

function frame()
	--print("frame called!")

	if gamemode == 1 then
		menu_frame()
	elseif gamemode == 3 then
		game_frame()
	elseif gamemode == 5 then
		intro_frame()
	elseif gamemode == 7 then
		game_megaslope_frame()
	elseif gamemode == 9 then
		game_camelback_frame()
	elseif gamemode == 11 then
		game_markusland_frame()
	elseif gamemode == 13 then
		admire_frame()
	elseif gamemode == 15 then
		members_frame()
	elseif gamemode == 17 then
		shareware_frame()
	end

	if (gotomode == nil) then
	else
		if (transition <= -transitionLen) then
			transition = transitionLen
		end
	end

	if (transition == 0) then
		if gotomode == 0 then
			print("lol going to menu")
			gamemode = 0
			boardname = menu_board
			gotomode = nil
			reloadboard()
		elseif (gotomode == 1) then
			print("lol going to game")
			gamemode = 2
			boardname = game_board
			gotomode = nil
			reloadboard()
		elseif (gotomode == 2) then
			print("lol going to intro")
			gamemode = 4
			boardname = intro_boared
			gotomode = nil
			reloadboard()
		elseif (gotomode == 3) then
			print("lol going to game megaslope")
			gamemode = 6
			boardname = game_megaslope_board
			gotomode = nil
			reloadboard()
		elseif (gotomode == 4) then
			print("lol going to game camelback")
			gamemode = 8
			boardname = game_camelback_board
			gotomode = nil
			reloadboard()
		elseif (gotomode == 5) then
			print("lol going to game markusland")
			gamemode = 10
			boardname = game_markusland_board
			gotomode = nil
			reloadboard()
		elseif (gotomode == 6) then
			print("lol going to admire")
			gamemode = 12
			boardname = admire_board
			gotomode = nil
			reloadboard()
		elseif (gotomode == 7) then
			print("lol going to members")
			gamemode = 14
			boardname = members_board
			gotomode = nil
			reloadboard()
		elseif (gotomode == 8) then
			print("lol going to shareware")
			gamemode = 16
			boardname = members_board
			gotomode = nil
			reloadboard()
		elseif (gotomode == 99) then
			gamemode = 99
			print("lol going to quit")
			quit()
		end
	end

	if transition > -transitionLen then
		numx = 16
		numy = 8
		size = 32
		for y=1, numy do
			for x=1, numx do
				id = -46 + 2 * transition + x + (numx * y) // 2
				z = (transitionLen - math.abs(id)) / transitionLen

				if z < 0.0 then
					z = 0.0
				elseif z > 1.0 then
					z = 1.0
				end
				z = z * 1.5
				rotatesprite(160 - (size // 2) - (numx // 2) * size + x *  size, 100 - (size // 2) - (numy // 2) * size + y * size, z, 0,   091,   2,0, 2,    0,0,1,1);
			end
		end


		transition = transition - 1
	end

	if transition > 0 then
		setvolume(0, (musicvol * transition) // transitionLen)
		--setvolume(1, (sfxvol   * transition) // transitionLen)
	else
		setvolume(0, musicvol)
		setvolume(1, sfxvol)
	end


end