guitarplayer616

Turtle Schematic Builder 2

Dec 17th, 2015
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 21.06 KB | None | 0 0
  1. function saveVarsToConsole(name)
  2.     local h = fs.open(name,'w')
  3.     local env = getfenv()
  4.     for i,v in pairs(env) do
  5.         if i then
  6.             h.write(i)
  7.             if v then
  8.                 --h.write("("..tostring(type(v))..")")
  9.                 h.write(': ')
  10.                 h.writeLine(v)
  11.             end
  12.         end
  13.     end
  14.     h.close()
  15. end
  16.  
  17. function openRednet()
  18.     for _,v in ipairs(peripheral.getNames()) do
  19.             if peripheral.getType(v)=="modem" then
  20.                     rednet.open(v)
  21.             end
  22.     end
  23. end
  24.  
  25. function checkIfRefill()
  26.     local _,inFront = turtle.inspect()
  27.     local _,below = turtle.inspectDown()
  28.     local mem = turtle.getSelectedSlot()
  29.     turtle.select(16)
  30.     local check = turtle.getItemDetail()
  31.     if check and check.name ~= "EnderStorage:enderChest" then
  32.         if not turtle.dropDown() then
  33.             if not turtle.drop() then
  34.                 turtle.dropUp()
  35.             end
  36.         end  
  37.     end
  38.     if inFront or below then
  39.         if inFront.name == "EnderStorage:enderChest" then
  40.             turtle.select(enderchest1)
  41.             turtle.dig()
  42.         end
  43.         if below.name == "EnderStorage:enderChest" then
  44.             turtle.select(enderchest2)
  45.             turtle.digDown()
  46.         end
  47.     end
  48.     turtle.select(mem)
  49. end
  50.  
  51. function calibrateDir()
  52.     if gpsi then
  53.         local dir
  54.         local h,k,l = gps.locate()
  55.         while not turtle.forward() do
  56.             turtle.dig()
  57.         end
  58.         local h2,k2,l2 = gps.locate()
  59.         turtle.back()
  60.         if h2>h then
  61.             dir = "east"
  62.         elseif h2<h then
  63.             dir = "west"
  64.         elseif l2>l then
  65.             dir = "south"
  66.         elseif l2<l then
  67.             dir = "north"
  68.         end
  69.         return dir
  70.     end
  71. end
  72.  
  73. function recalibratePos()
  74.     local h,k,l = gps.locate() 
  75.     local ytemp,xtemp,ztemp  = h - Hoff, k - Koff, l - Loff
  76.     local ynew,xnew,znew = 0,0,0   
  77.  
  78.     if origDir == "south" then
  79.         xnew = xnew + xtemp
  80.         ynew = ytemp
  81.         znew = znew + ztemp
  82.     elseif origDir == "north" then
  83.         xnew = xnew - xtemp
  84.         ynew = ytemp
  85.         znew = znew - ztemp
  86.     elseif origDir == "east" then
  87.         xnew = xnew - znew
  88.         ynew = ytemp
  89.         znew = znew + xtemp
  90.     end
  91. end
  92.  
  93. function recalibrateFace() 
  94.     face = calibrateDir()
  95.     if origDir ~= "south" then
  96.         if origDir == "north" then
  97.             update("right")
  98.             update("right")
  99.         elseif origDir == "east" then
  100.             update("right")
  101.         elseif origDir == "west" then
  102.             update("left")
  103.         end
  104.     end
  105. end
  106.  
  107. function saveVarsToFile()
  108.     local h = fs.open("currentVars",'w')
  109.     local env = getfenv()
  110.     for i,v in pairs(env) do
  111.         if type(v) == "string" then
  112.             h.write(tostring(i))
  113.             h.write(" = ")
  114.             h.write([["]]..tostring(v)..[["]])
  115.         elseif type(v) == "table" then
  116.             h.write(i)
  117.             h.write(" = ")
  118.             h.write(textutils.serialize(v))
  119.         end
  120.     end
  121.     h.close()
  122. end
  123.  
  124. function loadVars()
  125.     local h = fs.open("currentVars",'r')
  126. end
  127.  
  128. function recordPos(heightPos,widthPos,lengthPos,face)
  129.     local h = fs.open("position","w")
  130.     h.writeLine("heightPos = "..tostring(heightPos))
  131.     h.writeLine("widthPos = "..tostring(widthPos))
  132.     h.writeLine("lengthPos = "..tostring(lengthPos))
  133.     h.writeLine("face = ".."\""..tostring(face).."\"")
  134.     h.close()
  135. end
  136.  
  137. function recordObj(x,y,z)
  138.     local h = fs.open("objective",'w')
  139.     h.writeLine("x = "..tostring(x))
  140.     h.writeLine("y = "..tostring(y))
  141.     h.writeLine("z = "..tostring(z))
  142.     h.close()
  143. end
  144.  
  145. block_id = {}
  146. block_id[0] = "Air"
  147. block_id[1] = "Stone"
  148. block_id[2] = "Grass"
  149. block_id[3] = "Dirt"
  150. block_id[4] = "Cobblestone"
  151. block_id[5] = "Wooden Plank"
  152. block_id[6] = "Sapling"
  153. block_id[7] = "Bedrock"
  154. block_id[8] = "Water"
  155. block_id[9] = "Stationary water"
  156. block_id[10] = "Lava"
  157. block_id[11] = "Stationary lava"
  158. block_id[12] = "Sand"
  159. block_id[13] = "Gravel"
  160. block_id[14] = "Gold Ore"
  161. block_id[15] = "Iron (Ore)"
  162. block_id[16] = "Coal Ore"
  163. block_id[17] = "Log"
  164. block_id[18] = "Leaves"
  165. block_id[19] = "Sponge"
  166. block_id[20] = "Glass"
  167. block_id[21] = "Lapis Lazuli (Ore)"
  168. block_id[22] = "Lapis Lazuli (Block)"
  169. block_id[23] = "Dispenser"
  170. block_id[24] = "Sandstone"
  171. block_id[25] = "Note Block Tile entity"
  172. block_id[26] = "Bed"
  173. block_id[27] = "Powered Rail "
  174. block_id[28] = "Detector Rail "
  175. block_id[29] = "Sticky Piston"
  176. block_id[30] = "Cobweb"
  177. block_id[31] = "Tall Grass"
  178. block_id[32] = "Dead Bush"
  179. block_id[33] = "Piston"
  180. block_id[34] = "Piston Extension"
  181. block_id[35] = "Wool"
  182. block_id[36] = "Block moved by Piston"
  183. block_id[37] = "Dandelionandelion"
  184. block_id[38] = "Rose"
  185. block_id[39] = "Brown Mushroom"
  186. block_id[40] = "Red Mushroom"
  187. block_id[41] = "Block of Gold"
  188. block_id[42] = "Block of Iron"
  189. block_id[43] = "Double Slabs"
  190. block_id[44] = "Slabs"
  191. block_id[45] = "Brick Block"
  192. block_id[46] = "TNT"
  193. block_id[47] = "Bookshelf"
  194. block_id[48] = "Moss Stone"
  195. block_id[49] = "Obsidian"
  196. block_id[50] = "Torch"
  197. block_id[51] = "Fire"
  198. block_id[52] = "Monster Spawner"
  199. block_id[53] = "Wooden Stairs"
  200. block_id[54] = "Chest"
  201. block_id[55] = "Redstone (Wire)"
  202. block_id[56] = "Diamond (Ore)"
  203. block_id[57] = "Block of Diamond"
  204. block_id[58] = "Crafting Table"
  205. block_id[59] = "Seeds"
  206. block_id[60] = "Farland"
  207. block_id[61] = "Furnace"
  208. block_id[62] = "Burning Furnace"
  209. block_id[63] = "Sign Post"
  210. block_id[64] = "Wooden Door"
  211. block_id[65] = "Ladders"
  212. block_id[66] = "Rails"
  213. block_id[67] = "Cobblestone Stairs"
  214. block_id[68] = "Wall Sign"
  215. block_id[69] = "Lever"
  216. block_id[70] = "Stone Pressure Plate"
  217. block_id[71] = "Iron Door"
  218. block_id[72] = "Wooden Pressure Plates"
  219. block_id[73] = "Redstone Ore"
  220. block_id[74] = "Glowing Redstone Ore"
  221. block_id[75] = "Redstone Torch"
  222. block_id[76] = "Redstone Torch"
  223. block_id[77] = "Stone Button "
  224. block_id[78] = "Snow"
  225. block_id[79] = "Ice"
  226. block_id[80] = "Snow Block"
  227. block_id[81] = "Cactus"
  228. block_id[82] = "Clay (Block)"
  229. block_id[83] = "Sugar Cane"
  230. block_id[84] = "Jukebox"
  231. block_id[85] = "Fence"
  232. block_id[86] = "Pumpkin"
  233. block_id[87] = "Netherrack"
  234. block_id[88] = "Soul Sand"
  235. block_id[89] = "Glowstone"
  236. block_id[90] = "Portal"
  237. block_id[91] = "Jack-O-Lantern"
  238. block_id[92] = "Cake Block"
  239. block_id[93] = "Redstone Repeater"
  240. block_id[94] = "Redstone Repeater"
  241. block_id[95] = "Locked Chest"
  242. block_id[96] = "Trapdoors"
  243. block_id[97] = "Hidden Silverfish"
  244. block_id[98] = "Stone Brick"
  245. block_id[99] = "Huge brown and red mushroom"
  246. block_id[100] = "Huge brown and red mushroom"
  247. block_id[101] = "Iron Bars"
  248. block_id[102] = "Glass Pane"
  249. block_id[103] = "Melon"
  250. block_id[104] = "Pumpkin Stem"
  251. block_id[105] = "Melon Stem"
  252. block_id[106] = "Vines"
  253. block_id[107] = "Fence Gate"
  254. block_id[108] = "Brick Stairs"
  255. block_id[109] = "Stone Brick Stairs"
  256. block_id[110] = "Mycelium"
  257. block_id[111] = "Lily Pad"
  258. block_id[112] = "Nether Brick"
  259. block_id[113] = "Nether Brick Fence"
  260. block_id[114] = "Nether Brick Stairs"
  261. block_id[115] = "Nether Wart"
  262. block_id[116] = "Enchantment Table"
  263. block_id[117] = "Brewing Stand"
  264. block_id[118] = "Cauldron"
  265. block_id[119] = "End Portal"
  266. block_id[120] = "End Portal Frame"
  267. block_id[121] = "End Stone "
  268. block_id[256] = "Iron Ingotron Shovel"
  269. block_id[257] = "Iron Pickaxe"
  270. block_id[258] = "Iron Axe"
  271. block_id[259] = "Flint and Steel"
  272. block_id[260] = "Red Apple"
  273. block_id[261] = "Bow"
  274. block_id[262] = "Arrow"
  275. block_id[263] = "Coal"
  276.  
  277. woolColors = {}
  278. woolColors[0] = "White"
  279. woolColors[1] = "Orange"
  280. woolColors[2] = "Magenta"
  281. woolColors[3] = "Light Blue"
  282. woolColors[4] = "Yellow"
  283. woolColors[5] = "Lime"
  284. woolColors[6] = "Pink"
  285. woolColors[7] = "Gray"
  286. woolColors[8] = "Light Gray"
  287. woolColors[9] = "Cyan"
  288. woolColors[10] = "Purple"
  289. woolColors[11] = "Blue"
  290. woolColors[12] = "Brown"
  291. woolColors[13] = "Green"
  292. woolColors[14] = "Red"
  293. woolColors[15] = "Black"
  294.  
  295. --[[Navigation Functions]]--
  296.  
  297. function goto(heightGoal,widthGoal,lengthGoal)
  298.     shell.run("position")
  299.     if turtle.getFuelLevel() < 200 then
  300.         refill()
  301.     end
  302.     if heightGoal > heightPos then
  303.         while heightGoal > heightPos do
  304.             up()
  305.         end
  306.     elseif heightGoal < heightPos then
  307.         while heightGoal < heightPos do
  308.             down()
  309.         end
  310.     end
  311.     if widthGoal > widthPos then
  312.         turn("east")
  313.         while widthGoal > widthPos do
  314.             forward()
  315.         end
  316.     elseif widthGoal < widthPos then
  317.         turn("west")
  318.         while widthGoal < widthPos do
  319.             forward()
  320.         end
  321.     end
  322.     if lengthGoal > lengthPos then
  323.         turn("south")
  324.         while lengthGoal > lengthPos do
  325.             forward()
  326.         end
  327.     elseif lengthGoal < lengthPos then
  328.         turn("north")
  329.         while lengthGoal < lengthPos do
  330.             forward()
  331.         end
  332.     end
  333. end
  334.  
  335. function update(dir)
  336.         if dir == "forward" then
  337.             if face == "north" then
  338.                 lengthPos = lengthPos - 1
  339.             elseif face == "south" then
  340.                 lengthPos = lengthPos + 1
  341.             elseif face == "west" then
  342.                 widthPos = widthPos - 1
  343.             elseif face == "east" then
  344.                 widthPos = widthPos + 1
  345.             end
  346.         elseif dir == "backward" then
  347.                 if face == "north" then
  348.                         lengthPos = lengthPos + 1
  349.                 elseif face == "south" then
  350.                         lengthPos = lengthPos - 1
  351.                 elseif face == "west" then
  352.                         widthPos = widthPos + 1
  353.                 elseif face == "east" then
  354.                         widthPos = widthPos - 1
  355.                 end
  356.         elseif dir == "up" then
  357.                 heightPos = heightPos + 1
  358.         elseif dir == "down" then
  359.                 heightPos = heightPos - 1
  360.         elseif dir == "right" then
  361.                 if face == "north" then
  362.                         face = "east"
  363.                 elseif face == "east" then
  364.                         face = "south"
  365.                 elseif face == "south" then
  366.                         face = "west"
  367.                 elseif face == "west" then
  368.                         face = "north"
  369.                 end
  370.         elseif dir == "left" then
  371.                 if face == "north" then
  372.                         face = "west"
  373.                 elseif face == "west" then
  374.                         face = "south"
  375.                 elseif face == "south" then
  376.                         face = "east"
  377.                 elseif face == "east" then
  378.                         face = "north"
  379.                 end
  380.         end
  381.         recordPos(heightPos,widthPos,lengthPos,face)
  382. end
  383.  
  384. --[[Refill Functions]]--
  385.  
  386. function scanInv()
  387.         local invList = {}
  388.         for i = 1,16 do
  389.                 turtle.select(i)
  390.                 local item = turtle.getItemDetail()
  391.                 if item then
  392.                         invList[i] = {}
  393.             invList[i][item.name] = item.damage
  394.                 end
  395.         end
  396.         return invList
  397. end
  398.  
  399. function ParseInv()
  400.         turtle.select(16)
  401.         turtle.dropUp()
  402.         while turtle.suck() do
  403.                 local item = turtle.getItemDetail()
  404.                 for i =1,16 do
  405.                         if invList[i][item.name] then
  406.                 if invList[i][item.name] == item.damage then
  407.                                     turtle.transferTo(i)
  408.                                     break
  409.                 end
  410.                         end
  411.                         if item.name == "minecraft:lava_bucket" or item.name == "minecraft:coal" then
  412.                                 turtle.refuel(64)
  413.                         end
  414.                 end
  415.                 turtle.dropUp()
  416.         end
  417. end
  418.  
  419. function refill()
  420.     local mem = turtle.getSelectedSlot()
  421.     chester("set")
  422.         ParseInv()
  423.         chester("gather")
  424.     turtle.select(mem)
  425. end
  426.  
  427. function chester(action)
  428.         --action = 'set' or 'gather'
  429.         if action == "set" then
  430.                 turtle.select(tonumber(enderchest1))
  431.                 turtle.place()
  432.                 turtle.select(tonumber(enderchest2))
  433.                 turtle.placeUp()
  434.         elseif action == "gather" then
  435.                 if tonumber(chestOrder) == 1 then
  436.                         turtle.select(tonumber(enderchest1))
  437.                         turtle.digUp()
  438.                         turtle.select(tonumber(enderchest2))
  439.                         turtle.dig()
  440.                         chestOrder = 2
  441.                 elseif tonumber(chestOrder) == 2 then
  442.                         turtle.select(tonumber(enderchest1))
  443.                         turtle.dig()
  444.                         turtle.select(tonumber(enderchest2))
  445.                         turtle.digUp()
  446.                         chestOrder = 1
  447.                 end
  448.         end
  449. end
  450.  
  451. function getBlockName(id, blockData)
  452.   blockData = blockData or nil
  453.   if(block_id[id] == nil) then
  454.     return "ID: "..tostring(id)..", Data: "..tostring(blockData)
  455.   else
  456.     if(blockData) then
  457.       if(id == 35) then
  458.         str = woolColors[blockData] .. " " .. block_id[id]
  459.         return str
  460.       end
  461.     end
  462.     return block_id[id]
  463.   end
  464. end
  465.  
  466. function getBlockId(x,y,z)
  467.   return blocks[y + z*width + x*length*width + 1]
  468. end
  469.  
  470. function getData(x,y,z)
  471.   return data[y + z*width + x*length*width + 1]
  472. end
  473.  
  474. function readbytes(handle, n)
  475.   for i=1,n do
  476.     handle.read()
  477.   end
  478. end
  479.  
  480. function readname(handle)  
  481.   n1 = handle.read()
  482.   n2 = handle.read()
  483.  
  484.   if(n1 == nil or n2 == nil) then
  485.     return ""
  486.   end
  487.  
  488.   n = n1*256 + n2
  489.  
  490.   str = ""
  491.   for i=1,n do
  492.     c = handle.read()
  493.     if c == nil then
  494.       return
  495.     end  
  496.     str = str .. string.char(c)
  497.   end
  498.   return str
  499. end
  500.  
  501. function parse(a, handle, containsName)
  502.   containsName = containsName or true
  503.   if a==0 then
  504.     return
  505.   end
  506.   if containsName then
  507.     name = readname(handle)
  508.   end
  509.    
  510.   if a==1 then
  511.     readbytes(handle,1)  
  512.   elseif a==2 then
  513.     i1 = handle.read()
  514.     i2 = handle.read()
  515.     i = i1*256 + i2
  516.     if(name=="Height") then
  517.       height = i
  518.     elseif (name=="Length") then
  519.       length = i
  520.     elseif (name=="Width") then
  521.       width = i
  522.     end
  523.   elseif a==3 then
  524.     readbytes(handle,4)
  525.   elseif a==4 then
  526.     readbytes(handle,8)
  527.   elseif a==5 then
  528.     readbytes(handle,4)
  529.   elseif a==6 then
  530.     readbytes(handle,8)
  531.   elseif a==7 then
  532.     i1 = handle.read()
  533.     i2 = handle.read()
  534.     i3 = handle.read()
  535.     i4 = handle.read()
  536.     i = i1*256*256*256 + i2*256*256 + i3*256 + i4
  537.     if name == "Blocks" then
  538.       for i=1,i do
  539.         table.insert(blocks, handle.read())
  540.       end
  541.     elseif name == "Data" then
  542.       for i=1,i do
  543.         table.insert(data, handle.read())
  544.       end
  545.     else
  546.       readbytes(handle,i)
  547.     end
  548.   elseif a==8 then
  549.     i1 = handle.read()
  550.     i2 = handle.read()
  551.     i = i1*256 + i2
  552.     readbytes(handle,i)
  553.   elseif a==9 then
  554.         --readbytes(handle,5)
  555.         type = handle.read()
  556.         i1 = handle.read()
  557.     i2 = handle.read()
  558.     i3 = handle.read()
  559.     i4 = handle.read()
  560.     i = i1*256*256*256 + i2*256*256 + i3*256 + i4
  561.     for j=1,i do
  562.       parse(handle.read(), handle, false)
  563.     end
  564.   end
  565. end
  566.  
  567. --[[Movement Functions]]--
  568.  
  569. function forward()
  570.   update("forward")
  571.   while not turtle.forward() do
  572.     turtle.dig()
  573.   end
  574. end
  575.  
  576. function up()
  577.   update("up")
  578.   while not turtle.up() do
  579.     turtle.digUp()
  580.   end
  581. end
  582.  
  583. function down()
  584.   update("down")
  585.   while not turtle.down() do
  586.     turtle.digDown()
  587.   end
  588. end
  589.  
  590. function right()
  591.   update("right")
  592.   turtle.turnRight()
  593. end
  594.    
  595. function left()
  596.   update("left")
  597.   turtle.turnLeft()
  598. end
  599.  
  600. function turn(dir)
  601.     if face == "north" then
  602.         if dir == "east" then
  603.             while face ~= dir do
  604.                 right()
  605.             end
  606.         else
  607.             while face ~= dir do
  608.                 left()
  609.             end
  610.         end
  611.    elseif face == "east" then
  612.         if dir == "south" then
  613.             while face ~= dir do
  614.                 right()
  615.             end
  616.         else
  617.             while face ~= dir do
  618.                 left()
  619.             end
  620.         end
  621.     elseif face == "south" then
  622.         if dir == "west" then
  623.             while face ~= dir do
  624.                 right()
  625.             end
  626.         else
  627.             while face ~= dir do
  628.                 left()
  629.             end
  630.         end
  631.     elseif face == "west" then
  632.         if dir == "north" then
  633.             while face ~= dir do
  634.                 right()
  635.             end
  636.         else
  637.             while face ~= dir do
  638.                 left()
  639.             end
  640.         end
  641.     end
  642. end
  643.  
  644. function place()
  645.   while not turtle.placeDown() do
  646.     turtle.digDown()
  647.   end
  648.   return true
  649. end
  650.  
  651. --[[Working Functions]]--
  652.  
  653. function setup()
  654.     a = 0
  655.     while (a ~= nil) do
  656.         a = handle.read()
  657.         parse(a, handle)
  658.     end
  659.  
  660.     write("length: " .. length)
  661.     write("   width: " .. width)
  662.     write("   height: " .. height .. "\n")
  663.  
  664.     uniqueblocks={}
  665.     for i,v in ipairs(blocks) do
  666.         found = false
  667.         for j,w in ipairs(uniqueblocks) do
  668.             -- for now, data is only accounted for when the block is wool
  669.                 if (w.blockID==v and (w.data==data[i] or w.blockID ~= 35)) then
  670.                     found = true
  671.                     w.amount = w.amount + 1
  672.                     break
  673.             end
  674.         end
  675.  
  676.         if found==false then
  677.             uniqueblocks[#uniqueblocks+1] = {}
  678.             uniqueblocks[#uniqueblocks].blockID = v
  679.             uniqueblocks[#uniqueblocks].data = data[i]
  680.             uniqueblocks[#uniqueblocks].amount = 1
  681.         end
  682.     end
  683.  
  684.     print("number of block types: " .. #uniqueblocks)
  685.     for i,v in ipairs(uniqueblocks) do
  686.         if (i%9)==0 then
  687.             read()
  688.         end
  689.         print(" -" .. getBlockName(v.blockID, v.data) .. ": " .. v.amount)
  690.     end
  691.  
  692.     read()
  693.  
  694.     print("Give the numbers of all slots containing the specified block type:")
  695.  
  696.     slots={}
  697.     for i,block in ipairs(uniqueblocks) do
  698.         blockData = block.data
  699.         print(" -in which slots is " .. getBlockName(block.blockID, blockData) .. "?")
  700.         if not slots[block.blockID] then
  701.             slots[block.blockID] = {}
  702.         end
  703.         slots[block.blockID][blockData] = {}
  704.         write("   ")
  705.         str = read()
  706.         for i = 1, #str do
  707.                 local c = str:sub(i,i)
  708.                 n = tonumber(c)
  709.                 if(n) then
  710.                     if(n>0 and n<10) then
  711.                         table.insert(slots[block.blockID][blockData], n)
  712.                     end
  713.             end
  714.         end
  715.     end
  716.     invList = scanInv()
  717.  end
  718.  
  719. function run()
  720.     print("Press key to start building...")
  721.     read()
  722.     --invList = scanInv()
  723.  
  724.     up()
  725.     n = 1
  726.     turtle.select(n)
  727.  
  728.     for x=1,height do
  729.         for y=1,width do
  730.             for z=1,length do
  731.                 blockID = getBlockId(x-1,y-1,z-1)
  732.                     blockData = getData(x-1,y-1,z-1)
  733.                     forward()
  734.                 turtle.digDown()
  735.                     slot_lst = slots[blockID][blockData]
  736.                     if(slot_lst ~= nil) then
  737.                         if(#slot_lst > 0) then
  738.                             local found=false
  739.                             for i,v in ipairs(slot_lst) do
  740.                                 if(turtle.getItemCount(v) > 0) then
  741.                                     found=true
  742.                                     turtle.select(v)
  743.                                     break
  744.                                 end
  745.                             end
  746.                             if not found then
  747.                                 print("Not enough " .. getBlockName(blockID, blockData) .. ". Please refill...")
  748.                             refill()
  749.                             end
  750.                             while turtle.getItemCount() == 0 do
  751.                             refill()
  752.                         end
  753.                         if turtle.getFuelLevel() < 200 then
  754.                             refill()
  755.                         end
  756.                         end
  757.                     end
  758.                 end
  759.                 left()
  760.                 forward()
  761.             left()
  762.             for i=1,length do
  763.                 forward()
  764.             end
  765.             right()
  766.             right()
  767.         end
  768.         right()
  769.         for i=1,width do
  770.             forward()
  771.         end
  772.         left()
  773.         up()
  774.     end
  775.  
  776.     for i=1,height+1 do
  777.         down()
  778.     end
  779. end
  780.  
  781. function findNextBlock(x,y,z)
  782.     blockID = getBlockId(x,y,z)
  783.     blockData = getData(x,y,z)
  784.     if blockID then
  785.         slot_lst = slots[blockID][blockData]
  786.         if(slot_lst ~= nil) then
  787.                 if(#slot_lst > 0) then
  788.                     local found=false
  789.                     for i,v in ipairs(slot_lst) do
  790.                         if(turtle.getItemCount(v) > 0) then
  791.                             found=true
  792.                             turtle.select(v)
  793.                             break
  794.                         end
  795.                     end
  796.                     if not found then
  797.                         print("Not enough " .. getBlockName(blockID, blockData) .. ". Please refill...")
  798.                     refill()
  799.                     end
  800.                                 while turtle.getItemCount() == 0 do
  801.                                     refill()
  802.                                 end
  803.                 local doubleCheck = turtle.getItemDetail()
  804.                 if doubleCheck.name ~= "EnderStorage:enderChest" then
  805.                     place()
  806.                 else
  807.                     findNextBlock(x,y,z)   
  808.                 end
  809.             end
  810.             if turtle.getFuelLevel() < 200 then
  811.                 refill()
  812.             end
  813.         end
  814.     end
  815. end
  816.  
  817. function oldIterate()
  818.     --x,y,z = height,width,length
  819.     if z < length then
  820.         z = z + 1
  821.     elseif z == length then
  822.         z = 0
  823.         if y < width then
  824.             y = y + 1
  825.         elseif y == width then
  826.             y = 0
  827.             if x < height then
  828.                 x = x + 1
  829.             elseif x == height then
  830.                 x = 'max'
  831.                 y = 'max'
  832.                 z = 'max'
  833.             end
  834.         end
  835.     end
  836.     recordObj(x,y,z)
  837. end
  838.  
  839. function check()
  840.     if x%2==0 then
  841.         evenx = true
  842.         if y%2==0 then
  843.             eveny = true
  844.         else
  845.             eveny = false
  846.         end
  847.     else
  848.         evenx = false
  849.         if y%2==0 then
  850.             eveny = false
  851.         else
  852.             eveny = true
  853.         end
  854.     end
  855. end
  856.  
  857. function iterate()
  858.     check()
  859.     if z == length and eveny then
  860.         Yiterate()
  861.     elseif z==length and (not eveny) then
  862.         z = z - 1
  863.     elseif z == 0 and eveny then
  864.         z = z + 1
  865.     elseif z == 0 and (not eveny) then
  866.         Yiterate()
  867.     elseif z < length then
  868.         if eveny then
  869.             z = z + 1
  870.         else
  871.             z = z - 1
  872.         end
  873.     end
  874. end
  875.  
  876. function Yiterate()
  877.     if evenx then
  878.         if y < width then
  879.             y = y + 1
  880.         elseif y == width then
  881.             if x < height then
  882.                 x = x + 1
  883.             end
  884.         end
  885.     else
  886.         if y == 0 then
  887.             if x < height then
  888.                 x = x + 1
  889.             end
  890.         else
  891.             y=y-1
  892.         end
  893.     end
  894. end
  895.  
  896. function checkIfAir()
  897.     while true do
  898.         iterate()
  899.         blockID2 = getBlockId(x,y,z)
  900.         blockData2 = getData(x,y,z)
  901.         slot_2nd = slots[blockID2][blockData2]
  902.         if slot_2nd then
  903.             if #slot_2nd > 0 then
  904.                 recordObj(x,y,z)
  905.                 break
  906.             end
  907.         end
  908.     end
  909. end
  910.  
  911. function autorun()
  912.     --get the current coords
  913.     --get the iterater/goal block location
  914.     while true do
  915.         shell.run("position")
  916.         shell.run("objective")
  917.         goto(x,y,z)
  918.         findNextBlock(x,y,z)
  919.         checkIfAir()
  920.     end
  921. end
Add Comment
Please, Sign In to add comment