Advertisement
maz1lovo

Pupupupu

Jun 8th, 2025 (edited)
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.95 KB | None | 0 0
  1. --[[
  2. The specified software is distributed under the CC0 license
  3. No Rights Reserved
  4. ]] --
  5.  
  6. local ore_dict = {
  7.     {
  8.         "Железная руда",
  9.         "Железный слиток",
  10.         take = {name = "minecraft:iron_ore", damage = 0},
  11.         give = {name = "minecraft:iron_ingot", damage = 0},
  12.         rate = {take = 4, give = 9}
  13.     },
  14.     {
  15.         "Золотая руда",
  16.         "Золотой слиток",
  17.         take = {name = "minecraft:gold_ore", damage = 0},
  18.         give = {name = "minecraft:gold_ingot", damage = 0},
  19.         rate = {take = 4, give = 9}
  20.     },
  21.     {
  22.         "Медная руда",
  23.         "Медный слиток",
  24.         take = {name = "IC2:blockOreCopper", damage = 0},
  25.         give = {name = "IC2:itemIngot", damage = 0},
  26.         rate = {take = 4, give = 9}
  27.     },
  28.     {
  29.         "Медная руда",
  30.         "Медный слиток",
  31.         take = {name = "ProjRed:Exploration:projectred.exploration.ore", damage = 3},
  32.         give = {name = "IC2:itemIngot", damage = 0},
  33.         rate = {take = 4, give = 9}
  34.     },
  35.     {
  36.         "Медная руда",
  37.         "Медный слиток",
  38.         take = {name = "Forestry:resources", damage = 1},
  39.         give = {name = "IC2:itemIngot", damage = 0},
  40.         rate = {take = 4, give = 9}
  41.     },
  42.     {
  43.         "Оловянная руда",
  44.         "Оловянный слиток",
  45.         take = {name = "IC2:blockOreTin", damage = 0},
  46.         give = {name = "IC2:itemIngot", damage = 1},
  47.         rate = {take = 4, give = 9}
  48.     },
  49.     {
  50.         "Оловянная руда",
  51.         "Оловянный слиток",
  52.         take = {name = "Forestry:resources", damage = 2},
  53.         give = {name = "IC2:itemIngot", damage = 1},
  54.         rate = {take = 4, give = 9}
  55.     },
  56.     {
  57.         "Оловянная руда",
  58.         "Оловянный слиток",
  59.         take = {name = "ProjRed:Exploration:projectred.exploration.ore", damage = 4},
  60.         give = {name = "IC2:itemIngot", damage = 1},
  61.         rate = {take = 4, give = 9}
  62.     },
  63.     {
  64.         "Свинцовая руда",
  65.         "Свинцовый слиток",
  66.         take = {name = "IC2:blockOreLead", damage = 0},
  67.         give = {name = "IC2:itemIngot", damage = 5},
  68.         rate = {take = 4, give = 9}
  69.     },
  70.     {
  71.         "Серебряная руда",
  72.         "Серебрянный слиток",
  73.         take = {name = "ProjRed:Exploration:projectred.exploration.ore", damage = 5},
  74.         give = {name = "IC2:itemIngot", damage = 6},
  75.         rate = {take = 4, give = 9}
  76.     },
  77.     {
  78.         "Серебряная руда",
  79.         "Серебрянный слиток",
  80.         take = {name = "ThermalFoundation:Ore", damage = 2},
  81.         give = {name = "IC2:itemIngot", damage = 6},
  82.         rate = {take = 4, give = 9}
  83.     },
  84.     {
  85.         "Серебрянная руда",
  86.         "Серебрянный слиток",
  87.         take = {name = "ThermalFoundation:Ore", damage = 4},
  88.         give = {name = "ThermalFoundation:material", damage = 36},
  89.         rate = {take = 1, give = 2}
  90.     }
  91. }
  92.  
  93. local unicode = require("unicode")
  94. local computer = require("computer")
  95. local com = require("component")
  96. local event = require("event")
  97. local me = com.isAvailable("me_interface") and com.me_interface or error("нет ме интерфейса")
  98. local pim = com.isAvailable("pim") and com.pim or error("нет пима")
  99.  
  100. local gpu = com.gpu
  101. local w, h = 80, 50
  102. local defBG, defFG = gpu.getBackground(), gpu.getForeground()
  103. gpu.setResolution(w, h)
  104.  
  105. local function center(height, text, color)
  106.     gpu.setForeground(color)
  107.     gpu.set(math.floor(w / 2 - unicode.len(text) / 2), height, text)
  108. end
  109.  
  110. local function populateQty()
  111.     local totalOre = 0
  112.     for i, ore in pairs(ore_dict) do
  113.         local fingerprint = {id = ore.give.name, damage = ore.give.damage}
  114.         local item = me.getItemDetail(fingerprint).basic()
  115.         if item == nil then
  116.             return 0
  117.         else
  118.             ore.sizeOfStack = item.max_size
  119.             ore.qty = item.qty
  120.             totalOre = totalOre + item.qty
  121.         end
  122.     end
  123.     return totalOre
  124. end
  125.  
  126. local function displayOres()
  127.     local line = 2
  128.     for i, ore in pairs(ore_dict) do
  129.         local print_row = line + (i)
  130.  
  131.         gpu.setForeground(0x00ff00)
  132.         gpu.set(5, print_row, ore[1])
  133.         gpu.set(42, print_row, ore[2])
  134.  
  135.         gpu.setForeground(0xFF00FF)
  136.         gpu.set(28, print_row, tostring(ore.rate.take))
  137.         gpu.set(34, print_row, tostring(ore.rate.give))
  138.         gpu.set(73, print_row, tostring(ore.qty))
  139.  
  140.         gpu.setForeground(0xFFFF00)
  141.         gpu.set(30, print_row, "-->")
  142.         gpu.set(63, print_row, "Доступно:")
  143.         gpu.setForeground(0x202020)
  144.         gpu.set(2, print_row + 1, string.rep("═", w - 2))
  145.         line = line + 1
  146.     end
  147. end
  148.  
  149. local function updateOres()
  150.     local totalOre = populateQty()
  151.     gpu.fill(1, 1, w, h - 16, " ")
  152.     if totalOre == 0 then
  153.         gpu.fill(1, 1, w, h - 15, " ")
  154.         center(h - 15, "Нет соединения с МЭ", 0xff0000)
  155.     else
  156.         displayOres()
  157.     end
  158. end
  159.  
  160. local function giveItemFromMe(item, dmg, amount)
  161.     local fingerprint = {id = item, dmg = dmg}
  162.     local err, res = pcall(me.exportItem, fingerprint, "UP", amount)
  163.     if err == true then
  164.         return res.size
  165.     end
  166. end
  167.  
  168. local function giveIngot(toGive, ore)
  169.     local totalGive = 0
  170.     local givePreIteration = 0
  171.     while totalGive < toGive do
  172.         if toGive - totalGive > 64 then
  173.             givePreIteration = ore.sizeOfStack
  174.         else
  175.             givePreIteration = toGive - totalGive
  176.         end
  177.  
  178.         local gived = giveItemFromMe(ore.give.name, ore.give.damage, givePreIteration)
  179.  
  180.         totalGive = totalGive + gived
  181.         if gived == 0 then
  182.             gpu.fill(1, h - 15, w, 2, " ")
  183.             center(h - 15, "Oсвободите место в инвентаре", 0xff0000)
  184.             center(h - 14, "Ожидаю выдать " .. tostring(toGive - totalGive) .. " " .. ore[2], 0xFFFFFF)
  185.             os.sleep(1)
  186.         end
  187.         gpu.fill(1, h - 15, 2, h, " ")
  188.     end
  189. end
  190.  
  191. local function exchangeOre(slot, ore)
  192.     local curSlot = pim.getStackInSlot(slot)
  193.     if curSlot == nil then
  194.         center(h - 14, "Вы сошли с PIM, обмен прерван. (Не удалось прочесть слот)", 0xff0000)
  195.         os.sleep(0.5)
  196.         return
  197.     end
  198.     local userOreQty = curSlot.qty
  199.     local takeQty = userOreQty - (userOreQty % ore.rate.take)
  200.     local giveQty = userOreQty * ore.rate.give
  201.     if ore.qty < giveQty then
  202.         gpu.fill(1, h - 14, w, 1, " ")
  203.         center(h - 14, "Недостаточно слитков для обмена <" .. ore[1] .. ">", 0xff0000)
  204.         os.sleep(0.5)
  205.         return
  206.     end
  207.     -- Количетсво могло измениться..
  208.     local takedOre = pim.pushItem("DOWN", slot, takeQty)
  209.  
  210.     if takedOre == nil then
  211.         gpu.fill(1, h - 14, w, 1, " ")
  212.         center(h - 14, "Вы сошли с PIM, обмен прерван. (Не удалось извлечь руду)", 0xff0000)
  213.         os.sleep(0.5)
  214.     elseif takedOre == 0 then
  215.         gpu.fill(1, h - 14, w, 1, " ")
  216.         center(h - 14, "В выбраном слоте руды нет.. А была.. Хмм...", 0x505050)
  217.     else
  218.         local giveQty = (takedOre // ore.rate.take) * ore.rate.give
  219.         gpu.fill(1, h - 15, w, 2, " ")
  220.         center(h - 14, "Меняю  " .. takedOre .. "  " .. ore[1] .. "  на  " .. giveQty .. "  " .. ore[2], 0xffffff)
  221.         giveIngot(giveQty, ore)
  222.     end
  223. end
  224.  
  225. local function checkInventory()
  226.     local size = pim.getInventorySize()
  227.     local data = pim.getAllStacks(0)
  228.  
  229.     for slot = 1, size do
  230.         if data[slot] then
  231.             for i, ore in pairs(ore_dict) do
  232.                 if data[slot].id == ore.take.name and data[slot].dmg == ore.take.damage then
  233.                     exchangeOre(slot, ore)
  234.                 end
  235.             end
  236.         end
  237.     end
  238.  
  239.     updateOres()
  240.  
  241.     if pim.getInventoryName() ~= "pim" then
  242.         gpu.fill(1, h - 15, w, 2, " ")
  243.         center(h - 15, "Обмен окончен! Приходите ещё!", 0xffffff)
  244.  
  245.         checkInventory()
  246.     else
  247.         -- Может быть, мы не поймали событие когда юзер ушел
  248.         event.push("player_off")
  249.     end
  250. end
  251.  
  252. function handleEvent(eventID, ...)
  253.     local args = {...}
  254.     if eventID == "interrupted" then
  255.         gpu.setBackground(defBG)
  256.         gpu.setForeground(defFG)
  257.         gpu.fill(1, 1, w, h, " ")
  258.         os.exit()
  259.         return true
  260.     elseif eventID == "player_on" then
  261.         gpu.fill(1, h - 15, w, 1, " ")
  262.         center(h - 15, "Приветствую, " .. args[1] .. "! Начинаю обмен", 0xffffff)
  263.         updateOres()
  264.         checkInventory()
  265.     elseif eventID == "player_off" then
  266.         gpu.fill(1, h - 15, w, 1, " ")
  267.         center(h - 15, "Для обмена встаньте на PIM и не сходите до окончания обмена", 0xffffff)
  268.         updateOres()
  269.     end
  270. end
  271.  
  272. function drawStat()
  273.     gpu.setForeground(0x444444)
  274.     gpu.set(
  275.         2,
  276.         h - 1,
  277.         string.format(
  278.             "RAM: %.1fkB / %.1fkB",
  279.             (computer.totalMemory() - computer.freeMemory()) / 1024,
  280.             computer.totalMemory() / 1024
  281.         )
  282.     )
  283. end
  284.  
  285. local oreTimeFn = function()
  286.     updateOres()
  287.     -- Каджый 10 такт обновляем экран
  288.     for i = 1, 10 do
  289.         coroutine.yield()
  290.     end
  291. end
  292. local oreTimer = coroutine.create(oreTimeFn)
  293.  
  294. function draw_img(img, x, y, fore, back)
  295.     fore = fore or 0xffffff
  296.     back = back or 0x0
  297.     oldFG = gpu.getForeground()
  298.     gpu.setForeground(fore)
  299.     local i = 0
  300.     for line in img:gmatch("([^\n]*)\n?") do
  301.         gpu.set(x, y + i, line)
  302.         i = i + 1
  303.     end
  304.     gpu.setForeground(oldFG)
  305. end
  306. local img =
  307.     [[
  308.     ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
  309.     ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡐⢆⠆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
  310.     ⠀⠀⠀⠀⠀⠀⠀⠀⠀⡀⠀⠀⠀⠀⠀⠀⠀⢀⠀⠠⡐⣄⢢⡐⢄⢢⡐⣄⠂⡄⠀⠀⢄⢢⢰⣀⢦⡐⣄⢢⡐⠄⣂⠆⠀⠀⢀⡐⢦⠹⠌⠀⢀⡰⢠⠀⠐⣠⠀⠀⠀⠀⠀⠀⠀⠀⠄⡐⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
  311.     ⠀⠀⠀⠀⠀⠀⠀⠀⠀⢡⠂⡄⠀⠀⠀⠀⠌⡤⠃⠀⢳⡌⠃⠘⠈⠂⠑⠈⠑⠀⠀⡜⡢⠍⠂⠑⠈⠑⠈⠃⠀⠐⣌⠎⢀⡐⠦⣜⠂⠁⠀⠀⠀⡜⡡⠂⠘⠤⡁⠀⠀⠀⠀⠀⠀⠀⢂⠱⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
  312.     ⠀⠀⠀⠀⠀⠀⠀⠀⠀⢢⠑⡌⢢⠀⠤⣉⡜⣰⠁⠀⡣⢝⠀⠀⠀⠀⠀⠀⠀⠀⢀⠳⣹⢌⡱⢢⡱⢌⢲⢰⡀⠀⢎⠺⣤⣙⠳⣀⠀⠀⠀⠀⠠⣙⠦⠁⢨⣑⠂⠀⠀⠀⠀⠀⠀⠀⢌⠳⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
  313.     ⠀⠀⠀⠀⠀⠀⠀⠀⠀⢢⡑⠈⠒⡭⠒⠌⠸⣡⠃⠀⣙⠮⡄⠀⠀⠀⠀⠀⠀⠀⠀⠁⠀⠈⠁⠁⠉⣈⠞⡢⠄⠈⢎⡳⠂⠉⠳⢆⡆⣀⠀⠀⠐⡌⠦⠁⠠⣃⠎⠀⠀⠀⠀⠀⠀⠀⠌⢒⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
  314.     ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠒⡄⠁⠀⠀⠁⠀⠰⣡⠂⠀⠈⠸⣑⢋⢆⢳⡘⣔⠣⢆⢠⠱⣊⡕⣎⢖⡱⢎⠞⠁⠀⠈⣎⠱⠀⠀⠀⠈⠲⣡⠢⠄⠀⡜⡡⠂⠐⡥⢊⡴⢡⠎⡤⢁⠆⠀⠘⡀⠎⡐⢂⠰⠀⠆⡀⠀⠀⠀⠀⠀
  315.     ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠹⢌⡁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
  316.     ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠐⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
  317.     ]]
  318.  
  319. function copyleft()
  320.     local text = {
  321.         {"Авторы: ", 0x444444, w - 35},
  322.         {"Rijen, Furryonelove", 0x64fff2, w - 28}
  323.     }
  324.     local startPos = w - 32
  325.     for i, str in pairs(text) do
  326.         gpu.setForeground(str[2])
  327.         gpu.set(str[3], h - 1, str[1])
  328.     end
  329.  
  330.     gpu.setBackground(defBG)
  331.     gpu.setForeground(defFG)
  332. end
  333.  
  334. gpu.fill(1, 1, w, h, " ")
  335. center(h - 15, "Для обмена встаньте на PIM и не сходите до окончания обмена", 0xffffff)
  336. draw_img(img, 1, h - 13, 0x00a400)
  337. copyleft()
  338.  
  339. while true do
  340.     drawStat()
  341.  
  342.     handleEvent(event.pull(1))
  343.  
  344.     if coroutine.status(oreTimer) == "dead" then
  345.         oreTimer = coroutine.create(oreTimeFn)
  346.     else
  347.         coroutine.resume(oreTimer)
  348.     end
  349. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement