Combat System - Bug Fixes
Issues Identified
The enemy retaliation system was instantly killing players regardless of their HP levels.
Players were healing back to full HP during combat when equipment changed or retaliation occurred.
Root Causes Found
1. Uninitialized HP Values
Problem: Players could be loaded from the database with CurrentHP = 0 even when MaxHP > 0.
Fix: Added HP validation in UpdateCombatStats():
// Ensure CurrentHP is never 0 unless player is actually dead
if player.CombatStats.CurrentHP <= 0 && player.CombatStats.MaxHP > 0 {
player.CombatStats.CurrentHP = player.CombatStats.MaxHP
log.Printf("COMBAT: WARNING: Player %s had 0 HP, resetting to full health (%d)",
player.GameAlias, player.CombatStats.MaxHP)
}2. Player Loading Issues in Retaliation System
Problem: The retaliation system was trying to create new players when they didn't exist, causing crashes.
Fix: Modified retaliation system to only work with existing players:
// Try to load existing player data only (don't create new players)
players, err := loadPlayers()
if err != nil {
log.Printf("RETALIATION: ERROR: Failed to load players database: %v", err)
return
}
playerData, exists := players[request.PlayerClientName]
if !exists {
log.Printf("RETALIATION: Player %s not found in database, canceling retaliation",
request.PlayerClientName)
return
}3. Combat Stats Not Updated in Retaliation
Problem: Retaliation system wasn't ensuring combat stats were current.
Fix: Added explicit combat stats update:
// Ensure combat stats are up to date
UpdateCombatStats(player)4. Healing During Combat Bug
Problem: UpdateCombatStats() was healing players back to full HP whenever called during combat.
Root Cause: The condition if (CurrentHP <= 0 && MaxHP > 0) was triggering for damaged players.
Fix: Changed to only heal truly uninitialized players:
// Only reset HP if it's truly uninitialized (both MaxHP and CurrentHP are 0)
// Don't heal players who have taken damage in combat
if player.CombatStats.CurrentHP == 0 && player.CombatStats.MaxHP == 0 {
// This is a truly uninitialized player
player.CombatStats.MaxHP = 10 + player.Stats.Str/3
player.CombatStats.CurrentHP = player.CombatStats.MaxHP
log.Printf("COMBAT: Initializing HP for new player %s: %d/%d",
player.GameAlias, player.CombatStats.CurrentHP, player.CombatStats.MaxHP)
}Changes Made
lib/combat.go
Fixed HP validation in
UpdateCombatStats()to prevent healing during combatOnly initializes HP for truly new players (both CurrentHP and MaxHP are 0)
Preserves damage taken by players during combat
lib/enemy_retaliation.go
Changed player loading to use
loadPlayers()directly instead ofGetPlayer()Added proper error handling for missing players
Removed debug logging (was added during troubleshooting)
Added explicit combat stats update before damage calculation
Testing Results
Before Fix
Players were instantly dying from any enemy retaliation
Players were healing back to full HP during combat
System was trying to create players during retaliation processing
HP values were often 0 or uninitialized
After Fix
Players take appropriate damage based on enemy attack vs player defense
Players stay damaged during combat (no unwanted healing)
System gracefully handles missing players
HP values are properly initialized and maintained
Retaliation damage is reasonable (typically 1-5 damage)
Damage Calculation Examples
With the fix, enemy retaliation damage is now properly calculated:
Player Stats: Str=16, Dex=12, Int=10
Combat Stats: HP=15/15, Defense=4
Enemy Attack Scenarios:
- Enemy attack 3: 2 damage to player
- Enemy attack 4: 3 damage to player
- Enemy attack 5: 3 damage to player
- Enemy attack 6: 3 damage to player
- Enemy attack 7: 5 damage to player
Result: Player survives retaliations and takes reasonable damageSystem Robustness
The retaliation system now properly:
✅ Validates player existence before processing
✅ Ensures HP values are properly initialized
✅ Updates combat stats before damage calculation
✅ Handles missing players gracefully
✅ Logs appropriate error messages for debugging
✅ Maintains game balance with reasonable damage
Prevention Measures
To prevent similar issues in the future:
Always call
UpdateCombatStats()when loading player data for combatValidate HP values before applying damage
Use proper error handling in go routines
Test with real player data, not just mock objects
Add logging for critical combat operations
The enemy retaliation system is now working correctly and provides balanced, exciting combat encounters without instant death bugs.
Last updated
Was this helpful?