Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --BY WIGGLE1000! PLEASE DO NOT MODIFY OR REDISTRIBUTE UNDER YOUR NAME!
- PX = 30
- PY = 1
- P2X = 5
- P2Y = 1
- invX = 10
- invY = 4
- invSlotsH=6
- invSlotsV=3
- jumptimer=0
- gravityOn=true
- hotbarIndex=1
- mapWidth = 100
- mapHeight = 50
- screenx,screeny = term.getSize()
- camWidth = screenx-3
- camHeight = screeny-7
- camX=0
- camY=0
- editingBG=false
- map = {}
- mapBG = {}
- --[[ old map
- map[1] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
- map[2] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
- map[3] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
- map[4] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
- map[5] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
- map[6] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
- map[7] = {1,1,1,1,1,1,1,1,1,1,1,1,1,3,3,3,1,1,1,1,1,1,1,1}
- map[8] = {3,3,3,3,3,3,3,3,3,3,3,3,3,2,2,2,3,3,3,3,3,3,3,3}
- map[9] = {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}
- map[10]= {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}
- map[11]= {4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4}
- ]]--
- sandboxEnv={moveBlocks,setBlock,math.random,math.sin,math.round}
- blocks = {}--NAME, COLLIDES?, CHAR, FGC, BGC
- blocks[1] = {"air",false, " ", colors.lightBlue, colors.lightBlue}
- blocks[2] = {"dirt",true, " ", colors.brown, colors.brown}
- blocks[3] = {"grass",true, "~", colors.lime, colors.brown}
- blocks[4] = {"stone",true, "_", colors.lightGray, colors.gray}
- --5 is cloud
- blocks[6] = {"log",true, "^", colors.black, colors.brown}
- blocks[7] = {"leaves",true, "*", colors.green, colors.lime}
- blocks[8] = {"planks",true, "=", colors.brown, colors.orange}
- blocks[9] = {"coal",true, "*", colors.black, colors.gray}
- blocks[10] = {"iron",true, "*", colors.orange, colors.gray}
- --{ID,PERCENT}
- ores={{9,50},{10,10}}
- mouseX=0
- mouseY=0
- invOpen=false
- inventory = {}
- inventory[1]={1,2,3,4,5,6}
- inventory[2]={7,8,9,10,11,12}
- inventory[3]={13,14,15,16,17,18}
- hotbarItems = {}
- hotbarItems[1]=2
- hotbarItems[2]=3
- hotbarItems[3]=4
- hotbarItems[4]=5
- hotbarItems[5]=6
- hotbarItems[6]=7
- hotbarItems[7]=8
- oldfenv=getfenv()
- --utility funcs
- function split(str, pat)
- local t = {} -- NOTE: use {n = 0} in Lua-5.0
- local fpat = "(.-)" .. pat
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- return t
- end
- --by wiggle1000
- function strToCol(color)
- if(color=="white")then
- return colors.white
- end
- if(color=="orange")then
- return colors.orange
- end
- if(color=="magenta")then
- return colors.magenta
- end
- if(color=="lightBlue")then
- return colors.lightBlue
- end
- if(color=="yellow")then
- return colors.yellow
- end
- if(color=="lime")then
- return colors.lime
- end
- if(color=="pink")then
- return colors.pink
- end
- if(color=="gray" or color == "grey")then
- return colors.gray
- end
- if(color=="lightGray" or color == "lightGrey")then
- return colors.lightGray
- end
- if(color=="cyan")then
- return colors.cyan
- end
- if(color=="purple")then
- return colors.purple
- end
- if(color=="blue")then
- return colors.blue
- end
- if(color=="brown")then
- return colors.brown
- end
- if(color=="green")then
- return colors.green
- end
- if(color=="red")then
- return colors.red
- end
- if(color=="black")then
- return colors.black
- end
- return colors.black
- end
- --game funcs
- function growTree(baseX,baseY,height,width,leafHeight,leafWidth,rootDepth)
- for x = math.ceil(baseX-width),math.floor(baseX+(width)) do
- for y = baseY-height,baseY+rootDepth do
- if(mapBG[y]~=nil and mapBG[y][x]~=nil)then
- mapBG[y][x]=6 --log
- end
- end
- end
- for x = math.floor(baseX-math.floor(leafWidth)),math.floor(baseX+(leafWidth)) do
- for y = (baseY-height-leafHeight)-math.floor(leafHeight/2),baseY-height do
- if(map[y]~=nil and map[y][x]~=nil)then
- map[y][x]=7 --leaves
- end
- end
- end
- for x = math.floor(baseX-math.floor(leafWidth/2)),math.floor(baseX+(leafWidth/2)) do
- for y = (baseY-height-leafHeight)-(leafHeight),(baseY-height-leafHeight)-math.floor(leafHeight/2) do
- if(map[y]~=nil and map[y][x]~=nil)then
- map[y][x]=7 --leaves
- end
- end
- end
- end
- function spawnRandomOre(dictionary,x,y)
- ore=nil
- for i,arr in pairs(dictionary) do
- if(ore == nil and math.random(0,100)<arr[2])then
- ore=arr[1]
- end
- end
- if(ore~=nil)then
- map[y][x]=ore
- if(map[y]~=nil and map[y][x-1]~=nil)then
- map[y][x-1]=ore
- end
- if(map[y]~=nil and map[y][x+1]~=nil)then
- map[y][x+1]=ore
- end
- if(map[y-1]~=nil and map[y-1][x]~=nil)then
- map[y-1][x]=ore
- end
- if(map[y+1]~=nil and map[y+1][x]~=nil)then
- map[y+1][x]=ore
- end
- end
- end
- function genWorld(minHeight,maxHeight,w,h,top,mid,stone,stoneHeight)
- for y=1,mapHeight do
- map[y]={}
- for x=1,mapWidth do
- map[y][x]=1
- if(y == mapHeight)then
- map[y][x]=stone
- end
- end
- end
- --BG
- for y=1,mapHeight do
- mapBG[y]={}
- for x=1,mapWidth do
- mapBG[y][x]=1
- if(y == mapHeight)then
- mapBG[y][x]=stone
- end
- end
- end
- local CX = (mapHeight/2)
- for x=1,mapWidth do
- if(CX>maxHeight)then
- CX = maxHeight+math.random(2)-1
- elseif(CX<minHeight)then
- CX = minHeight+math.random(2)-1
- else
- CX=CX+math.random(3)-2
- end
- for y=1,mapHeight do
- if(y>=CX)then
- map[y][x]=mid
- if(y >= stoneHeight)then
- map[y][x]=stone
- if(math.random(1,100)<=5)then
- spawnRandomOre(ores,x,y)
- end
- end
- if(y == CX)then
- map[y][x]=top
- if(math.random(1,100)<=6)then
- if(math.random(1,100)<=20)then
- growTree(x,y-1,5+math.random(1,3),math.random(1,2),math.random(2,4),math.random(4,7),2)
- else
- growTree(x,y-1,2+math.random(1,3),math.random(5,8)/10,math.random(2,3),math.random(2,3),1)
- end
- end
- end
- if(y == CX+1)then
- map[y][x]=mid
- end
- end
- end
- end
- end
- function drawGround(xOff, yOff)
- for y = 0,camHeight do
- for x =0,camWidth do
- term.setCursorPos((xOff+x),(yOff+y))
- if((map[y+camY]~=nil)and(map[y+camY][x+camX]~=nil)and(mapBG[y+camY]~=nil)and(mapBG[y+camY][x+camX]~=nil))then
- if(y<=camY+camHeight and x<=camX+camWidth)then
- if(map[y+camY][x+camX]==1)then
- term.setTextColor(tonumber(blocks[mapBG[y+camY][x+camX]][4]))
- term.setBackgroundColor(tonumber(blocks[mapBG[y+camY][x+camX]][5]))
- term.write(tostring(blocks[mapBG[y+camY][x+camX]][3]))
- else
- term.setTextColor(tonumber(blocks[map[y+camY][x+camX]][4]))
- term.setBackgroundColor(tonumber(blocks[map[y+camY][x+camX]][5]))
- term.write(tostring(blocks[map[y+camY][x+camX]][3]))
- end
- end
- else
- term.setBackgroundColor(colors.black)
- term.write(" ")
- end
- end
- end
- end
- function input(mOffX,mOffY)
- event,p1,p2,p3,p4 = os.pullEvent()
- --W:17 A:30 S:31 D:32 Q:16 E:18 SPACE:57 ENTER:28
- if(event == "key") then
- if(p1==30) then--A
- if (blocks[map[PY][PX-1]][2]==false)then
- PX = PX - 1
- end
- elseif(p1==32) then--D
- if (blocks[map[PY][PX+1]][2]==false)then
- PX = PX + 1
- end
- elseif(p1==57) then--SPACE
- if (blocks[map[PY+1][PX]][2]==true and blocks[map[PY-1][PX]][2]==false)then
- PY = PY - 1
- gravityOn=false
- jumptimer = 4
- end
- elseif(p1==28)then--enter
- editingBG=not editingBG
- elseif(p1==18)then--E
- invOpen=not invOpen
- end
- elseif(event=="mouse_click")then
- if(invOpen==false)then --WORLD(inv closed)
- if(editingBG==false)then
- if(map[p3+camY-mOffY]~=nil and map[p3+camY-mOffY][p2+camX-mOffX]~=nil)then
- if(p1==1)then
- map[p3+camY-mOffY][p2+camX-mOffX]=1
- end
- if(p1==2)then
- map[p3+camY-mOffY][p2+camX-mOffX]=hotbarItems[hotbarIndex+1]
- end
- end
- else
- if(mapBG[p3+camY-mOffY][p2+camX-mOffX]~=nil)then
- if(p1==1)then
- mapBG[p3+camY-mOffY][p2+camX-mOffX]=1
- end
- if(p1==2)then
- mapBG[p3+camY-mOffY][p2+camX-mOffX]=hotbarItems[hotbarIndex+1]
- end
- end
- end
- else --INVENTORY
- for x = 1,(invSlotsH) do
- for y = 1,(invSlotsV) do
- if(inventory[y]~=nil and inventory[y][x]~=nil and blocks[inventory[y][x]]~=nil)then
- if(p2==(x*3)+invX and p3==(y*3)+invY-2)then
- hotbarItems[hotbarIndex+1]=inventory[y][x]
- invOpen=false
- end
- end
- end
- end
- end
- elseif(event=="mouse_scroll")then
- if(p1<0)then
- if(hotbarIndex>0)then
- hotbarIndex = hotbarIndex - 1
- else
- hotbarIndex=6
- end
- elseif(p1>0)then
- if(hotbarIndex<6)then
- hotbarIndex = hotbarIndex + 1
- else
- hotbarIndex=0
- end
- end
- end
- if(jumptimer==2)then
- if (blocks[map[PY-1][PX]][2]==false)then
- PY = PY - 1
- end
- end
- if(jumptimer<=0)then
- jumptimer=0
- gravityOn=true
- end
- end
- --by wiggle1000
- function drawBuildModeInfo(offX,offY)
- term.setCursorPos(offX,offY)
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- if(editingBG==false)then
- term.write("Editing: FG")
- else
- term.write("Editing: BG")
- end
- end
- function drawHotbar(offX,offY)
- for x = offX,offX+(7*3) do
- for y = offY, offY+2 do
- term.setCursorPos(x,y)
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.lightGray)
- term.write(" ")
- end
- end
- for x = offX+(hotbarIndex*3),offX+(hotbarIndex*3)+2 do
- for y = offY, offY+2 do
- term.setCursorPos(x,y)
- term.setBackgroundColor(colors.lightGray)
- term.setTextColor(colors.white)
- term.write(" ")
- end
- end
- for x = offX+(hotbarIndex*3),offX+(hotbarIndex*3) do
- if(hotbarItems[hotbarIndex+1]~=nil and blocks[hotbarItems[hotbarIndex+1]]~=nil)then
- term.setCursorPos(x+1,offY+1)
- term.setTextColor(tonumber(blocks[hotbarItems[hotbarIndex+1]][4]))
- term.setBackgroundColor(tonumber(blocks[hotbarItems[hotbarIndex+1]][5]))
- term.write(tostring(blocks[hotbarItems[hotbarIndex+1]][3]))
- end
- end
- end
- function drawPlayer(offX,offY)
- term.setCursorPos((camWidth/2)+offX,(camHeight/2)+offY)
- term.setBackgroundColor(tonumber(blocks[mapBG[PY][PX]][5]))
- term.setTextColor(playerColor)
- term.write("&")
- end
- function gravity()
- if(gravityOn==true)then
- if (blocks[map[PY+1][PX]][2]==false)then
- PY = PY + 1
- end
- end
- end
- function loadBlocks()
- if(not fs.exists("WC") or not fs.exists("WC/blocks"))then
- if(not not fs.exists("WC"))then
- fs.makeDir("WC")
- end
- print("GETTING EXAMPLE BLOCK... (DIRECTORY: WC/blocks)")
- shell.run("pastebin get VYh3rNRM WC/blocks")
- sleep(1)
- print("GETTING EXAMPLE SCRIPT... (DIRECTORY: WC/cloudscript)")
- shell.run("pastebin get SmH4KCc0 WC/cloudscript")
- sleep(5)
- end
- local filehandle = fs.open("WC/blocks","r")
- line1=filehandle.readLine()
- while (line1~=nil)do
- local parts = split(line1,",")
- isSolid=false
- if(tostring(parts[3])=="true")then
- isSolid=true
- end
- fcol = strToCol(tostring(parts[5]))
- bcol = strToCol(tostring(parts[6]))
- blocks[tonumber(parts[1])]={tostring(parts[2]),isSolid,tostring(parts[4]),fcol,bcol,tostring(parts[7])}
- line1=filehandle.readLine()
- end
- filehandle:close()
- end
- function moveBlocks(xoff,yoff,cblock,replaces)
- for xs = 1, mapWidth do
- for ys = 1, mapHeight do
- if(map[ys][xs]==cblock)then
- map[ys][xs]=1
- if(replaces)then
- if(map[ys+yoff]~=nil and map[ys+yoff][xs+xoff]~=nil)then
- map[ys+yoff][xs+xoff]=cblock
- end
- else
- if(map[ys+yoff]~=nil and map[ys+yoff][xs+xoff]~=nil and map[ys+yoff][xs+xoff]==1)then
- map[ys+yoff][xs+xoff]=cblock
- end
- end
- end
- if(mapBG[ys][xs]==cblock)then
- mapBG[ys][xs]=1
- if(replaces)then
- if(mapBG[ys+yoff]~=nil and mapBG[ys+yoff][xs+xoff]~=nil)then
- mapBG[ys+yoff][xs+xoff]=cblock
- end
- else
- if(mapBG[ys+yoff]~=nil and mapBG[ys+yoff][xs+xoff]~=nil and mapBG[ys+yoff][xs+xoff]==1)then
- mapBG[ys+yoff][xs+xoff]=cblock
- end
- end
- end
- end
- end
- end
- function setBlock(xoff,yoff,cblock,sblock,replaces)
- for xs = 1, mapWidth do
- for ys = 1, mapHeight do
- if(map[ys][xs]==cblock)then
- map[ys][xs]=1
- if(replaces)then
- if(map[ys+yoff]~=nil and map[ys+yoff][xs+xoff]~=nil)then
- map[ys+yoff][xs+xoff]=sblock
- end
- else
- if(map[ys+yoff]~=nil and map[ys+yoff][xs+xoff]~=nil and map[ys+yoff][xs+xoff]==1)then
- map[ys+yoff][xs+xoff]=sblock
- end
- end
- end
- if(mapBG[ys][xs]==cblock)then
- mapBG[ys][xs]=1
- if(replaces)then
- if(mapBG[ys+yoff]~=nil and mapBG[ys+yoff][xs+xoff]~=nil)then
- mapBG[ys+yoff][xs+xoff]=sblock
- end
- else
- if(mapBG[ys+yoff]~=nil and mapBG[ys+yoff][xs+xoff]~=nil and mapBG[ys+yoff][xs+xoff]==1)then
- mapBG[ys+yoff][xs+xoff]=sblock
- end
- end
- end
- end
- end
- end
- function drawInv(offX,offY,hSlots,vSlots,inv)
- if(invOpen)then
- for x = offX,offX+(hSlots*3)-1 do
- for y = offY, offY+(vSlots*3) do
- term.setCursorPos(x+2,y)
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.write("|")
- end
- end
- for x = 1,(hSlots) do
- for y = 1,(vSlots) do
- if(inventory[y]~=nil and inventory[y][x]~=nil and blocks[inventory[y][x]]~=nil)then
- term.setCursorPos((x*3)+offX,(y*3)+offY-2)
- term.setTextColor(tonumber(blocks[inventory[y][x]][4]))
- term.setBackgroundColor(tonumber(blocks[inventory[y][x]][5]))
- term.write(tostring(blocks[inventory[y][x]][3]))
- end
- end
- end
- end
- end
- function doBlockCode()
- for i,block in pairs(blocks) do
- if(block[6]~=nil)then
- file = block[6]
- setfenv(2,sandboxEnv)
- dofile("WC/"..block[6])
- setfenv(2,oldfenv)
- blockFunc()
- end
- end
- end
- --by wiggle1000
- function tickLoop()
- while true do
- os.startTimer(0.15)
- input(1,4)
- camX=PX-(camWidth/2)
- camY=PY-(camHeight/2)
- gravity()
- term.setCursorPos(1,1)
- doBlockCode()
- if(not invOpen)then
- drawGround(1,4)
- drawPlayer(1,4)
- end
- drawHotbar(1,1)
- drawBuildModeInfo(25,1)
- drawInv(invX,invY,invSlotsH,invSlotsV,inventory)
- sleep(0.1)
- if(jumptimer>0)then
- jumptimer = jumptimer - 1
- end
- end
- end
- --by wiggle1000
- term.clear()
- term.setCursorPos(1,1)
- term.write("Enter world seed(WIP):")
- math.randomseed = tonumber(read())
- term.clear()
- term.setCursorPos(1,1)
- term.write("Enter player color (EG. red):")
- playerColor = strToCol(read())
- term.clear()
- loadBlocks()
- genWorld(10,20,mapWidth,mapHeight,3,2,4,mapHeight/2)
- term.clear()
- term.setCursorPos(1,1)
- tickLoop()
- --by wiggle1000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement