more create component work, still crashes(i dont like component buffers type)

This commit is contained in:
bgkillas 2025-08-15 18:03:01 -04:00
parent 1919f38b09
commit fe386ac2ff
3 changed files with 111 additions and 38 deletions

View file

@ -51,6 +51,7 @@ pub struct GlobalsRef {
pub filenames: &'static StdVec<StdString>,
pub inventory: &'static Inventory,
pub mods: &'static Mods,
pub max_component: &'static usize,
}
#[derive(Debug)]
pub struct GlobalsMut {
@ -67,6 +68,7 @@ pub struct GlobalsMut {
pub filenames: &'static mut StdVec<StdString>,
pub inventory: &'static mut Inventory,
pub mods: &'static mut Mods,
pub max_component: &'static mut usize,
}
#[derive(Debug, Default)]
@ -84,6 +86,7 @@ pub struct Globals {
pub filenames: *mut StdVec<StdString>,
pub inventory: *mut Inventory,
pub mods: *mut Mods,
pub max_component: *mut usize,
}
#[allow(clippy::mut_from_ref)]
impl Globals {
@ -132,6 +135,9 @@ impl Globals {
pub fn mods(&self) -> &'static Mods {
unsafe { self.mods.as_ref().unwrap() }
}
pub fn max_component(&self) -> &'static usize {
unsafe { self.max_component.as_ref().unwrap() }
}
pub fn world_seed_mut(&self) -> &'static mut usize {
unsafe { self.world_seed.as_mut().unwrap() }
}
@ -177,6 +183,9 @@ impl Globals {
pub fn mods_mut(&self) -> &'static mut Mods {
unsafe { self.mods.as_mut().unwrap() }
}
pub fn max_component_mut(&self) -> &'static mut usize {
unsafe { self.max_component.as_mut().unwrap() }
}
pub fn as_ref(&self) -> GlobalsRef {
GlobalsRef {
world_seed: self.world_seed(),
@ -192,6 +201,7 @@ impl Globals {
filenames: self.filenames(),
inventory: self.inventory(),
mods: self.mods(),
max_component: self.max_component(),
}
}
pub fn as_mut(&self) -> GlobalsMut {
@ -209,6 +219,7 @@ impl Globals {
filenames: self.filenames_mut(),
inventory: self.inventory_mut(),
mods: self.mods_mut(),
max_component: self.max_component_mut(),
}
}
pub fn new(lua: LuaState) -> Self {
@ -230,6 +241,7 @@ impl Globals {
let filenames = 0x1207bd4 as *mut StdVec<StdString>;
let inventory = 0x12224f0 as *mut Inventory;
let mods = 0x1207e90 as *mut Mods;
let max_component = 0x1152ff0 as *mut usize;
Self {
world_seed,
new_game_count,
@ -244,6 +256,7 @@ impl Globals {
filenames,
inventory,
mods,
max_component,
}
}
}

View file

@ -1,6 +1,7 @@
use crate::noita::types::{
BitSet, CString, Component, Entity, EntityManager, StdMap, StdString, StdVec, TagManager,
};
use std::ptr;
#[repr(C)]
#[derive(Debug)]
pub struct ComponentData {
@ -83,7 +84,12 @@ pub struct ComponentBuffer {
pub component_list: StdVec<*mut ComponentData>,
}
impl ComponentBuffer {
pub fn create<C: Component>(&mut self, entry: usize, id: usize) -> &'static mut C {
pub fn create<C: Component>(
&mut self,
entity: &mut Entity,
id: usize,
type_id: usize,
) -> &'static mut C {
let com = C::default(ComponentData {
vtable: self
.component_list
@ -91,14 +97,14 @@ impl ComponentBuffer {
.iter()
.find_map(|a| unsafe { a.as_ref().map(|a| a.vtable) })
.unwrap(),
local_id: 0,
local_id: self.component_list.len(),
type_name: self
.component_list
.as_ref()
.iter()
.find_map(|a| unsafe { a.as_ref().map(|a| CString(a.type_name.0)) })
.unwrap(),
type_id: 0,
type_id,
id,
enabled: false,
unk2: [0; 3],
@ -109,16 +115,31 @@ impl ComponentBuffer {
let com = Box::leak(Box::new(com));
let index = self.component_list.len();
self.component_list.push((com as *mut C).cast());
while self.entity_entry.len() <= entry {
if self.entities.len() > index {
self.entities[index] = entity;
} else {
while self.entities.len() < index {
self.entities.push(ptr::null_mut())
}
self.entities.push(entity);
}
while self.entity_entry.len() <= entity.entry {
self.entity_entry.push(self.end)
}
let mut off = entry;
while let Some(next) = self.next.get(off).copied()
&& next != self.end
let mut off;
if let Some(e) = self.entity_entry.get(entity.entry).copied()
&& e != self.end
{
off = next
off = e;
while let Some(next) = self.next.get(off).copied()
&& next != self.end
{
off = next;
}
} else {
off = self.next.len();
self.entity_entry[entity.entry] = off;
}
self.entity_entry[entry] = off;
while self.next.len() <= off {
self.next.push(self.end)
}
@ -135,6 +156,7 @@ impl ComponentBuffer {
component_list: self.component_list.copy(),
off: *off,
next: self.next.copy(),
prev: self.prev.copy(),
end: self.end,
}
} else {
@ -142,6 +164,7 @@ impl ComponentBuffer {
component_list: StdVec::null(),
off: 0,
next: StdVec::null(),
prev: StdVec::null(),
end: 0,
}
}
@ -152,6 +175,7 @@ impl ComponentBuffer {
component_list: self.component_list.copy(),
off: *off,
next: self.next.copy(),
prev: self.prev.copy(),
end: self.end,
}
} else {
@ -159,6 +183,7 @@ impl ComponentBuffer {
component_list: StdVec::null(),
off: 0,
next: StdVec::null(),
prev: StdVec::null(),
end: 0,
}
}
@ -243,6 +268,7 @@ pub struct ComponentIter {
off: usize,
end: usize,
next: StdVec<usize>,
prev: StdVec<usize>,
}
impl Iterator for ComponentIter {
@ -258,12 +284,26 @@ impl Iterator for ComponentIter {
}
}
}
impl DoubleEndedIterator for ComponentIter {
fn next_back(&mut self) -> Option<Self::Item> {
unsafe {
if self.off == self.end {
return None;
}
let com = self.component_list.get(self.off)?.as_ref();
self.off = *self.prev.get(self.off)?;
com
}
}
}
#[derive(Debug)]
pub struct ComponentIterMut {
component_list: StdVec<*mut ComponentData>,
off: usize,
end: usize,
next: StdVec<usize>,
prev: StdVec<usize>,
}
impl Iterator for ComponentIterMut {
@ -279,6 +319,18 @@ impl Iterator for ComponentIterMut {
}
}
}
impl DoubleEndedIterator for ComponentIterMut {
fn next_back(&mut self) -> Option<Self::Item> {
unsafe {
if self.off == self.end {
return None;
}
let com = self.component_list.get(self.off)?.as_mut();
self.off = *self.prev.get(self.off)?;
com
}
}
}
impl BitSet<8> {
pub fn get(&self, n: u8) -> bool {
let out_index = n / 32;

View file

@ -176,8 +176,8 @@ impl EntityManager {
}
pub fn iter_components<C: Component + 'static>(
&self,
component_type_manager: &ComponentTypeManager,
entry: usize,
component_type_manager: &ComponentTypeManager,
) -> impl Iterator<Item = &'static C> {
let index = component_type_manager
.component_buffer_indices
@ -192,8 +192,8 @@ impl EntityManager {
}
pub fn iter_components_mut<C: Component + 'static>(
&mut self,
component_type_manager: &mut ComponentTypeManager,
entry: usize,
component_type_manager: &mut ComponentTypeManager,
) -> impl Iterator<Item = &'static mut C> {
component_type_manager
.get_mut::<C>(self)
@ -202,8 +202,8 @@ impl EntityManager {
}
pub fn iter_enabled_components<C: Component + 'static>(
&self,
component_type_manager: &ComponentTypeManager,
entry: usize,
component_type_manager: &ComponentTypeManager,
) -> impl Iterator<Item = &'static C> {
component_type_manager
.get::<C>(self)
@ -212,8 +212,8 @@ impl EntityManager {
}
pub fn iter_enabled_components_mut<C: Component + 'static>(
&mut self,
component_type_manager: &mut ComponentTypeManager,
entry: usize,
component_type_manager: &mut ComponentTypeManager,
) -> impl Iterator<Item = &'static mut C> {
component_type_manager
.get_mut::<C>(self)
@ -222,8 +222,8 @@ impl EntityManager {
}
pub fn iter_disabled_components<C: Component + 'static>(
&self,
component_type_manager: &ComponentTypeManager,
entry: usize,
component_type_manager: &ComponentTypeManager,
) -> impl Iterator<Item = &'static C> {
component_type_manager
.get::<C>(self)
@ -232,8 +232,8 @@ impl EntityManager {
}
pub fn iter_disabled_components_mut<C: Component + 'static>(
&mut self,
component_type_manager: &mut ComponentTypeManager,
entry: usize,
component_type_manager: &mut ComponentTypeManager,
) -> impl Iterator<Item = &'static mut C> {
component_type_manager
.get_mut::<C>(self)
@ -242,32 +242,40 @@ impl EntityManager {
}
pub fn create_component<C: Component + 'static>(
&mut self,
entry: usize,
component_type_manager: &'static mut ComponentTypeManager,
) -> &'static mut C {
let id = component_type_manager.next_id;
component_type_manager.next_id += 1;
let buffer = self.get_component_buffer_mut::<C>(component_type_manager);
buffer.create::<C>(entry, id)
entity: &mut Entity,
max_component: &mut usize,
component_type_manager: &mut ComponentTypeManager,
) -> &mut C {
let index = component_type_manager
.component_buffer_indices
.get(C::STD_NAME)
.copied()
.unwrap();
let mgr = self.component_buffers.get(index).unwrap();
let com = unsafe { mgr.as_mut() }
.unwrap()
.create::<C>(entity, *max_component, index);
*max_component += 1;
com
}
pub fn get_component_buffer<C: Component + 'static>(
pub fn get_component_buffer<'a, C: Component + 'static>(
&self,
component_type_manager: &'static ComponentTypeManager,
) -> &'static ComponentBuffer {
component_type_manager: &'a ComponentTypeManager,
) -> &'a ComponentBuffer {
//TODO this needs to deal with when it does not exist
component_type_manager.get::<C>(self)
}
pub fn get_component_buffer_mut<C: Component + 'static>(
pub fn get_component_buffer_mut<'a, C: Component + 'static>(
&mut self,
component_type_manager: &'static mut ComponentTypeManager,
) -> &'static mut ComponentBuffer {
component_type_manager: &'a mut ComponentTypeManager,
) -> &'a mut ComponentBuffer {
//TODO this needs to deal with when it does not exist
component_type_manager.get_mut::<C>(self)
}
pub fn get_first_component<C: Component + 'static>(
&self,
component_type_manager: &ComponentTypeManager,
entry: usize,
component_type_manager: &ComponentTypeManager,
) -> Option<&'static C> {
component_type_manager
.get::<C>(self)
@ -276,8 +284,8 @@ impl EntityManager {
}
pub fn get_first_component_mut<C: Component + 'static>(
&mut self,
component_type_manager: &mut ComponentTypeManager,
entry: usize,
component_type_manager: &mut ComponentTypeManager,
) -> Option<&'static mut C> {
component_type_manager
.get_mut::<C>(self)
@ -286,8 +294,8 @@ impl EntityManager {
}
pub fn get_first_enabled_component<C: Component + 'static>(
&self,
component_type_manager: &ComponentTypeManager,
entry: usize,
component_type_manager: &ComponentTypeManager,
) -> Option<&'static C> {
component_type_manager
.get::<C>(self)
@ -296,8 +304,8 @@ impl EntityManager {
}
pub fn get_first_enabled_component_mut<C: Component + 'static>(
&mut self,
component_type_manager: &mut ComponentTypeManager,
entry: usize,
component_type_manager: &mut ComponentTypeManager,
) -> Option<&'static mut C> {
component_type_manager
.get_mut::<C>(self)
@ -306,8 +314,8 @@ impl EntityManager {
}
pub fn get_first_disabled_component<C: Component + 'static>(
&self,
component_type_manager: &ComponentTypeManager,
entry: usize,
component_type_manager: &ComponentTypeManager,
) -> Option<&'static C> {
component_type_manager
.get::<C>(self)
@ -316,8 +324,8 @@ impl EntityManager {
}
pub fn get_first_disabled_component_mut<C: Component + 'static>(
&mut self,
component_type_manager: &mut ComponentTypeManager,
entry: usize,
component_type_manager: &mut ComponentTypeManager,
) -> Option<&'static mut C> {
component_type_manager
.get_mut::<C>(self)
@ -355,10 +363,10 @@ impl EntityManager {
}
pub fn iter_in_radius_with_tag(
&self,
tag_manager: &TagManager<u16>,
pos: Vec2,
radius: f32,
tag: &StdString,
tag_manager: &TagManager<u16>,
) -> impl Iterator<Item = &'static Entity> {
if let Some(tag) = tag_manager.tag_indices.get(tag).copied()
&& let Some(ents) = self.entity_buckets.get(tag as usize)
@ -384,10 +392,10 @@ impl EntityManager {
}
pub fn iter_in_radius_with_tag_mut(
&mut self,
tag_manager: &TagManager<u16>,
pos: Vec2,
radius: f32,
tag: &StdString,
tag_manager: &TagManager<u16>,
) -> impl Iterator<Item = &'static mut Entity> {
if let Some(tag) = tag_manager.tag_indices.get(tag).copied()
&& let Some(ents) = self.entity_buckets.get_mut(tag as usize)
@ -415,9 +423,9 @@ impl EntityManager {
}
pub fn get_closest_with_tag(
&self,
tag_manager: &TagManager<u16>,
pos: Vec2,
tag: &StdString,
tag_manager: &TagManager<u16>,
) -> Option<&'static Entity> {
tag_manager.tag_indices.get(tag).copied().and_then(|tag| {
self.entity_buckets.get(tag as usize).and_then(|b| {
@ -444,9 +452,9 @@ impl EntityManager {
}
pub fn get_closest_with_tag_mut(
&mut self,
tag_manager: &TagManager<u16>,
pos: Vec2,
tag: &StdString,
tag_manager: &TagManager<u16>,
) -> Option<&'static mut Entity> {
tag_manager.tag_indices.get(tag).copied().and_then(|tag| {
self.entity_buckets.get_mut(tag as usize).and_then(|b| {