Advertisement
guitarplayer616

Schematic Builder

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