Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2023
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.95 KB | None | 0 0
  1. /*================================================================================
  2.  
  3. --------------------------
  4. -*- [ZP] Items Manager -*-
  5. --------------------------
  6.  
  7. This plugin is part of Zombie Plague Mod and is distributed under the
  8. terms of the GNU General Public License. Check ZP_ReadMe.txt for details.
  9.  
  10. ================================================================================*/
  11.  
  12. #include <amxmodx>
  13. #include <fakemeta>
  14. #include <amx_settings_api>
  15. #include <zp50_colorchat>
  16. #include <zp50_core_const>
  17. #include <zp50_items_const>
  18.  
  19. // Extra Items file
  20. new const ZP_EXTRAITEMS_FILE[] = "zp_extraitems.ini"
  21.  
  22. // CS Player PData Offsets (win32)
  23. const OFFSET_CSMENUCODE = 205
  24.  
  25. #define MAXPLAYERS 32
  26.  
  27. // For item list menu handlers
  28. #define MENU_PAGE_ITEMS g_menu_data[id]
  29. new g_menu_data[MAXPLAYERS+1]
  30.  
  31. enum _:TOTAL_FORWARDS
  32. {
  33. FW_ITEM_SELECT_PRE = 0,
  34. FW_ITEM_SELECT_POST
  35. }
  36. new g_Forwards[TOTAL_FORWARDS]
  37. new g_ForwardResult
  38.  
  39. // Items data
  40. new Array:g_ItemRealName
  41. new Array:g_ItemName
  42. new Array:g_ItemCost
  43. new g_ItemCount
  44. new g_AdditionalMenuText[32]
  45.  
  46. public plugin_init()
  47. {
  48. register_plugin("[ZP] Items Manager", ZP_VERSION_STRING, "ZP Dev Team")
  49.  
  50. register_clcmd("say /items", "clcmd_items")
  51. register_clcmd("say items", "clcmd_items")
  52.  
  53. g_Forwards[FW_ITEM_SELECT_PRE] = CreateMultiForward("zp_fw_items_select_pre", ET_CONTINUE, FP_CELL, FP_CELL, FP_CELL)
  54. g_Forwards[FW_ITEM_SELECT_POST] = CreateMultiForward("zp_fw_items_select_post", ET_IGNORE, FP_CELL, FP_CELL, FP_CELL)
  55. }
  56.  
  57. public plugin_natives()
  58. {
  59. register_library("zp50_items")
  60. register_native("zp_items_register", "native_items_register")
  61. register_native("zp_items_get_id", "native_items_get_id")
  62. register_native("zp_items_get_name", "native_items_get_name")
  63. register_native("zp_items_get_real_name", "native_items_get_real_name")
  64. register_native("zp_items_get_cost", "native_items_get_cost")
  65. register_native("zp_items_show_menu", "native_items_show_menu")
  66. register_native("zp_items_force_buy", "native_items_force_buy")
  67. register_native("zp_items_menu_text_add", "native_items_menu_text_add")
  68.  
  69. // Initialize dynamic arrays
  70. g_ItemRealName = ArrayCreate(32, 1)
  71. g_ItemName = ArrayCreate(32, 1)
  72. g_ItemCost = ArrayCreate(1, 1)
  73. }
  74.  
  75. public native_items_register(plugin_id, num_params)
  76. {
  77. new name[32], cost = get_param(2)
  78. get_string(1, name, charsmax(name))
  79.  
  80. if (strlen(name) < 1)
  81. {
  82. log_error(AMX_ERR_NATIVE, "[ZP] Can't register item with an empty name")
  83. return ZP_INVALID_ITEM;
  84. }
  85.  
  86. new index, item_name[32]
  87. for (index = 0; index < g_ItemCount; index++)
  88. {
  89. ArrayGetString(g_ItemRealName, index, item_name, charsmax(item_name))
  90. if (equali(name, item_name))
  91. {
  92. log_error(AMX_ERR_NATIVE, "[ZP] Item already registered (%s)", name)
  93. return ZP_INVALID_ITEM;
  94. }
  95. }
  96.  
  97. // Load settings from extra items file
  98. new real_name[32]
  99. copy(real_name, charsmax(real_name), name)
  100. ArrayPushString(g_ItemRealName, real_name)
  101.  
  102. // Name
  103. if (!amx_load_setting_string(ZP_EXTRAITEMS_FILE, real_name, "NAME", name, charsmax(name)))
  104. amx_save_setting_string(ZP_EXTRAITEMS_FILE, real_name, "NAME", name)
  105. ArrayPushString(g_ItemName, name)
  106.  
  107. // Cost
  108. if (!amx_load_setting_int(ZP_EXTRAITEMS_FILE, real_name, "COST", cost))
  109. amx_save_setting_int(ZP_EXTRAITEMS_FILE, real_name, "COST", cost)
  110. ArrayPushCell(g_ItemCost, cost)
  111.  
  112. g_ItemCount++
  113. return g_ItemCount - 1;
  114. }
  115.  
  116. public native_items_get_id(plugin_id, num_params)
  117. {
  118. new real_name[32]
  119. get_string(1, real_name, charsmax(real_name))
  120.  
  121. // Loop through every item
  122. new index, item_name[32]
  123. for (index = 0; index < g_ItemCount; index++)
  124. {
  125. ArrayGetString(g_ItemRealName, index, item_name, charsmax(item_name))
  126. if (equali(real_name, item_name))
  127. return index;
  128. }
  129.  
  130. return ZP_INVALID_ITEM;
  131. }
  132.  
  133. public native_items_get_name(plugin_id, num_params)
  134. {
  135. new item_id = get_param(1)
  136.  
  137. if (item_id < 0 || item_id >= g_ItemCount)
  138. {
  139. log_error(AMX_ERR_NATIVE, "[ZP] Invalid item id (%d)", item_id)
  140. return false;
  141. }
  142.  
  143. new name[32]
  144. ArrayGetString(g_ItemName, item_id, name, charsmax(name))
  145.  
  146. new len = get_param(3)
  147. set_string(2, name, len)
  148. return true;
  149. }
  150.  
  151. public native_items_get_real_name(plugin_id, num_params)
  152. {
  153. new item_id = get_param(1)
  154.  
  155. if (item_id < 0 || item_id >= g_ItemCount)
  156. {
  157. log_error(AMX_ERR_NATIVE, "[ZP] Invalid item id (%d)", item_id)
  158. return false;
  159. }
  160.  
  161. new real_name[32]
  162. ArrayGetString(g_ItemRealName, item_id, real_name, charsmax(real_name))
  163.  
  164. new len = get_param(3)
  165. set_string(2, real_name, len)
  166. return true;
  167. }
  168.  
  169. public native_items_get_cost(plugin_id, num_params)
  170. {
  171. new item_id = get_param(1)
  172.  
  173. if (item_id < 0 || item_id >= g_ItemCount)
  174. {
  175. log_error(AMX_ERR_NATIVE, "[ZP] Invalid item id (%d)", item_id)
  176. return -1;
  177. }
  178.  
  179. return ArrayGetCell(g_ItemCost, item_id);
  180. }
  181.  
  182. public native_items_show_menu(plugin_id, num_params)
  183. {
  184. new id = get_param(1)
  185.  
  186. if (!is_user_connected(id))
  187. {
  188. log_error(AMX_ERR_NATIVE, "[ZP] Invalid Player (%d)", id)
  189. return false;
  190. }
  191.  
  192. clcmd_items(id)
  193. return true;
  194. }
  195.  
  196. public native_items_force_buy(plugin_id, num_params)
  197. {
  198. new id = get_param(1)
  199.  
  200. if (!is_user_connected(id))
  201. {
  202. log_error(AMX_ERR_NATIVE, "[ZP] Invalid Player (%d)", id)
  203. return false;
  204. }
  205.  
  206. new item_id = get_param(2)
  207.  
  208. if (item_id < 0 || item_id >= g_ItemCount)
  209. {
  210. log_error(AMX_ERR_NATIVE, "[ZP] Invalid item id (%d)", item_id)
  211. return false;
  212. }
  213.  
  214. new ignorecost = get_param(3)
  215.  
  216. buy_item(id, item_id, ignorecost)
  217. return true;
  218. }
  219.  
  220. public native_items_menu_text_add(plugin_id, num_params)
  221. {
  222. static text[32]
  223. get_string(1, text, charsmax(text))
  224. format(g_AdditionalMenuText, charsmax(g_AdditionalMenuText), "%s%s", g_AdditionalMenuText, text)
  225. }
  226.  
  227. public client_disconnect(id)
  228. {
  229. // Reset remembered menu pages
  230. MENU_PAGE_ITEMS = 0
  231. }
  232.  
  233. public clcmd_items(id)
  234. {
  235. // Player dead
  236. if (!is_user_alive(id))
  237. return;
  238.  
  239. show_items_menu(id)
  240. }
  241.  
  242. // Items Menu
  243. show_items_menu(id)
  244. {
  245. static menu[128], name[32], cost, transkey[64]
  246. new menuid, index, itemdata[2]
  247.  
  248. // Title
  249. formatex(menu, charsmax(menu), "%L:\r", id, "MENU_EXTRABUY")
  250. menuid = menu_create(menu, "menu_extraitems")
  251.  
  252. // Item List
  253. for (index = 0; index < g_ItemCount; index++)
  254. {
  255. // Additional text to display
  256. g_AdditionalMenuText[0] = 0
  257.  
  258. // Execute item select attempt forward
  259. ExecuteForward(g_Forwards[FW_ITEM_SELECT_PRE], g_ForwardResult, id, index, 0)
  260.  
  261. // Show item to player?
  262. if (g_ForwardResult >= ZP_ITEM_DONT_SHOW)
  263. continue;
  264.  
  265. // Add Item Name and Cost
  266. ArrayGetString(g_ItemName, index, name, charsmax(name))
  267. cost = ArrayGetCell(g_ItemCost, index)
  268.  
  269. // ML support for item name
  270. formatex(transkey, charsmax(transkey), "ITEMNAME %s", name)
  271. if (GetLangTransKey(transkey) != TransKey_Bad) formatex(name, charsmax(name), "%L", id, transkey)
  272.  
  273. // Item available to player?
  274. if (g_ForwardResult >= ZP_ITEM_NOT_AVAILABLE)
  275. formatex(menu, charsmax(menu), "\d%s %d %s", name, cost, g_AdditionalMenuText)
  276. else
  277. formatex(menu, charsmax(menu), "%s \y%d \w%s", name, cost, g_AdditionalMenuText)
  278.  
  279. itemdata[0] = index
  280. itemdata[1] = 0
  281. menu_additem(menuid, menu, itemdata)
  282. }
  283.  
  284. // No items to display?
  285. if (menu_items(menuid) <= 0)
  286. {
  287. zp_colored_print(id, "%L", id, "NO_EXTRA_ITEMS")
  288. menu_destroy(menuid)
  289. return;
  290. }
  291.  
  292. // Back - Next - Exit
  293. formatex(menu, charsmax(menu), "%L", id, "MENU_BACK")
  294. menu_setprop(menuid, MPROP_BACKNAME, menu)
  295. formatex(menu, charsmax(menu), "%L", id, "MENU_NEXT")
  296. menu_setprop(menuid, MPROP_NEXTNAME, menu)
  297. formatex(menu, charsmax(menu), "%L", id, "MENU_EXIT")
  298. menu_setprop(menuid, MPROP_EXITNAME, menu)
  299.  
  300. // If remembered page is greater than number of pages, clamp down the value
  301. MENU_PAGE_ITEMS = min(MENU_PAGE_ITEMS, menu_pages(menuid)-1)
  302.  
  303. // Fix for AMXX custom menus
  304. set_pdata_int(id, OFFSET_CSMENUCODE, 0)
  305. menu_display(id, menuid, MENU_PAGE_ITEMS)
  306. }
  307.  
  308. // Items Menu
  309. public menu_extraitems(id, menuid, item)
  310. {
  311. // Menu was closed
  312. if (item == MENU_EXIT)
  313. {
  314. MENU_PAGE_ITEMS = 0
  315. menu_destroy(menuid)
  316. return PLUGIN_HANDLED;
  317. }
  318.  
  319. // Remember items menu page
  320. MENU_PAGE_ITEMS = item / 7
  321.  
  322. // Dead players are not allowed to buy items
  323. if (!is_user_alive(id))
  324. {
  325. menu_destroy(menuid)
  326. return PLUGIN_HANDLED;
  327. }
  328.  
  329. // Retrieve item id
  330. new itemdata[2], dummy, itemid
  331. menu_item_getinfo(menuid, item, dummy, itemdata, charsmax(itemdata), _, _, dummy)
  332. itemid = itemdata[0]
  333.  
  334. // Attempt to buy the item
  335. buy_item(id, itemid)
  336. menu_destroy(menuid)
  337. return PLUGIN_HANDLED;
  338. }
  339.  
  340. // Buy Item
  341. buy_item(id, itemid, ignorecost = 0)
  342. {
  343. // Execute item select attempt forward
  344. ExecuteForward(g_Forwards[FW_ITEM_SELECT_PRE], g_ForwardResult, id, itemid, ignorecost)
  345.  
  346. // Item available to player?
  347. if (g_ForwardResult >= ZP_ITEM_NOT_AVAILABLE)
  348. return;
  349.  
  350. // Execute item selected forward
  351. ExecuteForward(g_Forwards[FW_ITEM_SELECT_POST], g_ForwardResult, id, itemid, ignorecost)
  352. }
  353.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement