praeda 0.2.1

A procedural loot generator library with C++ and C# FFI bindings
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Represents an item type with subtypes and weight
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ItemType {
    pub item_type: String,
    pub subtypes: HashMap<String, i32>,
    pub weight: i32,
    #[serde(default)]
    pub metadata: HashMap<String, serde_json::Value>,
}

impl ItemType {
    pub fn new(item_type: &str, subtypes: HashMap<String, i32>, weight: i32) -> Self {
        ItemType {
            item_type: item_type.to_string(),
            subtypes,
            weight,
            metadata: HashMap::new(),
        }
    }

    pub fn set_type(&mut self, item_type: String) {
        self.item_type = item_type;
    }

    pub fn get_type(&self) -> &str {
        &self.item_type
    }

    pub fn add_subtype(&mut self, subtype: &str, weight: i32) {
        self.subtypes.insert(subtype.to_string(), weight);
    }

    pub fn get_subtypes(&self) -> &HashMap<String, i32> {
        &self.subtypes
    }

    pub fn has_subtype(&self, subtype: &str) -> bool {
        self.subtypes.contains_key(subtype)
    }

    pub fn set_weight(&mut self, weight: i32) {
        self.weight = weight;
    }

    pub fn get_weight(&self) -> i32 {
        self.weight
    }

    pub fn set_metadata(&mut self, key: String, value: serde_json::Value) {
        self.metadata.insert(key, value);
    }

    pub fn get_metadata(&self, key: &str) -> Option<&serde_json::Value> {
        self.metadata.get(key)
    }

    pub fn get_all_metadata(&self) -> &HashMap<String, serde_json::Value> {
        &self.metadata
    }

    pub fn has_metadata(&self, key: &str) -> bool {
        self.metadata.contains_key(key)
    }
}

/// Represents item data (names for specific types/subtypes)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ItemData {
    pub item_type: String,
    pub subtype: String,
    pub names: Vec<String>,
    /// Per-item metadata: maps item name to metadata properties
    #[serde(default)]
    pub item_metadata: HashMap<String, HashMap<String, serde_json::Value>>,
}

impl ItemData {
    pub fn new(item_type: &str, subtype: &str, names: Vec<String>) -> Self {
        ItemData {
            item_type: item_type.to_string(),
            subtype: subtype.to_string(),
            names,
            item_metadata: HashMap::new(),
        }
    }

    pub fn set_item_type(&mut self, item_type: String) {
        self.item_type = item_type;
    }

    pub fn get_item_type(&self) -> &str {
        &self.item_type
    }

    pub fn set_subtype(&mut self, subtype: String) {
        self.subtype = subtype;
    }

    pub fn get_subtype(&self) -> &str {
        &self.subtype
    }

    pub fn add_name(&mut self, name: String) {
        self.names.push(name);
    }

    pub fn get_names(&self) -> &[String] {
        &self.names
    }

    /// Set metadata for a specific item name
    pub fn set_item_metadata(&mut self, item_name: String, key: String, value: serde_json::Value) {
        self.item_metadata
            .entry(item_name)
            .or_default()
            .insert(key, value);
    }

    /// Get metadata for a specific item name and key
    pub fn get_item_metadata(&self, item_name: &str, key: &str) -> Option<&serde_json::Value> {
        self.item_metadata
            .get(item_name)
            .and_then(|metadata| metadata.get(key))
    }

    /// Get all metadata for a specific item name
    pub fn get_item_all_metadata(&self, item_name: &str) -> Option<&HashMap<String, serde_json::Value>> {
        self.item_metadata.get(item_name)
    }

    /// Check if an item has metadata
    pub fn has_item_metadata(&self, item_name: &str, key: &str) -> bool {
        self.item_metadata
            .get(item_name)
            .map(|m| m.contains_key(key))
            .unwrap_or(false)
    }
}

/// Represents a single attribute or stat on an item.
///
/// Attributes are custom properties that can be attached to items during generation.
/// Examples: damage, defense, health, mana, armor class, critical strike chance, etc.
///
/// # Fields
///
/// * `name` - Attribute name (e.g., "damage", "defense")
/// * `initial_value` - Base value for this attribute
/// * `min` - Minimum possible value after scaling
/// * `max` - Maximum possible value after scaling
/// * `required` - If true, this attribute is always applied; if false, it depends on chance
/// * `scaling_factor` - Multiplier applied per level (linear: adds, exponential: multiplies)
/// * `chance` - Probability (0.0-1.0) of being included if not required
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ItemAttribute {
    pub name: String,
    pub initial_value: f64,
    pub min: f64,
    pub max: f64,
    pub required: bool,
    #[serde(default)]
    pub scaling_factor: f64,
    #[serde(default)]
    pub chance: f64,
}

