local frac_x = math.floor(x/48)*48-x
local frac_y = math.floor(y/48)*48-y
local floor_x = math.floor(x/48)
local floor_y = math.floor(y/48)
local old_x = floor_x+2
local old_y = floor_y+2
pressed = false
local sel_x = 0
local sel_y = 0
local sel = ""
local sel_rot = 0
local leg_ang = 0
local hand_ang = -1
local head_ang = -1
local side = true
moved = true
fall_speed = 4
jump_speed = 8
local place_x = 0
local place_y = 0
local min = game_data["min"]*48+960
local max = game_data["max"]*48-960
local block_health = false
local t_block_health = false
local block_x=0
local block_y=0
local shake=0
local a_x=0
local a_y=0
local l_x=0
local l_y=0
local last_x=x+48
local last_y=y+48

local inv={ "g","d","S","R","u","v","w","l","L",
			"b","1","2","3","4","5","W","V"," ",
			"s","c","i","a","e","A","r","G","D",
			"t"," ","J","z","E","H","p","B","P",
			"C","U","k","f"," "," "," "," "," "}
local inv_x=0
local inv_y=0

local clouds={}
for t=1,8 do
	table.insert(clouds,{math.random(480),math.random(10,80),math.random(38,82),math.random(20,62),pge.math.randfloat(0.2,0.6)})
end

local d_fps=0
local d_time=os.date("*t")["sec"]
local d_counter=0
local d_last=""
debug=0
keep=true
drop_block={}

local t_48=0
local last_time=d_time+10
local last_sec=time+30
	
