public void AddDefendingHero()
        {
            LogicAvatar visitorAvatar   = this.m_parent.GetLevel().GetVisitorAvatar();
            LogicAvatar homeOwnerAvatar = this.m_parent.GetLevel().GetHomeOwnerAvatar();

            int randomPatrolPoint = visitorAvatar != null
                ? (int)(((visitorAvatar.GetResourceCount(LogicDataTables.GetGoldData()) + 10 * this.m_hero.GetGlobalID()) & 0x7FFFFFFFu) % this.m_patrolPath.Size())
                : 0;
            int upgLevel      = homeOwnerAvatar.GetUnitUpgradeLevel(this.m_hero);
            int heroHitpoints = this.m_hero.GetHeroHitpoints(homeOwnerAvatar.GetHeroHealth(this.m_hero), upgLevel);

            if (this.m_hero.HasEnoughHealthForAttack(heroHitpoints, upgLevel))
            {
                LogicVector2   patrolPoint = this.m_patrolPath[randomPatrolPoint];
                LogicCharacter hero        = (LogicCharacter)LogicGameObjectFactory.CreateGameObject(this.m_hero, this.m_parent.GetLevel(), this.m_parent.GetVillageType());

                hero.GetMovementComponent().SetBaseBuilding((LogicBuilding)this.m_parent);
                hero.GetHitpointComponent().SetTeam(1);
                hero.SetUpgradeLevel(upgLevel);
                hero.GetHitpointComponent().SetHitpoints(heroHitpoints);

                hero.SetInitialPosition(patrolPoint.m_x, patrolPoint.m_y);

                this.m_parent.GetGameObjectManager().AddGameObject(hero, -1);

                hero.GetCombatComponent().SetSearchRadius(this.m_hero.GetMaxSearchRadiusForDefender() / 512);

                if (LogicDataTables.GetGlobals().EnableDefendingAllianceTroopJump())
                {
                    hero.GetMovementComponent().EnableJump(3600000);
                }
            }
        }
