Advertisement
Zantag

SlotMachine

Sep 2nd, 2013
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.25 KB | None | 0 0
  1. slotMonitors = {[1] = peripheral.wrap("monitor_1")}
  2. earingsMonitor = peripheral.wrap("monitor_2")
  3.  
  4.  
  5. resetNetwork_rs_side = "front"
  6. light_rs_side = "bottom"
  7. sound_rs_side = "left"
  8.  
  9. rsSide = "right"
  10. chestDirection = "west"
  11. pimDir = "east"
  12. chestIM = nil
  13. pIM = nil
  14.  
  15.  
  16.  
  17.  
  18. while chestIM == nil do
  19. chestIM = peripheral.wrap("ender_chest_0")
  20. sleep(0.2)
  21. end
  22. while pIM == nil do
  23. pIM = peripheral.wrap("pim_1")
  24. sleep(0.2)
  25. end
  26.  
  27. local names = {}
  28.  
  29. --local currencyID = {26247, 26252, 26248,26246}
  30. local currencyID = {265, 26252, 26248,26246}
  31. local currencyValue = {1, 9, 81, 729}
  32. backgroundColor = 1
  33.  
  34. wheels = 3
  35.  
  36. function resetNetwork()
  37. print("Reseting Network")
  38. while true do
  39. rs.setBundledOutput(resetNetwork_rs_side, 3)
  40. x= 0
  41. while x < 30 do
  42. local y = rs.getBundledInput(resetNetwork_rs_side)
  43.  
  44. if colors.test(y, 5) then
  45. print("Network Ready")
  46. return
  47. end
  48. sleep(0.1)
  49. x = x + 1
  50. end
  51. rs.setBundledOutput(resetNetwork_rs_side, 0)
  52. sleep(0.1)
  53. end
  54. end
  55.  
  56.  
  57.  
  58. mon = slotMonitors[1]
  59. term.redirect(mon)
  60. term.clear()
  61.  
  62.  
  63. imgHeight = 16
  64. if fs.exists("imgs.txt") == false then
  65. shell.run("pastebin get FD1z6HEF imgs.txt")
  66. --Assumes you have http enabled
  67. end
  68. imgs = paintutils.loadImage("imgs.txt")
  69. if fs.exists("names") == false then
  70. shell.run("pastebin get gf3DaU0c names")
  71. end
  72. if fs.exists("playSlotimg") == false then
  73. shell.run("pastebin get BZCiha3s playSlotimg")
  74. end
  75. playSlotimg = paintutils.loadImage("playSlotimg")
  76. if fs.exists("balloons") == false then
  77. shell.run("pastebin get F4Aq4QkA balloons")
  78. end
  79. balloons = paintutils.loadImage("balloons")
  80.  
  81. local function readLines(sPath)
  82. local file = fs.open(sPath, "r")
  83. if file then
  84. local tLines = {}
  85. local sLine = file.readLine()
  86. while sLine do
  87. table.insert(tLines, sLine)
  88. sLine = file.readLine()
  89. end
  90. file.close()
  91. return tLines
  92. end
  93. print("Failed to open")
  94. return nil
  95. end
  96.  
  97. function drawImg2(img, x,y, imgwidth, imgheight)
  98. curY = 0
  99. for yy = 1, imgheight do
  100. for xx = 1, imgwidth do
  101. local c = img[yy][xx]
  102. if (c == 0) then
  103. c = 15
  104. end
  105. paintutils.drawPixel(x+xx-1, y+curY, c)
  106. end
  107. curY = curY + 1
  108. end
  109. end
  110.  
  111. function drawImg3(index, x,y, imgWidth, imgHeight)
  112. index = index - 1
  113. curY = 0
  114.  
  115. for yy = index*imgHeight+1, (index+1)*imgHeight do
  116. for xx = 1, imgWidth do
  117. local c = balloons[yy][xx]
  118. if (c == 0) then
  119. c = 15
  120. end
  121. paintutils.drawPixel(x+xx-1, y+curY, c)
  122. end
  123. curY = curY + 1
  124. end
  125.  
  126. end
  127. function drawImg(index, x,y)
  128. index = index - 1
  129. curY = 0
  130. if index == -2 then
  131. for yy = 1, imgHeight do
  132. for xx = 1, imgHeight do
  133. c = 15
  134. paintutils.drawPixel(x+xx-1, y+curY, c)
  135. end
  136. curY = curY + 1
  137. end
  138. else
  139. for yy = index*imgHeight+1, (index+1)*imgHeight do
  140. for xx = 1, imgHeight do
  141. local c = imgs[yy][xx]
  142. if (c == 0) then
  143. c = 15
  144. end
  145. if c == nil then
  146. c = 15
  147. end
  148. paintutils.drawPixel(x+xx-1, y+curY, c)
  149. end
  150. curY = curY + 1
  151. end
  152. end
  153. end
  154.  
  155. function renderImage(name, x, y)
  156. local index = 0
  157. for i = 1, #names do
  158. if string.lower(name) == string.lower(names[i]) then
  159. index = i
  160. break
  161. end
  162. end
  163. if index ~= 0 then
  164. drawImg(index, x,y)
  165. return 1
  166. else
  167. return 0
  168. end
  169. end
  170.  
  171.  
  172. function topWinnerScreen()
  173. term.redirect(earingsMonitor)
  174. term.clear()
  175. for i = 1,16 do
  176. drawImg3(17-i, 1,1, 50, 12)
  177. sleep(0.2)
  178. end
  179. term.redirect(mon)
  180. end
  181.  
  182. utils = {
  183. clear = function()
  184. mon.setBackgroundColor(colors.black)
  185. mon.clear()
  186. end,
  187. writeDisplay = function(x, y, text, color, background)
  188. if color ~= nil then
  189. earingsMonitor.setTextColor(color)
  190. else
  191. earingsMonitor.setTextColor(colors.white)
  192. end
  193.  
  194. if background ~= nil then
  195. earingsMonitor.setBackgroundColor(background)
  196. else
  197. earingsMonitor.setBackgroundColor(colors.black)
  198. end
  199.  
  200. earingsMonitor.setCursorPos(x, y)
  201. earingsMonitor.write(text)
  202. end,
  203. write = function(x, y, text, color, background)
  204. if color ~= nil then
  205. mon.setTextColor(color)
  206. else
  207. mon.setTextColor(colors.white)
  208. end
  209.  
  210. if background ~= nil then
  211. mon.setBackgroundColor(background)
  212. else
  213. mon.setBackgroundColor(colors.black)
  214. end
  215.  
  216. mon.setCursorPos(x, y)
  217. mon.write(text)
  218. end,
  219. writeCentered = function(y, text, color, background)
  220. utils.write(utils.centerString(text), y, text, color, background)
  221. end,
  222. getWidth = function()
  223. local width, height = mon.getSize()
  224. return width
  225. end,
  226. getHeight = function()
  227. local width, height = mon.getSize()
  228. return height
  229. end,
  230. centerString = function(msg)
  231. local length = string.len(msg)
  232. local width = utils.getWidth()
  233. return (width - length) / 2
  234. end
  235. }
  236.  
  237.  
  238. names = readLines("names")
  239. fee = readLines("fee")
  240. function randomNumber()
  241. local n = math.random(1, 128)
  242. if n <= 5 then
  243. return 1
  244. elseif n <= 10 then
  245. return 6
  246. elseif n <= 26 then
  247. return 5
  248. elseif n <= 39 then
  249. return 4
  250. elseif n <= 50 then
  251. return 3
  252. elseif n <= 55 then
  253. return 2
  254. else
  255. return -1
  256. end
  257. end
  258.  
  259. function checkForPlayer()
  260. while true do
  261. if pIM == nil then
  262. print("FAIL")
  263. end
  264. if pIM.getInvName() ~= nil then
  265. return true
  266. end
  267. end
  268. end
  269. function copy(f)
  270. local r = {}
  271. for i = 1, 4 do
  272. r[i] = tonumber(f[i])
  273. end
  274. return r
  275. end
  276.  
  277. function deductFunds()
  278. local ret = {0,0,0,0}
  279. size = pIM.getInventorySize()
  280.  
  281. for i = 1, size do
  282. local tableInfo = pIM.getStackInSlot(i)
  283. if tableInfo ~= nil then
  284. for j = 1,4 do
  285. if tableInfo["id"] == currencyID[j] then
  286. ret[j] = ret[j] + tonumber(tableInfo["qty"])
  287. end
  288. end
  289. end
  290. end
  291.  
  292. for i = 1, 4 do
  293. if tonumber(fee[i]) > ret[i] then
  294. return false
  295. end
  296. end
  297. temp = copy(fee)
  298. for i = 1, size do
  299. local tableInfo = pIM.getStackInSlot(i)
  300. if tableInfo ~= nil then
  301. qty = tonumber(tableInfo["qty"])
  302. for j = 1,4 do
  303. if tableInfo["id"] == currencyID[j] then --We have a credit
  304. m = math.min(qty, temp[j])
  305. if pIM.pushItem(pimDir, i, m) then
  306. temp[j] = temp[j] - m
  307. end
  308. end
  309. end
  310. end
  311. end
  312. for i = 1, 4 do
  313. if temp[i] > 0 then
  314. print("Failing b/c t: " .. temp[i])
  315. return false
  316. end
  317. end
  318. return true
  319. end
  320. function spinWheel()
  321. local w = {}
  322. for i = 1,10 do
  323. for j = 1, wheels do
  324. w[j] = randomNumber()
  325. drawImg(w[j], 17*(j-1),0)
  326. end
  327. sleep(0.2)
  328. end
  329. return w
  330. end
  331.  
  332. local totOut = 0
  333. local totalGames = 0
  334. function canPayoutPrize()
  335. chest_size = chestIM.getInventorySize()
  336. local ret = {0,0,0,0}
  337. for i = 1, chest_size do
  338. local tableInfo = chestIM.getStackInSlot(i)
  339. if tableInfo ~= nil then
  340. for j = 1, 4 do
  341. if tableInfo["id"] == currencyID[j] then --We have a credit
  342. ret[j] = ret[j] + tableInfo["qty"]
  343. end
  344. end
  345. end
  346. end
  347.  
  348. for i = 1, 4 do
  349. if tonumber(fee[i]) > 0 then
  350. if ret[i] < 100 * fee[i] then
  351. print("Fee: " .. fee[i] .. " : " .. ret[i])
  352. return false
  353. end
  354. end
  355. end
  356. return true
  357. end
  358.  
  359. function givePlayerPrize(coinPrize)
  360. chest_size = chestIM.getInventorySize()
  361. local ret = {0,0,0,0}
  362. for i = 1, chest_size do
  363. local tableInfo = chestIM.getStackInSlot(i)
  364. if tableInfo ~= nil then
  365. for j = 1, 4 do
  366. if tableInfo["id"] == currencyID[j] then --We have a credit
  367. ret[j] = ret[j] + tableInfo["qty"]
  368. end
  369. end
  370. for j = 1, 4 do
  371. if coinPrize[j] >0 and ret[j] > 0 then
  372. m = math.min(coinPrize[j], ret[j])
  373. if chestIM.pushItem(chestDirection, i, m) then
  374. ret[j] = ret[j] - m
  375. coinPrize[j] = coinPrize[j] - m
  376. end
  377. end
  378. end
  379. end
  380. end
  381. return true
  382. end
  383. function printPayout(x)
  384. totOut = totOut + x
  385. topWinnerScreen()
  386. --print("You Win:" .. x .. " on game " .. totalGames .. " : Ave per game" .. (totOut / totalGames))
  387. redstone.setBundledOutput(light_rs_side, 1)
  388. sleep(0.1)
  389. redstone.setBundledOutput(light_rs_side, 0)
  390. local coinPrize = {tonumber(fee[1])*x, tonumber(fee[2])*x,tonumber(fee[3])*x,tonumber(fee[4])*x}
  391. tCoins = coinPrize[1] + coinPrize[2]*8+coinPrize[3]*64+coinPrize[4]*512
  392. utils.writeCentered(13, "You win! " .. tCoins .. " coins!")
  393. givePlayerPrize(coinPrize)
  394. end
  395.  
  396. function payoutPrize(w)
  397. local pics = {0,0,0,0,0,0, 0}
  398. for i = 1, wheels do
  399. if w[i] ~= -1 then
  400. pics[w[i]] = pics[w[i]]+1
  401. end
  402. end
  403. totalGames = totalGames + 1
  404. if pics[1] == 1 then
  405. printPayout(3)
  406. elseif pics[1] == 2 then
  407. printPayout(6)
  408. elseif pics[1] == 3 then
  409. printPayout(12)
  410. elseif pics[2] == 3 then
  411. printPayout(1)
  412. elseif pics[6] == 3 then
  413. printPayout(12)
  414. elseif pics[5] == 3 then
  415. printPayout(25)
  416. elseif pics[4] == 3 then
  417. printPayout(50)
  418. elseif pics[3] == 3 then
  419. printPayout(100)
  420. elseif pics[3] + pics[4] + pics[5] == 3 then
  421. printPayout(12)
  422. end
  423.  
  424. end
  425.  
  426.  
  427. function leverPulled()
  428.  
  429. if canPayoutPrize() then
  430. win = false
  431. if deductFunds() then
  432. -- sleep(1)
  433. redstone.setOutput(sound_rs_side, true)
  434. sleep(0.1)
  435. redstone.setOutput(sound_rs_side, false)
  436. w = spinWheel()
  437. payoutPrize(w)
  438. sleep(2.5)
  439. else
  440. print("Not enough funds")
  441. sleep(3)
  442. end
  443. else
  444. print("Please contact ZantagX for maintenance")
  445. sleep(10)
  446. end
  447. print("Exiting")
  448. end
  449.  
  450. function getLever()
  451. while true do
  452. r = os.pullEvent()
  453. if r == "redstone" then
  454. if redstone.getInput(rsSide) then
  455. break
  456. end
  457. end
  458. end
  459. end
  460. function waitForUserInput()
  461. while true do
  462. slotMonitors[1].clear()
  463. utils.clear()
  464. earingsMonitor.clear()
  465. earningsDisplay()
  466. drawImg2(playSlotimg, 1,1, 50,19)
  467. getLever()
  468. slotMonitors[1].clear()
  469. leverPulled()
  470. end
  471. end
  472. function align(number)
  473. str = "" .. number
  474. addSpaces = 5-#str
  475. for i = 1, addSpaces do
  476. str = " " .. str
  477. end
  478. return str
  479. end
  480. function earningsDisplay()
  481. local tMult = tonumber(fee[1])+tonumber(fee[2])*8+tonumber(fee[3])*64+tonumber(fee[4])*512
  482. len = 16
  483. utils.writeDisplay(1,1, "Reel Displays Reward")
  484. utils.writeDisplay(1,2, "3 " .. names[1] .. string.rep(' ', len- #names[1]+1) .. align(tMult*1) .. " coins")
  485. utils.writeDisplay(1,3, "1 " .. names[2] .. string.rep(' ', len- #names[2]+1) .. align(tMult*3) .. " coins")
  486. utils.writeDisplay(1,4, "2 " .. names[2] .. string.rep(' ', len- #names[2]+1) .. align(tMult*6) .. " coins")
  487. utils.writeDisplay(1,5, "3 " .. names[2] .. string.rep(' ', len- #names[2]+2) .. align(tMult*12) .. " coins")
  488. utils.writeDisplay(1,6, "3 " .. names[3] .. string.rep(' ', len- #names[3]+2) .. align(tMult*12) .. " coins")
  489. utils.writeDisplay(1,7, "3 Any 3 Gem ".. align(tMult*12) .. " coins")
  490. utils.writeDisplay(1,8, "3 " .. names[4] .. string.rep(' ', len- #names[4]+2) .. align(tMult*25) .. " coins")
  491. utils.writeDisplay(1,9, "3 " .. names[5] .. string.rep(' ', len- #names[5]+2) .. align(tMult*50) .. " coins")
  492. utils.writeDisplay(1,10,"3 " .. names[6] .. string.rep(' ', len- #names[6]+3) ..align(tMult*100) .. " coins")
  493. end
  494.  
  495. function main()
  496. waitForUserInput()
  497. end
  498.  
  499.  
  500. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement