local U = require "scriptbank\\utillib" -- controls how close the player needs to be before healthbar appears local showDistance = 3000 local gEnt = g_Entity local aiBot = ai_bot_state local healthBars = {} local charList = {} local controlEnt = nil local ctrlTimer = math.huge local deg = math.deg local atan = math.atan2 local modf = math.modf local function getUnused( name ) for k, v in pairs( healthBars ) do if not v.used and v.name == name then return k end end end function health_bar2_init_name( e, name ) Include( "utillib.lua" ) Hide( e ) healthBars[ e ] = { name = name, used = false } end function AttachHealthBar( c, release ) local Char = charList[ c ] if Char == nil then charList[ c ] = { maxHealth = gEnt[ c ].health, red = nil, green = nil, timer = math.huge } return end if not release and gEnt[ c ].health > 0 and U.PlayerCloserThan( c, showDistance ) then Char.timer = g_Time + 500 if Char.red == nil and Char.green == nil then local redBar = getUnused( 'HealthBarRed' ) local greenBar = getUnused( 'HealthBarGreen' ) if redBar == nil or greenBar == nil then return end -- we've found our health bars so attach to our character Char.red = redBar healthBars[ redBar ].used = true Char.green = greenBar healthBars[ greenBar ].used = true end else -- free up the bars if Char.red ~= nil then Hide( Char.red ) healthBars[ Char.red ].used = false Char.red = nil end if Char.green ~= nil then Hide( Char.green ) healthBars[ Char.green ].used = false Char.green = nil end end end local function repositionHealthBars( character, rB, gB, Px, Pz ) local Ent = gEnt[ character ] local ang = atan( Px - Ent.x, Pz - Ent.z ) local xo, zo = 0, 0 local mH = charList[ character ].maxHealth local tweakFactor = 3.1 * mH / 100 if Ent.health > 0 then -- position green bar xo = ( ( mH / 2 ) - ( Ent.health / 2 ) ) / tweakFactor ScaleObject( gEnt[ gB ].obj, modf( Ent.health / mH * 100 ), 19, 100 ) xo, _, zo = U.Rotate3D( xo, 0, 0, 0, ang, 0 ) CollisionOff( gB ) SetPosition( gB, Ent.x - xo, Ent.y + 87.5, Ent.z - zo ) SetRotation( gB, 0, deg( ang ), 0 ) CollisionOn( gB ) Show( gB ) -- position red bar xo = ( -( mH / 2 ) + ( mH - Ent.health) / 2 ) / tweakFactor ScaleObject( gEnt[ rB ].obj, modf( ( mH - Ent.health ) / mH * 100 ), 19, 100 ) xo, _, zo = U.Rotate3D( xo, 0, 0, 0, ang, 0 ) CollisionOff( rB ) SetPosition( rB, Ent.x - xo, Ent.y + 87.5, Ent.z - zo ) SetRotation( rB, 0, deg( ang ), 0 ) CollisionOn( rB ) Show( rB ) else Hide( rB ) healthBars[ rB ].used = false Hide( gB ) healthBars[ gB ].used = false charList[ character ] = nil end end local RealFireWeapon = nil local function MyFireWeapon( e ) if aiBot[e] ~= ai_state_startreload and aiBot[e] ~= ai_state_reload and aiBot[e] ~= ai_state_reloadsettle and gEnt[e].plrvisible == 1 then AttachHealthBar( e ) end RealFireWeapon( e ) end local RealReleaseCover = nil local function MyReleaseCover( e ) AttachHealthBar( e, true ) RealReleaseCover( e ) end local lastTime = 0 function health_bar2_main(e) if RealFireWeapon == nil then if module_combatcore.fireweapon ~= nil then RealFireWeapon = module_combatcore.fireweapon module_combatcore.fireweapon = MyFireWeapon end end if RealReleaseCover == nil then if module_combatcore.releasecover ~= nil then RealReleaseCover = module_combatcore.releasecover module_combatcore.releasecover = MyReleaseCover end end local timeNow = g_Time if timeNow > lastTime then lastTime = timeNow controlEnt = e end if e ~= controlEnt then return end -- we've got this far so this entity is controlling all the action local PX, PZ = g_PlayerPosX, g_PlayerPosZ for k, v in pairs( charList ) do if v.red ~= nil and v.green ~= nil then if timeNow > v.timer then AttachHealthBar( k, true ) else repositionHealthBars( k, v.red, v.green, PX, PZ ) end end end end