rpg-stat 2021.12.16

A library for computer stats for RPG game development
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
/*!
# Item


*/
use serde::{Deserialize, Serialize};
#[cfg(feature = "fltkform")]
use fltk::{prelude::*, *};
#[cfg(feature = "fltkform")]
use fltk_form_derive::*;
#[cfg(feature = "fltkform")]
use fltk_form::FltkForm;

use std::fmt;
use std::fmt::Debug;
use std::ops::{Add, AddAssign,  Div, DivAssign, Mul, MulAssign, Neg, Rem, RemAssign, Sub, SubAssign};


// #Condition
//use crate::effect::Normal as Effect;
// #Element
//use crate::types::Normal as Element;
use crate::random::Random;
use crate::stats::Basic as BasicStats;
use crate::stats::Normal as NormalStats;
use crate::stats::Advanced as AdvancedStats;
use crate::stats::Builder;

/*
# Item trait

This defines the functions for an item
 * `value()`
 * `cost()`
 * `resell()`

These Are applied to all *Item* types

TODO actual economic functions to vary prices
*/
pub trait Item {
    fn value(&self) -> f64;
    fn cost(&self) -> f64;
    fn resell(&self) -> f64;
}

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "fltkform", derive(FltkForm))]
pub enum Basic {
    Hp,
    Mp,
    None,
}
impl Default for Basic {
    fn default() -> Self {
        Self::None
    }
}impl Random for Basic {
    type Type = Basic;
    fn random_type(&self) -> Self::Type {
        let max = 2;
        let val = self.random_rate(max);
        match val {
            0 => Basic::Hp,
            1 => Basic::Mp,
            _=> Basic::None,
        }
    }
}
impl fmt::Display for Basic {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let v:String;
        match *self {
            Basic::Hp => v = String::from("Hp"),
            Basic::Mp => v = String::from("Mp"),
            Basic::None => v = String::from(""),
        }
        write!(f, "{}", v.as_str())
    }
}
impl Item for Basic {
    fn value(&self) -> f64 {
        match *self {
            Basic::Hp => 10.0,
            Basic::Mp => 5.0,
            Basic::None=> 0.0,
        }
    }
    fn cost(&self) -> f64 {
        match *self {
            Basic::Hp => 20.0,
            Basic::Mp => 50.0,
            Basic::None=> 0.0,
        }
    }
    fn resell(&self) -> f64 {
        match *self {
            Basic::Hp => 10.0,
            Basic::Mp => 25.0,
            Basic::None=> 0.0,
        }
    }
}
impl<T:Copy
    + Default
    + Debug
    + AddAssign
    + Add<Output = T>
    + Div<Output = T>
    + DivAssign
    + Mul<Output = T>
    + MulAssign
    + Neg<Output = T>
    + Rem<Output = T>
    + RemAssign
    + Sub<Output = T>
    + SubAssign
    + std::cmp::PartialOrd
    + num::NumCast> Builder<T> for Basic {
    fn build_basic(&self, id:T, level:T) -> BasicStats<T>{
        let mut hp:T = num::cast(0).unwrap();
        let mut mp:T = num::cast(0).unwrap();
        let xp:T = num::cast(0).unwrap();
        let xp_next:T = num::cast(0).unwrap();
        let gp:T = num::cast(0).unwrap();
        let speed:T = num::cast(0).unwrap();
        match *self {
            Basic::Hp => {
                hp = num::cast(5).unwrap();
            },
            Basic::Mp => {
                mp = num::cast(2).unwrap();
            },
            _=> (),
        }
        hp *= level;
        mp *= level;
        BasicStats {
            id,
            xp,
            xp_next,
            level,
            gp,
            hp,
            mp,
            hp_max:hp,
            mp_max:mp,
            speed,
        }
        
    }
    fn build_normal(&self, id:T, level:T) -> NormalStats<T>{
        let mut hp:T = num::cast(0).unwrap();
        let mut mp:T = num::cast(0).unwrap();
        let xp:T = num::cast(0).unwrap();
        let xp_next:T = num::cast(0).unwrap();
        let gp:T = num::cast(0).unwrap();
        let speed:T = num::cast(0).unwrap();
        let atk:T = num::cast(0).unwrap();
        let mut def:T = num::cast(0).unwrap();
        let m_atk:T = num::cast(0).unwrap();
        let mut m_def:T = num::cast(0).unwrap();
        match *self {
            Basic::Hp => {
                hp = num::cast(5).unwrap();
                def = num::cast(2).unwrap();
            },
            Basic::Mp => {
                mp = num::cast(2).unwrap();
                m_def = num::cast(2).unwrap();
            },
            _=> (),
        }
        hp *= level;
        mp *= level;
        def *= level;
        m_def *= level;
        NormalStats {
            id,
            xp,
            xp_next,
            level,
            gp,
            hp,
            mp,
            hp_max:hp,
            mp_max:mp,
            speed,
            atk,
            def,
            m_atk,
            m_def,
        }
    }
    fn build_advanced(&self, id:T, level:T) -> AdvancedStats<T>{
        let mut hp:T = num::cast(0).unwrap();
        let mut mp:T = num::cast(0).unwrap();
        let xp:T = num::cast(0).unwrap();
        let xp_next:T = num::cast(0).unwrap();
        let gp:T = num::cast(0).unwrap();
        let speed:T = num::cast(0).unwrap();
        let atk:T = num::cast(0).unwrap();
        let mut def:T = num::cast(0).unwrap();
        let m_atk:T = num::cast(0).unwrap();
        let mut m_def:T = num::cast(0).unwrap();
        let mut agility:T = num::cast(0).unwrap();
        let mut strength:T = num::cast(0).unwrap();
        let mut dexterity:T = num::cast(0).unwrap();
        let mut constitution:T = num::cast(0).unwrap();
        let mut intelligence:T = num::cast(0).unwrap();
        let mut charisma:T = num::cast(0).unwrap();
        let mut wisdom:T = num::cast(0).unwrap();
        let age:T = num::cast(0).unwrap();
        match *self {
            Basic::Hp => {
                hp = num::cast(5).unwrap();
                def = num::cast(2).unwrap();
                strength = num::cast(2).unwrap();
                constitution = num::cast(2).unwrap();
                charisma = num::cast(1).unwrap();
                dexterity = num::cast(2).unwrap();
            },
            Basic::Mp => {
                mp = num::cast(2).unwrap();
                m_def = num::cast(2).unwrap();
                agility = num::cast(2).unwrap();
                strength = num::cast(1).unwrap();
                constitution = num::cast(1).unwrap();
                charisma = num::cast(2).unwrap();
                dexterity = num::cast(1).unwrap();
                wisdom = num::cast(2).unwrap();
                intelligence = num::cast(2).unwrap();
            },
            _=> (),
        }
        hp *= level;
        mp *= level;
        def *= level;
        m_def *= level;
        strength *= level;
        constitution *= level;
        charisma *= level;
        dexterity *= level;
        wisdom *= level;
        intelligence *= level;
        AdvancedStats {
            id,
            xp,
            xp_next,
            level,
            gp,
            hp,
            mp,
            hp_max:hp,
            mp_max:mp,
            speed,
            atk,
            def,
            m_atk,
            m_def,
            agility,
            strength,
            dexterity,
            constitution,
            intelligence,
            charisma,
            wisdom,
            age,
        }
    }
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "fltkform", derive(FltkForm))]
pub enum Normal {
    Hp,
    Mp,
    Heal,
    Exp,
    Def,
    Atk,
    Speed,
    Special,
    None,
}
impl Default for Normal {
    fn default() -> Self {
        Self::None
    }
}
impl Random for Normal {
    type Type = Normal;
    fn random_type(&self) -> Self::Type {
        let max = 10;
        let val = self.random_rate(max);
        match val {
            0 => Normal::Hp,
            1 => Normal::Mp,
            4 => Normal::Heal,
            5 => Normal::Exp,
            7 => Normal::Def,
            8 => Normal::Atk,
            9 => Normal::Speed,
            10 => Normal::Special,
            _=> Normal::None,
        }
    }
}
impl fmt::Display for Normal {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let v:String;
        match *self {
            Normal::Hp => v = String::from("Hp"),
            Normal::Mp => v = String::from("Mp"),
            Normal::Heal => v = String::from("Heal"),
            Normal::Exp => v = String::from("Exp"),
            Normal::Def => v = String::from("Def"),
            Normal::Atk => v = String::from("Atk"),
            Normal::Speed => v = String::from("Speed"),
            Normal::Special => v = String::from("Special"),
            Normal::None => v = String::from(""),
        }
        write!(f, "{}", v.as_str())
    }
}
impl Item for Normal {
    fn value(&self) -> f64 {
        match *self {
            Normal::Hp => 10.0,
            Normal::Mp => 5.0,
            Normal::Heal => 10.0,
            Normal::Exp => 1.0,
            Normal::Def => 1.0,
            Normal::Atk => 1.0,
            Normal::Speed => 1.0,
            Normal::Special => 1.0,
            _=> 0.0,
        }
    }
    fn cost(&self) -> f64 {
        match *self {
            Normal::Hp => 20.0,
            Normal::Mp => 50.0,
            Normal::Heal => 10.0,
            Normal::Exp => 150.0,
            Normal::Def => 80.0,
            Normal::Atk => 90.0,
            Normal::Speed => 70.0,
            Normal::Special => 100.0,
            _=> 0.0,
        }
    }
    fn resell(&self) -> f64 {
        match *self {
            Normal::Hp => 10.0,
            Normal::Mp => 25.0,
            Normal::Heal => 5.0,
            Normal::Exp => 75.0,
            Normal::Def => 40.0,
            Normal::Atk => 45.0,
            Normal::Speed => 35.0,
            Normal::Special => 50.0,
            _=> 0.0,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "fltkform", derive(FltkForm))]
pub enum Advanced {
    Hp,
    Mp,
    Heal,
    Exp,
    Def,
    Atk,
    Speed,
    Special,
    Crystal,
    Powder,
    Gem,
    None,
}
impl Random for Advanced {
    type Type = Advanced;
    fn random_type(&self) -> Self::Type {
        let max = 10;
        let val = self.random_rate(max);
        match val {
            0 => Advanced::Hp,
            1 => Advanced::Mp,
            4 => Advanced::Heal,
            5 => Advanced::Exp,
            7 => Advanced::Def,
            8 => Advanced::Atk,
            9 => Advanced::Speed,
            10 => Advanced::Special,
            11 => Advanced::Crystal,
            12 => Advanced::Powder,
            13 => Advanced::Gem,
            _=> Advanced::None,
        }
    }
}
impl Default for Advanced {
    fn default() -> Self {
        Self::None
    }
}
impl fmt::Display for Advanced {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let v:String;
        match *self {
            Advanced::Hp => v = String::from("Hp"),
            Advanced::Mp => v = String::from("Mp"),
            Advanced::Heal => v = String::from("Heal"),
            Advanced::Exp => v = String::from("Exp"),
            Advanced::Def => v = String::from("Def"),
            Advanced::Atk => v = String::from("Atk"),
            Advanced::Speed => v = String::from("Speed"),
            Advanced::Special => v = String::from("Special"),
            Advanced::Crystal => v = String::from("Crystal"),
            Advanced::Powder => v = String::from("Powder"),
            Advanced::Gem  => v = String::from("Gem"),
            Advanced::None => v = String::from(""),
        }
        write!(f, "{}", v.as_str())
    }
}
impl Item for Advanced {
    fn value(&self) -> f64 {
        match *self {
            Advanced::Hp => 10.0,
            Advanced::Mp => 5.0,
            Advanced::Heal => 10.0,
            Advanced::Exp => 1.0,
            Advanced::Def => 1.0,
            Advanced::Atk => 1.0,
            Advanced::Speed => 1.0,
            Advanced::Special => 1.0,
            Advanced::Crystal => 30.0,
            Advanced::Powder => 40.0,
            Advanced::Gem => 50.0,
            _=> 0.0,
        }
    }
    fn cost(&self) -> f64 {
        match *self {
            Advanced::Hp => 20.0,
            Advanced::Mp => 50.0,
            Advanced::Heal => 10.0,
            Advanced::Exp => 150.0,
            Advanced::Def => 80.0,
            Advanced::Atk => 90.0,
            Advanced::Speed => 70.0,
            Advanced::Special => 100.0,
            Advanced::Crystal => 40.0,
            Advanced::Powder => 30.0,
            Advanced::Gem => 150.0,
            _=> 0.0,
        }
    }
    fn resell(&self) -> f64 {
        match *self {
            Advanced::Hp => 10.0,
            Advanced::Mp => 25.0,
            Advanced::Heal => 5.0,
            Advanced::Exp => 75.0,
            Advanced::Def => 40.0,
            Advanced::Atk => 45.0,
            Advanced::Speed => 35.0,
            Advanced::Special => 50.0,
            Advanced::Crystal => 20.0,
            Advanced::Powder => 15.0,
            Advanced::Gem => 90.0,
            _=> 0.0,
        }
    }
}