Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --]] Block Table [--
- term.redirect(term.native())
- local w, h = term.getSize()
- local selBlock = 1
- local sMenuIndex = 1
- local inSave = false
- local inGame = true
- local sPlay = false
- local map = {}
- blocks = {
- [0] = {name="Air",color="black",bcolor="black",graphic=" "},
- {name="Dirt",color="yellow",bcolor="brown",graphic=" "}, -- 1
- {name="Stone",color="lightGray",bcolor="gray",graphic="@"}, -- 2
- {name="Sand",color="white",bcolor="yellow",graphic="+"}, -- 3
- {name="Scifi",color="purple",bcolor="blue",graphic="0"}, -- 4
- {name="Grass",color="green",bcolor="brown",graphic="*"}, -- 5
- {name="Iron",color="white",bcolor="lightGray",graphic="/"}, -- 6
- {name="Gravel",color="white",bcolor="brown",graphic="G"}, -- 7
- {name="Quartz",color="lightGray",bcolor="white",graphic="X"}, -- 8
- {name="LOL",color="black",bcolor="white",graphic="O"}, -- 9
- {name="LAL",color="black",bcolor="white",graphic="?"}, -- 9
- }
- local sMenu = {
- {name="Save",run=function()
- term.setCursorPos(1,h)
- term.clearLine() write("Save As: ")
- sa=read()
- f = fs.open(sa,"w")
- --for i = 1, #map do
- -- f.writeLine(table.concat(map[i]))
- -- end
- f.write(textutils.serialize(map):gsub("\n%s*",""))
- f.close()
- term.setCursorPos(1,h)
- term.clearLine() write("Saved As "..sa)
- drawMap()
- inSave=false
- end};
- {name="Load",run=function()
- term.setCursorPos(1,h)
- term.clearLine()
- write("Load: ")
- sa=read()
- if(fs.exists(sa) and sa ~= "" and sa ~= " ")then
- f = fs.open(sa,"r")
- smap = f.readAll()
- f.close()
- if(type(smap) ~= "table")then
- term.setCursorPos(1,h)
- term.clearLine()
- write("Not a valid save file!")
- os.pullEvent("key")
- else
- term.setCursorPos(1,h)
- term.clearLine()
- map=textutils.unserialize(smap)
- write("Save loaded "..sa)
- os.pullEvent("key")
- end
- -- for line in f.readLine do
- -- map[#map+1] = {}
- -- for i = 1, #line do
- -- local l = line:sub(i,i)
- -- map[#map][i] = tonumber(l) or tostring(l)
- -- end
- -- end
- else
- term.setCursorPos(1,h)
- term.clearLine()
- write("File Not Found - "..sa)
- os.pullEvent("key")
- end
- inSave=false
- drawMap()
- end};
- -- {name="Play",run=function()
- -- sPlay=true
- -- inSave=false
- -- end};
- {name="Load Mod",run=function()
- term.setCursorPos(1,h)
- term.clearLine()
- write("Mod File (.bldmd): ")
- sa=read()
- local md = {}
- f = fs.open(sa,"r")
- con = textutils.unserialize(f.readAll())
- --con=f.readAll()
- f.close()
- if fs.exists(sa) then
- write("Loading....")
- --oldblks = blocks
- --blocks = setmetatable(con, {__index = blocks})
- for i = 1, #con do
- table.insert(blocks,con[i])
- end
- --blocks = oldblks
- term.setCursorPos(1,h)
- term.clearLine()
- term.setCursorPos(1,h)
- write("Mod Loaded! ")
- os.pullEvent("key")
- else
- term.clearLine()
- term.setCursorPos(1,h)
- write("Mod Invalid or Not Found - "..sa)
- os.pullEvent("key")
- end
- inSave=false
- drawMap()
- end};
- {name="Exit",run=function() inGame = false
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1,1)
- end};
- }
- function drawMenu()
- term.setCursorPos(1,h)
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.gray)
- if(not inSave)then
- if(selBlock < 8)then mCalc = 1 else mCalc = (selBlock-8)+1 end
- for i = mCalc, (mCalc-1)+8 do
- if(i ~= selBlock)then
- write("[")
- term.setTextColor(colors[blocks[i].color])
- term.setBackgroundColor(colors[blocks[i].bcolor])
- write(blocks[i].graphic)
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.gray)
- write("] ")
- else
- write(">")
- term.setTextColor(colors[blocks[i].color])
- term.setBackgroundColor(colors[blocks[i].bcolor])
- write(blocks[i].graphic)
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.gray)
- write("< ")
- end
- end
- term.setTextColor(colors.yellow)
- write("Press Ctrl For Menu")
- end
- if(inSave)then
- term.setCursorPos(1,h)
- term.clearLine()
- term.setTextColor(colors.white)
- for i = 1, #sMenu do
- if(i == sMenuIndex)then
- term.setTextColor(colors.yellow)
- write("[")
- term.setTextColor(colors.white)
- write(sMenu[i].name)
- term.setTextColor(colors.yellow)
- write("]")
- else
- term.setTextColor(colors.yellow)
- write(" ")
- term.setTextColor(colors.white)
- write(sMenu[i].name)
- term.setTextColor(colors.yellow)
- write(" ")
- end
- term.setTextColor(colors.white)
- end
- end
- end
- function buildMap(w,h)
- for i = 1, h do
- map[i] = {}
- for c = 1, w do
- map[i][c] = 0
- end
- end
- end
- function drawMap()
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- for i = 1, #map do
- for c = 1, #map[i] do
- term.setTextColor(colors[blocks[map[i][c]].color])
- term.setBackgroundColor(colors[blocks[map[i][c]].bcolor])
- write(blocks[map[i][c]].graphic)
- end
- write("\n")
- end
- end
- function isPlayer()
- local is = false
- for i = 1, #map do
- for c = 1, #map[i] do
- if(map[i][c] == tonumber(4))then
- is=true
- end
- end
- end
- return is
- end
- function getPlayerX()
- local fx = 1
- for i = 1, #map do
- for c = 1, #map[i] do
- if(map[i][c] == tonumber(4))then
- fx=i
- end
- end
- end
- return fx
- end
- function getPlayerY()
- local fy = 1
- for i = 1, #map do
- for c = 1, #map[i] do
- if(map[i][c] == tonumber(4))then
- fy=c
- end
- end
- end
- return fy
- end
- function drawMapBlock(x,y)
- term.setCursorPos(tonumber(y),tonumber(x))
- term.setTextColor(colors[blocks[map[x][y]].color])
- term.setBackgroundColor(colors[blocks[map[x][y]].bcolor])
- print(blocks[map[x][y]].graphic)
- end
- function placeBlock(id,x,y)
- for i = 1, #blocks do
- if(i == id)then
- map[x][y] = id
- end
- end
- end
- function breakBlock(x,y)
- map[x][y] = 0
- end
- buildMap(w,h-1)
- drawMap()
- function update()
- drawMenu()
- a = {os.pullEvent()}
- term.setCursorPos(1,1)
- --if(a[1]=="timer" and not sPlay)then gTimer=os.startTimer(fps) end
- if( (a[1] == "mouse_click" and a[4] < h) or (a[1] == "mouse_drag" and a[4] < h) and not inSave)then
- if(a[2] == 1)then
- placeBlock(selBlock,a[4],a[3])
- drawMapBlock(a[4],a[3])
- elseif(a[2] == 2)then
- breakBlock(a[4],a[3])
- drawMapBlock(a[4],a[3])
- end
- elseif(a[1] == "mouse_scroll" and not inSave )then
- if(a[2] == 1 and selBlock < #blocks)then
- selBlock = selBlock + 1
- elseif(a[2] == -1 and selBlock > 1) then
- selBlock = selBlock - 1
- end
- elseif(a[1] == "key")then
- if(a[2] == keys.enter and inSave)then sMenu[sMenuIndex].run() end
- if(a[2] == 29)then inSave = not inSave end
- if(a[2] == keys.left and sMenuIndex > 1 and inSave)then
- sMenuIndex = sMenuIndex - 1
- elseif(a[2] == keys.right and sMenuIndex < #sMenu and inSave)then
- sMenuIndex = sMenuIndex + 1
- end
- end
- end
- while inGame do
- update()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement