Skip to main content

weiss_core/env/
core.rs

1use std::collections::BTreeSet;
2use std::sync::Arc;
3
4use crate::config::{CurriculumConfig, EnvConfig};
5use crate::db::{CardDb, CardId};
6use crate::events::Event;
7use crate::legal::{ActionDesc, Decision, DecisionKind};
8use crate::replay::{ReplayConfig, ReplayEvent, ReplayWriter, StepMeta};
9use crate::state::{CardInstanceId, GameState, Phase};
10use crate::util::Rng64;
11
12use super::cache::{ActionCache, EnvScratch};
13use super::types::{DebugConfig, EngineErrorCode, FaultRecord};
14
15#[derive(Clone, Copy, Debug, PartialEq, Eq)]
16pub(crate) struct ProgressSignature {
17    pub active_player: u8,
18    pub turn_number: u32,
19    pub phase: Phase,
20    pub choice_id: Option<u32>,
21    pub choice_page_start: u16,
22    pub choice_total_candidates: u16,
23    pub deck_counts: [u16; 2],
24    pub hand_counts: [u16; 2],
25    pub waiting_room_counts: [u16; 2],
26    pub clock_counts: [u16; 2],
27    pub level_counts: [u16; 2],
28    pub stock_counts: [u16; 2],
29    pub memory_counts: [u16; 2],
30    pub climax_counts: [u16; 2],
31    pub resolution_counts: [u16; 2],
32    pub occupied_stage_counts: [u16; 2],
33    pub reversed_stage_counts: [u16; 2],
34    pub live_stage_counts: [u16; 2],
35}
36
37impl Default for ProgressSignature {
38    fn default() -> Self {
39        Self {
40            active_player: 0,
41            turn_number: 0,
42            phase: Phase::Mulligan,
43            choice_id: None,
44            choice_page_start: 0,
45            choice_total_candidates: 0,
46            deck_counts: [0; 2],
47            hand_counts: [0; 2],
48            waiting_room_counts: [0; 2],
49            clock_counts: [0; 2],
50            level_counts: [0; 2],
51            stock_counts: [0; 2],
52            memory_counts: [0; 2],
53            climax_counts: [0; 2],
54            resolution_counts: [0; 2],
55            occupied_stage_counts: [0; 2],
56            reversed_stage_counts: [0; 2],
57            live_stage_counts: [0; 2],
58        }
59    }
60}
61
62/// A single Weiss Schwarz environment instance with deterministic RNG state.
63pub struct GameEnv {
64    /// Shared card database backing this environment.
65    pub db: Arc<CardDb>,
66    /// Environment configuration (reward, limits, visibility).
67    pub config: EnvConfig,
68    /// Curriculum feature toggles for rules and mechanics.
69    pub curriculum: CurriculumConfig,
70    /// Current game state.
71    pub state: GameState,
72    /// Stable environment identifier within a pool.
73    pub env_id: u32,
74    /// Base seed used to derive per-episode randomness.
75    pub base_seed: u64,
76    /// Episode counter for this environment.
77    pub episode_index: u32,
78    /// Current decision, if the engine is waiting for an action.
79    pub decision: Option<Decision>,
80    pub(crate) action_cache: ActionCache,
81    pub(crate) output_mask_enabled: bool,
82    pub(crate) output_mask_bits_enabled: bool,
83    pub(crate) decision_id: u32,
84    /// Last action applied in canonical form.
85    pub last_action_desc: Option<ActionDesc>,
86    /// Player index that performed the last action.
87    pub last_action_player: Option<u8>,
88    /// Decision kind associated with the last applied action.
89    pub(crate) last_action_decision_kind: Option<DecisionKind>,
90    /// Whether the last action was illegal.
91    pub last_illegal_action: bool,
92    /// Whether the last step hit an engine error.
93    pub last_engine_error: bool,
94    /// Error code recorded for the last engine error.
95    pub last_engine_error_code: EngineErrorCode,
96    /// Perspective used for the last emitted observation.
97    pub last_perspective: u8,
98    /// Pending damage deltas awaiting resolution per player.
99    pub pending_damage_delta: [i32; 2],
100    /// Consecutive decisions without a meaningful progress signature change.
101    pub no_progress_decisions: u32,
102    /// Scratch buffer holding the most recent observation.
103    pub obs_buf: Vec<i32>,
104    pub(crate) obs_dirty: bool,
105    pub(crate) obs_perspective: u8,
106    pub(crate) player_obs_version: [u32; 2],
107    pub(crate) player_block_cache_version: [u32; 2],
108    pub(crate) player_block_cache_self: [Vec<i32>; 2],
109    pub(crate) player_block_cache_opp: [Vec<i32>; 2],
110    pub(crate) slot_power_cache: [[i32; crate::encode::MAX_STAGE]; 2],
111    pub(crate) slot_power_dirty: [[bool; crate::encode::MAX_STAGE]; 2],
112    pub(crate) slot_power_cache_card: [[Option<CardId>; crate::encode::MAX_STAGE]; 2],
113    pub(crate) slot_power_cache_mod_turn: [[i32; crate::encode::MAX_STAGE]; 2],
114    pub(crate) slot_power_cache_mod_battle: [[i32; crate::encode::MAX_STAGE]; 2],
115    pub(crate) slot_power_cache_modifiers_version: [[u64; crate::encode::MAX_STAGE]; 2],
116    pub(crate) modifiers_version: u64,
117    pub(crate) rule_actions_dirty: bool,
118    pub(crate) continuous_modifiers_dirty: bool,
119    pub(crate) last_rule_action_phase: Phase,
120    /// Replay configuration controlling sampling and storage.
121    pub replay_config: ReplayConfig,
122    /// Optional writer for replay output.
123    pub replay_writer: Option<ReplayWriter>,
124    /// Canonical action list for replay output.
125    pub replay_actions: Vec<ActionDesc>,
126    /// Raw action list prior to sanitization.
127    pub replay_actions_raw: Vec<ActionDesc>,
128    /// Canonical action ids for replay output.
129    pub replay_action_ids: Vec<u16>,
130    /// Raw action ids prior to sanitization.
131    pub replay_action_ids_raw: Vec<u16>,
132    /// Collected replay events.
133    pub replay_events: Vec<ReplayEvent>,
134    pub(crate) canonical_events: Vec<Event>,
135    /// Per-decision metadata for replay output.
136    pub replay_steps: Vec<StepMeta>,
137    /// Whether replay recording is active.
138    pub recording: bool,
139    /// RNG for non-gameplay metadata sampling.
140    pub meta_rng: Rng64,
141    /// Current episode seed.
142    pub episode_seed: u64,
143    /// Scratch buffer for slot replacement during play/move.
144    pub scratch_replacement_indices: Vec<usize>,
145    pub(crate) scratch: EnvScratch,
146    pub(crate) revealed_to_viewer: [BTreeSet<CardInstanceId>; 2],
147    pub(crate) debug: DebugConfig,
148    pub(crate) debug_event_ring: Option<[super::debug_events::EventRing; 2]>,
149    pub(crate) validate_state_enabled: bool,
150    pub(crate) fault_latched: Option<FaultRecord>,
151}