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); }
/// <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); }