Skip to content

Commit

Permalink
fix some problems with game effects
Browse files Browse the repository at this point in the history
  • Loading branch information
bgkillas committed Jan 16, 2025
1 parent ce9201f commit ca21e04
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 35 deletions.
75 changes: 42 additions & 33 deletions ewext/noita_api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::lua::LuaPutValue;
use crate::serialize::deserialize_entity;
use eyre::{eyre, Context, OptionExt};
use shared::{GameEffectData, GameEffectEnum};
use std::collections::HashMap;
use std::{
borrow::Cow,
num::{NonZero, TryFromIntError},
ops::Deref,
};
use crate::lua::LuaPutValue;
pub mod lua;
pub mod serialize;

Expand Down Expand Up @@ -169,10 +170,17 @@ impl EntityID {

pub fn get_game_effects(self) -> Option<Vec<(GameEffectData, EntityID)>> {
let mut effects = Vec::new();
let mut name_to_n: HashMap<String, i32> = HashMap::default();
for ent in self.children(None) {
if ent.has_tag("projectile") {
if let Ok(data) = serialize::serialize_entity(ent) {
effects.push((GameEffectData::Projectile((ent.0, data)), ent))
let n = ent.filename().unwrap_or(String::new());
let num = name_to_n.entry(n.clone()).or_insert(0);
*num += 1;
effects.push((
GameEffectData::Projectile((format!("{}{}", n, num), data)),
ent,
));
}
} else if let Some(effect) = ent
.try_get_first_component_including_disabled::<GameEffectComponent>(None)
Expand All @@ -185,10 +193,20 @@ impl EntityID {
if !file.is_empty() {
effects.push((GameEffectData::Custom(file), ent))
} else if let Ok(data) = serialize::serialize_entity(ent) {
effects.push((GameEffectData::Projectile((ent.0, data)), ent))
let n = ent.filename().unwrap_or(String::new());
effects.push((
GameEffectData::Projectile((n, data)),
ent,
))
}
} else if let Ok(data) = serialize::serialize_entity(ent) {
effects.push((GameEffectData::Projectile((ent.0, data)), ent))
let n = ent.filename().unwrap_or(String::new());
let num = name_to_n.entry(n.clone()).or_insert(0);
*num += 1;
effects.push((
GameEffectData::Projectile((format!("{}{}", n, num), data)),
ent,
))
}
} else {
effects.push((GameEffectData::Normal(name), ent))
Expand Down Expand Up @@ -231,21 +249,17 @@ impl EntityID {
}
let local_effects = self.get_game_effects().unwrap_or_default();
for effect in game_effect {
if let Some(ent) = local_effects.iter().find_map(|(e, ent)| {
if match (e, effect) {
(GameEffectData::Normal(e1), GameEffectData::Normal(e2)) => e1 == e2,
(GameEffectData::Custom(e1), GameEffectData::Custom(e2)) => e1 == e2,
(
GameEffectData::Projectile((e1, _)),
GameEffectData::Projectile((e2, _)),
) => e1 == e2,
_ => false,
} {
Some(ent)
} else {
None
}
}) {
if let Some(ent) =
local_effects.iter().find_map(
|(e, ent)| {
if e == effect {
Some(ent)
} else {
None
}
},
)
{
let _ = set_frames(*ent);
} else {
let ent = match effect {
Expand Down Expand Up @@ -286,14 +300,7 @@ impl EntityID {
}
let local_effects = self.get_game_effects().unwrap_or_default();
for (effect, ent) in local_effects {
if game_effect.iter().all(|e| match (e, effect.clone()) {
(GameEffectData::Normal(e1), GameEffectData::Normal(e2)) => *e1 != e2,
(GameEffectData::Custom(e1), GameEffectData::Custom(e2)) => *e1 != e2,
(GameEffectData::Projectile((e1, _)), GameEffectData::Projectile((e2, _))) => {
*e1 != e2
}
_ => true,
}) {
if game_effect.iter().all(|e| *e != effect) {
ent.kill()
}
}
Expand Down Expand Up @@ -333,7 +340,11 @@ impl EntityID {
if let Ok(Some(status)) = self.try_get_first_component::<StatusEffectDataComponent>(None) {
for (i, v) in status.0.stain_effects()?.iter().enumerate() {
if *v >= 0.15 && current_stains & (1 << i) == 0 {
raw::entity_remove_stain_status_effect(self.0.get() as i32, i.to_string().into(), None)?
raw::entity_remove_stain_status_effect(
self.0.get() as i32,
i.to_string().into(),
None,
)?
}
}
}
Expand Down Expand Up @@ -376,10 +387,9 @@ impl ComponentID {

pub fn object_set_value<T>(self, object: &str, key: &str, value: T) -> eyre::Result<()>
where
T: LuaPutValue
T: LuaPutValue,
{
raw::component_object_set_value::<T>(
self, object, key, value)?;
raw::component_object_set_value::<T>(self, object, key, value)?;
Ok(())
}

Expand Down Expand Up @@ -460,7 +470,6 @@ pub mod raw {
Ok(())
}


pub fn physics_body_id_get_transform(body: PhysicsBodyID) -> eyre::Result<Option<PhysData>> {
let lua = LuaState::current()?;
lua.get_global(c"PhysicsBodyIDGetTransform");
Expand Down Expand Up @@ -510,4 +519,4 @@ pub struct PhysData {
pub vx: f32,
pub vy: f32,
pub av: f32,
}
}
14 changes: 12 additions & 2 deletions shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,19 @@ pub enum GameEffectEnum {
_Last,
}

#[derive(Encode, Decode, Clone, PartialEq)]
#[derive(Encode, Decode, Clone)]
pub enum GameEffectData {
Normal(GameEffectEnum),
Custom(String),
Projectile((NonZero<isize>, Vec<u8>)),
Projectile((String, Vec<u8>)),
}
impl PartialEq for GameEffectData {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(GameEffectData::Normal(e1), GameEffectData::Normal(e2)) => e1 == e2,
(GameEffectData::Custom(e1), GameEffectData::Custom(e2)) => e1 == e2,
(GameEffectData::Projectile((e1, _)), GameEffectData::Projectile((e2, _))) => e1 == e2,
_ => false,
}
}
}

0 comments on commit ca21e04

Please sign in to comment.