-- MindCraft
-- Programming By Perli55

local temp = pge.texture.load("pix/load.png")
pge.gfx.startdrawing()
pge.texture.activate(temp)
pge.texture.draweasy(temp,0,0)
pge.gfx.enddrawing()
pge.gfx.swapbuffers()

math.randomseed(os.clock())
pge.math.srand(os.clock())
black = pge.gfx.createcolor(0,0,0)
red = pge.gfx.createcolor(255,0,0)
orange = pge.gfx.createcolor(255,100,0)
yellow = pge.gfx.createcolor(255,255,0)

function b(f_num) 
	local array={}
	for t=1,f_num do
		table.insert(array,black)
	end
	return array
end

function sign(f_num)
	if f_num>0 then 
		return 1
	elseif f_num<0 then
		return -1
	else
		return 0
	end
end

function char(f_s,f_num)
	return string.sub(f_s,f_num,f_num)
end

function at_pos(f_x,f_y)
	return char(game_data[math.floor(f_x/48)],math.floor(f_y/48))
end

function at_pos_r(f_x,f_y)
	return char(game_data[f_x],f_y)
end

function place(f_x,f_y,f_s)
	local temp=game_data[f_x]
	game_data[f_x]=string.sub(temp,1,f_y-1)..f_s..string.sub(temp,f_y+string.len(f_s))
end

function place_d(f_x,f_y,f_s)
	local temp=game_data[f_x]
	game_data[f_x]=string.sub(temp,1,f_y-string.len(f_s))..f_s..string.sub(temp,f_y+1)
end

function update_at(f_x,f_y)
	if funq_update[at_pos_r(f_x,f_y)] then
		funq_update[at_pos_r(f_x,f_y)](f_x,f_y)
	end
	if funq_update[at_pos_r(f_x+1,f_y)] then
		funq_update[at_pos_r(f_x+1,f_y)](f_x+1,f_y)
	end
	if funq_update[at_pos_r(f_x-1,f_y)] then
		funq_update[at_pos_r(f_x-1,f_y)](f_x-1,f_y)
	end
	if funq_update[at_pos_r(f_x,f_y+1)] then
		funq_update[at_pos_r(f_x,f_y+1)](f_x,f_y+1)
	end
	if funq_update[at_pos_r(f_x,f_y-1)] then
		funq_update[at_pos_r(f_x,f_y-1)](f_x,f_y-1)
	end
end

function is_none(f_s)
	return f_s==" " or f_s=="W" or f_s=="V"
end

function flag_set(f_x,f_y,f_val)
	if not flags[f_x] then
		flags[f_x]={}
	end
	flags[f_x][f_y]=f_val
end

function flag_get(f_x,f_y)
	if flags[f_x] then
		return flags[f_x][f_y]
	else
		return nil
	end
end

function flag_kill(f_x,f_y)
	if flags[f_x] then
		flags[f_x][f_y]=nil
		local temp=true
		for i,v in pairs(flags[f_x]) do
			temp=nil
			break
		end
		if temp then
			flags[f_x]=nil
		end
	end
end

ore_types=""
local temp={"c","i","a","e","A","r","G","D"}
local temp2={30,22,15,5,14,6,6,2}
for t=1,#temp do
	ore_types=ore_types..string.rep(temp[t],temp2[t])
end

