public Intension Generate(Perception perception, Habits habits, MentalState mental, PhysicalState ps, float t)
        {
            Intension intension  = default;
            var       sensorData = perception.GetSensorData();
            var       distance   = sensorData.closestDangerObj == null ? -1 : sensorData.closestDangerObj.distance;

            if (distance > 0)
            {
                var mono = sensorData.closestDangerObj.obj as MonoBehaviour;
                intension = new AvoidIntension(mono.gameObject);

                if (this.intensionStack.Count == 0 || this.intensionStack.Last.Value.IntensionType != Intension.Type.Avoid)
                {
                    this.intensionStack.AddLast(intension);
                }
            }
            else
            {
                //fear of most dangerous predator m
                var p  = sensorData.GetClosestByType(ObjectType.Predator);
                var Fm = p != null?ps.Fi(p.distance) : 0;

                var F = mental.fear;
                if (F > this.f0)
                {
                    if (Fm < this.f1 && habits.schooling)
                    {
                        intension = new SchoolIntension();
                    }
                    else
                    {
                        var mono = p.obj as MonoBehaviour;
                        intension = new EscapedIntension(mono.gameObject);
                    }
                }
                else
                {
                    if (this.IsLastEatOrMate())
                    {
                        intension = this.intensionStack.Last.Value;
                        this.intensionStack.RemoveLast();
                    }
                    else
                    {
                        intension = this.GenerateNewIntension(perception, habits, mental, ps, t);
                    }
                }
            }

            if (this.intensionStack.Count > 1)
            {
                this.intensionStack.RemoveFirst();
            }


            LogTool.AssertIsTrue(intension != null);

            return(intension);
        }
        public void Init(Intension intension, Perception perception, FishBody body)
        {
            this.motorControllers.Clear();

            var focusser = perception.GetFocuser();

            var normal = body.modelData.Normal;
            var left   = body.modelData.Left;
            var dir    = focusser.target.obj.obj.Position - body.modelData.GeometryCenter;

            var motorType = focusser.motorPreference.MaxValue.type;

            if (motorType == Focusser.MotorPreference.Type.MoveForward)
            {
                if (this.smc == null)
                {
                    this.smc    = new SwimMC();
                    this.curves = this.smc.ActivationData.ToAnimationCurves();
                }
                if (focusser.target.obj != null)
                {
                    this.smc.UpdateSpeed(focusser.target.obj.distance);
                }
                this.motorControllers.Add(this.smc);
            }
            else if (motorType == Focusser.MotorPreference.Type.TurnLeft)
            {
                if (this.tlmc == null)
                {
                    this.tlmc   = new TurnLeftMC();
                    this.curves = this.tlmc.ActivationData.ToAnimationCurves();
                }
                var angle = this.GetAngleInFish(dir, normal, left);
                this.tlmc.UpdateAngle(angle);
                this.motorControllers.Add(this.tlmc);

                this.smc?.UpdateSpeed(0);
            }
            else if (motorType == Focusser.MotorPreference.Type.TurnRight)
            {
                if (this.trmc == null)
                {
                    this.trmc   = new TurnRightMC();
                    this.curves = this.trmc.ActivationData.ToAnimationCurves();
                }
                var angle = this.GetAngleInFish(dir, normal, left);
                this.trmc.UpdateAngle(angle);
                this.motorControllers.Add(this.trmc);

                this.smc?.UpdateSpeed(0);
            }


            var balance = new BalanceMC();

            balance.UpdateBalance(left, normal);
            this.motorControllers.Add(balance);
        }
        protected BehaviorRoutine GenerateBehaviorRoutine(Intension intension, Perception perception)
        {
            //MC logical
            var ret = new BehaviorRoutine();

            ret.Init(intension, perception, this.body);

            return(ret);
        }
예제 #4
0
        public void UpdateBrain(FishSimulator.Delta delta, Perception perception)
        {
            var t = delta.deltaTime;

            this.physicalState.UpdateTime(t);
            this.mentalState.UpdateState(t, this.physicalState, perception.GetSensorData());

            this.currentIntension = this.intensionGenerator.Generate(perception, this.habits, this.mentalState, this.physicalState, t);
            this.currentDesire.UpdateDesire(this.mentalState, this.currentIntension);
        }
        // public FishBrain Brain => this.brain;

        // public bool IsDone { get; set; }

        public void Init()
        {
            this.body = this.GetComponentInChildren <FishBody>();
            this.body.Init();

            this.brain = this.GetComponentInChildren <FishBrain>();
            this.brain.Init();

            this.perception = this.GetComponentInChildren <Perception>();
            this.perception.Init();

            this.logger     = this.logger ?? new FishLogger();
            this.localDelta = this.localDelta ?? new FishSimulator.Delta();

            this.Reset();
            FishSimulatorRunner.Instance.Controller.AddController(this);
        }
        protected Intension GenerateNewIntension(Perception perception, Habits habits, MentalState mental, PhysicalState ps, float t)
        {
            Intension intension = default;
            var       H         = mental.hunger;
            var       L         = mental.libido;

            if (this.r < math.max(H, L))
            {
                if (H == L)
                {
                    if (ThreadSafeRandom.NextFloat() > 0.5f)
                    {
                        intension = new EatIntension();
                    }
                    else
                    {
                        intension = new MateIntension();
                    }
                }
                else
                {
                    if (H > L)
                    {
                        intension = new EatIntension();
                    }
                    else
                    {
                        intension = new MateIntension();
                    }
                }
            }
            else
            {
                //perception.temperatureSensor;
                //perception.visionSensor;

                //generate wonder or leave
                intension = new WanderIntension();
            }

            return(intension);
        }