g3d = require "g3d"
require "suppfunc"

function love.load()
	-- Personal seed generation
	personalseed = love.data.encode("string", "hex", love.data.hash("sha256", os.getenv("PATH")))
	notsorandomseed = tonumber(personalseed, 16)
	math.randomseed(notsorandomseed)
	
	-- GRAPHICS
		-- GOOD PIXEL
	love.graphics.setDefaultFilter("nearest")

		-- FONTS
	DefaultFont = love.graphics.newImageFont("default_font.png",
   " abcdefghijklmnopqrstuvwxyz" ..
   "ABCDEFGHIJKLMNOPQRSTUVWXYZ0" ..
	 "123456789.,!?-+/():;%&`'*#=[]\"")
	love.graphics.setFont(DefaultFont)

	obj_list = {}

	--	obj_flower_pot = g3d.newModel("objects/flower_pot.obj","objects/flower_pot/texture.png",{0,0,0},{0,0,0},{1,-1,1}),
	--	
	generate_museum(0,0,0)
end

function love.mousemoved(x,y, dx,dy)
	if not pause then
		g3d.camera.firstPersonLook(dx,dy)
	end
end

function love.update(dt)
	if not pause then
		g3d.camera.firstPersonMovement(dt,5)
	end
end

function love.keypressed(key)
	if key == "z" then 
		obj_list = {}
		notsorandomseed = os.time()
		math.randomseed(notsorandomseed )
		generate_museum(0,0,0)
	end
	if key == "escape" then 
		if pause then
			pause = false
			love.mouse.setRelativeMode(true)
		else
			pause = true
			love.mouse.setRelativeMode(false)
		end
	end
end
 
function love.draw()
	-- draw everything
	for _, obj in pairs(obj_list) do
		obj:draw()
	end

	-- print "Press [z] to generate a new museum"
	love.graphics.print("Press [z] to generate a new museum", 20, 20)

	-- print coords
	love.graphics.print("x: "..math.floor(g3d.camera.position[1])..", y: "..math.floor(g3d.camera.position[2])..", z: "..math.floor(g3d.camera.position[3]), 20, 40)

	-- print seed
	if notsorandomseed == tonumber(personalseed, 16) then
		love.graphics.setColor(hexrgb("#FFD700"))
		love.graphics.print("Seed: "..personalseed, 20, 60)
		love.graphics.setColor(hexrgb("#ffffff"))
	else
		love.graphics.print("Seed: "..notsorandomseed, 20, 60)
	end


	if pause then
		love.graphics.rectangle("fill", 20, 20, love.graphics.getWidth()-40, love.graphics.getHeight()-40)
	end
end

function generate_museum(x,y,z)
	local width = math.random(10,20)
	local height = 4
	local depth = math.random(10,20)

	-- make the ground
	for w = 0, width, 1 do for d = 0, depth, 1 do
				table.insert(obj_list,g3d.newModel("objects/ground.obj","objects/ground/texture.png",{x+w,y,z+d},{0,0,0},{1,-1,1}))	
	end	end

	-- make the walls
	for w = 0, width, 1 do for h = 0, height, 1 do
		table.insert(obj_list,g3d.newModel("objects/wall.obj","objects/wall/texture.png",{x+w,y-h,z},{0,math.pi/2,0},{1,-1,1}))	
		table.insert(obj_list,g3d.newModel("objects/wall.obj","objects/wall/texture.png",{x+w,y-h,z+depth},{0,math.pi/2,0},{1,-1,1}))	
	end	end

	for d = 0, depth, 1 do for h = 0, height, 1 do
		table.insert(obj_list,g3d.newModel("objects/wall.obj","objects/wall/texture.png",{x,y-h,z+d},{0,0,0},{1,-1,1}))	
		table.insert(obj_list,g3d.newModel("objects/wall.obj","objects/wall/texture.png",{x+width,y-h,z+d},{0,0,0},{1,-1,1}))	
	end	end

	-- decorate with paintings
	local art_height = 2.5

	for w = 2, width-2, 4 do
		random_art(x+w,y-art_height,z,0,math.pi/2,0)
		random_art(x+w,y-art_height,z+depth,0,-math.pi/2,0)
	end
	for d = 2, depth-2, 4 do
		random_art(x,y-art_height,z+d,0,-math.pi,0)
		random_art(x+width,y-art_height,z+d,0,0,0)
	end


	-- place carpet in the middle of the room
		table.insert(obj_list,g3d.newModel("objects/carpet.obj","objects/carpet/texture.png",{x+width/2, y, z+depth/2},{0,math.random(),0},{1,-1,1}))	

	-- place camera in the middle of the room
	g3d.camera.position = {x+width/2, y-2, z+depth/2}
end


function random_art(x,y,z,rotx,roty,rotz)
	local art = math.random(0,9)
	if art == 0 then
		-- no art :S
	elseif art == 1 then
		table.insert(obj_list,g3d.newModel("objects/art_9_12.obj","objects/art_9_12/eri_sueños.png",{x,y-(12/16),z},{rotx,roty,rotz},{-1,1,1}))	
	elseif art == 2 then	
		table.insert(obj_list,g3d.newModel("objects/art_1_1.obj","objects/art_1_1/yari_walk.png",{x,y-(10/16),z},{rotx,roty,rotz},{-1,1,1}))	
	elseif art == 3 then
		table.insert(obj_list,g3d.newModel("objects/art_16_9.obj","objects/art_16_9/clareta_tira.png",{x,y-(9/16),z},{rotx,roty,rotz},{-1,1,1}))	
	elseif art == 4 then
		table.insert(obj_list,g3d.newModel("objects/art_16_12.obj","objects/art_16_12/clareta_garden_throne.png",{x,y-(12/16),z},{rotx,roty,rotz},{-1,1,1}))	
	elseif art == 5 then
		table.insert(obj_list,g3d.newModel("objects/art_24_16.obj","objects/art_24_16/eri_dungeon.png",{x,y-(16/16),z},{rotx,roty,rotz},{-1,1,1}))	
	elseif art == 6 then
		table.insert(obj_list,g3d.newModel("objects/art_12_9.obj","objects/art_12_9/noe_nerielle.png",{x,y-(9/16),z},{rotx,roty,rotz},{-1,1,1}))	
	elseif art == 7 then
		table.insert(obj_list,g3d.newModel("objects/art_1_1.obj","objects/art_1_1/yari_pumpum.png",{x,y-(10/16),z},{rotx,roty,rotz},{-1,1,1}))	
	elseif art == 8 then
		table.insert(obj_list,g3d.newModel("objects/art_12_6.obj","objects/art_12_6/eri_sigils.png",{x,y-(6/16),z},{rotx,roty,rotz},{-1,1,1}))	
	elseif art == 9 then
		table.insert(obj_list,g3d.newModel("objects/art_16_24.obj","objects/art_16_24/eri_ariel_with_overalls.png",{x,y-(24/16),z},{rotx,roty,rotz},{-1,1,1}))	
	end
end
