Advertisement
guitarplayer616

[SchemParser] schem turtle

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