Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- 3d printer code
- -- By: Carbon
- -------------------
- local args = {...}
- local filename = args[1]
- if not fs.exists(filename) then
- error("Couldn't find the printer file!")
- end
- local x = 0
- local y = 0
- local z = 0
- local maxX = 0
- local maxY = 0
- local maxZ = 0
- local xDir, zDir = 0, 1
- local pmap = load_file(filename)
- ------------------------------------
- -- Printing Functions
- ------------------------------------
- function printMap()
- for block in pmap do
- local slot = getAvailableMaterialSlot(tonumber(block))
- if slot ~= 0 then
- turtle.select(slot)
- turtle.placeDown()
- end
- move()
- end
- end
- function move()
- if x == maxX and z == maxZ then
- -- if at corner, move to the start and go up.
- goToStartXZ()
- up()
- elseif
- end
- ------------------------------------
- -- Utility Functions
- ------------------------------------
- function hasFuel(sx, sy, sz)
- return getFuelRequired(sx, sy, sz) <= turtle.getFuelLevel()
- end
- function getFuelRequired(sx, sy, sz)
- return sx*sy*sz
- end
- function getAvailableMaterialSlot(mat)
- for i=0, 3 do
- if turtle.getItemCount(mat + 4*i) > 0 then
- return mat + 4*i
- end
- end
- return 0
- end
- ------------------------------------
- -- File-Related Functions
- ------------------------------------
- function load_file(file)
- local f = fs.open(file, "r")
- local line = nil
- local pmp = ""
- -- Header is in format [mode, width, depth, height]
- -- Possible modes are:
- -- 0: Normal. Read the map as usual, and print.
- -- 1: Fill. Generate a map using one block.
- -- 2: Pattern. Repeat first lines depth times, and do that height times. WIP.
- local header = split(f.readLine(), ",")
- -- convert them all to numbers
- for i=1, #header do
- header[i] = tonumber(header[i])
- end
- local mode = header[1]
- if not hasFuel(header[2], header[3], header[4]) then
- 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.")
- end
- if mode == 0 then
- -- Normal mode, just read the file and print
- local linec = 0 -- line count
- local pagec = 0 -- page count
- repeat
- line = f.readLine()
- if linec+1 > header[3] then
- linec = 0
- pagec = pagec + 1
- else
- linec = linec+1
- end
- if string.len(line) ~= header[2] then
- error("Mismached line width on line "..linec.." of page "..pagec..".")
- elseif line == nil then
- error("Line "..linec.." is null")
- end
- pmp = pmp..line -- append the line in the file to the map
- until line == nil
- elseif mode == 1 then
- -- TODO
- elseif mode == 2 then
- error("Pattern mode isn't supported yet!")
- end
- print("Map of size ".. string.len(pmp) .." loaded.")
- f.close()
- parse(pmp)
- maxX = header[2]
- maxY = header[3]
- maxZ = header[4]
- return pmp
- end
- function parse(mp)
- print("Parsing map...")
- for i in mp do
- if not (i == '-') and (tonumber(i) == nil or tonumber(i) < 1 or tonumber(i) > 4) then
- error("invalid character at map position "..i..".")
- end
- print("Map is valid.")
- end
- ------------------------------------
- -- Advanced Movement Functions
- ------------------------------------
- function goToStartXZ()
- while xDir > -1 do
- turnLeft()
- end
- while x > 0 do forward() end
- while zDir > -1 do
- turnLeft()
- end
- while z > 0 do forward() end
- end
- ------------------------------------
- -- Movement Functions
- ------------------------------------
- function forward()
- while not turtle.forward() do
- turtle.dig()
- end
- x = x + xDir
- z = z + zDir
- end
- function up()
- while not turtle.up() do
- turtle.digUp()
- end
- y = y+1
- end
- function down()
- while not turtle.down() do
- turtle.digDown()
- end
- y = y-1
- end
- function turnLeft()
- turtle.turnLeft()
- xDit, zDir = -zDir, xDir
- end
- function turnRight()
- turtle.turnRight()
- xDir, zDir = zDir, -xDir
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement