Example #1
0
        void updateBrain()
        {
            //input
            for (int i = 0; i < Eyes.Count; i++)
            {
                Inputs[i * 3]     = Eyes[i].Col.R / 255.0f;
                Inputs[i * 3 + 1] = Eyes[i].Col.G / 255.0f;
                Inputs[i * 3 + 2] = Eyes[i].Col.B / 255.0f;
            }
            Inputs[ListenId] = 0;

            /*foreach (var item in NearbyObjects)
             * {
             *  if(item is Creature)
             *  {
             *      Creature C = item as Creature;
             *      float Ratio = (C.Pos - Pos).MagSq() / (EyeRadius * EyeRadius);
             *      if (Ratio<=1)
             *      {
             *          Inputs[ListenId] += (1 - Ratio) * C.TalkValue;
             *      }
             *  }
             * }*/
            Inputs[ListenId] = Clamp01(Inputs[ListenId]);
            Inputs[EnergyId] = Clamp01(Energy / (StartEnergy + EggEnergy));
            Inputs[SpeedId]  = Clamp01(Vel.Mag() / MaxSpeed);
            Inputs[PollenId] = Pollen != null?1:0;
            Brain.SetInputs(Inputs);
            Brain.Update();
            //output
            Outputs   = Brain.GetOutputs();
            TalkValue = Clamp01(Outputs[3]);
        }
 public void Update(NeuralNetwork Network)
 {
     Age++;
     float[] Inputs = Tree.Inputs;
     Inputs[2] = 1;
     Inputs[3] = Layer / 3f;
     Inputs[4] = Branches.Count / 2f;
     Inputs[5] = Leaves.Count / 2f;
     Inputs[6] = Tree.Leaves.Count == 0 ? 0 : LocalEnergy / Tree.Leaves.Count;
     Inputs[7] = Age / (AgeScale * Form1.fps);
     Inputs[8] = Inputs[9] = 0;
     Network.SetInputs(Inputs);
     Network.Update();
     float[] Outputs = Network.GetOutputs();
     if (Outputs[0] > 0)
     {
         if (Tree.Energy > LeafCost)
         {
             Tree.Energy -= LeafCost;
             Leaf L = new Leaf(this, (Network.Clamp(Outputs[1], -1, 1)) * 90, (Outputs[2] + 1) * 180, Outputs[3] * 90);
             Tree.AddLeaf(this, L);
         }
         else
         {
             Tree.Energy -= LeafCost / 20f;
         }
     }
     if (Outputs[4] > 0)
     {
         if (Tree.Energy > BranchCost)
         {
             Tree.Energy -= BranchCost;
             Branch B = new Branch(Tree, this, 3, (Network.Clamp(Outputs[5], -1, 1)) * 90, Outputs[6] * 180);
             Branches.Add(B);
             Tree.Branches.Add(B);
         }
         else
         {
             Tree.Energy -= BranchCost / 20f;
         }
     }
 }
            public void Update(NeuralNetwork Network)
            {
                Age++;
                if (CurrentSeed != null)
                {
                    SeedTimer += Tree.MaxTimer / Form1.fps;
                    if (SeedTimer > SeedTimerMax)
                    {
                        CurrentSeed.Fall();
                        //UpdateWorld.AddEntity(CurrentSeed, CurrentSeed.Pos, CurrentSeed.ChunkPos);
                        CurrentSeed = null;
                    }
                }
                float L1 = (float)Form1.Rand.NextDouble();
                float L2 = (float)Form1.Rand.NextDouble();

                if (L1 + L2 > 1)
                {
                    L1 = 1 - L1;
                    L2 = 1 - L2;
                }
                RayPoint = Base + (Tip1 - Base) * L1 + (Tip2 - Base) * L2;

                float[] Inputs = Tree.Inputs;
                Inputs[2] = 0;
                Inputs[3] = (Parent.Layer + 1) / 3f;
                Inputs[4] = 0;
                Inputs[5] = CurrentSeed == null?0:1;
                Inputs[6] = EnergyFlow;
                Inputs[7] = Age / (AgeScale * Form1.fps);
                Inputs[8] = CurrentFlower != null ? 1 : 0;
                Inputs[9] = CurrentFlower != null ? (CurrentFlower.CurrentEnergy / FruitCost): 0;
                Network.SetInputs(Inputs);
                Network.Update();
                float[] Outputs = Network.GetOutputs();
                if (CurrentFlower != null)
                {
                    CurrentFlower.Update(Network.Clamp(Outputs[11], 0, 1));
                }
                if (Outputs[0] < -1)
                {
                    Tree.Energy += LeafCost / 2;
                    Kill();
                }
                else
                {
                    if (Outputs[9] > 0)
                    {
                        if (CurrentSeed == null && CurrentFlower == null && Tree.Energy > SeedCost)
                        {
                            Tree.Energy -= SeedCost;
                            CurrentSeed  = new Seed(Tree, this);
                            UpdateWorld.AddEntity(CurrentSeed, CurrentSeed.Pos, CurrentSeed.ChunkPos);
                        }
                        else
                        {
                            Tree.Energy -= SeedCost / 20;
                        }
                    }
                    if (Outputs[10] > 0)
                    {
                        if (CurrentSeed == null && CurrentFlower == null && Tree.Energy > FlowerCost)
                        {
                            Tree.Energy  -= FlowerCost;
                            CurrentFlower = new Flower(this);
                            UpdateWorld.AddEntity(CurrentFlower, Pos, ChunkPos);
                        }
                        else
                        {
                            Tree.Energy -= FlowerCost / 20;
                        }
                    }
                }
            }