Ejemplo n.º 2
0
        public virtual void ResourcesStolen(int damage, int hp)
        {
            if (damage > 0 && hp > 0)
            {
                LogicDataTable table = LogicDataTables.GetTable(LogicDataType.RESOURCE);

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

                    int stealableResource = this.GetStealableResourceCount(i);

                    if (damage < hp)
                    {
                        stealableResource = damage * stealableResource / hp;
                    }

                    if (stealableResource > 0)
                    {
                        this.m_parent.GetLevel().GetBattleLog().IncreaseStolenResourceCount(data, stealableResource);
                        this.m_resourceCount[i] -= stealableResource;

                        LogicAvatar homeOwnerAvatar = this.m_parent.GetLevel().GetHomeOwnerAvatar();
                        LogicAvatar visitorAvatar   = this.m_parent.GetLevel().GetVisitorAvatar();

                        homeOwnerAvatar.CommodityCountChangeHelper(0, data, -stealableResource);
                        visitorAvatar.CommodityCountChangeHelper(0, data, stealableResource);

                        if (homeOwnerAvatar.IsNpcAvatar())
                        {
                            LogicNpcData npcData = ((LogicNpcAvatar)homeOwnerAvatar).GetNpcData();

                            if (data == LogicDataTables.GetGoldData())
                            {
                                visitorAvatar.CommodityCountChangeHelper(1, npcData, stealableResource);
                            }
                            else if (data == LogicDataTables.GetElixirData())
                            {
                                visitorAvatar.CommodityCountChangeHelper(2, npcData, stealableResource);
                            }
                        }

                        this.m_stealableResourceCount[i] = LogicMath.Max(this.m_stealableResourceCount[i] - stealableResource, 0);
                    }
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Sets the npc data.
        /// </summary>
        public void SetNpcData(LogicNpcData data)
        {
            this._npcData = data;

            if (this._unitCount.Count != 0)
            {
                do
                {
                    this._unitCount[0].Destruct();
                    this._unitCount.Remove(0);
                } while (this._unitCount.Count != 0);

                this._unitCount = null;
            }

            this.SetResourceCount(LogicDataTables.GetGoldData(), this._npcData.Gold);
            this.SetResourceCount(LogicDataTables.GetElixirData(), this._npcData.Elixir);

            if (this._allianceUnitCount.Count != 0)
            {
                this.ClearUnitSlotArray(this._allianceUnitCount);

                do
                {
                    this._allianceUnitCount[0].Destruct();
                    this._allianceUnitCount.Remove(0);
                } while (this._allianceUnitCount.Count != 0);

                this._allianceUnitCount = null;
            }

            if (this._unitCount.Count != 0)
            {
                this.ClearDataSlotArray(this._unitCount);

                do
                {
                    this._unitCount[0].Destruct();
                    this._unitCount.Remove(0);
                } while (this._unitCount.Count != 0);

                this._unitCount = null;
            }

            this._allianceUnitCount = new LogicArrayList <LogicUnitSlot>();
            this._unitCount         = this._npcData.GetClonedUnits();
        }
        public override void Save(LogicJSONObject jsonObject, int villageType)
        {
            LogicDataTable resourceTable = LogicDataTables.GetTable(LogicDataType.RESOURCE);

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

                if (!resourceData.IsPremiumCurrency() && resourceData.GetWarResourceReferenceData() == null)
                {
                    if (LogicDataTables.GetGoldData() == resourceData)
                    {
                        int count = this.GetResourceCount(i);

                        if (count > 0)
                        {
                            jsonObject.Put("defg", new LogicJSONNumber(count));
                        }
                    }
                    else if (LogicDataTables.GetElixirData() == resourceData)
                    {
                        int count = this.GetResourceCount(i);

                        if (count > 0)
                        {
                            jsonObject.Put("defe", new LogicJSONNumber(count));
                        }
                    }
                    else if (LogicDataTables.GetDarkElixirData() == resourceData)
                    {
                        int count = this.GetResourceCount(i);

                        if (count > 0)
                        {
                            jsonObject.Put("defde", new LogicJSONNumber(count));
                        }
                    }
                }
            }
        }
        public override void Load(LogicJSONObject jsonObject)
        {
            LogicDataTable resourceTable = LogicDataTables.GetTable(LogicDataType.RESOURCE);

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

                if (!resourceData.IsPremiumCurrency() && resourceData.GetWarResourceReferenceData() == null)
                {
                    if (LogicDataTables.GetGoldData() == resourceData)
                    {
                        LogicJSONNumber count = jsonObject.GetJSONNumber("defg");

                        if (count != null)
                        {
                            this.SetResourceCount(i, count.GetIntValue());
                        }
                    }
                    else if (LogicDataTables.GetElixirData() == resourceData)
                    {
                        LogicJSONNumber count = jsonObject.GetJSONNumber("defe");

                        if (count != null)
                        {
                            this.SetResourceCount(i, count.GetIntValue());
                        }
                    }
                    else if (LogicDataTables.GetDarkElixirData() == resourceData)
                    {
                        LogicJSONNumber count = jsonObject.GetJSONNumber("defde");

                        if (count != null)
                        {
                            this.SetResourceCount(i, count.GetIntValue());
                        }
                    }
                }
            }
        }
        public void SetNpcData(LogicNpcData data)
        {
            this.m_npcData = data;

            this.SetResourceCount(LogicDataTables.GetGoldData(), this.m_npcData.GetGoldCount());
            this.SetResourceCount(LogicDataTables.GetElixirData(), this.m_npcData.GetElixirCount());

            if (this.m_allianceUnitCount.Size() != 0)
            {
                this.ClearUnitSlotArray(this.m_allianceUnitCount);
                this.m_allianceUnitCount = null;
            }

            if (this.m_unitCount.Size() != 0)
            {
                this.ClearDataSlotArray(this.m_unitCount);
                this.m_unitCount = null;
            }

            this.m_allianceUnitCount = new LogicArrayList <LogicUnitSlot>();
            this.m_unitCount         = this.m_npcData.GetClonedUnits();
        }
