Raised This Month: $51 Target: $400
 12% 

Solved Equivalent of SubString() in AMXX


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Rirre
Veteran Member
Join Date: Nov 2006
Old 07-26-2022 , 17:25   Equivalent of SubString() in AMXX
Reply With Quote #1

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); }

Last edited by Rirre; 07-26-2022 at 19:02.
Rirre is offline
jimaway
Heeeere's Jimmy!
Join Date: Jan 2009
Location: Estonia
Old 07-26-2022 , 17:53   Re: Equivalent of SubString() in AMXX
Reply With Quote #2

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();     }
jimaway is offline
Rirre
Veteran Member
Join Date: Nov 2006
Old 07-26-2022 , 18:09   Re: Equivalent of SubString() in AMXX
Reply With Quote #3

Quote:
Originally Posted by jimaway View Post
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 is offline
Rirre
Veteran Member
Join Date: Nov 2006
Old 07-26-2022 , 18:27   Re: Equivalent of SubString() in AMXX
Reply With Quote #4

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(); }
Rirre is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 07-26-2022 , 19:34   Re: Equivalent of SubString() in AMXX
Reply With Quote #5

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();

__________________

Last edited by Bugsy; 07-26-2022 at 19:34.
Bugsy is offline
Rirre
Veteran Member
Join Date: Nov 2006
Old 07-27-2022 , 03:20   Re: Equivalent of SubString() in AMXX
Reply With Quote #6

Quote:
Originally Posted by Bugsy View Post
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.
Rirre is offline
Reply



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 17:55.


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