Line data Source code
1 : package mapplanner
2 :
3 : import (
4 : gc "github.com/kijimaD/ruins/internal/components"
5 : "github.com/kijimaD/ruins/internal/consts"
6 : "github.com/kijimaD/ruins/internal/rawschema"
7 : )
8 :
9 : // BigRoomPlanner は大部屋を生成するビルダー
10 : // マップ全体の大部分を1つの部屋にする
11 : type BigRoomPlanner struct{}
12 :
13 : // PlanInitial は初期マップをプランする
14 : func (b BigRoomPlanner) PlanInitial(planData *MetaPlan) error {
15 1 : width := int(planData.Level.TileWidth)
16 1 : height := int(planData.Level.TileHeight)
17 1 :
18 : // マップの境界を考慮して大きな部屋を1つ作成
19 1 : room := gc.Rect{Min: consts.Coord[consts.Tile]{X: consts.Tile(0), Y: consts.Tile(0)}, Max: consts.Coord[consts.Tile]{X: consts.Tile(width - 1), Y: consts.Tile(height - 1)}}
20 1 :
21 : // 部屋をリストに追加
22 1 : planData.Rooms = append(planData.Rooms, room)
23 1 :
24 1 : return nil
25 1 : }
26 :
27 : // BigRoomDraw は大部屋を描画し、ランダムにバリエーションを適用するビルダー
28 : type BigRoomDraw struct {
29 : Row rawschema.BigRoomStep
30 : Tiles rawschema.LayoutTiles
31 : }
32 :
33 : // PlanMeta は大部屋をタイルに描画し、ランダムにバリエーションを適用する
34 : func (b BigRoomDraw) PlanMeta(planData *MetaPlan) error {
35 : // まず基本の大部屋を描画
36 1 : b.drawBasicBigRoom(planData)
37 1 :
38 : // ランダムにバリエーションを選択して適用
39 1 : variantType := planData.RNG.IntN(5)
40 1 :
41 1 : switch variantType {
42 1 : case 0:
43 : // 通常の大部屋(何も追加しない)
44 : case 1:
45 : // 柱を追加
46 1 : b.applyPillars(planData)
47 : case 2:
48 : // 障害物を追加
49 1 : b.applyObstacles(planData)
50 : case 3:
51 : // 迷路パターンを追加
52 1 : b.applyMazePattern(planData)
53 : case 4:
54 : // 中央台座を追加
55 1 : b.applyCenterPlatform(planData)
56 : }
57 1 : return nil
58 : }
59 :
60 : // drawBasicBigRoom は基本の大部屋を描画する。部屋の周りの通れないマスを壁にする
61 : func (b BigRoomDraw) drawBasicBigRoom(planData *MetaPlan) {
62 1 : for _, room := range planData.Rooms {
63 : // 部屋の内部を床タイルで埋める
64 1 : for x := room.Min.X; x <= room.Max.X; x++ {
65 1 : for y := room.Min.Y; y <= room.Max.Y; y++ {
66 1 : idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: x, Y: y})
67 1 : planData.Tiles[idx] = planData.GetTile(b.Tiles.Floor)
68 1 : }
69 : }
70 :
71 : // 部屋の境界を壁で囲む
72 1 : for y := room.Min.Y; y <= room.Max.Y; y++ {
73 : // 左辺
74 1 : if x := room.Min.X - 1; x >= 0 {
75 1 : idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: x, Y: y})
76 1 : if planData.Tiles[idx].BlockPass {
77 0 : planData.Tiles[idx] = planData.GetTile(b.Tiles.Wall)
78 0 : }
79 : }
80 : // 右辺
81 1 : if x := room.Max.X + 1; int(x) < int(planData.Level.TileWidth) {
82 1 : idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: x, Y: y})
83 1 : if planData.Tiles[idx].BlockPass {
84 1 : planData.Tiles[idx] = planData.GetTile(b.Tiles.Wall)
85 1 : }
86 : }
87 : }
88 : // 上辺と下辺
89 1 : for x := room.Min.X; x <= room.Max.X; x++ {
90 : // 上辺
91 1 : if y := room.Min.Y - 1; y >= 0 {
92 1 : idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: x, Y: y})
93 1 : if planData.Tiles[idx].BlockPass {
94 1 : planData.Tiles[idx] = planData.GetTile(b.Tiles.Wall)
95 1 : }
96 : }
97 : // 下辺
98 1 : if y := room.Max.Y + 1; int(y) < int(planData.Level.TileHeight) {
99 1 : idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: x, Y: y})
100 1 : if planData.Tiles[idx].BlockPass {
101 1 : planData.Tiles[idx] = planData.GetTile(b.Tiles.Wall)
102 1 : }
103 : }
104 : }
105 : }
106 : }
107 :
108 : // applyPillars は部屋に柱を追加する
109 : func (b BigRoomDraw) applyPillars(planData *MetaPlan) {
110 1 : spacing := b.Row.PillarSpacing.Roll(planData.RNG)
111 1 :
112 1 : for _, room := range planData.Rooms {
113 : // 柱の開始位置を計算(部屋の中心から対称に配置)
114 1 : startX := int(room.Min.X) + spacing
115 1 : startY := int(room.Min.Y) + spacing
116 1 :
117 : // 規則的に柱を配置
118 1 : for x := startX; x <= int(room.Max.X); x += spacing + 1 {
119 1 : for y := startY; y <= int(room.Max.Y); y += spacing + 1 {
120 1 : idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: consts.Tile(x), Y: consts.Tile(y)})
121 1 : planData.Tiles[idx] = planData.GetTile(b.Tiles.Wall)
122 1 : }
123 : }
124 : }
125 : }
126 :
127 : // applyObstacles は部屋にランダムな障害物を追加する
128 : func (b BigRoomDraw) applyObstacles(planData *MetaPlan) {
129 1 : for _, room := range planData.Rooms {
130 : // 障害物の数を部屋のサイズに基づいて決定
131 1 : roomWidth := int(room.Width())
132 1 : roomHeight := int(room.Height())
133 1 : obstacleCount := (roomWidth * roomHeight) / b.Row.ObstacleArea
134 1 :
135 1 : for range obstacleCount {
136 : // 部屋内のランダムな位置に障害物を配置する
137 : // IntNの引数が正であることを保証する
138 1 : maxXRange := max(1, roomWidth-2)
139 1 : maxYRange := max(1, roomHeight-2)
140 1 : x := int(room.Min.X) + 1 + planData.RNG.IntN(maxXRange)
141 1 : y := int(room.Min.Y) + 1 + planData.RNG.IntN(maxYRange)
142 1 :
143 1 : idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: consts.Tile(x), Y: consts.Tile(y)})
144 1 : planData.Tiles[idx] = planData.GetTile(b.Tiles.Wall)
145 1 : }
146 : }
147 : }
148 :
149 : // applyMazePattern は部屋に迷路パターンを追加する
150 : func (b BigRoomDraw) applyMazePattern(planData *MetaPlan) {
151 1 : for _, room := range planData.Rooms {
152 : // 格子状に壁を配置し、ランダムに開口部を作る
153 : // 上端行・下端行には壁を配置しない。上下端への接続性を保証するため
154 : // 縦の壁を部屋の右端から逆向きに配置
155 1 : for x := int(room.Max.X); x >= int(room.Min.X)+2; x -= 3 {
156 1 : for y := int(room.Min.Y) + 1; y <= int(room.Max.Y)-1; y++ {
157 : // 縦の壁を配置(ランダムに開口部を作る)
158 1 : if planData.RNG.Float64() > b.Row.MazeGapChance {
159 1 : idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: consts.Tile(x), Y: consts.Tile(y)})
160 1 : planData.Tiles[idx] = planData.GetTile(b.Tiles.Wall)
161 1 : }
162 : }
163 : }
164 :
165 : // 横の壁を部屋の下端から逆向きに配置
166 1 : for y := int(room.Max.Y) - 1; y >= int(room.Min.Y)+2; y -= 3 {
167 1 : for x := int(room.Min.X); x <= int(room.Max.X); x++ {
168 : // 横の壁を配置(ランダムに開口部を作る)
169 1 : if planData.RNG.Float64() > b.Row.MazeGapChance {
170 1 : idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: consts.Tile(x), Y: consts.Tile(y)})
171 1 : planData.Tiles[idx] = planData.GetTile(b.Tiles.Wall)
172 1 : }
173 : }
174 : }
175 : }
176 : }
177 :
178 : // applyCenterPlatform は部屋に中央台座を追加する
179 : func (b BigRoomDraw) applyCenterPlatform(planData *MetaPlan) {
180 1 : for _, room := range planData.Rooms {
181 1 : centerX := int(room.Min.X+room.Max.X) / 2
182 1 : centerY := int(room.Min.Y+room.Max.Y) / 2
183 1 :
184 : // 台座のサイズを部屋のサイズに基づいて決定
185 1 : platformSize := b.Row.PlatformRadius.Roll(planData.RNG)
186 1 :
187 : // 円形の台座を作成
188 1 : for dx := -platformSize; dx <= platformSize; dx++ {
189 1 : for dy := -platformSize; dy <= platformSize; dy++ {
190 1 : distance := dx*dx + dy*dy
191 1 : if distance <= platformSize*platformSize {
192 1 : x := centerX + dx
193 1 : y := centerY + dy
194 1 : if x >= int(room.Min.X) && x <= int(room.Max.X) &&
195 1 : y >= int(room.Min.Y) && y <= int(room.Max.Y) {
196 1 : idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: consts.Tile(x), Y: consts.Tile(y)})
197 1 : // 外周は壁、内部は床のまま
198 1 : if distance >= (platformSize-1)*(platformSize-1) {
199 1 : planData.Tiles[idx] = planData.GetTile(b.Tiles.Wall)
200 1 : }
201 : }
202 : }
203 : }
204 : }
205 : }
206 : }
|