buckshot_roulette_gameplay_engine/
seat.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use indexmap::IndexMap;
use rand::Rng;

use crate::{
    item::{initialize_item_count_map, Item, NotAdreneline, UnaryItem},
    player_number::PlayerNumber,
    round_player::{RoundPlayer, StunState},
    shell::{Shell, ShotgunDamage},
    LOG_RNG,
};

const MAX_ITEMS: usize = 8;

#[derive(Debug, Clone)]
pub struct Seat {
    player_number: PlayerNumber,
    player: Option<RoundPlayer>,
    items: Vec<Item>,
}

#[derive(Debug, Clone)]
pub struct SeatView {
    pub stun_state: Option<StunState>,
    pub player_number: PlayerNumber,
    pub items: Vec<Item>,
}

#[derive(Debug)]
pub struct OccupiedSeat<'seat> {
    pub player: &'seat mut RoundPlayer,
    pub items: &'seat mut Vec<Item>,
}

impl Seat {
    pub fn new(player: RoundPlayer) -> Self {
        Seat {
            player_number: player.player_number(),
            player: Some(player),
            items: Vec::with_capacity(MAX_ITEMS),
        }
    }

    pub fn player_number(&self) -> PlayerNumber {
        self.player_number
    }

    pub fn player(&self) -> Option<&RoundPlayer> {
        match &self.player {
            Some(player) => Some(player),
            None => None,
        }
    }

    pub fn create_occupied_seat(&mut self) -> Option<OccupiedSeat> {
        match &mut self.player {
            Some(player) => {
                let occupied_seat = OccupiedSeat {
                    player,
                    items: &mut self.items,
                };
                Some(occupied_seat)
            }
            None => None,
        }
    }

    pub fn empty_dead_body(&mut self) {
        let player = self.player.as_ref().unwrap();
        assert!(player.health() == 0);
        self.player = None;
    }

    pub fn items(&self) -> &Vec<Item> {
        &self.items
    }

    pub fn create_view(&self) -> SeatView {
        SeatView {
            stun_state: self.player.as_ref().map(|player| player.stun_state()),
            player_number: self.player_number,
            items: self.items.clone(),
        }
    }

    pub fn get_new_item<TRng>(
        &mut self,
        remaining_players: usize,
        current_table_item_counts: &IndexMap<Item, usize>,
        rng: &mut TRng,
    ) -> Option<Item>
    where
        TRng: Rng,
    {
        self.player.as_ref()?;

        let mut item_pool = Vec::with_capacity(current_table_item_counts.len());

        let mut player_item_counts = initialize_item_count_map();
        self.items.iter().for_each(|item| {
            let count = player_item_counts.get_mut(item).unwrap();
            *count += 1;
        });

        current_table_item_counts.keys().for_each(|item| {
            add_item_to_pool_checked(
                &mut item_pool,
                *item,
                current_table_item_counts,
                &mut player_item_counts,
                || {
                    *item != Item::NotAdreneline(NotAdreneline::UnaryItem(UnaryItem::Remote))
                        || remaining_players > 2
                },
            )
        });

        let index = rng.gen_range(0, item_pool.len());

        let item = item_pool[index];

        if LOG_RNG {
            println!("Player {} grabbed item {}", self.player_number, item);
        }

        self.items.push(item);
        Some(item)
    }
}

impl<'seat> OccupiedSeat<'seat> {
    pub fn shoot(&mut self, shell: Shell, sawn: bool) -> ShotgunDamage {
        if shell.fire() {
            let killed = self.player.take_damage(sawn);
            if sawn {
                ShotgunDamage::SawedShot(killed)
            } else {
                ShotgunDamage::RegularShot(killed)
            }
        } else {
            ShotgunDamage::Blank
        }
    }
}

fn global_item_limit(item: Item) -> usize {
    match item {
        Item::NotAdreneline(not_adreneline) => match not_adreneline {
            NotAdreneline::UnaryItem(unary_item) => match unary_item {
                UnaryItem::Remote => 2,
                UnaryItem::Phone
                | UnaryItem::Inverter
                | UnaryItem::MagnifyingGlass
                | UnaryItem::Cigarettes
                | UnaryItem::Handsaw
                | UnaryItem::Beer => 32,
            },
            NotAdreneline::Jammer => 1,
        },
        Item::Adreneline => 32,
    }
}

fn add_item_to_pool_checked<F>(
    pool: &mut Vec<Item>,
    item: Item,
    current_table_item_counts: &IndexMap<Item, usize>,
    player_item_counts: &mut IndexMap<Item, usize>,
    additional_check: F,
) where
    F: FnOnce() -> bool,
{
    let player_item_limit = player_item_limit(item);
    let current_count = player_item_counts.get(&item);
    if player_item_limit <= *current_count.unwrap() {
        return;
    }

    let global_item_limit = global_item_limit(item);
    let global_count = current_table_item_counts.get(&item);
    if global_item_limit <= *global_count.unwrap() {
        return;
    }

    if !additional_check() {
        return;
    }

    pool.push(item);
}

fn player_item_limit(item: Item) -> usize {
    match item {
        Item::NotAdreneline(not_adreneline) => match not_adreneline {
            NotAdreneline::UnaryItem(unary_item) => match unary_item {
                UnaryItem::Remote | UnaryItem::Cigarettes => 1,
                UnaryItem::MagnifyingGlass | UnaryItem::Handsaw => 2,
                UnaryItem::Inverter => 4,
                UnaryItem::Phone | UnaryItem::Beer => 8,
            },
            NotAdreneline::Jammer => 1,
        },
        Item::Adreneline => 4,
    }
}