Line data Source code
1 : package activity
2 :
3 : import (
4 : "fmt"
5 :
6 : gc "github.com/kijimaD/ruins/internal/components"
7 : "github.com/kijimaD/ruins/internal/consts"
8 : "github.com/kijimaD/ruins/internal/gamelog"
9 : w "github.com/kijimaD/ruins/internal/world"
10 : "github.com/kijimaD/ruins/internal/world/query"
11 : "github.com/mlange-42/ark/ecs"
12 : )
13 :
14 : // 戦闘での怪我のパラメータ。値は実プレイで調整する
15 : const (
16 : // injuryChancePercent は命中1回で怪我が付く確率。怪我はたまに起きる脇役なので低く保つ
17 : injuryChancePercent = 15
18 : // injuryInitialTimer は付いた怪我の初期進行度。発症する 25 以上にして即座に効かせる
19 : injuryInitialTimer = 40
20 : // maxInjuriesPerPartType は1部位に付く同種の怪我の上限。種類が違えば共存する
21 : maxInjuriesPerPartType = 1
22 : )
23 :
24 : // applyInjury は命中時に確率で命中部位へ怪我を1つ付ける。
25 : // 怪我の種類は攻撃種別の行が持ち、命中部位はその怪我が起こりうる部位から部位サイズの重みで抽選する。HealthStatus を持つ対象だけが対象。
26 : // 同じ部位に同種の怪我が既にあれば付けない。種類や部位が違う傷は独立で、身体機能と失血は全傷が合算される
27 : func applyInjury(actor, target ecs.Entity, world w.World, attack gc.Attacker) {
28 1 : if !world.Components.HealthStatus.Has(target) {
29 1 : return
30 1 : }
31 1 : if world.Resources.Config.RNG.IntN(100) >= injuryChancePercent {
32 1 : return
33 1 : }
34 :
35 1 : defs := world.Resources.ConditionDefs
36 1 : injuryType, ok := defs.Injury(attack.GetAttackCategory())
37 1 : if !ok {
38 0 : return
39 0 : }
40 1 : part := hitPart(world.Resources.Config.RNG, defs, injuryType)
41 1 :
42 1 : hs := world.Components.HealthStatus.Get(target)
43 1 : if hs.Parts[part].CountConditions(injuryType) >= maxInjuriesPerPartType {
44 1 : return
45 1 : }
46 1 : hs.Parts[part].AddCondition(gc.HealthCondition{
47 1 : Type: injuryType,
48 1 : Timer: injuryInitialTimer,
49 1 : Severity: gc.TimerToSeverity(injuryInitialTimer),
50 1 : })
51 1 :
52 1 : logInjury(actor, target, world, part, injuryType)
53 : }
54 :
55 : // hitPart は怪我 ct が起こりうる部位から、命中の重みで部位を1つ引く。
56 : func hitPart(r consts.Roller, defs gc.ConditionDefs, ct gc.ConditionType) gc.BodyPart {
57 1 : parts := make([]gc.BodyPart, gc.BodyPartCount)
58 1 : for p := range gc.BodyPartCount {
59 1 : parts[p] = p
60 1 : }
61 1 : i, ok := consts.Pick(r, parts, func(p gc.BodyPart) float64 {
62 1 : if !defs.CanAffect(ct, p) {
63 1 : return 0
64 1 : }
65 1 : return defs.HitWeight(p)
66 : })
67 1 : if !ok {
68 0 : panic(fmt.Sprintf("no body part can take %s", ct))
69 : }
70 1 : return parts[i]
71 : }
72 :
73 : // logInjury は怪我を負ったことをログに出す。攻撃者か対象が味方に関わるときだけ出す
74 : func logInjury(actor, target ecs.Entity, world w.World, part gc.BodyPart, injuryType gc.ConditionType) {
75 1 : if !query.IsAlly(world, actor) && !query.IsAlly(world, target) {
76 1 : return
77 1 : }
78 1 : targetName := query.GetEntityName(target, world)
79 1 : targetMarkup := query.NameMarkup(target, targetName, world)
80 1 : partName := query.BodyPartName(world, part)
81 1 : injuryName := query.ConditionName(world, injuryType)
82 1 : gamelog.New(query.GetGameLog(world)).
83 1 : Markup(query.T(world, "%s suffered %s on the %s.", targetMarkup, injuryName, partName)).
84 1 : Log()
85 : }
|