ctac.MapService.GetMovableNeighbors C# (CSharp) Method

GetMovableNeighbors() public method

Find neighboring tiles that aren't occupied by enemies, but always include the dest tile for attacking if it's passed but also make sure not to land on a tile with an occupant if attacking
public GetMovableNeighbors ( Tile center, int controllingPlayerId, Tile dest = null ) : Tile>.Dictionary
center Tile
controllingPlayerId int
dest Tile
return Tile>.Dictionary
        public Dictionary<Vector2, Tile> GetMovableNeighbors(Tile center, int controllingPlayerId, Tile dest = null)
        {
            var ret = GetNeighbors(center.position);

            //filter tiles that are too high/low to move to & are passable
            ret = ret.Where(t => !t.Value.unpassable && isHeightPassable(t.Value, center)).ToDictionary(k => k.Key, v => v.Value);

            //filter out tiles with enemies on them that aren't the destination
            ret = ret.Where(t =>
                (dest != null && t.Key == dest.position) ||
                !pieces.Pieces.Any(m => m.tilePosition == t.Key && m.playerId != controllingPlayerId)
            ).ToDictionary(k => k.Key, v => v.Value);

            bool destinationOccupied = dest != null && pieces.Pieces.Any(p => p.tilePosition == dest.position);

            //make sure not to consider tiles that would be where the moving pieces lands when it attacks
            ret = ret.Where(t =>
                dest == null
                || dest.position == t.Key
                || !destinationOccupied
                || TileDistance(t.Key, dest.position) > 1
                || !pieces.Pieces.Any(p => p.tilePosition == t.Key)
            ).ToDictionary(k => k.Key, v => v.Value);

            return ret;
        }