Advertisement
Carbon02

Printer

Jun 7th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 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. ------------------------------------
  24. -- Printing Functions
  25. ------------------------------------
  26.  
  27. function printMap(_pmap)
  28. for i=0, #_pmap do
  29. local block = _pmap:sub(i,i)
  30. if block ~= "-" then
  31. if tonumber(block) == nil then
  32. error("NIL:"..block)
  33. end
  34. local slot = getAvailableMaterialSlot(tonumber(block))
  35.  
  36. if slot ~= 0 then
  37. turtle.select(slot)
  38. turtle.placeDown()
  39. end
  40. end
  41.  
  42. move()
  43. end
  44. end
  45.  
  46. function move()
  47. if x == maxX and z == maxZ then
  48. -- if at corner, move to the start and go up.
  49. goToStartXZ()
  50. up()
  51. elseif x == maxX then
  52. if xDir > 0 then
  53. turnLeft()
  54. else
  55. turnRight()
  56. end
  57.  
  58. forward()
  59. else
  60. forward()
  61. end
  62. end
  63.  
  64. ------------------------------------
  65. -- Utility Functions
  66. ------------------------------------
  67.  
  68. function hasFuel(sx, sy, sz)
  69. return getFuelRequired(sx, sy, sz) <= turtle.getFuelLevel()
  70. end
  71.  
  72. function getFuelRequired(sx, sy, sz)
  73. return sx*sy*sz
  74. end
  75.  
  76. function getAvailableMaterialSlot(mat)
  77. print("MAT:"..mat)
  78. for i=0, 3 do
  79. if turtle.getItemCount(mat + 4*i) > 0 then
  80. return mat + 4*i
  81. end
  82. end
  83.  
  84. return 0
  85. end
  86.  
  87. ------------------------------------
  88. -- File-Related Functions
  89. ------------------------------------
  90.  
  91. function load_file(file)
  92. local f = fs.open(file, "r")
  93. local line = nil
  94. local pmp = ""
  95.  
  96. -- Header is in format [mode, width, depth, height]
  97. -- Possible modes are:
  98. -- 0: Normal. Read the map as usual, and print.
  99. -- 1: Fill. Generate a map using one block.
  100. -- 2: Pattern. Repeat first lines depth times, and do that height times. WIP.
  101. local header = split(f.readLine(), ",")
  102.  
  103. -- convert them all to numbers
  104. for i=1, #header do
  105. header[i] = tonumber(header[i])
  106. end
  107.  
  108. local mode = header[1]
  109.  
  110. if not hasFuel(header[2], header[3], header[4]) then
  111. 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.")
  112. end
  113.  
  114. if mode == 0 then
  115. -- Normal mode, just read the file and print
  116. local linec = 0 -- line count
  117. local pagec = 0 -- page count
  118.  
  119. repeat
  120. line = f.readLine()
  121. if line == nil then break end
  122.  
  123. if linec+1 > header[3] then
  124. linec = 0
  125. pagec = pagec + 1
  126. else
  127. linec = linec+1
  128. end
  129.  
  130. if #line ~= header[2] then
  131. error("Mismached line width on line "..linec.." of page "..pagec..".")
  132. elseif line == nil then
  133. error("Line "..linec.." is null")
  134. end
  135.  
  136. pmp = pmp..line -- append the line in the file to the map
  137. until line == nil
  138. elseif mode == 1 then
  139. -- TODO
  140. elseif mode == 2 then
  141. error("Pattern mode isn't supported yet!")
  142. end
  143.  
  144. print("Map of size ".. string.len(pmp) .." loaded.")
  145. f.close()
  146.  
  147. parse(pmp)
  148.  
  149. maxX = header[2]
  150. maxY = header[3]
  151. maxZ = header[4]
  152.  
  153. return pmp
  154. end
  155.  
  156. function parse(mp)
  157. print("Parsing map...")
  158. for i=1, #mp do
  159. local cc = mp:sub(i,i)
  160.  
  161. if not (cc == '-') and (tonumber(cc) == nil or tonumber(cc) < 1 or tonumber(cc) > 4) then
  162. error("invalid character at map position "..i..".")
  163. end
  164. end
  165. print("Map is valid.")
  166. end
  167.  
  168.  
  169. function split(str, spl)
  170. local arr = {}
  171. local cs = ""
  172.  
  173. for i=0, #str do
  174. if str:sub(i,i) == spl then
  175. table.insert(arr, cs)
  176. cs = ""
  177. else
  178. cs = cs..str:sub(i,i)
  179. end
  180. end
  181.  
  182. if #cs > 0 then
  183. table.insert(arr, cs)
  184. end
  185.  
  186. return arr
  187. end
  188.  
  189. ------------------------------------
  190. -- Advanced Movement Functions
  191. ------------------------------------
  192.  
  193. function goToStartXZ()
  194. while xDir > -1 do
  195. turnLeft()
  196. end
  197.  
  198. while x > 0 do forward() end
  199.  
  200. while zDir > -1 do
  201. turnLeft()
  202. end
  203.  
  204. while z > 0 do forward() end
  205. end
  206.  
  207. ------------------------------------
  208. -- Movement Functions
  209. ------------------------------------
  210. function forward()
  211. while not turtle.forward() do
  212. turtle.dig()
  213. end
  214.  
  215. x = x + xDir
  216. z = z + zDir
  217. end
  218.  
  219. function up()
  220. while not turtle.up() do
  221. turtle.digUp()
  222. end
  223.  
  224. y = y+1
  225. end
  226.  
  227. function down()
  228. while not turtle.down() do
  229. turtle.digDown()
  230. end
  231.  
  232. y = y-1
  233. end
  234.  
  235. function turnLeft()
  236. turtle.turnLeft()
  237. xDit, zDir = -zDir, xDir
  238. end
  239.  
  240. function turnRight()
  241. turtle.turnRight()
  242. xDir, zDir = zDir, -xDir
  243. end
  244.  
  245.  
  246.  
  247.  
  248.  
  249. -- Startup
  250.  
  251.  
  252.  
  253.  
  254. local pmap = load_file(filename)
  255.  
  256. printMap(pmap)
  257.  
  258. print("Finished printing map.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement