public override void Execute(AiContext context, UtilityPick pick)
 {
     _lastInvokedTime = Time.time;
     _invokedTimes++;
     _inExecution = true;
     actionTask?.Invoke(context, pick);
 }
예제 #2
0
        public void GoToTarget(AiContext context, UtilityPick pick)
        {
            var hitGameObject = context.target;
            var owner         = context.owner as NpcMainScript;

            if (owner != null)
            {
                owner._agent.SetDestination(hitGameObject.transform.position);
            }
        }
        public override UtilityPick Select(AiContext context, List <AbstractUtilityAction> actions)
        {
            var utilities = new List <UtilityWeights>();

            foreach (var action in actions)
            {
                UtilityPick utility = action.EvaluateAbsoluteUtility(context);
                if (utility != null && utility.Score > 0f)
                {
                    utilities.Add(new UtilityWeights(utility.Score, utility));
                }
            }

            var count = utilities.Count;

            if (count > 0)
            {
                utilities.Sort((first, second) => first.Rank.CompareTo(second.Rank));

                var std = utilities.GetStd();
                var sum = 0f;

                var minWeight = float.PositiveInfinity;
                var maxWeight = 0f;

                var selected = new List <UtilityWeights>();
                foreach (var u in utilities)
                {
                    if (u.Rank < std)
                    {
                        continue;
                    }
                    selected.Add(u);
                    sum += u.Rank;
                }

                foreach (var u in selected)
                {
                    u.Weight = u.Rank / sum;
                    if (u.Weight > maxWeight)
                    {
                        maxWeight = u.Weight;
                    }
                    if (u.Weight < minWeight)
                    {
                        minWeight = u.Weight;
                    }
                }
                // TODO: Weights are equal to 1 on the first run ??
                var rand = Random.Range(minWeight, maxWeight);
                return(utilities.Find(u => u.Weight >= rand).UAction);
            }

            return(null);
        }
예제 #4
0
 public void GoToCover(AiContext context, UtilityPick pick)
 {
     if (pick.UtilityAction is PickerAction picker)
     {
         var choices = context.GetParameter(picker.evaluatedParamName) as List <Vector3>;
         var owner   = context.owner as NpcMainScript;
         if (pick.SelectorIdx != -1)
         {
             owner._agent.SetDestination(choices[pick.SelectorIdx]);
         }
     }
 }
        public override UtilityPick Select(AiContext context, List <AbstractUtilityAction> actions)
        {
            var         maxUtility         = 0f;
            UtilityPick highestScoreAction = null;

            for (var i = 0; i < actions.Count; i++)
            {
                if (actions[i] != null)
                {
                    var utility = actions[i].EvaluateAbsoluteUtility(context);
                    if (utility.Score > 0 && utility.Score >= maxUtility)
                    {
                        maxUtility         = utility.Score;
                        highestScoreAction = utility;
                    }
                }
            }

            return(highestScoreAction);
        }
예제 #6
0
        public void AttackPlayer(AiContext context, UtilityPick pick)
        {
            var hitGameObject = context.target;
            var owner         = context.owner as NpcMainScript;

            owner._agent.updateRotation = false;

            Vector3    direction = (hitGameObject.transform.position - owner.transform.position).normalized;
            Quaternion qDir      = Quaternion.LookRotation(direction);

            owner.transform.rotation = Quaternion.Slerp(owner.transform.rotation, qDir, Time.deltaTime * 100f);

            owner._agent.updateRotation = true;

            if (hitGameObject != null && hitGameObject.GetComponent <PlayerMainScript>() != null)
            {
                (owner.npcObject as ICombatAggressor)?.AttackTarget(hitGameObject.GetComponent <PlayerMainScript>()
                                                                    .playerObject);
            }
        }
 public UtilityWeights(float rank, UtilityPick action)
 {
     Rank    = rank;
     UAction = action;
 }
예제 #8
0
        public void ReturnHome(AiContext context, UtilityPick pick)
        {
            var owner = context.owner as NpcMainScript;

            owner._agent.SetDestination(context.StartingPoint);
        }