예제 #1
0
 public CoordinateCmd(CoordinateCmd cmd) : base(cmd)
 {
     if (cmd.Coordinates != null)
     {
         Coordinates = new Coordinate[cmd.Coordinates.Length];
         for (int i = 0; i < Coordinates.Length; ++i)
         {
             Coordinates[i] = cmd.Coordinates[i];
         }
     }
 }
예제 #2
0
        protected void OnMoveCmd(Cmd cmd)
        {
            CoordinateCmd coordinateCmd = (CoordinateCmd)cmd;

            Coordinate[] path = coordinateCmd.Coordinates;
            if (!ValidatePath(path))
            {
                OnMoveSearchPath(cmd);
                return;
            }

            Coordinate closestCoordinate;
            int        coordIndex = Array.IndexOf(path, m_dataController.Coordinate);

            if (coordIndex > -1)  //data control is on path
            {
                path = path.Skip(coordIndex).ToArray();
                if (path.Length > 1)
                {
                    State = VoxelDataState.Moving;
                    PopulateCommandsQueue(Id, path, false, CmdCode.Move);
                }
            }
            else if (DataController.Coordinate.FindClosestTo(path, out closestCoordinate))
            {
                State = VoxelDataState.SearchingPath;
                //find path segment connection current unity coordinate with path found on client
                m_pathFinder.Find(Id, -1, m_dataController.Clone(),
                                  new[] { m_dataController.Coordinate, closestCoordinate }, (unitIndex, foundPath) =>
                {
                    if (Coordinate.CanMergePath(foundPath, path))
                    {
                        State = VoxelDataState.Moving;
                        PopulateCommandsQueue(Id, Coordinate.MergePath(foundPath, path), false, CmdCode.Move);
                    }
                    else
                    {
                        RaiseCmdFailed(cmd, CmdResultCode.Fail_NotFound);
                    }
                },
                                  null);
            }
            else
            {
                RaiseCmdFailed(cmd, CmdResultCode.Fail_NotFound);
            }
        }
        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);
        }
예제 #5
0
        private void ProcessRequest(ClientRequest request, Action <ClientRequest> callback)
        {
            Cmd cmd = request.Cmd;

            if (cmd != null)
            {
                if (cmd.Code == CmdCode.GrantBotCtrl)
                {
                    m_bots = new IBotController[m_game.PlayersCount];
                    for (int i = 0; i < m_bots.Length; ++i)
                    {
                        Player player = m_game.GetPlayer(i);
                        if (player.IsActiveBot)
                        {
                            IBotController bot = MatchFactoryCli.CreateBotController(player, m_taskEngines[i], new DefaultStrategy());
                            bot.Init();
                            m_bots[i] = bot;
                        }
                    }
                    callback(request);
                }
                else if (cmd.Code == CmdCode.DenyBotCtrl)
                {
                    m_bots = new IBotController[m_game.PlayersCount];
                    for (int i = 0; i < m_bots.Length; ++i)
                    {
                        IBotController bot = m_bots[i];
                        if (bot != null)
                        {
                            bot.Reset();
                        }
                    }
                    m_bots = null;
                    callback(request);
                }
                else
                {
                    IVoxelDataController dc = m_game.GetVoxelDataController(request.PlayerIndex, cmd.UnitIndex);
                    if (cmd.Code != CmdCode.Move || dc == null)
                    {
                        if (dc == null)
                        {
                            request.Cmd.ErrorCode = CmdResultCode.Fail_NoUnit;
                        }
                        else
                        {
                            request.Cmd.ErrorCode = CmdResultCode.Fail_NotSupported;
                        }

                        SubmitResponse(request);
                    }
                    else
                    {
                        CoordinateCmd coordinateCmd = (CoordinateCmd)cmd;
                        Debug.Assert(coordinateCmd.Coordinates.Length > 1);
                        IPathFinder pathFinder = m_pathFinders[request.PlayerIndex];
#warning PathFinder should igore dataController.ControlledVoxelData
                        pathFinder.Find(cmd.UnitIndex, -1, dc.Clone(), coordinateCmd.Coordinates, (unitIndex, path) =>
                        {
                            coordinateCmd.Coordinates = path;
                            request.Cmd = coordinateCmd;
                            callback(request);
                        }, null);
                    }
                }
            }
        }
        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);
        }
예제 #7
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);
        }