Esempio n. 1
0
        public int AddResources(int idx, int count, bool useRecommencedMax)
        {
            LogicArrayList <LogicComponent> resourceStorageComponents = this.m_components[(int)LogicComponentType.RESOURCE_STORAGE];

            while (count > 0)
            {
                int freeComponentCount = 0;

                for (int i = 0; i < resourceStorageComponents.Size(); i++)
                {
                    LogicResourceStorageComponent resourceStorageComponent = (LogicResourceStorageComponent)resourceStorageComponents[i];

                    int cap       = useRecommencedMax ? resourceStorageComponent.GetRecommendedMax(idx, count) : resourceStorageComponent.GetMax(idx);
                    int usedCount = resourceStorageComponent.GetCount(idx);

                    if (cap > usedCount)
                    {
                        ++freeComponentCount;
                    }
                }

                if (freeComponentCount > 0)
                {
                    int countPerStorage = (count + freeComponentCount - 1) / freeComponentCount;

                    for (int i = 0; i < resourceStorageComponents.Size(); i++)
                    {
                        LogicResourceStorageComponent resourceStorageComponent = (LogicResourceStorageComponent)resourceStorageComponents[i];

                        int cap               = useRecommencedMax ? resourceStorageComponent.GetRecommendedMax(idx, count) : resourceStorageComponent.GetMax(idx);
                        int usedCount         = resourceStorageComponent.GetCount(idx);
                        int unusedResourceCap = cap - usedCount;

                        if (unusedResourceCap > 0)
                        {
                            int resourceCount = LogicMath.Min(countPerStorage, count);

                            if (resourceCount >= unusedResourceCap)
                            {
                                --freeComponentCount;
                            }
                            if (resourceCount <= unusedResourceCap)
                            {
                                unusedResourceCap = resourceCount;
                            }
                            resourceStorageComponent.SetCount(idx, unusedResourceCap + usedCount);
                            count -= unusedResourceCap;

                            if (count == 0)
                            {
                                return(0);
                            }
                        }
                    }
                }

                if (freeComponentCount <= 0 && !useRecommencedMax)
                {
                    return(count);
                }
                useRecommencedMax &= freeComponentCount > 0;
            }

            return(0);
        }
        public void CauseDamage(int damage, int gameObjectId, LogicGameObject gameObject)
        {
            if (damage >= 0 || this.m_hp != 0)
            {
                if (this.m_parent == null)
                {
                    if (damage > 0 && this.m_invulnerabilityTime > 0)
                    {
                        return;
                    }
                }
                else
                {
                    LogicCombatComponent combatComponent = this.m_parent.GetCombatComponent();

                    if (combatComponent != null)
                    {
                        if (combatComponent.GetUndergroundTime() > 0 && damage > 0)
                        {
                            damage = 0;
                        }
                    }

                    if (!this.m_parent.GetLevel().GetInvulnerabilityEnabled())
                    {
                        if (damage > 0 && this.m_invulnerabilityTime > 0)
                        {
                            return;
                        }
                    }
                    else
                    {
                        damage = 0;
                    }

                    if (this.m_parent.GetGameObjectType() == LogicGameObjectType.CHARACTER)
                    {
                        LogicCharacter character = (LogicCharacter)this.m_parent;
                        LogicArrayList <LogicCharacter> childTroops = character.GetChildTroops();

                        if (childTroops != null && childTroops.Size() > 0 || character.GetSpawnDelay() > 0)
                        {
                            return;
                        }
                    }
                }

                if (gameObjectId != 0 && damage < 0)
                {
                    int prevHealingIdx = -1;
                    int healingIdx     = -1;

                    for (int i = 0; i < 8; i++)
                    {
                        if (this.m_healingId[i] == gameObjectId)
                        {
                            prevHealingIdx = i;
                        }
                        else if (healingIdx == -1)
                        {
                            healingIdx = i;

                            if (this.m_healingTime[i] > 0)
                            {
                                healingIdx = -1;
                            }
                        }
                    }

                    if (healingIdx < prevHealingIdx && prevHealingIdx != -1 && healingIdx != -1)
                    {
                        this.m_healingId[healingIdx]       = gameObjectId;
                        this.m_healingTime[healingIdx]     = 1000;
                        this.m_healingId[prevHealingIdx]   = 0;
                        this.m_healingTime[prevHealingIdx] = 0;
                    }
                    else if (prevHealingIdx == -1)
                    {
                        if (healingIdx != -1)
                        {
                            this.m_healingId[healingIdx]   = gameObjectId;
                            this.m_healingTime[healingIdx] = 1000;
                        }
                        else
                        {
                            healingIdx = 8;
                        }
                    }
                    else
                    {
                        healingIdx = prevHealingIdx;
                        this.m_healingTime[prevHealingIdx] = 1000;
                    }

                    damage = damage * LogicDataTables.GetGlobals().GetHealStackPercent(healingIdx) / 100;
                }

                int prevHp         = (this.m_hp + 99) / 100;
                int prevAccurateHp = this.m_hp;
                this.m_hp = LogicMath.Clamp(this.m_hp - damage, 0, this.m_maxHp);
                int hp = (this.m_hp + 99) / 100;

                if (prevHp > hp)
                {
                    LogicResourceStorageComponent resourceStorageComponent =
                        (LogicResourceStorageComponent)this.m_parent.GetComponent(LogicComponentType.RESOURCE_STORAGE);
                    LogicResourceProductionComponent resourceProductionComponent =
                        (LogicResourceProductionComponent)this.m_parent.GetComponent(LogicComponentType.RESOURCE_PRODUCTION);
                    LogicWarResourceStorageComponent warResourceStorageComponent =
                        (LogicWarResourceStorageComponent)this.m_parent.GetComponent(LogicComponentType.WAR_RESOURCE_STORAGE);

                    if (this.m_parent.GetGameObjectType() == LogicGameObjectType.BUILDING)
                    {
                        LogicBuilding building = (LogicBuilding)this.m_parent;

                        if (!building.GetBuildingData().IsLootOnDestruction() || prevAccurateHp > 0 && (this.m_hp == 0 || (uint)this.m_hp >= 0xFFFFFF3A))
                        {
                            if (resourceStorageComponent != null)
                            {
                                resourceStorageComponent.ResourcesStolen(prevHp - hp, prevHp);
                            }
                            if (resourceProductionComponent != null)
                            {
                                resourceProductionComponent.ResourcesStolen(prevHp - hp, prevHp);
                            }
                            if (warResourceStorageComponent != null)
                            {
                                warResourceStorageComponent.ResourcesStolen(prevHp - hp, prevHp);
                            }
                        }
                    }

                    if (this.m_parent.IsWall())
                    {
                        this.m_parent.RefreshPassable();
                    }
                    this.m_lastDamageTime = 0;
                }

                this.UpdateHeroHealthToAvatar(hp);

                if (damage <= 0)
                {
                    if (damage < 0)
                    {
                        // Listener
                    }
                }
                else
                {
                    if (this.m_parent.GetMovementComponent() != null)
                    {
                        this.m_parent.GetMovementComponent().SetPatrolFreeze();
                    }
                }

                if (prevAccurateHp > 0 && this.m_hp == 0)
                {
                    this.m_parent.DeathEvent();
                    this.m_parent.GetLevel().UpdateBattleStatus();

                    if (this.m_parent.IsWall())
                    {
                        this.WallRemoved();
                    }
                }
            }
        }