function gen(f_begin,f_end)
	local ores={}
	local temp
	if f_end>0 then
		temp=height[f_begin-1]-4
	else
		temp=height[f_end+1]-4
	end
	local temp2
	local temp3
	local temp4
	local temp5
	local water={}
	local w_x
	if math.random(10)<7 then
		w_x=math.random(f_begin,f_end-34)
	end
	local w_x2
	if math.random(1)==1 then
		w_x2=math.random(f_begin,f_end-34)
	else
		w_x2=nil
	end
	local t=f_begin
	while t<=f_end do
		if w_x==t or w_x2==t then
			
			local w_sel=true
			if w_x2==t then
				w_sel=nil
			end
			
			for t2=t,t+math.random(4) do
				temp=temp+math.random(0,1)
				if temp>56 then temp=56 end
				local temp2=math.random(-1,1)
				local sand_h=math.random(2,3)
				game_data[t2]=string.rep(" ",temp-1)..string.rep("S",sand_h)..string.rep("R",5-sand_h+temp2)..string.rep("s",128-(temp+5+temp2)+math.random(3))
				height[t2]=temp
				t=t+1
			end
			
			local w_l=math.random(8,26)
			local w_al=w_l*0.5
			local w_g=t
			local w_y=math.random(4,12)
			
			if w_sel then
				water[1]=t
				water[2]=t+w_l
				water[3]=temp
			else
				water[4]=t
				water[5]=t+w_l
				water[6]=temp
			end
			
			for t2=t,t+w_l do
				local length=math.ceil((1-math.abs(t2-w_g-w_al)/w_al)*w_y+0.1)
				local temp2=math.random(-1,1)
				local sand_h=math.random(2,3)
				game_data[t2]=string.rep(" ",temp-1+length)..string.rep("S",sand_h)..string.rep("R",5-sand_h+temp2)..string.rep("s",128-(temp+length+5+temp2)+math.random(3))
				height[t2]=temp+length
				t=t+1
			end
			
			for t2=t,t+math.random(4) do
				temp=temp+math.random(-1,0)
				if temp<16 then temp=16 end
				local temp2=math.random(-1,1)
				local sand_h=math.random(2,3)
				game_data[t2]=string.rep(" ",temp-1)..string.rep("S",sand_h)..string.rep("R",5-sand_h+temp2)..string.rep("s",128-(temp+5+temp2)+math.random(3))
				height[t2]=temp
				t=t+1
			end
		else
			temp=temp+math.random(-1,1)
			temp2=temp+4+math.random(-1,1)
			temp5=temp4
			temp4=temp3
			temp3=temp
			if temp5==temp and temp4~=temp then
				temp=temp+(temp4-temp5)*math.random(2)
				temp5=temp4
				temp4=temp3
				temp3=temp
			end
			if temp<16 then temp=16
			elseif temp>56 then temp=56
			end
			game_data[t]=string.rep(" ",temp-1).."g"..string.rep("d",temp2-temp)..string.rep("s",128-temp2+math.random(3))
			height[t]=temp
			t=t+1
		end
	end
	
	for t=f_begin,f_end,2 do
		if t>f_begin and t<f_end then table.insert(ores,{t,math.random(height[t]+6,126)}) end
	end
	
	for t=1,#ores do
		temp=math.random(100)
		local ore=char(ore_types,temp)
		temp=ores[t]
		local ore_x=temp[1]
		local ore_y=temp[2]
		temp=""
		for t=1,9 do
			if math.random(2)==1 then
				temp=temp.."s"
			else
				temp=temp..ore
			end
		end
		place(ore_x,ore_y,string.sub(temp,1,3))
		place(ore_x-1,ore_y,string.sub(temp,4,6))
		place(ore_x+1,ore_y,string.sub(temp,7))
	end
	
	for t=1,math.random(35,50) do
		local w=math.random(3,5)
		local half=w*0.5
		local h=math.random(3,5)
		local c_x=math.random(f_begin,f_end-w)-1
		local c_y=math.random(height[c_x]+4,128-h)
		for t2=1,w do
			local temp=math.ceil((half-math.abs(half-t2))/half*h+0.1)
			place(c_x+t2,c_y-temp*0.5,string.rep("R",temp))
		end
	end
	
	for t=1,math.random(2) do
		local c_x=math.random(f_begin+15,f_end-15)
		local c_y=height[c_x]-4
		local c_dir=sign(math.random()-0.5)
		local c_max=pge.math.randfloat(0.75,1)
		local c_min=c_max-1
		local rand_t=0
		local rand_r=math.random(10,16)
		for t2=1,math.random(40,80) do
			c_x=c_x+c_dir
			if c_x<f_begin or c_x>f_end then
				break
			end
			c_y=c_y+pge.math.round(pge.math.randfloat(c_min,c_max))
			if c_y>124 then
				break
			end
			local c_size=math.random(0,1)
			place(c_x,c_y+c_size,string.rep(" ",math.random(4,5)-c_size))
			
			rand_t=rand_t+1
			if rand_t>rand_r then
				c_max=pge.math.randfloat(0.75,1)
				c_min=c_max-1
				rand_t=0
				rand_r=math.random(10,16)
			end
			
		end
	end
	
	for t=1,math.random(8,12) do
		local c_x=math.random(f_begin+15,f_end-15)
		local c_y=math.random(height[c_x]+10,115)
		local c_dir=sign(math.random()-0.5)
		local c_max=math.random()
		local c_min=c_max-1
		local rand_t=0
		local rand_r=math.random(10,16)
		for t2=1,math.random(40,80) do
			c_x=c_x+c_dir
			if c_x<f_begin or c_x>f_end then
				break
			end
			c_y=c_y+pge.math.round(pge.math.randfloat(c_min,c_max))
			if c_y<(height[c_x]+5) or c_y>124 then
				break
			end
			local c_size=math.random(0,1)
			place(c_x,c_y+c_size,string.rep(" ",math.random(4,5)-c_size))
			
			rand_t=rand_t+1
			if rand_t>rand_r then
				rand_t=0
				rand_r=math.random(10,16)
				c_max=math.random()
				c_min=c_max-1
			end
			
		end
	end
	
	if water[1] then
		for t=water[1],water[2] do
			place(t,water[3],string.rep("W",height[t]-water[3]))
		end
	end
	if water[4] then
		for t=water[4],water[5] do
			place(t,water[6],string.rep("W",height[t]-water[6]))
		end
	end
	
	local plants=""
	while string.len(plants)<130 do
		plants=plants..string.rep(char("11223345",math.random(9)),math.random(5,10))
	end
	local t=f_begin
	local space=t
	while pge.running() do
		while game_data[t] do
			if string.find(game_data[t],"g") then
				break
			else
				t=t+1
				if t>f_end then break end
			end
		end
		if t>f_end then break end
		if t-3<space or t+3>f_end then
			temp=43
		else
			if temp~=1 then temp=1 end
		end
		local rand=math.random(temp,100)
		if rand<=24 then
			local trunk=char("uvw",math.random(3))
			local tree_len=math.random(4,6)
			local leaves=""
			for t2=1,21 do
				leaves=leaves..char("lL",math.random(2))
			end
			tree_y=height[t]-1-tree_len
			place(t,tree_y,char(leaves,1))
			place(t-1,tree_y,string.sub(leaves,5,7))
			place(t+1,tree_y,string.sub(leaves,2,4))
			place(t,tree_y-1,char(leaves,math.random(0,1)*11))
			place(t-1,tree_y-1,char(leaves,math.random(0,1)*12))
			place(t+1,tree_y-1,char(leaves,math.random(0,1)*13))
			place(t-1,tree_y+3,char(leaves,math.random(0,1)*14))
			place(t+1,tree_y+3,char(leaves,math.random(0,1)*15))
			place(t-2,tree_y,char(leaves,math.random(0,1)*16))
			place(t-2,tree_y+1,char(leaves,math.random(0,1)*17))
			place(t-2,tree_y+2,char(leaves,math.random(0,1)*18))
			place(t+2,tree_y,char(leaves,math.random(0,1)*19))
			place(t+2,tree_y+1,char(leaves,math.random(0,1)*20))
			place(t+2,tree_y+2,char(leaves,math.random(0,1)*21))
			if char(game_data[t+2],tree_y+1)~=" " then
				place(t+2,tree_y+1,char(leaves,math.random(0,1)*8))
			end
			if char(game_data[t-2],tree_y+1)~=" " then
				place(t-2,tree_y+1,char(leaves,math.random(0,1)*9))
			end
			if char(game_data[t],tree_y-1)~=" " then
				place(t,tree_y-2,char(leaves,math.random(0,1)*10))
			end
			place_d(t,height[t]-1,string.rep(trunk,tree_len))
			space=t
		elseif rand<=42 then
			if math.random(2)==1 then
				local h=math.min(height[t],height[t+1])-1
				place(t,h,string.rep("b",height[t]-h))
				place(t+1,h,string.rep("b",height[t+1]-h))
				place(t,height[t]-2,char("b",math.random(0,1)))
				place(t+1,height[t+1]-2,char("b",math.random(0,1)))
				t=t+1
			else
				local h=math.min(height[t],height[t+1],height[t+2])-1
				place(t,h,string.rep("b",height[t]-h))
				place(t+1,h-1,string.rep("b",height[t+1]-h+1))
				place(t+2,h,string.rep("b",height[t+2]-h))
				place(t,height[t]-2,char("b",math.random(0,1)))
				place(t+2,height[t+2]-2,char("b",math.random(0,1)))
				t=t+2
			end
			space=t-1
		elseif rand<=64 then
			place(t,height[t]-1,char(plants,math.abs(t-f_begin)+1))
		end
		t=t+1
	end
	
	for t=f_begin,f_end do
		height[t]=height[t]+4
	end
	
	if f_end<0 then
		local mid=f_begin+64
		local temp={}
		local temp2={}
		for t=1,64 do
			temp[mid+t-1]=game_data[mid-t]
			temp2[mid+t-1]=height[mid-t]
		end
		for t=1,64 do
			game_data[mid-t]=game_data[mid+t-1]
			height[mid-t]=height[mid+t-1]
		end
		for i,v in pairs(temp) do
			game_data[i]=v
		end
		for i,v in pairs(temp2) do
			height[i]=v
		end
	end
