//! Continuation cost of a called bet before the river (`FORTSETZUNG`, off by default). //! //! `bluff::branches` values the called branch of a bet as if the hand ended at showdown after this //! street: `called share * final pot - added`. Before the river it does not. The villains who call //! are the top `1 - P(fold)` of their range, and on each later street they bet often. A hand that is //! behind them then either folds (giving up its equity share) or calls and pays a bet it is behind //! for. Counting only one street overstates the called branch of bluffs, semibluffs and thin value. //! That is the shared root of over-barrelling, raise wars and the nit-read loss. //! //! The model per later street: the continuing range bets with probability `p_bet`, a bet of //! `BET_SHARE` pot. Hero with share `e` against that range picks the cheaper response. Folding loses //! `e * pot`; calling loses `bet * (1 - 2e)` (nothing when `e >= 0.5`). So the realised share drops by //! the factor `1 - p_bet * min(1, BET_SHARE * (1 - 2e)+ / e)` per street left, compounded. //! //! `p_bet` is the villain's aggression on the next street (its read, else `AGGRESSION_ANCHOR`), raised //! by the narrowness of the continuing range: `a + (1 - a) * P(fold) * NARROW`. Value raises, the //! river, all-ins and tables of over-callers only (`Style::CallingStation`) are untouched. use super::bluff::RaiseKind; use crate::game::{Spot, Street}; use std::cell::Cell; /// Size of a later-street bet as a share of the pot (the common two-thirds bet). pub const BET_SHARE: f64 = 0.66; /// Next-street aggression of a villain without a read. pub const AGGRESSION_ANCHOR: f64 = 0.35; /// Default weight of the continuing range's narrowness in its betting rate (`FORTSETZUNG=1`). pub const NARROW_DEFAULT: f64 = 0.5; /// The switch when on: the narrowness weight and whether thin value is discounted too. #[derive(Clone, Copy, Debug, PartialEq)] pub struct Mode { pub narrow: f64, pub thin_value: bool, } thread_local! { static OVERRIDE: Cell>> = const { Cell::new(None) }; } /// The switch, `None` when off. `FORTSETZUNG=1`: bluffs, semibluffs and thin value with /// `NARROW_DEFAULT`; `FORTSETZUNG=bluff`: bluffs and semibluffs only; `eng:` / `bluff:` the /// same with weight `x`. Anything else is off. pub fn mode() -> Option { use std::sync::OnceLock; static MODE: OnceLock> = OnceLock::new(); OVERRIDE.with(|o| o.get()).unwrap_or_else(|| { *MODE.get_or_init(|| std::env::var("FORTSETZUNG").ok().and_then(|s| parse(&s))) }) } fn parse(s: &str) -> Option { let weight = |x: &str| x.parse::().ok().filter(|x| x.is_finite()); match s { "1" => Some(Mode { narrow: NARROW_DEFAULT, thin_value: true }), "bluff" => Some(Mode { narrow: NARROW_DEFAULT, thin_value: false }), _ => { if let Some(x) = s.strip_prefix("eng:") { weight(x).map(|narrow| Mode { narrow, thin_value: true }) } else { s.strip_prefix("bluff:").and_then(weight).map(|narrow| Mode { narrow, thin_value: false }) } } } } /// Force the switch for this thread (`Some(None)` = off, `Some(Some(m))` = on with `m`), /// `None` = back to the environment. For node diagnosis and tests. pub fn set_override(mode: Option>) { OVERRIDE.with(|o| o.set(mode)); } /// Streets still to come after this one. fn streets_left(street: Street) -> i32 { match street { Street::Flop => 2, Street::Turn => 1, _ => 0, } } /// Probability that the range continuing against a bet that folds `fold` bets the next street. fn bet_rate(spot: &Spot, fold: f64, narrow: f64) -> f64 { let next = |p: &crate::exploit::profile::Profile| match spot.street { Street::Flop => p.turn_aggression.or(p.aggression), _ => p.river_aggression.or(p.aggression), }; let a = spot .live_villains() .filter_map(|v| v.profile.as_ref().and_then(next)) .filter(|x| x.is_finite()) .fold(None, |m: Option, x| Some(m.map_or(x, |m| m.max(x)))) .unwrap_or(AGGRESSION_ANCHOR) .clamp(0.0, 1.0); (a + (1.0 - a) * fold.clamp(0.0, 1.0) * narrow).clamp(0.0, 1.0) } /// Every live villain is read as a non-folding over-caller (`Style::CallingStation` with foldToBet at /// most `STATION_FOLD_TO_BET`): what continues is nearly its whole wide range, not the narrow betting /// range the model prices (pilot 15.11.: station -1,3 bb/100 with the discount, foldToBet read 0,25). /// A loose player who still folds (ladder maniac 0,39, reg) is read as a station too and gains from it /// (maniac +20, reg +9); aggression does not separate them (read 0,32 vs 0,35). fn over_callers_only(spot: &Spot) -> bool { use crate::exploit::style::{classify, Style, STATION_FOLD_TO_BET}; let passive_station = |p: &crate::exploit::profile::Profile| { classify(p) == Style::CallingStation && p.fold_to_bet.is_some_and(|f| f.is_finite() && f <= STATION_FOLD_TO_BET) }; let mut live = spot.live_villains().peekable(); live.peek().is_some() && live.all(|v| v.profile.as_ref().is_some_and(passive_station)) } /// Share of `against` hero keeps over the later streets, per the model above. 1 when off. pub fn realisation(spot: &Spot, kind: RaiseKind, to: f64, fold: f64, against: f64) -> f64 { let Some(Mode { narrow, thin_value }) = mode() else { return 1.0 }; let n = streets_left(spot.street); let untouched = kind == RaiseKind::Value || (kind == RaiseKind::ThinValue && !thin_value); if n == 0 || untouched || to >= spot.hero_stack || against <= 0.0 || against >= 0.5 { return 1.0; } if over_callers_only(spot) { return 1.0; } let p = bet_rate(spot, fold, narrow); let loss = p * (BET_SHARE * (1.0 - 2.0 * against) / against).min(1.0); (1.0 - loss).clamp(0.0, 1.0).powi(n) } /// `against` after the continuation cost. pub fn discounted(spot: &Spot, kind: RaiseKind, to: f64, fold: f64, against: f64) -> f64 { against * realisation(spot, kind, to, fold, against) } #[cfg(test)] mod tests { use super::*; #[test] fn loss_crosses_over_at_the_call_price() { // e = BET_SHARE / (1 + 2 BET_SHARE): folding and calling cost the same. let e = BET_SHARE / (1.0 + 2.0 * BET_SHARE); assert!((BET_SHARE * (1.0 - 2.0 * e) / e - 1.0).abs() < 1e-12); } #[test] fn parses_the_variants() { assert_eq!(parse("1"), Some(Mode { narrow: NARROW_DEFAULT, thin_value: true })); assert_eq!(parse("bluff:0.8"), Some(Mode { narrow: 0.8, thin_value: false })); assert_eq!(parse("0"), None); assert_eq!(parse("eng:x"), None); } #[test] fn off_is_identity() { set_override(Some(None)); assert_eq!(mode(), None); set_override(None); } }