Advertisement
guitarplayer616

Turtle Schematic Builder SimpleApproach

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