Ejemplo n.º 1
0
        public override void Initialize(EvoGame inGame)
        {
            base.Initialize(inGame);
            spriteBatch = new SpriteBatch(game.GraphicsDevice);

            GenerateCollisionGrid();
        }
Ejemplo n.º 2
0
 public static TileMap DeserializeFromFile(string fileName, EvoGame game)
 {
     try
     {
         FileStream   file     = File.Open(fileName, FileMode.Open);
         BinaryReader reader   = new BinaryReader(file);
         int          width    = reader.ReadInt32();
         int          height   = reader.ReadInt32();
         float        tileSize = reader.ReadSingle();
         TileMap      result   = new TileMap(width, height, tileSize);
         result.Initialize(game);
         for (int x = 0; x < width; x++)
         {
             for (int y = 0; y < height; y++)
             {
                 TileType type = (TileType)reader.ReadInt32();
                 result.SetTileType(x, y, type);
                 if (type == TileType.Land)
                 {
                     float foodValue = reader.ReadSingle();
                     result.SetFoodValue(x, y, foodValue);
                 }
             }
         }
         file.Close();
         return(result);
     }
     catch (System.IO.FileNotFoundException)
     {
         return(null);
     }
 }
Ejemplo n.º 3
0
 public void RandomizeWeights()
 {
     foreach (Connection c in connections)
     {
         c.weight = (float)EvoGame.RandomFloat() * 2 - 1;
     }
 }
Ejemplo n.º 4
0
        public Creature(Vector2 pos, float viewAngle)
        {
            id = currentId++;

            this.pos       = pos;
            this.viewAngle = viewAngle;
            inBias.SetName(NAME_IN_BIAS);
            inFoodValuePosition.SetName(NAME_IN_FOODVALUEPOSITION);
            inFoodValueFeeler.SetName(NAME_IN_FOODVALUEFEELER);
            inOcclusionFeeler.SetName(NAME_IN_OCCLUSIONFEELER);
            inEnergy.SetName(NAME_IN_ENERGY);
            inAge.SetName(NAME_IN_AGE);
            inGeneticDifference.SetName(NAME_IN_GENETICDIFFERENCE);
            inWasAttacked.SetName(NAME_IN_WASATTACKED);
            inWaterOnFeeler.SetName(NAME_IN_WATERONFEELER);
            inWaterOnCreature.SetName(NAME_IN_WATERONCREATURE);
            inMemory1.SetName(NAME_IN_MEMORY1);

            outBirth.SetName(NAME_OUT_BIRTH);
            outRotate.SetName(NAME_OUT_ROTATE);
            outForward.SetName(NAME_OUT_FORWARD);
            outFeelerAngle.SetName(NAME_OUT_FEELERANGLE);
            outAttack.SetName(NAME_OUT_ATTACK);
            outEat.SetName(NAME_OUT_EAT);
            outMemory1.SetName(NAME_OUT_MEMORY1);

            brain = new NeuronalNetwork();

            brain.AddInputNeuron(inBias);
            brain.AddInputNeuron(inFoodValuePosition);
            brain.AddInputNeuron(inFoodValueFeeler);
            brain.AddInputNeuron(inOcclusionFeeler);
            brain.AddInputNeuron(inEnergy);
            brain.AddInputNeuron(inAge);
            brain.AddInputNeuron(inGeneticDifference);
            brain.AddInputNeuron(inWasAttacked);
            brain.AddInputNeuron(inWaterOnFeeler);
            brain.AddInputNeuron(inWaterOnCreature);
            brain.AddInputNeuron(inMemory1);

            brain.GenerateHiddenNeurons(10);

            brain.AddOutputNeuron(outBirth);
            brain.AddOutputNeuron(outRotate);
            brain.AddOutputNeuron(outForward);
            brain.AddOutputNeuron(outFeelerAngle);
            brain.AddOutputNeuron(outAttack);
            brain.AddOutputNeuron(outEat);
            brain.AddOutputNeuron(outMemory1);

            brain.GenerateFullMesh();

            brain.RandomizeAllWeights();
            CalculateFeelerPos(MAXIMUMFEELERDISTANCE);

            color = new Color(EvoGame.RandomFloat(), EvoGame.RandomFloat(), EvoGame.RandomFloat());
            GenerateColorInv();
            CalculateCollisionGridPos();
        }
