Esempio n. 1
0
        /// <summary>
        /// Apply action-related changes to the world.
        /// </summary>
        /// <returns> Returns <see cref="System.Collections.Concurrent.ConcurrentBag"/> class with the affected region./></returns>
        public override ConcurrentBag <Core.Models.Region> Do()
        {
            var action            = (Core.Models.Action)Model;
            var entityPosition    = (PositionI)action.Parameters[CREATE_POSITION];
            var region            = Controller.Instance.RegionManagerController.GetRegion(entityPosition.RegionPosition);
            var entityCellPostion = entityPosition.CellPosition;
            var type = (long)action.Parameters[CREATION_TYPE];

            var entityDef    = Controller.Instance.DefinitionManagerController.DefinitionManager.GetDefinition((EntityType)type);
            var entityHealth = ((UnitDefinition)entityDef).Health;
            var entityMoves  = ((UnitDefinition)entityDef).Moves;

            // create the new entity and link to the correct account
            var entity = new Core.Models.Entity(
                IdGenerator.GetId(),
                entityDef,
                action.Account,
                entityPosition,
                entityHealth,
                entityMoves);

            entity.Position = entityPosition;
            region.AddEntity(action.ActionTime, entity);

            // link the headquarter to the current account and claim territory, , enable build options
            if (m_type.SubType == EntityType.Headquarter &&
                action.Account != null)
            {
                action.Account.TerritoryBuildings.Add(entity.Position, type);
                LogicRules.EnableBuildOptions(type, action.Account);
                region.ClaimTerritory(LogicRules.GetSurroundedPositions(entityPosition, m_drawArea), action.Account, region.RegionPosition, Controller.Instance.RegionManagerController.RegionManager);
                LogicRules.IncreaseWholeStorage(action.Account);
                LogicRules.GatherResources(action.Account, action.ActionTime, Controller.Instance.RegionManagerController, Constants.HEADQUARTER_TERRITORY_RANGE);
                LogicRules.SetCurrentMaxPopulation(action.Account);
                LogicRules.SetCurrentMaxEnergy(action.Account);
            }
            else if (action.Account != null)
            {
                action.Account.TerritoryBuildings.Add(entity.Position, type);
                region.ClaimTerritory(LogicRules.GetSurroundedPositions(entityPosition, m_drawArea), action.Account, region.RegionPosition, Controller.Instance.RegionManagerController.RegionManager);
            }

            LogicRules.ConsumeResource(action.Account, action.ActionTime, entityDef);
            return(new ConcurrentBag <Core.Models.Region>()
            {
                region
            });
        }
Esempio n. 2
0
        /// <summary>
        /// Returns if the action is even possible.
        /// </summary>
        /// <returns> True if the Building is buildable at the current position, otherwise false.</returns>
        public override bool Possible()
        {
            var action            = (Core.Models.Action)Model;
            var entityPosition    = (PositionI)action.Parameters[CREATE_POSITION];
            var entityCellPostion = entityPosition.CellPosition;
            var region            = Controller.Instance.RegionManagerController.GetRegion(entityPosition.RegionPosition);
            var type = (long)action.Parameters[CREATION_TYPE];

            m_type = Controller.Instance.DefinitionManagerController.DefinitionManager.GetDefinition((EntityType)type);
            var account = action.Account;

            if (type != null || account != null)
            {
                // Filter for territory buildings
                if (type == (long)Models.Definitions.EntityType.Headquarter)
                {
                    m_drawArea = Constants.HEADQUARTER_TERRITORY_RANGE;
                }
                else if (type == (long)Models.Definitions.EntityType.GuardTower)
                {
                    m_drawArea = Constants.GUARDTOWER_TERRITORY_RANGE;
                }

                if (!action.Account.TerritoryBuildings.ContainsValue((long)Core.Models.Definitions.EntityType.Headquarter) &&
                    m_type.SubType == Models.Definitions.EntityType.Headquarter &&
                    region.GetEntity(entityCellPostion) == null &&
                    region.GetClaimedTerritory(entityPosition) == null)
                {
                    // terrain check
                    var  td            = (TerrainDefinition)region.GetTerrain(entityCellPostion);
                    var  list          = LogicRules.GetSurroundedPositions(entityPosition, m_drawArea);
                    bool territoryFlag = true;
                    // check the map for enemy territory if there is a enemy territory to close at new borders a territory building cant be build
                    foreach (var position in list)
                    {
                        var tile = region.GetClaimedTerritory(position);
                        if (tile != null)
                        {
                            territoryFlag = false;
                        }
                    }
                    if (territoryFlag)
                    {
                        return(td.Buildable && LogicRules.CheckResource(account, action.ActionTime, m_type));
                    }
                }
                else if (region.GetEntity(entityCellPostion) == null &&
                         m_type.SubType != Models.Definitions.EntityType.Headquarter &&
                         region.GetClaimedTerritory(entityPosition) == account)
                {
                    // check for free tile and the terrain is possesed from the current player
                    var  td            = (TerrainDefinition)region.GetTerrain(entityCellPostion);
                    var  list          = LogicRules.GetSurroundedPositions(entityPosition, m_drawArea);
                    bool territoryFlag = true;
                    // check the map for enemy territory if there is a enemy territory to close at new borders a territory building cant be build
                    foreach (var position in list)
                    {
                        var tile = region.GetClaimedTerritory(position);
                        if (tile != account &&
                            tile != null)
                        {
                            territoryFlag = false;
                        }
                    }
                    if (territoryFlag)
                    {
                        return(td.Buildable && LogicRules.CheckResource(account, action.ActionTime, m_type));
                    }
                }
            }
            return(false);
        }