Esempio n. 1
0
        public override int Execute(LogicLevel level)
        {
            LogicClientAvatar playerAvatar = level.GetPlayerAvatar();

            if (level.GetVillageType() == 1)
            {
                LogicGameMode gameMode = level.GetGameMode();

                if (!gameMode.IsInAttackPreparationMode())
                {
                    if (gameMode.GetState() != 5)
                    {
                        return(-9);
                    }
                }

                if (this.m_oldUnitData != null && this.m_newUnitData != null && gameMode.GetCalendar().IsProductionEnabled(this.m_newUnitData))
                {
                    if (!this.m_newUnitData.IsUnlockedForBarrackLevel(playerAvatar.GetVillage2BarrackLevel()))
                    {
                        if (gameMode.GetState() != 7)
                        {
                            return(-7);
                        }
                    }

                    int oldUnitCount   = playerAvatar.GetUnitCountVillage2(this.m_oldUnitData);
                    int oldUnitsInCamp = this.m_oldUnitData.GetUnitsInCamp(playerAvatar.GetUnitUpgradeLevel(this.m_oldUnitData));

                    if (oldUnitCount >= oldUnitsInCamp)
                    {
                        int newUnitCount   = playerAvatar.GetUnitCountVillage2(this.m_newUnitData);
                        int newUnitsInCamp = this.m_newUnitData.GetUnitsInCamp(playerAvatar.GetUnitUpgradeLevel(this.m_newUnitData));

                        playerAvatar.SetUnitCountVillage2(this.m_oldUnitData, oldUnitCount - oldUnitsInCamp);
                        playerAvatar.SetUnitCountVillage2(this.m_newUnitData, newUnitCount + newUnitsInCamp);

                        LogicArrayList <LogicDataSlot> unitsNew = playerAvatar.GetUnitsNewVillage2();

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

                            if (slot.GetCount() > 0)
                            {
                                playerAvatar.CommodityCountChangeHelper(8, slot.GetData(), -slot.GetCount());
                            }
                        }

                        return(0);
                    }

                    return(-23);
                }

                return(-7);
            }

            return(-10);
        }
        public override void Save(LogicJSONObject root, int villageType)
        {
            LogicJSONObject jsonObject = new LogicJSONObject();

            jsonObject.Put("m", new LogicJSONNumber(1));
            jsonObject.Put("unit_type", new LogicJSONNumber(this.m_productionType));

            if (this.m_timer != null)
            {
                jsonObject.Put("t", new LogicJSONNumber(this.m_timer.GetRemainingSeconds(this.m_parent.GetLevel().GetLogicTime())));
            }

            if (this.m_slots.Size() > 0)
            {
                LogicJSONArray slotArray = new LogicJSONArray();

                for (int i = 0; i < this.m_slots.Size(); i++)
                {
                    LogicDataSlot   slot       = this.m_slots[i];
                    LogicJSONObject slotObject = new LogicJSONObject();

                    slotObject.Put("id", new LogicJSONNumber(slot.GetData().GetGlobalID()));
                    slotObject.Put("cnt", new LogicJSONNumber(slot.GetCount()));

                    slotArray.Add(slotObject);
                }

                jsonObject.Put("slots", slotArray);
            }

            root.Put("unit_prod", jsonObject);
        }
Esempio n. 3
0
        public int GetBuildingBoostCost(LogicBuildingData data, int upgLevel)
        {
            for (int i = 0; i < this.m_buildingBoostCost.Size(); i++)
            {
                LogicDataSlot slot = this.m_buildingBoostCost[i];

                if (slot.GetData() == data)
                {
                    return(slot.GetCount());
                }
            }

            return(data.GetBoostCost(upgLevel));
        }
Esempio n. 4
0
        public int GetTrainingCost(LogicCombatItemData data, int upgLevel)
        {
            int trainingCost = data.GetTrainingCost(upgLevel);

            for (int i = 0; i < this.m_troopDiscount.Size(); i++)
            {
                LogicDataSlot slot = this.m_troopDiscount[i];

                if (slot.GetData() == data)
                {
                    return((slot.GetCount() * trainingCost + 99) / 100);
                }
            }

            return(trainingCost);
        }
Esempio n. 5
0
        public static LogicJSONArray DataSlotArrayToJSONArray(LogicArrayList <LogicDataSlot> dataSlotArray)
        {
            LogicJSONArray jsonArray = new LogicJSONArray(dataSlotArray.Size());

            for (int i = 0; i < dataSlotArray.Size(); i++)
            {
                LogicDataSlot  dataSlot    = dataSlotArray[i];
                LogicJSONArray objectArray = new LogicJSONArray();

                objectArray.Add(new LogicJSONNumber(dataSlot.GetData().GetGlobalID()));
                objectArray.Add(new LogicJSONNumber(dataSlot.GetCount()));

                jsonArray.Add(objectArray);
            }

            return(jsonArray);
        }
