More work on switching battle data to interior mutability, instead of exterior mutability.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-06-19 12:07:54 +02:00
parent 2e19005a30
commit 715f16e2b8
14 changed files with 256 additions and 280 deletions

View File

@@ -185,113 +185,101 @@ impl<'own, 'library> Battle<'own, 'library> {
if target.is_fainted() {
break;
}
{
let mut hit_type = executing_move.use_move().move_type();
script_hook!(
change_move_type,
executing_move,
executing_move,
target,
hit_index,
&mut hit_type
);
executing_move
.get_hit_from_raw_index_mut(target_hit_stat + hit_index as usize)
.set_move_type(hit_type);
let mut effectiveness = self
let used_move = executing_move.use_move();
let mut hit_type = used_move.move_type();
script_hook!(
change_move_type,
executing_move,
executing_move,
target,
hit_index,
&mut hit_type
);
let hit_data = executing_move.get_hit_from_raw_index(target_hit_stat + hit_index as usize);
hit_data.set_move_type(hit_type);
let mut effectiveness = self
.library()
.static_data()
.types()
.get_effectiveness(hit_type, target.types());
script_hook!(
change_effectiveness,
executing_move,
executing_move,
target,
hit_index,
&mut effectiveness
);
hit_data.set_effectiveness(effectiveness);
let mut block_critical = false;
script_hook!(
block_critical,
executing_move,
executing_move,
target,
hit_index,
&mut block_critical
);
script_hook!(
block_incoming_critical,
target,
executing_move,
target,
hit_index,
&mut block_critical
);
if !block_critical {
let is_critical = self
.library()
.static_data()
.types()
.get_effectiveness(hit_type, target.types());
script_hook!(
change_effectiveness,
executing_move,
executing_move,
target,
hit_index,
&mut effectiveness
);
executing_move
.get_hit_from_raw_index_mut(target_hit_stat + hit_index as usize)
.set_effectiveness(effectiveness);
let mut block_critical = false;
script_hook!(
block_critical,
executing_move,
executing_move,
target,
hit_index,
&mut block_critical
);
script_hook!(
block_incoming_critical,
target,
executing_move,
target,
hit_index,
&mut block_critical
);
.misc_library()
.is_critical(self, executing_move, target, hit_index);
hit_data.set_critical(is_critical);
}
let base_power = self.library().damage_calculator().get_base_power(
executing_move,
target,
hit_index,
executing_move.get_hit_data(target, hit_index)?,
);
hit_data.set_base_power(base_power);
let damage = self.library().damage_calculator().get_damage(
executing_move,
target,
hit_index,
executing_move.get_hit_data(target, hit_index)?,
);
hit_data.set_damage(damage);
if !block_critical {
let is_critical =
self.library()
.misc_library()
.is_critical(self, executing_move, target, hit_index);
executing_move
.get_hit_from_raw_index_mut(target_hit_stat + hit_index as usize)
.set_critical(is_critical);
if used_move.category() == MoveCategory::Status {
if let Some(secondary_effect) = used_move.secondary_effect() {
let secondary_effect_chance = secondary_effect.chance();
if secondary_effect_chance == -1.0
|| self
.random()
.effect_chance(secondary_effect_chance, executing_move, target, hit_index)
{
script_hook!(on_secondary_effect, executing_move, executing_move, target, hit_index);
// TODO: on fail
}
}
let base_power = self.library().damage_calculator().get_base_power(
executing_move,
target,
hit_index,
executing_move.get_hit_data(target, hit_index)?,
);
executing_move
.get_hit_from_raw_index_mut(target_hit_stat + hit_index as usize)
.set_base_power(base_power);
let damage = self.library().damage_calculator().get_damage(
executing_move,
target,
hit_index,
executing_move.get_hit_data(target, hit_index)?,
);
executing_move
.get_hit_from_raw_index_mut(target_hit_stat + hit_index as usize)
.set_damage(damage);
if executing_move.use_move().category() == MoveCategory::Status {
if executing_move.use_move().has_secondary_effect() {
let secondary_effect_chance = executing_move.use_move().secondary_effect().chance();
if secondary_effect_chance == -1.0
|| self
.random()
.effect_chance(secondary_effect_chance, executing_move, target, hit_index)
{
script_hook!(on_secondary_effect, executing_move, executing_move, target, hit_index);
// TODO: on fail
}
} else {
let mut damage = hit_data.damage();
let current_health = target.current_health();
if damage > current_health {
damage = current_health;
hit_data.set_damage(damage);
}
if damage > 0 {
target.damage(damage, DamageSource::AttackDamage);
if !target.is_fainted() {
script_hook!(on_incoming_hit, target, executing_move, target, hit_index);
} else {
script_hook!(on_opponent_faints, executing_move, executing_move, target, hit_index);
}
} else {
let mut damage = executing_move
.get_hit_from_raw_index(target_hit_stat + hit_index as usize)
.damage();
let current_health = target.current_health();
if damage > current_health {
damage = current_health;
executing_move
.get_hit_from_raw_index_mut(target_hit_stat + hit_index as usize)
.set_damage(damage);
}
if damage > 0 {
target.damage(damage, DamageSource::AttackDamage);
if !target.is_fainted() {
script_hook!(on_incoming_hit, target, executing_move, target, hit_index);
} else {
script_hook!(on_opponent_faints, executing_move, executing_move, target, hit_index);
}
if executing_move.use_move().has_secondary_effect() && !target.is_fainted() {
if !target.is_fainted() {
if let Some(secondary_effect) = used_move.secondary_effect() {
let mut prevent_secondary = false;
script_hook!(
prevent_secondary_effect,
@@ -302,7 +290,7 @@ impl<'own, 'library> Battle<'own, 'library> {
&mut prevent_secondary
);
if !prevent_secondary {
let secondary_effect_chance = executing_move.use_move().secondary_effect().chance();
let secondary_effect_chance = secondary_effect.chance();
if secondary_effect_chance == -1.0
|| self.random().effect_chance(
secondary_effect_chance,