Example #1
0
        public void StartUpgrading(bool ignoreListener)
        {
            if (this.m_constructionTimer != null)
            {
                this.m_constructionTimer.Destruct();
                this.m_constructionTimer = null;
            }

            LogicVillageObjectData data = this.GetVillageObjectData();
            int constructionTime        = data.GetBuildTime(this.GetUpgradeLevel() + 1);

            this.m_upgrading = true;

            if (constructionTime <= 0)
            {
                this.FinishConstruction(false, true);
            }
            else
            {
                if (data.IsRequiresBuilder())
                {
                    this.m_level.GetWorkerManagerAt(data.GetVillageType()).AllocateWorker(this);
                }

                this.EnableComponent(LogicComponentType.RESOURCE_PRODUCTION, false);
                this.EnableComponent(LogicComponentType.UNIT_PRODUCTION, false);
                this.EnableComponent(LogicComponentType.UNIT_UPGRADE, false);

                this.m_constructionTimer = new LogicTimer();
                this.m_constructionTimer.StartTimer(constructionTime, this.m_level.GetLogicTime(), true, this.m_level.GetHomeOwnerAvatarChangeListener().GetCurrentTimestamp());
            }
        }
Example #2
0
        public override void Load(LogicJSONObject jsonObject)
        {
            this.LoadUpgradeLevel(jsonObject);

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

            LogicJSONNumber constTimeObject = jsonObject.GetJSONNumber("const_t");

            if (constTimeObject != null)
            {
                int constTime = constTimeObject.GetIntValue();

                if (!LogicDataTables.GetGlobals().ClampBuildingTimes())
                {
                    if (this.m_upgLevel < this.GetVillageObjectData().GetUpgradeLevelCount() - 1)
                    {
                        constTime = LogicMath.Min(constTime, this.GetVillageObjectData().GetBuildTime(this.m_upgLevel + 1));
                    }
                }

                this.m_constructionTimer = new LogicTimer();
                this.m_constructionTimer.StartTimer(constTime, this.m_level.GetLogicTime(), false, -1);

                LogicJSONNumber constTimeEndObject = jsonObject.GetJSONNumber("const_t_end");

                if (constTimeEndObject != null)
                {
                    this.m_constructionTimer.SetEndTimestamp(constTimeEndObject.GetIntValue());
                }

                LogicJSONNumber conffObject = jsonObject.GetJSONNumber("con_ff");

                if (conffObject != null)
                {
                    this.m_constructionTimer.SetFastForward(conffObject.GetIntValue());
                }

                LogicVillageObjectData villageObjectData = this.GetVillageObjectData();

                if (villageObjectData.IsRequiresBuilder() && !villageObjectData.IsAutomaticUpgrades())
                {
                    this.m_level.GetWorkerManagerAt(this.m_villageType).AllocateWorker(this);
                }

                this.m_upgrading = this.m_upgLevel != -1;
            }

            this.m_upgLevel = LogicMath.Clamp(this.m_upgLevel, 0, this.GetVillageObjectData().GetUpgradeLevelCount() - 1);

            base.Load(jsonObject);

            this.SetPositionXY((this.GetVillageObjectData().GetTileX100() << 9) / 100,
                               (this.GetVillageObjectData().GetTileY100() << 9) / 100);
        }
