From 9b5adaad9c162c389df277593d48bd0c11b7f92e Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 4 Feb 2022 09:53:30 -0500 Subject: [PATCH] Some code clarity tweaks --- src/raws/rawmaster.rs | 29 +++++++++++++++-------------- src/systems/ai/visible_ai_system.rs | 26 ++++++++++++++------------ 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/src/raws/rawmaster.rs b/src/raws/rawmaster.rs index 45abd13..d363286 100644 --- a/src/raws/rawmaster.rs +++ b/src/raws/rawmaster.rs @@ -424,65 +424,66 @@ fn parse_particle(n: &str) -> SpawnParticleBurst { macro_rules! apply_effects { ($effects:expr, $eb:expr) => { - for effect in $effects.iter() { - let effect_name = effect.0.as_str(); - match effect_name { + // Effects are defined in the raws file as name => value pairs + for (effect_name, value) in $effects.iter() { + match effect_name.as_str() { "provides_healing" => { $eb = $eb.with(ProvidesHealing { - heal_amount: effect.1.parse::().unwrap(), + heal_amount: value.parse::().unwrap(), }); } "provides_mana" => { $eb = $eb.with(ProvidesMana { - mana_amount: effect.1.parse::().unwrap(), + mana_amount: value.parse::().unwrap(), }) } "teach_spell" => { $eb = $eb.with(TeachesSpell { - spell: effect.1.to_string(), + spell: value.to_string(), }) } "ranged" => { $eb = $eb.with(Ranged { - range: effect.1.parse::().unwrap(), + range: value.parse::().unwrap(), }); } "damage" => { $eb = $eb.with(InflictsDamage { - damage: effect.1.parse::().unwrap(), + damage: value.parse::().unwrap(), }); } "area_of_effect" => { $eb = $eb.with(AreaOfEffect { - radius: effect.1.parse::().unwrap(), + radius: value.parse::().unwrap(), }); } "confusion" => { $eb = $eb.with(Confusion {}); $eb = $eb.with(Duration { - turns: effect.1.parse::().unwrap(), + turns: value.parse::().unwrap(), }); } "magic_mapping" => $eb = $eb.with(MagicMapper {}), "town_portal" => $eb = $eb.with(TownPortal {}), "food" => $eb = $eb.with(ProvidesFood {}), "single_activation" => $eb = $eb.with(SingleActivation {}), - "particle_line" => $eb = $eb.with(parse_particle_line(&effect.1)), - "particle" => $eb = $eb.with(parse_particle(&effect.1)), + "particle_line" => $eb = $eb.with(parse_particle_line(&value)), + "particle" => $eb = $eb.with(parse_particle(&value)), "remove_curse" => $eb = $eb.with(ProvidesRemoveCurse {}), "identify" => $eb = $eb.with(ProvidesIdentification {}), "slow" => { $eb = $eb.with(Slow { - initiative_penalty: effect.1.parse::().unwrap(), + initiative_penalty: value.parse::().unwrap(), }) } "damage_over_time" => { $eb = $eb.with(DamageOverTime { - damage: effect.1.parse::().unwrap(), + damage: value.parse::().unwrap(), }) } "target_self" => $eb = $eb.with(AlwaysTargetsSelf {}), _ => { + #[cfg(feature = "debug")] console::log(format!( "WARNING: consumable effect '{}' not implemented.", effect_name diff --git a/src/systems/ai/visible_ai_system.rs b/src/systems/ai/visible_ai_system.rs index dcb9bfd..a45a9c6 100644 --- a/src/systems/ai/visible_ai_system.rs +++ b/src/systems/ai/visible_ai_system.rs @@ -79,6 +79,8 @@ impl<'a> System<'a> for VisibleAI { reaction.0 as i32 / map.width, ), ); + + // First, see if we are attempting to cast a spell if let Some(abilities) = abilities.get(entity) { for ability in abilities.abilities.iter() { if range >= ability.min_range @@ -103,34 +105,33 @@ impl<'a> System<'a> for VisibleAI { }, ) .expect("Unable to insert intent to cast spell"); + done = true; } } } + // Check if there are targets if a ranged weapon is wielded if !done { for (weapon, equip) in (&weapons, &equipped).join() { if let Some(wrange) = weapon.range { - if equip.owner == entity { + if equip.owner == entity && wrange >= range as i32 { + #[cfg(feature = "debug")] console::log(format!( - "Owner found. Ranges: {}/{}", + "Owner found. Ranges: {}/{}. Inserting shoot", wrange, range )); - if wrange >= range as i32 { - console::log("Inserting shoot"); - wants_shoot - .insert( - entity, - WantsToShoot { target: reaction.2 }, - ) - .expect("Unable to insert intent to shoot"); - done = true; - } + wants_shoot + .insert(entity, WantsToShoot { target: reaction.2 }) + .expect("Unable to insert intent to shoot"); + + done = true; } } } } + // The target is not in range (yet), so approach/chase the target if !done { want_approach .insert( @@ -143,6 +144,7 @@ impl<'a> System<'a> for VisibleAI { chasing .insert(entity, Chasing { target: reaction.2 }) .expect("Unable to insert intent to chase"); + done = true; } }