Stawlie0

Untitled

Oct 12th, 2023
785
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.74 KB | None | 0 0
  1. local Casino = {}
  2.  
  3. local component = require("component")
  4. local chatbox = component.chat_box
  5.  
  6. chatbox.say("Casino: загруженно")
  7.  
  8. function Casino:new()
  9.     local newObj = {
  10.         items = {},       -- список предметов, их шансы и дополнительные свойства
  11.         totalChance = 0,   -- общий шанс (сумма всех шансов)
  12.         addressMoneyChest = nil, -- Адрес сундука с деньгами
  13.         goodWin = 20, -- Последний хороший выигрыш
  14.     }
  15.     self.__index = self
  16.     return setmetatable(newObj, self)
  17. end
  18.  
  19. function Casino:addItem(name, title, count, chance)
  20.     table.insert(self.items, {
  21.         name = name,
  22.         title = title,
  23.         count = count,
  24.         chance = chance,
  25.         address = nil,
  26.         slot = nil,
  27.     })
  28.  
  29.     self.totalChance = self.totalChance + chance
  30. end
  31. math.randomseed(os.time())
  32.  
  33. function Casino:roll(chance)
  34.     local randomValue = math.random() * self.totalChance
  35.     local accumulatedChance = 0
  36.  
  37.     if self.goodWin <= 0 then
  38.         self.goodWin = self.goodWin - 1
  39.         self.totalChance = self.totalChance - chance
  40.     end
  41.  
  42.     for _, item in ipairs(self.items) do
  43.         accumulatedChance = accumulatedChance + item.chance
  44.         if randomValue <= accumulatedChance then
  45.             if item.chance == 0.001 then
  46.                 self.goodWin = 100
  47.             end
  48.             return item
  49.         end
  50.     end
  51. end
  52.  
  53. function Casino:SearchMoney()
  54.     if not self.addressMoneyChest then
  55.         chatbox.say("Address is nil.")
  56.         os.sleep(0.5)
  57.         return nil
  58.     end
  59.     local trans = component.proxy(self.addressMoneyChest)
  60.     if not trans or not trans.getStackInSlot then     -- Проверка успешности получения прокси
  61.         chatbox.say("Не удалось получить прокси компонента для адреса.: " .. tostring(address))
  62.         os.sleep(0.5)
  63.         return nil
  64.     end
  65.     local stack = trans.getStackInSlot(0, 1)
  66.  
  67.     if stack and stack.name == "contenttweaker:money" and stack.size then     -- Проверка наличия стека денег и обновление общего баланса
  68.         return stack.size
  69.     end
  70.     return nil
  71. end
  72.  
  73. function Casino:displayItems()
  74.     for _, item in ipairs(self.items or {}) do
  75.         chatbox.say("Name: " .. item.name)
  76.         chatbox.say("Title: " .. item.title)
  77.         chatbox.say("Count: " .. item.count)
  78.         chatbox.say("Chance: " .. item.chance)
  79.         if item.address then
  80.             chatbox.say("Address: " .. item.address)
  81.             chatbox.say("Slot: " .. item.slot)
  82.         end
  83.         chatbox.say("-----")
  84.     end
  85. end
  86.  
  87.  
  88. function Casino:search_transposers()
  89.     -- Предварительное создание маппинга элементов для быстрого поиска
  90.     local itemMap = {}
  91.  
  92.     for _, item in ipairs(self.items or {}) do
  93.         itemMap[item.name] = item
  94.         if item.title then  -- предположим, что title хранит ваш label
  95.             itemMap[item.title] = item
  96.         end
  97.     end
  98.    
  99.     for address in component.list("transposer") do
  100.         local trans = component.proxy(address)
  101.         if not trans then return end
  102.  
  103.         local invName = trans.getInventoryName(0)
  104.         if invName == "appliedenergistics2:interface" then
  105.             local size = trans.getInventorySize(0) or 0
  106.             for i = 1, size do
  107.                 local stack = trans.getStackInSlot(0, i)
  108.                 if stack then
  109.                     local item = itemMap[stack.name] or itemMap[stack.label]
  110.                     if item then
  111.                         item.address = address
  112.                         item.slot = i
  113.                     end
  114.                 end
  115.             end
  116.         else -- Интерфейс хранения валюты
  117.             self.addressMoneyChest = address
  118.             if chatbox then chatbox.say("§fСохранил адрес сундука с деньгами: " .. address) end
  119.         end
  120.     end
  121. end
  122.  
  123. function Casino:get_list()
  124.     local result = {}
  125.     table.insert(result, "\n")
  126.     for _, item in ipairs(self.items or {}) do
  127.         local displayName = string.gsub(item.name, "&", "§", 1)
  128.         if item.count == 1 then
  129.             table.insert(result, "§l" .. displayName .. " Шанс: "  .. item.chance * 100 * 10 .. "%")
  130.         else
  131.             table.insert(result, "§l" .. displayName .. " x" .. item.count.. " Шанс: "  .. item.chance * 100 * 10 .. "%")
  132.         end
  133.     end
  134.     local resultString = table.concat(result, "\n")
  135. end
  136.  
  137. function Casino:sendMoney(count)
  138.     component.proxy(self.addressMoneyChest).transferItem(0, 1, count, 1, 1)
  139. end
  140.  
  141. function Casino:sendMoney(count)
  142.     component.proxy(self.addressMoneyChest).transferItem(0, 1, count, 1, 1)
  143. end
  144.  
  145. function Casino:generateWeightedArray()
  146.     local weightedArray = {}
  147.     for i, item in ipairs(self.items) do
  148.         local repetitions = math.floor(item.chance * 100)         -- Умножаем вероятность на 100, чтобы получить целое число для повторений
  149.         for j = 1, repetitions do
  150.             print(self.items.title .. " " .. tonumber(repetitions))
  151.             table.insert(weightedArray, i)
  152.         end
  153.     end
  154.     return weightedArray
  155. end
  156.  
  157. function Casino:weightedRoll()
  158.     local weightedArray = self:generateWeightedArray()
  159.     local randomIndex = math.random(1, #weightedArray)
  160.     print("Число индекса: " .. tonumber(randomIndex))
  161.     local itemIndex = weightedArray[randomIndex]
  162.     print(self.items[itemIndex])
  163.     return self.items[itemIndex]
  164. end
  165.  
  166. return Casino
  167.  
  168.  
Advertisement
Add Comment
Please, Sign In to add comment