/// <summary>
        ///     Executes this command.
        /// </summary>
        public override int Execute(LogicLevel level)
        {
            if (this._gameObjectIds.Count > 0)
            {
                LogicResourceData buildResourceData = null;
                int buildCost = 0;

                for (int i = 0; i < this._gameObjectIds.Count; i++)
                {
                    LogicGameObject gameObject = level.GetGameObjectManager().GetGameObjectByID(this._gameObjectIds[i]);

                    if (gameObject != null && gameObject.GetGameObjectType() == 0)
                    {
                        LogicBuilding     building     = (LogicBuilding)gameObject;
                        LogicBuildingData buildingData = building.GetBuildingData();

                        int upgradeLevel = building.GetUpgradeLevel();

                        if (buildingData.IsTownHallVillage2())
                        {
                            return(-76);
                        }

                        if (building.CanUpgrade(false) && buildingData.GetUpgradeLevelCount() > upgradeLevel + 1 && buildingData.GetAmountCanBeUpgraded(0) == 0)
                        {
                            buildResourceData = buildingData.GetBuildResource(upgradeLevel + 1);

                            if (this._useAltResources)
                            {
                                buildResourceData = buildingData.GetAltBuildResource(upgradeLevel + 1);
                            }

                            buildCost += buildingData.GetBuildCost(upgradeLevel + 1, level);
                        }
                    }
                }

                if (buildResourceData != null)
                {
                    LogicClientAvatar playerAvatar = level.GetPlayerAvatar();

                    if (playerAvatar.HasEnoughResources(buildResourceData, buildCost, true, this, false))
                    {
                        if (level.HasFreeWorkers(this, -1))
                        {
                            bool ignoreState = true;

                            for (int i = 0; i < this._gameObjectIds.Count; i++)
                            {
                                LogicGameObject gameObject = level.GetGameObjectManager().GetGameObjectByID(this._gameObjectIds[i]);

                                if (gameObject != null && gameObject.GetGameObjectType() == 0)
                                {
                                    LogicBuilding     building     = (LogicBuilding)gameObject;
                                    LogicBuildingData buildingData = building.GetBuildingData();

                                    int upgradeLevel = building.GetUpgradeLevel();

                                    if (building.CanUpgrade(false) && buildingData.GetUpgradeLevelCount() > upgradeLevel + 1 && buildingData.GetAmountCanBeUpgraded(0) == 0)
                                    {
                                        if (this._gameObjectIds.Count > 6)
                                        {
                                            ignoreState = (building.GetTileX() + building.GetTileY()) % (this._gameObjectIds.Count / 4) == 0;
                                        }

                                        building.StartUpgrading(ignoreState, false);
                                    }
                                }
                            }

                            playerAvatar.CommodityCountChangeHelper(0, buildResourceData, -buildCost);

                            return(0);
                        }
                    }
                }
            }

            return(-2);
        }
