protected virtual void OnAddAssignment(Cmd cmd) { CreateAssignmentCmd addCmd = (CreateAssignmentCmd)cmd; if (addCmd.CreatePreview) { CmdResultCode noFail = m_dataController.CreatePreview(addCmd.PreviewType, addCmd.PreviewCoordinate); if (noFail != CmdResultCode.Success) { throw new InvalidOperationException(); } Coordinate coordinate = addCmd.PreviewCoordinate; //Voxel cloneActor = null; if (m_voxelMap.IsVisible(coordinate.MapPos, coordinate.Weight)) { Debug.Assert(m_controlledVoxel.Owner == addCmd.TargetPlayerIndex); VoxelData voxelData = m_voxelMap.Map.GetPreview(addCmd.TargetPlayerIndex, coordinate); AcquireVoxel(voxelData, coordinate.MapPos, coordinate.Weight); } } }
private List <long> PostprocessCommand(List <long> spawnedUnitsList, Cmd cmd, IMatchUnitControllerCli unitController) { if (cmd.Code == CmdCode.Split || cmd.Code == CmdCode.Split4) { CoordinateCmd splitCmd = (CoordinateCmd)cmd; for (int i = 0; i < splitCmd.Coordinates.Length; ++i) { Coordinate coordinate = splitCmd.Coordinates[i]; VoxelData voxelData = m_voxelMap.Map.Get(coordinate); if (voxelData != null) { if (spawnedUnitsList == null) { spawnedUnitsList = new List <long>(); } spawnedUnitsList.Add(m_identity); CreateUnitController(voxelData, coordinate); } } } else if (cmd.Code == CmdCode.Convert) { Coordinate coordinate = unitController.DataController.Coordinate; MapCell cell = m_voxelMap.Map.Get(coordinate.Row, coordinate.Col, coordinate.Weight); VoxelData voxelData = cell.GetVoxelDataAt(coordinate.Altitude); if (voxelData != null) { if (VoxelData.IsUnit(voxelData.Type)) { if (spawnedUnitsList == null) { spawnedUnitsList = new List <long>(); } spawnedUnitsList.Add(m_identity); CreateUnitController(voxelData, coordinate); } else { CreateAsset(voxelData, m_allAbilities[voxelData.Owner][voxelData.Type], cell); } } } else if (cmd.Code == CmdCode.CreateAssignment) { CreateAssignmentCmd addCmd = (CreateAssignmentCmd)cmd; if (addCmd.CreatePreview) { Coordinate coord = addCmd.PreviewCoordinate; MapCell cell = m_voxelMap.Map.Get(coord.Row, coord.Col, coord.Weight); VoxelData voxelData = cell.GetPreviewAt(m_playerIndex, coord.Altitude); Debug.Assert(voxelData != null); if (spawnedUnitsList == null) { spawnedUnitsList = new List <long>(); } spawnedUnitsList.Add(m_identity); CreateUnitController(voxelData, coord); } AssignmentsController.CreateAssignment(addCmd.GroupId, addCmd.UnitIndex, addCmd.TaskLaunchInfo, addCmd.HasTarget, addCmd.TargetPlayerIndex, addCmd.TargetId); } return(spawnedUnitsList); }
private bool PostprocessCmd(bool unitsChanged, IMatchUnitController unitController, Cmd cmd) { if (cmd.IsFailed) { return(unitsChanged); } if (cmd.Code == CmdCode.Split || cmd.Code == CmdCode.Split4) { CoordinateCmd splitCmd = (CoordinateCmd)cmd; for (int c = 0; c < splitCmd.Coordinates.Length; ++c) { Coordinate coordinate = splitCmd.Coordinates[c]; VoxelData voxelData = m_engine.Map.Get(coordinate); if (voxelData != null) { CreateUnitController(voxelData, coordinate); unitsChanged = true; } } } else if (cmd.Code == CmdCode.Convert) { Coordinate coord = unitController.DataController.Coordinate; MapCell cell = m_engine.Map.Get(coord.Row, coord.Col, coord.Weight); VoxelData voxelData = cell.GetVoxelDataAt(coord.Altitude); if (voxelData != null) { if (VoxelData.IsUnit(voxelData.Type)) { CreateUnitController(voxelData, unitController.DataController.Coordinate); unitsChanged = true; } else { CreateAsset(voxelData, cell); } } } else if (cmd.Code == CmdCode.CreateAssignment) { CreateAssignmentCmd addCmd = (CreateAssignmentCmd)cmd; if (addCmd.CreatePreview) { Coordinate coord = addCmd.PreviewCoordinate; MapCell cell = m_engine.Map.Get(coord.Row, coord.Col, coord.Weight); VoxelData voxelData = cell.GetPreviewAt(m_playerIndex, coord.Altitude); Debug.Assert(voxelData != null); CreateUnitController(voxelData, coord); addCmd.HasTarget = true; addCmd.TargetId = voxelData.UnitOrAssetIndex; addCmd.TargetPlayerIndex = voxelData.Owner; } m_assignmentController.CreateAssignment(addCmd.GroupId, addCmd.UnitIndex, addCmd.TaskLaunchInfo, addCmd.HasTarget, addCmd.TargetPlayerIndex, addCmd.TargetId); } return(unitsChanged); }
protected override Cmd OnTick(long tick) //Tick should be able return several commands { if (State == VoxelDataState.Moving) { if (m_commandsQueue.Count > 0 && !m_dataController.IsCollapsedOrBlocked) { Cmd cmd = m_commandsQueue.Peek(); m_ticksBeforeNextCommand = cmd.Duration; bool dequeue = true; switch (cmd.Code) { case CmdCode.Move: { cmd = HandleNextMoveCmd(cmd); if (cmd == null) { m_failedMoveAttempts++; m_failedMoveAttempts %= (m_maxFailedMoveAttempts + 1); } else { m_failedMoveAttempts = 0; } dequeue = cmd != null; //if null then wait a little bit and try again break; } case CmdCode.RotateLeft: { m_dataController.RotateLeft(); break; } case CmdCode.RotateRight: { m_dataController.RotateRight(); break; } default: { cmd = HandleNextCmd(cmd); dequeue = cmd != null; //if null then wait a little bit and try again break; } } if (dequeue && m_commandsQueue.Count > 0) { m_commandsQueue.Dequeue(); } if (m_commandsQueue.Count == 0) { RaiseCmdExecuted(); } return(cmd); } if (m_commandsQueue.Count == 0) { RaiseCmdExecuted(); } return(null); } else if ((State & VoxelDataState.Busy) == VoxelDataState.Busy) { if (m_commandsQueue.Count > 0) { Cmd cmd = m_commandsQueue.Dequeue(); m_ticksBeforeNextCommand = cmd.Duration; switch (cmd.Code) { case CmdCode.BeginSplit: case CmdCode.BeginSplit4: case CmdCode.BeginGrow: case CmdCode.BeginDiminish: case CmdCode.BeginConvert: case CmdCode.BeginSetHealth: { m_dataController.ControlledData.Unit.MutationStartTick = tick; return(cmd); } case CmdCode.Split: { CoordinateCmd coordinateCmd = new CoordinateCmd(cmd.Code, cmd.UnitIndex, cmd.Duration); Coordinate[] coordinates; CmdResultCode result = m_dataController.Split(out coordinates, EatOrDestroyCallback); if (result == CmdResultCode.Success) { coordinateCmd.Coordinates = coordinates; RaiseCmdExecuted(); } else { RaiseCmdFailed(coordinateCmd, result); } return(coordinateCmd); } case CmdCode.Split4: { CoordinateCmd coordinateCmd = new CoordinateCmd(cmd.Code, cmd.UnitIndex, cmd.Duration); Coordinate[] coordinates; CmdResultCode result = m_dataController.Split4(out coordinates); if (result == CmdResultCode.Success) { coordinateCmd.Coordinates = coordinates; RaiseCmdExecuted(); } else { RaiseCmdFailed(coordinateCmd, result); } return(coordinateCmd); } case CmdCode.Grow: { CmdResultCode result = m_dataController.Grow(EatOrDestroyCallback); if (result == CmdResultCode.Success) { RaiseCmdExecuted(); } else { RaiseCmdFailed(cmd, result); } return(cmd); } case CmdCode.Diminish: { CmdResultCode result = m_dataController.Diminish(); if (result == CmdResultCode.Success) { RaiseCmdExecuted(); } else { RaiseCmdFailed(cmd, result); } return(cmd); } case CmdCode.Convert: { ChangeParamsCmd convertCmd = (ChangeParamsCmd)cmd; int type = convertCmd.IntParams[0]; CmdResultCode result = m_dataController.Convert(type); if (result == CmdResultCode.Success) { RaiseCmdExecuted(); } else { RaiseCmdFailed(cmd, result); } return(cmd); } case CmdCode.SetHealth: { ChangeParamsCmd changeCmd = (ChangeParamsCmd)cmd; int health = changeCmd.IntParams[0]; m_dataController.SetHealth(health); RaiseCmdExecuted(); return(changeCmd); } case CmdCode.CreateAssignment: { CreateAssignmentCmd addCmd = (CreateAssignmentCmd)cmd; if (addCmd.CreatePreview) { CmdResultCode result = m_dataController.CreatePreview(addCmd.PreviewType, addCmd.PreviewCoordinate); if (result == CmdResultCode.Success) { RaiseCmdExecuted(); } else { RaiseCmdFailed(cmd, result); } } else { RaiseCmdExecuted(); } return(cmd); } case CmdCode.Cancel: { IMatchPlayerController playerController = m_engine.GetPlayerController(m_dataController.PlayerIndex); playerController.AssignmentsController.RemoveAssignment(this, null); RaiseCmdExecuted(); return(cmd); } } } } return(null); }