tomtrein

Untitled

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