Line data Source code
1 : package overworld
2 :
3 : import (
4 : "cmp"
5 : "slices"
6 :
7 : "github.com/kijimaD/ruins/internal/consts"
8 : "github.com/kijimaD/ruins/internal/raw"
9 : "github.com/kijimaD/ruins/internal/rawschema"
10 : )
11 :
12 : // 道の幾何の唯一の出典。道が「隣接リージョンの当選集落を L 字で結び、塀に囲まれた区域は門を通る」という規則をここ一箇所に置き、
13 : // 舗装 road.go・散布回避 scatter.go・マクロ地図 road_overlay.go の3消費者が同じ分解を各粒度で解釈する。
14 : // 分解を1箇所にすることで、地図に出る道と実際に舗装される道が構造的に一致し続ける。
15 :
16 : // roadOrient は道の一辺の向き。実体は文字列で、%v やテスト失敗の出力に種別名が出る。
17 : type roadOrient string
18 :
19 : const (
20 : orientHorizontal roadOrient = "horizontal" // 行を固定して列方向へ伸びる
21 : orientVertical roadOrient = "vertical" // 列を固定して行方向へ伸びる
22 : )
23 :
24 : // roadSeg は道の一辺。座標は絶対タイルで、fixed は道の中心線。
25 : type roadSeg struct {
26 : orient roadOrient
27 : fixed consts.Tile // orient=horizontal なら固定する Y、vertical なら固定する X
28 : lo, hi consts.Tile // 可変軸の範囲。lo<=hi で端点を含む
29 : }
30 :
31 : // span は lo と hi を小さい順に並べた辺を作る
32 : func span(orient roadOrient, fixed, a, b consts.Tile) roadSeg {
33 1 : return roadSeg{orient: orient, fixed: fixed, lo: min(a, b), hi: max(a, b)}
34 1 : }
35 :
36 : // chunkCenter はチャンク c の中心のタイルを返す
37 : func chunkCenter(c consts.Chunk, size consts.Tile) consts.Tile {
38 1 : return c.Tiles(size) + size/2
39 1 : }
40 :
41 : // roadSegments は当選集落 a から b への道を水平辺・垂直辺の2区間で返す。分解規則の唯一の出典で、
42 : // 水平辺は始点 a の行、垂直辺は終点 b の列、角は (b.X, a.Y)。各辺はチャンクの中心を通る。端点は min/max で
43 : // 正規化するので、a と b の東西の大小に依らず同じ2辺になる。
44 : func roadSegments(a, b consts.Coord[consts.Chunk], chunkW, chunkH consts.Tile) []roadSeg {
45 1 : return []roadSeg{
46 1 : span(orientHorizontal, chunkCenter(a.Y, chunkH), chunkCenter(a.X, chunkW), chunkCenter(b.X, chunkW)),
47 1 : span(orientVertical, chunkCenter(b.X, chunkW), chunkCenter(a.Y, chunkH), chunkCenter(b.Y, chunkH)),
48 1 : }
49 : }
50 :
51 : // roadPath は当選集落 a から b への道を返す。roadSegments の垂直辺が塀に囲まれた区域を横切るなら、区域の手前の行で
52 : // 北の門の列へ折れて門へ入り、南の門から出て区域の向こうの行で元の列へ戻る。区域の中は区域が街路を敷くので辺を置かない。
53 : // 門は区域の外周の街路の帯に開くので、門の列では帯の幅の真ん中を通す
54 : func roadPath(raws rawschema.Raws, runSeed uint64, a, b consts.Coord[consts.Chunk], cols, spawnRow consts.Chunk, chunkW, chunkH consts.Tile) []roadSeg {
55 1 : segs := roadSegments(a, b, chunkW, chunkH)
56 1 : vertical := segs[1]
57 1 : x, y := vertical.fixed, vertical.lo
58 1 : out := []roadSeg{segs[0]}
59 1 : for _, r := range fencedDistrictsBetween(raws, runSeed, min(a.Y, b.Y), max(a.Y, b.Y), cols, spawnRow) {
60 1 : north, south := gateColumns(r, lotsOf(raws, zoneCatalogFrom(raws), r, spawnRow))
61 1 : top := r.anchor.Y.Tiles(chunkH)
62 1 : bottom := (r.anchor.Y + r.h).Tiles(chunkH) - 1
63 2 : lane := func(col consts.Chunk) consts.Tile { return col.Tiles(chunkW) + urbanStreetW/2 }
64 1 : if y < top {
65 1 : before := chunkCenter(r.anchor.Y-1, chunkH)
66 1 : out = append(out,
67 1 : span(orientVertical, x, y, before),
68 1 : span(orientHorizontal, before, x, lane(north)),
69 1 : span(orientVertical, lane(north), before, top),
70 1 : )
71 1 : }
72 1 : if vertical.hi <= bottom {
73 1 : return out
74 1 : }
75 1 : after := chunkCenter(r.anchor.Y+r.h, chunkH)
76 1 : out = append(out,
77 1 : span(orientVertical, lane(south), bottom, after),
78 1 : span(orientHorizontal, after, lane(south), x),
79 1 : )
80 1 : y = after
81 : }
82 1 : return append(out, span(orientVertical, x, y, vertical.hi))
83 : }
84 :
85 : // fencedDistrictsBetween は絶対チャンク行 top から bottom に掛かる、塀に囲まれた区域を北から順に返す
86 : func fencedDistrictsBetween(raws rawschema.Raws, runSeed uint64, top, bottom, cols, spawnRow consts.Chunk) []districtRegion {
87 1 : var out []districtRegion
88 1 : for _, d := range districtsFrom(raws) {
89 1 : if d.row.Fence == nil {
90 1 : continue
91 : }
92 1 : spacing := consts.Chunk(d.row.Spacing)
93 1 : for ry := consts.FloorDiv(top, spacing); ry <= consts.FloorDiv(bottom, spacing); ry++ {
94 1 : r := d.regionIn(runSeed, ry, cols)
95 1 : if r.anchor.Y > bottom || r.anchor.Y+r.h-1 < top {
96 1 : continue
97 : }
98 1 : if got, ok := districtAt(raws, runSeed, r.anchor, cols, spawnRow); ok && got == r {
99 1 : out = append(out, r)
100 1 : }
101 : }
102 : }
103 2 : slices.SortFunc(out, func(a, b districtRegion) int { return cmp.Compare(a.anchor.Y, b.anchor.Y) })
104 1 : return out
105 : }
106 :
107 : // roadLink は道で結ぶ2つの当選と、その道
108 : type roadLink struct {
109 : a, b consts.Coord[consts.Chunk]
110 : road rawschema.Road
111 : }
112 :
113 : // crossingRoads はチャンク c を横切りうる道を返す。道を持つ地物ごとに、c の行が属するリージョン r の
114 : // (r-1,r) と (r,r+1) の当選を結ぶ2本だけが c を横切りうる。road.go の舗装と scatter.go の回避が同じ結線を共有する
115 : func crossingRoads(raws rawschema.Raws, runSeed uint64, c consts.Coord[consts.Chunk], cols consts.Chunk) []roadLink {
116 1 : var links []roadLink
117 1 : for _, f := range raw.PtrSlice(raws.PointFeatures) {
118 1 : if f.Road == nil {
119 1 : continue
120 : }
121 1 : p := placementFrom(f)
122 1 : r := consts.FloorDiv(c.Y, p.Spacing)
123 1 : for pr := r - 1; pr <= r; pr++ {
124 1 : links = append(links, roadLink{a: p.WinnerOf(runSeed, pr, cols), b: p.WinnerOf(runSeed, pr+1, cols), road: *f.Road})
125 1 : }
126 : }
127 1 : return links
128 : }
|