Advertisement
guitarplayer616

Schematic Builder WIP v2

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