rpg_stat/
item.rs

1/*!
2# Item
3
4
5*/
6use serde::{Deserialize, Serialize};
7#[cfg(feature = "fltkform")]
8use fltk::{prelude::*, *};
9#[cfg(feature = "fltkform")]
10use fltk_form_derive::*;
11#[cfg(feature = "fltkform")]
12use fltk_form::FltkForm;
13
14use std::fmt;
15use std::fmt::Debug;
16use std::ops::{Add, AddAssign,  Div, DivAssign, Mul, MulAssign, Neg, Rem, RemAssign, Sub, SubAssign};
17
18
19// #Condition
20//use crate::effect::Normal as Effect;
21// #Element
22//use crate::types::Normal as Element;
23use crate::random::Random;
24use crate::stats::Basic as BasicStats;
25use crate::stats::Normal as NormalStats;
26use crate::stats::Advanced as AdvancedStats;
27use crate::stats::Builder;
28
29/*
30# Item trait
31
32This defines the functions for an item
33 * `value()`
34 * `cost()`
35 * `resell()`
36
37These Are applied to all *Item* types
38
39TODO actual economic functions to vary prices
40*/
41pub trait Item {
42    fn value(&self) -> f64;
43    fn cost(&self) -> f64;
44    fn resell(&self) -> f64;
45}
46
47#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
48#[cfg_attr(feature = "fltkform", derive(FltkForm))]
49pub enum Basic {
50    Hp,
51    Mp,
52    None,
53}
54impl Default for Basic {
55    fn default() -> Self {
56        Self::None
57    }
58}impl Random for Basic {
59    type Type = Basic;
60    fn random_type(&self) -> Self::Type {
61        let max = 2;
62        let val = self.random_rate(max);
63        match val {
64            0 => Basic::Hp,
65            1 => Basic::Mp,
66            _=> Basic::None,
67        }
68    }
69}
70impl fmt::Display for Basic {
71    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
72        let v:String;
73        match *self {
74            Basic::Hp => v = String::from("Hp"),
75            Basic::Mp => v = String::from("Mp"),
76            Basic::None => v = String::from(""),
77        }
78        write!(f, "{}", v.as_str())
79    }
80}
81impl Item for Basic {
82    fn value(&self) -> f64 {
83        match *self {
84            Basic::Hp => 10.0,
85            Basic::Mp => 5.0,
86            Basic::None=> 0.0,
87        }
88    }
89    fn cost(&self) -> f64 {
90        match *self {
91            Basic::Hp => 20.0,
92            Basic::Mp => 50.0,
93            Basic::None=> 0.0,
94        }
95    }
96    fn resell(&self) -> f64 {
97        match *self {
98            Basic::Hp => 10.0,
99            Basic::Mp => 25.0,
100            Basic::None=> 0.0,
101        }
102    }
103}
104impl<T:Copy
105    + Default
106    + Debug
107    + AddAssign
108    + Add<Output = T>
109    + Div<Output = T>
110    + DivAssign
111    + Mul<Output = T>
112    + MulAssign
113    + Neg<Output = T>
114    + Rem<Output = T>
115    + RemAssign
116    + Sub<Output = T>
117    + SubAssign
118    + std::cmp::PartialOrd
119    + num::NumCast> Builder<T> for Basic {
120    fn build_basic(&self, id:T, level:T) -> BasicStats<T>{
121        let mut hp:T = num::cast(0).unwrap();
122        let mut mp:T = num::cast(0).unwrap();
123        let xp:T = num::cast(0).unwrap();
124        let xp_next:T = num::cast(0).unwrap();
125        let gp:T = num::cast(0).unwrap();
126        let speed:T = num::cast(0).unwrap();
127        match *self {
128            Basic::Hp => {
129                hp = num::cast(5).unwrap();
130            },
131            Basic::Mp => {
132                mp = num::cast(2).unwrap();
133            },
134            _=> (),
135        }
136        hp *= level;
137        mp *= level;
138        BasicStats {
139            id,
140            xp,
141            xp_next,
142            level,
143            gp,
144            hp,
145            mp,
146            hp_max:hp,
147            mp_max:mp,
148            speed,
149        }
150        
151    }
152    fn build_normal(&self, id:T, level:T) -> NormalStats<T>{
153        let mut hp:T = num::cast(0).unwrap();
154        let mut mp:T = num::cast(0).unwrap();
155        let xp:T = num::cast(0).unwrap();
156        let xp_next:T = num::cast(0).unwrap();
157        let gp:T = num::cast(0).unwrap();
158        let speed:T = num::cast(0).unwrap();
159        let atk:T = num::cast(0).unwrap();
160        let mut def:T = num::cast(0).unwrap();
161        let m_atk:T = num::cast(0).unwrap();
162        let mut m_def:T = num::cast(0).unwrap();
163        match *self {
164            Basic::Hp => {
165                hp = num::cast(5).unwrap();
166                def = num::cast(2).unwrap();
167            },
168            Basic::Mp => {
169                mp = num::cast(2).unwrap();
170                m_def = num::cast(2).unwrap();
171            },
172            _=> (),
173        }
174        hp *= level;
175        mp *= level;
176        def *= level;
177        m_def *= level;
178        NormalStats {
179            id,
180            xp,
181            xp_next,
182            level,
183            gp,
184            hp,
185            mp,
186            hp_max:hp,
187            mp_max:mp,
188            speed,
189            atk,
190            def,
191            m_atk,
192            m_def,
193        }
194    }
195    fn build_advanced(&self, id:T, level:T) -> AdvancedStats<T>{
196        let mut hp:T = num::cast(0).unwrap();
197        let mut mp:T = num::cast(0).unwrap();
198        let xp:T = num::cast(0).unwrap();
199        let xp_next:T = num::cast(0).unwrap();
200        let gp:T = num::cast(0).unwrap();
201        let speed:T = num::cast(0).unwrap();
202        let atk:T = num::cast(0).unwrap();
203        let mut def:T = num::cast(0).unwrap();
204        let m_atk:T = num::cast(0).unwrap();
205        let mut m_def:T = num::cast(0).unwrap();
206        let mut agility:T = num::cast(0).unwrap();
207        let mut strength:T = num::cast(0).unwrap();
208        let mut dexterity:T = num::cast(0).unwrap();
209        let mut constitution:T = num::cast(0).unwrap();
210        let mut intelligence:T = num::cast(0).unwrap();
211        let mut charisma:T = num::cast(0).unwrap();
212        let mut wisdom:T = num::cast(0).unwrap();
213        let age:T = num::cast(0).unwrap();
214        match *self {
215            Basic::Hp => {
216                hp = num::cast(5).unwrap();
217                def = num::cast(2).unwrap();
218                strength = num::cast(2).unwrap();
219                constitution = num::cast(2).unwrap();
220                charisma = num::cast(1).unwrap();
221                dexterity = num::cast(2).unwrap();
222            },
223            Basic::Mp => {
224                mp = num::cast(2).unwrap();
225                m_def = num::cast(2).unwrap();
226                agility = num::cast(2).unwrap();
227                strength = num::cast(1).unwrap();
228                constitution = num::cast(1).unwrap();
229                charisma = num::cast(2).unwrap();
230                dexterity = num::cast(1).unwrap();
231                wisdom = num::cast(2).unwrap();
232                intelligence = num::cast(2).unwrap();
233            },
234            _=> (),
235        }
236        hp *= level;
237        mp *= level;
238        def *= level;
239        m_def *= level;
240        strength *= level;
241        constitution *= level;
242        charisma *= level;
243        dexterity *= level;
244        wisdom *= level;
245        intelligence *= level;
246        AdvancedStats {
247            id,
248            xp,
249            xp_next,
250            level,
251            gp,
252            hp,
253            mp,
254            hp_max:hp,
255            mp_max:mp,
256            speed,
257            atk,
258            def,
259            m_atk,
260            m_def,
261            agility,
262            strength,
263            dexterity,
264            constitution,
265            intelligence,
266            charisma,
267            wisdom,
268            age,
269        }
270    }
271}
272#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
273#[cfg_attr(feature = "fltkform", derive(FltkForm))]
274pub enum Normal {
275    Hp,
276    Mp,
277    Heal,
278    Exp,
279    Def,
280    Atk,
281    Speed,
282    Special,
283    None,
284}
285impl Default for Normal {
286    fn default() -> Self {
287        Self::None
288    }
289}
290impl Random for Normal {
291    type Type = Normal;
292    fn random_type(&self) -> Self::Type {
293        let max = 10;
294        let val = self.random_rate(max);
295        match val {
296            0 => Normal::Hp,
297            1 => Normal::Mp,
298            4 => Normal::Heal,
299            5 => Normal::Exp,
300            7 => Normal::Def,
301            8 => Normal::Atk,
302            9 => Normal::Speed,
303            10 => Normal::Special,
304            _=> Normal::None,
305        }
306    }
307}
308impl fmt::Display for Normal {
309    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
310        let v:String;
311        match *self {
312            Normal::Hp => v = String::from("Hp"),
313            Normal::Mp => v = String::from("Mp"),
314            Normal::Heal => v = String::from("Heal"),
315            Normal::Exp => v = String::from("Exp"),
316            Normal::Def => v = String::from("Def"),
317            Normal::Atk => v = String::from("Atk"),
318            Normal::Speed => v = String::from("Speed"),
319            Normal::Special => v = String::from("Special"),
320            Normal::None => v = String::from(""),
321        }
322        write!(f, "{}", v.as_str())
323    }
324}
325impl Item for Normal {
326    fn value(&self) -> f64 {
327        match *self {
328            Normal::Hp => 10.0,
329            Normal::Mp => 5.0,
330            Normal::Heal => 10.0,
331            Normal::Exp => 1.0,
332            Normal::Def => 1.0,
333            Normal::Atk => 1.0,
334            Normal::Speed => 1.0,
335            Normal::Special => 1.0,
336            _=> 0.0,
337        }
338    }
339    fn cost(&self) -> f64 {
340        match *self {
341            Normal::Hp => 20.0,
342            Normal::Mp => 50.0,
343            Normal::Heal => 10.0,
344            Normal::Exp => 150.0,
345            Normal::Def => 80.0,
346            Normal::Atk => 90.0,
347            Normal::Speed => 70.0,
348            Normal::Special => 100.0,
349            _=> 0.0,
350        }
351    }
352    fn resell(&self) -> f64 {
353        match *self {
354            Normal::Hp => 10.0,
355            Normal::Mp => 25.0,
356            Normal::Heal => 5.0,
357            Normal::Exp => 75.0,
358            Normal::Def => 40.0,
359            Normal::Atk => 45.0,
360            Normal::Speed => 35.0,
361            Normal::Special => 50.0,
362            _=> 0.0,
363        }
364    }
365}
366
367#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
368#[cfg_attr(feature = "fltkform", derive(FltkForm))]
369pub enum Advanced {
370    Hp,
371    Mp,
372    Heal,
373    Exp,
374    Def,
375    Atk,
376    Speed,
377    Special,
378    Crystal,
379    Powder,
380    Gem,
381    None,
382}
383impl Random for Advanced {
384    type Type = Advanced;
385    fn random_type(&self) -> Self::Type {
386        let max = 10;
387        let val = self.random_rate(max);
388        match val {
389            0 => Advanced::Hp,
390            1 => Advanced::Mp,
391            4 => Advanced::Heal,
392            5 => Advanced::Exp,
393            7 => Advanced::Def,
394            8 => Advanced::Atk,
395            9 => Advanced::Speed,
396            10 => Advanced::Special,
397            11 => Advanced::Crystal,
398            12 => Advanced::Powder,
399            13 => Advanced::Gem,
400            _=> Advanced::None,
401        }
402    }
403}
404impl Default for Advanced {
405    fn default() -> Self {
406        Self::None
407    }
408}
409impl fmt::Display for Advanced {
410    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
411        let v:String;
412        match *self {
413            Advanced::Hp => v = String::from("Hp"),
414            Advanced::Mp => v = String::from("Mp"),
415            Advanced::Heal => v = String::from("Heal"),
416            Advanced::Exp => v = String::from("Exp"),
417            Advanced::Def => v = String::from("Def"),
418            Advanced::Atk => v = String::from("Atk"),
419            Advanced::Speed => v = String::from("Speed"),
420            Advanced::Special => v = String::from("Special"),
421            Advanced::Crystal => v = String::from("Crystal"),
422            Advanced::Powder => v = String::from("Powder"),
423            Advanced::Gem  => v = String::from("Gem"),
424            Advanced::None => v = String::from(""),
425        }
426        write!(f, "{}", v.as_str())
427    }
428}
429impl Item for Advanced {
430    fn value(&self) -> f64 {
431        match *self {
432            Advanced::Hp => 10.0,
433            Advanced::Mp => 5.0,
434            Advanced::Heal => 10.0,
435            Advanced::Exp => 1.0,
436            Advanced::Def => 1.0,
437            Advanced::Atk => 1.0,
438            Advanced::Speed => 1.0,
439            Advanced::Special => 1.0,
440            Advanced::Crystal => 30.0,
441            Advanced::Powder => 40.0,
442            Advanced::Gem => 50.0,
443            _=> 0.0,
444        }
445    }
446    fn cost(&self) -> f64 {
447        match *self {
448            Advanced::Hp => 20.0,
449            Advanced::Mp => 50.0,
450            Advanced::Heal => 10.0,
451            Advanced::Exp => 150.0,
452            Advanced::Def => 80.0,
453            Advanced::Atk => 90.0,
454            Advanced::Speed => 70.0,
455            Advanced::Special => 100.0,
456            Advanced::Crystal => 40.0,
457            Advanced::Powder => 30.0,
458            Advanced::Gem => 150.0,
459            _=> 0.0,
460        }
461    }
462    fn resell(&self) -> f64 {
463        match *self {
464            Advanced::Hp => 10.0,
465            Advanced::Mp => 25.0,
466            Advanced::Heal => 5.0,
467            Advanced::Exp => 75.0,
468            Advanced::Def => 40.0,
469            Advanced::Atk => 45.0,
470            Advanced::Speed => 35.0,
471            Advanced::Special => 50.0,
472            Advanced::Crystal => 20.0,
473            Advanced::Powder => 15.0,
474            Advanced::Gem => 90.0,
475            _=> 0.0,
476        }
477    }
478}