issun-core 0.10.0

Core domain models and traits for ISSUN game engine
Documentation
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//! The core TemporalMechanic implementation.

use std::marker::PhantomData;

use crate::mechanics::{EventEmitter, Mechanic, ParallelSafe};

use super::policies::TemporalPolicy;
use super::strategies::StandardTemporalPolicy;
use super::types::{
    ActionBudget, ActionCost, TemporalConfig, TemporalEvent, TemporalInput, TemporalState,
};

/// The core temporal mechanic for time and action budget management.
///
/// # Type Parameters
///
/// - `P`: Temporal policy (determines cost calculation, reset behavior, etc.)
///
/// # Overview
///
/// The temporal mechanic handles:
/// - **Time Tracking**: Current tick/datetime, time progression
/// - **Action Budgets**: Discrete points or continuous energy
/// - **Cost Calculation**: Policy-driven action costs with modifiers
/// - **Budget Reset**: Day-based, tick-based, or continuous regeneration
/// - **Time-Based Effects**: Time of day and season modifiers
///
/// # Supported Systems
///
/// - **Turn-Based RPG**: Discrete actions per day/turn
/// - **Real-Time RPG**: Stamina/energy with regeneration
/// - **Strategy Games**: Variable costs, season effects
/// - **Persona-Style**: Calendar-based with limited daily actions
///
/// # Examples
///
/// ```
/// use issun_core::mechanics::temporal::prelude::*;
/// use issun_core::mechanics::{Mechanic, EventEmitter};
///
/// // Use default policy
/// type GameTemporal = TemporalMechanic;
///
/// // Create configuration
/// let config = TemporalConfig::turn_based(3); // 3 actions per day
/// let mut state = TemporalState::with_points(3);
///
/// // Request an action
/// let input = TemporalInput {
///     current_time: TemporalPoint::DateTime(GameDateTime::new(1, 1, 1, 10, 0)),
///     requested_action: Some(ActionRequest::new("move")),
///     ..Default::default()
/// };
///
/// // Event collector
/// # struct VecEmitter(Vec<TemporalEvent>);
/// # impl EventEmitter<TemporalEvent> for VecEmitter {
/// #     fn emit(&mut self, event: TemporalEvent) { self.0.push(event); }
/// # }
/// let mut emitter = VecEmitter(vec![]);
///
/// // Execute
/// GameTemporal::step(&config, &mut state, input, &mut emitter);
///
/// // Check result
/// if let ActionBudget::Points(ap) = &state.budget {
///     assert_eq!(ap.available, 2); // Used 1 action
/// }
/// ```
pub struct TemporalMechanic<P: TemporalPolicy = StandardTemporalPolicy> {
    _marker: PhantomData<P>,
}

