/// <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 region = Controller.Instance.RegionManagerController.GetRegion(entityPosition.RegionPosition); var type = (long)action.Parameters[CREATION_TYPE]; var entityDef = Controller.Instance.DefinitionManagerController.DefinitionManager.GetDefinition((EntityType)type); var account = action.Account; var list = new List <long>(); if (account != null) { account.BuildableBuildings.TryGetValue((long)Models.Definitions.EntityType.Headquarter, out list); if (list != null) { if (list.Contains(type)) { if (region.GetEntity(entityPosition.CellPosition) == null && region.GetClaimedTerritory(entityPosition) == account) { // check for free tile and the terrain is possesed from the current player var td = (TerrainDefinition)region.GetTerrain(entityPosition.CellPosition); return(td.Buildable && LogicRules.CheckResource(account, action.ActionTime, entityDef)); } } } } return(false); }
/// <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); action.Account.Buildings.Add(entity.Position, type); LogicRules.IncreaseResourceGeneration(action.Account, action.ActionTime, entity.Position, Controller.Instance.RegionManagerController); LogicRules.EnableBuildOptions(type, action.Account); LogicRules.IncreaseStorage(action.Account, entity); LogicRules.ConsumeResource(action.Account, action.ActionTime, entityDef); return(new ConcurrentBag <Core.Models.Region>() { region }); }
/// <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 regionManagerC = Controller.Instance.RegionManagerController; var action = (Core.Models.Action)Model; var positionI = (PositionI)action.Parameters[CREATE_POSITION]; var type = (long)action.Parameters[CREATION_TYPE]; var entity = Controller.Instance.RegionManagerController.GetRegion(positionI.RegionPosition).GetEntity(positionI.CellPosition); var list = new List <long>(); if (entity != null) { action.Account.BuildableBuildings.TryGetValue(entity.DefinitionID, out list); if (list != null) { if (action.AccountID == entity.OwnerID && list.Contains(type)) { RealCreatePosition = GetFreeField(positionI, regionManagerC); return(RealCreatePosition != null && LogicRules.CheckResource(action.Account, action.ActionTime, Controller.Instance.DefinitionManagerController.DefinitionManager.GetDefinition((EntityType)type))); } } } return(false); }
public void StorageTest() { var acc = GetTestAcc(); var oldEnergy = acc.Energy.MaximumValue; var oldPopulation = acc.Population.MaximumValue; var oldPlutonium = acc.Population.MaximumValue; var oldScrap = acc.Scrap.MaximumValue; var oldTechnology = acc.Technology.MaximumValue; LogicRules.IncreaseWholeStorage(acc); Assert.AreNotEqual(oldEnergy, acc.Energy.MaximumValue); Assert.AreNotEqual(oldPopulation, acc.Population.MaximumValue); Assert.AreNotEqual(oldPlutonium, acc.Plutonium.MaximumValue); Assert.AreNotEqual(oldScrap, acc.Scrap.MaximumValue); Assert.AreNotEqual(oldTechnology, acc.Technology.MaximumValue); LogicRules.DecreaseWholeStorage(acc); Assert.AreEqual(oldEnergy, acc.Energy.MaximumValue); Assert.AreEqual(oldPopulation, acc.Population.MaximumValue); Assert.AreEqual(oldPlutonium, acc.Plutonium.MaximumValue); Assert.AreEqual(oldScrap, acc.Scrap.MaximumValue); Assert.AreEqual(oldTechnology, acc.Technology.MaximumValue); // Increase and Decrease Scrap Strorage var entity = new Entity(336, Core.Models.Definitions.EntityType.Scrapyard, acc, new PositionI(new Position(new LatLon(50.97695325, 11.02396488))), 100, 0); LogicRules.IncreaseStorage(acc, entity); Assert.AreNotEqual(oldScrap, acc.Scrap.MaximumValue); LogicRules.DecreasStorage(acc, entity); Assert.AreEqual(oldScrap, acc.Scrap.MaximumValue); // Increase and Decrease Population Strorage entity = new Entity(342, Core.Models.Definitions.EntityType.Tent, acc, new PositionI(new Position(new LatLon(50.97695325, 11.02396488))), 100, 0); LogicRules.IncreaseStorage(acc, entity); Assert.AreNotEqual(oldPopulation, acc.Population.MaximumValue); LogicRules.DecreasStorage(acc, entity); Assert.AreEqual(oldPopulation, acc.Population.MaximumValue); // Increase and Decrease Energy Strorage entity = new Entity(330, Core.Models.Definitions.EntityType.Transformer, acc, new PositionI(new Position(new LatLon(50.97695325, 11.02396488))), 100, 0); LogicRules.IncreaseStorage(acc, entity); Assert.AreNotEqual(oldEnergy, acc.Energy.MaximumValue); LogicRules.DecreasStorage(acc, entity); Assert.AreEqual(oldEnergy, acc.Energy.MaximumValue); }
/// <summary> /// Get a list of algebraic and logic simplification rules /// </summary> /// <returns></returns> public static IRule[] GetSimplificationRules() { var simplificationRules = AlgebraicRules.Get() .Where(r => r.Tags.Contains(StdTags.Simplification)) .ToList(); simplificationRules.AddRange(LogicRules.Get().Where(r => r.Tags.Contains(StdTags.Simplification))); return(simplificationRules.ToArray()); }
public LogicRules GenerateRules(IEnumerable <CustomSocket> sockets) { var logicRules = new LogicRules(); foreach (var socket in sockets) { logicRules.AddSocket(socket); } return(logicRules); }
/// <summary> /// Check all possible spawn locations around a building. /// Check surrounding area around the building for units, if there is a free location this will be returned. /// </summary> /// <returns>The free field.</returns> /// <param name="position">IPosition of the current selected building..</param> /// <param name="regionManagerC">Region manager c.</param> private PositionI GetFreeField(PositionI position, RegionManagerController regionManagerC) { foreach (var surpos in LogicRules.GetSurroundedFields(position)) { var td = regionManagerC.GetRegion(surpos.RegionPosition).GetTerrain(surpos.CellPosition); var ed = regionManagerC.GetRegion(surpos.RegionPosition).GetEntity(surpos.CellPosition); if (td.Walkable && ed == null) { return(surpos); } } return(null); }
/// <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 }); }
public void BuildOptions() { var acc = GetTestAcc(); Assert.IsNotNull(acc.BuildableBuildings); var buildOpt = acc.BuildableBuildings; LogicRules.EnableBuildOptions((long)Core.Models.Definitions.EntityType.Headquarter, acc); Assert.AreNotEqual(buildOpt, acc.BuildableBuildings); Assert.Contains((long)Core.Models.Definitions.EntityType.Headquarter, (System.Collections.ICollection)acc.BuildableBuildings); LogicRules.DisableBuildOptions((long)Core.Models.Definitions.EntityType.Headquarter, acc); Assert.AreEqual(buildOpt, acc.BuildableBuildings); }
/// <summary> /// Gets the adjacent regions, which can be affected. /// </summary> /// <returns>The adjacent regions.</returns> /// <param name="regionManagerC">Region manager c.</param> /// <param name="position">Current Position od the selected building.</param> /// <param name="buildpoint">PositionI from the new unit.</param> private ConcurrentBag <RegionPosition> GetAdjacentRegions(RegionManagerController regionManagerC, RegionPosition position, PositionI buildpoint) { var list = new ConcurrentBag <RegionPosition>(); var surlist = LogicRules.SurroundRegions; var regionSizeX = Constants.REGION_SIZE_X; var regionSizeY = Constants.REGION_SIZE_Y; var currentpos = LogicRules.GetSurroundedFields(buildpoint); var currentregion = regionManagerC.RegionManager.GetRegion(buildpoint.RegionPosition); foreach (var checkingpos in currentpos) { if (regionManagerC.GetRegion(checkingpos.RegionPosition) != currentregion) { list.Add(regionManagerC.RegionManager.GetRegion(checkingpos.RegionPosition).RegionPosition); } } return(list); }
/// <summary> /// Apply action-related changes to the world. /// Returns set of changed Regions if everything worked, otherwise null /// </summary> /// <returns>all affected (changed) regions</returns> public override ConcurrentBag <Core.Models.Region> Do() { var regionManagerC = Controller.Instance.RegionManagerController; var action = (Core.Models.Action)Model; var positionI = (PositionI)action.Parameters[CREATE_POSITION]; var type = (long)action.Parameters[CREATION_TYPE]; RealCreatePosition = GetFreeField(positionI, regionManagerC); positionI = RealCreatePosition; var region = regionManagerC.GetRegion(positionI.RegionPosition); var entityDef = Controller.Instance.DefinitionManagerController.DefinitionManager.GetDefinition((EntityType)type); var unitHealth = ((UnitDefinition)entityDef).Health; var unitMoves = ((UnitDefinition)entityDef).Moves; // create the new entity and link to the correct account var entity = new Core.Models.Entity( IdGenerator.GetId(), entityDef, action.Account, positionI, unitHealth, unitMoves); entity.Position = positionI; region.AddEntity(action.ActionTime, entity); if (action.Account != null) { action.Account.Units.AddLast(entity.Position); LogicRules.ConsumeResource(action.Account, action.ActionTime, entityDef); } return(new ConcurrentBag <Core.Models.Region>() { region }); }
/// <summary> /// Gets the surrounded positions. /// </summary> /// <returns>The surrounded positions.</returns> /// <param name="startPoint">Start point.</param> /// <param name="accountID">Account ID.</param> /// <param name="moves">Moves of the entity.</param> private HashSet <PositionI> GetSurroundedPositions(PositionI startPoint, int accountID, int moves) { var fieldSet = new HashSet <PositionI>(); var fieldSetHelper = new HashSet <PositionI>(); fieldSet.Add(startPoint); fieldSetHelper.Add(startPoint); for (int index = 0; index != moves; ++index) { var tempfieldSet = new HashSet <PositionI>(); foreach (var item in fieldSetHelper) { var surroundedTiles = LogicRules.GetSurroundedFields(item); foreach (var tile in surroundedTiles) { if (!fieldSet.Contains(tile)) { var region = World.Instance.RegionManager.GetRegion(tile.RegionPosition); var terrainDefinition = region.GetTerrain(tile.CellPosition); if (terrainDefinition.Walkable) { var unit = region.GetEntity(tile.CellPosition); if (unit == null) { fieldSet.Add(tile); tempfieldSet.Add(tile); } } } } } fieldSetHelper = tempfieldSet; } return(fieldSet); }
public LogicProcessor(LogicRules rules, ProcessorOptions options) : base(options) { _rules = rules; }
/// <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); }
/// <summary> /// Apply action-related changes to the world. /// Returns set of changed Regions if everything worked, otherwise null /// </summary> /// <returns>all affected (changed) regions</returns> public override ConcurrentBag <Core.Models.Region> Do() { var action = (Core.Models.Action)Model; var startPosition = (PositionI)action.Parameters[START_POSITION]; var endPosition = (PositionI)action.Parameters[END_POSITION]; var regionStartPos = Controller.Instance.RegionManagerController.GetRegion(startPosition.RegionPosition); var regionEndPos = Controller.Instance.RegionManagerController.GetRegion(endPosition.RegionPosition); var bag = new ConcurrentBag <Core.Models.Region>(); var entity = regionStartPos.GetEntity(startPosition.CellPosition); if (m_fight) { var enemyEntity = regionEndPos.GetEntity(endPosition.CellPosition); // Ranged attack units deal only 1 dmg to buildings if (((UnitDefinition)entity.Definition).AttackRange > 1 && enemyEntity.Definition.Category == Category.Building) { enemyEntity.Health -= 1; } else if (((UnitDefinition)entity.Definition).AttackRange - m_fightDistance < 0) { // iterate trough all methods to modifie the attack LogicRules.AllAttackModifierRangedInMeele(entity); enemyEntity.Health -= entity.ModfiedAttackValue - enemyEntity.ModifiedDefenseValue; if (((UnitDefinition)enemyEntity.Definition).AttackRange >= m_fightDistance) { entity.Health -= enemyEntity.ModfiedAttackValue - entity.ModifiedDefenseValue; } } else { // iterate trough all methods to modifie the attack LogicRules.AllAttackModifier(entity); LogicRules.AllDefenseModifier(enemyEntity); enemyEntity.Health -= entity.ModfiedAttackValue - enemyEntity.ModifiedDefenseValue; if (((UnitDefinition)enemyEntity.Definition).AttackRange >= m_fightDistance) { LogicRules.AllAttackModifier(enemyEntity); LogicRules.AllDefenseModifier(entity); entity.Health -= enemyEntity.ModfiedAttackValue - entity.ModifiedDefenseValue; } } if (enemyEntity.Health <= 0) { LogicRules.DestroyBuilding(enemyEntity, regionEndPos, action, Controller.Instance.RegionManagerController); regionEndPos.RemoveEntity(action.ActionTime, enemyEntity); enemyEntity.Owner.Units.Remove(enemyEntity.Position); } else if (entity.Health <= 0) { LogicRules.DestroyBuilding(entity, regionStartPos, action, Controller.Instance.RegionManagerController); regionStartPos.RemoveEntity(action.ActionTime, entity); entity.Owner.Units.Remove(entity.Position); } } else { regionStartPos.RemoveEntity(action.ActionTime, entity); regionEndPos.AddEntity(action.ActionTime, entity); entity.Position = endPosition; } bag.Add(regionStartPos); if (startPosition.RegionPosition != endPosition.RegionPosition) { bag.Add(regionEndPos); } action.Parameters[CLIENT_UNIT_INFOS] = entity; return(bag); }
/// <summary> /// Returns any nodes that are adjacent to <paramref name="fromNode"/> and may be considered to form the next step in the path /// </summary> /// <param name="fromNode">The node from which to return the next possible nodes in the path</param> /// <returns>A list of next possible nodes in the path</returns> private List <Node> GetAdjacentWalkableNodes(Node fromNode) { List <Node> walkableNodes = new List <Node>(); // check surrounded tiles of the current position foreach (var newPosition in LogicRules.GetSurroundedFields(fromNode.Location)) { // gather environment information var region = World.Instance.RegionManager.GetRegion(newPosition.RegionPosition); var terrainDefinition = region.GetTerrain(newPosition.CellPosition); // check terrain for walkable and other units in the path if (terrainDefinition.Walkable) { var unit = region.GetEntity(newPosition.CellPosition); // field is free from everything if (unit == null) { if (m_nodes.ContainsKey(newPosition)) { // use the dictionary and get the Node at the positonI var node = m_nodes[newPosition]; if (node.State == NodeState.Open) { // calculate the travel cost to the next tile double traversalCost = terrainDefinition.TravelCost; double temp = node.G + traversalCost; if (temp < node.G) { node.ParentNode = fromNode; walkableNodes.Add(node); } } } else { // if the Node was not open insert a new one in the dictionary var newNode = new Node(newPosition, searchParameters.EndLocation); newNode.ParentNode = fromNode; walkableNodes.Add(newNode); m_nodes[newPosition] = newNode; } } else if (unit != null && newPosition == searchParameters.EndLocation && unit.OwnerID != searchParameters.AccountID) { if (m_nodes.ContainsKey(newPosition)) { // use the dictionary and get the Node at the positonI var node = m_nodes[newPosition]; if (node.State == NodeState.Open) { // calculate the travel cost to the next tile double traversalCost = terrainDefinition.TravelCost; double temp = node.G + traversalCost; if (temp < node.G) { node.ParentNode = fromNode; walkableNodes.Add(node); } } } else { // if the Node was not open insert a new one in the dictionary var newNode = new Node(newPosition, searchParameters.EndLocation); newNode.ParentNode = fromNode; walkableNodes.Add(newNode); m_nodes[newPosition] = newNode; } } } } return(walkableNodes); }