Exemple #1
0
        public void OnGameTick(float dt)
        {
            foreach (IAiTask task in Tasks)
            {
                int slot = task.Slot;

                if ((ActiveTasksBySlot[slot] == null || task.Priority > ActiveTasksBySlot[slot].PriorityForCancel) && task.ShouldExecute() && entity.ShouldExecuteTask(task))
                {
                    if (ActiveTasksBySlot[slot] != null)
                    {
                        ActiveTasksBySlot[slot].FinishExecute(true);
                    }

                    ActiveTasksBySlot[slot] = task;
                    task.StartExecute();
                }
            }


            for (int i = 0; i < ActiveTasksBySlot.Length; i++)
            {
                IAiTask task = ActiveTasksBySlot[i];
                if (task == null)
                {
                    continue;
                }

                if (!task.ContinueExecute(dt))
                {
                    task.FinishExecute(false);
                    ActiveTasksBySlot[i] = null;
                }
            }


            if (entity.World.EntityDebugMode)
            {
                string tasks = "";
                int    j     = 0;
                for (int i = 0; i < ActiveTasksBySlot.Length; i++)
                {
                    IAiTask task = ActiveTasksBySlot[i];
                    if (task == null)
                    {
                        continue;
                    }
                    if (j++ > 0)
                    {
                        tasks += ", ";
                    }
                    tasks += TaskCodes[task.GetType()] + "(" + task.Priority + ")";
                }
                entity.DebugAttributes.SetString("AI Tasks", tasks.Length > 0 ? tasks : "-");
            }
        }
        public void OnGameTick(float dt)
        {
            entity.World.FrameProfiler.Mark("entity-ai-tasks-tick-begin");

            foreach (IAiTask task in Tasks)
            {
                int slot = task.Slot;

                if ((ActiveTasksBySlot[slot] == null || task.Priority > ActiveTasksBySlot[slot].PriorityForCancel) && task.ShouldExecute() && ShouldExecuteTask(task))
                {
                    if (ActiveTasksBySlot[slot] != null)
                    {
                        ActiveTasksBySlot[slot].FinishExecute(true);
                    }

                    ActiveTasksBySlot[slot] = task;
                    task.StartExecute();
                    OnTaskStarted?.Invoke(task);
                }

                if (entity.World.FrameProfiler.Enabled)
                {
                    entity.World.FrameProfiler.Mark("entity-ai-tasks-tick-start-exec" + task.GetType());
                }
            }

            entity.World.FrameProfiler.Mark("entity-ai-tasks-tick-begin-cont");

            for (int i = 0; i < ActiveTasksBySlot.Length; i++)
            {
                IAiTask task = ActiveTasksBySlot[i];
                if (task == null)
                {
                    continue;
                }

                if (!task.ContinueExecute(dt))
                {
                    task.FinishExecute(false);
                    ActiveTasksBySlot[i] = null;
                }

                if (entity.World.FrameProfiler.Enabled)
                {
                    entity.World.FrameProfiler.Mark("entity-ai-tasks-tick-cont-" + task.GetType());
                }
            }


            entity.World.FrameProfiler.Mark("entity-ai-tasks-tick-cont-exec");


            if (entity.World.EntityDebugMode)
            {
                string tasks = "";
                int    j     = 0;
                for (int i = 0; i < ActiveTasksBySlot.Length; i++)
                {
                    IAiTask task = ActiveTasksBySlot[i];
                    if (task == null)
                    {
                        continue;
                    }
                    if (j++ > 0)
                    {
                        tasks += ", ";
                    }

                    string code;
                    AiTaskRegistry.TaskCodes.TryGetValue(task.GetType(), out code);

                    tasks += code + "(" + task.Priority + ")";
                }
                entity.DebugAttributes.SetString("AI Tasks", tasks.Length > 0 ? tasks : "-");
            }
        }
        public void OnGameTick(float dt)
        {
            foreach (IAiTask task in tasks)
            {
                int     slot    = task.Slot;
                IAiTask oldTask = activeTasksBySlot[slot];
                if ((oldTask == null || task.Priority > oldTask.PriorityForCancel) && task.ShouldExecute() && ShouldExecuteTask(task))
                {
                    //if (oldTask!=null) Console.WriteLine("stop " + oldTask.ProfilerName);
                    oldTask?.FinishExecute(true);
                    activeTasksBySlot[slot] = task;
                    task.StartExecute();
                    OnTaskStarted?.Invoke(task);
                    //Console.WriteLine("start " + task.ProfilerName);
                }

                if (entity.World.FrameProfiler.Enabled)
                {
                    entity.World.FrameProfiler.Mark(task.ProfilerName);
                }
            }


            for (int i = 0; i < activeTasksBySlot.Length; i++)
            {
                IAiTask task = activeTasksBySlot[i];
                if (task == null)
                {
                    continue;
                }
                if (!task.CanContinueExecute())
                {
                    continue;
                }

                if (!task.ContinueExecute(dt))
                {
                    task.FinishExecute(false);
                    //Console.WriteLine("stop " + task.ProfilerName);
                    activeTasksBySlot[i] = null;
                }

                if (entity.World.FrameProfiler.Enabled)
                {
                    entity.World.FrameProfiler.Mark("task-continueexec-" + AiTaskRegistry.TaskCodes[task.GetType()]);
                }
            }


            if (entity.World.EntityDebugMode)
            {
                string tasks = "";
                int    j     = 0;
                for (int i = 0; i < activeTasksBySlot.Length; i++)
                {
                    IAiTask task = activeTasksBySlot[i];
                    if (task == null)
                    {
                        continue;
                    }
                    if (j++ > 0)
                    {
                        tasks += ", ";
                    }

                    string code;
                    AiTaskRegistry.TaskCodes.TryGetValue(task.GetType(), out code);

                    tasks += code + "(p" + task.Priority + ", pc" + task.PriorityForCancel + ")";
#if DEBUG
                    // temporary for debugging
                    if (entity.Properties.Habitat == EnumHabitat.Underwater && task is AiTaskWander wand)
                    {
                        tasks += String.Format(" Heading to: {0:0.00},{1:0.00},{2:0.00}", wand.MainTarget.X - 500000, wand.MainTarget.Y, wand.MainTarget.Z - 500000);
                    }
#endif
                }
                entity.DebugAttributes.SetString("AI Tasks", tasks.Length > 0 ? tasks : "-");
            }
        }