AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved Equivalent of SubString() in AMXX (https://forums.alliedmods.net/showthread.php?t=338801)

Rirre 07-26-2022 17:25

Equivalent of SubString() in AMXX
 
For some reason show_motd() cuts the message at 48 characters in Sven Co-op 5.0+.
So I have been googling around for an alternative and found this AS (AngelScript) script functioning for this game.
I gave it a try to convert it and got stuck at the SubString() part. Don't know how to proceed from there though.
This is the original AS code:
Code:
void UTIL_ShowMOTD( edict_t@ client, string motd, int mlen, const string name ) {     const string hostname = g_EngineFuncs.CVarGetString( "hostname" ); //g_pvHostname.GetString();     if ( !hostname.IsEmpty() && hostname != name )     {         NetworkMessage msgname( MSG_ONE, NetworkMessages::ServerName, g_vecZero, client );         msgname.WriteString( name );         msgname.End();     }     string chunk;     int a = 0;     while ( mlen > a )     {         chunk = motd.SubString( a, a + MAX_MOTD_CHUNK > mlen ? mlen - a : MAX_MOTD_CHUNK );         a += MAX_MOTD_CHUNK;         NetworkMessage msgmotd( MSG_ONE, NetworkMessages::MOTD, g_vecZero, client );         msgmotd.WriteByte( chunk.Length() == MAX_MOTD_CHUNK ? uint8( 0 ) : uint8( 1 ) ); // 0 means there is still more message to come         msgmotd.WriteString( chunk );         msgmotd.End();      }         if ( !hostname.IsEmpty() && hostname != name )     {         NetworkMessage msghostname( MSG_ONE, NetworkMessages::ServerName, g_vecZero, client );         msghostname.WriteString( hostname );         msghostname.End();     } }

How far I have come with the conversion to AMXX:
Code:
stock UTIL_ShowMOTD(player, szMessage[], iLen, szHead[]) {     new szServerName[MAX_NAME_LENGTH];     get_cvar_string("hostname", szServerName, charsmax(szServerName));     Util_ServerName(player, szHead);     new chunk[MAX_MOTD_CHUNK];     new a = 0;     while( iLen > a )     {         copy(chunk, sizeof(chunk), szMessage);         // The equivalent of SubString() here         a += MAX_MOTD_CHUNK;         message_begin(player ? MSG_ONE : MSG_ALL, get_user_msgid("MOTD"), _, player);         write_byte(strlen(chunk) == MAX_MOTD_CHUNK ? 0 : 1);         write_string(chunk);         message_end();     }     Util_ServerName(player, szServerName); }

jimaway 07-26-2022 17:53

Re: Equivalent of SubString() in AMXX
 
Code:
while( iLen > a )     {         a += copy(chunk, sizeof(chunk), szMessage[a]);         message_begin(player ? MSG_ONE : MSG_ALL, get_user_msgid("MOTD"), _, player);         write_byte(strlen(chunk) == MAX_MOTD_CHUNK ? 0 : 1);         write_string(chunk);         message_end();     }

Rirre 07-26-2022 18:09

Re: Equivalent of SubString() in AMXX
 
Quote:

Originally Posted by jimaway (Post 2784940)
Code:
while( iLen > a )     {         a += copy(chunk, sizeof(chunk), szMessage[a]);         message_begin(player ? MSG_ONE : MSG_ALL, get_user_msgid("MOTD"), _, player);         write_byte(strlen(chunk) == MAX_MOTD_CHUNK ? 0 : 1);         write_string(chunk);         message_end();     }

Thanks, but this caused a infinite loop.

Rirre 07-26-2022 18:27

Re: Equivalent of SubString() in AMXX
 
Found the solution:
Code:
while( iLen > a ) {     copy( chunk, a + MAX_MOTD_CHUNK > iLen ? iLen - a : MAX_MOTD_CHUNK, szMessage[a] );     a += MAX_MOTD_CHUNK;     message_begin(player ? MSG_ONE : MSG_ALL, get_user_msgid("MOTD"), _, player);     write_byte(strlen(chunk) == MAX_MOTD_CHUNK ? 0 : 1);     write_string(chunk);     message_end(); }

Bugsy 07-26-2022 19:34

Re: Equivalent of SubString() in AMXX
 
Minor efficiency tweaks
PHP Code:

new iChunkSize;
new 
msgMOTD get_user_msgid("MOTD");
    
while( 
iLen )
{
    
iChunkSize copychunkMAX_MOTD_CHUNK iLen iLen MAX_MOTD_CHUNKszMessage[a] );
    
+= MAX_MOTD_CHUNK;
        
    
message_begin(player MSG_ONE MSG_ALLmsgMOTD_player);
    
write_byte(iChunkSize == MAX_MOTD_CHUNK 1);
    
write_string(chunk);
    
message_end();



Rirre 07-27-2022 03:20

Re: Equivalent of SubString() in AMXX
 
Quote:

Originally Posted by Bugsy (Post 2784953)
Minor efficiency tweaks
PHP Code:

new iChunkSize;
new 
msgMOTD get_user_msgid("MOTD");
    
while( 
iLen )
{
    
iChunkSize copychunkMAX_MOTD_CHUNK iLen iLen MAX_MOTD_CHUNKszMessage[a] );
    
+= MAX_MOTD_CHUNK;
        
    
message_begin(player MSG_ONE MSG_ALLmsgMOTD_player);
    
write_byte(iChunkSize == MAX_MOTD_CHUNK 1);
    
write_string(chunk);
    
message_end();



Thank you. Appreciate it.


All times are GMT -4. The time now is 15:38.

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