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
/// Praeda C++ Wrapper
///
/// High-level C++ interface to the Praeda Rust loot generation library
/// Zero JSON - all data exchanged through native C++ types

#pragma once

#include <string>
#include <memory>
#include <vector>
#include <map>
#include <stdexcept>
#include <cstdint>
#include <cstring>

// ============================================================================
// C FFI Declarations
// ============================================================================

extern "C" {
    // Handle types (opaque pointers)
    typedef struct PraedaGeneratorHandle PraedaGeneratorHandle;
    typedef struct CItemArrayHandle CItemArrayHandle;

    // C-compatible structs
    typedef struct {
        char* name;
        double initial_value;
        double min;
        double max;
        uint8_t required;
        double scaling_factor;
        double chance;
    } CItemAttribute;

    typedef struct {
        char* name;
        CItemAttribute* attributes;
        uint32_t attributes_count;
    } CAffix;

    typedef struct {
        char* name;
        char* quality;
        char* item_type;
        char* subtype;
        CAffix prefix;
        CAffix suffix;
        CItemAttribute* attributes;
        uint32_t attributes_count;
    } CItem;

    // Memory management
    PraedaGeneratorHandle* praeda_generator_new(void);
    void praeda_generator_free(PraedaGeneratorHandle* handle);
    void praeda_string_free(char* ptr);
    void praeda_error_free(char* ptr);
    void praeda_item_array_free(CItemArrayHandle* handle);

    // Configuration
    int praeda_generator_load_toml(
        PraedaGeneratorHandle* handle,
        const char* toml_str,
        char** error_out
    );

    // Programmatic configuration
    int praeda_generator_set_quality_data(
        PraedaGeneratorHandle* handle,
        const char* quality_name,
        int weight
    );

    int praeda_generator_set_item_type(
        PraedaGeneratorHandle* handle,
        const char* type_name,
        int weight
    );

    int praeda_generator_set_item_subtype(
        PraedaGeneratorHandle* handle,
        const char* type_name,
        const char* subtype_name,
        int weight
    );

    int praeda_generator_set_attribute(
        PraedaGeneratorHandle* handle,
        const char* type_name,
        const char* subtype_name,
        const char* attr_name,
        double initial_value,
        double min_value,
        double max_value,
        int required
    );

    int praeda_generator_set_item_names(
        PraedaGeneratorHandle* handle,
        const char* type_name,
        const char* subtype_name,
        const char** names,
        uint32_t names_count
    );

    int praeda_generator_set_prefix_attribute(
        PraedaGeneratorHandle* handle,
        const char* type_name,
        const char* subtype_name,
        const char* affix_name,
        const char* attr_name,
        double initial_value,
        double min_value,
        double max_value,
        int required
    );

    int praeda_generator_set_suffix_attribute(
        PraedaGeneratorHandle* handle,
        const char* type_name,
        const char* subtype_name,
        const char* affix_name,
        const char* attr_name,
        double initial_value,
        double min_value,
        double max_value,
        int required
    );

    // Loot generation
    CItemArrayHandle* praeda_generator_generate_loot(
        PraedaGeneratorHandle* handle,
        uint32_t number_of_items,
        double base_level,
        double level_variance,
        double affix_chance,
        uint8_t linear,
        double scaling_factor,
        char** error_out
    );

    // Item array access
    uint32_t praeda_item_array_count(const CItemArrayHandle* handle);
    const CItem* praeda_item_array_get(const CItemArrayHandle* handle, uint32_t index);

    // Queries
    int praeda_generator_has_quality(const PraedaGeneratorHandle* handle, const char* quality);
    char* praeda_version(void);
}

// ============================================================================
// C++ Wrappers
// ============================================================================

namespace praeda {

/// RAII wrapper for C strings
class CStringWrapper {
public:
    explicit CStringWrapper(char* ptr) : ptr_(ptr) {}
    ~CStringWrapper() {
        if (ptr_) {
            praeda_error_free(ptr_);
        }
    }

    CStringWrapper(const CStringWrapper&) = delete;
    CStringWrapper& operator=(const CStringWrapper&) = delete;

    CStringWrapper(CStringWrapper&& other) noexcept : ptr_(other.release()) {}
    CStringWrapper& operator=(CStringWrapper&& other) noexcept {
        if (ptr_) praeda_error_free(ptr_);
        ptr_ = other.release();
        return *this;
    }

    const char* c_str() const { return ptr_ ? ptr_ : ""; }
    std::string str() const { return std::string(ptr_ ? ptr_ : ""); }

    char* release() {
        char* temp = ptr_;
        ptr_ = nullptr;
        return temp;
    }

private:
    char* ptr_;
};

/// Exception thrown by Praeda
class Exception : public std::runtime_error {
public:
    explicit Exception(const std::string& what) : std::runtime_error(what) {}
};

/// Item attribute - mirrors Rust ItemAttribute struct
class ItemAttribute {
public:
    ItemAttribute() : initial_value(0.0), min(0.0), max(0.0), required(false),
                      scaling_factor(1.0), chance(0.0) {}