impl ItemAttribute {
    /// Creates a new item attribute.
    ///
    /// # Arguments
    ///
    /// * `name` - Name of the attribute
    /// * `initial_value` - Starting value before level scaling
    /// * `min` - Minimum value after scaling
    /// * `max` - Maximum value after scaling
    /// * `required` - If true, always applied; if false, chance-based
    pub fn new(
        name: &str,
        initial_value: f64,
        min: f64,
        max: f64,
        required: bool,
    ) -> Self {
        ItemAttribute {
            name: name.to_string(),
            initial_value,
            min,
            max,
            required,
            scaling_factor: 1.0,
            chance: 0.0,
        }
    }

    pub fn set_name(&mut self, name: String) {
        self.name = name;
    }

    pub fn get_name(&self) -> &str {
        &self.name
    }

    pub fn set_initial_value(&mut self, initial_value: f64) {
        // LCOV_EXCL_START - Rare path: setting bounds when both are zero
        if self.min == 0.0 && self.max == 0.0 {
            self.min = initial_value;
            self.max = initial_value;
        }
        // LCOV_EXCL_END
        self.initial_value = initial_value;
    }

    pub fn get_initial_value(&self) -> f64 {
        self.initial_value
    }

    pub fn set_min(&mut self, min: f64) {
        self.min = min;
    }

    pub fn get_min(&self) -> f64 {
        self.min
    }

    pub fn set_max(&mut self, max: f64) {
        self.max = max;
    }

    pub fn get_max(&self) -> f64 {
        self.max
    }

    pub fn set_required(&mut self, required: bool) {
        self.required = required;
    }

    pub fn get_required(&self) -> bool {
        self.required
    }

    /// Generate a scaled value based on level, scaling factor, and linear/exponential progression
    pub fn generate_value(&mut self, new_level: f64, linear: bool, scaling_factor: f64) {
        if self.min == 0.0 && self.max == 0.0 && self.initial_value != 0.0 {
            self.min = self.initial_value;
            self.max = self.initial_value;
        }

        if self.initial_value == 0.0 && !linear {
            self.initial_value = 1.0;
        }

        if linear {
            self.initial_value += new_level * scaling_factor;
        } else {
            self.initial_value *= scaling_factor.powf(new_level);
        }

        if self.initial_value < 0.0 {
            self.initial_value = 0.0;
        }
    }
}

/// Represents a prefix or suffix affix.
///
/// Affixes are optional name modifiers that can be applied to items (e.g., "Flaming", "of Strength").
/// Each affix is associated with a set of attributes that are applied to the item when the affix is selected.
///
/// # Example
///
/// A "Flaming" prefix might add extra fire damage to a weapon, implemented as an affix with a
/// single damage attribute.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Affix {
    pub name: String,
    pub attributes: Vec<ItemAttribute>,
}

impl Affix {
    pub fn new(name: &str, attributes: Vec<ItemAttribute>) -> Self {
        Affix { name: name.to_string(), attributes }
    }

    pub fn empty() -> Self {
        Affix {
            name: String::new(),
            attributes: Vec::new(),
        }
    }

    pub fn get_name(&self) -> &str {
        &self.name
    }

    pub fn set_name(&mut self, name: String) {
        self.name = name;
    }

    pub fn get_attributes(&self) -> &[ItemAttribute] {
        &self.attributes
    }

    pub fn set_attributes(&mut self, attributes: Vec<ItemAttribute>) {
        self.attributes = attributes;
    }

    pub fn set_attribute(&mut self, new_attribute: ItemAttribute) {
        if let Some(pos) = self
            .attributes
            .iter()
            .position(|a| a.get_name() == new_attribute.get_name())
        {
            self.attributes[pos] = new_attribute;
        } else {
            self.attributes.push(new_attribute);
        }
    }
}

/// Represents a complete generated item.
///
/// An `Item` is the output of the loot generation process. It contains all the information
/// about a single generated item, including its identity, quality tier, type, affixes, and stats.
///
/// # Fields
///
/// * `name` - Display name of the item (e.g., "Iron Sword", "Leather Armor")
/// * `quality` - Quality/rarity tier (e.g., "common", "rare", "legendary")
/// * `item_type` - Category type (e.g., "weapon", "armor")
/// * `subtype` - Specific subtype (e.g., "sword", "plate armor")
/// * `prefix` - Prefix affix applied to this item (empty if none)
/// * `suffix` - Suffix affix applied to this item (empty if none)
/// * `attributes` - Map of attribute names to their values (damage, defense, etc.)
/// * `metadata` - Additional metadata (application-specific data)
///
/// # Example
///
/// A generated item might look like:
/// ```text
/// name: "Flaming Iron Sword"
/// quality: "rare"
/// item_type: "weapon"
/// subtype: "sword"
/// prefix: Affix { name: "Flaming", attributes: [damage: 5.0] }
/// suffix: Affix { name: "of Strength", attributes: [strength: 3.0] }
/// attributes: { "damage": 15.0, "crit_chance": 0.1 }
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Item {
    pub name: String,
    pub quality: String,
    #[serde(rename = "type")]
    pub item_type: String,
    pub subtype: String,
    pub prefix: Affix,
    pub suffix: Affix,
    pub attributes: HashMap<String, ItemAttribute>,
    #[serde(default)]
    pub metadata: HashMap<String, serde_json::Value>,
}

