Advertisement
guitarplayer616

Setup FIxed

Dec 21st, 2016
367
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. --local type
  4.  
  5. function save(name,table)
  6.     local h = fs.open(name,"w")
  7.     table = textutils.serialize(table)
  8.     h.write(table)
  9.     h.close()
  10. end
  11.  
  12.  
  13. local block_id = {}
  14.  
  15. block_id[0] = "Air"
  16. block_id[1] = "Stone"
  17. block_id[2] = "Grass"
  18. block_id[3] = "Dirt"
  19. block_id[4] = "Cobblestone"
  20. block_id[5] = "Wooden Plank"
  21. block_id[6] = "Sapling"
  22. block_id[7] = "Bedrock"
  23. block_id[8] = "Water"
  24. block_id[9] = "Stationary water"
  25. block_id[10] = "Lava"
  26. block_id[11] = "Stationary lava"
  27. block_id[12] = "Sand"
  28. block_id[13] = "Gravel"
  29. block_id[14] = "Gold Ore"
  30. block_id[15] = "Iron (Ore)"
  31. block_id[16] = "Coal Ore"
  32. block_id[17] = "Log"
  33. block_id[18] = "Leaves"
  34. block_id[19] = "Sponge"
  35. block_id[20] = "Glass"
  36. block_id[21] = "Lapis Lazuli (Ore)"
  37. block_id[22] = "Lapis Lazuli (Block)"
  38. block_id[23] = "Dispenser"
  39. block_id[24] = "Sandstone"
  40. block_id[25] = "Note Block Tile entity"
  41. block_id[26] = "Bed"
  42. block_id[27] = "Powered Rail "
  43. block_id[28] = "Detector Rail "
  44. block_id[29] = "Sticky Piston"
  45. block_id[30] = "Cobweb"
  46. block_id[31] = "Tall Grass"
  47. block_id[32] = "Dead Bush"
  48. block_id[33] = "Piston"
  49. block_id[34] = "Piston Extension"
  50. block_id[35] = "Wool"
  51. block_id[36] = "Block moved by Piston"
  52. block_id[37] = "Dandelionandelion"
  53. block_id[38] = "Rose"
  54. block_id[39] = "Brown Mushroom"
  55. block_id[40] = "Red Mushroom"
  56. block_id[41] = "Block of Gold"
  57. block_id[42] = "Block of Iron"
  58. block_id[43] = "Double Slabs"
  59. block_id[44] = "Slabs"
  60. block_id[45] = "Brick Block"
  61. block_id[46] = "TNT"
  62. block_id[47] = "Bookshelf"
  63. block_id[48] = "Moss Stone"
  64. block_id[49] = "Obsidian"
  65. block_id[50] = "Torch"
  66. block_id[51] = "Fire"
  67. block_id[52] = "Monster Spawner"
  68. block_id[53] = "Wooden Stairs"
  69. block_id[54] = "Chest"
  70. block_id[55] = "Redstone (Wire)"
  71. block_id[56] = "Diamond (Ore)"
  72. block_id[57] = "Block of Diamond"
  73. block_id[58] = "Crafting Table"
  74. block_id[59] = "Seeds"
  75. block_id[60] = "Farland"
  76. block_id[61] = "Furnace"
  77. block_id[62] = "Burning Furnace"
  78. block_id[63] = "Sign Post"
  79. block_id[64] = "Wooden Door"
  80. block_id[65] = "Ladders"
  81. block_id[66] = "Rails"
  82. block_id[67] = "Cobblestone Stairs"
  83. block_id[68] = "Wall Sign"
  84. block_id[69] = "Lever"
  85. block_id[70] = "Stone Pressure Plate"
  86. block_id[71] = "Iron Door"
  87. block_id[72] = "Wooden Pressure Plates"
  88. block_id[73] = "Redstone Ore"
  89. block_id[74] = "Glowing Redstone Ore"
  90. block_id[75] = "Redstone Torch"
  91. block_id[76] = "Redstone Torch"
  92. block_id[77] = "Stone Button "
  93. block_id[78] = "Snow"
  94. block_id[79] = "Ice"
  95. block_id[80] = "Snow Block"
  96. block_id[81] = "Cactus"
  97. block_id[82] = "Clay (Block)"
  98. block_id[83] = "Sugar Cane"
  99. block_id[84] = "Jukebox"
  100. block_id[85] = "Fence"
  101. block_id[86] = "Pumpkin"
  102. block_id[87] = "Netherrack"
  103. block_id[88] = "Soul Sand"
  104. block_id[89] = "Glowstone"
  105. block_id[90] = "Portal"
  106. block_id[91] = "Jack-O-Lantern"
  107. block_id[92] = "Cake Block"
  108. block_id[93] = "Redstone Repeater"
  109. block_id[94] = "Redstone Repeater"
  110. block_id[95] = "Locked Chest"
  111. block_id[96] = "Trapdoors"
  112. block_id[97] = "Hidden Silverfish"
  113. block_id[98] = "Stone Brick"
  114. block_id[99] = "Huge brown and red mushroom"
  115. block_id[100] = "Huge brown and red mushroom"
  116. block_id[101] = "Iron Bars"
  117. block_id[102] = "Glass Pane"
  118. block_id[103] = "Melon"
  119. block_id[104] = "Pumpkin Stem"
  120. block_id[105] = "Melon Stem"
  121. block_id[106] = "Vines"
  122. block_id[107] = "Fence Gate"
  123. block_id[108] = "Brick Stairs"
  124. block_id[109] = "Stone Brick Stairs"
  125. block_id[110] = "Mycelium"
  126. block_id[111] = "Lily Pad"
  127. block_id[112] = "Nether Brick"
  128. block_id[113] = "Nether Brick Fence"
  129. block_id[114] = "Nether Brick Stairs"
  130. block_id[115] = "Nether Wart"
  131. block_id[116] = "Enchantment Table"
  132. block_id[117] = "Brewing Stand"
  133. block_id[118] = "Cauldron"
  134. block_id[119] = "End Portal"
  135. block_id[120] = "End Portal Frame"
  136. block_id[121] = "End Stone "
  137. block_id[256] = "Iron Ingotron Shovel"
  138. block_id[257] = "Iron Pickaxe"
  139. block_id[258] = "Iron Axe"
  140. block_id[259] = "Flint and Steel"
  141. block_id[260] = "Red Apple"
  142. block_id[261] = "Bow"
  143. block_id[262] = "Arrow"
  144. block_id[263] = "Coal"
  145.  
  146. local woolColors = {}
  147. woolColors[0] = "White"
  148. woolColors[1] = "Orange"
  149. woolColors[2] = "Magenta"
  150. woolColors[3] = "Light Blue"
  151. woolColors[4] = "Yellow"
  152. woolColors[5] = "Lime"
  153. woolColors[6] = "Pink"
  154. woolColors[7] = "Gray"
  155. woolColors[8] = "Light Gray"
  156. woolColors[9] = "Cyan"
  157. woolColors[10] = "Purple"
  158. woolColors[11] = "Blue"
  159. woolColors[12] = "Brown"
  160. woolColors[13] = "Green"
  161. woolColors[14] = "Red"
  162. woolColors[15] = "Black"
  163.  
  164. --[[
  165. local tArgs = { ... }
  166. if #tArgs ~= 1 then
  167.   print("Usage: build <gunzipped schematic file>")
  168.   return
  169. end
  170.  
  171. local filename = tArgs[1]
  172.  
  173. if not fs.exists(filename) then
  174.   print("File does not exist.")
  175.   return
  176. end
  177. ]]--
  178.  
  179.  
  180. local length = 0
  181. local height = 0
  182. local width = 0
  183. local blocks = {}
  184. local data = {}
  185.  
  186. function getBlockName(id, blockData)
  187.   blockData = blockData or nil
  188.   if(block_id[id] == nil) then
  189.     return "UNKNOWN"
  190.   else
  191.     if(blockData) then
  192.       if(id == 35) then
  193.         str = woolColors[blockData] .. " " .. block_id[id]
  194.         return str
  195.       end
  196.     end
  197.     return block_id[id]
  198.   end
  199. end
  200.  
  201. function getBlockId(x,y,z)
  202.   return blocks[y + z*width + x*length*width + 1]
  203. end
  204.  
  205. function getData(x,y,z)
  206.   return data[y + z*width + x*length*width + 1]
  207. end
  208.  
  209. function readbytes(h, n)
  210.   for i=1,n do
  211.     h.read()
  212.   end
  213. end
  214.  
  215. function readname(h)  
  216.   local n1 = h.read()
  217.   local n2 = h.read()
  218.  
  219.   if(n1 == nil or n2 == nil) then
  220.     return ""
  221.   end
  222.  
  223.   local n = n1*256 + n2
  224.  
  225.   local str = ""
  226.   for i=1,n do
  227.     local c = h.read()
  228.     if c == nil then
  229.       return
  230.     end  
  231.     str = str .. string.char(c)
  232.   end
  233.   return str
  234. end
  235.  
  236. function parse(a, h, containsName)
  237.   local containsName = containsName or true
  238.   local i,i1,i2,i3,i4
  239.   if a==0 then
  240.     return
  241.   end
  242.   if containsName then
  243.     name = readname(h)
  244.   end
  245.    
  246.   if a==1 then
  247.     readbytes(h,1)  
  248.   elseif a==2 then
  249.     i1 = h.read()
  250.     i2 = h.read()
  251.     i = i1*256 + i2
  252.     if(name=="Height") then
  253.       height = i
  254.     elseif (name=="Length") then
  255.       length = i
  256.     elseif (name=="Width") then
  257.       width = i
  258.     end
  259.   elseif a==3 then
  260.     readbytes(h,4)
  261.   elseif a==4 then
  262.     readbytes(h,8)
  263.   elseif a==5 then
  264.     readbytes(h,4)
  265.   elseif a==6 then
  266.     readbytes(h,8)
  267.   elseif a==7 then
  268.     i1 = h.read()
  269.     i2 = h.read()
  270.     i3 = h.read()
  271.     i4 = h.read()
  272.     i = i1*256*256*256 + i2*256*256 + i3*256 + i4
  273.     if name == "Blocks" then
  274.       for i=1,i do
  275.         table.insert(blocks, h.read())
  276.       end
  277.     elseif name == "Data" then
  278.       for i=1,i do
  279.         table.insert(data, h.read())
  280.       end
  281.     else
  282.       readbytes(h,i)
  283.     end
  284.   elseif a==8 then
  285.     i1 = h.read()
  286.     i2 = h.read()
  287.     i = i1*256 + i2
  288.     readbytes(h,i)
  289.   elseif a==9 then
  290.     --readbytes(h,5)
  291.     local type = h.read()
  292.     i1 = h.read()
  293.     i2 = h.read()
  294.     i3 = h.read()
  295.     i4 = h.read()
  296.     i = i1*256*256*256 + i2*256*256 + i3*256 + i4
  297.     for j=1,i do
  298.       parse(h.read(), h, false)
  299.     end
  300.   end
  301. end
  302.  
  303. function forward()
  304.   while not turtle.forward() do
  305.     turtle.dig()
  306.   end
  307. end
  308.  
  309. function up()
  310.   while not turtle.up() do
  311.     turtle.digUp()
  312.   end
  313. end
  314.  
  315. function down()
  316.   while not turtle.down() do
  317.     turtle.digDown()
  318.   end
  319. end
  320.  
  321. function place()
  322.   while not turtle.placeDown() do
  323.     turtle.digDown()
  324.   end
  325. end
  326.  
  327.  
  328. function setup(filename)
  329. --input file
  330. --returns blocks,data
  331. --requires parse, getBlockName
  332.     if not fs.exists(filename) then
  333.         error("File "..tostring(filename).." does not exist.")
  334.     end
  335.     h = fs.open(filename, "rb")
  336.  
  337.     a = 0
  338.     while (a ~= nil) do
  339.         a = h.read()
  340.         parse(a, h)
  341.     end
  342.  
  343.     write("length: " .. length)
  344.     write("   width: " .. width)
  345.     write("   height: " .. height .. "\n")
  346.    
  347.     uniqueblocks={}
  348.     for i,v in ipairs(blocks) do
  349.         found = false
  350.         for j,w in ipairs(uniqueblocks) do
  351.  
  352.             --[[if (w.blockID==v and (w.data==data[i] or w.blockID ~= 35)) then
  353.                 found = true
  354.                 w.amount = w.amount + 1
  355.                 break
  356.             end]]--
  357.  
  358.             --for now, data is only accounted for when the block is whool
  359.             if (w.blockID==v) and (w.data==data[i]) then
  360.                 found = true
  361.                 w.amount = w.amount + 1
  362.                 break
  363.             end
  364.            
  365.            
  366.  
  367.  
  368.         end
  369.  
  370.         if found==false then
  371.             uniqueblocks[#uniqueblocks+1] = {}
  372.             uniqueblocks[#uniqueblocks].blockID = v
  373.             uniqueblocks[#uniqueblocks].data = data[i]
  374.             uniqueblocks[#uniqueblocks].amount = 1
  375.         end
  376.     end
  377.  
  378.     print("number of block types: " .. #uniqueblocks)
  379.     for i,v in ipairs(uniqueblocks) do
  380.         if (i%9)==0 then
  381.             read()
  382.         end
  383.         print(" -" .. getBlockName(v.blockID, v.data) .. ": " .. v.amount)
  384.     end
  385.  
  386.     read()
  387.  
  388.     print("Give the numbers of all slots containing the specified block type:")
  389.  
  390.     slots={}
  391.     for i,block in ipairs(uniqueblocks) do
  392.         blockData = block.data
  393.         print(" -in which slots is " .. getBlockName(block.blockID, blockData) .. "?")
  394.         if not slots[block.blockID] then
  395.             slots[block.blockID] = {}
  396.         end
  397.         slots[block.blockID][blockData] = {}
  398.         write("   ")
  399.         str = read()
  400.         for i = 1, #str do
  401.             local c = str:sub(i,i)
  402.             n = tonumber(c)
  403.             if(n) then
  404.                 if(n>0 and n<10) then
  405.                     table.insert(slots[block.blockID][blockData], n)
  406.                 end
  407.             end
  408.         end
  409.     end
  410.    
  411.     h.close()
  412.     save("slots",slots)
  413.     save("blocks",blocks)
  414.     save("data",data)
  415.     save("uniqueblocks",uniqueblocks)
  416. end
  417.  
  418.  
  419. setup(...)
  420. -- [[ Iterators ]] --
  421.  
  422. function checkIfAir()
  423.     --finds the location to place the next block
  424.     while true do
  425.     x,y,z = iterate(x,y,z,startx,starty,startz,height-1,width-1,length-1)
  426.         --makes the turtle build faster by having to travel less
  427.         blockID2 = getBlockId(x,y,z)    -- temporary variable
  428.         blockData2 = getData(x,y,z)     -- temporary variable
  429.         if slots[blockID2] then
  430.             -- makes sure the next block to place at location is not air
  431.             slot_2nd = slots[blockID2][blockData2]
  432.             if slot_2nd then
  433.                 if #slot_2nd > 0 then
  434.                     recordObj(x,y,z)
  435.                     break
  436.                 end
  437.             end
  438.         end
  439.     end
  440. end
  441.  
  442. function check(x,y,z,startx,starty,startz)
  443.     if x%2==startx%2 then
  444.         oddx = true
  445.         if y%2==starty%2 then
  446.             oddy = true
  447.         else
  448.             oddy = false
  449.         end
  450.     else
  451.         oddx = false
  452.         if y%2==starty%2 then
  453.             oddy = false
  454.         else
  455.             oddy = true
  456.         end
  457.     end
  458.     return oddx,oddy
  459. end
  460.  
  461.  
  462. function Yiterate(x,y,z,startx,starty,startz,finalx,finaly,finalz)
  463.    
  464.     local height = finalx
  465.     local width = finaly
  466.     local length = finalz
  467.    
  468.     if oddx then
  469.         if y < width then
  470.             y = y + 1
  471.         elseif y == width then
  472.             if x < height then
  473.                 x = x + 1
  474.             --elseif x == height then
  475.                 --x,y,z = "max","max","max"
  476.             end
  477.         end
  478.     else
  479.         if y <= starty then
  480.             if x < height then
  481.                 x = x + 1
  482.             elseif x == height then
  483.                 x = "max"
  484.                 y = "max"
  485.                 z = "max"
  486.             end
  487.         else
  488.             y=y-1
  489.         end
  490.     end
  491.     return x,y,z
  492. end
  493.  
  494. function iterate(x,y,z,startx,starty,startz,finalx,finaly,finalz)
  495.    
  496.     local height = finalx
  497.     local width = finaly
  498.     local length = finalz
  499.    
  500.    
  501.     local oddx,oddy = check(x,y,z,startx,starty,startz)
  502.     if z == length and oddy then
  503.         x,y,z = Yiterate(x,y,z,startx,starty,startz,finalx,finaly,finalz)    
  504.     elseif z == startz and oddy then
  505.         z = z + 1
  506.     elseif z == startz and (not oddy) then
  507.         x,y,z = Yiterate(x,y,z,startx,starty,startz,finalx,finaly,finalz)
  508.     elseif z==length and (not oddy) then
  509.         z = z - 1
  510.  
  511.     elseif z < length then
  512.         if oddy then
  513.             z = z + 1
  514.         else
  515.             z = z - 1
  516.         end
  517.     end
  518.     return x,y,z
  519.  end
  520.  
  521.  
  522. -- [[ schematic --> blueprint ]] --
  523.  
  524. --turtles[i].instructions[n] = {x,y,z,id,data}
  525. --i = multiturtle ie 1,2,3,4 ; n = step ie 1 - 256 (no air)
  526.  
  527. --fn splits 16x16 grid between 4 turtles returning startx,y,z and endx,y,z of 4 4x4 grids
  528. --fn takes each 4x4 grid and turns them into instructions[n] = {x,y,z,id,data}
  529. --fn for master turtle to setup all slave turtles with the instructions[n] table and goto/find/place functions
  530.  
  531.  
  532. function blueprint()
  533.     --uses iterator to make instructions[n] table
  534.     local x,y,z = 0,0,0
  535.     local instructions = {}
  536.     while true do
  537.         if x == "max" then
  538.             break
  539.         end
  540.         local id = getBlockId(x,y,z)
  541.         local data = getData(x,y,z)
  542.         if id > 0 then
  543.             table.insert(instructions,{x,y,z,id,data})
  544.         end
  545.         x,y,z = iterate(x,y,z,0,0,0,height-1,width-1,length-1)
  546.     end
  547.     return instructions
  548. end
  549.  
  550.  
  551. function createInstructions(startx,starty,startz,height,width,length,nTurtles,placeMode)
  552.  
  553.      local x,y,z = startx,starty,startz
  554.      instructions = {}
  555.      nTurtles = nTurtles or 1
  556.      --startx,starty,startz = startx,starty,startz or 0,0,0
  557.      placeMode = placeMode or "horizontal"
  558.      local oTurtles = {}
  559.    
  560.     --if horizontal
  561.     for i = 1,nTurtles do
  562.         oTurtles[i] = {}
  563.         if i == tonumber(nTurtles) then
  564.             oTurtles[tonumber(nTurtles)].responsibleLength = math.floor(x/nTurtles) + (x - (math.floor(x/nTurtles)*nTurtles))
  565.         else
  566.             oTurtles[i].responsibleLength =  math.floor(x/nTurtles)
  567.         end
  568.         oTurtles[i].startHeight = startx
  569.         if i == 1 then
  570.             oTurtles[i].startWidth = starty
  571.         else
  572.             oTurtles[i].startWidth = oTurtles[i-1].startWidth + oTurtles[i].responsibleLength
  573.         end
  574.         oTurtles[i].startLength = startz
  575.         oTurtles[i].finalHeight = height-1
  576.         oTurtles[i].finalWidth = oTurtles[i].startWidth + oTurtles[i].responsibleLength - 1
  577.         oTurtles[i].finalLength = length-1
  578.         instructions[i] = {}
  579.     end
  580.    
  581.    
  582.  
  583.         for i = 1,nTurtles do
  584.             x,y,z = startx,starty,startz
  585.            
  586.            
  587.             while true do
  588.                 local n = #instructions[i]+1
  589.                 instructions[i][n]={}
  590.                 instructions[i][n].x,instructions[1][n].y,instructions[1][n].z = x,y,z
  591.                 instructions[i][n].placeMode = placeMode
  592.                 instructions[i][n].id = getBlockId(x,y,z)
  593.                 instructions[i][n].data = getData(x,y,z)
  594.                 x,y,z = iterate(x,y,z,
  595.                     oTurtles[i].startHeight, oTurtles[i].startWidth, oTurtles[i].startLength,
  596.                     oTurtles[i].finalHeight, oTurtles[i].finalWidth, oTurtles[i].finalLength
  597.                 )
  598.                 --attach methods here
  599.             end
  600.            
  601.            
  602.         end
  603.     return instructions
  604. end
  605.  
  606. local ins = blueprint()
  607. save("instructions",ins)
  608.  
  609. --[[
  610. print("Press key to start building...")
  611. read()
  612.  
  613. up()
  614. n = 1
  615. turtle.select(n)
  616.  
  617. for x=1,height do
  618.   for y=1,width do
  619.     for z=1,length do
  620.       blockID = getBlockId(x-1,y-1,z-1)
  621.       blockData = getData(x-1,y-1,z-1)
  622.       forward()
  623.       turtle.digDown()
  624.       slot_lst = slots[blockID][blockData]
  625.       if(slot_lst ~= nil) then
  626.         if(#slot_lst > 0) then
  627.           local found=false
  628.           for i,v in ipairs(slot_lst) do
  629.             if(turtle.getItemCount(v) > 0) then
  630.               found=true
  631.               turtle.select(v)
  632.               break
  633.             end
  634.           end
  635.           if not found then
  636.             print("Not enough " .. getBlockName(blockID, blockData) .. ". Please refill...")
  637.             read()
  638.           end
  639.           place()
  640.         end
  641.       end
  642.     end
  643.     turtle.turnLeft()
  644.     forward()
  645.     turtle.turnLeft()
  646.     for i=1,length do
  647.       forward()
  648.     end
  649.     turtle.turnRight()
  650.     turtle.turnRight()
  651.   end
  652.   turtle.turnRight()
  653.   for i=1,width do
  654.     forward()
  655.   end
  656.   turtle.turnLeft()
  657.   up()
  658. end
  659.  
  660. for i=1,height+1 do
  661.   down()
  662. end]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement