Advertisement
Rijen

new_exchange

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