/
difficulty.go
120 lines (103 loc) · 4.67 KB
/
difficulty.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package world
// Difficulty represents the difficulty of a Minecraft world. The difficulty of
// a world influences all kinds of aspects of the world, such as the damage
// enemies deal to players, the way hunger depletes, whether hostile monsters
// spawn or not and more.
type Difficulty interface {
// FoodRegenerates specifies if players' food levels should automatically
// regenerate with this difficulty.
FoodRegenerates() bool
// StarvationHealthLimit specifies the amount of health at which a player
// will no longer receive damage from starvation.
StarvationHealthLimit() float64
// FireSpreadIncrease returns a number that increases the rate at which fire
// spreads.
FireSpreadIncrease() int
}
var (
// DifficultyPeaceful prevents most hostile mobs from spawning and makes
// players rapidly regenerate health and food.
DifficultyPeaceful difficultyPeaceful
// DifficultyEasy has mobs that deal less damage to players than normal and
// starvation won't occur if a player has less than 5 hearts of health.
DifficultyEasy difficultyEasy
// DifficultyNormal has mobs that deal normal damage to players. Starvation
// will occur until the player is down to a single heart.
DifficultyNormal difficultyNormal
// DifficultyHard has mobs that deal above average damage to players.
// Starvation will kill players with too little food and monsters will get
// additional effects.
DifficultyHard difficultyHard
)
var difficultyReg = newDifficultyRegistry(map[int]Difficulty{
0: DifficultyPeaceful,
1: DifficultyEasy,
2: DifficultyNormal,
3: DifficultyHard,
})
// DifficultyByID looks up a Difficulty for the ID passed, returning
// DifficultyPeaceful for 0, DifficultyEasy for 1, DifficultyNormal for 2 and
// DifficultyHard for 3. If the ID is unknown, the bool returned is false. In
// this case the Difficulty returned is DifficultyNormal.
func DifficultyByID(id int) (Difficulty, bool) {
return difficultyReg.Lookup(id)
}
// DifficultyID looks up the ID that a Difficulty was registered with. If not
// found, false is returned.
func DifficultyID(diff Difficulty) (int, bool) {
return difficultyReg.LookupID(diff)
}
type difficultyRegistry struct {
difficulties map[int]Difficulty
ids map[Difficulty]int
}
// newDifficultyRegistry returns an initialised difficultyRegistry.
func newDifficultyRegistry(diff map[int]Difficulty) *difficultyRegistry {
ids := make(map[Difficulty]int, len(diff))
for k, v := range diff {
ids[v] = k
}
return &difficultyRegistry{difficulties: diff, ids: ids}
}
// Lookup looks up a Difficulty for the ID passed, returning DifficultyPeaceful
// for 0, DifficultyEasy for 1, DifficultyNormal for 2 and DifficultyHard for
// 3. If the ID is unknown, the bool returned is false. In this case the
// Difficulty returned is DifficultyNormal.
func (reg *difficultyRegistry) Lookup(id int) (Difficulty, bool) {
dim, ok := reg.difficulties[id]
if !ok {
dim = DifficultyNormal
}
return dim, ok
}
// LookupID looks up the ID that a Difficulty was registered with. If not found,
// false is returned.
func (reg *difficultyRegistry) LookupID(diff Difficulty) (int, bool) {
id, ok := reg.ids[diff]
return id, ok
}
// difficultyPeaceful difficulty prevents most hostile mobs from spawning and
// makes players rapidly regenerate health and food.
type difficultyPeaceful struct{}
func (difficultyPeaceful) FoodRegenerates() bool { return true }
func (difficultyPeaceful) StarvationHealthLimit() float64 { return 20 }
func (difficultyPeaceful) FireSpreadIncrease() int { return 0 }
// difficultyEasy difficulty has mobs deal less damage to players than normal
// and starvation won't occur if a player has less than 5 hearts of health.
type difficultyEasy struct{}
func (difficultyEasy) FoodRegenerates() bool { return false }
func (difficultyEasy) StarvationHealthLimit() float64 { return 10 }
func (difficultyEasy) FireSpreadIncrease() int { return 7 }
// difficultyNormal difficulty has mobs that deal normal damage to players.
// Starvation will occur until the player is down to a single heart.
type difficultyNormal struct{}
func (difficultyNormal) FoodRegenerates() bool { return false }
func (difficultyNormal) StarvationHealthLimit() float64 { return 2 }
func (difficultyNormal) FireSpreadIncrease() int { return 14 }
// difficultyHard difficulty has mobs that deal above average damage to
// players. Starvation will kill players with too little food and monsters will
// get additional effects.
type difficultyHard struct{}
func (difficultyHard) FoodRegenerates() bool { return false }
func (difficultyHard) StarvationHealthLimit() float64 { return -1 }
func (difficultyHard) FireSpreadIncrease() int { return 21 }