Advertisement
Link712011

Chest sorter rype

Sep 30th, 2018
501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 17.37 KB | None | 0 0
  1. local LENTH_MAX = 21
  2. local PASTE_ID = "e9EasxCL"
  3. local SCRIPT_NAME = "test"
  4. local UNKNOWN_LIST = "unsortable_items"
  5. local arg = {...}
  6. local LOG_REPORT_ID = 633
  7.  
  8. function pastebin_download(code, name)
  9.     fs.delete(name)
  10.     if pcall(shell.run, "pastebin", "get", code, name) then
  11.         print(name, "downloaded successfully !")
  12.     else
  13.         print("Failed to download ", name)
  14.     end
  15. end
  16.  
  17. function init_item_list(lenght)
  18.     local tab
  19.     local i
  20.  
  21.     tab = {}
  22.     i = 1
  23.     while i <= lenght do
  24.         tab[i] = {}
  25.         i = i + 1
  26.     end
  27.     return tab
  28. end
  29.  
  30. function get_item_list_right()
  31.     local right
  32.  
  33.     right = init_item_list(LENTH_MAX)
  34.     right[1][#right[1] + 1] = "minecraft:cobblestone"
  35.     right[2][#right[2] + 1] = "minecraft:dirt"
  36.     right[3][#right[3] + 1] = "minecraft:log"
  37.     right[3][#right[3] + 1] = "minecraft:log2"
  38.     right[4][#right[4] + 1] = "minecraft:glass"
  39.     right[5][#right[5] + 1] = "minecraft:sand"
  40.     right[6][#right[6] + 1] = "minecraft:stone,0"
  41.     right[7][#right[7] + 1] = "thaumcraft:crystal_essence"
  42.     right[8][#right[8] + 1] = ""
  43.     right[9][#right[9] + 1] = "minecraft:grass"
  44.     right[10][#right[10] + 1] = "minecraft:gold_ore"
  45.     right[10][#right[10] + 1] = "minecraft:iron_ore"
  46.     right[10][#right[10] + 1] = "thaumcraft:ore_cinnabar"
  47.     right[11][#right[11] + 1] = "minecraft:iron_ingot"
  48.     right[12][#right[12] + 1] = "minecraft:gold_ingot"
  49.     right[13][#right[13] + 1] = "minecraft:gold_block"
  50.     right[13][#right[13] + 1] = "minecraft:diamond"
  51.     right[14][#right[14] + 1] = "minecraft:emerald"
  52.     right[15][#right[15] + 1] = "minecraft:gravel"
  53.     right[16][#right[16] + 1] = "minecraft:stone,5"
  54.     right[17][#right[17] + 1] = "minecraft:stone,3"
  55.     right[18][#right[18] + 1] = "minecraft:stone,1"
  56.     right[19][#right[19] + 1] = "chisel:marble2"
  57.     right[20][#right[20] + 1] = "chisel:limestone2"
  58.     right[21][#right[21] + 1] = "minecraft:rotten_flesh"
  59.     return right
  60. end
  61.  
  62. function get_item_list_left()
  63.     local right
  64.  
  65.     right = init_item_list(LENTH_MAX)
  66.     right[1][#right[1] + 1] = "minecraft:wooden_slab"
  67.     right[1][#right[1] + 1] = "minecraft:stone_slab"
  68.     right[1][#right[1] + 1] = "minecraft:birch_stairs"
  69.     right[1][#right[1] + 1] = "minecraft:spruce_stairs"
  70.     right[1][#right[1] + 1] = "minecraft:stone_stairs"
  71.     right[1][#right[1] + 1] = "minecraft:brick_stairs"
  72.     right[1][#right[1] + 1] = "minecraft:sandstone_stairs"
  73.     right[1][#right[1] + 1] = "minecraft:carpet"
  74.     right[1][#right[1] + 1] = "minecraft:oak_stairs"
  75.     right[1][#right[1] + 1] = "minecraft:nether_brick_stairs"
  76.     right[1][#right[1] + 1] = "minecraft:quartz_stairs"
  77.     right[2][#right[2] + 1] = "minecraft:birch_fence"
  78.     right[2][#right[2] + 1] = "minecraft:fence_gate"
  79.     right[2][#right[2] + 1] = "minecraft:oak_fence"
  80.     right[2][#right[2] + 1] = "minecraft:nether_brick_fence"
  81.     right[2][#right[2] + 1] = "minecraft:spruce_fence"
  82.     right[2][#right[2] + 1] = "minecraft:jungle_fence"
  83.     right[2][#right[2] + 1] = "minecraft:dark_oak_fence"
  84.     right[2][#right[2] + 1] = "minecraft:acacia_fence"
  85.     right[2][#right[2] + 1] = "minecraft:birch_fence_gate"
  86.     right[2][#right[2] + 1] = "minecraft:oak_fence_gate"
  87.     right[2][#right[2] + 1] = "minecraft:spruce_fence_gate"
  88.     right[2][#right[2] + 1] = "minecraft:jungle_fence_gate"
  89.     right[2][#right[2] + 1] = "minecraft:dark_oak_fence_gate"
  90.     right[2][#right[2] + 1] = "minecraft:acacia_fence_gate"
  91.     right[2][#right[2] + 1] = "minecraft:cobblestone_wall"
  92.     right[2][#right[2] + 1] = "minecraft:iron_bars"
  93.     right[2][#right[2] + 1] = "minecraft:stained_glass_pane"
  94.     right[2][#right[2] + 1] = "minecraft:glass_pane"
  95.     right[2][#right[2] + 1] = "minecraft:fence"
  96.     right[3][#right[3] + 1] = "minecraft:flint"
  97.     right[4][#right[4] + 1] = "minecraft:sandstone"
  98.     right[5][#right[5] + 1] = "OpenBlocks:technicolorGlasses"
  99.     right[5][#right[5] + 1] = "ComputerCraft:CC-TurtleAdvanced"
  100.     right[5][#right[5] + 1] = "ComputerCraft:CC-Turtle"
  101.     right[5][#right[5] + 1] = "ComputerCraft:treasureDisk"
  102.     right[5][#right[5] + 1] = "ComputerCraft:CC-Computer"
  103.     right[5][#right[5] + 1] = "ComputerCraft:CC-Peripheral"
  104.     right[5][#right[5] + 1] = "ComputerCraft:disk"
  105.     right[5][#right[5] + 1] = "ComputerCraft:advanced_modem"
  106.     right[5][#right[5] + 1] = "ComputerCraft:CC-Cable"
  107.     right[5][#right[5] + 1] = "ComputerCraft:pocketComputer"
  108.     right[5][#right[5] + 1] = "ComputerCraft:CC-TurtleExpanded"
  109.     right[6][#right[6] + 1] = "minecraft:potion"
  110.     right[6][#right[6] + 1] = "minecraft:sugar"
  111.     right[6][#right[6] + 1] = "minecraft:nether_wart"
  112.     right[6][#right[6] + 1] = "minecraft:fish,3"
  113.     right[6][#right[6] + 1] = "minecraft:magma_cream"
  114.     right[6][#right[6] + 1] = "minecraft:glass_bottle"
  115.     right[6][#right[6] + 1] = "minecraft:blaze_rod"
  116.     right[6][#right[6] + 1] = "minecraft:nether_star"
  117.     right[6][#right[6] + 1] = "minecraft:blaze_powder"
  118.     right[6][#right[6] + 1] = "minecraft:spider_eye"
  119.     right[6][#right[6] + 1] = "minecraft:brewing_stand"
  120.     right[6][#right[6] + 1] = "minecraft:fermented_spider_eye"
  121.     right[6][#right[6] + 1] = "minecraft:golden_apple"
  122.     right[6][#right[6] + 1] = "minecraft:speckled_melon"
  123.     right[6][#right[6] + 1] = "minecraft:ender_eye"
  124.     right[7][#right[7] + 1] = "minecraft:anvil"
  125.     right[7][#right[7] + 1] = "minecraft:hopper"
  126.     right[7][#right[7] + 1] = "minecraft:chest"
  127.     right[7][#right[7] + 1] = "minecraft:bed"
  128.     right[7][#right[7] + 1] = "minecraft:item_frame"
  129.     right[7][#right[7] + 1] = "minecraft:repeater"
  130.     right[7][#right[7] + 1] = "minecraft:torch"
  131.     right[7][#right[7] + 1] = "minecraft:redstone_lamp"
  132.     right[7][#right[7] + 1] = "minecraft:piston"
  133.     right[7][#right[7] + 1] = "minecraft:sticky_piston"
  134.     right[7][#right[7] + 1] = "minecraft:tripwire_hook"
  135.     right[7][#right[7] + 1] = "minecraft:rail"
  136.     right[7][#right[7] + 1] = "minecraft:golden_rail"
  137.     right[7][#right[7] + 1] = "minecraft:birch_door"
  138.     right[7][#right[7] + 1] = "minecraft:oak_door"
  139.     right[7][#right[7] + 1] = "minecraft:spruce_door"
  140.     right[7][#right[7] + 1] = "minecraft:jungle_door"
  141.     right[7][#right[7] + 1] = "minecraft:dark_oak_door"
  142.     right[7][#right[7] + 1] = "minecraft:acacia_door"
  143.     right[7][#right[7] + 1] = "minecraft:wooden_door"
  144.     right[7][#right[7] + 1] = "minecraft:redstone_torch"
  145.     right[7][#right[7] + 1] = "minecraft:dispenser"
  146.     right[7][#right[7] + 1] = "minecraft:furnace"
  147.     right[7][#right[7] + 1] = "minecraft:crafting_table"
  148.     right[7][#right[7] + 1] = "minecraft:trapped_chest"
  149.     right[7][#right[7] + 1] = "minecraft:dropper"
  150.     right[7][#right[7] + 1] = "minecraft:trapdoor"
  151.     right[7][#right[7] + 1] = "minecraft:stone_button"
  152.     right[7][#right[7] + 1] = "minecraft:wooden_pressure_plate"
  153.     right[7][#right[7] + 1] = "minecraft:stone_pressure_plate"
  154.     right[7][#right[7] + 1] = "minecraft:lever"
  155.     right[7][#right[7] + 1] = "minecraft:sign"
  156.     right[7][#right[7] + 1] = "ironchest:BlockIronChest"
  157.     right[7][#right[7] + 1] = "minecraft:activator_rail"
  158.     right[7][#right[7] + 1] = "minecraft:wooden_button"
  159.     right[7][#right[7] + 1] = "minecraft:comparator"
  160.     right[7][#right[7] + 1] = "ironchest:BlockIronChest"
  161.     right[7][#right[7] + 1] = "minecraft:enchanting_table"
  162.     right[7][#right[7] + 1] = "minecraft:ender_chest"
  163.     right[7][#right[7] + 1] = "minecraft:iron_door"
  164.     right[7][#right[7] + 1] = "minecraft:ladder"
  165.     right[7][#right[7] + 1] = "EnderStorage:enderChest"
  166.     right[8][#right[8] + 1] = "minecraft:stonebrick"
  167.     right[8][#right[8] + 1] = "minecraft:stone_brick_stairs"
  168.     right[9][#right[9] + 1] = "minecraft:planks"
  169.     right[9][#right[9] + 1] = "minecraft:stick"
  170.     right[10][#right[10] + 1] = "minecraft:redstone"
  171.     right[11][#right[11] + 1] = "minecraft:redstone_block"
  172.     right[12][#right[12] + 1] = "minecraft:coal,0"
  173.     right[13][#right[13] + 1] = "minecraft:coal,1"
  174.     right[14][#right[14] + 1] = "minecraft:dye,4"
  175.     return right
  176. end
  177.  
  178. function get_info(chest_item)
  179.     local str
  180.     local name
  181.  
  182.     name, str = chest_item:match("([^,]+),([^,]+)")
  183.     if name then
  184.         return name, tonumber(str)
  185.     else
  186.         return chest_item, nil
  187.     end
  188. end
  189.  
  190. function get_item_list()
  191.     local chest = {}
  192.  
  193.     chest["left"] = get_item_list_left()
  194.     chest["right"] = get_item_list_right()
  195.     return chest
  196. end
  197.  
  198. function get_item_chest_id(side, item, damage)
  199.     local i
  200.     local meta
  201.     local damageRef
  202.     local bestCandidate
  203.     local name
  204.     local chest
  205.  
  206.     i = 1
  207.     meta = 1
  208.     name = true
  209.     chest = get_item_list()
  210.     if (side and side ~= "left" and side ~= "right") or not item or not side then
  211.         error("get_item_chest_id: Invalid side or nil item")
  212.     end
  213.     while chest[side][i] do
  214.         meta = 1
  215.         while chest[side][i][meta] do
  216.             name, damageRef = get_info(chest[side][i][meta])
  217.             if name == item and damageRef and damageRef == damage then
  218.                 return i
  219.             elseif name == item and not damageRef then
  220.                 bestCandidate = i
  221.             end
  222.             meta = meta + 1
  223.         end
  224.         i = i + 1
  225.     end
  226.     return bestCandidate
  227. end
  228.  
  229. function get_max_distance()
  230.     local slot
  231.     local maxDistance
  232.     local data
  233.     local tmpr
  234.     local tmpl
  235.  
  236.     slot = 1
  237.     maxDistance = 1
  238.     while slot <= 16 do
  239.         data = turtle.getItemDetail(slot)
  240.         if data then
  241.             tmpl = get_item_chest_id("left", data.name, data.damage)
  242.             tmpr = get_item_chest_id("right", data.name, data.damage)
  243.             maxDistance = get_max_distance_compare(maxDistance, tmpl)
  244.             maxDistance = get_max_distance_compare(maxDistance, tmpr)
  245.             if not tmpl and not tmpr then
  246.                 maxDistance = LENTH_MAX
  247.             end
  248.         end
  249.         slot = slot + 1
  250.     end
  251.     return maxDistance
  252. end
  253.  
  254. function get_max_distance_compare(val1, val2)
  255.     if not val2 or val2 < val1 then
  256.         return val1
  257.     end
  258.     return val2
  259. end
  260.  
  261. function select_fuel()
  262.     local slot = 1
  263.     local data
  264.  
  265.     while slot <= 16 do
  266.         data = turtle.getItemDetail(slot)
  267.         if data and (data.name == "minecraft:lava_bucket" or data.name == "minecraft:coal" or data.name == "minecraft:planks") then
  268.             turtle.select(slot)
  269.             return true
  270.         end
  271.         slot = slot + 1
  272.     end
  273.     if slot > 16 then
  274.         return false
  275.     end
  276. end
  277.  
  278. --function send_logs(str)
  279. --    rednet.open("right")
  280. --    rednet.send(LOG_REPORT_ID, "Turtle ")
  281. --    rednet.send(LOG_REPORT_ID, os.getComputerLabel())
  282. --    rednet.send(LOG_REPORT_ID, ": ")
  283. --    rednet.send(LOG_REPORT_ID, "newline")
  284. --    rednet.send(LOG_REPORT_ID, str)
  285. --    rednet.send(LOG_REPORT_ID, "newline")
  286. --    rednet.send(LOG_REPORT_ID, "newline")
  287. --    rednet.close("right")
  288. --end
  289.  
  290. function check_fuel()
  291.     if turtle.getFuelLevel() == 0 then
  292.         while not select_fuel() do
  293.             print("No fuel found. Retrying in 10 seconds")
  294. --            send_logs("No fuel found. Retrying in 10 seconds")
  295.             sleep(10)
  296.         end
  297.         turtle.refuel()
  298.         return true
  299.     end
  300.     return false
  301. end
  302.  
  303. function avance(i)
  304.     if not i then
  305.         i = 1
  306.     end
  307.     while i > 0 do
  308.         if not turtle.forward() then
  309.             print("Can't forward. Checking fuel...")
  310.             if check_fuel() then
  311.                 print("Fuel ok !")
  312.             else
  313.                 print("Looks I'm lost, fuel ok but can't forward")
  314.                 sleep(2)
  315.             end
  316.         else
  317.             i = i - 1
  318.         end
  319.     end
  320. end
  321.  
  322. function is_inventory_empty()
  323.     local slot = 1
  324.  
  325.     while slot <= 16 do
  326.         if turtle.getItemCount(slot) > 0 then
  327.             return false
  328.         end
  329.         slot = slot + 1
  330.     end
  331.     return true
  332. end
  333.  
  334. function line(side, maxDistance)
  335.     local i = 1
  336.  
  337.     if side == "right" then
  338.         put_under_chest(side, i)
  339.     else
  340.         put_under_chest(side, maxDistance)
  341.     end
  342.     while i < maxDistance do
  343.         avance()
  344.         i = i + 1
  345.         if side == "right" then
  346.             put_under_chest(side, i)
  347.         else
  348.             put_under_chest(side, maxDistance - (i - 1))
  349.         end
  350.     end
  351. end
  352.  
  353. function put_under_chest(side, chestId)
  354.     local slot = 1
  355.     local data
  356.     local tmpItem
  357.  
  358.     while slot <= 16 do
  359.         data = turtle.getItemDetail(slot)
  360.         if data then
  361.             tmpItem = get_item_chest_id(side, data.name, data.damage)
  362.             if tmpItem and chestId == tmpItem then
  363.                 column(data.name, data.damage)
  364.             elseif not tmpItem and chestId == LENTH_MAX and side == "left" then
  365.                 column(data.name, data.damage)
  366.             end
  367.         end
  368.         slot = slot + 1
  369.     end
  370. end
  371.  
  372. function column(name, damage)
  373.     local slot = 1
  374.     local data
  375.     local offset = 0
  376.     local bool = false
  377.  
  378.     while slot <= 16 do
  379.         data = turtle.getItemDetail(slot)
  380.         if data and data.name == name and damage == data.damage then
  381.             bool = false
  382.             turtle.select(slot)
  383.             while not turtle.dropDown() do
  384.                 turtle.turnRight()
  385.                 avance()
  386.                 turtle.turnLeft()
  387.                 offset = offset + 1
  388.             end
  389.         end
  390.         slot = slot + 1
  391.     end
  392.     while offset > 0 do
  393.         if not bool then
  394.             turtle.turnLeft()
  395.             bool = true
  396.         end
  397.         avance()
  398.         offset = offset - 1
  399.     end
  400.     if bool then
  401.         turtle.turnRight()
  402.     end
  403. end
  404.  
  405. function suck_all()
  406.     local success = false
  407.  
  408.     while turtle.suckUp() do
  409.         -- TODO: Send logs here
  410.         success = true
  411.     end
  412.     return success
  413. end
  414.  
  415. function core()
  416.     suck_all()
  417.     while not is_inventory_empty() do
  418.         local max_distance
  419.  
  420.         update_unknown_list()
  421.         max_distance = get_max_distance()
  422.         turtle.turnRight()
  423.         avance(3)
  424.         turtle.turnLeft()
  425.         line("right", max_distance)
  426.         turtle.turnLeft()
  427.         avance(6)
  428.         turtle.turnLeft()
  429.         line("left", max_distance)
  430.         turtle.turnLeft()
  431.         avance(3)
  432.         turtle.turnLeft()
  433.         suck_all()
  434.     end
  435. end
  436.  
  437. function dump(o)
  438.     if type(o) == 'table' then
  439.         local s = '{ '
  440.         for k,v in pairs(o) do
  441.             if type(k) ~= 'number' then k = '"'..k..'"' end
  442.             s = s .. '['..k..'] = ' .. dump(v) .. '\n'
  443.         end
  444.         return s .. '}\n'
  445.     else
  446.         return tostring(o)
  447.     end
  448. end
  449.  
  450. function is_in_list(elem, list)
  451.     local i
  452.  
  453.     i = 1
  454.     while i <= #list do
  455.         if elem == list[i] then
  456.             return true
  457.         end
  458.         i = i + 1
  459.     end
  460.     return false
  461. end
  462.  
  463. function get_known_and_unknown()
  464.     local maxDistance
  465.     local slot
  466.     local data
  467.     local tmp
  468.     local k = {}
  469.     local u = {}
  470.  
  471.     maxDistance = 1
  472.     slot = 1
  473.     while slot <= 16 do
  474.         data = turtle.getItemDetail(slot)
  475.         if data then
  476.             tmp = tostring(data.name) .. "," .. tostring(data.damage)
  477.             if not get_item_chest_id("left", data.name, data.damage) and
  478.                     not get_item_chest_id("right", data.name, data.damage) then
  479.                 if not is_in_list(tmp, u) then
  480.                     u[#u + 1] = tmp
  481.                 end
  482.                 print("No chest found for ", data.name, " ", data.damage)
  483. --                send_logs("No chest for:" .. data.name .. ", subID:".. data.damage)
  484.             elseif not is_in_list(tmp, k) then
  485.                 k[#k + 1] = tmp
  486.             end
  487.         end
  488.         slot = slot + 1
  489.     end
  490.     return u, k
  491. end
  492.  
  493. function concatenate_arrays(t1, t2)
  494.     local i
  495.  
  496.     i = 1
  497.     while i <= #t2 do
  498.         t1[#t1 + 1] = t2[i]
  499.         i = i + 1
  500.     end
  501.     return t1
  502. end
  503.  
  504. function purge_includes(main, sub)
  505.     local i
  506.     local x
  507.  
  508.     i = 1
  509.     while i <= #main do
  510.         x = 1
  511.         while x <= #sub do
  512.             if main[i] == sub[x] then
  513.                 main[i] = main[#main]
  514.                 main[#main] = nil
  515.             end
  516.             x = x + 1
  517.         end
  518.         i = i + 1
  519.     end
  520.     return main
  521. end
  522.  
  523. function file_exists(file)
  524.     local f
  525.  
  526.     f = io.open(file, "rb")
  527.     if f then
  528.         f:close()
  529.     end
  530.     return f ~= nil
  531. end
  532.  
  533. function check_default_file(filename)
  534.     local f
  535.  
  536.     if not file_exists(filename) then
  537.         f = io.open(filename, "w")
  538.         f:write("minecraft:air\n")
  539.         f:close()
  540.     end
  541. end
  542.  
  543. function get_file_per_lines(filename)
  544.     local lines = {}
  545.  
  546.     for line in io.lines(filename) do
  547.         lines[#lines + 1] = line
  548.     end
  549.     table.sort(lines)
  550.     return lines
  551. end
  552.  
  553. function update_unknown_list()
  554.     local lines
  555.     local u
  556.     local k
  557.  
  558.     check_default_file(UNKNOWN_LIST)
  559.     u, k = get_known_and_unknown()
  560.     io.input(UNKNOWN_LIST)
  561.     lines = get_file_per_lines(UNKNOWN_LIST)
  562.     io.input():close()
  563.     lines = purge_includes(lines, k)
  564.     lines = purge_includes(lines, u)
  565.     lines = concatenate_arrays(lines, u)
  566.     table.sort(lines)
  567.     --  send_logs(dump(lines))
  568.     io.output(UNKNOWN_LIST)
  569.     for i, l in ipairs(lines) do
  570.         io.write(l .. "\n")
  571.     end
  572.     io.output():close()
  573. end
  574.  
  575. if arg[1] == "--update" or arg[1] == "-u" then
  576.     pastebin_download(PASTE_ID, SCRIPT_NAME)
  577.     return true
  578. end
  579. core()
  580. --send_logs("Fuel:" .. turtle.getFuelLevel())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement