Esempio n. 1
0
        public static TaskInfo HealMoveToCoordGrow(Coordinate[] path, TaskInputInfo unitIndexInput, TaskInputInfo playerIndexInput)
        {
            TaskInfo taskInfo = TaskInfo.Procedure
                                (
                TaskInfo.SetHealth(unitIndexInput, 64),
                MoveToCoordinate(path, unitIndexInput, playerIndexInput),
                TaskInfo.Grow(unitIndexInput),
                TaskInfo.Return(ExpressionInfo.PrimitiveVal(TaskInfo.TaskSucceded))
                                );

            taskInfo.Inputs = new TaskInputInfo[] { unitIndexInput, playerIndexInput };
            return(taskInfo);
        }
        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);
        }
Esempio n. 3
0
        public void ExpreessionInfoCloneTest2()
        {
            ExpressionInfo canRun = ExpressionInfo.Eq(
                ExpressionInfo.PrimitiveVal(CmdResultCode.Success),
                ExpressionInfo.Val(new TaskInputInfo()));

            Assert.DoesNotThrow(() =>
            {
                TaskInfo task = new TaskInfo {
                    Expression = canRun
                };
                byte[] b = m_protobufSerializer.Serialize(SerializedTask.FromTaskInfo(task));
                SerializedTask.ToTaskInfo(m_protobufSerializer.Deserialize <SerializedTask>(b));
            });
        }
        public static TaskInfo SearchAndMoveAnyway(Coordinate[] waypoints, TaskInputInfo unitIndexInput, TaskInputInfo playerIndexInput)
        {
            TaskInfo defineGoalTask = TaskInfo.EvalExpression(ExpressionInfo.Val(waypoints));
            TaskInfo findPathTask   = TaskInfo.FindPath(unitIndexInput, new TaskInputInfo(defineGoalTask, 0));
            TaskInfo taskInfo       = TaskInfo.Procedure
                                      (
                defineGoalTask,
                findPathTask,
                TaskInfo.Move(unitIndexInput, new TaskInputInfo(findPathTask, 0)),
                TaskInfo.Return(ExpressionInfo.PrimitiveVal(TaskInfo.TaskSucceded))
                                      );

            taskInfo.Inputs = new TaskInputInfo[] { unitIndexInput, playerIndexInput };
            return(taskInfo);
        }
        public void RepeatProcedureReturnTest()
        {
            Assert.DoesNotThrow(() =>
            {
                BeginTest(TestEnv0, 4);
            });


            TaskInfo procedure = TaskInfo.Procedure(
                TaskInfo.Repeat(
                    ExpressionInfo.PrimitiveVal(true),
                    new TaskInfo(TaskType.TEST_Mock),
                    new TaskInfo(TaskType.TEST_MockImmediate),
                    new TaskInfo(TaskType.TEST_MockImmediate),
                    TaskInfo.Return(ExpressionInfo.PrimitiveVal(TaskInfo.TaskSucceded)),
                    new TaskInfo(TaskType.TEST_Fail)
                    )
                );

            TaskInfo root = TaskInfo.Sequence
                            (
                procedure,
                TaskInfo.Branch(
                    ExpressionInfo.TaskSucceded(procedure),
                    new TaskInfo(TaskType.Nop),
                    new TaskInfo(TaskType.TEST_Fail)
                    )
                            );

            root.SetParents();

            const int playerId = 1;

            BeginCleanupCheck(playerId);

            FinializeTest(playerId, root, result =>
            {
                Assert.AreEqual(root.TaskId, result.TaskId);
                Assert.AreEqual(TaskState.Completed, result.State);
                Assert.IsFalse(result.IsFailed);
                CleanupCheck(playerId);

                Assert.Pass();
            },
                          childTask =>
            {
            });
        }
Esempio n. 6
0
        public void SearchForPathCloneTest2()
        {
            TaskInfo searchForTask = TaskInfo.SearchFor(TaskType.SearchForFood, new TaskInputInfo());

            ExpressionInfo searchForSucceded = ExpressionInfo.TaskSucceded(searchForTask);

            TaskInputInfo  coordinateInput  = new TaskInputInfo(searchForTask, 1);
            TaskInfo       findPathTask     = TaskInfo.FindPath(new TaskInputInfo(), coordinateInput);
            ExpressionInfo findPathSucceded = ExpressionInfo.TaskSucceded(findPathTask);

            TaskInputInfo  pathVariableInput  = new TaskInputInfo(findPathTask, 0);
            ExpressionInfo assignPathVariable = ExpressionInfo.Assign(
                TaskInfo.Var(),
                ExpressionInfo.Val(pathVariableInput));

            ExpressionInfo whileTrue = ExpressionInfo.PrimitiveVal(true);

            TaskInfo task =
                TaskInfo.Procedure(
                    TaskInfo.Repeat(
                        whileTrue,
                        searchForTask,
                        TaskInfo.Branch(
                            searchForSucceded,
                            TaskInfo.Sequence(
                                findPathTask,
                                TaskInfo.Branch(
                                    findPathSucceded,
                                    TaskInfo.Sequence(
                                        TaskInfo.EvalExpression(assignPathVariable),
                                        TaskInfo.Return()
                                        ),
                                    TaskInfo.Continue()
                                    )
                                ),


                            TaskInfo.Return(ExpressionInfo.PrimitiveVal(TaskInfo.TaskFailed))
                            )
                        )
                    );

            Assert.DoesNotThrow(() =>
            {
                SerializedTask.ToTaskInfo(m_protobufSerializer.DeepClone(SerializedTask.FromTaskInfo(task)));
            });
        }
        public void ContinueInSequenceTest()
        {
            Assert.DoesNotThrow(() =>
            {
                BeginTest(TestEnv0, 4);
            });

            const int playerId = 2;

            ExpressionInfo expr = ExpressionInfo.PrimitiveVal(true);
            TaskInfo       task = TaskInfo.Repeat(expr);

            task.Children = new[]
            {
                new TaskInfo(TaskType.TEST_Mock),
                new TaskInfo(TaskType.TEST_Mock),
                TaskInfo.Sequence
                (
                    new TaskInfo(TaskType.TEST_MockImmediate),
                    TaskInfo.Assert((tb, ti) =>
                {
                    expr       = TaskInfo.FindById(task.TaskId, ti.Root).Expression;
                    expr.Value = PrimitiveContract.Create(false);
                    return(TaskState.Completed);
                }),
                    TaskInfo.Continue(),
                    new TaskInfo(TaskType.TEST_Fail)
                ),
                new TaskInfo(TaskType.TEST_MockImmediate),
                new TaskInfo(TaskType.TEST_MockImmediate)
            };


            BeginCleanupCheck(playerId);
            FinializeTest(playerId, task, result =>
            {
                Assert.AreEqual(TaskState.Completed, result.State);
                Assert.IsFalse(result.IsFailed);
                CleanupCheck(playerId);
                Assert.Pass();
            });
        }
        private TaskInfo PathToRandomLocation(TaskInputInfo unitIndexInput, TaskInputInfo playerInput)
        {
            int           radius      = 3;
            TaskInfo      radiusVar   = TaskInfo.EvalExpression(ExpressionInfo.PrimitiveVal(radius));
            TaskInputInfo radiusInput = new TaskInputInfo
            {
                OutputIndex = 0,
                OutputTask  = radiusVar
            };

            TaskInfo      pathToRandomLocation = TaskInfo.PathToRandomLocation(unitIndexInput, radiusInput);
            TaskInputInfo pathInput            = new TaskInputInfo
            {
                OutputIndex = 0,
                OutputTask  = pathToRandomLocation
            };

            TaskInfo assert = TaskInfo.Assert((taskBase, taskInfo) =>
            {
                Coordinate[] path = taskBase.ReadInput <Coordinate[]>(taskInfo.Inputs[0]);
                Assert.IsNotNull(path);
                Assert.IsTrue(path.Length > 1);
                Coordinate first = path[0];
                Coordinate last  = path[path.Length - 1];
                Assert.LessOrEqual(Mathf.Abs(first.Row - last.Row), 3);
                Assert.LessOrEqual(Mathf.Abs(first.Col - last.Col), 3);
                return(TaskState.Completed);
            });

            assert.Inputs = new[] { pathInput };

            return(TaskInfo.Sequence(
                       radiusVar,
                       pathToRandomLocation,
                       TaskInfo.Branch(
                           ExpressionInfo.TaskSucceded(pathToRandomLocation),
                           assert,
                           new TaskInfo(TaskType.TEST_Fail)
                           )
                       ));
        }
        private TaskInfo SearchForTestTargetAndMove(TaskInputInfo unitIndexInput, TaskInputInfo playerIndexInput)
        {
            TaskInfo       pathVar          = TaskInfo.Var();
            TaskInfo       searhForPathTask = TaskInfo.SearchForPath(TaskType.TEST_SearchForWall, pathVar, unitIndexInput);
            ExpressionInfo pathFound        = ExpressionInfo.TaskSucceded(searhForPathTask);
            TaskInputInfo  pathInput        = new TaskInputInfo(pathVar, 0);
            TaskInfo       moveTask         = TaskInfo.Move(unitIndexInput, pathInput);

            return(TaskInfo.Procedure(
                       pathVar,
                       TaskInfo.Log("search for path started"),
                       searhForPathTask,
                       TaskInfo.Log(
                           ExpressionInfo.Add(
                               ExpressionInfo.PrimitiveVal("path found ? "),
                               pathFound)
                           ),
                       moveTask,
                       TaskInfo.Return(ExpressionInfo.TaskStatus(moveTask))
                       ));
        }
Esempio n. 10
0
        public void ProcedureRepeatBranchSearchCloneTest()
        {
            ExpressionInfo whileTrue     = ExpressionInfo.PrimitiveVal(true);
            TaskInfo       searchForTask = TaskInfo.SearchFor(TaskType.SearchForFood, new TaskInputInfo());
            TaskInfo       task          =
                TaskInfo.Procedure(
                    TaskInfo.Repeat(
                        whileTrue,
                        searchForTask,
                        TaskInfo.Branch(
                            whileTrue,
                            new TaskInfo()
                            )
                        )
                    );

            Assert.DoesNotThrow(() =>
            {
                SerializedTask.ToTaskInfo(m_protobufSerializer.DeepClone(SerializedTask.FromTaskInfo(task)));
            });
        }
Esempio n. 11
0
        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 void IterateRepeatTest()
        {
            Assert.DoesNotThrow(() =>
            {
                BeginTest(TestEnv0, 4);
            });

            List <int> integers = new List <int>()
            {
                1, 2, 3, 4, 5
            };
            TaskInfo iterateTask = new TaskInfo
            {
                TaskType     = TaskType.EvalExpression,
                Expression   = ExpressionInfo.Iterate(integers),
                OutputsCount = 1,
            };

            TaskInputInfo input = new TaskInputInfo
            {
                OutputIndex = 0,
                OutputTask  = iterateTask,
            };

            ExpressionInfo isTrue = ExpressionInfo.Eq(
                ExpressionInfo.Val(PrimitiveContract.Create(true)),
                ExpressionInfo.Get(
                    ExpressionInfo.Val(input),
                    ExpressionInfo.PrimitiveVal("IsLast")));

            TaskInfo breakIfCompleted = new TaskInfo
            {
                TaskType   = TaskType.Branch,
                Expression = isTrue,
                Children   = new[]
                {
                    new TaskInfo
                    {
                        TaskType = TaskType.Break,
                    },
                    null
                }
            };

            TaskInfo repeat = new TaskInfo
            {
                TaskType   = TaskType.Repeat,
                Expression = ExpressionInfo.PrimitiveVal(true),
                Children   = new[]
                {
                    iterateTask,
                    breakIfCompleted,
                }
            };

            repeat.SetParents();
            input.SetScope();

            const int playerId   = 1;
            bool      isIterated = false;
            int       counter    = 0;

            BeginCleanupCheck(playerId);

            FinializeTest(playerId, repeat, result =>
            {
                Assert.IsTrue(isIterated);

                Assert.AreEqual(TaskState.Completed, result.State);
                Assert.IsFalse(result.IsFailed);
                CleanupCheck(playerId);

                Assert.Pass();
            },
                          childTask =>
            {
                if (childTask.TaskId == iterateTask.TaskId && childTask.State == TaskState.Completed)
                {
                    counter++;
                    ITaskMemory memory         = m_engine.GetTaskEngine(playerId).Memory;
                    IterationResult iterResult = (IterationResult)memory.ReadOutput(iterateTask.Parent.TaskId, iterateTask.TaskId, 0);
                    if (iterResult.IsLast)
                    {
                        isIterated = true;
                        Assert.IsNull(iterResult.Object);
                    }
                    else
                    {
                        Assert.AreEqual(counter, (int)iterResult.Object);
                    }
                }
            });
        }
        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());
        }
        public IEnumerator SearchMoveOrRandomMoveLimitedRandomSearchesFailTest()
        {
            yield return(TaskTest(1, (unitIndexInput, playerId) =>
            {
                const int maxRandLocationPicks = 10;
                const int maxRandMovements = 0;
                TaskInfo randomMovementsVar = TaskInfo.Var();
                TaskInputInfo randomMovementsInput = new TaskInputInfo(randomMovementsVar, 0);

                TaskInfo searchMoveOrRandomMove = TaskInfo.SearchMoveOrRandomMove(TaskType.SearchForFood, unitIndexInput, maxRandLocationPicks, randomMovementsInput, maxRandMovements);
                return TaskInfo.Procedure(
                    randomMovementsVar,
                    TaskInfo.EvalExpression(ExpressionInfo.Assign(randomMovementsVar, ExpressionInfo.PrimitiveVal(0))),
                    searchMoveOrRandomMove,
                    TaskInfo.Return(ExpressionInfo.TaskStatus(searchMoveOrRandomMove))
                    );
            }
                                  , true,
                                  rootTaskInfo =>
            {
                Assert.AreEqual(TaskState.Completed, rootTaskInfo.State);
                Assert.IsTrue(rootTaskInfo.IsFailed);
            }));
        }