Ejemplo n.º 7
0
        /// <summary>
        ///     Loads the npc attack state.
        /// </summary>
        public void LoadNpcAttackState(LogicClientHome home, LogicAvatar homeOwnerAvatar, LogicAvatar visitorAvatar, int currentTimestamp, int secondsSinceLastSave)
        {
            if (this._state == 1)
            {
                Debugger.Error("loadAttackState called from invalid state");
            }
            else
            {
                this._state            = 2;
                this._currentTimestamp = currentTimestamp;
                this._calendar.Load(home.GetCalendarJSON(), currentTimestamp);

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

                if (homeOwnerAvatar.IsNpcAvatar())
                {
                    LogicNpcAvatar npcAvatar = (LogicNpcAvatar)homeOwnerAvatar;
                    LogicNpcData   npcData   = npcAvatar.GetNpcData();

                    homeOwnerAvatar.SetResourceCount(LogicDataTables.GetGoldData(), LogicMath.Max(npcData.Gold - visitorAvatar.GetLootedNpcGold(npcData), 0));
                    homeOwnerAvatar.SetResourceCount(LogicDataTables.GetElixirData(), LogicMath.Max(npcData.Elixir - visitorAvatar.GetLootedNpcElixir(npcData), 0));

                    this._level.SetMatchType(2, 0);
                    this._level.SetHome(home, false);
                    this._level.SetHomeOwnerAvatar(homeOwnerAvatar);
                    this._level.SetVisitorAvatar(visitorAvatar);
                    this._level.FastForwardTime(secondsSinceLastSave);
                    this._level.LoadingFinished();
                }
                else
                {
                    Debugger.Error("loadNpcAttackState called and home owner is not npc avatar");
                }
            }
        }
        public override int Execute(LogicLevel level)
        {
            if (this.m_layoutId == 7)
            {
                return(-21);
            }

            if (this.m_village2)
            {
                if (this.m_layoutId != 0 && this.m_layoutId != 2 && this.m_layoutId != 3)
                {
                    return(-22);
                }
            }

            if (LogicDataTables.GetGlobals().UseVersusBattle())
            {
                int villageType = this.m_village2 ? 1 : 0;

                if (level.GetTownHallLevel(villageType) < level.GetRequiredTownHallLevelForLayout(this.m_layoutId, villageType))
                {
                    return(-3);
                }

                if (level.GetPlayerAvatar() == null)
                {
                    return(-10);
                }

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

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

                level.GetGameObjectManagerAt(this.m_village2 ? 1 : 0).GetGameObjects(gameObjects, filter);

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

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

                gameObjects.Destruct();
                filter.Destruct();

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

                    if (homeOwnerAvatar == null || homeOwnerAvatar.IsChallengeStarted())
                    {
                        if (level.GetLayoutCooldown(this.m_layoutId) > 0)
                        {
                            return(-7);
                        }
                    }
                }

                LogicBuilding allianceCastle = level.GetGameObjectManagerAt(0).GetAllianceCastle();

                if (allianceCastle != null)
                {
                    LogicBunkerComponent bunkerComponent = allianceCastle.GetBunkerComponent();

                    if (bunkerComponent == null || bunkerComponent.GetChallengeCooldownTime() != 0)
                    {
                        return(-6);
                    }

                    LogicClientAvatar playerAvatar = level.GetPlayerAvatar();

                    if (!this.m_challenge)
                    {
                        if (playerAvatar.GetChallengeId() != null)
                        {
                            int challengeState = playerAvatar.GetChallengeState();

                            if (challengeState != 2 && challengeState != 4)
                            {
                                Debugger.Warning("chal state: " + challengeState);
                                return(-8);
                            }
                        }
                    }

                    int friendlyCost = LogicDataTables.GetGlobals().GetFriendlyBattleCost(playerAvatar.GetTownHallLevel());

                    if (friendlyCost != 0)
                    {
                        if (!playerAvatar.HasEnoughResources(LogicDataTables.GetGoldData(), friendlyCost, true, this, false))
                        {
                            return(0);
                        }

                        if (friendlyCost > 0)
                        {
                            playerAvatar.CommodityCountChangeHelper(0, LogicDataTables.GetGoldData(), friendlyCost);
                        }
                    }

                    bunkerComponent.StartChallengeCooldownTime();

                    bool warLayout = this.m_layoutId == 1 || this.m_layoutId == 4 || this.m_layoutId == 5;

                    if (this.m_village2)
                    {
                        if (this.m_challenge)
                        {
                            playerAvatar.GetChangeListener().SendChallengeRequest(this.m_message, this.m_layoutId, warLayout, villageType);
                        }
                        else
                        {
                            playerAvatar.GetChangeListener().SendFriendlyBattleRequest(this.m_message, this.m_battleId, this.m_layoutId, warLayout, villageType);
                        }
                    }
                    else
                    {
                        this.SaveChallengeLayout(level, warLayout);

                        if (this.m_challenge)
                        {
                            playerAvatar.GetChangeListener().SendChallengeRequest(this.m_message, this.m_layoutId, warLayout, villageType);
                        }
                        else
                        {
                            playerAvatar.GetChangeListener().SendFriendlyBattleRequest(this.m_message, this.m_battleId, this.m_layoutId, warLayout, villageType);
                        }

                        playerAvatar.SetVariableByName("ChallengeStarted", 1);
                    }

                    return(0);
                }

                return(-3);
            }

            return(2);
        }
        public static void CalculateCombatScore(LogicClientAvatar attacker, LogicClientAvatar defender, int stars, bool ignoreLeague, bool revenge, int destructionPercentage,
                                                int starBonusMultiplier, bool duelMatch)
        {
            int multiplier = stars;

            if (stars <= 0)
            {
                multiplier = LogicDataTables.GetGlobals().GetScoreMultiplierOnAttackLose();
            }

            int attackerScore = attacker.GetScore();
            int defenderScore = defender.GetScore();

            LogicLeagueData attackerLeagueData = attacker.GetLeagueTypeData();
            LogicLeagueData defenderLeagueData = defender.GetLeagueTypeData();

            int newAttackerScore;
            int newDefenderScore;

            if (LogicDataTables.GetGlobals().EloOffsetDampeningEnabled())
            {
                newAttackerScore = LogicELOMath.CalculateNewRating(stars > 0, attackerScore, defenderScore, 20 * multiplier, LogicDataTables.GetGlobals().GetEloDampeningFactor(),
                                                                   LogicDataTables.GetGlobals().GetEloDampeningLimit(), LogicDataTables.GetGlobals().GetEloDampeningScoreLimit());
                newDefenderScore = LogicELOMath.CalculateNewRating(stars <= 0, defenderScore, attackerScore, 20 * multiplier, LogicDataTables.GetGlobals().GetEloDampeningFactor(),
                                                                   LogicDataTables.GetGlobals().GetEloDampeningLimit(), LogicDataTables.GetGlobals().GetEloDampeningScoreLimit());
            }
            else
            {
                newAttackerScore = LogicELOMath.CalculateNewRating(stars > 0, attackerScore, defenderScore, 20 * multiplier);
                newDefenderScore = LogicELOMath.CalculateNewRating(stars <= 0, defenderScore, attackerScore, 20 * multiplier);
            }

            int attackerGainCount = newAttackerScore - attackerScore;
            int defenderGainCount = newDefenderScore - defenderScore;

            if (attackerScore < 1000 && attackerGainCount < 0)
            {
                attackerGainCount = attackerScore * attackerGainCount / 1000;
            }

            if (defenderScore < 1000 && defenderGainCount < 0)
            {
                defenderGainCount = defenderScore * defenderGainCount / 1000;
            }

            if (LogicELOMath.CalculateNewRating(true, attackerScore, defenderScore, 60) > attackerScore)
            {
                if (stars <= 0)
                {
                    if (attackerGainCount >= 0)
                    {
                        attackerGainCount = -1;
                    }
                }
                else
                {
                    if (attackerGainCount <= 0)
                    {
                        attackerGainCount = 1;
                    }

                    if (defenderGainCount >= 0)
                    {
                        defenderGainCount = -1;
                    }
                }
            }

            newAttackerScore = LogicMath.Max(attackerScore + attackerGainCount, 0);
            newDefenderScore = LogicMath.Max(defenderScore + defenderGainCount, 0);

            if (!ignoreLeague)
            {
                attacker.SetScore(newAttackerScore);
                defender.SetScore(newDefenderScore);

                if (LogicDataTables.GetGlobals().EnableLeagues())
                {
                    if (!duelMatch)
                    {
                        if (attackerLeagueData != null)
                        {
                            if (stars <= 0)
                            {
                                attacker.SetAttackLoseCount(attacker.GetAttackLoseCount() + 1);
                            }
                            else
                            {
                                attacker.SetAttackWinCount(attacker.GetAttackWinCount() + 1);
                            }
                        }

                        if (defenderLeagueData != null)
                        {
                            if (stars > 0)
                            {
                                defender.SetDefenseLoseCount(defender.GetDefenseLoseCount() + 1);
                            }
                            else
                            {
                                defender.SetDefenseWinCount(defender.GetDefenseLoseCount() + 1);
                            }
                        }

                        if (stars > 0)
                        {
                            if (!revenge || LogicDataTables.GetGlobals().RevengeGiveLeagueBonus())
                            {
                                int leagueBonusPercentage = LogicDataTables.GetGlobals().GetLeagueBonusPercentage(destructionPercentage);

                                attacker.CommodityCountChangeHelper(0, LogicDataTables.GetGoldData(), attackerLeagueData.GetGoldReward() * leagueBonusPercentage / 100);
                                attacker.CommodityCountChangeHelper(0, LogicDataTables.GetElixirData(), attackerLeagueData.GetElixirReward() * leagueBonusPercentage / 100);

                                if (attacker.IsDarkElixirUnlocked())
                                {
                                    attacker.CommodityCountChangeHelper(0, LogicDataTables.GetDarkElixirData(),
                                                                        attackerLeagueData.GetDarkElixirReward() * leagueBonusPercentage / 100);
                                }
                            }
                        }

                        LogicGamePlayUtil.UpdateLeagueRank(attacker, newAttackerScore, false);
                        LogicGamePlayUtil.UpdateLeagueRank(defender, newDefenderScore, true);
                    }
                }
            }

            attacker.GetChangeListener().ScoreChanged(attacker.GetCurrentHomeId(), attackerGainCount, stars > 0 ? 1 : -1, true, attacker.GetLeagueTypeData(), attackerLeagueData,
                                                      destructionPercentage);
            defender.GetChangeListener().ScoreChanged(defender.GetCurrentHomeId(), defenderGainCount, stars > 0 ? -1 : 1, false, defender.GetLeagueTypeData(), defenderLeagueData,
                                                      destructionPercentage);

            if (stars > 0 && !ignoreLeague && !duelMatch && (!revenge || LogicDataTables.GetGlobals().RevengeGiveStarBonus()) && !attacker.GetStarBonusCooldown())
            {
                int totalStars = stars + attacker.GetStarBonusCounter();

                if (totalStars >= LogicDataTables.GetGlobals().GetStarBonusStarCount())
                {
                    LogicLeagueData leagueData = attacker.GetLeagueTypeData();

                    attacker.AddStarBonusReward(leagueData.GetGoldRewardStarBonus() * starBonusMultiplier, leagueData.GetElixirRewardStarBonus() * starBonusMultiplier,
                                                leagueData.GetDarkElixirRewardStarBonus() * starBonusMultiplier);
                    attacker.StarBonusCollected();

                    if (LogicDataTables.GetGlobals().AllowStarsOverflowInStarBonus() && !attacker.GetStarBonusCooldown())
                    {
                        totalStars %= LogicDataTables.GetGlobals().GetStarBonusStarCount();
                    }
                    else
                    {
                        totalStars = 0;
                    }
                }

                attacker.SetStarBonusCounter(totalStars);
            }
        }