end

-- 1: Health (number)
-- 2: None-solid? (nil or true) (set to nil if it's not meant to be placed)
-- 3: Not precise crosshair? (nil or true)
-- 4: Made of (one of d=dirt,s=sand,w=wood,p=plant,r=rock,m=metal,g=glass)
-- 5: Item drop(s) (as a function that returns a string or nil if nothing and true if itself)
-- 6: Collision cordinates

block_data={
	g={90,nil,nil,"d"},
	d={90,nil,nil,"d"},
	R={50,nil,nil,"s"},
	S={60,nil,nil,"s"},
	u={140,nil,nil,"w"},
	v={70,nil,nil,"w"},
	w={105,nil,nil,"w"},
	l={70,nil,nil,"p"},
	L={75,nil,nil,"p"},
	b={110,nil,nil,"p"},
	["1"]={10,true,true,"p"},
	["2"]={10,true,true,"p"},
	["3"]={10,true,true,"p"},
	["4"]={10,true,true,"p"},
	["5"]={20,true,true,"p"},
	["6"]={20,true,true,"p"},
	
	s={150,nil,nil,"r"},
	c={180,nil,nil,"r"},
	i={270,nil,nil,"r"},
	a={330,nil,nil,"r"},
	e={450,nil,nil,"r"},
	A={265,nil,nil,"r"},
	r={390,nil,nil,"r"},
	G={415,nil,nil,"r"},
	D={450,nil,nil,"r"},
	
	f={80,true,nil,"w"},
	C={80,nil,nil,"g"},
	U={100,nil,nil,"w"},
	t={130,nil,nil,"w"},
	k={195,nil,nil,"r"},
	J={300,nil,nil,"m"},
	z={360,nil,nil,"m"},
	E={480,nil,nil,"m"},
	H={295,nil,nil,"m"},
	p={420,nil,nil,"m"},
	B={445,nil,nil,"m"},
	P={480,nil,nil,"m"},
}
block_data["W"]={nil,true}
block_data["V"]={nil,true}
block_data[" "]={nil,true}
block_data[""]={nil,nil,nil,"r"}

funq_break={}
funq_touch={}
funq_place={}
funq_use={}
funq_update={}
funq_screen={}

function funq_touch.W(f_x,f_y)
	if not funq.swin then
		function funq.swin()
			if at_pos(x+232,y+88)=="W" or at_pos(x+232,y+88)=="W" or at_pos(x+232,y+183)=="W" or at_pos(x+232,y+183)=="W" then
				if m_jump() then
					jump_speed=3
				end
				fall_speed=4
			else
				funq.swin=nil
			end
		end
	end
end

function funq_touch.f(f_x,f_y)
	if not funq.climb then
		function funq.climb()
			if at_pos(x+232,y+88)=="f" or at_pos(x+232,y+88)=="f" or at_pos(x+232,y+183)=="f" or at_pos(x+232,y+183)=="f" then
				if m_jump() then
					jump_speed=3
				end
				fall_speed=4
			else
				funq.climb=nil
			end
		end
	end
end

function funq_update.W(f_x,f_y)
	table.insert(water_flow,f_x)
	table.insert(water_flow,f_y)
	if not funq_05.w_flow then
		funq_05.w_flow=w_flow
	end
end

function w_flow()
	local temp={}
	for t=1,#water_flow*0.5 do
		local t_x=water_flow[t*2-1]
		local t_y=water_flow[t*2]
		if game_data["min"]<t_x-1 and t_x+1<game_data["max"] then
			if at_pos_r(t_x+1,t_y)==" " then
				table.insert(temp,t_x+1)
				table.insert(temp,t_y)
				place(t_x+1,t_y,"W")
			end
			if at_pos_r(t_x-1,t_y)==" " then
				table.insert(temp,t_x-1)
				table.insert(temp,t_y)
				place(t_x-1,t_y,"W")
			end
		end
		if at_pos_r(t_x,t_y+1)==" " then
			table.insert(temp,t_x)
			table.insert(temp,t_y+1)
			place(t_x,t_y+1,"W")
		end
	end
	if temp[1] then
		water_flow=temp
		for t=1,#water_flow*0.5 do
			update_at(water_flow[t*2-1],water_flow[t*2])
		end
	else
		funq_05.w_flow=nil
		water_flow={}
	end
end

function funq_update.S(f_x,f_y)
	if is_none(at_pos_r(f_x,f_y+1)) then
		drop_block[f_x..","..f_y]=true
		if not funq_05.block_drop then
			funq_05.block_drop=block_drop
		end
	end
end

function funq_update.R(f_x,f_y)
	if is_none(at_pos_r(f_x,f_y+1)) then
		drop_block[f_x..","..f_y]=true
		if not funq_05.block_drop then
			funq_05.block_drop=block_drop
		end
	end
end

function block_drop()
	local temp=true
	for i,v in pairs(drop_block) do
		temp=nil
		local temp2=string.find(i,",")
		local t_x=tonumber(string.sub(i,1,temp2-1))
		local t_y=tonumber(string.sub(i,temp2+1))
		if at_pos_r(t_x,t_y)=="S" or at_pos_r(t_x,t_y)=="R" and is_none(at_pos_r(t_x,t_y+1)) then
			place(t_x,t_y+1,at_pos_r(t_x,t_y))
			place(t_x,t_y," ")
			update_at(t_x,t_y)
			update_at(t_x,t_y+1)
			pressed=true
		end
		drop_block[i]=nil
	end
	if temp then
		funq_05.block_drop=nil
	end
end

font_1=pge.font.load("fonts/verdana.ttf",18)
font_2=pge.font.load("fonts/verdana.ttf",12)

local temp=pge.file.open("settings.cfg",PGE_FILE_RDONLY)
temp=pge.file.read(temp,pge.file.size(temp))
theme=string.sub(temp,16)
cons_file=string.sub(temp,1,15)
pix={}
dofile("resources.lua")