It creates an NPC but doesn't save it...
PHP Code:
#include < amxmodx >
#include < amxmisc >
#include < fakemeta >
#define MODEL "models/player/vip/vip.mdl" // Model
new const szPrefix[ ] = "AMXX";
new szFile[ 256 ], entNPC;
public plugin_init()
{
register_plugin( "NPC", "1.0", "Rejack" );
/* Loading NPC */
static szConfigsFolder[ 32 ];
get_mapname( szFile, charsmax( szFile ) );
get_configsdir( szConfigsFolder, charsmax( szConfigsFolder ) );
format( szFile, charsmax( szFile ), "%s/NPC/%s.coord", szConfigsFolder, szFile );
set_task( 3.5, "taskLoadNPC" );
register_concmd( "create_npc", "CmdCreateNPC", ADMIN_RCON, "- Create an NPC" );
}
public plugin_precache()
precache_model( MODEL );
public taskLoadNPC()
CmdCreateNPC( 0 );
public CmdCreateNPC( client )
{
static Float: iOrigin[ 3 ], Float: fAngles[ 3 ];
if ( client > 0 )
{
pev( client, pev_origin, iOrigin );
pev( client, pev_angles, fAngles );
}
else
{
static File;
static fData[ 6 ][ 128 ];
File = fopen( szFile, "r" );
if ( File ) /* File Exists */
{
static szData[ 128 ], i; i = 0;
while ( !feof( File ) )
{
fgets( File, szData, charsmax( szData ) );
if ( szData[ 0 ] == ';' || strlen( szData ) < 10 )
continue;
remove_quotes( szData );
replace_all( szData, charsmax( szData ), "#", " " );
parse( szData, fData[ 0 ], 127, fData[ 1 ], 127, fData[ 2 ], 127, fData[ 3 ], 127, fData[ 4 ], 127, fData[ 5 ], 127 );
for ( i = 0; i < 6; i++ )
{
if ( i < 3 ) // 1 - 3 = Origin
iOrigin[ i] = str_to_float( fData[ i ] );
else if ( i > 2 )
fAngles[ i - 3 ] = str_to_float( fData[ i - 3 ] );
}
create_npc( client, fAngles, iOrigin );
}
fclose( File );
}
else /* File was not found */
{
log_amx( "Error: Could not create an NPC." );
log_amx( "Error: The file ^"%s^" was not found!", szFile );
return 1;
}
}
/* Save the NPC to the file */
if ( client > 0 )
{
static iFile;
static szText[ 128 ];
iFile = fopen( szFile, "a" );
if ( iFile )
{
formatex( szText, charsmax( szText ), "^"%f#%f#%f#%f#%f#%f^"", iOrigin[ 0 ], iOrigin[ 1 ], iOrigin[ 2 ], fAngles[ 0 ], fAngles[ 1 ], fAngles[ 2 ] );
write_file( szFile, szText, -1 );
fclose( iFile );
create_npc( client, fAngles, iOrigin );
ColorPrint( client, "You successfuly created an^3 NPC^1." );
}
}
return FMRES_HANDLED;
}
/* Stocks */
stock ColorPrint( const index, const string[], any:... )
{
new szMsg[ 191 ], Players[ 32 ], PNum = 1;
static iLen; iLen = formatex( szMsg, charsmax( szMsg ), "^4[%s]^1 ", szPrefix );
vformat( szMsg[ iLen ], charsmax( szMsg ) - iLen, string, 3 );
if ( index )
Players[ 0 ] = index;
else
get_players( Players, PNum, "ch" );
for ( new i; i < PNum; i++ )
{
if( is_user_connected( Players[ i ] ) )
{
message_begin( MSG_ONE_UNRELIABLE, get_user_msgid( "SayText" ), _, Players[ i ] );
write_byte( Players[ i ] );
write_string( szMsg );
message_end( );
}
}
return 1;
}
stock create_npc( const index, Float: fAngles[ 3 ], Float: iOrigin[ 3 ] )
{
/* Creating the NPC ent */
entNPC = engfunc( EngFunc_CreateNamedEntity, engfunc( EngFunc_AllocString, "info_target" ) );
engfunc( EngFunc_SetModel, entNPC, MODEL );
engfunc( EngFunc_SetSize, entNPC, Float: { -17.0, -17.0, -36.0 }, Float: { 17.0, 17.0, 36.0 } );
fAngles[ 0 ] = 0.0;
set_pev( entNPC, pev_angles, fAngles );
engfunc( EngFunc_SetOrigin, entNPC, iOrigin );
if ( is_user_connected( index ) )
{
iOrigin[ 2 ] += 80;
engfunc( EngFunc_SetOrigin, index, iOrigin );
}
set_pev( entNPC, pev_solid, SOLID_BBOX );
set_pev( entNPC, pev_classname, "thenpc" );
/* Setting Default Animation */
set_pev( entNPC, pev_animtime, 2.0 );
set_pev( entNPC, pev_framerate, 1.0 );
set_pev( entNPC, pev_sequence, 1 );
/* Setting bones positions */
set_pev( entNPC, pev_controller_0, 125 );
set_pev( entNPC, pev_controller_1, 125 );
set_pev( entNPC, pev_controller_2, 125 );
set_pev( entNPC, pev_controller_3, 125 );
/* Setting damage and HP */
set_pev( entNPC, pev_takedamage, 0.0 );
/* Drop NPC to the ground */
engfunc( EngFunc_DropToFloor, entNPC );
}