impl Item {
    /// Creates a new item with the specified properties.
    ///
    /// This is typically called internally by [`PraedaGenerator`](crate::generator::PraedaGenerator)
    /// during loot generation, but can also be used to manually construct items.
    pub fn new(
        name: &str,
        quality: &str,
        item_type: &str,
        subtype: &str,
        prefix: Affix,
        suffix: Affix,
        attributes: HashMap<String, ItemAttribute>,
    ) -> Self {
        Item {
            name: name.to_string(),
            quality: quality.to_string(),
            item_type: item_type.to_string(),
            subtype: subtype.to_string(),
            prefix,
            suffix,
            attributes,
            metadata: HashMap::new(),
        }
    }

    pub fn empty() -> Self {
        Item {
            name: String::new(),
            quality: String::new(),
            item_type: String::new(),
            subtype: String::new(),
            prefix: Affix::empty(),
            suffix: Affix::empty(),
            attributes: HashMap::new(),
            metadata: HashMap::new(),
        }
    }

    pub fn set_name(&mut self, name: String) {
        self.name = name;
    }

    pub fn get_name(&self) -> &str {
        &self.name
    }

    pub fn set_quality(&mut self, quality: String) {
        self.quality = quality;
    }

    pub fn get_quality(&self) -> &str {
        &self.quality
    }

    pub fn set_type(&mut self, item_type: String) {
        self.item_type = item_type;
    }

    pub fn get_type(&self) -> &str {
        &self.item_type
    }

    pub fn set_subtype(&mut self, subtype: String) {
        self.subtype = subtype;
    }

    pub fn get_subtype(&self) -> &str {
        &self.subtype
    }

    pub fn set_prefix(&mut self, prefix: Affix) {
        self.prefix = prefix;
    }

    pub fn get_prefix(&self) -> &Affix {
        &self.prefix
    }

    pub fn get_prefix_mut(&mut self) -> &mut Affix {
        &mut self.prefix
    }

    pub fn set_suffix(&mut self, suffix: Affix) {
        self.suffix = suffix;
    }

    pub fn get_suffix(&self) -> &Affix {
        &self.suffix
    }

    #[cfg(not(tarpaulin_include))]
    pub fn get_suffix_mut(&mut self) -> &mut Affix {
        &mut self.suffix
    }

    #[cfg(not(tarpaulin_include))]
    pub fn set_attributes(&mut self, attributes: HashMap<String, ItemAttribute>) {
        self.attributes = attributes;
    }

    pub fn get_attributes(&self) -> &HashMap<String, ItemAttribute> {
        &self.attributes
    }

    pub fn set_attribute(&mut self, name: &str, attr: ItemAttribute) {
        self.attributes.insert(name.to_string(), attr);
    }

    pub fn has_attribute(&self, name: &str) -> bool {
        self.attributes.contains_key(name)
    }

    pub fn get_attribute(&self, name: &str) -> Option<&ItemAttribute> {
        self.attributes.get(name)
    }

    pub fn get_attribute_mut(&mut self, name: &str) -> Option<&mut ItemAttribute> {
        self.attributes.get_mut(name)
    }

    pub fn set_metadata(&mut self, key: &str, value: serde_json::Value) {
        self.metadata.insert(key.to_string(), value);
    }

    pub fn get_metadata(&self, key: &str) -> Option<&serde_json::Value> {
        self.metadata.get(key)
    }

    pub fn get_all_metadata(&self) -> &HashMap<String, serde_json::Value> {
        &self.metadata
    }

    pub fn has_metadata(&self, key: &str) -> bool {
        self.metadata.contains_key(key)
    }
}

