Advertisement
Slowhand-VI

gambling_roulette.fos (reviewed)

Aug 19th, 2015
605
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.19 KB | None | 0 0
  1. /**
  2.  *  Code for roulette gambling. Made for the basic FOnline development tutorial at fodev.net.
  3.  *  Author: Slowhand.
  4.  *  Reviewer: N/A
  5.  */
  6.  
  7. #include "gambling_h.fos"
  8.  
  9. #include "economy_h.fos"
  10. #include "_math.fos"
  11. #include "_vars.fos"
  12. #include "_macros.fos"
  13.  
  14. /** DIALOGUE FUNCTIONS
  15.  *
  16.  *  Functions called directly from dialogues, deamands or results.
  17.  *  - Signatures keep 2 Critter params and extra params if needed.
  18.  *  - Prefixes used depends on dialogue calls: result - "r_", demand - "d_", dialogue - "dlg_"
  19.  */
  20.  
  21. void r_ResetCasinoBet(Critter& player, Critter@ casinoHost)
  22. {
  23.     resetCasinoBet(player);
  24. }
  25.  
  26. void r_SetBetValue(Critter& player, Critter@ npc, int value)
  27. {
  28.     setBetValue(player, value);
  29. }
  30.  
  31. void r_SetBetType(Critter& player, Critter@ npc, int type)
  32. {
  33.     setBetType(player, type);
  34. }
  35.  
  36. void r_SetRouletteRoll(Critter& player, Critter@ npc, int roll)
  37. {
  38.     setRouletteRoll(player, roll);
  39. }
  40.  
  41. void r_SetBetProgress(Critter& player, Critter@ npc, int progress)
  42. {
  43.     setBetProgress(player, progress);
  44. }
  45.  
  46. /**< This is used to set the number the player bets on using the "Say" option in dialogue menu. */
  47. uint dlg_SetBetNumberUsingSay(Critter& player, Critter@ npc, string@ say)
  48. {
  49.     if(!IS_DIALOG_SAY_MODE(say))
  50.         return 0;
  51.  
  52.     /**< If the number the player enters is not in range, notify him and make him try again. */
  53.     int betNumber = 0;
  54.     if(!StrToInt(say, betNumber) || (betNumber < 0) || (betNumber > 36))
  55.     {
  56.         player.Say(SAY_DIALOG, "Please type a number between 0 and 36 in numeric format.");
  57.         return 0;
  58.     }
  59.  
  60.     setBetNumber(player, betNumber);
  61.     setBetProgress(player, GAMB_BET_PLACED);
  62.     player.Say(SAY_DIALOG, "You bet on " + betNumber + ".");
  63.     return FORCE_DIALOG_ROULETTE_BET_VALUE;
  64. }
  65.  
  66. /**< Rolls the winning number. Sets LVAR_gambling_bet_value to the amount won. (does not pay it) */
  67. void r_SpinTheRoulette(Critter& player, Critter@ casinoHost)
  68. {
  69.     int betType = getBetType(player);
  70.     int betValue = getBetValue(player);
  71.     int betProgress = getBetProgress(player);
  72.  
  73.     if (betType == GAMB_NO_BET || betValue == GAMB_NO_BET
  74.             || betProgress != GAMB_BET_PAYED)
  75.     {
  76.         resetCasinoBet(player);
  77.         return;
  78.     }
  79.  
  80.     SpinCasinoWheel(player);
  81.  
  82.     if (betType == GAMB_ROULETTE_BET_NUMBER)
  83.     {
  84.         DecideIfStraightBetWon(player, casinoHost);
  85.     }
  86.     if (betType == GAMB_ROULETTE_BET_RED)
  87.     {
  88.         DecideIfRedBetWon(player, casinoHost);
  89.     }
  90.     if (betType == GAMB_ROULETTE_BET_BLACK)
  91.     {
  92.         DecideIfBlackBetWon(player, casinoHost);
  93.     }
  94.     if (betType == GAMB_ROULETTE_BET_EVEN)
  95.     {
  96.         DecideIfEvenBetWon(player, casinoHost);
  97.     }
  98.     if (betType == GAMB_ROULETTE_BET_ODD)
  99.     {
  100.         DecideIfOddBetWon(player, casinoHost);
  101.     }
  102.     if (betType == GAMB_ROULETTE_BET_MANQUE)
  103.     {
  104.         DecideIfManqueBetWon(player, casinoHost);
  105.     }
  106.     if (betType == GAMB_ROULETTE_BET_PASSE)
  107.     {
  108.         DecideIfPasseBetWon(player, casinoHost);
  109.     }
  110. }
  111.  
  112. /**< Adds bet win value to players inventory.*/
  113. void r_PayWinnerBet(Critter& player, Critter@ casinoHost)
  114. {
  115.     int betValue = getBetValue(player);
  116.     int betProgress = getBetProgress(player);
  117.     if (betValue < 0)
  118.         return;
  119.     if (betProgress == GAMB_BET_WON)
  120.     {
  121.         BanksTransferToCritter(player, betValue, TRANSACTION_DEFAULT, false);
  122.     }
  123. }
  124.  
  125. /**< Used with Lexers to show current bet and the number rolled. */
  126. void dlg_ShowBetInfo(Critter& player, Critter@ npc, string@ text)
  127. {
  128.     if(!IS_DIALOG_GENERATED(text))
  129.         return;
  130.  
  131.     text += "$betValue" + getBetValue(player);
  132.     text += "$betType" + getBetTypeAsString(player);
  133.     text += "$rouletteRoll" + getRouletteRoll(player);
  134.     text += "$rollColor" + getRouletteNumberColorAsString(getRouletteNumberColor(getRouletteRoll(player)));
  135.     text += "$betProgress" + getBetProgress(player);
  136. }
  137.  
  138. /**< END OF: Dialogue Functions */
  139.  
  140.  
  141. /**< SETTERS */
  142.  
  143. void setBetValue(Critter& player, int value)
  144. {
  145.     GameVar@ betValue = GetLocalVar(LVAR_gambling_bet_value, player.Id);
  146.     if (!valid(betValue))
  147.             return;
  148.     betValue = value;
  149. }
  150.  
  151. void setBetType(Critter& player, int type)
  152. {
  153.     GameVar@ betType = GetLocalVar(LVAR_gambling_bet_type, player.Id);
  154.     if (!valid(betType))
  155.             return;
  156.     betType = type;
  157. }
  158.  
  159. void setBetNumber(Critter& player, int number)
  160. {
  161.     GameVar@ betNumber = GetLocalVar(LVAR_gambling_bet_number, player.Id);
  162.     if (!valid(betNumber))
  163.             return;
  164.     betNumber = number;
  165. }
  166.  
  167. void setRouletteRoll(Critter& player, int roll)
  168. {
  169.     GameVar@ rouletteRoll = GetLocalVar(LVAR_gambling_roulette_roll, player.Id);
  170.     if (!valid(rouletteRoll))
  171.             return;
  172.     rouletteRoll = roll;
  173. }
  174.  
  175. void setBetProgress(Critter& player, int progress)
  176. {
  177.     GameVar@ betProgress = GetLocalVar(LVAR_gambling_bet_progress, player.Id);
  178.     if (!valid(betProgress))
  179.             return;
  180.     betProgress = progress;
  181. }
  182.  
  183. /**< END OF: Setters */
  184.  
  185.  
  186. /**< GETTERS */
  187.  
  188. int getBetValue(Critter& player)
  189. {
  190.     GameVar@ betValue = GetLocalVar(LVAR_gambling_bet_value, player.Id);
  191.     if (!valid(betValue))
  192.             return 0;
  193.     return betValue.GetValue();
  194. }
  195.  
  196. int getBetType(Critter& player)
  197. {
  198.     GameVar@ betType = GetLocalVar(LVAR_gambling_bet_type, player.Id);
  199.     if (!valid(betType))
  200.             return 0;
  201.     return betType.GetValue();
  202. }
  203.  
  204. int getBetNumber(Critter& player)
  205. {
  206.     GameVar@ betNumber = GetLocalVar(LVAR_gambling_bet_number, player.Id);
  207.     if (!valid(betNumber))
  208.             return 0;
  209.     return betNumber.GetValue();
  210. }
  211.  
  212. int getRouletteRoll(Critter& player)
  213. {
  214.     GameVar@ rouletteRoll = GetLocalVar(LVAR_gambling_roulette_roll, player.Id);
  215.     if (!valid(rouletteRoll))
  216.             return 0;
  217.     return rouletteRoll.GetValue();
  218. }
  219.  
  220. int getBetProgress(Critter& player)
  221. {
  222.     GameVar@ betProgress = GetLocalVar(LVAR_gambling_bet_progress, player.Id);
  223.     if (!valid(betProgress))
  224.             return 0;
  225.     return betProgress.GetValue();
  226. }
  227.  
  228. /**< END OF: Setters */
  229.  
  230.  
  231. /**< INIT */
  232.  
  233. void resetCasinoBet(Critter& player)
  234. {
  235.     setBetValue(player, GAMB_NO_BET);
  236.     setBetType(player, GAMB_NO_BET);
  237.     setBetNumber(player, GAMB_NO_BET);
  238.     setRouletteRoll(player, GAMB_NO_BET);
  239.     setBetProgress(player, GAMB_NO_BET);
  240. }
  241.  
  242. /**< END OF: Init */
  243.  
  244.  
  245. /**< CASINO LOGIC */
  246.  
  247. void DecideIfStraightBetWon(Critter& player, Critter@ casinoHost)
  248. {
  249.     int betNumber = getBetNumber(player);
  250.     if (betNumber == getRouletteRoll(player))
  251.     {
  252.         if (isRerollNeeded(player, casinoHost))
  253.         {
  254.             SpinCasinoWheel(player);
  255.             if (betNumber == getRouletteRoll(player))
  256.             {
  257.                 RouletteNumberWin(player);
  258.             }
  259.             else
  260.             {
  261.                 RouletteLost(player);
  262.             }
  263.         }
  264.         else
  265.         {
  266.             RouletteNumberWin(player);
  267.         }
  268.     }
  269.     else
  270.     {
  271.         RouletteLost(player);
  272.     }
  273. }
  274.  
  275. void DecideIfRedBetWon(Critter& player, Critter@ casinoHost)
  276. {
  277.     if (getRouletteNumberColor(getRouletteRoll(player)) == GAMB_ROULETTE_COLOR_RED)
  278.     {
  279.         if (isRerollNeeded(player, casinoHost))
  280.         {
  281.             SpinCasinoWheel(player);
  282.             if (getRouletteNumberColor(getRouletteRoll(player)) == GAMB_ROULETTE_COLOR_RED)
  283.             {
  284.                 RouletteDoubleWin(player);
  285.             }
  286.             else
  287.             {
  288.                 RouletteLost(player);
  289.             }
  290.         }
  291.         else
  292.         {
  293.             RouletteDoubleWin(player);
  294.         }
  295.     }
  296.     else
  297.     {
  298.         RouletteLost(player);
  299.     }
  300. }
  301.  
  302. void DecideIfBlackBetWon(Critter& player, Critter@ casinoHost)
  303. {
  304.     if (getRouletteNumberColor(getRouletteRoll(player)) == GAMB_ROULETTE_COLOR_BLACK)
  305.     {
  306.         if (isRerollNeeded(player, casinoHost))
  307.         {
  308.             SpinCasinoWheel(player);
  309.             if (getRouletteNumberColor(getRouletteRoll(player)) == GAMB_ROULETTE_COLOR_BLACK)
  310.             {
  311.                 RouletteDoubleWin(player);
  312.             }
  313.             else
  314.             {
  315.                 RouletteLost(player);
  316.             }
  317.         }
  318.         else
  319.         {
  320.             RouletteDoubleWin(player);
  321.         }
  322.     }
  323.     else
  324.     {
  325.         RouletteLost(player);
  326.     }
  327. }
  328.  
  329. void DecideIfOddBetWon(Critter& player, Critter@ casinoHost)
  330. {
  331.     if (isOdd(getRouletteRoll(player)))
  332.     {
  333.         if (isRerollNeeded(player, casinoHost))
  334.         {
  335.             SpinCasinoWheel(player);
  336.             if (isOdd(getRouletteRoll(player)))
  337.             {
  338.                 RouletteDoubleWin(player);
  339.             }
  340.             else
  341.             {
  342.                 RouletteLost(player);
  343.             }
  344.         }
  345.         else
  346.         {
  347.             RouletteDoubleWin(player);
  348.         }
  349.     }
  350.     else
  351.     {
  352.         RouletteLost(player);
  353.     }
  354. }
  355.  
  356. void DecideIfEvenBetWon(Critter& player, Critter@ casinoHost)
  357. {
  358.     if (isEven(getRouletteRoll(player)))
  359.     {
  360.         if (isRerollNeeded(player, casinoHost))
  361.         {
  362.             SpinCasinoWheel(player);
  363.             if (isEven(getRouletteRoll(player)))
  364.             {
  365.                 RouletteDoubleWin(player);
  366.             }
  367.             else
  368.             {
  369.                 RouletteLost(player);
  370.             }
  371.         }
  372.         else
  373.         {
  374.             RouletteDoubleWin(player);
  375.         }
  376.     }
  377.     else
  378.     {
  379.         RouletteLost(player);
  380.     }
  381. }
  382.  
  383. void DecideIfManqueBetWon(Critter& player, Critter@ casinoHost)
  384. {
  385.     if (isManque(getRouletteRoll(player)))
  386.     {
  387.         if (isRerollNeeded(player, casinoHost))
  388.         {
  389.             SpinCasinoWheel(player);
  390.             if (isManque(getRouletteRoll(player)))
  391.             {
  392.                 RouletteDoubleWin(player);
  393.             }
  394.             else
  395.             {
  396.                 RouletteLost(player);
  397.             }
  398.         }
  399.         else
  400.         {
  401.             RouletteDoubleWin(player);
  402.         }
  403.     }
  404.     else
  405.     {
  406.         RouletteLost(player);
  407.     }
  408. }
  409.  
  410. void DecideIfPasseBetWon(Critter& player, Critter@ casinoHost)
  411. {
  412.     if (isPasse(getRouletteRoll(player)))
  413.     {
  414.         if (isRerollNeeded(player, casinoHost))
  415.         {
  416.             SpinCasinoWheel(player);
  417.             if (isPasse(getRouletteRoll(player)))
  418.             {
  419.                 RouletteDoubleWin(player);
  420.             }
  421.             else
  422.             {
  423.                 RouletteLost(player);
  424.             }
  425.         }
  426.         else
  427.         {
  428.             RouletteDoubleWin(player);
  429.         }
  430.     }
  431.     else
  432.     {
  433.         RouletteLost(player);
  434.     }
  435. }
  436.  
  437. void SpinCasinoWheel(Critter& player)
  438. {
  439.     int roll = Random(0, 36);
  440.     setRouletteRoll(player, roll);
  441. }
  442.  
  443. /**< Decides if a re-roll is needed based on player and casino host gambling difference. */
  444. /**< If player gambling skill is higher or equal to casino host skill, then there is no extra difficulty in winning. */
  445. bool isRerollNeeded(Critter& player, Critter@ casinoHost)
  446. {
  447.     if (!valid(casinoHost))
  448.         return false;
  449.  
  450.     int gamblingDisadvantage = CLAMP(casinoHost.Skill[SK_GAMBLING] - player.Skill[SK_GAMBLING], 0, 100);
  451.     if (Random(1, 100) <= gamblingDisadvantage)
  452.     {
  453.         return true;
  454.     }
  455.     else
  456.     {
  457.         return false;
  458.     }
  459. }
  460.  
  461. void RouletteLost(Critter& player)
  462. {
  463.     setBetProgress(player, GAMB_BET_LOST);
  464. }
  465.  
  466. void RouletteNumberWin(Critter& player)
  467. {
  468.     setBetValue(player, getBetValue(player) * 35);
  469.     setBetProgress(player, GAMB_BET_WON);
  470. }
  471.  
  472. void RouletteDoubleWin(Critter& player)
  473. {
  474.     setBetValue(player, getBetValue(player) * 2);
  475.     setBetProgress(player, GAMB_BET_WON);
  476. }
  477.  
  478. /**< END OF: Casino Logic */
  479.  
  480.  
  481. /**< AUX */
  482.  
  483. bool isOdd(int number)
  484. {
  485.     if (number <= 0 || number > 36)
  486.         return false;
  487.  
  488.     if (number % 2 != 0)
  489.     {
  490.         return true;
  491.     }
  492.     else
  493.     {
  494.         return false;
  495.     }
  496. }
  497.  
  498. bool isEven(int number)
  499. {
  500.     if (number <= 0 || number > 36)
  501.         return false;
  502.  
  503.     if (number % 2 == 0)
  504.     {
  505.         return true;
  506.     }
  507.     else
  508.     {
  509.         return false;
  510.     }
  511. }
  512.  
  513. bool isManque(int number)
  514. {
  515.     if (number <= 0 || number > 36)
  516.         return false;
  517.  
  518.     if (number <= 18)
  519.     {
  520.         return true;
  521.     }
  522.     else
  523.     {
  524.         return false;
  525.     }
  526. }
  527.  
  528. bool isPasse(int number)
  529. {
  530.     if (number <= 0 || number > 36)
  531.         return false;
  532.  
  533.     if (number > 18)
  534.     {
  535.         return true;
  536.     }
  537.     else
  538.     {
  539.         return false;
  540.     }
  541. }
  542.  
  543. string getBetTypeAsString(Critter& player)
  544. {
  545.     string retValue = "";
  546.     int betType = getBetType(player);
  547.     switch (betType)
  548.     {
  549.         case GAMB_ROULETTE_BET_NUMBER:
  550.             retValue ="" + getBetNumber(player);
  551.             break;
  552.         case GAMB_ROULETTE_BET_RED:
  553.             retValue ="red";
  554.             break;
  555.         case GAMB_ROULETTE_BET_BLACK:
  556.             retValue ="black";
  557.             break;
  558.         case GAMB_ROULETTE_BET_ODD:
  559.             retValue ="odd";
  560.             break;
  561.         case GAMB_ROULETTE_BET_EVEN:
  562.             retValue ="even";
  563.             break;
  564.         case GAMB_ROULETTE_BET_MANQUE:
  565.             retValue ="manque";
  566.             break;
  567.         case GAMB_ROULETTE_BET_PASSE:
  568.             retValue ="passe";
  569.             break;
  570.         default:
  571.             retValue ="No bet.";
  572.     }
  573.     return retValue;
  574. }
  575.  
  576. string getRouletteNumberColorAsString(int colorValue)
  577. {
  578.     string retValue = "";
  579.     if (colorValue == GAMB_ROULETTE_COLOR_RED)
  580.     {
  581.         retValue = "red";
  582.     }
  583.     if (colorValue == GAMB_ROULETTE_COLOR_BLACK)
  584.     {
  585.         retValue = "black";
  586.     }
  587.     if (colorValue == GAMB_ROULETTE_COLOR_GREEN)
  588.     {
  589.         retValue = "green";
  590.     }
  591.     return retValue;
  592. }
  593.  
  594. int getRouletteNumberColor(int number)
  595. {
  596.     array <int> red = {1, 3, 5, 7, 9, 12, 14, 16, 18, 21, 23, 25, 27, 28, 30, 32, 34, 36};
  597.     if (number < 0 || number > 36)
  598.         return GAMB_ROULETTE_COLOR_NONE;
  599.     if (number == 0)
  600.         return GAMB_ROULETTE_COLOR_GREEN;
  601. //    for (int i = 0; i < red.length(); i++)
  602. //    {
  603. //        if (red[i] == number)
  604. //            return GAMB_ROULETTE_COLOR_RED;
  605. //    }
  606.     if (red.find(number) >= 0)
  607.     {
  608.         return GAMB_ROULETTE_COLOR_RED;
  609.     }
  610.     return GAMB_ROULETTE_COLOR_BLACK;
  611. }
  612.  
  613. /**< END OF: AUX */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement