From 7a954873dd6a4dbd9d13c43a6be4952850147467 Mon Sep 17 00:00:00 2001 From: Lemon-King Date: Mon, 21 Oct 2024 01:12:37 -0400 Subject: [PATCH] LemonTree: Refactor and simplify --- resources/assets/mapinfo.lemontree | 2 +- .../assets/zscript/lib/LemonTree/lemontree.zs | 172 +++++------------- .../zscript/lib/LemonTree/lemontreebranch.zs | 15 +- 3 files changed, 54 insertions(+), 135 deletions(-) diff --git a/resources/assets/mapinfo.lemontree b/resources/assets/mapinfo.lemontree index 0628ddc..3c144bd 100644 --- a/resources/assets/mapinfo.lemontree +++ b/resources/assets/mapinfo.lemontree @@ -1,3 +1,3 @@ GameInfo { - AddEventHandlers = "LemonTreeStatic", "LemonTreeSession" + AddEventHandlers = "LemonTree" } \ No newline at end of file diff --git a/resources/assets/zscript/lib/LemonTree/lemontree.zs b/resources/assets/zscript/lib/LemonTree/lemontree.zs index c120da9..959c026 100644 --- a/resources/assets/zscript/lib/LemonTree/lemontree.zs +++ b/resources/assets/zscript/lib/LemonTree/lemontree.zs @@ -1,25 +1,21 @@ -// Cached store will be assigned data during world change, gameplay it is in a locked state -class LemonTree { - static LemonTreeStatic GetStatic() { - LemonTreeStatic handler = LemonTreeStatic(StaticEventHandler.Find("LemonTreeStatic")); - if (handler == null) { - //console.printf("LemonTreeStatic instance not found! Panic?"); - } - return handler; - } - static LemonTreeSession GetSession() { - LemonTreeSession handler = LemonTreeSession(EventHandler.Find("LemonTreeSession")); +class LemonTree : EventHandler { + Map stores; + + // Singleton access method + static LemonTree GetInstance() { + LemonTree handler = LemonTree(EventHandler.Find("LemonTree")); if (handler == null) { - //console.printf("LemonTreeSession instance not found! Panic?"); + //console.printf("LemonTree instance not found! Panic?"); } return handler; } + static LemonTreeBranch GetStore(String key) { - LemonTreeSession ltSession = LemonTree.GetSession(); - if (ltSession) { + LemonTree ltInstance = LemonTree.GetInstance(); + if (ltInstance) { bool exists = false; LemonTreeBranch store; - [store, exists] = ltSession.stores.CheckValue(key); + [store, exists] = ltInstance.stores.CheckValue(key); if (exists) { return store; } @@ -28,129 +24,36 @@ class LemonTree { // are we outside a map? return null; } -} - -class LemonTreeStatic : StaticEventHandler { - Map stores; - Array storeClasses; - - // Singleton access method, should only be accessed by LemonTreeSession - static LemonTreeStatic GetInstance() { - LemonTreeStatic handler = LemonTreeStatic(StaticEventHandler.Find("LemonTreeStatic")); - if (handler == null) { - //console.printf("LemonTreeStatic instance not found! Panic?"); - } - return handler; - } - - static uint GetStoresCount() { - return LemonTree.GetStatic().stores.CountUsed(); - } - - static void MoveStoresFromSession() { - LemonTree.GetStatic().stores.Move(LemonTree.GetSession().stores); - } - static void MoveStoresToSession() { - LemonTree.GetSession().stores.Move(LemonTree.GetStatic().stores); - } - - static void GetStoreClasses(in out Array ioStoreClasses) { - ioStoreClasses.Copy(LemonTree.GetStatic().storeClasses); - } - - override void OnRegister() { - console.printf("LemonTree: Initialized"); - - SetOrder(1000); - - String cvarDataStore = LemonUtil.CVAR_GetString("hxdd_lemontree_sessionstorage", "LemonTreeBranch"); - Array dataStoreClasses; - cvarDataStore.Substitute(" ", ""); - cvarDataStore.split(self.storeClasses, ","); - if (self.storeClasses.Find("LemonTreeBranch") != -1) { - console.printf("LemonTree.Static: The default storage class [LemonTreeBranch] is in use!"); - } - } - - override void NewGame() { - if (Level.MapName.MakeLower() == "titlemap") { - return; - } - if (!!LemonTree.GetSession()) { - LemonTree.GetStatic().MoveStoresFromSession(); - } - Array removal; - foreach(key, store : self.stores) { - if (store._persist) { - console.printf("LemonTree.Static: NewGame, %s Store Persist", key); - store.OnReset(); - } else { - console.printf("LemonTree.Static: NewGame, %s Store Cleared", key); - removal.Push(key); - } - } - if (removal.Size() > 0) { - for (int i = 0; i < removal.Size(); i++) { - console.printf("LemonTree.Static: Removing %s for Reset", removal[i]); - self.stores.Remove(removal[i]); - } - } - } - - override void WorldLoaded(WorldEvent e) { - console.printf("LemonTree.Static: WorldLoaded"); - if (e.IsSaveGame || Level.MapName.MakeLower() == "titlemap") { - return; - } - self.stores.Clear(); - } - - override void WorldUnloaded(WorldEvent e) { - console.printf("LemonTree.Static: WorldUnloaded"); - if (Level.MapName.MakeLower() == "titlemap") { - return; - } - } -} - -class LemonTreeSession : EventHandler { - Map stores; - - // Singleton access method - static LemonTreeSession GetInstance() { - LemonTreeSession handler = LemonTreeSession(EventHandler.Find("LemonTreeSession")); - if (handler == null) { - //console.printf("LemonTreeSession instance not found! Panic?"); - } - return handler; - } - static void MoveStoresFromStatic() { - if (LemonTree.GetStatic().stores.CountUsed() != 0) { - LemonTree.GetSession().stores.Move(LemonTree.GetStatic().stores); - } - } - static void MoveStoresToStatic() { - LemonTree.GetStatic().stores.Move(LemonTree.GetSession().stores); - } // Load data before game starts override void OnRegister() { if (Level.MapName.MakeLower() == "titlemap") { return; } - if (self.stores.CountUsed() == 0) { // loading from a save if the value is > 0 - if (LemonTree.GetStatic().stores.CountUsed() != 0) { - self.stores.Move(LemonTree.GetStatic().stores); - } + + // Recover Branches + if (self.stores.CountUsed() == 0) { Array storeNames; - LemonTree.GetStatic().GetStoreClasses(storeNames); + String cvarDataStore = LemonUtil.CVAR_GetString("hxdd_lemontree_sessionstorage", "LemonTreeBranch"); + cvarDataStore.Substitute(" ", ""); + cvarDataStore.split(storeNames, ","); for (let i = 0; i < storeNames.Size(); i++) { String storeName = storeNames[i]; if (storeName != "") { - LemonTreeBranch newStore = LemonTreeBranch(new(storeName)); - if (newStore) { - newStore.Init(); - self.stores.Insert(storeName, newStore); + ThinkerIterator it = ThinkerIterator.Create(storeName, Thinker.STAT_STATIC); + let foundStore = LemonTreeBranch(it.Next()); + if (foundStore && !foundStore._clearOnMapChange) { + self.stores.Insert(storeName, foundStore); + } else { + if (foundStore) { + foundStore.Destroy(); + } + LemonTreeBranch newStore = LemonTreeBranch(new(storeName)); + if (newStore) { + newStore.ChangeStatNum(Thinker.STAT_STATIC); + newStore.Init(); + self.stores.Insert(storeName, newStore); + } } } } @@ -160,13 +63,24 @@ class LemonTreeSession : EventHandler { } } + override void NewGame() { + if (Level.MapName.MakeLower() == "titlemap") { + return; + } + foreach(store : self.stores) { + store.OnNewGame(); + } + } + override void OnUnregister() { + if (Level.MapName.MakeLower() == "titlemap") { + return; + } foreach(store : self.stores) { if (store) { store.OnMapLeave(); } } - LemonTree.GetSession().MoveStoresToStatic(); } // Event handlers diff --git a/resources/assets/zscript/lib/LemonTree/lemontreebranch.zs b/resources/assets/zscript/lib/LemonTree/lemontreebranch.zs index 7b76ab9..0abbc73 100644 --- a/resources/assets/zscript/lib/LemonTree/lemontreebranch.zs +++ b/resources/assets/zscript/lib/LemonTree/lemontreebranch.zs @@ -1,22 +1,23 @@ // Extend with classes and use mixins, this is just a base class LemonTreeBranch : Thinker { - bool _persist; // allows data to persist between games + bool _clearOnMapChange; LemonTreeBranch GetStore() { - LemonTreeSession ltSession = LemonTree.GetSession(); - if (ltSession) { + LemonTree ltInstance = LemonTree.GetInstance(); + if (ltInstance) { bool exists = false; LemonTreeBranch store; - [store, exists] = ltSession.stores.CheckValue(self.GetClassName()); + [store, exists] = ltInstance.stores.CheckValue(self.GetClassName()); if (exists) { return store; } } + + // are we outside a map? return null; } virtual void Init() { - self._persist = false; console.printf("%s: Init", self.GetClassName()); } @@ -26,6 +27,10 @@ class LemonTreeBranch : Thinker { console.printf("%s: OnReset", self.GetClassName()); } + virtual void OnNewGame() { + console.printf("%s: OnNewGame", self.GetClassName()); + } + // Map event handlers virtual void OnMapEnter() { console.printf("%s: Map Enter", self.GetClassName());