impl<P> Mechanic for TemporalMechanic<P>
where
    P: TemporalPolicy,
{
    type Config = TemporalConfig;
    type State = TemporalState;
    type Input = TemporalInput;
    type Event = TemporalEvent;

    // Temporal is per-entity and doesn't need cross-entity access
    type Execution = ParallelSafe;

    fn step(
        config: &Self::Config,
        state: &mut Self::State,
        input: Self::Input,
        emitter: &mut impl EventEmitter<Self::Event>,
    ) {
        // 1. Handle time-based regeneration (for real-time systems)
        if let Some(delta_seconds) = input.delta_seconds {
            let old_current = match &state.budget {
                ActionBudget::Energy(e) => e.current,
                _ => 0.0,
            };

            state.budget.regenerate(delta_seconds);

            // Emit regen event if energy changed
            if let ActionBudget::Energy(e) = &state.budget {
                if (e.current - old_current).abs() > 0.01 {
                    emitter.emit(TemporalEvent::EnergyRegenerated {
                        amount: e.current - old_current,
                        current: e.current,
                    });
                }
            }
        }

        // 2. Check for time-based reset
        let should_reset = P::should_reset(config, &state.last_reset_time, &input.current_time);

        if should_reset {
            state.budget.reset();
            state.last_reset_time = input.current_time.clone();
            state.actions_this_period = 0;

            let new_available = match &state.budget {
                ActionBudget::Points(ap) => ap.available,
                ActionBudget::Energy(ae) => ae.current as u32,
            };

            emitter.emit(TemporalEvent::BudgetReset { new_available });

            // Check for day change event
            let old_dt = state.last_action_time.to_datetime(&config.calendar);
            let new_dt = input.current_time.to_datetime(&config.calendar);
            if !old_dt.is_same_day(&new_dt) {
                emitter.emit(TemporalEvent::DayChanged {
                    new_day: new_dt.day,
                    new_month: new_dt.month,
                    new_year: new_dt.year,
                });
            }
        }

        // 3. Handle action request
        if let Some(action_request) = &input.requested_action {
            // Calculate cost
            let cost = if let Some(override_cost) = action_request.override_cost {
                ActionCost::new(override_cost)
            } else {
                P::calculate_cost(config, &action_request.action_type, &input.actor_context)
            };

            // Apply time-based modifiers
            let datetime = input.current_time.to_datetime(&config.calendar);
            let time_modifier = P::time_of_day_modifier(config, &datetime);
            let season = P::get_season(config, &datetime);
            let season_modifier = P::season_modifier(config, season);
            let final_cost = cost.apply_modifier(time_modifier * season_modifier);

            // Attempt to consume
            match state.budget.consume(&final_cost) {
                Ok(result) => {
                    state.last_action_time = input.current_time.clone();
                    state.actions_this_period += 1;

                    emitter.emit(TemporalEvent::ActionConsumed {
                        action_type: action_request.action_type.clone(),
                        cost: final_cost,
                        remaining: result.remaining,
                    });

                    if result.depleted {
                        emitter.emit(TemporalEvent::BudgetDepleted);
                    }
                }
                Err(reason) => {
                    emitter.emit(TemporalEvent::ActionRejected {
                        action_type: action_request.action_type.clone(),
                        reason,
                    });
                }
            }
        }

        // 4. Emit time advancement event if time changed
        if state.last_action_time != input.current_time {
            emitter.emit(TemporalEvent::TimeAdvanced {
                from: state.last_action_time.clone(),
                to: input.current_time.clone(),
            });
        }
    }
}

/// Type alias for simple temporal mechanic using default policy.
pub type SimpleTemporalMechanic = TemporalMechanic<StandardTemporalPolicy>;

#[cfg(test)]
mod tests {
    use super::*;
    use crate::mechanics::temporal::types::{
        ActionBudget, ActionRequest, ActorContext, GameDateTime, TemporalPoint,
    };

    struct VecEmitter {
        events: Vec<TemporalEvent>,
    }

    impl EventEmitter<TemporalEvent> for VecEmitter {
        fn emit(&mut self, event: TemporalEvent) {
            self.events.push(event);
        }
    }

    #[test]
    fn test_action_consumption() {
        let config = TemporalConfig::turn_based(3);
        let mut state = TemporalState::with_points(3);

        let input = TemporalInput {
            current_time: TemporalPoint::DateTime(GameDateTime::new(1, 1, 1, 10, 0)),
            requested_action: Some(ActionRequest::new("move")),
            ..Default::default()
        };

        let mut emitter = VecEmitter { events: vec![] };
        SimpleTemporalMechanic::step(&config, &mut state, input, &mut emitter);

        // Should have consumed 1 action
        if let ActionBudget::Points(ap) = &state.budget {
            assert_eq!(ap.available, 2);
        } else {
            panic!("Expected ActionPoints");
        }

        // Should have emitted ActionConsumed
        let consumed = emitter
            .events
            .iter()
            .any(|e| matches!(e, TemporalEvent::ActionConsumed { .. }));
        assert!(consumed);
    }

