2026-01-06 17:18:31 +01:00

91 lines
2.8 KiB
C#

using UnityEngine;
using LinearAlgebra;
[System.Serializable]
public class Perceptoid : Neuroid {
// A neuroid which has no neurons as input
// But receives value from a receptor
public Receptor receptor;
//public VelocityNeuroid velocityNeuroid;
#region Serialization
[SerializeField]
public int thingType;
public int thingId;
public override void Rebuild(NanoBrainObj brain) {
base.Rebuild(brain);
this.receptor = Receptor.GetReceptor(brain, thingType);
this.receptor.perceptei.Add(this);
}
public override void Deserialize(Nucleus nucleus) {
base.Deserialize(nucleus);
if (nucleus is Perceptoid perceptoid)
this.receptor.thingType = perceptoid.thingType;
// Point all receivers to this perceptoid instead of the default nucleus
foreach (Receiver receiver in nucleus.receivers) {
foreach (Synapse synapse in receiver.nucleus.synapses) {
if (synapse.nucleus == nucleus)
synapse.nucleus = this;
}
}
// Point all synapses to this perceptoid instead of the default nucleus
foreach (Synapse synapse in nucleus.synapses) {
foreach (Receiver receiver in synapse.nucleus.receivers) {
if (receiver.nucleus == nucleus)
receiver.nucleus = this;
}
}
// Copy all the synapses
this.synapses = nucleus.synapses;
// Copy all receivers
this.receivers = nucleus.receivers;
}
#endregion Serialization
public Perceptoid(NanoBrainObj brain, int thingType, string name = "sensor") : base(name) {
this.brain = brain;
if (this.brain != null) {
this.brain.perceptei.Add(this);
}
else
Debug.LogError("No neuroid network");
this.nucleusType = nameof(Perceptoid);
this.name = name;
this.thingType = thingType;
this.receptor = Receptor.GetReceptor(brain, thingType);
this.receptor.perceptei.Add(this);
}
public void Replace(int thingType, string name = "sensor") {
this.name = name;
this.thingType = thingType;
this.receptor.thingType = thingType;
this.receptor.localPosition = Spherical.zero;
this.outputValue = Spherical.zero;
this.receivers = new();
}
public override void UpdateState() {
Spherical result = this.receptor.localPosition;
UpdateResult(result);
}
public static Perceptoid GetPerception(NanoBrainObj brain, int thingType = 0) {
foreach (Nucleus nucleus in brain.nuclei) {
if (nucleus is Perceptoid perceptoid && (thingType == 0 || perceptoid.receptor.thingType == thingType))
return perceptoid;
}
return null;
}
}