Advertisement
guitarplayer616

Turtle Schematic Builder 2 Backup

Dec 11th, 2015
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.22 KB | None | 0 0
  1. function saveVarsToFile()
  2.         local h = fs.open("currentVars","w")
  3.         local env = getfenv()
  4.         for i,v in pairs(env) do
  5.                 h.write(i)
  6.                 h.write(': ')
  7.                 h.writeLine(v)
  8.         end
  9.         h.close()
  10. end
  11.  
  12. if turtle.getFuelLevel() == 0 then
  13.         print("No Fuel")
  14.         error()
  15. elseif turtle.getFuelLevel() < 300 then
  16.         print("Low Fuel")
  17.         error()
  18. end
  19.  
  20. invList = {}
  21. chestOrder = 1
  22. enderchest1 = 15
  23. enderchest2 = 16
  24.  
  25. tArgs = { ... }
  26. if #tArgs ~= 1 then
  27.   print("Usage: build <gunzipped schematic file>")
  28.   return
  29. end
  30.  
  31. filename = tArgs[1]
  32.  
  33. if not fs.exists(filename) then
  34.   print("File does not exist.")
  35.   return
  36. end
  37.  
  38. length = 0
  39. height = 0
  40. width = 0
  41. blocks = {}
  42. data = {}
  43.  
  44. block_id = {}
  45.  
  46. block_id[0] = "Air"
  47. block_id[1] = "Stone"
  48. block_id[2] = "Grass"
  49. block_id[3] = "Dirt"
  50. block_id[4] = "Cobblestone"
  51. block_id[5] = "Wooden Plank"
  52. block_id[6] = "Sapling"
  53. block_id[7] = "Bedrock"
  54. block_id[8] = "Water"
  55. block_id[9] = "Stationary water"
  56. block_id[10] = "Lava"
  57. block_id[11] = "Stationary lava"
  58. block_id[12] = "Sand"
  59. block_id[13] = "Gravel"
  60. block_id[14] = "Gold Ore"
  61. block_id[15] = "Iron (Ore)"
  62. block_id[16] = "Coal Ore"
  63. block_id[17] = "Log"
  64. block_id[18] = "Leaves"
  65. block_id[19] = "Sponge"
  66. block_id[20] = "Glass"
  67. block_id[21] = "Lapis Lazuli (Ore)"
  68. block_id[22] = "Lapis Lazuli (Block)"
  69. block_id[23] = "Dispenser"
  70. block_id[24] = "Sandstone"
  71. block_id[25] = "Note Block Tile entity"
  72. block_id[26] = "Bed"
  73. block_id[27] = "Powered Rail "
  74. block_id[28] = "Detector Rail "
  75. block_id[29] = "Sticky Piston"
  76. block_id[30] = "Cobweb"
  77. block_id[31] = "Tall Grass"
  78. block_id[32] = "Dead Bush"
  79. block_id[33] = "Piston"
  80. block_id[34] = "Piston Extension"
  81. block_id[35] = "Wool"
  82. block_id[36] = "Block moved by Piston"
  83. block_id[37] = "Dandelionandelion"
  84. block_id[38] = "Rose"
  85. block_id[39] = "Brown Mushroom"
  86. block_id[40] = "Red Mushroom"
  87. block_id[41] = "Block of Gold"
  88. block_id[42] = "Block of Iron"
  89. block_id[43] = "Double Slabs"
  90. block_id[44] = "Slabs"
  91. block_id[45] = "Brick Block"
  92. block_id[46] = "TNT"
  93. block_id[47] = "Bookshelf"
  94. block_id[48] = "Moss Stone"
  95. block_id[49] = "Obsidian"
  96. block_id[50] = "Torch"
  97. block_id[51] = "Fire"
  98. block_id[52] = "Monster Spawner"
  99. block_id[53] = "Wooden Stairs"
  100. block_id[54] = "Chest"
  101. block_id[55] = "Redstone (Wire)"
  102. block_id[56] = "Diamond (Ore)"
  103. block_id[57] = "Block of Diamond"
  104. block_id[58] = "Crafting Table"
  105. block_id[59] = "Seeds"
  106. block_id[60] = "Farland"
  107. block_id[61] = "Furnace"
  108. block_id[62] = "Burning Furnace"
  109. block_id[63] = "Sign Post"
  110. block_id[64] = "Wooden Door"
  111. block_id[65] = "Ladders"
  112. block_id[66] = "Rails"
  113. block_id[67] = "Cobblestone Stairs"
  114. block_id[68] = "Wall Sign"
  115. block_id[69] = "Lever"
  116. block_id[70] = "Stone Pressure Plate"
  117. block_id[71] = "Iron Door"
  118. block_id[72] = "Wooden Pressure Plates"
  119. block_id[73] = "Redstone Ore"
  120. block_id[74] = "Glowing Redstone Ore"
  121. block_id[75] = "Redstone Torch"
  122. block_id[76] = "Redstone Torch"
  123. block_id[77] = "Stone Button "
  124. block_id[78] = "Snow"
  125. block_id[79] = "Ice"
  126. block_id[80] = "Snow Block"
  127. block_id[81] = "Cactus"
  128. block_id[82] = "Clay (Block)"
  129. block_id[83] = "Sugar Cane"
  130. block_id[84] = "Jukebox"
  131. block_id[85] = "Fence"
  132. block_id[86] = "Pumpkin"
  133. block_id[87] = "Netherrack"
  134. block_id[88] = "Soul Sand"
  135. block_id[89] = "Glowstone"
  136. block_id[90] = "Portal"
  137. block_id[91] = "Jack-O-Lantern"
  138. block_id[92] = "Cake Block"
  139. block_id[93] = "Redstone Repeater"
  140. block_id[94] = "Redstone Repeater"
  141. block_id[95] = "Locked Chest"
  142. block_id[96] = "Trapdoors"
  143. block_id[97] = "Hidden Silverfish"
  144. block_id[98] = "Stone Brick"
  145. block_id[99] = "Huge brown and red mushroom"
  146. block_id[100] = "Huge brown and red mushroom"
  147. block_id[101] = "Iron Bars"
  148. block_id[102] = "Glass Pane"
  149. block_id[103] = "Melon"
  150. block_id[104] = "Pumpkin Stem"
  151. block_id[105] = "Melon Stem"
  152. block_id[106] = "Vines"
  153. block_id[107] = "Fence Gate"
  154. block_id[108] = "Brick Stairs"
  155. block_id[109] = "Stone Brick Stairs"
  156. block_id[110] = "Mycelium"
  157. block_id[111] = "Lily Pad"
  158. block_id[112] = "Nether Brick"
  159. block_id[113] = "Nether Brick Fence"
  160. block_id[114] = "Nether Brick Stairs"
  161. block_id[115] = "Nether Wart"
  162. block_id[116] = "Enchantment Table"
  163. block_id[117] = "Brewing Stand"
  164. block_id[118] = "Cauldron"
  165. block_id[119] = "End Portal"
  166. block_id[120] = "End Portal Frame"
  167. block_id[121] = "End Stone "
  168. block_id[256] = "Iron Ingotron Shovel"
  169. block_id[257] = "Iron Pickaxe"
  170. block_id[258] = "Iron Axe"
  171. block_id[259] = "Flint and Steel"
  172. block_id[260] = "Red Apple"
  173. block_id[261] = "Bow"
  174. block_id[262] = "Arrow"
  175. block_id[263] = "Coal"
  176.  
  177. woolColors = {}
  178. woolColors[0] = "White"
  179. woolColors[1] = "Orange"
  180. woolColors[2] = "Magenta"
  181. woolColors[3] = "Light Blue"
  182. woolColors[4] = "Yellow"
  183. woolColors[5] = "Lime"
  184. woolColors[6] = "Pink"
  185. woolColors[7] = "Gray"
  186. woolColors[8] = "Light Gray"
  187. woolColors[9] = "Cyan"
  188. woolColors[10] = "Purple"
  189. woolColors[11] = "Blue"
  190. woolColors[12] = "Brown"
  191. woolColors[13] = "Green"
  192. woolColors[14] = "Red"
  193. woolColors[15] = "Black"
  194.  
  195. function scanInv()
  196.         local invList = {}
  197.         for i = 1,16 do
  198.                 turtle.select(i)
  199.                 local item = turtle.getItemDetail()
  200.                 if item then
  201.                         invList[i] = item.name
  202.                 end
  203.         end
  204.         return invList
  205. end
  206.  
  207. function ParseInv()
  208.         turtle.select(16)
  209.         turtle.dropUp()
  210.         while turtle.suck() do
  211.                 local item = turtle.getItemDetail()
  212.                 for i =1,16 do
  213.                         if item.name == invList[i] then
  214.                                 turtle.transferTo(i)
  215.                                 break
  216.                         end
  217.                         if item.name == "minecraft:lava_bucket" or item.name == "minecraft:coal" then
  218.                                 turtle.refuel(64)
  219.                         end
  220.                 end
  221.                 turtle.dropUp()
  222.         end
  223. end
  224.  
  225. function refill()
  226.         EnderChests("set")
  227.         ParseInv()
  228.         EnderChests("gather")
  229. end
  230.  
  231. function EnderChests(action)
  232.         --action = 'set' or 'gather'
  233.         if action == "set" then
  234.                 turtle.select(enderchest1)
  235.                 turtle.place()
  236.                 turtle.select(enderchest2)
  237.                 turtle.placeUp()
  238.         elseif action == "gather" then
  239.                 if chestOrder == 1 then
  240.                         turtle.select(enderchest1)
  241.                         turtle.digUp()
  242.                         turtle.select(enderchest2)
  243.                         turtle.dig()
  244.                         chestOrder = 2
  245.                 elseif chestOrder == 2 then
  246.                         turtle.select(enderchest1)
  247.                         turtle.dig()
  248.                         turtle.select(enderchest2)
  249.                         turtle.digUp()
  250.                         chestOrder = 1
  251.                 end
  252.         end
  253. end
  254.  
  255. function getBlockName(id, blockData)
  256.   blockData = blockData or nil
  257.   if(block_id[id] == nil) then
  258.     return "ID: "..tostring(id)..", Data: "..tostring(blockData)
  259.   else
  260.     if(blockData) then
  261.       if(id == 35) then
  262.         str = woolColors[blockData] .. " " .. block_id[id]
  263.         return str
  264.       end
  265.     end
  266.     return block_id[id]
  267.   end
  268. end
  269.  
  270. function getBlockId(x,y,z)
  271.   return blocks[y + z*width + x*length*width + 1]
  272. end
  273.  
  274. function getData(x,y,z)
  275.   return data[y + z*width + x*length*width + 1]
  276. end
  277.  
  278. function readbytes(h, n)
  279.   for i=1,n do
  280.     h.read()
  281.   end
  282. end
  283.  
  284. function readname(h)  
  285.   n1 = h.read()
  286.   n2 = h.read()
  287.  
  288.   if(n1 == nil or n2 == nil) then
  289.     return ""
  290.   end
  291.  
  292.   n = n1*256 + n2
  293.  
  294.   str = ""
  295.   for i=1,n do
  296.     c = h.read()
  297.     if c == nil then
  298.       return
  299.     end  
  300.     str = str .. string.char(c)
  301.   end
  302.   return str
  303. end
  304.  
  305. function parse(a, h, containsName)
  306.   containsName = containsName or true
  307.   if a==0 then
  308.     return
  309.   end
  310.   if containsName then
  311.     name = readname(h)
  312.   end
  313.    
  314.   if a==1 then
  315.     readbytes(h,1)  
  316.   elseif a==2 then
  317.     i1 = h.read()
  318.     i2 = h.read()
  319.     i = i1*256 + i2
  320.     if(name=="Height") then
  321.       height = i
  322.     elseif (name=="Length") then
  323.       length = i
  324.     elseif (name=="Width") then
  325.       width = i
  326.     end
  327.   elseif a==3 then
  328.     readbytes(h,4)
  329.   elseif a==4 then
  330.     readbytes(h,8)
  331.   elseif a==5 then
  332.     readbytes(h,4)
  333.   elseif a==6 then
  334.     readbytes(h,8)
  335.   elseif a==7 then
  336.     i1 = h.read()
  337.     i2 = h.read()
  338.     i3 = h.read()
  339.     i4 = h.read()
  340.     i = i1*256*256*256 + i2*256*256 + i3*256 + i4
  341.     if name == "Blocks" then
  342.       for i=1,i do
  343.         table.insert(blocks, h.read())
  344.       end
  345.     elseif name == "Data" then
  346.       for i=1,i do
  347.         table.insert(data, h.read())
  348.       end
  349.     else
  350.       readbytes(h,i)
  351.     end
  352.   elseif a==8 then
  353.     i1 = h.read()
  354.     i2 = h.read()
  355.     i = i1*256 + i2
  356.     readbytes(h,i)
  357.   elseif a==9 then
  358.         --readbytes(h,5)
  359.         type = h.read()
  360.         i1 = h.read()
  361.     i2 = h.read()
  362.     i3 = h.read()
  363.     i4 = h.read()
  364.     i = i1*256*256*256 + i2*256*256 + i3*256 + i4
  365.     for j=1,i do
  366.       parse(h.read(), h, false)
  367.     end
  368.   end
  369. end
  370.  
  371. function forward()
  372.   while not turtle.forward() do
  373.     turtle.dig()
  374.   end
  375. end
  376.  
  377. function up()
  378.   while not turtle.up() do
  379.     turtle.digUp()
  380.   end
  381. end
  382.  
  383. function down()
  384.   while not turtle.down() do
  385.     turtle.digDown()
  386.   end
  387. end
  388.  
  389. function place()
  390.   while not turtle.placeDown() do
  391.     turtle.digDown()
  392.   end
  393. end
  394.  
  395. h = fs.open(filename, "rb")
  396.  
  397. a = 0
  398. while (a ~= nil) do
  399.   a = h.read()
  400.   parse(a, h)
  401. end
  402.  
  403. write("length: " .. length)
  404. write("   width: " .. width)
  405. write("   height: " .. height .. "\n")
  406.  
  407. uniqueblocks={}
  408. for i,v in ipairs(blocks) do
  409.   found = false
  410.   for j,w in ipairs(uniqueblocks) do
  411.     -- for now, data is only accounted for when the block is whool
  412.     if (w.blockID==v and (w.data==data[i] or w.blockID ~= 35)) then
  413.       found = true
  414.       w.amount = w.amount + 1
  415.       break
  416.     end
  417.   end
  418.  
  419.   if found==false then
  420.     uniqueblocks[#uniqueblocks+1] = {}
  421.     uniqueblocks[#uniqueblocks].blockID = v
  422.     uniqueblocks[#uniqueblocks].data = data[i]
  423.     uniqueblocks[#uniqueblocks].amount = 1
  424.   end
  425. end
  426.  
  427. print("number of block types: " .. #uniqueblocks)
  428. for i,v in ipairs(uniqueblocks) do
  429.   if (i%9)==0 then
  430.     read()
  431.   end
  432.   print(" -" .. getBlockName(v.blockID, v.data) .. ": " .. v.amount)
  433. end
  434.  
  435. read()
  436.  
  437. print("Give the numbers of all slots containing the specified block type:")
  438.  
  439. slots={}
  440. for i,block in ipairs(uniqueblocks) do
  441.   blockData = block.data
  442.   print(" -in which slots is " .. getBlockName(block.blockID, blockData) .. "?")
  443.   if not slots[block.blockID] then
  444.     slots[block.blockID] = {}
  445.   end
  446.   slots[block.blockID][blockData] = {}
  447.   write("   ")
  448.   str = read()
  449.   for i = 1, #str do
  450.     local c = str:sub(i,i)
  451.     n = tonumber(c)
  452.     if(n) then
  453.       if(n>0 and n<10) then
  454.         table.insert(slots[block.blockID][blockData], n)
  455.       end
  456.     end
  457.   end
  458. end
  459.  
  460. print("Press key to start building...")
  461. read()
  462. invList = scanInv()
  463.  
  464. up()
  465. n = 1
  466. turtle.select(n)
  467.  
  468. for x=1,height do
  469.   for y=1,width do
  470.     for z=1,length do
  471.       blockID = getBlockId(x-1,y-1,z-1)
  472.       blockData = getData(x-1,y-1,z-1)
  473.       forward()
  474.       turtle.digDown()
  475.       slot_lst = slots[blockID][blockData]
  476.       if(slot_lst ~= nil) then
  477.         if(#slot_lst > 0) then
  478.           local found=false
  479.           for i,v in ipairs(slot_lst) do
  480.             if(turtle.getItemCount(v) > 0) then
  481.               found=true
  482.               turtle.select(v)
  483.               break
  484.             end
  485.           end
  486.           if not found then
  487.                 print("Not enough " .. getBlockName(blockID, blockData) .. ". Please refill...")
  488.                 local mem = turtle.getSelectedSlot()
  489.                 refill()
  490.                 turtle.select(mem)
  491.           end
  492.           place()
  493.           if turtle.getFuelLevel() < 200 then
  494.                 refill()
  495.           end
  496.           --save()
  497.         end
  498.       end
  499.     end
  500.     turtle.turnLeft()
  501.     forward()
  502.     turtle.turnLeft()
  503.     for i=1,length do
  504.       forward()
  505.     end
  506.     turtle.turnRight()
  507.     turtle.turnRight()
  508.   end
  509.   turtle.turnRight()
  510.   for i=1,width do
  511.     forward()
  512.   end
  513.   turtle.turnLeft()
  514.   up()
  515. end
  516.  
  517. for i=1,height+1 do
  518.   down()
  519. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement