Line data Source code
1 : package entityspec
2 :
3 : import (
4 : "fmt"
5 : "image/color"
6 : "strconv"
7 :
8 : gc "github.com/kijimaD/ruins/internal/components"
9 : "github.com/kijimaD/ruins/internal/consts"
10 : "github.com/kijimaD/ruins/internal/raw"
11 : "github.com/kijimaD/ruins/internal/rawschema"
12 : w "github.com/kijimaD/ruins/internal/world"
13 : "github.com/mlange-42/ark/ecs"
14 :
15 : "github.com/kijimaD/ruins/internal/world/query"
16 : )
17 :
18 : // SpecRow は性能表示の1行。Header が真ならカテゴリ見出し行、偽なら ラベル/値 のデータ行。
19 : // 詳細モーダルはこの行の並びを単位としてページ分割する。行の高さは一定なので行数が高さの目安になる。
20 : // Color が非 nil のデータ行はその色で描く。クラフトの必要素材を条件可否で色分けする用途に使う
21 : type SpecRow struct {
22 : Label string
23 : Value string
24 : Header bool
25 : Indent int // 見出しからのインデント段数。0 は段なし
26 : Color *color.RGBA
27 : }
28 :
29 : // markChildren は見出し以外の行を1段深くする。見出しを持つビルダの戻り値を包んでグループ化する
30 : func markChildren(rows []SpecRow) []SpecRow {
31 1 : for i := range rows {
32 1 : if !rows[i].Header {
33 1 : rows[i].Indent++
34 1 : }
35 : }
36 1 : return rows
37 : }
38 :
39 : // specPart は性能表示の1要素。実体と raw spec の2つのデータ源それぞれから行を作る。
40 : // fromSpec が nil の要素は raw spec 表示には出ない。生成後にしか定まらない鮮度などが該当する。
41 : // component を足すときは specParts か basicParts へ1要素足すだけで両ビューに反映され、片方への入れ忘れが起きない
42 : type specPart struct {
43 : fromEntity func(world w.World, entity ecs.Entity) []SpecRow
44 : fromSpec func(world w.World, spec gc.EntitySpec) []SpecRow
45 : }
46 :
47 : // specParts は独自の見出しを持つか単独で並ぶ性能要素。表示順を兼ねる。
48 : // 材質や重量など多くのアイテムに共通の単値属性は basicParts に分け、末尾の「基本」見出しへまとめる
49 : var specParts = []specPart{
50 : { // 能力値。実体のみ
51 : fromEntity: func(world w.World, e ecs.Entity) []SpecRow {
52 1 : if !world.Components.Abilities.Has(e) {
53 1 : return nil
54 1 : }
55 1 : return abilityRows(world, world.Components.Abilities.Get(e))
56 : },
57 : },
58 : { // 近接
59 : fromEntity: func(world w.World, e ecs.Entity) []SpecRow {
60 1 : if !world.Components.Melee.Has(e) {
61 1 : return nil
62 1 : }
63 1 : return attackerRows(world, world.Components.Melee.Get(e))
64 : },
65 : fromSpec: func(world w.World, s gc.EntitySpec) []SpecRow {
66 1 : if s.Melee == nil {
67 1 : return nil
68 1 : }
69 1 : return attackerRows(world, s.Melee)
70 : },
71 : },
72 : { // 射撃
73 : fromEntity: func(world w.World, e ecs.Entity) []SpecRow {
74 1 : if !world.Components.Fire.Has(e) {
75 1 : return nil
76 1 : }
77 1 : fire := world.Components.Fire.Get(e)
78 1 : return append(attackerRows(world, fire), fireAmmoRows(world, fire)...)
79 : },
80 : fromSpec: func(world w.World, s gc.EntitySpec) []SpecRow {
81 1 : if s.Fire == nil {
82 1 : return nil
83 1 : }
84 1 : return append(attackerRows(world, s.Fire), fireAmmoRows(world, s.Fire)...)
85 : },
86 : },
87 : { // 防具
88 : fromEntity: func(world w.World, e ecs.Entity) []SpecRow {
89 1 : if !world.Components.Wearable.Has(e) {
90 1 : return nil
91 1 : }
92 1 : return wearableRows(world, world.Components.Wearable.Get(e))
93 : },
94 : fromSpec: func(world w.World, s gc.EntitySpec) []SpecRow {
95 1 : if s.Wearable == nil {
96 1 : return nil
97 1 : }
98 1 : return wearableRows(world, s.Wearable)
99 : },
100 : },
101 : { // 本
102 : fromEntity: func(world w.World, e ecs.Entity) []SpecRow {
103 1 : if !world.Components.Book.Has(e) {
104 1 : return nil
105 1 : }
106 1 : return bookRows(world, world.Components.Book.Get(e))
107 : },
108 : fromSpec: func(world w.World, s gc.EntitySpec) []SpecRow {
109 1 : if s.Book == nil {
110 1 : return nil
111 1 : }
112 1 : return bookRows(world, s.Book)
113 : },
114 : },
115 : { // 治療。価値や重量など多くのアイテムに共通の項目の後に置く
116 : fromEntity: func(world w.World, e ecs.Entity) []SpecRow {
117 1 : if !world.Components.Remedy.Has(e) {
118 1 : return nil
119 1 : }
120 1 : return remedyRows(world, world.Components.Remedy.Get(e))
121 : },
122 : fromSpec: func(world w.World, s gc.EntitySpec) []SpecRow {
123 1 : if s.Remedy == nil {
124 1 : return nil
125 1 : }
126 1 : return remedyRows(world, s.Remedy)
127 : },
128 : },
129 : }
130 :
131 : // basicParts は材質や重量など多くのアイテムに共通の単値属性。末尾に「基本」見出しでひとまとめにする
132 : var basicParts = []specPart{
133 : { // 材質
134 : fromEntity: func(world w.World, e ecs.Entity) []SpecRow {
135 1 : if !world.Components.Material.Has(e) {
136 1 : return nil
137 1 : }
138 1 : return []SpecRow{materialRow(world, world.Components.Material.Get(e))}
139 : },
140 : fromSpec: func(world w.World, s gc.EntitySpec) []SpecRow {
141 1 : if s.Material == nil {
142 1 : return nil
143 1 : }
144 1 : return []SpecRow{materialRow(world, s.Material)}
145 : },
146 : },
147 : { // 燃料。熱量は保持せず材質と重量から導くので、実体と spec で算出元が違う
148 : fromEntity: func(world w.World, e ecs.Entity) []SpecRow {
149 1 : if heat := query.HeatContent(world, e); heat > 0 {
150 1 : return []SpecRow{fuelRow(world, heat)}
151 1 : }
152 1 : return nil
153 : },
154 : fromSpec: func(world w.World, s gc.EntitySpec) []SpecRow {
155 1 : if s.Material == nil || s.Weight == nil {
156 1 : return nil
157 1 : }
158 1 : if heat := world.Resources.MaterialDefs.HeatOf(s.Material.Kind, s.Weight.Milligram); heat > 0 {
159 1 : return []SpecRow{fuelRow(world, heat)}
160 1 : }
161 1 : return nil
162 : },
163 : },
164 : { // 分解。燃料と同じく材質と重量から導く
165 : fromEntity: func(world w.World, e ecs.Entity) []SpecRow {
166 1 : if id, count, ok := query.ItemDisassemblyYield(world, e); ok {
167 1 : return []SpecRow{disassemblyRow(world, id, count)}
168 1 : }
169 1 : return nil
170 : },
171 : fromSpec: func(world w.World, s gc.EntitySpec) []SpecRow {
172 1 : if id, count, ok := query.SpecDisassemblyYield(world, s); ok {
173 1 : return []SpecRow{disassemblyRow(world, id, count)}
174 1 : }
175 1 : return nil
176 : },
177 : },
178 : { // 回復
179 : fromEntity: func(world w.World, e ecs.Entity) []SpecRow {
180 1 : if !world.Components.ProvidesHealing.Has(e) {
181 1 : return nil
182 1 : }
183 1 : return healingRows(world, world.Components.ProvidesHealing.Get(e))
184 : },
185 : fromSpec: func(world w.World, s gc.EntitySpec) []SpecRow {
186 1 : if s.ProvidesHealing == nil {
187 1 : return nil
188 1 : }
189 1 : return healingRows(world, s.ProvidesHealing)
190 : },
191 : },
192 : { // 栄養
193 : fromEntity: func(world w.World, e ecs.Entity) []SpecRow {
194 1 : if !world.Components.ProvidesNutrition.Has(e) {
195 1 : return nil
196 1 : }
197 1 : return nutritionRows(world, world.Components.ProvidesNutrition.Get(e))
198 : },
199 : fromSpec: func(world w.World, s gc.EntitySpec) []SpecRow {
200 1 : if s.ProvidesNutrition == nil {
201 1 : return nil
202 1 : }
203 1 : return nutritionRows(world, s.ProvidesNutrition)
204 : },
205 : },
206 : { // 鮮度。生成時の刻印が要るので実体のみ
207 : fromEntity: func(world w.World, e ecs.Entity) []SpecRow {
208 1 : if !world.Components.Perishable.Has(e) {
209 1 : return nil
210 1 : }
211 1 : return []SpecRow{freshnessRow(world, e)}
212 : },
213 : },
214 : { // 価値
215 : fromEntity: func(world w.World, e ecs.Entity) []SpecRow {
216 1 : if !world.Components.Value.Has(e) {
217 1 : return nil
218 1 : }
219 1 : return valueRows(world, world.Components.Value.Get(e))
220 : },
221 : fromSpec: func(world w.World, s gc.EntitySpec) []SpecRow {
222 1 : if s.Value == nil {
223 1 : return nil
224 1 : }
225 1 : return valueRows(world, s.Value)
226 : },
227 : },
228 : { // 重量
229 : fromEntity: func(world w.World, e ecs.Entity) []SpecRow {
230 1 : if !world.Components.Weight.Has(e) {
231 1 : return nil
232 1 : }
233 1 : return weightRows(world, world.Components.Weight.Get(e))
234 : },
235 : fromSpec: func(world w.World, s gc.EntitySpec) []SpecRow {
236 1 : if s.Weight == nil {
237 1 : return nil
238 1 : }
239 1 : return weightRows(world, s.Weight)
240 : },
241 : },
242 : }
243 :
244 : // SpecRows はエンティティの性能表示を行の並びとして返す。
245 : // specParts に続けて、basicParts を末尾の「基本」見出しでまとめて足す
246 : func SpecRows(world w.World, entity ecs.Entity) []SpecRow {
247 1 : return appendBasicGroup(world,
248 1 : collectEntityRows(world, entity, specParts),
249 1 : collectEntityRows(world, entity, basicParts))
250 1 : }
251 :
252 : func collectEntityRows(world w.World, entity ecs.Entity, parts []specPart) []SpecRow {
253 1 : var rows []SpecRow
254 1 : for _, p := range parts {
255 1 : if p.fromEntity != nil {
256 1 : rows = append(rows, p.fromEntity(world, entity)...)
257 1 : }
258 : }
259 1 : return rows
260 : }
261 :
262 : // appendBasicGroup は基本属性の行を「基本」見出しでまとめて rows の末尾へ足す。基本属性が無ければ rows をそのまま返す
263 : func appendBasicGroup(world w.World, rows, basic []SpecRow) []SpecRow {
264 1 : if len(basic) == 0 {
265 1 : return rows
266 1 : }
267 1 : group := make([]SpecRow, 0, len(basic)+1)
268 1 : group = append(group, SpecRow{Label: query.T(world, "Basic"), Header: true})
269 1 : group = append(group, basic...)
270 1 : return append(rows, markChildren(group)...)
271 : }
272 :
273 : // SpecRowsFromSpec は EntitySpec の性能表示を行の並びとして返す。
274 : // エンティティを生成せず raw 定義から詳細を出す商店などで使う。鮮度など fromSpec が nil の要素は出ない
275 : func SpecRowsFromSpec(world w.World, spec gc.EntitySpec) []SpecRow {
276 1 : return appendBasicGroup(world,
277 1 : collectSpecRows(world, spec, specParts),
278 1 : collectSpecRows(world, spec, basicParts))
279 1 : }
280 :
281 : // collectSpecRows は fromSpec を持つ parts の行だけを連結する。fromSpec が nil の要素は raw spec 表示に出ない
282 : func collectSpecRows(world w.World, spec gc.EntitySpec, parts []specPart) []SpecRow {
283 1 : var rows []SpecRow
284 1 : for _, p := range parts {
285 1 : if p.fromSpec != nil {
286 1 : rows = append(rows, p.fromSpec(world, spec)...)
287 1 : }
288 : }
289 1 : return rows
290 : }
291 :
292 : // materialRow は材質の1行を返す。可燃性と燃焼熱量の根拠で、不燃の材質も見せて燃える/燃えないを読み取れる
293 : func materialRow(world w.World, material *gc.Material) SpecRow {
294 1 : return SpecRow{Label: query.T(world, "Material"), Value: query.T(world, world.Resources.MaterialDefs[material.Kind].Name)}
295 1 : }
296 :
297 : // fuelRow は燃料の熱量の1行を返す。燃やしたとき火へ移す熱量で、燃焼時間の目安になる。
298 : // 値の無い見出し行は置かず、材質の行と並べて読ませる。熱量は保持せず材質と重量から導く
299 : func fuelRow(world w.World, heat consts.Heat) SpecRow {
300 : // 熱量は炎アイコンで見せる。満burn時の燃焼ターン数に等しく、地面直の火では効率で減る
301 1 : return SpecRow{Label: query.T(world, "Fuel"), Value: heat.String()}
302 1 : }
303 :
304 : // disassemblyRow は1個を分解したときに戻る素材と個数の行を返す
305 : func disassemblyRow(world w.World, id string, count int) SpecRow {
306 1 : name := query.T(world, raw.ItemName(world.Resources.RawMaster, id))
307 1 : return SpecRow{Label: query.T(world, "Disassembly"), Value: fmt.Sprintf("%s x%d", name, count)}
308 1 : }
309 :
310 : // attackerRows は攻撃パラメータの行を返す。先頭は攻撃種別の見出し
311 : func attackerRows(world w.World, attack gc.Attacker) []SpecRow {
312 1 : rows := []SpecRow{
313 1 : {Label: query.AttackTypeName(world, attack.GetAttackCategory()), Header: true},
314 1 : {Label: query.T(world, "Attack power"), Value: strconv.Itoa(attack.GetDamage())},
315 1 : {Label: query.T(world, "Accuracy"), Value: strconv.Itoa(attack.GetAccuracy())},
316 1 : {Label: query.T(world, "Hits"), Value: strconv.Itoa(attack.GetAttackCount())},
317 1 : {Label: query.T(world, "Attack cost"), Value: strconv.Itoa(attack.GetCost())},
318 1 : }
319 1 : if attack.GetElement() != rawschema.ElementNONE {
320 1 : rows = append(rows, SpecRow{Label: query.T(world, "Element"), Value: query.T(world, gc.ElementName(attack.GetElement()))})
321 1 : }
322 1 : return markChildren(rows)
323 : }
324 :
325 : // fireAmmoRows は射程・弾薬の行を返す
326 : func fireAmmoRows(world w.World, fire *gc.Fire) []SpecRow {
327 1 : var rows []SpecRow
328 1 : if rangeParams, ok := world.Resources.SkillDefs.RangeParams(fire.AttackCategory); ok {
329 1 : rows = append(rows,
330 1 : SpecRow{Label: query.T(world, "Optimal range"), Value: strconv.Itoa(rangeParams.OptimalRange)},
331 1 : SpecRow{Label: query.T(world, "Max range"), Value: strconv.Itoa(rangeParams.MaxRange)},
332 1 : )
333 1 : }
334 1 : if fire.MagazineSize > 0 {
335 1 : rows = append(rows,
336 1 : SpecRow{Label: query.T(world, "Magazine"), Value: fmt.Sprintf("%d/%d", fire.Magazine, fire.MagazineSize)},
337 1 : SpecRow{Label: query.T(world, "Reload"), Value: strconv.Itoa(fire.ReloadEffort)},
338 1 : )
339 1 : }
340 1 : return markChildren(rows)
341 : }
342 :
343 : // wearableRows は防具の行を返す。先頭は装備部位の見出し
344 : // abilityRows は能力値をキャラ画面と同じラベルで縦に並べる。能力を持つ実体の詳細で使う
345 : func abilityRows(world w.World, a *gc.Abilities) []SpecRow {
346 1 : return []SpecRow{
347 1 : {Label: query.T(world, "Vitality"), Value: strconv.Itoa(a.Vitality.Base)},
348 1 : {Label: query.T(world, "Strength"), Value: strconv.Itoa(a.Strength.Base)},
349 1 : {Label: query.T(world, "Sensation"), Value: strconv.Itoa(a.Sensation.Base)},
350 1 : {Label: query.T(world, "Dexterity"), Value: strconv.Itoa(a.Dexterity.Base)},
351 1 : {Label: query.T(world, "Agility"), Value: strconv.Itoa(a.Agility.Base)},
352 1 : {Label: query.T(world, "Defense"), Value: strconv.Itoa(a.Defense.Base)},
353 1 : }
354 : }
355 :
356 : func wearableRows(world w.World, wearable *gc.Wearable) []SpecRow {
357 1 : rows := []SpecRow{
358 1 : {Label: query.T(world, gc.EquipmentCategoryName(wearable.EquipmentCategory)), Header: true},
359 1 : {Label: query.T(world, "Defense"), Value: fmt.Sprintf("%+d", wearable.Defense)},
360 1 : }
361 1 : if wearable.InsulationCold != 0 {
362 1 : rows = append(rows, SpecRow{Label: query.T(world, "Cold resist"), Value: fmt.Sprintf("%+d", wearable.InsulationCold)})
363 1 : }
364 1 : if wearable.InsulationHeat != 0 {
365 1 : rows = append(rows, SpecRow{Label: query.T(world, "Heat resist"), Value: fmt.Sprintf("%+d", wearable.InsulationHeat)})
366 1 : }
367 1 : rows = append(rows, equipBonusRows(world, wearable.EquipBonus)...)
368 1 : return markChildren(rows)
369 : }
370 :
371 : // equipBonusRows は装備ボーナスの行を返す。0 の項目は出さない
372 : func equipBonusRows(world w.World, equipBonus gc.EquipBonus) []SpecRow {
373 1 : var rows []SpecRow
374 1 : add := func(label string, v int) {
375 1 : if v != 0 {
376 1 : rows = append(rows, SpecRow{Label: label, Value: fmt.Sprintf("%+d", v)})
377 1 : }
378 : }
379 1 : add(query.T(world, "Vitality"), equipBonus.Vitality)
380 1 : add(query.T(world, "Strength"), equipBonus.Strength)
381 1 : add(query.T(world, "Sensation"), equipBonus.Sensation)
382 1 : add(query.T(world, "Dexterity"), equipBonus.Dexterity)
383 1 : add(query.T(world, "Agility"), equipBonus.Agility)
384 1 : return rows
385 : }
386 :
387 : // healingRows は回復量の行を返す
388 : func healingRows(world w.World, healing *gc.ProvidesHealing) []SpecRow {
389 1 : var healValue string
390 1 : switch healing.Kind {
391 : case gc.HealNumeral:
392 1 : healValue = strconv.Itoa(int(healing.Amount))
393 : case gc.HealRatio:
394 1 : healValue = fmt.Sprintf("%.0f%%", healing.Amount*100)
395 : default:
396 1 : healValue = "-"
397 : }
398 1 : return []SpecRow{{Label: query.T(world, "Vitality"), Value: healValue}}
399 : }
400 :
401 : // nutritionRows は栄養の行を返す
402 : func nutritionRows(world w.World, nutrition *gc.ProvidesNutrition) []SpecRow {
403 1 : return []SpecRow{{Label: query.T(world, "Nutrition"), Value: strconv.Itoa(nutrition.Amount)}}
404 1 : }
405 :
406 : // remedyRows は治療できる不調と効力の行を返す。先頭は見出し。何を治すか分からないと選べないので示す
407 : func remedyRows(world w.World, remedy *gc.Remedy) []SpecRow {
408 1 : rows := make([]SpecRow, 0, 2+len(remedy.Treats))
409 1 : rows = append(rows, SpecRow{Label: query.T(world, "Treatment"), Header: true})
410 1 : for _, ct := range remedy.Treats {
411 1 : rows = append(rows, SpecRow{Label: query.T(world, "Target"), Value: query.ConditionName(world, ct)})
412 1 : }
413 1 : rows = append(rows, SpecRow{Label: query.T(world, "Potency"), Value: fmt.Sprintf("%d%%", int(remedy.Potency))})
414 1 : return markChildren(rows)
415 : }
416 :
417 : // freshnessRow は鮮度の1行を返す。鮮度の算出は query.FreshnessStageOf に委ねる
418 : func freshnessRow(world w.World, entity ecs.Entity) SpecRow {
419 1 : stage, _ := query.FreshnessStageOf(world, entity)
420 1 : return SpecRow{Label: query.T(world, "Freshness"), Value: query.FreshnessName(world, stage)}
421 1 : }
422 :
423 : // valueRows は価値の行を返す
424 : func valueRows(world w.World, value *gc.Value) []SpecRow {
425 1 : return []SpecRow{{Label: query.T(world, "Value"), Value: consts.Currency(value.Value).String()}}
426 1 : }
427 :
428 : // weightRows は重量の行を返す
429 : func weightRows(world w.World, weight *gc.Weight) []SpecRow {
430 1 : return []SpecRow{{Label: query.T(world, "Weight"), Value: weight.String()}}
431 1 : }
432 :
433 : // bookRows は本の行を返す。先頭は見出し
434 : func bookRows(world w.World, book *gc.Book) []SpecRow {
435 1 : rows := []SpecRow{{Label: query.T(world, "Book"), Header: true}}
436 1 : if book.Skill != nil {
437 1 : rows = append(rows,
438 1 : SpecRow{Label: query.T(world, "Skill"), Value: query.SkillName(world, book.Skill.TargetSkill)},
439 1 : SpecRow{Label: "Lv", Value: fmt.Sprintf("%d %s %d", book.Skill.RequiredLevel, consts.IconArrowRight, book.Skill.MaxLevel)},
440 1 : )
441 1 : }
442 1 : if book.Effort.Current > 0 && book.Effort.Max > 0 {
443 1 : pct := book.Effort.Current * 100 / book.Effort.Max
444 1 : rows = append(rows, SpecRow{Label: query.T(world, "Progress"), Value: fmt.Sprintf("%d%%", pct)})
445 1 : }
446 1 : return markChildren(rows)
447 : }
|