Line data Source code
1 : package overworld
2 :
3 : import (
4 : "fmt"
5 : "math/rand/v2"
6 :
7 : "github.com/kijimaD/ruins/internal/consts"
8 : "github.com/kijimaD/ruins/internal/raw"
9 : w "github.com/kijimaD/ruins/internal/world"
10 : "github.com/kijimaD/ruins/internal/world/lifecycle"
11 : "github.com/kijimaD/ruins/internal/world/query"
12 : "github.com/mlange-42/ark/ecs"
13 : )
14 :
15 : // spawnFieldLoot は item group から1山を引き、pos の床へ置く。地上なので深度は扱わない
16 : func spawnFieldLoot(world w.World, groupID string, pos consts.Coord[consts.Tile], rng *rand.Rand) error {
17 1 : draws, err := raw.SelectFromItemGroup(world.Resources.RawMaster, groupID, rng)
18 1 : if err != nil {
19 1 : return err
20 1 : }
21 1 : for _, d := range draws {
22 1 : if d.Name == "" {
23 0 : continue
24 : }
25 1 : if _, err := lifecycle.SpawnFieldItem(world, d.Name, pos.X, pos.Y, d.Count); err != nil {
26 0 : return err
27 0 : }
28 : }
29 1 : return nil
30 : }
31 :
32 : // spawnFacilityBeing は施設 fac の敵の表から危険度で1体を引き、建物の中の pos へ置く。危険度帯に該当が無ければ置かない。
33 : func spawnFacilityBeing(world w.World, fac string, danger consts.Danger, pos consts.Coord[consts.Tile], rng *rand.Rand) error {
34 1 : table, err := urbanEnemyTableFor(world.Resources.RawMaster, fac)
35 1 : if err != nil {
36 1 : return err
37 1 : }
38 1 : name, err := raw.SelectEnemyByWeight(table, rng, int(danger))
39 1 : if err != nil {
40 0 : return err
41 0 : }
42 1 : if name == "" {
43 0 : return nil
44 0 : }
45 1 : _, err = lifecycle.SpawnEnemy(world, pos, name)
46 1 : return err
47 : }
48 :
49 : // populateStorageLoot は収納家具に、パレットが書いた戦利品表 tableID から件数ぶん引いて入れる。施設 fac の
50 : // 荒らされ率で空のまま残す。収納家具なのに表が無ければ error を返す。
51 : func populateStorageLoot(world w.World, entity ecs.Entity, propName, tableID, fac string, rng *rand.Rand) error {
52 1 : propRaw, err := raw.GetProp(world.Resources.RawMaster, propName)
53 1 : if err != nil {
54 0 : return err
55 0 : }
56 1 : if propRaw.Storage == nil {
57 1 : return nil
58 1 : }
59 1 : if tableID == "" {
60 1 : return fmt.Errorf("storage furniture %q has no loot table in the room definition", propName)
61 1 : }
62 1 : looted, err := raw.FacilityLootedChance(world.Resources.RawMaster, fac)
63 1 : if err != nil {
64 1 : return err
65 1 : }
66 1 : if rng.IntN(100) < looted {
67 1 : return nil
68 1 : }
69 1 : itemTable, err := raw.GetItemTable(world.Resources.RawMaster, tableID)
70 1 : if err != nil {
71 0 : return err
72 0 : }
73 1 : lootDice := consts.Dice{Base: 1, Sides: 1}
74 1 : if propRaw.Storage.LootCount != nil {
75 1 : d, err := consts.ParseDice(*propRaw.Storage.LootCount)
76 1 : if err != nil {
77 0 : return fmt.Errorf("invalid lootCount notation for storage '%s': %w", propName, err)
78 0 : }
79 1 : lootDice = d
80 : }
81 1 : danger := query.DangerLevelAt(world)
82 1 : if sb := query.GetSeamlessBand(world); sb != nil {
83 1 : depth := consts.NorthDepth(sb.SpawnChunkY(), sb.AbsChunkRow(world.Components.GridElement.Get(entity).Y))
84 1 : if d := query.DangerLevelForDepth(depth); d > danger {
85 1 : danger = d
86 1 : }
87 : }
88 1 : n := lootDice.Roll(rng)
89 1 : for range n {
90 1 : draws, err := raw.DrawFromItemTable(world.Resources.RawMaster, itemTable, rng, int(danger))
91 1 : if err != nil {
92 0 : return err
93 0 : }
94 1 : for _, d := range draws {
95 1 : if _, err := lifecycle.SpawnStorageItem(world, d.Name, d.Count, entity); err != nil {
96 0 : return err
97 0 : }
98 : }
99 : }
100 1 : return nil
101 : }
|