public static void SetBuildingUpgradeLevel(LogicLevel level, int gameObjectId, int upgLevel, int villageType)
        {
            LogicGameObject gameObject = level.GetGameObjectManagerAt(villageType).GetGameObjectByID(gameObjectId);

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

                    if (building.GetGearLevel() > 0 || building.IsGearing())
                    {
                        if (buildingData.GetMinUpgradeLevelForGearUp() > upgLevel)
                        {
                            Debugger.HudPrint("Can't downgrade geared up building below gear up limit!");
                            upgLevel = buildingData.GetMinUpgradeLevelForGearUp();
                        }
                    }

                    if (buildingData.IsTownHall())
                    {
                        level.GetPlayerAvatar().SetTownHallLevel(upgLevel);
                    }

                    building.SetUpgradeLevel(LogicMath.Max(upgLevel - 1, 0));
                    building.FinishConstruction(false, true);
                    building.SetUpgradeLevel(upgLevel);

                    if (building.GetListener() != null)
                    {
                        building.GetListener().RefreshState();
                    }
                    if (buildingData.IsTownHall() || buildingData.IsTownHallVillage2())
                    {
                        level.RefreshNewShopUnlocks(buildingData.GetVillageType());
                    }
                }
                else if (gameObject.GetGameObjectType() == LogicGameObjectType.TRAP)
                {
                    LogicTrap trap = (LogicTrap)gameObject;

                    trap.SetUpgradeLevel(LogicMath.Max(upgLevel - 1, 0));
                    trap.FinishConstruction(false);
                    trap.SetUpgradeLevel(upgLevel);
                    trap.RepairTrap();

                    if (trap.GetListener() != null)
                    {
                        trap.GetListener().RefreshState();
                    }
                }
                else if (gameObject.GetGameObjectType() == LogicGameObjectType.VILLAGE_OBJECT)
                {
                    LogicVillageObject villageObject = (LogicVillageObject)gameObject;

                    villageObject.SetUpgradeLevel(LogicMath.Max(upgLevel - 1, 0));
                    villageObject.SetUpgradeLevel(upgLevel);

                    if (villageObject.GetListener() != null)
                    {
                        villageObject.GetListener().RefreshState();
                    }
                }
            }
        }
        public override int Execute(LogicLevel level)
        {
            if (this.m_gameObjectData != null && level.GetUnplacedObjectCount(this.m_gameObjectData) > 0)
            {
                if (level.GetVillageType() == this.m_gameObjectData.GetVillageType())
                {
                    LogicDataType dataType = this.m_gameObjectData.GetDataType();

                    if (dataType == LogicDataType.BUILDING)
                    {
                        LogicBuildingData buildingData = (LogicBuildingData)this.m_gameObjectData;

                        if (level.IsValidPlaceForBuilding(this.m_x, this.m_y, buildingData.GetWidth(), buildingData.GetHeight(), null))
                        {
                            if (!level.RemoveUnplacedObject(this.m_gameObjectData, this.m_upgradeLevel))
                            {
                                return(-63);
                            }

                            LogicBuilding building = (LogicBuilding)LogicGameObjectFactory.CreateGameObject(this.m_gameObjectData, level, level.GetVillageType());

                            building.SetPositionXY(this.m_x << 9, this.m_y << 9);
                            level.GetGameObjectManager().AddGameObject(building, -1);
                            building.FinishConstruction(false, true);
                            building.SetUpgradeLevel(this.m_upgradeLevel);
                        }

                        return(0);
                    }

                    if (dataType == LogicDataType.TRAP)
                    {
                        LogicTrapData trapData = (LogicTrapData)this.m_gameObjectData;

                        if (level.IsValidPlaceForBuilding(this.m_x, this.m_y, trapData.GetWidth(), trapData.GetHeight(), null))
                        {
                            if (!level.RemoveUnplacedObject(this.m_gameObjectData, this.m_upgradeLevel))
                            {
                                return(-64);
                            }

                            LogicTrap trap = (LogicTrap)LogicGameObjectFactory.CreateGameObject(this.m_gameObjectData, level, level.GetVillageType());

                            trap.SetPositionXY(this.m_x << 9, this.m_y << 9);
                            trap.FinishConstruction(false);
                            trap.SetUpgradeLevel(this.m_upgradeLevel);
                            level.GetGameObjectManager().AddGameObject(trap, -1);
                        }

                        return(0);
                    }

                    if (dataType == LogicDataType.DECO)
                    {
                        LogicDecoData decoData = (LogicDecoData)this.m_gameObjectData;

                        if (level.IsValidPlaceForBuilding(this.m_x, this.m_y, decoData.GetWidth(), decoData.GetHeight(), null))
                        {
                            if (!level.RemoveUnplacedObject(this.m_gameObjectData, this.m_upgradeLevel))
                            {
                                return(-65);
                            }

                            LogicDeco deco = (LogicDeco)LogicGameObjectFactory.CreateGameObject(this.m_gameObjectData, level, level.GetVillageType());

                            deco.SetPositionXY(this.m_x << 9, this.m_y << 9);
                            level.RemoveUnplacedObject(this.m_gameObjectData, this.m_upgradeLevel);
                            level.GetGameObjectManager().AddGameObject(deco, -1);
                        }

                        return(0);
                    }

                    return(-3);
                }

                return(-35);
            }

            return(0);
        }
        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);
        }