TechManDylan

CCcasino

Apr 30th, 2026 (edited)
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.44 KB | None | 0 0
  1. -- Diamond Casino Game Launcher
  2. -- Main menu for switching between turtle casino games
  3. -- Includes shared player credit tracking and cash out button
  4.  
  5. --------------------------------------------------
  6. -- CONFIG
  7. --------------------------------------------------
  8.  
  9. local DIAMOND_ID = "minecraft:diamond"
  10. local TEXT_SCALE = 0.5
  11.  
  12. local CASINO_BANK_FILE = "casino_bank_base.txt"
  13.  
  14. local ROULETTE_PROGRAM = "roulette"
  15. local BLACKJACK_PROGRAM = "blackjack"
  16. local PLINKO_PROGRAM = "plinko"
  17.  
  18. --------------------------------------------------
  19. -- PERIPHERALS
  20. --------------------------------------------------
  21.  
  22. local mon = peripheral.find("monitor")
  23.  
  24. if not mon then
  25. error("No monitor found.")
  26. end
  27.  
  28. local speaker = nil
  29.  
  30. for _, name in ipairs(peripheral.getNames()) do
  31. local p = peripheral.wrap(name)
  32. if p then
  33. if type(p.playNote) == "function" or type(p.playSound) == "function" then
  34. speaker = p
  35. break
  36. end
  37. end
  38. end
  39.  
  40. mon.setTextScale(TEXT_SCALE)
  41. mon.setBackgroundColor(colors.black)
  42. mon.setTextColor(colors.white)
  43. mon.clear()
  44.  
  45. local W, H = mon.getSize()
  46.  
  47. --------------------------------------------------
  48. -- UTILS
  49. --------------------------------------------------
  50.  
  51. local buttons = {}
  52. local message = "Deposit diamonds into the turtle, then choose a game."
  53.  
  54. local function writeAt(x, y, text, fg, bg)
  55. if bg then mon.setBackgroundColor(bg) end
  56. if fg then mon.setTextColor(fg) end
  57. mon.setCursorPos(x, y)
  58. mon.write(text)
  59. end
  60.  
  61. local function fillBox(x, y, w, h, bg)
  62. if w <= 0 or h <= 0 then return end
  63.  
  64. mon.setBackgroundColor(bg)
  65.  
  66. for yy = y, y + h - 1 do
  67. if yy >= 1 and yy <= H then
  68. mon.setCursorPos(x, yy)
  69. mon.write(string.rep(" ", math.max(0, math.min(w, W - x + 1))))
  70. end
  71. end
  72. end
  73.  
  74. local function centerText(x, y, w, text, fg, bg)
  75. text = tostring(text)
  76.  
  77. if #text > w then
  78. text = string.sub(text, 1, w)
  79. end
  80.  
  81. local pad = math.floor((w - #text) / 2)
  82. writeAt(x + pad, y, text, fg, bg)
  83. end
  84.  
  85. local function drawButton(id, x, y, w, h, label, bg, fg)
  86. fillBox(x, y, w, h, bg)
  87. centerText(x, y + math.floor(h / 2), w, label, fg or colors.white, bg)
  88.  
  89. table.insert(buttons, {
  90. id = id,
  91. x = x,
  92. y = y,
  93. w = w,
  94. h = h
  95. })
  96. end
  97.  
  98. local function inBox(px, py, b)
  99. return px >= b.x and px < b.x + b.w and py >= b.y and py < b.y + b.h
  100. end
  101.  
  102. --------------------------------------------------
  103. -- SOUND
  104. --------------------------------------------------
  105.  
  106. local function playNoteSafe(instrument, volume, pitch)
  107. if not speaker then return end
  108.  
  109. instrument = instrument or "harp"
  110. volume = volume or 1
  111. pitch = pitch or 12
  112.  
  113. if type(speaker.playNote) == "function" then
  114. local ok = pcall(function()
  115. speaker.playNote(instrument, volume, pitch)
  116. end)
  117.  
  118. if ok then
  119. return
  120. end
  121. end
  122.  
  123. if type(speaker.playSound) == "function" then
  124. pcall(function()
  125. speaker.playSound("note.harp", volume, 1)
  126. end)
  127. end
  128. end
  129.  
  130. local function soundClick()
  131. playNoteSafe("hat", 0.8, 12)
  132. end
  133.  
  134. local function soundWin()
  135. playNoteSafe("bell", 1, 16)
  136. sleep(0.08)
  137. playNoteSafe("bell", 1, 20)
  138. sleep(0.08)
  139. playNoteSafe("bell", 1, 24)
  140. end
  141.  
  142. local function soundLoss()
  143. playNoteSafe("bass", 1, 6)
  144. sleep(0.12)
  145. playNoteSafe("bass", 1, 4)
  146. end
  147.  
  148. --------------------------------------------------
  149. -- DIAMOND BANKING
  150. --------------------------------------------------
  151.  
  152. local function countDiamonds()
  153. local total = 0
  154.  
  155. for slot = 1, 16 do
  156. local d = turtle.getItemDetail(slot)
  157. if d and d.name == DIAMOND_ID then
  158. total = total + d.count
  159. end
  160. end
  161.  
  162. return total
  163. end
  164.  
  165. local function selectDiamondSlot()
  166. for slot = 1, 16 do
  167. local d = turtle.getItemDetail(slot)
  168.  
  169. if d and d.name == DIAMOND_ID and d.count > 0 then
  170. turtle.select(slot)
  171. return true
  172. end
  173. end
  174.  
  175. return false
  176. end
  177.  
  178. local function dropDiamondsDown(amount)
  179. local remaining = amount
  180.  
  181. while remaining > 0 do
  182. if not selectDiamondSlot() then
  183. return false, amount - remaining
  184. end
  185.  
  186. local d = turtle.getItemDetail()
  187.  
  188. if not d or d.name ~= DIAMOND_ID then
  189. return false, amount - remaining
  190. end
  191.  
  192. local toDrop = math.min(remaining, d.count)
  193. local ok = turtle.dropDown(toDrop)
  194.  
  195. if not ok then
  196. return false, amount - remaining
  197. end
  198.  
  199. remaining = remaining - toDrop
  200. sleep(0.03)
  201. end
  202.  
  203. return true, amount
  204. end
  205.  
  206. local function loadBankBase()
  207. if fs.exists(CASINO_BANK_FILE) then
  208. local f = fs.open(CASINO_BANK_FILE, "r")
  209. if f then
  210. local raw = f.readAll()
  211. f.close()
  212.  
  213. local n = tonumber(raw)
  214. if n and n >= 0 then
  215. return n
  216. end
  217. end
  218. end
  219.  
  220. return countDiamonds()
  221. end
  222.  
  223. local bankBase = loadBankBase()
  224.  
  225. local function saveBankBase()
  226. local f = fs.open(CASINO_BANK_FILE, "w")
  227. if f then
  228. f.write(tostring(math.floor(bankBase)))
  229. f.close()
  230. end
  231. end
  232.  
  233. local function playerCredits()
  234. local credits = countDiamonds() - bankBase
  235.  
  236. if credits < 0 then
  237. credits = 0
  238. end
  239.  
  240. return credits
  241. end
  242.  
  243. saveBankBase()
  244.  
  245. --------------------------------------------------
  246. -- CASH OUT
  247. --------------------------------------------------
  248.  
  249. local confirmCashOut = false
  250.  
  251. local function cashOutAll()
  252. local amount = playerCredits()
  253.  
  254. if amount <= 0 then
  255. message = "No player credits to cash out."
  256. soundLoss()
  257. return
  258. end
  259.  
  260. local ok, dropped = dropDiamondsDown(amount)
  261.  
  262. if ok then
  263. message = "Cashed out " .. dropped .. " diamond(s) below."
  264. soundWin()
  265. else
  266. message = "Cash out blocked! Dropped " .. dropped .. "/" .. amount .. "."
  267. soundLoss()
  268. end
  269.  
  270. saveBankBase()
  271. end
  272.  
  273. local function requestCashOut()
  274. local amount = playerCredits()
  275.  
  276. if amount <= 0 then
  277. message = "No player credits to cash out."
  278. soundLoss()
  279. return
  280. end
  281.  
  282. confirmCashOut = true
  283. message = "Confirm cash out of " .. amount .. " diamond(s)."
  284. soundClick()
  285. end
  286.  
  287. --------------------------------------------------
  288. -- GAME LAUNCHING
  289. --------------------------------------------------
  290.  
  291. local function launchGame(programName)
  292. saveBankBase()
  293.  
  294. mon.setBackgroundColor(colors.black)
  295. mon.setTextColor(colors.white)
  296. mon.clear()
  297.  
  298. if fs.exists(programName) then
  299. shell.run(programName)
  300. elseif fs.exists(programName .. ".lua") then
  301. shell.run(programName .. ".lua")
  302. else
  303. message = "Missing program: " .. programName
  304. soundLoss()
  305. return
  306. end
  307.  
  308. bankBase = loadBankBase()
  309. end
  310.  
  311. --------------------------------------------------
  312. -- DRAWING
  313. --------------------------------------------------
  314.  
  315. local function drawConfirmCashOut()
  316. buttons = {}
  317.  
  318. fillBox(1, 1, W, H, colors.black)
  319.  
  320. centerText(1, 3, W, "CONFIRM CASH OUT", colors.yellow, colors.black)
  321. centerText(1, 6, W, "Player credits: " .. playerCredits() .. " diamond(s)", colors.white, colors.black)
  322. centerText(1, 8, W, "This will drop all player credits below the turtle.", colors.lightGray, colors.black)
  323. centerText(1, 10, W, "House bank diamonds will stay inside the casino.", colors.lightGray, colors.black)
  324.  
  325. local btnW = 32
  326. local btnH = 5
  327. local gap = 6
  328. local totalW = btnW * 2 + gap
  329. local startX = math.floor((W - totalW) / 2)
  330. local y = 14
  331.  
  332. drawButton("cashout_cancel", startX, y, btnW, btnH, "CANCEL", colors.lime, colors.black)
  333. drawButton("cashout_confirm", startX + btnW + gap, y, btnW, btnH, "CASH OUT ALL", colors.yellow, colors.black)
  334. end
  335.  
  336. local function drawMenu()
  337. if confirmCashOut then
  338. drawConfirmCashOut()
  339. return
  340. end
  341.  
  342. buttons = {}
  343.  
  344. mon.setBackgroundColor(colors.black)
  345. mon.clear()
  346.  
  347. fillBox(1, 1, W, 1, colors.gray)
  348. centerText(1, 1, W, "DIAMOND CASINO GAME MENU", colors.lime, colors.gray)
  349.  
  350. centerText(1, 4, W, "Tap a game to start", colors.white, colors.black)
  351.  
  352. local infoW = math.min(78, W - 4)
  353. local infoX = math.floor((W - infoW) / 2)
  354.  
  355. fillBox(infoX, 6, infoW, 5, colors.gray)
  356. centerText(infoX, 6, infoW, "CASINO STATUS", colors.yellow, colors.gray)
  357. centerText(infoX, 8, infoW, "Player Credits: " .. playerCredits() .. " diamond(s)", colors.lime, colors.gray)
  358. centerText(infoX, 9, infoW, "Diamonds In Turtle: " .. countDiamonds() .. " | House Bank Base: " .. bankBase, colors.white, colors.gray)
  359.  
  360. fillBox(infoX, 12, infoW, 3, colors.black)
  361. centerText(infoX, 13, infoW, message, colors.lightGray, colors.black)
  362.  
  363. local btnW = 24
  364. local btnH = 5
  365. local gap = 3
  366.  
  367. local totalW = btnW * 4 + gap * 3
  368. local startX = math.floor((W - totalW) / 2)
  369. local y = math.floor(H / 2) - 1
  370.  
  371. drawButton("roulette", startX, y, btnW, btnH, "ROULETTE", colors.green, colors.black)
  372. drawButton("blackjack", startX + btnW + gap, y, btnW, btnH, "BLACKJACK", colors.lime, colors.black)
  373. drawButton("plinko", startX + (btnW + gap) * 2, y, btnW, btnH, "PLINKO", colors.orange, colors.black)
  374. drawButton("cashout", startX + (btnW + gap) * 3, y, btnW, btnH, "CASH OUT", colors.yellow, colors.black)
  375.  
  376. centerText(1, H - 4, W, "Deposit diamonds into the turtle to add credits.", colors.lightGray, colors.black)
  377. centerText(1, H - 3, W, "Cash Out drops player credits below the turtle.", colors.lightGray, colors.black)
  378. end
  379.  
  380. --------------------------------------------------
  381. -- INPUT
  382. --------------------------------------------------
  383.  
  384. local function handleTouch(x, y)
  385. for _, b in ipairs(buttons) do
  386. if inBox(x, y, b) then
  387. if b.id == "roulette" then
  388. soundClick()
  389. launchGame(ROULETTE_PROGRAM)
  390. drawMenu()
  391. return
  392.  
  393. elseif b.id == "blackjack" then
  394. soundClick()
  395. launchGame(BLACKJACK_PROGRAM)
  396. drawMenu()
  397. return
  398.  
  399. elseif b.id == "plinko" then
  400. soundClick()
  401. launchGame(PLINKO_PROGRAM)
  402. drawMenu()
  403. return
  404.  
  405. elseif b.id == "cashout" then
  406. requestCashOut()
  407. drawMenu()
  408. return
  409.  
  410. elseif b.id == "cashout_cancel" then
  411. confirmCashOut = false
  412. message = "Cash out cancelled."
  413. soundClick()
  414. drawMenu()
  415. return
  416.  
  417. elseif b.id == "cashout_confirm" then
  418. confirmCashOut = false
  419. cashOutAll()
  420. drawMenu()
  421. return
  422. end
  423. end
  424. end
  425. end
  426.  
  427. --------------------------------------------------
  428. -- MAIN
  429. --------------------------------------------------
  430.  
  431. drawMenu()
  432.  
  433. while true do
  434. local e, side, x, y = os.pullEvent()
  435.  
  436. if e == "monitor_touch" then
  437. handleTouch(x, y)
  438. elseif e == "peripheral" or e == "peripheral_detach" then
  439. drawMenu()
  440. elseif e == "timer" then
  441. drawMenu()
  442. end
  443. end
Add Comment
Please, Sign In to add comment