    #[test]
    fn test_budget_depletion() {
        let config = TemporalConfig::turn_based(2);
        let mut state = TemporalState::with_points(2);

        let mut emitter = VecEmitter { events: vec![] };

        // Use first action
        let input1 = TemporalInput {
            current_time: TemporalPoint::DateTime(GameDateTime::new(1, 1, 1, 10, 0)),
            requested_action: Some(ActionRequest::new("move")),
            ..Default::default()
        };
        SimpleTemporalMechanic::step(&config, &mut state, input1, &mut emitter);

        // Use second action
        let input2 = TemporalInput {
            current_time: TemporalPoint::DateTime(GameDateTime::new(1, 1, 1, 10, 30)),
            requested_action: Some(ActionRequest::new("attack")),
            ..Default::default()
        };
        SimpleTemporalMechanic::step(&config, &mut state, input2, &mut emitter);

        // Should be depleted
        if let ActionBudget::Points(ap) = &state.budget {
            assert!(ap.is_depleted());
        }

        // Should have emitted BudgetDepleted
        let depleted = emitter
            .events
            .iter()
            .any(|e| matches!(e, TemporalEvent::BudgetDepleted));
        assert!(depleted);
    }

    #[test]
    fn test_action_rejection() {
        let config = TemporalConfig::turn_based(1);
        let mut state = TemporalState::with_points(1);

        // Use the only action
        let input1 = TemporalInput {
            current_time: TemporalPoint::DateTime(GameDateTime::new(1, 1, 1, 10, 0)),
            requested_action: Some(ActionRequest::new("move")),
            ..Default::default()
        };

        let mut emitter = VecEmitter { events: vec![] };
        SimpleTemporalMechanic::step(&config, &mut state, input1, &mut emitter);

        // Try to use another action
        let input2 = TemporalInput {
            current_time: TemporalPoint::DateTime(GameDateTime::new(1, 1, 1, 10, 30)),
            requested_action: Some(ActionRequest::new("attack")),
            ..Default::default()
        };
        SimpleTemporalMechanic::step(&config, &mut state, input2, &mut emitter);

        // Should have emitted ActionRejected
        let rejected = emitter
            .events
            .iter()
            .any(|e| matches!(e, TemporalEvent::ActionRejected { .. }));
        assert!(rejected);
    }

    #[test]
    fn test_day_change_reset() {
        let config = TemporalConfig::turn_based(3);
        let mut state = TemporalState::with_points(3);
        state.last_reset_time = TemporalPoint::DateTime(GameDateTime::new(1, 1, 1, 0, 0));
        state.last_action_time = TemporalPoint::DateTime(GameDateTime::new(1, 1, 1, 20, 0));

        // Use 2 actions on day 1
        let input1 = TemporalInput {
            current_time: TemporalPoint::DateTime(GameDateTime::new(1, 1, 1, 10, 0)),
            requested_action: Some(ActionRequest::new("move")),
            ..Default::default()
        };

        let mut emitter = VecEmitter { events: vec![] };
        SimpleTemporalMechanic::step(&config, &mut state, input1, &mut emitter);

        let input2 = TemporalInput {
            current_time: TemporalPoint::DateTime(GameDateTime::new(1, 1, 1, 12, 0)),
            requested_action: Some(ActionRequest::new("talk")),
            ..Default::default()
        };
        SimpleTemporalMechanic::step(&config, &mut state, input2, &mut emitter);

        if let ActionBudget::Points(ap) = &state.budget {
            assert_eq!(ap.available, 1);
        }

        // Advance to day 2
        let input3 = TemporalInput {
            current_time: TemporalPoint::DateTime(GameDateTime::new(1, 1, 2, 8, 0)),
            requested_action: None,
            ..Default::default()
        };
        SimpleTemporalMechanic::step(&config, &mut state, input3, &mut emitter);

        // Should have reset
        if let ActionBudget::Points(ap) = &state.budget {
            assert_eq!(ap.available, 3);
        }

        // Should have emitted BudgetReset and DayChanged
        let reset = emitter
            .events
            .iter()
            .any(|e| matches!(e, TemporalEvent::BudgetReset { .. }));
        let day_changed = emitter
            .events
            .iter()
            .any(|e| matches!(e, TemporalEvent::DayChanged { .. }));
        assert!(reset);
        assert!(day_changed);
    }