Ejemplo n.º 5
0
        public void Initialize(EvoGame game)
        {
            gameConfiguration = game.gameConfiguration;
            Initialize(game.sim);
            camera = game.simRenderer.Camera;

            this.game = game;
        }
Ejemplo n.º 6
0
 public override void Initialize(EvoGame ingame)
 {
     base.Initialize(ingame);
     game = ingame;
     gameConfiguration = ingame.gameConfiguration;
     camera            = Camera.instanceGameWorld;
     scrollWheelValue  = Mouse.GetState().ScrollWheelValue;
 }
Ejemplo n.º 7
0
        public void RandomMutation(float MutationRate)
        {
            int index = EvoGame.RandomInt(hiddenNeurons.Count + outputNeurons.Count);

            if (index < hiddenNeurons.Count)
            {
                ((WorkingNeuron)hiddenNeurons[index]).RandomMutation(MutationRate);
            }
            else
            {
                ((WorkingNeuron)outputNeurons[index - hiddenNeurons.Count]).RandomMutation(MutationRate);
            }
        }
Ejemplo n.º 8
0
        public void calculate()
        {
            int currentFrequencyX = startFrequencyX;
            int currentFrequencyY = startFrequencyY;

            float currentAlpha = 1;

            for (int oc = 0; oc < octaves; oc++)
            {
                if (oc > 0)
                {
                    currentFrequencyX *= 2;
                    currentFrequencyY *= 2;
                    currentAlpha      /= 2;
                }

                float[,] discretePoints = new float[currentFrequencyX + 1, currentFrequencyY + 1];
                for (int i = 0; i < currentFrequencyX + 1; i++)
                {
                    for (int k = 0; k < currentFrequencyY + 1; k++)
                    {
                        discretePoints[i, k] = (float)EvoGame.RandomFloat() * currentAlpha;
                    }
                }

                for (int i = 0; i < WIDTH; i++)
                {
                    for (int k = 0; k < HEIGHT; k++)
                    {
                        float currentX = i / (float)WIDTH * currentFrequencyX;
                        float currentY = k / (float)HEIGHT * currentFrequencyY;

                        int indexX = (int)currentX;
                        int indexY = (int)currentY;

                        float w0 = interpolate(discretePoints[indexX, indexY], discretePoints[indexX + 1, indexY], currentX - indexX);
                        float w1 = interpolate(discretePoints[indexX, indexY + 1], discretePoints[indexX + 1, indexY + 1], currentX - indexX);
                        float w  = interpolate(w0, w1, currentY - indexY);

                        heightMap[i, k] += w;
                    }
                }
            }

            normalize();
        }
Ejemplo n.º 9
0
        public void Initialize(EvoGame game)
        {
            spriteBatch = new SpriteBatch(game.GraphicsDevice);

            SandTexture   = game.Content.Load <Texture2D>("Map/SandTexture");
            GrassTexture  = game.Content.Load <Texture2D>("Map/GrassTexture");
            BlendMap      = game.Content.Load <Texture2D>("Map/BlendMap");
            Water1Texture = game.Content.Load <Texture2D>("Map/Water1");
            Water2Texture = game.Content.Load <Texture2D>("Map/Water2");
            BlendMap      = game.Content.Load <Texture2D>("Map/BlendMap");
            LandShader    = game.Content.Load <Effect>("Map/GrassDisplay");
            WaterShader   = game.Content.Load <Effect>("Map/WaterEffect");

            LandShader.Parameters["GrassTexture"].SetValue(GrassTexture);
            LandShader.Parameters["SandTexture"].SetValue(SandTexture);
            LandShader.Parameters["BlendMap"].SetValue(BlendMap);
            WaterShader.Parameters["Water2"].SetValue(Water2Texture);
        }
