cache frame num and world pos

This commit is contained in:
bgkillas 2025-06-26 11:46:34 -04:00
parent 651b69396b
commit 618cf08439
3 changed files with 42 additions and 14 deletions

View file

@ -1295,6 +1295,8 @@ pub struct EntityManager {
current_data: EntityData,
has_ran: bool,
pub files: Option<FxHashMap<Cow<'static, str>, Vec<String>>>,
frame_num: i32,
camera_pos: (f64, f64),
}
impl Default for EntityManager {
fn default() -> Self {
@ -1304,6 +1306,8 @@ impl Default for EntityManager {
current_data: Default::default(),
has_ran: false,
files: Some(FxHashMap::with_capacity_and_hasher(512, FxBuildHasher)),
frame_num: -1,
camera_pos: (0.0, 0.0),
}
}
}
@ -1357,6 +1361,24 @@ impl EntityData {
}
}
impl EntityManager {
pub fn init_frame_num(&mut self) -> eyre::Result<()> {
if self.frame_num == -1 {
self.frame_num = raw::game_get_frame_num()?;
} else {
self.frame_num += 1;
}
Ok(())
}
pub fn frame_num(&self) -> i32 {
self.frame_num
}
pub fn init_pos(&mut self) -> eyre::Result<()> {
self.camera_pos = raw::game_get_camera_pos()?;
Ok(())
}
pub fn camera_pos(&self) -> (f64, f64) {
self.camera_pos
}
pub fn cache_entity(&mut self, ent: EntityID) -> eyre::Result<()> {
self.cache.insert(ent, EntityData::new(ent)?);
Ok(())

View file

@ -9,7 +9,6 @@ use bimap::BiHashMap;
use diff_model::{DES_TAG, LocalDiffModel, RemoteDiffModel, entity_is_item};
use eyre::{Context, OptionExt};
use interest::InterestTracker;
use noita_api::raw::game_get_frame_num;
use noita_api::serialize::serialize_entity;
use noita_api::{
CachedTag, ComponentTag, DamageModelComponent, DamageType, EntityID, EntityManager,
@ -174,8 +173,13 @@ impl EntitySync {
}
Ok(())
}
pub(crate) fn spawn_once(&mut self, ctx: &mut ModuleCtx, frame_num: usize) -> eyre::Result<()> {
let (x, y) = noita_api::raw::game_get_camera_pos()?;
pub(crate) fn spawn_once(
&mut self,
ctx: &mut ModuleCtx,
frame_num: usize,
x: f64,
y: f64,
) -> eyre::Result<()> {
let len = self.spawn_once.len();
if len > 0 {
let batch_size = (len / 20).max(1);
@ -601,10 +605,12 @@ impl Module for EntitySync {
fn on_world_update(&mut self, ctx: &mut ModuleCtx) -> eyre::Result<()> {
let start = std::time::Instant::now();
let (x, y) = noita_api::raw::game_get_camera_pos()?;
self.entity_manager.init_pos()?;
self.entity_manager.init_frame_num()?;
let (x, y) = self.entity_manager.camera_pos();
let pos = WorldPos::from_f64(x, y);
self.interest_tracker.set_center(x, y);
let frame_num = game_get_frame_num()? as usize;
let frame_num = self.entity_manager.frame_num();
if frame_num < 10 {
return Ok(());
}
@ -805,7 +811,7 @@ impl Module for EntitySync {
}
}
}
if let Err(s) = self.spawn_once(ctx, frame_num) {
if let Err(s) = self.spawn_once(ctx, frame_num as usize, x, y) {
crate::print_error(s)?;
}
@ -817,7 +823,7 @@ impl Module for EntitySync {
},
))?;
}
let pos_data = self.local_diff_model.get_pos_data(frame_num);
let pos_data = self.local_diff_model.get_pos_data(frame_num as usize);
if !pos_data.is_empty() {
ctx.net
.send(&NoitaOutbound::DesToProxy(UpdatePositions(pos_data)))?;

View file

@ -2,7 +2,7 @@ use super::NetManager;
use crate::{ephemerial, modules::ModuleCtx, my_peer_id, print_error};
use bimap::BiHashMap;
use eyre::{Context, OptionExt, eyre};
use noita_api::raw::{game_get_frame_num, raytrace_platforms};
use noita_api::raw::raytrace_platforms;
use noita_api::serialize::{deserialize_entity, serialize_entity};
use noita_api::{
AIAttackComponent, AbilityComponent, AdvancedFishAIComponent, AnimalAIComponent,
@ -411,7 +411,7 @@ impl LocalDiffModelTracker {
if let Some(vel) =
entity_manager.try_get_first_component::<VelocityComponent>(ComponentTag::None)?
{
let (cx, cy) = noita_api::raw::game_get_camera_pos()?;
let (cx, cy) = entity_manager.camera_pos();
if ((cx - x) as f32).powi(2) + ((cy - y) as f32).powi(2) > 512.0 * 512.0 {
vel.set_gravity_y(0.0)?;
vel.set_air_friction(10.0)?;
@ -438,7 +438,7 @@ impl LocalDiffModelTracker {
{
info.cost = item_cost.cost()?;
} else if entity_manager.has_tag(const { CachedTag::from_tag("boss_wizard") }) {
info.cost = game_get_frame_num()? as i64;
info.cost = entity_manager.frame_num() as i64;
info.counter = entity
.children(None)
.filter_map(|ent| {
@ -651,7 +651,7 @@ impl LocalDiffModelTracker {
if let Some(cost) = entity_manager
.try_get_first_component::<ItemCostComponent>(ComponentTag::None)?
{
let (cx, cy) = noita_api::raw::game_get_camera_pos()?;
let (cx, cy) = entity_manager.camera_pos();
if ((cx - x) as f32).powi(2) + ((cy - y) as f32).powi(2) < 256.0 * 256.0 {
cost.set_stealable(true)?;
entity_manager.remove_component(var)?;
@ -1142,7 +1142,7 @@ impl LocalDiffModel {
entity_manager: &mut EntityManager,
) -> eyre::Result<(Vec<(WorldPos, SpawnOnce)>, usize)> {
self.update_buffer.clear();
let (cam_x, cam_y) = noita_api::raw::game_get_camera_pos()?;
let (cam_x, cam_y) = entity_manager.camera_pos();
let cam_x = cam_x as f32;
let cam_y = cam_y as f32;
let mut dead = Vec::with_capacity(self.tracker.pending_death_notify.len());
@ -2398,7 +2398,7 @@ pub fn init_remote_entity(
entity_manager.remove_component(var)?;
}
if let Some(var) = entity_manager.get_var(const { VarName::from_str("throw_time") }) {
var.set_value_int(game_get_frame_num().unwrap_or(0) - 4)?;
var.set_value_int(entity_manager.frame_num() - 4)?;
}
if let Some(lid) = lid {
@ -2550,7 +2550,7 @@ fn _safe_wandkill(entity: &mut EntityManager) -> eyre::Result<()> {
lc.add_tag("enabled_in_world")?;
lc.add_tag("enabled_in_hand")?;
lc.set_execute_on_added(false)?;
lc.set_m_next_execution_time(game_get_frame_num()? + 1)?;
lc.set_m_next_execution_time(entity.frame_num() + 1)?;
Ok(())
}