public bool HasEnoughFreeHousingSpace()
        {
            LogicCalendar       calendar        = this.m_level.GetCalendar();
            LogicAvatar         homeOwnerAvatar = this.m_level.GetHomeOwnerAvatar();
            LogicUnitProduction unitProduction  = this.m_level.GetGameObjectManagerAt(0).GetUnitProduction();
            LogicDataTable      characterTable  = LogicDataTables.GetTable(LogicDataType.CHARACTER);

            int freeHousingSpace     = unitProduction.GetMaxTrainCount() - (homeOwnerAvatar.GetUnitsTotalCapacity() - unitProduction.GetTotalCount());
            int requiredHousingSpace = 0;

            for (int i = 0; i < characterTable.GetItemCount(); i++)
            {
                LogicCharacterData data = (LogicCharacterData)characterTable.GetItemAt(i);

                if (calendar.IsProductionEnabled(data) && !data.IsSecondaryTroop())
                {
                    int count = homeOwnerAvatar.GetUnitPresetCount(data, this.m_presetId);

                    if (count > 0)
                    {
                        requiredHousingSpace += data.GetHousingSpace() * count;
                    }
                }
            }

            if (requiredHousingSpace <= freeHousingSpace)
            {
                LogicUnitProduction spellProduction = this.m_level.GetGameObjectManagerAt(0).GetSpellProduction();
                LogicDataTable      spellTable      = LogicDataTables.GetTable(LogicDataType.SPELL);

                int freeSpellHousingSpace     = spellProduction.GetMaxTrainCount() - (homeOwnerAvatar.GetSpellsTotalCapacity() - spellProduction.GetTotalCount());
                int requiredSpellHousingSpace = 0;

                for (int i = 0; i < spellTable.GetItemCount(); i++)
                {
                    LogicSpellData data = (LogicSpellData)spellTable.GetItemAt(i);

                    if (calendar.IsProductionEnabled(data))
                    {
                        int count = homeOwnerAvatar.GetUnitPresetCount(data, this.m_presetId);

                        if (count > 0)
                        {
                            requiredSpellHousingSpace += data.GetHousingSpace() * count;
                        }
                    }
                }

                return(requiredSpellHousingSpace <= freeSpellHousingSpace);
            }

            return(false);
        }
        public void TrainUnits()
        {
            LogicAvatar    homeOwnerAvatar = this.m_level.GetHomeOwnerAvatar();
            LogicDataTable characterTable  = LogicDataTables.GetTable(LogicDataType.CHARACTER);
            LogicDataTable spellTable      = LogicDataTables.GetTable(LogicDataType.SPELL);
            LogicArrayList <LogicCombatItemData> productionUnits  = new LogicArrayList <LogicCombatItemData>(characterTable.GetItemCount());
            LogicArrayList <LogicCombatItemData> productionSpells = new LogicArrayList <LogicCombatItemData>(spellTable.GetItemCount());

            for (int i = 0; i < characterTable.GetItemCount(); i++)
            {
                LogicCharacterData data = (LogicCharacterData)characterTable.GetItemAt(i);

                if (this.m_level.GetCalendar().IsProductionEnabled(data) && !data.IsSecondaryTroop())
                {
                    productionUnits.Add(data);
                }
            }

            this.SortProduction(productionUnits);

            for (int i = 0; i < productionUnits.Size(); i++)
            {
                int unitCount = homeOwnerAvatar.GetUnitPresetCount(productionUnits[i], this.m_presetId);

                if (unitCount > 0)
                {
                    this.AddUnitsToQueue(productionUnits[i], unitCount);
                }
            }

            for (int i = 0; i < spellTable.GetItemCount(); i++)
            {
                LogicSpellData data = (LogicSpellData)spellTable.GetItemAt(i);

                if (this.m_level.GetCalendar().IsProductionEnabled(data))
                {
                    productionSpells.Add(data);
                }
            }

            this.SortProduction(productionSpells);

            for (int i = 0; i < productionSpells.Size(); i++)
            {
                int spellCount = homeOwnerAvatar.GetUnitPresetCount(productionSpells[i], this.m_presetId);

                if (spellCount > 0)
                {
                    this.AddUnitsToQueue(productionSpells[i], spellCount);
                }
            }
        }
        public int GetResourceCost(LogicResourceData resourceData)
        {
            int cost = 0;

            LogicAvatar    homeOwnerAvatar = this.m_level.GetHomeOwnerAvatar();
            LogicCalendar  calendar        = this.m_level.GetCalendar();
            LogicDataTable table           = LogicDataTables.GetTable(LogicDataType.CHARACTER);

            for (int i = 0; i < table.GetItemCount(); i++)
            {
                LogicCharacterData data = (LogicCharacterData)table.GetItemAt(i);

                if (calendar.IsProductionEnabled(data) && !data.IsSecondaryTroop())
                {
                    int count = homeOwnerAvatar.GetUnitPresetCount(data, this.m_presetId);

                    if (count > 0)
                    {
                        if (data.GetTrainingResource() == resourceData)
                        {
                            cost += count * calendar.GetTrainingCost(data, homeOwnerAvatar.GetUnitUpgradeLevel(data));
                        }
                    }
                }
            }

            table = LogicDataTables.GetTable(LogicDataType.SPELL);

            for (int i = 0; i < table.GetItemCount(); i++)
            {
                LogicSpellData data = (LogicSpellData)table.GetItemAt(i);

                if (calendar.IsProductionEnabled(data))
                {
                    int count = homeOwnerAvatar.GetUnitPresetCount(data, this.m_presetId);

                    if (count > 0)
                    {
                        if (data.GetTrainingResource() == resourceData)
                        {
                            cost += count * calendar.GetTrainingCost(data, homeOwnerAvatar.GetUnitUpgradeLevel(data));
                        }
                    }
                }
            }

            return(cost);
        }
        public override int Execute(LogicLevel level)
        {
            this.m_level = level;

            if (LogicDataTables.GetGlobals().EnablePresets())
            {
                LogicAvatar homeOwnerAvatar = level.GetHomeOwnerAvatar();

                if (homeOwnerAvatar.GetTownHallLevel() >= LogicDataTables.GetGlobals().GetEnablePresetsTownHallLevel())
                {
                    if (this.m_presetId <= 3)
                    {
                        LogicDataTable        table            = LogicDataTables.GetTable(LogicDataType.CHARACTER);
                        LogicComponentManager componentManager = level.GetComponentManager();

                        int totalMaxHousing = componentManager.GetTotalMaxHousing(0);

                        for (int i = 0, housingSpace = 0; i < table.GetItemCount(); i++)
                        {
                            LogicCharacterData data = (LogicCharacterData)table.GetItemAt(i);

                            if (level.GetGameMode().GetCalendar().IsProductionEnabled(data) && !data.IsSecondaryTroop())
                            {
                                int count = 0;

                                if (this.m_slots.Size() > 0)
                                {
                                    for (int j = 0; j < this.m_slots.Size(); j++)
                                    {
                                        if (this.m_slots[j].GetData() == data)
                                        {
                                            count = this.m_slots[j].GetCount();
                                            break;
                                        }
                                    }
                                }

                                housingSpace += count * data.GetHousingSpace();

                                if (housingSpace > totalMaxHousing || !this.IsUnlocked(data))
                                {
                                    this.SetUnitPresetCount(data, 0);
                                }
                                else
                                {
                                    this.SetUnitPresetCount(data, count);
                                }
                            }
                        }

                        table           = LogicDataTables.GetTable(LogicDataType.SPELL);
                        totalMaxHousing = componentManager.GetTotalMaxHousing(0);

                        for (int i = 0, housingSpace = 0; i < table.GetItemCount(); i++)
                        {
                            LogicSpellData data = (LogicSpellData)table.GetItemAt(i);

                            if (level.GetGameMode().GetCalendar().IsProductionEnabled(data))
                            {
                                int count = 0;

                                if (this.m_slots.Size() > 0)
                                {
                                    for (int j = 0; j < this.m_slots.Size(); j++)
                                    {
                                        if (this.m_slots[j].GetData() == data)
                                        {
                                            count = this.m_slots[j].GetCount();
                                            break;
                                        }
                                    }
                                }

                                housingSpace += count * data.GetHousingSpace();

                                if (housingSpace > totalMaxHousing || !this.IsUnlocked(data))
                                {
                                    this.SetUnitPresetCount(data, 0);
                                }
                                else
                                {
                                    this.SetUnitPresetCount(data, count);
                                }
                            }
                        }

                        return(0);
                    }

                    return(-2);
                }
            }

            return(-1);
        }
