Multiple players working-ish
This commit is contained in:
parent
335dae788b
commit
b2bc92b05f
@ -64,6 +64,22 @@ namespace NanoBrain {
|
|||||||
public class GraphEditor : GraphView {
|
public class GraphEditor : GraphView {
|
||||||
|
|
||||||
protected ClusterPrefab prefab;
|
protected ClusterPrefab prefab;
|
||||||
|
protected Nucleus currentPrefabNucleus;
|
||||||
|
|
||||||
|
protected override Nucleus currentNucleus {
|
||||||
|
get => base.currentNucleus;
|
||||||
|
set {
|
||||||
|
base.currentNucleus = value;
|
||||||
|
this.currentPrefabNucleus = this.prefab.GetNucleus(value.name);
|
||||||
|
// int nucleusIx = this.prefab.nuclei.IndexOf(base.currentNucleus);
|
||||||
|
// if (nucleusIx >= 0)
|
||||||
|
// this.currentPrefabNucleus = this.prefab.nuclei[nucleusIx];
|
||||||
|
// else {
|
||||||
|
// Debug.LogWarning("Could not find nucleus in prefab");
|
||||||
|
// this.currentPrefabNucleus = this.prefab.GetNucleus(value.name);
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public GraphEditor(ClusterPrefab prefab) : base(prefab.output.parent) {
|
public GraphEditor(ClusterPrefab prefab) : base(prefab.output.parent) {
|
||||||
this.prefab = prefab;
|
this.prefab = prefab;
|
||||||
@ -146,10 +162,10 @@ namespace NanoBrain {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
GUIStyle headerStyle = new(EditorStyles.boldLabel) {
|
GUIStyle headerStyle = new(EditorStyles.boldLabel) {
|
||||||
alignment = TextAnchor.MiddleLeft,
|
alignment = TextAnchor.MiddleLeft,
|
||||||
margin = new RectOffset(10, 0, 4, 4)
|
margin = new RectOffset(10, 0, 4, 4)
|
||||||
};
|
};
|
||||||
// Nucleus type
|
// Nucleus type
|
||||||
string nucleusType = this.currentNucleus.GetType().Name;
|
string nucleusType = this.currentNucleus.GetType().Name;
|
||||||
GUILayout.Label(nucleusType, headerStyle);
|
GUILayout.Label(nucleusType, headerStyle);
|
||||||
@ -170,6 +186,9 @@ namespace NanoBrain {
|
|||||||
else {
|
else {
|
||||||
string newName = EditorGUILayout.TextField(this.currentNucleus.name, boldTextFieldStyle);
|
string newName = EditorGUILayout.TextField(this.currentNucleus.name, boldTextFieldStyle);
|
||||||
if (newName != this.currentNucleus.name) {
|
if (newName != this.currentNucleus.name) {
|
||||||
|
Nucleus prefabNucleus = this.prefab.GetNucleus(this.currentNucleus.name);
|
||||||
|
prefabNucleus.name = newName;
|
||||||
|
// This changes it in the temporary cluster instance
|
||||||
this.currentNucleus.name = newName;
|
this.currentNucleus.name = newName;
|
||||||
this.prefab.RefreshOutputs();
|
this.prefab.RefreshOutputs();
|
||||||
// outputsPopup.choices = this.prefab.outputs.Select(output => output.name).ToList();
|
// outputsPopup.choices = this.prefab.outputs.Select(output => output.name).ToList();
|
||||||
@ -222,7 +241,7 @@ namespace NanoBrain {
|
|||||||
Nucleus newOutput = new Neuron(this.prefab, "New Output");
|
Nucleus newOutput = new Neuron(this.prefab, "New Output");
|
||||||
// Regenerate the temporary clsuter instance
|
// Regenerate the temporary clsuter instance
|
||||||
// See also the constructor
|
// See also the constructor
|
||||||
this.currentCluster = new (this.prefab);
|
this.currentCluster = new(this.prefab);
|
||||||
this.currentNucleus = newOutput;
|
this.currentNucleus = newOutput;
|
||||||
this.selectedOutput = this.currentNucleus;
|
this.selectedOutput = this.currentNucleus;
|
||||||
}
|
}
|
||||||
@ -290,14 +309,17 @@ namespace NanoBrain {
|
|||||||
EditorGUIUtility.labelWidth = 100;
|
EditorGUIUtility.labelWidth = 100;
|
||||||
|
|
||||||
Vector3 newBias = EditorGUILayout.Vector3Field("Bias", this.currentNucleus.bias);
|
Vector3 newBias = EditorGUILayout.Vector3Field("Bias", this.currentNucleus.bias);
|
||||||
anythingChanged |= newBias != this.currentNucleus.bias;
|
if (newBias != this.currentPrefabNucleus.bias) {
|
||||||
this.currentNucleus.bias = newBias;
|
anythingChanged |= newBias != this.currentNucleus.bias;
|
||||||
|
this.currentPrefabNucleus.bias = newBias;
|
||||||
|
this.currentNucleus.bias = newBias;
|
||||||
|
}
|
||||||
EditorGUIUtility.labelWidth = previousLabelWidth;
|
EditorGUIUtility.labelWidth = previousLabelWidth;
|
||||||
|
|
||||||
Nucleus[] array = null;
|
Nucleus[] array = null;
|
||||||
int elementIx = -1;
|
int elementIx = -1;
|
||||||
if (this.currentNucleus.synapses.Count > 0) {
|
if (this.currentPrefabNucleus.synapses.Count > 0) {
|
||||||
Synapse[] synapses = this.currentNucleus.synapses.ToArray();
|
Synapse[] synapses = this.currentPrefabNucleus.synapses.ToArray();
|
||||||
foreach (Synapse synapse in synapses) {
|
foreach (Synapse synapse in synapses) {
|
||||||
if (synapse.neuron == null)
|
if (synapse.neuron == null)
|
||||||
continue;
|
continue;
|
||||||
@ -336,24 +358,20 @@ namespace NanoBrain {
|
|||||||
EditorGUILayout.BeginHorizontal();
|
EditorGUILayout.BeginHorizontal();
|
||||||
|
|
||||||
if (synapse.neuron.clusterPrefab != this.currentNucleus.clusterPrefab) {
|
if (synapse.neuron.clusterPrefab != this.currentNucleus.clusterPrefab) {
|
||||||
// If it is a cluster
|
// If it is a different cluster
|
||||||
GUIStyle labelStyle = new(GUI.skin.label);
|
GUIStyle labelStyle = new(GUI.skin.label);
|
||||||
float labelWidth = 200;
|
float labelWidth = 200;
|
||||||
if (synapse.neuron.clusterPrefab != null) {
|
if (synapse.neuron.clusterPrefab != null) {
|
||||||
labelWidth = labelStyle.CalcSize(new GUIContent($"{synapse.neuron.clusterPrefab.name}.")).x;
|
labelWidth = labelStyle.CalcSize(new GUIContent($"{synapse.neuron.clusterPrefab.name}.")).x;
|
||||||
GUILayout.Label($"{synapse.neuron.clusterPrefab.name}", GUILayout.Width(labelWidth));
|
GUILayout.Label($"{synapse.neuron.clusterPrefab.name}", GUILayout.Width(labelWidth));
|
||||||
}
|
}
|
||||||
//string[] options = synapse.neuron.parent.clusterNuclei.Select(n => n.name).ToArray();
|
|
||||||
string[] options = synapse.neuron.clusterPrefab.nuclei.Select(n => n.name).ToArray();
|
string[] options = synapse.neuron.clusterPrefab.nuclei.Select(n => n.name).ToArray();
|
||||||
int selectedIndex = System.Array.IndexOf(options, synapse.neuron.name);
|
int selectedIndex = System.Array.IndexOf(options, synapse.neuron.name);
|
||||||
int newIndex = EditorGUILayout.Popup(selectedIndex, options);
|
int newIndex = EditorGUILayout.Popup(selectedIndex, options);
|
||||||
// if (newIndex != selectedIndex && synapse.neuron.clusterPrefab.nuclei[newIndex] is Neuron newNeuron)
|
|
||||||
// ChangeSynapse(synapse, newNeuron);
|
|
||||||
if (newIndex != selectedIndex) {
|
if (newIndex != selectedIndex) {
|
||||||
// It shall be ensured that the parent.clusterNuclei and
|
// Nucleus selectedNucleus = synapse.neuron.parent.clusterNuclei[newIndex];
|
||||||
// clusterPrefab.nuclei contain the same neurons in the same order....
|
// Neuron newNeuron = selectedNucleus as Neuron;
|
||||||
Nucleus selectedNucleus = synapse.neuron.parent.clusterNuclei[newIndex];
|
Neuron newNeuron = synapse.neuron.clusterPrefab.nuclei[newIndex] as Neuron;
|
||||||
Neuron newNeuron = selectedNucleus as Neuron;
|
|
||||||
ChangeSynapse(synapse, newNeuron);
|
ChangeSynapse(synapse, newNeuron);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -373,14 +391,6 @@ namespace NanoBrain {
|
|||||||
EditorGUI.indentLevel++;
|
EditorGUI.indentLevel++;
|
||||||
float newWeight = EditorGUILayout.FloatField("Weight", synapse.weight);
|
float newWeight = EditorGUILayout.FloatField("Weight", synapse.weight);
|
||||||
if (newWeight != synapse.weight) {
|
if (newWeight != synapse.weight) {
|
||||||
// if (synapse.neuron.parent is IReceptor receptor) {
|
|
||||||
// Nucleus[] receptorArray = receptor.nucleiArray;
|
|
||||||
// foreach (Synapse s in this.currentNucleus.synapses) {
|
|
||||||
// if (s.neuron.parent is IReceptor r && r.nucleiArray == receptorArray)
|
|
||||||
// s.weight = newWeight;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// else
|
|
||||||
synapse.weight = newWeight;
|
synapse.weight = newWeight;
|
||||||
anythingChanged = true;
|
anythingChanged = true;
|
||||||
}
|
}
|
||||||
@ -537,7 +547,7 @@ namespace NanoBrain {
|
|||||||
// this.prefab.nuclei.Remove(nucleus);
|
// this.prefab.nuclei.Remove(nucleus);
|
||||||
// Neuron.Delete(nucleus);
|
// Neuron.Delete(nucleus);
|
||||||
this.prefab.RefreshOutputs();
|
this.prefab.RefreshOutputs();
|
||||||
|
|
||||||
|
|
||||||
this.currentNucleus = this.prefab.output;
|
this.currentNucleus = this.prefab.output;
|
||||||
this.selectedOutput = this.currentNucleus;
|
this.selectedOutput = this.currentNucleus;
|
||||||
@ -565,7 +575,7 @@ namespace NanoBrain {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void ChangeSynapse(Synapse synapse, Neuron newNucleus) {
|
protected virtual void ChangeSynapse(Synapse synapse, Neuron newNucleus) {
|
||||||
Neuron synapseNeuron = synapse.neuron as Neuron;
|
Neuron synapseNeuron = synapse.neuron;
|
||||||
if (synapse.neuron.parent is Cluster subCluster && subCluster.prefab != this.prefab) {
|
if (synapse.neuron.parent is Cluster subCluster && subCluster.prefab != this.prefab) {
|
||||||
// if (synapse.neuron.parent is ClusterReceptor receptor) {
|
// if (synapse.neuron.parent is ClusterReceptor receptor) {
|
||||||
// // the new nucleus is part of a (cluster) receptor,
|
// // the new nucleus is part of a (cluster) receptor,
|
||||||
@ -595,8 +605,8 @@ namespace NanoBrain {
|
|||||||
// }
|
// }
|
||||||
// else {
|
// else {
|
||||||
// it is a neuron in a subcluster
|
// it is a neuron in a subcluster
|
||||||
synapseNeuron.RemoveReceiver(this.currentNucleus);
|
synapseNeuron.RemoveReceiver(this.currentPrefabNucleus);
|
||||||
newNucleus.AddReceiver(this.currentNucleus);
|
newNucleus.AddReceiver(this.currentPrefabNucleus);
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|||||||
@ -15,7 +15,12 @@ namespace NanoBrain {
|
|||||||
//protected readonly ClusterPrefab prefab;
|
//protected readonly ClusterPrefab prefab;
|
||||||
protected Cluster currentCluster;
|
protected Cluster currentCluster;
|
||||||
protected SerializedObject serializedBrain;
|
protected SerializedObject serializedBrain;
|
||||||
protected Nucleus currentNucleus;
|
protected Nucleus _currentNucleus;
|
||||||
|
protected virtual Nucleus currentNucleus {
|
||||||
|
get => _currentNucleus;
|
||||||
|
set => _currentNucleus = value;
|
||||||
|
}
|
||||||
|
//protected Nucleus currentNucleus;
|
||||||
protected Nucleus selectedOutput;
|
protected Nucleus selectedOutput;
|
||||||
|
|
||||||
protected GameObject gameObject;
|
protected GameObject gameObject;
|
||||||
|
|||||||
@ -93,6 +93,8 @@ namespace NanoBrain {
|
|||||||
nucleus.ShallowCloneTo(this);
|
nucleus.ShallowCloneTo(this);
|
||||||
}
|
}
|
||||||
Nucleus[] clonedNuclei = this.clusterNuclei.ToArray();
|
Nucleus[] clonedNuclei = this.clusterNuclei.ToArray();
|
||||||
|
// foreach (Nucleus n in clonedNuclei)
|
||||||
|
// n.name += "(c)";
|
||||||
|
|
||||||
// Now clone the connections
|
// Now clone the connections
|
||||||
for (int nucleusIx = 0; nucleusIx < prefabNuclei.Length; nucleusIx++) {
|
for (int nucleusIx = 0; nucleusIx < prefabNuclei.Length; nucleusIx++) {
|
||||||
@ -106,19 +108,15 @@ namespace NanoBrain {
|
|||||||
|
|
||||||
foreach (Synapse prefabSynapse in prefabNeuron.synapses) {
|
foreach (Synapse prefabSynapse in prefabNeuron.synapses) {
|
||||||
Neuron synapseNeuron = prefabSynapse.neuron;
|
Neuron synapseNeuron = prefabSynapse.neuron;
|
||||||
if (synapseNeuron.parent is not null && synapseNeuron.clusterPrefab != this.clusterPrefab) {
|
if (synapseNeuron.clusterPrefab != null && synapseNeuron.clusterPrefab != this.prefab) {
|
||||||
// Neuron is in another cluster, find the cloned cluster first
|
// Neuron is in another cluster, find the cloned cluster first
|
||||||
Cluster prefabCluster = synapseNeuron.parent;
|
ClusterPrefab prefabCluster = synapseNeuron.clusterPrefab;
|
||||||
int clusterIx = GetNucleusIndex(prefabNuclei, prefabCluster);
|
Cluster clonedCluster = this.clusterNuclei.Find(n => n.name == prefabCluster.name) as Cluster;
|
||||||
if (clusterIx < 0)
|
if (clonedCluster == null)
|
||||||
// Could not find the cluster in the prefab
|
|
||||||
continue;
|
|
||||||
if (clonedNuclei[clusterIx] is not Cluster clonedCluster)
|
|
||||||
// Could not find the cloned cluster
|
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Now find the neuron in that cloned cluster
|
// Now find the neuron in that cloned cluster
|
||||||
int neuronIx = GetNucleusIndex(prefabCluster.prefab.nuclei, prefabSynapse.neuron);
|
int neuronIx = GetNucleusIndex(prefabCluster.nuclei, prefabSynapse.neuron.name);
|
||||||
if (neuronIx < 0)
|
if (neuronIx < 0)
|
||||||
// Could not find the neuron in the prefab cluster
|
// Could not find the neuron in the prefab cluster
|
||||||
continue;
|
continue;
|
||||||
@ -402,13 +400,25 @@ namespace NanoBrain {
|
|||||||
int i = 0;
|
int i = 0;
|
||||||
foreach (Nucleus nucleiElement in nuclei) {
|
foreach (Nucleus nucleiElement in nuclei) {
|
||||||
//for (int i = 0; i < nuclei.Length; i++) {
|
//for (int i = 0; i < nuclei.Length; i++) {
|
||||||
if (nucleus == nucleiElement)
|
if (nucleiElement == nucleus)
|
||||||
return i;
|
return i;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static int GetNucleusIndex(List<Nucleus> nuclei, string nucleusName) {
|
||||||
|
int i = 0;
|
||||||
|
foreach (Nucleus nucleiElement in nuclei) {
|
||||||
|
//for (int i = 0; i < nuclei.Length; i++) {
|
||||||
|
if (nucleiElement.name == nucleusName)
|
||||||
|
return i;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#endregion Init
|
#endregion Init
|
||||||
|
|
||||||
#region Cluster Array
|
#region Cluster Array
|
||||||
@ -690,7 +700,7 @@ namespace NanoBrain {
|
|||||||
if (TryGetNucleus(nucleus.name, out nucleus) == false)
|
if (TryGetNucleus(nucleus.name, out nucleus) == false)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Neuron.Delete(nucleus);
|
Neuron.Delete(nucleus);
|
||||||
int nucleusIx = this.clusterNuclei.IndexOf(nucleus);
|
int nucleusIx = this.clusterNuclei.IndexOf(nucleus);
|
||||||
this.clusterNuclei.Remove(nucleus);
|
this.clusterNuclei.Remove(nucleus);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user