Example #3
0
        public bool CanUpgrade(bool callListener)
        {
            if (!this.m_locked)
            {
                LogicVillageObjectData villageObjectData = this.GetVillageObjectData();

                if (this.m_upgLevel < villageObjectData.GetUpgradeLevelCount() - 1)
                {
                    if (this.m_level.GetTownHallLevel(this.m_level.GetVillageType()) >= this.GetRequiredTownHallLevelForUpgrade())
                    {
                        return(true);
                    }

                    if (callListener)
                    {
                        this.m_level.GetGameListener().TownHallLevelTooLow(this.GetRequiredTownHallLevelForUpgrade());
                    }

                    return(false);
                }
            }

            return(false);
        }
        /// <summary>
        ///     Loads this instance.
        /// </summary>
        public override void Load(LogicJSONObject jsonObject)
        {
            LogicJSONNumber lvlObject = jsonObject.GetJSONNumber("lvl");

            if (lvlObject != null)
            {
                this._upgLevel = lvlObject.GetIntValue();
                int maxLvl = this.GetVillageObjectData().GetUpgradeLevelCount();

                if (this._upgLevel >= maxLvl)
                {
                    Debugger.Warning(string.Format("LogicVillageObject::load() - Loaded upgrade level {0} is over max! (max = {1}) id {2} data id {3}",
                                                   lvlObject.GetIntValue(),
                                                   maxLvl,
                                                   this._globalId,
                                                   this._data.GetGlobalID()));
                    this._upgLevel = maxLvl - 1;
                }
                else
                {
                    if (this._upgLevel < -1)
                    {
                        Debugger.Error("LogicVillageObject::load() - Loaded an illegal upgrade level!");
                    }
                }
            }
            else
            {
                Debugger.Error("LogicVillageObject::load - Upgrade level was not found!");
            }

            if (this.GetVillageObjectData().RequiresBuilder)
            {
                this._level.GetWorkerManagerAt(this._villageType).DeallocateWorker(this);
            }

            LogicJSONBoolean lockedObject = jsonObject.GetJSONBoolean("locked");

            if (lockedObject != null)
            {
                this._isLocked = lockedObject.IsTrue();
            }

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

            LogicJSONNumber constTimeObject = jsonObject.GetJSONNumber("const_t");

            if (constTimeObject != null)
            {
                int constTime = constTimeObject.GetIntValue();

                if (!LogicDataTables.GetGlobals().ClampBuildingTimes())
                {
                    if (this._upgLevel < this.GetVillageObjectData().GetUpgradeLevelCount() - 1)
                    {
                        constTime = LogicMath.Min(constTime, this.GetVillageObjectData().GetBuildTime(this._upgLevel + 1));
                    }
                }

                this._constructionTimer = new LogicTimer();
                this._constructionTimer.StartTimer(constTime, this._level.GetLogicTime(), false, -1);

                LogicJSONNumber constTimeEndObject = jsonObject.GetJSONNumber("const_t_end");

                if (constTimeEndObject != null)
                {
                    this._constructionTimer.SetEndTimestamp(constTimeEndObject.GetIntValue());
                }

                LogicJSONNumber conffObject = jsonObject.GetJSONNumber("con_ff");

                if (conffObject != null)
                {
                    this._constructionTimer.SetFastForward(conffObject.GetIntValue());
                }

                LogicVillageObjectData villageObjectData = this.GetVillageObjectData();

                if (villageObjectData.RequiresBuilder && !villageObjectData.AutomaticUpgrades)
                {
                    this._level.GetWorkerManagerAt(this._villageType).AllocateWorker(this);
                }

                this._isUpgrading = this._upgLevel != -1;
            }

            this._upgLevel = LogicMath.Min(this._upgLevel, this.GetVillageObjectData().GetUpgradeLevelCount());

            base.Load(jsonObject);

            this.SetInitialPosition((this.GetVillageObjectData().TileX100 << 9) / 100,
                                    (this.GetVillageObjectData().TileY100 << 9) / 100);
        }
Example #5
0
        public void FinishConstruction(bool ignoreState, bool ignoreListener)
        {
            int state = this.m_level.GetState();

            if (state == 1 || !LogicDataTables.GetGlobals().CompleteConstructionOnlyHome() && ignoreState)
            {
                if (this.m_level.GetHomeOwnerAvatar() != null &&
                    this.m_level.GetHomeOwnerAvatar().IsClientAvatar())
                {
                    if (this.m_constructionTimer != null)
                    {
                        this.m_constructionTimer.Destruct();
                        this.m_constructionTimer = null;
                    }

                    LogicAvatar            homeOwnerAvatar = this.m_level.GetHomeOwnerAvatar();
                    LogicVillageObjectData data            = this.GetVillageObjectData();

                    if (data.IsRequiresBuilder())
                    {
                        this.m_level.GetWorkerManagerAt(data.GetVillageType()).DeallocateWorker(this);
                    }

                    this.m_locked = false;

                    if (this.m_upgLevel != 0 || this.m_upgrading)
                    {
                        int nextUpgLevel = this.m_upgLevel + 1;

                        if (this.m_upgLevel >= data.GetUpgradeLevelCount() - 1)
                        {
                            Debugger.Warning("LogicVillageObject - Trying to upgrade to level that doesn't exist! - " + data.GetName());
                            nextUpgLevel = data.GetUpgradeLevelCount() - 1;
                        }

                        int constructionTime = data.GetBuildTime(nextUpgLevel);
                        int xpGain           = LogicMath.Sqrt(constructionTime);

                        this.m_upgLevel = nextUpgLevel;

                        this.XpGainHelper(xpGain, homeOwnerAvatar, ignoreState);
                    }
                    else
                    {
                        int constructionTime = data.GetBuildTime(0);
                        int xpGain           = LogicMath.Sqrt(constructionTime);

                        this.XpGainHelper(xpGain, homeOwnerAvatar, ignoreState);
                    }

                    this.m_upgrading = false;

                    if (this.m_listener != null)
                    {
                        this.m_listener.RefreshState();
                    }

                    if (state == 1)
                    {
                        this.m_level.GetAchievementManager().RefreshStatus();
                    }
                }
                else
                {
                    Debugger.Error("LogicVillageObject::finishCostruction failed - Avatar is null or not client avatar");
                }
            }
        }
