I made a fix that works by replacing the entity_died method in control.lua with this:
script.on_event(defines.events.on_entity_died, function(event)
local entity = event.entity
if not entity or not entity.valid then return end
--DEBUG: Ensure enemy death being detected correctly
-- game.print("entity died: " .. entity.name)
-- Skip players
if entity.type == "character" then return end
-- Optional: only count enemies (recommended)
if entity.force ~= game.forces.enemy then return end
-- Optional: restrict to combat entities only
if entity.type ~= "unit" and entity.type ~= "unit-spawner" then
return
end
-- Get max health safely
local max_health = entity.max_health or 0
if max_health <= 0 then return end
-- Tune this ratio to your liking
local SPIRIT_RATIO = 0.05 -- 5% of max HP
local spirit_gain = math.max(1, math.ceil(max_health * SPIRIT_RATIO))
-- Award to all players
for _, player in pairs(game.connected_players) do
if player.valid and player.character then
local player_data = storage.players[player.index]
local force_data = storage.forces[player.force.name]
player_data.spirit = math.min(player_data.max_spirit + force_data.max_spirit,player_data.spirit + spirit_gain)
end
end
end)