Advertisement
massacring

Gacha

Oct 15th, 2024 (edited)
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.08 KB | None | 0 0
  1. local chatBox = peripheral.find("chatBox")
  2. if (chatBox == nil) then
  3.     error("No Chat Box peripheral connected.", 0)
  4. end
  5.  
  6. local storage = peripheral.wrap("top")
  7. local input = peripheral.wrap("left")
  8. local output = peripheral.wrap("right")
  9.  
  10. local inputSlot, inputItem
  11.  
  12. local winnings = {}
  13. local values = {
  14.     ["minecraft:gold_nugget"]=1,
  15.     ["minecraft:coal"]=2,
  16.     ["minecraft:redstone"]=3,
  17.     ["minecraft:glowstone_dust"]=3,
  18.     ["minecraft:amethyst_shard"]=4,
  19.     ["minecraft:copper_ingot"]=4,
  20.     ["minecraft:iron_ingot"]=6,
  21.     ["minecraft:quartz"]=7,
  22.     ["minecraft:gold_ingot"]=9,
  23.     ["kubejs:small_coin"]=9,
  24.     ["minecraft:experience_bottle"]=18,
  25.     ["minecraft:copper_block"]=36,
  26.     ["kubejs:medium_coin"]=36,
  27.     ["minecraft:diamond"]=45,
  28.     ["minecraft:iron_block"]=54,
  29.     ["minecraft:gold_block"]=81,
  30.     ["kubejs:small_coin_block"]=81,
  31.     ["kubejs:big_coin"]=144,
  32.     ["kubejs:medium_coin_block"]=324,
  33.     ["minecraft:diamond_block"]=405,
  34. }
  35.  
  36. local function GetValuesBelow(num)
  37.     local availableValues = {}
  38.     for name, value in pairs(values) do
  39.         if value <= num then availableValues[name] = value end
  40.     end
  41.     return availableValues
  42. end
  43.  
  44. local function GetRandomValue(randomTable)
  45.     local valueNames = {}
  46.     for name in pairs(randomTable) do
  47.         table.insert(valueNames, name)
  48.     end
  49.  
  50.     ::retry::
  51.     local randomName = valueNames[math.random(#valueNames)]
  52.     local randomValue = randomTable[randomName]
  53.     if math.random(#valueNames) > randomValue
  54.     or math.random(#valueNames) > randomValue
  55.     or math.random(#valueNames) > randomValue
  56.     then goto retry end
  57.  
  58.     return randomName, randomValue
  59. end
  60.  
  61. --[[
  62. local function GetHighestValue(num)
  63.     local maxValue = 0
  64.  
  65.     local HighestValues = {}
  66.  
  67.     for _, value in pairs(values) do
  68.         if value > num then goto continue end
  69.         if value <= maxValue then goto continue end
  70.  
  71.         maxValue = value
  72.  
  73.         ::continue::
  74.     end
  75.  
  76.     for name, value in pairs(values) do
  77.         if value == maxValue then HighestValues[name] = value end
  78.     end
  79.  
  80.     local randomMaxName, randomMaxValue = GetRandomValue(HighestValues)
  81.  
  82.     return randomMaxName, randomMaxValue
  83. end
  84. --]]
  85.  
  86. local function GetRandomValueBelow(num)
  87.     local availableValues = GetValuesBelow(num)
  88.     local next = next
  89.  
  90.     if next(availableValues) == nil then return end
  91.  
  92.     local randomName, randomValue = GetRandomValue(availableValues)
  93.  
  94.     return randomName, randomValue
  95. end
  96.  
  97. local function GetItemSlot(inventory, name)
  98.     for i = 1,inventory.size(),1 do
  99.         local item = inventory.getItemDetail(i)
  100.         if item == nil then goto continue end
  101.         if (item.name == name) then return i end
  102.         ::continue::
  103.     end
  104. end
  105.  
  106. local function GrantRewards()
  107.     for name, data in pairs(winnings) do
  108.         if data.slot ~= nil then storage.pushItems(peripheral.getName(output), data.slot, data.amount) end
  109.     end
  110.  
  111.     winnings = {}
  112. end
  113.  
  114. local function OutOfCommission(message)
  115.     chatBox.sendMessage(message, "&cMassaWish Inc", "[]", "&8", 30)
  116.  
  117.     for i = 1, input.size(), 1 do
  118.         input.pushItems(peripheral.getName(output), i)
  119.     end
  120.     storage.pushItems(peripheral.getName(output), GetItemSlot(storage, inputItem.name), 1)
  121.  
  122.     GrantRewards()
  123.  
  124.     sleep(15)
  125.     os.reboot()
  126. end
  127.  
  128. local function CheckWinning(name)
  129.     local slot = GetItemSlot(storage, name)
  130.     if slot == nil then
  131.         print("Out of " .. name)
  132.         return false
  133.     end
  134.     local item = storage.getItemDetail(slot)
  135.     if item == nil or item.count < winnings[name].amount then
  136.         print("Out of " .. name)
  137.         -- Out of item
  138.         -- If possible, craft more
  139.         return false
  140.     end
  141.     winnings[name].slot = slot
  142.     return true
  143. end
  144.  
  145. local function AddWinning(name, amount)
  146.     amount = amount or 1
  147.     if winnings[name] == nil then
  148.         winnings[name] = { amount = amount }
  149.     else
  150.         winnings[name].amount = winnings[name].amount + amount
  151.     end
  152.  
  153.     if not CheckWinning(name) then OutOfCommission("The magic has run dry. Contact massacring.") end
  154. end
  155.  
  156. local function GenerateWinnings(num)
  157.     local availableValue = num
  158.  
  159.     while availableValue > 0 do
  160.         local randomName, randomValue = GetRandomValueBelow(availableValue)
  161.         if randomName == nil then break end
  162.  
  163.         AddWinning(randomName)
  164.         availableValue = availableValue - randomValue
  165.     end
  166. end
  167.  
  168. local function ToggleRedstone(side, power)
  169.     redstone.setAnalogOutput(side, power)
  170.     sleep(0.05)
  171.     redstone.setAnalogOutput(side, 0)
  172. end
  173.  
  174. local function MakeAWish()
  175.     local lot = math.random(100)
  176.     local value = values[inputItem.name]
  177.     if lot < 40 then
  178.         -- 40% Chance to lose
  179.         parallel.waitForAll(
  180.             function() ToggleRedstone("front", 1) end,
  181.             function() print("Lost :(") end
  182.         )
  183.     elseif lot < 60 then
  184.         -- 20% Chance to cut Losses
  185.         parallel.waitForAll(
  186.             function() ToggleRedstone("front", 2) end,
  187.             function()
  188.                 print("Cut Losses")
  189.                 GenerateWinnings(math.floor(value/2))
  190.             end
  191.         )
  192.     elseif lot < 75 then
  193.         -- 15% Chance to get item back
  194.         parallel.waitForAll(
  195.             function() ToggleRedstone("front", 3) end,
  196.             function()
  197.                 print("Item Back")
  198.                 AddWinning(inputItem.name)
  199.             end
  200.         )
  201.     elseif lot < 90 then
  202.         -- 15% Chance to get 2x value
  203.         parallel.waitForAll(
  204.             function() ToggleRedstone("front", 4) end,
  205.             function()
  206.                 print("2x Value")
  207.                 GenerateWinnings(value*2)
  208.             end
  209.         )
  210.     elseif lot < 98 then
  211.         -- 8% Chance to get 3x value
  212.         parallel.waitForAll(
  213.             function() ToggleRedstone("front", 5) end,
  214.             function()
  215.                 print("3x Value")
  216.                 GenerateWinnings(value*3)
  217.             end
  218.         )
  219.     else
  220.         -- 2% Chance to get 7x value
  221.         parallel.waitForAll(
  222.             function() ToggleRedstone("front", 6) end,
  223.             function()
  224.                 print("7x Value")
  225.                 chatBox.sendMessage("JACKPOT! Congratulations!", "&aMassaWish Inc", "[]", "&8", 30)
  226.                 GenerateWinnings(value*7)
  227.             end
  228.         )
  229.     end
  230. end
  231.  
  232. local function GetInput()
  233.     inputSlot, inputItem = nil, nil
  234.     local next = next
  235.     local items = input.list()
  236.     while next(items) ~= nil and inputItem == nil do
  237.         inputSlot = next(items)
  238.         inputItem = input.getItemDetail(inputSlot)
  239.  
  240.         if values[inputItem.name] == nil then
  241.             input.pushItems(peripheral.getName(output), inputSlot)
  242.             inputSlot, inputItem = nil, nil
  243.             items = input.list()
  244.         end
  245.     end
  246. end
  247.  
  248. while true do
  249.     for i = 1, 10, 1 do
  250.         GetInput()
  251.         if inputItem == nil then break end
  252.  
  253.         input.pushItems(peripheral.getName(storage), inputSlot, 1)
  254.         MakeAWish()
  255.         sleep(0.05)
  256.     end
  257.     GrantRewards()
  258.     sleep(2)
  259. end
  260.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement