Пример #1
0
        public override void Update(float frameTime)
        {
            // If we asked for a new action we don't want to dump the existing one.
            if (_actionRequest != null)
            {
                if (_actionRequest.Status != JobStatus.Finished)
                {
                    return;
                }

                ReceivedAction();
                // Do something next tick
                return;
            }

            _planCooldownRemaining -= frameTime;

            // Might find a better action while we're doing one already
            if (_planCooldownRemaining <= 0.0f)
            {
                _planCooldownRemaining = PlanCooldown;
                _actionCancellation    = new CancellationTokenSource();
                _actionRequest         = _planner.RequestAction(new AiActionRequest(SelfEntity.Uid, _blackboard, _availableActions), _actionCancellation);

                return;
            }

            // When we spawn in we won't get an action for a bit
            if (CurrentAction == null)
            {
                return;
            }

            var outcome = CurrentAction.Execute(frameTime);

            switch (outcome)
            {
            case Outcome.Success:
                if (CurrentAction.ActionOperators.Count == 0)
                {
                    CurrentAction.Shutdown();
                    CurrentAction = null;
                    // Nothing to compare new action to
                    _blackboard.GetState <LastUtilityScoreState>().SetValue(0.0f);
                }
                break;

            case Outcome.Continuing:
                break;

            case Outcome.Failed:
                CurrentAction.Shutdown();
                CurrentAction = null;
                _blackboard.GetState <LastUtilityScoreState>().SetValue(0.0f);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Пример #2
0
        public bool Tick(bool autopilotEnabled)
        {
            if (ExecuteUntil > DateTime.UtcNow && CurrentAction != null)
            {
                CurrentAction.Execute();
                return(true);
            }

            if (LastActionExecuted + Cooldown <= DateTime.UtcNow)
            {
                IEnumerable <IIdleAction> filteredActions = IdleActions.Where(e => (!e.AutopilotOnly || autopilotEnabled) && !LastActions.Any(e => e.Value == e.GetType()) || LastActions.Where(x => x.Value == e.GetType() && (DateTime.UtcNow - x.Key).TotalMilliseconds > Rnd.Next(CurrentAction.MinCooldown, CurrentAction.MaxCooldown)).Any());

                if (filteredActions.Any())
                {
                    CurrentAction = filteredActions.ElementAtOrDefault(Rnd.Next(0, filteredActions.Count()));

                    if (CurrentAction != null && CurrentAction.Enter())
                    {
                        LastActionExecuted = DateTime.UtcNow;
                        Cooldown           = TimeSpan.FromMilliseconds(Rnd.Next(MinCooldown, MaxCooldown));
                        ExecuteUntil       = LastActionExecuted + TimeSpan.FromMilliseconds(Rnd.Next(CurrentAction.MinDuration, CurrentAction.MaxDuration));

                        LastActions.Add(new KeyValuePair <DateTime, Type>(LastActionExecuted, CurrentAction.GetType()));

                        CurrentAction.Execute();
                        return(true);
                    }
                }
            }

            return(false);
        }
Пример #3
0
 public void StartNextActionOrReaction()
 {
     if (HasReaction())
     {
         CurrentReaction.Start();
     }
     else if (CurrentActive != null && HasAction())
     {
         CurrentAction.Execute();
     }
 }
Пример #4
0
 public void ExecuteNextAction()
 {
     if (CurrentAction != null)
     {
         GameObject.Destroy(CurrentAction);
     }
     if (actionQueue.Count() > 0)
     {
         CurrentAction = actionQueue.Dequeue();
         CurrentAction.Execute();
     }
 }
Пример #5
0
        public bool Tick(bool autopilotEnabled)
        {
            if (ExecuteUntil > DateTime.UtcNow && CurrentAction != null)
            {
                CurrentAction.Execute();
                return(true);
            }

            // cleanup old events
            LastActions.RemoveAll(e => e.Key < e.Value.Cooldown);

            if (LastActionExecuted + Cooldown <= DateTime.UtcNow)
            {
                IEnumerable <IIdleAction> filteredActions = IdleActions.Where
                                                            (
                    e => Config.IdleActionsEnabled.TryGetValue(e.ToString(), out bool b) && b &&
                    (!e.AutopilotOnly || autopilotEnabled) &&
                    DateTime.Now > e.Cooldown
                                                            );

                if (filteredActions.Any())
                {
                    CurrentAction = filteredActions.ElementAtOrDefault(Rnd.Next(0, filteredActions.Count()));

                    if (CurrentAction != null && CurrentAction.Enter())
                    {
                        LastActionExecuted = DateTime.UtcNow;

                        Cooldown = TimeSpan.FromMilliseconds(Rnd.Next(MinActionCooldown, MaxActionCooldown));
                        CurrentAction.Cooldown = DateTime.Now + TimeSpan.FromMilliseconds(Rnd.Next(CurrentAction.MinCooldown, CurrentAction.MaxCooldown));
                        ExecuteUntil           = LastActionExecuted + TimeSpan.FromMilliseconds(Rnd.Next(CurrentAction.MinDuration, CurrentAction.MaxDuration));

                        LastActions.Add(new(LastActionExecuted, CurrentAction));

                        CurrentAction.Execute();
                        return(true);
                    }
                }
            }

            return(false);
        }
        public void Process()
        {
            if (Ship.Alive)
            {
                if (ActionList != null && ActionList.Count > 0)
                {
                    ActionListFinished = false;

                    if (!ActionsPaused)
                    {
                        if (CurrentAction == null)
                        {
                            CurrentAction = ActionList[0];
                        }

                        if (CurrentAction.Completed)
                        {
                            ActionList.Remove(CurrentAction);
                            if (ActionList.Count > 0)
                            {
                                CurrentAction = ActionList[0];
                            }
                            else
                            {
                                CurrentAction = null;
                            }
                        }

                        if (CurrentAction != null)
                        {
                            CurrentAction.Execute(Ship);
                        }
                    }
                }
                else
                if (!ActionListFinished)
                {
                    ActionListFinished = true;
                    if (Ship != null && Ship.Commander != null)
                    {
                        Ship.Commander.ExecuteRun();
                    }
                }

                if (EventList != null && EventList.Count > 0)
                {
                    EventListFinished = false;

                    if (CurrentEvent == null)
                    {
                        CurrentEvent = EventList[0];
                    }

                    if (CurrentEvent.Completed)
                    {
                        if (CurrentEvent is StopAction)
                        {
                            ActionsPaused = false;
                        }

                        EventList.Remove(CurrentEvent);
                        if (EventList.Count > 0)
                        {
                            CurrentEvent = EventList[0];
                        }
                        else
                        {
                            CurrentEvent = null;
                        }
                    }

                    if (CurrentEvent != null)
                    {
                        if (CurrentEvent is StopAction)
                        {
                            ActionsPaused = true;
                        }
                        CurrentEvent.Execute(Ship);
                    }
                }
                else
                if (!EventListFinished)
                {
                    EventListFinished = true;
                }
            }
        }