Advertisement
Doob

[OpenComputers] market

Dec 11th, 2015
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.14 KB | None | 0 0
  1. local component = require("component")
  2. local deb = component.debug
  3. local pim = component.pim
  4. local item
  5. local tWList = {
  6.   ['Obsidian'] = "minecraft:obsidian:0",
  7.   ['Block of Redstone'] = "minecraft:redstone_block:0",
  8.   ['Block of Coal'] = "minecraft:coal_block:0",
  9.   ['Lapis Lazuli Block'] = "minecraft:lapis_block:0",
  10.   ['Block of Iron'] = "minecraft:iron_block:0",
  11.   ['Block of Gold'] = "minecraft:gold_block:0",
  12.   ['Block of Diamond'] = "minecraft:diamond_block:0",
  13.   ['Block of Emerald'] = "emerald:block:0",
  14.  
  15.   ['Steak'] = "minecraft:cooked_beef:0",
  16.   ['Cooked Chiken'] = "minecraft:cooked_chiken:0",
  17.   ['Cooked Porkchop'] = "minecraft:cooked_porkchop:0",
  18.   ['Baked Potato'] = "minecraft:baked_potato:0",
  19.   ['Bread'] = "minecraft:bread:0",
  20.   ['Melon'] = "minecraft:melon:0",
  21.   ['Apple'] = "minecraft:apple:0",
  22.  
  23.   ['Redstone'] = "minecraft:redstone:0",
  24.   ['Coal'] = "minecraft:coal:0",
  25.   ['Lapis Lazuli'] = "minecraft:dye:4",
  26.   ['Iron Ingot'] = "minecraft:iron_ingot:0",
  27.   ['Gold Ingot'] = "minecraft:gold_ingot:0",
  28.   ['Diamond'] = "minecraft:diamond:0",
  29.   ['Emerald'] = "minecraft:emerald:0",
  30. }
  31.  
  32. local tMarket = {} -- список товаров на рынке
  33. local tGoods = {}  -- список товаров игрока
  34. --[[
  35. local tGoods = {
  36.   [лейбл итема] = {
  37.     [0] = общее количество
  38.     [слот] = количество
  39.   }
  40. }
  41. ]]
  42.  
  43. local function market(i_tem, amount, tpl) -- количество под индексом 0, для совместимости с tGoods
  44.   if tpl == 1 then
  45.     if tMarket[i_tem] = nil then
  46.       tMarket[i_tem] = {[0] = amount}
  47.     else
  48.       tMarket[i_tem][0] = tMarket[i_tem][0] + amount
  49.     end
  50.   elseif tpl == 0
  51.     if tMarket[i_tem][0] < amount then
  52.       tMarket[i_tem][0] = tMarket[i_tem][0] - tMarket[i_tem][0]
  53.     else
  54.       tMarket[i_tem][0] = tMarket[i_tem][0] - amount
  55.     end
  56.   end
  57. end
  58.  
  59. local function scanner()  -- сканирует инвентарь
  60.   for slot = 1, 36 do
  61.     item = pim.getStackInSlot(slot)
  62.     if item ~= nil then
  63.       if tWList[item.display_name] ~= nil then  -- проверка, есть ли он в вайтлисте
  64.         if tWList[item.display_name] == item.id .. ':' .. item.dmg then  -- проверка ID и дамага
  65.           if tGoods[item.display_name] == nil then
  66.             tGoods[item.display_name] = {[0] = item.qty}
  67.             tGoods[item.display_name][slot] = item.qty
  68.           end
  69.           tGoods[item.display_name][slot] = item.qty
  70.           tGoods[item.display_name][0] = tGoods[item.display_name][0] + item.qty
  71.         end
  72.       else
  73.         if tGoods[item.display_name] == nil then
  74.           tGoods[item.display_name] = {[0] = item.qty}
  75.           tGoods[item.display_name][slot] = item.qty
  76.         end
  77.         tGoods[item.display_name][slot] = item.qty
  78.         tGoods[item.display_name][0] = tGoods[item.display_name][0] + item.qty
  79.       end
  80.     end
  81.   end
  82. end
  83.  
  84. local function disintegrator(i_tem, amount)  -- перемещает указанные предметы из инвентаря в утилизатор
  85.   if tGoods[i_tem][0] >= amount
  86.     for slot = 1, 36 do
  87.       if tGoods[i_tem][slot] >= amount then
  88.         pim.pushItem('DOWN', slot, amount)
  89.         tGoods[i_tem][slot] = tGoods[i_tem][slot] - amount
  90.         tGoods[i_tem][0] = tGoods[i_tem][0] - amount
  91.         amount = amount - amount
  92.         market(i_tem, amount, 1)
  93.       elseif tGoods[i_tem][slot] < amount then
  94.         pim.pushItem('DOWN', slot, tGoods[i_tem][slot])
  95.         tGoods[i_tem][slot] = tGoods[i_tem][slot] - tGoods[i_tem][slot]
  96.         tGoods[i_tem][0] = tGoods[i_tem][0] - tGoods[i_tem][slot]
  97.         amount = amount - tGoods[i_tem][slot]
  98.         market(i_tem, amount, 1)
  99.       end
  100.       if amount == 0 then
  101.         break
  102.       end
  103.     end
  104.   end
  105. end
  106.  
  107. local function giver(i_tem, amount, dmg)  -- выдает указанные предметы
  108.   if dmg == nil then
  109.     dmg = 0
  110.   end
  111.   deb.runCommand('give ' .. player .. ' ' .. i_tem .. ' ' .. amount .. ' ' .. dmg)
  112.   market(i_tem, amount, 0)
  113. end
  114.  
  115. --------------------------------------------
  116. local component =require('component')
  117. local gpu = component.gpu
  118. local tListA, tListP = {}, {} -- список всех товаров под цифровыми индексами, список товаров на текущей странице
  119. local page = 0
  120. --gpu.setResolution(80, 25)
  121. local function center(text, line)
  122.   gpu.set(20-(#text/2), line, text)
  123. end
  124.  
  125. local function clear()
  126.   gpu.fill(1, 1, 80, 25, ' ')
  127. end
  128.  
  129. local tButtons = { -- x1, x2, y1
  130.   {2, 39, 4}, {42, 79, 4},
  131.   {2, 39, 6}, {42, 79, 6},
  132.   {2, 39, 8}, {42, 79, 8},
  133.   {2, 39, 10}, {42, 79, 10},
  134.   {2, 39, 12}, {42, 79, 12},
  135.   {2, 39, 14}, {42, 79, 14},
  136.   {2, 39, 16}, {42, 79, 16},
  137.   {2, 39, 18}, {42, 79, 18},
  138.   {2, 39, 20}, {42, 79, 20},
  139.   {2, 39, 22}, {42, 79, 22},
  140.   {2, 8, 24}, {73, 79, 24}
  141. }
  142.  
  143. local tGoodsList = {
  144.   '╔══════════════════════╦═══════╦═══════╦╦══════════════════════╦═══════╦═══════╗',
  145.   '║   название товара    ║ кол-во║  цена ║║   название товара    ║ кол-во║  цена ║',
  146.   '╠══════════════════════╬═══════╬═══════╬╬══════════════════════╬═══════╬═══════╣',
  147.   '║1                     ║       ║       ║║2                     ║       ║       ║',
  148.   '╠══════════════════════╬═══════╬═══════╬╬══════════════════════╬═══════╬═══════╣',
  149.   '║3                     ║       ║       ║║4                     ║       ║       ║',
  150.   '╠══════════════════════╬═══════╬═══════╬╬══════════════════════╬═══════╬═══════╣',
  151.   '║5                     ║       ║       ║║6                     ║       ║       ║',
  152.   '╠══════════════════════╬═══════╬═══════╬╬══════════════════════╬═══════╬═══════╣',
  153.   '║7                     ║       ║       ║║8                     ║       ║       ║',
  154.   '╠══════════════════════╬═══════╬═══════╬╬══════════════════════╬═══════╬═══════╣',
  155.   '║9                     ║       ║       ║║10                    ║       ║       ║',
  156.   '╠══════════════════════╬═══════╬═══════╬╬══════════════════════╬═══════╬═══════╣',
  157.   '║11                    ║       ║       ║║12                    ║       ║       ║',
  158.   '╠══════════════════════╬═══════╬═══════╬╬══════════════════════╬═══════╬═══════╣',
  159.   '║13                    ║       ║       ║║14                    ║       ║       ║',
  160.   '╠══════════════════════╬═══════╬═══════╬╬══════════════════════╬═══════╬═══════╣',
  161.   '║15                    ║       ║       ║║16                    ║       ║       ║',
  162.   '╠══════════════════════╬═══════╬═══════╬╬══════════════════════╬═══════╬═══════╣',
  163.   '║17                    ║       ║       ║║18                    ║       ║       ║',
  164.   '╠══════════════════════╬═══════╬═══════╬╬══════════════════════╬═══════╬═══════╣',
  165.   '║19                    ║       ║       ║║20                    ║       ║       ║',
  166.   '╠═══════╦══════════════╩═══════╩═════╦═╩╩═╦════════════════════╩═══════╬═══════╣',
  167.   '║  <<<  ║                            ║0001║                            ║  >>>  ║',
  168.   '╚═══════╩════════════════════════════╩════╩════════════════════════════╩═══════╝'
  169. }
  170.  
  171. local function goodsList(tbl) -- tbl = {[1] = {'название товара', 'количество', 'стоимость'}}
  172.   for d = 1, #tTradeScreen do
  173.     gpu.set(1, d, tTradeScreen[d])
  174.   end
  175.   for f = 1, #tbl do
  176.     gpu.set(tButtons[f][1], tButtons[f][3], string.sub(tbl[f][1], 1, 22))
  177.     gpu.set(tButtons[f][1]+23, tButtons[f][3], tostring(tbl[f][2]))
  178.     gpu.set(tButtons[f][1]+31, tButtons[f][3], tostring(tbl[f][3]))
  179.   end
  180. end
  181.  
  182. local function refresh(tbl) -- принимает tGoods и tMarket, {['лейбл'] = {[0] = количество}}
  183.   for label, amount in pairs(tbl) do
  184.     table.insert(tListA, {label, amount[0]})
  185.   end
  186. end
  187.  
  188. local function filler() -- заполняет текущую страницу из общего списка предметов
  189.   tListP = {}
  190.   for list = 1, 20 do
  191.     tListP[list] = tListA[list+page]
  192.   end
  193. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement