aboutsummaryrefslogtreecommitdiff
path: root/go/game
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2022-12-28 16:17:06 +0100
committerFlorian Fischer <florian.fischer@muhq.space>2025-01-27 16:43:43 +0100
commit41d16fb4910fc0402b6b081302c953f7dabc8cf9 (patch)
tree334507bffb28b11760907e04c95602b9fa29efb8 /go/game
parent7f7389422834f628c9e45278e90fab6911173b25 (diff)
downloadmuhqs-game-41d16fb4910fc0402b6b081302c953f7dabc8cf9.tar.gz
muhqs-game-41d16fb4910fc0402b6b081302c953f7dabc8cf9.zip
add random tile selection from map to the web tools
Diffstat (limited to 'go/game')
-rw-r--r--go/game/map.go75
-rw-r--r--go/game/map_test.go10
2 files changed, 69 insertions, 16 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
+}
diff --git a/go/game/map_test.go b/go/game/map_test.go
index 732c8b86..8212f648 100644
--- a/go/game/map_test.go
+++ b/go/game/map_test.go
@@ -30,7 +30,7 @@ kings:
- (9,1)`
r := strings.NewReader(s)
- m, err := parseMap(r)
+ m, err := readMap(r)
if err != nil {
t.Fatalf("Parsing failed with: %v", err)
}
@@ -51,3 +51,11 @@ kings:
t.Fatalf("ressource gain is not the default one")
}
}
+
+func TestLoadingAllMaps(T *testing.T) {
+ maps := []string{"the-tyrant", "2P-ring-street", "2P-river-king", "3P-ring-street", "4P-islands", "the-kraken"}
+
+ for _, m := range maps {
+ LoadMap("../../maps/" + m + ".yml")
+ }
+}