/// <summary> /// Checks if all the inputs and outputs for a brain are valid and returns invalid ones /// </summary> /// <param name="brain"></param> /// <param name="entity"></param> /// <param name="invalidInputs"></param> /// <param name="invalidControls"></param> /// <returns></returns> public static bool ValidateBrain(BrainComponent brain, Entity entity, out List <string> invalidInputs, out List <string> invalidControls) { invalidInputs = new List <string>(); invalidControls = new List <string>(); var validInputs = GetAllInputs(entity); var validOutputs = GetAllControls(entity); foreach (string input in brain.InputMap.Values) { if (!validInputs.Contains(input)) { invalidInputs.Add(input); } } foreach (string output in brain.OutputMap.Values) { if (!validOutputs.Contains(output)) { invalidControls.Add(output); } } return(invalidControls.Count == 0 && invalidInputs.Count == 0); }
private BrainComponent.Macro GetMacroFromRequest(WebJSON.Request msg) { if (selector.selectedPerso == null || selector.selectedPerso.brain == null) { return(null); } BrainComponent brain = selector.selectedPerso.brain; Pointer offset = msg.Offset; switch (msg.BehaviorType) { case WebJSON.BehaviorType.Macro: if (brain.Macros == null) { return(null); } foreach (var m in brain.Macros) { if (m.Offset == offset) { return(m); } } break; } return(null); }
public override void CreateBrain(BrainComponent brainComp, IMutatorConfig config) { var neatConfig = (NeatMutationConfig)config; // Add nodes for inputs and outputs foreach ((string namedInput, string _) in brainComp.InputMap) { // Note activation function on input nodes is not used. this.AddNamedNode(namedInput, NodeType.INPUT, ActivationFunctionType.SOFTSIGN); } foreach ((string namedOutput, string _) in brainComp.OutputMap) { this.AddNamedNode(namedOutput, NodeType.OUTPUT, (ActivationFunctionType)neatConfig.OutputActivationFunction); } // Increase connection innovation id as we loop int connInnovationId = 0; Random r = new Random(); double chanceToMakeConnection = 1d; foreach (var input in NodeGenes.FindAll((g) => g.NodeType == NodeType.INPUT || g.NodeType == NodeType.BIAS)) { foreach (var output in NodeGenes.FindAll((g) => g.NodeType == NodeType.OUTPUT)) { if (r.NextDouble() < chanceToMakeConnection) { // Add new connection gene ConnectionGenes.Add(new ConnectionGene(connInnovationId, input.InnovationId, output.InnovationId, r.Normal(0, 1.5))); connInnovationId++; } } } }
private void OnRemovedFromBody(EntityUid uid, BrainComponent component, RemovedFromBodyEvent args) { // This one needs to be special, okay? if (!EntityManager.TryGetComponent(uid, out MechanismComponent mech)) { return; } HandleMind((mech.Part !).Owner, (args.Old).Owner); }
public override void CreateBrain(BrainComponent brainComponent, IMutatorConfig config) { Inputs = new(); Outputs = new(); foreach (string input in brainComponent.InputMap.Values) { Inputs.Add(input); } foreach (string output in brainComponent.OutputMap.Values) { Outputs.Add(output); } }
public BrainEditor(BrainComponent component) { BuildUI(); foreach (string input in component.InputMap.Values) { var item = new ListItem(input); _inputList.Items.Add(item); } foreach (string output in component.OutputMap.Values) { var item = new ListItem(output); _outputList.Items.Add(item); } }
private BrainComponent.Comport GetComportFromRequest(WebJSON.Request msg) { if (selector.selectedPerso == null || selector.selectedPerso.brain == null) { return(null); } BrainComponent brain = selector.selectedPerso.brain; Pointer offset = msg.Offset; switch (msg.BehaviorType) { case WebJSON.BehaviorType.Intelligence: if (brain.Intelligence == null) { return(null); } foreach (var be in brain.Intelligence) { if (be.Offset == offset) { return(be); } } break; case WebJSON.BehaviorType.Reflex: if (brain.Reflex == null) { return(null); } foreach (var be in brain.Reflex) { if (be.Offset == offset) { return(be); } } break; } return(null); }
public abstract void CreateBrain(BrainComponent brainComponent, IMutatorConfig config);
private BaseScriptComponent GetScriptFromRequest(WebJSON.Request msg) { if (selector.selectedPerso == null || selector.selectedPerso.brain == null) { return(null); } BrainComponent brain = selector.selectedPerso.brain; Pointer offset = msg.Offset; switch (msg.BehaviorType) { case WebJSON.BehaviorType.Intelligence: if (brain.Intelligence == null) { return(null); } foreach (var be in brain.Intelligence) { if (be.FirstScript != null && be.FirstScript.Offset == offset) { return(be.FirstScript); } foreach (var s in be.Scripts) { if (s.Offset == offset) { return(s); } } } break; case WebJSON.BehaviorType.Reflex: if (brain.Reflex == null) { return(null); } foreach (var be in brain.Reflex) { if (be.FirstScript != null && be.FirstScript.Offset == offset) { return(be.FirstScript); } foreach (var s in be.Scripts) { if (s.Offset == offset) { return(s); } } } break; case WebJSON.BehaviorType.Macro: if (brain.Macros == null) { return(null); } foreach (var m in brain.Macros) { if (m.Script != null && m.Script.Offset == offset) { return(m.Script); } } break; } return(null); }
public void CreateGameObjects(OpenSpace.AI.Behavior.BehaviorType type, BrainComponent brain, Perso perso) { if (this.comports.Value == null) { return; } string ruleOrReflex = type.ToString(); GameObject intelParent = new GameObject(ruleOrReflex + " behaviours"); intelParent.transform.parent = brain.gameObject.transform; Reference <Comport>[] behaviors = this.comports.Value.comports; int iter = 0; foreach (var behaviorRef in behaviors) { Comport behavior = behaviorRef.Value; if (behavior == null) { continue; } BrainComponent.Comport c = new BrainComponent.Comport(); GameObject behaviorGao = new GameObject(ruleOrReflex + " #" + iter); behaviorGao.transform.parent = intelParent.transform; if (behavior.scripts?.Value?.scripts != null) { foreach (Reference <OpenSpace.ROM.Script> scriptRef in behavior.scripts?.Value?.scripts) { OpenSpace.ROM.Script script = scriptRef.Value; GameObject scriptGao = new GameObject("Script"); scriptGao.transform.parent = behaviorGao.transform; ROMScriptComponent scriptComponent = scriptGao.AddComponent <ROMScriptComponent>(); scriptComponent.SetScript(script, perso); c.Scripts.Add(scriptComponent); } } if (behavior.firstScript.Value != null) { ROMScriptComponent scriptComponent = behaviorGao.AddComponent <ROMScriptComponent>(); scriptComponent.SetScript(behavior.firstScript.Value, perso); c.FirstScript = scriptComponent; } if (iter == 0) { behaviorGao.name += " (Init)"; } if ((behavior.scripts?.Value == null || behavior.scripts?.Value.length == 0) && behavior.firstScript?.Value == null) { behaviorGao.name += " (Empty)"; } c.Offset = behavior.Offset; c.GaoName = behaviorGao.name; c.Name = behavior.IndexString; switch (type) { case AI.Behavior.BehaviorType.Intelligence: brain.Intelligence.Add(c); break; case AI.Behavior.BehaviorType.Reflex: brain.Reflex.Add(c); break; } iter++; } }
public static Entity GetCritter(Color color) { float radius = 10f; var critter = new Entity(); var pos = new Vector2(0, 0); var transform = new TransformComponent(critter) { LocalPosition = pos, LocalRotation = new Rotation(MathHelper.PiOver4), LocalDepth = 0.1f, Scale = new Vector2(1, 1) }; var graphics = new GraphicsComponent(critter) { TexturePath = TextureAtlas.CirclePath, Dimensions = new Point((int)(radius * 2), (int)(radius * 2)), Color = color }; var velocity = new VelocityComponent(critter) { RotationalVelocity = 0, Velocity = Vector2.Zero }; var circleCollider = new CircleColliderComponent(critter) { Radius = radius }; var rigidbody = new RigidBodyComponent(critter) { Mass = 0.05f, Restitution = 0.1f }; var drag = new DragComponent(critter) { MovementDragCoefficient = 0.1f, RotationDragCoefficient = 10f }; var movementControl = new MovementControlComponent(critter) { MaxMovementForceNewtons = 10.0f, MaxRotationForceNewtons = 10.0f, MovementMode = MovementMode.TwoWheels, }; var colour = new VisibleColourComponent(critter) { }; var energy = new EnergyComponent(critter) { Energy = 4f }; var reproduction = new ReproductionComponent(critter) { Efficency = 0.7f, ReproductionEnergyCost = 8f, Reproduce = 0, ReproductionThreshold = 0.5f, RequiredRemainingEnergy = 1f, ChildDefinitionId = "Soupling" }; var health = new HealthComponent(critter) { Health = 100, MaxHealth = 100, }; var age = new OldAgeComponent(critter) { MaxAge = 5 * 60 }; var brain = new BrainComponent(critter) { InputMap = new Dictionary <string, string> { { "eye1R", "eye1.EyeComponent.ActivationR" }, { "eye1G", "eye1.EyeComponent.ActivationG" }, { "eye1B", "eye1.EyeComponent.ActivationB" }, { "eye2R", "eye2.EyeComponent.ActivationR" }, { "eye2G", "eye2.EyeComponent.ActivationG" }, { "eye2B", "eye2.EyeComponent.ActivationB" }, { "eye3R", "eye3.EyeComponent.ActivationR" }, { "eye3G", "eye3.EyeComponent.ActivationG" }, { "eye3B", "eye3.EyeComponent.ActivationB" }, { "eye4R", "eye4.EyeComponent.ActivationR" }, { "eye4G", "eye4.EyeComponent.ActivationG" }, { "eye4B", "eye4.EyeComponent.ActivationB" }, { "mouth", "mouth.MouthComponent.Eating" }, { "nosecos", "nose.NoseComponent.CosActivation" }, { "nosesin", "nose.NoseComponent.SinActivation" }, { "health", "HealthComponent.HealthPercent" }, { "myRed", "VisibleColourComponent.RealR" }, { "myGreen", "VisibleColourComponent.RealG" }, { "myBlue", "VisibleColourComponent.RealB" }, { "Random", "Random" }, { "Bias", "Bias" }, }, //OutputMap = new Dictionary<string, string> // { // {"forwardback", "MovementControlComponent.WishForceForward" }, // {"rotation", "MovementControlComponent.WishRotForce" }, // {"reproduce", "ReproductionComponent.Reproduce" }, // {"red", "VisibleColourComponent.R" }, // {"green", "VisibleColourComponent.G" }, // {"blue", "VisibleColourComponent.B" }, // {"attack", "weapon.WeaponComponent.Activation" } // } OutputMap = new Dictionary <string, string> { { "wheelLeft", "MovementControlComponent.WishWheelLeftForce" }, { "wheelRight", "MovementControlComponent.WishWheelRightForce" }, { "reproduce", "ReproductionComponent.Reproduce" }, { "red", "VisibleColourComponent.R" }, { "green", "VisibleColourComponent.G" }, { "blue", "VisibleColourComponent.B" }, { "attack", "weapon.WeaponComponent.Activation" } } }; critter.AddComponents(transform, graphics, velocity, circleCollider, rigidbody, drag, movementControl, reproduction, colour, energy, health, age, brain); var eye1 = Eye.GetEye(Color.White, MathHelper.ToRadians(30)); eye1.Tag = "eye1"; critter.AddChild(eye1); var eye2 = Eye.GetEye(Color.White, MathHelper.ToRadians(-30)); eye2.Tag = "eye2"; critter.AddChild(eye2); var eye3 = Eye.GetEye(Color.White, MathHelper.ToRadians(90)); eye3.Tag = "eye3"; critter.AddChild(eye3); var eye4 = Eye.GetEye(Color.White, MathHelper.ToRadians(-90)); eye4.Tag = "eye4"; critter.AddChild(eye4); var mouth = Mouth.GetMouth(Color.White); critter.AddChild(mouth); mouth.GetComponent <TransformComponent>().LocalPosition = new Vector2(15, 0); var nose = Nose.GetNose(Color.White, 0, 10); nose.Tag = "nose"; critter.AddChild(nose); var weapon = Weapon.GetWeapon(Color.White); weapon.Tag = "weapon"; critter.AddChild(weapon); weapon.GetComponent <TransformComponent>().LocalPosition = new Vector2(30, 0); return(critter); }