Raised This Month: $32 Target: $400
 8% 

Reading and using values


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
safetymoose
Senior Member
Join Date: Feb 2015
Old 03-17-2015 , 18:49   Reading and using values
Reply With Quote #1

Hey guys, any help would be appreciated

I'm trying to read coordinates from a file, then use those coordinates as origins. I've hit an obstacle..
PHP Code:
New Float:origins[ ][ ] = {
    { 
x1y1z1 },
    { 
x2y2z2 },
    { 
x3y3z3 }
};

Plugin_init() {

    
LoadValues()

}

public 
LoadValues() {
    new 
file[200]
    new 
map[50]

    
//directory folder
    
get_configsdir(file199)
    
format(file199"%s/origins"file)
    
    
// Extension for files
    
get_mapname(map49)
    
format(file199"%s/%s.ini"filemap)
    
    if (!
file_exists(file))
    {
        
log_amx("File not found")
        return
    }
    
    new 
input[1000], line 0len
    
    
while( (line read_file(file line input 127 len) ) != 
    {
        if (!
strlen(input)  || (input[0] == ';')) continue;

        new 
data[20]
        
        
//Coordinates
        
strbreak(inputdata20input999);    origin_first[0] = str_to_float(data);
        
strbreak(inputdata20input999);    origin_first[1] = str_to_float(data);
        
strbreak(inputdata20input999);    origin_first[2] = str_to_float(data);

      
//etc...
     
}

    
origins[ ][ ] = {
        { 
origin_first[0], origin_first[1]. origin_first[2] },
        { 
origin_second[0], origin_second[1], origin_second[2] },
        { 
origin_third[0], origin_third[1], origin_third[2] }
    };

I am getting errors on the last part, where i want to set origins[][3] with the data from the file. It says "Assumed zero". How can i set them properly?
safetymoose is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 03-18-2015 , 11:48   Re: Reading and using values
Reply With Quote #2

Your code is crap, lol. Use new file natives and learn how to properly work with strings. Secondly, are you sure that origins din exists ?

PHP Code:
New Float:origins[ ][ ] = {
    { 
x1y1z1 },
    { 
x2y2z2 },
    { 
x3y3z3 }
}; 
Here you have a typo, it should be "," and "new", not "New"

PHP Code:
 origins[ ][ ] = {
        { 
origin_first[0], origin_first[1]. origin_first[2] },
        { 
origin_second[0], origin_second[1], origin_second[2] },
        { 
origin_third[0], origin_third[1], origin_third[2] }
    }; 
What ?

I did something like that before, hope this could help you. I need to know what you will do with this origins later, how you would use them ?

PHP Code:
#include <amxmodx>
#include <amxmisc>

public plugin_init()
{
    
LoadValues()
}

LoadValues()
{
    new const 
FolderName[] = "origins"
    
    
new HandleConfigsDir[160]
    
get_configsdir(HandleConfigsDircharsmax(HandleConfigsDir))
    
format(HandleConfigsDircharsmax(HandleConfigsDir), "%s/%s"HandleConfigsDirFolderName)
    
    if(!
dir_exists(HandleConfigsDir))
    {
        
mkdir(HandleConfigsDir)
    }
    
    new 
MapName[32]
    
get_mapname(MapNamecharsmax(MapName))
    
format(HandleConfigsDircharsmax(HandleConfigsDir), "%s/%s.ini"HandleConfigsDirMapName)
    
    new 
FilePointer fopen(HandleConfigsDir"rt")
    if(
FilePointer)
    {
            
        new 
FileData[100], XOrigin[10], YOrigin[10], ZOrigin[10], FloatFloatOrigin[10]
        while(!
feof(FilePointer))
        {
            
fgets(FilePointerFileDatacharsmax(FileData))
            
trim(FileData)
    
            if(!
FileData[0] || FileData[0] == ';' || FileData[0] == '#' || FileData[0] == '/')
            {
                continue
            }
            
parse
            
(
                
FileData,
                
XOrigincharsmax(XOrigin), 
                
YOrigincharsmax(YOrigin), 
                
ZOrigincharsmax(ZOrigin)
            )
    
            
FloatOrigin[0] = str_to_float(XOrigin)
            
FloatOrigin[1] = str_to_float(YOrigin)
            
FloatOrigin[2] = str_to_float(ZOrigin)
            
            
server_print("Debug: %f %f %f"FloatOrigin[0], FloatOrigin[1], FloatOrigin[2])
        }
        
fclose(FilePointer)
    }

In my file:
Code:
1.0 2.0 3.0
4.0 5.0 6.0
Get when running it:
Code:
Debug: 1.000000 2.000000 3.000000
Debug: 4.000000 5.000000 6.000000
Folder is created automatically, you have to create the file manually.

You see that it is working correctly, so, answer the question above if you want more help.
__________________

Last edited by HamletEagle; 03-18-2015 at 11:55.
HamletEagle is offline
safetymoose
Senior Member
Join Date: Feb 2015
Old 04-10-2015 , 14:28   Re: Reading and using values
Reply With Quote #3

I want to read map coordinates from a file, and use some as teleport origins, some as mins, maxs. So my question is how to properly store these values once they are read from the file, because i will need to compare them later on...
safetymoose is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 04-10-2015 , 15:18   Re: Reading and using values
Reply With Quote #4

Dynamic array. This seems to be the best way considering the short information you provided.
__________________

Last edited by HamletEagle; 04-10-2015 at 15:19.
HamletEagle is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-10-2015 , 19:12   Re: Reading and using values
Reply With Quote #5

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.

Commands:
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 BLOCK_INT;

new 
szFile64 ];

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 
szMap32 ] , iPos;

    
get_mapnameszMap charsmaxszMap ) );
    
