Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local root = ".3DD/"
- local cursor = {1, 1, 1}
- local psoff = {0, 0, 0}
- local ps = {}
- local mapdim = {}
- local width = 7
- local height = 7
- local pname = "example.tdd"
- function split(str, pat)
- local t = {} -- NOTE: use {n = 0} in Lua-5.0
- if str ~= nil then
- 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
- else
- print("##3DD ERROR failed to split ["..str.."] by:"..pat)
- end
- return t
- end
- local function generate(w, h, d)
- for x=1,w,1 do
- for y=1,h,1 do
- for z=1,d,1 do
- ps[#ps + 1] = {x,y,z,nil}
- end
- end
- end
- end
- local function save(name)
- if fs.exists(root..name) then fs.delete(root..name) end
- local file = fs.open(root..name, "a")
- file.writeLine("MDIM,"..mapdim[1]..","..mapdim[2]..","..mapdim[3])
- for i=1,#ps do
- local point = ps[i]
- local str = point[4]
- if str == nil then str = "#NIL" end
- local line = point[1]..","..point[2]..","..point[3]..","..str
- file.writeLine(line)
- end
- file.close()
- end
- local function open(name)
- local nps = {}
- if fs.exists(root..name) then
- local file = fs.open(root..name, "r")
- local line = file.readLine()
- while line ~= nil do
- local splt = split(line, ",")
- if splt[1] == "MDIM" then
- local mx = tonumber(splt[2])
- local my = tonumber(splt[3])
- local mz = tonumber(splt[4])
- mapdim = {mx, my, mz}
- else
- local x = tonumber(splt[1])
- local y = tonumber(splt[2])
- local z = tonumber(splt[3])
- local str = splt[4]
- if str == "#NIL" then str = nil end
- nps[#nps + 1] = {x, y, z, str}
- end
- line = file.readLine()
- end
- file.close()
- ps = nps
- else
- generate(6, 6, 6)
- end
- end
- local function compareTable(table, value, toindex, amt)
- local n = 0
- for i=1,#table do
- local val = table[i]
- local curok = true
- for j=1,toindex do
- if val[i] == value[j] then else
- curok = false
- end
- end
- if curok then n = n + 1 end
- end
- return n >= amt
- end
- local function printWindow(points, poff, offset, x, y, name)
- local valx = width
- local valy = width
- if x == 2 then valx = height end
- if y == 2 then valy = height end
- for i,v in ipairs(points) do
- if v[x] > poff[x] and v[x] < poff[x] + valx and v[y] > poff[y] and v[y] < poff[y] + valy then
- term.setCursorPos(offset[1]+(v[x]-poff[x]), offset[2]+(v[y]-poff[y]))
- local str = v[4]
- if str == nil then str = "." end
- --if compareTable(points, v, 3, 2) then else
- --if str == "." then else
- term.write(str)
- --end
- --end
- end
- end
- term.setCursorPos(offset[1], offset[2])
- print(name)
- term.setCursorPos(1,1)
- end
- local function draw(pnts, pntoff, curs, w, h, blink, interv)
- local points = {}
- for _,v in pairs(pnts) do points[#points + 1] = v end
- if blink then
- points[#points + 1] = {curs[1], curs[2], curs[3], "#"}
- end
- term.setCursorPos(1,1)
- local yo = 3
- local xo = 2
- print(" ("..interv..") X:"..curs[1].." Y:"..curs[2].." Z:"..curs[3])
- print("--------------------")
- printWindow(points, pntoff, {xo, yo}, 1, 2, "X:Y") --ignore z
- printWindow(points, pntoff, {(xo*2)+w, yo}, 1, 3, "X:Z") --ignore y
- printWindow(points, pntoff, {xo, (yo*2)+h-2}, 3, 2, "Z:Y") --ignore x
- end
- local function setPoint(x, y, z, str)
- local nps = {}
- for i,v in ipairs(ps) do
- local splt = v
- local nv = {v[1], v[2], v[3], v[4]}
- if splt[1] == x and splt[2] == y and splt[3] == z then else
- nps[#nps + 1] = nv
- end
- end
- ps = nps
- ps[#ps + 1] = {x, y, z, str}
- end
- local function removePoint(x, y, z)
- local nps = {}
- for i,v in ipairs(ps) do
- local splt = v
- if splt[1] == x and splt[2] == y and splt[3] == z then else
- nps[#nps + 1] = {x, y, z, nil}
- end
- nps[#nps + 1] = v
- end
- ps = nps
- end
- local function movePoint(x1, y1, z1, x2, y2, z2)
- local nps = {}
- for i,v in ipairs(ps) do
- local nv = v
- if splt[1] == x1 and splt[2] == y1 and splt[3] == z1 then else
- nps[i] = {x2, y2, z2, v[4]}
- end
- end
- ps = nps
- end
- local function openP(name)
- pname = name
- open(name)
- end
- local function createP(name)
- pname = name
- print("Width?")
- mapdim[1] = tonumber(io.read())
- print("Height?")
- mapdim[2] = tonumber(io.read())
- print("Depth?")
- mapdim[3] = tonumber(io.read())
- for i=1,3,1 do
- local val = width
- if i == 2 then val = height end
- val = val - 1
- if mapdim[i] < val then mapdim[i] = val end
- end
- generate(mapdim[1], mapdim[2], mapdim[3])
- end
- local function start()
- fs.makeDir(root)
- print("Enter command number:")
- print("(1)Create / (2)Open?")
- local inp = io.read()
- inp = string.lower(inp)
- print("Name of project?")
- local name = io.read()
- name = name..".tdd"
- if inp == "1" then
- createP(name)
- elseif inp == "2" then
- openP(name)
- else
- return false
- end
- return true
- end
- local function checkOffset()
- for i=1,3,1 do
- if psoff[i] < 0 then psoff[i] = 0 end
- if psoff[i] > mapdim[i] then psoff[i] = mapdim[i] end
- end
- end
- local function moveCursor(dirx, diry, dirz)
- cursor[1] = cursor[1] + dirx
- cursor[2] = cursor[2] + diry
- cursor[3] = cursor[3] + dirz
- for i=1,3,1 do
- local val = width
- if i == 2 then val = height end
- val = val - 1
- if cursor[i] < 1 then cursor[i] = 1 end
- if cursor[i] > mapdim[i] then cursor[i] = mapdim[i] end
- if cursor[i] > val + psoff[i] then psoff[i] = psoff[i] + 1 end
- if cursor[i] < psoff[i] then psoff[i] = psoff[i] - 1 end
- end
- checkOffset()
- end
- local on = start()
- local ok = true
- local force = false
- local timer = os.startTimer(0)
- local interval = 0.5
- while on do
- local event, p1, p2 = os.pullEvent()
- if event == "timer" and p1 == timer then
- timer = os.startTimer(interval)
- term.clear()
- term.setCursorPos(1,1)
- draw(ps, psoff, cursor, width, height, blink, interval)
- if blink then blink = false else blink = true end
- elseif event == "key" then
- if force then
- force = false
- else
- local key = p1
- if key == 200 then -- up
- moveCursor(0, -1, 0)
- elseif key == 203 then --left
- moveCursor(-1, 0, 0)
- elseif key == 208 then --down
- moveCursor(0, 1, 0)
- elseif key == 205 then --right
- moveCursor(1, 0, 0)
- elseif key == 79 then --scroll down
- ok = false
- moveCursor(0, 0, -1)
- elseif key == 82 then --scroll up
- ok = false
- moveCursor(0, 0, 1)
- elseif key == 80 then --force
- force = true
- ok = false
- elseif key == 74 then
- interval = interval - 0.05
- if interval < 0.05 then interval = 0.05 end
- elseif key == 78 then
- interval = interval + 0.05
- if interval >= 2 then interval = 2 end
- elseif key == 14 then --backspace
- removePoint(cursor[1], cursor[2], cursor[3])
- elseif key == 157 then --right control
- on = false
- elseif key == 28 or key == 156 then
- save(pname)
- end
- end
- elseif event == "char" then
- local char = p1
- if ok then
- setPoint(cursor[1], cursor[2], cursor[3], char)
- end
- ok = true
- end
- end
- term.clear()
- term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement