--LevelOS.maximize()
gen = require("gen")
dict = require("dictionary")
local snow = #dict
dict[snow].color = colors.pink
assets = require("assets")
os.sleep(0.1)
--[[term.setPaletteColor(colors.magenta,118/255,92/255,72/255)
term.setPaletteColor(colors.purple,71/255,103/255,28/255)
term.setPaletteColor(colors.pink,238/255,195/255,154/255)
term.setPaletteColor(colors.orange,0.45,0.45,0.45)]]
local w,h = term.getSize()
local oterm = term.current()
local bwin = window.create(term.current(),1,1,w,h,false)
local useBuffer = true
local map
local rmap = {}
local objects
local ogrid
local debugobjects = false
local zoom = 1
local player
local outfits = {colors.brown,colors.lightGray,colors.black}
local function generate()
	map = gen(300,300)
	objects = {}
	ogrid = {}
	for y=1,#map[1] do
		for x=1,#map do
			if not dict[map[x][y]] then
				if map[x][y] > snow then
					dict[map[x][y]] = {name=dict[snow].name,color=dict[snow].color,color2=colors.lightGray,height=dict[snow].height+(map[x][y]-snow)}
				else
					dict[map[x][y]] = dict[1]
				end
			end
			local s = dict[map[x][y]]
		end
	end
end
local function render(rx,ry)
	rmap = {}
	if useBuffer then
		term.redirect(bwin)
	end
	term.setBackgroundColor(colors.blue)
	term.clear()
	local w,h = term.getSize()
	rx = 0-rx
	ry = 0-ry
	local drawn = 0
	local had = {}
	local orows = {} -- object rows (one for every line on screen)
	if zoom == 1 then
		for k,v in pairs(objects) do
			local y = v.y
			local x = math.floor(v.x + 0.5)
			local s = dict[map[x][y]]
			local ox,oy = (rx-1)+x,(ry-1)+y-s.height
			if ox >= 1 and oy >= 1-s.height and ox <= w and oy <= h then
				v.rx = ox
				v.ry = oy
				if not orows[y] then
					orows[y] = {}
				end
				table.insert(orows[y],v)
			end
		end
	end
	for y1=zoom,#map[1] do
		local y = math.floor(y1 + 0.5)
		lOS.debugY = {y,y1,zoom}
		for x1=zoom,#map do
			local x = math.floor(x1 + 0.5)
			lOS.debugX = {x,x1,zoom}
			local s = dict[map[x][y]]
			local ox,oy = (rx-1)+x,(ry-1)+y-s.height
			if zoom > 1 then
				oy = oy+s.height
			end
			ox,oy = math.floor(ox/zoom+0.5),math.floor(oy/zoom+0.5)
			if ox >= 1 and oy >= 1-s.height and ox <= w and oy <= h and not (zoom > 1 and had[ox..","..oy]) then
				if zoom == 1 and map[x][y+1] and dict[map[x][y+1]] and dict[map[x][y+1]].height < s.height then
					for t=1,s.height do
						local tx,ty = math.floor(((rx-1)+x)/zoom+0.5),math.floor(((ry-1)+y-(t-1))/zoom+0.5)
						term.setCursorPos(tx,ty)
						term.setBackgroundColor(s.color2 or s.color)
						term.write(" ")
					end
				end
				term.setCursorPos(ox,oy)
				term.setBackgroundColor(s.color)
				term.write(" ")
				drawn = drawn+1
				had[ox..","..oy] = true
			end
		end
	end
	if useBuffer then
		term.redirect(oterm)
		for t=1,h do
			term.setCursorPos(1,t)
			term.blit(bwin.getLine(t))
		end
	end
end
generate()
local w,h = term.getSize()
local x,y = 150-(math.floor(w/2)),150-(math.floor(h/2))
render(x,y)
local hold
local tID = os.startTimer(1)
if not stupid then return end
while true do
	local e = {os.pullEvent()}
	w,h = term.getSize()
	--[[if e[1] == "timer" and e[2] == tID then
		x = x+1
		render(x,y)
		tID = os.startTimer(0.05)
	else]]if e[1] == "term_resize" then
		bwin.reposition(1,1,term.getSize())
		local w,h = term.getSize()
		x,y = 150-(math.floor(w/2)),150-(math.floor(h/2))
		render(x,y)
	end
end
