public override void ApplyAvatarChange(LogicClientAvatar avatar)
 {
     avatar.SetExpPoints(avatar.GetExpPoints() + this.Points);
 }
Beispiel #2
0
        public override int Execute(LogicLevel level)
        {
            LogicClientAvatar playerAvatar = level.GetPlayerAvatar();

            if (playerAvatar != null)
            {
                if (this.m_unitData != null)
                {
                    if (this.m_unitData.GetVillageType() == 0)
                    {
                        if (!this.m_unitData.IsDonationDisabled())
                        {
                            bool containsUnit = false;
                            int  upgLevel     = playerAvatar.GetUnitUpgradeLevel(this.m_unitData);

                            LogicUnitProductionComponent unitProductionComponent = null;

                            if (this.m_quickDonate)
                            {
                                int cost = this.m_unitData.GetDonateCost();

                                if (!playerAvatar.HasEnoughDiamonds(cost, true, level) || !LogicDataTables.GetGlobals().EnableQuickDonateWar())
                                {
                                    playerAvatar.GetChangeListener().WarDonateFailed(this.m_unitData, upgLevel, this.m_streamId, this.m_quickDonate);
                                    return(0);
                                }
                            }
                            else
                            {
                                LogicGameObjectManager gameObjectManager = level.GetGameObjectManagerAt(0);

                                if (LogicDataTables.GetGlobals().UseNewTraining())
                                {
                                    LogicUnitProduction unitProduction  = gameObjectManager.GetUnitProduction();
                                    LogicUnitProduction spellProduction = gameObjectManager.GetSpellProduction();

                                    if (unitProduction.GetWaitingForSpaceUnitCount(this.m_unitData) > 0)
                                    {
                                        if (unitProduction.GetUnitProductionType() == this.m_unitData.GetDataType())
                                        {
                                            containsUnit = true;
                                        }
                                    }

                                    if (spellProduction.GetWaitingForSpaceUnitCount(this.m_unitData) > 0)
                                    {
                                        if (spellProduction.GetUnitProductionType() == this.m_unitData.GetDataType())
                                        {
                                            containsUnit = true;
                                        }
                                    }
                                }
                                else
                                {
                                    for (int i = 0, c = gameObjectManager.GetNumGameObjects(); i < c; i++)
                                    {
                                        LogicGameObject gameObject = gameObjectManager.GetGameObjectByIndex(i);

                                        if (gameObject.GetGameObjectType() == LogicGameObjectType.BUILDING)
                                        {
                                            LogicBuilding building = (LogicBuilding)gameObject;
                                            LogicUnitProductionComponent component = building.GetUnitProductionComponent();

                                            if (component != null)
                                            {
                                                unitProductionComponent = component;

                                                if (component.ContainsUnit(this.m_unitData))
                                                {
                                                    if (component.GetRemainingSeconds() == 0 && component.GetCurrentlyTrainedUnit() == this.m_unitData)
                                                    {
                                                        containsUnit = true;
                                                    }
                                                }
                                                else
                                                {
                                                    unitProductionComponent = null;
                                                }
                                            }
                                        }
                                    }
                                }

                                if (!containsUnit)
                                {
                                    if (playerAvatar.GetUnitCount(this.m_unitData) <= 0)
                                    {
                                        playerAvatar.GetChangeListener().WarDonateFailed(this.m_unitData, upgLevel, this.m_streamId, this.m_quickDonate);
                                        return(0);
                                    }
                                }
                            }

                            if (this.m_unitData.GetCombatItemType() != LogicCombatItemData.COMBAT_ITEM_TYPE_CHARACTER)
                            {
                                playerAvatar.XpGainHelper(this.m_unitData.GetHousingSpace() * LogicDataTables.GetGlobals().GetDarkSpellDonationXP());
                                level.GetAchievementManager().AlianceSpellDonated((LogicSpellData)this.m_unitData);
                            }
                            else
                            {
                                playerAvatar.XpGainHelper(((LogicCharacterData)this.m_unitData).GetDonateXP());
                                level.GetAchievementManager().AlianceUnitDonated((LogicCharacterData)this.m_unitData);
                            }

                            playerAvatar.GetChangeListener().WarDonateOk(this.m_unitData, upgLevel, this.m_streamId, this.m_quickDonate);

                            if (this.m_quickDonate)
                            {
                                int cost = this.m_unitData.GetDonateCost();

                                playerAvatar.UseDiamonds(cost);
                                playerAvatar.GetChangeListener().DiamondPurchaseMade(12, this.m_unitData.GetGlobalID(), 0, cost, level.GetVillageType());

                                if (level.GetState() == 1 || level.GetState() == 3 &&
                                    this.m_unitData.GetCombatItemType() == LogicCombatItemData.COMBAT_ITEM_TYPE_CHARACTER)
                                {
                                    // TODO: Finish this.
                                }
                            }
                            else
                            {
                                if (!containsUnit)
                                {
                                    playerAvatar.CommodityCountChangeHelper(0, this.m_unitData, -1);
                                }

                                LogicResourceData trainingResource = this.m_unitData.GetTrainingResource();
                                int trainingCost = level.GetGameMode().GetCalendar().GetTrainingCost(this.m_unitData, upgLevel);
                                int refund       = playerAvatar.GetTroopDonationRefund() * trainingCost / 100;

                                playerAvatar.CommodityCountChangeHelper(0, trainingResource, LogicMath.Max(refund, 0));

                                if (level.GetState() == 1 || level.GetState() == 3)
                                {
                                    if (containsUnit)
                                    {
                                        if (LogicDataTables.GetGlobals().UseNewTraining())
                                        {
                                            LogicGameObjectManager gameObjectManager = level.GetGameObjectManagerAt(0);
                                            LogicUnitProduction    unitProduction    = this.m_unitData.GetCombatItemType() != LogicCombatItemData.COMBAT_ITEM_TYPE_CHARACTER
                                                ? gameObjectManager.GetSpellProduction()
                                                : gameObjectManager.GetUnitProduction();

                                            unitProduction.RemoveTrainedUnit(this.m_unitData);
                                        }

                                        if (this.m_unitData.GetCombatItemType() == LogicCombatItemData.COMBAT_ITEM_TYPE_CHARACTER)
                                        {
                                            LogicBuilding productionHouse = null;

                                            if (unitProductionComponent != null)
                                            {
                                                productionHouse = (LogicBuilding)unitProductionComponent.GetParent();
                                            }
                                            else
                                            {
                                                if (LogicDataTables.GetGlobals().UseTroopWalksOutFromTraining())
                                                {
                                                    LogicGameObjectManager gameObjectManager = level.GetGameObjectManagerAt(0);
                                                    int gameObjectCount = gameObjectManager.GetNumGameObjects();

                                                    for (int i = 0; i < gameObjectCount; i++)
                                                    {
                                                        LogicGameObject gameObject = gameObjectManager.GetGameObjectByIndex(i);

                                                        if (gameObject != null && gameObject.GetGameObjectType() == LogicGameObjectType.BUILDING)
                                                        {
                                                            LogicBuilding tmpBuilding = (LogicBuilding)gameObject;
                                                            LogicUnitProductionComponent tmpComponent = tmpBuilding.GetUnitProductionComponent();

                                                            if (tmpComponent != null)
                                                            {
                                                                if (tmpComponent.GetProductionType() == this.m_unitData.GetCombatItemType())
                                                                {
                                                                    if (tmpBuilding.GetBuildingData().GetProducesUnitsOfType() == this.m_unitData.GetUnitOfType() &&
                                                                        !tmpBuilding.IsUpgrading() &&
                                                                        !tmpBuilding.IsConstructing())
                                                                    {
                                                                        if (this.m_unitData.IsUnlockedForProductionHouseLevel(tmpBuilding.GetUpgradeLevel()))
                                                                        {
                                                                            if (productionHouse != null)
                                                                            {
                                                                                int seed = playerAvatar.GetExpPoints();

                                                                                if (tmpBuilding.Rand(seed) % 1000 > 750)
                                                                                {
                                                                                    productionHouse = tmpBuilding;
                                                                                }
                                                                            }
                                                                            else
                                                                            {
                                                                                productionHouse = tmpBuilding;
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }

                                            if (productionHouse != null)
                                            {
                                                // TODO: Implement listener.
                                            }
                                        }
                                    }
                                    else
                                    {
                                        LogicArrayList <LogicComponent> components = level.GetComponentManagerAt(0).GetComponents(LogicComponentType.UNIT_STORAGE);

                                        for (int i = 0; i < components.Size(); i++)
                                        {
                                            LogicUnitStorageComponent unitStorageComponent = (LogicUnitStorageComponent)components[i];
                                            int idx = unitStorageComponent.GetUnitTypeIndex(this.m_unitData);

                                            if (idx != -1)
                                            {
                                                if (unitStorageComponent.GetUnitCount(idx) > 0)
                                                {
                                                    unitStorageComponent.RemoveUnits(this.m_unitData, 1);

                                                    if (LogicDataTables.GetGlobals().UseNewTraining())
                                                    {
                                                        LogicGameObjectManager gameObjectManager = level.GetGameObjectManagerAt(0);
                                                        LogicUnitProduction    unitProduction    = this.m_unitData.GetCombatItemType() != LogicCombatItemData.COMBAT_ITEM_TYPE_CHARACTER
                                                            ? gameObjectManager.GetSpellProduction()
                                                            : gameObjectManager.GetUnitProduction();

                                                        unitProduction.MergeSlots();
                                                        unitProduction.UnitRemoved();
                                                    }

                                                    break;
                                                }
                                            }
                                        }
                                    }

                                    // TODO: Finish this.
                                }
                            }

                            return(0);
                        }

                        return(-91);
                    }

                    return(-45);
                }
            }

            return(0);
        }