Exemple #5
0
        public void CheckSpawning(LogicCharacterData spawnCharacterData, int spawnCount, int spawnUpgradeLevel, int invulnerabilityTime)
        {
            LogicCharacterData data = this.GetCharacterData();

            if (spawnCharacterData == null)
            {
                spawnCharacterData = data.GetSecondaryTroop();

                if (spawnCharacterData == null)
                {
                    spawnCharacterData = data.GetAttackerItemData(this.m_upgradeLevel).GetSummonTroop();

                    if (spawnCharacterData == null)
                    {
                        return;
                    }
                }
            }

            if (spawnCharacterData.IsSecondaryTroop() || this.IsHero())
            {
                int totalSpawnCount = spawnCount;
                int upgLevel        = this.m_upgradeLevel;

                if (upgLevel >= spawnCharacterData.GetUpgradeLevelCount())
                {
                    upgLevel = spawnCharacterData.GetUpgradeLevelCount() - 1;
                }

                if (this.IsHero())
                {
                    if (this.m_summonSpawnCount >= spawnCount)
                    {
                        return;
                    }

                    upgLevel        = spawnUpgradeLevel;
                    totalSpawnCount = LogicMath.Max(0, LogicMath.Min(3, spawnCount - this.m_summonSpawnCount));
                }
                else
                {
                    if (data.GetSecondaryTroopCount(this.m_upgradeLevel) != 0)
                    {
                        totalSpawnCount = data.GetSecondaryTroopCount(this.m_upgradeLevel);
                    }
                    else if (spawnCount == 0)
                    {
                        totalSpawnCount = data.GetAttackerItemData(this.m_upgradeLevel).GetSummonTroopCount();

                        if (this.m_summonTroops.Size() + totalSpawnCount > data.GetAttackerItemData(this.m_upgradeLevel).GetSummonLimit())
                        {
                            totalSpawnCount = data.GetAttackerItemData(this.m_upgradeLevel).GetSummonLimit() - this.m_summonTroops.Size();
                        }
                    }
                }

                if (totalSpawnCount > 0)
                {
                    LogicVector2 position = new LogicVector2();
                    LogicRandom  random   = new LogicRandom(this.m_globalId);

                    int  team = this.GetHitpointComponent().GetTeam();
                    bool randomizeSecSpawnDist = this.GetCharacterData().GetRandomizeSecSpawnDist();

                    for (int i = 0, j = 0, k = 0; i < totalSpawnCount; i++, j += 360, k += 100)
                    {
                        int seed = j / totalSpawnCount;

                        if (this.IsHero())
                        {
                            seed = 360 * (i + this.m_summonSpawnCount) / LogicMath.Max(1, LogicMath.Min(6, spawnCount));
                        }

                        int rnd = 59 * this.m_globalId % 360 + seed;

                        if (spawnCharacterData.IsFlying())
                        {
                            LogicCharacterData parentData = this.GetCharacterData();

                            position.Set(this.GetX() + LogicMath.GetRotatedX(parentData.GetSecondarySpawnOffset(), 0, rnd),
                                         this.GetY() + LogicMath.GetRotatedY(parentData.GetSecondarySpawnOffset(), 0, rnd));
                        }
                        else if (spawnCharacterData.GetSpeed() == 0)
                        {
                            position.Set(this.GetX(), this.GetY());
                        }
                        else
                        {
                            if (!this.m_level.GetTileMap().GetNearestPassablePosition(this.GetX(), this.GetY(), position, 1536))
                            {
                                continue;
                            }
                        }

                        LogicCharacter spawnGameObject = (LogicCharacter)LogicGameObjectFactory.CreateGameObject(spawnCharacterData, this.m_level, this.m_villageType);

                        if (this.GetCharacterData().GetAttackerItemData(this.m_upgradeLevel).GetSummonTroop() != null || this.IsHero())
                        {
                            this.m_summonTroops.Add(spawnGameObject);
                        }

                        spawnGameObject.GetHitpointComponent().SetTeam(team);
                        spawnGameObject.SetUpgradeLevel(upgLevel);

                        spawnGameObject.SetInitialPosition(position.m_x, position.m_y);

                        if (this.m_duplicate)
                        {
                            spawnGameObject.m_duplicateLifeTime = this.m_duplicateLifeTime;
                            spawnGameObject.m_duplicate         = true;
                        }

                        if (!this.IsHero())
                        {
                            spawnGameObject.m_summoner = (LogicCharacterData)this.m_data;
                        }

                        if (invulnerabilityTime > 0)
                        {
                            spawnGameObject.GetHitpointComponent().SetInvulnerabilityTime(invulnerabilityTime);
                        }

                        int secondarySpawnDistance = this.IsHero() ? 768 : this.GetCharacterData().GetSecondarySpawnDistance();

                        if (secondarySpawnDistance > 0)
                        {
                            if (randomizeSecSpawnDist)
                            {
                                secondarySpawnDistance = (int)(random.Rand(secondarySpawnDistance) + ((uint)secondarySpawnDistance >> 1));
                            }

                            position.Set(LogicMath.Cos(rnd, secondarySpawnDistance),
                                         LogicMath.Sin(rnd, secondarySpawnDistance));

                            int pushBackSpeed = spawnGameObject.GetCharacterData().GetPushbackSpeed();

                            if (pushBackSpeed <= 0)
                            {
                                pushBackSpeed = 1;
                            }

                            int pushBackTime = 2 * secondarySpawnDistance / (3 * pushBackSpeed);

                            if (this.GetHitpointComponent().GetHitpoints() > 0)
                            {
                                if (this.GetAttackerItemData().GetSummonTroop() != null)
                                {
                                    spawnGameObject.SetSpawnTime(pushBackTime);
                                }
                                else if (this.IsHero())
                                {
                                    spawnGameObject.SetSpawnTime(pushBackTime + k);
                                }
                            }

                            spawnGameObject.GetMovementComponent().GetMovementSystem().PushTrap(position, pushBackTime, 0, false, false);
                        }

                        if (team == 1 || spawnGameObject.GetCharacterData().IsJumper())
                        {
                            spawnGameObject.GetMovementComponent().EnableJump(3600000);
                            spawnGameObject.GetCombatComponent().RefreshTarget(true);
                        }

                        if (team == 1)
                        {
                            if (LogicDataTables.GetGlobals().AllianceTroopsPatrol())
                            {
                                spawnGameObject.GetCombatComponent().SetSearchRadius(LogicDataTables.GetGlobals().GetClanCastleRadius() >> 9);

                                if (this.GetMovementComponent().GetBaseBuilding() != null)
                                {
                                    spawnGameObject.GetMovementComponent().SetBaseBuilding(this.GetMovementComponent().GetBaseBuilding());
                                }
                            }
                        }

                        this.GetGameObjectManager().AddGameObject(spawnGameObject, -1);

                        if (this.IsHero())
                        {
                            ++this.m_summonSpawnCount;
                        }
                    }

                    position.Destruct();
                }
            }
            else
            {
                Debugger.Warning("checkSpawning: trying to spawn normal troops!");
            }
        }
