Advertisement
Guest User

Untitled

a guest
Jul 1st, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 11.99 KB | None | 0 0
  1. #![allow(dead_code)]
  2. #![allow(non_snake_case)]
  3. #![allow(unused_variables)]
  4. #![allow(unused_mut)]
  5. #![allow(unused_imports)]
  6. #![allow(unused_assignments)]
  7.  
  8. // Imports and Modules
  9. #[macro_use]
  10. extern crate serde_derive;
  11. #[macro_use]
  12. extern crate rand;
  13.  
  14. extern crate serde;
  15. extern crate serde_json;
  16.  
  17. use rand::Rng;
  18. use std::cmp;
  19.  
  20.  
  21. // Constants and Globals
  22. const CONST: i32 = 16i32;
  23. static mut GLOBAL: i32 = 5i32;
  24.  
  25. pub const STAT_NAMES: [String; 13] = [toS("Life"), toS("Magic"), toS("Endurance"), toS("Strength"), toS("Constitution"), toS("Dexterity"), toS("Agility"), toS("Perception"), toS("Intelligence"), toS("Willpower"), toS("Charisma"), toS("Speed"), toS("Luck")];
  26. pub const VITAL_NAMES: [String; 4] = [toS("Level"), toS("Health"), toS("Mana"), toS("Stamina")];
  27.  
  28. const STAT_TOTAL: i8 = 13;
  29. const STAT_LIF: i8 = 0;
  30. const STAT_MAG: i8 = 1;
  31. const STAT_END: i8 = 2;
  32. const STAT_STR: i8 = 3;
  33. const STAT_CON: i8 = 4;
  34. const STAT_DEX: i8 = 5;
  35. const STAT_AGI: i8 = 6;
  36. const STAT_PER: i8 = 7;
  37. const STAT_INT: i8 = 8;
  38. const STAT_WIL: i8 = 9;
  39. const STAT_CHA: i8 = 10;
  40. const STAT_SPD: i8 = 11;
  41. const STAT_LUK: i8 = 12;
  42.  
  43. const VITAL_TOTAL: i8 = 4;
  44. const VITAL_LEVEL: i8 = 0;
  45. const VITAL_HP: i8 = 1;
  46. const VITAL_MP: i8 = 2;
  47. const VITAL_SP: i8 = 3;
  48.  
  49. // Classes
  50. #[derive(Clone)]
  51. struct Actor {
  52.     name: String,
  53.     inventory: Inventory,
  54.     statTable: StatTable,
  55.     vitalTable: VitalStatTable,
  56.     statusEffects: Vec<StatusEffect>
  57. }
  58. impl Actor {
  59.     fn init() -> Actor {
  60.         let mut initActor:Actor = Actor{name: toS("TestActor"), inventory: Inventory{name: toS("Backpack"), contents: Vec::new()},
  61.             statTable: StatTable::init(), vitalTable: VitalStatTable{stats: Vec::new()}, statusEffects: Vec::new()};
  62.        
  63.         initActor.vitalTable.add_stat(VitalStat{name: toS("Lv"), value: 1, baseMax: 99, bonuses: [0.0, 1.0], finalMax: 99, maxLimits: [1, 99]});
  64.         initActor.vitalTable.add_stat(VitalStat{name: toS("Hp"), value: 50, baseMax: 50, bonuses: [0.0, 1.0], finalMax: 50, maxLimits: [10, 99999]});
  65.         initActor.vitalTable.add_stat(VitalStat{name: toS("Mp"), value: 30, baseMax: 30, bonuses: [0.0, 1.0], finalMax: 30, maxLimits: [1, 9999]});
  66.         initActor.vitalTable.add_stat(VitalStat{name: toS("Sp"), value: 100, baseMax: 100, bonuses: [0.0, 1.0], finalMax: 100, maxLimits: [10, 999]});
  67.  
  68.         return initActor;
  69.     }
  70.     fn update(&mut self) {
  71.         self.statTable.updateAll();
  72.         self.vitalTable.updateAll();
  73.     }
  74. }
  75.  
  76. #[derive(Clone)]
  77. struct Stat {
  78.     name: String,
  79.     baseValue: i32,
  80.     baseLimits: [i32; 2],
  81.     finalValue: i32,
  82.     finalLimits: [i32; 2],
  83.     bonuses: [f32; 2]
  84. }
  85. impl Stat {
  86.     fn init() -> Stat {
  87.         return Stat{name: toS("NoName"), baseValue: 1, finalValue: 0, bonuses: [0.0, 1.0], baseLimits: [1, 99], finalLimits: [1, 999]};
  88.     }
  89.     fn update(&mut self) {
  90.         self.finalValue = clamp(((self.baseValue as f32 * self.bonuses[1]).round() + self.bonuses[0]) as i32, self.finalLimits[0], self.finalLimits[1]);
  91.     }
  92. }
  93.  
  94. #[derive(Clone)]
  95. struct StatTable {
  96.     stats: [Stat; STAT_TOTAL as usize]
  97. }
  98. impl StatTable {
  99.     fn init() -> StatTable {
  100.         return StatTable{stats: StatTable::returnPopulatedStats()};
  101.     }
  102.     fn get_size(&self) -> usize {
  103.         return self.stats.len();
  104.     }
  105.     fn updateAll(&mut self) {
  106.         for x in 0..self.stats.len() {
  107.             self.stats[x].update()
  108.         }
  109.     }
  110.     fn returnPopulatedStats() -> [Stat; STAT_TOTAL as usize] {
  111.         let mut tempStats:[Stat; STAT_TOTAL as usize] = [Stat::init(), Stat::init(), Stat::init(), Stat::init(), Stat::init(), Stat::init(), Stat::init(), Stat::init(), Stat::init(), Stat::init(), Stat::init(), Stat::init(), Stat::init()];
  112.         for x in 0..tempStats.len() {
  113.             tempStats[x] = Stat{name: STAT_NAMES[x], baseValue: 5, finalValue: 5, bonuses: [0.0, 1.0],
  114.                 baseLimits: [1, 99], finalLimits: [1, 999]};
  115.         }
  116.         return tempStats;
  117.     }
  118. }
  119.  
  120. #[derive(Clone)]
  121. struct VitalStat {
  122.     name: String,
  123.     value: i32,
  124.     baseMax: i32,
  125.     finalMax: i32,
  126.     maxLimits: [i32; 2],
  127.     bonuses: [f32; 2]
  128. }
  129. impl VitalStat {
  130.     fn init() -> VitalStat {
  131.         return VitalStat{name: toS("NoNameVital"), value: 10, baseMax: 10, bonuses: [0.0, 1.0], finalMax: 10, maxLimits: [10, 99999]};
  132.     }
  133.     fn update(&mut self) {
  134.         self.finalMax = clamp(((self.baseMax as f32 * self.bonuses[1]).round() + self.bonuses[0]) as i32, self.maxLimits[0], self.maxLimits[1]);
  135.     }
  136. }
  137.  
  138. #[derive(Clone)]
  139. struct VitalStatTable {
  140.     stats: Vec<VitalStat>
  141. }
  142. impl VitalStatTable {
  143.     fn init() -> VitalStatTable {
  144.         return VitalStatTable{stats: Vec::new()};
  145.     }
  146.     fn get_size(&self) -> usize {
  147.         return self.stats.len();
  148.     }
  149.     fn add_stat(&mut self, statToAdd: VitalStat) -> () {
  150.         self.stats.push(statToAdd);
  151.     }
  152.     fn updateAll(&mut self) {
  153.         for x in 0..self.stats.len() {
  154.             self.stats[x].update();
  155.         }
  156.     }
  157. }
  158.  
  159. #[derive(Clone)]
  160. struct StatusEffect {
  161.     name: String,
  162.     targetStat: i32,
  163.     bonuses: [f32; 2]
  164. }
  165. impl StatusEffect {
  166.     fn init() -> StatusEffect {
  167.         return StatusEffect{name: toS("NoEffectName"), targetStat: 10, bonuses: [0.0, 1.0]};
  168.     }
  169.     fn update(&mut self) {
  170.         //self.finalMax = clamp(((self.baseMax as f32 * self.bonuses[1]).round() + self.bonuses[0]) as i32, self.maxLimits[0], self.maxLimits[1]);
  171.     }
  172. }
  173.  
  174. #[derive(Clone)]
  175. struct Inventory {
  176.     name: String,
  177.     contents: Vec<ItemStack>
  178. }
  179. impl Inventory {
  180.     fn init() -> Inventory {
  181.         return Inventory{contents: Vec::new(), name: toS("Nameless Inventory")};
  182.     }
  183.     fn output_contents(&self) -> () {
  184.         println!(" - {} - ", self.name);
  185.         for x in 0..self.contents.len() {
  186.             println!("Slot {5}({4}) of {6} contains [Lv{2}]{0} x {1}, value £{3}.",
  187.             self.contents[x].get_item_name(), self.contents[x].get_quantity(), self.contents[x].get_item_level(),
  188.             self.contents[x].get_item_value(), x, x + 1, self.contents.len());
  189.         }
  190.         let mut totalValue:i32 = 0;
  191.         for x in 0..self.contents.len() {
  192.             totalValue += self.contents[x].get_quantity() as i32 * self.contents[x].get_item_value() as i32;
  193.         }
  194.         println!("{}'s total value is £{}.", self.name, totalValue);
  195.     }
  196.     fn get_size(&self) -> usize {
  197.         return self.contents.len();
  198.     }
  199.     fn add_item_stack(&mut self, item_stack: ItemStack) -> () {
  200.         for x in 0..self.contents.len() {
  201.             if self.contents[x].get_item().check_identical(item_stack.get_item()) {
  202.                 self.contents[x].quantity += item_stack.get_quantity();
  203.                 return;
  204.             }
  205.         }
  206.         self.contents.push(item_stack);
  207.     }
  208. }
  209.  
  210. #[derive(Clone)]
  211. struct ItemStack {
  212.     item: Item,
  213.     quantity: u16,
  214.     max_stack: u16
  215. }
  216. impl ItemStack {
  217.     fn init() -> ItemStack {
  218.         return ItemStack{item: Item::init(), quantity: 0u16, max_stack: 0u16}
  219.     }
  220.     fn get_item(&self) -> Item {
  221.         return self.item.clone();
  222.     }
  223.     fn get_quantity(&self) -> u16 {
  224.         return self.quantity;
  225.     }
  226.     fn get_max_stack(&self) -> u16 {
  227.         return self.max_stack;
  228.     }
  229.     fn get_item_name(&self) -> String {
  230.         return self.item.get_name();
  231.     }
  232.     fn get_item_category(&self) -> String {
  233.         return self.item.get_category();
  234.     }
  235.     fn get_item_value(&self) -> u32 {
  236.         return self.item.get_value();
  237.     }
  238.     fn get_item_level(&self) -> u8 {
  239.         return self.item.get_level();
  240.     }
  241. }
  242.  
  243. #[derive(Clone)]
  244. struct Item {
  245.     name: String,
  246.     category: String,
  247.     value: u32,
  248.     level: u8
  249. }
  250. impl Item {
  251.     fn init() -> Item {
  252.         return Item{name: toS("None"), category: toS("None"), value: 0u32, level: 0u8}
  253.     }
  254.     fn check_identical(&self, other_item: Item) -> bool {
  255.         if !(self.get_name() == other_item.get_name()) {
  256.             return false;
  257.         }
  258.         if !(self.get_value() == other_item.get_value()) {
  259.             return false;
  260.         }
  261.         if !(self.get_level() == other_item.get_level()) {
  262.             return false;
  263.         }
  264.         if !(self.get_category() == other_item.get_category()) {
  265.             return false;
  266.         }
  267.         return true;
  268.     }
  269.     fn get_name(&self) -> String {
  270.         return self.name;
  271.     }
  272.     fn get_category(&self) -> String {
  273.         return self.category;
  274.     }
  275.     fn get_value(&self) -> u32 {
  276.         return self.value;
  277.     }
  278.     fn get_level(&self) -> u8 {
  279.         return self.level;
  280.     }
  281. }
  282.  
  283. #[derive(Serialize, Deserialize, Debug)]
  284. struct StatusEffectTest {
  285.     //name: String,
  286.     level: i32,
  287.     duration: i32,
  288.     eternal: bool,
  289.     hidden: bool
  290. }
  291.  
  292.  
  293. // Public functions
  294. fn fibonacci(n: i32) -> i32 {
  295.     if n == 0 {
  296.         0
  297.     } else if n == 1 {
  298.         1
  299.     } else {
  300.         fibonacci(n - 1) + fibonacci(n - 2)
  301.     }
  302. }
  303.  
  304. fn analyze_slice(slice: &[i32]) {
  305.     println!("first element of the slice: {}", slice[0]);
  306.     println!("the slice has {} elements", slice.len());
  307. }
  308.  
  309. fn random_range(min: u16, max: u16) -> u16 {
  310.     let mut rng = rand::thread_rng();
  311.     return min + rng.gen::<u16>() % ((max - min) + 1);
  312. }
  313.  
  314. fn clamp(val: i32, minimum: i32, maximum: i32) -> i32 {
  315.     return cmp::max(minimum, cmp::min(val, maximum))
  316. }
  317.  
  318. fn toS(strng: &'static str) -> String {
  319.    return String::from(strng);
  320. }
  321.  
  322. // Main function
  323. fn main() -> () {
  324.    let mut rng = rand::thread_rng();
  325.    
  326.    let item_cheese:Item = Item{name: toS("Cheese"), category: toS("Food"), value: 3u32, level: 1u8};
  327.    let item_cake:Item = Item{name: toS("Cake"), category: toS("Food"), value: 12u32, level: 2u8};
  328.    let item_waffle:Item = Item{name: toS("Waffle"), category: toS("Food"), value: 2u32, level: 1u8};
  329.  
  330.    let mut testActor:Actor = Actor::init();
  331.    testActor.update();
  332.    println!(" - {} - ", testActor.name);
  333.  
  334.    let tempStat = testActor.statTable.stats[STAT_STR as usize];
  335.    println!("{}'s {} is {}({}).", testActor.name, tempStat.name, tempStat.finalValue, tempStat.baseValue);
  336.  
  337.    let mut tempVital = testActor.vitalTable.stats[VITAL_HP as usize];
  338.    println!("{}'s {} is {} / {}({}).", testActor.name, tempVital.name, tempVital.value, tempVital.finalMax, tempVital.baseMax);
  339.    tempVital = testActor.vitalTable.stats[VITAL_MP as usize];
  340.    println!("{}'s {} is {} / {}({}).", testActor.name, tempVital.name, tempVital.value, tempVital.finalMax, tempVital.baseMax);
  341.    tempVital = testActor.vitalTable.stats[VITAL_SP as usize];
  342.    println!("{}'s {} is {} / {}({}).", testActor.name, tempVital.name, tempVital.value, tempVital.finalMax, tempVital.baseMax);
  343.  
  344.    testActor.inventory.add_item_stack(ItemStack{item: item_cheese, quantity: random_range(1u16, 25u16), max_stack: 99u16});
  345.    testActor.inventory.add_item_stack(ItemStack{item: item_cheese, quantity: random_range(10u16, 40u16), max_stack: 99u16});
  346.    testActor.inventory.add_item_stack(ItemStack{item: item_cake, quantity: random_range(1u16, 5u16), max_stack: 99u16});
  347.    testActor.inventory.add_item_stack(ItemStack{item: item_waffle, quantity: random_range(25u16, 80u16), max_stack: 99u16});
  348.    testActor.inventory.output_contents();
  349.  
  350.  
  351.    let sePoison = StatusEffectTest{level: 1, duration: 10, eternal: false, hidden: false};
  352.  
  353.    let serialized = serde_json::to_string(&sePoison).unwrap();
  354.    println!("serialized = {}", serialized);
  355.  
  356.    let deserialized: StatusEffectTest = serde_json::from_str(&serialized).unwrap();
  357.    println!("deserialized = {:?}", deserialized);
  358.  
  359.    /*
  360.        let f = 3.3_f32;
  361.        let g = -3.3_f32;
  362.        assert_eq!(f.round(), 3.0);
  363.        assert_eq!(g.round(), -3.0);
  364.        println!("F is {0}, G is {1}.", f, g);
  365.  
  366.        let m = 4.5669_f32;
  367.        let n = m.round() as i32;
  368.        println!("M is {0}, N is {1}.", m, n);
  369.        println!("The inventory's size is {0}.", test_inventory.get_size());
  370.        test_inventory.contents.pop();
  371.        println!("The inventory's size is now {0}.", test_inventory.get_size());
  372.    */
  373. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement