From ce5b15e44ca47b0895c0d85be260a12b14dccc4f Mon Sep 17 00:00:00 2001
From: MaZiFAU <63099053+MaZiFAU@users.noreply.github.com>
Date: Mon, 3 Jul 2023 00:53:03 +0200
Subject: [PATCH] BugFix/Feature;

BugFix/Feature:
+"Hint" know marks actual Facts in Scene if present
---
 .../FactHandling/FactOrganizer.cs             | 37 ++++++++++++-------
 .../FactHandling/FactSpawner.cs               |  6 +--
 .../FactHandling/Facts/Fact.cs                |  2 +-
 .../Scripts/InteractionEngine/WorldCursor.cs  | 15 +++++---
 .../Scripts/InventoryStuff/ScrollDetails.cs   |  4 +-
 Assets/Scripts/UI/MainMenue/ListLoader.cs     |  3 +-
 6 files changed, 39 insertions(+), 28 deletions(-)

diff --git a/Assets/Scripts/InteractionEngine/FactHandling/FactOrganizer.cs b/Assets/Scripts/InteractionEngine/FactHandling/FactOrganizer.cs
index 94d864e7..0654b782 100644
--- a/Assets/Scripts/InteractionEngine/FactHandling/FactOrganizer.cs
+++ b/Assets/Scripts/InteractionEngine/FactHandling/FactOrganizer.cs
@@ -42,7 +42,7 @@ public class FactOrganizer : IJSONsavable<FactOrganizer>, IDisposable
     /// - <c>Value</c>: <see cref="Fact"/>
     /// </summary>
     [JsonIgnore]
-    protected IReadOnlyDictionary<string, Fact> MyFactSpace
+    public IReadOnlyDictionary<string, Fact> MyFactSpace
     {
         get => GlobalFactDictionary.MyFactSpace(this);
     }
@@ -403,10 +403,11 @@ Fact ReInitializeFact(string old_id)
                 return null;
             }
 
-            Fact new_Fact = old_Fact.ReInitializeMe(_old_to_new, target); // check for dupes?
-
-            _old_to_new.Add(old_Fact.Id, new_Fact.Id); //move up
+            //// old_Fact => map dependent Facts!!
+            //if (!GlobalFactDictionary.FindEquivalent(AllFacts, old_Fact, out string uri, out Fact new_Fact, out _))
+            Fact new_Fact = old_Fact.ReInitializeMe(_old_to_new, target);
 
+            _old_to_new.Add(old_Fact.Id, new_Fact.Id);
             return new_Fact;
         }
     }
@@ -548,7 +549,7 @@ public string Add(Fact value, out bool exists, bool samestep, Gadget gadget, str
         string key;
 #pragma warning restore IDE0018 // Inlinevariablendeklaration
 
-        if (exists = GlobalFactDictionary.FindEquivalent(MyFactSpace, value, out key, out bool _))
+        if (exists = GlobalFactDictionary.FindEquivalent(MyFactSpace, value, out key, out Fact _, out bool _))
         {
             if (exists = MetaInf[key].active) //Fact in Scene?
                 // desired outcome already achieved
@@ -565,7 +566,7 @@ public string Add(Fact value, out bool exists, bool samestep, Gadget gadget, str
             //if (GlobalFactDictionary.FindEquivalent(AllFacts, value, out key, out bool _))
             //    value = AllFacts[key];
             //else
-                key = value.Id;
+            key = value.Id;
 
             GlobalFactDictionary.FactSpaceAdd(this, value);
             MetaInf.Add(key, new meta(marker, true, isImmutable));
@@ -966,6 +967,8 @@ public IEnumerable<string> GetUsedScrolls()
 
     public int GetNumberOfFacts() => MyFactSpace.Count;
 
+    public static bool FindEquivalent(IReadOnlyDictionary<string, Fact> FactSpace, Fact search, out string found_key, out Fact found_value, out bool exact, bool allow_exact = true)
+        => GlobalFactDictionary.FindEquivalent(FactSpace, search, out found_key, out found_value, out exact, allow_exact);
 
     protected static class GlobalFactDictionary
     {
@@ -1035,19 +1038,23 @@ public static void FactSpaceRemove(FactOrganizer me, string key)
 
         //TODO? MMT? PERF: O(n), every Fact-insertion
         /// <summary>
-        /// Looks for existent <see cref="Fact"/> (<paramref name="found"/>) which is very similar or identical (<paramref name="exact"/>) to prposed <see cref="Fact"/> (<paramref name="search"/>)
+        /// Looks for existent <see cref="Fact"/> (<paramref name="found_key"/>) which is very similar or identical (<paramref name="exact"/>) to prposed <see cref="Fact"/> (<paramref name="search"/>)
         /// <remarks>does not check active state</remarks>
         /// </summary>
         /// <param name="FactSpace">to search in</param>
         /// <param name="search">to be searched for</param>
-        /// <param name="found"><see cref="Fact.Id"/> if return value is <c>true</c></param>
-        /// <param name="exact"><c>true</c> iff <paramref name="found"/> == <paramref name="search"/><see cref="Fact.Id">.Id</see></param>
+        /// <param name="found_key"><see cref="Fact.Id"/> if return value is <c>true</c></param>
+        /// <param name="found_value"><see cref="Fact"/> if return value is <c>true</c></param>
+        /// <param name="exact"><c>true</c> iff <paramref name="found_key"/> == <paramref name="search"/><see cref="Fact.Id">.Id</see></param>
+        /// <param name="allow_exact">if set to <c>true</c>: ignores cases where <paramref name="exact"/> returns <c>true</c></param>
         /// <returns><c>true</c> iff the exact same or an equivalent <see cref="Fact"/> to <paramref name="search"/> was found in <see cref="MyFactSpace"/></returns>
-        public static bool FindEquivalent(IReadOnlyDictionary<string, Fact> FactSpace, Fact search, out string found, out bool exact)
+        public static bool FindEquivalent(IReadOnlyDictionary<string, Fact> FactSpace, Fact search, out string found_key, out Fact found_value, out bool exact, bool allow_exact = true)
         {
-            if (exact = FactSpace.ContainsKey(search.Id))
+            if (exact = FactSpace.ContainsKey(search.Id)
+             && allow_exact)
             {
-                found = search.Id;
+                found_key = search.Id;
+                found_value = FactSpace[found_key];
                 return true;
             }
 
@@ -1055,12 +1062,14 @@ public static bool FindEquivalent(IReadOnlyDictionary<string, Fact> FactSpace, F
             {
                 if (entry.Value.Equivalent(search))
                 {
-                    found = entry.Key;
+                    found_key = entry.Key;
+                    found_value = entry.Value;
                     return true;
                 }
             }
 
-            found = null;
+            found_key = null;
+            found_value = null;
             return false;
         }
     }
diff --git a/Assets/Scripts/InteractionEngine/FactHandling/FactSpawner.cs b/Assets/Scripts/InteractionEngine/FactHandling/FactSpawner.cs
index 2ede0e55..681d0712 100644
--- a/Assets/Scripts/InteractionEngine/FactHandling/FactSpawner.cs
+++ b/Assets/Scripts/InteractionEngine/FactHandling/FactSpawner.cs
@@ -147,14 +147,12 @@ public void SpawnCircle(CircleFact circleFact, Transform parent = null)
 
     public void AnimateNonExistingFactTrigger(Fact fact)
     {
-        StartCoroutine(animateNonExistingFact(fact));
+        StartCoroutine(_BlossomAndDie(fact));
 
-        IEnumerator animateNonExistingFact(Fact fact)
+        IEnumerator _BlossomAndDie(Fact fact)
         {
             SpawnFactRepresentation(fact);
 
-            AnimateExistingFactEvent.Invoke(fact.Id, FactWrapper.FactMaterials.Hint);
-
             yield return new WaitForSeconds(GlobalBehaviour.HintAnimationDuration);
 
             RemoveFactEvent.Invoke(fact);
diff --git a/Assets/Scripts/InteractionEngine/FactHandling/Facts/Fact.cs b/Assets/Scripts/InteractionEngine/FactHandling/Facts/Fact.cs
index 03139734..e6cfd7aa 100644
--- a/Assets/Scripts/InteractionEngine/FactHandling/Facts/Fact.cs
+++ b/Assets/Scripts/InteractionEngine/FactHandling/Facts/Fact.cs
@@ -516,7 +516,7 @@ protected bool DependentFactsEquivalent(T f1, T f2)
         => f1.GetGetDependentFactIds()
             .Zip(f2.GetGetDependentFactIds(),
                 (id1, id2) =>
-                    id1.Equals(id2)
+                    id1 == id2
                     || FactOrganizer.AllFacts[id1].Equivalent(FactOrganizer.AllFacts[id2])
             )
             .All(b => b);
diff --git a/Assets/Scripts/InteractionEngine/WorldCursor.cs b/Assets/Scripts/InteractionEngine/WorldCursor.cs
index aaa376b6..bbc26561 100644
--- a/Assets/Scripts/InteractionEngine/WorldCursor.cs
+++ b/Assets/Scripts/InteractionEngine/WorldCursor.cs
@@ -8,6 +8,7 @@
 public class WorldCursor : MonoBehaviour
 {
     public RaycastHit[] Hits;
+    public Fact[] HitFacts;
 
     public float IntersectionDistance = 0.1f;
 
@@ -115,12 +116,10 @@ void Update()
             break; // we had at least 1 succesfull case
         }
 
-        Fact[] facts;
-
         if (i == Hits.Length)
         {
             Hits = new[] { Hits[0] };
-            facts = new Fact[0];
+            HitFacts = new Fact[0];
         }
         else
         {
@@ -128,7 +127,7 @@ void Update()
                        .TakeWhile(h => h.distance - Hits[i].distance < IntersectionDistance)
                        .ToArray();
 
-            facts = Hits
+            HitFacts = Hits
                 .Select(h =>
                     h.transform.TryGetComponent(out FactObject factObj)
                     ? FactOrganizer.AllFacts[factObj.URI]
@@ -138,8 +137,8 @@ void Update()
 
         if (Hits.Length > 1)
         { // we have two or more objects intersecting
-            if (facts[0] is RayFact rayFact1
-             && facts[1] is RayFact rayFact2)
+            if (HitFacts[0] is RayFact rayFact1
+             && HitFacts[1] is RayFact rayFact2)
             {
                 if (Math3d.LineLineIntersection(out Vector3 intersectionPoint
                     , rayFact2.Point1.Point, rayFact2.Dir
@@ -156,6 +155,10 @@ void Update()
         transform.up = Hits[0].normal;
         transform.position = Hits[0].point + .01f * Hits[0].normal;
 
+        //for (i = 0; i < HitFacts.Length; i++) //TODO: not constantly => see ShinyThings.Update
+        //    if (HitFacts[i]?.Id != null)
+        //        CommunicationEvents.AnimateExistingFactEvent.Invoke(HitFacts[i].Id, FactWrapper.FactMaterials.Selected);
+
         if (Input.GetMouseButtonDown(0))
             ClickHandler();
     }
diff --git a/Assets/Scripts/InventoryStuff/ScrollDetails.cs b/Assets/Scripts/InventoryStuff/ScrollDetails.cs
index 22e20cc7..eedf91dc 100644
--- a/Assets/Scripts/InventoryStuff/ScrollDetails.cs
+++ b/Assets/Scripts/InventoryStuff/ScrollDetails.cs
@@ -297,10 +297,10 @@ public void animateHint(string scrollParameterUri)
         else if (null !=
             (fact = LatestRenderedHints.Find(x => x.Id == scrollParameterUri)))
         {
-            if (FactOrganizer.AllFacts.ContainsKey(fact.Id))
+            if (FactOrganizer.FindEquivalent(StageStatic.stage.factState.MyFactSpace, fact, out string found_key, out Fact _, out bool _, false))
                 // existing fact -> Animate that
                 AnimateExistingFactEvent.Invoke(
-                    fact.Id,
+                    found_key,
                     FactWrapper.FactMaterials.Hint
                 );
 
diff --git a/Assets/Scripts/UI/MainMenue/ListLoader.cs b/Assets/Scripts/UI/MainMenue/ListLoader.cs
index a43a6161..c78b473f 100644
--- a/Assets/Scripts/UI/MainMenue/ListLoader.cs
+++ b/Assets/Scripts/UI/MainMenue/ListLoader.cs
@@ -20,7 +20,8 @@ protected void OnEnable()
 
         System.Collections.IEnumerator _Init()
         {
-            yield return new WaitForEndOfFrame();
+            yield return new WaitForFixedUpdate();
+
             Clear();
             Init();
         }
-- 
GitLab