Advertisement
bob558

Bild-2

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