mrWhiskasss

обмен слитков руды с пимом

Jul 22nd, 2023
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local ore_dict = {
  2.     {
  3.        
  4.             'Железная руда',
  5.             'Железный слиток',
  6.             2,
  7.             take = {name='minecraft:iron_ore',damage=0},
  8.             give = {name='minecraft:iron_ingot',damage=0}
  9.         },
  10.        
  11.         {
  12.             'Золотая руда',
  13.             'Золотой слиток',
  14.             2,
  15.             take = {name='minecraft:gold_ore',damage=0},
  16.             give = {name='minecraft:gold_ingot',damage=0}
  17.         },
  18.         {
  19.             'Никилевая руда',
  20.             'Никелевый слиток',
  21.             2,
  22.             take = {name='ThermalFoundation:blockOreNickel',damage=0},
  23.             give = {name='ThermalFoundation:material',damage=68}
  24.         },
  25.         {
  26.             'Мифриловая руда',
  27.             'Мифриловый слиток',
  28.             2,
  29.             take = {name='ThermalFoundation:blockOreMithril',damage=0},
  30.             give = {name='ThermalFoundation:material',damage=70}
  31.         },
  32.         {
  33.             'Платиновая руда',
  34.             'Платиновый слиток',
  35.             2,
  36.             take = {'ThermalFoundation:blockOrePlatinum',damage=0},
  37.             give = {name='ThermalFoundation:material',damage=69}
  38.         },
  39.         {
  40.             'Свинцовая руда',
  41.             'Свинцовый слиток',
  42.             2,
  43.             take = {name='IC2:lead_ore',damage=0},
  44.             give = {name='IC2:itemIngot',damage=5}
  45.         },
  46.         {
  47.             'Серебрянная руда',
  48.             'Серебрянный слиток',
  49.             2,
  50.             take = {name='ThermalFoundation:blockOreSilver',damage=0},
  51.             give = {name='IC2:itemIngot',damage=6}
  52.         },
  53.         {
  54.             'Медная руда',
  55.             'Медный слиток',
  56.             2,
  57.             take = {name='IC2:blockOreCopper',damage=0},
  58.             give = {name='IC2:itemIngot',damage=0}
  59.         },
  60.         {
  61.             'Оловянная руда',
  62.             'Оловянный слиток',
  63.             2,
  64.             take = {name='IC2:blockOreTin',damage=0},
  65.             give = {name='IC2:itemIngot',damage=1}
  66.         },
  67.         {
  68.             'Адская Медная Руда',
  69.             'Медный слиток',
  70.             4,
  71.             take = {name='NetherOres:tile.netherores.ore.0',damage=6},
  72.             give = {name='IC2:itemIngot',damage=0}
  73.         },
  74.         {
  75.             'Адская Оловянная руда',
  76.             'Оловянный слиток',
  77.             4,
  78.             take = {name='NetherOres:tile.netherores.ore.0',damage=7},
  79.             give = {name='IC2:itemIngot',damage=1}
  80.         },
  81.         {
  82.             'Кварцевая руда',
  83.             'Кварц',
  84.             3,
  85.             take = {name='minecraft:quartz_ore',damege=0},
  86.             give = {name='minecraft:quartz',damege=0}
  87.         },
  88.         {
  89.             'Серная руда',
  90.             'Сера',
  91.             24,
  92.             take = {name='NetherOres:tile.netherores.1',damage=5},
  93.             give = {name='IC2:itemDust',damage=13}
  94.         },
  95.  
  96.  
  97.  
  98.  }
  99.     local unicode = require('unicode')
  100.     local computer = require("computer")
  101.     local com = require('component')
  102.     local event = require('event')
  103.     local me = com.isAvailable("me_interface") and com.me_interface or error("нет ме интерфейса")
  104.     local db = com.isAvailable("database") and com.database or error("нет базы данных")
  105.     local pim = com.isAvailable("pim") and com.pim or error("нет пима")
  106.    
  107.     local gpu = com.gpu
  108.     local w,h = 48,30
  109.     local defBG, defFG = gpu.getBackground(),gpu.getForeground()
  110.     gpu.setResolution(w,h)
  111.    
  112.    
  113.     local function center(height,text,color)
  114.         gpu.setForeground(color)
  115.         gpu.set(math.floor(w/2-unicode.len(text)/2),height,text)
  116.     end
  117.    
  118.     local function populateQty()
  119.         local totalOre = 0
  120.         for i,ore in pairs(ore_dict) do
  121.             local item = me.getItemsInNetwork(ore.give)
  122.             if item == nil then
  123.                 return 0
  124.             else
  125.                 if item.n then
  126.                     item = item[1]
  127.                     ore.qty = item.size
  128.                     totalOre = totalOre+item.size
  129.                 end
  130.             end
  131.         end
  132.         return totalOre
  133.     end
  134.    
  135.        local function displayOres()
  136.         local line = 10
  137.         for i, ore in pairs(ore_dict) do
  138.             local print_row = line+(i)
  139.    
  140.             gpu.setForeground(0x00ff00)
  141.             gpu.set(1,print_row,ore[1])
  142.             gpu.set(28,print_row,ore[2])
  143.    
  144.             gpu.setForeground(0xFF00FF)
  145.             gpu.set(23,print_row,'1')
  146.             gpu.set(24,print_row,'к'..ore[3])
  147.    
  148.        
  149.         end
  150.     end
  151.    
  152.     local function updateOres()
  153.         local totalOre = populateQty()
  154.         gpu.fill(1,1,w,h-16," ")
  155.         if totalOre==0 then
  156.            
  157.         else
  158.             displayOres()
  159.         end
  160.     end
  161.    
  162.      local function giveIngot(toGive,ore)
  163.    
  164.         db.clear(2)
  165.         me.store(ore.give,db.address,1)
  166.         me.setInterfaceConfiguration(1,db.address,1,64)
  167.         local totalGive = 0
  168.         while totalGive < toGive do
  169.             local gived =  me.pushItem("UP",1,toGive-totalGive)
  170.             totalGive = totalGive+gived
  171.             if gived == 0 then
  172.             end
  173.         end
  174.     end
  175.    
  176.     local function exchangeOre(slot,ore)
  177.         local curSlot = pim.getStackInSlot(slot)
  178.         if curSlot == nil then
  179.             return
  180.         end
  181.         local userOreQty = curSlot.qty
  182.         local giveQty = userOreQty*ore[3]
  183.         if ore.qty<giveQty then
  184.            
  185.         end
  186.         -- Количетсво могло измениться..
  187.         local takedOre = pim.pushItem("DOWN",slot, userOreQty)
  188.        
  189.         if takedOre == nil then
  190.            
  191.         elseif takedOre == 0 then
  192.            
  193.         else
  194.             local giveQty = takedOre*ore[3]
  195.            
  196.             giveIngot(giveQty,ore)
  197.         end
  198.     end
  199.    
  200.     local function checkInventory()
  201.         local size = pim.getInventorySize()
  202.         local data = pim.getAllStacks(0)
  203.    
  204.         for slot =1, size do
  205.             if data[slot] then
  206.                 for i,ore in pairs(ore_dict) do
  207.                     if data[slot].id==ore.take.name and data[slot].dmg == ore.take.damage then
  208.                         exchangeOre(slot, ore)
  209.                     end
  210.                 end
  211.             end
  212.         end
  213.        
  214.         updateOres()
  215.        
  216.         if pim.getInventoryName() ~= 'pim' then
  217.            
  218.              
  219.             checkInventory()
  220.         else
  221.             -- Может быть, мы не поймали событие когда юзер ушел
  222.             event.push('player_off')
  223.         end
  224.      
  225.     end
  226.    
  227.    
  228.     function handleEvent(eventID,...)
  229.         local args = {...}
  230.         if eventID == 'interrupted' then
  231.             gpu.setBackground(defBG)
  232.             gpu.setForeground(defFG)
  233.             gpu.fill(1,1,w,h," ")
  234.             os.exit()
  235.             return true
  236.         elseif eventID =='player_on' then
  237.            
  238.             updateOres()
  239.             checkInventory()
  240.         elseif eventID =='player_off' then
  241.             checkInventory()
  242.             updateOres()
  243.         end
  244.    
  245.     end
  246.    
  247.    
  248.     function drawStat()
  249.         gpu.setForeground(0x444444)
  250.         gpu.set(2, h-1, string.format("RAM: %.1fkB / %.1fkB", (computer.totalMemory() - computer.freeMemory()) / 1024, computer.totalMemory() / 1024))
  251.     end
  252.    
  253.     local oreTimeFn = function()
  254.         updateOres()
  255.         -- Каджый 10 такт обновляем экран
  256.         for i=1,1 do
  257.             coroutine.yield()
  258.         end
  259.     end
  260.     local oreTimer = coroutine.create(oreTimeFn)
  261.    
  262.    
  263.    
  264.    
  265.     while true do
  266.         drawStat()
  267.    
  268.         handleEvent(event.pull(1))
  269.    
  270.         if coroutine.status(oreTimer) == 'dead' then
  271.             oreTimer = coroutine.create(oreTimeFn)
  272.         else
  273.             coroutine.resume(oreTimer)
  274.         end
  275.     end
Add Comment
Please, Sign In to add comment