Exemplo n.º 1
0
        public void simulate()
        {
            #region 1.) Make plans
            //Herbivores
            foreach (Herbivore h in herbivores)
            {
                h.planAction();
            }

            //Carnivores
            foreach (Carnivore c in carnivores)
            {
                c.planAction();
            }
            #endregion

            #region 2.) Perform actions (from plans)
            //Herbivores
            foreach (Herbivore h in herbivores)
            {
                h.performAction();
            }

            //Carnivores
            foreach (Carnivore c in carnivores)
            {
                c.performAction();
            }
            #endregion

            #region 3.) Remove the dead
            int counter = 0;
            //Plants
            counter = 0;
            foreach (Plant a in plants.FindAll(a => a.energy <= 0))
            {
                //Remove form map
                map[a.location.X, a.location.Y, a.layer] = null;

                //Remove from list
                plants.Remove(a);
                counter++;
            }
            stats.plantDeathsPerCycle.Add(counter);

            //Herbivores
            counter = 0;
            foreach (Herbivore a in herbivores.FindAll(a => a.energy <= 0))
            {
                //Remove form map
                map[a.location.X, a.location.Y, a.layer] = null;

                //Remove from list
                herbivores.Remove(a);
                counter++;
            }
            stats.herbivoreDeathsPerCycle.Add(counter);

            //Carnivores
            counter = 0;
            foreach (Carnivore a in carnivores.FindAll(a => a.energy <= 0))
            {
                //Remove form map
                map[a.location.X, a.location.Y, a.layer] = null;

                //Remove from list
                carnivores.Remove(a);
                counter++;
            }
            stats.carnivoreDeathsPerCycle.Add(counter);

            #endregion

            #region 4.) Increase age
            foreach (Agent a in plants)
            {
                a.age++;
            }

            foreach (Agent a in herbivores)
            {
                a.age++;
            }

            foreach (Agent a in carnivores)
            {
                a.age++;
            }


            if (herbivores.Count > 0)
            {
                stats.herbivoreOldestPerCycle.Add(herbivores.Max(a => a.age));
            }
            else
            {
                stats.herbivoreOldestPerCycle.Add(0);
            }

            if (carnivores.Count > 0)
            {
                stats.carnivoreOldestPerCycle.Add(carnivores.Max(a => a.age));
            }
            else
            {
                stats.carnivoreOldestPerCycle.Add(0);
            }
            #endregion

            //Count herbivores and carnivores
            stats.herbivoreCountPerCycle.Add(herbivores.Count);
            stats.carnivoreCountPerCycle.Add(carnivores.Count);

            #region 5.) Reproduce
            //Herbivore
            counter = 0;
            foreach (Herbivore h in herbivores.FindAll(a => a.energy >= energyReprHerbivores))
            {
                if (herbivores.Count < numHerbivores)
                {
                    Herbivore child = (Herbivore)h.reproduce();
                    putAgentOnMap(child);
                    herbivores.Add(child);
                    counter++;
                }
            }
            stats.herbivoreBirthsPerCycle.Add(counter);

            //Carnivore
            counter = 0;
            foreach (Carnivore c in carnivores.FindAll(a => a.energy >= energyReprCarnivores))
            {
                if (carnivores.Count < numCarnivores)
                {
                    Carnivore child = (Carnivore)c.reproduce();
                    putAgentOnMap(child);
                    carnivores.Add(child);
                    counter++;
                }
            }
            stats.carnivoreBirthsPerCycle.Add(counter);

            #endregion

            // 6.) Replenish, if too low
            addPlants(minPlants - plants.Count);
            if (herbivores.Count <= numHerbivores / 10)
            {
                addHerbivores(numHerbivores * 9 / 10, energyInitHerbivores);
            }
            if (carnivores.Count <= numCarnivores / 10)
            {
                addCarnivores(numCarnivores * 9 / 10, energyInitCarnivores);
            }
        }