hit event

This commit is contained in:
ntr 2019-12-04 19:21:43 +10:00
parent 7c486812f4
commit d53d14233a
4 changed files with 69 additions and 44 deletions

View File

@ -5,4 +5,5 @@ aoe event
cooldown events leak skills cooldown events leak skills
hit event hit event
reqs moving eventconstruct into specific events reqs moving eventconstruct into specific events
cooldown checking -> go through round and find all casters cooldown checking -> go through round and find all casters

View File

@ -4,7 +4,7 @@ use rand::prelude::*;
use failure::Error; use failure::Error;
use failure::err_msg; use failure::err_msg;
use skill::{Skill, Cast, Disable, Event, EventVariant}; use skill::{Skill, Cast, Disable, Event, EventVariant, EventConstruct};
use game::{Colour}; use game::{Colour};
use effect::{Cooldown, Effect}; use effect::{Cooldown, Effect};
use spec::{Spec}; use spec::{Spec};
@ -418,18 +418,16 @@ impl Construct {
pub fn increase_cooldowns(&mut self, turns: usize) -> Vec<Event> { pub fn increase_cooldowns(&mut self, turns: usize) -> Vec<Event> {
let mut events = vec![]; let mut events = vec![];
let state = self.clone();
for skill in self.skills.iter_mut() { for skill in self.skills.iter_mut() {
if skill.skill.base_cd().is_some() { // if has a cooldown if skill.skill.base_cd().is_some() { // if has a cooldown
match skill.cd { match skill.cd {
Some(cd) => { Some(cd) => {
skill.cd = Some(cd.saturating_add(turns)); skill.cd = Some(cd.saturating_add(turns));
events.push(Event::new(EventVariant::CooldownIncrease { turns, skill: skill.skill }, &state)) events.push(Event::new(EventVariant::CooldownIncrease { turns, skill: skill.skill }, self.id))
}, },
None => { None => {
skill.cd = Some(turns); skill.cd = Some(turns);
events.push(Event::new(EventVariant::CooldownIncrease { turns, skill: skill.skill }, &state)) events.push(Event::new(EventVariant::CooldownIncrease { turns, skill: skill.skill }, self.id))
}, },
} }
} }
@ -567,7 +565,7 @@ impl Construct {
let blue = new_blue_life - current_blue_life; let blue = new_blue_life - current_blue_life;
if red != 0 || blue != 0 { if red != 0 || blue != 0 {
events.push(Event::new(EventVariant::Recharge { red, blue }, self)); events.push(Event::new(EventVariant::Recharge { red, blue }, self.id));
} }
}, },
true => { true => {
@ -596,8 +594,9 @@ impl Construct {
events.push(Event::new(EventVariant::Damage { events.push(Event::new(EventVariant::Damage {
amount: red_damage_amount, amount: red_damage_amount,
mitigation: red_mitigation, mitigation: red_mitigation,
colour: Colour::Red colour: Colour::Red,
}, self)); stats: EventConstruct::new(self),
}, self.id));
} }
if blue_amount != 0 { if blue_amount != 0 {
@ -624,8 +623,9 @@ impl Construct {
events.push(Event::new(EventVariant::Damage { events.push(Event::new(EventVariant::Damage {
amount: blue_damage_amount, amount: blue_damage_amount,
mitigation: blue_mitigation, mitigation: blue_mitigation,
colour: Colour::Blue colour: Colour::Blue,
}, self)); stats: EventConstruct::new(self),
}, self.id));
} }
} }
} }
@ -656,7 +656,7 @@ impl Construct {
events.push(Event::new(EventVariant::Healing { events.push(Event::new(EventVariant::Healing {
amount: healing, amount: healing,
overhealing, overhealing,
}, self)); }, self.id));
}, },
true => { true => {
// events.push(Event::new(EventVariant::Inversion { skill })); // events.push(Event::new(EventVariant::Inversion { skill }));
@ -670,7 +670,8 @@ impl Construct {
amount: delta, amount: delta,
mitigation: 0, mitigation: 0,
colour: Colour::Green, colour: Colour::Green,
}, self)); stats: EventConstruct::new(self),
}, self.id));
} }
} }
@ -712,8 +713,9 @@ impl Construct {
amount: delta, amount: delta,
mitigation, mitigation,
colour: Colour::Red, colour: Colour::Red,
stats: EventConstruct::new(self),
}, },
self self.id
)); ));
}, },
true => { true => {
@ -734,12 +736,12 @@ impl Construct {
EventVariant::Healing{ EventVariant::Healing{
amount: healing, amount: healing,
overhealing: overhealing - recharge, overhealing: overhealing - recharge,
}, self }, self.id
)); ));
} }
if recharge > 0 { if recharge > 0 {
events.push(Event::new(EventVariant::Recharge { red: recharge, blue: 0 }, self)); events.push(Event::new(EventVariant::Recharge { red: recharge, blue: 0 }, self.id));
} }
} }
}; };
@ -777,7 +779,8 @@ impl Construct {
amount: delta, amount: delta,
mitigation, mitigation,
colour: Colour::Blue, colour: Colour::Blue,
}, self)); stats: EventConstruct::new(self),
}, self.id));
}, },
true => { true => {
// events.push(Event::new(EventVariant::Inversion { skill })); // events.push(Event::new(EventVariant::Inversion { skill }));
@ -798,12 +801,12 @@ impl Construct {
amount: healing, amount: healing,
overhealing, overhealing,
}, },
self self.id
)); ));
} }
if recharge > 0 { if recharge > 0 {
events.push(Event::new(EventVariant::Recharge { red: 0, blue: recharge }, self)); events.push(Event::new(EventVariant::Recharge { red: 0, blue: recharge }, self.id));
} }
} }
}; };
@ -833,7 +836,7 @@ impl Construct {
effect: effect.effect, effect: effect.effect,
duration: effect.duration, duration: effect.duration,
construct_effects: self.effects.clone(), construct_effects: self.effects.clone(),
}, self }, self.id
); );
return vec![result]; return vec![result];

