示例#1
0
    public void Revise(Agent reviseur, Creature from, Creature to)
    {
        DataSpecies data = reviseur.Memory.Species.GetByKey(from.SpecieID);

        if (data == null)
        {
            return;
        }

        bool          fromIsPredateur      = false;
        GoalComposite currentCompositeGoal = from.agentCreature.IsThinking ? from.agentCreature.Thinking.ActiveGoal as GoalComposite : null;

        while (currentCompositeGoal != null)
        {
            Goal currentGoal = currentCompositeGoal.GetActiveGoal();
            if (currentGoal.GetType() == typeof(GoalHunt))
            {
                fromIsPredateur = true;
                break;
            }
            currentCompositeGoal = currentGoal as GoalComposite;
        }

        if (fromIsPredateur)
        {
            data.addCarnivorousFood(new CarnivorousFood(to.SpecieID, Time.time));
        }

        base.Revise(reviseur);
    }
示例#2
0
    public override void Write(Data data)
    {
        if (!(data is DataSpecies))
        {
            return;
        }
        DataSpecies dataSpecies = data as DataSpecies;

        DataSpecies sameData = species.Find(d => d.SpeciesID == dataSpecies.SpeciesID);

        if (sameData != null)
        {
            /*sameData.preyIDs = sameData.preyIDs.Union(dataSpecies.preyIDs).ToList();
             * sameData.eatFoodTypes = sameData.eatFoodTypes.Union(dataSpecies.eatFoodTypes).ToList();*/
            foreach (CarnivorousFood food in dataSpecies.CarnivorousFoods)
            {
                sameData.addCarnivorousFood(new CarnivorousFood(food));
            }
            foreach (HerbivorFood food in dataSpecies.HerbivorFoods)
            {
                sameData.addHerbivorFood(new HerbivorFood(food));
            }
        }
        else
        {
            species.Add(new DataSpecies(dataSpecies));
        }
    }
示例#3
0
 public DataSpecies(DataSpecies dataSpecies)
 {
     SpeciesID        = dataSpecies.SpeciesID;
     carnivorousFoods = new List <CarnivorousFood>(dataSpecies.carnivorousFoods);
     herbivorFoods    = new List <HerbivorFood>(dataSpecies.herbivorFoods);
     CheckIfCarnivorous();
 }
示例#4
0
    public void RemoveByKey(int key)
    {
        DataSpecies element = species.Find(data => data.SpeciesID == key);

        if (element != null)
        {
            species.Remove(element);
        }
    }
示例#5
0
 private Predicate <FoodType> GetFoodTypeFilter()
 {
     return(foodType => {
         DataSpecies data = owner.Memory.Species.GetByKey(owner.Creature.SpecieID);
         if (data == null)
         {
             return false;
         }
         return data.EatFoodType.Contains(foodType);
     });
 }
    public void Revise(Agent reviseur, Creature from, Creature to)
    {
        DataSpecies data = reviseur.Memory.Species.GetByKey(from.SpecieID);

        if (data == null)
        {
            return;
        }

        data.addCarnivorousFood(new CarnivorousFood(to.SpecieID, Time.time));

        base.Revise(reviseur);
    }
示例#7
0
    public void Revise(Agent reviseur, Creature from, FoodType foodType)
    {
        DataSpecies data = reviseur.Memory.Species.GetByKey(from.SpecieID);

        if (data == null)
        {
            return;
        }

        data.addHerbivorFood(new HerbivorFood(foodType, Time.time));

        base.Revise(reviseur);
    }
示例#8
0
    private Predicate <Creature> GetCreatureFilter()
    {
        return(creature => {
            //if(creature.SpecieID == owner.Creature.SpecieID) return false;
            //return true;

            DataSpecies data = owner.Memory.Species.GetByKey(owner.Creature.SpecieID);
            if (data == null)
            {
                return false;
            }
            return data.PreyIDs.Contains(creature.SpecieID);
        });
    }
示例#9
0
    public static bool CreatureIsHostil(Agent owner, Creature other)
    {
        if (owner.Creature.SpecieID == other.SpecieID)
        {
            return(false);
        }

        DataSpecies data = owner.Memory.Species.GetByKey(other.SpecieID);

        if (data == null)
        {
            return(false);
        }
        if (owner.Debug)
        {
            /*Debug.Log("List : " + other.SpecieID);
             * foreach(int i in data.preyIDs){
             *  Debug.Log("i");
             * }*/
        }

        bool isEatMe = data.IsCarnivorous && data.PreyIDs.Contains(owner.Creature.SpecieID);

        if (other.agentCreature.IsThinking)
        {
            if (!isEatMe)
            {
                return(false);
            }
        }
        else
        {
            if (!isEatMe && (other.agentCreature.Steering.Target == null || other.agentCreature.Steering.Target.Creature.SpecieID != owner.Creature.SpecieID))
            {
                return(false);
            }
        }

        //if(other.Traits.Carnivorous.Value < 0.5f) return false;

        DesirabilitiesConfig desirabilitiesConfig = GameManager.Instance.DesirabilitiesConfig;

        return(Vector3.Distance(owner.transform.position, other.transform.position) < desirabilitiesConfig.EvadeConsiderationMaxDistance / 2);
    }
示例#10
0
    public static float Ratio(Agent evaluator, Creature target)
    {
        Creature evaluatorCreature = evaluator.Creature;

        //TODO à utiliser plus tard, pour la complexificaiton
        DataSpecies evaluatorSpecies = evaluator.Memory.Species.GetByKey(evaluator.Creature.SpecieID);
        DataSpecies targetSpecies    = target.agentCreature.Memory.Species.GetByKey(evaluator.Creature.SpecieID);

        if (evaluatorSpecies == null || targetSpecies == null)
        {
            return(1);
        }

        int nbAllies   = 1;
        int nbEnnemies = 1;

        IEnumerable <DataCreature> creatures = evaluator.Memory.Creatures.Read();

        foreach (DataCreature data in creatures)
        {
            Creature creature = data.creature;
            if (!creature || !creature.gameObject.activeSelf || data.RegistrationDate < Time.time - 5f)
            {
                continue;
            }
            if (creature.agentCreature == evaluator || creature == target)
            {
                continue;
            }

            int specieID = creature.SpecieID;
            if (specieID == evaluatorCreature.SpecieID)
            {
                ++nbAllies;
            }
            else if (specieID == target.SpecieID)
            {
                ++nbEnnemies;
            }
        }

        return(nbAllies / nbEnnemies);
    }
