behaviorsim-rs 0.7.0

Domain-agnostic specification for modeling individual psychology and social dynamics
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
//! Directional (asymmetric) relationship dimensions.
//!
//! These dimensions may differ from one entity's perspective to another.
//! A's warmth toward B may differ from B's warmth toward A.

use crate::state::StateValue;
use crate::types::Duration;

/// Default decay half-life for directional dimensions (14 days).
const DEFAULT_DECAY_HALF_LIFE: Duration = Duration::days(14);

/// Directional dimensions from one entity toward another.
///
/// These are asymmetric - A's feelings toward B may differ from
/// B's feelings toward A.
///
#[derive(Debug, Clone, PartialEq)]
pub struct DirectionalDimensions {
    /// Positive feeling toward the other entity.
    /// Range: 0 (cold) to 1 (warm)
    warmth: StateValue,

    /// Negative feeling toward the other entity.
    /// Range: 0 (no resentment) to 1 (high resentment)
    resentment: StateValue,

    /// Reliance on the other entity.
    /// Range: 0 (independent) to 1 (highly dependent)
    dependence: StateValue,

    /// Romantic or sexual interest.
    /// Range: 0 (none) to 1 (high attraction)
    attraction: StateValue,

    /// Emotional bonding and fear of loss.
    /// Range: 0 (detached) to 1 (strongly attached)
    attachment: StateValue,

    /// Possessiveness and rivalry.
    /// Range: 0 (none) to 1 (high jealousy)
    jealousy: StateValue,

    /// Threat perception.
    /// Range: 0 (no fear) to 1 (high fear)
    fear: StateValue,

    /// Sense of duty or authority pressure.
    /// Range: 0 (no obligation) to 1 (strong obligation)
    obligation: StateValue,
}

impl DirectionalDimensions {
    /// Creates new DirectionalDimensions with default values.
    ///
    /// Defaults represent a neutral starting point with slight warmth.
    #[must_use]
    pub fn new() -> Self {
        DirectionalDimensions {
            warmth: StateValue::new(0.2)
                .with_bounds(0.0, 1.0)
                .with_decay_half_life(DEFAULT_DECAY_HALF_LIFE),
            resentment: StateValue::new(0.0)
                .with_bounds(0.0, 1.0)
                .with_decay_half_life(DEFAULT_DECAY_HALF_LIFE),
            dependence: StateValue::new(0.0)
                .with_bounds(0.0, 1.0)
                .with_decay_half_life(DEFAULT_DECAY_HALF_LIFE),
            attraction: StateValue::new(0.0)
                .with_bounds(0.0, 1.0)
                .with_decay_half_life(DEFAULT_DECAY_HALF_LIFE),
            attachment: StateValue::new(0.0)
                .with_bounds(0.0, 1.0)
                .with_decay_half_life(Duration::days(30)), // Attachment decays slower
            jealousy: StateValue::new(0.0)
                .with_bounds(0.0, 1.0)
                .with_decay_half_life(Duration::days(7)), // Jealousy decays faster
            fear: StateValue::new(0.0)
                .with_bounds(0.0, 1.0)
                .with_decay_half_life(Duration::days(7)), // Fear decays faster
            obligation: StateValue::new(0.0)
                .with_bounds(0.0, 1.0)
                .with_decay_half_life(Duration::days(30)), // Obligation is more stable
        }
    }

    // Effective value accessors

    /// Returns the effective warmth (base + delta).
    #[must_use]
    pub fn warmth_effective(&self) -> f32 {
        self.warmth.effective()
    }

    /// Returns the effective resentment (base + delta).
    #[must_use]
    pub fn resentment_effective(&self) -> f32 {
        self.resentment.effective()
    }

    /// Returns the effective dependence (base + delta).
    #[must_use]
    pub fn dependence_effective(&self) -> f32 {
        self.dependence.effective()
    }

    /// Returns the effective attraction (base + delta).
    #[must_use]
    pub fn attraction_effective(&self) -> f32 {
        self.attraction.effective()
    }

