Advertisement
bob558

Bild

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