Skip to content

Commit

Permalink
dont allow duping items, fix poly enemies
Browse files Browse the repository at this point in the history
  • Loading branch information
bgkillas committed Jan 21, 2025
1 parent 8a3fea6 commit b4a4b7a
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 14 deletions.
7 changes: 7 additions & 0 deletions ewext/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ewext/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ bimap = "0.6.3"
#enables cross-compilation on older systems (for example, when compiling on ubuntu 20.04)
#due to unresolved bug in rust toolchain
#https://github.com/rust-lang/rust/issues/79609
pre2204 = []
pre2204 = []
7 changes: 7 additions & 0 deletions ewext/noita_api/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion ewext/noita_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ eyre = "0.6.12"
libloading = "0.8.5"
noita_api_macro = {path = "../noita_api_macro"}
shared = {path = "../../shared"}
strum = "0.26.3"
strum = "0.26.3"
base64 = "0.22.1"
51 changes: 50 additions & 1 deletion ewext/noita_api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::lua::{LuaGetValue, LuaPutValue};
use crate::serialize::deserialize_entity;
use base64::Engine;
use eyre::{eyre, Context, OptionExt};
use shared::des::Gid;
use shared::{GameEffectData, GameEffectEnum};
use std::collections::HashMap;
use std::{
Expand All @@ -13,7 +15,6 @@ pub mod serialize;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct EntityID(pub NonZero<isize>);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ComponentID(pub NonZero<isize>);

Expand Down Expand Up @@ -42,6 +43,54 @@ impl EntityID {
raw::entity_get_name(self).map(|s| s.to_string())
}

pub fn handle_poly(&self) -> Option<Gid> {
for ent in self.children(None) {
if let Ok(Some(effect)) =
ent.try_get_first_component_including_disabled::<GameEffectComponent>(None)
{
let name = effect.effect().unwrap();
match name {
GameEffectEnum::Polymorph
| GameEffectEnum::PolymorphRandom
| GameEffectEnum::PolymorphUnstable
| GameEffectEnum::PolymorphCessation => {
if let Ok(data) =
raw::component_get_value::<Cow<str>>(effect.0, "mSerializedData")
{
if data.is_empty() {
return None;
}
if let Ok(data) =
base64::engine::general_purpose::STANDARD.decode(data.to_string())
{
let data = String::from_utf8_lossy(&data)
.chars()
.filter(|c| c.is_ascii_alphanumeric())
.collect::<String>();
let mut data = data.split("VariableStorageComponentewgidlid");
let _ = data.next();
if let Some(data) = data.next() {
let mut gid = String::new();
for c in data.chars() {
if c.is_numeric() {
gid.push(c)
} else {
break;
}
}
return Some(Gid(gid.parse::<u64>().ok()?));
}
}
}
return None;
}
_ => {}
}
}
}
None
}

pub fn add_tag(self, tag: impl AsRef<str>) -> eyre::Result<()> {
raw::entity_add_tag(self, tag.as_ref().into())
}
Expand Down
29 changes: 27 additions & 2 deletions ewext/src/modules/entity_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use diff_model::{entity_is_item, LocalDiffModel, RemoteDiffModel, DES_TAG};
use eyre::{Context, OptionExt};
use interest::InterestTracker;
use noita_api::serialize::serialize_entity;
use noita_api::{EntityID, ProjectileComponent};
use noita_api::{game_print, EntityID, ProjectileComponent, VariableStorageComponent};
use rustc_hash::{FxHashMap, FxHashSet};
use shared::{
des::{
Expand Down Expand Up @@ -47,6 +47,7 @@ pub(crate) struct EntitySync {

pending_fired_projectiles: Arc<Vec<ProjectileFired>>,
dont_kill: FxHashSet<EntityID>,
dont_kill_by_gid: FxHashSet<Gid>,
dont_track: FxHashSet<EntityID>,
}
impl EntitySync {
Expand Down Expand Up @@ -78,6 +79,7 @@ impl Default for EntitySync {

pending_fired_projectiles: Vec::new().into(),
dont_kill: Default::default(),
dont_kill_by_gid: Default::default(),
dont_track: Default::default(),
}
}
Expand Down Expand Up @@ -145,7 +147,27 @@ impl EntitySync {
if !entity.is_alive() || self.dont_track.remove(&entity) {
continue;
}
if entity.has_tag(DES_TAG) && !self.dont_kill.remove(&entity) {
if let Some(gid) = entity.handle_poly() {
game_print(gid.0.to_string());
game_print(self.local_diff_model.find_by_gid(gid).is_some().to_string());
self.dont_kill_by_gid.insert(gid);
}
if entity.has_tag(DES_TAG)
&& !self.dont_kill.remove(&entity)
&& entity
.iter_all_components_of_type_including_disabled::<VariableStorageComponent>(
None,
)?
.find(|var| var.name().unwrap_or("".into()) == "ew_gid_lid")
.map(|var| {
if let Ok(n) = var.value_string().unwrap().parse::<u64>() {
!self.dont_kill_by_gid.remove(&Gid(n))
} else {
true
}
})
.unwrap_or(true)
{
entity.kill();
continue;
}
Expand All @@ -170,6 +192,9 @@ impl EntitySync {
remote.remove_entities()
}
}
shared::des::ProxyToDes::DeleteEntity(entity) => {
EntityID(entity).kill();
}
}
}

Expand Down
13 changes: 9 additions & 4 deletions ewext/src/modules/entity_sync/diff_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl LocalDiffModelTracker {
let entity = self.entity_by_lid(lid)?;

if !entity.is_alive() {
self.untrack_entity(ctx, gid, lid)?;
self.untrack_entity(ctx, gid, lid, None)?;
return Ok(());
}
let item_and_was_picked = info.kind == EntityKind::Item && item_in_inventory(entity)?;
Expand Down Expand Up @@ -362,10 +362,11 @@ impl LocalDiffModelTracker {
ctx: &mut ModuleCtx<'_>,
gid: Gid,
lid: Lid,
ent: Option<NonZero<isize>>,
) -> Result<(), eyre::Error> {
self.pending_removal.push(lid);
ctx.net.send(&NoitaOutbound::DesToProxy(
shared::des::DesToProxy::DeleteEntity(gid),
shared::des::DesToProxy::DeleteEntity(gid, ent),
))?;

Ok(())
Expand All @@ -378,7 +379,7 @@ impl LocalDiffModelTracker {
lid: Lid,
entity: EntityID,
) -> Result<(), eyre::Error> {
self.untrack_entity(ctx, gid, lid)?;
self.untrack_entity(ctx, gid, lid, Some(entity.0))?;
entity.remove_tag(DES_TAG)?;
with_entity_scripts(entity, |luac| {
luac.set_script_throw_item(
Expand Down Expand Up @@ -596,7 +597,7 @@ impl LocalDiffModel {
.wrap_err("Failed to update local entity")
{
print_error(error)?;
self.tracker.untrack_entity(ctx, *gid, lid)?;
self.tracker.untrack_entity(ctx, *gid, lid, None)?;
}
}
Ok(())
Expand Down Expand Up @@ -1601,6 +1602,10 @@ fn _safe_wandkill(entity: EntityID) -> eyre::Result<()> {
lc.set_script_source_file(
"mods/quant.ew/files/system/entity_sync_helper/scripts/killself.lua".into(),
)?;
entity.set_component_enabled(*lc, true)?;
lc.add_tag("enabled_in_inventory")?;
lc.add_tag("enabled_in_world")?;
lc.add_tag("enabled_in_hand")?;
lc.set_execute_on_added(false)?;
lc.set_m_next_execution_time(noita_api::raw::game_get_frame_num()? + 1)?;
Ok(())
Expand Down
11 changes: 8 additions & 3 deletions noita-proxy/src/net/des.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,14 @@ impl DesManager {
.entities
.insert(full_entity_data.gid, full_entity_data);
}
DesToProxy::DeleteEntity(gid) => {
self.authority.remove(&gid);
self.entity_storage.entities.remove(&gid);
DesToProxy::DeleteEntity(gid, ent) => {
if self.entity_storage.entities.contains_key(&gid) {
self.authority.remove(&gid);
self.entity_storage.entities.remove(&gid);
} else if let Some(ent) = ent {
self.pending_messages
.push((source, ProxyToDes::DeleteEntity(ent)));
}
}
DesToProxy::ReleaseAuthority(gid) => {
self.authority.remove(&gid);
Expand Down
5 changes: 3 additions & 2 deletions shared/src/des.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::num::NonZero;
use std::sync::Arc;

use crate::{GameEffectData, PeerId, WorldPos};
Expand Down Expand Up @@ -44,7 +45,7 @@ pub struct UpdatePosition {
#[derive(Encode, Decode, Clone)]
pub enum DesToProxy {
InitOrUpdateEntity(FullEntityData),
DeleteEntity(Gid),
DeleteEntity(Gid, Option<NonZero<isize>>),
ReleaseAuthority(Gid),
RequestAuthority { pos: WorldPos, radius: i32 },
UpdatePositions(Vec<UpdatePosition>),
Expand All @@ -56,8 +57,8 @@ pub enum ProxyToDes {
/// Got authority over entity.
GotAuthority(FullEntityData),
RemoveEntities(PeerId),
DeleteEntity(NonZero<isize>),
}

#[derive(Encode, Decode, Clone)]
pub struct InterestRequest {
pub pos: WorldPos,
Expand Down

0 comments on commit b4a4b7a

Please sign in to comment.