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};
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/// A single Weiss Schwarz environment instance with deterministic RNG state.
16pub struct GameEnv {
17    /// Shared card database backing this environment.
18    pub db: Arc<CardDb>,
19    /// Environment configuration (reward, limits, visibility).
20    pub config: EnvConfig,
21    /// Curriculum feature toggles for rules and mechanics.
22    pub curriculum: CurriculumConfig,
23    /// Current game state.
24    pub state: GameState,
25    /// Stable environment identifier within a pool.
26    pub env_id: u32,
27    /// Base seed used to derive per-episode randomness.
28    pub base_seed: u64,
29    /// Episode counter for this environment.
30    pub episode_index: u32,
31    /// Current decision, if the engine is waiting for an action.
32    pub decision: Option<Decision>,
33    pub(crate) action_cache: ActionCache,
34    pub(crate) output_mask_enabled: bool,
35    pub(crate) output_mask_bits_enabled: bool,
36    pub(crate) decision_id: u32,
37    /// Last action applied in canonical form.
38    pub last_action_desc: Option<ActionDesc>,
39    /// Player index that performed the last action.
40    pub last_action_player: Option<u8>,
41    /// Whether the last action was illegal.
42    pub last_illegal_action: bool,
43    /// Whether the last step hit an engine error.
44    pub last_engine_error: bool,
45    /// Error code recorded for the last engine error.
46    pub last_engine_error_code: EngineErrorCode,
47    /// Perspective used for the last emitted observation.
48    pub last_perspective: u8,
49    /// Pending damage deltas awaiting resolution per player.
50    pub pending_damage_delta: [i32; 2],
51    /// Scratch buffer holding the most recent observation.
52    pub obs_buf: Vec<i32>,
53    pub(crate) obs_dirty: bool,
54    pub(crate) obs_perspective: u8,
55    pub(crate) player_obs_version: [u32; 2],
56    pub(crate) player_block_cache_version: [u32; 2],
57    pub(crate) player_block_cache_self: [Vec<i32>; 2],
58    pub(crate) player_block_cache_opp: [Vec<i32>; 2],
59    pub(crate) slot_power_cache: [[i32; crate::encode::MAX_STAGE]; 2],
60    pub(crate) slot_power_dirty: [[bool; crate::encode::MAX_STAGE]; 2],
61    pub(crate) slot_power_cache_card: [[Option<CardId>; crate::encode::MAX_STAGE]; 2],
62    pub(crate) slot_power_cache_mod_turn: [[i32; crate::encode::MAX_STAGE]; 2],
63    pub(crate) slot_power_cache_mod_battle: [[i32; crate::encode::MAX_STAGE]; 2],
64    pub(crate) slot_power_cache_modifiers_version: [[u64; crate::encode::MAX_STAGE]; 2],
65    pub(crate) modifiers_version: u64,
66    pub(crate) rule_actions_dirty: bool,
67    pub(crate) continuous_modifiers_dirty: bool,
68    pub(crate) last_rule_action_phase: Phase,
69    /// Replay configuration controlling sampling and storage.
70    pub replay_config: ReplayConfig,
71    /// Optional writer for replay output.
72    pub replay_writer: Option<ReplayWriter>,
73    /// Canonical action list for replay output.
74    pub replay_actions: Vec<ActionDesc>,
75    /// Raw action list prior to sanitization.
76    pub replay_actions_raw: Vec<ActionDesc>,
77    /// Canonical action ids for replay output.
78    pub replay_action_ids: Vec<u16>,
79    /// Raw action ids prior to sanitization.
80    pub replay_action_ids_raw: Vec<u16>,
81    /// Collected replay events.
82    pub replay_events: Vec<ReplayEvent>,
83    pub(crate) canonical_events: Vec<Event>,
84    /// Per-decision metadata for replay output.
85    pub replay_steps: Vec<StepMeta>,
86    /// Whether replay recording is active.
87    pub recording: bool,
88    /// RNG for non-gameplay metadata sampling.
89    pub meta_rng: Rng64,
90    /// Current episode seed.
91    pub episode_seed: u64,
92    /// Scratch buffer for slot replacement during play/move.
93    pub scratch_replacement_indices: Vec<usize>,
94    pub(crate) scratch: EnvScratch,
95    pub(crate) revealed_to_viewer: [BTreeSet<CardInstanceId>; 2],
96    pub(crate) debug: DebugConfig,
97    pub(crate) debug_event_ring: Option<[super::debug_events::EventRing; 2]>,
98    pub(crate) validate_state_enabled: bool,
99    pub(crate) fault_latched: Option<FaultRecord>,
100}