1257 lines
40 KiB
Rust
1257 lines
40 KiB
Rust
use uuid::Uuid;
|
|
use rand::prelude::*;
|
|
|
|
use failure::Error;
|
|
use failure::err_msg;
|
|
|
|
use skill::{Skill, Cast};
|
|
use game::{Colour, Disable, Immunity, Event, EventConstruct};
|
|
use effect::{Cooldown, Effect};
|
|
use spec::{Spec};
|
|
use item::{Item};
|
|
|
|
#[derive(Debug,Clone,Serialize,Deserialize)]
|
|
pub struct Colours {
|
|
pub red: u8,
|
|
pub green: u8,
|
|
pub blue: u8,
|
|
}
|
|
|
|
impl Colours {
|
|
pub fn new() -> Colours {
|
|
Colours { red: 0, green: 0, blue: 0 }
|
|
}
|
|
|
|
pub fn from_construct(construct: &Construct) -> Colours {
|
|
let mut count = Colours::new();
|
|
|
|
for spec in construct.specs.iter() {
|
|
let v = Item::from(*spec);
|
|
v.colours(&mut count);
|
|
}
|
|
|
|
for cs in construct.skills.iter() {
|
|
let v = Item::from(cs.skill);
|
|
v.colours(&mut count);
|
|
}
|
|
|
|
count
|
|
}
|
|
}
|
|
|
|
|
|
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
|
|
pub struct ConstructSkill {
|
|
pub skill: Skill,
|
|
pub cd: Cooldown,
|
|
// used for Uon client
|
|
pub disabled: bool,
|
|
}
|
|
|
|
impl ConstructSkill {
|
|
pub fn new(skill: Skill) -> ConstructSkill {
|
|
ConstructSkill {
|
|
skill,
|
|
cd: skill.delay(),
|
|
disabled: false,
|
|
}
|
|
}
|
|
|
|
pub fn set_cooldown(&mut self, cd: Cooldown) -> () {
|
|
self.cd = cd;
|
|
}
|
|
}
|
|
|
|
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
|
|
pub enum EffectMeta {
|
|
CastOnHit(Skill), // maybe needs source/target
|
|
CastTick { source: Uuid, target: Uuid, skill: Skill, speed: usize, amount: usize, id: Uuid },
|
|
AddedDamage(usize),
|
|
Multiplier(usize),
|
|
}
|
|
|
|
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
|
|
pub struct ConstructEffect {
|
|
pub effect: Effect,
|
|
pub duration: u8,
|
|
pub meta: Option<EffectMeta>,
|
|
}
|
|
|
|
impl ConstructEffect {
|
|
pub fn new(effect: Effect, duration: u8) -> ConstructEffect {
|
|
ConstructEffect { effect, duration, meta: None }
|
|
}
|
|
|
|
pub fn set_meta(mut self, meta: EffectMeta) -> ConstructEffect {
|
|
self.meta = Some(meta);
|
|
self
|
|
}
|
|
|
|
pub fn get_duration(&self) -> u8 {
|
|
self.duration
|
|
}
|
|
|
|
pub fn get_multiplier(&self) -> usize {
|
|
match self.meta {
|
|
Some(EffectMeta::Multiplier(s)) => s,
|
|
_ => 0
|
|
}
|
|
}
|
|
|
|
pub fn get_skill(&self) -> Option<Skill> {
|
|
match self.meta {
|
|
Some(EffectMeta::CastTick { source: _, target: _, skill, speed: _, amount: _, id: _ }) => Some(skill),
|
|
Some(EffectMeta::CastOnHit(s)) => Some(s),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
pub fn get_tick_id(&self) -> Option<Uuid> {
|
|
match self.meta {
|
|
Some(EffectMeta::CastTick { source: _, target: _, skill: _, speed: _, amount: _, id }) => Some(id),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
|
|
pub enum Stat {
|
|
RedLife,
|
|
RedPower,
|
|
RedDamageReceived,
|
|
RedHealingReceived,
|
|
|
|
GreenLife,
|
|
GreenPower,
|
|
// GreenDamageReceived, // green damage is only applied by inverting healing
|
|
GreenHealingReceived,
|
|
|
|
BlueLife,
|
|
BluePower,
|
|
BlueDamageReceived,
|
|
BlueHealingReceived,
|
|
|
|
Speed,
|
|
|
|
Cooldowns,
|
|
EffectsCount,
|
|
Skills(Colour),
|
|
TickDamage(Effect),
|
|
}
|
|
|
|
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
|
|
pub struct ConstructStat {
|
|
base: usize,
|
|
value: usize,
|
|
max: usize,
|
|
pub stat: Stat,
|
|
}
|
|
|
|
impl ConstructStat {
|
|
// pub fn set(&mut self, v: usize, specs: &Vec<Spec>) -> &mut ConstructStat {
|
|
// self.base = v;
|
|
// self.recalculate(specs)
|
|
// }
|
|
|
|
pub fn recalculate(&mut self, specs: &Vec<Spec>, player_colours: &Colours) -> &mut ConstructStat {
|
|
let specs = specs
|
|
.iter()
|
|
.filter(|s| s.affects().contains(&self.stat))
|
|
.map(|s| *s)
|
|
.collect::<Vec<Spec>>();
|
|
|
|
// applied with fold because it can be zeroed or multiplied
|
|
// but still needs access to the base amount
|
|
let value = specs.iter().fold(self.base, |acc, s| s.apply(acc, self.base, player_colours));
|
|
self.value = value;
|
|
self.max = value;
|
|
|
|
self
|
|
}
|
|
|
|
pub fn reduce(&mut self, amt: usize) -> &mut ConstructStat {
|
|
self.value = self.value.saturating_sub(amt);
|
|
self
|
|
}
|
|
|
|
pub fn increase(&mut self, amt: usize) -> &mut ConstructStat {
|
|
self.value = *[
|
|
self.value.saturating_add(amt),
|
|
self.max
|
|
].iter().min().unwrap();
|
|
|
|
self
|
|
}
|
|
|
|
pub fn force(&mut self, v: usize) -> &mut ConstructStat {
|
|
self.base = v;
|
|
self.value = v;
|
|
self.max = v;
|
|
|
|
self
|
|
}
|
|
}
|
|
|
|
#[derive(Debug,Clone,Serialize,Deserialize)]
|
|
pub struct ConstructSkeleton {
|
|
pub id: Uuid,
|
|
pub account: Uuid,
|
|
pub img: Uuid,
|
|
pub name: String,
|
|
}
|
|
|
|
#[derive(Debug,Clone,Serialize,Deserialize)]
|
|
pub struct Construct {
|
|
pub id: Uuid,
|
|
pub account: Uuid,
|
|
pub img: Uuid,
|
|
pub red_power: ConstructStat,
|
|
pub red_life: ConstructStat,
|
|
pub blue_life: ConstructStat,
|
|
pub blue_power: ConstructStat,
|
|
pub green_power: ConstructStat,
|
|
pub speed: ConstructStat,
|
|
pub green_life: ConstructStat,
|
|
// pub evasion: ConstructStat,
|
|
pub skills: Vec<ConstructSkill>,
|
|
pub effects: Vec<ConstructEffect>,
|
|
pub specs: Vec<Spec>,
|
|
pub colours: Colours,
|
|
pub name: String,
|
|
}
|
|
|
|
impl Construct {
|
|
pub fn new() -> Construct {
|
|
let id = Uuid::new_v4();
|
|
return Construct {
|
|
id,
|
|
account: id,
|
|
img: Uuid::new_v4(),
|
|
red_power: ConstructStat { base: 320, value: 320, max: 320, stat: Stat::RedPower },
|
|
red_life: ConstructStat { base: 125, value: 125, max: 125, stat: Stat::RedLife },
|
|
blue_power: ConstructStat { base: 320, value: 320, max: 320, stat: Stat::BluePower },
|
|
blue_life: ConstructStat { base: 125, value: 125, max: 125, stat: Stat::BlueLife },
|
|
green_power: ConstructStat { base: 320, value: 320, max: 320, stat: Stat::GreenPower },
|
|
green_life: ConstructStat { base: 800, value: 800, max: 800, stat: Stat::GreenLife },
|
|
speed: ConstructStat { base: 100, value: 100, max: 100, stat: Stat::Speed },
|
|
// evasion: ConstructStat { base: 0, value: 0, max: 0, stat: Stat::Evasion },
|
|
skills: vec![],
|
|
effects: vec![],
|
|
specs: vec![],
|
|
colours: Colours::new(),
|
|
name: String::new(),
|
|
};
|
|
}
|
|
|
|
pub fn from_skeleton(skeleton: &ConstructSkeleton) -> Construct {
|
|
return Construct {
|
|
id: skeleton.id,
|
|
account: skeleton.account,
|
|
img: skeleton.img,
|
|
name: skeleton.name.clone(),
|
|
|
|
.. Construct::new()
|
|
};
|
|
}
|
|
|
|
pub fn to_skeleton(&self) -> ConstructSkeleton {
|
|
ConstructSkeleton {
|
|
id: self.id,
|
|
account: self.account,
|
|
img: self.img,
|
|
name: self.name.clone(),
|
|
}
|
|
}
|
|
|
|
|
|
pub fn named(mut self, name: &String) -> Construct {
|
|
self.name = name.clone();
|
|
self
|
|
}
|
|
|
|
pub fn set_account(mut self, account: Uuid) -> Construct {
|
|
self.account = account;
|
|
self
|
|
}
|
|
|
|
pub fn new_img(mut self) -> Construct {
|
|
self.img = Uuid::new_v4();
|
|
self
|
|
}
|
|
|
|
pub fn new_name(self, name: String) -> Result<Construct, Error> {
|
|
if name.len() > 20 {
|
|
return Err(err_msg("20 character name maximum"));
|
|
}
|
|
Ok(self.named(&name))
|
|
}
|
|
|
|
pub fn learn(mut self, s: Skill) -> Construct {
|
|
self.skills.push(ConstructSkill::new(s));
|
|
self.colours = Colours::from_construct(&self);
|
|
self
|
|
}
|
|
|
|
pub fn learn_mut(&mut self, s: Skill) -> &mut Construct {
|
|
self.skills.push(ConstructSkill::new(s));
|
|
self.calculate_colours()
|
|
}
|
|
|
|
pub fn forget(&mut self, skill: Skill) -> Result<&mut Construct, Error> {
|
|
match self.skills.iter().position(|s| s.skill == skill) {
|
|
Some(i) => {
|
|
self.skills.remove(i);
|
|
return Ok(self.calculate_colours());
|
|
},
|
|
None => Err(format_err!("{:?} does not know {:?}", self.name, skill)),
|
|
}
|
|
}
|
|
|
|
pub fn spec_add(&mut self, spec: Spec) -> Result<&mut Construct, Error> {
|
|
if self.specs.len() >= 3 {
|
|
return Err(err_msg("maximum specs equipped"));
|
|
}
|
|
|
|
self.specs.push(spec);
|
|
return Ok(self.calculate_colours());
|
|
}
|
|
|
|
pub fn spec_remove(&mut self, spec: Spec) -> Result<&mut Construct, Error> {
|
|
match self.specs.iter().position(|s| *s == spec) {
|
|
Some(p) => self.specs.remove(p),
|
|
None => return Err(err_msg("spec not found")),
|
|
};
|
|
|
|
Ok(self.calculate_colours())
|
|
}
|
|
|
|
fn calculate_colours(&mut self) -> &mut Construct {
|
|
self.colours = Colours::from_construct(&self);
|
|
self
|
|
}
|
|
|
|
pub fn apply_modifiers(&mut self, player_colours: &Colours) -> &mut Construct {
|
|
self.red_power.recalculate(&self.specs, player_colours);
|
|
self.red_life.recalculate(&self.specs, player_colours);
|
|
self.blue_power.recalculate(&self.specs, player_colours);
|
|
self.blue_life.recalculate(&self.specs, player_colours);
|
|
// self.evasion.recalculate(&self.specs, &self.colours, player_colours);
|
|
self.speed.recalculate(&self.specs, player_colours);
|
|
self.green_power.recalculate(&self.specs, player_colours);
|
|
self.green_life.recalculate(&self.specs, player_colours);
|
|
|
|
self
|
|
}
|
|
|
|
pub fn is_ko(&self) -> bool {
|
|
self.green_life.value == 0
|
|
}
|
|
|
|
pub fn force_ko(&mut self) -> &mut Construct {
|
|
self.green_life.value = 0;
|
|
self
|
|
}
|
|
|
|
pub fn disabled(&self, skill: Skill) -> Option<Disable> {
|
|
if self.is_ko() && !skill.ko_castable() {
|
|
return Some(vec![Effect::Ko]);
|
|
}
|
|
|
|
let disables = self.effects.iter()
|
|
.filter(|e| e.effect.disables_skill(skill))
|
|
.map(|e| e.effect)
|
|
.collect::<Vec<Effect>>();
|
|
|
|
if disables.len() > 0 {
|
|
return Some(disables);
|
|
}
|
|
|
|
None
|
|
}
|
|
|
|
pub fn immune(&self, skill: Skill) -> Option<Immunity> {
|
|
// also checked in resolve stage so shouldn't happen really
|
|
if self.is_ko() {
|
|
return Some(vec![Effect::Ko]);
|
|
}
|
|
|
|
let immunities = self.effects.iter()
|
|
.filter(|e| e.effect.immune(skill))
|
|
.map(|e| e.effect)
|
|
.collect::<Vec<Effect>>();
|
|
|
|
if immunities.len() > 0 {
|
|
return Some(immunities);
|
|
}
|
|
|
|
None
|
|
}
|
|
|
|
pub fn additional_skills(&self, skill: Skill) -> Vec<Skill> {
|
|
self.effects.iter()
|
|
.filter_map(|e| skill.additional_skill(e.effect))
|
|
.collect::<Vec<Skill>>()
|
|
}
|
|
|
|
pub fn is_stunned(&self) -> bool {
|
|
self.available_skills().len() == 0
|
|
}
|
|
|
|
pub fn affected(&self, effect: Effect) -> bool {
|
|
self.effects.iter().any(|s| s.effect == effect)
|
|
}
|
|
|
|
pub fn available_skills(&self) -> Vec<&ConstructSkill> {
|
|
self.skills.iter()
|
|
.filter(|s| s.cd.is_none())
|
|
.filter(|s| self.disabled(s.skill).is_none())
|
|
.collect()
|
|
}
|
|
|
|
pub fn mob_select_skill(&self) -> Option<Skill> {
|
|
let available = self.available_skills();
|
|
|
|
if available.len() == 0 {
|
|
return None;
|
|
}
|
|
|
|
let mut rng = thread_rng();
|
|
|
|
let i = match available.len() {
|
|
1 => 0,
|
|
_ => rng.gen_range(0, available.len()),
|
|
};
|
|
|
|
return Some(available[i].skill);
|
|
|
|
// let highest_cd = available.iter()
|
|
// .filter(|s| s.skill.base_cd().is_some())
|
|
// .max_by_key(|s| s.skill.base_cd().unwrap());
|
|
|
|
// return match highest_cd {
|
|
// Some(s) => Some(s.skill),
|
|
// None => Some(available[0].skill),
|
|
// };
|
|
}
|
|
|
|
pub fn knows(&self, skill: Skill) -> bool {
|
|
self.skills.iter().any(|s| s.skill == skill)
|
|
}
|
|
|
|
pub fn skill_on_cd(&self, skill: Skill) -> Option<&ConstructSkill> {
|
|
self.skills.iter().find(|s| s.skill == skill && s.cd.is_some())
|
|
}
|
|
|
|
pub fn set_construct_delays(&mut self) -> () {
|
|
// for every multiple of speed threshold delays are reduced by 1 at start of game
|
|
let speed_threshold = 250;
|
|
let delay_reduction = self.speed.value.wrapping_div(speed_threshold);
|
|
|
|
self.skills
|
|
.iter_mut()
|
|
.for_each(|s| match s.skill.delay() {
|
|
Some(cd) => match cd.saturating_sub(delay_reduction) {
|
|
0 => s.set_cooldown(None),
|
|
_ => s.set_cooldown(Some(cd.saturating_sub(delay_reduction)))
|
|
},
|
|
None => s.set_cooldown(None)
|
|
});
|
|
}
|
|
|
|
pub fn skill_set_cd(&mut self, skill: Skill) -> &mut Construct {
|
|
// println!("{:?} {:?} skill cooldown set", self.name, skill);
|
|
|
|
// tests force resolve some skills
|
|
// which cause the game to attempt to put them on cd
|
|
// even though the construct doesn't know the skill
|
|
// if let Some(i) = self.skills.iter().position(|s| s.skill == skill) {
|
|
// self.skills.remove(i);
|
|
// self.skills.insert(i, ConstructSkill::new(skill));
|
|
//}
|
|
if let Some(sk) = self.skills.iter_mut().find(|s| s.skill == skill) {
|
|
sk.set_cooldown(skill.base_cd());
|
|
}
|
|
|
|
self
|
|
}
|
|
|
|
pub fn increase_cooldowns(&mut self, turns: usize) -> Vec<Event> {
|
|
let mut cd_event = false;
|
|
for skill in self.skills.iter_mut() {
|
|
if skill.skill.base_cd().is_some() { // if has a cooldown
|
|
cd_event = true;
|
|
match skill.cd {
|
|
Some(cd) => {
|
|
skill.cd = Some(cd.saturating_add(turns));
|
|
},
|
|
None => {
|
|
skill.cd = Some(turns);
|
|
},
|
|
}
|
|
}
|
|
}
|
|
|
|
if cd_event {
|
|
return vec![Event::CooldownIncrease { construct: self.id, turns }];
|
|
}
|
|
|
|
return vec![];
|
|
}
|
|
|
|
pub fn reduce_cooldowns(&mut self) -> &mut Construct {
|
|
for skill in self.skills.iter_mut() {
|
|
// if used cooldown
|
|
if skill.skill.base_cd().is_some() {
|
|
// what is the current cd
|
|
if let Some(current_cd) = skill.cd {
|
|
// if it's 1 set it to none
|
|
if current_cd == 1 {
|
|
skill.cd = None;
|
|
continue;
|
|
}
|
|
|
|
// otherwise decrement it
|
|
skill.cd = Some(current_cd.saturating_sub(1));
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
self
|
|
}
|
|
|
|
pub fn reduce_effect_durations(&mut self) -> &mut Construct {
|
|
self.effects = self.effects.clone().into_iter().filter_map(|mut effect| {
|
|
effect.duration = effect.duration.saturating_sub(1);
|
|
|
|
// println!("{:?}", effect);
|
|
|
|
if effect.duration == 0 {
|
|
return None;
|
|
}
|
|
|
|
// info!("reduced effect {:?}", effect);
|
|
return Some(effect);
|
|
}).collect::<Vec<ConstructEffect>>();
|
|
|
|
self
|
|
}
|
|
|
|
// Stats
|
|
pub fn stat(&self, stat: Stat) -> usize {
|
|
match stat {
|
|
Stat::RedLife => self.red_life.value,
|
|
Stat::RedPower => self.modified_amount(self.red_power.value, Stat::RedPower),
|
|
// Stat::RedDamageReceived => self.modified_amount(self.red_power.value, Stat::RedPower),
|
|
|
|
Stat::GreenLife => self.green_life.value,
|
|
Stat::GreenPower => self.modified_amount(self.red_power.value, Stat::GreenPower),
|
|
// Stat::GreenDamageReceived => self.modified_amount(self.red_power.value, Stat::RedPower),
|
|
|
|
Stat::BlueLife => self.blue_life.value,
|
|
Stat::BluePower => self.modified_amount(self.red_power.value, Stat::BluePower),
|
|
// Stat::BlueDamageReceived => self.modified_amount(self.red_power.value, Stat::RedPower),
|
|
|
|
Stat::Speed => self.modified_amount(self.speed.value, Stat::Speed),
|
|
|
|
Stat::Cooldowns => self.skills.iter().filter(|cs| cs.cd.is_some()).count(),
|
|
Stat::Skills(colour) => self.skills.iter().filter(|cs| cs.skill.colours().contains(&colour)).count(),
|
|
Stat::EffectsCount => self.effects.iter().filter(|ce| !ce.effect.hidden()).count(),
|
|
|
|
Stat::TickDamage(effect) => self.tick_damage(effect),
|
|
|
|
_ => panic!("{:?} cannot be calculated without an amount", stat),
|
|
}
|
|
}
|
|
|
|
fn tick_damage(&self, effect: Effect) -> usize {
|
|
match self.effects.iter().find_map(|ce| match ce.effect == effect {
|
|
true => match ce.meta {
|
|
Some(EffectMeta::CastTick { source: _, target: _, skill: _, speed: _, amount, id: _ }) => Some(amount),
|
|
_ => None,
|
|
},
|
|
false => None,
|
|
}) {
|
|
Some(amount) => amount,
|
|
None => {
|
|
warn!("no tick damage {:?} {:?}", self.effects, effect);
|
|
0
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn skill_speed(&self, s: Skill) -> usize {
|
|
self.stat(Stat::Speed).saturating_mul(s.speed() as usize)
|
|
}
|
|
|
|
// todo complete with specs
|
|
pub fn skill_is_aoe(&self, s: Skill) -> bool {
|
|
s.aoe()
|
|
}
|
|
|
|
fn reduce_green_life(&mut self, amount: usize) {
|
|
self.green_life.reduce(amount);
|
|
if self.affected(Effect::Sustain) && self.stat(Stat::GreenLife) == 0 {
|
|
self.green_life.value = 1;
|
|
}
|
|
}
|
|
|
|
fn modified_amount(&self, amount: usize, modification: Stat) -> usize {
|
|
let mods = self.effects.iter()
|
|
.filter(|e| e.effect.modifications().contains(&modification))
|
|
.map(|e| (e.effect, e.meta))
|
|
.collect::<Vec<(Effect, Option<EffectMeta>)>>();
|
|
|
|
return mods.iter()
|
|
.fold(amount, |acc, fx| fx.0.apply(acc, fx.1))
|
|
}
|
|
|
|
pub fn damage(&mut self, amount: usize, colour: Colour) -> Vec<Event> {
|
|
if self.is_ko() { return vec![Event::TargetKo { construct: self.id }] }
|
|
|
|
match colour {
|
|
Colour::Red => self.deal_red_damage(amount),
|
|
Colour::Blue => self.deal_blue_damage(amount),
|
|
Colour::Green => panic!("green damage should be implemented as Action::Healing"),
|
|
}
|
|
}
|
|
|
|
fn deal_red_damage(&mut self, amount: usize) -> Vec<Event> {
|
|
let mut events = vec![];
|
|
|
|
let construct = self.id;
|
|
|
|
let modified_amount = self.modified_amount(amount, Stat::RedDamageReceived);
|
|
|
|
match self.affected(Effect::Invert) {
|
|
false => {
|
|
// calculate amount of damage red_life will not absorb
|
|
// eg 50 red_life 25 damage -> 0 remainder 25 mitigation
|
|
// 50 red_life 100 damage -> 50 remainder 50 mitigation
|
|
// 50 red_life 5 damage -> 0 remainder 5 mitigation
|
|
let remainder = modified_amount.saturating_sub(self.red_life.value);
|
|
let mitigation = modified_amount.saturating_sub(remainder);
|
|
|
|
// reduce red_life by mitigation amount
|
|
self.red_life.reduce(mitigation);
|
|
|
|
// deal remainder to green_life
|
|
let current_green_life = self.stat(Stat::GreenLife);
|
|
self.reduce_green_life(remainder);
|
|
let delta = current_green_life - self.stat(Stat::GreenLife);
|
|
|
|
events.push(
|
|
Event::Damage {
|
|
construct,
|
|
amount: delta,
|
|
mitigation,
|
|
colour: Colour::Red,
|
|
display: EventConstruct::new(self),
|
|
}
|
|
);
|
|
|
|
if self.is_ko() {
|
|
events.push(Event::Ko { construct });
|
|
}
|
|
},
|
|
true => {
|
|
|
|
// 50/100 green life
|
|
// 0/100 red life
|
|
// 200 red dmg
|
|
//
|
|
// 50 green healing
|
|
// 100 red healing
|
|
// 50 red overhealing
|
|
let current_green_life = self.stat(Stat::GreenLife);
|
|
self.green_life.increase(modified_amount);
|
|
let new_green_life = self.green_life.value;
|
|
let green_healing = new_green_life - current_green_life;
|
|
|
|
let recharge = modified_amount - green_healing;
|
|
let current_red_life = self.red_life.value;
|
|
self.red_life.increase(recharge);
|
|
let red_healing = self.red_life.value - current_red_life;
|
|
let overhealing = recharge - red_healing;
|
|
|
|
if green_healing > 0 {
|
|
events.push(
|
|
Event::Healing {
|
|
construct,
|
|
amount: green_healing,
|
|
overhealing: 0,
|
|
display: EventConstruct::new(self),
|
|
colour: Colour::Green
|
|
}
|
|
);
|
|
}
|
|
|
|
if red_healing > 0 {
|
|
events.push(
|
|
Event::Healing {
|
|
construct,
|
|
amount: recharge,
|
|
overhealing,
|
|
display: EventConstruct::new(self),
|
|
colour: Colour::Red
|
|
}
|
|
);
|
|
}
|
|
}
|
|
};
|
|
|
|
return events;
|
|
}
|
|
|
|
fn deal_blue_damage(&mut self, amount: usize) -> Vec<Event> {
|
|
let mut events = vec![];
|
|
let construct = self.id;
|
|
|
|
let modified_amount = self.modified_amount(amount, Stat::BlueDamageReceived);
|
|
|
|
match self.affected(Effect::Invert) {
|
|
false => {
|
|
let remainder = modified_amount.saturating_sub(self.blue_life.value);
|
|
let mitigation = modified_amount.saturating_sub(remainder);
|
|
|
|
// reduce blue_life by mitigation amount
|
|
self.blue_life.reduce(mitigation);
|
|
|
|
// deal remainder to green_life
|
|
let current_green_life = self.stat(Stat::GreenLife);
|
|
self.reduce_green_life(remainder);
|
|
let delta = current_green_life - self.stat(Stat::GreenLife);
|
|
|
|
events.push(
|
|
Event::Damage {
|
|
construct,
|
|
amount: delta,
|
|
mitigation,
|
|
colour: Colour::Blue,
|
|
display: EventConstruct::new(self),
|
|
}
|
|
);
|
|
|
|
if self.is_ko() {
|
|
events.push(Event::Ko { construct });
|
|
}
|
|
},
|
|
true => {
|
|
let current_green_life = self.stat(Stat::GreenLife);
|
|
self.green_life.increase(modified_amount);
|
|
let new_green_life = self.green_life.value;
|
|
let green_healing = new_green_life - current_green_life;
|
|
|
|
let recharge = modified_amount - green_healing;
|
|
let current_blue_life = self.blue_life.value;
|
|
self.blue_life.increase(recharge);
|
|
let blue_healing = self.blue_life.value - current_blue_life;
|
|
let overhealing = recharge - blue_healing;
|
|
|
|
if green_healing > 0 {
|
|
events.push(
|
|
Event::Healing {
|
|
construct,
|
|
amount: green_healing,
|
|
overhealing: 0,
|
|
display: EventConstruct::new(self),
|
|
colour: Colour::Green
|
|
}
|
|
);
|
|
}
|
|
|
|
if blue_healing > 0 {
|
|
events.push(
|
|
Event::Healing {
|
|
construct,
|
|
amount: recharge,
|
|
overhealing,
|
|
display: EventConstruct::new(self),
|
|
colour: Colour::Blue
|
|
}
|
|
);
|
|
}
|
|
}
|
|
};
|
|
|
|
return events;
|
|
}
|
|
|
|
pub fn healing(&mut self, amount: usize, colour: Colour) -> Vec<Event> {
|
|
if self.is_ko() { return vec![Event::TargetKo { construct: self.id }] }
|
|
|
|
match colour {
|
|
Colour::Red => self.red_healing(amount),
|
|
Colour::Blue => self.blue_healing(amount),
|
|
Colour::Green => self.green_healing(amount),
|
|
}
|
|
}
|
|
|
|
fn red_healing(&mut self, amount: usize) -> Vec<Event> {
|
|
let mut events = vec![];
|
|
|
|
let mods = self.effects.iter()
|
|
.filter(|e| e.effect.modifications().contains(&Stat::RedHealingReceived))
|
|
.map(|e| (e.effect, e.meta))
|
|
.collect::<Vec<(Effect, Option<EffectMeta>)>>();
|
|
|
|
let modified_amount = mods.iter()
|
|
.fold(amount, |acc, fx| fx.0.apply(acc, fx.1));
|
|
|
|
match self.affected(Effect::Invert) {
|
|
false => {
|
|
let current_red_life = self.stat(Stat::RedLife);
|
|
self.red_life.increase(modified_amount);
|
|
let new_red_life = self.red_life.value;
|
|
let healing = new_red_life - current_red_life;
|
|
let overhealing = modified_amount - healing;
|
|
|
|
if healing != 0 {
|
|
events.push(Event::Healing {
|
|
construct: self.id,
|
|
amount: healing,
|
|
overhealing,
|
|
display: EventConstruct::new(self),
|
|
colour: Colour::Red
|
|
});
|
|
}
|
|
},
|
|
true => {
|
|
// Recharge takes a red and blue amount so check for them
|
|
let red_remainder = modified_amount.saturating_sub(self.red_life.value);
|
|
let red_mitigation = modified_amount.saturating_sub(red_remainder);
|
|
|
|
// reduce red_life by mitigation amount
|
|
self.red_life.reduce(red_mitigation);
|
|
|
|
// deal remainder to green_life
|
|
let red_current_green_life = self.stat(Stat::GreenLife);
|
|
self.reduce_green_life(red_remainder);
|
|
let red_damage_amount = red_current_green_life - self.stat(Stat::GreenLife);
|
|
|
|
events.push(Event::Damage {
|
|
construct: self.id,
|
|
amount: red_damage_amount,
|
|
mitigation: red_mitigation,
|
|
colour: Colour::Red,
|
|
display: EventConstruct::new(self),
|
|
});
|
|
}
|
|
}
|
|
|
|
return events;
|
|
}
|
|
|
|
fn green_healing(&mut self, amount: usize) -> Vec<Event> {
|
|
let mut events = vec![];
|
|
|
|
let mods = self.effects.iter()
|
|
.filter(|e| e.effect.modifications().contains(&Stat::GreenHealingReceived))
|
|
.map(|e| (e.effect, e.meta))
|
|
.collect::<Vec<(Effect, Option<EffectMeta>)>>();
|
|
|
|
let modified_amount = mods.iter()
|
|
.fold(amount, |acc, fx| fx.0.apply(acc, fx.1));
|
|
|
|
match self.affected(Effect::Invert) {
|
|
false => {
|
|
let current_green_life = self.stat(Stat::GreenLife);
|
|
self.green_life.increase(modified_amount);
|
|
let new_green_life = self.green_life.value;
|
|
|
|
let healing = new_green_life - current_green_life;
|
|
let overhealing = modified_amount - healing;
|
|
|
|
events.push(Event::Healing {
|
|
construct: self.id,
|
|
amount: healing,
|
|
overhealing,
|
|
colour: Colour::Green,
|
|
display: EventConstruct::new(self),
|
|
});
|
|
},
|
|
true => {
|
|
// there is no green shield (yet)
|
|
let current_green_life = self.stat(Stat::GreenLife);
|
|
self.reduce_green_life(modified_amount);
|
|
let delta = current_green_life - self.stat(Stat::GreenLife);
|
|
|
|
events.push(Event::Damage {
|
|
construct: self.id,
|
|
amount: delta,
|
|
mitigation: 0,
|
|
colour: Colour::Green,
|
|
display: EventConstruct::new(self),
|
|
});
|
|
|
|
if self.is_ko() {
|
|
events.push(Event::Ko { construct: self.id });
|
|
}
|
|
}
|
|
}
|
|
|
|
return events;
|
|
}
|
|
|
|
fn blue_healing(&mut self, amount: usize) -> Vec<Event> {
|
|
let mut events = vec![];
|
|
|
|
let mods = self.effects.iter()
|
|
.filter(|e| e.effect.modifications().contains(&Stat::BlueHealingReceived))
|
|
.map(|e| (e.effect, e.meta))
|
|
.collect::<Vec<(Effect, Option<EffectMeta>)>>();
|
|
|
|
let modified_amount = mods.iter()
|
|
.fold(amount, |acc, fx| fx.0.apply(acc, fx.1));
|
|
|
|
match self.affected(Effect::Invert) {
|
|
false => {
|
|
let current_blue_life = self.stat(Stat::BlueLife);
|
|
self.blue_life.increase(modified_amount);
|
|
let new_blue_life = self.blue_life.value;
|
|
let healing = new_blue_life - current_blue_life;
|
|
let overhealing = modified_amount - healing;
|
|
|
|
if healing != 0 {
|
|
events.push(Event::Healing {
|
|
construct: self.id,
|
|
amount: healing,
|
|
overhealing,
|
|
colour: Colour::Blue,
|
|
display: EventConstruct::new(self),
|
|
});
|
|
}
|
|
},
|
|
true => {
|
|
// Recharge takes a red and blue amount so check for them
|
|
let blue_remainder = modified_amount.saturating_sub(self.blue_life.value);
|
|
let blue_mitigation = modified_amount.saturating_sub(blue_remainder);
|
|
|
|
// reduce blue_life by mitigation amount
|
|
self.blue_life.reduce(blue_mitigation);
|
|
|
|
// deal remainder to green_life
|
|
let blue_current_green_life = self.stat(Stat::GreenLife);
|
|
self.reduce_green_life(blue_remainder);
|
|
let blue_damage_amount = blue_current_green_life - self.stat(Stat::GreenLife);
|
|
|
|
events.push(Event::Damage {
|
|
construct: self.id,
|
|
amount: blue_damage_amount,
|
|
mitigation: blue_mitigation,
|
|
colour: Colour::Blue,
|
|
display: EventConstruct::new(self),
|
|
});
|
|
}
|
|
}
|
|
|
|
return events;
|
|
}
|
|
|
|
pub fn effect_add(&mut self, effect: ConstructEffect) -> Vec<Event> {
|
|
let mut results = vec![];
|
|
if self.is_ko() { return vec![Event::TargetKo { construct: self.id }] }
|
|
|
|
if let Some(p) = self.effects.iter().position(|ce| ce.effect == effect.effect) {
|
|
// duplicate effect
|
|
// replace existing
|
|
|
|
self.effects[p] = effect;
|
|
} else {
|
|
// new effect
|
|
self.effects.push(effect);
|
|
}
|
|
|
|
// probably not a good idea
|
|
if !effect.effect.hidden() {
|
|
results.push(Event::Effect {
|
|
construct: self.id,
|
|
effect: effect.effect,
|
|
duration: effect.duration,
|
|
display: EventConstruct::new(self)
|
|
});
|
|
};
|
|
|
|
return results;
|
|
}
|
|
|
|
pub fn effect_remove(&mut self, effect: Effect) -> Vec<Event> {
|
|
if self.is_ko() { return vec![Event::TargetKo { construct: self.id }] }
|
|
|
|
if let Some(p) = self.effects.iter().position(|ce| ce.effect == effect) {
|
|
let ce = self.effects.remove(p);
|
|
|
|
if ce.effect.hidden() { return vec![] }
|
|
|
|
return vec![Event::Removal {
|
|
construct: self.id,
|
|
effect: effect,
|
|
display: EventConstruct::new(self),
|
|
}];
|
|
}
|
|
|
|
return vec![];
|
|
}
|
|
|
|
pub fn effect_meta(&mut self, effect: Effect, amount: usize) -> Vec<Event> {
|
|
let construct = self.id;
|
|
self.effects.iter_mut()
|
|
.filter(|ce| ce.effect == effect)
|
|
.flat_map(|ce| match ce.meta.unwrap() {
|
|
EffectMeta::AddedDamage(_) => {
|
|
let meta = EffectMeta::AddedDamage(amount);
|
|
ce.meta = Some(meta);
|
|
vec![Event::Meta { construct, effect, meta }]
|
|
},
|
|
EffectMeta::Multiplier(_) => {
|
|
let meta = EffectMeta::Multiplier(amount);
|
|
ce.meta = Some(meta);
|
|
vec![Event::Meta { construct, effect, meta }]
|
|
},
|
|
_ => vec![],
|
|
}
|
|
).collect()
|
|
}
|
|
|
|
pub fn remove_all(&mut self) -> Vec<Event> {
|
|
let mut removals = vec![];
|
|
if self.is_ko() { return vec![Event::TargetKo { construct: self.id }] }
|
|
|
|
while let Some(ce) = self.effects.pop() {
|
|
if !ce.effect.hidden() {
|
|
removals.push(Event::Removal {
|
|
construct: self.id,
|
|
effect: ce.effect,
|
|
display: EventConstruct::new(self),
|
|
});
|
|
}
|
|
}
|
|
|
|
return removals;
|
|
}
|
|
|
|
pub fn on_ko(&mut self, _cast: &Cast, _event: &Event) -> Vec<Cast> {
|
|
self.effects.clear();
|
|
self.green_life.value = 0;
|
|
self.red_life.value = 0;
|
|
self.blue_life.value = 0;
|
|
return vec![];
|
|
}
|
|
|
|
pub fn damage_trigger_casts(&mut self, cast: &Cast, event: &Event) -> Vec<Cast> {
|
|
if self.is_ko() { return vec![] }
|
|
|
|
match event {
|
|
Event::Damage { construct: _, colour, amount: _, mitigation: _, display: _ } => {
|
|
let mut casts = vec![];
|
|
if self.affected(Effect::Electric) && !cast.skill.is_tick() {
|
|
let ConstructEffect { effect: _, duration: _, meta } =
|
|
self.effects.iter().find(|e| e.effect == Effect::Electric).unwrap();
|
|
|
|
match meta {
|
|
Some(EffectMeta::CastOnHit(skill)) =>
|
|
casts.push(Cast::new(self.id, self.account, cast.source, *skill)),
|
|
_ => panic!("no electrify skill {:?}", meta),
|
|
};
|
|
}
|
|
|
|
if self.affected(Effect::Absorb) {
|
|
let ConstructEffect { effect: _, duration: _, meta } =
|
|
self.effects.iter().find(|e| e.effect == Effect::Absorb).unwrap();
|
|
|
|
|
|
let skill = match meta {
|
|
Some(EffectMeta::CastOnHit(s)) => s,
|
|
_ => panic!("no absorb skill {:?}", meta),
|
|
};
|
|
|
|
casts.push(Cast::new(self.id, self.account, self.id, *skill));
|
|
}
|
|
|
|
if self.affected(Effect::Counter) && *colour == Colour::Red {
|
|
let ConstructEffect { effect: _, duration: _, meta } =
|
|
self.effects.iter().find(|e| e.effect == Effect::Counter).unwrap();
|
|
|
|
let skill = match meta {
|
|
Some(EffectMeta::CastOnHit(s)) => s,
|
|
_ => panic!("no counter skill {:?}", meta),
|
|
};
|
|
|
|
casts.push(Cast::new(self.id, self.account, cast.source, *skill));
|
|
}
|
|
|
|
casts
|
|
}
|
|
_ => vec![],
|
|
}
|
|
}
|
|
|
|
// pub fn evade(&self, skill: Skill) -> Option<Event> {
|
|
// if self.evasion.value == 0 {
|
|
// return None;
|
|
// }
|
|
|
|
// let mut rng = thread_rng();
|
|
// let green_life_pct = (self.green_life.value * 100) / self.green_life.value;
|
|
// let evasion_rating = (self.evasion.value * green_life_pct) / 100;
|
|
// let roll = rng.gen_range(0, 100);
|
|
// info!("{:} < {:?}", roll, evasion_rating);
|
|
|
|
// match roll < evasion_rating {
|
|
// true => Some(Event::Evasion {
|
|
// skill,
|
|
// evasion_rating: evasion_rating,
|
|
// }),
|
|
// false => None,
|
|
// }
|
|
// }
|
|
}
|
|
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use construct::*;
|
|
use util::IntPct;
|
|
|
|
#[test]
|
|
fn create_construct_test() {
|
|
let construct = Construct::new()
|
|
.named(&"hatchling".to_string());
|
|
|
|
assert_eq!(construct.name, "hatchling".to_string());
|
|
return;
|
|
}
|
|
|
|
#[test]
|
|
fn construct_colours_test() {
|
|
let mut construct = Construct::new()
|
|
.named(&"redboi".to_string());
|
|
|
|
construct.learn_mut(Skill::Strike);
|
|
construct.spec_add(Spec::LifeGG).unwrap();
|
|
construct.spec_add(Spec::PowerRR).unwrap();
|
|
construct.spec_add(Spec::LifeBB).unwrap();
|
|
|
|
assert_eq!(construct.colours.red, 4);
|
|
assert_eq!(construct.colours.green, 2);
|
|
assert_eq!(construct.colours.blue, 2);
|
|
|
|
return;
|
|
}
|
|
|
|
#[test]
|
|
fn construct_player_modifiers_test() {
|
|
let mut construct = Construct::new()
|
|
.named(&"player player".to_string());
|
|
|
|
construct.spec_add(Spec::PowerRR).unwrap();
|
|
construct.spec_add(Spec::PowerGG).unwrap();
|
|
construct.spec_add(Spec::PowerBB).unwrap();
|
|
construct.learn_mut(Skill::StrikePlusPlus); // 18 reds (24 total)
|
|
|
|
let player_colours = Colours {
|
|
red: 5,
|
|
green: 15,
|
|
blue: 25,
|
|
};
|
|
|
|
construct.apply_modifiers(&player_colours);
|
|
|
|
assert!(construct.red_power.value == construct.red_power.base + construct.red_power.base.pct(15));
|
|
assert!(construct.green_power.value == construct.green_power.base + construct.green_power.base.pct(24));
|
|
assert!(construct.blue_power.value == construct.blue_power.base + construct.blue_power.base.pct(37));
|
|
|
|
return;
|
|
}
|
|
|
|
#[test]
|
|
fn construct_player_modifiers_base_test() {
|
|
let mut construct = Construct::new()
|
|
.named(&"player player".to_string());
|
|
|
|
construct.spec_add(Spec::Power).unwrap();
|
|
construct.spec_add(Spec::Life).unwrap();
|
|
|
|
let player_colours = Colours {
|
|
red: 5,
|
|
green: 15,
|
|
blue: 25,
|
|
};
|
|
|
|
construct.apply_modifiers(&player_colours);
|
|
|
|
assert!(construct.red_power.value == construct.red_power.base + construct.red_power.base.pct(10));
|
|
assert!(construct.green_power.value == construct.green_power.base + construct.green_power.base.pct(10));
|
|
assert!(construct.blue_power.value == construct.blue_power.base + construct.blue_power.base.pct(10));
|
|
assert!(construct.green_life.value == construct.green_life.base + 125);
|
|
|
|
return;
|
|
}
|
|
|
|
#[test]
|
|
fn construct_colour_calc_test() {
|
|
let mut construct = Construct::new()
|
|
.named(&"player player".to_string());
|
|
|
|
construct.spec_add(Spec::PowerRR).unwrap();
|
|
construct.spec_add(Spec::PowerGG).unwrap();
|
|
construct.spec_add(Spec::PowerBB).unwrap();
|
|
|
|
let colours = Colours::from_construct(&construct);
|
|
assert!(colours.red == 2);
|
|
assert!(colours.blue == 2);
|
|
assert!(colours.green == 2);
|
|
|
|
let construct = construct
|
|
.learn(Skill::Strike)
|
|
.learn(Skill::BlastPlusPlus);
|
|
|
|
let colours = Colours::from_construct(&construct);
|
|
assert!(colours.red == 4);
|
|
assert!(colours.blue == 10);
|
|
assert!(colours.green == 2);
|
|
}
|
|
|
|
#[test]
|
|
fn construct_player_modifiers_spec_bonus_test() {
|
|
let mut construct = Construct::new()
|
|
.named(&"player player".to_string());
|
|
|
|
construct.spec_add(Spec::PowerRR).unwrap();
|
|
construct.spec_add(Spec::PowerGG).unwrap();
|
|
construct.spec_add(Spec::PowerBB).unwrap();
|
|
|
|
let player_colours = Colours {
|
|
red: 5,
|
|
green: 0,
|
|
blue: 0,
|
|
};
|
|
|
|
construct.apply_modifiers(&player_colours);
|
|
|
|
assert!(construct.red_power.value == construct.red_power.base + construct.red_power.base.pct(15));
|
|
assert!(construct.green_power.value == construct.green_power.base + construct.green_power.base.pct(10));
|
|
assert!(construct.blue_power.value == construct.blue_power.base + construct.blue_power.base.pct(10));
|
|
|
|
return;
|
|
}
|
|
|
|
#[test]
|
|
fn construct_reduce_cooldown_test() {
|
|
let mut construct = Construct::new()
|
|
.named(&"sleepygirl".to_string());
|
|
|
|
construct.learn_mut(Skill::Sleep);
|
|
|
|
let mut i = 0;
|
|
while construct.skill_on_cd(Skill::Sleep).is_some() {
|
|
construct.reduce_cooldowns();
|
|
i += 1;
|
|
}
|
|
|
|
assert_eq!(i, Skill::Sleep.delay().unwrap());
|
|
}
|
|
|
|
}
|