    ItemAttribute(const std::string& n, double iv, double min_val, double max_val, bool req)
        : name(n), initial_value(iv), min(min_val), max(max_val), required(req),
          scaling_factor(1.0), chance(0.0) {}

    std::string name;
    double initial_value;
    double min;
    double max;
    bool required;
    double scaling_factor;
    double chance;

    static ItemAttribute from_c(const CItemAttribute& c_attr) {
        ItemAttribute attr;
        attr.name = std::string(c_attr.name ? c_attr.name : "");
        attr.initial_value = c_attr.initial_value;
        attr.min = c_attr.min;
        attr.max = c_attr.max;
        attr.required = c_attr.required != 0;
        attr.scaling_factor = c_attr.scaling_factor;
        attr.chance = c_attr.chance;
        return attr;
    }
};

/// Affix (prefix or suffix) - mirrors Rust Affix struct
class Affix {
public:
    Affix() = default;
    Affix(const std::string& n, const std::vector<ItemAttribute>& attrs)
        : name(n), attributes(attrs) {}

    std::string name;
    std::vector<ItemAttribute> attributes;

    static Affix from_c(const CAffix& c_affix) {
        Affix affix;
        affix.name = std::string(c_affix.name ? c_affix.name : "");

        for (uint32_t i = 0; i < c_affix.attributes_count; ++i) {
            affix.attributes.push_back(ItemAttribute::from_c(c_affix.attributes[i]));
        }

        return affix;
    }
};

/// Generated item - mirrors Rust Item struct
class Item {
public:
    Item() = default;

    Item(const std::string& n, const std::string& q, const std::string& t,
         const std::string& st, const Affix& pre, const Affix& suf,
         const std::map<std::string, ItemAttribute>& attrs)
        : name(n), quality(q), type(t), subtype(st),
          prefix(pre), suffix(suf), attributes(attrs) {}

    std::string name;
    std::string quality;
    std::string type;
    std::string subtype;
    Affix prefix;
    Affix suffix;
    std::map<std::string, ItemAttribute> attributes;

private:
    friend class Generator;

    static Item from_c(const CItem& c_item) {
        Item item;
        item.name = std::string(c_item.name ? c_item.name : "");
        item.quality = std::string(c_item.quality ? c_item.quality : "");
        item.type = std::string(c_item.item_type ? c_item.item_type : "");
        item.subtype = std::string(c_item.subtype ? c_item.subtype : "");

        item.prefix = Affix::from_c(c_item.prefix);
        item.suffix = Affix::from_c(c_item.suffix);

        for (uint32_t i = 0; i < c_item.attributes_count; ++i) {
            const CItemAttribute& c_attr = c_item.attributes[i];
            ItemAttribute attr = ItemAttribute::from_c(c_attr);
            item.attributes[attr.name] = attr;
        }

        return item;
    }
};

/// Loot generation options - mirrors Rust GeneratorOptions struct
struct GenerationOptions {
    uint32_t number_of_items = 1;
    double base_level = 1.0;
    double level_variance = 1.0;
    double affix_chance = 0.25;
    bool linear = true;
    double scaling_factor = 1.0;
};

/// Main Praeda generator class
class Generator {
public:
    /// Create a new Praeda generator
    static std::unique_ptr<Generator> create() {
        PraedaGeneratorHandle* handle = praeda_generator_new();
        if (!handle) {
            throw Exception("Failed to create generator");
        }
        return std::unique_ptr<Generator>(new Generator(handle));
    }

    /// Destructor
    ~Generator() {
        if (handle_) {
            praeda_generator_free(handle_);
        }
    }

    // Delete copy operations
    Generator(const Generator&) = delete;
    Generator& operator=(const Generator&) = delete;

    // Allow move operations
    Generator(Generator&& other) noexcept : handle_(other.release()) {}
    Generator& operator=(Generator&& other) noexcept {
        if (handle_) praeda_generator_free(handle_);
        handle_ = other.release();
        return *this;
    }

    /// Load configuration from TOML string
    void load_toml_string(const std::string& toml_content) {
        char* error = nullptr;
        int result = praeda_generator_load_toml(handle_, toml_content.c_str(), &error);
        if (result != 0) {
            if (error) {
                CStringWrapper error_wrapper(error);
                throw Exception(error_wrapper.str());
            }
            throw Exception("Failed to load TOML");
        }
    }

    /// Set quality tier data
    void set_quality_data(const std::string& quality_name, int weight) {
        int result = praeda_generator_set_quality_data(handle_, quality_name.c_str(), weight);
        if (result != 0) {
            throw Exception("Failed to set quality data");
        }
    }

    /// Set item type with weight
    void set_item_type(const std::string& type_name, int weight) {
        int result = praeda_generator_set_item_type(handle_, type_name.c_str(), weight);
        if (result != 0) {
            throw Exception("Failed to set item type");
        }
    }

    /// Set item subtype with weight
    void set_item_subtype(const std::string& type_name, const std::string& subtype_name, int weight) {
        int result = praeda_generator_set_item_subtype(
            handle_,
            type_name.c_str(),
            subtype_name.c_str(),
            weight
        );
        if (result != 0) {
            throw Exception("Failed to set item subtype");
        }
    }

    /// Set attribute for an item type/subtype
    void set_attribute(const std::string& type_name, const std::string& subtype_name,
                       const ItemAttribute& attribute) {
        int result = praeda_generator_set_attribute(
            handle_,
            type_name.c_str(),
            subtype_name.c_str(),
            attribute.name.c_str(),
            attribute.initial_value,
            attribute.min,
            attribute.max,
            attribute.required ? 1 : 0
        );
        if (result != 0) {
            throw Exception("Failed to set attribute");
        }
    }

    /// Set item names for a type/subtype combination
    void set_item_names(const std::string& type_name, const std::string& subtype_name,
                        const std::vector<std::string>& names) {
        std::vector<const char*> c_names;
        for (const auto& name : names) {
            c_names.push_back(name.c_str());
        }

        int result = praeda_generator_set_item_names(
            handle_,
            type_name.c_str(),
            subtype_name.c_str(),
            c_names.data(),
            static_cast<uint32_t>(c_names.size())
        );
        if (result != 0) {
            throw Exception("Failed to set item names");
        }
    }

    /// Set prefix attribute for a type/subtype
    void set_prefix_attribute(const std::string& type_name, const std::string& subtype_name,
                              const std::string& affix_name, const ItemAttribute& attribute) {
        int result = praeda_generator_set_prefix_attribute(
            handle_,
            type_name.c_str(),
            subtype_name.c_str(),
            affix_name.c_str(),
            attribute.name.c_str(),
            attribute.initial_value,
            attribute.min,
            attribute.max,
            attribute.required ? 1 : 0
        );
        if (result != 0) {
            throw Exception("Failed to set prefix attribute");
        }
    }

    /// Set suffix attribute for a type/subtype
    void set_suffix_attribute(const std::string& type_name, const std::string& subtype_name,
                              const std::string& affix_name, const ItemAttribute& attribute) {
        int result = praeda_generator_set_suffix_attribute(
            handle_,
            type_name.c_str(),
            subtype_name.c_str(),
            affix_name.c_str(),
            attribute.name.c_str(),
            attribute.initial_value,
            attribute.min,
            attribute.max,
            attribute.required ? 1 : 0
        );
        if (result != 0) {
            throw Exception("Failed to set suffix attribute");
        }
    }

    /// Generate loot items
    /// Returns a vector of native Item objects.
    std::vector<Item> generate_loot(const GenerationOptions& options) {
        char* error = nullptr;
        CItemArrayHandle* array_handle = praeda_generator_generate_loot(
            handle_,
            options.number_of_items,
            options.base_level,
            options.level_variance,
            options.affix_chance,
            options.linear ? 1 : 0,
            options.scaling_factor,
            &error
        );

        if (!array_handle) {
            if (error) {
                CStringWrapper error_wrapper(error);
                throw Exception(error_wrapper.str());
            }
            throw Exception("Failed to generate loot");
        }

        std::vector<Item> items;
        uint32_t count = praeda_item_array_count(array_handle);

        for (uint32_t i = 0; i < count; ++i) {
            const CItem* c_item = praeda_item_array_get(array_handle, i);
            if (c_item) {
                items.push_back(Item::from_c(*c_item));
            }
        }

        praeda_item_array_free(array_handle);
        return items;
    }

    /// Check if a quality exists
    bool has_quality(const std::string& quality) {
        int result = praeda_generator_has_quality(handle_, quality.c_str());
        if (result < 0) {
            throw Exception("Error checking quality");
        }
        return result == 1;
    }

    /// Get generator info (version string)
    std::string info() {
        char* v = praeda_version();
        CStringWrapper wrapper(v);
        return wrapper.str();
    }

    /// Get the underlying FFI handle (advanced usage only)
    PraedaGeneratorHandle* native_handle() const { return handle_; }

private:
    PraedaGeneratorHandle* handle_;

    explicit Generator(PraedaGeneratorHandle* handle) : handle_(handle) {}

    PraedaGeneratorHandle* release() {
        PraedaGeneratorHandle* temp = handle_;
        handle_ = nullptr;
        return temp;
    }
};

/// Get Praeda library version
inline std::string version() {
    char* v = praeda_version();
    CStringWrapper wrapper(v);
    return wrapper.str();
}

} // namespace praeda