/// Options controlling loot generation behavior.
///
/// These parameters define how items are generated, including how many items to create,
/// what level range they should be, and how attributes scale.
///
/// # Fields
///
/// * `number_of_items` - How many items to generate
/// * `base_level` - Starting level for items (used for attribute scaling)
/// * `level_variance` - Range around base_level (actual level = base ± variance)
/// * `affix_chance` - Probability (0.0-1.0) that an affix is selected for each item
/// * `linear` - If true, attributes scale linearly; if false, exponentially
/// * `scaling_factor` - Multiplier applied per level
///   - Linear: adds `level * scaling_factor` to attribute value
///   - Exponential: multiplies attribute value by `scaling_factor^level`
///
/// # Example
///
/// ```rust,ignore
/// let options = GeneratorOptions {
///     number_of_items: 5,        // Generate 5 items
///     base_level: 10.0,          // Level 10 items
///     level_variance: 2.0,       // Range: level 8-12
///     affix_chance: 0.25,        // 25% chance for affixes
///     linear: true,              // Linear attribute scaling
///     scaling_factor: 1.5,       // Adds 1.5 per level linearly
/// };
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct GeneratorOptions {
    pub number_of_items: u32,
    pub base_level: f64,
    pub level_variance: f64,
    pub affix_chance: f64,
    pub linear: bool,
    pub scaling_factor: f64,
}

impl GeneratorOptions {
    pub fn new(
        number_of_items: u32,
        base_level: f64,
        level_variance: f64,
        affix_chance: f64,
        linear: bool,
        scaling_factor: f64,
    ) -> Self {
        GeneratorOptions {
            number_of_items,
            base_level,
            level_variance,
            affix_chance,
            linear,
            scaling_factor,
        }
    }

    pub fn is_linear(&self) -> bool {
        self.linear
    }

    pub fn is_exponential(&self) -> bool {
        !self.linear
    }
}

impl Default for GeneratorOptions {
    fn default() -> Self {
        GeneratorOptions {
            number_of_items: 1,
            base_level: 1.0,
            level_variance: 1.0,
            affix_chance: 0.25,
            linear: true,
            scaling_factor: 1.0,
        }
    }
}

/// Per-generation overrides for loot generation.
///
/// Allow forcing specific item properties during generation instead of random selection.
/// Leave fields empty to use random selection for that property.
///
/// # Fields
///
/// * `quality_override` - If set, forces items to this quality; if empty, quality is random
/// * `type_override` - If set, forces items to this type; if empty, type is random
/// * `subtype_override` - If set, forces items to this subtype; if empty, subtype is random
///
/// # Example
///
/// ```rust,ignore
/// // Generate legendary weapons only
/// let overrides = GeneratorOverrides {
///     quality_override: "legendary".to_string(),
///     type_override: "weapon".to_string(),
///     subtype_override: "".to_string(),  // Random subtype
/// };
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct GeneratorOverrides {
    pub quality_override: String,
    pub type_override: String,
    pub subtype_override: String,
}

impl GeneratorOverrides {
    pub fn new(
        quality_override: &str,
        type_override: &str,
        subtype_override: &str,
    ) -> Self {
        GeneratorOverrides {
            quality_override: quality_override.to_string(),
            type_override: type_override.to_string(),
            subtype_override: subtype_override.to_string(),
        }
    }

    pub fn empty() -> Self {
        GeneratorOverrides {
            quality_override: String::new(),
            type_override: String::new(),
            subtype_override: String::new(),
        }
    }

    pub fn get_quality_override(&self) -> &str {
        &self.quality_override
    }

    pub fn get_type_override(&self) -> &str {
        &self.type_override
    }

    pub fn get_subtype_override(&self) -> &str {
        &self.subtype_override
    }
}

// ============================================================================
// TOML Intermediate Structures for Deserialization
// ============================================================================

/// Intermediate structure for loading TOML configuration
#[derive(Debug, Deserialize)]
pub struct TomlConfig {
    pub quality_data: HashMap<String, i32>,
    #[serde(default)]
    pub item_types: Vec<ItemType>,
    #[serde(default)]
    pub item_attributes: Vec<TomlItemAttributes>,
    #[serde(default)]
    pub item_list: Vec<TomlItemList>,
    #[serde(default)]
    pub item_affixes: Vec<TomlItemAffixes>,
}

/// Item attributes for a specific type/subtype combination
#[derive(Debug, Deserialize)]
pub struct TomlItemAttributes {
    #[serde(default)]
    pub item_type: String,
    #[serde(default)]
    pub subtype: String,
    #[serde(default)]
    pub attributes: Vec<ItemAttribute>,
}

/// Item list for a specific type/subtype combination
#[derive(Debug, Deserialize)]
pub struct TomlItemList {
    pub item_type: String,
    pub subtype: String,
    #[serde(default)]
    pub names: Vec<String>,
    #[serde(default)]
    pub item_metadata: HashMap<String, HashMap<String, serde_json::Value>>,
}

/// Item affixes for a specific type/subtype combination
#[derive(Debug, Deserialize)]
pub struct TomlItemAffixes {
    #[serde(default)]
    pub item_type: String,
    #[serde(default)]
    pub subtype: String,
    #[serde(default)]
    pub prefixes: Vec<Affix>,
    #[serde(default)]
    pub suffixes: Vec<Affix>,
    #[serde(default)]
    pub metadata: HashMap<String, serde_json::Value>,
}