Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ------------------------------------------------------------
- -- FixHPforClass 1.2
- -- Per-class max HP, no "reviving" from 0 HP.
- ------------------------------------------------------------
- modname = "FixHPforClass"
- version = "1.2"
- ------------------------------------------------------------
- -- Class IDs in Wolf:ET (typical ordering)
- ------------------------------------------------------------
- local CLASS_SOLDIER = 0
- local CLASS_MEDIC = 1
- local CLASS_ENGINEER = 2
- local CLASS_FIELDOPS = 3
- local CLASS_COVERTOPS = 4
- ------------------------------------------------------------
- -- Configuration: Max HP per class
- ------------------------------------------------------------
- local classMaxHP = {
- [CLASS_SOLDIER] = 190,
- [CLASS_MEDIC] = 175,
- [CLASS_ENGINEER] = 190,
- [CLASS_FIELDOPS] = 190,
- [CLASS_COVERTOPS] = 190
- }
- ------------------------------------------------------------
- -- Regeneration rate for NON-Medics (HP/sec)
- ------------------------------------------------------------
- local REGEN_RATE = 2.15
- ------------------------------------------------------------
- -- We'll track last time we regenerated (once per second)
- -- and fractional leftover for sub-integer increments.
- ------------------------------------------------------------
- local lastRegenTime = {}
- local regenAccumulator = {}
- ------------------------------------------------------------
- -- et_InitGame: Called at map/server start
- ------------------------------------------------------------
- function et_InitGame(levelTime, randomSeed, restart)
- et.RegisterModname(modname .. " " .. version)
- end
- ------------------------------------------------------------
- -- et_ClientSpawn: Called when a player spawns (or is revived)
- -- => Give them full HP, reset regeneration tracking
- ------------------------------------------------------------
- function et_ClientSpawn(clientNum, revived)
- local class = et.gentity_get(clientNum, "sess.playerType")
- local maxHP = getMaxHPForClass(class)
- -- Set Max HP and current health
- et.gentity_set(clientNum, "ps.stats", 3, maxHP) -- STAT_MAX_HEALTH = 3
- et.gentity_set(clientNum, "health", maxHP)
- -- Reset regeneration tracking
- lastRegenTime[clientNum] = et.trap_Milliseconds()
- regenAccumulator[clientNum] = 0.0
- end
- ------------------------------------------------------------
- -- et_RunFrame: Called ~100 times per second
- -- 1) Keep custom max HP enforced
- -- 2) Regenerate HP for non-Medics (only if HP >= 1)
- ------------------------------------------------------------
- function et_RunFrame(levelTime)
- local maxClients = tonumber(et.trap_Cvar_Get("sv_maxclients"))
- for clientNum = 0, maxClients - 1 do
- if isActivePlayer(clientNum) then
- fixMaxHP(clientNum)
- regenHPIfNeeded(clientNum, levelTime)
- else
- -- Clear if not active or dead
- lastRegenTime[clientNum] = nil
- regenAccumulator[clientNum] = nil
- end
- end
- end
- ------------------------------------------------------------
- -- Helper: check if a client is connected & alive (hp > 0)
- ------------------------------------------------------------
- function isActivePlayer(clientNum)
- local connected = et.gentity_get(clientNum, "pers.connected")
- if connected ~= 2 then -- 2 = CON_CONNECTED
- return false
- end
- local hp = et.gentity_get(clientNum, "health")
- return (hp > 0) -- Must be above 0 to be "alive"
- end
- ------------------------------------------------------------
- -- Helper: retrieve the max HP for a given class
- ------------------------------------------------------------
- function getMaxHPForClass(classID)
- -- Default/fallback if unexpected class ID
- return classMaxHP[classID] or 100
- end
- ------------------------------------------------------------
- -- 1) Enforce max HP for that player's class
- -- 2) Clamp current HP if it exceeds the max
- ------------------------------------------------------------
- function fixMaxHP(clientNum)
- local class = et.gentity_get(clientNum, "sess.playerType")
- local maxHP = getMaxHPForClass(class)
- -- Force STAT_MAX_HEALTH to be correct
- et.gentity_set(clientNum, "ps.stats", 3, maxHP)
- local curHP = et.gentity_get(clientNum, "health")
- if curHP > maxHP then
- et.gentity_set(clientNum, "health", maxHP)
- end
- end
- ------------------------------------------------------------
- -- Regenerate HP at REGEN_RATE HP/s for NON-Medics ONLY.
- -- We do partial increments once per second to keep it smooth.
- -- Skips if current HP < 1, so players at 0 HP can't be "rescued."
- ------------------------------------------------------------
- function regenHPIfNeeded(clientNum, levelTime)
- local class = et.gentity_get(clientNum, "sess.playerType")
- -- Skip regeneration for Medics
- if class == CLASS_MEDIC then
- return
- end
- -- Current HP (already guaranteed > 0 from isActivePlayer, but let's be explicit)
- local curHP = et.gentity_get(clientNum, "health")
- if curHP < 1 then
- -- This ensures if for some reason HP is 0, we do NOT regen
- return
- end
- -- If we have no record, initialize
- if not lastRegenTime[clientNum] then
- lastRegenTime[clientNum] = levelTime
- regenAccumulator[clientNum] = 0.0
- return
- end
- -- Only apply regen every full 1000 ms
- local elapsed = levelTime - lastRegenTime[clientNum]
- if elapsed >= 1000 then
- -- Advance lastRegenTime by exactly 1000 ms
- lastRegenTime[clientNum] = lastRegenTime[clientNum] + 1000
- -- Get this player's max HP
- local maxHP = getMaxHPForClass(class)
- -- Skip if already at or above max
- if curHP >= maxHP then
- return
- end
- -- Add REGEN_RATE to the accumulator
- regenAccumulator[clientNum] = regenAccumulator[clientNum] + REGEN_RATE
- -- Take the integer part
- local plus = math.floor(regenAccumulator[clientNum])
- if plus > 0 then
- regenAccumulator[clientNum] = regenAccumulator[clientNum] - plus
- local newHP = curHP + plus
- if newHP > maxHP then
- newHP = maxHP
- end
- et.gentity_set(clientNum, "health", newHP)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement