Exemple #1
0
        public bool TrainingFinished()
        {
            bool success = false;

            if (this.m_timer != null)
            {
                this.m_timer.Destruct();
                this.m_timer = null;
            }

            if (this.m_slots.Size() > 0)
            {
                LogicUnitProductionSlot prodSlot = this.GetCurrentlyTrainedSlot();
                int prodIdx = this.GetCurrentlyTrainedIndex();

                if (prodSlot != null)
                {
                    if (prodSlot.GetCount() == 1)
                    {
                        prodSlot.SetTerminate(true);
                    }
                    else
                    {
                        prodSlot.SetCount(prodSlot.GetCount() - 1);

                        LogicUnitProductionSlot previousSlot = this.m_slots[LogicMath.Max(prodIdx - 1, 0)];

                        if (previousSlot != null &&
                            previousSlot.IsTerminate() &&
                            previousSlot.GetData().GetGlobalID() == prodSlot.GetData().GetGlobalID())
                        {
                            previousSlot.SetCount(previousSlot.GetCount() + 1);
                        }
                        else
                        {
                            this.m_slots.Add(prodIdx, new LogicUnitProductionSlot(prodSlot.GetData(), 1, true));
                        }
                    }
                }

                if (this.m_slots.Size() > 0)
                {
                    LogicCombatItemData nextProductionData = this.GetCurrentlyTrainedUnit();

                    if (nextProductionData != null && this.m_timer == null)
                    {
                        this.m_timer = new LogicTimer();
                        this.m_timer.StartTimer(nextProductionData.GetTrainingTime(this.m_level.GetHomeOwnerAvatar().GetUnitUpgradeLevel(nextProductionData), this.m_level, 0),
                                                this.m_level.GetLogicTime(), false, -1);
                        success = true;
                    }
                }
            }

            this.MergeSlots();

            return(success);
        }
        /// <summary>
        ///     Tries to merge slots.
        /// </summary>
        public void MergeSlots()
        {
            LogicAvatar homeOwnerAvatar = this._level.GetHomeOwnerAvatar();

            if (this._slots.Count > 0)
            {
                if (this._slots.Count > 1)
                {
                    for (int i = 1; i < this._slots.Count; i++)
                    {
                        LogicUnitProductionSlot slot1 = this._slots[i];
                        LogicUnitProductionSlot slot2 = this._slots[i - 1];

                        if (slot1.GetData() == slot2.GetData())
                        {
                            if (slot1.IsTerminate() == slot2.IsTerminate())
                            {
                                this._slots.Remove(i--);
                                slot2.SetCount(slot2.GetCount() + slot1.GetCount());
                                slot1.Destruct();
                                slot1 = null;
                            }
                        }
                    }
                }
            }

            LogicComponentManager componentManager = this._level.GetComponentManagerAt(this._villageType);
            Int32 totalCapcity = componentManager.GetTotalMaxHousing(this._unitProductionType != 3 ? 1 : 0);
            Int32 usedCapacity = this._unitProductionType == 25 ? homeOwnerAvatar.GetSpellsTotalCapacity() : homeOwnerAvatar.GetUnitsTotalCapacity();
            Int32 freeCapacity = totalCapcity - usedCapacity;

            for (int i = 0; i < this._slots.Count; i++)
            {
                LogicUnitProductionSlot slot = this._slots[i];
                LogicCombatItemData     data = (LogicCombatItemData)slot.GetData();

                int housingSpace = data.GetHousingSpace() * slot.GetCount();

                if (freeCapacity < housingSpace)
                {
                    if (slot.GetCount() > 0 && housingSpace / data.GetHousingSpace() > 0)
                    {
                        if (slot.GetCount() > housingSpace / data.GetHousingSpace())
                        {
                            this._slots.Add(i + 1, new LogicUnitProductionSlot(data, slot.GetCount() - housingSpace / data.GetHousingSpace(), slot.IsTerminate()));
                        }
                    }

                    break;
                }

                freeCapacity -= housingSpace;
            }
        }
        /// <summary>
        ///     Speed up the unit production.
        /// </summary>
        public void SpeedUp()
        {
            LogicAvatar           homeOwnerAvatar  = this._level.GetHomeOwnerAvatar();
            LogicComponentManager componentManager = this._level.GetComponentManagerAt(this._villageType);
            Int32 totalMaxHousing   = componentManager.GetTotalMaxHousing(this._unitProductionType != 3 ? 1 : 0);
            Int32 totalUsedCapacity = this._unitProductionType == 3 ? homeOwnerAvatar.GetUnitsTotalCapacity() : homeOwnerAvatar.GetSpellsTotalCapacity();
            Int32 freeCapacity      = totalMaxHousing - totalUsedCapacity;

            while (this._slots.Count > 0)
            {
                LogicUnitProductionSlot slot = this._slots[0];
                LogicCombatItemData     data = (LogicCombatItemData)slot.GetData();
                Int32 count = slot.GetCount();

                if (count > 0)
                {
                    do
                    {
                        freeCapacity -= data.GetHousingSpace();

                        if (freeCapacity < 0)
                        {
                            return;
                        }

                        this.ProductionCompleted(true);
                    } while (--count > 0);
                }
                else
                {
                    break;
                }
            }
        }
Exemple #4
0
        public void RemoveTrainedUnit(LogicCombatItemData data)
        {
            int idx = -1;

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

            if (idx != -1)
            {
                LogicUnitProductionSlot slot = this.m_slots[idx];

                if (slot.GetCount() > 1)
                {
                    slot.SetCount(slot.GetCount() - 1);
                }
                else
                {
                    this.m_slots.Remove(idx);

                    slot.Destruct();
                    slot = null;
                }

                this.MergeSlots();

                if (this.GetWaitingForSpaceUnit() != null)
                {
                    this.ProductionCompleted(false);
                }
            }
        }
Exemple #5
0
        public int GetTotalCount()
        {
            int count = 0;

            for (int i = 0; i < this.m_slots.Size(); i++)
            {
                LogicUnitProductionSlot slot = this.m_slots[i];
                LogicCombatItemData     data = (LogicCombatItemData)slot.GetData();

                count += data.GetHousingSpace() * slot.GetCount();
            }

            return(count);
        }
Exemple #6
0
        public int AddUnitToQueue(LogicCombatItemData data)
        {
            if (data != null)
            {
                if (this.CanAddUnitToQueue(data, false))
                {
                    LogicAvatar homeOwnerAvatar = this.m_level.GetHomeOwnerAvatar();

                    for (int i = this.m_slots.Size() - 1; i >= 0; i--)
                    {
                        LogicUnitProductionSlot tmp = this.m_slots[i];

                        if (tmp != null)
                        {
                            if (tmp.GetData() == data)
                            {
                                tmp.SetCount(tmp.GetCount() + 1);
                                this.MergeSlots();

                                return(i);
                            }

                            break;
                        }
                    }

                    this.m_slots.Add(new LogicUnitProductionSlot(data, 1, false));
                    this.MergeSlots();

                    if (this.m_slots.Size() > 0)
                    {
                        LogicCombatItemData productionData = this.GetCurrentlyTrainedUnit();

                        if (productionData != null && this.m_timer == null)
                        {
                            this.m_timer = new LogicTimer();
                            this.m_timer.StartTimer(productionData.GetTrainingTime(homeOwnerAvatar.GetUnitUpgradeLevel(productionData), this.m_level, 0),
                                                    this.m_level.GetLogicTime(), false, -1);
                        }
                    }
                }
            }
            else
            {
                Debugger.Error("LogicUnitProduction - Trying to add NULL character!");
            }

            return(-1);
        }
Exemple #7
0
        public void Save(LogicJSONObject root)
        {
            LogicJSONObject jsonObject = new LogicJSONObject();

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

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

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

                    if (slot != null)
                    {
                        LogicJSONObject slotObject = new LogicJSONObject();

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

                        if (slot.IsTerminate())
                        {
                            slotObject.Put("t", new LogicJSONBoolean(true));
                        }

                        slotArray.Add(slotObject);
                    }
                }

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

            if (this.m_boostTimer != null)
            {
                jsonObject.Put("boost_t", new LogicJSONNumber(this.m_boostTimer.GetRemainingSeconds(this.m_level.GetLogicTime())));
            }

            if (this.m_boostPause)
            {
                jsonObject.Put("boost_pause", new LogicJSONBoolean(true));
            }

            root.Put("unit_prod", jsonObject);
        }
Exemple #8
0
        public int GetTotalRemainingSeconds()
        {
            LogicAvatar           homeOwnerAvatar  = this.m_level.GetHomeOwnerAvatar();
            LogicComponentManager componentManager = this.m_level.GetComponentManagerAt(this.m_villageType);

            int totalMaxHousing   = componentManager.GetTotalMaxHousing(this.m_unitProductionType != LogicDataType.CHARACTER ? 1 : 0);
            int totalUsedCapacity = this.m_unitProductionType == LogicDataType.CHARACTER ? homeOwnerAvatar.GetUnitsTotalCapacity() : homeOwnerAvatar.GetSpellsTotalCapacity();
            int freeCapacity      = totalMaxHousing - totalUsedCapacity;
            int remainingSecs     = 0;

            for (int i = 0; i < this.m_slots.Size(); i++)
            {
                LogicUnitProductionSlot slot = this.m_slots[i];
                LogicCombatItemData     data = (LogicCombatItemData)slot.GetData();
                int housingSpace             = data.GetHousingSpace();
                int count = slot.GetCount();

                if (count > 0)
                {
                    if (i == 0)
                    {
                        if (!slot.IsTerminate() && freeCapacity - housingSpace >= 0)
                        {
                            if (this.m_timer != null)
                            {
                                remainingSecs += this.m_timer.GetRemainingSeconds(this.m_level.GetLogicTime());
                            }
                        }

                        freeCapacity -= housingSpace;
                        count        -= 1;
                    }

                    for (int j = 0; j < count; j++)
                    {
                        if (!slot.IsTerminate() && freeCapacity - housingSpace >= 0)
                        {
                            remainingSecs += data.GetTrainingTime(homeOwnerAvatar.GetUnitUpgradeLevel(data), this.m_level, 0);
                        }

                        freeCapacity -= housingSpace;
                    }
                }
            }

            return(remainingSecs);
        }
Exemple #9
0
        public int GetWaitingForSpaceUnitCount(LogicCombatItemData data)
        {
            int count = 0;

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

                if (slot.GetData() == data)
                {
                    if (slot.IsTerminate())
                    {
                        count += slot.GetCount();
                    }
                }
            }

            return(count);
        }
Exemple #10
0
        public void SpeedUp()
        {
            LogicAvatar           homeOwnerAvatar  = this.m_level.GetHomeOwnerAvatar();
            LogicComponentManager componentManager = this.m_level.GetComponentManagerAt(this.m_villageType);

            int totalMaxHousing   = componentManager.GetTotalMaxHousing(this.m_unitProductionType != LogicDataType.CHARACTER ? 1 : 0);
            int totalUsedCapacity = this.m_unitProductionType == LogicDataType.CHARACTER ? homeOwnerAvatar.GetUnitsTotalCapacity() : homeOwnerAvatar.GetSpellsTotalCapacity();
            int freeCapacity      = totalMaxHousing - totalUsedCapacity;

            bool armyCampFull = false;

            while (!armyCampFull && this.m_slots.Size() > 0)
            {
                LogicUnitProductionSlot slot = this.m_slots[0];
                LogicCombatItemData     data = (LogicCombatItemData)slot.GetData();

                int count = slot.GetCount();

                if (count <= 0)
                {
                    break;
                }

                armyCampFull = true;

                do
                {
                    freeCapacity -= data.GetHousingSpace();

                    if (freeCapacity >= 0)
                    {
                        this.ProductionCompleted(true);
                        armyCampFull = false;
                    }
                } while (--count > 0);
            }
        }
