public virtual void Think(float time, IBotSubmitTask bot) { Dictionary <long, IMatchUnitAssetView> freeEaters = m_state.FreeUnits[KnownVoxelTypes.Eater]; if (freeEaters.Count > 0) { List <IMatchUnitAssetView> selectedUnits = null; foreach (IMatchUnitAssetView freeEater in freeEaters.Values) { if (freeEater.Assignment != null) { if (selectedUnits == null) { selectedUnits = new List <IMatchUnitAssetView>(); } selectedUnits.Add(freeEater); } } if (selectedUnits != null) { for (int i = 0; i < selectedUnits.Count; ++i) { IMatchUnitAssetView unit = selectedUnits[i]; bot.SubmitTask(time, unit, unit.Assignment.TaskLaunchInfo.Type, unit.Assignment.TaskLaunchInfo.DeserializedParameters); } } } }
private void RemoveUnitFromAssignment(IMatchUnitAssetView unit) { Assignment assignment = unit.Assignment; if (assignment == null) { return; } unit.Assignment = null; assignment.HasUnit = false; assignment.UnitId = 0; if (!assignment.HasTarget) { AssignmentGroup group; if (m_groupIdToAssignments.TryGetValue(assignment.GroupId, out group)) { group.Assignments.Remove(assignment); if (group.Assignments.Count == 0) { m_groupIdToAssignments.Remove(assignment.GroupId); } } } }
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); }
private void RemoveAsset(VoxelData data) { if (data.IsNeutral) { return; } long id; if (m_voxelDataToId.TryGetValue(data, out id)) { m_voxelDataToId.Remove(data); IMatchUnitAssetView asset = m_idToAsset[id]; m_idToAsset.Remove(id); if (asset.Assignment != null) { AssignmentsController.RemoveAssignment(asset, null); } if (asset.TargetForAssignments != null) { AssignmentsController.RemoveTargetFromAssignments(asset, null); } if (AssetRemoved != null) { AssetRemoved(asset); } } }
protected override void OnConstruct() { SearchAroundContext ctx = ReadInput <SearchAroundContext>(m_taskInfo.Inputs[0]); long unitIndex = ReadInput <long>(m_taskInfo.Inputs[1]); m_unit = m_taskEngine.MatchEngine.GetPlayerView(m_taskInfo.PlayerIndex).GetUnit(unitIndex); if (m_unit == null) { m_taskInfo.StatusCode = TaskInfo.TaskFailed; m_taskInfo.State = TaskState.Completed; } else { m_dataController = m_unit.DataController.Clone(); int mapSize = m_dataController.Map.GetMapSizeWith(m_unit.Data.Weight); if (ctx == null) { ctx = new SearchAroundContext(m_unit.Position, mapSize, m_unit.Data.Weight, StartRadius, GetMaxRadius(m_unit)); } m_taskEngine.TaskRunner.Run(unitIndex, -1, ctx, FindSuitableData, FindSuitableDataCompleted, (id, context) => { if (m_taskInfo.State == TaskState.Active) { Debug.Assert(false, "Task Runner should not be interrupted when task is in active state"); m_taskInfo.StatusCode = TaskInfo.TaskFailed; m_taskInfo.State = TaskState.Completed; } }); } }
public RunningTaskInfo(TaskTemplateType type, IMatchUnitAssetView target, long taskId, float startTime) { Type = type; Target = target; TaskId = taskId; StartTime = startTime; }
public void CreateAssignment(Guid groupId, long unitId, SerializedTaskLaunchInfo taskLaunchInfo, bool hasTarget = false, int targetPlayerIndex = -1, long targetId = 0) { IMatchPlayerView targetPlayerView = null; IMatchUnitAssetView targetUnitOrAsset = null; if (hasTarget) { targetPlayerView = m_players[targetPlayerIndex]; targetUnitOrAsset = targetPlayerView.GetUnitOrAsset(targetId); if (targetUnitOrAsset == null || !targetUnitOrAsset.IsAlive) { hasTarget = false; targetId = 0; targetPlayerIndex = -1; } } IMatchUnitAssetView unit = m_players[m_playerIndex].GetUnitOrAsset(unitId); Assignment assignment = new Assignment { GroupId = groupId, UnitId = unitId, HasUnit = true, TaskLaunchInfo = taskLaunchInfo, TargetPlayerIndex = targetPlayerIndex, TargetId = targetId, HasTarget = hasTarget }; if (unit.Assignment != null) { RemoveUnitFromAssignment(unit); } unit.Assignment = assignment; if (hasTarget) { if (targetUnitOrAsset.TargetForAssignments == null) { targetUnitOrAsset.TargetForAssignments = new List <Assignment>(); } targetUnitOrAsset.TargetForAssignments.Add(assignment); } AssignmentGroup group; if (!m_groupIdToAssignments.TryGetValue(groupId, out group)) { group = new AssignmentGroup { GroupId = groupId, Assignments = new List <Assignment>() }; m_groupIdToAssignments.Add(groupId, group); } group.Assignments.Add(assignment); }
public void RemoveAssignment(IMatchUnitAssetView unit, Action <VoxelData> dieCallback) { Assignment assignment = unit.Assignment; if (assignment == null) { return; } RemoveAssignment(assignment, dieCallback); }
private void OnUnitCreated(IMatchUnitAssetView unit) { Dictionary <long, IMatchUnitAssetView> freeUnits; if (m_state.FreeUnits.TryGetValue((KnownVoxelTypes)unit.Data.Type, out freeUnits)) { freeUnits.Add(unit.Id, unit); } m_strategy.OnUnitCreated(unit); }
protected override void OnConstruct() { long unitIndex = ReadInput <long>(m_taskInfo.Inputs[0]); Coordinate[] waypoints = ReadInput <Coordinate[]>(m_taskInfo.Inputs[1]); m_unit = m_taskEngine.MatchEngine.GetPlayerView(m_taskInfo.PlayerIndex).GetUnit(unitIndex); if (m_unit == null || waypoints == null || waypoints.Length == 0) { #if !SERVER UnityEngine.Debug.LogWarning("no waypoints task failed"); #endif m_taskInfo.StatusCode = TaskInfo.TaskFailed; m_taskInfo.State = TaskState.Completed; } else { if (waypoints.Length == 1) { waypoints = new[] { m_unit.DataController.Coordinate, waypoints[0] }; } m_taskEngine.PathFinder.Find(unitIndex, -1, m_unit.DataController.Clone(), waypoints, (id, path) => { if (path[path.Length - 1] == waypoints[waypoints.Length - 1]) { WriteOutput(0, path); if (m_taskInfo.State != TaskState.Active) { throw new InvalidOperationException("m_taskInfo.State shoud be equal to Active but its " + m_taskInfo.State); } m_taskInfo.State = TaskState.Completed; } else { WriteOutput(0, path); if (m_taskInfo.State != TaskState.Active) { throw new InvalidOperationException("m_taskInfo.State shoud be equal to Active but its " + m_taskInfo.State); } m_taskInfo.StatusCode = TaskInfo.TaskFailed; m_taskInfo.State = TaskState.Completed; } }, id => { if (m_taskInfo.State == TaskState.Active) { UnityEngine.Debug.LogWarning("Path finding should not be termiated when task is in active state" + m_taskInfo.ToString()); //throw new InvalidOperationException(); m_taskInfo.StatusCode = TaskInfo.TaskFailed; m_taskInfo.State = TaskState.Completed; } }); } }
private void RemoveAssignment(Assignment assignment, Action <VoxelData> dieCallback) { if (assignment.HasUnit) { IMatchPlayerView playerView = m_players[m_playerIndex]; IMatchUnitAssetView unitOrAsset = playerView.GetUnitOrAsset(assignment.UnitId); if (unitOrAsset != null) { unitOrAsset.Assignment = null; } } assignment.HasUnit = false; assignment.UnitId = 0; if (assignment.HasTarget) { IMatchPlayerView targetPlayerView = m_players[assignment.TargetPlayerIndex]; IMatchUnitAssetView targetUnitOrAsset = targetPlayerView.GetUnitOrAsset(assignment.TargetId); if (targetUnitOrAsset != null && targetUnitOrAsset.TargetForAssignments != null) { targetUnitOrAsset.TargetForAssignments.Remove(assignment); if (targetUnitOrAsset.TargetForAssignments.Count == 0) { targetUnitOrAsset.TargetForAssignments = null; //destroy preview here if (VoxelData.IsPreview(targetUnitOrAsset.Data.Type)) { targetUnitOrAsset.DataController.SetHealth(0); if (dieCallback != null) { dieCallback(targetUnitOrAsset.Data); } } } } assignment.HasTarget = false; assignment.TargetId = 0; assignment.TargetPlayerIndex = -1; } AssignmentGroup group; if (m_groupIdToAssignments.TryGetValue(assignment.GroupId, out group)) { group.Assignments.Remove(assignment); if (group.Assignments.Count == 0) { m_groupIdToAssignments.Remove(assignment.GroupId); } } }
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); }
protected override void OnConstruct() { long unitIndex = ReadInput <long>(m_taskInfo.Inputs[0]); TaskInputInfo firstInput = m_taskInfo.Inputs[1]; int radius = ReadInput(firstInput, 10); m_unit = m_taskEngine.MatchEngine.GetPlayerView(m_taskInfo.PlayerIndex).GetUnit(unitIndex); if (m_unit == null) { m_taskInfo.StatusCode = TaskInfo.TaskFailed; m_taskInfo.State = TaskState.Completed; return; } Coordinate coordinate = m_unit.DataController.Coordinate; int deltaCol; int deltaRow; do { deltaCol = m_random.Next(0, radius + 1); deltaRow = m_random.Next(0, radius + 1); }while (deltaCol == 0 && deltaRow == 0); coordinate.Col = (m_random.Next() % 2 == 0) ? coordinate.Col + deltaCol : coordinate.Col - deltaCol; coordinate.Row = (m_random.Next() % 2 == 0) ? coordinate.Row + deltaRow : coordinate.Row - deltaRow; m_taskEngine.Memory.WriteOutput( firstInput.Scope.TaskId, firstInput.OutputTask.TaskId, firstInput.OutputIndex, new[] { m_unit.DataController.Coordinate, coordinate }); base.OnConstruct(); m_taskEngine.Memory.WriteOutput( firstInput.Scope.TaskId, firstInput.OutputTask.TaskId, firstInput.OutputIndex, radius); }
protected override void OnConstruct() { if (InputsCount > 1) { ReadFirstInput(); } if (InputsCount > 0) { long unitIndex = ReadInput <long>(m_taskInfo.Inputs[0]); m_taskInfo.Cmd.UnitIndex = unitIndex; } if (m_unit != null) { return; } IMatchPlayerView playerView = m_taskEngine.MatchEngine.GetPlayerView(m_taskInfo.PlayerIndex); if (playerView == null) { m_taskInfo.State = TaskState.Completed; m_taskInfo.StatusCode = TaskInfo.TaskFailed; return; } m_unit = playerView.GetUnit(m_taskInfo.Cmd.UnitIndex); if (m_unit == null) { m_taskInfo.State = TaskState.Completed; m_taskInfo.StatusCode = TaskInfo.TaskFailed; return; } else { if (!m_taskEngine.IsClient) { m_unit.CmdExecuted += OnCmdExecuted; } SubmitCommand(); } }
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); }
protected override void OnEvaluating(ExpressionInfo expression, ITaskEngine taskEngine, Action <object> callback) { ExpressionInfo left = expression.Children[0]; ExpressionInfo right = expression.Children[1]; taskEngine.GetExpression(left.Code).Evaluate(left, taskEngine, first => { taskEngine.GetExpression(right.Code).Evaluate(right, taskEngine, second => { long unitId = (long)first; int playerId = (int)second; IMatchPlayerView player = taskEngine.MatchEngine.GetPlayerView(playerId); IMatchUnitAssetView unit = player.GetUnitOrAsset(unitId); OnEvaluating(expression, player, unit, taskEngine, callback); }); }); }
public override void Think(float time, IBotSubmitTask bot) { base.Think(time, bot); Dictionary <long, IMatchUnitAssetView> freeEaters = m_state.FreeUnits[KnownVoxelTypes.Eater]; if (freeEaters.Count > 0) { List <IMatchUnitAssetView> selectedUnits = new List <IMatchUnitAssetView>(); foreach (IMatchUnitAssetView freeEater in freeEaters.Values) { selectedUnits.Add(freeEater); } for (int i = 0; i < selectedUnits.Count; ++i) { IMatchUnitAssetView unit = selectedUnits[i]; bot.SubmitTask(time, unit, TaskTemplateType.EatGrowSplit4); } } }
public void RemoveTargetFromAssignments(IMatchUnitAssetView unitOrAsset, Action <VoxelData> dieCallback) { List <Assignment> targetForAssignments = unitOrAsset.TargetForAssignments; if (targetForAssignments == null) { return; } unitOrAsset.TargetForAssignments = null; //destroy preview here if (VoxelData.IsPreview(unitOrAsset.Data.Type)) { unitOrAsset.DataController.SetHealth(0); if (dieCallback != null) { dieCallback(unitOrAsset.Data); } } for (int i = 0; i < targetForAssignments.Count; ++i) { Assignment assignment = targetForAssignments[i]; assignment.HasTarget = false; assignment.TargetId = 0; assignment.TargetPlayerIndex = -1; if (!assignment.HasUnit) { AssignmentGroup group; if (m_groupIdToAssignments.TryGetValue(assignment.GroupId, out group)) { group.Assignments.Remove(assignment); if (group.Assignments.Count == 0) { m_groupIdToAssignments.Remove(assignment.GroupId); } } } } }
private void OnTaskStateChanged(TaskInfo taskInfo) { RunningTaskInfo completedTask; if (m_state.TaskIdToTask.TryGetValue(taskInfo.TaskId, out completedTask)) { if (taskInfo.State != TaskState.Active) { IMatchUnitAssetView unit = completedTask.Target; Dictionary <long, IMatchUnitAssetView> busyUnits; if (m_state.BusyUnits.TryGetValue((KnownVoxelTypes)unit.Data.Type, out busyUnits)) { if (busyUnits.ContainsKey(unit.Id)) { busyUnits.Remove(unit.Id); m_state.FreeUnits[(KnownVoxelTypes)unit.Data.Type].Add(unit.Id, unit); } } TaskInfoPool pool; if (m_state.TaskTemplates.TryGetValue(completedTask.Type, out pool)) { if (taskInfo.Children != null) { for (int i = 0; i < taskInfo.Children.Length - 2; ++i) { taskInfo.Children[i] = null; } } pool.Release(taskInfo); } m_state.TaskIdToTask.Remove(taskInfo.TaskId); m_state.UnitIdToTask.Remove(unit.Id); m_strategy.OnTaskCompleted(unit, completedTask, taskInfo); } } }
protected override void OnEvaluating(ExpressionInfo expression, IMatchPlayerView player, IMatchUnitAssetView unit, ITaskEngine taskEngine, Action <object> callback) { ExpressionInfo target = expression.Children[2]; taskEngine.GetExpression(target.Code).Evaluate(target, taskEngine, targetType => { CmdResultCode can = unit.DataController.CanConvertImmediate((int)targetType); callback(can); }); }
protected override void OnEvaluating(ExpressionInfo expression, IMatchPlayerView player, IMatchUnitAssetView unit, ITaskEngine taskEngine, Action <object> callback) { CmdResultCode can = unit.DataController.CanSplit4Immediate(); callback(can); }
protected override void OnEvaluating(ExpressionInfo expression, IMatchPlayerView player, IMatchUnitAssetView unit, ITaskEngine taskEngine, Action <object> callback) { VoxelDataState state = unit.DataController.GetVoxelDataState(); callback(state); }
protected override void OnEvaluating(ExpressionInfo expression, IMatchPlayerView player, IMatchUnitAssetView unit, ITaskEngine taskEngine, Action <object> callback) { callback(unit.DataController.Coordinate); }
protected override void OnEvaluating(ExpressionInfo expression, IMatchPlayerView player, IMatchUnitAssetView unit, ITaskEngine taskEngine, Action <object> callback) { callback(unit != null); }
protected abstract void OnEvaluating(ExpressionInfo expression, IMatchPlayerView player, IMatchUnitAssetView unit, ITaskEngine taskEngine, Action <object> callback);
public virtual void OnAssetRemoved(IMatchUnitAssetView asset) { }
public virtual void OnUnitCreated(IMatchUnitAssetView unit) { }
public virtual void OnUnitRemoved(IMatchUnitAssetView unit, RunningTaskInfo activeTask, TaskInfo 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"); }
private void OnAssetRemoved(IMatchUnitAssetView asset) { m_strategy.OnAssetRemoved(asset); }