protected virtual void OnConvert(Cmd cmd) { ChangeParamsCmd changeParams = (ChangeParamsCmd)cmd; int type = changeParams.IntParams[0]; bool isVisible = m_controlledVoxel != null; if (isVisible) { m_controlledVoxel.Kill(); } CmdResultCode noFail = m_dataController.Convert(type, DieCallback); if (noFail != CmdResultCode.Success) { throw new InvalidOperationException(); } if (isVisible) { VoxelData voxelData = m_voxelMap.Map.Get(m_dataController.Coordinate); MapPos mapPos = m_dataController.Coordinate.MapPos; int weight = m_dataController.Coordinate.Weight; AcquireVoxel(voxelData, mapPos, weight); } }
protected virtual void OnSetHealth(Cmd cmd) { ChangeParamsCmd changeCmd = (ChangeParamsCmd)cmd; m_dataController.SetHealth(changeCmd.IntParams[0], DieCallback); if (m_controlledVoxel != null) { m_controlledVoxel.Health = changeCmd.IntParams[0]; } }
protected virtual void OnStateChanged(Cmd cmd) { ChangeParamsCmd changeParamsCmd = (ChangeParamsCmd)cmd; VoxelDataState prevState = m_dataController.GetVoxelDataState(); CmdResultCode noFail = m_dataController.SetVoxelDataState((VoxelDataState)changeParamsCmd.IntParams[1]); if (noFail != CmdResultCode.Success) { throw new InvalidOperationException(); } if (m_controlledVoxel != null) { m_controlledVoxel.OnStateChanged(prevState, m_dataController.GetVoxelDataState()); } }
public ChangeParamsCmd(ChangeParamsCmd cmd) : base(cmd) { if (cmd.IntParams != null) { IntParams = new int[cmd.IntParams.Length]; for (int i = 0; i < cmd.IntParams.Length; ++i) { IntParams[i] = cmd.IntParams[i]; } } if (cmd.FloatParams != null) { FloatParams = new float[cmd.FloatParams.Length]; for (int i = 0; i < cmd.FloatParams.Length; ++i) { FloatParams[i] = cmd.FloatParams[i]; } } }
public void SubmitConsoleCommand(PlayerUnitConsoleCmd cmd, string[] args, IConsole console) { int playerIndex = m_gameState.LocalToPlayerIndex(m_localPlayerIndex); long[] selectedUnits = m_unitSelection.GetSelection(playerIndex, playerIndex); List <Cmd> commandsToSubmit = new List <Cmd>(); if (cmd == PlayerUnitConsoleCmd.Move) { int row; int col; int weight; int altitude; if (args.Length < 4 || !int.TryParse(args[0], out row) || !int.TryParse(args[1], out col) || !int.TryParse(args[2], out weight) || !int.TryParse(args[3], out altitude)) { console.GetChild(LocalPlayerIndex).Echo("Move <unitId> <row> <col> <weight> <altitude>"); return; } for (int i = 0; i < selectedUnits.Length; ++i) { long selectedUnitIndex = selectedUnits[i]; CoordinateCmd coordCmd = new CoordinateCmd(); coordCmd.Code = CmdCode.Move; coordCmd.Coordinates = new[] { new Coordinate(row, col, weight, altitude) }; coordCmd.UnitIndex = selectedUnitIndex; commandsToSubmit.Add(coordCmd); } } else if (cmd == PlayerUnitConsoleCmd.Split) { CreateStdCommand(selectedUnits, commandsToSubmit, CmdCode.Split); } else if (cmd == PlayerUnitConsoleCmd.Grow) { CreateStdCommand(selectedUnits, commandsToSubmit, CmdCode.Grow); } else if (cmd == PlayerUnitConsoleCmd.Diminish) { CreateStdCommand(selectedUnits, commandsToSubmit, CmdCode.Diminish); } else if (cmd == PlayerUnitConsoleCmd.Convert) { KnownVoxelTypes voxelType; if (args.Length < 1 || !(args[0].TryParse(true, out voxelType))) { console.GetChild(LocalPlayerIndex).Echo("Convert KnowVoxelType"); return; } CreateStdCommand(selectedUnits, commandsToSubmit, CmdCode.Convert, () => new ChangeParamsCmd() { IntParams = new[] { (int)voxelType } }); } else if (cmd == PlayerUnitConsoleCmd.Heal) { int health = -1; if (args.Length < 1 || !int.TryParse(args[0], out health)) { console.GetChild(LocalPlayerIndex).Echo("SetHealth <health>"); return; } for (int i = 0; i < selectedUnits.Length; ++i) { long selectedUnitIndex = selectedUnits[i]; ChangeParamsCmd command = new ChangeParamsCmd { Code = CmdCode.SetHealth, UnitIndex = selectedUnitIndex, IntParams = new int[] { health } }; commandsToSubmit.Add(command); } } SubmitToEngine(playerIndex, commandsToSubmit); }
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); }
public void Tick(long tick, out Cmd cmd) { if (m_createdVoxels.Count != 0) { m_createdVoxels.Clear(); } if (m_eatenOrDestroyedVoxels.Count != 0) { m_eatenOrDestroyedVoxels.Clear(); } if (!m_dataController.IsAlive) { if (State != VoxelDataState.Idle) { RaiseCmdFailed(null, CmdResultCode.Fail_NoUnit); } else { GoToIdleState(); } cmd = null; return; } if (m_ticksBeforeNextCommand == 0) { cmd = OnTick(tick); if (State != m_prevState) { if (cmd != null) { cmd = new CompositeCmd { UnitIndex = cmd.UnitIndex, Duration = cmd.Duration, Commands = new[] { cmd, new ChangeParamsCmd(CmdCode.StateChanged) { UnitIndex = cmd.UnitIndex, Duration = cmd.Duration, IntParams = new[] { (int)m_prevState, (int)State } } } }; } else { cmd = new ChangeParamsCmd(CmdCode.StateChanged) { UnitIndex = Id, IntParams = new[] { (int)m_prevState, (int)State } }; } CmdResultCode noFail = m_dataController.SetVoxelDataState(State); if (noFail != CmdResultCode.Success) { throw new InvalidOperationException(""); } m_prevState = State; } return; } else { m_ticksBeforeNextCommand--; } cmd = null; return; }