summaryrefslogtreecommitdiff
path: root/src/game/objlist.h
diff options
context:
space:
mode:
authornavewindre <boneyaard@gmail.com>2025-09-03 20:10:09 +0200
committernavewindre <boneyaard@gmail.com>2025-09-03 20:10:09 +0200
commitf8b92ce3aa08b1445c9f956d8166830946562d12 (patch)
tree94e63a5aec9f8f52b577f56799e0c9201fd976a5 /src/game/objlist.h
a
Diffstat (limited to 'src/game/objlist.h')
-rw-r--r--src/game/objlist.h122
1 files changed, 122 insertions, 0 deletions
diff --git a/src/game/objlist.h b/src/game/objlist.h
new file mode 100644
index 0000000..f8a46d7
--- /dev/null
+++ b/src/game/objlist.h
@@ -0,0 +1,122 @@
+#pragma once
+
+#include "player.h"
+#include "world/world.h"
+
+const U32 OBJ_LIST_MAX = 8192;
+
+struct OBJECT_LIST {
+ template <typename T>
+ T* add( const char* name ) {
+ if( objects.size >= OBJ_LIST_MAX ) {
+ dlog( "OBJECT_LIST::add() : object list is full" );
+ return 0;
+ }
+
+ for( I32 i = 0; i < objects.size; ++i ) {
+ if( !strcmp( name, objects[i]->name ) ) {
+ dlog( "OBJECT_LIST::add() : object %s already exists", name );
+ return 0;
+ }
+ }
+
+ T* newt = new T;
+ newt->classid = T::CLASSID;
+ strcpy( newt->name, name );
+ objects.push( newt );
+ return newt;
+ }
+
+ template < typename T >
+ T* add( const T& obj ) {
+ for( I32 i = 0; i < objects.size; ++i ) {
+ if( !strcmp( obj.name, objects[i]->name ) ) {
+ dlog( "OBJECT_LIST::add() : object %s already exists", obj.name );
+ return 0;
+ }
+ }
+
+ T* newt = new T( obj );
+ newt->classid = T::CLASSID;
+ objects.push( newt );
+ return newt;
+ }
+
+ void remove( OBJECT* obj ) {
+ I32 idx = objects.idx_of( obj );
+ if( idx != -1 )
+ free( objects[idx] );
+ }
+
+ void remove( const char* obj ) {
+ for( I32 i = 0; i < objects.size; ++i ) {
+ if( !strcmp( obj, objects[i]->name ) ) {
+ free( objects[i] );
+ return;
+ }
+ }
+ }
+
+ template < typename T = OBJECT >
+ T* find( const char* name ) {
+ for( I32 i = 0; i < objects.size; ++i ) {
+ if( !strcmp( name, objects[i]->name ) )
+ return objects[i];
+ }
+
+ return 0;
+ }
+
+ void each( LIST<OBJECT*>::ON_EACH_FN func ) {
+ objects.each( func );
+ }
+
+ WORLD* world;
+ PLAYER* pl;
+ LIST<OBJECT*> objects;
+};
+
+extern OBJECT_LIST* objl;
+
+extern OBJECT_LIST* objl_init();
+extern void objl_clear( U8 delete_player = 0 );
+extern void objl_load_world( GAME_DATA* game, WORLD_MAP* map );
+
+
+template < typename T >
+inline T* obj_add( const char* name ) {
+ if( !objl ) {
+ dlog( "obj_add() : objl null\n" );
+ return 0;
+ }
+
+ return objl->add<T>( name );
+}
+
+template < typename T >
+inline T* obj_add( T obj ) {
+ T* newt = new T( obj );
+ newt = objl->add( newt );
+ return newt;
+}
+
+template < typename T >
+inline T* obj_add( OBJECT* obj, const char* name ) {
+ T* newt = objl->add<T>( name );
+ if( !newt )
+ return 0;
+
+ newt->parent = obj;
+ obj->children.push( newt );
+ return newt;
+}
+
+template < typename T >
+inline T* obj_add( OBJECT* obj, T newobj ) {
+ T* newt = objl->add( newobj );
+ if( !newt )
+ return 0;
+
+ newt->parent = obj;
+ obj->children.push( newt );
+}