use crate::card::{mask_has, Card};
use std::collections::HashMap;
use crate::range::Combo;
pub(super) struct TerminalIndex {
same_pair: [Vec<Vec<usize>>; 2],
}
pub(super) struct BoardOrder {
order: [Vec<usize>; 2],
rank_bounds: [Vec<(usize, usize)>; 2],
}
impl BoardOrder {
pub(super) fn new(combos: &[Vec<Combo>; 2], scores: &[Vec<u32>; 2], board: u64) -> Self {
let order: [Vec<usize>; 2] = std::array::from_fn(|p| {
let mut order: Vec<_> = (0..combos[p].len())
.filter(|&i| live(combos[p][i], board))
.collect();
order.sort_unstable_by_key(|&i| scores[p][i]);
order
});
let rank_bounds = std::array::from_fn(|p| {
let opponent = &order[1 - p];
let (mut lower, mut upper) = (0, 0);
order[p]
.iter()
.map(|&i| {
let score = scores[p][i];
while lower < opponent.len() && scores[1 - p][opponent[lower]] < score {
lower += 1;
}
upper = upper.max(lower);
while upper < opponent.len() && scores[1 - p][opponent[upper]] <= score {
upper += 1;
}
(lower, upper)
})
.collect()
});
Self { order, rank_bounds }
}
}
#[derive(Clone)]
struct ReachMass {
total: f64,
card: [f64; 52],
support: i32,
card_support: [i32; 52],
}
impl ReachMass {
fn new() -> Self {
Self {
total: 0.0,
card: [0.0; 52],
support: 0,
card_support: [0; 52],
}
}
fn add(&mut self, combo: Combo, weight: f64) {
if weight == 0.0 {
return;
}
self.total += weight;
self.card[combo.cards[0].0 as usize] += weight;
self.card[combo.cards[1].0 as usize] += weight;
self.support += 1;
self.card_support[combo.cards[0].0 as usize] += 1;
self.card_support[combo.cards[1].0 as usize] += 1;
}
fn support_excluding(&self, combo: Combo) -> i32 {
self.support
- self.card_support[combo.cards[0].0 as usize]
- self.card_support[combo.cards[1].0 as usize]
}
fn cancellation_prone(&self, value: f64) -> bool {
let error_bound = 8.0 * f64::EPSILON * self.support as f64 * self.total.abs();
value <= error_bound / 1e-10
}
fn excluding(&self, combo: Combo) -> f64 {
if self.support_excluding(combo) == 0 {
0.0
} else {
self.total - self.card[combo.cards[0].0 as usize] - self.card[combo.cards[1].0 as usize]
}
}
}
fn disjoint(a: Combo, b: Combo) -> bool {
let ([a0, a1], [b0, b1]) = (a.cards, b.cards);
a0 != b0 && a0 != b1 && a1 != b0 && a1 != b1
}
fn live(combo: Combo, board: u64) -> bool {
!mask_has(board, combo.cards[0]) && !mask_has(board, combo.cards[1])
}
impl TerminalIndex {
pub(super) fn new(combos: &[Vec<Combo>; 2]) -> Self {
let lookup: [HashMap<(Card, Card), Vec<usize>>; 2] = std::array::from_fn(|p| {
let mut lookup: HashMap<_, Vec<_>> = HashMap::new();
for (i, combo) in combos[p].iter().enumerate() {
lookup.entry(combo.sorted_cards()).or_default().push(i);
}
lookup
});
Self {
same_pair: std::array::from_fn(|p| {
combos[p]
.iter()
.map(|combo| {
lookup[1 - p]
.get(&combo.sorted_cards())
.cloned()
.unwrap_or_default()
})
.collect()
}),
}
}
pub(super) fn compatible_mass(
&self,
combos: &[Vec<Combo>; 2],
player: usize,
reach: &[f64],
board: u64,
) -> Vec<f64> {
let mut mass = ReachMass::new();
for (&combo, &weight) in combos[1 - player].iter().zip(reach) {
if live(combo, board) {
mass.add(combo, weight);
}
}
combos[player]
.iter()
.enumerate()
.map(|(i, &combo)| {
if live(combo, board) {
let same = &self.same_pair[player][i];
let support = mass.support_excluding(combo)
+ same.iter().filter(|&&j| reach[j] != 0.0).count() as i32;
if support == 0 {
0.0
} else {
let value = mass.total
- mass.card[combo.cards[0].0 as usize]
- mass.card[combo.cards[1].0 as usize]
+ same.iter().map(|&j| reach[j]).sum::<f64>();
if mass.cancellation_prone(value) {
combos[1 - player]
.iter()
.zip(reach)
.filter(|(other, _)| {
live(**other, board) && disjoint(combo, **other)
})
.map(|(_, &weight)| weight)
.sum()
} else {
value
}
}
} else {
0.0
}
})
.collect()
}
#[allow(clippy::too_many_arguments)] pub(super) fn showdown(
&self,
combos: &[Vec<Combo>; 2],
player: usize,
reach: &[f64],
scores: &[Vec<u32>; 2],
order: &BoardOrder,
board: u64,
invested: &[f64; 2],
) -> Vec<f64> {
let opp = 1 - player;
let small = invested[0].abs().min(invested[1].abs());
let large = invested[0].abs().max(invested[1].abs());
if invested[0] != invested[1] && small < large * 1e-8 {
return combos[player]
.iter()
.enumerate()
.map(|(i, &hero)| {
if !live(hero, board) {
return 0.0;
}
order.order[opp]
.iter()
.copied()
.filter(|&j| disjoint(hero, combos[opp][j]))
.map(|j| {
let net = match scores[player][i].cmp(&scores[opp][j]) {
std::cmp::Ordering::Greater => invested[opp],
std::cmp::Ordering::Less => -invested[player],
std::cmp::Ordering::Equal => {
(invested[opp] - invested[player]) / 2.0
}
};
reach[j] * net
})
.sum()
})
.collect();
}
let half_pot = (invested[0] + invested[1]) / 2.0;
let tie_value = (invested[opp] - invested[player]) / 2.0;
let mut values = if tie_value == 0.0 {
vec![0.0; combos[player].len()]
} else {
self.compatible_mass(combos, player, reach, board)
.into_iter()
.map(|mass| mass * tie_value)
.collect()
};
let mut weaker = ReachMass::new();
let mut j = 0;
for (&i, &(end, _)) in order.order[player].iter().zip(&order.rank_bounds[player]) {
while j < end {
let k = order.order[opp][j];
weaker.add(combos[opp][k], reach[k]);
j += 1;
}
let mut mass = weaker.excluding(combos[player][i]);
if weaker.support_excluding(combos[player][i]) > 0 && weaker.cancellation_prone(mass) {
mass = order.order[opp][..j]
.iter()
.copied()
.filter(|&k| disjoint(combos[player][i], combos[opp][k]))
.map(|k| reach[k])
.sum();
}
values[i] += half_pot * mass;
}
let mut stronger = ReachMass::new();
let mut j = order.order[opp].len();
for (&i, &(_, start)) in order.order[player]
.iter()
.zip(&order.rank_bounds[player])
.rev()
{
while j > start {
j -= 1;
let k = order.order[opp][j];
stronger.add(combos[opp][k], reach[k]);
}
let mut mass = stronger.excluding(combos[player][i]);
if stronger.support_excluding(combos[player][i]) > 0
&& stronger.cancellation_prone(mass)
{
mass = order.order[opp][j..]
.iter()
.copied()
.filter(|&k| disjoint(combos[player][i], combos[opp][k]))
.map(|k| reach[k])
.sum();
}
values[i] -= half_pot * mass;
}
values
}
}
#[cfg(test)]
#[path = "terminal_rank_reference.rs"]
mod rank_reference;