Rejack

NPC Banker

Jul 22nd, 2014 (edited)
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 8.00 KB | None | 0 0
  1. #include < amxmodx >
  2. #include < amxmisc >
  3. #include < fakemeta >
  4.  
  5. #define MODEL       "models/player/assassin/assassin.mdl"   // Model
  6. #define DISTANCE    200                 // In units
  7.  
  8. native get_user_cash( client );
  9. native set_user_cash( client, cash );
  10. native get_user_bank( client );
  11. native set_user_bank( client, cash );
  12.  
  13. new const szPrefix[ ] = "AMXX";
  14.  
  15. new szItem[ 128 ], szFile[ 256 ], entNPC, bool: bInMenu[ 33 ], bool: bMadeNPC, Float: fNPCOrigin[ 3 ];
  16.  
  17. public plugin_init()
  18. {
  19.     register_plugin( "NPC Bank", "1.0", "Rejack" );
  20.    
  21.     /* Loading NPC */
  22.    
  23.     static szConfigsFolder[ 32 ];
  24.    
  25.     get_mapname( szFile, charsmax( szFile ) );
  26.    
  27.     get_configsdir( szConfigsFolder, charsmax( szConfigsFolder ) );
  28.    
  29.     format( szFile, charsmax( szFile ), "%s/NPC/%s.coord", szConfigsFolder, szFile );
  30.    
  31.     set_task( 3.5, "taskLoadNPC" );
  32.    
  33.     /* Forwards */
  34.    
  35.     register_forward( FM_Touch, "FwdTouch" );
  36.    
  37.     /* Events */
  38.    
  39.     register_event( "ResetHUD", "evResetHUD", "be" );
  40.    
  41.     /* Commands */
  42.    
  43.     register_concmd( "create_npc", "CmdCreateNPC", ADMIN_RCON, "- Create a Banker NPC" );
  44.    
  45.     register_concmd( "client_deposit", "CmdDeposit" );
  46.    
  47.     register_concmd( "client_withdraw", "CmdWithdraw" );
  48. }
  49.  
  50. public plugin_precache()
  51.     precache_model( MODEL );
  52.  
  53. public client_putinserver( client )
  54.     bInMenu[ client ] = false;
  55.  
  56. public evResetHUD( client )
  57.     bInMenu[ client ] = false;
  58.    
  59. public taskLoadNPC()
  60.     CmdCreateNPC( 0 );
  61.  
  62. public FwdTouch( ent, client )
  63. {
  64.     if ( !is_user_alive( client ) )
  65.         return 1;
  66.    
  67.     new szClass[ 32 ];
  68.     pev( ent, pev_classname, szClass, charsmax( szClass ) );
  69.    
  70.     if ( equali( szClass, "banker" ) && !bInMenu[ client ] )
  71.         return CmdBankMenu( client );
  72.    
  73.     return 1;
  74. }
  75.  
  76. public CmdBankMenu( client )
  77. {
  78.     if ( !is_distance( client ) )
  79.         return ColorPrint( client, "You're too far from the^3 NPC Banker^1." );
  80.    
  81.     bInMenu[ client ] = true;
  82.    
  83.     formatex( szItem, charsmax( szItem ), "\r%s \d|\y NPC Banker", szPrefix );
  84.    
  85.     new Menu = menu_create( szItem, "SubBankMenu" );
  86.    
  87.     menu_additem( Menu, "\yDeposit\d cash into your bank account^n" );
  88.    
  89.     menu_additem( Menu, "\rWithdraw\d cash from your bank account^n^n" );
  90.    
  91.     formatex( szItem, charsmax( szItem ), "\dYou have\r %i Cash^n\dYou have\r %i Cash\d in the bank^nYou have a total of\r %i Cash",
  92.     get_user_cash( client ), get_user_bank( client ), get_user_cash( client ) + get_user_bank( client ) );
  93.    
  94.     menu_addtext( Menu, szItem, 0 );
  95.    
  96.     menu_display( client, Menu );
  97.    
  98.     return 1;
  99. }
  100.  
  101. public SubBankMenu( client, Menu, Item )
  102. {
  103.     bInMenu[ client ] = false;
  104.    
  105.     if ( Item == MENU_EXIT )
  106.     {
  107.         menu_destroy( Menu );
  108.        
  109.         return 1;
  110.     }
  111.    
  112.     if ( !is_distance( client ) )
  113.         return ColorPrint( client, "You're too far from the^3 NPC Banker^1." );
  114.    
  115.     switch ( Item )
  116.     {
  117.         case 0:
  118.             client_cmd( client, "messagemode client_deposit" );
  119.        
  120.         case 1:
  121.             client_cmd( client, "messagemode client_withdraw" );
  122.     }
  123.    
  124.     menu_destroy( Menu );
  125.    
  126.     return 1;
  127. }
  128.  
  129. public CmdDeposit( client )
  130. {
  131.     if ( !is_distance( client ) )
  132.         return ColorPrint( client, "You're too far from the^3 NPC Banker^1." );
  133.    
  134.     static szArgs[ 128 ], Amount;
  135.    
  136.     read_args( szArgs, charsmax( szArgs ) );
  137.    
  138.     remove_quotes( szArgs );
  139.    
  140.     Amount = str_to_num( szArgs );
  141.    
  142.     if ( !is_str_num( szArgs ) )
  143.     {
  144.         client_cmd( client, "messagemode client_deposit" );
  145.        
  146.         return ColorPrint( client, "Please use valid numbers." );
  147.     }
  148.    
  149.     if ( Amount > get_user_cash( client ) )
  150.     {
  151.         client_cmd( client, "messagemode client_deposit" );
  152.        
  153.         return ColorPrint( client, "You don't have this amount of cash." );
  154.     }
  155.    
  156.     set_user_bank( client, get_user_bank( client ) + Amount );
  157.    
  158.     set_user_cash( client, get_user_cash( client ) - Amount );
  159.    
  160.     ColorPrint( client, "You made a^3 deposit^1 of^4 %i^1 cash to the bank.", Amount );
  161.    
  162.     return CmdBankMenu( client );
  163. }
  164.  
  165. public CmdWithdraw( client )
  166. {
  167.     if ( !is_distance( client ) )
  168.         return ColorPrint( client, "You're too far from the^3 NPC Banker^1." );
  169.    
  170.     static szArgs[ 128 ], Amount;
  171.    
  172.     read_args( szArgs, charsmax( szArgs ) );
  173.    
  174.     remove_quotes( szArgs );
  175.    
  176.     Amount = str_to_num( szArgs );
  177.    
  178.     if ( !is_str_num( szArgs ) )
  179.     {
  180.         client_cmd( client, "messagemode client_withdraw" );
  181.        
  182.         return ColorPrint( client, "Please use valid numbers." );
  183.     }
  184.    
  185.     if ( Amount > get_user_bank( client ) )
  186.     {
  187.         client_cmd( client, "messagemode client_withdraw" );
  188.        
  189.         return ColorPrint( client, "You don't have this amount of cash in the bank." );
  190.     }
  191.    
  192.     set_user_bank( client, get_user_bank( client ) - Amount );
  193.    
  194.     set_user_cash( client, get_user_cash( client ) + Amount );
  195.    
  196.     ColorPrint( client, "You made a^3 withdraw^1 of^4 %i^1 cash from the bank.", Amount );
  197.    
  198.     return CmdBankMenu( client );
  199. }
  200.  
  201. public CmdCreateNPC( client )
  202. {
  203.     if ( bMadeNPC && pev_valid( entNPC ) )
  204.         engfunc( EngFunc_RemoveEntity, entNPC );
  205.    
  206.     bMadeNPC = true;
  207.    
  208.     static Float: iOrigin[ 3 ], Float: fAngles[ 3 ];
  209.    
  210.     if ( client > 0 )
  211.     {
  212.         pev( client, pev_origin, iOrigin );
  213.        
  214.         pev( client, pev_angles, fAngles );
  215.     }
  216.    
  217.     else
  218.     {
  219.         static File;
  220.        
  221.         File = fopen( szFile, "r" );
  222.        
  223.         if ( File ) /* File Exists */
  224.         {
  225.             static szData[ 128 ], i; i = 0;
  226.            
  227.             while ( !feof( File ) )
  228.             {
  229.                 fgets( File, szData, charsmax( szData ) );
  230.                
  231.                 if ( i < 3 )    // 1 - 3 = Origin
  232.                     iOrigin[ i ] = str_to_float( szData );
  233.                
  234.                 else if ( i > 2 )
  235.                     fAngles[ i - 3 ] = str_to_float( szData );
  236.                
  237.                 i++;
  238.             }
  239.            
  240.             fclose( File );
  241.         }
  242.        
  243.         else    /* File was not found */
  244.         {
  245.             log_amx( "Error: Could not create an NPC Banker." );
  246.            
  247.             log_amx( "Error: The file ^"%s^" was not found!", szFile );
  248.            
  249.             return 1;
  250.         }
  251.     }
  252.    
  253.     /* Creating the NPC ent */
  254.    
  255.     entNPC = engfunc( EngFunc_CreateNamedEntity, engfunc( EngFunc_AllocString, "info_target" ) );
  256.    
  257.     engfunc( EngFunc_SetModel, entNPC, MODEL );
  258.    
  259.     engfunc( EngFunc_SetSize, entNPC, Float: { -17.0, -17.0, -36.0 }, Float: { 17.0, 17.0, 36.0 } );
  260.    
  261.     fAngles[ 0 ] = 0.0;
  262.    
  263.     set_pev( entNPC, pev_angles, fAngles );
  264.    
  265.     engfunc( EngFunc_SetOrigin, entNPC, iOrigin );
  266.    
  267.     if ( client > 0 )
  268.     {
  269.         iOrigin[ 2 ] += 80;
  270.        
  271.         engfunc( EngFunc_SetOrigin, client, iOrigin );
  272.     }
  273.    
  274.     set_pev( entNPC, pev_solid, SOLID_BBOX );
  275.    
  276.     set_pev( entNPC, pev_classname, "banker" );
  277.    
  278.     /* Setting Default Animation */
  279.    
  280.     set_pev( entNPC, pev_animtime, 2.0 );
  281.    
  282.     set_pev( entNPC, pev_framerate, 1.0 );
  283.    
  284.     set_pev( entNPC, pev_sequence, 1 );
  285.    
  286.     /* Setting bones positions */
  287.    
  288.     set_pev( entNPC, pev_controller_0, 125 );
  289.    
  290.     set_pev( entNPC, pev_controller_1, 125 );
  291.    
  292.     set_pev( entNPC, pev_controller_2, 125 );
  293.    
  294.     set_pev( entNPC, pev_controller_3, 125 );
  295.    
  296.     /* Setting damage and HP */
  297.    
  298.     set_pev( entNPC, pev_takedamage, 0.0 );
  299.    
  300.     /* Drop NPC to the ground */
  301.    
  302.     engfunc( EngFunc_DropToFloor, entNPC );
  303.    
  304.     /* Save the NPC to the file */
  305.    
  306.     if ( client > 0 )
  307.     {
  308.         static File;
  309.        
  310.         File = fopen( szFile, "w+" );
  311.        
  312.         if ( File )
  313.         {
  314.             fprintf( File, "%f^n%f^n%f^n%f^n%f^n%f", iOrigin[ 0 ], iOrigin[ 1 ], iOrigin[ 2 ], fAngles[ 0 ], fAngles[ 1 ], fAngles[ 2 ] );
  315.            
  316.             fclose( File );
  317.            
  318.             ColorPrint( client, "You successfuly created an^3 NPC Banker^1." );
  319.         }
  320.     }
  321.    
  322.     fNPCOrigin = iOrigin;
  323.    
  324.     return FMRES_HANDLED;
  325. }
  326.  
  327. /* Stocks */
  328.  
  329. stock bool: is_distance( const client )
  330. {
  331.     static Float: iOrigin[ 3 ];
  332.    
  333.     pev( client, pev_origin, iOrigin );
  334.    
  335.     if ( get_distance_f( iOrigin, fNPCOrigin ) <= DISTANCE )
  336.         return true;
  337.    
  338.     return false;
  339. }
  340.  
  341. stock ColorPrint( const index, const string[], any:... )
  342. {
  343.     new szMsg[ 191 ], Players[ 32 ], PNum = 1;
  344.    
  345.     static iLen; iLen = formatex( szMsg, charsmax( szMsg ), "^4[%s]^1 ", szPrefix );
  346.    
  347.     vformat( szMsg[ iLen ], charsmax( szMsg ) - iLen, string, 3 );
  348.    
  349.     if ( index )
  350.         Players[ 0 ] = index;
  351.    
  352.     else
  353.         get_players( Players, PNum, "ch" );
  354.    
  355.     for ( new i; i < PNum; i++ )
  356.     {
  357.         if( is_user_connected( Players[ i ] ) )
  358.         {
  359.             message_begin( MSG_ONE_UNRELIABLE, get_user_msgid( "SayText" ), _, Players[ i ] );
  360.            
  361.             write_byte( Players[ i ] );
  362.            
  363.             write_string( szMsg );
  364.            
  365.             message_end( );
  366.         }
  367.     }
  368.    
  369.     return 1;
  370. }
Advertisement
Add Comment
Please, Sign In to add comment