Advertisement
Hyuna

Surprise Box v1.2.2 by Hyuna

Jul 2nd, 2016
974
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 12.23 KB | None | 0 0
  1. /*
  2.  
  3. ****** Information ******
  4.  
  5. * Plugin Name: Surprise Box
  6. * Plugin Descption: Drop a Surprise Box from killed players.
  7. * Plugin Version: 1.2.2
  8. * Plugin Creator: Hyuna aka NorToN
  9. * Creator URL: http://steamcommunity.com/id/KissMyAsscom
  10. * License: GNU GPL v3 (see below)
  11.  
  12.  
  13. ****** Requirements ******
  14.  
  15. * Amx Mod X v1.8.3+
  16. * Modules: Fakemeta, HamSandwich
  17.  
  18.  
  19. ****** Commands ******
  20.  
  21. * amx_purgeboxes - Deletes all Surprise Boxes.
  22.  
  23.  
  24. ****** Cvars ******
  25.  
  26. * amx_sboxversion - Shows plugin version.
  27.  
  28.  
  29. ****** Natives ******
  30.  
  31. * is_client_onboxmenu(client) - Returns if a selected client is in the select menu.
  32. * client_forceboxmenu(client) - Force showing surprise box menu to a selected client.
  33. * create_surprisebox(Float:origin[3]) - Makes a surprise box with given origin. Returns ent id if success or -1 if failed (too much boxes/can't create).
  34. * remove_surprisebox(iEnt) - Removes a given surprise box entity. Returns 1 on success, 0 if the ent isn't surprise box or throwing an error when entity isn't valid.
  35. * purge_surpriseboxes() - Deletes all supply boxes, if there are. Returns 1 on success or 0 if no box entities found.
  36. * get_surprisebox_count() - Returns current surprise box count (It just returns g_iEntCount value).
  37.  
  38.  
  39. ****** Forwards ******
  40.  
  41. * OnClientAttempOpenBox(client) - Called when a player attemps open a box. return PLUGIN_HANDLED to block.
  42. * OnClientTouchBox(client, iEnt) - Called when a player touches a box. return PLUGIN_HANDLED to block.
  43.  
  44.  
  45. ****** Includes ******
  46.  
  47. * #include <amxmodx>
  48. * #include <amxmisc>
  49. * #include <fakemeta>
  50. * #include <hamsandwich>
  51. * #include <surprisebox>
  52.  
  53.  
  54. ****** Change Log ******
  55.  
  56. * V 1.0 - First Public Release.
  57.  
  58. * V 1.1 - Added OnClientOpenBox forward.
  59. * Added OnClientTouchBox forward.
  60. * Added is_client_onboxmenu native.
  61.  
  62. * V 1.2 - Added create_surprisebox native.
  63. * Added client_forceboxmenu native.
  64. * Added purge_surpriseboxes native.
  65. * Added get_surprisebox_count native.
  66. * Added get_surprisebox_maxcount native.
  67. * Updated API: Now the plugin has an official include.
  68.  
  69. * V 1.2.1 - Small fixes
  70.  
  71. * V 1.2.2 - Added remove_surprisebox native.
  72.  
  73.  
  74. ****** Credits ******
  75.  
  76. * Roy [NorToN aka Hyuna] - Plugin Creator.
  77.  
  78.  
  79. */
  80.  
  81. // License:
  82. /*
  83.     AMX Mod X Plugin: Surprise Box
  84.     Copyright (C) 2016  Hyuna aka NorToN
  85.  
  86.     This program is free software: you can redistribute it and/or modify
  87.     it under the terms of the GNU General Public License as published by
  88.     the Free Software Foundation, either version 3 of the License, or
  89.     (at your option) any later version.
  90.  
  91.     This program is distributed in the hope that it will be useful,
  92.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  93.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  94.     GNU General Public License for more details.
  95.  
  96.     You should have received a copy of the GNU General Public License
  97.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  98. */
  99.  
  100. #include <amxmodx>
  101. #include <amxmisc>
  102. #include <fakemeta>
  103. #include <hamsandwich>
  104. #include <surprisebox>
  105.  
  106. #pragma semicolon 1
  107.  
  108. #if AMXX_VERSION_NUM < 183
  109.     #assert Amx Mod X Version 1.83 and above is needed to run this plugin!
  110. #endif
  111.  
  112. #define PLUGIN_VERSION "v1.2.2"
  113.  
  114. #define PREFIX "[ ^4AMXX^1 ]"
  115.  
  116. // Entity Size
  117. new Float:g_iEntMax[3] = { 5.0, 5.0, 5.0 };
  118. new Float:g_iEntMin[3] = { -2.0, -2.0, -2.0 };
  119.  
  120. // Model
  121. new g_szModel[] = "models/w_surprisebox.mdl";
  122. new g_szSurpriseBoxClassname[] = "ent_surprisebox";
  123.  
  124. new g_iMenu;
  125.  
  126. new g_iEntCount;
  127.  
  128. new g_iForwardOnAttemp, g_iForwardOnTouch;
  129.  
  130. new bool:g_bIsInMenu[MAX_PLAYERS + 1];
  131.  
  132. public plugin_init() {
  133.     register_plugin("Surprise Box",PLUGIN_VERSION,"Hyuna");
  134.  
  135.     create_cvar("amx_sboxversion",PLUGIN_VERSION,(FCVAR_SERVER | FCVAR_SPONLY | FCVAR_PRINTABLEONLY),"Shows the plugin version.");
  136.  
  137.     register_concmd("amx_purgeboxes","cmdPurgeBoxes",ADMIN_RCON,"- Deletes all Surprise Boxes");
  138.  
  139.     RegisterHam(Ham_Touch,"info_target","fw_HamEntityTouchPost",1);
  140.     RegisterHamPlayer(Ham_Spawn,"fw_HamPlayerSpawnPost",1);
  141.     RegisterHamPlayer(Ham_Killed,"fw_HamPlayerKilledPost",1);
  142.  
  143.     register_event("HLTV","fw_eventNewRound","a","1=0","2=0");
  144.  
  145.     g_iForwardOnAttemp = CreateMultiForward("OnClientAttempOpenBox",ET_STOP,FP_CELL);
  146.     g_iForwardOnTouch = CreateMultiForward("OnClientTouchBox",ET_STOP,FP_CELL,FP_CELL);
  147.  
  148.     if (g_iForwardOnAttemp == INVALID_HANDLE)
  149.         set_fail_state("Failed to create OnClientAttempOpenBox forward");
  150.  
  151.     if (g_iForwardOnTouch == INVALID_HANDLE)
  152.         set_fail_state("Failed to create OnClientTouchBox forward");
  153. }
  154.  
  155. public plugin_precache() {
  156.     if (!file_exists(g_szModel))
  157.     {
  158.         // Safe fail check
  159.         log_amx("ERROR: File not found (%s)",g_szModel);
  160.         set_fail_state("FATAL ERROR: Failed to find Surprise Box model.");
  161.     }
  162.  
  163.     precache_model(g_szModel);
  164. }
  165.  
  166. public plugin_natives() {
  167.     // Library
  168.     register_library("SurpriseBox");
  169.  
  170.     // Natives
  171.     register_native("is_client_onboxmenu","native_is_client_onboxmenu",0);
  172.     register_native("client_forceboxmenu","native_client_forceboxmenu",0);
  173.     register_native("create_surprisebox","native_create_surprisebox",0);
  174.     register_native("remove_surprisebox","native_remove_surprisebox",0);
  175.     register_native("purge_surpriseboxes","native_purge_surpriseboxes",0);
  176.     register_native("get_surprisebox_count","native_get_surprisebox_count",0);
  177. }
  178.  
  179. public native_is_client_onboxmenu(pluginid, params) {
  180.     static client;
  181.     client = get_param(1);
  182.  
  183.     if (!is_user_connected(client))
  184.     {
  185.         log_error(AMX_ERR_NATIVE,"[Suprise Box API] ERROR: Client %d is invalid.",client);
  186.         return false;
  187.     }
  188.  
  189.     return g_bIsInMenu[client];
  190. }
  191.  
  192. public native_client_forceboxmenu(pluginid, params) {
  193.     static client;
  194.     client = get_param(1);
  195.  
  196.     if (!is_user_connected(client))
  197.     {
  198.         log_error(AMX_ERR_NATIVE,"[Suprise Box API] ERROR: Client %d is invalid.",client);
  199.         return 0;
  200.     }
  201.  
  202.     if (is_user_bot(client) || is_user_hltv(client))
  203.         return -1;
  204.  
  205.     menu_display(client,g_iMenu);
  206.  
  207.     return 1;
  208. }
  209.  
  210. public native_create_surprisebox(pluginid, params) {
  211.     static Float:fOrigin[3], ent;
  212.     get_array_f(1,fOrigin,charsmax(fOrigin));
  213.  
  214.     if (g_iEntCount == MAX_BOX_ENTITES)
  215.         return INVALID_HANDLE;
  216.  
  217.     ent = CreateSupplyBox(fOrigin,true);
  218.  
  219.     if (!pev_valid(ent))
  220.         return INVALID_HANDLE;
  221.  
  222.     return ent;
  223. }
  224.  
  225. public native_remove_surprisebox(pluginid, params) {
  226.     static entid, szClassname[32];
  227.  
  228.     entid = get_param(1);
  229.  
  230.     if (!pev_valid(entid))
  231.     {
  232.         log_error(AMX_ERR_NATIVE,"[Suprise Box API] ERROR: Invalid entity ID %d",entid);
  233.         return 0;
  234.     }
  235.  
  236.     pev(entid,pev_classname,szClassname,charsmax(szClassname));
  237.  
  238.     if (!equal(szClassname,g_szSurpriseBoxClassname))
  239.         return 0;
  240.  
  241.     engfunc(EngFunc_RemoveEntity,entid);
  242.     g_iEntCount--;
  243.  
  244.     return 1;
  245. }
  246.  
  247. public native_purge_surpriseboxes(pluginid, params) {
  248.     if (!g_iEntCount)
  249.         return 0;
  250.  
  251.     PurgeBoxEntities();
  252.  
  253.     return 1;
  254. }
  255.  
  256. public native_get_surprisebox_count(pluginid, params) {
  257.     return g_iEntCount;
  258. }
  259.  
  260. public plugin_cfg() {
  261.     g_iMenu = menu_create("\d[ \rAMXX \d] \yYou have picked a Surprise Box. ^nDo you want to open it?","mHandler");
  262.     menu_additem(g_iMenu,"Yes");
  263.     menu_additem(g_iMenu,"No");
  264.  
  265.     menu_setprop(g_iMenu,MPROP_EXIT,MEXIT_NEVER);
  266. }
  267.  
  268. public plugin_end() {
  269.     // Prevent AMXX memory leak
  270.     DestroyForward(g_iForwardOnAttemp);
  271.     DestroyForward(g_iForwardOnTouch);
  272. }
  273.  
  274. public cmdPurgeBoxes(client,level,cid) {
  275.     static szName[32], szAuthid[32];
  276.  
  277.     if (!cmd_access(client,level,cid,1))
  278.         return PLUGIN_HANDLED;
  279.  
  280.     if (!g_iEntCount)
  281.     {
  282.         if (client)
  283.             client_print_color(client,print_team_red,"%s There ^3aren't^1 any surprise box entites to purge.",PREFIX);
  284.  
  285.         return PLUGIN_HANDLED;
  286.     }
  287.  
  288.     PurgeBoxEntities();
  289.  
  290.     if (client)
  291.     {
  292.         get_user_name(client,szName,charsmax(szName));
  293.         get_user_authid(client,szAuthid,charsmax(szAuthid));
  294.  
  295.         log_amx("Cmd: ^"%s<%d><%s><>^" purged all Surprise Box entites.",szName,get_user_userid(client),szAuthid);
  296.         client_print_color(0,client,"%s ^4ADMIN ^3%s^1 has purged all surprise box entites.",PREFIX,szName);
  297.     }
  298.  
  299.     else
  300.     {
  301.         log_amx("Server Cmd: Purge all Surprise Box entites.");
  302.         client_print_color(0,print_team_default," %s ^4Server^1 has purged all surprise box entites.",PREFIX);
  303.     }
  304.  
  305.     return PLUGIN_HANDLED;
  306. }
  307.  
  308. public fw_eventNewRound() {
  309.     PurgeBoxEntities();
  310. }
  311.  
  312. public fw_HamPlayerSpawnPost(client) {
  313.     if (!is_user_alive(client))
  314.         return;
  315.  
  316.     if (g_bIsInMenu[client])
  317.     {
  318.         menu_cancel(client);
  319.         reset_menu(client);
  320.  
  321.         g_bIsInMenu[client] = false;
  322.     }
  323. }
  324.  
  325. public fw_HamPlayerKilledPost(idvictim, idattacker, bool:shouldgib) {
  326.     static Float:fOrigin[3];
  327.  
  328.     if (!is_user_connected(idvictim))
  329.         return;
  330.  
  331.     pev(idvictim,pev_origin,fOrigin);
  332.  
  333.     CreateSupplyBox(fOrigin,false);
  334. }
  335.  
  336. public fw_HamEntityTouchPost(iEnt, idother) {
  337.     static szClassname[32], ret;
  338.  
  339.     if (!is_user_alive(idother) || !pev_valid(iEnt))
  340.         return;
  341.  
  342.     pev(iEnt,pev_classname,szClassname,charsmax(szClassname));
  343.  
  344.     if (!equal(szClassname,g_szSurpriseBoxClassname))
  345.         return;
  346.  
  347.     ExecuteForward(g_iForwardOnTouch,ret,idother,iEnt);
  348.  
  349.     if (ret == PLUGIN_HANDLED)
  350.         return;
  351.  
  352.     if (g_bIsInMenu[idother])
  353.         return;
  354.  
  355.     engfunc(EngFunc_RemoveEntity,iEnt);
  356.     g_iEntCount--;
  357.  
  358.     menu_cancel(idother);
  359.     reset_menu(idother);
  360.     menu_display(idother,g_iMenu);
  361.  
  362.     g_bIsInMenu[idother] = true;
  363. }
  364.  
  365. public mHandler(client, menu, item) {
  366.     static szName[32], ret;
  367.  
  368.     if (item == 0)
  369.     {
  370.         ExecuteForward(g_iForwardOnAttemp,ret,client);
  371.  
  372.         if (ret == PLUGIN_HANDLED)
  373.         {
  374.             g_bIsInMenu[client] = false;
  375.             return PLUGIN_HANDLED;
  376.         }
  377.  
  378.         if (is_user_alive(client))
  379.         {
  380.             get_user_name(client,szName,charsmax(szName));
  381.  
  382.             switch(random(100))
  383.             {
  384.                 case 0..19: // 20% chance
  385.                 {
  386.                     client_print_color(client,print_team_default,"%s You opened the box and got ^4XXXXX^1.",PREFIX);
  387.                     client_print_color(0,client,"%s ^3%s^1 opened a ^4Surprise Box^1, and got ^4XXXXX^1.",PREFIX,szName);
  388.                 }
  389.  
  390.                 case 20..39: // 20% chance
  391.                 {
  392.                     client_print_color(client,print_team_default,"%s You opened the box and got ^4YYYYY^1.",PREFIX);
  393.                     client_print_color(0,client,"%s ^3%s^1 opened a ^4Surprise Box^1, and got ^4YYYYY^1.",PREFIX,szName);
  394.                 }
  395.  
  396.                 case 40..59: // 20% chance
  397.                 {
  398.                     client_print_color(client,print_team_default,"%s You opened the box and got ^4ZZZZZ^1.",PREFIX);
  399.                     client_print_color(0,client,"%s ^3%s^1 opened a ^4Surprise Box^1, and got ^4ZZZZZ^1.",PREFIX,szName);
  400.                 }
  401.  
  402.                 case 60..99: // 40% chance
  403.                 {
  404.                     client_print_color(client,print_team_red,"%s Sorry, but the box is ^3empty^1! Try next time.",PREFIX);
  405.                     client_print_color(0,client,"%s ^3%s^1 opened a ^4Surprise Box^1, but it was ^4empty^1.",PREFIX,szName);
  406.                 }
  407.             }
  408.         }
  409.  
  410.         else
  411.             client_print_color(client,print_team_default,"%s You must be ^4alive^1 to open a surprise box.",PREFIX);
  412.     }
  413.  
  414.     else
  415.         client_print_color(client,print_team_red,"%s You choosed ^3not^1 to open the box. It has been ^3destroyed^1.",PREFIX);
  416.  
  417.     g_bIsInMenu[client] = false;
  418.  
  419.     return PLUGIN_HANDLED;
  420. }
  421.  
  422. CreateSupplyBox(Float:origin[3], bool:isnative) {
  423.     if (!isnative)
  424.     {
  425.         if (g_iEntCount == MAX_BOX_ENTITES)
  426.         {
  427.             log_amx("Warning: Max box entites reached (%d). Plugin won't create new box entites until it will purge.",MAX_BOX_ENTITES);
  428.             log_amx("Use amx_purgeboxes command to purge all boxes.");
  429.             client_print_color(0,print_team_red,"%s ^3WARNING:^1 Max surprise box entities reached (^4%d^1), need a purge!",PREFIX,MAX_BOX_ENTITES);
  430.  
  431.             return -1;
  432.         }
  433.     }
  434.  
  435.     new iEnt = engfunc(EngFunc_CreateNamedEntity,engfunc(EngFunc_AllocString,"info_target"));
  436.  
  437.     if (!pev_valid(iEnt))
  438.     {
  439.         if (!isnative)
  440.             set_fail_state("Error creating surprise box entity");
  441.  
  442.         return -1;
  443.     }
  444.  
  445.     // Set classname
  446.     set_pev(iEnt,pev_classname,g_szSurpriseBoxClassname);
  447.  
  448.     // Set Surprise Box origin
  449.     engfunc(EngFunc_SetOrigin,iEnt,origin);
  450.  
  451.     // Set Surprise Box model
  452.     engfunc(EngFunc_SetModel,iEnt,g_szModel);
  453.  
  454.     // Set soild state so players can touch the box
  455.     set_pev(iEnt,pev_solid,SOLID_BBOX);
  456.  
  457.     // Set the box's size
  458.     engfunc(EngFunc_SetSize,iEnt,g_iEntMin,g_iEntMax);
  459.  
  460.     // Drop box to floor
  461.     engfunc(EngFunc_DropToFloor,iEnt);
  462.  
  463.     g_iEntCount++;
  464.  
  465.     return iEnt;
  466. }
  467.  
  468. PurgeBoxEntities() {
  469.     new ent = -1;
  470.  
  471.     while ((ent = engfunc(EngFunc_FindEntityByString,ent,"classname",g_szSurpriseBoxClassname)) > 0)
  472.     {
  473.         if (pev_valid(ent))
  474.             engfunc(EngFunc_RemoveEntity,ent);
  475.     }
  476.  
  477.     g_iEntCount = 0;
  478. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement