TaskInfo IBotSubmitTask.SubmitTask(float time, IMatchUnitAssetView unit, TaskTemplateType type, params TaskInfo[] parameters)
        {
            if (m_state.BusyUnits[(KnownVoxelTypes)unit.Data.Type].ContainsKey(unit.Id))
            {
                throw new InvalidOperationException("unit " + unit.Id + " of type  " + (KnownVoxelTypes)unit.Data.Type + " is busy");
            }

            TaskInfo taskInfo     = m_state.TaskTemplates[type].Acquire();
            TaskInfo unitIdTask   = TaskInfo.EvalExpression(ExpressionInfo.PrimitiveVal(unit.Id));
            TaskInfo playerIdTask = TaskInfo.EvalExpression(ExpressionInfo.PrimitiveVal(m_playerView.Index));

            taskInfo.Children[0] = unitIdTask;
            taskInfo.Children[1] = playerIdTask;

            int      argsLength = parameters != null ? parameters.Length : 0;
            TaskInfo rootTask   = taskInfo.Children[2 + argsLength];

            if (rootTask == null || taskInfo.Children.Length <= argsLength + 1 || taskInfo.Children[argsLength + 1] != null)
            {
                throw new ArgumentException("wrong number of arguments for task template: " + type, "type");
            }
            rootTask.Inputs[0].OutputTask = unitIdTask;
            rootTask.Inputs[1].OutputTask = playerIdTask;

            if (parameters != null)
            {
                for (int i = 0; i < parameters.Length; ++i)
                {
                    TaskInfo define = parameters[i];
                    define.Inputs[0].ExtensionSocket = rootTask.Inputs[0];
                    define.Inputs[1].ExtensionSocket = rootTask.Inputs[1];
                    taskInfo.Children[2 + i]         = define;
                    taskInfo.Children[2 + parameters.Length].Inputs[2 + i].OutputTask = define;
                }
            }

            taskInfo.SetParents();
            taskInfo.Initialize(m_playerView.Index);

            m_state.FreeUnits[(KnownVoxelTypes)unit.Data.Type].Remove(unit.Id);
            m_state.BusyUnits[(KnownVoxelTypes)unit.Data.Type].Add(unit.Id, unit);

            m_taskEngine.SubmitTask(taskInfo);

            RunningTaskInfo runningTaskInfo = new RunningTaskInfo(type, unit, taskInfo.TaskId, time);

            m_state.TaskIdToTask.Add(taskInfo.TaskId, runningTaskInfo);
            m_state.UnitIdToTask.Add(unit.Id, runningTaskInfo);

            return(taskInfo);
        }
        public IEnumerator FindPathClientSideTaskTest()
        {
            BeginTest(TestEnv1, 2, 0, () =>
            {
                MapRoot map = Dependencies.Map.Map;
                IMatchEngineCli matchEngineCli = Dependencies.MatchEngine;

                const int playerId          = 1;
                Coordinate[] coords         = map.FindDataOfType((int)KnownVoxelTypes.Eater, playerId);
                VoxelData data              = map.Get(coords[0]);
                Coordinate targetCoordinate = coords[0].Add(1, -1);
                MovementCmd moveCmd         = new MovementCmd(CmdCode.Move, data.UnitOrAssetIndex, 0);
                moveCmd.Coordinates         = new[] { coords[0], targetCoordinate };

                ITaskEngine taskEngine = matchEngineCli.GetClientTaskEngine(playerId);
                TaskEngineEvent <TaskInfo> taskStateChanged = null;
                taskStateChanged = taskStateInfo =>
                {
                    if (taskStateInfo.State == TaskState.Completed)
                    {
                        taskEngine.TaskStateChanged -= taskStateChanged;

                        Coordinate[] newCoords = map.FindDataOfType((int)KnownVoxelTypes.Eater, playerId);
                        Assert.AreEqual(targetCoordinate, newCoords[0]);

                        EndTest();
                    }
                    else
                    {
                        Assert.AreEqual(TaskState.Active, taskStateInfo.State);
                    }
                };
                taskEngine.TaskStateChanged += taskStateChanged;

                TaskInfo taskInfo = new TaskInfo(moveCmd, playerId);
                taskInfo.RequiresClientSidePreprocessing = true;

                taskEngine.SubmitTask(taskInfo);
            });
            yield return(Run());
        }
        public IEnumerator TaskTest(
            int playerId,
            Func <TaskInputInfo, TaskInputInfo, TaskInfo> GetTestTaskInfo,
            bool shouldTaskBeFailed,
            Action <MapRoot> testStarted,
            Action <TaskInfo> rootTaskCompleted,
            Action <TaskInfo> childTaskCompleted = null,
            int unitNumber = 0,
            string testEnv = TestEnv2)
        {
            BeginTest(testEnv, 2, 0, () =>
            {
                MapRoot map = Dependencies.Map.Map;
                testStarted(map);

                IMatchEngineCli matchEngineCli = Dependencies.MatchEngine;
                Coordinate[] coords            = map.FindDataOfType((int)KnownVoxelTypes.Eater, playerId);
                VoxelData voxel              = map.Get(coords[unitNumber]);
                TaskInfo unitIndexTask       = TaskInfo.UnitOrAssetIndex(voxel.UnitOrAssetIndex);
                TaskInfo playerIndexTask     = TaskInfo.EvalExpression(ExpressionInfo.PrimitiveVal(playerId));
                TaskInputInfo unitIndexInput = new TaskInputInfo
                {
                    OutputIndex = 0,
                    OutputTask  = unitIndexTask
                };
                TaskInputInfo playerIndexInput = new TaskInputInfo
                {
                    OutputIndex = 0,
                    OutputTask  = playerIndexTask
                };

                TaskInfo testTaskInfo = GetTestTaskInfo(unitIndexInput, playerIndexInput);
                TaskInfo rootTask     = TaskInfo.Procedure(
                    playerIndexTask,
                    unitIndexTask,
                    testTaskInfo,
                    TaskInfo.Return(ExpressionInfo.TaskStatus(testTaskInfo)));
                rootTask.SetParents();
                rootTask.Initialize(playerId);

                ITaskEngine taskEngine = matchEngineCli.GetClientTaskEngine(playerId);
                TaskEngineEvent <TaskInfo> taskStateChanged = null;
                taskStateChanged = taskInfo =>
                {
                    if (taskInfo.State == TaskState.Completed)
                    {
                        if (taskInfo.TaskId == rootTask.TaskId)
                        {
                            Assert.AreEqual(shouldTaskBeFailed, taskInfo.IsFailed, taskInfo.ToString());
                            taskEngine.TaskStateChanged -= taskStateChanged;
                            rootTaskCompleted(taskInfo);
                            EndTest();
                        }
                        else
                        {
                            if (childTaskCompleted != null)
                            {
                                childTaskCompleted(taskInfo);
                            }
                        }
                    }
                    else if (taskInfo.State != TaskState.Idle)
                    {
                        Assert.AreEqual(TaskState.Active, taskInfo.State, taskInfo.ToString());
                    }
                };
                taskEngine.TaskStateChanged += taskStateChanged;
                taskEngine.SubmitTask(rootTask);
            });


            yield return(Run());
        }
        public IEnumerator SearchForFoodTaskTest(int playerId, Action <ITaskEngine, TaskInfo, TaskInfo, Coordinate[]> callback)
        {
            BeginTest(TestEnv1, 2, 0, () =>
            {
                MapRoot map = Dependencies.Map.Map;
                IMatchEngineCli matchEngineCli = Dependencies.MatchEngine;

                Coordinate[] coords = map.FindDataOfType((int)KnownVoxelTypes.Eater, playerId);
                VoxelData voxel     = map.Get(coords[0]);

                TaskInfo searchForFoodTask = new TaskInfo(TaskType.SearchForFood)
                {
                    OutputsCount = 2
                };
                TaskInputInfo searchForFoodContext = new TaskInputInfo
                {
                    OutputIndex = 0,
                    OutputTask  = searchForFoodTask,
                };

                TaskInfo getUnitIndexTask = new TaskInfo(TaskType.EvalExpression)
                {
                    Expression   = ExpressionInfo.PrimitiveVal(voxel.UnitOrAssetIndex),
                    OutputsCount = 1
                };
                TaskInputInfo unitIndex = new TaskInputInfo
                {
                    OutputIndex = 0,
                    OutputTask  = getUnitIndexTask
                };

                searchForFoodTask.Inputs = new[] { searchForFoodContext, unitIndex };
                TaskInfo rootTask        = new TaskInfo(TaskType.Sequence)
                {
                    Children = new[] { getUnitIndexTask, searchForFoodTask }
                };

                rootTask.SetParents();
                rootTask.Initialize(playerId);

                ITaskEngine taskEngine = matchEngineCli.GetClientTaskEngine(playerId);
                TaskEngineEvent <TaskInfo> taskStateChanged = null;
                taskStateChanged = taskInfo =>
                {
                    if (taskInfo.State == TaskState.Completed)
                    {
                        if (taskInfo.TaskId == searchForFoodTask.TaskId)
                        {
                            callback(taskEngine, taskInfo, searchForFoodTask, coords);
                        }
                        else if (taskInfo.TaskId == rootTask.TaskId)
                        {
                            taskEngine.TaskStateChanged -= taskStateChanged;

                            EndTest();
                        }
                    }
                    else
                    {
                        Assert.AreEqual(TaskState.Active, taskInfo.State);
                    }
                };
                taskEngine.TaskStateChanged += taskStateChanged;
                taskEngine.SubmitTask(rootTask);
            });

            yield return(Run());
        }