    #[test]
    fn test_energy_regeneration() {
        let config = TemporalConfig::real_time(100.0, 10.0); // 10 energy/sec
        let mut state = TemporalState::with_energy(100.0, 10.0);

        // Consume some energy
        if let ActionBudget::Energy(e) = &mut state.budget {
            e.consume(50.0).unwrap();
        }

        // Regenerate over 2 seconds
        let input = TemporalInput {
            current_time: TemporalPoint::Tick(100),
            delta_seconds: Some(2.0),
            requested_action: None,
            ..Default::default()
        };

        let mut emitter = VecEmitter { events: vec![] };
        SimpleTemporalMechanic::step(&config, &mut state, input, &mut emitter);

        // Should have regenerated 20 energy
        if let ActionBudget::Energy(e) = &state.budget {
            assert!((e.current - 70.0).abs() < 0.1);
        }

        // Should have emitted EnergyRegenerated
        let regen = emitter
            .events
            .iter()
            .any(|e| matches!(e, TemporalEvent::EnergyRegenerated { .. }));
        assert!(regen);
    }

    #[test]
    fn test_skill_reduces_cost() {
        let config = TemporalConfig::turn_based(3).with_action_cost("expensive", 2);
        let mut state = TemporalState::with_points(3);

        // Skilled actor
        let input = TemporalInput {
            current_time: TemporalPoint::DateTime(GameDateTime::new(1, 1, 1, 10, 0)),
            requested_action: Some(ActionRequest::new("expensive")),
            actor_context: ActorContext {
                skill_level: 1.0, // High skill
                efficiency_bonus: 0.0,
                temporary_modifiers: Default::default(),
            },
            ..Default::default()
        };

        let mut emitter = VecEmitter { events: vec![] };
        SimpleTemporalMechanic::step(&config, &mut state, input, &mut emitter);

        // With skill, cost should be reduced from 2
        if let ActionBudget::Points(ap) = &state.budget {
            // Base cost 2, with skill modifier should be less
            assert!(ap.available >= 1); // At least 1 remaining (cost <= 2)
        }
    }

    #[test]
    fn test_override_cost() {
        let config = TemporalConfig::turn_based(5);
        let mut state = TemporalState::with_points(5);

        // Override cost to 3
        let input = TemporalInput {
            current_time: TemporalPoint::DateTime(GameDateTime::new(1, 1, 1, 10, 0)),
            requested_action: Some(ActionRequest::with_cost("special", 3)),
            ..Default::default()
        };

        let mut emitter = VecEmitter { events: vec![] };
        SimpleTemporalMechanic::step(&config, &mut state, input, &mut emitter);

        // Should have used exactly 3
        if let ActionBudget::Points(ap) = &state.budget {
            assert_eq!(ap.available, 2);
        }
    }

    #[test]
    fn test_actions_this_period_tracking() {
        let config = TemporalConfig::turn_based(5);
        let mut state = TemporalState::with_points(5);

        let mut emitter = VecEmitter { events: vec![] };

        for i in 0..3 {
            let input = TemporalInput {
                current_time: TemporalPoint::DateTime(GameDateTime::new(1, 1, 1, 10 + i, 0)),
                requested_action: Some(ActionRequest::new("action")),
                ..Default::default()
            };
            SimpleTemporalMechanic::step(&config, &mut state, input, &mut emitter);
        }

        assert_eq!(state.actions_this_period, 3);
    }
}