public void ReengageLootCart(int secs)
        {
            LogicObstacleData      obstacleData      = this.GetObstacleData();
            LogicLootCartComponent lootCartComponent = (LogicLootCartComponent)this.GetComponent(LogicComponentType.LOOT_CART);
            LogicBuilding          townHall          = this.m_level.GetGameObjectManagerAt(0).GetTownHall();

            Debugger.DoAssert(obstacleData.IsLootCart(), string.Empty);
            Debugger.DoAssert(lootCartComponent != null, string.Empty);
            Debugger.DoAssert(townHall != null, string.Empty);

            LogicDataTable resourceTable = LogicDataTables.GetTable(LogicDataType.RESOURCE);

            for (int i = 0; i < resourceTable.GetItemCount(); i++)
            {
                LogicResourceData      resourceData      = (LogicResourceData)resourceTable.GetItemAt(i);
                LogicTownhallLevelData townhallLevelData = LogicDataTables.GetTownHallLevel(townHall.GetUpgradeLevel());

                int cap = secs * townhallLevelData.GetCartLootReengagement(resourceData) / 100;

                if (cap > lootCartComponent.GetResourceCount(i))
                {
                    lootCartComponent.SetResourceCount(i, cap);
                }
            }
        }
        public void ClearingFinished(bool ignoreState)
        {
            int state = this.m_level.GetState();

            if (state == 1 || !LogicDataTables.GetGlobals().CompleteConstructionOnlyHome() && ignoreState)
            {
                if (this.m_level.GetHomeOwnerAvatar().IsClientAvatar())
                {
                    LogicClientAvatar homeOwnerAvatar  = (LogicClientAvatar)this.m_level.GetHomeOwnerAvatar();
                    LogicObstacleData obstacleData     = this.GetObstacleData();
                    LogicResourceData lootResourceData = obstacleData.GetLootResourceData();

                    int lootCount = obstacleData.GetLootCount();

                    if (obstacleData.IsLootCart())
                    {
                        LogicLootCartComponent lootCartComponent = (LogicLootCartComponent)this.GetComponent(LogicComponentType.LOOT_CART);

                        if (lootCartComponent != null)
                        {
                            LogicDataTable resourceTable = LogicDataTables.GetTable(LogicDataType.RESOURCE);

                            bool empty = true;

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

                                if (!resourceData.IsPremiumCurrency() && resourceData.GetWarResourceReferenceData() == null)
                                {
                                    int resourceCount  = lootCartComponent.GetResourceCount(i);
                                    int rewardCount    = LogicMath.Min(homeOwnerAvatar.GetUnusedResourceCap(resourceData), resourceCount);
                                    int remainingCount = resourceCount - rewardCount;

                                    if (rewardCount > 0)
                                    {
                                        homeOwnerAvatar.CommodityCountChangeHelper(0, resourceData, rewardCount);
                                        lootCartComponent.SetResourceCount(i, remainingCount);
                                    }

                                    if (remainingCount > 0)
                                    {
                                        empty = false;
                                    }
                                }
                            }

                            if (!empty)
                            {
                                return;
                            }
                        }
                    }

                    if (!obstacleData.IsTombstone() && !obstacleData.IsLootCart())
                    {
                        this.m_level.GetAchievementManager().ObstacleCleared();
                    }

                    this.m_level.GetWorkerManagerAt(this.m_villageType).DeallocateWorker(this);
                    this.XpGainHelper(LogicGamePlayUtil.TimeToExp(obstacleData.GetClearTime()), homeOwnerAvatar, ignoreState || state == 1);

                    if (lootResourceData != null && lootCount > 0)
                    {
                        if (homeOwnerAvatar != null)
                        {
                            if (lootResourceData.IsPremiumCurrency())
                            {
                                int lootMultipler = 1;

                                if (this.m_lootMultiplyVersion >= 2)
                                {
                                    lootMultipler = obstacleData.GetLootMultiplierVersion2();
                                }

                                int diamondsCount = obstacleData.GetName().Equals("Bonus Gembox")
                                    ? lootCount * lootMultipler
                                    : this.m_level.GetGameObjectManagerAt(this.m_villageType).IncreaseObstacleClearCounter(lootMultipler);

                                if (diamondsCount > 0)
                                {
                                    homeOwnerAvatar.SetDiamonds(homeOwnerAvatar.GetDiamonds() + diamondsCount);
                                    homeOwnerAvatar.SetFreeDiamonds(homeOwnerAvatar.GetFreeDiamonds() + diamondsCount);
                                    homeOwnerAvatar.GetChangeListener().FreeDiamondsAdded(diamondsCount, 6);
                                }
                            }
                            else
                            {
                                int gainCount = LogicMath.Min(homeOwnerAvatar.GetUnusedResourceCap(lootResourceData), lootCount);

                                if (gainCount > 0)
                                {
                                    homeOwnerAvatar.CommodityCountChangeHelper(0, lootResourceData, gainCount);
                                }
                            }
                        }
                        else
                        {
                            Debugger.Error("LogicObstacle::clearingFinished - Home owner avatar is NULL!");
                        }
                    }

                    if (obstacleData.IsEnabledInVillageType(this.m_level.GetVillageType()))
                    {
                        // ?
                    }

                    if (this.m_clearTimer != null)
                    {
                        this.m_clearTimer.Destruct();
                        this.m_clearTimer = null;
                    }

                    this.m_fadeTime = 1;
                }
            }
        }
示例#3
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;
        }