示例#1
0
        public void AddRewardUnits()
        {
            LogicCharacterData characterData = this.m_data.GetRewardCharacterData();

            if (characterData != null)
            {
                int characterCount = this.m_data.GetRewardCharacterCount();

                if (characterCount > 0)
                {
                    LogicClientAvatar    playerAvatar = this.m_level.GetPlayerAvatar();
                    LogicComponentFilter filter       = new LogicComponentFilter();

                    for (int i = 0; i < characterCount; i++)
                    {
                        filter.RemoveAllIgnoreObjects();

                        while (true)
                        {
                            LogicUnitStorageComponent component =
                                (LogicUnitStorageComponent)this.m_level.GetComponentManagerAt(this.m_level.GetVillageType()).GetClosestComponent(0, 0, filter);

                            if (component != null)
                            {
                                if (component.CanAddUnit(characterData))
                                {
                                    playerAvatar.CommodityCountChangeHelper(0, characterData, 1);
                                    component.AddUnit(characterData);

                                    if (this.m_level.GetState() == 1 || this.m_level.GetState() == 3)
                                    {
                                        if (component.GetParentListener() != null)
                                        {
                                            component.GetParentListener().ExtraCharacterAdded(characterData, null);
                                        }
                                    }

                                    break;
                                }

                                filter.AddIgnoreObject(component.GetParent());
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        ///     Executes this instance.
        /// </summary>
        public override int Execute(LogicLevel level)
        {
            for (int i = 0; i < this._unitsCount.Count; i++)
            {
                if (this._unitsCount[i] < 0)
                {
                    return(-1);
                }
            }

            if (LogicDataTables.GetGlobals().EnableTroopDeletion() && level.GetState() == 1 && this._unitsData.Count > 0)
            {
                LogicClientAvatar playerAvatar = level.GetPlayerAvatar();
                Int32             removedUnits = 0;

                for (int i = 0; i < this._unitsData.Count; i++)
                {
                    LogicCombatItemData data = this._unitsData[i];
                    Int32 unitCount          = this._unitsCount[i];

                    if (this._removeType[i] != 0)
                    {
                        Int32 upgLevel = this._unitsUpgLevel[i];

                        if (data.GetCombatItemType() != 0)
                        {
                            if (data.GetCombatItemType() == 1)
                            {
                                playerAvatar.SetAllianceUnitCount(data, upgLevel, LogicMath.Max(0, playerAvatar.GetAllianceUnitCount(data, upgLevel) - unitCount));

                                if (unitCount > 0)
                                {
                                    do
                                    {
                                        playerAvatar.GetChangeListener().AllianceUnitRemoved(data, upgLevel);
                                    } while (--unitCount != 0);
                                }

                                removedUnits |= 2;
                            }
                        }
                        else
                        {
                            LogicBuilding allianceCastle = level.GetGameObjectManagerAt(0).GetAllianceCastle();

                            if (allianceCastle != null)
                            {
                                LogicBunkerComponent bunkerComponent = allianceCastle.GetBunkerComponent();
                                Int32 unitTypeIndex = bunkerComponent.GetUnitTypeIndex(data);

                                if (unitTypeIndex != -1)
                                {
                                    Int32 cnt = bunkerComponent.GetUnitCount(unitTypeIndex);

                                    if (cnt > 0)
                                    {
                                        bunkerComponent.RemoveUnits(data, upgLevel, cnt);
                                        playerAvatar.SetAllianceUnitCount(data, upgLevel, LogicMath.Max(0, playerAvatar.GetAllianceUnitCount(data, upgLevel) - unitCount));

                                        removedUnits |= 1;

                                        if (unitCount > 0)
                                        {
                                            do
                                            {
                                                playerAvatar.GetChangeListener().AllianceUnitRemoved(data, upgLevel);
                                            } while (--unitCount != 0);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        if (playerAvatar != null && data != null)
                        {
                            playerAvatar.CommodityCountChangeHelper(0, data, -unitCount);
                        }

                        LogicArrayList <LogicComponent> components = level.GetComponentManager().GetComponents(0);

                        for (int j = 0; j < components.Count; j++)
                        {
                            if (unitCount > 0)
                            {
                                LogicUnitStorageComponent storageComponent = (LogicUnitStorageComponent)components[j];
                                Int32 unitTypeIndex = storageComponent.GetUnitTypeIndex(data);

                                if (unitTypeIndex != -1)
                                {
                                    Int32 cnt = storageComponent.GetUnitCount(unitTypeIndex);

                                    if (cnt > 0)
                                    {
                                        cnt = LogicMath.Min(cnt, unitCount);
                                        storageComponent.RemoveUnits(data, cnt);

                                        int type = 2;

                                        if (data.GetCombatItemType() == 0)
                                        {
                                            if (storageComponent.GetParentListener() != null)
                                            {
                                                LogicGameObjectListener listener = storageComponent.GetParentListener();

                                                for (int k = 0; k < cnt; k++)
                                                {
                                                    listener.UnitRemoved(data);
                                                }
                                            }

                                            type = 1;
                                        }

                                        unitCount    -= cnt;
                                        removedUnits |= type;
                                    }
                                }
                            }
                        }
                    }
                }

                switch (removedUnits)
                {
                case 3:
                    if (LogicDataTables.GetGlobals().UseNewTraining())
                    {
                        level.GetGameObjectManager().GetUnitProduction().MergeSlots();
                        level.GetGameObjectManager().GetSpellProduction().MergeSlots();
                    }
                    break;

                case 2:
                    if (LogicDataTables.GetGlobals().UseNewTraining())
                    {
                        level.GetGameObjectManager().GetSpellProduction().MergeSlots();
                    }
                    break;

                case 1:
                    if (LogicDataTables.GetGlobals().UseNewTraining())
                    {
                        level.GetGameObjectManager().GetUnitProduction().MergeSlots();
                    }
                    break;

                default:
                    Debugger.Print("WTF: " + removedUnits);
                    break;
                }
            }

            return(0);
        }