    /// Returns the effective attachment (base + delta).
    #[must_use]
    pub fn attachment_effective(&self) -> f32 {
        self.attachment.effective()
    }

    /// Returns the effective jealousy (base + delta).
    #[must_use]
    pub fn jealousy_effective(&self) -> f32 {
        self.jealousy.effective()
    }

    /// Returns the effective fear (base + delta).
    #[must_use]
    pub fn fear_effective(&self) -> f32 {
        self.fear.effective()
    }

    /// Returns the effective obligation (base + delta).
    #[must_use]
    pub fn obligation_effective(&self) -> f32 {
        self.obligation.effective()
    }

    // StateValue references

    /// Returns a reference to the warmth StateValue.
    #[must_use]
    pub fn warmth(&self) -> &StateValue {
        &self.warmth
    }

    /// Returns a reference to the resentment StateValue.
    #[must_use]
    pub fn resentment(&self) -> &StateValue {
        &self.resentment
    }

    /// Returns a reference to the dependence StateValue.
    #[must_use]
    pub fn dependence(&self) -> &StateValue {
        &self.dependence
    }

    /// Returns a reference to the attraction StateValue.
    #[must_use]
    pub fn attraction(&self) -> &StateValue {
        &self.attraction
    }

    /// Returns a reference to the attachment StateValue.
    #[must_use]
    pub fn attachment(&self) -> &StateValue {
        &self.attachment
    }

    /// Returns a reference to the jealousy StateValue.
    #[must_use]
    pub fn jealousy(&self) -> &StateValue {
        &self.jealousy
    }

    /// Returns a reference to the fear StateValue.
    #[must_use]
    pub fn fear(&self) -> &StateValue {
        &self.fear
    }

    /// Returns a reference to the obligation StateValue.
    #[must_use]
    pub fn obligation(&self) -> &StateValue {
        &self.obligation
    }

    // Mutable references

    /// Returns a mutable reference to the warmth StateValue.
    pub fn warmth_mut(&mut self) -> &mut StateValue {
        &mut self.warmth
    }

    /// Returns a mutable reference to the resentment StateValue.
    pub fn resentment_mut(&mut self) -> &mut StateValue {
        &mut self.resentment
    }

    /// Returns a mutable reference to the dependence StateValue.
    pub fn dependence_mut(&mut self) -> &mut StateValue {
        &mut self.dependence
    }

    /// Returns a mutable reference to the attraction StateValue.
    pub fn attraction_mut(&mut self) -> &mut StateValue {
        &mut self.attraction
    }

    /// Returns a mutable reference to the attachment StateValue.
    pub fn attachment_mut(&mut self) -> &mut StateValue {
        &mut self.attachment
    }

    /// Returns a mutable reference to the jealousy StateValue.
    pub fn jealousy_mut(&mut self) -> &mut StateValue {
        &mut self.jealousy
    }

    /// Returns a mutable reference to the fear StateValue.
    pub fn fear_mut(&mut self) -> &mut StateValue {
        &mut self.fear
    }

    /// Returns a mutable reference to the obligation StateValue.
    pub fn obligation_mut(&mut self) -> &mut StateValue {
        &mut self.obligation
    }

    // Delta modifiers

    /// Adds to the warmth delta.
    pub fn add_warmth_delta(&mut self, amount: f32) {
        self.warmth.add_delta(amount);
    }

    /// Adds to the resentment delta.
    pub fn add_resentment_delta(&mut self, amount: f32) {
        self.resentment.add_delta(amount);
    }

    /// Adds to the dependence delta.
    pub fn add_dependence_delta(&mut self, amount: f32) {
        self.dependence.add_delta(amount);
    }

    /// Adds to the attraction delta.
    pub fn add_attraction_delta(&mut self, amount: f32) {
        self.attraction.add_delta(amount);
    }

    /// Adds to the attachment delta.
    pub fn add_attachment_delta(&mut self, amount: f32) {
        self.attachment.add_delta(amount);
    }

