2022-06-19 10:07:54 +00:00
|
|
|
use std::ops::Deref;
|
2022-06-14 17:11:24 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-06-19 19:34:08 +00:00
|
|
|
use num_traits::abs;
|
|
|
|
|
|
|
|
use crate::dynamic_data::Battle;
|
|
|
|
use crate::dynamic_data::Pokemon;
|
|
|
|
use crate::static_data::MoveTarget;
|
|
|
|
|
|
|
|
/// Helper type for the vector of targets we will return.
|
2022-06-18 13:52:39 +00:00
|
|
|
pub type TargetList<'own, 'library> = Vec<Option<Arc<Pokemon<'own, 'library>>>>;
|
2022-06-14 17:11:24 +00:00
|
|
|
|
2022-06-19 19:34:08 +00:00
|
|
|
/// This returns all Pokemon in the battle.
|
2022-06-16 15:59:33 +00:00
|
|
|
fn get_all_targets<'b, 'library>(battle: &Battle<'b, 'library>) -> TargetList<'b, 'library> {
|
|
|
|
let mut v = Vec::with_capacity(battle.pokemon_per_side() as usize * battle.number_of_sides() as usize);
|
2022-06-14 17:11:24 +00:00
|
|
|
for side in battle.sides() {
|
2022-06-19 10:07:54 +00:00
|
|
|
for pokemon in side.pokemon().deref() {
|
2022-06-14 17:11:24 +00:00
|
|
|
v.push(pokemon.as_ref().cloned());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
v
|
|
|
|
}
|
|
|
|
|
2022-06-19 19:34:08 +00:00
|
|
|
/// Little helper function to get the other side. 1 --> 0, 0 --> 1
|
2022-06-14 17:11:24 +00:00
|
|
|
fn get_opposite_side(side: u8) -> u8 {
|
|
|
|
if side == 0 {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
0
|
|
|
|
}
|
|
|
|
|
2022-06-19 19:34:08 +00:00
|
|
|
/// Gets all Pokemon that are adjacent to of directly opposite of a Pokemon. This means the target,
|
|
|
|
/// the Pokemon left of it, the Pokemon right of it, and the Pokemon opposite of it.
|
|
|
|
fn get_all_adjacent_opponent<'b, 'library>(
|
|
|
|
side: u8,
|
|
|
|
index: u8,
|
|
|
|
battle: &Battle<'b, 'library>,
|
|
|
|
) -> TargetList<'b, 'library> {
|
2022-06-14 17:11:24 +00:00
|
|
|
let left = index as i32 - 1;
|
|
|
|
let right = index + 1;
|
|
|
|
if left < 0 && right >= battle.pokemon_per_side() {
|
|
|
|
return vec![
|
|
|
|
battle.get_pokemon(side, index).as_ref().cloned(),
|
2022-06-16 15:59:33 +00:00
|
|
|
battle.get_pokemon(get_opposite_side(side), index).as_ref().cloned(),
|
2022-06-14 17:11:24 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
if left >= 0 {
|
|
|
|
return if right < battle.pokemon_per_side() {
|
|
|
|
vec![
|
|
|
|
battle.get_pokemon(side, index).as_ref().cloned(),
|
|
|
|
battle.get_pokemon(side, left as u8).as_ref().cloned(),
|
|
|
|
battle.get_pokemon(side, right).as_ref().cloned(),
|
2022-07-01 15:07:22 +00:00
|
|
|
battle
|
|
|
|
.get_pokemon(get_opposite_side(side), left as u8)
|
|
|
|
.as_ref()
|
|
|
|
.cloned(),
|
2022-06-16 15:59:33 +00:00
|
|
|
battle.get_pokemon(get_opposite_side(side), index).as_ref().cloned(),
|
2022-07-01 15:07:22 +00:00
|
|
|
battle.get_pokemon(get_opposite_side(side), right).as_ref().cloned(),
|
2022-06-14 17:11:24 +00:00
|
|
|
]
|
|
|
|
} else {
|
|
|
|
vec![
|
|
|
|
battle.get_pokemon(side, index).as_ref().cloned(),
|
|
|
|
battle.get_pokemon(side, left as u8).as_ref().cloned(),
|
2022-06-16 15:59:33 +00:00
|
|
|
battle.get_pokemon(get_opposite_side(side), index).as_ref().cloned(),
|
2022-07-01 15:07:22 +00:00
|
|
|
battle
|
|
|
|
.get_pokemon(get_opposite_side(side), left as u8)
|
|
|
|
.as_ref()
|
|
|
|
.cloned(),
|
2022-06-14 17:11:24 +00:00
|
|
|
]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
vec![
|
|
|
|
battle.get_pokemon(side, index).as_ref().cloned(),
|
|
|
|
battle.get_pokemon(side, right).as_ref().cloned(),
|
2022-06-16 15:59:33 +00:00
|
|
|
battle.get_pokemon(get_opposite_side(side), index).as_ref().cloned(),
|
2022-07-01 15:07:22 +00:00
|
|
|
battle.get_pokemon(get_opposite_side(side), right).as_ref().cloned(),
|
2022-06-14 17:11:24 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2022-06-27 17:21:50 +00:00
|
|
|
/// Gets all Pokemon that are adjacent to a Pokemon. This includes the target, the Pokemon to the
|
2022-06-19 19:34:08 +00:00
|
|
|
/// left of it, and the Pokemon to the right of it.
|
|
|
|
fn get_all_adjacent<'b, 'library>(side: u8, index: u8, battle: &Battle<'b, 'library>) -> TargetList<'b, 'library> {
|
2022-06-14 17:11:24 +00:00
|
|
|
let left = index as i32 - 1;
|
|
|
|
let right = index + 1;
|
|
|
|
if left < 0 && right >= battle.pokemon_per_side() {
|
|
|
|
return vec![battle.get_pokemon(side, index).as_ref().cloned()];
|
|
|
|
}
|
|
|
|
if left >= 0 {
|
|
|
|
if right < battle.pokemon_per_side() {
|
|
|
|
return vec![
|
|
|
|
battle.get_pokemon(side, index).as_ref().cloned(),
|
|
|
|
battle.get_pokemon(side, left as u8).as_ref().cloned(),
|
|
|
|
battle.get_pokemon(side, right).as_ref().cloned(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
return vec![
|
|
|
|
battle.get_pokemon(side, index).as_ref().cloned(),
|
|
|
|
battle.get_pokemon(side, left as u8).as_ref().cloned(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
vec![
|
|
|
|
battle.get_pokemon(side, index).as_ref().cloned(),
|
|
|
|
battle.get_pokemon(side, right).as_ref().cloned(),
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2022-06-19 19:34:08 +00:00
|
|
|
/// Gets the target for a specific move target type, given the targeted position.
|
2022-06-16 15:59:33 +00:00
|
|
|
pub fn resolve_targets<'b, 'library>(
|
2022-06-14 17:11:24 +00:00
|
|
|
side: u8,
|
|
|
|
index: u8,
|
|
|
|
target: MoveTarget,
|
2022-06-16 15:59:33 +00:00
|
|
|
battle: &Battle<'b, 'library>,
|
|
|
|
) -> TargetList<'b, 'library> {
|
2022-06-14 17:11:24 +00:00
|
|
|
match target {
|
2022-06-19 19:34:08 +00:00
|
|
|
// These all resolve to a single position. We let the client deal with where the target is,
|
|
|
|
// and just return the Pokemon at that given target here.
|
2022-06-14 17:11:24 +00:00
|
|
|
MoveTarget::Adjacent
|
|
|
|
| MoveTarget::AdjacentAlly
|
|
|
|
| MoveTarget::AdjacentAllySelf
|
|
|
|
| MoveTarget::AdjacentOpponent
|
|
|
|
| MoveTarget::Any
|
|
|
|
| MoveTarget::RandomOpponent
|
|
|
|
| MoveTarget::SelfUse => {
|
|
|
|
vec![battle.get_pokemon(side, index).as_ref().cloned()]
|
|
|
|
}
|
2022-06-19 19:34:08 +00:00
|
|
|
// If all pokemon are requested, give all Pokemon on the battlefield
|
2022-06-14 17:11:24 +00:00
|
|
|
MoveTarget::All => get_all_targets(battle),
|
2022-06-19 19:34:08 +00:00
|
|
|
// If the adjacent Pokemon are requested, pass those.
|
2022-06-14 17:11:24 +00:00
|
|
|
MoveTarget::AllAdjacent => get_all_adjacent(side, index, battle),
|
2022-06-19 19:34:08 +00:00
|
|
|
// If the adjacent and opponent Pokemon are requested, give those.
|
2022-06-14 17:11:24 +00:00
|
|
|
MoveTarget::AllAdjacentOpponent => get_all_adjacent_opponent(side, index, battle),
|
2022-06-19 19:34:08 +00:00
|
|
|
// If all Pokemon on a side are requested, simply give all Pokemon on the side back, and let
|
|
|
|
// the client deal with what side is passed.
|
2022-06-14 17:11:24 +00:00
|
|
|
MoveTarget::AllAlly | MoveTarget::AllOpponent => {
|
|
|
|
let mut v = Vec::new();
|
2022-06-19 10:07:54 +00:00
|
|
|
for pokemon in battle.sides()[side as usize].pokemon().deref() {
|
2022-06-14 17:11:24 +00:00
|
|
|
v.push(pokemon.as_ref().cloned());
|
|
|
|
}
|
|
|
|
v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-27 17:21:50 +00:00
|
|
|
/// Checks whether a given side and index are valid for a given move target and user.
|
2022-06-14 17:11:24 +00:00
|
|
|
pub fn is_valid_target(side: u8, index: u8, target: MoveTarget, user: &Pokemon) -> bool {
|
|
|
|
let user_side = user.get_battle_side_index();
|
|
|
|
let user_index = user.get_battle_index();
|
|
|
|
// If the user is not on the field, nothing is a valid target
|
|
|
|
if user_side.is_none() || user_index.is_none() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
match target {
|
|
|
|
MoveTarget::Adjacent | MoveTarget::AllAdjacent => {
|
|
|
|
let diff = abs(index as i32 - user_index.unwrap() as i32);
|
|
|
|
if diff == 0 {
|
|
|
|
return user_side.unwrap() != side;
|
|
|
|
}
|
|
|
|
diff <= 1
|
|
|
|
}
|
|
|
|
MoveTarget::AdjacentAlly => {
|
|
|
|
if user_side.unwrap() != side {
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
abs(index as i32 - user_index.unwrap() as i32) == 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MoveTarget::AdjacentAllySelf => {
|
|
|
|
if user_side.unwrap() != side {
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
abs(index as i32 - user_index.unwrap() as i32) <= 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MoveTarget::AdjacentOpponent | MoveTarget::AllAdjacentOpponent => {
|
|
|
|
if user_side.unwrap() == side {
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
abs(index as i32 - user_index.unwrap() as i32) <= 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MoveTarget::All | MoveTarget::Any | MoveTarget::RandomOpponent => true,
|
|
|
|
MoveTarget::AllAlly => user_side.unwrap() == side,
|
|
|
|
MoveTarget::AllOpponent => user_side.unwrap() != side,
|
|
|
|
MoveTarget::SelfUse => user_side.unwrap() == side && user_index.unwrap() == index,
|
|
|
|
}
|
|
|
|
}
|