Exemplo n.º 1
0
    protected virtual void SelectAction()
    {
        //Debug.Log(gameObject.name + ": Try Select an Action");

        // go thru all objects
        List <KeyValuePair <float, AbstractAction> > listedActions = new List <KeyValuePair <float, AbstractAction> >();
        float sqrDistance, currentMood, futureMood;  //score,

        foreach (var obj in AdvertisingObject.allObjects)
        {
            // go thru all actions
            foreach (var action in obj.GetAdvertisedActions())
            {
                // calculate the score
                currentMood = needs.CalculateMood();
                futureMood  = needs.CalculatePotentialMood(action.AdvertisedReward);
                score       = currentMood - futureMood;

                if (float.IsNaN(score))
                {
                    score = 0;
                }

                if (useDistanceAttenuation)
                {
                    sqrDistance = (transform.position - action.MyObjectPosition).sqrMagnitude;
                    score       = score / sqrDistance;
                }

                listedActions.Add(new KeyValuePair <float, AbstractAction>(score, action));
            }
        }

        if (listedActions.Count <= 0)
        {
            return;
        }

        // sort the scores and actions
        listedActions.Sort((emp1, emp2) => emp2.Key.CompareTo(emp1.Key));
        Debug.Log("<b>" + gameObject.name + " Score List</b>" + listedActions.PrintList());

        // queue one of the three best actions
        int            numOptions = Mathf.Min(listedActions.Count, 2); //max 3 options
        int            selectedOption = Random.Range(0, numOptions);
        AbstractAction selectedAction = listedActions[selectedOption].Value;

        Debug.Log(gameObject.name + ": Selected: " + selectedAction.Name);
        selectedAction.MyObject.RemoveAction(selectedAction); // remove action from advertised actions
        actionQueue.Enqueue(selectedAction);
    }