Edit: Added delete command to delete origins from the file. Also now uses configs/map_coords folder. There is a separate coord file used for each map. Added random command to teleport player to random origin from file.
save -player says this in game to save his current location to the current map coord file.
random -player says this in game to teleport to random origin saved in the current map coord file.
read - display in server console all saved origins for current map.
delete # - delete specified coord index (as printed from read command) from current map coord file.
PHP Code:
#include <amxmodx>
#include <amxmisc>
#include <fun>
new const szFileName[] = "coords";
new const OriginSize = 3 * BLOCK_INT;
new szFile[ 64 ];
public plugin_init()
{
register_plugin( "Save Origins" , "0.3" , "bugsy" );
register_clcmd( "say save" , "SaveOrigin" );
register_clcmd( "say random" , "TeleportRandom" );
register_concmd( "read" , "ReadOrigins" );
register_concmd( "delete" , "DeleteOrigin" );
}
public plugin_cfg()
{
new szMap[ 32 ] , iPos;
get_mapname( szMap , charsmax( szMap ) );
iPos = formatex( szFile[ get_configsdir( szFile , charsmax( szFile ) ) ] , charsmax( szFile ) , "/map_coords" ) + sizeof( szFileName ) + 15;
if ( !dir_exists( szFile ) )
mkdir( szFile );
formatex( szFile[ iPos ] , charsmax( szFile ) - iPos , "/%s_%s" , szMap , szFileName );
}
public SaveOrigin( id )
{
new iFile , iOrigin[ 3 ];
if ( ( iFile = fopen( szFile , "ab" ) ) )
{
get_user_origin( id , iOrigin );
fwrite_raw( iFile , iOrigin , sizeof( iOrigin ) , BLOCK_INT );
fclose( iFile );
client_print( id , print_chat , "Origin saved: [ %d , %d , %d ]" , iOrigin[ 0 ] , iOrigin[ 1 ] , iOrigin[ 2 ] );
}
}
public TeleportRandom( id )
{
new iOrigin[ 3 ];
GetRandomOrigin( iOrigin );
set_user_origin( id , iOrigin );
}
public ReadOrigins()
{
new iFile , iOrigin[ 3 ] , iCount;
if ( ( iFile = fopen( szFile , "rb" ) ) )
{
iCount = file_size( szFile , 0 ) / OriginSize;
server_print( "%d origins exist" , iCount );
for ( new i = 1 ; i <= iCount ; i++ )
{
fread_raw( iFile , iOrigin , sizeof( iOrigin ) , BLOCK_INT );
server_print( "%d - [ %d , %d , %d ]" , i , iOrigin[ 0 ] , iOrigin[ 1 ] , iOrigin[ 2 ] );
}
fclose( iFile );
}
}
public DeleteOrigin()
{
new iFile , iNewFile , iOrigin[ 3 ] , iCount , szArg[ 4 ] , iDeleteIndex , szTmpFile[ 64 ];
if ( !read_argv( 1 , szArg , charsmax( szArg ) ) )
{
server_print( "No coord index specified to delete." );
return;
}
iDeleteIndex = str_to_num( szArg );
iCount = file_size( szFile , 0 ) / OriginSize;
if ( !iCount )
{
server_print( "No coords exists for this map." );
return;
}
else if ( !( 1 <= iDeleteIndex <= iCount ) )
{
server_print( "Invalid coord index specified. Must be in range of 1-%d." , iCount );
return;
}
formatex( szTmpFile , charsmax( szTmpFile ) , "%s_tmp" , szFile );
rename_file( szFile , szTmpFile , 1 );
if ( ( iFile = fopen( szTmpFile , "rb" ) ) )
{
if ( ( iNewFile = fopen( szFile , "wb" ) ) )
{
for ( new i = 1 ; i <= iCount ; i++ )
{
fread_raw( iFile , iOrigin , sizeof( iOrigin ) , BLOCK_INT );
if ( i == iDeleteIndex )
{
server_print( "Deleted: %d - [ %d , %d , %d ]" , iDeleteIndex , iOrigin[ 0 ] , iOrigin[ 1 ] , iOrigin[ 2 ] );
}
else
{
fwrite_raw( iNewFile , iOrigin , sizeof( iOrigin ) , BLOCK_INT );
}
}
fclose( iNewFile );
}
fclose( iFile );
}
delete_file( szTmpFile );
}
public GetRandomOrigin( iOrigin[ 3 ] )
{
new iFile;
if ( ( iFile = fopen( szFile , "rb" ) ) )
{
fseek( iFile , ( random( file_size( szFile , 0 ) / OriginSize ) ) * OriginSize , SEEK_SET );
fread_raw( iFile , iOrigin , sizeof( iOrigin ) , BLOCK_INT );
fclose( iFile );
}
}