Advertisement
guitarplayer616

Turtle Schematic Builder v3

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