Advertisement
NeonJ

Slot Machine DX

Nov 14th, 2012
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 35.25 KB | None | 0 0
  1. -- ***** Cirom's Custom Slot Machine: DX *****
  2.  
  3. -- Designed for a private server, but usable by everyone!
  4. -- A few notes, I've tried to comment the code to make it easier to read and use.
  5. -- I especially commented the parts the casino owner is supposed to edit.
  6.  
  7. -- Requirements to run this slot machine: ComputerCraft (duh), RedPower2.
  8. -- The mods are included in Tekkit. This code was originally made for a Tekkit server, but it should work as long as you have the two mods.
  9.  
  10. -- ***** VARIABLES *****
  11.  
  12. -- I tried to add lots of variables, so casino owners have lots of control over their machine.
  13. -- You can edit the prizes, prize chances and more!
  14.  
  15.   -- DO NOT ALTER THESE - They are variables edited by the machine itself, editing these can result in the machine giving players free money.
  16. credits = 0       -- How many credits the user currently has to use.
  17. creditswin = 0    -- How many credits the user has won..
  18. creditsextra = 0  -- How many credits the user has won visible on screen. Used for visual effect.
  19.  
  20.   -- Alter the prizes.
  21. prizeA = 1        -- 3x X
  22. prizeBB = 2       -- 2x Cherries
  23. prizeBBB = 8      -- 3x Cherries
  24. prizeC = 32       -- 3x BAR
  25. prizeD = 64       -- 3x Lucky 7
  26. prizeE = 192      -- 3x Jackpot
  27. allowcherrywin = 1     -- Set to 0 to disallow winning when 2 cherries appear, forcing the player to get 3 cherries.
  28.  
  29.   -- Alter the chances of the rolls.
  30. chanceA = 30      -- 30 / 100  - Do note that these are the chances for ONE reel.
  31. chanceB = 25      -- 25 / 100  - To find the chances for all 3 reels...
  32. chanceC = 20      -- 20 / 100  -                              (20 / 100) ^ 3
  33. chanceD = 15      -- 15 / 100  - Replace the power with 2 for the "Two Cherry" win.
  34. chanceE = 10      -- 10 / 100  - The rolls are designed to add up to 100, but I coded them to work properly with ANY chances.
  35.  
  36.   -- Alter the monitor side and redstone sides.
  37. detectside = "back"     -- Bundled Cable to detect items and button presses. YELLOW: Item Inserted, ORANGE: Payout Button Pressed, RED: Play Button Pressed.
  38. monside = "front"       -- The side the monitor is on.
  39. dispenserside = "top"   -- The side the dispenser is on.
  40. stopperside = "right"   -- Redstone wire on this side will prevent the Filter from accepting items at the wrong time. Be sure to ATTACH A NOT GATE, as the output is inverted!
  41.  
  42.   -- Alter the looks of the reels. You can alter how each "prize" looks like.
  43.   -- Each prize string must be 3 characters wide, including spaces. Spaces can be used to fill empty character space at the end.
  44. drawXA = "X X"
  45. drawXB = " X "  -- The design of the "X" icon.
  46. drawXC = "X X"
  47.  
  48. drawOA = "  ."
  49. drawOB = " /|"  -- The design of the "cherry" icon.
  50. drawOC = "O O"
  51.  
  52. drawBA = "---"
  53. drawBB = "BAR"  -- The design of the "BAR" icon.
  54. drawBC = "---"
  55.  
  56. draw7A = "777"
  57. draw7B = "  7"  -- The design of the "Lucky 7" icon.
  58. draw7C = "  7"
  59.  
  60. drawJA = "* *"
  61. drawJB = "[J]"  -- The design of the "Jackpot" icon.
  62. drawJC = "* *"
  63.  
  64. -- ***** CODING *****
  65.  
  66. -- For the normal user, you probably don't need to alter this.
  67. -- For the coder, I have commented the code so you can easier understand what's going on.
  68. -- Basic LUA knowledge is still needed to truly understand it, though.
  69. -- But hey, the best way I learn is by reading source code. So it'll help, right?
  70.  
  71. mon = peripheral.wrap(monside)          -- This connects the monitor to the screen.
  72. redstone.setOutput(stopperside,true)    -- Let the Filter accept items!
  73. shell.run("clear",1)                    -- Clear the console screen.
  74. print("Slot Machine DX loaded. Press CTRL + T to terminate.")
  75.  
  76. mon.setCursorPos(1,1)                                                   -- | ------------------------- |
  77. mon.write(" ------------------------- ")                                -- | |  X X  |    7  |  ---  | |
  78. mon.setCursorPos(1,2)                                                   -- | |-------|-------|-------| |
  79. mon.write(" |  "..drawXC.."  |  "..draw7C.."  |  "..drawBC.."  | ")     -- | |    .  |    .  |    .  | |
  80. mon.setCursorPos(1,3)                                                   -- |>|   /|  |   /|  |   /|  |<|
  81. mon.write(" |-------|-------|-------| ")                                -- | |  O O  |  O O  |  O O  | |
  82. mon.setCursorPos(1,4)                                                   -- | |-------|-------|-------| |
  83. mon.write(" |  "..drawOA.."  |  "..drawOA.."  |  "..drawOA.."  | ")     -- | |  JJJ  |  X X  |    .  | |
  84. mon.setCursorPos(1,5)                                                   -- | ------------------------- |
  85. mon.write(">|  "..drawOB.."  |  "..drawOB.."  |  "..drawOB.."  |<")     -- | CREDITS    WIN    INSERT  |
  86. mon.setCursorPos(1,6)                                                   -- |   000      000     CREDIT |
  87. mon.write(" |  "..drawOC.."  |  "..drawOC.."  |  "..drawOC.."  | ")     --  ---------------------------
  88. mon.setCursorPos(1,7)                                                   -- ^ It basically looks like this.
  89. mon.write(" |-------|-------|-------| ")
  90. mon.setCursorPos(1,8)
  91. mon.write(" |  "..drawJA.."  |  "..drawXA.."  |  "..drawOA.."  | ")
  92. mon.setCursorPos(1,9)
  93. mon.write(" ------------------------- ")
  94. mon.setCursorPos(1,10)
  95. mon.write(" CREDITS    WIN    INSERT  ")
  96. mon.setCursorPos(1,11)
  97. mon.write("   000      000     CREDIT ")
  98. print("Displayed starter screen.")          -- Console should see all.
  99.  
  100. function winner()           -- This function is called when a user has won.
  101.     mon.setCursorPos(20,10)
  102.     mon.write("YOU    ")
  103.     mon.setCursorPos(20,11)
  104.     mon.write("    WIN")        -- Well, duh. We're not calling this function if they lose.
  105.     sleep(2)
  106.  
  107.     if creditswin == prizeE then    -- If they get the jackpot, we should tell them!
  108.         mon.setCursorPos(20,10)     -- Doesn't it feel awesome when you're told you won the best thing?
  109.         mon.write("JACKPOT")        -- Which is why I made the machine do this.
  110.         mon.setCursorPos(20,11)
  111.         mon.write("  WIN  ")        -- Center the word WIN, for big justice.
  112.     end
  113.  
  114.     while creditsextra < creditswin do              -- First, we increment the WIN counter.
  115.         creditsextra = creditsextra + 1             -- I mainly did this because it looks really cool.
  116.         cred = string.format("%03d", creditsextra)  -- It's also useful for seeing exactly how much you just won.
  117.         mon.setCursorPos(13,11)                     -- The "creditswin" variable is dealt with later, in the "rolling reels" code.
  118.         mon.write(cred.."  ")   -- This bit just writes stuff on the monitor, making sure it's 3 digits long.
  119.         sleep(0.1)
  120.     end
  121.  
  122.     sleep(1)                                        -- Suspense wait. Yup.
  123.  
  124.     while creditswin > 0 do                         -- Then, we decrease the WIN counter while increasing the CREDITS.
  125.         credits = credits + 1
  126.         creditswin = creditswin - 1
  127.         creditsextra = creditsextra - 1
  128.  
  129.         str = string.format("%03d", credits)        -- Of course, we'll show the user that he's getting more credits..
  130.         mon.setCursorPos(1,11)
  131.         mon.write("   "..str.."  ")
  132.  
  133.         cred = string.format("%03d", creditsextra)  -- .. And show him that the credits are coming from his winnings!
  134.         mon.setCursorPos(13,11)
  135.         mon.write(cred.."  ")
  136.  
  137.         sleep(0.1)                                  -- Not too fast, not too slow.
  138.     end
  139.  
  140.     sleep(2)
  141.     if credits > 256 then payout() end              -- Limit 256 credits, so that players cannot drain the entire dispenser in one go.
  142.     mon.setCursorPos(20,10)
  143.     mon.write("PRESS  ")
  144.     mon.setCursorPos(20,11)                         -- No need to tell him to "INSERT COIN" if there's already credits inside.
  145.     mon.write(" BUTTON")
  146.     redstone.setOutput(stopperside, true)           -- Allow items to come through the Filter!
  147. end
  148.  
  149. function roll()
  150.     credits = credits - 1                           -- Take away 1 credit, because it costs 1 credit to play the slots.
  151.     rollsdone = 0                                   -- ROLLSDONE gives it a nice "scrolling" effect.
  152.     rollout = 0                                     -- This determines which part of the scrolling effect it's on.
  153.    
  154.     str = string.format("%03d", credits)
  155.     mon.setCursorPos(1,11)
  156.     mon.write("   "..str.."  ")                      -- Show the user that the computer took away a credit.
  157.    
  158.     result4 = math.random(1,chanceA + chanceB + chanceC + chanceD + chanceE) -- Start off with some numbers so that some reels aren't just blank.
  159.     result4 = result4 - ".0"    -- <-- I think this bit makes sure there are no decimal places.
  160.     result5 = math.random(1,chanceA + chanceB + chanceC + chanceD + chanceE) -- Add up all the chances! ALL THE THINGS
  161.     result5 = result5 - ".0"
  162.     result6 = math.random(1,chanceA + chanceB + chanceC + chanceD + chanceE) -- They're added up because fractions.
  163.     result6 = result6 - ".0"
  164.     result7 = math.random(1,chanceA + chanceB + chanceC + chanceD + chanceE) -- _____A_____
  165.     result7 = result7 - ".0"                                                 --  A+B+C+D+E   and stuff
  166.     result8 = math.random(1,chanceA + chanceB + chanceC + chanceD + chanceE)
  167.     result8 = result8 - ".0"
  168.     result9 = math.random(1,chanceA + chanceB + chanceC + chanceD + chanceE)
  169.     result9 = result9 - ".0"                                                 -- These are the very first numbers, so yay.
  170.  
  171.     R1A = "   "
  172.     R2A = "   "
  173.     R3A = "   "
  174.     R4A = "   "
  175.     R4B = "   "
  176.     R4C = "   "
  177.     R5A = "   "
  178.     R5B = "   "
  179.     R5C = "   "
  180.     R6A = "   "
  181.     R6B = "   "         -- These are used for the machine to display icons.
  182.     R6C = "   "         -- Editing them will do nothing, they're changed immediately later anyway.
  183.     R7A = "   "
  184.     R7B = "   "
  185.     R7C = "   "
  186.     R8A = "   "
  187.     R8B = "   "
  188.     R8C = "   "
  189.     R9A = "   "
  190.     R9B = "   "
  191.     R9C = "   "
  192.    
  193.     if result4 <= chanceA then result4 = "X"                                -- Turn the result "numbers" into actual results.
  194.         R4A = drawXA                                                        -- This saves unneeded code later, saving CPU power.
  195.         R4B = drawXB
  196.         R4C = drawXC
  197.     elseif result4 <= (chanceA + chanceB) then result4 = "O"                -- Here, I'm also setting the "R4X" variables.
  198.         R4A = drawOA                                                        -- These variables are used to display the icons on the screen.
  199.         R4B = drawOB                                                        -- Notice how they're using the "drawXY" variables, which is in the VARIABLES section.
  200.         R4C = drawOC                                                        -- You edit those variables, and they'll be made use of here!
  201.     elseif result4 <= (chanceA + chanceB + chanceC) then result4 = "B"
  202.         R4A = drawBA
  203.         R4B = drawBB
  204.         R4C = drawBC
  205.     elseif result4 <= (chanceA + chanceB + chanceC + chanceD) then result4 = "7"
  206.         R4A = draw7A
  207.         R4B = draw7B
  208.         R4C = draw7C
  209.     else result4 = "J"
  210.         R4A = drawJA
  211.         R4B = drawJB
  212.         R4C = drawJC
  213.     end
  214.     if result5 <= chanceA then result5 = "X"                                 -- The chance conditions are added up, otherwise
  215.         R5A = drawXA                                                         -- they would overlap, and that's no good. D:
  216.         R5B = drawXB
  217.         R5C = drawXC
  218.     elseif result5 <= (chanceA + chanceB) then result5 = "O"
  219.         R5A = drawOA
  220.         R5B = drawOB
  221.         R5C = drawOC
  222.     elseif result5 <= (chanceA + chanceB + chanceC) then result5 = "B"
  223.         R5A = drawBA
  224.         R5B = drawBB
  225.         R5C = drawBC
  226.     elseif result5 <= (chanceA + chanceB + chanceC + chanceD) then result5 = "7"
  227.         R5A = draw7A
  228.         R5B = draw7B
  229.         R5C = draw7C
  230.     else result5 = "J"
  231.         R5A = drawJA
  232.         R5B = drawJB
  233.         R5C = drawJC
  234.     end
  235.     if result6 <= chanceA then result6 = "X"
  236.         R6A = drawXA
  237.         R6B = drawXB
  238.         R6C = drawXC
  239.     elseif result6 <= (chanceA + chanceB) then result6 = "O"
  240.         R6A = drawOA
  241.         R6B = drawOB
  242.         R6C = drawOC
  243.     elseif result6 <= (chanceA + chanceB + chanceC) then result6 = "B"
  244.         R6A = drawBA
  245.         R6B = drawBB
  246.         R6C = drawBC
  247.     elseif result6 <= (chanceA + chanceB + chanceC + chanceD) then result6 = "7"    -- This bit's a bit boring actually.
  248.         R6A = draw7A
  249.         R6B = draw7B
  250.         R6C = draw7C
  251.     else result6 = "J"
  252.         R6A = drawJA
  253.         R6B = drawJB
  254.         R6C = drawJC
  255.     end
  256.     if result7 <= chanceA then result7 = "X"
  257.         R7A = drawXA
  258.         R7B = drawXB
  259.         R7C = drawXC
  260.     elseif result7 <= (chanceA + chanceB) then result7 = "O"
  261.         R7A = drawOA
  262.         R7B = drawOB
  263.         R7C = drawOC
  264.     elseif result7 <= (chanceA + chanceB + chanceC) then result7 = "B"
  265.         R7A = drawBA
  266.         R7B = drawBB
  267.         R7C = drawBC
  268.     elseif result7 <= (chanceA + chanceB + chanceC + chanceD) then result7 = "7"
  269.         R7A = draw7A
  270.         R7B = draw7B
  271.         R7C = draw7C
  272.     else result7 = "J"
  273.         R7A = drawJA
  274.         R7B = drawJB
  275.         R7C = drawJC
  276.     end
  277.     if result8 <= chanceA then result8 = "X"
  278.         R8A = drawXA
  279.         R8B = drawXB
  280.         R8C = drawXC
  281.     elseif result8 <= (chanceA + chanceB) then result8 = "O"
  282.         R8A = drawOA
  283.         R8B = drawOB
  284.         R8C = drawOC
  285.     elseif result8 <= (chanceA + chanceB + chanceC) then result8 = "B"              -- Tum-de-dum.
  286.         R8A = drawBA
  287.         R8B = drawBB
  288.         R8C = drawBC
  289.     elseif result8 <= (chanceA + chanceB + chanceC + chanceD) then result8 = "7"
  290.         R8A = draw7A
  291.         R8B = draw7B
  292.         R8C = draw7C
  293.     else result8 = "J"
  294.         R8A = drawJA
  295.         R8B = drawJB
  296.         R8C = drawJC
  297.     end
  298.     if result9 <= chanceA then result9 = "X"
  299.         R9A = drawXA
  300.         R9B = drawXB
  301.         R9C = drawXC
  302.     elseif result9 <= (chanceA + chanceB) then result9 = "O"
  303.         R9A = drawOA
  304.         R9B = drawOB
  305.         R9C = drawOC
  306.     elseif result9 <= (chanceA + chanceB + chanceC) then result9 = "B"
  307.         R9A = drawBA
  308.         R9B = drawBB
  309.         R9C = drawBC
  310.     elseif result9 <= (chanceA + chanceB + chanceC + chanceD) then result9 = "7"
  311.         R9A = draw7A
  312.         R9B = draw7B
  313.         R9C = draw7C
  314.     else result9 = "J"                                                              -- Finally, an end.
  315.         R9A = drawJA
  316.         R9B = drawJB
  317.         R9C = drawJC
  318.     end
  319.    
  320.    
  321.     while rollsdone < 80 do                 -- The slots will "tick" 80 times. If you edit this, make it a multiple of 4.
  322.         rollsdone = rollsdone + 1           -- If not, the reel will think it's finished when it's only half spun.
  323.         rollout = rollout + 1               -- And that's just GLITCHY.
  324.         mon.setCursorPos(20,10)
  325.         mon.write(" REELS ")
  326.         mon.setCursorPos(20,11)             -- We'll also change this bit from saying "PRESS BUTTON". Pointless saying to press a button when the reels are rolling.
  327.         mon.write("ROLLING")
  328.         if rollsdone <= 40 then             -- Stop Reel 1 when rollsdone > 40. If you edit the 40, make it a multiple of 4, and less than rollsdone.
  329.             if rollout == 1 then            -- The 40 is when the first reel stops spinning.
  330.                 mon.setCursorPos(3,2)
  331.                 mon.write("  "..R7B.."  ")
  332.                 mon.setCursorPos(3,3)
  333.                 mon.write("  "..R7C.."  ")
  334.                 mon.setCursorPos(3,4)
  335.                 mon.write("-------")
  336.                 mon.setCursorPos(3,5)
  337.                 mon.write("  "..R4A.."  ")      -- This part basically just draws onto the monitor using the "R4X" variables we made and edited earlier.
  338.                 mon.setCursorPos(3,6)
  339.                 mon.write("  "..R4B.."  ")
  340.                 mon.setCursorPos(3,7)
  341.                 mon.write("  "..R4C.."  ")
  342.                 mon.setCursorPos(3,8)
  343.                 mon.write("-------")
  344.             elseif rollout == 2 then
  345.                 mon.setCursorPos(3,2)
  346.                 mon.write("  "..R7A.."  ")
  347.                 mon.setCursorPos(3,3)
  348.                 mon.write("  "..R7B.."  ")
  349.                 mon.setCursorPos(3,4)
  350.                 mon.write("  "..R7C.."  ")
  351.                 mon.setCursorPos(3,5)
  352.                 mon.write("-------")
  353.                 mon.setCursorPos(3,6)
  354.                 mon.write("  "..R4A.."  ")
  355.                 mon.setCursorPos(3,7)
  356.                 mon.write("  "..R4B.."  ")
  357.                 mon.setCursorPos(3,8)
  358.                 mon.write("  "..R4C.."  ")
  359.             elseif rollout == 3 then
  360.                 mon.setCursorPos(3,2)
  361.                 mon.write("-------")
  362.                 mon.setCursorPos(3,3)
  363.                 mon.write("  "..R7A.."  ")
  364.                 mon.setCursorPos(3,4)
  365.                 mon.write("  "..R7B.."  ")
  366.                 mon.setCursorPos(3,5)
  367.                 mon.write("  "..R7C.."  ")
  368.                 mon.setCursorPos(3,6)
  369.                 mon.write("-------")
  370.                 mon.setCursorPos(3,7)
  371.                 mon.write("  "..R4A.."  ")
  372.                 mon.setCursorPos(3,8)
  373.                 mon.write("  "..R4B.."  ")
  374.             elseif rollout == 4 then
  375.                 result1 = result4               -- When a multiple of 4 is hit, we pretty much "shift" everything up one.
  376.                 R1A = R4A                       -- It's a bit of a cheap effect, but it works so well and is invisible to the normal user.
  377.                 result4 = result7               -- Especially seeing as how I managed it is awesome! ;3
  378.                 R4A = R7A
  379.                 R4B = R7B
  380.                 R4C = R7C
  381.                 result7 = math.random(1,chanceA + chanceB + chanceC + chanceD + chanceE)
  382.                 result7 = result7 - ".0"
  383.                 if result7 <= chanceA then result7 = "X"                      -- We're basically just translating Result 7's new number into a result again.
  384.                     R7A = drawXA                                              -- We don't have to do this for the others, cause they're using Result 7 as a base.
  385.                     R7B = drawXB
  386.                     R7C = drawXC
  387.                 elseif result7 <= (chanceA + chanceB) then result7 = "O"
  388.                     R7A = drawOA
  389.                     R7B = drawOB
  390.                     R7C = drawOC
  391.                 elseif result7 <= (chanceA + chanceB + chanceC) then result7 = "B"
  392.                     R7A = drawBA
  393.                     R7B = drawBB
  394.                     R7C = drawBC
  395.                 elseif result7 <= (chanceA + chanceB + chanceC + chanceD) then result7 = "7"
  396.                     R7A = draw7A
  397.                     R7B = draw7B
  398.                     R7C = draw7C
  399.                 else result7 = "J"                                              -- If you're wondering, the reels turn out like this, result number wise...
  400.                     R7A = drawJA                                                -- 7 8 9
  401.                     R7B = drawJB                                                -- 4 5 6   <- Kind of like the Numberpad on a keyboard!
  402.                     R7C = drawJC                                                -- 1 2 3
  403.                 end
  404.                 mon.setCursorPos(3,2)
  405.                 mon.write("  "..R7C.."  ")
  406.                 mon.setCursorPos(3,3)
  407.                 mon.write("-------")
  408.                 mon.setCursorPos(3,4)
  409.                 mon.write("  "..R4A.."  ")
  410.                 mon.setCursorPos(3,5)
  411.                 mon.write("  "..R4B.."  ")
  412.                 mon.setCursorPos(3,6)
  413.                 mon.write("  "..R4C.."  ")
  414.                 mon.setCursorPos(3,7)
  415.                 mon.write("-------")
  416.                 mon.setCursorPos(3,8)
  417.                 mon.write("  "..R1A.."  ")
  418.             else
  419.                 print("ERROR IN REEL 1 ROLLOUT")                -- If an error ever does happen, let the console know!
  420.             end
  421.         end
  422.        
  423.         if rollsdone <= 60 then             -- Stop Reel 2 when rollsdone > 60. Multiple of 4 rule again, if you want to edit it.
  424.             if rollout == 1 then            -- This one should be higher than the previous "40" you were allowed to edit.
  425.                 mon.setCursorPos(11,2)
  426.                 mon.write("  "..R8B.."  ")
  427.                 mon.setCursorPos(11,3)
  428.                 mon.write("  "..R8C.."  ")
  429.                 mon.setCursorPos(11,4)
  430.                 mon.write("-------")
  431.                 mon.setCursorPos(11,5)
  432.                 mon.write("  "..R5A.."  ")
  433.                 mon.setCursorPos(11,6)
  434.                 mon.write("  "..R5B.."  ")          -- Yay for clone code. Ugh.
  435.                 mon.setCursorPos(11,7)
  436.                 mon.write("  "..R5C.."  ")
  437.                 mon.setCursorPos(11,8)
  438.                 mon.write("-------")
  439.             elseif rollout == 2 then
  440.                 mon.setCursorPos(11,2)
  441.                 mon.write("  "..R8A.."  ")
  442.                 mon.setCursorPos(11,3)
  443.                 mon.write("  "..R8B.."  ")
  444.                 mon.setCursorPos(11,4)
  445.                 mon.write("  "..R8C.."  ")
  446.                 mon.setCursorPos(11,5)
  447.                 mon.write("-------")
  448.                 mon.setCursorPos(11,6)
  449.                 mon.write("  "..R5A.."  ")
  450.                 mon.setCursorPos(11,7)
  451.                 mon.write("  "..R5B.."  ")
  452.                 mon.setCursorPos(11,8)
  453.                 mon.write("  "..R5C.."  ")
  454.             elseif rollout == 3 then
  455.                 mon.setCursorPos(11,2)
  456.                 mon.write("-------")
  457.                 mon.setCursorPos(11,3)
  458.                 mon.write("  "..R8A.."  ")
  459.                 mon.setCursorPos(11,4)
  460.                 mon.write("  "..R8B.."  ")
  461.                 mon.setCursorPos(11,5)
  462.                 mon.write("  "..R8C.."  ")
  463.                 mon.setCursorPos(11,6)
  464.                 mon.write("-------")
  465.                 mon.setCursorPos(11,7)
  466.                 mon.write("  "..R5A.."  ")
  467.                 mon.setCursorPos(11,8)
  468.                 mon.write("  "..R5B.."  ")
  469.             elseif rollout == 4 then
  470.                 result2 = result5
  471.                 R2A = R5A
  472.                 result5 = result8
  473.                 R5A = R8A
  474.                 R5B = R8B
  475.                 R5C = R8C
  476.                 result8 = math.random(1,chanceA + chanceB + chanceC + chanceD + chanceE)
  477.                 result8 = result8 - ".0"
  478.  
  479.                 if result8 <= chanceA then result8 = "X"                      -- Turn Result8 into a proper result.
  480.                     R8A = drawXA
  481.                     R8B = drawXB
  482.                     R8C = drawXC
  483.                 elseif result8 <= (chanceA + chanceB) then result8 = "O"
  484.                     R8A = drawOA
  485.                     R8B = drawOB
  486.                     R8C = drawOC
  487.                 elseif result8 <= (chanceA + chanceB + chanceC) then result8 = "B"
  488.                     R8A = drawBA
  489.                     R8B = drawBB
  490.                     R8C = drawBC
  491.                 elseif result8 <= (chanceA + chanceB + chanceC + chanceD) then result8 = "7"
  492.                     R8A = draw7A
  493.                     R8B = draw7B
  494.                     R8C = draw7C
  495.                 else result8 = "J"
  496.                     R8A = drawJA
  497.                     R8B = drawJB
  498.                     R8C = drawJC
  499.                 end
  500.                 mon.setCursorPos(11,2)
  501.                 mon.write("  "..R8C.."  ")
  502.                 mon.setCursorPos(11,3)
  503.                 mon.write("-------")
  504.                 mon.setCursorPos(11,4)
  505.                 mon.write("  "..R5A.."  ")
  506.                 mon.setCursorPos(11,5)
  507.                 mon.write("  "..R5B.."  ")
  508.                 mon.setCursorPos(11,6)
  509.                 mon.write("  "..R5C.."  ")              -- Is it me or is this just exactly the same but with 2, 5 and 8?
  510.                 mon.setCursorPos(11,7)                  -- OH WAIT, THAT'S CAUSE IT IS.
  511.                 mon.write("-------")
  512.                 mon.setCursorPos(11,8)
  513.                 mon.write("  "..R2A.."  ")
  514.             else
  515.                 print("ERROR IN REEL 2 ROLLOUT")
  516.             end
  517.         end
  518.         if rollout == 1 then            -- No need to keep counting after the third reel, so no need for an IF statement.
  519.             mon.setCursorPos(19,2)      -- The third reel always finishes last, at "rollsdone" 80.
  520.             mon.write("  "..R9B.."  ")
  521.             mon.setCursorPos(19,3)
  522.             mon.write("  "..R9C.."  ")
  523.             mon.setCursorPos(19,4)
  524.             mon.write("-------")
  525.             mon.setCursorPos(19,5)
  526.             mon.write("  "..R6A.."  ")
  527.             mon.setCursorPos(19,6)
  528.             mon.write("  "..R6B.."  ")
  529.             mon.setCursorPos(19,7)
  530.             mon.write("  "..R6C.."  ")
  531.             mon.setCursorPos(19,8)
  532.             mon.write("-------")
  533.         elseif rollout == 2 then
  534.             mon.setCursorPos(19,2)
  535.             mon.write("  "..R9A.."  ")
  536.             mon.setCursorPos(19,3)
  537.             mon.write("  "..R9B.."  ")
  538.             mon.setCursorPos(19,4)
  539.             mon.write("  "..R9C.."  ")
  540.             mon.setCursorPos(19,5)
  541.             mon.write("-------")
  542.             mon.setCursorPos(19,6)
  543.             mon.write("  "..R6A.."  ")
  544.             mon.setCursorPos(19,7)
  545.             mon.write("  "..R6B.."  ")
  546.             mon.setCursorPos(19,8)
  547.             mon.write("  "..R6C.."  ")
  548.         elseif rollout == 3 then
  549.             mon.setCursorPos(19,2)
  550.             mon.write("-------")
  551.             mon.setCursorPos(19,3)
  552.             mon.write("  "..R9A.."  ")
  553.             mon.setCursorPos(19,4)
  554.             mon.write("  "..R9B.."  ")
  555.             mon.setCursorPos(19,5)
  556.             mon.write("  "..R9C.."  ")
  557.             mon.setCursorPos(19,6)
  558.             mon.write("-------")
  559.             mon.setCursorPos(19,7)
  560.             mon.write("  "..R6A.."  ")
  561.             mon.setCursorPos(19,8)
  562.             mon.write("  "..R6B.."  ")
  563.         elseif rollout == 4 then
  564.             result3 = result6
  565.             R3A = R6A
  566.             result6 = result9
  567.             R6A = R9A
  568.             R6B = R9B
  569.             R6C = R9C
  570.             result9 = math.random(1,chanceA + chanceB + chanceC + chanceD + chanceE)
  571.             result9 = result9 - ".0"
  572.            
  573.             if result9 <= chanceA then result9 = "X"                      -- Turn Result9 into a proper result.
  574.                 R9A = drawXA
  575.                 R9B = drawXB
  576.                 R9C = drawXC
  577.             elseif result9 <= (chanceA + chanceB) then result9 = "O"
  578.                 R9A = drawOA
  579.                 R9B = drawOB
  580.                 R9C = drawOC
  581.             elseif result9 <= (chanceA + chanceB + chanceC) then result9 = "B"
  582.                 R9A = drawBA
  583.                 R9B = drawBB
  584.                 R9C = drawBC
  585.             elseif result9 <= (chanceA + chanceB + chanceC + chanceD) then result9 = "7"
  586.                 R9A = draw7A
  587.                 R9B = draw7B
  588.                 R9C = draw7C
  589.             else result9 = "J"
  590.                 R9A = drawJA
  591.                 R9B = drawJB
  592.                 R9C = drawJC
  593.             end
  594.        
  595.             mon.setCursorPos(19,2)
  596.             mon.write("  "..R9A.."  ")
  597.             mon.setCursorPos(19,3)
  598.             mon.write("-------")
  599.             mon.setCursorPos(19,4)
  600.             mon.write("  "..R6A.."  ")
  601.             mon.setCursorPos(19,5)
  602.             mon.write("  "..R6B.."  ")
  603.             mon.setCursorPos(19,6)
  604.             mon.write("  "..R6C.."  ")
  605.             mon.setCursorPos(19,7)
  606.             mon.write("-------")
  607.             mon.setCursorPos(19,8)
  608.             mon.write("  "..R3A.."  ")
  609.             rollout = 0
  610.         else
  611.             print("ERROR IN REEL 3 ROLLOUT")
  612.         end
  613.         sleep(0.1)                                          -- Edit this lower for faster reels, or higher for slower reels.
  614.     end                                                     -- This sleep variable * rollsdone "80" = time taken for the reels to complete.
  615.                                                             -- In this case, it's currently 8, or 8 seconds.
  616.     if result4 == result5 and result5 == result6 then
  617.         print("Detected win in middle row!")
  618.         if result4 == "O" then creditswin = prizeBBB end        -- Basically, check what the player won by looking at the first.
  619.         if result4 == "X" then creditswin = prizeA end          -- We know they're all matching, no need to check them all.
  620.         if result4 == "B" then creditswin = prizeC end
  621.         if result4 == "7" then creditswin = prizeD end
  622.         if result4 == "J" then creditswin = prizeE end
  623.         winner()
  624.     elseif result4 == "O" and result5 == "O" and allowcherrywin == 1 then   -- You can edit "allowcherrywin" in the VARIABLES section if you don't want 2-cherry wins.
  625.         creditswin = prizeBB                                                -- The 2-cherry win only occurs in Reels 1 and 2, not 2 and 3.
  626.         print("Detected win - cherry win!")                                 -- C C ? <- YES   ---   ? C C <- NO
  627.         winner()
  628.     else
  629.         print("There was No Match")     -- Well, if they lost...
  630.         if credits > 0 then
  631.             mon.setCursorPos(20,10)
  632.             mon.write("PRESS  ")        -- Tell them to press a button if they still have credits.
  633.             mon.setCursorPos(20,11)
  634.             mon.write(" BUTTON")
  635.         else
  636.             mon.setCursorPos(20,10)
  637.             mon.write("INSERT ")        -- Or tell them to insert a credit if they're all out.
  638.             mon.setCursorPos(20,11)
  639.             mon.write(" CREDIT")
  640.         end                             -- This instantly goes back to checking for a redstone signal,
  641.     end                                 -- so there's no delay in getting credits in or spinning again.
  642.     redstone.setOutput(stopperside, true)
  643. end
  644.  
  645. function payout()
  646.     sleep(0.2)                                      -- Little delay, so it doesn't feel too instant.
  647.     mon.setCursorPos(20,10)
  648.     mon.write("PAY    ")
  649.     mon.setCursorPos(20,11)                         -- Tell the user he's getting his credits!
  650.     mon.write("    OUT")
  651.     while credits > 0 do
  652.         redstone.setOutput(dispenserside, true)     -- Turn dispenser on,
  653.         sleep(0.2)                                  -- and
  654.         redstone.setOutput(dispenserside, false)    -- Turn it back off. Basically, how redstone works.
  655.         credits = credits - 1                       -- Little tip, dispenser works best when directly touching the computer block.
  656.  
  657.         str = string.format("%03d", credits)        -- Always inform the user what you're doing. Makes it easier to debug AND play.
  658.         mon.setCursorPos(1,11)
  659.         mon.write("   "..str.."  ")
  660.     end
  661.  
  662.     mon.setCursorPos(20,10)
  663.     mon.write("INSERT ")
  664.     mon.setCursorPos(20,11)                         -- Well, there are no credits left now, are there. It dispensed them all. You need more credits to play.
  665.     mon.write(" CREDIT")
  666.     redstone.setOutput(stopperside, true)
  667. end
  668.  
  669. redstone.setOutput(stopperside, true)  -- Failsafe!
  670.  
  671. --[[local payoutA = (((chanceA/(chanceA + chanceB + chanceC + chanceD + chanceE))^3)*prizeA)
  672. local payoutB = (((chanceB/(chanceA + chanceB + chanceC + chanceD + chanceE))^3)*prizeBBB)
  673. local payoutBB = (((chanceB/(chanceA + chanceB + chanceC + chanceD + chanceE))^2)*prizeBB)
  674. local payoutC = (((chanceC/(chanceA + chanceB + chanceC + chanceD + chanceE))^3)*prizeC)
  675. local payoutD = (((chanceD/(chanceA + chanceB + chanceC + chanceD + chanceE))^3)*prizeD)
  676. local payoutE = (((chanceE/(chanceA + chanceB + chanceC + chanceD + chanceE))^3)*prizeE)
  677. local payoutfinal = payoutA + payoutB + payout BB + payout C + payout D + payout E
  678.  
  679. if payoutfinal > 1 then
  680.     print("Caution: Payout higher than 100% ("..payoutfinal.."%)")
  681.     print("You may lose money by letting players use this machine.")
  682.     print("Prevent this by lowering the prizes or decreasing the chances.")
  683. else
  684.     print("Payout at "..payoutfinal.."%.")
  685.     print("Current payout allows for a profit to be made from the machine.")
  686. end]]--
  687.  
  688. local rRunning = true
  689.  
  690. print("Activating rRunning Loop.")          -- AKA, "waiting for someone to press a button."
  691. while rRunning do
  692.     sleep(0.05)
  693.     local rEvent, param = os.pullEvent()
  694.     redstone.setOutput(stopperside, true)   -- Also failsafe!
  695.     if rEvent == "redstone" then            -- We found a redstone signal, so we should check which one it is!
  696.         if redstone.getBundledInput(detectside) then
  697.             if redstone.testBundledInput(detectside, colors.yellow) == true then    -- Yellow = Item Detector. It detects when a user has inserted a credit.
  698.                 credits = credits + 1                                               -- +1 Credit, as he just put one in the Filter.
  699.  
  700.                 str = string.format("%03d", credits)
  701.                 print("Detected credit input. Current credits: "..str)
  702.                 mon.setCursorPos(1,11)
  703.                 mon.write("   "..str.."  ")                                 -- Show the user and console that the computer gave him a credit.
  704.  
  705.                 mon.setCursorPos(20,10)                                     -- Well, there's now some cash, so we don't have to "INSERT CREDIT"!
  706.                 mon.write("PRESS  ")
  707.                 mon.setCursorPos(20,11)        
  708.                 mon.write(" BUTTON")
  709.                 if credits > 256 then payout() end        -- Hardcoded limit so we don't drain the dispenser too much. People will complain if the dispenser can't dispense everything!
  710.             elseif redstone.testBundledInput(detectside, colors.red) == true then       -- Red = PLAY button!
  711.                 redstone.setOutput(stopperside, false)
  712.                 if credits > 0 then                       -- Well, you can't play if there's no money in.
  713.                     str = string.format("%03d", credits)
  714.                     print("Slots rolling... "..str.." credits remain.")
  715.                     roll()
  716.                 else
  717.                     mon.setCursorPos(20,10)               -- Tell the user he needs a credit to play the slots.
  718.                     mon.write("CREDIT ")
  719.                     mon.setCursorPos(20,11)
  720.                     mon.write(" NEEDED")
  721.                     sleep(2)
  722.  
  723.                     mon.setCursorPos(20,10)               -- And tell him to come back with more cash, cheapskate!
  724.                     mon.write("INSERT ")
  725.                     mon.setCursorPos(20,11)
  726.                     mon.write(" CREDIT")
  727.                     redstone.setOutput(stopperside, true)
  728.                 end
  729.             elseif redstone.testBundledInput(detectside, colors.orange) == true then    -- Orange = PAYOUT button!
  730.                 redstone.setOutput(stopperside, false)
  731.                 if credits > 0 then                       -- Can't exactly pay out money if there's nothing to pay out.
  732.                     str = string.format("%03d", credits)
  733.                     print("Paying out winnings of "..str.." credits.")
  734.                     payout()
  735.                 else
  736.                     mon.setCursorPos(20,10)               -- Tell the user he hasn't won anything.
  737.                     mon.write("CREDIT ")
  738.                     mon.setCursorPos(20,11)
  739.                     mon.write(" NEEDED")
  740.                     sleep(2)
  741.  
  742.                     mon.setCursorPos(20,10)               -- Then tell him to come back with more cash!
  743.                     mon.write("INSERT ")
  744.                     mon.setCursorPos(20,11)
  745.                     mon.write(" CREDIT")
  746.                     redstone.setOutput(stopperside, true)
  747.                 end
  748.             end
  749.         end
  750.     end
  751. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement