Clem105

Scanning schematic

Apr 24th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.09 KB | None | 0 0
  1. tab = {}
  2.  
  3. local tArgs = { ... }
  4. if #tArgs ~= 2 then
  5.     error("Usage : scan <fileName> <side of diskDrive>")
  6.  
  7. else
  8.     peri = peripheral.getType(tArgs[2])
  9.     if peri ~= nil then
  10.         if peri == "drive" then
  11.         isDiskPresent = disk.hasData(tArgs[2])
  12.         if isDiskPresent == false then
  13.                 error("There is no Disk in the diskDrive !")
  14.             end
  15.       else
  16.         error("There is no DiskDrive attached to "..tArgs[2].." !")
  17.       end
  18.     else
  19.         error("There is no peripheral attached to "..tArgs[2].." !")
  20.     end
  21.     dir = disk.getMountPath(tArgs[2])
  22.     sPath = dir.."/"..tArgs[1]
  23.     if fs.exists(sPath) == true then
  24.         error("The fileName "..tArgs[1].." already exists !")
  25.     end
  26. end
  27.  
  28.  
  29.  
  30.  
  31. isFinished = false
  32. local currentX = 0
  33. local currentY = 1
  34. local currentZ = 1
  35. local handleFacing = {{1,0},{0,1},{-1,0},{0,-1}}
  36. local facing = 1
  37. print("Please enter lenght :")
  38. local x = read()
  39. print("Please enter height :")
  40. local y = read()
  41. print("Please enter width :")
  42. local z = read()
  43. x = tonumber(x)
  44. y = tonumber(y)
  45. z = tonumber(z)
  46. local layers = math.ceil(y/3)
  47. local lastLayerCount = y%3
  48.  
  49. if lastLayerCount == 0 then
  50.     lastLayerCount = 3
  51. end
  52. local currentLayer = 1
  53. local area = x*z
  54. local volume = x*y*z
  55. --TEST--
  56. print("X: "..x..", Y: "..y..", Z: "..z..", Volume: "..volume)
  57. --------
  58. if x < 1 or y < 1 or z < 1 then
  59.     error("x,y and z must be non null positive numbers.")
  60. end
  61.  
  62.  
  63. -- Checking for pickaxe
  64. function checkPickaxe()  
  65.     turtle.select(16)
  66.     turtle.equipLeft()
  67.     obj = turtle.getItemDetail()
  68.     if obj ~= nil then
  69.         if obj["name"] == "minecraft:diamond_pickaxe" then
  70.         print("Mining turtle : OK ")
  71.             turtle.equipLeft()
  72.         else
  73.             error("Turtle need to be a mining turtle.")
  74.         end
  75.     else
  76.     error("Turtle need to be a mining turtle.")
  77.     end
  78. end
  79.  
  80. -- Cheking for fuel
  81. function fuel()
  82.     turtle.select(1)
  83.     term.write("Fuel : ")
  84.     currentFuelLevel = turtle.getFuelLevel()
  85.     if currentFuelLevel < math.floor(1.1*area*layers) then
  86.         term.write("Need more !\n")
  87.         print("Current level : "..currentFuelLevel.."\n")
  88.         print("Need "..math.floor(1.1*volume)-currentFuelLevel.." more fuel point.\n")
  89.         print("Also known as "..math.ceil((math.floor(1.1*volume)-currentFuelLevel)/80).." coals/charcoals or "..math.ceil((math.floor(1.1*volume)-currentFuelLevel)/15).." wood log\n")
  90.         while currentFuelLevel < math.floor(1.1*volume) do
  91.              turtle.refuel()
  92.              currentFuelLevel = turtle.getFuelLevel()
  93.              sleep(2)
  94.           end
  95.     else
  96.           term.write("OK.\n")
  97.     end
  98. end    
  99.  
  100. ---------FUNCTION/SETTING UP----------
  101.  
  102. for l = 1,x do
  103.     tab[l] = {}
  104.     for m = 1,y do
  105.         tab[l][m] = {}
  106.         for n = 1,z do
  107.           tab[l][m][n] = {}
  108.           tab[l][m][n]["id"] = ""
  109.           tab[l][m][n]["metadata"] = 0
  110.           tab[l][m][n]["facing"] = ""
  111.           tab[l][m][n]["half"] = ""
  112.         end
  113.     end
  114. end
  115.    
  116.    
  117. function analyze(t)
  118.     local tData = {}
  119.     if t["name"] == "minecraft:log" or t["name"] == "minecraft:log2" then
  120.         tData["facing"] = t["state"]["axis"]
  121.     elseif t["name"]:find("slab")~= nil and t["name"]:find("double") == nil then
  122.         tData["half"] = t["state"]["half"]
  123.   elseif t["name"]:find("stairs") ~= nil then
  124.         tData["half"] = t["state"]["half"]
  125.         tData["facing"] = t["state"]["facing"]
  126.   elseif t["name"]:find("button") ~= nil or t["name"] == "ladder" then
  127.         tData["facing"] = t["state"]["facing"]
  128.   elseif t["name"]:find("trapdoor") ~= nil then
  129.         tData["half"] = t["state"]["half"]
  130.         tData["facing"] = t["state"]["facing"]
  131.   elseif t["name"]:find("chest") ~= nil or t["name"]:find("furnace") ~= nil or t["name"]:find("anvil") then
  132.         tData["facing"] = t["state"]["facing"]
  133.   elseif t["name"]:find("piston")~= nil or t["name"] == "minecraft:dropper" or t["name"] == "minecraft:dispenser" then
  134.         tData["facing"] = t["state"]["facing"]
  135.   end
  136.     tData["id"] = t["name"]
  137.     tData["metadata"] = t["metadata"]
  138.     return tData
  139. end
  140.  
  141.  
  142. function advInspect(level,a,b,c)
  143.     if level == 0 then
  144.             sucess,tInspect = turtle.inspect()
  145.     elseif level == -1 then
  146.             sucess,tInspect = turtle.inspectDown()
  147.     elseif level == 1 then
  148.             sucess,tInspect = turtle.inspectUp()
  149.     end
  150.     if sucess == true then
  151.        temp = analyze(tInspect)
  152.        for k,v in pairs(temp) do
  153.           tab[a][b+level][c][k] = v
  154.        end
  155.     else
  156.        tab[a][b+level][c]["id"] = "minecraft:air"
  157.     end
  158. end
  159.  
  160. function turningRight()
  161.     turtle.turnRight()
  162.     facing = facing +1
  163.     if facing == 5 then
  164.         facing = 1
  165.     end
  166. end
  167.  
  168. function upping()
  169.     currentX = 0
  170.     currentZ = 1
  171.     facing = 1
  172.     currentLayer = currentLayer + 1
  173.     for _ = 1,3 do
  174.     turtle.up()
  175.     currentY = currentY+1
  176.     turtle.digUp()
  177.     end
  178. end
  179.  
  180. function turningLeft()
  181.     turtle.turnLeft()
  182.     facing = facing -1
  183.     if facing == 0 then
  184.         facing = 4
  185.     end
  186. end
  187.  
  188. function moveOn()
  189.     if currentLayer == layers then
  190.         if lastLayerCount >=2 then
  191.             advInspect(0,currentX+handleFacing[facing][1],currentY,currentZ+handleFacing[facing][2])
  192.       end
  193.    else
  194.         advInspect(0,currentX+handleFacing[facing][1],currentY,currentZ+handleFacing[facing][2])
  195.     end
  196.     turtle.dig()
  197.     turtle.forward()
  198.     currentZ = currentZ+handleFacing[facing][2]
  199.     currentX = currentX+handleFacing[facing][1]
  200. end
  201.  
  202. -- MAIN PROGRAMM
  203. checkPickaxe()  
  204. fuel()
  205. turtle.up()
  206. currentY = currentY+1
  207. moveOn()
  208. while not isFinished do
  209.    
  210.     advInspect(-1,currentX,currentY,currentZ)
  211.     turtle.digDown()
  212.   if currentLayer == layers then
  213.         if lastLayerCount == 3 then
  214.             advInspect(1,currentX,currentY,currentZ)
  215.            turtle.digUp()
  216.       end
  217.   else
  218.         advInspect(1,currentX,currentY,currentZ)
  219.       turtle.digUp()
  220.   end
  221.   if currentZ == z then
  222.      if currentX == x and facing == 1 then
  223.         turtle.turnLeft()
  224.         if z == 1 then
  225.            error("You can't use a  one by one area ! ")
  226.         end
  227.         for i = 1,(z-1) do
  228.            turtle.forward()
  229.         end
  230.         turtle.turnLeft()
  231.         for j = 1,x do
  232.             turtle.forward()
  233.         end
  234.         turningRight()
  235.         turningRight()
  236.         if currentLayer < layers then
  237.                 upping()
  238.         else
  239.             isFinished = true
  240.                 for _ = 1,(currentY-1) do
  241.                 turtle.down()
  242.             end
  243.                
  244.         end
  245.      elseif currentX == 1 and facing == 3 then
  246.         turtle.turnRight()
  247.         for i = 1,(z-1) do
  248.            turtle.forward()
  249.         end
  250.         turtle.turnRight()
  251.         turtle.back()
  252.           if currentLayer < layers then
  253.                 upping()
  254.         else
  255.             isFinished = true
  256.                 for _ = 1,(currentY-1) do
  257.                 turtle.down()
  258.             end
  259.                
  260.         end
  261.      end
  262.   end
  263.   if not isFinished then
  264.         if currentX == x and facing == 1 then
  265.             turningRight()
  266.            moveOn()
  267.             turningRight()
  268.         elseif currentX == 1 and facing == 3 then
  269.           turningLeft()
  270.              moveOn()
  271.              turningLeft()
  272.      else
  273.              moveOn()
  274.         end
  275.   end
  276. end
  277.  
  278. h = fs.open(sPath,"w")
  279. h.write(textutils.serialize(tab))
  280. h.close()
Advertisement
Add Comment
Please, Sign In to add comment