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);
        }
Example #2
0
        /// <summary>
        ///     Executes this command.
        /// </summary>
        public override int Execute(LogicLevel level)
        {
            if (this._trapData != null)
            {
                if (this._trapData.GetVillageType() == level.GetVillageType())
                {
                    if (level.IsValidPlaceForBuilding(this._x, this._y, this._trapData.GetWidth(), this._trapData.GetHeight(), null))
                    {
                        LogicClientAvatar playerAvatar      = level.GetPlayerAvatar();
                        LogicResourceData buildResourceData = this._trapData.GetBuildResource();

                        int buildCost = this._trapData.GetBuildCost(0);

                        if (playerAvatar.HasEnoughResources(buildResourceData, buildCost, true, this, false) && !level.IsTrapCapReached(this._trapData, true))
                        {
                            if (buildResourceData.PremiumCurrency)
                            {
                                playerAvatar.UseDiamonds(buildCost);
                            }
                            else
                            {
                                playerAvatar.CommodityCountChangeHelper(0, buildResourceData, -buildCost);
                            }

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

                            if (this._trapData.GetBuildTime(0) == 0)
                            {
                                trap.FinishConstruction(false);
                            }

                            trap.SetInitialPosition(this._x << 9, this._y << 9);
                            level.GetGameObjectManager().AddGameObject(trap, -1);

                            if (level.IsTrapCapReached(this._trapData, false))
                            {
                                level.GetGameListener().TrapCapReached(this._trapData);
                            }

                            if (trap.GetVillageType() != 0)
                            {
                                int x      = trap.GetTileX();
                                int y      = trap.GetTileY();
                                int width  = trap.GetWidthInTiles();
                                int height = trap.GetHeightInTiles();

                                for (int i = 0; i < width; i++)
                                {
                                    for (int j = 0; j < height; j++)
                                    {
                                        LogicObstacle tallGrass = level.GetTileMap().GetTile(x + i, y + j).GetTallGrass();

                                        if (tallGrass != null)
                                        {
                                            level.GetGameObjectManager().RemoveGameObject(tallGrass);
                                        }
                                    }
                                }
                            }

                            return(0);
                        }
                    }

                    return(-1);
                }

                return(-32);
            }

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