aboutsummaryrefslogtreecommitdiff
path: root/go/game/map.go
diff options
context:
space:
mode:
Diffstat (limited to 'go/game/map.go')
-rw-r--r--go/game/map.go75
1 files changed, 60 insertions, 15 deletions
diff --git a/go/game/map.go b/go/game/map.go
index 0b6fe87c..cecce9ca 100644
--- a/go/game/map.go
+++ b/go/game/map.go
@@ -2,12 +2,15 @@ package game
import (
"fmt"
+ "errors"
"io"
"log"
+ "math/rand"
"net/http"
"os"
"path"
"strings"
+ "time"
"gopkg.in/yaml.v3"
)
@@ -27,25 +30,30 @@ type MapYml struct {
type Map struct {
Tiles [][]Tile
+ symbols map[string]string
RessourceGain int
}
-func parseMap(r io.Reader) (*Map, error) {
+func readMap(r io.Reader) (*Map, error) {
data, err := io.ReadAll(r)
if err != nil {
return nil, err
}
+ return ParseMap(data)
+}
+
+func ParseMap(data []byte) (*Map, error) {
mapYml := MapYml{}
- err = yaml.Unmarshal(data, &mapYml)
+ err := yaml.Unmarshal(data, &mapYml)
if err != nil {
return nil, err
}
- m := &Map{[][]Tile{}, DEFAULT_RESSOURCE_GAIN}
-
mapYml.Symbols[" "] = "neutral"
+ m := &Map{[][]Tile{}, mapYml.Symbols, DEFAULT_RESSOURCE_GAIN}
+
rows := strings.Split(mapYml.Map, "\n")
for y := 0; y < len(rows); y++ {
m.Tiles = append(m.Tiles, []Tile{})
@@ -111,19 +119,19 @@ func (m *Map) FindAnyConnections(x, y int) (connections int, left, right, above,
leftT, rightT, aboveT, belowT := m.FindNeighbours(x, y)
connections = 0
- if leftT.Type != TileTypes.Neutral {
+ if leftT != nil && leftT.Type != TileTypes.Neutral {
left = true
connections += 1
}
- if rightT.Type != TileTypes.Neutral {
+ if rightT != nil && rightT.Type != TileTypes.Neutral {
right = true
connections += 1
}
- if aboveT.Type != TileTypes.Neutral {
+ if aboveT != nil && aboveT.Type != TileTypes.Neutral {
above = true
connections += 1
}
- if belowT.Type != TileTypes.Neutral {
+ if belowT != nil && belowT.Type != TileTypes.Neutral {
below = true
connections += 1
}
@@ -135,19 +143,19 @@ func (m *Map) FindStreetConnections(x int, y int) (connections int, left, right,
leftT, rightT, aboveT, belowT := m.FindNeighbours(x, y)
connections = 0
- if leftT.Type == TileTypes.Street {
+ if leftT != nil && leftT.Type == TileTypes.Street {
left = true
connections += 1
}
- if rightT.Type == TileTypes.Street {
+ if rightT != nil && rightT.Type == TileTypes.Street {
right = true
connections += 1
}
- if aboveT.Type == TileTypes.Street {
+ if aboveT != nil && aboveT.Type == TileTypes.Street {
above = true
connections += 1
}
- if belowT.Type == TileTypes.Street {
+ if belowT != nil && belowT.Type == TileTypes.Street {
below = true
connections += 1
}
@@ -163,19 +171,56 @@ func GetMap(name string) (*Map, error) {
log.Fatal(err)
}
defer resp.Body.Close()
- return parseMap(resp.Body)
+ return readMap(resp.Body)
}
func LoadMap(file string) (*Map, error) {
- f, err := os.Open("/tmp/123")
+ f, err := os.Open(file)
if err != nil {
log.Fatal("Opening map ", file, " failed: ", err)
}
defer f.Close()
- return parseMap(f)
+ return readMap(f)
}
func (m *Map) TileAt(p Position) *Tile {
return &m.Tiles[p.Y][p.X]
}
+
+func (m *Map) RandomTile(types []TileType) Position {
+ candidates := []Position{}
+ for y := 0; y < len(m.Tiles); y++ {
+ for x := 0; x < len(m.Tiles[y]); x++ {
+ candidate := m.Tiles[y][x]
+ for _, t := range types {
+ if candidate.Type == t {
+ candidates = append(candidates, candidate.Position)
+ break
+ }
+ }
+ }
+ }
+
+ r := rand.New(rand.NewSource(time.Now().UnixNano()))
+ randIdx := r.Intn(len(candidates))
+ return candidates[randIdx]
+}
+
+func (m *Map) RandomTileFromSymbols(symbols []string) (Position, error) {
+ types := make([]TileType, 0, len(symbols))
+ for _, symbol := range symbols {
+ tileString, ok := m.symbols[symbol]
+ if !ok {
+ return INVALID_POSITION(), errors.New(fmt.Sprintf("symbol %s not in the map symbols %v", symbol, m.symbols))
+ }
+
+ tileType, ok := TileNames[tileString]
+ if !ok {
+ return INVALID_POSITION(), errors.New("tile type unknown")
+ }
+
+ types = append(types, tileType)
+ }
+ return m.RandomTile(types), nil
+}