Line data Source code
1 : package mapplanner
2 :
3 : import (
4 : "math"
5 :
6 : gc "github.com/kijimaD/ruins/internal/components"
7 : "github.com/kijimaD/ruins/internal/consts"
8 : "github.com/kijimaD/ruins/internal/geometry"
9 : "github.com/kijimaD/ruins/internal/rawschema"
10 : )
11 :
12 : // ForestPlanner は森風レイアウトを生成するビルダー
13 : // 木々が点在し、自然な通路を持つ森を作成
14 : type ForestPlanner struct {
15 : Row rawschema.ClearingStart
16 : }
17 :
18 : // PlanInitial は初期森マップをプランする
19 : func (f ForestPlanner) PlanInitial(planData *MetaPlan) error {
20 1 : width := int(planData.Level.TileWidth)
21 1 : height := int(planData.Level.TileHeight)
22 1 :
23 : // 森の中に小さな空き地(部屋)をいくつか作成
24 1 : clearingCount := f.Row.Clearings.Roll(planData.RNG)
25 1 :
26 1 : for range clearingCount {
27 : // 空き地のサイズとランダムな位置
28 1 : clearingWidth := f.Row.ClearingSize.Roll(planData.RNG)
29 1 : clearingHeight := f.Row.ClearingSize.Roll(planData.RNG)
30 1 :
31 : // IntNの引数が正であることを保証
32 1 : maxXRange := max(1, width-clearingWidth-6)
33 1 : maxYRange := max(1, height-clearingHeight-6)
34 1 : x := 3 + planData.RNG.IntN(maxXRange)
35 1 : y := 3 + planData.RNG.IntN(maxYRange)
36 1 :
37 : // 円形に近い空き地を作成
38 1 : centerX := x + clearingWidth/2
39 1 : centerY := y + clearingHeight/2
40 1 : radius := math.Min(float64(clearingWidth), float64(clearingHeight)) / 2.0
41 1 :
42 1 : clearing := gc.Rect{Min: consts.Coord[consts.Tile]{X: consts.Tile(centerX - int(radius)), Y: consts.Tile(centerY - int(radius))}, Max: consts.Coord[consts.Tile]{X: consts.Tile(centerX + int(radius)), Y: consts.Tile(centerY + int(radius))}}
43 1 :
44 1 : planData.Rooms = append(planData.Rooms, clearing)
45 1 : }
46 1 : return nil
47 : }
48 :
49 : // ForestTerrain は森の基本地形を生成する
50 : type ForestTerrain struct {
51 : Tiles rawschema.LayoutTiles
52 : }
53 :
54 : // PlanMeta は森の基本地形をタイルに描画する
55 : func (f ForestTerrain) PlanMeta(planData *MetaPlan) error {
56 1 : for i := range planData.Tiles {
57 1 : planData.Tiles[i] = planData.GetTile(f.Tiles.Floor)
58 1 : }
59 :
60 1 : for _, clearing := range planData.Rooms {
61 1 : f.createCircularClearing(planData, clearing)
62 1 : }
63 1 : return nil
64 : }
65 :
66 : // createCircularClearing は円形の空き地を作成する
67 : func (f ForestTerrain) createCircularClearing(planData *MetaPlan, clearing gc.Rect) {
68 1 : centerX := float64(clearing.Min.X+clearing.Max.X) / 2.0
69 1 : centerY := float64(clearing.Min.Y+clearing.Max.Y) / 2.0
70 1 : radius := math.Min(float64(clearing.Max.X-clearing.Min.X), float64(clearing.Max.Y-clearing.Min.Y)) / 2.0
71 1 :
72 1 : for x := clearing.Min.X - 1; x <= clearing.Max.X+1; x++ {
73 1 : for y := clearing.Min.Y - 1; y <= clearing.Max.Y+1; y++ {
74 1 : distance := geometry.Distance(float64(x), float64(y), centerX, centerY)
75 1 :
76 1 : if distance <= radius {
77 1 : idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: x, Y: y})
78 1 : planData.Tiles[idx] = planData.GetTile(f.Tiles.Floor)
79 1 : }
80 : }
81 : }
82 : }
83 :
84 : // ForestTrees は森に木々を配置する
85 : type ForestTrees struct {
86 : Row rawschema.TreeStep
87 : Tiles rawschema.LayoutTiles
88 : }
89 :
90 : // PlanMeta は森に木を配置する
91 : func (f ForestTrees) PlanMeta(planData *MetaPlan) error {
92 1 : width := int(planData.Level.TileWidth)
93 1 : height := int(planData.Level.TileHeight)
94 1 :
95 1 : for x := 1; x < width-1; x++ {
96 1 : for y := 1; y < height-1; y++ {
97 1 : idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: consts.Tile(x), Y: consts.Tile(y)})
98 1 :
99 1 : if !planData.Tiles[idx].BlockPass {
100 : // 空き地の近くでは木の密度を下げる
101 1 : treeDensity := f.calculateTreeDensity(planData, x, y)
102 1 :
103 1 : if planData.RNG.Float64() < treeDensity {
104 : // TODO: 木エンティティとして追加する
105 1 : planData.Tiles[idx] = planData.GetTile(f.Tiles.Wall)
106 1 :
107 : // 大きな木の場合、周囲にも追加の木を配置
108 1 : if planData.RNG.Float64() < f.Row.LargeTreeChance {
109 1 : f.placeLargeTree(planData, x, y)
110 1 : }
111 : }
112 : }
113 : }
114 : }
115 1 : return nil
116 : }
117 :
118 : // calculateTreeDensity は位置に基づいて木の密度を計算する
119 : func (f ForestTrees) calculateTreeDensity(planData *MetaPlan, x, y int) float64 {
120 1 : baseDensity := f.Row.Density
121 1 :
122 : // 空き地からの距離に基づいて密度を調整
123 1 : minDistanceToClearing := 1000.0
124 1 :
125 1 : for _, clearing := range planData.Rooms {
126 1 : centerX := float64(clearing.Min.X+clearing.Max.X) / 2.0
127 1 : centerY := float64(clearing.Min.Y+clearing.Max.Y) / 2.0
128 1 :
129 1 : distance := geometry.Distance(float64(x), float64(y), centerX, centerY)
130 1 :
131 1 : if distance < minDistanceToClearing {
132 1 : minDistanceToClearing = distance
133 1 : }
134 : }
135 :
136 : // 空き地に近いほど木の密度を下げる
137 1 : if minDistanceToClearing < 5 {
138 1 : return baseDensity * 0.3 // 空き地の近くは30%
139 2 : } else if minDistanceToClearing < 10 {
140 1 : return baseDensity * 0.7 // 中距離は70%
141 1 : }
142 1 : return baseDensity // 遠くは基本密度
143 : }
144 :
145 : // placeLargeTree は大きな木を配置する
146 : func (f ForestTrees) placeLargeTree(planData *MetaPlan, centerX, centerY int) {
147 1 : width := int(planData.Level.TileWidth)
148 1 : height := int(planData.Level.TileHeight)
149 1 :
150 : // 大木の周囲2x2または3x3エリアに追加の木を配置
151 1 : size := 1 + planData.RNG.IntN(2) // 1または2
152 1 :
153 1 : for dx := -size; dx <= size; dx++ {
154 1 : for dy := -size; dy <= size; dy++ {
155 1 : x, y := centerX+dx, centerY+dy
156 1 :
157 1 : if x >= 0 && x < width && y >= 0 && y < height {
158 1 : idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: consts.Tile(x), Y: consts.Tile(y)})
159 1 :
160 1 : if !planData.Tiles[idx].BlockPass && planData.RNG.Float64() < 0.7 {
161 1 : planData.Tiles[idx] = planData.GetTile(f.Tiles.Wall)
162 1 : }
163 : }
164 : }
165 : }
166 : }
167 :
168 : // ForestPaths は森の中に自然な通路を作成する
169 : type ForestPaths struct {
170 : Row rawschema.TrailStep
171 : }
172 :
173 : // PlanMeta は自然な通路を作成する
174 : func (f ForestPaths) PlanMeta(planData *MetaPlan) error {
175 1 : width := int(planData.Level.TileWidth)
176 1 : height := int(planData.Level.TileHeight)
177 1 :
178 : // 最上列から最下列への確実な通路を1本作成(接続性保証)
179 1 : f.createGuaranteedVerticalPath(planData, width, height)
180 1 :
181 1 : if len(planData.Rooms) >= 2 {
182 : // 各空き地を他の空き地と繋ぐ
183 1 : for i := range len(planData.Rooms) {
184 1 : for j := i + 1; j < len(planData.Rooms); j++ {
185 : // 距離が近い空き地のみ繋ぐ(50%の確率)
186 1 : if f.shouldCreatePath(planData, planData.Rooms[i], planData.Rooms[j]) {
187 1 : f.createNaturalPath(planData, planData.Rooms[i], planData.Rooms[j])
188 1 : }
189 : }
190 : }
191 : }
192 1 : return nil
193 : }
194 :
195 : // createGuaranteedVerticalPath は最上列から最下列への確実な通路を作成する
196 : func (f ForestPaths) createGuaranteedVerticalPath(planData *MetaPlan, width, height int) {
197 : // ランダムなX位置を選択(中央付近を優先)
198 1 : centerX := width / 2
199 1 : offsetRange := max(width/4, 1)
200 1 : pathX := max(
201 1 : // 境界チェック
202 1 : centerX+planData.RNG.IntN(offsetRange*2+1)-offsetRange, 1)
203 1 : if pathX >= width-1 {
204 0 : pathX = width - 2
205 0 : }
206 :
207 : // 上から下まで確実に通路を作る
208 1 : for y := range height {
209 : // 通路とその左右1タイルを道にし、幅3の通路にする
210 1 : for dx := -1; dx <= 1; dx++ {
211 1 : x := pathX + dx
212 1 : if x >= 0 && x < width {
213 1 : idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: consts.Tile(x), Y: consts.Tile(y)})
214 1 : planData.Tiles[idx] = planData.GetTile(f.Row.Tile)
215 1 : }
216 : }
217 :
218 : // 通路に自然な蛇行を追加(20%の確率で左右に移動)
219 1 : if y > 0 && y < height-1 && planData.RNG.Float64() < 0.2 {
220 1 : if planData.RNG.Float64() < 0.5 {
221 1 : pathX = min(pathX+1, width-2)
222 1 : } else {
223 1 : pathX = max(pathX-1, 1)
224 1 : }
225 : }
226 : }
227 : }
228 :
229 : // shouldCreatePath は通路を作成するかどうかを判定する
230 : func (f ForestPaths) shouldCreatePath(planData *MetaPlan, room1, room2 gc.Rect) bool {
231 : // 空き地間の距離を計算
232 1 : center1X := float64(room1.Min.X+room1.Max.X) / 2.0
233 1 : center1Y := float64(room1.Min.Y+room1.Max.Y) / 2.0
234 1 : center2X := float64(room2.Min.X+room2.Max.X) / 2.0
235 1 : center2Y := float64(room2.Min.Y+room2.Max.Y) / 2.0
236 1 :
237 1 : distance := geometry.Distance(center1X, center1Y, center2X, center2Y)
238 1 :
239 : // 近い空き地ほど繋がりやすい
240 1 : if distance < 15 {
241 1 : return planData.RNG.Float64() < 0.8
242 2 : } else if distance < 25 {
243 1 : return planData.RNG.Float64() < 0.4
244 1 : }
245 1 : return planData.RNG.Float64() < 0.1
246 : }
247 :
248 : // createNaturalPath は自然な曲線状の通路を作成する
249 : func (f ForestPaths) createNaturalPath(planData *MetaPlan, room1, room2 gc.Rect) {
250 1 : width := int(planData.Level.TileWidth)
251 1 : height := int(planData.Level.TileHeight)
252 1 :
253 1 : center1X := int(room1.Min.X+room1.Max.X) / 2
254 1 : center1Y := int(room1.Min.Y+room1.Max.Y) / 2
255 1 : center2X := int(room2.Min.X+room2.Max.X) / 2
256 1 : center2Y := int(room2.Min.Y+room2.Max.Y) / 2
257 1 :
258 : // ベジェ曲線風の自然な通路を作成
259 1 : steps := 50
260 1 : for i := 0; i <= steps; i++ {
261 1 : t := float64(i) / float64(steps)
262 1 :
263 : // 制御点を追加してカーブを作る
264 1 : midX := (center1X + center2X) / 2
265 1 : midY := (center1Y + center2Y) / 2
266 1 :
267 : // ランダムな偏向を追加
268 1 : randomOffsetX := int(float64(planData.RNG.IntN(11)-5) * (1.0 - math.Abs(t-0.5)*2))
269 1 : randomOffsetY := int(float64(planData.RNG.IntN(11)-5) * (1.0 - math.Abs(t-0.5)*2))
270 1 :
271 : // 2次ベジェ曲線の近似
272 1 : x := int((1-t)*(1-t)*float64(center1X) + 2*(1-t)*t*float64(midX+randomOffsetX) + t*t*float64(center2X))
273 1 : y := int((1-t)*(1-t)*float64(center1Y) + 2*(1-t)*t*float64(midY+randomOffsetY) + t*t*float64(center2Y))
274 1 :
275 : // 通路を作成(少し幅を持たせる)
276 1 : for dx := -1; dx <= 1; dx++ {
277 1 : for dy := -1; dy <= 1; dy++ {
278 1 : nx, ny := x+dx, y+dy
279 1 : if nx >= 0 && nx < width && ny >= 0 && ny < height {
280 1 : idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: consts.Tile(nx), Y: consts.Tile(ny)})
281 1 :
282 : // 70%の確率で通路を作成(自然な感じに)
283 1 : if planData.RNG.Float64() < 0.7 {
284 1 : planData.Tiles[idx] = planData.GetTile(f.Row.Tile)
285 1 : }
286 : }
287 : }
288 : }
289 : }
290 : }
291 :
292 : // ForestWildlife は森に野生動物の痕跡(小さな空き地)を追加する
293 : type ForestWildlife struct {
294 : Row rawschema.GladeStep
295 : Tiles rawschema.LayoutTiles
296 : }
297 :
298 : // PlanMeta は森に小さな動物の痕跡を追加する
299 : func (f ForestWildlife) PlanMeta(planData *MetaPlan) error {
300 1 : width := int(planData.Level.TileWidth)
301 1 : height := int(planData.Level.TileHeight)
302 1 :
303 : // 小さな動物の通り道や巣穴を作成
304 1 : wildlifeSpotCount := f.Row.Spots.Roll(planData.RNG)
305 1 :
306 1 : for range wildlifeSpotCount {
307 : // IntNの引数が正であることを保証
308 1 : maxXRange := max(1, width-4)
309 1 : maxYRange := max(1, height-4)
310 1 : x := 2 + planData.RNG.IntN(maxXRange)
311 1 : y := 2 + planData.RNG.IntN(maxYRange)
312 1 :
313 : // 小さな円形の空き地を作成
314 1 : radius := 1 + planData.RNG.IntN(2)
315 1 :
316 1 : for dx := -radius; dx <= radius; dx++ {
317 1 : for dy := -radius; dy <= radius; dy++ {
318 1 : nx, ny := x+dx, y+dy
319 1 : // 上端・下端行には描画しない(ForestPathsで保証した接続性を壊さないため)
320 1 : if nx >= 0 && nx < width && ny > 0 && ny < height-1 {
321 1 : distance := geometry.Distance(float64(nx), float64(ny), float64(x), float64(y))
322 1 : if distance <= float64(radius) {
323 1 : idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: consts.Tile(nx), Y: consts.Tile(ny)})
324 1 : planData.Tiles[idx] = planData.GetTile(f.Tiles.Floor)
325 1 : }
326 : }
327 : }
328 : }
329 : }
330 1 : return nil
331 : }
|