Advertisement
Stawlie0

Untitled

Oct 12th, 2023
809
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.59 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.     end
  95.  
  96.     for address in component.list("transposer") do
  97.         local trans = component.proxy(address)
  98.         if not trans then return end
  99.  
  100.         local invName = trans.getInventoryName(0)
  101.         if invName == "appliedenergistics2:interface" then
  102.             local size = trans.getInventorySize(0) or 0
  103.             for i = 1, size do
  104.                 local stack = trans.getStackInSlot(0, i)
  105.                 if stack then
  106.                     local item = itemMap[stack.name] or itemMap[stack.label]
  107.                     if item then
  108.                         item.address = address
  109.                         item.slot = i
  110.                     end
  111.                 end
  112.             end
  113.         else -- Интерфейс хранения валюты
  114.             self.addressMoneyChest = address
  115.             if chatbox then chatbox.say("§fСохранил адрес сундука с деньгами: " .. address) end
  116.         end
  117.     end
  118. end
  119.  
  120. function Casino:get_list()
  121.     local result = {}
  122.     table.insert(result, "\n")
  123.     for _, item in ipairs(self.items or {}) do
  124.         local displayName = string.gsub(item.name, "&", "§", 1)
  125.         if item.count == 1 then
  126.             table.insert(result, "§l" .. displayName .. " Шанс: "  .. item.chance * 100 * 10 .. "%")
  127.         else
  128.             table.insert(result, "§l" .. displayName .. " x" .. item.count.. " Шанс: "  .. item.chance * 100 * 10 .. "%")
  129.         end
  130.     end
  131.     local resultString = table.concat(result, "\n")
  132. end
  133.  
  134. function Casino:sendMoney(count)
  135.     component.proxy(self.addressMoneyChest).transferItem(0, 1, count, 1, 1)
  136. end
  137.  
  138. function Casino:sendMoney(count)
  139.     component.proxy(self.addressMoneyChest).transferItem(0, 1, count, 1, 1)
  140. end
  141.  
  142. function Casino:generateWeightedArray()
  143.     local weightedArray = {}
  144.     for i, item in ipairs(self.items) do
  145.         local repetitions = math.floor(item.chance * 100)         -- Умножаем вероятность на 100, чтобы получить целое число для повторений
  146.         for j = 1, repetitions do
  147.             print(self.items.title .. " " .. tonumber(repetitions))
  148.             table.insert(weightedArray, i)
  149.         end
  150.     end
  151.     return weightedArray
  152. end
  153.  
  154. function Casino:weightedRoll()
  155.     local weightedArray = self:generateWeightedArray()
  156.     local randomIndex = math.random(1, #weightedArray)
  157.     print("Число индекса: " .. tonumber(randomIndex))
  158.     local itemIndex = weightedArray[randomIndex]
  159.     print(self.items[itemIndex])
  160.     return self.items[itemIndex]
  161. end
  162.  
  163. return Casino
  164.  
  165.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement