./object_indicator.lua

  1.  
  2. -- All of these defines exist also in data/lua_scripts/game_lib.lua 
  3. -- Object Indicator Assets 
  4. OI_ASSET_INVALID		= -1 
  5. OI_ASSET_KILL			= 0 
  6. OI_ASSET_DEFEND		= 1 
  7. OI_ASSET_USE			= 2 
  8. OI_ASSET_REVIVE		= 3 
  9. OI_ASSET_LOCATION		= 4 
  10. OI_ASSET_COOP			= 5 
  11. OI_ASSET_KILL_FULL	= 6 
  12. OI_ASSET_FINSHER		= 7 -- Not used in lua, just here so we stay in sync with C. 
  13. OI_ASSET_KILL_TIRES  = 8 -- Should not be used, but added so the assets after it stay in sync. 
  14. OI_ASSET_HITMAN		= 9 
  15. OI_ASSET_CHOPSHOP		= 10  
  16. OI_ASSET_ALL			= 11 -- Not used in lua, just here so we stay in sync with C. 
  17. OI_ASSET_SYNDICATE	= 12 
  18. OI_ASSET_GRENADE 		= 13 
  19.  
  20.  
  21. -- This must match the flags defined in object_indicators.h 
  22. OI_FLAG_NONE					= 0x00 
  23. OI_FLAG_STICKY					= 0x01 
  24. OI_FLAG_DISPLAY_DISTANCE	= 0x02 
  25. OI_FLAG_PULSE					= 0x04 
  26. OI_FLAG_FADE					= 0x08 
  27. OI_FLAG_PARTIAL_HIDE			= 0x10 
  28.  
  29. OI_OFFSET_Y = 30 
  30. Oi_offset_scale = 1.0 
  31.  
  32. Oi_elements = { 
  33. 	in_game_grp_h = -1, 
  34. 	pulse_anim_h = -1, 
  35. 	pulse_circle = -1, 
  36. } 
  37.  
  38. object_indicator = {} 
  39.  
  40. local Object_indicator_types = { 
  41. 	[OI_ASSET_KILL]			= 	{ img = "ui_target_icon_kill", 		color = COLOR_TARGET_KILL}, 
  42. 	[OI_ASSET_DEFEND]			=  { img = "ui_target_icon_defend",		color = COLOR_TARGET_DEFEND}, 
  43. 	[OI_ASSET_USE]				=  { img = "ui_target_icon_use", 		color = COLOR_TARGET_USE}, 
  44. 	[OI_ASSET_REVIVE]			=  { img = "ui_target_icon_revive", 	color = COLOR_TARGET_REVIVE,  depth = -20}, 
  45. 	[OI_ASSET_LOCATION]		= 	{ img = "ui_target_icon_location", 	color = COLOR_TARGET_LOCATION}, 
  46. 	[OI_ASSET_COOP]			= 	{ img = "ui_target_icon_coop", 		color = COLOR_TARGET_COOP}, 
  47. 	[OI_ASSET_KILL_FULL]		= 	{ img = "ui_target_icon_kill", 		color = COLOR_TARGET_KILL}, 
  48. 	[OI_ASSET_FINSHER]		= 	{ img = "ui_target_icon_kill", 		color = COLOR_TARGET_REVIVE},		-- Not used in lua, just here so we stay in sync with C. 
  49. 	[OI_ASSET_KILL_TIRES]	= 	{ img = "ui_target_icon_kill", 		color = COLOR_TARGET_KILL}, 
  50. 	[OI_ASSET_HITMAN]			= 	{ img = "ui_saintsbook_hitman", 		color = COLOR_TARGET_HITMAN}, 
  51. 	[OI_ASSET_CHOPSHOP]		=	{ img = "ui_saintsbook_vehicle", 	color = COLOR_TARGET_CHOPSHOP},	 
  52. 	[OI_ASSET_ALL]				= 	{ img = "ui_target_icon_kill", 		color = COLOR_TARGET_REVIVE},		-- Not used in lua, just here so we stay in sync with C. 
  53. 	[OI_ASSET_SYNDICATE]		= 	{ img = "ui_target_icon_syndicate",	color = COLOR_TARGET_SYNDICATE}, 
  54. 	[OI_ASSET_GRENADE]		= 	{ img = "ui_target_icon_grenade",	color = COLOR_TARGET_GRENADE}, 
  55. } 
  56.  
  57. --[[ 
  58. To use Lua implementation, change the value below to true.  Also, in 
  59. object_indicator.cpp, uncomment the line #define OBJECT_INDICATOR_USE_LUA.   
  60. If you tweak the Lua implementation, be sure to ask a programmer to make the  
  61. corresponding tweak to the C implementation as well, and don't check the system 
  62. in using the Lua implementation. 
  63. --]] 
  64. Object_indicator_use_lua = false 
  65.  
  66. function object_indicator_init() 
  67. 	if Object_indicator_use_lua then 
  68. 		-- Lua implementation 
  69. 	 
  70. 		if vint_is_std_res() then 
  71. 			Oi_offset_scale = Oi_offset_scale * .667 
  72. 		else 
  73. 			Oi_offset_scale = Oi_offset_scale * .667 
  74. 		end 
  75. 		 
  76. 		--Find and store elements 
  77. 		Oi_elements.in_game_grp_h = vint_object_find("oi_grp") 
  78. 		Oi_elements.pulse_anim_h = vint_object_find("oi_pulse_anim") 
  79. 		Oi_elements.pulse_circle = vint_object_find("pulse_circle") 
  80. 		vint_set_property(Oi_elements.pulse_circle, "visible", false) 
  81. 		 
  82. 		--Whole title group... 
  83. 		Oi_elements.title_grp_h = vint_object_find("title_grp") 
  84. 		vint_set_property(Oi_elements.title_grp_h, "visible", false) 
  85. 				 
  86. 		--Subscribe to data items 
  87. 		vint_datagroup_add_subscription("object_indicator", "insert", "object_indicator_update") 
  88. 		vint_datagroup_add_subscription("object_indicator", "update", "object_indicator_update") 
  89. 		vint_datagroup_add_subscription("object_indicator", "remove", "object_indicator_remove") 
  90. 	else 
  91. 		-- C implementation 
  92. 		object_indicator_init_lua( ) 
  93. 	end 
  94. end 
  95.  
  96. -- object_indicator_update: called from game via datagroup subscription 
  97. function object_indicator_update(di_h) 
  98. 	--[[ 
  99. 		Object Indicator datagroup members: 
  100. 		VINT_PROP_TYPE_VECTOR2F    	- Screen position 
  101. 		VINT_PROP_TYPE_INT            - Asset 
  102. 		VINT_PROP_TYPE_FLOAT          - Rotation 
  103. 		VINT_PROP_TYPE_FLOAT          - Health 
  104. 		VINT_PROP_TYPE_INT            - Distance (in meters) 
  105. 		VINT_PROP_TYPE_FLOAT          - Viewing Distance (for scaling indicator) 
  106. 		VINT_PROP_TYPE_INT            - Flags 
  107. 	]] 
  108. 	local screen_pos_x, screen_pos_y, asset_id, rotation, health, dist, view_dist, flags, alpha, partial_hide, title_crc, body_crc  = vint_dataitem_get(di_h) 
  109. 								 
  110. 	--title crc 
  111. 	-- default is 0, when text is 0 
  112. 	-- 0 after population it will fade out and cleanup. 
  113. 	-- 
  114.  
  115. 	 
  116. 	--If this item has't been created, make a new one and set it up 
  117. 	if object_indicator[di_h] == nil then 
  118. 		--Reset text color 
  119. 		--oi_text_color_change(text_intensity) 
  120. 		 
  121. 		--Clone New item 
  122. 		local grp_h = vint_object_clone(Oi_elements.in_game_grp_h) 
  123. 		local sub_grp_h = vint_object_find("oi_sub_grp", grp_h) 
  124. 		local arrow_grp_h = vint_object_find("oi_arrow_grp", grp_h) 
  125. 		local oi_icon = vint_object_find("oi_icon", grp_h) 
  126. 		local oi_arrow = vint_object_find("oi_arrow", grp_h) 
  127. 		local oi_arrow_shadow = vint_object_find("oi_arrow_shadow", grp_h) 
  128. 		local oi_text = vint_object_find("oi_text", grp_h) 
  129. 		local oi_health = vint_object_find("oi_health", grp_h) 
  130. 		local oi_health_shadow = vint_object_find("oi_health_shadow", grp_h) 
  131. 		local anim_h = 0 
  132.  
  133. 		vint_set_property(grp_h, "visible", true) 
  134. 		vint_set_property(oi_arrow, "rotation", rotation) 
  135. 		vint_set_property(oi_arrow_shadow, "rotation", rotation) 
  136.  
  137. 		if floor(flags / OI_FLAG_PULSE) % 2 == 1 then 
  138. 			local pulse_circle_h = vint_object_find("pulse_circle", grp_h) 
  139. 			vint_set_property(pulse_circle_h, "visible", true) 
  140. 			anim_h = vint_object_clone(Oi_elements.pulse_anim_h) 
  141. 			vint_set_property(anim_h, "target_handle", sub_grp_h) 
  142. 			lua_play_anim(anim_h, 0) 
  143. 		end 
  144.  
  145. 		if floor(flags / OI_FLAG_DISPLAY_DISTANCE) % 2 == 1 then 
  146. 			vint_set_property(oi_text, "visible", true) 
  147. 		else 
  148. 			vint_set_property(oi_text, "visible", false) 
  149. 		end 
  150.  
  151. 		--Special case hiding if a grenade is tossed.... 
  152. 		if asset_id == OI_ASSET_GRENADE then 
  153. 			vint_set_property(Oi_elements.pulse_circle, "visible", false) 
  154. 			vint_set_property(oi_health, "visible", false) 
  155. 			vint_set_property(oi_health_shadow, "visible", false) 
  156. 		end 
  157. 		 
  158. 		--vint_set_property(grp_h, "tint", rand_float(0.0, 1.0), rand_float(0.0, 1.0), rand_float(0.0, 1.0)) 
  159. 		--vint_set_property(oi_health, "end_angle", rand_float(0.0, 6.28)) 
  160. 		 
  161. 		--Grab info from our indicator type table... 
  162. 		local icon_img		=		Object_indicator_types[asset_id].img 
  163. 		local color	 		=		Object_indicator_types[asset_id].color 
  164. 		local depth 		= 		Object_indicator_types[asset_id].depth 
  165. 		 
  166. 		-- Set image and colors... 
  167. 		vint_set_property(oi_icon , "image", icon_img) 
  168. 		vint_set_property(arrow_grp_h, 	"tint", color.R, color.G, color.B)		 
  169. 		vint_set_property(sub_grp_h, 		"tint", color.R, color.G, color.B)		 
  170. 		vint_set_property(oi_text, 		"tint", color.R, color.G, color.B)		 
  171. 		 
  172. 		--Set depth if specified by indicator type... 
  173. 		if depth ~= nil then 
  174. 			vint_set_property(grp_h, "depth", -20) -- above all others 
  175. 		end 
  176. 			 
  177. 		--Store values and handles to process later 
  178. 		object_indicator[di_h] = { 
  179. 			di_h = di_h, 
  180. 			grp_h = grp_h, 
  181. 			sub_grp_h = sub_grp_h, 
  182. 			arrow_grp_h = arrow_grp_h, 
  183. 			oi_arrow_shadow = oi_arrow_shadow, 
  184. 			anim_h = anim_h, 
  185. 			oi_text = oi_text, 
  186. 			oi_health = oi_health, 
  187. 			oi_health_shadow = oi_health_shadow, 
  188. 			text_offset_y = 0, 
  189. 			title_crc = title_crc, 
  190. 			body_crc = body_crc, 
  191. 		} 
  192. 		 
  193. 		if title_crc ~= 0 then 
  194. 			local color = Object_indicator_types[asset_id].color 
  195. 			object_indicator_title_create(di_h, title_crc, body_crc, color) 
  196. 		end	 
  197. 	end 
  198.  
  199. 	local oi_item = object_indicator[di_h] 
  200. 	local grp_h = oi_item.grp_h 
  201. 	local oi_arrow = vint_object_find("oi_arrow", grp_h) 
  202. 	local oi_arrow_shadow = oi_item.oi_arrow_shadow 
  203. 	local sub_grp_h = oi_item.sub_grp_h  
  204. 	local arrow_grp_h = oi_item.arrow_grp_h 
  205. 	local oi_text = oi_item.oi_text 
  206. 	local oi_health = oi_item.oi_health 
  207. 	local oi_health_shadow = oi_item.oi_health_shadow 
  208.  
  209. 	--Calculate and set the position 
  210. 	local scale = .9 
  211. 	if sqrt(view_dist) > 3 then 
  212. 		scale = 1.2 / sqrt(view_dist) + (scale - 0.4) 
  213. 	end 
  214. 	scale = scale * 0.72 
  215.  
  216. 	vint_set_property(sub_grp_h,   "scale", scale, scale) 
  217. 	vint_set_property(arrow_grp_h, "scale", scale, scale) 
  218. 	local size_x, size_y = element_get_actual_size(oi_arrow) 
  219. 	local offset_x, offset_y = vint_get_property(oi_arrow, "offset") 
  220. 	local offset_distance = (size_y - offset_y - OI_OFFSET_Y) * Oi_offset_scale 
  221. 	 
  222. 	screen_pos_x = screen_pos_x - (sin(rotation) * offset_distance) 
  223. 	screen_pos_y = screen_pos_y + (cos(rotation) * offset_distance) 
  224. 	 
  225. 	vint_set_property(grp_h, "anchor", screen_pos_x, screen_pos_y) 
  226. 	vint_set_property(grp_h, "alpha", alpha) 
  227.  
  228. 	--set arrow rotation 
  229. 	vint_set_property(oi_arrow, "rotation", rotation) 
  230. 	vint_set_property(oi_arrow_shadow, "rotation", rotation) 
  231.  
  232.  
  233. 	if partial_hide == true then 
  234. 		vint_set_property(oi_text,   "visible", false) 
  235. 		vint_set_property(sub_grp_h, "visible", false) 
  236. 	else 
  237. 		vint_set_property(sub_grp_h, "visible", true) 
  238.  
  239. 		if floor(flags / OI_FLAG_DISPLAY_DISTANCE) % 2 == 1 then 
  240. 			vint_set_property(oi_text, "visible", true) 
  241. 			vint_set_property(oi_text, "text_tag", format_distance(dist)) 
  242. 			local c_w, c_h = element_get_actual_size(oi_text)  
  243. 			local text_width_half = c_w * .5 
  244. 			local text_height_half = c_h * .5 
  245. 			 
  246. 			--40 is the base size including padding of the oi circle. 
  247. 			local text_offset_x = -sin(rotation) * ((40 * scale) + text_width_half) 
  248. 			local text_offset_y =  cos(rotation) * ((40 * scale) + text_height_half) 
  249. 			vint_set_property(object_indicator[di_h].oi_text, "anchor", text_offset_x, text_offset_y) 
  250. 			object_indicator[di_h].text_offset_x = text_offset_x  
  251. 			object_indicator[di_h].text_offset_y = text_offset_y - text_height_half 
  252. 		end 
  253.  
  254. 		if health == 1 then 
  255. 			vint_set_property(oi_health, "end_angle", 6.283) 
  256. 			vint_set_property(oi_health_shadow, "end_angle", 0 ) 
  257. 		else 
  258. 			vint_set_property(oi_health, "end_angle", health * 6.28) 
  259. 			vint_set_property(oi_health_shadow, "end_angle", 6.28 - (health * 6.28) ) 
  260. 			 
  261. 		end 
  262. 	end 
  263. 	 
  264. 	 
  265. 	if title_crc ~= oi_item.title_crc or title_crc ~= 0 then 
  266. 		object_indicator_title_update(di_h,screen_pos_x, screen_pos_y, scale) 
  267. 		oi_item.title_crc = title_crc 
  268.  
  269. 		-- begin fading out when the crc is back to 0 
  270. 		if title_crc == 0 then 
  271. 			local title_obj = Object_indicator_titles[di_h] 
  272. 			lua_play_anim(title_obj.fade_out_anim_h) 
  273. 		end 
  274. 	end 
  275. end 
  276.  
  277. function object_indicator_remove(di_h) 
  278. 	--Destroy objects, animation and values 
  279. 	if object_indicator[di_h] ~= nil then 
  280. 		--Destroy animation 
  281. 		vint_object_destroy(object_indicator[di_h].anim_h) 
  282. 		--Destroy group object 
  283. 		vint_object_destroy(object_indicator[di_h].grp_h) 
  284. 		--Destroy stored values 
  285. 		object_indicator[di_h] = nil	 
  286. 		 
  287. 		--Attempt to Destroy the indicator title if exists... 
  288. 		object_indicator_title_destroy(di_h) 
  289. 	end 
  290. end 
  291.  
  292.  
  293. Object_indicator_titles = {} 
  294.  
  295. ------------------------------------------------------------------------------- 
  296. -- Creates table, clone and reference objects 
  297. -- 
  298. function object_indicator_title_create(di_h, title_crc, body_crc, color) 
  299. 	--create table, clone and reference objects 
  300. 	local title_obj = {} 
  301. 	local title_grp_h 		= vint_object_clone(Oi_elements.title_grp_h) 
  302. 	vint_set_property(title_grp_h, "visible", true) 
  303. 	 
  304. 	title_obj.title_grp_h 	= title_grp_h 
  305. 	title_obj.title_h 		= vint_object_find("title_txt", 	title_grp_h) 
  306. 	title_obj.body_h 			= vint_object_find("body_txt", 	title_grp_h) 
  307. 	 
  308. 	--get base scale for standard def stuff. 
  309. 	local base_scale_grp_h 	= vint_object_find("title_scale_grp", title_grp_h) 
  310. 	title_obj.base_scale		= vint_get_property(base_scale_grp_h, "scale") 
  311. 	 
  312. 	 
  313. 	--Animation 
  314. 	title_obj.fade_out_anim_h 	= vint_object_clone(vint_object_find("title_fade_out_anim")) 
  315. 	title_obj.fade_out_twn_h 	= vint_object_find("title_twn", title_obj.fade_out_anim_h) 
  316. 	vint_set_property(title_obj.fade_out_anim_h, "target_handle", title_grp_h) 
  317. 	vint_set_property(title_obj.fade_out_twn_h, "end_event", "object_indicator_title_fade_cb") 
  318. 	 
  319. 	--Set titles of object 
  320. 	vint_set_property(title_obj.title_h, 	"text_tag_crc", title_crc) 
  321. 	vint_set_property(title_obj.body_h, 	"text_tag_crc", body_crc) 
  322. 	 
  323. 	--Set color of title 
  324. 	vint_set_property(title_obj.title_h, 	"tint", color.R, color.G, color.B) 
  325. 	 
  326. 	--Set parent to the object... 
  327. 	vint_object_set_parent(title_grp_h, object_indicator[di_h].grp_h) 
  328.  
  329. 	--Vertically align elements so they are spaced properly. 
  330. 	local elements = { 
  331. 				{	h = title_obj.title_h, type = VINT_OBJECT_TEXT, space = 0}, 
  332. 				{	h = title_obj.body_h, type = VINT_OBJECT_TEXT, space = 2}, 
  333. 			} 
  334. 	local width, height = vint_align_elements(elements) 
  335. 	title_obj.width 	= width 
  336. 	title_obj.height 	= height 
  337. 	 
  338. 	--TODO: create these objects in script only. 
  339. 	Object_indicator_titles[di_h] = title_obj 
  340. end 
  341.  
  342. ------------------------------------------------------------------------------- 
  343. -- Destroys title object 
  344. -- 
  345. function object_indicator_title_destroy(di_h) 
  346. 	if Object_indicator_titles[di_h] then 
  347. 		vint_object_destroy(Object_indicator_titles[di_h].title_h) 
  348. 		vint_object_destroy(Object_indicator_titles[di_h].body_h) 
  349. 		vint_object_destroy(Object_indicator_titles[di_h].fade_out_anim_h) 
  350. 		Object_indicator_titles[di_h] = nil 
  351. 	end 
  352. end 
  353.  
  354. ------------------------------------------------------------------------------- 
  355. -- Updates title object 
  356. -- 
  357. function object_indicator_title_update(di_h, x, y, scale) 
  358. 	--get references to objects, store them as locals 
  359. 	local title_obj = Object_indicator_titles[di_h] 
  360. 	if title_obj == nil then 
  361. 		return 
  362. 	end 
  363. 	 
  364. 	local title_grp_h = title_obj.title_grp_h 	 
  365. 	local title_h 		= title_obj.title_h 		 
  366. 	local body_h 		= title_obj.body_h 			 
  367. 	 
  368. 	--Adjust scale value to apply to text. 
  369. 	scale = scale * 1.8 
  370. 	if scale < 0.85 then 
  371. 		scale = 0.85 
  372. 	end 
  373. 	 
  374. 	--Get our alignement values. 
  375. 	local text_height = title_obj.height + 10 
  376. 	local text_width = title_obj.width * Oi_offset_scale * scale 
  377. 	local text_offset_y	= object_indicator[di_h].text_offset_y 
  378. 	 
  379. 	--Make sure we don't drop below the indicator. 
  380. 	if text_offset_y > -15 then 
  381. 		text_offset_y = -15 
  382. 	end 
  383. 	 
  384. 	--Right or left aligned 
  385. 	local is_right_aligned = false 
  386.  
  387. 	local right_side_of_text_box = x + text_width 
  388. 	if right_side_of_text_box > Safe_frame_e then 
  389. 		--need to right align 
  390. 		is_right_aligned = true 
  391. 	end 
  392. 	 
  393. 	local x = -30 
  394. 	if is_right_aligned then 
  395. 		--Align right 
  396. 		vint_set_property(title_h, "auto_offset", "ne") 
  397. 		vint_set_property(body_h, 	"auto_offset", "ne") 
  398. 		vint_set_property(title_h, "horz_align", "right") 
  399. 		vint_set_property(body_h, 	"horz_align", "right") 
  400. 		x = 30 
  401. 	else 
  402. 		--Align left 
  403. 		vint_set_property(title_h, "auto_offset", "nw") 
  404. 		vint_set_property(body_h, 	"auto_offset", "nw") 
  405. 		vint_set_property(title_h, "horz_align", "left") 
  406. 		vint_set_property(body_h, 	"horz_align", "left") 
  407. 	end 
  408. 	 
  409. 	--Set position and scale on screen 
  410. 	local y = text_offset_y - ((text_height * scale) * title_obj.base_scale) 
  411. 	 
  412. 	--scale positioning based on our base scale 
  413. 	x = x * title_obj.base_scale 
  414. 	 
  415. 	vint_set_property(title_grp_h, "anchor", x, y) 
  416. 	vint_set_property(title_grp_h, "scale", scale, scale) 
  417. end 
  418.  
  419. ------------------------------------------------------------------------------- 
  420. -- Cleanup object when we are done with it... this is called by tween event 
  421. --  
  422. function object_indicator_title_fade_cb(twn_h) 
  423. 	--find di_h based on tween handle... 
  424. 	local di_h = 0 
  425. 	for idx, val in pairs(Object_indicator_titles) do 
  426. 		if twn_h == val.fade_out_twn_h then 
  427. 			di_h = idx 
  428. 		end 
  429. 	end 
  430. 	 
  431. 	--Exit if we didn't find it... 
  432. 	if di_h == 0 then 
  433. 		return 
  434. 	end 
  435. 	 
  436. 	--Destroy the object... 
  437. 	object_indicator_title_destroy(di_h) 
  438. end