Advertisement
Guest User

build

a guest
Apr 24th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.39 KB | None | 0 0
  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 forward()
  321.     if cDir == "north" then
  322.         cZ = cZ-1
  323.     elseif cDir == "east" then
  324.         cX = cX+1
  325.     elseif cDir == "south" then
  326.         cZ = cZ+1
  327.     elseif cDir == "west" then
  328.         cX = cX-1
  329.     else
  330.         print("Error!!")
  331.     end
  332.     while not turtle.forward() do
  333.         turtle.dig()
  334.     end
  335. end
  336.  
  337. function up()
  338.     cY = cY+1
  339.     while not turtle.up() do
  340.         turtle.digUp()
  341.     end
  342. end
  343.  
  344. function down()
  345.     cY = cY-1
  346.     while not turtle.down() do
  347.         turtle.digDown()
  348.     end
  349. end
  350.  
  351. function place()
  352.     while not turtle.placeDown() do
  353.         turtle.digDown()
  354.     end
  355. end
  356.  
  357. h = fs.open(filename, "rb")
  358.  
  359. a = 0
  360. while (a ~= nil) do
  361.     a = h.read()
  362.     parse(a, h)
  363. end
  364.  
  365. write("length: " .. length)
  366. write("   width: " .. width)
  367. write("   height: " .. height .. "\n")
  368.  
  369. uniqueblocks={}
  370. for i,v in ipairs(blocks) do
  371.     found = false
  372.     for j,w in ipairs(uniqueblocks) do
  373.         -- for now, data is only accounted for when the block is whool
  374.         if (w.blockID==v and (w.data==data[i] or w.blockID ~= 35)) then
  375.             found = true
  376.             w.amount = w.amount + 1
  377.             break
  378.         end
  379.     end
  380.    
  381.     if found==false then
  382.         uniqueblocks[#uniqueblocks+1] = {}
  383.         uniqueblocks[#uniqueblocks].blockID = v
  384.         uniqueblocks[#uniqueblocks].data = data[i]
  385.         uniqueblocks[#uniqueblocks].amount = 1
  386.     end
  387. end
  388.  
  389. print("number of block types: " .. #uniqueblocks)
  390. for i,v in ipairs(uniqueblocks) do
  391.     if (i%9)==0 then
  392.         read()
  393.     end
  394.     print(" -" .. getBlockName(v.blockID, v.data) .. ": " .. v.amount)
  395. end
  396.  
  397. read()
  398.  
  399. print("Give the numbers of all slots containing the specified block type:")
  400.  
  401. slots={}
  402. for i,block in ipairs(uniqueblocks) do
  403.     blockData = block.data
  404.     print(" -in which slots is " .. getBlockName(block.blockID, blockData) .. "?")
  405.     if not slots[block.blockID] then
  406.         slots[block.blockID] = {}
  407.     end
  408.     slots[block.blockID][blockData] = {}
  409.     write("   ")
  410.     str = read()
  411.     for i = 1, #str do
  412.         local c = str:sub(i,i)
  413.         n = tonumber(c)
  414.         if(n) then
  415.           if(n>0 and n<10) then
  416.             table.insert(slots[block.blockID][blockData], n)
  417.         end
  418.     end
  419.   end
  420. end
  421.  
  422. print("Press key to start building...")
  423. read()
  424.  
  425. up()
  426. n = 1
  427. turtle.select(n)
  428.  
  429. for x=1,height do
  430.     for y=1,width do
  431.         for z=1,length do
  432.             blockID = getBlockId(x-1,y-1,z-1)
  433.             blockData = getData(x-1,y-1,z-1)
  434.             forward()
  435.             turtle.digDown()
  436.             slot_lst = slots[blockID][blockData]
  437.             if(slot_lst ~= nil) then
  438.               if(#slot_lst > 0) then
  439.                 local found=false
  440.                 for i,v in ipairs(slot_lst) do
  441.                     if(turtle.getItemCount(v) > 0) then
  442.                       found=true
  443.                       turtle.select(v)
  444.                       break
  445.                   end
  446.               end
  447.               if not found then
  448.                   print("Not enough " .. getBlockName(blockID, blockData) .. ". Please refill...")
  449.                    shell.run("moveTo tonumber(cX) tonumber(cY) tonumber(cZ) tonumber(cDir) tonumber(tArgs[2]) tonumber(tArgs[3]) tonumber(tArgs[4]) tostring(tArgs[5]")
  450.                   for i=1,16 do
  451.                   turtle.suckdown()
  452.               end
  453.             end
  454.           place()
  455.           end
  456.       end
  457.     end
  458.     turnLeft()
  459.     forward()
  460.     turnLeft()
  461.     for i=1,length do
  462.         forward()
  463.     end
  464.     turnRight()
  465.     turnRight()
  466.   end
  467.     turnRight()
  468.   for i=1,width do
  469.       forward()
  470.   end
  471.   turnLeft()
  472.   up()
  473. end
  474.  
  475. for i=1,height+1 do
  476.     down()
  477. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement