Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Code for roulette gambling. Made for the basic FOnline development tutorial at fodev.net.
- * Author: Slowhand.
- * Reviewer: N/A
- */
- #include "gambling_h.fos"
- #include "economy_h.fos"
- #include "_math.fos"
- #include "_vars.fos"
- #include "_macros.fos"
- /** DIALOGUE FUNCTIONS
- *
- * Functions called directly from dialogues, deamands or results.
- * - Signatures keep 2 Critter params and extra params if needed.
- * - Prefixes used depends on dialogue calls: result - "r_", demand - "d_", dialogue - "dlg_"
- */
- void r_ResetCasinoBet(Critter& player, Critter@ casinoHost)
- {
- resetCasinoBet(player);
- }
- void r_SetBetValue(Critter& player, Critter@ npc, int value)
- {
- setBetValue(player, value);
- }
- void r_SetBetType(Critter& player, Critter@ npc, int type)
- {
- setBetType(player, type);
- }
- void r_SetRouletteRoll(Critter& player, Critter@ npc, int roll)
- {
- setRouletteRoll(player, roll);
- }
- void r_SetBetProgress(Critter& player, Critter@ npc, int progress)
- {
- setBetProgress(player, progress);
- }
- /**< This is used to set the number the player bets on using the "Say" option in dialogue menu. */
- uint dlg_SetBetNumberUsingSay(Critter& player, Critter@ npc, string@ say)
- {
- if(!IS_DIALOG_SAY_MODE(say))
- return 0;
- /**< If the number the player enters is not in range, notify him and make him try again. */
- int betNumber = 0;
- if(!StrToInt(say, betNumber) || (betNumber < 0) || (betNumber > 36))
- {
- player.Say(SAY_DIALOG, "Please type a number between 0 and 36 in numeric format.");
- return 0;
- }
- setBetNumber(player, betNumber);
- setBetProgress(player, GAMB_BET_PLACED);
- player.Say(SAY_DIALOG, "You bet on " + betNumber + ".");
- return FORCE_DIALOG_ROULETTE_BET_VALUE;
- }
- /**< Rolls the winning number. Sets LVAR_gambling_bet_value to the amount won. (does not pay it) */
- void r_SpinTheRoulette(Critter& player, Critter@ casinoHost)
- {
- int betType = getBetType(player);
- int betValue = getBetValue(player);
- int betProgress = getBetProgress(player);
- if (betType == GAMB_NO_BET || betValue == GAMB_NO_BET
- || betProgress != GAMB_BET_PAYED)
- {
- resetCasinoBet(player);
- return;
- }
- SpinCasinoWheel(player);
- if (betType == GAMB_ROULETTE_BET_NUMBER)
- {
- DecideIfStraightBetWon(player, casinoHost);
- }
- if (betType == GAMB_ROULETTE_BET_RED)
- {
- DecideIfRedBetWon(player, casinoHost);
- }
- if (betType == GAMB_ROULETTE_BET_BLACK)
- {
- DecideIfBlackBetWon(player, casinoHost);
- }
- if (betType == GAMB_ROULETTE_BET_EVEN)
- {
- DecideIfEvenBetWon(player, casinoHost);
- }
- if (betType == GAMB_ROULETTE_BET_ODD)
- {
- DecideIfOddBetWon(player, casinoHost);
- }
- if (betType == GAMB_ROULETTE_BET_MANQUE)
- {
- DecideIfManqueBetWon(player, casinoHost);
- }
- if (betType == GAMB_ROULETTE_BET_PASSE)
- {
- DecideIfPasseBetWon(player, casinoHost);
- }
- }
- /**< Adds bet win value to players inventory.*/
- void r_PayWinnerBet(Critter& player, Critter@ casinoHost)
- {
- int betValue = getBetValue(player);
- int betProgress = getBetProgress(player);
- if (betValue < 0)
- return;
- if (betProgress == GAMB_BET_WON)
- {
- BanksTransferToCritter(player, betValue, TRANSACTION_DEFAULT, false);
- }
- }
- /**< Used with Lexers to show current bet and the number rolled. */
- void dlg_ShowBetInfo(Critter& player, Critter@ npc, string@ text)
- {
- if(!IS_DIALOG_GENERATED(text))
- return;
- text += "$betValue" + getBetValue(player);
- text += "$betType" + getBetTypeAsString(player);
- text += "$rouletteRoll" + getRouletteRoll(player);
- text += "$rollColor" + getRouletteNumberColorAsString(getRouletteNumberColor(getRouletteRoll(player)));
- text += "$betProgress" + getBetProgress(player);
- }
- /**< END OF: Dialogue Functions */
- /**< SETTERS */
- void setBetValue(Critter& player, int value)
- {
- GameVar@ betValue = GetLocalVar(LVAR_gambling_bet_value, player.Id);
- if (!valid(betValue))
- return;
- betValue = value;
- }
- void setBetType(Critter& player, int type)
- {
- GameVar@ betType = GetLocalVar(LVAR_gambling_bet_type, player.Id);
- if (!valid(betType))
- return;
- betType = type;
- }
- void setBetNumber(Critter& player, int number)
- {
- GameVar@ betNumber = GetLocalVar(LVAR_gambling_bet_number, player.Id);
- if (!valid(betNumber))
- return;
- betNumber = number;
- }
- void setRouletteRoll(Critter& player, int roll)
- {
- GameVar@ rouletteRoll = GetLocalVar(LVAR_gambling_roulette_roll, player.Id);
- if (!valid(rouletteRoll))
- return;
- rouletteRoll = roll;
- }
- void setBetProgress(Critter& player, int progress)
- {
- GameVar@ betProgress = GetLocalVar(LVAR_gambling_bet_progress, player.Id);
- if (!valid(betProgress))
- return;
- betProgress = progress;
- }
- /**< END OF: Setters */
- /**< GETTERS */
- int getBetValue(Critter& player)
- {
- GameVar@ betValue = GetLocalVar(LVAR_gambling_bet_value, player.Id);
- if (!valid(betValue))
- return 0;
- return betValue.GetValue();
- }
- int getBetType(Critter& player)
- {
- GameVar@ betType = GetLocalVar(LVAR_gambling_bet_type, player.Id);
- if (!valid(betType))
- return 0;
- return betType.GetValue();
- }
- int getBetNumber(Critter& player)
- {
- GameVar@ betNumber = GetLocalVar(LVAR_gambling_bet_number, player.Id);
- if (!valid(betNumber))
- return 0;
- return betNumber.GetValue();
- }
- int getRouletteRoll(Critter& player)
- {
- GameVar@ rouletteRoll = GetLocalVar(LVAR_gambling_roulette_roll, player.Id);
- if (!valid(rouletteRoll))
- return 0;
- return rouletteRoll.GetValue();
- }
- int getBetProgress(Critter& player)
- {
- GameVar@ betProgress = GetLocalVar(LVAR_gambling_bet_progress, player.Id);
- if (!valid(betProgress))
- return 0;
- return betProgress.GetValue();
- }
- /**< END OF: Setters */
- /**< INIT */
- void resetCasinoBet(Critter& player)
- {
- setBetValue(player, GAMB_NO_BET);
- setBetType(player, GAMB_NO_BET);
- setBetNumber(player, GAMB_NO_BET);
- setRouletteRoll(player, GAMB_NO_BET);
- setBetProgress(player, GAMB_NO_BET);
- }
- /**< END OF: Init */
- /**< CASINO LOGIC */
- void DecideIfStraightBetWon(Critter& player, Critter@ casinoHost)
- {
- int betNumber = getBetNumber(player);
- if (betNumber == getRouletteRoll(player))
- {
- if (isRerollNeeded(player, casinoHost))
- {
- SpinCasinoWheel(player);
- if (betNumber == getRouletteRoll(player))
- {
- RouletteNumberWin(player);
- }
- else
- {
- RouletteLost(player);
- }
- }
- else
- {
- RouletteNumberWin(player);
- }
- }
- else
- {
- RouletteLost(player);
- }
- }
- void DecideIfRedBetWon(Critter& player, Critter@ casinoHost)
- {
- if (getRouletteNumberColor(getRouletteRoll(player)) == GAMB_ROULETTE_COLOR_RED)
- {
- if (isRerollNeeded(player, casinoHost))
- {
- SpinCasinoWheel(player);
- if (getRouletteNumberColor(getRouletteRoll(player)) == GAMB_ROULETTE_COLOR_RED)
- {
- RouletteDoubleWin(player);
- }
- else
- {
- RouletteLost(player);
- }
- }
- else
- {
- RouletteDoubleWin(player);
- }
- }
- else
- {
- RouletteLost(player);
- }
- }
- void DecideIfBlackBetWon(Critter& player, Critter@ casinoHost)
- {
- if (getRouletteNumberColor(getRouletteRoll(player)) == GAMB_ROULETTE_COLOR_BLACK)
- {
- if (isRerollNeeded(player, casinoHost))
- {
- SpinCasinoWheel(player);
- if (getRouletteNumberColor(getRouletteRoll(player)) == GAMB_ROULETTE_COLOR_BLACK)
- {
- RouletteDoubleWin(player);
- }
- else
- {
- RouletteLost(player);
- }
- }
- else
- {
- RouletteDoubleWin(player);
- }
- }
- else
- {
- RouletteLost(player);
- }
- }
- void DecideIfOddBetWon(Critter& player, Critter@ casinoHost)
- {
- if (isOdd(getRouletteRoll(player)))
- {
- if (isRerollNeeded(player, casinoHost))
- {
- SpinCasinoWheel(player);
- if (isOdd(getRouletteRoll(player)))
- {
- RouletteDoubleWin(player);
- }
- else
- {
- RouletteLost(player);
- }
- }
- else
- {
- RouletteDoubleWin(player);
- }
- }
- else
- {
- RouletteLost(player);
- }
- }
- void DecideIfEvenBetWon(Critter& player, Critter@ casinoHost)
- {
- if (isEven(getRouletteRoll(player)))
- {
- if (isRerollNeeded(player, casinoHost))
- {
- SpinCasinoWheel(player);
- if (isEven(getRouletteRoll(player)))
- {
- RouletteDoubleWin(player);
- }
- else
- {
- RouletteLost(player);
- }
- }
- else
- {
- RouletteDoubleWin(player);
- }
- }
- else
- {
- RouletteLost(player);
- }
- }
- void DecideIfManqueBetWon(Critter& player, Critter@ casinoHost)
- {
- if (isManque(getRouletteRoll(player)))
- {
- if (isRerollNeeded(player, casinoHost))
- {
- SpinCasinoWheel(player);
- if (isManque(getRouletteRoll(player)))
- {
- RouletteDoubleWin(player);
- }
- else
- {
- RouletteLost(player);
- }
- }
- else
- {
- RouletteDoubleWin(player);
- }
- }
- else
- {
- RouletteLost(player);
- }
- }
- void DecideIfPasseBetWon(Critter& player, Critter@ casinoHost)
- {
- if (isPasse(getRouletteRoll(player)))
- {
- if (isRerollNeeded(player, casinoHost))
- {
- SpinCasinoWheel(player);
- if (isPasse(getRouletteRoll(player)))
- {
- RouletteDoubleWin(player);
- }
- else
- {
- RouletteLost(player);
- }
- }
- else
- {
- RouletteDoubleWin(player);
- }
- }
- else
- {
- RouletteLost(player);
- }
- }
- void SpinCasinoWheel(Critter& player)
- {
- int roll = Random(0, 36);
- setRouletteRoll(player, roll);
- }
- /**< Decides if a re-roll is needed based on player and casino host gambling difference. */
- /**< If player gambling skill is higher or equal to casino host skill, then there is no extra difficulty in winning. */
- bool isRerollNeeded(Critter& player, Critter@ casinoHost)
- {
- if (!valid(casinoHost))
- return false;
- int gamblingDisadvantage = CLAMP(casinoHost.Skill[SK_GAMBLING] - player.Skill[SK_GAMBLING], 0, 100);
- if (Random(1, 100) <= gamblingDisadvantage)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- void RouletteLost(Critter& player)
- {
- setBetProgress(player, GAMB_BET_LOST);
- }
- void RouletteNumberWin(Critter& player)
- {
- setBetValue(player, getBetValue(player) * 35);
- setBetProgress(player, GAMB_BET_WON);
- }
- void RouletteDoubleWin(Critter& player)
- {
- setBetValue(player, getBetValue(player) * 2);
- setBetProgress(player, GAMB_BET_WON);
- }
- /**< END OF: Casino Logic */
- /**< AUX */
- bool isOdd(int number)
- {
- if (number <= 0 || number > 36)
- return false;
- if (number % 2 != 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- bool isEven(int number)
- {
- if (number <= 0 || number > 36)
- return false;
- if (number % 2 == 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- bool isManque(int number)
- {
- if (number <= 0 || number > 36)
- return false;
- if (number <= 18)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- bool isPasse(int number)
- {
- if (number <= 0 || number > 36)
- return false;
- if (number > 18)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- string getBetTypeAsString(Critter& player)
- {
- string retValue = "";
- int betType = getBetType(player);
- switch (betType)
- {
- case GAMB_ROULETTE_BET_NUMBER:
- retValue ="" + getBetNumber(player);
- break;
- case GAMB_ROULETTE_BET_RED:
- retValue ="red";
- break;
- case GAMB_ROULETTE_BET_BLACK:
- retValue ="black";
- break;
- case GAMB_ROULETTE_BET_ODD:
- retValue ="odd";
- break;
- case GAMB_ROULETTE_BET_EVEN:
- retValue ="even";
- break;
- case GAMB_ROULETTE_BET_MANQUE:
- retValue ="manque";
- break;
- case GAMB_ROULETTE_BET_PASSE:
- retValue ="passe";
- break;
- default:
- retValue ="No bet.";
- }
- return retValue;
- }
- string getRouletteNumberColorAsString(int colorValue)
- {
- string retValue = "";
- if (colorValue == GAMB_ROULETTE_COLOR_RED)
- {
- retValue = "red";
- }
- if (colorValue == GAMB_ROULETTE_COLOR_BLACK)
- {
- retValue = "black";
- }
- if (colorValue == GAMB_ROULETTE_COLOR_GREEN)
- {
- retValue = "green";
- }
- return retValue;
- }
- int getRouletteNumberColor(int number)
- {
- array <int> red = {1, 3, 5, 7, 9, 12, 14, 16, 18, 21, 23, 25, 27, 28, 30, 32, 34, 36};
- if (number < 0 || number > 36)
- return GAMB_ROULETTE_COLOR_NONE;
- if (number == 0)
- return GAMB_ROULETTE_COLOR_GREEN;
- // for (int i = 0; i < red.length(); i++)
- // {
- // if (red[i] == number)
- // return GAMB_ROULETTE_COLOR_RED;
- // }
- if (red.find(number) >= 0)
- {
- return GAMB_ROULETTE_COLOR_RED;
- }
- return GAMB_ROULETTE_COLOR_BLACK;
- }
- /**< END OF: AUX */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement