Ejemplo n.º 1
0
        public void DecideAction(Animal animal, List <GameObject> nearbyObjects)
        {
            List <ActionUtility> actionAds = nearbyObjects.
                                             Where(o => o != animal)
                                             .SelectMany(o => o.GetAdvertisedActions(animal, 0.0)).ToList();

            actionAds.Add(new ActionUtility(animal.IdleAction, new Tuple <Need, double>[]
            {
                ActionUtility.NewPair(Need.JobFullfilment, 0.002)
            }));

            EntityAction attackerResponseAction;

            // Add actions to respond to the attacker being nearby.
            // Bears will always attack nearby entities, but wolves might not, and goats and hogs will not.
            if (animal.Attacker != null && nearbyObjects.Contains(animal.Attacker))
            {
                if (animal.Type == AnimalType.Bear)
                {
                    attackerResponseAction = new AttackAction(animal.Attacker);
                }
                // Wolves will fight back if it can win. I.e. it will take less ticks for the wolf to kill the attacker than for the
                //  attacker to kill the wolf.
                else if (animal.Type == AnimalType.Wolf && animal.GetHealth() / animal.Attacker.Strength > animal.GetHealth() / animal.Strength)
                {
                    attackerResponseAction = new AttackAction(animal.Attacker);
                }
                else
                {
                    attackerResponseAction = new FleeAction(animal.Attacker);
                }
                actionAds.Add(new ActionUtility(attackerResponseAction, new Tuple <Need, double>[]
                {
                    ActionUtility.NewPair(Need.JobFullfilment, DDeltaConfig.attackerResponseDelta)
                }));
            }

            EntityAction bestAction = utilityDecider.GetBestAction(animal, actionAds);

            if (!Utilities.AreSameBaseType(animal.Action, bestAction))
            {
                animal.TrySetAction(bestAction);
            }
        }
Ejemplo n.º 2
0
        // Uses the person's needs and the list of GameObjects that are nearby
        public void DecideAction(Person person, List <GameObject> nearbyObjects, int personCount, int food, int mouseX, int mouseY)
        {
            //person.SetAction(new FollowMouseAction(mouseX, mouseY, housePosition, houseRectangle));

            double personCountMod = (personCount == 0 ? 1.0 : food / (2 * personCount));

            List <ActionUtility> actionAds = nearbyObjects
                                             .Where(o => o != person)
                                             // Kinda clunky but this will allow extra data to control action advertisements.
                                             .SelectMany(o => o is Person ? o.GetAdvertisedActions(person, personCountMod) : o.GetAdvertisedActions(person, 0.0))
                                             .ToList();

            //if (personCount >= personLimit)
            //    actionAds = actionAds.Where(ad => !(ad.Action is MateAction)).ToList();


            // Limit the number of people that can be present at one time. This would be more efficient as a check inside Person to prevent
            //  people from advertising a mate action if their are too many people.
            if (personCount >= personLimit)
            {
                actionAds = actionAds.Where(au => !(au.Action is MateAction)).ToList();
            }

            double deliverFoodDelta = person.GetItemCount(ItemType.Apple) / 2;

            // Return food to house.
            actionAds.Add(new ActionUtility(new DeliverFoodAction(houseRectangle), new Tuple <Need, double>[]
            {
                ActionUtility.NewPair(Need.JobFullfilment, deliverFoodDelta)
            }));
            // Wander around.
            actionAds.Add(new ActionUtility(person.IdleAction, new Tuple <Need, double>[]
            {
                ActionUtility.NewPair(Need.JobFullfilment, DDeltaConfig.idleActionAdvertisement)
            }));

            // Hunger need.
            if ((person.GetItemCount(ItemType.Apple) > 0 || food > 0) && person.Hunger <= 0.5)
            {
                actionAds.Add(new ActionUtility(new EatAction(), new Tuple <Need, double>[]
                {
                    ActionUtility.NewPair(Need.Hunger, 1.0 - person.Hunger)
                }));
            }

            // Tiredness need.
            if ((person.Tiredness < 0.6 || person.Health < 50) && !person.AttackerInRange()) // Sleep required. ** Add check to see if attacker is nearby **.
            {
                actionAds.Add(new ActionUtility(new SleepAction(Person.sleepSeconds, true), new Tuple <Need, double>[]
                {
                    ActionUtility.NewPair(Need.Tiredness, DDeltaConfig.sleepActionAdvertisement),
                    ActionUtility.NewPair(Need.Health, DDeltaConfig.sleepHealthAdvertisement)
                }));
            }
            else // Napping optional. This should be picked more often unless there are a lot of other actions to pick.
            {
                actionAds.Add(new ActionUtility(new SleepAction(Person.napSeconds, false), new Tuple <Need, double>[]
                {
                    ActionUtility.NewPair(Need.Tiredness, DDeltaConfig.napActionDelta)
                }));
            }

            if (person.GetRectangleF().IntersectsWith(houseRectangle))
            {
                actionAds.Add(dropUtility);
            }

            EntityAction attackerResponseAction;

            if (person.Attacker != null && nearbyObjects.Contains(person.Attacker))
            {
                if (person.Sex == Sex.Male && person.GetHealth() / person.Attacker.Strength > person.GetHealth() / person.Strength)
                {
                    attackerResponseAction = new AttackAction(person.Attacker);
                }
                else
                {
                    attackerResponseAction = new FleeAction(person.Attacker);
                }

                actionAds.Add(new ActionUtility(attackerResponseAction, new Tuple <Need, double>[]
                {
                    ActionUtility.NewPair(Need.JobFullfilment, DDeltaConfig.attackerResponseDelta)
                }));
            }

            // *** Apply action ad filters here from EntitController's tasks ***
            ApplyTaskFilters(actionAds);

            // NOTE: Doesn't filter actions that give 0.0 delta or a negative!
            EntityAction bestAction = utilityDecider.GetBestAction(person, actionAds);

            if (!Utilities.AreSameBaseType(person.Action, bestAction))
            {
                if (!(bestAction is WanderAction)) // Remove leftover velocity from a WanderAction.
                {
                    person.Stop();
                }
                person.TrySetAction(bestAction);
            }
        }