Exemple #1
0
        public void DivideAvatarUnitsToStorages(int villageType)
        {
            if (this.m_level.GetHomeOwnerAvatar() != null)
            {
                if (villageType == 1)
                {
                    for (int i = this.m_units.Size() - 1; i >= 0; i--)
                    {
                        this.m_units[i].Destruct();
                        this.m_units.Remove(i);
                    }

                    LogicAvatar homeOwnerAvatar = this.m_level.GetHomeOwnerAvatar();
                    LogicArrayList <LogicComponent> components = this.m_components[(int)LogicComponentType.VILLAGE2_UNIT];

                    if (homeOwnerAvatar.GetUnitsNewTotalVillage2() <= 0 || LogicDataTables.GetGlobals().Village2TrainingOnlyUseRegularStorage())
                    {
                        for (int i = 0; i < components.Size(); i++)
                        {
                            LogicVillage2UnitComponent village2UnitComponent = (LogicVillage2UnitComponent)components[i];
                            LogicCombatItemData        unitData = village2UnitComponent.GetUnitData();

                            int idx = -1;

                            for (int j = 0; j < this.m_units.Size(); j++)
                            {
                                if (this.m_units[j].GetData() == unitData)
                                {
                                    idx = j;
                                    break;
                                }
                            }

                            if (idx == -1)
                            {
                                this.m_units.Add(new LogicDataSlot(unitData, -village2UnitComponent.GetUnitCount()));
                            }
                            else
                            {
                                this.m_units[idx].SetCount(this.m_units[idx].GetCount() - village2UnitComponent.GetUnitCount());
                            }
                        }

                        LogicArrayList <LogicDataSlot> units = homeOwnerAvatar.GetUnitsVillage2();

                        for (int i = 0; i < units.Size(); i++)
                        {
                            LogicDataSlot       slot = units[i];
                            LogicCombatItemData data = (LogicCombatItemData)slot.GetData();

                            int count = slot.GetCount();
                            int idx   = -1;

                            for (int j = 0; j < this.m_units.Size(); j++)
                            {
                                if (this.m_units[j].GetData() == data)
                                {
                                    idx = j;
                                    break;
                                }
                            }

                            if (idx == -1)
                            {
                                this.m_units.Add(new LogicDataSlot(data, count));
                            }
                            else
                            {
                                this.m_units[idx].SetCount(this.m_units[idx].GetCount() + count);
                            }
                        }

                        for (int i = 0; i < this.m_units.Size(); i++)
                        {
                            LogicDataSlot      slot     = this.m_units[i];
                            LogicCharacterData unitData = (LogicCharacterData)slot.GetData();

                            int unitCount = slot.GetCount();

                            if (unitCount != 0)
                            {
                                for (int j = 0; j < components.Size(); j++)
                                {
                                    LogicVillage2UnitComponent component = (LogicVillage2UnitComponent)components[j];

                                    if (component.GetUnitData() == unitData)
                                    {
                                        int highestBuildingLevel = this.m_level.GetGameObjectManagerAt(1).GetHighestBuildingLevel(unitData.GetProductionHouseData());

                                        if (unitData.IsUnlockedForProductionHouseLevel(highestBuildingLevel))
                                        {
                                            if (unitCount < 0)
                                            {
                                                int count = component.GetUnitCount();

                                                if (count >= -unitCount)
                                                {
                                                    component.SetUnit(unitData, LogicMath.Max(0, count + unitCount));
                                                    unitCount += count;
                                                }
                                                else
                                                {
                                                    component.SetUnit(unitData, 0);
                                                    unitCount += count;
                                                }
                                            }
                                            else
                                            {
                                                int maxUnits = component.GetMaxUnitsInCamp(unitData);
                                                int addCount = LogicMath.Min(maxUnits, unitCount);

                                                component.SetUnit(unitData, addCount);
                                                unitCount -= addCount;
                                            }

                                            component.TrainUnit(unitData);
                                        }
                                        else
                                        {
                                            component.RemoveUnits();
                                        }
                                    }

                                    if (unitCount == 0)
                                    {
                                        break;
                                    }
                                }

                                if (unitCount > 0)
                                {
                                    homeOwnerAvatar.SetUnitCountVillage2(unitData, 0);
                                    homeOwnerAvatar.GetChangeListener().CommodityCountChanged(7, unitData, 0);
                                }
                            }
                        }
                    }
                    else
                    {
                        LogicArrayList <LogicDataSlot> unitsNew = homeOwnerAvatar.GetUnitsNewVillage2();

                        for (int i = 0; i < unitsNew.Size(); i++)
                        {
                            LogicDataSlot      slot = unitsNew[i];
                            LogicCharacterData data = (LogicCharacterData)slot.GetData();

                            int count = slot.GetCount();
                            int index = -1;

                            for (int j = 0; j < this.m_units.Size(); j++)
                            {
                                if (this.m_units[j].GetData() == data)
                                {
                                    index = j;
                                    break;
                                }
                            }

                            if (count > 0)
                            {
                                if (index != -1)
                                {
                                    this.m_units[index].SetCount(this.m_units[index].GetCount() + count);
                                    this.m_units.Add(new LogicDataSlot(data, count));
                                }
                                else
                                {
                                    this.m_units.Add(new LogicDataSlot(data, count));
                                }
                            }
                        }

                        for (int i = 0; i < this.m_units.Size(); i++)
                        {
                            homeOwnerAvatar.CommodityCountChangeHelper(8, this.m_units[i].GetData(), -this.m_units[i].GetCount());
                        }

                        for (int i = 0; i < this.m_units.Size(); i++)
                        {
                            LogicDataSlot      slot = this.m_units[i];
                            LogicCharacterData data = (LogicCharacterData)slot.GetData();

                            int count = slot.GetCount();

                            if (count > 0)
                            {
                                for (int j = 0; j < components.Size(); j++)
                                {
                                    LogicVillage2UnitComponent village2UnitComponent = (LogicVillage2UnitComponent)components[j];

                                    int maxUnitsInCamp = village2UnitComponent.GetMaxUnitsInCamp(data);
                                    int addCount       = LogicMath.Min(count, maxUnitsInCamp);

                                    village2UnitComponent.SetUnit(data, addCount);
                                    village2UnitComponent.TrainUnit(data);

                                    count -= addCount;

                                    if (count <= 0)
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    for (int i = this.m_units.Size() - 1; i >= 0; i--)
                    {
                        this.m_units[i].Destruct();
                        this.m_units.Remove(i);
                    }

                    LogicArrayList <LogicComponent> components = this.m_components[(int)LogicComponentType.UNIT_STORAGE];

                    for (int i = 0; i < components.Size(); i++)
                    {
                        LogicUnitStorageComponent storageComponent = (LogicUnitStorageComponent)components[i];

                        for (int j = 0; j < storageComponent.GetUnitTypeCount(); j++)
                        {
                            LogicCombatItemData unitType = storageComponent.GetUnitType(j);
                            int unitCount = storageComponent.GetUnitCount(j);
                            int index     = -1;

                            for (int k = 0; k < this.m_units.Size(); k++)
                            {
                                LogicDataSlot tmp = this.m_units[k];

                                if (tmp.GetData() == unitType)
                                {
                                    index = k;
                                    break;
                                }
                            }

                            if (index != -1)
                            {
                                this.m_units[index].SetCount(this.m_units[index].GetCount() - unitCount);
                            }
                            else
                            {
                                this.m_units.Add(new LogicDataSlot(unitType, -unitCount));
                            }
                        }
                    }

                    LogicArrayList <LogicDataSlot> units = this.m_level.GetHomeOwnerAvatar().GetUnits();

                    for (int i = 0; i < units.Size(); i++)
                    {
                        LogicDataSlot slot  = units[i];
                        int           index = -1;

                        for (int j = 0; j < this.m_units.Size(); j++)
                        {
                            LogicDataSlot tmp = this.m_units[j];

                            if (tmp.GetData() == slot.GetData())
                            {
                                index = j;
                                break;
                            }
                        }

                        if (index != -1)
                        {
                            this.m_units[index].SetCount(this.m_units[index].GetCount() + slot.GetCount());
                        }
                        else
                        {
                            this.m_units.Add(new LogicDataSlot(slot.GetData(), slot.GetCount()));
                        }
                    }

                    LogicArrayList <LogicDataSlot> spells = this.m_level.GetHomeOwnerAvatar().GetSpells();

                    for (int i = 0; i < spells.Size(); i++)
                    {
                        LogicDataSlot slot  = spells[i];
                        int           index = -1;

                        for (int j = 0; j < this.m_units.Size(); j++)
                        {
                            LogicDataSlot tmp = this.m_units[j];

                            if (tmp.GetData() == slot.GetData())
                            {
                                index = j;
                                break;
                            }
                        }

                        if (index != -1)
                        {
                            this.m_units[index].SetCount(this.m_units[index].GetCount() + slot.GetCount());
                        }
                        else
                        {
                            this.m_units.Add(new LogicDataSlot(slot.GetData(), slot.GetCount()));
                        }
                    }

                    for (int i = 0; i < this.m_units.Size(); i++)
                    {
                        LogicDataSlot       slot = this.m_units[i];
                        LogicCombatItemData data = (LogicCombatItemData)slot.GetData();
                        int unitCount            = slot.GetCount();

                        if (unitCount != 0)
                        {
                            for (int j = 0; j < components.Size(); j++)
                            {
                                LogicUnitStorageComponent unitStorageComponent = (LogicUnitStorageComponent)components[j];

                                if (unitCount >= 0)
                                {
                                    while (unitStorageComponent.CanAddUnit(data))
                                    {
                                        unitStorageComponent.AddUnit(data);

                                        if (--unitCount <= 0)
                                        {
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    int idx = unitStorageComponent.GetUnitTypeIndex(data);

                                    if (idx != -1)
                                    {
                                        int count = unitStorageComponent.GetUnitCount(idx);

                                        if (count < -unitCount)
                                        {
                                            unitStorageComponent.RemoveUnits(data, count);
                                            unitCount += count;
                                        }
                                        else
                                        {
                                            unitStorageComponent.RemoveUnits(data, -unitCount);
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }