Example #1
0
        public void StartUseTroopEvent(LogicAvatar homeOwnerAvatar, LogicLevel level)
        {
            if (homeOwnerAvatar != null)
            {
                for (int i = 0; i < this.m_useTroops.Size(); i++)
                {
                    LogicCalendarUseTroop calendarUseTroop = this.m_useTroops[i];
                    LogicCombatItemData   data             = calendarUseTroop.GetData();

                    int housingSpace;
                    int totalMaxHousing;
                    int unitCount;

                    if (data.GetCombatItemType() != LogicCombatItemData.COMBAT_ITEM_TYPE_CHARACTER)
                    {
                        housingSpace    = data.GetHousingSpace() * 2;
                        totalMaxHousing = data.GetHousingSpace() + 2 * (level.GetComponentManagerAt(data.GetVillageType()).GetTotalMaxHousing(data.GetCombatItemType()) *
                                                                        calendarUseTroop.GetParameter(1) / 100);
                        unitCount = totalMaxHousing / housingSpace;
                    }
                    else
                    {
                        LogicBuildingData troopHousingData      = LogicDataTables.GetBuildingByName("Troop Housing", null);
                        LogicBuildingData barrackData           = LogicDataTables.GetBuildingByName("Barrack", null);
                        LogicBuildingData darkElixirBarrackData = LogicDataTables.GetBuildingByName("Dark Elixir Barrack", null);

                        int townHallLevel        = homeOwnerAvatar.GetTownHallLevel();
                        int maxUpgradeLevelForTH = troopHousingData.GetMaxUpgradeLevelForTownHallLevel(townHallLevel);
                        int unitStorageCapacity  = troopHousingData.GetUnitStorageCapacity(maxUpgradeLevelForTH);

                        housingSpace = data.GetHousingSpace();

                        if (data.GetUnitOfType() == 1 && barrackData.GetRequiredTownHallLevel(data.GetRequiredProductionHouseLevel()) <= townHallLevel ||
                            data.GetUnitOfType() == 2 && darkElixirBarrackData.GetRequiredTownHallLevel(data.GetRequiredProductionHouseLevel()) <= townHallLevel)
                        {
                            int totalHousing = (int)((long)LogicDataTables.GetTownHallLevel(townHallLevel).GetUnlockedBuildingCount(troopHousingData) *
                                                     calendarUseTroop.GetParameter(1) *
                                                     unitStorageCapacity);
                            unitCount = (int)((housingSpace * 0.5f + totalHousing / 100) / housingSpace);
                        }
                        else
                        {
                            LogicBuildingData allianceCastleData = LogicDataTables.GetBuildingByName("Alliance Castle", null);

                            totalMaxHousing = allianceCastleData.GetUnitStorageCapacity(allianceCastleData.GetMaxUpgradeLevelForTownHallLevel(townHallLevel));
                            unitCount       = totalMaxHousing / housingSpace;
                        }
                    }

                    int eventCounter = LogicMath.Max(1, unitCount) << 16;

                    homeOwnerAvatar.SetCommodityCount(6, data, eventCounter);
                    homeOwnerAvatar.GetChangeListener().CommodityCountChanged(6, data, eventCounter);

                    Debugger.HudPrint("EVENT: Use troop/spell event started!");
                }
            }
        }
        public override int Execute(LogicLevel level)
        {
            this.m_level = level;

            if (level.GetVillageType() == 0)
            {
                if (LogicDataTables.GetGlobals().EnablePresets())
                {
                    LogicAvatar homeOwnerAvatar = level.GetHomeOwnerAvatar();

                    if (homeOwnerAvatar.GetTownHallLevel() >= LogicDataTables.GetGlobals().GetEnablePresetsTownHallLevel())
                    {
                        if (this.m_presetId <= 3)
                        {
                            if (this.HasEnoughFreeHousingSpace())
                            {
                                int elixirCost     = this.GetResourceCost(LogicDataTables.GetElixirData());
                                int darkElixirCost = this.GetResourceCost(LogicDataTables.GetDarkElixirData());

                                if (level.GetPlayerAvatar().HasEnoughResources(LogicDataTables.GetElixirData(), elixirCost, LogicDataTables.GetDarkElixirData(), darkElixirCost,
                                                                               true,
                                                                               this, false))
                                {
                                    this.TrainUnits();
                                    return(0);
                                }
                            }

                            return(-1);
                        }

                        return(-2);
                    }
                }

                return(-3);
            }

            return(-32);
        }
        /// <summary>
        ///     Executes this command.
        /// </summary>
        public override int Execute(LogicLevel level)
        {
            int count = this._gameObjectIds.Count;

            if (count > 0)
            {
                bool validSize        = true;
                bool validGameObjects = true;

                if (this._xPositions.Count != count || this._xPositions.Count != count || count > 500)
                {
                    validSize = false;
                }

                if (validSize)
                {
                    LogicGameObject[] gameObjects = new LogicGameObject[count];

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

                        if (gameObject != null)
                        {
                            int gameObjectType = gameObject.GetGameObjectType();

                            if (gameObjectType > 6 || gameObjectType == 3)
                            {
                                validGameObjects = false;
                            }

                            gameObjects[i] = gameObject;
                        }
                        else
                        {
                            validGameObjects = false;
                        }
                    }

                    if (validGameObjects)
                    {
                        bool validWallBlock = true;

                        for (int i = 0; i < count; i++)
                        {
                            LogicGameObject gameObject = gameObjects[i];

                            if (gameObject.GetGameObjectType() == 0 && ((LogicBuilding)gameObject).GetWallIndex() != 0 && validWallBlock)
                            {
                                LogicBuilding baseWallBlock = (LogicBuilding)gameObject;

                                int x             = this._xPositions[i];
                                int y             = this._yPositions[i];
                                int minX          = 0;
                                int minY          = 0;
                                int maxX          = 0;
                                int maxY          = 0;
                                int minWallBlockX = 0;
                                int minWallBlockY = 0;
                                int maxWallBlockX = 0;
                                int maxWallBlockY = 0;

                                bool success = true;

                                int wallBlockCnt = -1;

                                for (int j = 0; j < count; j++)
                                {
                                    LogicGameObject obj = gameObjects[j];

                                    if (obj.GetGameObjectType() == 0 && ((LogicBuilding)obj).GetWallIndex() == baseWallBlock.GetWallIndex())
                                    {
                                        LogicBuilding wallBlock = (LogicBuilding)obj;

                                        int tmp1 = x - this._xPositions[j];
                                        int tmp2 = y - this._yPositions[j];

                                        if ((x & this._xPositions[j]) != -1)
                                        {
                                            success = false;
                                        }

                                        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(j);
                                        int wallBlockY = wallBlock.GetBuildingData().GetWallBlockY(j);

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

                                        ++wallBlockCnt;
                                    }
                                }

                                if (baseWallBlock.GetBuildingData().GetWallBlockCount() == wallBlockCnt)
                                {
                                    int wallBlockSizeX = maxWallBlockX - minWallBlockX;
                                    int wallBlockSizeY = maxWallBlockY - minWallBlockY;
                                    int lengthX        = maxX - minX;
                                    int lengthY        = maxY - minY;

                                    if (wallBlockSizeX != lengthX || wallBlockSizeY != lengthY)
                                    {
                                        if (!success && (wallBlockSizeX != lengthX) != (wallBlockSizeY != lengthY))
                                        {
                                            validGameObjects = false;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        // EditmodeInvalidGameObjectType.
                    }

                    bool objectsOverlap = false;

                    if (validGameObjects)
                    {
                        int idx = 0;

                        while (idx < count)
                        {
                            int x = this._xPositions[idx];
                            int y = this._yPositions[idx];

                            LogicGameObject gameObject = gameObjects[idx];

                            int width  = gameObject.GetWidthInTiles();
                            int height = gameObject.GetHeightInTiles();

                            int tmp1 = x + width;
                            int tmp2 = y + height;

                            for (int i = 0; i < count; i++)
                            {
                                LogicGameObject gameObject2 = gameObjects[idx];

                                if (gameObject2 != gameObject)
                                {
                                    int x2      = this._xPositions[i];
                                    int y2      = this._yPositions[i];
                                    int width2  = gameObject2.GetWidthInTiles();
                                    int height2 = gameObject2.GetHeightInTiles();
                                    int tmp3    = x + width2;
                                    int tmp4    = y + height2;

                                    if (tmp1 > x2 && tmp2 > y2 && x2 < tmp3 && y2 < tmp4)
                                    {
                                        objectsOverlap = true;
                                        return(0);
                                    }
                                }
                            }

                            ++idx;
                        }
                    }

                    if (validGameObjects)
                    {
                        for (int i = 0; i < count; i++)
                        {
                            int x = this._xPositions[i];
                            int y = this._yPositions[i];

                            LogicGameObject gameObject = gameObjects[i];

                            int width  = gameObject.GetWidthInTiles();
                            int height = gameObject.GetHeightInTiles();

                            if (!level.IsValidPlaceForBuildingWithIgnoreList(x, y, width, height, gameObjects, count))
                            {
                                if (validGameObjects)
                                {
                                    // EditmodeInvalidPosition.
                                }

                                validGameObjects = false;
                            }
                        }
                    }

                    if (validGameObjects)
                    {
                        for (int i = 0; i < count; i++)
                        {
                            int x = this._xPositions[i];
                            int y = this._yPositions[i];

                            LogicGameObject gameObject = gameObjects[i];

                            gameObject.SetPositionXY(x << 9, y << 9);
                        }

                        for (int i = 0; i < count; i++)
                        {
                            int x = this._xPositions[i];
                            int y = this._yPositions[i];

                            LogicGameObject gameObject = gameObjects[i];

                            int width  = gameObject.GetWidthInTiles();
                            int height = gameObject.GetHeightInTiles();

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

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

                        if (validGameObjects)
                        {
                            if (level.GetHomeOwnerAvatar() != null)
                            {
                                LogicAvatar homeOwnerAvatar = level.GetHomeOwnerAvatar();

                                if (homeOwnerAvatar.GetTownHallLevel() >= LogicDataTables.GetGlobals().GetChallengeBaseCooldownTownHall())
                                {
                                    level.SetLayoutCooldownSecs(level.GetActiveLayout(level.GetVillageType()), LogicDataTables.GetGlobals().GetChallengeBaseSaveCooldown());
                                }
                            }
                        }

                        return(0);
                    }
                }
            }

            return(-1);
        }
Example #4
0
        public void RecalculateAvailableLoot()
        {
            LogicAvatar homeOwnerAvatar = this.m_parent.GetLevel().GetHomeOwnerAvatar();

            if (!homeOwnerAvatar.IsNpcAvatar())
            {
                int matchType = this.m_parent.GetLevel().GetMatchType();

                if (matchType >= 10 || matchType != 3 && matchType != 5)
                {
                    int resourceProductionLootPercentage = LogicDataTables.GetGlobals().GetResourceProductionLootPercentage(this.m_resourceData);

                    if (homeOwnerAvatar.IsClientAvatar())
                    {
                        LogicAvatar visitorAvatar = this.m_parent.GetLevel().GetVisitorAvatar();

                        if (visitorAvatar != null && visitorAvatar.IsClientAvatar())
                        {
                            resourceProductionLootPercentage = resourceProductionLootPercentage *
                                                               LogicDataTables.GetGlobals().GetLootMultiplierByTownHallDiff(visitorAvatar.GetTownHallLevel(),
                                                                                                                            homeOwnerAvatar.GetTownHallLevel()) / 100;
                        }
                    }

                    if (resourceProductionLootPercentage > 100)
                    {
                        resourceProductionLootPercentage = 100;
                    }

                    this.m_availableLoot = (int)((long)this.GetResourceCount() * resourceProductionLootPercentage / 100L);
                }
                else
                {
                    this.m_availableLoot = 0;
                }
            }
            else
            {
                this.m_availableLoot = 0;
            }
        }
Example #5
0
        public override int Execute(LogicLevel level)
        {
            if (this.m_layoutId != 6)
            {
                if (this.m_layoutId != 7)
                {
                    LogicGameObjectFilter            filter      = new LogicGameObjectFilter();
                    LogicArrayList <LogicGameObject> gameObjects = new LogicArrayList <LogicGameObject>(500);

                    int moved = 0;

                    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   = gameObjects[i].GetPositionLayout(this.m_layoutId, true);

                        int x = position.m_x;
                        int y = position.m_y;

                        if (x != -1 && y != -1)
                        {
                            int tmp1 = x + gameObject.GetWidthInTiles();
                            int tmp2 = y + gameObject.GetHeightInTiles();

                            for (int j = 0; j < gameObjects.Size(); j++)
                            {
                                LogicGameObject tmp = gameObjects[j];

                                if (tmp != gameObject)
                                {
                                    LogicVector2 position2 = tmp.GetPositionLayout(this.m_layoutId, true);

                                    int x2 = position2.m_x;
                                    int y2 = position2.m_y;

                                    if (x2 != -1 && y2 != -1)
                                    {
                                        int tmp3 = x2 + tmp.GetWidthInTiles();
                                        int tmp4 = y2 + tmp.GetHeightInTiles();

                                        if (tmp1 > x2 && tmp2 > y2 && x < tmp3 && y < tmp4)
                                        {
                                            Debugger.Warning("LogicSaveBaseLayoutCommand: overlap");
                                            return(-1);
                                        }
                                    }
                                }
                            }
                        }
                    }

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

                        if (gameObject.GetGameObjectType() != LogicGameObjectType.DECO)
                        {
                            if (layoutPosition.m_x != editModePosition.m_x || layoutPosition.m_y != editModePosition.m_y)
                            {
                                ++moved;
                            }
                        }

                        gameObject.SetPositionLayoutXY(editModePosition.m_x, editModePosition.m_y, this.m_layoutId, false);
                    }

                    filter.Destruct();

                    if (moved > 0)
                    {
                        LogicAvatar homeOwnerAvatar = level.GetHomeOwnerAvatar();

                        if (homeOwnerAvatar.GetTownHallLevel() >= LogicDataTables.GetGlobals().GetChallengeBaseCooldownEnabledTownHall())
                        {
                            level.SetLayoutCooldownSecs(this.m_layoutId, LogicDataTables.GetGlobals().GetChallengeBaseSaveCooldown());
                        }
                    }

                    return(0);
                }

                return(-11);
            }

            return(10);
        }
        public override int Execute(LogicLevel level)
        {
            int count = this.m_gameObjectIds.Size();

            if (count > 0)
            {
                bool validGameObjectType = true;

                if (this.m_xPositions.Size() == count && this.m_xPositions.Size() == count && count <= 500)
                {
                    LogicGameObject[] gameObjects = new LogicGameObject[count];

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

                        if (gameObject != null)
                        {
                            LogicGameObjectType gameObjectType = gameObject.GetGameObjectType();

                            if (gameObjectType != LogicGameObjectType.BUILDING &&
                                gameObjectType != LogicGameObjectType.TRAP &&
                                gameObjectType != LogicGameObjectType.DECO)
                            {
                                validGameObjectType = false;
                            }

                            gameObjects[i] = gameObject;
                        }
                        else
                        {
                            validGameObjectType = false;
                        }
                    }

                    if (validGameObjectType)
                    {
                        for (int i = 0; i < count; i++)
                        {
                            LogicGameObject gameObject = gameObjects[i];

                            if (gameObject.GetGameObjectType() == LogicGameObjectType.BUILDING && validGameObjectType)
                            {
                                LogicBuilding baseWallBlock = (LogicBuilding)gameObject;

                                if (baseWallBlock.GetWallIndex() != 0)
                                {
                                    int x             = this.m_xPositions[i];
                                    int y             = this.m_yPositions[i];
                                    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;

                                    bool success = true;

                                    for (int j = 0; j < count; j++)
                                    {
                                        LogicGameObject obj = gameObjects[j];

                                        if (obj.GetGameObjectType() == LogicGameObjectType.BUILDING)
                                        {
                                            LogicBuilding wallBlock = (LogicBuilding)obj;

                                            if (wallBlock.GetWallIndex() == baseWallBlock.GetWallIndex())
                                            {
                                                int tmp1 = x - this.m_xPositions[j];
                                                int tmp2 = y - this.m_yPositions[j];

                                                if ((x & this.m_xPositions[j]) != -1)
                                                {
                                                    success = false;
                                                }

                                                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)
                                    {
                                        int wallBlockSizeX = maxWallBlockX - minWallBlockX;
                                        int wallBlockSizeY = maxWallBlockY - minWallBlockY;
                                        int lengthX        = maxX - minX;
                                        int lengthY        = maxY - minY;

                                        if (wallBlockSizeX != lengthX || wallBlockSizeY != lengthY)
                                        {
                                            if (!success && wallBlockSizeX != lengthX != (wallBlockSizeY != lengthY))
                                            {
                                                validGameObjectType = false;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        Debugger.Warning("EditModeInvalidGameObjectType");
                        return(-1);
                    }

                    if (validGameObjectType)
                    {
                        for (int i = 0; i < count; i++)
                        {
                            int x = this.m_xPositions[i];
                            int y = this.m_yPositions[i];

                            LogicGameObject gameObject = gameObjects[i];

                            int maxX = x + gameObject.GetWidthInTiles();
                            int maxY = y + gameObject.GetHeightInTiles();

                            for (int j = 0; j < count; j++)
                            {
                                LogicGameObject gameObject2 = gameObjects[j];

                                if (gameObject2 != gameObject)
                                {
                                    int x2    = this.m_xPositions[j];
                                    int y2    = this.m_yPositions[j];
                                    int maxX2 = x2 + gameObject2.GetWidthInTiles();
                                    int maxY2 = y2 + gameObject2.GetHeightInTiles();

                                    if (maxX > x2 && maxY > y2 && x < maxX2 && y < maxY2)
                                    {
                                        Debugger.Warning("EditModeObjectsOverlap");
                                        return(-1);
                                    }
                                }
                            }
                        }
                    }

                    if (validGameObjectType)
                    {
                        for (int i = 0; i < count; i++)
                        {
                            int x = this.m_xPositions[i];
                            int y = this.m_yPositions[i];

                            LogicGameObject gameObject = gameObjects[i];

                            int width  = gameObject.GetWidthInTiles();
                            int height = gameObject.GetHeightInTiles();

                            if (!level.IsValidPlaceForBuildingWithIgnoreList(x, y, width, height, gameObjects, count))
                            {
                                Debugger.Warning("EditModeInvalidPosition");
                                return(-1);
                            }
                        }
                    }

                    if (validGameObjectType)
                    {
                        for (int i = 0; i < count; i++)
                        {
                            int x = this.m_xPositions[i];
                            int y = this.m_yPositions[i];

                            LogicGameObject gameObject = gameObjects[i];

                            gameObject.SetPositionXY(x << 9, y << 9);
                        }

                        for (int i = 0; i < count; i++)
                        {
                            int x = this.m_xPositions[i];
                            int y = this.m_yPositions[i];

                            LogicGameObject gameObject = gameObjects[i];

                            int width  = gameObject.GetWidthInTiles();
                            int height = gameObject.GetHeightInTiles();

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

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

                        if (level.GetHomeOwnerAvatar() != null)
                        {
                            LogicAvatar homeOwnerAvatar = level.GetHomeOwnerAvatar();

                            if (homeOwnerAvatar.GetTownHallLevel() >= LogicDataTables.GetGlobals().GetChallengeBaseCooldownEnabledTownHall())
                            {
                                level.SetLayoutCooldownSecs(level.GetActiveLayout(level.GetVillageType()), LogicDataTables.GetGlobals().GetChallengeBaseSaveCooldown());
                            }
                        }

                        return(0);
                    }
                }
                else
                {
                    Debugger.Warning("EditModeSizeMismatch");
                }

                return(-1);
            }

            return(-92);
        }
        public override int Execute(LogicLevel level)
        {
            this.m_level = level;

            if (LogicDataTables.GetGlobals().EnablePresets())
            {
                LogicAvatar homeOwnerAvatar = level.GetHomeOwnerAvatar();

                if (homeOwnerAvatar.GetTownHallLevel() >= LogicDataTables.GetGlobals().GetEnablePresetsTownHallLevel())
                {
                    if (this.m_presetId <= 3)
                    {
                        LogicDataTable        table            = LogicDataTables.GetTable(LogicDataType.CHARACTER);
                        LogicComponentManager componentManager = level.GetComponentManager();

                        int totalMaxHousing = componentManager.GetTotalMaxHousing(0);

                        for (int i = 0, housingSpace = 0; i < table.GetItemCount(); i++)
                        {
                            LogicCharacterData data = (LogicCharacterData)table.GetItemAt(i);

                            if (level.GetGameMode().GetCalendar().IsProductionEnabled(data) && !data.IsSecondaryTroop())
                            {
                                int count = 0;

                                if (this.m_slots.Size() > 0)
                                {
                                    for (int j = 0; j < this.m_slots.Size(); j++)
                                    {
                                        if (this.m_slots[j].GetData() == data)
                                        {
                                            count = this.m_slots[j].GetCount();
                                            break;
                                        }
                                    }
                                }

                                housingSpace += count * data.GetHousingSpace();

                                if (housingSpace > totalMaxHousing || !this.IsUnlocked(data))
                                {
                                    this.SetUnitPresetCount(data, 0);
                                }
                                else
                                {
                                    this.SetUnitPresetCount(data, count);
                                }
                            }
                        }

                        table           = LogicDataTables.GetTable(LogicDataType.SPELL);
                        totalMaxHousing = componentManager.GetTotalMaxHousing(0);

                        for (int i = 0, housingSpace = 0; i < table.GetItemCount(); i++)
                        {
                            LogicSpellData data = (LogicSpellData)table.GetItemAt(i);

                            if (level.GetGameMode().GetCalendar().IsProductionEnabled(data))
                            {
                                int count = 0;

                                if (this.m_slots.Size() > 0)
                                {
                                    for (int j = 0; j < this.m_slots.Size(); j++)
                                    {
                                        if (this.m_slots[j].GetData() == data)
                                        {
                                            count = this.m_slots[j].GetCount();
                                            break;
                                        }
                                    }
                                }

                                housingSpace += count * data.GetHousingSpace();

                                if (housingSpace > totalMaxHousing || !this.IsUnlocked(data))
                                {
                                    this.SetUnitPresetCount(data, 0);
                                }
                                else
                                {
                                    this.SetUnitPresetCount(data, count);
                                }
                            }
                        }

                        return(0);
                    }

                    return(-2);
                }
            }

            return(-1);
        }
        public override int Execute(LogicLevel level)
        {
            if (this.m_inputLayoutId != 6)
            {
                if (this.m_outputLayoutId != 6)
                {
                    if (this.m_inputLayoutId != 7)
                    {
                        if (this.m_outputLayoutId != 7)
                        {
                            int townHallLevel = level.GetTownHallLevel(level.GetVillageType());

                            if (townHallLevel >= level.GetRequiredTownHallLevelForLayout(this.m_inputLayoutId, -1) &&
                                townHallLevel >= level.GetRequiredTownHallLevelForLayout(this.m_outputLayoutId, -1))
                            {
                                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);

                                if (this.m_outputLayoutId == level.GetActiveLayout())
                                {
                                    LogicMoveMultipleBuildingsCommand moveMultipleBuildingsCommand = new LogicMoveMultipleBuildingsCommand();

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

                                        moveMultipleBuildingsCommand.AddNewMove(gameObject.GetGlobalID(), position.m_x, position.m_y);
                                    }

                                    int result = moveMultipleBuildingsCommand.Execute(level);

                                    moveMultipleBuildingsCommand.Destruct();

                                    if (result != 0)
                                    {
                                        filter.Destruct();
                                        return(-2);
                                    }
                                }

                                for (int i = 0; i < gameObjects.Size(); i++)
                                {
                                    LogicGameObject gameObject       = gameObjects[i];
                                    LogicVector2    layoutPosition   = gameObject.GetPositionLayout(this.m_inputLayoutId, false);
                                    LogicVector2    editModePosition = gameObject.GetPositionLayout(this.m_inputLayoutId, true);

                                    gameObject.SetPositionLayoutXY(layoutPosition.m_x, layoutPosition.m_y, this.m_outputLayoutId, false);
                                    gameObject.SetPositionLayoutXY(editModePosition.m_x, editModePosition.m_y, this.m_outputLayoutId, true);

                                    if (gameObject.GetGameObjectType() == LogicGameObjectType.BUILDING)
                                    {
                                        LogicCombatComponent combatComponent = gameObject.GetCombatComponent(false);

                                        if (combatComponent != null)
                                        {
                                            if (combatComponent.HasAltAttackMode())
                                            {
                                                if (combatComponent.UseAltAttackMode(this.m_inputLayoutId, false) ^ combatComponent.UseAltAttackMode(this.m_outputLayoutId, false))
                                                {
                                                    combatComponent.ToggleAttackMode(this.m_outputLayoutId, false);
                                                }

                                                if (combatComponent.UseAltAttackMode(this.m_inputLayoutId, true) ^ combatComponent.UseAltAttackMode(this.m_outputLayoutId, true))
                                                {
                                                    combatComponent.ToggleAttackMode(this.m_outputLayoutId, true);
                                                }
                                            }

                                            if (combatComponent.GetAttackerItemData().GetTargetingConeAngle() != 0)
                                            {
                                                int aimAngle1 = combatComponent.GetAimAngle(this.m_inputLayoutId, false);
                                                int aimAngle2 = combatComponent.GetAimAngle(this.m_outputLayoutId, false);

                                                if (aimAngle1 != aimAngle2)
                                                {
                                                    combatComponent.ToggleAimAngle(aimAngle1 - aimAngle2, this.m_outputLayoutId, false);
                                                }
                                            }
                                        }
                                    }
                                    else if (gameObject.GetGameObjectType() == LogicGameObjectType.TRAP)
                                    {
                                        LogicTrap trap = (LogicTrap)gameObject;

                                        if (trap.HasAirMode())
                                        {
                                            if (trap.IsAirMode(this.m_inputLayoutId, false) ^ trap.IsAirMode(this.m_outputLayoutId, false))
                                            {
                                                trap.ToggleAirMode(this.m_outputLayoutId, false);
                                            }

                                            if (trap.IsAirMode(this.m_inputLayoutId, true) ^ trap.IsAirMode(this.m_outputLayoutId, true))
                                            {
                                                trap.ToggleAirMode(this.m_outputLayoutId, true);
                                            }
                                        }
                                    }
                                }

                                filter.Destruct();
                                level.SetLayoutState(this.m_outputLayoutId, level.GetVillageType(), level.GetLayoutState(this.m_inputLayoutId, level.GetVillageType()));

                                LogicAvatar homeOwnerAvatar = level.GetHomeOwnerAvatar();

                                if (homeOwnerAvatar.GetTownHallLevel() >= LogicDataTables.GetGlobals().GetChallengeBaseCooldownEnabledTownHall())
                                {
                                    level.SetLayoutCooldownSecs(this.m_outputLayoutId, level.GetLayoutCooldown(this.m_inputLayoutId) / 15);
                                }

                                return(0);
                            }

                            return(-1);
                        }

                        return(-8);
                    }

                    return(-7);
                }

                return(-6);
            }

            return(-5);
        }
Example #9
0
        public virtual void RecalculateAvailableLoot()
        {
            int matchType = this.m_parent.GetLevel().GetMatchType();

            LogicAvatar    homeOwnerAvatar = this.m_parent.GetLevel().GetHomeOwnerAvatar();
            LogicAvatar    visitorAvatar   = this.m_parent.GetLevel().GetVisitorAvatar();
            LogicDataTable resourceTable   = LogicDataTables.GetTable(LogicDataType.RESOURCE);

            for (int i = 0; i < this.m_resourceCount.Size(); i++)
            {
                LogicResourceData data = (LogicResourceData)resourceTable.GetItemAt(i);
                int resourceCount      = this.m_resourceCount[i];

                if (!homeOwnerAvatar.IsNpcAvatar())
                {
                    if (matchType == 5 && this.m_parent.GetLevel().IsArrangedWar())
                    {
                        if (resourceCount >= 0)
                        {
                            resourceCount = 0;
                        }
                    }
                    else if (LogicDataTables.GetGlobals().UseTownHallLootPenaltyInWar() || matchType != 5)
                    {
                        if (matchType != 8 && matchType != 9)
                        {
                            int multiplier = 100;
                            int calculateAvailableLootCount = 0;

                            if (homeOwnerAvatar != null && homeOwnerAvatar.IsClientAvatar() &&
                                visitorAvatar != null && visitorAvatar.IsClientAvatar())
                            {
                                multiplier = LogicDataTables.GetGlobals().GetLootMultiplierByTownHallDiff(visitorAvatar.GetTownHallLevel(), homeOwnerAvatar.GetTownHallLevel());
                            }

                            if (this.m_parent.GetData() == LogicDataTables.GetTownHallData() && LogicDataTables.GetGlobals().GetTownHallLootPercentage() != -1)
                            {
                                calculateAvailableLootCount = resourceCount * (multiplier * LogicDataTables.GetGlobals().GetTownHallLootPercentage() / 100) / 100;
                            }
                            else if (!data.IsPremiumCurrency())
                            {
                                int townHallLevel         = homeOwnerAvatar.GetTownHallLevel();
                                int lootableResourceCount = 0;

                                if (matchType != 3)
                                {
                                    if (matchType == 5)
                                    {
                                        lootableResourceCount = resourceCount;
                                    }
                                    else if (matchType != 7)
                                    {
                                        lootableResourceCount = (int)((long)resourceCount * LogicDataTables.GetTownHallLevel(townHallLevel).GetStorageLootPercentage(data) / 100);
                                    }
                                }

                                int storageLootCap   = LogicDataTables.GetTownHallLevel(townHallLevel).GetStorageLootCap(data);
                                int maxResourceCount = LogicMath.Min(homeOwnerAvatar.GetResourceCount(data), homeOwnerAvatar.GetResourceCap(data));

                                if (maxResourceCount > storageLootCap && maxResourceCount > 0)
                                {
                                    int clampedValue;

                                    if (storageLootCap < 1000000)
                                    {
                                        if (storageLootCap < 100000)
                                        {
                                            if (storageLootCap < 10000)
                                            {
                                                if (storageLootCap < 1000)
                                                {
                                                    clampedValue = (resourceCount * storageLootCap + (maxResourceCount >> 1)) / maxResourceCount;
                                                }
                                                else
                                                {
                                                    if (!LogicDataTables.GetGlobals().UseMoreAccurateLootCap())
                                                    {
                                                        clampedValue = 100 * ((resourceCount * (storageLootCap / 100) + (maxResourceCount >> 1)) / maxResourceCount);
                                                    }
                                                    else
                                                    {
                                                        if (resourceCount / 100 > maxResourceCount / storageLootCap)
                                                        {
                                                            clampedValue = 100 * ((resourceCount * (storageLootCap / 100) + (maxResourceCount >> 1)) / maxResourceCount);
                                                        }
                                                        else
                                                        {
                                                            clampedValue = (resourceCount * storageLootCap + (maxResourceCount >> 1)) / maxResourceCount;
                                                        }
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                if (!LogicDataTables.GetGlobals().UseMoreAccurateLootCap())
                                                {
                                                    clampedValue = 1000 * ((resourceCount * (storageLootCap / 1000) + (maxResourceCount >> 1)) / maxResourceCount);
                                                }
                                                else
                                                {
                                                    if (resourceCount / 1000 > maxResourceCount / storageLootCap)
                                                    {
                                                        clampedValue = 1000 * ((resourceCount * (storageLootCap / 1000) + (maxResourceCount >> 1)) / maxResourceCount);
                                                    }
                                                    else
                                                    {
                                                        if (resourceCount / 100 > maxResourceCount / storageLootCap)
                                                        {
                                                            clampedValue = 100 * ((resourceCount * (storageLootCap / 100) + (maxResourceCount >> 1)) / maxResourceCount);
                                                        }
                                                        else
                                                        {
                                                            clampedValue = (resourceCount * storageLootCap + (maxResourceCount >> 1)) / maxResourceCount;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        else
                                        {
                                            if (!LogicDataTables.GetGlobals().UseMoreAccurateLootCap())
                                            {
                                                clampedValue = 10000 * ((resourceCount * (storageLootCap / 10000) + (maxResourceCount >> 1)) / maxResourceCount);
                                            }
                                            else
                                            {
                                                if (resourceCount / 10000 > maxResourceCount / storageLootCap)
                                                {
                                                    clampedValue = 10000 * ((resourceCount * (storageLootCap / 10000) + (maxResourceCount >> 1)) / maxResourceCount);
                                                }
                                                else
                                                {
                                                    if (resourceCount / 1000 > maxResourceCount / storageLootCap)
                                                    {
                                                        clampedValue = 1000 * ((resourceCount * (storageLootCap / 1000) + (maxResourceCount >> 1)) / maxResourceCount);
                                                    }
                                                    else
                                                    {
                                                        if (resourceCount / 100 > maxResourceCount / storageLootCap)
                                                        {
                                                            clampedValue = 100 * ((resourceCount * (storageLootCap / 100) + (maxResourceCount >> 1)) / maxResourceCount);
                                                        }
                                                        else
                                                        {
                                                            clampedValue = (resourceCount * storageLootCap + (maxResourceCount >> 1)) / maxResourceCount;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        clampedValue = 40000 * ((resourceCount * (storageLootCap / 40000) + (maxResourceCount >> 1)) / maxResourceCount);
                                    }

                                    if (lootableResourceCount > clampedValue)
                                    {
                                        lootableResourceCount = clampedValue;
                                    }
                                }

                                calculateAvailableLootCount = multiplier * lootableResourceCount / 100;
                            }

                            if (calculateAvailableLootCount <= resourceCount)
                            {
                                resourceCount = calculateAvailableLootCount;
                            }
                        }
                    }
                }

                this.m_stealableResourceCount[i] = resourceCount;
            }
        }
Example #10
0
        public void SetUpgradeLevel(int upgLevel)
        {
            this.m_upgradeLevel = upgLevel;

            LogicCharacterData     data = this.GetCharacterData();
            LogicHitpointComponent hitpointComponent = this.GetHitpointComponent();
            LogicCombatComponent   combatComponent   = this.GetCombatComponent();

            int hp = data.GetHitpoints(upgLevel);
            int damagePercentage = 100;

            if (data.GetScaleByTH())
            {
                LogicAvatar avatar = this.m_level.GetHomeOwnerAvatar();

                if (hitpointComponent != null && hitpointComponent.GetTeam() == 0)
                {
                    avatar = this.m_level.GetVisitorAvatar();
                }

                int tmp1 = 700 * avatar.GetTownHallLevel() / (LogicDataTables.GetTownHallLevelCount() - 1);

                damagePercentage = tmp1 / 10 + 30;
                hp = damagePercentage * hp / 100;

                if (damagePercentage * hp < 200)
                {
                    hp = 1;
                }

                if (tmp1 < -289)
                {
                    damagePercentage = 1;
                }
            }

            hitpointComponent.SetMaxHitpoints(hp);
            hitpointComponent.SetHitpoints(data.GetHitpoints(upgLevel));
            hitpointComponent.SetDieEffect(data.GetDieEffect(upgLevel), data.GetDieEffect2(upgLevel));

            if (combatComponent != null)
            {
                combatComponent.SetAttackValues(data.GetAttackerItemData(upgLevel), damagePercentage);
            }

            if (this.m_childrens != null)
            {
                for (int i = 0; i < this.m_childrens.Size(); i++)
                {
                    this.m_childrens[i].SetUpgradeLevel(upgLevel);
                }
            }

            if (this.IsHero())
            {
                LogicHeroData heroData = (LogicHeroData)this.m_data;
                LogicAvatar   avatar   = this.m_level.GetHomeOwnerAvatar();

                if (hitpointComponent.GetTeam() == 0)
                {
                    avatar = this.m_level.GetVisitorAvatar();
                }

                this.m_flying = heroData.IsFlying(avatar.GetHeroMode(heroData));
                this.GetMovementComponent().SetFlying(this.m_flying);
            }

            if (data.GetAutoMergeDistance() > 0)
            {
                this.m_autoMergeTime = 2000;
            }

            int speed = data.GetSpeed();

            if (data.GetSpecialAbilityLevel(this.m_upgradeLevel) > 0 &&
                data.GetSpecialAbilityType() == LogicCharacterData.SPECIAL_ABILITY_TYPE_SPEED_BOOST)
            {
                speed = speed * data.GetSpecialAbilityAttribute(this.m_upgradeLevel) / 100;
            }

            this.GetMovementComponent().SetSpeed(speed);
        }
Example #11
0
        public override int Execute(LogicLevel level)
        {
            LogicGameObject gameObject = level.GetGameObjectManager().GetGameObjectByID(this.m_gameObjectId);

            if (gameObject != null)
            {
                LogicGameObjectType gameObjectType = gameObject.GetGameObjectType();

                if (gameObjectType == LogicGameObjectType.BUILDING || gameObjectType == LogicGameObjectType.TRAP ||
                    gameObjectType == LogicGameObjectType.DECO)
                {
                    if (gameObjectType != LogicGameObjectType.BUILDING || ((LogicBuildingData)gameObject.GetData()).GetVillageType() == level.GetVillageType())
                    {
                        if (gameObjectType == LogicGameObjectType.BUILDING)
                        {
                            if (((LogicBuilding)gameObject).GetWallIndex() != 0)
                            {
                                return(-21);
                            }
                        }

                        int x      = gameObject.GetTileX();
                        int y      = gameObject.GetTileY();
                        int width  = gameObject.GetWidthInTiles();
                        int height = gameObject.GetHeightInTiles();

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

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

                        if (level.IsValidPlaceForBuilding(this.m_x, this.m_y, width, height, gameObject))
                        {
                            gameObject.SetPositionXY(this.m_x << 9, this.m_y << 9);

                            if (this.m_x != x || this.m_y != y)
                            {
                                LogicAvatar homeOwnerAvatar = level.GetHomeOwnerAvatar();

                                if (homeOwnerAvatar != null)
                                {
                                    if (homeOwnerAvatar.GetTownHallLevel() >= LogicDataTables.GetGlobals().GetChallengeBaseCooldownEnabledTownHall() &&
                                        gameObject.GetGameObjectType() != LogicGameObjectType.DECO)
                                    {
                                        level.SetLayoutCooldownSecs(level.GetActiveLayout(level.GetVillageType()), LogicDataTables.GetGlobals().GetChallengeBaseSaveCooldown());
                                    }
                                }
                            }

                            return(0);
                        }

                        return(-3);
                    }

                    return(-32);
                }

                return(-1);
            }

            return(-2);
        }
Example #12
0
        public void SetBattleOver()
        {
            if (this.m_battleOver)
            {
                return;
            }

            this.m_level.GetBattleLog().SetBattleEnded(LogicDataTables.GetGlobals().GetAttackLengthSecs() - this.GetRemainingAttackSeconds());
            this.m_level.GetMissionManager().Tick();

            LogicArrayList <LogicComponent> components = this.m_level.GetComponentManager().GetComponents(LogicComponentType.COMBAT);

            for (int i = 0; i < components.Size(); i++)
            {
                ((LogicCombatComponent)components[i]).Boost(0, 0, 0);
            }

            bool duelMatch = (this.m_level.GetMatchType() & 0xFFFFFFFE) == 8;

            if (duelMatch)
            {
                LogicAvatar avatar = this.m_level.GetVisitorAvatar();

                if (avatar != null && avatar.IsClientAvatar())
                {
                    ((LogicClientAvatar)avatar).RemoveUnitsVillage2();
                }
            }

            if (this.m_state == 3)
            {
                this.EndDefendState();
            }
            else
            {
                LogicBattleLog battleLog = this.m_level.GetBattleLog();

                if (battleLog.GetBattleStarted())
                {
                    LogicAvatar visitorAvatar   = this.m_level.GetVisitorAvatar();
                    LogicAvatar homeOwnerAvatar = this.m_level.GetHomeOwnerAvatar();

                    int stars = battleLog.GetStars();

                    if (!this.m_level.GetVisitorAvatar().IsClientAvatar() || !this.m_level.GetHomeOwnerAvatar().IsClientAvatar())
                    {
                        if (visitorAvatar.IsClientAvatar() && homeOwnerAvatar.IsNpcAvatar())
                        {
                            LogicNpcAvatar npcAvatar = (LogicNpcAvatar)homeOwnerAvatar;
                            LogicNpcData   npcData   = npcAvatar.GetNpcData();

                            int npcStars = visitorAvatar.GetNpcStars(npcData);

                            if (stars > npcStars && npcData.IsSinglePlayer())
                            {
                                visitorAvatar.SetNpcStars(npcData, stars);
                                visitorAvatar.GetChangeListener().CommodityCountChanged(0, npcData, stars);
                            }

                            // TODO: LogicBattleLog::sendNpcAttackEndEvents.
                        }
                    }
                    else
                    {
                        LogicClientAvatar attacker = (LogicClientAvatar)visitorAvatar;
                        LogicClientAvatar defender = (LogicClientAvatar)homeOwnerAvatar;

                        int originalAttackerScore = attacker.GetScore();
                        int originalDefenderScore = defender.GetScore();
                        int matchType             = this.m_level.GetMatchType();

                        if (matchType == 1 || !LogicDataTables.GetGlobals().ScoringOnlyFromMatchedMode() && (matchType == 0 || matchType == 2 || matchType == 4 || matchType == 6))
                        {
                            LogicGamePlayUtil.CalculateCombatScore(attacker, defender, stars, false,
                                                                   matchType == 4, battleLog.GetDestructionPercentage(), this.m_calendar.GetStarBonusMultiplier(), duelMatch);

                            if (!duelMatch && homeOwnerAvatar.GetTownHallLevel() >= LogicDataTables.GetGlobals().GetLootCartEnabledTownHall())
                            {
                                LogicDataTable resourceTable = LogicDataTables.GetTable(LogicDataType.RESOURCE);

                                if (resourceTable.GetItemCount() > 0)
                                {
                                    bool hasStolen = false;

                                    for (int i = 0; i < resourceTable.GetItemCount(); i++)
                                    {
                                        LogicResourceData data = (LogicResourceData)resourceTable.GetItemAt(i);

                                        if (!data.IsPremiumCurrency())
                                        {
                                            if (battleLog.GetStolenResources(data) > 0)
                                            {
                                                hasStolen = true;
                                            }
                                        }
                                    }

                                    if (hasStolen)
                                    {
                                        LogicGameObjectManager gameObjectManager = this.m_level.GetGameObjectManagerAt(0);
                                        LogicObstacle          lootCart          = gameObjectManager.GetLootCart();

                                        if (lootCart == null)
                                        {
                                            gameObjectManager.AddLootCart();
                                            lootCart = gameObjectManager.GetLootCart();
                                        }

                                        if (lootCart != null)
                                        {
                                            LogicLootCartComponent lootCartComponent = lootCart.GetLootCartComponent();

                                            if (lootCartComponent != null)
                                            {
                                                for (int i = 0; i < resourceTable.GetItemCount(); i++)
                                                {
                                                    LogicResourceData data = (LogicResourceData)resourceTable.GetItemAt(i);

                                                    if (!data.IsPremiumCurrency() && data.GetWarResourceReferenceData() == null)
                                                    {
                                                        int lootPercentage = lootCart.GetObstacleData().GetLootDefensePercentage();
                                                        int lootCount      = battleLog.GetStolenResources(data) * lootPercentage / 100;

                                                        lootCartComponent.SetResourceCount(i,
                                                                                           LogicMath.Min(LogicMath.Max(lootCount, lootCartComponent.GetResourceCount(i)),
                                                                                                         lootCartComponent.GetCapacityCount(i)));
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                            this.m_level.UpdateBattleShieldStatus(false);

                            if (stars > 0)
                            {
                                LogicArrayList <LogicDataSlot> castedUnits         = battleLog.GetCastedUnits();
                                LogicArrayList <LogicDataSlot> castedSpells        = battleLog.GetCastedSpells();
                                LogicArrayList <LogicUnitSlot> castedAllianceUnits = battleLog.GetCastedAllianceUnits();

                                LogicArrayList <LogicDataSlot> placedUnits = new LogicArrayList <LogicDataSlot>(castedUnits.Size());

                                for (int i = 0; i < castedUnits.Size(); i++)
                                {
                                    placedUnits.Add(new LogicDataSlot(castedUnits[i].GetData(), castedUnits[i].GetCount()));
                                }

                                for (int i = 0; i < castedSpells.Size(); i++)
                                {
                                    int idx = -1;

                                    for (int j = 0; j < placedUnits.Size(); j++)
                                    {
                                        if (placedUnits[j].GetData() == castedSpells[i].GetData())
                                        {
                                            idx = j;
                                            break;
                                        }
                                    }

                                    if (idx != -1)
                                    {
                                        placedUnits[idx].SetCount(placedUnits[idx].GetCount() + castedSpells[i].GetCount());
                                    }
                                    else
                                    {
                                        placedUnits.Add(new LogicDataSlot(castedSpells[i].GetData(), castedSpells[i].GetCount()));
                                    }
                                }

                                for (int i = 0; i < castedAllianceUnits.Size(); i++)
                                {
                                    placedUnits.Add(new LogicDataSlot(castedAllianceUnits[i].GetData(), castedAllianceUnits[i].GetCount()));
                                }

                                for (int i = 0; i < placedUnits.Size(); i++)
                                {
                                    LogicCombatItemData   data = (LogicCombatItemData)placedUnits[i].GetData();
                                    LogicCalendarUseTroop calendarUseTroopEvent = this.m_calendar.GetUseTroopEvents(data);

                                    if (calendarUseTroopEvent != null)
                                    {
                                        int count = attacker.GetEventUnitCounterCount(data);

                                        if (placedUnits[i].GetCount() >= count >> 16)
                                        {
                                            int progressCount = (short)count + 1;
                                            int eventCounter  = progressCount | (int)(count & 0xFFFF0000);

                                            attacker.SetCommodityCount(6, data, eventCounter);
                                            attacker.GetChangeListener().CommodityCountChanged(6, data, eventCounter);

                                            if (calendarUseTroopEvent.GetParameter(0) == progressCount)
                                            {
                                                int diamonds = calendarUseTroopEvent.GetParameter(2);
                                                int xp       = calendarUseTroopEvent.GetParameter(3);

                                                attacker.XpGainHelper(xp);
                                                attacker.SetDiamonds(attacker.GetDiamonds() + diamonds);
                                                attacker.SetFreeDiamonds(attacker.GetFreeDiamonds() + diamonds);
                                                attacker.GetChangeListener().FreeDiamondsAdded(diamonds, 9);

                                                Debugger.HudPrint(string.Format("USE TROOP Event: Awarding XP: {0} GEMS {1}", xp, diamonds));
                                            }
                                        }
                                    }
                                }

                                for (int i = 0; i < placedUnits.Size(); i++)
                                {
                                    placedUnits[i].Destruct();
                                }

                                placedUnits.Destruct();
                            }
                        }

                        if (this.m_state != 5 &&
                            this.m_level.GetDefenseShieldActivatedHours() == 0 &&
                            battleLog.GetDestructionPercentage() > 0)
                        {
                            int defenseVillageGuardCounter = defender.GetDefenseVillageGuardCounter() + 1;

                            defender.SetDefenseVillageGuardCounter(defenseVillageGuardCounter);
                            defender.GetChangeListener().DefenseVillageGuardCounterChanged(defenseVillageGuardCounter);

                            int villageGuardMins = (defenseVillageGuardCounter & 0xFFFFFF) == 3 * ((defenseVillageGuardCounter & 0xFFFFFF) / 3)
                                ? defender.GetLeagueTypeData().GetVillageGuardInMins()
                                : LogicDataTables.GetGlobals().GetDefaultDefenseVillageGuard();

                            this.m_level.GetHome().GetChangeListener().GuardActivated(60 * villageGuardMins);

                            Debugger.HudPrint(string.Format("Battle end. No Shield, Village Guard for defender: {0}", villageGuardMins));
                        }

                        battleLog.SetAttackerScore(attacker.GetScore() - originalAttackerScore);
                        battleLog.SetDefenderScore(defender.GetScore() - originalDefenderScore);
                        battleLog.SetOriginalAttackerScore(originalAttackerScore);
                        battleLog.SetOriginalDefenderScore(originalDefenderScore);

                        if (this.m_state != 5)
                        {
                            if (stars != 0)
                            {
                                if (matchType != 3 && matchType != 7 && matchType != 8 && matchType != 9)
                                {
                                    if (matchType == 5)
                                    {
                                        if (stars > this.m_level.GetPreviousAttackStars() && !this.m_level.GetIgnoreAttack())
                                        {
                                            this.m_level.GetAchievementManager().IncreaseWarStars(stars);
                                        }
                                    }
                                    else
                                    {
                                        this.m_level.GetAchievementManager().PvpAttackWon();
                                    }
                                }
                            }
                            else if (matchType > 9 || matchType == 3 || matchType == 5 || matchType == 7 || matchType == 8 || matchType == 9)
                            {
                                this.m_level.GetAchievementManager().PvpDefenseWon();
                            }
                        }
                    }
                }
            }

            this.m_battleOver = true;
        }
Example #13
0
        public override void RecalculateAvailableLoot()
        {
            LogicAvatar    homeOwnerAvatar = this.m_parent.GetLevel().GetHomeOwnerAvatar();
            LogicDataTable resourceTable   = LogicDataTables.GetTable(LogicDataType.RESOURCE);

            for (int i = 0; i < this.m_resourceCount.Size(); i++)
            {
                LogicResourceData data = (LogicResourceData)resourceTable.GetItemAt(i);

                if (this.m_parent.GetData() == LogicDataTables.GetAllianceCastleData())
                {
                    LogicResourceData refData = data.GetWarResourceReferenceData();

                    int resourceCount = this.m_resourceCount[i];

                    if (refData != null)
                    {
                        int warLootPercentage     = LogicDataTables.GetGlobals().GetWarLootPercentage();
                        int lootableResourceCount = 0;

                        if ((this.m_parent.GetLevel().GetMatchType() | 4) != 7 && !this.m_parent.GetLevel().IsArrangedWar())
                        {
                            lootableResourceCount = (int)((long)resourceCount * warLootPercentage / 100);
                        }

                        int storageLootCap   = LogicDataTables.GetTownHallLevel(homeOwnerAvatar.GetTownHallLevel()).GetStorageLootCap(data);
                        int maxResourceCount = LogicMath.Min(homeOwnerAvatar.GetResourceCount(data), homeOwnerAvatar.GetResourceCap(data));

                        if (maxResourceCount > storageLootCap && maxResourceCount > 0)
                        {
                            int clampedValue;

                            if (storageLootCap < 1000000)
                            {
                                if (storageLootCap < 100000)
                                {
                                    if (storageLootCap < 10000)
                                    {
                                        clampedValue = storageLootCap < 1000
                                            ? (resourceCount * storageLootCap + (maxResourceCount >> 1)) / maxResourceCount
                                            : 10 * ((resourceCount * (storageLootCap / 10) + (maxResourceCount >> 1)) / maxResourceCount);
                                    }
                                    else
                                    {
                                        clampedValue = 100 * ((resourceCount * (storageLootCap / 100) + (maxResourceCount >> 1)) / maxResourceCount);
                                    }
                                }
                                else
                                {
                                    clampedValue = 1000 * ((resourceCount * (storageLootCap / 1000) + (maxResourceCount >> 1)) / maxResourceCount);
                                }
                            }
                            else
                            {
                                clampedValue = 40000 * ((resourceCount * (storageLootCap / 40000) + (maxResourceCount >> 1)) / maxResourceCount);
                            }

                            if (lootableResourceCount > clampedValue)
                            {
                                lootableResourceCount = clampedValue;
                            }
                        }

                        if (lootableResourceCount > resourceCount)
                        {
                            lootableResourceCount = resourceCount;
                        }

                        this.m_stealableResourceCount[i] = lootableResourceCount;
                    }
                }
            }
        }