From 770bc6cf967fc779775ccadc49fa4347859185a3 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Mon, 25 Mar 2024 16:04:31 +0100 Subject: improve tile connection detection --- go/game/map.go | 39 +++++++++++++-------------------------- 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/go/game/map.go b/go/game/map.go index 5ed1335e..378e9f2c 100644 --- a/go/game/map.go +++ b/go/game/map.go @@ -140,52 +140,39 @@ func (m *Map) FindNeighbours(x, y int) (left, right, above, below *Tile) { return left, right, above, below } -func (m *Map) FindAnyConnections(x, y int) (connections int, left, right, above, below bool) { +func (m *Map) FindConnections(x, y int, isConnection func(TileType) bool) (connections int, left, right, above, below bool) { leftT, rightT, aboveT, belowT := m.FindNeighbours(x, y) connections = 0 - if leftT != nil && leftT.Type != TileTypes.Neutral { + if leftT != nil && isConnection(leftT.Type) { left = true connections += 1 } - if rightT != nil && rightT.Type != TileTypes.Neutral { + if rightT != nil && isConnection(rightT.Type) { right = true connections += 1 } - if aboveT != nil && aboveT.Type != TileTypes.Neutral { + if aboveT != nil && isConnection(aboveT.Type) { above = true connections += 1 } - if belowT != nil && belowT.Type != TileTypes.Neutral { + if belowT != nil && isConnection(belowT.Type) { below = true connections += 1 } - return } -func (m *Map) FindStreetConnections(x int, y int) (connections int, left, right, above, below bool) { - leftT, rightT, aboveT, belowT := m.FindNeighbours(x, y) - connections = 0 +func (m *Map) FindAnyConnections(x, y int) (connections int, left, right, above, below bool) { + return m.FindConnections(x, y, func(t TileType) bool { return t != TileTypes.Neutral }) +} - if leftT != nil && leftT.Type == TileTypes.Street { - left = true - connections += 1 - } - if rightT != nil && rightT.Type == TileTypes.Street { - right = true - connections += 1 - } - if aboveT != nil && aboveT.Type == TileTypes.Street { - above = true - connections += 1 - } - if belowT != nil && belowT.Type == TileTypes.Street { - below = true - connections += 1 - } +func (m *Map) FindStreetConnections(x int, y int) (connections int, left, right, above, below bool) { + return m.FindConnections(x, y, func(t TileType) bool { return t == TileTypes.Street }) +} - return +func (m *Map) FindFortificationConnections(x int, y int) (connections int, left, right, above, below bool) { + return m.FindConnections(x, y, func(t TileType) bool { return t.IsFortification() }) } func GetMap(name string) (*Map, error) { -- cgit v1.2.3