Advertisement
billysback

3DPrinter

Oct 28th, 2012
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.10 KB | None | 0 0
  1. local root = ".3DD/"
  2.  
  3. local ps = {}
  4.  
  5. local points = {}
  6. local mdim = {}
  7.  
  8. local converter = {}
  9.  
  10. function split(str, pat)
  11.     local t = {}  -- NOTE: use {n = 0} in Lua-5.0
  12.     if str ~= nil then
  13.        local fpat = "(.-)" .. pat
  14.        local last_end = 1
  15.        local s, e, cap = str:find(fpat, 1)
  16.        while s do
  17.           if s ~= 1 or cap ~= "" then
  18.          table.insert(t,cap)
  19.           end
  20.           last_end = e+1
  21.           s, e, cap = str:find(fpat, last_end)
  22.        end
  23.        if last_end <= #str then
  24.           cap = str:sub(last_end)
  25.           table.insert(t, cap)
  26.        end
  27.     else
  28.         print("##3DD ERROR failed to split ["..str.."] by:"..pat)
  29.     end
  30.     return t
  31. end
  32.  
  33. local function open(name)
  34.     local nps = {}
  35.     if fs.exists(root..name) then
  36.         local file = fs.open(root..name, "r")
  37.         local line = file.readLine()
  38.         while line ~= nil do
  39.             local splt = split(line, ",")
  40.             if splt[1] == "MDIM" then
  41.                 local mx = tonumber(splt[2])
  42.                 local my = tonumber(splt[3])
  43.                 local mz = tonumber(splt[4])
  44.                 mdim = {mx, my, mz}
  45.             else
  46.                 local x = tonumber(splt[1])
  47.                 local y = tonumber(splt[2])
  48.                 local z = tonumber(splt[3])
  49.                 local str = splt[4]
  50.                 if str == "#NIL" then str = nil end
  51.                 nps[#nps + 1] = {x, y, z, str}
  52.             end
  53.             line = file.readLine()
  54.         end
  55.         file.close()
  56.         ps = nps
  57.         return true
  58.     else
  59.         return false
  60.     end
  61. end
  62.  
  63. local function convert(pts, dim)
  64.     mdim = dim
  65.     for i=1,#tps do
  66.         local tabl = tps[i]
  67.         local x = tabl[1]
  68.         local y = tabl[2]
  69.         local z = tabl[3]
  70.         local str = tabl[4]
  71.         if points[x] == nil then points[x] = {} end
  72.         if points[x][y] == nil then points[x][y] = {} end
  73.         points[x][y][z] = str
  74.     end
  75. end
  76.  
  77. local function dig()
  78.     while turtle.detect() do
  79.         turtle.dig()
  80.         sleep(0.25)
  81.     end
  82. end
  83.  
  84. local function place(type)
  85.     local slot = 1
  86.     for i=1,#converter do
  87.         local table = converter[i]
  88.         if table[1] == type then slot = table[2] end
  89.     end
  90.     if slot > 9 then slot = 9 end
  91.     if slot < 1 then slot = 1 end
  92.     turtle.select(slot)
  93.    
  94.     dig()
  95.    
  96.     if type ~= nil then
  97.         if turtle.detectDown() then turtle.digDown() end
  98.         turtle.placeDown()
  99.     end
  100. end
  101.  
  102. local function printBLP()
  103.     local left = true
  104.     for y=1,mdim[2] do
  105.         for x=1,mdim[1] do
  106.            
  107.             for z=1,mdim[3] do
  108.                 local str = point[x][y][z]
  109.                 place(str)
  110.                 turtle.forwards()
  111.             end
  112.             if left then
  113.                 turtle.turnLeft()
  114.                 dig()
  115.                 turtle.forward()
  116.                 turtle.turnLeft()
  117.                 left = false
  118.             else
  119.                 turtle.turnRight()
  120.                 dig()
  121.                 turtle.forward()
  122.                 turtle.turnRight()
  123.                 left = true
  124.             end
  125.         end
  126.         turtle.up()
  127.     end
  128. end
  129.  
  130. local function start()
  131.     print("What is the name of the project you wish to build?")
  132.     local name = io.read()
  133.     print("Enter the format (character:slot), seperate with space:")
  134.     local formater = io.read()
  135.     local splt = split(formater, " ")
  136.     for i=1,#splt do
  137.         converter[#converter + 1] = {splt[1], splt[2]}
  138.     end
  139.    
  140.     print("Opening blueprint ("..name..") ...")
  141.     if not open(name) then
  142.         print("Blueprint "..name.." does not exist!")
  143.     else
  144.         print("Converting blueprint...")
  145.         convert(ps, mdim)
  146.         print("Printing blueprint...")
  147.         printBLP()
  148.         print("Printed blueprint.")
  149.     end
  150. end
  151.  
  152. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement