Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------------------------------------------
- -- Vrill's 3D scaner for '3dprinter.lua' prog. --
- -------------------------------------------------
- function createSchemeFile(filename,slotsNameTable,sX,sY,sZ,matrix)
- local i,x,y,z
- local file
- file = io.open(filename, "w")
- file:write("3dp -> 3D printer scheme file for ComputerCraft turtle\n")
- file:write("title = '")
- file:write(filename)
- file:write("'\n")
- for i=1,16 do
- file:write("slot")
- file:write(tostring(i))
- file:write(" = '")
- file:write(slotsNameTable[i])
- file:write("'\n")
- end
- file:write("sizeX = ")
- file:write(tostring(sX))
- file:write("\n")
- file:write("sizeY = ")
- file:write(tostring(sY))
- file:write("\n")
- file:write("sizeZ = ")
- file:write(tostring(sZ))
- file:write("\n")
- file:write("--Use 0123456789ABCDEFG chars --\n")
- for z=1,sZ do
- file:write("--layer")
- file:write(tostring(z))
- file:write("--\n")
- for y=1,sY do
- for x=1,sX do
- if matrix[z][sY-y+1][x] >=0 and matrix[z][sY-y+1][x] <=9 then
- file:write(tostring(matrix[z][sY-y+1][x]))
- elseif matrix[z][sY-y+1][x] == 10 then
- file:write("A")
- elseif matrix[z][sY-y+1][x] == 11 then
- file:write("B")
- elseif matrix[z][sY-y+1][x] == 12 then
- file:write("C")
- elseif matrix[z][sY-y+1][x] == 13 then
- file:write("D")
- elseif matrix[z][sY-y+1][x] == 14 then
- file:write("E")
- elseif matrix[z][sY-y+1][x] == 15 then
- file:write("F")
- elseif matrix[z][sY-y+1][x] == 16 then
- file:write("G")
- end
- end
- file:write("\n")
- end
- end
- file:close()
- end
- function forceMoveForward()
- local can_move=true
- can_move=turtle.forward()
- while not can_move do
- --print("ERR: There is a something on my way!")
- sleep(0.5)
- --
- turtle.dig()
- turtle.attack()
- --
- can_move=turtle.forward()
- end
- end
- function forceMoveBack()
- local can_move=true
- can_move=turtle.back()
- while not can_move do
- --print("ERR: There is a something on my way!")
- sleep(0.5)
- can_move=turtle.back()
- end
- end
- function forceMoveUp()
- local can_move=true
- can_move=turtle.up()
- while not can_move do
- --print("ERR: There is a something on my way!")
- sleep(0.5)
- --
- turtle.digUp()
- turtle.attackUp()
- --
- can_move=turtle.up()
- end
- end
- function forceMoveDown()
- local can_move=true
- can_move=turtle.down()
- while not can_move do
- --print("ERR: There is a something on my way!")
- sleep(0.5)
- --
- turtle.digDown()
- turtle.attackDown()
- --
- can_move=turtle.down()
- end
- end
- function left()
- if tD[1]==1 and tD[2]==0 then
- tD[1]=0
- tD[2]=-1
- elseif tD[1]==0 and tD[2]==-1 then
- tD[1]=-1
- tD[2]=0
- elseif tD[1]==-1 and tD[2]==0 then
- tD[1]=0
- tD[2]=1
- elseif tD[1]==0 and tD[2]==1 then
- tD[1]=1
- tD[2]=0
- end
- turtle.turnLeft()
- end
- function right()
- if tD[1]==1 and tD[2]==0 then
- tD[1]=0
- tD[2]=1
- elseif tD[1]==0 and tD[2]==1 then
- tD[1]=-1
- tD[2]=0
- elseif tD[1]==-1 and tD[2]==0 then
- tD[1]=0
- tD[2]=-1
- elseif tD[1]==0 and tD[2]==-1 then
- tD[1]=1
- tD[2]=0
- end
- turtle.turnRight()
- end
- function turnL(num1,num2)
- while tD[1]~=num1 or tD[2]~=num2 do
- left()
- end
- end
- function turnR(num1,num2)
- while tD[1]~=num1 or tD[2]~=num2 do
- right()
- end
- end
- function turnB(num1,num2)
- if math.abs(num1-tD[1])==2 then
- turnR(num1,num2)
- elseif math.abs(num2-tD[2])==2 then
- turnL(num1,num2)
- elseif tD[1]==1 then
- if num2==1 then
- turnR(num1,num2)
- elseif num2==-1 then
- turnL(num1,num2)
- end
- elseif tD[1]==-1 then
- if num2==-1 then
- turnR(num1,num2)
- elseif num2==1 then
- turnL(num1,num2)
- end
- elseif tD[2]==1 then
- if num1==-1 then
- turnR(num1,num2)
- elseif num1==1 then
- turnL(num1,num2)
- end
- elseif tD[2]==-1 then
- if num1==1 then
- turnR(num1,num2)
- elseif num1==-1 then
- turnL(num1,num2)
- end
- end
- end
- function moveXYZ(lx,ly,lz)
- local i
- if tZ<lz then
- for i=1,lz-tZ do
- forceMoveUp()
- end
- else
- for i=1,tZ-lz do
- forceMoveDown()
- end
- end
- if tY<ly then
- turnB(1,0)
- for i=1,ly-tY do
- forceMoveForward()
- end
- elseif tY>ly then
- turnB(-1,0)
- for i=1,tY-ly do
- forceMoveForward()
- end
- end
- if tX<lx then
- turnB(0,1)
- for i=1,lx-tX do
- forceMoveForward()
- end
- elseif tX>lx then
- turnB(0,-1)
- for i=1,tX-lx do
- forceMoveForward()
- end
- end
- tX,tY,tZ=lx,ly,lz
- end
- function GUIinputFileName()
- local input
- local success="test.3dp"
- term.clear()
- term.setTextColor(colors.yellow)
- term.setCursorPos(1,6)
- term.write("+----------input file name:-----------+")
- term.setCursorPos(1,7)
- term.write("| |")
- term.setCursorPos(1,8)
- term.write("+-------------------------------------+")
- term.setTextColor(colors.white)
- term.setCursorPos(4,7)
- term.write("file name=")
- input = read()
- if input == nil then
- success=false
- else
- success=input
- end
- return success
- end
- function nameOnly(sName)
- local str,i,j
- str=sName
- i, j = string.find(str, ":")
- if i then
- str = string.sub(str,i+1)
- end
- return str
- end
- function updateMatrix(blockName,x,y,z)
- local i
- local success=false
- for i=1,16 do
- if blockName == slotName[i] then
- matr[z][y][x]=i
- success=true
- break
- end
- end
- if not success then
- for i=1,16 do
- if slotName[i] == "" then
- slotName[i] = blockName
- matr[z][y][x]=i
- success=true
- break
- end
- end
- end
- return success
- end
- function startScan()
- local x,y,z
- local i,j
- local success,data
- local str
- turtle.select(1)
- turtle.dropUp()
- for z=sizeZ,1,-1 do
- for y=1,sizeY do
- if math.fmod(y,2) == 1 then
- for x=1,sizeX do
- moveXYZ(x,y,z+1)
- success, data = turtle.inspectDown()
- if success then
- str = nameOnly(data.name) .. data.metadata
- updateMatrix(str,x,y,z)
- turtle.digDown()
- turtle.dropUp()
- end
- end
- else
- for x=sizeX,1,-1 do
- moveXYZ(x,y,z+1)
- success, data = turtle.inspectDown()
- if success then
- str = nameOnly(data.name) .. data.metadata
- updateMatrix(str,x,y,z)
- turtle.digDown()
- turtle.dropUp()
- end
- end
- end
- end
- end
- moveXYZ(1,0,1)
- turnB(1,0)
- end
- function GUIsuccessScan(fileName)
- term.clear()
- term.setTextColor(colors.yellow)
- term.setCursorPos(1,6)
- term.write("+-------------------------------------+")
- term.setCursorPos(1,7)
- term.write("| Scan successfully ended! |")
- term.setCursorPos(1,8)
- term.write("| |")
- term.setCursorPos(1,9)
- term.write("+-------------------------------------+")
- term.setTextColor(colors.lime)
- term.setCursorPos(2,8)
- term.write("saved into:" .. fileName)
- term.setTextColor(colors.white)
- term.setCursorPos(1,1)
- end
- -----------------------------------------
- --global variables:
- tX,tY,tZ=1,0,1
- tD={1,0}
- sizeX, sizeY, sizeZ = 1,1,1
- matr={}
- slotName={}
- --Locals:
- local args={...}
- local x,y,z
- local i,j
- local fileName="test.3dp"
- if #args<3 or args[1]==nil or args[2]==nil or args[3]==nil or args[1]=="?" then
- print("Usage: 3dscan <sizeX> <sizeY> <sizeZ>")
- print(" sizeX, sizeY, sizeZ >= 1")
- return
- end
- sizeX = tonumber(args[1])
- if sizeX==nil or sizeX<1 then sizeX=1 end
- sizeY = tonumber(args[2])
- if sizeY==nil or sizeY<1 then sizeY=1 end
- sizeZ = tonumber(args[3])
- if sizeZ==nil or sizeZ<1 then sizeZ=1 end
- for z=1,sizeZ do
- matr[z]={}
- for y=1,sizeY do
- matr[z][y]={}
- for x=1,sizeX do
- matr[z][y][x]=0
- end
- end
- end
- fileName=GUIinputFileName()
- if fileName==false then
- term.clear()
- term.setTextColor(colors.white)
- term.setCursorPos(1,1)
- print("INVALID file name!")
- return
- end
- i, j = string.find(fileName, ".3dp")
- if not i then
- fileName = fileName .. ".3dp"
- end
- for i=1,16 do
- slotName[i]=""
- end
- --Start Scan:--
- startScan()
- createSchemeFile(fileName,slotName,sizeX,sizeY,sizeZ,matr)
- GUIsuccessScan(fileName)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement