Development Diary - Dice Poker
More than a year ago, I got that idea of implementing the Dice Poker mini-game from the Witcher video games in Witcher Kings...
As CK2 patches introduced variables and scripted trigger/effects, it was indeed becoming technically feasible: dice can be represented by variables ranging 1-6, and the board simply consists of outputting variables in the localized description of an event...
View attachment 282535
All what was remaining were the implementation details ^^
I've struggled a lot with the randomness (see section below), and also painfully discovered that repeat_event should never be used in another scope than ROOT - otherwise it messes the scope chain (FROM, FROMFROM, ...).
The first implementation was quick & dirty and could only be played from one side with a dumb AI, so I finally had to rewrite the event chain more cleanly, once I knew where it was actually going.
The rules
The rules are based on
TW2, mainly because it's a slightly easier to implement than TW1.
Each game consists of a single round, composed of two rolls:
- After the initial bets (0.5 gold), each player rolls the dice once
- Players can then decide (in turn) to check, raise (0.5 gold), call or resign
- Then each player decide which dice to re-roll, or to pass.
- The winner is the one with the best combination (Five-of-a-kind > Four-of-a-kind > Full House > Six-high straight > Five-high straight > Three-of-a-kind > Two pairs > One pair).
- In case both player have the same combination, the one with the highest combination value wins (One pair of 2s > One pair of 1s).
- In case of same combination of same value or no combination, the values of the remaining dice are used to decide.
- If the hands are exactly identical (five same dice), then it's a draw.
View attachment 282540
Gambler trait
There was already a Gambler lifestyle trait in the mod (though not used in events). With WoL it became somehow redundant with the Game Master lifestyle trait.
So I've switched it to a personality trait:
View attachment 282530
View attachment 282532
As a comparison, if a Gambler can hardly resist playing few dice games in the tavern, a Game Master would make it a full time job and participate in high stake tournaments.
Play a game of Dice Poker
The mini-game is triggered via a targeted decision.
It requires the characters to be at the same location, and the AI will accept if it has the Gambler trait, has a positive opinion of the player, and is not at war with the player (no dice poker during sieges).
Because CK2 doesn't simulate the economy for courtiers, they are usually poor (0 gold), so their wealth is not a condition (they'll just have negative gold if they lose)
View attachment 282534
The game events work from both sides (i.e. should work in multiplayer), so the player may sometime receive an invitation from a wealthy gambler AI at court (with an anti-spam timer).
The decision won't be used between AI characters, for performances (and because it would be pointless, as a simple event could simulate the 50/50 outcome).
The decision doesn't require the player to be a Gambler, and in fact winning a game gives a chance to gain the gambler trait.
Determining the winner
The following steps are used for each player:
- Count the number 1s, 2s, 3s, etc.
- Check if the player matches a combination, starting from the best possible (Five-of-a-Kind of 6s, Five-of-a-Kind of 5s, ... , One pair of 2s , One pair of 1s)
- Store the value of the dice in the combination in rank 1 variable (and also rank 2 variable for Full House and Two pairs)
- Determine the remaining 'lone' dice rank, and store them in a variable (1s + 2s*10 + 3s*100 + ...)
Then between the 2 players try to find a winner by:
- Checking if a combination is better
- If same combination, checking if the rank 1 (and rank 2) values of the combination are higher
- If same rank(s) or no combination, comparing the 'lone' dice variable
- If same 'lone' dice rank, then it's a draw.
Which dice to re-roll ?
It's possible to select the dice to re-roll one by one:
View attachment 282538
But it's a bit painful, and most of the time you just want to keep your Pair or Three-of-a-kind, and re-roll the rest.
This is also exactly what the AI needs to decide which dice to re-roll (i.e. keeping all dice with rank 1 and rank 2 values, if any).
View attachment 282539
Randomness
There's a bit of an issue with the random number generator: it's not completely random...
Because all events happen instantly, the same seed is re-used, and it happens that both players will roll the same values, except for a shift of one dice.
Now, with patch 2.7 and some hidden ping events, it seems a bit better, but no guarantee that the game is not rigged !
Potential further ideas
The mod has some opposite congenital traits "Lucky" and "Cursed", that are not widely used in events yet. It could be interesting that these traits sometimes influence the dice roll, making lucky character slightly more likely to win. Currently you just get a chance to gain the Lucky trait by rolling a Five-of-a-kind combination.
High stake tournaments, with larger bets and multiple games/rounds ?