示例#11
0
    /*public void Reproduce(GameObject parent1, GameObject parent2)
     * {
     *  Assert.IsNotNull(parent1);
     *  Assert.IsNotNull(parent2);
     *  GetCreature(parent1.transform.parent, (parent1.transform.position + parent2.transform.position) / 2f + new Vector3(0, 1f, 0),
     *      Quaternion.Slerp(parent1.transform.rotation, parent2.transform.rotation, 0.5f),
     *      spawnedCreatures[parent1].values.attackPower, spawnedCreatures[parent1].values.nutritionnal,
     *      spawnedCreatures[parent1].values.maxLifeNumber, spawnedCreatures[parent1].values.SpecieID,
     *      spawnedCreatures[parent1].values.Traits, null);
     *  //ARDUINO ISLAND
     *  parent1.GetComponent<Creature>();
     *  //
     * }*/

    /*public Creature Reproduce(Creature parent1, Creature parent2)
     * {
     *  Assert.IsNotNull(parent1);
     *  Assert.IsNotNull(parent2);
     *
     *  // TODO
     *  // faudrais merge les listes d'alimentation des deux parent en vrai, là je prend juste le parent 1 pour implémenter le changement rapidement
     *  IReadOnlyCollection<CarnivorousFood> carnivorousFoodsParent1 = parent1.agentCreature.Memory.Species.GetByKey(parent1.SpecieID).CarnivorousFoods;
     *  IReadOnlyCollection<HerbivorFood> herbivorFoodsParent1 = parent1.agentCreature.Memory.Species.GetByKey(parent1.SpecieID).HerbivorFoods;
     *
     *  List<int> carnivorousFoods = new List<int>(carnivorousFoodsParent1.Select(food => food.preyID));
     *  List<FoodType> herbivorFoods = new List<FoodType>(herbivorFoodsParent1.Select(food => food.foodType));
     *
     *  Creature creature = GetCreature((parent1.transform.position + parent2.transform.position) / 2f + new Vector3(0, 1f, 0),
     *      Quaternion.Slerp(parent1.transform.rotation, parent2.transform.rotation, 0.5f),
     *      parent1.SpecieID,
     *      parent1.Traits, null,
     *      carnivorousFoods, herbivorFoods);
     *
     *  creature.agentCreature.Memory.MergeFrom(parent1.agentCreature, MemoryType.Species);
     *  creature.agentCreature.Memory.MergeFrom(parent2.agentCreature, MemoryType.Species);
     *
     *  return creature;
     * }*/

    /*public GameObject[] AllSpawnedGameObjects
     * {
     *  get
     *  {
     *      return spawnedCreatures.Keys.ToArray();
     *  }
     * }*/

    /*public Creature[] AllSpawnedCreatures
     * {
     *  get
     *  {
     *      Creature[] returned = new Creature[spawnedCreatures.Count];
     *      int i = -1;
     *      foreach (CreatureReferences value in spawnedCreatures.Values)
     *      {
     ++i;
     *          returned[i] = value.values;
     *      }
     *
     *      return returned;
     *  }
     * }*/
    //private Dictionary<GameObject, CreatureReferences> spawnedCreatures;

    /*public Creature GetCreature(Transform newParent, Vector3 worldPosition, Quaternion rotation, int attackPower, int nutritionnal, int maxLifeNumber, int SpecieID, CreatureTraits newTrait, CreatureGauges newGauge)
     * {
     *  CreatureReferences refCreat = new CreatureReferences();
     *  Creature newCreature = Instantiate(CreaturePrefab, worldPosition, rotation);
     *
     *  if(newParent != null)
     *      newCreature.transform.SetParent(newParent);
     *  newCreature.transform.position = worldPosition;
     *  newCreature.transform.rotation = rotation;
     *  newCreature.nutritionnal = nutritionnal;
     *  newCreature.attackPower = attackPower;
     *  newCreature.maxLifeNumber = maxLifeNumber;
     *  newCreature.SpecieID = SpecieID;
     *
     *  if(newTrait != null)
     *      newCreature.Traits = new CreatureTraits(newTrait);
     *  if(newGauge != null)
     *      newCreature.Gauges = new CreatureGauges(newGauge);
     *
     *  newCreature.gameObject.SetActive(true);
     *
     *  configGauges.InitializeGauges(newCreature, true, false, false);
     *
     *  refCreat.go = newCreature.gameObject;
     *  refCreat.values = newCreature;
     *  refCreat.listenerEvent = (x) => updateGaugesOnTraitModified(x, refCreat.values);
     *  refCreat.values.Traits.UpdatedTrait += refCreat.listenerEvent;
     *  spawnedCreatures[newCreature] = refCreat;
     *  refCreat.values.ColorSwap.Swap(CreatureTraits.GetColor(refCreat.values.Traits));
     *  //Metrics.NotifyCreatureCreated(refCreat.values);
     *  return newCreature;
     * }*/

    public Creature GetCreature(Vector3 worldPosition, Quaternion rotation, int SpecieID,
                                CreatureTraits newTrait, CreatureGauges newGauge,
                                List <int> carnivorousFoods, List <FoodType> herbivorFoods,
                                List <ParticularityType> particularities, Color color)
    {
        Creature newCreature = Instantiate(CreaturePrefab, worldPosition, rotation);

        //newCreature.attackPower = attackPower;
        //newCreature.maxLifeNumber = maxLifeNumber;
        newCreature.SpecieID = SpecieID;

        if (newTrait != null)
        {
            newCreature.Traits = new CreatureTraits(newTrait);
        }
        if (newGauge != null)
        {
            newCreature.Gauges = new CreatureGauges(newGauge);
        }

        foreach (ParticularityType particularityType in particularities)
        {
            newCreature.DNADistortion.AddParticularity(ParticularitiesManager.Instance.CreateParticularity(newCreature, particularityType));
        }

        newCreature.agentCreature.Init();

        DataSpecies data = newCreature.agentCreature.Memory.Species.GetByKey(SpecieID);

        if (newTrait.Carnivorous > 0.5f)
        {
            foreach (int food in carnivorousFoods)
            {
                if (food == SpecieID)
                {
                    continue;
                }
                data.addCarnivorousFood(new CarnivorousFood(food, Time.time));
            }
        }
        else
        {
            foreach (FoodType food in herbivorFoods)
            {
                data.addHerbivorFood(new HerbivorFood(food, Time.time));
            }
        }

        Nest nest = nests != null?nests.FirstOrDefault(n => n.SpecieID == SpecieID) : null;

        if (nest)
        {
            newCreature.agentCreature.Memory.Nests.Write(new DataNest(nest));
        }

        newCreature.gameObject.SetActive(true);

        configGauges.InitializeGauges(newCreature, true, false, false);

        if (customColor)
        {
            newCreature.ColorSwap.Swap(color);
        }
        else
        {
            newCreature.ColorSwap.Swap(CreatureTraits.GetColor(newCreature.Traits));
        }

        aliveCreatures.Add(newCreature);
        //ARDUINO ISLAND
        if (CreatureCountTable != null)
        {
            ++CreatureCountTable[newCreature.SpecieID];
        }

        // Jirachi
        creaturePopulationChangedEvent?.Invoke();

        // Lucas
        CreatureBirth?.Invoke();
        newCreature.CreatureDoing.CreatureEatCreature += CreatureEatCreature;
        newCreature.CreatureDoing.CreatureEatFood     += CreatureEatFood;

        /*Debug.Log("--------------CreatureList-------------");
         * foreach (int specie in CreatureCountTable)
         * {
         *  Debug.Log(specie);
         * }
         * Debug.Log("-----------");*/
        //

        //Metrics.NotifyCreatureCreated(refCreat.values);
        return(newCreature);
    }
示例#12
0
    private List <Creature> CreateCreaturesFromConfig(int specieID, int nb)
    {
        List <Creature> creatures = new List <Creature>();

        Specie specie       = configSpawn.Species[specieID];
        float  boundRandMin = specie.randomizer * -1f;
        float  boundRandMax = specie.randomizer;
        //TODO FORMULE POUR ATTACK POWER, NUTRITIONNAL VALUE, MAX LIFE NUMBER
        //TODO je le fais dans l'init physique de l'agent

        /*int attackPow = (int) specie.specieTraits.Strength * 15 + 5;
         * int nuttritionnalValue = (int) specie.specieTraits.Constitution * 100 + 50;*/
        //int maxLifeNumber = 50;
        CreatureTraits newCreatureTrait = new CreatureTraits(specie.specieTraits);

        if (specie.randomizeOneTraitOnly)
        {
            float traitValue = specie.specieTraits.Get(specie.randomizedTraitOnly).Value;
            newCreatureTrait.Get(specie.randomizedTraitOnly).AddValueClamped(UnityEngine.Random.Range(
                                                                                 (traitValue + boundRandMin) < 0f ? 0f : boundRandMin,
                                                                                 (traitValue + boundRandMax) > 1f ? 1f : boundRandMax
                                                                                 )
                                                                             );
        }
        else
        {
            foreach (CREATURE_TRAITS traits in (CREATURE_TRAITS[])System.Enum.GetValues(typeof(CREATURE_TRAITS)))
            {
                float traitValue = specie.specieTraits.Get(traits).Value;
                newCreatureTrait.Get(traits).AddValueClamped(UnityEngine.Random.Range(
                                                                 (traitValue + boundRandMin) < 0f ? 0f : boundRandMin,
                                                                 (traitValue + boundRandMax) > 1f ? 1f : boundRandMax
                                                                 )
                                                             );
            }
        }
        for (int i = 0; i < nb; ++i)
        {
            Creature spawnedCreature = GetCreature(Vector3.zero, Quaternion.identity,
                                                   specieID, newCreatureTrait, null, specie.CarnivorousFoods, specie.HerbivorFoods,
                                                   specie.particularities, specie.defaultColor);
            spawnedCreature.gameObject.SetActive(false);

            spawnedCreature.Age = UnityEngine.Random.Range(0, 0.5f);

            for (int k = 0; k < configSpawn.Species.Length; ++k)
            {
                Specie      specieInfo = configSpawn.Species[k];
                DataSpecies data       = new DataSpecies(k);
                foreach (int food in specieInfo.CarnivorousFoods)
                {
                    data.addCarnivorousFood(new CarnivorousFood(food, Time.time));
                }
                foreach (FoodType food in specieInfo.HerbivorFoods)
                {
                    data.addHerbivorFood(new HerbivorFood(food, Time.time));
                }
                spawnedCreature.agentCreature.Memory.Species.Write(data);
            }

            creatures.Add(spawnedCreature);
        }

        return(creatures);
    }
示例#13
0
    private void UpdateStats()
    {
        Color[] colors   = new Color[nbSpecies];
        int[]   nbs      = new int[nbSpecies];
        float[] traits   = new float[nbSpecies];
        int[][] nbsFoods = new int[nbSpecies][];

        for (int i = 0; i < nbSpecies; ++i)
        {
            nbsFoods[i] = new int[nbSpecies + 2];
        }

        foreach (Creature creature in factory.AliveCreature)
        {
            int id = creature.SpecieID;
            colors[id] += creature.ColorSwap.GetColor();
            ++nbs[id];
            traits[id] += creature.Traits.Get(observedTrait);

            DataSpecies data = creature.agentCreature.Memory.Species.GetByKey(creature.SpecieID);
            foreach (CarnivorousFood food in data.CarnivorousFoods)
            {
                ++nbsFoods[id][food.preyID];
            }
            foreach (HerbivorFood food in data.HerbivorFoods)
            {
                ++nbsFoods[id][nbSpecies + (food.foodType == FoodType.Fruit ? 0 : 1)];
            }
        }

        for (int i = 0; i < nbSpecies; ++i)
        {
            int nb = nbs[i];
            NbCreatures[i] = nb;

            Color mediumColor = colors[i] / nb;
            float mediumTrait = traits[i] / nb;

            int maxFoodID = 0;
            for (int k = 1; k < nbSpecies + 2; k++)
            {
                if (nbsFoods[i][k] > nbsFoods[i][maxFoodID])
                {
                    maxFoodID = k;
                }
            }

            string prefFood;
            if (maxFoodID >= nbSpecies)
            {
                prefFood = maxFoodID == 0 ? "Fruit" : "Vegetable";
            }
            else
            {
                prefFood = "Meal of Species " + maxFoodID;
            }

            DebugGUI.SetGraphProperties("population " + i, "Population of Species " + i, 0, 25, 0, mediumColor, false);
            if (currentObservedTrait != observedTrait)
            {
                DebugGUI.RemoveGraph("trait " + i);
            }
            DebugGUI.SetGraphProperties("trait " + i, observedTrait + " of Species " + i, 0, 1, 1, mediumColor, false);
            DebugGUI.SetGraphProperties("prefFood " + i, "Favorite of Species " + i + " : " + prefFood, 0, 0, 2, mediumColor, false);

            DebugGUI.Graph("population " + i, nb);
            DebugGUI.Graph("trait " + i, mediumTrait);
        }

        currentObservedTrait = observedTrait;
    }
示例#14
0
    public bool ContainKey(int key)
    {
        DataSpecies element = species.Find(data => data.SpeciesID == key);

        return(element != null);
    }