Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Diamond Casino Game Launcher
- -- Main menu for switching between turtle casino games
- -- Includes shared player credit tracking and cash out button
- --------------------------------------------------
- -- CONFIG
- --------------------------------------------------
- local DIAMOND_ID = "minecraft:diamond"
- local TEXT_SCALE = 0.5
- local CASINO_BANK_FILE = "casino_bank_base.txt"
- local ROULETTE_PROGRAM = "roulette"
- local BLACKJACK_PROGRAM = "blackjack"
- local PLINKO_PROGRAM = "plinko"
- --------------------------------------------------
- -- PERIPHERALS
- --------------------------------------------------
- local mon = peripheral.find("monitor")
- if not mon then
- error("No monitor found.")
- end
- local speaker = nil
- for _, name in ipairs(peripheral.getNames()) do
- local p = peripheral.wrap(name)
- if p then
- if type(p.playNote) == "function" or type(p.playSound) == "function" then
- speaker = p
- break
- end
- end
- end
- mon.setTextScale(TEXT_SCALE)
- mon.setBackgroundColor(colors.black)
- mon.setTextColor(colors.white)
- mon.clear()
- local W, H = mon.getSize()
- --------------------------------------------------
- -- UTILS
- --------------------------------------------------
- local buttons = {}
- local message = "Deposit diamonds into the turtle, then choose a game."
- local function writeAt(x, y, text, fg, bg)
- if bg then mon.setBackgroundColor(bg) end
- if fg then mon.setTextColor(fg) end
- mon.setCursorPos(x, y)
- mon.write(text)
- end
- local function fillBox(x, y, w, h, bg)
- if w <= 0 or h <= 0 then return end
- mon.setBackgroundColor(bg)
- for yy = y, y + h - 1 do
- if yy >= 1 and yy <= H then
- mon.setCursorPos(x, yy)
- mon.write(string.rep(" ", math.max(0, math.min(w, W - x + 1))))
- end
- end
- end
- local function centerText(x, y, w, text, fg, bg)
- text = tostring(text)
- if #text > w then
- text = string.sub(text, 1, w)
- end
- local pad = math.floor((w - #text) / 2)
- writeAt(x + pad, y, text, fg, bg)
- end
- local function drawButton(id, x, y, w, h, label, bg, fg)
- fillBox(x, y, w, h, bg)
- centerText(x, y + math.floor(h / 2), w, label, fg or colors.white, bg)
- table.insert(buttons, {
- id = id,
- x = x,
- y = y,
- w = w,
- h = h
- })
- end
- local function inBox(px, py, b)
- return px >= b.x and px < b.x + b.w and py >= b.y and py < b.y + b.h
- end
- --------------------------------------------------
- -- SOUND
- --------------------------------------------------
- local function playNoteSafe(instrument, volume, pitch)
- if not speaker then return end
- instrument = instrument or "harp"
- volume = volume or 1
- pitch = pitch or 12
- if type(speaker.playNote) == "function" then
- local ok = pcall(function()
- speaker.playNote(instrument, volume, pitch)
- end)
- if ok then
- return
- end
- end
- if type(speaker.playSound) == "function" then
- pcall(function()
- speaker.playSound("note.harp", volume, 1)
- end)
- end
- end
- local function soundClick()
- playNoteSafe("hat", 0.8, 12)
- end
- local function soundWin()
- playNoteSafe("bell", 1, 16)
- sleep(0.08)
- playNoteSafe("bell", 1, 20)
- sleep(0.08)
- playNoteSafe("bell", 1, 24)
- end
- local function soundLoss()
- playNoteSafe("bass", 1, 6)
- sleep(0.12)
- playNoteSafe("bass", 1, 4)
- end
- --------------------------------------------------
- -- DIAMOND BANKING
- --------------------------------------------------
- local function countDiamonds()
- local total = 0
- for slot = 1, 16 do
- local d = turtle.getItemDetail(slot)
- if d and d.name == DIAMOND_ID then
- total = total + d.count
- end
- end
- return total
- end
- local function selectDiamondSlot()
- for slot = 1, 16 do
- local d = turtle.getItemDetail(slot)
- if d and d.name == DIAMOND_ID and d.count > 0 then
- turtle.select(slot)
- return true
- end
- end
- return false
- end
- local function dropDiamondsDown(amount)
- local remaining = amount
- while remaining > 0 do
- if not selectDiamondSlot() then
- return false, amount - remaining
- end
- local d = turtle.getItemDetail()
- if not d or d.name ~= DIAMOND_ID then
- return false, amount - remaining
- end
- local toDrop = math.min(remaining, d.count)
- local ok = turtle.dropDown(toDrop)
- if not ok then
- return false, amount - remaining
- end
- remaining = remaining - toDrop
- sleep(0.03)
- end
- return true, amount
- end
- local function loadBankBase()
- if fs.exists(CASINO_BANK_FILE) then
- local f = fs.open(CASINO_BANK_FILE, "r")
- if f then
- local raw = f.readAll()
- f.close()
- local n = tonumber(raw)
- if n and n >= 0 then
- return n
- end
- end
- end
- return countDiamonds()
- end
- local bankBase = loadBankBase()
- local function saveBankBase()
- local f = fs.open(CASINO_BANK_FILE, "w")
- if f then
- f.write(tostring(math.floor(bankBase)))
- f.close()
- end
- end
- local function playerCredits()
- local credits = countDiamonds() - bankBase
- if credits < 0 then
- credits = 0
- end
- return credits
- end
- saveBankBase()
- --------------------------------------------------
- -- CASH OUT
- --------------------------------------------------
- local confirmCashOut = false
- local function cashOutAll()
- local amount = playerCredits()
- if amount <= 0 then
- message = "No player credits to cash out."
- soundLoss()
- return
- end
- local ok, dropped = dropDiamondsDown(amount)
- if ok then
- message = "Cashed out " .. dropped .. " diamond(s) below."
- soundWin()
- else
- message = "Cash out blocked! Dropped " .. dropped .. "/" .. amount .. "."
- soundLoss()
- end
- saveBankBase()
- end
- local function requestCashOut()
- local amount = playerCredits()
- if amount <= 0 then
- message = "No player credits to cash out."
- soundLoss()
- return
- end
- confirmCashOut = true
- message = "Confirm cash out of " .. amount .. " diamond(s)."
- soundClick()
- end
- --------------------------------------------------
- -- GAME LAUNCHING
- --------------------------------------------------
- local function launchGame(programName)
- saveBankBase()
- mon.setBackgroundColor(colors.black)
- mon.setTextColor(colors.white)
- mon.clear()
- if fs.exists(programName) then
- shell.run(programName)
- elseif fs.exists(programName .. ".lua") then
- shell.run(programName .. ".lua")
- else
- message = "Missing program: " .. programName
- soundLoss()
- return
- end
- bankBase = loadBankBase()
- end
- --------------------------------------------------
- -- DRAWING
- --------------------------------------------------
- local function drawConfirmCashOut()
- buttons = {}
- fillBox(1, 1, W, H, colors.black)
- centerText(1, 3, W, "CONFIRM CASH OUT", colors.yellow, colors.black)
- centerText(1, 6, W, "Player credits: " .. playerCredits() .. " diamond(s)", colors.white, colors.black)
- centerText(1, 8, W, "This will drop all player credits below the turtle.", colors.lightGray, colors.black)
- centerText(1, 10, W, "House bank diamonds will stay inside the casino.", colors.lightGray, colors.black)
- local btnW = 32
- local btnH = 5
- local gap = 6
- local totalW = btnW * 2 + gap
- local startX = math.floor((W - totalW) / 2)
- local y = 14
- drawButton("cashout_cancel", startX, y, btnW, btnH, "CANCEL", colors.lime, colors.black)
- drawButton("cashout_confirm", startX + btnW + gap, y, btnW, btnH, "CASH OUT ALL", colors.yellow, colors.black)
- end
- local function drawMenu()
- if confirmCashOut then
- drawConfirmCashOut()
- return
- end
- buttons = {}
- mon.setBackgroundColor(colors.black)
- mon.clear()
- fillBox(1, 1, W, 1, colors.gray)
- centerText(1, 1, W, "DIAMOND CASINO GAME MENU", colors.lime, colors.gray)
- centerText(1, 4, W, "Tap a game to start", colors.white, colors.black)
- local infoW = math.min(78, W - 4)
- local infoX = math.floor((W - infoW) / 2)
- fillBox(infoX, 6, infoW, 5, colors.gray)
- centerText(infoX, 6, infoW, "CASINO STATUS", colors.yellow, colors.gray)
- centerText(infoX, 8, infoW, "Player Credits: " .. playerCredits() .. " diamond(s)", colors.lime, colors.gray)
- centerText(infoX, 9, infoW, "Diamonds In Turtle: " .. countDiamonds() .. " | House Bank Base: " .. bankBase, colors.white, colors.gray)
- fillBox(infoX, 12, infoW, 3, colors.black)
- centerText(infoX, 13, infoW, message, colors.lightGray, colors.black)
- local btnW = 24
- local btnH = 5
- local gap = 3
- local totalW = btnW * 4 + gap * 3
- local startX = math.floor((W - totalW) / 2)
- local y = math.floor(H / 2) - 1
- drawButton("roulette", startX, y, btnW, btnH, "ROULETTE", colors.green, colors.black)
- drawButton("blackjack", startX + btnW + gap, y, btnW, btnH, "BLACKJACK", colors.lime, colors.black)
- drawButton("plinko", startX + (btnW + gap) * 2, y, btnW, btnH, "PLINKO", colors.orange, colors.black)
- drawButton("cashout", startX + (btnW + gap) * 3, y, btnW, btnH, "CASH OUT", colors.yellow, colors.black)
- centerText(1, H - 4, W, "Deposit diamonds into the turtle to add credits.", colors.lightGray, colors.black)
- centerText(1, H - 3, W, "Cash Out drops player credits below the turtle.", colors.lightGray, colors.black)
- end
- --------------------------------------------------
- -- INPUT
- --------------------------------------------------
- local function handleTouch(x, y)
- for _, b in ipairs(buttons) do
- if inBox(x, y, b) then
- if b.id == "roulette" then
- soundClick()
- launchGame(ROULETTE_PROGRAM)
- drawMenu()
- return
- elseif b.id == "blackjack" then
- soundClick()
- launchGame(BLACKJACK_PROGRAM)
- drawMenu()
- return
- elseif b.id == "plinko" then
- soundClick()
- launchGame(PLINKO_PROGRAM)
- drawMenu()
- return
- elseif b.id == "cashout" then
- requestCashOut()
- drawMenu()
- return
- elseif b.id == "cashout_cancel" then
- confirmCashOut = false
- message = "Cash out cancelled."
- soundClick()
- drawMenu()
- return
- elseif b.id == "cashout_confirm" then
- confirmCashOut = false
- cashOutAll()
- drawMenu()
- return
- end
- end
- end
- end
- --------------------------------------------------
- -- MAIN
- --------------------------------------------------
- drawMenu()
- while true do
- local e, side, x, y = os.pullEvent()
- if e == "monitor_touch" then
- handleTouch(x, y)
- elseif e == "peripheral" or e == "peripheral_detach" then
- drawMenu()
- elseif e == "timer" then
- drawMenu()
- end
- end
Add Comment
Please, Sign In to add comment