Skip to content

Commit

Permalink
player/hunger.go: Cleaned up methods with min/max functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandertv committed Nov 27, 2024
1 parent 569e2c9 commit 39e0f0f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 45 deletions.
49 changes: 9 additions & 40 deletions server/player/hunger.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,15 @@ func (m *hungerManager) SetFood(level int) {
m.mu.Lock()
defer m.mu.Unlock()

if level < 0 {
level = 0
} else if level > 20 {
level = 20
}
m.foodLevel = level
m.foodLevel = max(min(level, 20), 0)
}

// AddFood adds a number of food points to the current food level of a player.
func (m *hungerManager) AddFood(points int) {
m.mu.Lock()
defer m.mu.Unlock()

level := m.foodLevel + points
if level < 0 {
level = 0
} else if level > 20 {
level = 20
}
m.foodLevel = level
m.foodLevel = max(min(m.foodLevel+points, 20), 0)
}

// Reset resets the hunger manager to its default values, identical to those set when creating a new manager
Expand All @@ -64,17 +53,17 @@ func (m *hungerManager) Reset() {
m.foodLevel = 20
m.saturationLevel = 5
m.exhaustionLevel = 0
m.foodTick = 0
m.foodTick = 1
}

// ResetExhaustion resets the player's exhaustion level to 0.
// Prevents the player's food level from decreasing after preventing food loss.
// ResetExhaustion resets the player's exhaustion level to 0. It prevents the
// player's food level from decreasing immediately after cancelling food loss.
func (m *hungerManager) resetExhaustion() {
m.mu.Lock()
defer m.mu.Unlock()
m.exhaustionLevel = 0
m.saturationLevel = 0
m.foodTick = 0
m.foodTick = 1
}

// exhaust exhausts the player by the amount of points passed. If the total exhaustion level exceeds 4, a
Expand All @@ -101,24 +90,8 @@ func (m *hungerManager) saturate(food int, saturation float64) {
m.mu.Lock()
defer m.mu.Unlock()

level := m.foodLevel + food
if level < 0 {
level = 0
} else if level > 20 {
level = 20
}
m.foodLevel = level

sat := m.saturationLevel + saturation
if sat < 0 {
sat = 0
} else if sat > 20 {
sat = 20
}
if sat > float64(m.foodLevel) {
sat = float64(m.foodLevel)
}
m.saturationLevel = sat
m.foodLevel = max(min(m.foodLevel+food, 20), 0)
m.saturationLevel = max(min(m.saturationLevel+saturation, float64(m.foodLevel)), 0)
}

// desaturate removes one saturation point from the player. If the saturation level of the player is already
Expand All @@ -128,11 +101,7 @@ func (m *hungerManager) desaturate() {
if m.saturationLevel <= 0 && m.foodLevel != 0 {
m.foodLevel--
} else if m.saturationLevel > 0 {
m.saturationLevel--
if m.saturationLevel < 0 {
// Some foods provide saturation with decimals, so we have to account for an overcompensation.
m.saturationLevel = 0
}
m.saturationLevel = max(m.saturationLevel-1, 0)
}
}

Expand Down
8 changes: 3 additions & 5 deletions server/player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,12 +762,10 @@ func (p *Player) Exhaust(points float64) {

ctx := event.C(p)
if p.Handler().HandleFoodLoss(ctx, before, &after); ctx.Cancelled() {
// Reset the exhaustion level if the event was cancelled.
// Because if we cancel this on some moments, and after a time we don't cancel it,
// the first food level going to decrease a bit faster than expected.
// An example is if we cancelled that on a world, and we change of world, our food decrease very fast on the next tick
// Reset the exhaustion level if the event was cancelled. Because if
// we cancel this, and at some point we stop cancelling it, the
// first food point will be lost more quickly than expected.
p.hunger.resetExhaustion()

return
}
p.hunger.SetFood(after)
Expand Down

0 comments on commit 39e0f0f

Please sign in to comment.