    /// Adds to the jealousy delta.
    pub fn add_jealousy_delta(&mut self, amount: f32) {
        self.jealousy.add_delta(amount);
    }

    /// Adds to the fear delta.
    pub fn add_fear_delta(&mut self, amount: f32) {
        self.fear.add_delta(amount);
    }

    /// Adds to the obligation delta.
    pub fn add_obligation_delta(&mut self, amount: f32) {
        self.obligation.add_delta(amount);
    }

    // Decay

    /// Applies decay to all directional dimensions over the specified duration.
    pub fn apply_decay(&mut self, elapsed: Duration) {
        self.warmth.apply_decay(elapsed);
        self.resentment.apply_decay(elapsed);
        self.dependence.apply_decay(elapsed);
        self.attraction.apply_decay(elapsed);
        self.attachment.apply_decay(elapsed);
        self.jealousy.apply_decay(elapsed);
        self.fear.apply_decay(elapsed);
        self.obligation.apply_decay(elapsed);
    }

    /// Resets all deltas to zero.
    pub fn reset_deltas(&mut self) {
        self.warmth.reset_delta();
        self.resentment.reset_delta();
        self.dependence.reset_delta();
        self.attraction.reset_delta();
        self.attachment.reset_delta();
        self.jealousy.reset_delta();
        self.fear.reset_delta();
        self.obligation.reset_delta();
    }
}

