./hud_healthbars.lua

  1. Hud_healthbars_status = {	 
  2. 	 
  3. 	--Hostiles Info 
  4. 	hostiles = { }, 
  5. 	 
  6. 	hostile_elements = { 
  7. 		grp_h = 0, fill_h = 0, border_h = 0, 
  8. 	}, 
  9. 	 
  10. 	--animations 
  11. 	anims = { }, 
  12. 	 
  13. 	anim_elements = { 
  14. 		healthbar_anim_h = 0,  
  15. 	}, 
  16. 	 
  17. 	handles = {}, 
  18. } 
  19.  
  20. USE_OBJECT_HANDLE_KEY = 0 
  21.  
  22. -- MUST MATCH autil_hud.h::healthbar_color 
  23. COLOR_RED				= 0 
  24. COLOR_BLUE				= 1 
  25. COLOR_NEUTRAL			= 2 
  26.  
  27. -- MUST MATCH autil_hud.h::healthbar_cash_format 
  28. HEALTHBAR_CASH_FORMAT_CASH		= 0 
  29. HEALTHBAR_CASH_FORMAT_FRAUD		= 1 
  30.  
  31. function hud_healthbars_init() 
  32. 	--mini healthbar 
  33. 	local h = -1 
  34. 	Hud_healthbars_status.hostile_elements.grp_h = vint_object_find("health_mini_grp") 
  35. 	vint_set_property(Hud_healthbars_status.hostile_elements.grp_h, "visible", false) 
  36. 	 
  37. 	h = Hud_healthbars_status.hostile_elements.grp_h 
  38. 	Hud_healthbars_status.hostile_elements.fill_h = vint_object_find("health_mini_fill", h) 
  39. 	Hud_healthbars_status.hostile_elements.border_h = vint_object_find("health_mini_border", h) 
  40. 	Hud_healthbars_status.hostile_elements.grit_h = vint_object_find("health_mini_grit", h) 
  41. 	Hud_healthbars_status.handles.mp_arrow_h = vint_object_find("mp_arrow", h) 
  42. 	Hud_healthbars_status.handles.mp_name_h = vint_object_find("mp_name", h) 
  43. 	Hud_healthbars_status.handles.overhead_cash_h = vint_object_find("overhead_cash", h) 
  44. 	Hud_healthbars_status.handles.race_pos_h = vint_object_find("race_pos", h) 
  45. 	 
  46. 	--large healthbar 
  47. 	Hud_healthbars_status.handles.health_large_grp = vint_object_find("health_large_grp") 
  48. 	vint_set_property(Hud_healthbars_status.handles.health_large_grp, "visible", false) 
  49. 	 
  50. 	h = Hud_healthbars_status.handles.health_large_grp 
  51. 	Hud_healthbars_status.handles.health_large_fill = vint_object_find("health_large_fill", h) 
  52. 	Hud_healthbars_status.handles.health_large_border = vint_object_find("health_large_border", h) 
  53. 	 
  54. 	--animation 
  55. 	Hud_healthbars_status.anim_elements.healthbar_anim_h = vint_object_find("health_mini_fade_out") 
  56. 	--large 
  57. 	Hud_healthbars_status.handles.health_large_fade_out = vint_object_find("health_large_fade_out") 
  58. 	--overhead_cash fade out 
  59. 	Hud_healthbars_status.handles.overhead_cash_fade_out = vint_object_find("overhead_cash_fade_out") 
  60. 	 
  61. 	--Pause animations 
  62. 	vint_set_property(Hud_healthbars_status.anim_elements.healthbar_anim_h, "is_paused", true) 
  63. 	--large 
  64. 	vint_set_property(Hud_healthbars_status.handles.health_large_fade_out, "is_paused", true) 
  65. 	--overhead_cash fade out 
  66. 	vint_set_property(Hud_healthbars_status.handles.overhead_cash_fade_out, "is_paused", true) 
  67. 		 
  68. 	--Health Bar Subscription 
  69. 	vint_datagroup_add_subscription("sr2_local_player_septic", "insert", "hud_healthbars_update") 
  70. 	vint_datagroup_add_subscription("sr2_local_player_septic", "update", "hud_healthbars_update") 
  71. 	vint_datagroup_add_subscription("sr2_local_player_septic", "remove", "hud_healthbars_update") 
  72. end 
  73.  
  74. function hud_healthbars_cleanup() 
  75. end 
  76.  
  77. function tint_element( handle, color ) 
  78. 	if color == COLOR_RED then 
  79. 		vint_set_property( handle, "tint", 0.71, 0, 0 ) 
  80. 	elseif color == COLOR_BLUE then 
  81. 		vint_set_property( handle, "tint", COLOR_SAINTS_PURPLE.R, COLOR_SAINTS_PURPLE.G, COLOR_SAINTS_PURPLE.B ) 
  82. 		--SR2 COOP Blue 
  83. 		--vint_set_property( handle,	"tint", 0.27, 0.51, 0.84 ) 
  84. 	else 
  85. 		vint_set_property( handle, "tint", 1, 1, 1 ) 
  86. 	end 
  87. end 
  88.  
  89. function tint_healthbar( hostile, color ) 
  90. 	tint_element( hostile.fill_h, color ) 
  91. 	tint_element( hostile.mp_name_h, color ) 
  92. 	tint_element( hostile.mp_arrow_h, color ) 
  93. 	tint_element( hostile.cash_h, color ) 
  94. 	tint_element( hostile.race_pos_h, color ) 
  95. end 
  96.  
  97. function hud_healthbars_update(di_h, event) 
  98.  
  99. 	local no_object_key, object_handle, screen_x, screen_y, z_depth, 
  100. 		distance, health_pct, name, overhead_cash, race_position, cash_format, 
  101. 		is_visible, color, is_big, health_full_alpha, mp_respawn_invulnerable, 
  102. 		show_bar, show_name, show_arrow, show_cash, show_race_pos = vint_dataitem_get(di_h) 
  103. 	 
  104. 	local decoded_name = vint_debug_decode_wide_string( name )	 
  105. 	 
  106. 	local key = ( no_object_key == USE_OBJECT_HANDLE_KEY ) and object_handle or no_object_key 
  107. 	 
  108. 	if event == "update" then 
  109. 	 
  110. 		--check to see if there is a hostile indicator for the key 
  111. 		if Hud_healthbars_status.hostiles[key] == nil then 
  112. 			 
  113. 			--Doesn't currently exist so create the hostile indicator 
  114. 			local grp_h = vint_object_clone(Hud_healthbars_status.hostile_elements.grp_h) 
  115. 			local fill_h = vint_object_find("health_mini_fill", grp_h) 
  116. 			local border_h = vint_object_find("health_mini_border", grp_h) 
  117. 			local grit_h = vint_object_find("health_mini_grit", grp_h) 
  118. 			local mp_name_h = vint_object_find("mp_name", grp_h) 
  119. 			local mp_arrow_h = vint_object_find("mp_arrow", grp_h) 
  120. 			local overhead_cash_h = vint_object_find("overhead_cash", grp_h) 
  121. 			local race_pos_h = vint_object_find("race_pos", grp_h) 
  122. 			 
  123. 			--Create the animation clones			 
  124. 			local anim_clone_0 = vint_object_clone(Hud_healthbars_status.anim_elements.healthbar_anim_h ) 
  125. 			local anim_0_fade = vint_object_find("mini_alpha_twn", anim_clone_0) 
  126. 			vint_set_property(anim_clone_0, "is_paused", true) 
  127. 			vint_set_property(anim_0_fade, "target_handle",	grp_h) 
  128. 			 
  129. 			 
  130. 			local invulnerable_anim_0 = 0  
  131. 			if MP_enabled == true then 
  132. 				 
  133. 				--Create invulnerable animation clones				 
  134. 				invulnerable_anim_0 = vint_object_clone(vint_object_find("health_anim_1")) 
  135. 				 
  136. 				---   0 = 191; 112; 0  ->> 0 = 255; 255; 147 
  137. 				local h = vint_object_find("health_flash_0_tween", invulnerable_anim_0) 
  138. 				vint_set_property(h, "start_value", 0.749, 0.439, 0) 
  139. 				vint_set_property(h, "end_value", 1, 1, 0.576) 
  140. 				vint_set_property(h, "loop_mode", "bounce") 
  141. 				vint_set_property(h, "target_handle", fill_h) 
  142. 				 
  143. 				--Destroy the other tween :) 
  144. 				local h = vint_object_find("health_flash_1_tween", invulnerable_anim_0) 
  145. 				vint_object_destroy(h) 
  146. 				 
  147. 				if mp_respawn_invulnerable == false then 
  148. 					vint_set_property(invulnerable_anim_0, "is_paused", true) 
  149. 				else 
  150. 					lua_play_anim(invulnerable_anim_0, 0) 
  151. 				end 
  152. 			end 
  153. 			 
  154. 			 
  155. 			--Create the animation clones			 
  156. 			local cash_fade_clone = vint_object_clone(Hud_healthbars_status.handles.overhead_cash_fade_out) 
  157. 			local cash_fade_1 = vint_object_find("over_cash_fade_1", cash_fade_clone) 
  158. 			local cash_fade_2 = vint_object_find("over_cash_fade_2", cash_fade_clone) 
  159. 			vint_set_property(cash_fade_clone, "is_paused", true) 
  160. 			vint_set_property(cash_fade_1, "target_handle",	overhead_cash_h) 
  161. 			vint_set_property(cash_fade_2, "target_handle",	overhead_cash_h) 
  162.  
  163. 			-- Assign the handles (these values shouldn't change after creation) 
  164. 			Hud_healthbars_status.hostiles[key] = { 
  165. 				--dont delete items from this table................ 
  166. 				grp_h = grp_h, 
  167. 				fill_h = fill_h, 
  168. 				border_h = border_h, 
  169. 				grit_h = grit_h, 
  170. 				health_pct = health_pct , 
  171. 				anim_clone_0 = anim_clone_0, 
  172. 				anim_0_fade = anim_0_fade, 
  173. 				invulnerable_anim_0 = invulnerable_anim_0, 
  174. 				mp_respawn_invulnerable = mp_respawn_invulnerable, 
  175. 				cash_fade_clone = cash_fade_clone, 
  176. 				cash_fade_1 = cash_fade_1, 
  177. 				cash_fade_2 = cash_fade_2, 
  178. 				distance = distance, 
  179. 				is_visible = is_visible, 
  180. 				screen_x = screen_x, 
  181. 				screen_y = screen_y, 
  182. 				z_depth = z_depth, 
  183. 				mp_arrow_h = mp_arrow_h, 
  184. 				mp_name_h = mp_name_h,  
  185. 				overhead_cash_h = overhead_cash_h, 
  186. 				overhead_cash = overhead_cash, 
  187. 				race_pos_h = race_pos_h, 
  188. 				race_position = race_position, 
  189. 			} 
  190. 			 
  191. 			-- Convenience local var 
  192. 			local hostile = Hud_healthbars_status.hostiles[key] 
  193. 						 
  194. 			if is_big then 
  195. 				-- Different fill graphics 
  196. 				vint_set_property(hostile.fill_h, "image", "ui_hud_meter_fill") 
  197. 				vint_set_property(hostile.border_h, "image", "ui_hud_meter_border") 
  198. 				vint_set_property(hostile.grit_h, "image", "ui_hud_meter_grit")	 
  199. 			end 
  200. 			 
  201. --			debug_print( "vint_healthbars", "Creating healthbar for " .. decoded_name .. " key: " .. key .. "\n" ) 
  202. 		end 
  203. 		 
  204. 		-- Convenience local var 
  205. 		local hostile = Hud_healthbars_status.hostiles[key] 
  206. 		 
  207. 		-- This is all debug calculations, so comment this out if you don't need to see it 
  208. 		do 
  209. 			local current_visible = vint_get_property( hostile.grp_h, "visible" ) 
  210. 			local current_bar = vint_get_property( hostile.fill_h, "visible" ) 
  211. 			local current_name = vint_get_property( hostile.mp_name_h, "visible" ) 
  212. 			local current_arrow = vint_get_property( hostile.mp_arrow_h, "visible" ) 
  213. 			local current_cash = vint_get_property( hostile.overhead_cash_h, "visible" ) 
  214. 			local current_race_pos = vint_get_property( hostile.race_pos_h, "visible" ) 
  215. 			 
  216. 			if current_visible ~= is_visible or 
  217. 				current_bar ~= show_bar or 
  218. 				current_name ~= show_name or 
  219. 				current_arrow ~= show_arrow or 
  220. 				current_cash ~= show_cash or 
  221. 				current_race_pos ~= show_race_pos then 
  222. 				 
  223. 				--[[ 
  224. 				debug_print( "vint_healthbars", "Updating healthbar for " .. decoded_name .. 
  225. 					" key: " .. key .. 
  226. 					( is_visible and " visible " or "" ) .. 
  227. 					( show_bar and " bar " or "" ) .. 
  228. 					( show_name and " name " or "" ) .. 
  229. 					( show_arrow and " arrow " or "" ) .. 
  230. 					( show_cash and " cash " or "" ) .. 
  231. 					( show_race_pos and " race_pos " or "" ) .. 
  232. 					"\n" )	]] 
  233. 			end 
  234. 		end 
  235. 		 
  236. 		if is_big then 
  237. 			--big bar 
  238. 			local w, h = element_get_actual_size(Hud_healthbars_status.handles.health_large_fill) 
  239. 			vint_set_property(hostile.fill_h, "source_se", w * health_pct, h) 
  240. 			vint_set_property(hostile.border_h, "source_se", (w * health_pct) + 4, h + 4) 
  241. 		else 
  242. 			--regular bar 
  243. 			local w, h = element_get_actual_size(Hud_healthbars_status.hostile_elements.fill_h) 
  244. 			vint_set_property(hostile.fill_h, "source_se", w * health_pct, h) 
  245. 			vint_set_property(hostile.border_h, "source_se", (w * health_pct) + 4, h + 4) 
  246. 		end 
  247. 		 
  248. 		--debug_print("vint", "health_full_alpha " .. var_to_string(health_full_alpha) .. "\n") 
  249. 		 
  250. 		if health_pct ~= hostile.health_pct and health_pct == 0 then 
  251. 			if health_full_alpha == false then 
  252. 				lua_play_anim(hostile.anim_clone_0, 0) 
  253. 			end 
  254. 		end 
  255. 		 
  256. 		if health_full_alpha == true then 
  257. 			vint_set_property(hostile.grp_h, "alpha", 1) 
  258. 		end 
  259. 		 
  260. 		-- Change visibility for: whole group 
  261. 		vint_set_property(hostile.grp_h, "visible", is_visible) 
  262. 		 
  263. 		-- ...bar 
  264. 		vint_set_property(hostile.fill_h, "visible", show_bar) 
  265. 		vint_set_property(hostile.border_h, "visible", show_bar) 
  266. 		vint_set_property(hostile.grit_h, "visible", show_bar) 
  267. 		 
  268. 		-- ...name 
  269. 		vint_set_property(hostile.mp_name_h, "visible", show_name) 
  270. 		 
  271. 		-- ...arrow 
  272. 		vint_set_property(hostile.mp_arrow_h, "visible", show_arrow) 
  273. 		 
  274. 		-- ...cash 
  275. 		if overhead_cash ~= 0 then 
  276. 			if cash_format == HEALTHBAR_CASH_FORMAT_FRAUD then 
  277. 				vint_set_property(hostile.overhead_cash_h, "text_tag", format_cash(overhead_cash) .. " {ACT_FRAUD_SCORE_UNIT}") 
  278. 			else 
  279. 				vint_set_property(hostile.overhead_cash_h, "text_tag", "+{GAME_CASH}" .. format_cash(overhead_cash)) 
  280. 			end 
  281. 		end 
  282. 		 
  283. 		if overhead_cash == 0 and hostile.overhead_cash ~= 0  then 
  284. 			lua_play_anim(hostile.cash_fade_clone) 
  285. 		else 
  286. 			vint_set_property(hostile.overhead_cash_h, "visible", show_cash) 
  287. 			vint_set_property(hostile.overhead_cash_h, "alpha", 1) 
  288. 			vint_set_property(hostile.case_fade_clone, "is_paused", true) 
  289. 		end 
  290. 		 
  291. 		hostile.overhead_cash = overhead_cash 
  292. 		 
  293. 		-- ...race_pos 
  294. 		vint_set_property(hostile.race_pos_h, "visible", show_race_pos) 
  295. 		if show_race_pos then 
  296. 			vint_set_property(hostile.race_pos_h, "image", "ingame_race_position_" .. race_position)  
  297. 		end 
  298. 		 
  299. 		-- Set the name 
  300. 		vint_set_property(hostile.mp_name_h, "text_tag", name) 
  301. 		 
  302. 		-- Set the color 
  303. 		tint_healthbar( hostile, color ) 
  304. 		 
  305. 		--Screen Position		 
  306. 		local x = screen_x 
  307. 		local y = screen_y 
  308. 		vint_set_property(hostile.grp_h, "anchor", x, y)	 
  309. 		 
  310. 		--Scale 
  311. 		local maxclamp = 0.40 
  312. 		local minclamp = 0.1 
  313. 		 
  314. 		local maxscale = 1 
  315. 		local minscale = 0.5 
  316. 		 
  317. 		--Clamp the distances 
  318. 		if distance <= minclamp then 
  319. 			distance = minclamp  
  320. 		elseif distance >= maxclamp then 
  321. 			distance = maxclamp 
  322. 		end 
  323.  
  324. 		local newdist = (distance - minclamp) 
  325. 		local ratio = 1 - (newdist / (maxclamp - minclamp)) 
  326. 		local scale = (ratio * (maxscale-minscale)) + minscale 
  327. 		 
  328. 		vint_set_property(hostile.grp_h, "scale", scale, scale) 
  329. 		--vint_set_property(hostile.grp_h, "alpha", scale) 
  330. 			 
  331. 		--Z Depth 
  332. 		if z_depth ~= hostile.z_depth then 
  333. 			vint_set_property(hostile.grp_h,  "depth", z_depth) 
  334. 		end 
  335. 		 
  336. 		--Invulnerability change 
  337. 		if MP_enabled == true then 
  338. 			if hostile.mp_respawn_invulnerable ~= mp_respawn_invulnerable then 
  339. 				if mp_respawn_invulnerable == false then 
  340. 					vint_set_property(hostile.invulnerable_anim_0, "is_paused", true) 
  341. 				else 
  342. 					lua_play_anim(hostile.invulnerable_anim_0, 0) 
  343. 				end 
  344. 			end 
  345. 			hostile.mp_respawn_invulnerable = mp_respawn_invulnerable 
  346. 		end 
  347. 		 
  348. 		--dont ever delete this......... 
  349. 		hostile.health_pct = health_pct 
  350. 		 
  351. 		-- For debugging purposes we also store the decoded name 
  352. 		hostile.decoded_name = decoded_name 
  353. 	end 
  354.  
  355. 		 
  356. 	--If you got a remove event, remove the clones	 
  357. 	if event == "remove" and Hud_healthbars_status.hostiles[key] ~= nil then 
  358. --		debug_print("vint_healthbars", "Remove healthbar for " .. decoded_name .. " key: " .. key .. "\n") 
  359. 		local hostile = Hud_healthbars_status.hostiles[key] 
  360. 		vint_object_destroy(hostile.grp_h) 
  361. 		vint_object_destroy(hostile.anim_clone_0) 
  362. 		vint_object_destroy(hostile.cash_fade_clone) 
  363. 		vint_object_destroy(hostile.invulnerable_anim_0) 
  364. 		Hud_healthbars_status.hostiles[key] = nil 
  365. 	end 
  366. 		 
  367. end 
  368.  
  369. -- Prints out info for every entry in the current hostiles list 
  370. -- 
  371. function hud_healthbars_debug_print() 
  372. --	debug_print( "vint_healthbars", "Current healthbar entries:\n" ) 
  373. 	 
  374. --	for key, entry in pairs(Hud_healthbars_status.hostiles) do 
  375. --		debug_print( "vint_healthbars", "\tkey: " .. key .. " name: " .. entry.decoded_name .. "\n" ) 
  376. --	end 
  377. end 
  378.  
  379.  
  380.  
  381.  
  382.