Advertisement
Carbon02

myrstartup

Jun 7th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.47 KB | None | 0 0
  1. -- 3d printer code
  2. -- By: Carbon
  3. -------------------
  4.  
  5. local args = {...}
  6.  
  7. local filename = args[1]
  8.  
  9. if not fs.exists(filename) then
  10.     error("Couldn't find the printer file!")
  11. end
  12.  
  13. local x = 0
  14. local y = 0
  15. local z = 0
  16.  
  17. local maxX = 0
  18. local maxY = 0
  19. local maxZ = 0
  20.  
  21. local xDir, zDir = 0, 1
  22.  
  23. local pmap = load_file(filename)
  24.  
  25.  
  26. ------------------------------------
  27. -- Printing Functions
  28. ------------------------------------
  29.  
  30. function printMap()
  31.     for block in pmap do
  32.         local slot = getAvailableMaterialSlot(tonumber(block))
  33.  
  34.         if slot ~= 0 then
  35.             turtle.select(slot)
  36.             turtle.placeDown()
  37.         end
  38.  
  39.         move()
  40.     end
  41. end
  42.  
  43. function move()
  44.     if x == maxX and z == maxZ then
  45.         -- if at corner, move to the start and go up.
  46.         goToStartXZ()
  47.         up()
  48.     elseif
  49.  
  50. end
  51.  
  52. ------------------------------------
  53. -- Utility Functions
  54. ------------------------------------
  55.  
  56. function hasFuel(sx, sy, sz)
  57.     return getFuelRequired(sx, sy, sz) <= turtle.getFuelLevel()
  58. end
  59.  
  60. function getFuelRequired(sx, sy, sz)
  61.     return sx*sy*sz
  62. end
  63.  
  64. function getAvailableMaterialSlot(mat)
  65.     for i=0, 3 do
  66.         if turtle.getItemCount(mat + 4*i) > 0 then
  67.             return mat + 4*i
  68.         end
  69.     end
  70.  
  71.     return 0
  72. end
  73.  
  74. ------------------------------------
  75. -- File-Related Functions
  76. ------------------------------------
  77.  
  78. function load_file(file)
  79.     local f = fs.open(file, "r")
  80.     local line = nil
  81.     local pmp = ""
  82.  
  83.     -- Header is in format [mode, width, depth, height]
  84.     -- Possible modes are:
  85.     -- 0: Normal. Read the map as usual, and print.
  86.     -- 1: Fill. Generate a map using one block.
  87.     -- 2: Pattern. Repeat first lines depth times, and do that height times. WIP.
  88.     local header = split(f.readLine(), ",")  
  89.  
  90.     -- convert them all to numbers
  91.     for i=1, #header do
  92.         header[i] = tonumber(header[i])
  93.     end
  94.  
  95.     local mode = header[1]
  96.  
  97.     if not hasFuel(header[2], header[3], header[4]) then
  98.         error("You don't have enough fuel to print this!\nFuel Required: "..getFuelRequired(header[2], header[3], header[4]).."\nFuel stored: "..turtle.getFuelLevel().."\nTurle needs "..getFuelRequired() - turtle.getFuelLevel().." more fuel units to print this.")
  99.     end
  100.  
  101.     if mode == 0 then
  102.         -- Normal mode, just read the file and print
  103.         local linec = 0  -- line count
  104.         local pagec = 0  -- page count
  105.  
  106.         repeat
  107.             line = f.readLine()
  108.            
  109.             if linec+1 > header[3] then
  110.                 linec = 0
  111.                 pagec = pagec + 1
  112.             else
  113.                 linec = linec+1
  114.             end
  115.  
  116.             if string.len(line) ~= header[2] then
  117.                 error("Mismached line width on line "..linec.." of page "..pagec..".")
  118.             elseif line == nil then
  119.                 error("Line "..linec.." is null")
  120.             end
  121.  
  122.             pmp = pmp..line  -- append the line in the file to the map
  123.         until line == nil
  124.     elseif mode == 1 then
  125.         -- TODO
  126.     elseif mode == 2 then
  127.         error("Pattern mode isn't supported yet!")
  128.     end
  129.  
  130.     print("Map of size ".. string.len(pmp) .." loaded.")
  131.     f.close()
  132.  
  133.     parse(pmp)
  134.  
  135.     maxX = header[2]
  136.     maxY = header[3]
  137.     maxZ = header[4]
  138.  
  139.     return pmp
  140. end
  141.  
  142. function parse(mp)
  143.     print("Parsing map...")
  144.     for i in mp do
  145.         if not (i == '-') and (tonumber(i) == nil or tonumber(i) < 1 or tonumber(i) > 4) then
  146.             error("invalid character at map position "..i..".")
  147.     end
  148.     print("Map is valid.")
  149. end
  150.  
  151.  
  152. ------------------------------------
  153. -- Advanced Movement Functions
  154. ------------------------------------
  155.  
  156. function goToStartXZ()
  157.     while xDir > -1 do
  158.         turnLeft()
  159.     end
  160.  
  161.     while x > 0 do forward() end
  162.  
  163.     while zDir > -1 do
  164.         turnLeft()
  165.     end
  166.  
  167.     while z > 0 do forward() end
  168. end
  169.  
  170. ------------------------------------
  171. -- Movement Functions
  172. ------------------------------------
  173. function forward()
  174.     while not turtle.forward() do
  175.         turtle.dig()
  176.     end
  177.  
  178.     x = x + xDir
  179.     z = z + zDir
  180. end
  181.  
  182. function up()
  183.     while not turtle.up() do
  184.         turtle.digUp()
  185.     end
  186.  
  187.     y = y+1
  188. end
  189.  
  190. function down()
  191.     while not turtle.down() do
  192.         turtle.digDown()
  193.     end
  194.  
  195.     y = y-1
  196. end
  197.  
  198. function turnLeft()
  199.     turtle.turnLeft()
  200.     xDit, zDir = -zDir, xDir
  201. end
  202.  
  203. function turnRight()
  204.     turtle.turnRight()
  205.     xDir, zDir = zDir, -xDir
  206. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement