Exemple #1
0
        public double Evaluate(Game game, EventContext context, Weights weights)
        {
            //Get the weights for the character that will be choosing the character.
            Weights charWeights = context.CurrentCharacter.GetWeights(weights.Perspective);

            Character[] availableCharacters = game.FilterCharacters(requirements, context).ToArray();
            bool[] allowed = new bool[availableCharacters.Length];
            if (context.CurrentCharacter is AICharacter)
            {
                //AICharacter will only select characters from his important character's list.
                var important = (context.CurrentCharacter as AICharacter).GetImportantCharacters().Select(pair => pair.Key).ToList();
                for (int i = 0; i < availableCharacters.Length; ++i)
                {
                    allowed[i] = important.Contains(availableCharacters[i]);
                }
            }
            else
            {
                //The player could select anything.  We don't know anything about his preferences.
                for (int i = 0; i < availableCharacters.Length; ++i)
                {
                    allowed[i] = true;
                }
            }

            //Figure out which ones we think he will choose.
            int[] bestIndices = AIHelper.GetBest(availableCharacters, allowed, charWeights, (character, localWeights) =>
            {
                //We are only considering things theoretically: Don't make any changes to the context
                //we are given.  We want a new local context for each character so variable changes for
                //one character don't influence the others.
                EventContext localContext = new EventContext(context);
                localContext.PushScope(character, scopeName);
                double directResult = operation.Evaluate(game, localContext, localWeights);
                //We need to take into account any prestige modifiers because we are throwing away
                //the local context now.
                return directResult + localWeights.MeasureAfter(localContext, game);
            });
            //Evaluate each of those character using our weights
            double result = 0.0;
            foreach(var bestIndex in bestIndices)
            {
                Character best = availableCharacters[bestIndex];
                //We are only considering things theoretically: Don't make any changes to the context
                //we are given.  We want a new local context for each character so variable changes for
                //one character don't influence the others.
                EventContext localContext = new EventContext(context);
                localContext.PushScope(best, scopeName);
                result += operation.Evaluate(game, localContext, weights);
                //We need to take into account any prestige modifiers because we are throwing away
                //the local context now.
                result += weights.MeasureAfter(localContext, game);
            }
            return result / bestIndices.Length;
        }
Exemple #2
0
        public double Evaluate(Game game, EventContext context, Weights weights)
        {
            double result = 0.0;

            //DirectExecute always happens if it is present.
            if (DirectExecute != null)
                result += DirectExecute.Evaluate(game, context, weights);

            EventOption[] options = GetAvailableOptions(context, game);
            if(options.Length == 1)
            {
                //This is an optimization.  We don't need to evaluate another character's perspective
                //if they only have a single option.

                //We are only considering things theoretically: Don't make any changes to the context
                //we are given.  We want a new local context for each option so variable changes for
                //one option don't influence the others.
                EventContext localContext = new EventContext(context);
                result += options[0].DirectExecute.Evaluate(game, localContext, weights);
                //We need to take into account any prestige modifiers because we are throwing away
                //the local context now.
                result += weights.MeasureAfter(localContext, game);
            }
            else if (options.Length > 0)
            {
                //Get the weights that will be used to decide the best option.
                Weights optionWeights = context.CurrentCharacter.GetWeights(weights.Perspective);

                //Evaluate each option from the perpsective of the character choosing.
                EventOption[] bestOptions = AIHelper.GetBest(options, optionWeights, (option, w) =>
                {
                    //We are only considering things theoretically: Don't make any changes to the context
                    //we are given.  We want a new local context for each option so variable changes for
                    //one option don't influence the others.
                    EventContext localContext = new EventContext(context);
                    double localResult = option.DirectExecute.Evaluate(game, localContext, w);
                    //We need to take into account any prestige modifiers because we are throwing away
                    //the local context now.
                    return localResult + w.MeasureAfter(localContext, game);
                });

                //The current character will choose one of the best (or so we think)
                //We need to evaluate the options from our perspective and average those values.
                foreach(var best in bestOptions)
                {
                    //We are only considering things theoretically: Don't make any changes to the context
                    //we are given.  We want a new local context for each option so variable changes for
                    //one option don't influence the others.
                    EventContext localContext = new EventContext(context);
                    result += best.DirectExecute.Evaluate(game, localContext, weights);
                    //We need to take into account any prestige modifiers because we are throwing away
                    //the local context now.
                    result += weights.MeasureAfter(localContext, game);
                }
                result /= bestOptions.Length;
            }
            return result;
        }