Exemple #11
0
        public void MergeSlots()
        {
            LogicAvatar homeOwnerAvatar = this.m_level.GetHomeOwnerAvatar();

            if (this.m_slots.Size() > 0)
            {
                if (this.m_slots.Size() > 1)
                {
                    for (int i = 1; i < this.m_slots.Size(); i++)
                    {
                        LogicUnitProductionSlot slot1 = this.m_slots[i];
                        LogicUnitProductionSlot slot2 = this.m_slots[i - 1];

                        if (slot1.GetData() == slot2.GetData())
                        {
                            if (slot1.IsTerminate() == slot2.IsTerminate())
                            {
                                this.m_slots.Remove(i--);

                                slot2.SetCount(slot2.GetCount() + slot1.GetCount());
                                slot1.Destruct();
                                slot1 = null;
                            }
                        }
                    }
                }
            }

            LogicComponentManager componentManager = this.m_level.GetComponentManagerAt(this.m_villageType);

            int usedCapacity  = this.m_unitProductionType == LogicDataType.SPELL ? homeOwnerAvatar.GetSpellsTotalCapacity() : homeOwnerAvatar.GetUnitsTotalCapacity();
            int totalCapacity = componentManager.GetTotalMaxHousing(this.m_unitProductionType != LogicDataType.CHARACTER ? 1 : 0);
            int freeCapacity  = totalCapacity - usedCapacity;

            for (int i = 0, j = freeCapacity; i < this.m_slots.Size(); i++)
            {
                LogicUnitProductionSlot slot = this.m_slots[i];
                LogicCombatItemData     data = (LogicCombatItemData)slot.GetData();

                int count        = slot.GetCount();
                int housingSpace = data.GetHousingSpace() * count;

                if (j < housingSpace)
                {
                    if (count > 1)
                    {
                        int maxInProduction = j / data.GetHousingSpace();

                        if (maxInProduction > 0)
                        {
                            int inQueue = count - maxInProduction;

                            if (inQueue > 0)
                            {
                                slot.SetCount(maxInProduction);
                                this.m_slots.Add(i + 1, new LogicUnitProductionSlot(data, inQueue, slot.IsTerminate()));
                            }
                        }
                    }

                    break;
                }

                j -= housingSpace;
            }
        }
Exemple #12
0
        public bool RemoveUnit(LogicCombatItemData data, int index)
        {
            LogicUnitProductionSlot slot = null;
            bool removed = false;

            if (index > -1 &&
                this.m_slots.Size() > index &&
                this.m_slots[index].GetData() == data)
            {
                slot = this.m_slots[index];
            }
            else
            {
                index = -1;

                for (int i = 0; i < this.m_slots.Size(); i++)
                {
                    LogicUnitProductionSlot tmp = this.m_slots[i];

                    if (tmp.GetData() == data)
                    {
                        index = i;
                        break;
                    }
                }

                if (index == -1)
                {
                    return(false);
                }

                slot = this.m_slots[index];
            }

            int count = slot.GetCount();

            if (count > 0)
            {
                removed = true;
                slot.SetCount(count - 1);

                if (count == 1)
                {
                    int prodIdx = this.GetCurrentlyTrainedIndex();

                    if (prodIdx == index)
                    {
                        if (this.m_timer != null)
                        {
                            this.m_timer.Destruct();
                            this.m_timer = null;
                        }
                    }

                    this.m_slots[index].Destruct();
                    this.m_slots.Remove(index);
                }
            }

            if (this.m_slots.Size() > 0)
            {
                LogicUnitProductionSlot productionSlot = this.GetCurrentlyTrainedSlot();

                if (productionSlot == null || this.m_timer != null)
                {
                    if (!removed)
                    {
                        return(false);
                    }

                    this.MergeSlots();
                }
                else
                {
                    LogicAvatar         homeOwnerAvatar = this.m_level.GetHomeOwnerAvatar();
                    LogicCombatItemData productionData  = (LogicCombatItemData)productionSlot.GetData();

                    this.m_timer = new LogicTimer();
                    this.m_timer.StartTimer(productionData.GetTrainingTime(homeOwnerAvatar.GetUnitUpgradeLevel(productionData), this.m_level, 0), this.m_level.GetLogicTime(),
                                            false,
                                            -1);

                    if (removed)
                    {
                        this.MergeSlots();
                    }
                }
            }
            else
            {
                if (!removed)
                {
                    return(false);
                }

                this.MergeSlots();
            }

            return(true);
        }