Line data Source code
1 : package overworld
2 :
3 : import (
4 : "fmt"
5 :
6 : gc "github.com/kijimaD/ruins/internal/components"
7 : "github.com/kijimaD/ruins/internal/consts"
8 : "github.com/kijimaD/ruins/internal/rawschema"
9 : w "github.com/kijimaD/ruins/internal/world"
10 : "github.com/mlange-42/ark/ecs"
11 : )
12 :
13 : // 道は隣接リージョンの小集落どうしを結ぶ舗装路。
14 : // 集落の位置は Placement.WinnerOf で生成せずに算出できるため、各チャンクは自分を
15 : // 横切る区間だけを独立に描ける。経路は西の集落から東の集落への L 字で、端点が
16 : // リージョン順に正規化されているので、どちらのチャンクから生成しても同じ道になる。
17 : type roadFeature struct{}
18 :
19 : func (roadFeature) place(world w.World, runSeed uint64, c consts.Coord[consts.Chunk], cols consts.Chunk, g chunkGeom) error {
20 : // 区域の中は区域が街路を敷き、墜落現場の中は絵のとおりに残すので、道を描かない
21 1 : if kind := chunkTypeAt(world.Resources.RawMaster, runSeed, c, cols, spawnRowOf(world)); kind == chunkUrban || kind == chunkCrashSite {
22 1 : return nil
23 1 : }
24 1 : tiles := g.tiles.get()
25 1 : for _, link := range crossingRoads(world.Resources.RawMaster, runSeed, c, cols) {
26 1 : segs := roadPath(world.Resources.RawMaster, runSeed, link.a, link.b, cols, spawnRowOf(world), g.chunkW, g.chunkH)
27 1 : if err := drawRoadSegments(world, tiles, segs, link.road, c, g); err != nil {
28 0 : return err
29 0 : }
30 : }
31 1 : return nil
32 : }
33 :
34 : // drawRoadSegments は道の辺 segs のうち、チャンク c に含まれるマスだけを道 road のタイルで舗装する。既存タイルが土のマスだけを
35 : // 置き換え、市街地の壁や床、集落は壊さない。幅は中心線に対し進行方向と垂直へ広げ、偶数なら中心からわずかに片側へ寄せる
36 : func drawRoadSegments(world w.World, tiles map[gc.GridElement]ecs.Entity, segs []roadSeg, road rawschema.Road, c consts.Coord[consts.Chunk], g chunkGeom) error {
37 1 : earth := earthTilesOf(world.Resources.RawMaster)
38 1 : pave := func(px, py consts.Tile) error {
39 1 : loX := c.X.Tiles(g.chunkW)
40 1 : loY := c.Y.Tiles(g.chunkH)
41 1 : if px < loX || px >= loX+g.chunkW || py < loY || py >= loY+g.chunkH {
42 1 : return nil
43 1 : }
44 1 : wx := g.offsetX + (px - loX)
45 1 : wy := g.offsetY + (py - loY)
46 1 : if err := paveEarthTile(world, tiles, earth, consts.Coord[consts.Tile]{X: wx, Y: wy}, road.Tile); err != nil {
47 0 : return fmt.Errorf("failed to pave road (x=%d, y=%d): %w", wx, wy, err)
48 0 : }
49 1 : return nil
50 : }
51 :
52 : // 各辺を道の幅のバンドで敷く。進行軸に沿って可変軸を進め、垂直に幅ぶん広げる。
53 : // 角付近は両辺のバンドが重なるが paveEarthTile は冪等なので二重舗装は無害。
54 1 : width := consts.Tile(road.Width)
55 1 : for _, seg := range segs {
56 1 : for v := seg.lo; v <= seg.hi; v++ {
57 1 : for w := range width {
58 1 : off := w - width/2
59 1 : px, py := v, seg.fixed+off
60 1 : if seg.orient == orientVertical {
61 1 : px, py = seg.fixed+off, v
62 1 : }
63 1 : if err := pave(px, py); err != nil {
64 0 : return err
65 0 : }
66 : }
67 : }
68 : }
69 1 : return nil
70 : }
71 :
72 : // paveEarthTile は座標のタイルが土の地面のときだけ道のタイル tile へ置き換える。土でないタイルは他の地物の
73 : // 生成物なので触らない。
74 : func paveEarthTile(world w.World, tiles map[gc.GridElement]ecs.Entity, earth earthTiles, pos consts.Coord[consts.Tile], tile string) error {
75 1 : if !earth.at(world, tiles, pos) {
76 1 : return nil
77 1 : }
78 1 : _, err := replaceTile(world, tiles, pos, tile)
79 1 : return err
80 : }
|