Line data Source code
1 : package overworld
2 :
3 : import (
4 : "fmt"
5 :
6 : "github.com/kijimaD/ruins/internal/consts"
7 : "github.com/kijimaD/ruins/internal/raw"
8 : "github.com/kijimaD/ruins/internal/rawschema"
9 : w "github.com/kijimaD/ruins/internal/world"
10 : "github.com/kijimaD/ruins/internal/world/lifecycle"
11 : )
12 :
13 : // relSpot は基準点からの相対座標に置くエンティティの定義。散布の起点を基準に、
14 : // prop をずらして配置するのに使う。
15 : type relSpot struct {
16 : name string
17 : dx consts.Tile
18 : dy consts.Tile
19 : }
20 :
21 : // settlementFeature は小集落の feature 実装。当選チャンクに村または一軒家を置く。
22 : type settlementFeature struct{}
23 :
24 : func (settlementFeature) place(world w.World, runSeed uint64, c consts.Coord[consts.Chunk], cols consts.Chunk, g chunkGeom) error {
25 : // 区域や墜落現場と重なった当選は譲る。地物の優先度は分類が一元管理する
26 1 : if chunkTypeAt(world.Resources.RawMaster, runSeed, c, cols, spawnRowOf(world)) != chunkSettlement {
27 1 : return nil
28 1 : }
29 1 : center := consts.Coord[consts.Tile]{X: g.offsetX + g.chunkW/2, Y: g.offsetY + g.chunkH/2}
30 1 : return spawnSettlement(world, center, settlementAt(world.Resources.RawMaster, runSeed, c))
31 : }
32 :
33 : // settlementAt は集落の種類を raw の settlements の行から重みの比で選ぶ。純粋に (runSeed, 座標) の
34 : // 関数で、地図の分類と生成の両方がこれを使うので種類の見た目と実体が一致する。
35 : // 行は読み込み時に1つ以上あり、重みは1以上であることを検証している。
36 : func settlementAt(raws rawschema.Raws, runSeed uint64, c consts.Coord[consts.Chunk]) rawschema.Settlement {
37 1 : rows := raw.PtrSlice(raws.Settlements)
38 1 : total := 0
39 1 : for i := range rows {
40 1 : total += int(rows[i].Weight)
41 1 : }
42 1 : roll := int(ChunkSeed2D(runSeed^placementOf(raws, rawschema.PointFeatureKindSettlement).Salt, c.X, c.Y) % uint64(total))
43 1 : for i := range rows {
44 1 : roll -= int(rows[i].Weight)
45 1 : if roll < 0 {
46 1 : return rows[i]
47 1 : }
48 : }
49 0 : return rows[len(rows)-1]
50 : }
51 :
52 : // spawnSettlement は小集落を構成する。center を集落の中心として、行の住人と生活感の prop を
53 : // 近傍へ決定的に配置する。住人と prop の座標は重ねない。
54 : // 集落はステージでなくオーバーワールドの地物なので、専用の State を持たず prop として常在する。
55 : // 帯への束縛は呼び出し元のチャンク生成が一括で行う。
56 : func spawnSettlement(world w.World, center consts.Coord[consts.Tile], s rawschema.Settlement) error {
57 1 : for _, n := range s.Members {
58 1 : pos := consts.Coord[consts.Tile]{X: center.X + consts.Tile(n.Dx), Y: center.Y + consts.Tile(n.Dy)}
59 1 : if _, err := lifecycle.SpawnNeutralNPC(world, pos, n.Name); err != nil {
60 0 : return fmt.Errorf("failed to place settlement NPC (%s): %w", n.Name, err)
61 0 : }
62 : }
63 1 : for _, p := range s.Props {
64 1 : if _, err := lifecycle.SpawnProp(world, p.Name, center.X+consts.Tile(p.Dx), center.Y+consts.Tile(p.Dy)); err != nil {
65 0 : return fmt.Errorf("failed to place settlement prop (%s): %w", p.Name, err)
66 0 : }
67 : }
68 1 : return nil
69 : }
|