View File

@ -10,7 +10,7 @@ use failure::Error;
use failure::err_msg; use failure::err_msg;
use construct::{Construct, ConstructEffect, Stat}; use construct::{Construct, ConstructEffect, Stat};
use skill::{Skill, Cast, Event, resolve}; use skill::{Skill, Cast, Event, EventVariant, resolve};
use player::{Player}; use player::{Player};
use instance::{TimeControl}; use instance::{TimeControl};
@ -40,6 +40,7 @@ pub enum Colour {
#[derive(Debug,Clone,PartialEq,Serialize,Deserialize)] #[derive(Debug,Clone,PartialEq,Serialize,Deserialize)]
pub enum Action { pub enum Action {
Hit { construct: Uuid, skill: Skill },
Damage { construct: Uuid, values: Vec<Value>, colour: Colour }, Damage { construct: Uuid, values: Vec<Value>, colour: Colour },
Effect { construct: Uuid, effect: ConstructEffect }, Effect { construct: Uuid, effect: ConstructEffect },
IncreaseCooldowns { construct: Uuid, turns: usize }, IncreaseCooldowns { construct: Uuid, turns: usize },
@ -491,7 +492,7 @@ impl Game {
self.skill_phase_start(r_animation_ms) self.skill_phase_start(r_animation_ms)
} }
fn resolution_add(&mut self, mut events: Vec<Event>) -> &mut Game { fn event_add(&mut self, mut events: Vec<Event>) -> &mut Game {
self.events.last_mut().unwrap().append(&mut events); self.events.last_mut().unwrap().append(&mut events);
self self
} }
@ -499,6 +500,7 @@ impl Game {
pub fn actions(&mut self, actions: Vec<Action>) -> &mut Game { pub fn actions(&mut self, actions: Vec<Action>) -> &mut Game {
for action in actions { for action in actions {
match action { match action {
Action::Hit { construct, skill } => self.hit(construct, skill),
Action::Damage { construct, values, colour } => self.damage(construct, values, colour), Action::Damage { construct, values, colour } => self.damage(construct, values, colour),
Action::Effect { construct, effect } => self.effect(construct, effect), Action::Effect { construct, effect } => self.effect(construct, effect),
Action::IncreaseCooldowns { construct, turns } => self.increase_cooldowns(construct, turns), Action::IncreaseCooldowns { construct, turns } => self.increase_cooldowns(construct, turns),
@ -508,24 +510,29 @@ impl Game {
self self
} }
fn hit(&mut self, construct: Uuid, skill: Skill) -> &mut Game {
self.event_add(vec![Event::new(EventVariant::Hit { skill: skill }, construct)]);
self
}
fn damage(&mut self, construct: Uuid, values: Vec<Value>, colour: Colour) -> &mut Game { fn damage(&mut self, construct: Uuid, values: Vec<Value>, colour: Colour) -> &mut Game {
let events = match colour { let events = match colour {
_ => self.construct_by_id(construct).unwrap().deal_red_damage(128) // fixme unwrap _ => self.construct_by_id(construct).unwrap().deal_red_damage(128) // fixme unwrap
}; };
self.resolution_add(events); self.event_add(events);
self self
} }
fn effect(&mut self, construct: Uuid, effect: ConstructEffect) -> &mut Game { fn effect(&mut self, construct: Uuid, effect: ConstructEffect) -> &mut Game {
let events = self.construct_by_id(construct).unwrap().add_effect(effect); let events = self.construct_by_id(construct).unwrap().add_effect(effect);
self.resolution_add(events); self.event_add(events);
self self
} }
fn increase_cooldowns(&mut self, construct: Uuid, turns: usize) -> &mut Game { fn increase_cooldowns(&mut self, construct: Uuid, turns: usize) -> &mut Game {
let events = self.construct_by_id(construct).unwrap().increase_cooldowns(turns); let events = self.construct_by_id(construct).unwrap().increase_cooldowns(turns);
self.resolution_add(events); self.event_add(events);
self self
} }

View File

@ -62,9 +62,7 @@ pub fn resolve(game: &mut Game, cast: Cast) {
// } // }
for cast in casts { for cast in casts {
let mut actions = vec![]; // fixme hit event game.actions(cast.actions());
actions.append(&mut cast.actions());
game.actions(actions);
} }
} }
@ -107,8 +105,9 @@ impl Cast {
} }
pub fn actions(&self) -> Vec<Action> { pub fn actions(&self) -> Vec<Action> {
match self.skill { let mut actions = vec![Action::Hit { construct: self.target, skill: self.skill }];
let mut rest = match self.skill {
Skill::Amplify => vec![ Skill::Amplify => vec![
Action::Effect { Action::Effect {
construct: self.target, construct: self.target,
@ -169,7 +168,7 @@ impl Cast {
}, },
Action::Effect { Action::Effect {
construct: self.target, construct: self.target,
effect: ConstructEffect {effect: Effect::Stun, duration: 2, meta: Some(EffectMeta::Skill(Skill::Bash)), tick: None } effect: ConstructEffect {effect: Effect::Stun, duration: 2, meta: None, tick: None }
}, },
Action::IncreaseCooldowns { Action::IncreaseCooldowns {
construct: self.target, construct: self.target,
@ -217,33 +216,29 @@ impl Cast {
], ],
_ => unimplemented!() _ => unimplemented!()
} };
}
actions.append(&mut rest);
return actions;
}
} }
pub type Disable = Vec<Effect>; pub type Disable = Vec<Effect>;
#[derive(Debug,Clone,PartialEq,Serialize,Deserialize)] #[derive(Debug,Clone,PartialEq,Serialize,Deserialize)]
pub struct Event { pub struct Event {
pub target: EventConstruct, pub target: Uuid,
pub variant: EventVariant, pub variant: EventVariant,
pub stages: EventStages, pub stages: EventStages,
pub delay: i64, pub delay: i64,
} }
impl Event { impl Event {
pub fn new(variant: EventVariant, target: &Construct) -> Event { pub fn new(variant: EventVariant, target: Uuid) -> Event {
let stages = variant.stages(); let stages = variant.stages();
Event { Event {
target: EventConstruct { target,
id: target.id,
red: target.red_life(),
green: target.green_life(),
blue: target.blue_life(),
},
variant, variant,
delay: stages.delay(), delay: stages.delay(),
stages: stages, stages: stages,
@ -254,15 +249,15 @@ impl Event {
#[derive(Debug,Clone,PartialEq,Serialize,Deserialize)] #[derive(Debug,Clone,PartialEq,Serialize,Deserialize)]
pub enum EventVariant { pub enum EventVariant {
Hit { skill: Skill }, Hit { skill: Skill },
Damage { amount: usize, mitigation: usize, colour: Colour, stats: EventConstruct },
Effect { effect: Effect, duration: u8, construct_effects: Vec<ConstructEffect> },
Disable { disable: Disable }, Disable { disable: Disable },
Damage { amount: usize, mitigation: usize, colour: Colour },
Healing { amount: usize, overhealing: usize }, Healing { amount: usize, overhealing: usize },
Recharge { red: usize, blue: usize }, Recharge { red: usize, blue: usize },
Inversion { skill: Skill }, Inversion { skill: Skill },
Reflection { skill: Skill }, Reflection { skill: Skill },
AoeSkill { skill: Skill }, AoeSkill { skill: Skill },
Skill { skill: Skill }, Skill { skill: Skill },
Effect { effect: Effect, duration: u8, construct_effects: Vec<ConstructEffect> },
Removal { effect: Option<Effect>, construct_effects: Vec<ConstructEffect> }, Removal { effect: Option<Effect>, construct_effects: Vec<ConstructEffect> },
TargetKo { skill: Skill }, TargetKo { skill: Skill },
// skill not necessary but makes it neater as all events are arrays in js // skill not necessary but makes it neater as all events are arrays in js
@ -281,7 +276,7 @@ impl EventVariant {
match self { match self {
EventVariant::Disable { disable: _} EventVariant::Disable { disable: _}
=> EventStages::PostOnly, => EventStages::PostOnly,
EventVariant::Damage { amount: _, mitigation: _, colour: _} EventVariant::Damage { amount: _, mitigation: _, colour: _, stats: _ }
=> EventStages::PostOnly, => EventStages::PostOnly,
EventVariant::Healing { amount: _, overhealing: _} EventVariant::Healing { amount: _, overhealing: _}
=> EventStages::PostOnly, => EventStages::PostOnly,
@ -331,6 +326,17 @@ pub struct EventConstruct {
pub blue: usize, pub blue: usize,
} }
impl EventConstruct {
pub fn new(construct: &Construct) -> EventConstruct {
EventConstruct {
id: construct.id,
red: construct.red_life(),
green: construct.green_life(),
blue: construct.blue_life(),
}
}
}
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)] #[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
pub enum EventStages { pub enum EventStages {
#[serde(rename = "START_SKILL END_SKILL POST_SKILL")] #[serde(rename = "START_SKILL END_SKILL POST_SKILL")]
@ -1637,8 +1643,16 @@ mod tests {
#[test] #[test]
fn attack_actions_test() { fn attack_actions_test() {
let cast = Cast::new(Uuid::new_v4(), Uuid::new_v4(), Uuid::new_v4(), Skill::Attack); let cast = Cast::new(Uuid::new_v4(), Uuid::new_v4(), Uuid::new_v4(), Skill::Attack);
let mut actions = cast.actions(); let actions = cast.actions();
match actions[0] { match actions[0] {
Action::Hit { construct: _, skill } => {
assert_eq!(skill, Skill::Attack);
},
_ => panic!("{:?}", actions),
};
match actions[1] {
Action::Damage { construct: _, values: _, colour } => { Action::Damage { construct: _, values: _, colour } => {
assert_eq!(colour, Colour::Red); assert_eq!(colour, Colour::Red);
}, },