ttt = texture.load "mapobjects/tannentex.tga"
ttt:clamp(true)
--assert(ttt:clamp()==true)
--print(ttt:clamp())


function luxinia.dataID(...) return ... end

print("Project Xmas!");

xmasfont = fontset.new16x16()
for i=0,127 do
	xmasfont:lookup(i+32,i+128)
end

vistest.maxdepthscene(7)


function reload2 ()
	_print("deleting actor")
--	actornode.deleteall()
	_print("deleting scenenode")
--	scenenode.deleteall()
	_print("deleting l3d")
--	l3dnode.deleteall()
	_print("deleting l2d")
--	l2dnode.deleteall()
	_print("deleting dworld")
	--dworld.deleteall()
	_print("deleting finished")
	dofile("main.lua")
	_print("reloading finished")
end

local edit = false

do
	local _mdllo = model.load
	--function model.load (a,b,c,d)
	--	return _mdllo(a,b,true)
	--end
end

do
	local _l3dmodnew = l3dmodel.new
	function l3dmodel.new(...)
		local ret = _l3dmodnew(...)
		ret:rfFog(true)
		return ret
	end
end


assert(l3dlist.l3dlayercount() >= 8)


lset = l3dset.get(0)
layer = lset:layer(0)

do	--- setup view
	
	normalview = lset:getdefaultview()
	topview = l3dview.new()
	topview:activate(lset,normalview,true)
	topview:filldrawlayers(false)
	
	-- use layers 0-3 for normalview
	-- and 4-7 for topview
	
	topviewoffset = 4
	
	local ignore = {}
	for i=1,l3dlist.l3dlayercount() do
		ignore[i] = i > topviewoffset
	end
	
	UtilFunctions.simplerenderqueue(normalview,nil,ignore)
	ignore = {true,true,true,true}
	UtilFunctions.simplerenderqueue(topview,nil,ignore)


	local r,g,b = 0.6,0.6,0.6
	local amb = 0.33
	normalview.rClear:colorvalue(r,g,b,1)
	normalview:fogcolor(r,g,b,0.1)
	normalview:fogstart(128)
	normalview:fogend(1600)
	normalview:fogstate(true)

	topview.rClear:stencil(false)
	topview.rClear:depth(false)
	topview.rClear:color(false)
	topview:fogcolor(r,g,b,0.1)
	topview:fogstart(1280)
	topview:fogend(2048)
	topview:fogstate(true)

	l3dcamera.default():backplane(2048)
	l3dcamera.default():frontplane(256)
	

	sunl3d = l3dlight.new("sun",layer)
	sunl3d:makesun()
	sun = actornode.new("")
	sun:pos(-3000000,-1700000,1000000)
	sunl3d:diffuse(r,g,b,0)
	sunl3d:ambient(amb,amb,amb,0)
	sunl3d:linkinterface(sun)
end

function bgclear() normalview.rClear:color(true) end
function nosun() sunl3d:diffuse(0,0,0,0) sunl3d:ambient(0,0,0,0) end

function viewdist(near,far)
	near,far = near or 256, far or 2048
	l3dcamera.default():backplane(far)
	l3dcamera.default():frontplane(near)
	topview:fogstart(5*near)
	topview:fogend(far)
	normalview:fogstart(near*2)
	normalview:fogend(far-512)
end

dworld.autodisable(true)
dworld.autodisablesteps(0)
dworld.autodisablevel(0.04)
dworld.autodisableang(0.01)
dworld.autodisabletime(100)
dworld.iterations(8)
dworld.gravity(0,0,-0.01)

dworld.surfacecombination(0,1,1)
dworld.surfacemu(1,0)
dworld.surfacemu2(1,10000000)
dworld.surfacebitmu2(1,true)

dworld.surfacecombination(0,2,2)
dworld.surfacemu(2,0.1)

dworld.surfacecombination(0,3,3)
dworld.surfacemu(3,0)

dworld.surfacecombination(0,5,5)
dworld.surfacemu(5,10000)

do
	-- FIXME particle bbox is pretty huge
	-- override with smaller and attach to camera...
	snow = {}
	snow.psys = particlesys.load("snowfall.prt")
	snow.psys2 = particlesys.load("snowfallfar.prt")
	snow.s3d = actornode.new("snow",true,250,250,299)
	snow.s3d:rotdeg(-90,0,0)
	snow.s3d2 = actornode.new("snowfar",true,250,250,380)
	snow.s3d2:rotdeg(-90,0,0)
	
	snow.l3d = l3dpemitter.new("snow",lset:layer(topviewoffset),snow.psys)
	snow.l3d:linkinterface(snow.s3d)
--	snow.l3d:start(0)
	snow.l3d2 = l3dpemitter.new("snow",lset:layer(topviewoffset),snow.psys2)
	snow.l3d2:linkinterface(snow.s3d2)
--	snow.l3d2:start(0)

	snow.psys:usevolume(true)
	snow.psys:volumesize(250,250,250)
	snow.psys:volumedistance(350)

	snow.psys2:usevolume(true)
	snow.psys2:volumesize(700,700,900)
	snow.psys2:volumedistance(500)
end

function snow.stop ()
	--_print("stop")
	snow.l3d:stop(0,true)
	snow.l3d2:stop(0,true)
end

function snow.start ()
	--_print("start")
	snow.l3d:start(0)
	snow.l3d2:start(0)
end

dofile("scripts/gui.lua")
loading = assert(loadfile("scripts/menu.lua"))
loader = coroutine.create(
	function ()
		loading()
		--xpcall(loading,function (err) print (err) print(debug.traceback()) end)
	end)

input.escquit(false)

Keyboard.setKeyBinding (Keyboard.keycode.ESC,function ()
	if (xgame) then
		if (xgame.dietime) then
			xgame.dietime = 0
		else
			finishgame = true
		end
	elseif (menupage and menupage.activepage ~= menu and runs) then
		menu:activate()
	elseif (vehiclelevel) then
		cartestend()
	else
		system.exit()
	end
end)

do
	local start = luxinia.time()
	function xmasloader ()
		local ret = {coroutine.resume(loader)}
		if (not ret[1]) then
			_print ("----->",unpack(ret))
			_print(debug.traceback (loader))
		end
		runs = coroutine.status(loader)~="dead"
		xmasloader = coroutine.status(loader)~="dead" and xmasloader or nil
		if (xmasloader) then
			Timer.set("xmasloader",xmasloader)
		else
			Timer.remove("xmasloader")
		end
		if (not runs) then
			_print(string.format("Loading needed %.2f s",(luxinia.time()-start)/1000))
		end
	end

	Timer.set("xmasloader",xmasloader)
end