Exemple #6
0
        public override void DeathEvent()
        {
            LogicHitpointComponent hitpointComponent = this.GetHitpointComponent();
            LogicCharacterData     data = this.GetCharacterData();

            if (hitpointComponent != null && hitpointComponent.GetTeam() == 1 && !this.IsHero() && !data.IsSecondaryTroop() &&
                this.m_level.GetVillageType() == 0 && this.m_allianceUnit)
            {
                LogicAvatar homeOwnerAvatar = this.m_level.GetHomeOwnerAvatar();

                homeOwnerAvatar.RemoveAllianceUnit(data, this.m_upgradeLevel);
                homeOwnerAvatar.GetChangeListener().AllianceUnitRemoved(data, this.m_upgradeLevel);
            }

            if (data.GetSpecialAbilityType() != LogicCharacterData.SPECIAL_ABILITY_TYPE_RESPAWN_AS_CANNON ||
                data.GetSpecialAbilityLevel(this.m_upgradeLevel) <= 0)
            {
                if (data.GetSpecialAbilityType() == LogicCharacterData.SPECIAL_ABILITY_TYPE_SPAWN_UNITS)
                {
                    if (data.GetSpecialAbilityLevel(this.m_upgradeLevel) > 0)
                    {
                        this.CheckSpawning(null, data.GetSpecialAbilityAttribute(this.m_upgradeLevel), 0, 0);
                    }
                }
                else if (data.GetSecondaryTroop() != null)
                {
                    this.CheckSpawning(null, 0, 0, 0);
                }
            }
            else if (!this.m_ejected)
            {
                this.CheckSpawning(LogicDataTables.GetCharacterByName("MovingCannonSecondary", null), 1, data.GetSpecialAbilityAttribute(this.m_upgradeLevel), 500);
            }

            this.AddTombstoneIfNeeded();

            if (this.m_parent != null)
            {
                this.m_parent.RemoveChildren(this);
                this.m_parent = null;
            }

            base.DeathEvent();
        }