github.com/df-mc/dragonfly@v0.9.13/server/world/difficulty.go (about) 1 package world 2 3 // Difficulty represents the difficulty of a Minecraft world. The difficulty of 4 // a world influences all kinds of aspects of the world, such as the damage 5 // enemies deal to players, the way hunger depletes, whether hostile monsters 6 // spawn or not and more. 7 type Difficulty interface { 8 // FoodRegenerates specifies if players' food levels should automatically 9 // regenerate with this difficulty. 10 FoodRegenerates() bool 11 // StarvationHealthLimit specifies the amount of health at which a player 12 // will no longer receive damage from starvation. 13 StarvationHealthLimit() float64 14 // FireSpreadIncrease returns a number that increases the rate at which fire 15 // spreads. 16 FireSpreadIncrease() int 17 } 18 19 var ( 20 // DifficultyPeaceful prevents most hostile mobs from spawning and makes 21 // players rapidly regenerate health and food. 22 DifficultyPeaceful difficultyPeaceful 23 // DifficultyEasy has mobs that deal less damage to players than normal and 24 // starvation won't occur if a player has less than 5 hearts of health. 25 DifficultyEasy difficultyEasy 26 // DifficultyNormal has mobs that deal normal damage to players. Starvation 27 // will occur until the player is down to a single heart. 28 DifficultyNormal difficultyNormal 29 // DifficultyHard has mobs that deal above average damage to players. 30 // Starvation will kill players with too little food and monsters will get 31 // additional effects. 32 DifficultyHard difficultyHard 33 ) 34 35 var difficultyReg = newDifficultyRegistry(map[int]Difficulty{ 36 0: DifficultyPeaceful, 37 1: DifficultyEasy, 38 2: DifficultyNormal, 39 3: DifficultyHard, 40 }) 41 42 // DifficultyByID looks up a Difficulty for the ID passed, returning 43 // DifficultyPeaceful for 0, DifficultyEasy for 1, DifficultyNormal for 2 and 44 // DifficultyHard for 3. If the ID is unknown, the bool returned is false. In 45 // this case the Difficulty returned is DifficultyNormal. 46 func DifficultyByID(id int) (Difficulty, bool) { 47 return difficultyReg.Lookup(id) 48 } 49 50 // DifficultyID looks up the ID that a Difficulty was registered with. If not 51 // found, false is returned. 52 func DifficultyID(diff Difficulty) (int, bool) { 53 return difficultyReg.LookupID(diff) 54 } 55 56 type difficultyRegistry struct { 57 difficulties map[int]Difficulty 58 ids map[Difficulty]int 59 } 60 61 // newDifficultyRegistry returns an initialised difficultyRegistry. 62 func newDifficultyRegistry(diff map[int]Difficulty) *difficultyRegistry { 63 ids := make(map[Difficulty]int, len(diff)) 64 for k, v := range diff { 65 ids[v] = k 66 } 67 return &difficultyRegistry{difficulties: diff, ids: ids} 68 } 69 70 // Lookup looks up a Difficulty for the ID passed, returning DifficultyPeaceful 71 // for 0, DifficultyEasy for 1, DifficultyNormal for 2 and DifficultyHard for 72 // 3. If the ID is unknown, the bool returned is false. In this case the 73 // Difficulty returned is DifficultyNormal. 74 func (reg *difficultyRegistry) Lookup(id int) (Difficulty, bool) { 75 dim, ok := reg.difficulties[id] 76 if !ok { 77 dim = DifficultyNormal 78 } 79 return dim, ok 80 } 81 82 // LookupID looks up the ID that a Difficulty was registered with. If not found, 83 // false is returned. 84 func (reg *difficultyRegistry) LookupID(diff Difficulty) (int, bool) { 85 id, ok := reg.ids[diff] 86 return id, ok 87 } 88 89 // difficultyPeaceful difficulty prevents most hostile mobs from spawning and 90 // makes players rapidly regenerate health and food. 91 type difficultyPeaceful struct{} 92 93 func (difficultyPeaceful) FoodRegenerates() bool { return true } 94 func (difficultyPeaceful) StarvationHealthLimit() float64 { return 20 } 95 func (difficultyPeaceful) FireSpreadIncrease() int { return 0 } 96 97 // difficultyEasy difficulty has mobs deal less damage to players than normal 98 // and starvation won't occur if a player has less than 5 hearts of health. 99 type difficultyEasy struct{} 100 101 func (difficultyEasy) FoodRegenerates() bool { return false } 102 func (difficultyEasy) StarvationHealthLimit() float64 { return 10 } 103 func (difficultyEasy) FireSpreadIncrease() int { return 7 } 104 105 // difficultyNormal difficulty has mobs that deal normal damage to players. 106 // Starvation will occur until the player is down to a single heart. 107 type difficultyNormal struct{} 108 109 func (difficultyNormal) FoodRegenerates() bool { return false } 110 func (difficultyNormal) StarvationHealthLimit() float64 { return 2 } 111 func (difficultyNormal) FireSpreadIncrease() int { return 14 } 112 113 // difficultyHard difficulty has mobs that deal above average damage to 114 // players. Starvation will kill players with too little food and monsters will 115 // get additional effects. 116 type difficultyHard struct{} 117 118 func (difficultyHard) FoodRegenerates() bool { return false } 119 func (difficultyHard) StarvationHealthLimit() float64 { return -1 } 120 func (difficultyHard) FireSpreadIncrease() int { return 21 }