Jump to content

How to replicate this in MTA?


Cassandra

Recommended Posts

Does anyone know how to replicate this in MTA?

  
enum fInfo 
{ 
    fName[40], 
    fRanks[8] 
} 
new FactionInfo[MAX_FACTIONS][fInfo]; 
new bool:fCreated[MAX_FACTIONS]; 
  
  
stock FactionGetFreeSlot() 
{ 
    for(new i = 0; i < MAX_FACTIONS; i++) 
    { 
        if(!fCreated[i]) return i; 
    } 
     
    return -1; 
} 
stock FactionCreate(factionName[], factionRanks[]) 
{ 
    new factionid = FactionGetFreeSlot(); 
    format(FactionInfo[factionid][fName], 40, factionName); 
    //strcpy(FactionInfo[factionid][fName], factionName, 40); 
    for(new i = 0; i < 8; i++) 
    { 
        FactionInfo[factionid][fRanks][i] = factionRanks[i]; 
    } 
    fCreated[factionid] = true; 
     
    return factionid; 
} 
stock FactionLoad(factionID, file[]) 
{ 
    INI_ParseFile(file, "LoadFactionData", .bExtra = true, .extra = factionID); 
    FactionCreate(FactionInfo[factionID][fName], 
        FactionInfo[factionID][fRanks] 
    ); 
} 
stock FactionLoadAll() 
{ 
    new index = 0; 
    format(smallstrz, sizeof(smallstrz), #SERVER_NAME "/%s/%d.ini", FactionFolder, index); 
  
    while(fexist(smallstrz)) 
    { 
        FactionLoad(index, smallstrz); 
        index++; 
        format(smallstrz, sizeof(smallstrz), #SERVER_NAME "/%s/%d.ini", FactionFolder, index); 
    } 
     
    printf("Factions Loaded: %d", index); 
} 
forward LoadFactionData(factionID, name[], value[]); 
public LoadFactionData(factionID, name[], value[]) 
{ 
    new rank_parse[165]; 
    INI_String("name", FactionInfo[factionID][fName], 40); 
    INI_String("ranks", rank_parse, 165); 
    sscanf(rank_parse, "p<:>iiiiiiii", 
        FactionInfo[factionID][fRanks][0],  FactionInfo[factionID][fRanks][1],  FactionInfo[factionID][fRanks][2], 
        FactionInfo[factionID][fRanks][3],  FactionInfo[factionID][fRanks][4], FactionInfo[factionID][fRanks][5], 
        FactionInfo[factionID][fRanks][6], FactionInfo[factionID][fRanks][7] 
    ); 
  
    return 1; 
} 
stock FactionSave(factionID) 
{ 
    format(smallstrz, sizeof(smallstrz), #SERVER_NAME "/%s/%d.ini", FactionFolder, factionID); 
    new INI:file = INI_Open(smallstrz); 
    new rank_parse[165]; 
    format(rank_parse, sizeof(rank_parse), "%s", FactionInfo[factionID][fRanks][0]); 
    for(new i = 1; i < 8; i++) 
    { 
        format(rank_parse, sizeof(rank_parse), ":%s", FactionInfo[factionID][fRanks][i]); 
    } 
    INI_WriteString(file, "name", FactionInfo[factionID][fName]); 
    INI_WriteString(file, "ranks", rank_parse); 
    INI_Close(file); 
} 
stock FactionSaveAll() 
{ 
    new index = 0; 
     
    for(new i = 0; i < MAX_FACTIONS; i++) 
    { 
        if(fCreated[i]) 
        { 
            FactionSave(index); 
            index++; 
        } 
    } 
     
    printf("Factions Saved: %d", index); 
} 
  
  

Edited by Guest
Link to comment

- Basic LUA knowledge and..

fileCreate 
fileOpen 
fileRead 
fileWrite 
fileClose 
outputChatBox 

If you just don't have enough patience to create a basic interpreter to make something like INI files, use XML functions instead (if you're going to store a small amount of things).

Link to comment
- Basic LUA knowledge and..
fileCreate 
fileOpen 
fileRead 
fileWrite 
fileClose 
outputChatBox 

If you just don't have enough patience to create a basic interpreter to make something like INI files, use XML functions instead (if you're going to store a small amount of things).

I want to know the LUA knowledge of doing that. For example, multi-dimensional array and normal arrays which reference id and stuff. Thanks for your response.

Link to comment
function enum ( vars ) 
    assert ( vars, "no values in enum declaration" ); 
     
    for enum_name, int in pairs ( vars ) do 
        _G[enum_name] = int; 
    end 
end 
  
--Example: 
enum { COLOR_A = 1, COLOR_B = 2, COLOR_C = 3 }; 
  
local someValue; 
function setValue ( str ) 
    if ( str:find ( "a", 0 ) ) then 
        someValue = COLOR_A; 
    elseif ( str:find ( "b", 0 ) ) then 
        someValue = COLOR_B; 
    elseif ( str:find ( "c", 0 ) ) then 
        someValue = COLOR_C; 
    end 
end 
  
setValue ( "b" ); 
  
--lets see: 
if ( someValue == COLOR_A ) then 
    outputChatBox ( "COLOR_A" ); 
elseif ( someValue == COLOR_B ) then 
    outputChatBox ( "COLOR_B" ); 
elseif ( someValue == COLOR_C ) then 
    outputChatBox ( "COLOR_C" ); 
else 
    outputChatBox ( "Something has gone wrong." ); 
end 

But you'll still need to add 1, 2, 3, 4, etc. in each value unless you use strings instead of just COLOR_A, COLOR_B and you'd also need to change the for loop in the "enum" function, unlike C/C++ where it automatically sets a number to each value (in order).

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...