impl Default for DirectionalDimensions {
    fn default() -> Self {
        DirectionalDimensions::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn new_creates_default_values() {
        let dims = DirectionalDimensions::new();
        assert!((dims.warmth_effective() - 0.2).abs() < f32::EPSILON);
        assert!(dims.resentment_effective().abs() < f32::EPSILON);
        assert!(dims.dependence_effective().abs() < f32::EPSILON);
        assert!(dims.attraction_effective().abs() < f32::EPSILON);
        assert!(dims.attachment_effective().abs() < f32::EPSILON);
        assert!(dims.jealousy_effective().abs() < f32::EPSILON);
        assert!(dims.fear_effective().abs() < f32::EPSILON);
        assert!(dims.obligation_effective().abs() < f32::EPSILON);
    }

    #[test]
    fn add_warmth_delta() {
        let mut dims = DirectionalDimensions::new();
        dims.add_warmth_delta(0.3);
        assert!((dims.warmth().delta() - 0.3).abs() < f32::EPSILON);
    }

    #[test]
    fn add_resentment_delta() {
        let mut dims = DirectionalDimensions::new();
        dims.add_resentment_delta(0.4);
        assert!((dims.resentment().delta() - 0.4).abs() < f32::EPSILON);
    }

    #[test]
    fn add_dependence_delta() {
        let mut dims = DirectionalDimensions::new();
        dims.add_dependence_delta(0.2);
        assert!((dims.dependence().delta() - 0.2).abs() < f32::EPSILON);
    }

    #[test]
    fn add_attraction_delta() {
        let mut dims = DirectionalDimensions::new();
        dims.add_attraction_delta(0.5);
        assert!((dims.attraction().delta() - 0.5).abs() < f32::EPSILON);
    }

    #[test]
    fn add_attachment_delta() {
        let mut dims = DirectionalDimensions::new();
        dims.add_attachment_delta(0.3);
        assert!((dims.attachment().delta() - 0.3).abs() < f32::EPSILON);
    }

    #[test]
    fn add_jealousy_delta() {
        let mut dims = DirectionalDimensions::new();
        dims.add_jealousy_delta(0.2);
        assert!((dims.jealousy().delta() - 0.2).abs() < f32::EPSILON);
    }

    #[test]
    fn add_fear_delta() {
        let mut dims = DirectionalDimensions::new();
        dims.add_fear_delta(0.4);
        assert!((dims.fear().delta() - 0.4).abs() < f32::EPSILON);
    }

    #[test]
    fn add_obligation_delta() {
        let mut dims = DirectionalDimensions::new();
        dims.add_obligation_delta(0.3);
        assert!((dims.obligation().delta() - 0.3).abs() < f32::EPSILON);
    }

    #[test]
    fn warmth_decays_over_14_days() {
        let mut dims = DirectionalDimensions::new();
        dims.add_warmth_delta(0.4);
        dims.apply_decay(Duration::days(14));
        assert!((dims.warmth().delta() - 0.2).abs() < 0.01);
    }

    #[test]
    fn fear_decays_over_7_days() {
        let mut dims = DirectionalDimensions::new();
        dims.add_fear_delta(0.4);
        dims.apply_decay(Duration::days(7));
        assert!((dims.fear().delta() - 0.2).abs() < 0.01);
    }

    #[test]
    fn jealousy_decays_over_7_days() {
        let mut dims = DirectionalDimensions::new();
        dims.add_jealousy_delta(0.4);
        dims.apply_decay(Duration::days(7));
        assert!((dims.jealousy().delta() - 0.2).abs() < 0.01);
    }

    #[test]
    fn attachment_decays_over_30_days() {
        let mut dims = DirectionalDimensions::new();
        dims.add_attachment_delta(0.4);
        dims.apply_decay(Duration::days(30));
        assert!((dims.attachment().delta() - 0.2).abs() < 0.01);
    }

    #[test]
    fn obligation_decays_over_30_days() {
        let mut dims = DirectionalDimensions::new();
        dims.add_obligation_delta(0.4);
        dims.apply_decay(Duration::days(30));
        assert!((dims.obligation().delta() - 0.2).abs() < 0.01);
    }

    #[test]
    fn fear_decays_faster_than_warmth() {
        let mut dims = DirectionalDimensions::new();
        dims.add_fear_delta(0.4);
        dims.add_warmth_delta(0.4);

        dims.apply_decay(Duration::days(7));

        // Fear should have decayed more (half-life reached)
        assert!(dims.fear().delta() < dims.warmth().delta());
    }

    #[test]
    fn reset_deltas() {
        let mut dims = DirectionalDimensions::new();
        dims.add_warmth_delta(0.3);
        dims.add_fear_delta(0.2);

        dims.reset_deltas();

        assert!(dims.warmth().delta().abs() < f32::EPSILON);
        assert!(dims.fear().delta().abs() < f32::EPSILON);
    }

    #[test]
    fn mutable_references_work() {
        let mut dims = DirectionalDimensions::new();
        dims.warmth_mut().add_delta(0.1);
        dims.resentment_mut().add_delta(0.2);
        dims.dependence_mut().add_delta(0.3);
        dims.attraction_mut().add_delta(0.4);
        dims.attachment_mut().add_delta(0.5);
        dims.jealousy_mut().add_delta(0.1);
        dims.fear_mut().add_delta(0.2);
        dims.obligation_mut().add_delta(0.3);

        assert!((dims.warmth().delta() - 0.1).abs() < f32::EPSILON);
        assert!((dims.resentment().delta() - 0.2).abs() < f32::EPSILON);
        assert!((dims.dependence().delta() - 0.3).abs() < f32::EPSILON);
        assert!((dims.attraction().delta() - 0.4).abs() < f32::EPSILON);
        assert!((dims.attachment().delta() - 0.5).abs() < f32::EPSILON);
        assert!((dims.jealousy().delta() - 0.1).abs() < f32::EPSILON);
        assert!((dims.fear().delta() - 0.2).abs() < f32::EPSILON);
        assert!((dims.obligation().delta() - 0.3).abs() < f32::EPSILON);
    }

    #[test]
    fn default_equals_new() {
        let d = DirectionalDimensions::default();
        let n = DirectionalDimensions::new();
        assert_eq!(d, n);
    }

    #[test]
    fn clone_and_equality() {
        let d1 = DirectionalDimensions::new();
        let d2 = d1.clone();
        assert_eq!(d1, d2);
    }

    #[test]
    fn debug_format() {
        let dims = DirectionalDimensions::new();
        let debug = format!("{:?}", dims);
        assert!(debug.contains("DirectionalDimensions"));
    }
}