forked from df-mc/dragonfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
damage.go
52 lines (47 loc) · 1.75 KB
/
damage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package enchantment
import (
"github.com/Adrian8115/dragonfly-Amethyst-Protocol/server/item"
"github.com/Adrian8115/dragonfly-Amethyst-Protocol/server/world"
"math"
)
// AffectedDamageSource represents a world.DamageSource whose damage may be
// affected by an enchantment. A world.DamageSource does not need to implement
// AffectedDamageSource to let Protection affect the damage. This happens
// depending on the (world.DamageSource).ReducedByResistance() method.
type AffectedDamageSource interface {
world.DamageSource
// AffectedByEnchantment specifies if a world.DamageSource is affected by
// the item.EnchantmentType passed.
AffectedByEnchantment(e item.EnchantmentType) bool
}
// DamageModifier is an item.EnchantmentType that can reduce damage through a
// modifier if an AffectedDamageSource returns true for it.
type DamageModifier interface {
Modifier() float64
}
// ProtectionFactor calculates the combined protection factor for a slice of
// item.Enchantment. The factor depends on the world.DamageSource passed and is
// in a range of [0, 0.8], where 0.8 means incoming damage would be reduced by
// 80%.
func ProtectionFactor(src world.DamageSource, enchantments []item.Enchantment) float64 {
f := 0.0
for _, e := range enchantments {
t := e.Type()
modifier, ok := t.(DamageModifier)
if !ok {
continue
}
reduced := false
if _, ok := t.(Protection); ok && src.ReducedByResistance() {
// Special case for Protection, because it applies to all damage
// sources by default, except those not reduced by resistance.
reduced = true
} else if asrc, ok := src.(AffectedDamageSource); ok && asrc.AffectedByEnchantment(t) {
reduced = true
}
if reduced {
f += float64(e.Level()) * modifier.Modifier()
}
}
return math.Min(f, 0.8)
}