Advertisement
1ng0

Turtle Schematic Builder

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