Advertisement
massacring

startup

Jun 21st, 2025 (edited)
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.30 KB | None | 0 0
  1. local Commons = require("Commons")
  2. local Card = require("Card")
  3. local Hand = require("Hand")
  4. local Button = require("Button")
  5. local cashout = require('cashout')
  6. local monitor = peripheral.find("monitor")
  7. local input = peripheral.find("minecraft:hopper")
  8. local storage = peripheral.find("minecraft:chest")
  9. local modem = peripheral.wrap("bottom")
  10. local printer = peripheral.find("printer")
  11. if printer.getInkLevel() == 0 and printer.getPaperLevel() == 0 then
  12.   error("Cannot start a new page. Do you have ink and paper?")
  13. end
  14.  
  15. -- Declares my own colors
  16. local m_colors = {
  17.     white = colors.white,
  18.     black = colors.orange,
  19.     gray = colors.magenta,
  20.     lightGray = colors.lightBlue,
  21.     darkRed = colors.yellow,
  22.     red = colors.lime,
  23.     lightRed = colors.pink,
  24.     green = colors.gray,
  25.     lightGreen = colors.lightGray,
  26.     darkYellow = colors.cyan,
  27.     yellow = colors.purple,
  28. }
  29.  
  30. local window = window.create(monitor, 1, 1, 80, 38)
  31.  
  32. -- Changes the default palette
  33. window.setPaletteColor(colors.orange, 0x191919) -- Black
  34. window.setPaletteColor(colors.magenta, 0x262626) -- Gray
  35. window.setPaletteColor(colors.lightBlue, 0x565656) -- Light Gray
  36. window.setPaletteColor(colors.yellow, 0xDD2F00) -- Dark Red
  37. window.setPaletteColor(colors.lime, 0xEF4A21) -- Red
  38. window.setPaletteColor(colors.pink, 0xFFBAAA) -- Light Red
  39. window.setPaletteColor(colors.gray, 0x355E19) -- Green
  40. window.setPaletteColor(colors.lightGray, 0x356D19) -- Light Green
  41. window.setPaletteColor(colors.cyan, 0xEAB327) -- Dark Yellow
  42. window.setPaletteColor(colors.purple, 0xEDC125) -- Yellow
  43. window.setPaletteColor(colors.blue, 0xE8C958) --
  44. window.setPaletteColor(colors.brown, 0xE8C958) --
  45. window.setPaletteColor(colors.green, 0xE8C958) --
  46. window.setPaletteColor(colors.red, 0xE8C958) --
  47. window.setPaletteColor(colors.black, 0xE8C958) --
  48.  
  49. monitor.setTextScale(0.5)
  50.  
  51. local width, height = window.getSize()
  52.  
  53. local playerHand = Hand.new(false)
  54. local dealerHand = Hand.new(false)
  55.  
  56. local startButton = {}
  57. local gameButtons = {}
  58. local gameStarted = false
  59. local multiplier = 2
  60.  
  61. local function clearSide(up)
  62.     local oldTerm = term.redirect(window)
  63.     local start,limit
  64.     if up then
  65.         start = 2
  66.         limit = height/2-3
  67.     else
  68.         start = height/2+3
  69.         limit = height
  70.     end
  71.     for x=1,width,1 do
  72.         for y=start,limit,1 do
  73.             if (y % 2 == 1) then
  74.                 term.setBackgroundColor(m_colors.green)
  75.             else
  76.                 term.setBackgroundColor(m_colors.lightGreen)
  77.             end
  78.             term.setCursorPos(x,y)
  79.             term.write(" ")
  80.         end
  81.     end
  82.     term.redirect(oldTerm)
  83. end
  84.  
  85. local function setupDealer(first)
  86.     clearSide(true)
  87.     dealerHand:draw(window, width, 2, first)
  88. end
  89.  
  90. local function setupPlayer()
  91.     clearSide(false)
  92.  
  93.     playerHand:draw(window, width, height-16)
  94. end
  95.  
  96. local function gameWin()
  97.     Card.drawWin(window, math.floor(width/2), math.floor(height/2))
  98.     Commons.Score.updateScore(Commons.Score.getScore() * multiplier)
  99.     cashout()
  100.     sleep(3)
  101.     os.reboot()
  102. end
  103.  
  104. local function gameLose()
  105.     Card.drawBust(window, math.floor(width/2), math.floor(height/2))
  106.     sleep(3)
  107.     os.reboot()
  108. end
  109.  
  110. local function gameDraw()
  111.     Card.drawDraw(window, math.floor(width/2), math.floor(height/2))
  112.     cashout()
  113.     sleep(3)
  114.     os.reboot()
  115. end
  116.  
  117. local function compareHands()
  118.     local playerValue = playerHand:evaluateHand()
  119.     local dealerValue = dealerHand:evaluateHand()
  120.     if playerValue > dealerValue then
  121.         gameWin()
  122.     elseif playerValue < dealerValue then
  123.         gameLose()
  124.     else
  125.         gameDraw()
  126.     end
  127. end
  128.  
  129. local function dealerPlay()
  130.     setupDealer()
  131.     local eval = dealerHand:evaluateHand()
  132.     sleep(0.5)
  133.     if eval == 0 then
  134.         gameWin()
  135.     elseif eval >= 17 then
  136.         compareHands()
  137.     end
  138.     sleep(0.5)
  139.     while true do
  140.         dealerHand:addCard(Card.newRandom(false))
  141.         setupDealer()
  142.         local eval = dealerHand:evaluateHand()
  143.         sleep(0.5)
  144.         if eval == 0 then
  145.             gameWin()
  146.         elseif eval >= 17 then
  147.             compareHands()
  148.         end
  149.         sleep(0.5)
  150.     end
  151. end
  152.  
  153. local function stand()
  154.     dealerPlay()
  155. end
  156.  
  157. local function hit()
  158.     playerHand:addCard(Card.newRandom(false))
  159.     setupPlayer()
  160.     local eval = playerHand:evaluateHand()
  161.     sleep(0.5)
  162.     if eval == 0 then
  163.         gameLose()
  164.     end
  165. end
  166.  
  167. local function double()
  168.     multiplier = multiplier * 2
  169.     hit()
  170.     stand()
  171. end
  172.  
  173. local function surrender()
  174.     Commons.Score.updateScore(math.floor(Commons.Score.getScore()/2))
  175.     gameDraw()
  176. end
  177.  
  178. local function createButtons()
  179.     local labelMap = {
  180.         ["Hit"] = hit,
  181.         ["Stand"] = stand,
  182.         --["Double Down"] = double,
  183.         ["Surrender"] = surrender
  184.     }
  185.     local labels = {
  186.         "Hit",
  187.         "Stand",
  188.         --"Double Down",
  189.         "Surrender"
  190.     }
  191.     local totalLength = 0
  192.     for _, label in ipairs(labels) do
  193.         totalLength = totalLength + #label + 3
  194.     end
  195.     local startX = math.floor(width / 2) - math.floor(totalLength / 2)
  196.     local x = startX
  197.     local y = math.floor(height / 2 - 1)
  198.     for _, label in ipairs(labels) do
  199.         local button = Button.new(label, labelMap[label], x, y, #label, 1, 0, m_colors.red, m_colors.darkRed, m_colors.white)
  200.         table.insert(gameButtons, button)
  201.         x = x + #label + 3
  202.     end
  203. end
  204.  
  205. local function drawButtons()
  206.     for _, button in ipairs(gameButtons) do
  207.         button:displayOnScreen(window, Commons.Paint.drawSquare, Commons.Paint.write)
  208.     end
  209. end
  210.  
  211. local function start()
  212.     if Commons.Credits.selectedCredit == nil then return end
  213.     gameStarted = true
  214.     startButton:disable()
  215.     Commons.Paint.clear(window, width, height, m_colors.green, m_colors.lightGreen)
  216.     Commons.Credits.drawCredit(window, 1, 1, m_colors.white, m_colors.red)
  217.     Commons.Score.drawScore(window, 2, height/2 - 2, m_colors.white, m_colors.yellow, m_colors.white, m_colors.lightGray)
  218.     setupDealer(true)
  219.     setupPlayer()
  220.     createButtons()
  221.     drawButtons()
  222. end
  223.  
  224. local function createStartButton()
  225.     local label = "Start Game!"
  226.     local x = math.floor(width / 2 - 1) - math.floor(#label / 2 + 1)
  227.     local y = math.floor(height / 2 - 1) - 1
  228.     startButton = Button.new(label, start, x, y, #label, 1, 1, m_colors.darkRed, m_colors.red, m_colors.white)
  229.     startButton:displayOnScreen(window, Commons.Paint.drawSquare, Commons.Paint.write)
  230. end
  231.  
  232. local function countCredits()
  233.     local shouldThrow = false
  234.     for i = 1,input.size(),1 do
  235.         local item = input.getItemDetail(i)
  236.         if item ~= nil
  237.             and Commons.Credits.validCurrency(item.name)
  238.             and (Commons.Credits.selectedCredit == nil or Commons.Credits.selectedCredit == Commons.Credits.getName(item.name))
  239.         then
  240.             if Commons.Credits.selectedCredit == nil then
  241.                 Commons.Credits.selectedCredit = Commons.Credits.getName(item.name)
  242.                 Commons.Credits.drawCredit(window, 1, 1, m_colors.white, m_colors.red)
  243.             end
  244.             local count = item.count
  245.             local value = Commons.Credits.getValueByName(item.name)
  246.             for _ = 1, count, 1 do
  247.                 local newValue = Commons.Score.getScore() + value
  248.                 if (newValue > Commons.Score.max) then
  249.                     input.pushItems(modem.getNameLocal(), i, 1)
  250.                     shouldThrow = true
  251.                 else
  252.                     Commons.Score.updateScore(newValue)
  253.                     Commons.Score.drawScore(window, width / 2, 3, m_colors.white, m_colors.yellow, m_colors.white, m_colors.lightGray, true)
  254.                     input.pushItems(peripheral.getName(storage), i, 1)
  255.                 end
  256.             end
  257.         elseif item ~= nil then
  258.             input.pushItems(modem.getNameLocal(), i)
  259.             shouldThrow = true
  260.         end
  261.     end
  262.     if shouldThrow then
  263.         for i = 1,16,1 do
  264.             turtle.select(i)
  265.             turtle.drop()
  266.         end
  267.         turtle.select(1)
  268.     end
  269. end
  270.  
  271. local function drawScreen()
  272.     Commons.Paint.clear(window, width, height, m_colors.green, m_colors.lightGreen)
  273.     Commons.Score.drawScore(window, width / 2, 3, m_colors.white, m_colors.yellow, m_colors.white, m_colors.lightGray, true)
  274.     createStartButton()
  275. end
  276.  
  277. local function tick()
  278.     while true do
  279.         if (not gameStarted) then countCredits() end
  280.         sleep(0.05)
  281.     end
  282. end
  283.  
  284. local function events()
  285.     sleep(0.5)
  286.     while true do
  287.         local eventData = {os.pullEvent()}
  288.         local event = eventData[1]
  289.         if event == "key_up" and eventData[2] == keys.q then
  290.             return
  291.         elseif event == "monitor_touch" then
  292.             local x, y = eventData[3], eventData[4]
  293.             if not gameStarted and startButton:collides(x, y) then
  294.                 startButton.clickEvent()
  295.             elseif gameStarted then
  296.                 for _, button in pairs(gameButtons) do
  297.                     if button:collides(x, y) then
  298.                         button.clickEvent()
  299.                     end
  300.                 end
  301.             end
  302.             sleep(0.1)
  303.         end
  304.     end
  305. end
  306.  
  307. local function runGame()
  308.     while true do
  309.         drawScreen()
  310.         parallel.waitForAny(tick, events)
  311.     end
  312. end
  313.  
  314. runGame()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement