HLMOD.HU Forrás Megtekintés - www.hlmod.hu
  1. /*================================================================================
  2.  
  3. ------------------------
  4. -*- [ZP] Core/Engine -*-
  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 <amxmisc>
  14. #include <cstrike>
  15. #include <fakemeta>
  16. #include <hamsandwich>
  17. #include <cs_ham_bots_api>
  18. #include <zp50_core_const>
  19.  
  20. #define MAXPLAYERS 32
  21.  
  22. // Custom Forwards
  23. enum _:TOTAL_FORWARDS
  24. {
  25. FW_USER_INFECT_PRE = 0,
  26. FW_USER_INFECT,
  27. FW_USER_INFECT_POST,
  28. FW_USER_CURE_PRE,
  29. FW_USER_CURE,
  30. FW_USER_CURE_POST,
  31. FW_USER_LAST_ZOMBIE,
  32. FW_USER_LAST_HUMAN,
  33. FW_USER_SPAWN_POST
  34. }
  35.  
  36. #define flag_get(%1,%2) (%1 & (1 << (%2 & 31)))
  37. #define flag_get_boolean(%1,%2) (flag_get(%1,%2) ? true : false)
  38. #define flag_set(%1,%2) %1 |= (1 << (%2 & 31))
  39. #define flag_unset(%1,%2) %1 &= ~(1 << (%2 & 31))
  40.  
  41. new g_MaxPlayers
  42. new g_IsZombie
  43. new g_IsFirstZombie
  44. new g_IsLastZombie
  45. new g_LastZombieForwardCalled
  46. new g_IsLastHuman
  47. new g_LastHumanForwardCalled
  48. new g_RespawnAsZombie
  49. new g_ForwardResult
  50. new g_Forwards[TOTAL_FORWARDS]
  51.  
  52. public plugin_init()
  53. {
  54. register_plugin("[ZP] Core/Engine", ZP_VERSION_STRING, "ZP Dev Team")
  55. register_dictionary("zombie_plague.txt")
  56. register_dictionary("zombie_plague50.txt")
  57.  
  58. g_Forwards[FW_USER_INFECT_PRE] = CreateMultiForward("zp_fw_core_infect_pre", ET_CONTINUE, FP_CELL, FP_CELL)
  59. g_Forwards[FW_USER_INFECT] = CreateMultiForward("zp_fw_core_infect", ET_IGNORE, FP_CELL, FP_CELL)
  60. g_Forwards[FW_USER_INFECT_POST] = CreateMultiForward("zp_fw_core_infect_post", ET_IGNORE, FP_CELL, FP_CELL)
  61.  
  62. g_Forwards[FW_USER_CURE_PRE] = CreateMultiForward("zp_fw_core_cure_pre", ET_CONTINUE, FP_CELL, FP_CELL)
  63. g_Forwards[FW_USER_CURE] = CreateMultiForward("zp_fw_core_cure", ET_IGNORE, FP_CELL, FP_CELL)
  64. g_Forwards[FW_USER_CURE_POST] = CreateMultiForward("zp_fw_core_cure_post", ET_IGNORE, FP_CELL, FP_CELL)
  65.  
  66. g_Forwards[FW_USER_LAST_ZOMBIE] = CreateMultiForward("zp_fw_core_last_zombie", ET_IGNORE, FP_CELL)
  67. g_Forwards[FW_USER_LAST_HUMAN] = CreateMultiForward("zp_fw_core_last_human", ET_IGNORE, FP_CELL)
  68.  
  69. g_Forwards[FW_USER_SPAWN_POST] = CreateMultiForward("zp_fw_core_spawn_post", ET_IGNORE, FP_CELL)
  70.  
  71. RegisterHam(Ham_Spawn, "player", "fw_PlayerSpawn_Post", 1)
  72. RegisterHamBots(Ham_Spawn, "fw_PlayerSpawn_Post", 1)
  73. RegisterHam(Ham_Killed, "player", "fw_PlayerKilled_Post", 1)
  74. RegisterHamBots(Ham_Killed, "fw_PlayerKilled_Post", 1)
  75. register_forward(FM_ClientDisconnect, "fw_ClientDisconnect_Post", 1)
  76.  
  77. g_MaxPlayers = get_maxplayers()
  78.  
  79. // To help players find ZP servers
  80. register_cvar("zp_version", ZP_VERSION_STR_LONG, FCVAR_SERVER|FCVAR_SPONLY)
  81. set_cvar_string("zp_version", ZP_VERSION_STR_LONG)
  82. }
  83.  
  84. public plugin_cfg()
  85. {
  86. // Get configs dir
  87. new cfgdir[32]
  88. get_configsdir(cfgdir, charsmax(cfgdir))
  89.  
  90. // Execute config file (zombieplague.cfg)
  91. server_cmd("exec %s/zombieplague.cfg", cfgdir)
  92. }
  93.  
  94. public plugin_natives()
  95. {
  96. register_library("zp50_core")
  97. register_native("zp_core_is_zombie", "native_core_is_zombie")
  98. register_native("zp_core_is_first_zombie", "native_core_is_first_zombie")
  99. register_native("zp_core_is_last_zombie", "native_core_is_last_zombie")
  100. register_native("zp_core_is_last_human", "native_core_is_last_human")
  101. register_native("zp_core_get_zombie_count", "native_core_get_zombie_count")
  102. register_native("zp_core_get_human_count", "native_core_get_human_count")
  103. register_native("zp_core_infect", "native_core_infect")
  104. register_native("zp_core_cure", "native_core_cure")
  105. register_native("zp_core_force_infect", "native_core_force_infect")
  106. register_native("zp_core_force_cure", "native_core_force_cure")
  107. register_native("zp_core_respawn_as_zombie", "native_core_respawn_as_zombie")
  108. }
  109.  
  110. public fw_ClientDisconnect_Post(id)
  111. {
  112. // Reset flags AFTER disconnect (to allow checking if the player was zombie before disconnecting)
  113. flag_unset(g_IsZombie, id)
  114. flag_unset(g_RespawnAsZombie, id)
  115.  
  116. // This should be called AFTER client disconnects (post forward)
  117. CheckLastZombieHuman()
  118. }
  119.  
  120. public fw_PlayerSpawn_Post(id)
  121. {
  122. // Not alive or didn't join a team yet
  123. if (!is_user_alive(id) || !cs_get_user_team(id))
  124. return;
  125.  
  126. // ZP Spawn Forward
  127. ExecuteForward(g_Forwards[FW_USER_SPAWN_POST], g_ForwardResult, id)
  128.  
  129. // Set zombie/human attributes upon respawn
  130. if (flag_get(g_RespawnAsZombie, id))
  131. InfectPlayer(id, id)
  132. else
  133. CurePlayer(id)
  134.  
  135. // Reset flag afterwards
  136. flag_unset(g_RespawnAsZombie, id)
  137. }
  138.  
  139. // Ham Player Killed Post Forward
  140. public fw_PlayerKilled_Post()
  141. {
  142. CheckLastZombieHuman()
  143. }
  144.  
  145. InfectPlayer(id, attacker = 0)
  146. {
  147. ExecuteForward(g_Forwards[FW_USER_INFECT_PRE], g_ForwardResult, id, attacker)
  148.  
  149. // One or more plugins blocked infection
  150. if (g_ForwardResult >= PLUGIN_HANDLED)
  151. return;
  152.  
  153. ExecuteForward(g_Forwards[FW_USER_INFECT], g_ForwardResult, id, attacker)
  154.  
  155. flag_set(g_IsZombie, id)
  156.  
  157. if (GetZombieCount() == 1)
  158. flag_set(g_IsFirstZombie, id)
  159. else
  160. flag_unset(g_IsFirstZombie, id)
  161.  
  162. ExecuteForward(g_Forwards[FW_USER_INFECT_POST], g_ForwardResult, id, attacker)
  163.  
  164. CheckLastZombieHuman()
  165. }
  166.  
  167. CurePlayer(id, attacker = 0)
  168. {
  169. ExecuteForward(g_Forwards[FW_USER_CURE_PRE], g_ForwardResult, id, attacker)
  170.  
  171. // One or more plugins blocked cure
  172. if (g_ForwardResult >= PLUGIN_HANDLED)
  173. return;
  174.  
  175. ExecuteForward(g_Forwards[FW_USER_CURE], g_ForwardResult, id, attacker)
  176.  
  177. flag_unset(g_IsZombie, id)
  178.  
  179. ExecuteForward(g_Forwards[FW_USER_CURE_POST], g_ForwardResult, id, attacker)
  180.  
  181. CheckLastZombieHuman()
  182. }
  183.  
  184. // Last Zombie/Human Check
  185. CheckLastZombieHuman()
  186. {
  187. new id, last_zombie_id, last_human_id
  188. new zombie_count = GetZombieCount()
  189. new human_count = GetHumanCount()
  190.  
  191. if (zombie_count == 1)
  192. {
  193. for (id = 1; id <= g_MaxPlayers; id++)
  194. {
  195. // Last zombie
  196. if (is_user_alive(id) && flag_get(g_IsZombie, id))
  197. {
  198. flag_set(g_IsLastZombie, id)
  199. last_zombie_id = id
  200. }
  201. else
  202. flag_unset(g_IsLastZombie, id)
  203. }
  204. }
  205. else
  206. {
  207. g_LastZombieForwardCalled = false
  208.  
  209. for (id = 1; id <= g_MaxPlayers; id++)
  210. flag_unset(g_IsLastZombie, id)
  211. }
  212.  
  213. // Last zombie forward
  214. if (last_zombie_id > 0 && !g_LastZombieForwardCalled)
  215. {
  216. ExecuteForward(g_Forwards[FW_USER_LAST_ZOMBIE], g_ForwardResult, last_zombie_id)
  217. g_LastZombieForwardCalled = true
  218. }
  219.  
  220. if (human_count == 1)
  221. {
  222. for (id = 1; id <= g_MaxPlayers; id++)
  223. {
  224. // Last human
  225. if (is_user_alive(id) && !flag_get(g_IsZombie, id))
  226. {
  227. flag_set(g_IsLastHuman, id)
  228. last_human_id = id
  229. }
  230. else
  231. flag_unset(g_IsLastHuman, id)
  232. }
  233. }
  234. else
  235. {
  236. g_LastHumanForwardCalled = false
  237.  
  238. for (id = 1; id <= g_MaxPlayers; id++)
  239. flag_unset(g_IsLastHuman, id)
  240. }
  241.  
  242. // Last human forward
  243. if (last_human_id > 0 && !g_LastHumanForwardCalled)
  244. {
  245. ExecuteForward(g_Forwards[FW_USER_LAST_HUMAN], g_ForwardResult, last_human_id)
  246. g_LastHumanForwardCalled = true
  247. }
  248. }
  249.  
  250. public native_core_is_zombie(plugin_id, num_params)
  251. {
  252. new id = get_param(1)
  253.  
  254. if (!is_user_connected(id))
  255. {
  256. log_error(AMX_ERR_NATIVE, "[ZP] Invalid Player (%d)", id)
  257. return -1;
  258. }
  259.  
  260. return flag_get_boolean(g_IsZombie, id);
  261. }
  262.  
  263. public native_core_is_first_zombie(plugin_id, num_params)
  264. {
  265. new id = get_param(1)
  266.  
  267. if (!is_user_connected(id))
  268. {
  269. log_error(AMX_ERR_NATIVE, "[ZP] Invalid Player (%d)", id)
  270. return -1;
  271. }
  272.  
  273. return flag_get_boolean(g_IsFirstZombie, id);
  274. }
  275.  
  276. public native_core_is_last_zombie(plugin_id, num_params)
  277. {
  278. new id = get_param(1)
  279.  
  280. if (!is_user_connected(id))
  281. {
  282. log_error(AMX_ERR_NATIVE, "[ZP] Invalid Player (%d)", id)
  283. return -1;
  284. }
  285.  
  286. return flag_get_boolean(g_IsLastZombie, id);
  287. }
  288.  
  289. public native_core_is_last_human(plugin_id, num_params)
  290. {
  291. new id = get_param(1)
  292.  
  293. if (!is_user_connected(id))
  294. {
  295. log_error(AMX_ERR_NATIVE, "[ZP] Invalid Player (%d)", id)
  296. return -1;
  297. }
  298.  
  299. return flag_get_boolean(g_IsLastHuman, id);
  300. }
  301.  
  302. public native_core_get_zombie_count(plugin_id, num_params)
  303. {
  304. return GetZombieCount();
  305. }
  306.  
  307. public native_core_get_human_count(plugin_id, num_params)
  308. {
  309. return GetHumanCount();
  310. }
  311.  
  312. public native_core_infect(plugin_id, num_params)
  313. {
  314. new id = get_param(1)
  315.  
  316. if (!is_user_alive(id))
  317. {
  318. log_error(AMX_ERR_NATIVE, "[ZP] Invalid Player (%d)", id)
  319. return false;
  320. }
  321.  
  322. if (flag_get(g_IsZombie, id))
  323. {
  324. log_error(AMX_ERR_NATIVE, "[ZP] Player already infected (%d)", id)
  325. return false;
  326. }
  327.  
  328. new attacker = get_param(2)
  329.  
  330. if (attacker && !is_user_alive(attacker))
  331. {
  332. log_error(AMX_ERR_NATIVE, "[ZP] Invalid Player (%d)", attacker)
  333. return false;
  334. }
  335.  
  336. InfectPlayer(id, attacker)
  337. return true;
  338. }
  339.  
  340. public native_core_cure(plugin_id, num_params)
  341. {
  342. new id = get_param(1)
  343.  
  344. if (!is_user_alive(id))
  345. {
  346. log_error(AMX_ERR_NATIVE, "[ZP] Invalid Player (%d)", id)
  347. return false;
  348. }
  349.  
  350. if (!flag_get(g_IsZombie, id))
  351. {
  352. log_error(AMX_ERR_NATIVE, "[ZP] Player not infected (%d)", id)
  353. return false;
  354. }
  355.  
  356. new attacker = get_param(2)
  357.  
  358. if (attacker && !is_user_alive(attacker))
  359. {
  360. log_error(AMX_ERR_NATIVE, "[ZP] Invalid Player (%d)", attacker)
  361. return false;
  362. }
  363.  
  364. CurePlayer(id, attacker)
  365. return true;
  366. }
  367.  
  368. public native_core_force_infect(plugin_id, num_params)
  369. {
  370. new id = get_param(1)
  371.  
  372. if (!is_user_alive(id))
  373. {
  374. log_error(AMX_ERR_NATIVE, "[ZP] Invalid Player (%d)", id)
  375. return false;
  376. }
  377.  
  378. InfectPlayer(id)
  379. return true;
  380. }
  381.  
  382. public native_core_force_cure(plugin_id, num_params)
  383. {
  384. new id = get_param(1)
  385.  
  386. if (!is_user_alive(id))
  387. {
  388. log_error(AMX_ERR_NATIVE, "[ZP] Invalid Player (%d)", id)
  389. return false;
  390. }
  391.  
  392. CurePlayer(id)
  393. return true;
  394. }
  395.  
  396. public native_core_respawn_as_zombie(plugin_id, num_params)
  397. {
  398. new id = get_param(1)
  399.  
  400. if (!is_user_connected(id))
  401. {
  402. log_error(AMX_ERR_NATIVE, "[ZP] Invalid Player (%d)", id)
  403. return false;
  404. }
  405.  
  406. new respawn_as_zombie = get_param(2)
  407.  
  408. if (respawn_as_zombie)
  409. flag_set(g_RespawnAsZombie, id)
  410. else
  411. flag_unset(g_RespawnAsZombie, id)
  412.  
  413. return true;
  414. }
  415.  
  416. // Get Zombie Count -returns alive zombies number-
  417. GetZombieCount()
  418. {
  419. new iZombies, id
  420.  
  421. for (id = 1; id <= g_MaxPlayers; id++)
  422. {
  423. if (is_user_alive(id) && flag_get(g_IsZombie, id))
  424. iZombies++
  425. }
  426.  
  427. return iZombies;
  428. }
  429.  
  430. // Get Human Count -returns alive humans number-
  431. GetHumanCount()
  432. {
  433. new iHumans, id
  434.  
  435. for (id = 1; id <= g_MaxPlayers; id++)
  436. {
  437. if (is_user_alive(id) && !flag_get(g_IsZombie, id))
  438. iHumans++
  439. }
  440.  
  441. return iHumans;
  442. }
  443.