Ejemplo n.º 1
0
        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);
        }