Advertisement
billysback

3DDrawer

Oct 27th, 2012
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.21 KB | None | 0 0
  1. local root = ".3DD/"
  2.  
  3. local cursor = {1, 1, 1}
  4. local psoff = {0, 0, 0}
  5. local ps = {}
  6.  
  7. local mapdim = {}
  8.  
  9. local width = 7
  10. local height = 7
  11.  
  12. local pname = "example.tdd"
  13.  
  14. function split(str, pat)
  15.     local t = {}  -- NOTE: use {n = 0} in Lua-5.0
  16.     if str ~= nil then
  17.        local fpat = "(.-)" .. pat
  18.        local last_end = 1
  19.        local s, e, cap = str:find(fpat, 1)
  20.        while s do
  21.           if s ~= 1 or cap ~= "" then
  22.          table.insert(t,cap)
  23.           end
  24.           last_end = e+1
  25.           s, e, cap = str:find(fpat, last_end)
  26.        end
  27.        if last_end <= #str then
  28.           cap = str:sub(last_end)
  29.           table.insert(t, cap)
  30.        end
  31.     else
  32.         print("##3DD ERROR failed to split ["..str.."] by:"..pat)
  33.     end
  34.     return t
  35. end
  36.  
  37. local function generate(w, h, d)
  38.     for x=1,w,1 do
  39.         for y=1,h,1 do
  40.             for z=1,d,1 do
  41.                 ps[#ps + 1] = {x,y,z,nil}
  42.             end
  43.         end
  44.     end
  45. end
  46.  
  47. local function save(name)
  48.    
  49.     if fs.exists(root..name) then fs.delete(root..name) end
  50.  
  51.     local file = fs.open(root..name, "a")
  52.     file.writeLine("MDIM,"..mapdim[1]..","..mapdim[2]..","..mapdim[3])
  53.     for i=1,#ps do
  54.         local point = ps[i]
  55.         local str = point[4]
  56.         if str == nil then str = "#NIL" end
  57.         local line = point[1]..","..point[2]..","..point[3]..","..str
  58.         file.writeLine(line)
  59.     end
  60.    
  61.     file.close()
  62. end
  63.  
  64. local function open(name)
  65.     local nps = {}
  66.     if fs.exists(root..name) then
  67.         local file = fs.open(root..name, "r")
  68.         local line = file.readLine()
  69.         while line ~= nil do
  70.             local splt = split(line, ",")
  71.             if splt[1] == "MDIM" then
  72.                 local mx = tonumber(splt[2])
  73.                 local my = tonumber(splt[3])
  74.                 local mz = tonumber(splt[4])
  75.                 mapdim = {mx, my, mz}
  76.             else
  77.                 local x = tonumber(splt[1])
  78.                 local y = tonumber(splt[2])
  79.                 local z = tonumber(splt[3])
  80.                 local str = splt[4]
  81.                 if str == "#NIL" then str = nil end
  82.                 nps[#nps + 1] = {x, y, z, str}
  83.             end
  84.             line = file.readLine()
  85.         end
  86.         file.close()
  87.         ps = nps
  88.     else
  89.         generate(6, 6, 6)
  90.     end
  91. end
  92.  
  93. local function compareTable(table, value, toindex, amt)
  94.     local n = 0
  95.     for i=1,#table do
  96.         local val = table[i]
  97.         local curok = true
  98.         for j=1,toindex do
  99.             if val[i] == value[j] then else
  100.                 curok = false
  101.             end
  102.         end
  103.         if curok then n = n + 1 end
  104.     end
  105.     return n >= amt
  106. end
  107.  
  108. local function printWindow(points, poff, offset, x, y, name)
  109.     local valx = width
  110.     local valy = width
  111.     if x == 2 then valx = height end
  112.     if y == 2 then valy = height end
  113.     for i,v in ipairs(points) do
  114.         if v[x] > poff[x] and v[x] < poff[x] + valx and v[y] > poff[y] and v[y] < poff[y] + valy then
  115.             term.setCursorPos(offset[1]+(v[x]-poff[x]), offset[2]+(v[y]-poff[y]))
  116.             local str = v[4]
  117.             if str == nil then str = "." end
  118.             --if compareTable(points, v, 3, 2) then else
  119.                 --if str == "." then else
  120.                     term.write(str)
  121.                 --end
  122.             --end
  123.         end
  124.     end
  125.     term.setCursorPos(offset[1], offset[2])
  126.     print(name)
  127.     term.setCursorPos(1,1)
  128. end
  129.  
  130. local function draw(pnts, pntoff, curs, w, h, blink, interv)
  131.     local points = {}
  132.     for _,v in pairs(pnts) do points[#points + 1] = v end
  133.     if blink then
  134.         points[#points + 1] = {curs[1], curs[2], curs[3], "#"}
  135.     end
  136.     term.setCursorPos(1,1)
  137.     local yo = 3
  138.     local xo = 2
  139.     print(" ("..interv..") X:"..curs[1].." Y:"..curs[2].." Z:"..curs[3])
  140.     print("--------------------")
  141.     printWindow(points, pntoff, {xo, yo}, 1, 2, "X:Y") --ignore z
  142.     printWindow(points, pntoff, {(xo*2)+w, yo}, 1, 3, "X:Z") --ignore y
  143.     printWindow(points, pntoff, {xo, (yo*2)+h-2}, 3, 2, "Z:Y") --ignore x
  144. end
  145.  
  146. local function setPoint(x, y, z, str)
  147.     local nps = {}
  148.     for i,v in ipairs(ps) do
  149.         local splt = v
  150.         local nv = {v[1], v[2], v[3], v[4]}
  151.         if splt[1] == x and splt[2] == y and splt[3] == z then else
  152.             nps[#nps + 1] = nv
  153.         end
  154.     end
  155.     ps = nps
  156.     ps[#ps + 1] = {x, y, z, str}
  157. end
  158.  
  159. local function removePoint(x, y, z)
  160.     local nps = {}
  161.     for i,v in ipairs(ps) do
  162.         local splt = v
  163.         if splt[1] == x and splt[2] == y and splt[3] == z then else
  164.             nps[#nps + 1] = {x, y, z, nil}
  165.         end
  166.         nps[#nps + 1] = v
  167.     end
  168.     ps = nps
  169. end
  170.  
  171. local function movePoint(x1, y1, z1, x2, y2, z2)
  172.     local nps = {}
  173.     for i,v in ipairs(ps) do
  174.         local nv = v
  175.         if splt[1] == x1 and splt[2] == y1 and splt[3] == z1 then else
  176.             nps[i] = {x2, y2, z2, v[4]}
  177.         end
  178.     end
  179.     ps = nps
  180. end
  181.  
  182.  
  183. local function openP(name)
  184.     pname = name
  185.     open(name)
  186. end
  187.  
  188. local function createP(name)
  189.     pname = name
  190.     print("Width?")
  191.     mapdim[1] = tonumber(io.read())
  192.     print("Height?")
  193.     mapdim[2] = tonumber(io.read())
  194.     print("Depth?")
  195.     mapdim[3] = tonumber(io.read())
  196.     for i=1,3,1 do
  197.         local val = width
  198.         if i == 2 then val = height end
  199.         val = val - 1
  200.         if mapdim[i] < val then mapdim[i] = val end
  201.     end
  202.     generate(mapdim[1], mapdim[2], mapdim[3])
  203. end
  204.  
  205. local function start()
  206.  
  207.     fs.makeDir(root)
  208.     print("Enter command number:")
  209.     print("(1)Create / (2)Open?")
  210.     local inp = io.read()
  211.     inp = string.lower(inp)
  212.    
  213.     print("Name of project?")
  214.     local name = io.read()
  215.     name = name..".tdd"
  216.    
  217.     if inp == "1" then
  218.         createP(name)
  219.     elseif inp == "2" then
  220.         openP(name)
  221.     else
  222.         return false
  223.     end
  224.     return true
  225. end
  226.  
  227. local function checkOffset()
  228.     for i=1,3,1 do
  229.        
  230.         if psoff[i] < 0 then psoff[i] = 0 end
  231.         if psoff[i] > mapdim[i] then psoff[i] = mapdim[i] end
  232.     end
  233. end
  234.  
  235. local function moveCursor(dirx, diry, dirz)
  236.     cursor[1] = cursor[1] + dirx
  237.     cursor[2] = cursor[2] + diry
  238.     cursor[3] = cursor[3] + dirz
  239.    
  240.     for i=1,3,1 do
  241.         local val = width
  242.         if i == 2 then val = height end
  243.        
  244.         val = val - 1
  245.        
  246.         if cursor[i] < 1 then cursor[i] = 1 end
  247.         if cursor[i] > mapdim[i] then cursor[i] = mapdim[i] end
  248.        
  249.         if cursor[i] > val + psoff[i] then psoff[i] = psoff[i] + 1 end
  250.         if cursor[i] < psoff[i] then psoff[i] = psoff[i] - 1 end
  251.     end
  252.    
  253.     checkOffset()
  254. end
  255.  
  256. local on = start()
  257. local ok = true
  258. local force = false
  259.  
  260. local timer = os.startTimer(0)
  261.  
  262. local interval = 0.5
  263. while on do
  264.     local event, p1, p2 = os.pullEvent()
  265.     if event == "timer" and p1 == timer then
  266.         timer = os.startTimer(interval)
  267.         term.clear()
  268.         term.setCursorPos(1,1)
  269.         draw(ps, psoff, cursor, width, height, blink, interval)
  270.         if blink then blink = false else blink = true end
  271.     elseif event == "key" then
  272.         if force then
  273.             force = false
  274.         else
  275.             local key = p1
  276.             if key == 200 then -- up
  277.                 moveCursor(0, -1, 0)
  278.             elseif key == 203 then --left
  279.                 moveCursor(-1, 0, 0)
  280.             elseif key == 208 then --down
  281.                 moveCursor(0, 1, 0)
  282.             elseif key == 205 then --right
  283.                 moveCursor(1, 0, 0)
  284.             elseif key == 79 then --scroll down
  285.                 ok = false
  286.                 moveCursor(0, 0, -1)
  287.             elseif key == 82 then --scroll up
  288.                 ok = false
  289.                 moveCursor(0, 0, 1)
  290.             elseif key == 80 then --force  
  291.                 force = true
  292.                 ok = false
  293.             elseif key == 74 then
  294.                 interval = interval - 0.05
  295.                 if interval < 0.05  then interval = 0.05 end
  296.             elseif key == 78 then
  297.                 interval = interval + 0.05
  298.                 if interval >= 2 then interval = 2 end
  299.             elseif key == 14 then --backspace  
  300.                 removePoint(cursor[1], cursor[2], cursor[3])
  301.             elseif key == 157 then --right control
  302.                 on = false
  303.             elseif key == 28 or key == 156 then
  304.                 save(pname)
  305.             end
  306.         end
  307.     elseif event == "char" then
  308.         local char = p1
  309.         if ok then
  310.             setPoint(cursor[1], cursor[2], cursor[3], char)
  311.         end
  312.         ok = true
  313.     end
  314. end
  315.  
  316. term.clear()
  317. term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement