From 308a6a1ee7893c6dd02208045b02fce5483add50 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Mon, 20 Apr 2026 12:53:58 +0200 Subject: [PATCH] The Entities are battling --- Editor/ClusterInspector.cs | 89 +++++++++++---------------------- Editor/ClusterViewer.cs | 43 ++++------------ Runtime/Scripts/Core/Cluster.cs | 26 ++++++++++ Runtime/Scripts/Core/Neuron.cs | 18 +++++-- 4 files changed, 78 insertions(+), 98 deletions(-) diff --git a/Editor/ClusterInspector.cs b/Editor/ClusterInspector.cs index d4fe1c6..60ff610 100644 --- a/Editor/ClusterInspector.cs +++ b/Editor/ClusterInspector.cs @@ -174,23 +174,6 @@ namespace NanoBrain { memory.staticMemory = EditorGUILayout.Toggle("Static Memory", memory.staticMemory); } - // if (this.currentNucleus is IReceptor receptor1) { - // EditorGUILayout.BeginHorizontal(); - // EditorGUILayout.IntField("Array size", receptor1.nucleiArray.Count()); - // if (GUILayout.Button("Add")) { - // Undo.RecordObject(prefabAsset, "Array add " + prefabAsset.name); - // receptor1.AddReceptorElement(this.prefab); - // anythingChanged = true; - // } - // if (GUILayout.Button("Del")) { - // Undo.RecordObject(prefabAsset, "Array delete " + prefabAsset.name); - // receptor1.RemoveReceptorElement(); - // anythingChanged = true; - // } - // EditorGUILayout.EndHorizontal(); - - // } - // else if (this.currentNucleus is Cluster cluster) { EditorGUILayout.BeginHorizontal(); if (cluster.siblingClusters != null && cluster.siblingClusters.Length > 1) @@ -356,6 +339,8 @@ namespace NanoBrain { DeleteNucleus(this.currentNucleus); if (this.currentNucleus is Cluster subCluster) { + if (GUILayout.Button("Reimport Cluster")) + ReimportCluster(subCluster); if (GUILayout.Button("Edit Cluster")) EditCluster(subCluster); } @@ -376,26 +361,26 @@ namespace NanoBrain { } } - void OnSceneGUI(SceneView sceneView) { - if (this.gameObject != null) { - // if (this.currentNucleus is IReceptor receptor) { - // foreach (Nucleus nucleus in receptor.nucleiArray) { - // if (nucleus is Neuron neuron) { - // Vector3 worldVector = this.gameObject.transform.TransformVector(neuron.outputValue); - // Handles.color = Color.yellow; - // Handles.DrawLine(this.gameObject.transform.position, this.gameObject.transform.position + worldVector); - // } - // } - // } - // else { - if (this.currentNucleus is Neuron currentNeuron) { - Vector3 worldVector = this.gameObject.transform.TransformVector(currentNeuron.outputValue); - Handles.color = Color.yellow; - Handles.DrawLine(this.gameObject.transform.position, this.gameObject.transform.position + worldVector); - } - // } - } - } + // void OnSceneGUI(SceneView sceneView) { + // if (this.gameObject != null) { + // // if (this.currentNucleus is IReceptor receptor) { + // // foreach (Nucleus nucleus in receptor.nucleiArray) { + // // if (nucleus is Neuron neuron) { + // // Vector3 worldVector = this.gameObject.transform.TransformVector(neuron.outputValue); + // // Handles.color = Color.yellow; + // // Handles.DrawLine(this.gameObject.transform.position, this.gameObject.transform.position + worldVector); + // // } + // // } + // // } + // // else { + // if (this.currentNucleus is Neuron currentNeuron) { + // Vector3 worldVector = this.gameObject.transform.TransformVector(currentNeuron.outputValue); + // Handles.color = Color.yellow; + // Handles.DrawLine(this.gameObject.transform.position, this.gameObject.transform.position + worldVector); + // } + // // } + // } + // } #region Synapses @@ -446,36 +431,18 @@ namespace NanoBrain { subclusterInstance.defaultOutput.AddReceiver(nucleus); } - // protected virtual void AddReceptorInput(Nucleus nucleus) { - // Receptor newReceptor = new(this.prefab, "New Receptor"); - // newReceptor.AddReceiver(nucleus); - // this.currentNucleus = newReceptor; - // BuildLayers(); - // } - - // protected virtual void AddClusterReceptorInput(Nucleus nucleus) { - // ClusterPickerWindow.ShowPicker(prefab => OnClusterReceptorPicked(nucleus, prefab), "Select Cluster"); - // } - // private void OnClusterReceptorPicked(Nucleus nucleus, ClusterPrefab selectedPrefab) { - // ClusterReceptor clusterInstance = new(selectedPrefab, this.prefab, "New " + selectedPrefab.name); - // clusterInstance.defaultOutput.AddReceiver(nucleus); - // this.currentNucleus = clusterInstance; - // BuildLayers(); - // } - private void EditCluster(Cluster subCluster) { // May be used with storedPrefab... Selection.activeObject = subCluster.prefab; EditorGUIUtility.PingObject(subCluster.prefab); - var editor = Editor.CreateEditor(subCluster.prefab); + _ = CreateEditor(subCluster.prefab); } - // protected virtual void AddClusterArrayInput(Nucleus nucleus) { - // ClusterPickerWindow.ShowPicker(prefab => OnPickedClusterArray(nucleus, prefab), "Select Cluster"); - // } - // private void OnPickedClusterArray(Nucleus nucleus, ClusterPrefab selectedPrefab) { - // _ = new ClusterArray(selectedPrefab, this.prefab, 1, nucleus); - // } + private void ReimportCluster(Cluster subCluster) { + Cluster reimportedCluster = new(subCluster.prefab, this.prefab); + subCluster.MoveReceivers(reimportedCluster); + // subcluster should be garbage now... + } int selectedConnectNucleus = -1; // Connect to another nucleus in the same cluster diff --git a/Editor/ClusterViewer.cs b/Editor/ClusterViewer.cs index 6a386b1..8e55da5 100644 --- a/Editor/ClusterViewer.cs +++ b/Editor/ClusterViewer.cs @@ -321,17 +321,9 @@ namespace NanoBrain { if (synapse.neuron == null) continue; - // if (synapse.neuron is Receptor receptor) { - // if (drawnArrays.Contains(receptor.nucleiArray)) - // continue; - // drawnArrays.Add(receptor.nucleiArray); - // } - // else if (synapse.neuron.parent is ClusterReceptor clusterReceptor) { - // if (drawnArrays.Contains(clusterReceptor.nucleiArray)) - // continue; - // drawnArrays.Add(clusterReceptor.nucleiArray); - // } - if (synapse.neuron.parent is Cluster cluster && cluster.siblingClusters != null) { + if (synapse.neuron.parent is Cluster cluster && + cluster.siblingClusters != null && + synapse.neuron.parent != nucleus.parent) { if (drawnArrays.Contains(cluster.siblingClusters)) continue; drawnArrays.Add(cluster.siblingClusters); @@ -421,28 +413,6 @@ namespace NanoBrain { fontStyle = FontStyle.Bold, }; - // if (nucleus is IReceptor receptor1) { - // if (expandArray) { - // // Put array indices above elements - // style.alignment = TextAnchor.LowerCenter; - // Vector3 labelPos1 = position + Vector3.down * (size + 5); // below disc - // int colonPos1 = nucleus.name.IndexOf(":"); - // if (colonPos1 > 0) { - // string extName = nucleus.name[(colonPos1 + 2)..]; - // Handles.Label(labelPos1, extName, style); - // } - // } - // else { - // // draw the array size label - // if (color.grayscale > 0.5f) - // style.normal.textColor = Color.black; - // else - // style.normal.textColor = Color.white; - // Handles.Label(labelPosition, receptor1.nucleiArray.Length.ToString(), style); - // style.normal.textColor = Color.white; - // } - // } - // else if (nucleus.parent != null && nucleus.parent is Cluster parentCluster) { if (expandArray) { // Put array indices above elements @@ -579,6 +549,13 @@ namespace NanoBrain { else expandArray = false; } + else if (nucleus.parent != this.currentNucleus.parent) { + // We go to a different cluster + // select the cluster, not the neuron in the cluster + this.currentNucleus = nucleus.parent; + expandArray = false; + BuildLayers(); + } else { this.currentNucleus = nucleus; expandArray = false; diff --git a/Runtime/Scripts/Core/Cluster.cs b/Runtime/Scripts/Core/Cluster.cs index 731e435..2fb58b2 100644 --- a/Runtime/Scripts/Core/Cluster.cs +++ b/Runtime/Scripts/Core/Cluster.cs @@ -544,6 +544,32 @@ namespace NanoBrain { return receivers; } + public void MoveReceivers(Cluster newCluster) { + foreach (Nucleus outputNucleus in this.clusterNuclei) { + if (outputNucleus is not Neuron output) + continue; + + // Find the existing output in the new cluster + if (newCluster.GetNucleus(output.name) is not Neuron newOutput) { + Debug.LogWarning("Could not find output {output.name} in {newCluster.name}"); + continue; + } + Debug.Log($"Check {output.name} receivers"); + Nucleus[] receivers = output.receivers.ToArray(); + foreach (Nucleus receiver in receivers) { + Debug.Log("."); + if (receiver.clusterPrefab != this.prefab) { + // Replace synapse with new synapse + // to the new cluster + Debug.Log($"move {receiver.name} from {output.name} to {newOutput.name}"); + Synapse synapse = receiver.GetSynapse(output); + newOutput.AddReceiver(receiver, synapse.weight); + output.RemoveReceiver(receiver); + } + } + } + } + #endregion Receivers #region Update diff --git a/Runtime/Scripts/Core/Neuron.cs b/Runtime/Scripts/Core/Neuron.cs index f4b7338..530f44f 100644 --- a/Runtime/Scripts/Core/Neuron.cs +++ b/Runtime/Scripts/Core/Neuron.cs @@ -209,7 +209,12 @@ namespace NanoBrain { public float outputSqrMagnitude => _outputValue.sqrMagnitude; #endif - public bool isFiring => this.outputMagnitude > 0.5f; + public bool isFiring { + get { + SleepCheck(); + return this.outputMagnitude > 0.5f; + } + } public Action WhenFiring; @@ -221,14 +226,12 @@ namespace NanoBrain { #else this.bias = new Vector3(0,0,0); #endif + this._outputValue = this.bias; } } - // [NonSerialized] - // public int stale = 1000; [NonSerialized] public float lastUpdate = 0; - // public readonly int staleValueForSleep = 20; public readonly float timeToSleep = 1f; /// \copydoc NanoBrain::Nucleus::ShallowCloneTo @@ -521,8 +524,15 @@ namespace NanoBrain { } public virtual void RemoveReceiver(Nucleus receiverToRemove) { + int n1 = _receivers.Count; this._receivers.RemoveAll(receiver => receiver == receiverToRemove); + int n2 = _receivers.Count; + Debug.Log($" Removed {n1} - {n2} receivers"); + + n1 = receiverToRemove.synapses.Count; receiverToRemove.synapses.RemoveAll(synapse => synapse.neuron == this); + n2 = receiverToRemove.synapses.Count; + Debug.Log($" Removed {n1} - {n2} synapses"); }