sr2lua/liquor_store.lua

  1. ----------------------- 
  2. -- FOOD/LIQUOR STORE -- 
  3. ----------------------- 
  4. --[[	Though everything is prefixed as liquor store,  
  5. 		this is the store for all food/drug/liquor 		]]-- 
  6.  
  7. -- "global" variables 
  8. Liquor_store_data = { } 
  9. Liquor_store_footer = { } 
  10. Liquor_store_current_selection = 0 
  11.  
  12. LIQUOR_STORE_TYPE_FOOD = 0 
  13. LIQUOR_STORE_TYPE_DRUG = 1 
  14.  
  15. Liquor_store_player_cash = -1 
  16.  
  17. Liquor_store_is_opening = true 
  18.  
  19. ---------------------- 
  20. --	SYSTEM FUNCTIONS -- 
  21. ---------------------- 
  22. --[ INITIALIZE AND SHUTDOWN ]-- 
  23. ------------------------------- 
  24. --	Initialize the menu 
  25. function liquor_store_init() 
  26. 	vint_dataitem_add_subscription("sr2_local_player_status_infrequent", "update", "liquor_store_player_cash_update") 
  27. 	menu_store_use_hud(true) 
  28. 	menu_store_init(false) 
  29. 	hud_fade_out() 
  30. 	hud_element_fade("RADIAL", 2) 
  31. 	menu_init() 
  32. 	menu_show(Liquor_store_menu) 
  33. end 
  34.  
  35. -- Shutdown and cleanup the menu 
  36. function liquor_store_cleanup() 
  37. 	hud_fade_in() 
  38. 	hud_inventory_food_hide()	 
  39. end 
  40.  
  41. function liquor_store_player_cash_update(di_h) 
  42. 	local cash = vint_dataitem_get(di_h) 
  43. 	Liquor_store_player_cash = cash 
  44. end 
  45.  
  46.  
  47. --[ EXIT ]-- 
  48. ------------ 
  49. function liquor_store_exit(menu_data) 
  50. 	local body = "FOOD_STORE_EXIT_PROMPT" 
  51. 	local heading = "MENU_TITLE_WARNING" 
  52. 	 
  53. 	dialog_box_confirmation(heading, body, "liquor_store_exit_confirm") 
  54. end 
  55. function liquor_store_exit_confirm(result, action) 
  56. 	if action ~= DIALOG_ACTION_CLOSE then 
  57. 		return 
  58. 	end 
  59. 	 
  60. 	if result == 0 then 
  61. 		menu_close(liquor_store_exit_final) 
  62. 	end 
  63. end 
  64. function liquor_store_exit_final() 
  65. 	vint_document_unload(vint_document_find("liquor_store"))	 
  66. end 
  67. -------------------------- 
  68. -- FOOTER FUNCTIONALITY -- 
  69. -------------------------- 
  70. function liquor_store_build_footer(menu_data) 
  71. 	local grp = vint_object_clone(vint_object_find("liquor_store_footer"), Menu_option_labels.control_parent) 
  72. 	vint_set_property(grp, "visible", true) 
  73.  
  74. 	if menu_data.footer ~= nil and menu_data.footer.footer_grp ~= nil and menu_data.footer.footer_grp ~= 0 then 
  75. 		vint_object_destroy(menu_data.footer.footer_grp) 
  76. 	end 
  77.  
  78. 	menu_data.footer = { } 
  79. 	menu_data.footer.footer_grp = grp 
  80. 	 
  81. 	Liquor_store_footer.label_h = vint_object_find("price_label", grp) 
  82. 	Liquor_store_footer.price_h = vint_object_find("price_amount", grp) 
  83. 	Liquor_store_footer.benefit_h = vint_object_find("benefit_label", grp) 
  84. 	 
  85. 	vint_set_property(Liquor_store_footer.price_h , "visible", true) 
  86. 	 
  87. 	--Build second horizontal rule 
  88. 	local bg_h = vint_object_find("menu_bg_header_hr", Menu_option_labels.control_parent) 
  89. 	local bg_h = vint_object_clone(bg_h, grp) 
  90. 	vint_object_rename(bg_h, "footer_line") 
  91. 	vint_set_property(bg_h, "anchor", -17, 48) 
  92. 	 
  93. 	Liquor_store_footer.line_h = bg_h 
  94. 	 
  95. 	--Attach button tips to this footer_grp 
  96. 	btn_tips_footer_build(grp, -5, 57) 
  97. 	 
  98. 	if Liquor_store_data.type == LIQUOR_STORE_TYPE_FOOD then 
  99. 		vint_set_property(Liquor_store_footer.benefit_h, "visible", true) 
  100. 	else  
  101. 		vint_set_property(Liquor_store_footer.benefit_h, "visible", false) 
  102. 	end 
  103. end 
  104.  
  105. function liquor_store_update_footer(idx) 
  106. 	local swatch = Liquor_store_menu[0].swatches[idx] 
  107. 	if Liquor_store_player_cash < swatch.price then 
  108. 		vint_set_property(Liquor_store_footer.price_h, "tint", 1, 0, 0) 
  109. 	else 
  110. 		vint_set_property(Liquor_store_footer.price_h, "tint", 0.88, 0.749, 0.05) 
  111. 	end 
  112.  
  113. 	vint_set_property(Liquor_store_footer.price_h, "text_tag", "$" .. format_cash(swatch.price)) 
  114. 	 
  115. 	if Liquor_store_data.type == LIQUOR_STORE_TYPE_FOOD then 
  116. 		local values = { [0] = swatch.benefit .. "%%"} 
  117. 		local tag = vint_insert_values_in_string("FOOD_STORE_BENEFIT", values) 
  118. 		vint_set_property(Liquor_store_footer.benefit_h, "text_tag", tag) 
  119. 	end 
  120. 	vint_set_property(Liquor_store_footer.label_h, "text_tag_crc", swatch.label_crc) 
  121. 	 
  122. 	local slot_highlight = liquor_store_check_slots(swatch.id, true) 
  123. 		if slot_highlight == -1 then 
  124. 			slot_highlight = nil 
  125. 		end 
  126. 	 
  127. 	if Liquor_store_is_opening == true then 
  128. 		thread_new("liquor_store_delayed_food_highlight", slot_highlight) 
  129. 	else 
  130. 		hud_inventory_food_highlight_slot(slot_highlight)		 
  131. 	end 
  132. end 
  133.  
  134. function liquor_store_delayed_food_highlight(slot_highlight) 
  135. 	delay(.2) 
  136. 	Liquor_store_is_opening = false 
  137. 	hud_inventory_food_show() 
  138. 	hud_inventory_food_highlight_slot(slot_highlight) 
  139. end 
  140.  
  141. function liquor_store_finalize_footer(menu_data) 
  142. 	vint_set_property(Liquor_store_footer.price_h, "anchor", menu_data.menu_width - 10, 0) 
  143. 	 
  144. 	--Scale extra line for button tips 
  145. 	local source_x, source_y = vint_get_property(Liquor_store_footer.line_h, "source_se") 
  146. 	vint_set_property(Liquor_store_footer.line_h, "source_se", menu_data.menu_width + 10, source_y) 
  147. end 
  148.  
  149. function	liquor_store_release(menu_data) 
  150. 	if menu_data.footer ~= nil then 
  151. 		vint_set_property(menu_data.footer.footer_grp, "visible", false) 
  152. 		vint_object_destroy(menu_data.footer.footer_grp) 
  153. 	end 
  154. 	menu_grid_release(menu_data[0]) 
  155. end 
  156.  
  157. ------------------------ 
  158. --	MENU FUNCTIONALITY -- 
  159. ------------------------ 
  160. function liquor_store_init_store(menu_data) 
  161. 	local menu_item = menu_data[0] 
  162. 	 
  163. 	menu_item.swatches = { num_swatches = 0 } 
  164. 	menu_item.cur_idx = 0 
  165. 	 
  166. 	vint_dataresponder_request("liquor_store_setup_menu", "liquor_store_get_menu_data", 0) 
  167. 	vint_dataresponder_request("liquor_store_populate_menu", "liquor_store_add_items", 0) 
  168. 	 
  169. 	menu_data.header_label_crc = Liquor_store_data.title_crc 
  170. 	local master_swatch = vint_object_find("swatch_general", nil, MENU_BASE_DOC_HANDLE) 
  171. 	 
  172. 	--Event Tracking  
  173. 	if menu_data.store_type == LIQUOR_STORE_TYPE_FOOD then 
  174. 		event_tracking_interface_enter("Food Store") 
  175. 	else 
  176. 		event_tracking_interface_enter("Liqour Store") 
  177. 	end 
  178. 	 
  179. 	liquor_store_sort_swatches(menu_item.swatches) 
  180. 	menu_grid_show(menu_data, menu_item, master_swatch)	 
  181. 	liquor_store_build_footer(menu_data) 
  182. 	liquor_store_update_footer(0) 
  183. end 
  184.  
  185. function liquor_store_sort_swatches(swatches) 
  186. 	local temp 
  187. 	local flag = false 
  188. 	 
  189. 	while flag == false do 
  190. 		flag = true 
  191. 		for i = 0, swatches.num_swatches - 2 do 
  192. 			if swatches[i].price > swatches[i + 1].price then 
  193. 				temp = swatches[i] 
  194. 				swatches[i] = swatches[i + 1] 
  195. 				swatches[i + 1] = temp 
  196. 				flag = false 
  197. 				i = swatches.num_swatches 
  198. 			end 
  199. 		end 
  200. 	end 
  201. end 
  202.  
  203. function liquor_store_update_swatch(swatch) 
  204. 	local icon_h = vint_object_find("icon", swatch.swatch_h) 
  205. 	local icon_highlight_h = vint_object_find("icon_highlight", swatch.swatch_h) 
  206. 	local icon_select_h = vint_object_find("icon_select", swatch.swatch_h) 
  207. 	vint_set_property(icon_h, "image", swatch.bitmap_name) 
  208. 	vint_set_property(icon_highlight_h, "image", swatch.bitmap_name) 
  209. 	vint_set_property(icon_select_h, "image", swatch.bitmap_name) 
  210. end 
  211.  
  212. function liquor_store_get_menu_data(store_title_crc, store_type) 
  213. 	Liquor_store_data = { title_crc = store_title_crc, type = store_type } 
  214. end 
  215.  
  216. function liquor_store_add_items(display_name, display_name_crc, bitmap_name, price, benefit) 
  217. 	local swatches = Liquor_store_menu[0].swatches 
  218. 	 
  219. 	swatches[swatches.num_swatches] =  
  220. 	{ label_str = display_name, label_crc = display_name_crc, bitmap_name = bitmap_name, price = price, benefit = benefit, id = swatches.num_swatches } 
  221. 	swatches.num_swatches = swatches.num_swatches + 1	 
  222. end 
  223.  
  224. function liquor_store_select_item(menu_label, menu_data) 
  225. 	local swatches = menu_data.swatches 
  226. 	 
  227. 	if Liquor_store_current_selection < swatches.num_swatches then 
  228. 		local body = "" 
  229. 		local heading = "" 
  230. 		 
  231. 		if Liquor_store_player_cash > swatches[Liquor_store_current_selection].price then 
  232. 			local inv_full, drug_name, bitmap_name = liquor_store_check_slots(swatches[Liquor_store_current_selection].id) 
  233. 			if inv_full == 1 then 
  234. 				local price = format_cash(swatches[Liquor_store_current_selection].price)  
  235. 				local insert_values = {  
  236. 					[0] = drug_name,  
  237. 					[1] = bitmap_name,  
  238. 					[2] = swatches[Liquor_store_current_selection].label_crc,  
  239. 					[3] = swatches[Liquor_store_current_selection].bitmap_name,  
  240. 					[4] = price, 
  241. 				} 
  242. 				body = vint_insert_values_in_string("FOOD_STORE_REPLACE_DRUG_PROMPT", insert_values) 
  243. 				heading = "MENU_TITLE_CONFIRM" 
  244. 				dialog_box_confirmation(heading, body, "liquor_store_confirm_selection") 
  245. 			elseif inv_full == 0 then 
  246. 				local price = format_cash(swatches[Liquor_store_current_selection].price) 
  247. 				local insert_values = {  
  248. 					[0] = swatches[Liquor_store_current_selection].label_crc,  
  249. 					[1] = swatches[Liquor_store_current_selection].bitmap_name,  
  250. 					[2] = price,  
  251. 				}  
  252. 				body = vint_insert_values_in_string("FOOD_STORE_PURCHASE_DRUG_PROMPT", insert_values) 
  253. 				heading = "MENU_TITLE_CONFIRM" 
  254. 				dialog_box_confirmation(heading, body, "liquor_store_confirm_selection") 
  255. 			else 
  256. 				heading = "MENU_TITLE_NOTICE" 
  257. 				body = "STORE_CANNOT_CARRY_ANYMORE_HEADING" 
  258. 				dialog_box_message(heading, body) 
  259. 			end 
  260. 		else 
  261. 			heading = "MENU_TITLE_NOTICE" 
  262. 			body = "HUD_SHOP_INSUFFICIENT_FUNDS" 
  263. 			dialog_box_message(heading, body) 
  264. 		end 
  265. 	end 
  266. end 
  267.  
  268. function liquor_store_confirm_selection(result, action) 
  269. 	if action ~= DIALOG_ACTION_CLOSE then 
  270. 		return 
  271. 	end 
  272. 	 
  273. 	if result == 0 then 
  274. 		liquor_store_make_purchase(Liquor_store_menu[0].swatches[Liquor_store_current_selection].id) 
  275. 		audio_play(Menu_sound_purchase) 
  276. 		liquor_store_update_footer(Liquor_store_current_selection) 
  277. 	end 
  278. end 
  279.  
  280. function liquor_store_nav_item(menu_label, menu_data) 
  281. 	local swatches = menu_data.swatches 
  282. 	local idx = menu_data.cur_row * menu_data.num_cols + menu_data.cur_col 
  283. 	 
  284. 	if idx < swatches.num_swatches then 
  285. 		Liquor_store_current_selection = idx 
  286. 		liquor_store_update_footer(idx) 
  287. 	end 
  288. end 
  289.  
  290. --------------- 
  291. -- MENU DATA -- 
  292. --------------- 
  293. Liquor_store_menu = { 
  294. 	header_str = "NONE", 
  295. 	on_show 	  = liquor_store_init_store, 
  296. 	on_back 	  = liquor_store_exit, 
  297. 	on_release = liquor_store_release, 
  298. 	on_post_show = liquor_store_finalize_footer, 
  299.  
  300. 	num_items = 1, 
  301. 	[0] = { label = "", type = MENU_ITEM_TYPE_GRID, on_select = liquor_store_select_item, on_nav = liquor_store_nav_item, 
  302. 		row_height = MENU_SWATCH_DEFAULT_ROW_HEIGHT, num_cols = 4, col_width = MENU_SWATCH_DEFAULT_COL_WIDTH, highlight_scale = 1, unhighlight_scale = 0.8, update_swatch=liquor_store_update_swatch  
  303. 	}, 
  304.  
  305. 	footer_height = 83, 
  306. } 
  307.  
  308.