Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![allow(dead_code)]
- #![allow(non_snake_case)]
- #![allow(unused_variables)]
- #![allow(unused_mut)]
- #![allow(unused_imports)]
- #![allow(unused_assignments)]
- // Imports and Modules
- #[macro_use]
- extern crate serde_derive;
- #[macro_use]
- extern crate rand;
- extern crate serde;
- extern crate serde_json;
- use rand::Rng;
- use std::cmp;
- // Constants and Globals
- const CONST: i32 = 16i32;
- static mut GLOBAL: i32 = 5i32;
- 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")];
- pub const VITAL_NAMES: [String; 4] = [toS("Level"), toS("Health"), toS("Mana"), toS("Stamina")];
- const STAT_TOTAL: i8 = 13;
- const STAT_LIF: i8 = 0;
- const STAT_MAG: i8 = 1;
- const STAT_END: i8 = 2;
- const STAT_STR: i8 = 3;
- const STAT_CON: i8 = 4;
- const STAT_DEX: i8 = 5;
- const STAT_AGI: i8 = 6;
- const STAT_PER: i8 = 7;
- const STAT_INT: i8 = 8;
- const STAT_WIL: i8 = 9;
- const STAT_CHA: i8 = 10;
- const STAT_SPD: i8 = 11;
- const STAT_LUK: i8 = 12;
- const VITAL_TOTAL: i8 = 4;
- const VITAL_LEVEL: i8 = 0;
- const VITAL_HP: i8 = 1;
- const VITAL_MP: i8 = 2;
- const VITAL_SP: i8 = 3;
- // Classes
- #[derive(Clone)]
- struct Actor {
- name: String,
- inventory: Inventory,
- statTable: StatTable,
- vitalTable: VitalStatTable,
- statusEffects: Vec<StatusEffect>
- }
- impl Actor {
- fn init() -> Actor {
- let mut initActor:Actor = Actor{name: toS("TestActor"), inventory: Inventory{name: toS("Backpack"), contents: Vec::new()},
- statTable: StatTable::init(), vitalTable: VitalStatTable{stats: Vec::new()}, statusEffects: Vec::new()};
- initActor.vitalTable.add_stat(VitalStat{name: toS("Lv"), value: 1, baseMax: 99, bonuses: [0.0, 1.0], finalMax: 99, maxLimits: [1, 99]});
- initActor.vitalTable.add_stat(VitalStat{name: toS("Hp"), value: 50, baseMax: 50, bonuses: [0.0, 1.0], finalMax: 50, maxLimits: [10, 99999]});
- initActor.vitalTable.add_stat(VitalStat{name: toS("Mp"), value: 30, baseMax: 30, bonuses: [0.0, 1.0], finalMax: 30, maxLimits: [1, 9999]});
- initActor.vitalTable.add_stat(VitalStat{name: toS("Sp"), value: 100, baseMax: 100, bonuses: [0.0, 1.0], finalMax: 100, maxLimits: [10, 999]});
- return initActor;
- }
- fn update(&mut self) {
- self.statTable.updateAll();
- self.vitalTable.updateAll();
- }
- }
- #[derive(Clone)]
- struct Stat {
- name: String,
- baseValue: i32,
- baseLimits: [i32; 2],
- finalValue: i32,
- finalLimits: [i32; 2],
- bonuses: [f32; 2]
- }
- impl Stat {
- fn init() -> Stat {
- return Stat{name: toS("NoName"), baseValue: 1, finalValue: 0, bonuses: [0.0, 1.0], baseLimits: [1, 99], finalLimits: [1, 999]};
- }
- fn update(&mut self) {
- self.finalValue = clamp(((self.baseValue as f32 * self.bonuses[1]).round() + self.bonuses[0]) as i32, self.finalLimits[0], self.finalLimits[1]);
- }
- }
- #[derive(Clone)]
- struct StatTable {
- stats: [Stat; STAT_TOTAL as usize]
- }
- impl StatTable {
- fn init() -> StatTable {
- return StatTable{stats: StatTable::returnPopulatedStats()};
- }
- fn get_size(&self) -> usize {
- return self.stats.len();
- }
- fn updateAll(&mut self) {
- for x in 0..self.stats.len() {
- self.stats[x].update()
- }
- }
- fn returnPopulatedStats() -> [Stat; STAT_TOTAL as usize] {
- 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()];
- for x in 0..tempStats.len() {
- tempStats[x] = Stat{name: STAT_NAMES[x], baseValue: 5, finalValue: 5, bonuses: [0.0, 1.0],
- baseLimits: [1, 99], finalLimits: [1, 999]};
- }
- return tempStats;
- }
- }
- #[derive(Clone)]
- struct VitalStat {
- name: String,
- value: i32,
- baseMax: i32,
- finalMax: i32,
- maxLimits: [i32; 2],
- bonuses: [f32; 2]
- }
- impl VitalStat {
- fn init() -> VitalStat {
- return VitalStat{name: toS("NoNameVital"), value: 10, baseMax: 10, bonuses: [0.0, 1.0], finalMax: 10, maxLimits: [10, 99999]};
- }
- fn update(&mut self) {
- self.finalMax = clamp(((self.baseMax as f32 * self.bonuses[1]).round() + self.bonuses[0]) as i32, self.maxLimits[0], self.maxLimits[1]);
- }
- }
- #[derive(Clone)]
- struct VitalStatTable {
- stats: Vec<VitalStat>
- }
- impl VitalStatTable {
- fn init() -> VitalStatTable {
- return VitalStatTable{stats: Vec::new()};
- }
- fn get_size(&self) -> usize {
- return self.stats.len();
- }
- fn add_stat(&mut self, statToAdd: VitalStat) -> () {
- self.stats.push(statToAdd);
- }
- fn updateAll(&mut self) {
- for x in 0..self.stats.len() {
- self.stats[x].update();
- }
- }
- }
- #[derive(Clone)]
- struct StatusEffect {
- name: String,
- targetStat: i32,
- bonuses: [f32; 2]
- }
- impl StatusEffect {
- fn init() -> StatusEffect {
- return StatusEffect{name: toS("NoEffectName"), targetStat: 10, bonuses: [0.0, 1.0]};
- }
- fn update(&mut self) {
- //self.finalMax = clamp(((self.baseMax as f32 * self.bonuses[1]).round() + self.bonuses[0]) as i32, self.maxLimits[0], self.maxLimits[1]);
- }
- }
- #[derive(Clone)]
- struct Inventory {
- name: String,
- contents: Vec<ItemStack>
- }
- impl Inventory {
- fn init() -> Inventory {
- return Inventory{contents: Vec::new(), name: toS("Nameless Inventory")};
- }
- fn output_contents(&self) -> () {
- println!(" - {} - ", self.name);
- for x in 0..self.contents.len() {
- println!("Slot {5}({4}) of {6} contains [Lv{2}]{0} x {1}, value £{3}.",
- self.contents[x].get_item_name(), self.contents[x].get_quantity(), self.contents[x].get_item_level(),
- self.contents[x].get_item_value(), x, x + 1, self.contents.len());
- }
- let mut totalValue:i32 = 0;
- for x in 0..self.contents.len() {
- totalValue += self.contents[x].get_quantity() as i32 * self.contents[x].get_item_value() as i32;
- }
- println!("{}'s total value is £{}.", self.name, totalValue);
- }
- fn get_size(&self) -> usize {
- return self.contents.len();
- }
- fn add_item_stack(&mut self, item_stack: ItemStack) -> () {
- for x in 0..self.contents.len() {
- if self.contents[x].get_item().check_identical(item_stack.get_item()) {
- self.contents[x].quantity += item_stack.get_quantity();
- return;
- }
- }
- self.contents.push(item_stack);
- }
- }
- #[derive(Clone)]
- struct ItemStack {
- item: Item,
- quantity: u16,
- max_stack: u16
- }
- impl ItemStack {
- fn init() -> ItemStack {
- return ItemStack{item: Item::init(), quantity: 0u16, max_stack: 0u16}
- }
- fn get_item(&self) -> Item {
- return self.item.clone();
- }
- fn get_quantity(&self) -> u16 {
- return self.quantity;
- }
- fn get_max_stack(&self) -> u16 {
- return self.max_stack;
- }
- fn get_item_name(&self) -> String {
- return self.item.get_name();
- }
- fn get_item_category(&self) -> String {
- return self.item.get_category();
- }
- fn get_item_value(&self) -> u32 {
- return self.item.get_value();
- }
- fn get_item_level(&self) -> u8 {
- return self.item.get_level();
- }
- }
- #[derive(Clone)]
- struct Item {
- name: String,
- category: String,
- value: u32,
- level: u8
- }
- impl Item {
- fn init() -> Item {
- return Item{name: toS("None"), category: toS("None"), value: 0u32, level: 0u8}
- }
- fn check_identical(&self, other_item: Item) -> bool {
- if !(self.get_name() == other_item.get_name()) {
- return false;
- }
- if !(self.get_value() == other_item.get_value()) {
- return false;
- }
- if !(self.get_level() == other_item.get_level()) {
- return false;
- }
- if !(self.get_category() == other_item.get_category()) {
- return false;
- }
- return true;
- }
- fn get_name(&self) -> String {
- return self.name;
- }
- fn get_category(&self) -> String {
- return self.category;
- }
- fn get_value(&self) -> u32 {
- return self.value;
- }
- fn get_level(&self) -> u8 {
- return self.level;
- }
- }
- #[derive(Serialize, Deserialize, Debug)]
- struct StatusEffectTest {
- //name: String,
- level: i32,
- duration: i32,
- eternal: bool,
- hidden: bool
- }
- // Public functions
- fn fibonacci(n: i32) -> i32 {
- if n == 0 {
- 0
- } else if n == 1 {
- 1
- } else {
- fibonacci(n - 1) + fibonacci(n - 2)
- }
- }
- fn analyze_slice(slice: &[i32]) {
- println!("first element of the slice: {}", slice[0]);
- println!("the slice has {} elements", slice.len());
- }
- fn random_range(min: u16, max: u16) -> u16 {
- let mut rng = rand::thread_rng();
- return min + rng.gen::<u16>() % ((max - min) + 1);
- }
- fn clamp(val: i32, minimum: i32, maximum: i32) -> i32 {
- return cmp::max(minimum, cmp::min(val, maximum))
- }
- fn toS(strng: &'static str) -> String {
- return String::from(strng);
- }
- // Main function
- fn main() -> () {
- let mut rng = rand::thread_rng();
- let item_cheese:Item = Item{name: toS("Cheese"), category: toS("Food"), value: 3u32, level: 1u8};
- let item_cake:Item = Item{name: toS("Cake"), category: toS("Food"), value: 12u32, level: 2u8};
- let item_waffle:Item = Item{name: toS("Waffle"), category: toS("Food"), value: 2u32, level: 1u8};
- let mut testActor:Actor = Actor::init();
- testActor.update();
- println!(" - {} - ", testActor.name);
- let tempStat = testActor.statTable.stats[STAT_STR as usize];
- println!("{}'s {} is {}({}).", testActor.name, tempStat.name, tempStat.finalValue, tempStat.baseValue);
- let mut tempVital = testActor.vitalTable.stats[VITAL_HP as usize];
- println!("{}'s {} is {} / {}({}).", testActor.name, tempVital.name, tempVital.value, tempVital.finalMax, tempVital.baseMax);
- tempVital = testActor.vitalTable.stats[VITAL_MP as usize];
- println!("{}'s {} is {} / {}({}).", testActor.name, tempVital.name, tempVital.value, tempVital.finalMax, tempVital.baseMax);
- tempVital = testActor.vitalTable.stats[VITAL_SP as usize];
- println!("{}'s {} is {} / {}({}).", testActor.name, tempVital.name, tempVital.value, tempVital.finalMax, tempVital.baseMax);
- testActor.inventory.add_item_stack(ItemStack{item: item_cheese, quantity: random_range(1u16, 25u16), max_stack: 99u16});
- testActor.inventory.add_item_stack(ItemStack{item: item_cheese, quantity: random_range(10u16, 40u16), max_stack: 99u16});
- testActor.inventory.add_item_stack(ItemStack{item: item_cake, quantity: random_range(1u16, 5u16), max_stack: 99u16});
- testActor.inventory.add_item_stack(ItemStack{item: item_waffle, quantity: random_range(25u16, 80u16), max_stack: 99u16});
- testActor.inventory.output_contents();
- let sePoison = StatusEffectTest{level: 1, duration: 10, eternal: false, hidden: false};
- let serialized = serde_json::to_string(&sePoison).unwrap();
- println!("serialized = {}", serialized);
- let deserialized: StatusEffectTest = serde_json::from_str(&serialized).unwrap();
- println!("deserialized = {:?}", deserialized);
- /*
- let f = 3.3_f32;
- let g = -3.3_f32;
- assert_eq!(f.round(), 3.0);
- assert_eq!(g.round(), -3.0);
- println!("F is {0}, G is {1}.", f, g);
- let m = 4.5669_f32;
- let n = m.round() as i32;
- println!("M is {0}, N is {1}.", m, n);
- println!("The inventory's size is {0}.", test_inventory.get_size());
- test_inventory.contents.pop();
- println!("The inventory's size is now {0}.", test_inventory.get_size());
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement