Advertisement
Vrill

Computercraft: 3D scaner for turtle

Aug 28th, 2019
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.94 KB | None | 0 0
  1. -------------------------------------------------
  2. -- Vrill's 3D scaner for '3dprinter.lua' prog. --
  3. -------------------------------------------------
  4.  
  5. function createSchemeFile(filename,slotsNameTable,sX,sY,sZ,matrix)
  6. local i,x,y,z
  7. local file
  8.    
  9.     file = io.open(filename, "w")
  10.    
  11.     file:write("3dp -> 3D printer scheme file for ComputerCraft turtle\n")
  12.     file:write("title = '")
  13.     file:write(filename)
  14.     file:write("'\n")
  15.     for i=1,16 do
  16.         file:write("slot")
  17.         file:write(tostring(i))
  18.         file:write(" = '")
  19.         file:write(slotsNameTable[i])
  20.         file:write("'\n")
  21.     end
  22.    
  23.     file:write("sizeX = ")
  24.     file:write(tostring(sX))
  25.     file:write("\n")
  26.     file:write("sizeY = ")
  27.     file:write(tostring(sY))
  28.     file:write("\n")
  29.     file:write("sizeZ = ")
  30.     file:write(tostring(sZ))
  31.     file:write("\n")
  32.     file:write("--Use 0123456789ABCDEFG chars --\n")
  33.    
  34.     for z=1,sZ do
  35.         file:write("--layer")
  36.         file:write(tostring(z))
  37.         file:write("--\n")
  38.         for y=1,sY do
  39.             for x=1,sX do
  40.                 if matrix[z][sY-y+1][x] >=0 and matrix[z][sY-y+1][x] <=9 then
  41.                     file:write(tostring(matrix[z][sY-y+1][x]))
  42.                 elseif matrix[z][sY-y+1][x] == 10 then
  43.                     file:write("A")
  44.                 elseif matrix[z][sY-y+1][x] == 11 then
  45.                     file:write("B")
  46.                 elseif matrix[z][sY-y+1][x] == 12 then
  47.                     file:write("C")
  48.                 elseif matrix[z][sY-y+1][x] == 13 then
  49.                     file:write("D")
  50.                 elseif matrix[z][sY-y+1][x] == 14 then
  51.                     file:write("E")
  52.                 elseif matrix[z][sY-y+1][x] == 15 then
  53.                     file:write("F")
  54.                 elseif matrix[z][sY-y+1][x] == 16 then
  55.                     file:write("G")
  56.                 end
  57.             end
  58.             file:write("\n")
  59.         end
  60.     end
  61.    
  62.     file:close()  
  63. end
  64.  
  65. function forceMoveForward()
  66. local can_move=true
  67.     can_move=turtle.forward()
  68.     while not can_move do
  69.         --print("ERR: There is a something on my way!")
  70.         sleep(0.5)
  71.         --
  72.         turtle.dig()
  73.         turtle.attack()
  74.         --
  75.         can_move=turtle.forward()
  76.     end
  77. end
  78.  
  79. function forceMoveBack()
  80. local can_move=true
  81.     can_move=turtle.back()
  82.     while not can_move do
  83.         --print("ERR: There is a something on my way!")
  84.         sleep(0.5)
  85.         can_move=turtle.back()
  86.     end
  87. end
  88.  
  89. function forceMoveUp()
  90. local can_move=true
  91.     can_move=turtle.up()
  92.     while not can_move do
  93.         --print("ERR: There is a something on my way!")
  94.         sleep(0.5)
  95.         --
  96.         turtle.digUp()
  97.         turtle.attackUp()
  98.         --
  99.         can_move=turtle.up()
  100.     end
  101. end
  102.  
  103. function forceMoveDown()
  104. local can_move=true
  105.     can_move=turtle.down()
  106.     while not can_move do
  107.         --print("ERR: There is a something on my way!")
  108.         sleep(0.5)
  109.         --
  110.         turtle.digDown()
  111.         turtle.attackDown()
  112.         --
  113.         can_move=turtle.down()
  114.     end
  115. end
  116.  
  117. function left()
  118.     if tD[1]==1 and tD[2]==0 then
  119.         tD[1]=0
  120.         tD[2]=-1
  121.     elseif tD[1]==0 and tD[2]==-1 then
  122.         tD[1]=-1
  123.         tD[2]=0
  124.     elseif tD[1]==-1 and tD[2]==0 then
  125.         tD[1]=0
  126.         tD[2]=1
  127.     elseif tD[1]==0 and tD[2]==1 then
  128.         tD[1]=1
  129.         tD[2]=0
  130.     end
  131.     turtle.turnLeft()
  132. end
  133.  
  134. function right()
  135.     if tD[1]==1 and tD[2]==0 then
  136.         tD[1]=0
  137.         tD[2]=1
  138.     elseif tD[1]==0 and tD[2]==1 then
  139.         tD[1]=-1
  140.         tD[2]=0
  141.     elseif tD[1]==-1 and tD[2]==0 then
  142.         tD[1]=0
  143.         tD[2]=-1
  144.     elseif tD[1]==0 and tD[2]==-1 then
  145.         tD[1]=1
  146.         tD[2]=0
  147.     end
  148.     turtle.turnRight()
  149. end
  150.  
  151. function turnL(num1,num2)
  152.     while tD[1]~=num1 or tD[2]~=num2 do
  153.         left()
  154.     end
  155. end
  156.  
  157. function turnR(num1,num2)
  158.     while tD[1]~=num1 or tD[2]~=num2 do
  159.         right()
  160.     end
  161. end
  162.  
  163. function turnB(num1,num2)
  164.     if math.abs(num1-tD[1])==2 then
  165.         turnR(num1,num2)
  166.  
  167.     elseif math.abs(num2-tD[2])==2 then
  168.         turnL(num1,num2)
  169.  
  170.     elseif tD[1]==1 then
  171.         if num2==1 then
  172.             turnR(num1,num2)
  173.         elseif num2==-1 then
  174.             turnL(num1,num2)
  175.         end
  176.  
  177.     elseif tD[1]==-1 then
  178.         if num2==-1 then
  179.             turnR(num1,num2)
  180.         elseif num2==1 then
  181.             turnL(num1,num2)
  182.         end
  183.  
  184.     elseif tD[2]==1 then
  185.         if num1==-1 then
  186.             turnR(num1,num2)
  187.         elseif num1==1 then
  188.             turnL(num1,num2)
  189.         end
  190.  
  191.     elseif tD[2]==-1 then
  192.         if num1==1 then
  193.             turnR(num1,num2)
  194.         elseif num1==-1 then
  195.             turnL(num1,num2)
  196.         end
  197.  
  198.     end
  199. end
  200.  
  201. function moveXYZ(lx,ly,lz)
  202. local i
  203.  
  204.     if tZ<lz then
  205.         for i=1,lz-tZ do
  206.             forceMoveUp()
  207.         end
  208.     else
  209.         for i=1,tZ-lz do
  210.             forceMoveDown()
  211.         end
  212.     end
  213.  
  214.     if tY<ly then
  215.         turnB(1,0)
  216.         for i=1,ly-tY do
  217.             forceMoveForward()
  218.         end
  219.     elseif tY>ly then
  220.         turnB(-1,0)
  221.         for i=1,tY-ly do
  222.             forceMoveForward()
  223.         end
  224.     end
  225.  
  226.     if tX<lx then
  227.         turnB(0,1)
  228.         for i=1,lx-tX do
  229.             forceMoveForward()
  230.         end
  231.     elseif tX>lx then
  232.         turnB(0,-1)
  233.         for i=1,tX-lx do
  234.             forceMoveForward()
  235.         end
  236.     end
  237.  
  238.     tX,tY,tZ=lx,ly,lz
  239. end
  240.  
  241. function GUIinputFileName()
  242. local input
  243. local success="test.3dp"
  244.  
  245.     term.clear()   
  246.     term.setTextColor(colors.yellow)
  247.     term.setCursorPos(1,6)
  248.     term.write("+----------input file name:-----------+")
  249.     term.setCursorPos(1,7)
  250.     term.write("|                                     |")
  251.     term.setCursorPos(1,8)
  252.     term.write("+-------------------------------------+")
  253.     term.setTextColor(colors.white)
  254.     term.setCursorPos(4,7)
  255.     term.write("file name=")
  256.     input = read()
  257.     if input == nil then
  258.         success=false
  259.     else
  260.         success=input
  261.     end
  262.  
  263. return success
  264. end
  265.  
  266. function nameOnly(sName)
  267. local str,i,j
  268.     str=sName
  269.     i, j = string.find(str, ":")
  270.     if i then
  271.         str = string.sub(str,i+1)
  272.     end
  273. return str
  274. end
  275.  
  276. function updateMatrix(blockName,x,y,z)
  277. local i
  278. local success=false
  279.     for i=1,16 do
  280.         if blockName == slotName[i] then
  281.             matr[z][y][x]=i
  282.             success=true
  283.             break
  284.         end
  285.     end
  286.     if not success then
  287.         for i=1,16 do
  288.             if slotName[i] == "" then
  289.                 slotName[i] = blockName
  290.                 matr[z][y][x]=i
  291.                 success=true
  292.                 break
  293.             end
  294.         end
  295.     end
  296. return success
  297. end
  298.  
  299. function startScan()
  300. local x,y,z
  301. local i,j
  302. local success,data
  303. local str
  304.     turtle.select(1)
  305.     turtle.dropUp()
  306.     for z=sizeZ,1,-1 do
  307.         for y=1,sizeY do
  308.             if math.fmod(y,2) == 1 then
  309.                 for x=1,sizeX do
  310.                     moveXYZ(x,y,z+1)
  311.                     success, data = turtle.inspectDown()
  312.                     if success then
  313.                         str = nameOnly(data.name) .. data.metadata
  314.                         updateMatrix(str,x,y,z)
  315.                         turtle.digDown()
  316.                         turtle.dropUp()
  317.                     end            
  318.                 end
  319.             else
  320.                 for x=sizeX,1,-1 do
  321.                     moveXYZ(x,y,z+1)
  322.                     success, data = turtle.inspectDown()
  323.                     if success then
  324.                         str = nameOnly(data.name) .. data.metadata
  325.                         updateMatrix(str,x,y,z)
  326.                         turtle.digDown()
  327.                         turtle.dropUp()
  328.                     end            
  329.                 end
  330.             end
  331.         end
  332.     end
  333.     moveXYZ(1,0,1)
  334.     turnB(1,0)
  335. end
  336.  
  337. function GUIsuccessScan(fileName)
  338.     term.clear()   
  339.     term.setTextColor(colors.yellow)
  340.     term.setCursorPos(1,6)
  341.     term.write("+-------------------------------------+")
  342.     term.setCursorPos(1,7)
  343.     term.write("|       Scan successfully ended!      |")
  344.     term.setCursorPos(1,8)
  345.     term.write("|                                     |")
  346.     term.setCursorPos(1,9)
  347.     term.write("+-------------------------------------+")
  348.     term.setTextColor(colors.lime)
  349.     term.setCursorPos(2,8)
  350.     term.write("saved into:" .. fileName)
  351.     term.setTextColor(colors.white)
  352.     term.setCursorPos(1,1)
  353. end
  354.  
  355. -----------------------------------------
  356.  
  357. --global variables:
  358. tX,tY,tZ=1,0,1
  359. tD={1,0}
  360. sizeX, sizeY, sizeZ = 1,1,1
  361. matr={}
  362. slotName={}
  363.  
  364. --Locals:
  365. local args={...}
  366. local x,y,z
  367. local i,j
  368. local fileName="test.3dp"
  369.  
  370. if #args<3 or args[1]==nil or args[2]==nil or args[3]==nil or args[1]=="?" then
  371.     print("Usage: 3dscan <sizeX> <sizeY> <sizeZ>")
  372.     print("       sizeX, sizeY, sizeZ >= 1")
  373.     return
  374. end
  375. sizeX = tonumber(args[1])
  376. if sizeX==nil or sizeX<1 then sizeX=1 end
  377. sizeY = tonumber(args[2])
  378. if sizeY==nil or sizeY<1 then sizeY=1 end
  379. sizeZ = tonumber(args[3])
  380. if sizeZ==nil or sizeZ<1 then sizeZ=1 end
  381.  
  382. for z=1,sizeZ do
  383.     matr[z]={}
  384.     for y=1,sizeY do
  385.         matr[z][y]={}
  386.         for x=1,sizeX do
  387.             matr[z][y][x]=0
  388.         end
  389.     end
  390. end
  391.  
  392. fileName=GUIinputFileName()
  393. if fileName==false then
  394.     term.clear()   
  395.     term.setTextColor(colors.white)
  396.     term.setCursorPos(1,1)
  397.     print("INVALID file name!")
  398.     return
  399. end
  400.  
  401. i, j = string.find(fileName, ".3dp")
  402. if not i then
  403.     fileName = fileName .. ".3dp"
  404. end
  405.  
  406. for i=1,16 do
  407.     slotName[i]=""
  408. end
  409.  
  410. --Start Scan:--
  411. startScan()
  412.  
  413. createSchemeFile(fileName,slotName,sizeX,sizeY,sizeZ,matr)
  414.  
  415. GUIsuccessScan(fileName)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement