public TaskInfo SubmitAssignment(float time, Guid groupId, IMatchUnitAssetView unit, int previewType, Coordinate previewCoordinate, TaskTemplateType type, params TaskInfo[] defines)
        {
            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 = TaskInfo.Command(new CreateAssignmentCmd(unit.Id)
            {
                CreatePreview     = true,
                PreviewType       = previewType,
                PreviewCoordinate = previewCoordinate,
                GroupId           = groupId,
                TaskLaunchInfo    = new SerializedTaskLaunchInfo
                {
                    Type = type,
                    DeserializedParameters = defines
                },
            });

            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(TaskTemplateType.None, unit, taskInfo.TaskId, time);

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

            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);
        }
        private void OnUnitRemoved(IMatchUnitAssetView unit)
        {
            bool freeUnitRemoved = false;
            Dictionary <long, IMatchUnitAssetView> freeUnits;

            if (m_state.FreeUnits.TryGetValue((KnownVoxelTypes)unit.Data.Type, out freeUnits))
            {
                if (freeUnits.ContainsKey(unit.Id))
                {
                    freeUnits.Remove(unit.Id);
                    freeUnitRemoved = true;
                }
            }

            Dictionary <long, IMatchUnitAssetView> busyUnits;

            if (m_state.BusyUnits.TryGetValue((KnownVoxelTypes)unit.Data.Type, out busyUnits))
            {
                if (busyUnits.ContainsKey(unit.Id))
                {
                    if (freeUnitRemoved)
                    {
                        m_log.LogWarningFormat("unit {0} appears to be in both collections free and busy !?", unit.Id);
                    }

                    busyUnits.Remove(unit.Id);
                }
            }

            RunningTaskInfo activeTask = null;
            TaskInfo        taskInfo   = null;

            if (m_state.UnitIdToTask.TryGetValue(unit.Id, out activeTask))
            {
                //int taskId = activeTask.TaskId;
                //taskInfo = m_taskEngine.TerminateTask(taskId);
                //m_state.TaskIdToTask.Remove(activeTask.TaskId);
                //m_state.UnitIdToTask.Remove(unit.Id);
                //m_strategy.OnTaskCompleted(unit, activeTask, taskInfo);
            }

            m_strategy.OnUnitRemoved(unit, activeTask, taskInfo);
        }
 public override void OnTaskCompleted(IMatchUnitAssetView unit, RunningTaskInfo completedTask, TaskInfo taskInfo)
 {
     m_logger.LogFormat("Unit {0} task {1} {2} with status {3}", unit.Id, completedTask.TaskId, taskInfo.State, taskInfo.IsFailed ? "failed" : "succeded");
 }
 public virtual void OnUnitRemoved(IMatchUnitAssetView unit, RunningTaskInfo activeTask, TaskInfo taskInfo)
 {
 }