iPos formatexszFileget_configsdirszFile charsmaxszFile ) ) ] , charsmaxszFile ) , "/map_coords" ) + sizeofszFileName ) + 15;
    
    if ( !
dir_existsszFile ) )
        
mkdirszFile );
        
    
formatexszFileiPos ] , charsmaxszFile ) - iPos "/%s_%s" szMap szFileName );
}

public 
SaveOriginid )
{
    new 
iFile iOrigin];
    
    if ( ( 
iFile fopenszFile "ab" ) ) )
    {
        
get_user_originid iOrigin );
        
        
fwrite_rawiFile iOrigin sizeofiOrigin ) , BLOCK_INT );
        
fcloseiFile );
        
        
client_printid print_chat "Origin saved: [ %d , %d , %d ]" iOrigin] , iOrigin] , iOrigin] );
    }
}

public 
TeleportRandomid )
{
    new 
iOrigin];
    
    
GetRandomOriginiOrigin );
    
set_user_originid iOrigin );
}

public 
ReadOrigins()
{
    new 
iFile iOrigin] , iCount;
    
    if ( ( 
iFile fopenszFile "rb" ) ) )
    {
        
iCount file_sizeszFile ) / OriginSize;
        
        
server_print"%d origins exist" iCount );
        
        for ( new 
<= iCount i++ )
        {
            
fread_rawiFile iOrigin sizeofiOrigin ) , BLOCK_INT );
            
server_print"%d - [ %d , %d , %d ]" ,  iOrigin] , iOrigin] , iOrigin] );
        }
    
        
fcloseiFile );
    }
}

public 
DeleteOrigin()
{
    new 
iFile iNewFile iOrigin] , iCount szArg] , iDeleteIndex szTmpFile64 ];
    
    if ( !
read_argvszArg charsmaxszArg ) ) )
    {
        
server_print"No coord index specified to delete." );
        return;
    }
    
    
iDeleteIndex str_to_numszArg );
    
iCount file_sizeszFile ) / OriginSize;
        
    if ( !
iCount )
    {
        
server_print"No coords exists for this map." );
        return;
    }
    else if ( !( 
<= iDeleteIndex <= iCount ) ) 
    {
        
server_print"Invalid coord index specified. Must be in range of 1-%d." iCount );
        return;
    }
        
    
formatexszTmpFile charsmaxszTmpFile ) , "%s_tmp" szFile );
    
rename_fileszFile szTmpFile );
    
    if ( ( 
iFile fopenszTmpFile "rb" ) ) )
    {
        if ( ( 
iNewFile fopenszFile "wb" ) ) )
        {
            for ( new 
<= iCount i++ )
            {
                
fread_rawiFile iOrigin sizeofiOrigin ) , BLOCK_INT );
                
                if ( 
== iDeleteIndex )
                {
                    
server_print"Deleted: %d - [ %d , %d , %d ]" iDeleteIndex iOrigin] , iOrigin] , iOrigin] );    
                }
                else
                {
                    
fwrite_rawiNewFile iOrigin sizeofiOrigin ) , BLOCK_INT );
                }
            }
        
            
fcloseiNewFile );
        }
        
fcloseiFile );
    }

    
delete_fileszTmpFile );
}

public 
GetRandomOriginiOrigin] )
{
    new 
iFile;
    
    if ( ( 
iFile fopenszFile "rb" ) ) )
    {
        
fseekiFile , ( randomfile_sizeszFile ) / OriginSize ) ) * OriginSize SEEK_SET );
        
fread_rawiFile iOrigin sizeofiOrigin ) , BLOCK_INT );
        
        
fcloseiFile );
    }    

__________________

Last edited by Bugsy; 04-12-2015 at 11:28.
Bugsy is offline
Old 04-11-2015, 02:55
HamletEagle
This message has been deleted by HamletEagle.
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 20:28.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode