Line data Source code
1 : package overworld
2 :
3 : import (
4 : "fmt"
5 : "math"
6 : "math/rand/v2"
7 :
8 : gc "github.com/kijimaD/ruins/internal/components"
9 : "github.com/kijimaD/ruins/internal/consts"
10 : "github.com/kijimaD/ruins/internal/raw"
11 : w "github.com/kijimaD/ruins/internal/world"
12 : "github.com/kijimaD/ruins/internal/world/lifecycle"
13 : )
14 :
15 : // 屋外の散布 loot は開けた地形へ拾える物をごくまばらに撒く。建物内の役割別 loot と違い、屋外は屑物だけを
16 : // 置く。屋外に武器・防具・回復薬や錬金素材のような価値ある品を撒くと拾い放題になりバランスを壊し、原野に
17 : // 装備が落ちている絵面も不自然なので、無価値な item group からだけ引く。道沿いは人の営みの残りの紙屑、奥地は
18 : // 打ち捨てられた廃材やくず鉄。密度も低くして原野を埋め尽くさない。土系の地面かつ非占有のタイルにだけ置く。
19 :
20 : // outdoorLootItemChannel はアイテム抽選を座標選択と無相関にするハッシュチャネル。scatterTiles のタイル選択
21 : // seed と別チャネルにし、どのタイルに何が出るかの相関を切る。scatter.go の grass/weed チャネルと同じ考え方。
22 : const outdoorLootItemChannel uint64 = 0x6c6f6f745f69746d // "loot_itm"
23 :
24 : // scatterOutdoorLoot は開けたチャンクの地表へ屑物をごく疎に撒く。候補は土系の地面かつ非占有で、accept と
25 : // occupied は草・樹木の散布と共有して重なりを避ける。group は散布ゾーン行の lootGroup から引き、
26 : // 抽選は建物 loot と同じ raw.SelectFromItemGroup で行い、深度は扱わない。乱数は
27 : // 散布とは別の salt から引き、地物や建物 loot の消費順と干渉させない。同一 seed で同一結果、再訪で一致する。
28 : func scatterOutdoorLoot(world w.World, runSeed uint64, c consts.Coord[consts.Chunk], g chunkGeom, cat scatterCatalog, accept func(consts.Coord[consts.Tile]) bool, occupied map[gc.GridElement]bool) error {
29 1 : groupID := cat.LootGroup
30 1 :
31 : // タイル選択の seed とアイテム抽選の rng を別チャネルにする。同一 seed だと「どこに置くか」と「何を置くか」が
32 : // 相関しうるので、アイテム側は専用チャネルを XOR して無相関にする
33 1 : selSeed := ChunkSeed2D(runSeed^outdoorLootSalt, c.X, c.Y)
34 1 : rng := rand.New(rand.NewPCG(selSeed^outdoorLootItemChannel, outdoorLootSalt))
35 1 : // 置くタイル数は散布ゾーンの密度から面積で導く。反復乱数でなく密度で決めるので純関数のまま
36 1 : count := int(math.Round(float64(g.chunkW) * float64(g.chunkH) * cat.LootDensity))
37 1 :
38 1 : for _, rel := range scatterTiles(g.chunkW, g.chunkH, accept, selSeed, count) {
39 1 : draws, err := raw.SelectFromItemGroup(world.Resources.RawMaster, groupID, rng)
40 1 : if err != nil {
41 0 : return err
42 0 : }
43 1 : if len(draws) == 0 {
44 1 : continue // 屑物 group は確率が低く、何も出ないタイルが多い。そのタイルは空けておく
45 : }
46 1 : bl := consts.Coord[consts.Tile]{X: g.offsetX + rel.X, Y: g.offsetY + rel.Y}
47 1 : for _, d := range draws {
48 1 : if d.Name == "" {
49 0 : continue
50 : }
51 1 : if _, err := lifecycle.SpawnFieldItem(world, d.Name, bl.X, bl.Y, d.Count); err != nil {
52 0 : return fmt.Errorf("failed to place outdoor loot (%s): %w", d.Name, err)
53 0 : }
54 : }
55 1 : occupied[gc.GridElement{Coord: bl}] = true
56 : }
57 1 : return nil
58 : }
|