// for a given unit, get a list of all the possible fields in moving range of the unit List <MGPumField> getPossibleFields(MGPumUnit unit) { List <MGPumField> possibleFields = new List <MGPumField>(); Vector2Int position = unit.field.coords; MGPumField field = new MGPumField(unit.coords.x, unit.coords.y); for (int horizontal = 0; horizontal <= unit.currentSpeed; horizontal++) { for (int vertical = 0; vertical <= unit.currentSpeed; vertical++) { // check every direction if (state.getField(position + Vector2Int.up * vertical + Vector2Int.left * horizontal) != null) { field = state.getField(position + Vector2Int.up * vertical + Vector2Int.left * horizontal); if (!possibleFields.Contains(field) && field.coords != position && state.fields.inBounds(field.coords) && state.getUnitForField(field) == null) { possibleFields.Add(field); } } if (state.getField(position + Vector2Int.up * vertical + Vector2Int.right * horizontal) != null) { field = state.getField(position + Vector2Int.up * vertical + Vector2Int.right * horizontal); if (!possibleFields.Contains(field) && field.coords != position && state.fields.inBounds(field.coords) && state.getUnitForField(field) == null) { possibleFields.Add(field); } } if (state.getField(position + Vector2Int.down * vertical + Vector2Int.left * horizontal) != null) { field = state.getField(position + Vector2Int.down * vertical + Vector2Int.left * horizontal); if (!possibleFields.Contains(field) && field.coords != position && state.fields.inBounds(field.coords) && state.getUnitForField(field) == null) { possibleFields.Add(field); } } if (state.getField(position + Vector2Int.down * vertical + Vector2Int.right * horizontal) != null) { field = state.getField(position + Vector2Int.down * vertical + Vector2Int.right * horizontal); if (!possibleFields.Contains(field) && field.coords != position && state.fields.inBounds(field.coords) && state.getUnitForField(field) == null) { possibleFields.Add(field); } } } } return(possibleFields); }
internal override MGPumCommand calculateCommand() { int enemyID = 1 - playerID; //List<MGPumUnit> possibleMovers = new List<MGPumUnit>; foreach (MGPumUnit unit in state.getAllUnitsInZone(MGPumZoneType.Battlegrounds, this.playerID)) { if (stateOracle.canAttack(unit) && unit.currentRange > 0) { MGPumField goal = state.getField(unit.field.coords + Vector2Int.up); if (goal != null && goal.unit != null && goal.unit.ownerID == enemyID) { MGPumAttackChainMatcher matcher = unit.getAttackMatcher(); MGPumFieldChain chain = new MGPumFieldChain(this.playerID, matcher); chain.add(unit.field); chain.add(goal); MGPumAttackCommand command = new MGPumAttackCommand(playerID, chain, unit); return(command); } } if (stateOracle.canMove(unit) && unit.currentSpeed > 0) { //possibleMovers.Add(unit); MGPumField goal = state.getField(unit.field.coords + Vector2Int.up); if (goal != null) { if (goal.unit == null) { MGPumMoveChainMatcher matcher = unit.getMoveMatcher(); MGPumFieldChain chain = new MGPumFieldChain(this.playerID, matcher); chain.add(unit.field); chain.add(goal); MGPumMoveCommand command = new MGPumMoveCommand(this.playerID, chain, unit); return(command); } } } } return(new MGPumEndTurnCommand(this.playerID)); }
// find possible attack and return it MGPumAttackCommand findAttack(MGPumField field, MGPumUnit unit, MGPumGameState state) { // can we find this out in a better way? queued = new bool[8, 8]; tilesToVisit = new SortedSet <Vector2Int>(new AStarComparer(field.coords)); predecessors = new Dictionary <Vector2Int, Vector2Int>(); //distance = new Dictionary<Vector2Int, int>(); tilesToVisit.Add(unit.field.coords); queued[unit.field.coords.x, unit.field.coords.y] = true; int recursion = 0; int maxRecursion = 500; while (tilesToVisit.Count > 0) { recursion++; if (recursion > maxRecursion) { break; } Vector2Int position = tilesToVisit.Min; tilesToVisit.Remove(position); if (!state.fields.inBounds(position)) { continue; } //touchedTiles.Add(position); if (position == field.coords) { List <Vector2Int> path = new List <Vector2Int>(); path.Add(position); //reconstruct path in reverse while (predecessors.ContainsKey(path[0])) { path.Insert(0, predecessors[path[0]]); } MGPumAttackChainMatcher matcher = unit.getAttackMatcher(); MGPumFieldChain chain = new MGPumFieldChain(unit.ownerID, matcher); for (int i = 0; i < path.Count; i++) { chain.add(state.getField(path[i])); } if (chain.isValidChain()) { MGPumAttackCommand ac = new MGPumAttackCommand(this.playerID, chain, unit); return(ac); } continue; } foreach (Vector2Int direction in getDirections()) { Vector2Int neighbor = position + direction; if (!state.fields.inBounds(neighbor)) { continue; } if (!queued[neighbor.x, neighbor.y]) { queued[neighbor.x, neighbor.y] = true; tilesToVisit.Add(neighbor); predecessors.Add(neighbor, position); //distance.Add(neighbor, ) } } } return(null); }
// return a list of fields where allied units are standing List <MGPumField> getAllyFields(MGPumUnit unit, MGPumGameState state) { List <MGPumField> possibleFields = new List <MGPumField>(); Vector2Int position = unit.field.coords; MGPumField field = new MGPumField(unit.coords.x, unit.coords.y); MGPumUnit foundUnit = null; for (int horizontal = 0; horizontal <= unit.currentSpeed; horizontal++) { for (int vertical = 0; vertical <= unit.currentSpeed; vertical++) { if (state.getField(position + Vector2Int.up * vertical + Vector2Int.left * horizontal) != null) { field = state.getField(position + Vector2Int.up * vertical + Vector2Int.left * horizontal); foundUnit = state.getUnitForField(field); if (foundUnit != null) { if (!possibleFields.Contains(field) && field.coords != position && state.fields.inBounds(field.coords) && foundUnit.ownerID == unit.ownerID) { possibleFields.Add(field); } } } if (state.getField(position + Vector2Int.up * vertical + Vector2Int.right * horizontal) != null) { field = state.getField(position + Vector2Int.up * vertical + Vector2Int.right * horizontal); foundUnit = state.getUnitForField(field); if (foundUnit != null) { if (!possibleFields.Contains(field) && field.coords != position && state.fields.inBounds(field.coords) && foundUnit.ownerID == unit.ownerID) { possibleFields.Add(field); } } } if (state.getField(position + Vector2Int.down * vertical + Vector2Int.left * horizontal) != null) { field = state.getField(position + Vector2Int.down * vertical + Vector2Int.left * horizontal); foundUnit = state.getUnitForField(field); if (foundUnit != null) { if (!possibleFields.Contains(field) && field.coords != position && state.fields.inBounds(field.coords) && foundUnit.ownerID == unit.ownerID) { possibleFields.Add(field); } } } if (state.getField(position + Vector2Int.down * vertical + Vector2Int.right * horizontal) != null) { field = state.getField(position + Vector2Int.down * vertical + Vector2Int.right * horizontal); foundUnit = state.getUnitForField(field); if (foundUnit != null) { if (!possibleFields.Contains(field) && field.coords != position && state.fields.inBounds(field.coords) && foundUnit.ownerID == unit.ownerID) { possibleFields.Add(field); } } } } } return(possibleFields); }