Пример #2
0
        public override int Execute(LogicLevel level)
        {
            if (this.m_layoutId != -1)
            {
                if (level.GetTownHallLevel(level.GetVillageType()) >= level.GetRequiredTownHallLevelForLayout(this.m_layoutId, -1))
                {
                    if ((this.m_layoutId & 0xFFFFFFFE) != 6)
                    {
                        if (this.m_layoutType != 0)
                        {
                            if (this.m_layoutId != 1 && this.m_layoutId != 4 && this.m_layoutId != 5)
                            {
                                return(-5);
                            }
                        }
                        else
                        {
                            if (this.m_layoutId != 0 && this.m_layoutId != 2 && this.m_layoutId != 3)
                            {
                                return(-4);
                            }
                        }
                    }

                    LogicGameObjectFilter            filter      = new LogicGameObjectFilter();
                    LogicArrayList <LogicGameObject> gameObjects = new LogicArrayList <LogicGameObject>(500);

                    filter.AddGameObjectType(LogicGameObjectType.BUILDING);
                    filter.AddGameObjectType(LogicGameObjectType.TRAP);
                    filter.AddGameObjectType(LogicGameObjectType.DECO);

                    level.GetGameObjectManager().GetGameObjects(gameObjects, filter);

                    for (int i = 0; i < gameObjects.Size(); i++)
                    {
                        LogicGameObject gameObject = gameObjects[i];
                        LogicVector2    position   = gameObject.GetPositionLayout(this.m_layoutId, false);

                        if ((this.m_layoutId & 0xFFFFFFFE) != 6 && (position.m_x == -1 || position.m_y == -1))
                        {
                            return(-2);
                        }
                    }

                    LogicGameObjectManager           gameObjectManager = level.GetGameObjectManager();
                    LogicArrayList <LogicGameObject> buildings         = gameObjectManager.GetGameObjects(LogicGameObjectType.BUILDING);
                    LogicArrayList <LogicGameObject> obstacles         = gameObjectManager.GetGameObjects(LogicGameObjectType.OBSTACLE);

                    for (int i = 0; i < gameObjects.Size(); i++)
                    {
                        LogicGameObject gameObject = gameObjects[i];
                        LogicVector2    position   = gameObject.GetPositionLayout(this.m_layoutId, false);

                        int minX = position.m_x;
                        int minY = position.m_y;
                        int maxX = minX + gameObject.GetWidthInTiles();
                        int maxY = minY + gameObject.GetHeightInTiles();

                        for (int j = 0; j < obstacles.Size(); j++)
                        {
                            LogicObstacle obstacle = (LogicObstacle)obstacles[j];

                            int minX2 = obstacle.GetTileX();
                            int minY2 = obstacle.GetTileY();
                            int maxX2 = minX2 + obstacle.GetWidthInTiles();
                            int maxY2 = minY2 + obstacle.GetHeightInTiles();

                            if (maxX > minX2 && maxY > minY2 && minX < maxX2 && minY < maxY2)
                            {
                                if ((this.m_layoutId & 0xFFFFFFFE) != 6)
                                {
                                    return(-2);
                                }

                                gameObjectManager.RemoveGameObject(obstacle);
                                j -= 1;
                            }
                        }
                    }

                    for (int i = 0; i < buildings.Size(); i++)
                    {
                        LogicBuilding baseWallBlock = (LogicBuilding)buildings[i];

                        if (baseWallBlock.GetWallIndex() != 0)
                        {
                            int x             = baseWallBlock.GetTileX();
                            int y             = baseWallBlock.GetTileY();
                            int minX          = 0;
                            int minY          = 0;
                            int maxX          = 0;
                            int maxY          = 0;
                            int minWallBlockX = 0;
                            int minWallBlockY = 0;
                            int maxWallBlockX = 0;
                            int maxWallBlockY = 0;
                            int wallBlockCnt  = 0;

                            for (int j = 0; j < buildings.Size(); j++)
                            {
                                LogicBuilding wallBlock = (LogicBuilding)buildings[j];

                                if (wallBlock.GetWallIndex() == baseWallBlock.GetWallIndex())
                                {
                                    int tmp1 = x - wallBlock.GetTileX();
                                    int tmp2 = y - wallBlock.GetTileY();

                                    minX = LogicMath.Min(minX, tmp1);
                                    minY = LogicMath.Min(minY, tmp2);
                                    maxX = LogicMath.Max(maxX, tmp1);
                                    maxY = LogicMath.Max(maxY, tmp2);

                                    int wallBlockX = wallBlock.GetBuildingData().GetWallBlockX(wallBlockCnt);
                                    int wallBlockY = wallBlock.GetBuildingData().GetWallBlockY(wallBlockCnt);

                                    minWallBlockX = LogicMath.Min(minWallBlockX, wallBlockX);
                                    minWallBlockY = LogicMath.Min(minWallBlockY, wallBlockY);
                                    maxWallBlockX = LogicMath.Max(maxWallBlockX, wallBlockX);
                                    maxWallBlockY = LogicMath.Max(maxWallBlockY, wallBlockY);

                                    ++wallBlockCnt;
                                }
                            }

                            if (baseWallBlock.GetBuildingData().GetWallBlockCount() != wallBlockCnt)
                            {
                                return(-24);
                            }

                            int wallBlockSizeX = maxWallBlockX - minWallBlockX;
                            int wallBlockSizeY = maxWallBlockY - minWallBlockY;
                            int lengthX        = maxX - minX;
                            int lengthY        = maxY - minY;

                            if (wallBlockSizeX != lengthX || wallBlockSizeY != lengthY)
                            {
                                if (wallBlockSizeX != lengthX != (wallBlockSizeY != lengthY))
                                {
                                    return(-25);
                                }
                            }
                        }
                    }

                    if (this.m_layoutType != 0)
                    {
                        if (level.IsWarBase())
                        {
                            level.SetWarBase(true);
                        }

                        level.SetActiveWarLayout(this.m_layoutId);
                    }
                    else
                    {
                        level.SetActiveLayout(this.m_layoutId, level.GetVillageType());

                        for (int i = 0; i < gameObjects.Size(); i++)
                        {
                            LogicGameObject gameObject = gameObjects[i];
                            LogicVector2    position   = gameObject.GetPositionLayout(this.m_layoutId, false);

                            gameObject.SetPositionXY(position.m_x << 9, position.m_y << 9);
                        }
                    }

                    return(0);
                }
            }

            return(-10);
        }
        public static LogicClientHome GenerateBase(LogicGameObjectData gameObjectData)
        {
            LogicClientHome        logicClientHome        = new LogicClientHome();
            LogicGameMode          logicGameMode          = new LogicGameMode();
            LogicLevel             logicLevel             = logicGameMode.GetLevel();
            LogicGameObjectManager logicGameObjectManager = logicLevel.GetGameObjectManagerAt(0);

            logicLevel.SetLoadingVillageType(-1);
            logicLevel.SetVillageType(0);
            logicLevel.SetExperienceVersion(1);
            logicLevel.SetHomeOwnerAvatar(GameBaseGenerator.HomeOwnerAvatar);

            LogicBuilding townHall = new LogicBuilding(LogicDataTables.GetTownHallData(), logicLevel, 0);

            townHall.SetInitialPosition((LogicLevel.TILEMAP_SIZE_X / 2 - townHall.GetWidthInTiles() / 2) << 9, (LogicLevel.TILEMAP_SIZE_Y / 2 - townHall.GetHeightInTiles() / 2) << 9);
            townHall.SetUpgradeLevel(townHall.GetBuildingData().GetUpgradeLevelCount() - 1);

            logicGameObjectManager.AddGameObject(townHall, -1);

            LogicTownhallLevelData townhallLevelData = LogicDataTables.GetTownHallLevel(townHall.GetUpgradeLevel());

            if (gameObjectData == null)
            {
                LogicArrayList <LogicGameObject> scrambler = new LogicArrayList <LogicGameObject>();

                for (int i = 0; i < GameBaseGenerator.m_defenseBuildingArray.Size(); i++)
                {
                    LogicBuildingData buildingData = GameBaseGenerator.m_defenseBuildingArray[i];

                    for (int j = townhallLevelData.GetUnlockedBuildingCount(buildingData); j > 0; j--)
                    {
                        LogicBuilding logicBuilding =
                            (LogicBuilding)GameBaseGenerator.CreateAndPlaceRandomlyGameObject(buildingData, logicLevel, buildingData.GetWidth(), buildingData.GetHeight(), 0);

                        if (logicBuilding != null)
                        {
                            logicBuilding.SetLocked(false);
                            logicBuilding.SetUpgradeLevel(buildingData.GetUpgradeLevelCount() - 1);
                            scrambler.Add(logicBuilding);

                            int upgradeLevel             = buildingData.GetUpgradeLevelCount() - 1;
                            int minUpgradeLevelForGearUp = buildingData.GetMinUpgradeLevelForGearUp();

                            if (minUpgradeLevelForGearUp > -1 && upgradeLevel >= minUpgradeLevelForGearUp)
                            {
                                if (GameBaseGenerator.m_random.Rand(100) >= 50)
                                {
                                    logicBuilding.SetGearLevel(1);
                                }
                            }

                            if (buildingData.GetAttackerItemData(upgradeLevel).GetTargetingConeAngle() != 0)
                            {
                                logicBuilding.GetCombatComponent().ToggleAimAngle(buildingData.GetAimRotateStep() * GameBaseGenerator.m_random.Rand(360 / buildingData.GetAimRotateStep()), 0, false);
                            }

                            if (buildingData.GetAttackerItemData(upgradeLevel).HasAlternativeAttackMode())
                            {
                                if (minUpgradeLevelForGearUp > -1 && logicBuilding.GetGearLevel() != 1)
                                {
                                    continue;
                                }
                                if (GameBaseGenerator.m_random.Rand(100) >= 50)
                                {
                                    logicBuilding.GetCombatComponent().ToggleAttackMode(0, false);
                                }
                            }
                        }
                    }
                }

                for (int i = 0; i < GameBaseGenerator.m_otherBuildingArray.Size(); i++)
                {
                    LogicBuildingData buildingData = GameBaseGenerator.m_otherBuildingArray[i];

                    for (int j = townhallLevelData.GetUnlockedBuildingCount(buildingData); j > 0; j--)
                    {
                        LogicBuilding logicBuilding =
                            (LogicBuilding)GameBaseGenerator.CreateAndPlaceRandomlyGameObject(buildingData, logicLevel, buildingData.GetWidth(), buildingData.GetHeight(), 0);

                        if (logicBuilding != null)
                        {
                            logicBuilding.SetLocked(false);
                            logicBuilding.SetUpgradeLevel(buildingData.GetUpgradeLevelCount() - 1);
                            scrambler.Add(logicBuilding);
                        }
                    }
                }

                for (int i = 0; i < GameBaseGenerator.m_trapArray.Size(); i++)
                {
                    LogicTrapData trapData = (LogicTrapData)GameBaseGenerator.m_trapArray[i];

                    for (int j = townhallLevelData.GetUnlockedTrapCount(trapData); j > 0; j--)
                    {
                        LogicTrap trap = (LogicTrap)GameBaseGenerator.CreateAndPlaceRandomlyGameObject(trapData, logicLevel, trapData.GetWidth(), trapData.GetHeight(), 0);

                        if (trap != null)
                        {
                            trap.SetUpgradeLevel(trapData.GetUpgradeLevelCount() - 1);
                            scrambler.Add(trap);
                        }
                    }
                }

                for (int i = 0; i < scrambler.Size(); i++)
                {
                    LogicGameObject gameObject = scrambler[i];
                    LogicData       data       = gameObject.GetData();

                    int width  = gameObject.GetWidthInTiles();
                    int height = gameObject.GetHeightInTiles();
                    int x      = gameObject.GetX();
                    int y      = gameObject.GetY();

                    LogicArrayList <LogicGameObject> available = new LogicArrayList <LogicGameObject>();

                    for (int j = i + 1; j < scrambler.Size(); j++)
                    {
                        if (data != scrambler[j].GetData())
                        {
                            if (scrambler[j].GetWidthInTiles() == width && scrambler[j].GetHeightInTiles() == height)
                            {
                                available.Add(scrambler[j]);
                            }
                        }
                    }

                    if (available.Size() != 0)
                    {
                        LogicGameObject swapObj = available[GameBaseGenerator.m_random.Rand(available.Size())];

                        gameObject.SetInitialPosition(swapObj.GetX(), swapObj.GetY());
                        swapObj.SetInitialPosition(x, y);
                    }
                }
            }
            else
            {
                int maxUpgradeLevel = 0;
                int width           = 1;
                int height          = 1;

                switch (gameObjectData.GetDataType())
                {
                case LogicDataType.BUILDING:
                {
                    LogicBuildingData logicBuildingData = (LogicBuildingData)gameObjectData;
                    maxUpgradeLevel = logicBuildingData.GetUpgradeLevelCount();
                    width           = logicBuildingData.GetWidth();
                    height          = logicBuildingData.GetHeight();
                    break;
                }

                case LogicDataType.TRAP:
                    LogicTrapData logicTrapData = (LogicTrapData)gameObjectData;
                    maxUpgradeLevel = logicTrapData.GetUpgradeLevelCount();
                    width           = logicTrapData.GetWidth();
                    height          = logicTrapData.GetHeight();
                    break;
                }

                int upgLevel = maxUpgradeLevel - 1;
                int x        = 0;
                int y        = 0;

                while (true)
                {
                    LogicBuilding building =
                        (LogicBuilding)GameBaseGenerator.CreateGameObjectIfAnyPlaceExist(gameObjectData, logicLevel, width, height, 0, x, y);

                    if (building == null)
                    {
                        break;
                    }

                    building.SetLocked(false);
                    building.SetUpgradeLevel(upgLevel != -1 ? upgLevel : GameBaseGenerator.m_random.Rand(building.GetUpgradeLevel()));
                    x = building.GetTileX();
                    y = building.GetTileY();
                }
            }

            for (int i = 0; i < 10; i++)
            {
                logicGameObjectManager.Village1CreateObstacle();
            }

            LogicJSONObject jsonObject = new LogicJSONObject();

            logicGameMode.SaveToJSON(jsonObject);
            logicGameMode.Destruct();

            logicClientHome.SetHomeJSON(LogicJSONParser.CreateJSONString(jsonObject, 2048));

            CompressibleStringHelper.Compress(logicClientHome.GetCompressibleHomeJSON());

            return(logicClientHome);
        }