Skip to content

Commit

Permalink
Merge pull request ozkriff#656 from ozkriff/knockback_check
Browse files Browse the repository at this point in the history
check_ability_knockback: Add strengh check
  • Loading branch information
ozkriff authored Jun 7, 2021
2 parents 97d6e9d + 54f7603 commit aded120
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 25 deletions.
15 changes: 11 additions & 4 deletions src/core/battle/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::core::{
self,
ability::{self, Ability},
command::{self, Command},
state, Attacks, Id, Jokers, Moves, State,
state, Attacks, Id, Jokers, Moves, PushStrength, State, Weight,
},
map::{self, Distance, PosHex},
};
Expand All @@ -24,9 +24,10 @@ pub fn check(state: &State, command: &Command) -> Result<(), Error> {
}
}

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Error {
NotEnoughMovePoints,
NotEnoughStrength,
BadActorId,
BadTargetId,
BadTargetType,
Expand Down Expand Up @@ -125,11 +126,17 @@ fn check_command_use_ability(state: &State, command: &command::UseAbility) -> Re
}

fn check_ability_knockback(state: &State, id: Id, pos: PosHex) -> Result<(), Error> {
let strength = PushStrength(Weight::Normal);
let selected_pos = state.parts().pos.get(id).0;
check_min_distance(selected_pos, pos, Distance(1))?;
check_max_distance(selected_pos, pos, Distance(1))?;
if state::blocker_id_at_opt(state, pos).is_none() {
return Err(Error::NoTarget);
let target_id = match state::agent_id_at_opt(state, pos) {
Some(id) => id,
None => return Err(Error::NoTarget),
};
let target_weight = state.parts().blocker.get(target_id).weight;
if !strength.can_push(target_weight) {
return Err(Error::NotEnoughStrength);
}
Ok(())
}
Expand Down
23 changes: 2 additions & 21 deletions src/core/battle/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1509,34 +1509,15 @@ fn knockback_normal_vs_heavy() {
.object(P0, "knockbacker", PosHex { q: 0, r: 0 })
.object(P1, "heavy_target", initial_heavy_position);
let mut state = debug_state(prototypes, scenario);
exec_and_check(
let knockback_result = try_exec(
&mut state,
command::UseAbility {
id: Id(0),
pos: initial_heavy_position,
ability: Ability::Knockback,
},
&[Event {
active_event: event::UseAbility {
id: Id(0),
pos: initial_heavy_position,
ability: Ability::Knockback,
}
.into(),
actor_ids: vec![Id(1), Id(0)],
instant_effects: vec![(
Id(1),
vec![effect::Knockback {
from: initial_heavy_position,
to: initial_heavy_position,
strength: PushStrength(Weight::Normal),
}
.into()],
)],
timed_effects: Vec::new(),
scheduled_abilities: Vec::new(),
}],
);
assert_eq!(knockback_result, Err(check::Error::NotEnoughStrength));
assert_eq!(state.parts().pos.get(Id(1)).0, initial_heavy_position);
}

Expand Down

0 comments on commit aded120

Please sign in to comment.