diff --git a/Cluster.cs b/Cluster.cs index 4fd1363..f922bc8 100644 --- a/Cluster.cs +++ b/Cluster.cs @@ -113,6 +113,11 @@ public class Cluster : Nucleus { clonedNucleus.nucleiArray = clonedFirstNucleus.nucleiArray; } } + + foreach (Nucleus nucleus in this.clusterNuclei) { + if (nucleus is Cluster clonedSubCluster) + RestoreAllExternalReceivers(clonedSubCluster, this.prefab, this); + } } // Sort the nuclei in a correct evaluation order @@ -204,7 +209,44 @@ public class Cluster : Nucleus { return clone; } + private static void RestoreAllExternalReceivers(Cluster clonedCluster, ClusterPrefab prefabParent, Cluster clonedParent) { + int clonedClusterIx = GetNucleusIndex(clonedParent.clusterNuclei, clonedCluster); + if (prefabParent.nuclei[clonedClusterIx] is not Cluster sourceCluster) + return; + + for (int nucleusIx = 0; nucleusIx < sourceCluster.clusterNuclei.Count; nucleusIx++) { + Nucleus sourceNucleus = sourceCluster.clusterNuclei[nucleusIx]; + if (sourceNucleus is not Neuron sourceNeuron) + continue; + + Nucleus clonedNucleus = clonedCluster.clusterNuclei[nucleusIx]; + if (clonedNucleus is not Neuron clonedNeuron) + continue; + + // copy the receivers (and thus synapses) from the source to the clone + foreach (Nucleus receiver in sourceNeuron.receivers) { + int ix = GetNucleusIndex(prefabParent.nuclei, receiver); + if (ix < 0 || ix >= clonedParent.clusterNuclei.Count) + continue; + + Nucleus clonedReceiver = clonedParent.clusterNuclei[ix]; + + // Find the synapse for the weight + float weight = 1; + foreach (Synapse synapse in receiver.synapses) { + // Find the weight for this synapse + if (synapse.nucleus == sourceNucleus) { + weight = synapse.weight; + break; + } + } + + clonedNeuron.AddReceiver(clonedReceiver, weight); + } + } + } protected void RestoreExternalReceivers(Cluster clone, ClusterPrefab prefabParent, Cluster clonedParent) { + // Loop over all nuclei of this (the prefab of the clone?) for (int nucleusIx = 0; nucleusIx < this.clusterNuclei.Count; nucleusIx++) { Nucleus prefabNucleus = this.clusterNuclei[nucleusIx]; if (prefabNucleus is not Neuron prefabNeuron) @@ -217,7 +259,7 @@ public class Cluster : Nucleus { // Copy the receivers, which will also create the synapses foreach (Nucleus receiver in prefabNeuron.receivers) { int ix = GetNucleusIndex(prefabParent.nuclei, receiver); - if (ix < 0) + if (ix < 0 || ix >= clonedParent.clusterNuclei.Count) continue; //if (clone.clusterNuclei[ix] is not Nucleus clonedReceiver) @@ -249,7 +291,7 @@ public class Cluster : Nucleus { return -1; } - public int GetNucleusIndex(List nuclei, Nucleus nucleus) { + public static int GetNucleusIndex(List nuclei, Nucleus nucleus) { int i = 0; foreach (Nucleus nucleiElement in nuclei) { //for (int i = 0; i < nuclei.Length; i++) { @@ -345,14 +387,14 @@ public class Cluster : Nucleus { receivers = cluster.CollectReceivers(); //if (current is Neuron neuron) { - - foreach (Nucleus receiver in receivers) { - if (visited.Contains(receiver)) { - inDegree[receiver]--; - if (inDegree[receiver] == 0) // If all dependencies resolved - queue.Enqueue(receiver); - } + + foreach (Nucleus receiver in receivers) { + if (visited.Contains(receiver)) { + inDegree[receiver]--; + if (inDegree[receiver] == 0) // If all dependencies resolved + queue.Enqueue(receiver); } + } //} } diff --git a/ClusterReceptor.cs b/ClusterReceptor.cs index 2ecbd8f..33cba20 100644 --- a/ClusterReceptor.cs +++ b/ClusterReceptor.cs @@ -31,7 +31,7 @@ public class ClusterReceptor : Cluster, IReceptor { //CloneFields(clone); // This cloned the prefab with the clusternuclei, // but did not clone the receivers outside the cluster - RestoreExternalReceivers(clone, this.clusterPrefab, parent); + //RestoreExternalReceivers(clone, this.clusterPrefab, parent); return clone; } @@ -158,7 +158,7 @@ public class ClusterReceptor : Cluster, IReceptor { selectedReceiver.name = baseName + ": " + thingName; } - int inputIx = this.GetNucleusIndex(this.clusterNuclei, input); + int inputIx = GetNucleusIndex(this.clusterNuclei, input); if (inputIx < 0) return; diff --git a/Editor/ClusterInspector.cs b/Editor/ClusterInspector.cs index 2ec258a..d35acff 100644 --- a/Editor/ClusterInspector.cs +++ b/Editor/ClusterInspector.cs @@ -1004,8 +1004,8 @@ public class ClusterInspector : Editor { if (synapse.nucleus.parent is ClusterReceptor receptor) { // the new nucleus is part of a (cluster) receptor, // so we have to change all synapses to this nucleus array elements - int oldNucleusIx = subCluster.GetNucleusIndex(subCluster.clusterNuclei, synapse.nucleus); - int newNucleusIx = subCluster.GetNucleusIndex(subCluster.clusterNuclei, newNucleus); + int oldNucleusIx = Cluster.GetNucleusIndex(subCluster.clusterNuclei, synapse.nucleus); + int newNucleusIx = Cluster.GetNucleusIndex(subCluster.clusterNuclei, newNucleus); foreach (Nucleus element in receptor.nucleiArray) { if (element is not ClusterReceptor clusterReceptor) continue;