while pge.running() do
	pge.controls.update()
	
	if pge.controls.pressed(PGE_CTRL_DOWN + PGE_CTRL_START) then
		pge.utils.oskinit("Type Lua command here...",d_last)
		local state
		local string
		while pge.running() do
			pge.gfx.startdrawing()
			pge.texture.activate(pix["bg"])	
			pge.texture.draweasy(pix["bg"],0,0)
			pge.gfx.enddrawing()
			state,string = pge.utils.oskupdate()
			pge.gfx.swapbuffers()
			if state then
				if string~="" then
					d_last=string
					loadstring(string)()
				end
				break
			end
		end
	end
	
	if m_inv() then
		while pge.running() do
			pge.controls.update()
			if pge.controls.pressed(PGE_CTRL_UP) and inv_y>0 then
				inv_y=inv_y-1
			end
			if pge.controls.pressed(PGE_CTRL_DOWN) and inv_y<4 then
				inv_y=inv_y+1
			end
			if pge.controls.pressed(PGE_CTRL_LEFT) and inv_x>0 then
				inv_x=inv_x-1
			end
			if pge.controls.pressed(PGE_CTRL_RIGHT) and inv_x<8 then
				inv_x=inv_x+1
			end
			if pge.controls.pressed(PGE_CTRL_CROSS) then
				if inv[inv_x+inv_y*9+1]~=" " then
					table.insert(blocks,1,inv[inv_x+inv_y*9+1])
					blocks[10]=nil
				end
			end
			if pge.controls.pressed(PGE_CTRL_CIRCLE) or pge.controls.pressed(PGE_CTRL_SELECT) then
				break
			end
			pge.gfx.startdrawing()

			pge.texture.activate(pix["inv_slot"])
			for t=1,9 do
				pge.texture.draweasy(pix["inv_slot"],64+t*32,240)
			end
			for t=0,4 do
				for t2=1,9 do
					pge.texture.draweasy(pix["inv_slot"],64+t2*32,64+t*32)
				end
			end
			pge.texture.activate(pix["inv_sel"])
			pge.texture.draweasy(pix["inv_sel"],96+inv_x*32,64+inv_y*32)

			for t=1,9 do
				pge.texture.activate(pix[blocks[t]])
				pge.texture.draw(pix[blocks[t]],68+t*32,244,24,24)
			end
			for t=1,9 do
				for t2=0,4 do
					if inv[t+t2*9]~=" " then
						pge.texture.activate(pix[inv[t+t2*9]])
						pge.texture.draw(pix[inv[t+t2*9]],68+t*32,68+t2*32,24,24)
					end
				end
			end

			pge.gfx.enddrawing()
			pge.gfx.swapbuffers()
		end
	end
	
	if m_pause() then
		local temp2=menu_t
		menu_t=1
		local temp=pge.font.measure(font_1,"Save And Exit To Menu")
		local sel=b(4)
		while pge.running() do
			pge.controls.update()

			if pge.controls.pressed(PGE_CTRL_DOWN) and menu_t<4 then
				menu_t = menu_t + 1
				sel=b(4)
			end
			if pge.controls.pressed(PGE_CTRL_UP) and menu_t>1 then
				menu_t = menu_t - 1
				sel=b(4)
			end
			sel[menu_t]=pge.gfx.createcolor(255,255,0,math.abs(math.sin(name_rot)*255))

			if pge.controls.pressed(PGE_CTRL_CIRCLE) or pge.controls.pressed(PGE_CTRL_START) then
				break
			end
			
			if pge.controls.pressed(PGE_CTRL_CROSS) then
				if menu_t==1 then
					break
				elseif menu_t==2 then
					funq.swim=nil
					local file=""
					local temp={"game_data","height","blocks","funq","funq_05","funq_10","flags","water_flow"}
					for t=1,#temp do
						file = file..temp[t].."={"
						for i,v in pairs(loadstring("return "..temp[t])()) do
							if type(i)=="number" then
								file=file.."\["..i.."]\="
							else
								file=file..i.."="
							end
							if type(v)=="string" then
								file=file.."'"..v.."',"
							elseif type(v)=="number" then
								file=file..v..","
							elseif type(v)=="function" then
								file=file..i..","
							elseif type(v)=="table" then
								local temp=""
								for i2,v2 in pairs(v) do
									if type(i2)=="number" then
										temp=temp.."\["..i2.."]\="
									else
										temp=temp..i2.."="
									end
									if type(v2)=="number" then
										temp=temp..v2..","
									else
										temp=temp.."'"..v.."',"
									end
								end
								temp=string.sub(temp,1,-2)
								file=file.."{"..temp.."},"
							end
						end
						if char(file,string.len(file))=="," then
							file = string.sub(file,1,string.len(file)-1)
						end
						file = file.."}\n"
					end
					temp={"x","y","time"}
					for t=1,#temp do
						file=file..temp[t].."="..loadstring("return "..temp[t])().."\n"
					end
					file=file.."dofile('game.lua')"
					if not pge.file.exists("saves/"..name) then
						local temp = pge.file.open("saves/"..name,PGE_FILE_WRONLY+PGE_FILE_CREATE)
						pge.file.close(temp)
					end
					local temp = pge.file.open("saves/"..name,PGE_FILE_WRONLY+PGE_FILE_TRUNC)
					pge.file.write(temp,file)
					pge.file.close(temp)
					game_data=nil
					height=nil
					collectgarbage("collect")
					dofile("menu.lua")
				elseif menu_t==3 then
					if not music then
						music={}
						music_d={}
						music_t=1
						vol=30
						pge.mp3.volume(30000)
						loop="off"
						pge.mp3.loop(false)
						if pge.dir.exists("ms0:/MUSIC") and pge.dir.read(pge.dir.open("ms0:/MUSIC")) then
							local temp=pge.dir.read(pge.dir.open("ms0:/MUSIC"))
							for i,v in ipairs(temp) do
								if v.dir then
									local temp2=pge.dir.read(pge.dir.open("ms0:/MUSIC/"..v.name))
									for i2,v2 in ipairs(temp2) do
										if string.lower(string.sub(v2.name,-4))==".mp3" then
											table.insert(music,string.sub(v2.name,1,-5))
											table.insert(music_d,"ms0:/MUSIC/"..v.name.."/"..v2.name)
										end
									end
								elseif string.lower(string.sub(v.name,-4))==".mp3" then
									table.insert(music,string.sub(v.name,1,-5))
									table.insert(music_d,"ms0:/MUSIC/"..v.name)
								end
							end
						end
						if pge.dir.exists("ms0:/PSP/MUSIC") and pge.dir.read(pge.dir.open("ms0:/PSP/MUSIC")) then
							local temp=pge.dir.read(pge.dir.open("ms0:/PSP/MUSIC"))
							for i,v in ipairs(temp) do
								if v.dir then
									local temp2=pge.dir.read(pge.dir.open("ms0:/PSP/MUSIC/"..v.name))
									for i2,v2 in ipairs(temp2) do
										if string.lower(string.sub(v2.name,-4))==".mp3" then
											table.insert(music,string.sub(v2.name,1,-5))
											table.insert(music_d,"ms0:/PSP/MUSIC/"..v.name.."/"..v2.name)
										end
									end
								elseif string.lower(string.sub(v.name,-4))==".mp3" then
									table.insert(music,string.sub(v.name,1,-5))
									table.insert(music_d,"ms0:/PSP/MUSIC/"..v.name)
								end
							end
						end
						if not music[1] then
							music[1]="No MP3 Files Found"
						end
					end

					local temp=math.max(pge.font.measure(font_1,"<"..music[music_t]..">"),pge.font.measure(font_1,"Resume / Pause"))
					menu_t=1
					sel=b(5)
					while pge.running() do
						pge.controls.update()

						if pge.controls.pressed(PGE_CTRL_DOWN) and menu_t<5 then
							menu_t = menu_t + 1
							sel=b(5)
						end
						if pge.controls.pressed(PGE_CTRL_UP) and menu_t>1 then
							menu_t = menu_t - 1
							sel=b(5)
						end
						sel[menu_t]=pge.gfx.createcolor(255,255,0,math.abs(math.sin(name_rot)*255))

						if pge.controls.pressed(PGE_CTRL_CROSS) then
							if menu_t==2 then
								if pge.mp3.isplaying() then
									pge.mp3.stop()
									music_data=nil
									collectgarbage("collect")
								else
									local temp=pge.file.open(music_d[music_t],PGE_FILE_RDONLY)
									music_data=pge.file.read(temp,pge.file.size(temp))
									pge.mp3.playmemory(music_data)
									collectgarbage("collect")
								end
							elseif menu_t==3 then
								pge.mp3.pause()
							elseif menu_t==4 then
								if loop=="on" then
									loop="off"
								else
									loop="on"
								end
								pge.mp3.loop(loop=="on")
							end
						end

						if menu_t==1 then
							if pge.controls.pressed(PGE_CTRL_LEFT) and music_t>1 then
								music_t=music_t-1
							end
							if pge.controls.pressed(PGE_CTRL_RIGHT) and music_t<#music then
								music_t=music_t+1
							end
							temp=math.max(pge.font.measure(font_1,"<"..music[music_t]..">"),pge.font.measure(font_1,"Resume / Pause"))
						elseif menu_t==5 then
							if pge.controls.held(PGE_CTRL_LEFT) and vol>0 then
								vol=vol-0.25
							end
							if pge.controls.held(PGE_CTRL_RIGHT) and vol<100 then
								vol=vol+0.25
							end
							pge.mp3.volume(vol*1000)
						end

						if pge.controls.pressed(PGE_CTRL_CIRCLE) or pge.controls.pressed(PGE_CTRL_START) then
							break
						end

						name_rot = name_rot + 0.045

						pge.gfx.startdrawing()

						pge.gfx.drawrect((480-temp)/2-29,50,58+temp,156,orange)
						pge.texture.activate(pix["bg"])
						pge.texture.draw(pix["bg"],(480-temp)/2-24,55,48+temp,146)

						pge.font.activate(font_1)
						pge.font.activate(font_1)
						pge.font.printcenter(font_1,60,sel[1],"<"..music[music_t]..">")
						pge.font.printcenter(font_1,90,sel[2],"Play / Stop")
						pge.font.printcenter(font_1,120,sel[3],"Resume / Pause")
						pge.font.printcenter(font_1,150,sel[4],"Loop: "..loop)
						pge.font.printcenter(font_1,180,sel[5],"Volume: "..math.floor(vol))

						pge.gfx.enddrawing()
						pge.gfx.swapbuffers()
					end
					menu_t=3
					sel=b(4)
				elseif menu_t==4 then
					if not pge.dir.exists("ms0:/PICTURE") then
						pge.dir.mkdir("ms0:/PICTURE")
					end
					function funq.screeny()
						pge.gfx.screenshot("ms0:/PICTURE/MindCraft Screenshot "..string.gsub(string.gsub(os.date(),"/","."),":",".").." .PNG")
						funq.screeny=nil
					end
					break
				end
			end

			name_rot = name_rot + 0.045

			pge.gfx.startdrawing()

			pge.gfx.drawrect((480-temp)/2-29,50,58+temp,136,orange)
			pge.texture.activate(pix["bg"])
			pge.texture.draw(pix["bg"],(480-temp)/2-24,55,48+temp,126)

			pge.font.activate(font_1)
			pge.font.activate(font_1)
			pge.font.printcenter(font_1,60,sel[1],"Continue")
			pge.font.printcenter(font_1,90,sel[2],"Save And Return To Menu")
			pge.font.printcenter(font_1,120,sel[3],"Play Music")
			pge.font.printcenter(font_1,150,sel[4],"Screenshot")

			pge.gfx.enddrawing()
			pge.gfx.swapbuffers()
		end
		menu_t=temp2
	end
	
	if m_inv_right() and menu_t<9 then
		menu_t=menu_t+1
	end
	if m_inv_left() and menu_t>1 then
		menu_t=menu_t-1
	end
	
	if m_left() then
		if side then side=false end
		if hand_ang<0 then hand_ang=-hand_ang end
		if block_data[at_pos(x+231,y+88)][2] and block_data[at_pos(x+231,y+136)][2] and block_data[at_pos(x+231,y+183)][2] then
			moved = true
			leg_ang=leg_ang-0.14
			x=x-4
			if x<0 and x<min then
				local temp=game_data["min"]
				gen(temp-128,temp-1)
				game_data["min"]=temp-128
				min=game_data["min"]*48+960
			end
		else
			pressed=true
		end
	elseif m_right() then
		if not side then side=true end
		if hand_ang>0 then hand_ang=-hand_ang end
		if block_data[at_pos(x+244,y+88)][2] and block_data[at_pos(x+244,y+136)][2] and block_data[at_pos(x+244,y+183)][2] then
			moved = true
			leg_ang=leg_ang+0.14
			x=x+4
			if x>0 and x>max then
				local temp=game_data["max"]
				gen(temp+1,temp+128)
				game_data["max"]=temp+128
				max=game_data["max"]*48-960
			end
		else
			pressed=true
		end
	elseif math.sin(leg_ang)~=0 then
		local temp = math.sin(leg_ang)
		if temp>-0.1 and temp<0.1 then
			leg_ang=0
		else
			leg_ang = leg_ang - sign(temp)*0.14
		end
	end
	
	if m_look_up() then
		hand_ang=hand_ang+0.048*sign(hand_ang)
		pressed=true
		if side and hand_ang<-math.pi then
			hand_ang=-math.pi
		elseif not side and hand_ang>math.pi then
			hand_ang=math.pi
		end
	end
	if m_look_down() then
		hand_ang=hand_ang-0.048*sign(hand_ang)
		pressed=true
		if side and hand_ang>0 then
			hand_ang=-0.01
		elseif not side and hand_ang<0 then
			hand_ang=0.01
		end
	end
	
	if m_jump() and jump_speed>0 and fall_speed==4 and block_data[at_pos(x+232,y+87)][2] and block_data[at_pos(x+243,y+87)][2] then
		if moved~=true then moved=true end
		y=y-jump_speed
		y=pge.math.round(y/2)*2
		jump_speed=jump_speed-0.48
	elseif jump_speed~=8 then
		jump_speed=8
	end
	
	if sel and m_remove() and sel~="" then
		if not block_health then
			block_health=15  --block_data[sel][1]
			t_block_health=block_health
			block_x=math.floor(sel_x/48)
			block_y=math.floor(sel_y/48)
		end
		block_health=block_health-1
		shake=shake+0.18
		if block_health<0 then
			local temp2=math.floor(sel_x/48)
			local temp3=math.floor(sel_y/48)
			if funq_break[at_pos_r(temp2,temp3)] then
				funq_break[at_pos_r(temp2,temp3)](temp2,temp3)
			end
			place(temp2,temp3," ")
			update_at(temp2,temp3)
			if hand_ang<0.54 and hand_ang>-0.54 then
				moved=true
			end
			pressed=true
			block_health=false
		end
	elseif shake~=0 and math.sin(shake)~=0 then
		if block_health then
			block_health=false
		end
		local temp = math.sin(shake)
		if temp>-0.1 and temp<0.1 then
			shake=0
		else
			shake=shake-sign(shake)*0.18
		end
	end
	
	if sel and m_place() then
		local temp2=math.floor(sel_x/48)
		local temp3=math.floor(sel_y/48)
		if funq_use[at_pos_r(temp2,temp3)] then
			funq_use[at_pos_r(temp2,temp3)](temp2,temp3)
		else
			if funq_place[blocks[menu_t]] then
				funq_place[blocks[menu_t]](temp2+place_x/48,temp3+place_y/48)
			end
			local temp2=temp2*48+place_x
			local temp3=temp3*48+place_y
			if keep then
				if not block_data[blocks[menu_t]][2] then
					if math.abs(x+214-temp2)<30 and math.abs(y+112-temp3)<72 then
						keep=false
					end
				end
				if keep then
					place(math.floor(temp2/48),math.floor(temp3/48),blocks[menu_t])
					pressed=true
					update_at(math.floor(temp2/48),math.floor(temp3/48))
				else
					keep=true
				end
			else
				keep=true
			end
		end
	end
	
	if moved or pressed then
		if floor_x~=last_x or last_y~=floor_y then
			if funq_touch[at_pos(x+231,y+88)] then
				funq_touch[at_pos(x+231,y+88)](math.floor((x+231)/48),math.floor((x+88)/48))
			end
			if funq_touch[at_pos(x+231,y+136)] then
				funq_touch[at_pos(x+231,y+136)](math.floor((x+231)/48),math.floor((x+136)/48))
			end
			if funq_touch[at_pos(x+231,y+183)] then
				funq_touch[at_pos(x+231,y+183)](math.floor((x+231)/48),math.floor((x+183)/48))
			end
			if funq_touch[at_pos(x+244,y+88)] then
				funq_touch[at_pos(x+244,y+88)](math.floor((x+244)/48),math.floor((x+88)/48))
			end
			if funq_touch[at_pos(x+244,y+136)] then
				funq_touch[at_pos(x+244,y+136)](math.floor((x+244)/48),math.floor((x+136)/48))
			end
			if funq_touch[at_pos(x+244,y+183)] then
				funq_touch[at_pos(x+244,y+183)](math.floor((x+244)/48),math.floor((x+183)/48))
			end
			if funq_touch[at_pos(x+232,y+87)] then
				funq_touch[at_pos(x+232,y+87)](math.floor((x+232)/48),math.floor((x+87)/48))
			end
			if funq_touch[at_pos(x+232,y+184)] then
				funq_touch[at_pos(x+232,y+184)](math.floor((x+232)/48),math.floor((x+184)/48))
			end
			if funq_touch[at_pos(x+243,y+87)] then
				funq_touch[at_pos(x+243,y+87)](math.floor((x+243)/48),math.floor((x+87)/48))
			end
			if funq_touch[at_pos(x+243,y+184)] then
				funq_touch[at_pos(x+243,y+184)](math.floor((x+243)/48),math.floor((x+184)/48))
			end
			last_x=floor_x
			last_y=floor_y
		end
		if hand_ang<0.68 and hand_ang>-0.68 then
			head_ang=sign(hand_ang)*-0.88
		else
			head_ang=hand_ang+sign(hand_ang)*-1.57
		end
		pressed=false
		if block_health and (block_x~=math.floor(sel_x/48) or block_y~=math.floor(sel_y/48)) then
			block_health=false
		end
		sel=false
		local hand_x=-math.sin(hand_ang)
		local hand_y=math.cos(hand_ang)
		for t=1,16 do
			if not is_none(at_pos(x+238+hand_x*t*12,y+112+hand_y*t*12)) then
				sel_y=y+112+hand_y*t*12
				if sel_y<48 then
					break
				end
				sel_x=x+238+hand_x*t*12
				local temp2=math.atan2(sel_x-math.floor(sel_x/48)*48-24,sel_y-math.floor(sel_y/48)*48-24)+math.pi*0.25
				place_x=0
				place_y=0
				if temp2>0 then
					if temp2>math.pi*0.5 then
						place_x=48
						sel_rot=math.pi*0.5
					else
						place_y=48
						sel_rot=math.pi
					end
				else
					if temp2>math.pi*-0.5 then
						place_x=-48
						sel_rot=math.pi*-0.5
					else
						place_y=-48
						sel_rot=0
					end
				end
				if is_none(at_pos(sel_x+place_x,sel_y+place_y)) then
					sel=at_pos(x+238+hand_x*t*12,y+112+hand_y*t*12)
				else
					temp2=temp2-math.pi*0.25
					if place_x~=0 then
						if math.abs(temp2)>math.pi*0.5 then
							place_x=0
							place_y=-48
							sel_rot=0
						else
							place_x=0
							place_y=48
							sel_rot=math.pi
						end
					else
						if temp2>0 then
							place_x=48
							place_y=0
							sel_rot=math.pi*0.5
						else
							place_x=-48
							place_y=0
							sel_rot=math.pi*-0.5
						end
					end
					if is_none(at_pos(sel_x+place_x,sel_y+place_y)) then
						sel=at_pos(x+238+hand_x*t*12,y+112+hand_y*t*12)
					else
						break
					end
				end
				if block_data[sel][3] then
					sel_rot=false
				end
				break
			end
		end
	end
	
	if moved and jump_speed==8 then
		local temp=y+184
		if block_data[at_pos(x+232,temp)][2] and block_data[at_pos(x+243,temp)][2] then
			y=y+fall_speed
			y=pge.math.round(y/2)*2
			if fall_speed<32 then
				fall_speed=fall_speed+0.14
			end
		else
			if fall_speed~=4 then
				fall_speed=4
				y=math.floor(y/48)*48+8
			end
			moved=false
		end
	end
	
	if m_cam_left() then
		l_x=x
		--if a_x>-240 then
			a_x=a_x-6
		--end
		x=x+a_x
	elseif m_cam_right() then
		l_x=x
		--if a_x<240 then
			a_x=a_x+6
		--end
		x=x+a_x
	elseif a_x~=0 then
		l_x=x
		a_x=a_x-sign(a_x)*6
		x=x+a_x
	end
	if m_cam_down() then
		l_y=y
		--if a_y<240 then
			a_y=a_y+6
		--end
		y=y+a_y
	elseif m_cam_up() then
		l_y=y
		--if a_y>-240 then
			a_y=a_y-6
		--end
		y=y+a_y
	elseif a_y~=0 then
		l_y=y
		a_y=a_y-sign(a_y)*6
		y=y+a_y
	end
	
	if floor_x~=old_x then
		if math.abs(floor_x-old_x)>1 then
			for t=0,10 do
				for t2=0,6 do
					if funq_screen[at_pos_r(floor_x+t,floor_y+t2)] then
						funq_screen[at_pos_r(floor_x+t,floor_y+t2)](floor_x+t,floor_y+t2)
					end
				end
			end
			old_y=floor_y
		elseif floor_x<old_x then
			for t=0,6 do
				if funq_screen[at_pos_r(floor_x,floor_y+t)] then
					funq_screen[at_pos_r(floor_x,floor_y+t)](floor_x,floor_y+t)
				end
			end
		else
			for t=0,6 do
				if funq_screen[at_pos_r(floor_x+10,floor_y+t)] then
					funq_screen[at_pos_r(floor_x+10,floor_y+t)](floor_x+10,floor_y+t)
				end
			end
		end
		old_x=floor_x
	end
	if floor_y~=old_y then
		if math.abs(floor_y-old_y)>1 then
			for t=0,10 do
				for t2=0,6 do
					if funq_screen[at_pos_r(floor_x+t,floor_y+t2)] then
						funq_screen[at_pos_r(floor_x+t,floor_y+t2)](floor_x+t,floor_y+t2)
					end
				end
			end
		elseif floor_y<old_y then
			for t=0,10 do
				if funq_screen[at_pos_r(floor_x+t,floor_y)] then
					funq_screen[at_pos_r(floor_x+t,floor_y)](floor_x+t,floor_y)
				end
			end
		else
			for t=0,10 do
				if funq_screen[at_pos_r(floor_x+t,floor_y+6)] then
					funq_screen[at_pos_r(floor_x+t,floor_y+6)](floor_x+t,floor_y+6)
				end
			end
		end
		old_y=floor_y
	end
	
	floor_x=math.floor(x/48)
	floor_y=math.floor(y/48)
	frac_x=floor_x*48-x
	frac_y=floor_y*48-y
	
	pge.gfx.startdrawing()
	
	pge.texture.activate(pix["sky"])
	pge.texture.draweasy(pix["sky"],0,0)
	
	pge.texture.activate(pix["cloud"])
	for t=1,#clouds do
		if clouds[t][1]<480 then
			if clouds[t][1]+clouds[t][3]>0 then
				pge.texture.draw(pix["cloud"], pge.math.round(clouds[t][1]),pge.math.round(clouds[t][2]),clouds[t][3],clouds[t][4])
			end
			clouds[t][1]=clouds[t][1]+clouds[t][5]
		else
			clouds[t]={math.random(-260,-90),math.random(10,80),math.random(38,82),math.random(20,62),pge.math.randfloat(0.2,0.6)}
		end
	end
	
	for t=0,10 do
		if floor_y+7>height[floor_x+t] then
			pge.texture.activate(pix["cave"])
			if floor_y>height[floor_x+t] then
				local temp=48*t+frac_x
				pge.texture.draw(pix["cave"],temp,0,48,272,temp,0,48,272)
			else
				local temp=48*t+frac_x
				local temp2=(height[floor_x+t]-floor_y)*48+frac_y
				pge.texture.draw(pix["cave"],temp,temp2,48,272-temp2,temp,temp2,48,272-temp2)
			end
		end
	end
	
	t_48=time-math.floor(time/48)*48
	
	active=false
	for t=0,10 do
		local temp
		if floor_y>1 then
			temp=string.sub(game_data[floor_x+t],floor_y,floor_y+6)
		else
			temp=string.rep(" ",1-floor_y)..string.sub(game_data[floor_x+t],1,7+floor_y)
		end
		for t2=0,6 do
			local temp2=char(temp,t2+1)
			if temp2~=" " then
				if active~=temp2 then
					pge.texture.activate(pix[temp2])
					active=temp2
				end
				if temp2=="W" then
					pge.texture.draw(pix[temp2],48*t+frac_x,48*t2+frac_y,t_48,48,48-t_48,0,t_48,48)
					pge.texture.draw(pix[temp2],48*t+frac_x+t_48,48*t2+frac_y,48-t_48,48,0,0,48-t_48,48)
				else
					if active~=temp2 then
						pge.texture.activate(pix[temp2])
						active=temp2
					end
					pge.texture.draweasy(pix[temp2],48*t+frac_x,48*t2+frac_y)
				end
			end
		end
	end
	
	if sel then
		local temp=math.floor(sel_x/48)*48-x
		local temp2=math.floor(sel_y/48)*48-y
		local temp3=math.abs(math.sin(time*0.03))*255
		if sel_rot then
			pge.texture.activate(pix["select"])
			pge.texture.draweasy(pix["select"],temp,temp2,sel_rot,temp3)
			pge.texture.activate(pix["select_2"])
			pge.texture.draweasy(pix["select_2"],temp,temp2,sel_rot,255-temp3)
		else
			pge.texture.activate(pix["selectf"])
			pge.texture.draweasy(pix["selectf"],temp,temp2,0,temp3)
			pge.texture.activate(pix["selectf_2"])
			pge.texture.draweasy(pix["selectf_2"],temp,temp2,0,255-temp3)
		end
		if block_health then
			local temp4="b_"..math.floor(block_health/t_block_health*10)
			pge.texture.activate(pix[temp4])
			pge.texture.draweasy(pix[temp4],temp,temp2)
		end
	end
	
	for i,v in pairs(funq) do
		v()
	end
	
	if a_x~=0 then
		x=l_x
	end
	if a_y~=0 then
		y=l_y
	end
	
	if a_x~=0 or a_y~=0 then
		if side then
			pge.texture.activate(pix["leg"])
			pge.texture.draweasy(pix["leg"],232-a_x,88-a_y,math.sin(leg_ang)*-0.48)
			pge.texture.activate(pix["body"])
			pge.texture.draweasy(pix["body"],232-a_x,88-a_y)
			pge.texture.activate(pix["leg"])
			pge.texture.draweasy(pix["leg"],232-a_x,88-a_y,math.sin(leg_ang)*0.48)
			pge.texture.activate(pix["head"])
			pge.texture.draweasy(pix["head"],228-a_x,88-a_y,head_ang)
			local r_hand_ang=hand_ang+math.sin(shake)*0.21
			pge.texture.activate(pix["hand"])
			pge.texture.draweasy(pix["hand"],233-a_x,80-a_y,r_hand_ang)
			pge.texture.activate(pix[blocks[menu_t]])
			pge.texture.draw(pix[blocks[menu_t]],228-a_x-math.sin(r_hand_ang)*38,102-a_y+math.cos(r_hand_ang)*38,20,20,0,0,48,48,r_hand_ang+math.pi*0.5)
		else
			pge.texture.activate(pix["leg_l"])
			pge.texture.draweasy(pix["leg_l"],232-a_x,88-a_y,math.sin(leg_ang)*-0.48)
			pge.texture.activate(pix["body_l"])
			pge.texture.draweasy(pix["body_l"],232-a_x,88-a_y)
			pge.texture.activate(pix["leg_l"])
			pge.texture.draweasy(pix["leg_l"],232-a_x,88-a_y,math.sin(leg_ang)*0.48)
			pge.texture.activate(pix["head_l"])
			pge.texture.draweasy(pix["head_l"],228-a_x,88-a_y,head_ang)
			local r_hand_ang=hand_ang+math.sin(shake)*0.21
			pge.texture.activate(pix["hand_l"])
			pge.texture.draweasy(pix["hand_l"],233-a_x,80-a_y,r_hand_ang)
			pge.texture.activate(pix[blocks[menu_t]])
			pge.texture.draw(pix[blocks[menu_t]],228-a_x-math.sin(r_hand_ang)*38,102-a_y+math.cos(r_hand_ang)*38,20,20,0,0,48,48,r_hand_ang-math.pi*0.5)
		end
	else
		if side then
			pge.texture.activate(pix["leg"])
			pge.texture.draweasy(pix["leg"],232,88,math.sin(leg_ang)*-0.48)
			pge.texture.activate(pix["body"])
			pge.texture.draweasy(pix["body"],232,88)
			pge.texture.activate(pix["leg"])
			pge.texture.draweasy(pix["leg"],232,88,math.sin(leg_ang)*0.48)
			pge.texture.activate(pix["head"])
			pge.texture.draweasy(pix["head"],228,88,head_ang)
			local r_hand_ang=hand_ang+math.sin(shake)*0.21
			pge.texture.activate(pix["hand"])
			pge.texture.draweasy(pix["hand"],233,80,r_hand_ang)
			pge.texture.activate(pix[blocks[menu_t]])
			pge.texture.draw(pix[blocks[menu_t]],228-math.sin(r_hand_ang)*38,102+math.cos(r_hand_ang)*38,20,20,0,0,48,48,r_hand_ang+math.pi*0.5)
		else
			pge.texture.activate(pix["leg_l"])
			pge.texture.draweasy(pix["leg_l"],232,88,math.sin(leg_ang)*-0.48)
			pge.texture.activate(pix["body_l"])
			pge.texture.draweasy(pix["body_l"],232,88)
			pge.texture.activate(pix["leg_l"])
			pge.texture.draweasy(pix["leg_l"],232,88,math.sin(leg_ang)*0.48)
			pge.texture.activate(pix["head_l"])
			pge.texture.draweasy(pix["head_l"],228,88,head_ang)
			local r_hand_ang=hand_ang+math.sin(shake)*0.21
			pge.texture.activate(pix["hand_l"])
			pge.texture.draweasy(pix["hand_l"],233,80,r_hand_ang)
			pge.texture.activate(pix[blocks[menu_t]])
			pge.texture.draw(pix[blocks[menu_t]],228-math.sin(r_hand_ang)*38,102+math.cos(r_hand_ang)*38,20,20,0,0,48,48,r_hand_ang-math.pi*0.5)
		end
	end
	
	pge.texture.activate(pix["inv_slot"])
	for t=1,9 do
		pge.texture.draweasy(pix["inv_slot"],64+t*32,240)
	end
	pge.texture.activate(pix["inv_sel"])
	pge.texture.draweasy(pix["inv_sel"],64+menu_t*32,240)
	for t=1,9 do
		pge.texture.activate(pix[blocks[t]])
		pge.texture.draw(pix[blocks[t]],68+t*32,244,24,24)
	end
	
	debug=0
	for i,v in pairs(drop_block) do
		debug=debug+1
	end
	pge.font.activate(font_1)
	pge.font.print(font_1,10,10,yellow,"X: "..x.." Y: "..y.." FPS: "..d_fps)
	pge.font.print(font_1,10,30,yellow,debug)
	
	pge.gfx.enddrawing()
	pge.gfx.swapbuffers()
	
	time=time+1
	if time>last_sec then
		for i,v in pairs(funq_05) do
			v()
		end
		last_sec=time+30
	end
	
	d_counter=d_counter+1
	if d_time~=os.date("*t")["sec"] then
		d_fps=d_counter
		d_counter=0
		d_time=os.date("*t")["sec"]
		if last_time<d_time then
			for i,v in pairs(funq_10) do
				v()
			end
			last_time=d_time+10
		end
	end
	
end