Ejemplo n.º 10
0
        public Creature(Creature mother)
        {
            id = currentId++;
            //this.mother = mother;
            generation = mother.generation + 1;
            if (generation > _maximumGeneration)
            {
                _maximumGeneration = generation;
            }
            this.pos       = mother.pos;
            this.viewAngle = EvoGame.RandomFloat() * Mathf.PI * 2;
            this.brain     = mother.brain.CloneFullMesh();

            SetupVariablesFromBrain();


            CalculateFeelerPos(MAXIMUMFEELERDISTANCE);
            for (int i = 0; i < 10; i++)
            {
                brain.RandomMutation(0.1f);
            }

            int r = mother.color.R;
            int g = mother.color.G;
            int b = mother.color.B;

            r += EvoGame.RandomInt(-5, 6);
            g += EvoGame.RandomInt(-5, 6);
            b += EvoGame.RandomInt(-5, 6);

            r = Mathf.ClampColorValue(r);
            g = Mathf.ClampColorValue(g);
            b = Mathf.ClampColorValue(b);

            color = new Color(r, g, b);
            GenerateColorInv();

            if (CreatureManager.SelectedCreature == null || CreatureManager.SelectedCreature.Energy < 100)
            {
                CreatureManager.SelectedCreature = this;
            }
        }
Ejemplo n.º 11
0
        protected override void Update(GameTime deltaTime)
        {
            while (Creatures.Count < 50)
            {
                Creature justSpawned = new Creature(
                    new Vector2(
                        EvoGame.RandomFloat() * game.tileMap.GetWorldWidth(),
                        EvoGame.RandomFloat() * game.tileMap.GetWorldHeight()),
                    EvoGame.RandomFloat() * Mathf.PI * 2);
                justSpawned.Manager = this;
                Creatures.Add(justSpawned);
            }

            for (int i = 0; i < AmountOfCores; i++)
            {
                int upperBound = Creatures.Count * (i + 1) / AmountOfCores;
                if (upperBound > Creatures.Count)
                {
                    upperBound = Creatures.Count;
                }
                int lowerBound = Creatures.Count * i / AmountOfCores;
                MultithreadingHelper.StartWork((object state) => {
                    for (int k = lowerBound; k < upperBound; k++)
                    {
                        Creatures[k].ReadSensors();
                    }
                    MultithreadingHelper.PulseAndFinish();
                });
            }
            MultithreadingHelper.WaitForEmptyThreadPool();
            for (int i = 0; i < AmountOfCores; i++)
            {
                int upperBound = Creatures.Count * (i + 1) / AmountOfCores;
                if (upperBound > Creatures.Count)
                {
                    upperBound = Creatures.Count;
                }
                int lowerBound = Creatures.Count * i / AmountOfCores;
                MultithreadingHelper.StartWork((object state) => {
                    for (int k = lowerBound; k < upperBound; k++)
                    {
                        Creatures[k].Act(deltaTime);
                    }
                    MultithreadingHelper.PulseAndFinish();
                });
            }
            MultithreadingHelper.WaitForEmptyThreadPool();
            numberOfDeaths += CreaturesToKill.Count;

            RemoveCreaturesFromDeathList();
            MergeCreaturesAndSpawnCreatures();

            year += (float)deltaTime.ElapsedGameTime.TotalSeconds;

            HandleCollision();

            if (Creatures.Count > 0)
            {
                OldestCreatureAlive = Creatures[0];
                foreach (Creature c in Creatures)
                {
                    if (c.Age > OldestCreatureAlive.Age)
                    {
                        OldestCreatureAlive = c;
                    }
                }
            }

            AliveCreaturesRecord.Add(Creatures.Count);
        }
Ejemplo n.º 12
0
        public void RandomMutation(float MutationRate)
        {
            Connection c = connections[EvoGame.RandomInt(connections.Count)];

            c.weight += (float)EvoGame.RandomFloat() * 2 * MutationRate - MutationRate;
        }