Example #6
0
        public override int Execute(LogicLevel level)
        {
            LogicGameObject gameObject = level.GetGameObjectManager().GetGameObjectByID(this.m_gameObjectId);

            if (gameObject != null)
            {
                if (gameObject.GetGameObjectType() == LogicGameObjectType.BUILDING)
                {
                    LogicBuilding     building     = (LogicBuilding)gameObject;
                    LogicBuildingData buildingData = building.GetBuildingData();

                    if (buildingData.IsTownHallVillage2())
                    {
                        if (!LogicUpgradeBuildingCommand.CanUpgradeTHV2(level))
                        {
                            return(-76);
                        }
                    }

                    if (buildingData.GetVillageType() == level.GetVillageType())
                    {
                        if (level.GetGameObjectManager().GetAvailableBuildingUpgradeCount(building) <= 0)
                        {
                            return(-34);
                        }

                        if (building.GetWallIndex() == 0)
                        {
                            if (building.CanUpgrade(true))
                            {
                                int nextUpgradeLevel = building.GetUpgradeLevel() + 1;
                                int buildCost        = buildingData.GetBuildCost(nextUpgradeLevel, level);

                                LogicResourceData buildResourceData = this.m_useAltResource
                                    ? buildingData.GetAltBuildResource(nextUpgradeLevel)
                                    : buildingData.GetBuildResource(nextUpgradeLevel);

                                if (buildResourceData != null)
                                {
                                    LogicClientAvatar playerAvatar = level.GetPlayerAvatar();

                                    if (playerAvatar.HasEnoughResources(buildResourceData, buildCost, true, this, false))
                                    {
                                        if (buildingData.GetConstructionTime(nextUpgradeLevel, level, 0) != 0 || LogicDataTables.GetGlobals().WorkerForZeroBuilTime())
                                        {
                                            if (!level.HasFreeWorkers(this, -1))
                                            {
                                                return(-1);
                                            }
                                        }

                                        playerAvatar.CommodityCountChangeHelper(0, buildResourceData, -buildCost);
                                        building.StartUpgrading(true, false);

                                        return(0);
                                    }
                                }
                            }

                            return(-1);
                        }

                        return(-35);
                    }

                    return(-32);
                }

                if (gameObject.GetGameObjectType() == LogicGameObjectType.TRAP)
                {
                    LogicTrap trap = (LogicTrap)gameObject;

                    if (trap.CanUpgrade(true))
                    {
                        LogicTrapData     data = trap.GetTrapData();
                        LogicResourceData buildResourceData = data.GetBuildResource();
                        LogicClientAvatar playerAvatar      = level.GetPlayerAvatar();

                        int buildCost = data.GetBuildCost(trap.GetUpgradeLevel() + 1);

                        if (playerAvatar.HasEnoughResources(buildResourceData, buildCost, true, this, false))
                        {
                            if (data.GetBuildTime(trap.GetUpgradeLevel() + 1) != 0 || LogicDataTables.GetGlobals().WorkerForZeroBuilTime())
                            {
                                if (!level.HasFreeWorkers(this, -1))
                                {
                                    return(-1);
                                }
                            }

                            playerAvatar.CommodityCountChangeHelper(0, buildResourceData, -buildCost);
                            trap.StartUpgrading();

                            return(0);
                        }
                    }
                }
                else if (gameObject.GetGameObjectType() == LogicGameObjectType.VILLAGE_OBJECT)
                {
                    if (!this.m_useAltResource)
                    {
                        LogicVillageObject villageObject = (LogicVillageObject)gameObject;

                        if (villageObject.CanUpgrade(true))
                        {
                            LogicVillageObjectData data = villageObject.GetVillageObjectData();
                            LogicResourceData      buildResourceData = data.GetBuildResource();

                            int buildCost = data.GetBuildCost(villageObject.GetUpgradeLevel() + 1);

                            if (buildResourceData != null)
                            {
                                LogicClientAvatar playerAvatar = level.GetPlayerAvatar();

                                if (playerAvatar.HasEnoughResources(buildResourceData, buildCost, true, this, false))
                                {
                                    if (data.GetBuildTime(villageObject.GetUpgradeLevel() + 1) != 0 || LogicDataTables.GetGlobals().WorkerForZeroBuilTime())
                                    {
                                        if (!level.HasFreeWorkers(this, -1))
                                        {
                                            return(-1);
                                        }
                                    }

                                    playerAvatar.CommodityCountChangeHelper(0, buildResourceData, -buildCost);
                                    villageObject.StartUpgrading(true);

                                    return(0);
                                }
                            }
                        }

                        return(-1);
                    }

                    return(-31);
                }
            }

            return(-1);
        }