From 445084f4b0ce65a8ade312e04f5d607735cc39ed Mon Sep 17 00:00:00 2001
From: Misha <106913236+MishaProductions@users.noreply.github.com>
Date: Sun, 28 Jan 2024 17:10:04 -0500
Subject: [PATCH] Implement LegoOmni::DeleteObject (#501)

* Push changes

* fixes

* Match

---------

Co-authored-by: Christian Semmler <mail@csemmler.com>
---
 LEGO1/lego/legoomni/include/legoomni.h      |  1 +
 LEGO1/lego/legoomni/include/legoworldlist.h |  3 +
 LEGO1/lego/legoomni/src/main/legoomni.cpp   | 70 +++++++++++++++++++--
 3 files changed, 69 insertions(+), 5 deletions(-)

diff --git a/LEGO1/lego/legoomni/include/legoomni.h b/LEGO1/lego/legoomni/include/legoomni.h
index 2f2622c7..7a8ee7f0 100644
--- a/LEGO1/lego/legoomni/include/legoomni.h
+++ b/LEGO1/lego/legoomni/include/legoomni.h
@@ -105,6 +105,7 @@ public:
 
 	LegoWorld* FindWorld(const MxAtomId& p_atom, MxS32 p_entityid);
 	void AddWorld(LegoWorld* p_world);
+	void DeleteWorld(LegoWorld* p_world);
 	void FUN_1005b4f0(MxBool p_disable, MxU16 p_flags);
 
 	LegoVideoManager* GetVideoManager() { return (LegoVideoManager*) m_videoManager; }
diff --git a/LEGO1/lego/legoomni/include/legoworldlist.h b/LEGO1/lego/legoomni/include/legoworldlist.h
index 339f5393..64849968 100644
--- a/LEGO1/lego/legoomni/include/legoworldlist.h
+++ b/LEGO1/lego/legoomni/include/legoworldlist.h
@@ -86,4 +86,7 @@ public:
 // SYNTHETIC: LEGO1 0x10059be0
 // MxPtrList<LegoWorld>::`scalar deleting destructor'
 
+// TEMPLATE: LEGO1 0x1005b740
+// MxList<LegoWorld *>::DeleteEntry
+
 #endif // LEGOWORLDLIST_H
diff --git a/LEGO1/lego/legoomni/src/main/legoomni.cpp b/LEGO1/lego/legoomni/src/main/legoomni.cpp
index bd28f071..743489a0 100644
--- a/LEGO1/lego/legoomni/src/main/legoomni.cpp
+++ b/LEGO1/lego/legoomni/src/main/legoomni.cpp
@@ -19,6 +19,7 @@
 #include "mxdsfile.h"
 #include "mxomnicreateflags.h"
 #include "mxomnicreateparam.h"
+#include "mxstreamer.h"
 #include "mxticklemanager.h"
 #include "mxtransitionmanager.h"
 
@@ -605,10 +606,46 @@ void LegoOmni::AddWorld(LegoWorld* p_world)
 	m_worldList->Append(p_world);
 }
 
-// STUB: LEGO1 0x1005af10
-void LegoOmni::RemoveWorld(const MxAtomId&, MxLong)
+// FUNCTION: LEGO1 0x1005adb0
+void LegoOmni::DeleteWorld(LegoWorld* p_world)
 {
-	// TODO
+	if (m_worldList) {
+		LegoWorldListCursor cursor(m_worldList);
+
+		if (cursor.Find(p_world)) {
+			cursor.Detach();
+
+			if (m_currentWorld == p_world) {
+				m_currentWorld = NULL;
+			}
+
+			delete p_world;
+		}
+	}
+}
+
+// FUNCTION: LEGO1 0x1005af10
+void LegoOmni::RemoveWorld(const MxAtomId& p_atom, MxLong p_objectId)
+{
+	if (m_worldList) {
+		LegoWorldListCursor a(m_worldList);
+		LegoWorldListCursor b(m_worldList);
+		LegoWorld* world;
+
+		a.Head();
+		while (a.Current(world)) {
+			b = a;
+			b.Next();
+
+			if ((p_objectId == -1 || world->GetEntityId() == p_objectId) &&
+				(!p_atom.GetInternal() || world->GetAtom() == p_atom)) {
+				a.Detach();
+				delete world;
+			}
+
+			a = b;
+		}
+	}
 }
 
 // FUNCTION: LEGO1 0x1005b0c0
@@ -628,10 +665,33 @@ LegoWorld* LegoOmni::FindWorld(const MxAtomId& p_atom, MxS32 p_entityid)
 	return NULL;
 }
 
-// STUB: LEGO1 0x1005b1d0
+// FUNCTION: LEGO1 0x1005b1d0
 void LegoOmni::DeleteObject(MxDSAction& p_dsAction)
 {
-	// TODO
+	if (p_dsAction.GetAtomId().GetInternal() != NULL) {
+		LegoWorld* world = FindWorld(p_dsAction.GetAtomId(), p_dsAction.GetObjectId());
+		if (world) {
+			DeleteWorld(world);
+			return;
+		}
+
+		if (m_currentWorld != NULL) {
+			MxCore* entity = m_currentWorld->Find(p_dsAction.GetAtomId(), p_dsAction.GetObjectId());
+			if (entity) {
+				m_currentWorld->Remove(entity);
+
+				if (entity->IsA("MxPresenter")) {
+					Streamer()->FUN_100b98f0(((MxPresenter*) entity)->GetAction());
+					((MxPresenter*) entity)->EndAction();
+				}
+				else {
+					delete entity;
+				}
+				return;
+			}
+		}
+	}
+	MxOmni::DeleteObject(p_dsAction);
 }
 
 // FUNCTION: LEGO1 0x1005b2f0