Esempio n. 6
0
        /// <summary>
        ///     Places one unit.
        /// </summary>
        public bool PlaceOneUnit()
        {
            if (this._placePositionX == -1 && this._placePositionY == -1)
            {
                int widthArea = this._level.GetPlayArea().GetStartX();
                int width     = this._level.GetWidthInTiles();

                if (width > 0)
                {
                    int tileIdx = -1;
                    int tmp     = width / 2;

                    for (int x = 0; x < width; x++)
                    {
                        if (widthArea >= 2)
                        {
                            int middleX = (widthArea - 1) / 2;
                            int square  = (tmp - x) * (tmp - x);

                            for (int y = 0; y != widthArea - 1; y++, middleX--)
                            {
                                if (tileIdx == -1 || square + middleX * middleX < tileIdx)
                                {
                                    if (this._level.GetTileMap().GetTile(x, y).GetPassableFlag() == 1)
                                    {
                                        this._placePositionX = x;
                                        this._placePositionY = y;
                                        tileIdx = square + middleX * middleX;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (this._placePositionX == -1 && this._placePositionY == -1)
            {
                Debugger.Error("LogicNpcAttack::placeOneUnit - No attack position found!");
            }
            else
            {
                LogicArrayList <LogicDataSlot> units = this._npcAvatar.GetUnits();

                for (int i = 0; i < units.Count; i++)
                {
                    LogicDataSlot slot = units[i];

                    if (slot.GetCount() > 0)
                    {
                        slot.SetCount(slot.GetCount() - 1);

                        LogicCharacter character = LogicPlaceAttackerCommand.PlaceAttacker(this._npcAvatar, (LogicCharacterData)slot.GetData(), this._level, this._placePositionX, this._placePositionY);

                        if (!this._unitsDeployed)
                        {
                            character.GetListener().MapUnlocked();
                        }

                        // character.GetCombatComponent().SetPreferredTarget(this._buildingClass, 100, 0);
                        this._unitsDeployed = true;

                        return(true);
                    }
                }
            }

            return(false);
        }
Esempio n. 7
0
        public bool PlaceOneUnit()
        {
            if (this.m_placePositionX == -1 && this.m_placePositionY == -1)
            {
                int startAreaY   = this.m_level.GetPlayArea().GetStartY();
                int widthInTiles = this.m_level.GetWidthInTiles();

                int minDistance = -1;

                for (int i = 0; i < widthInTiles; i++)
                {
                    int centerY = (startAreaY - 1) / 2;

                    for (int j = 0; j < startAreaY - 1; j++, centerY--)
                    {
                        int distance = ((widthInTiles >> 1) - i) * ((widthInTiles >> 1) - i) + centerY * centerY;

                        if (minDistance == -1 || distance < minDistance)
                        {
                            LogicTile tile = this.m_level.GetTileMap().GetTile(i, j);

                            if (tile.GetPassableFlag() != 0)
                            {
                                this.m_placePositionX = i;
                                this.m_placePositionY = j;
                                minDistance           = distance;
                            }
                        }
                    }
                }
            }

            if (this.m_placePositionX == -1 && this.m_placePositionY == -1)
            {
                Debugger.Error("LogicNpcAttack::placeOneUnit - No attack position found!");
            }
            else
            {
                LogicArrayList <LogicDataSlot> units = this.m_npcAvatar.GetUnits();

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

                    if (slot.GetCount() > 0)
                    {
                        LogicCharacter character = LogicPlaceAttackerCommand.PlaceAttacker(this.m_npcAvatar, (LogicCharacterData)slot.GetData(), this.m_level,
                                                                                           this.m_placePositionX << 9,
                                                                                           this.m_placePositionY << 9);

                        if (!this.m_unitsDeployStarted)
                        {
                            character.GetListener().MapUnlocked();
                        }

                        character.GetCombatComponent().SetPreferredTarget(this.m_buildingClass, 100, false);

                        this.m_unitsDeployStarted = true;

                        return(true);
                    }
                }
            }

            return(false);
        }
Esempio n. 8
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;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        ///     Divides the avatar units to storages.
        /// </summary>
        public void DivideAvatarUnitsToStorages(int villageType)
        {
            if (this._level.GetHomeOwnerAvatar() != null)
            {
                if (villageType == 1)
                {
                    // TODO: Implement divideAvatarUnitsToStorages(vType) for village type 1.
                }
                else
                {
                    if (this._units.Count != 0)
                    {
                        do
                        {
                            this._units[0].Destruct();
                            this._units.Remove(0);
                        } while (this._units.Count != 0);
                    }

                    LogicArrayList <LogicComponent> components = this._components[0];

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                        if (unitCount != 0)
                        {
                            for (int j = 0; j < components.Count; 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;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }