[STOCK] Creating customizable fog

For scripting examples for learning how to use AMXX.
User avatar
Archive-Poster
Posts: 1267
Gender:
United States of America

[STOCK] Creating customizable fog

Post by Archive-Poster »

Here is a stock i made for creating fog ingame without restarting the server or changing the map

Code: Select all

stock msg_create_fog( iRed, iGreen, iBlue, iDensity )
{
    // Fog density offsets [Thnx to DA]
    new const fog_density[ ] = { 0, 0, 0, 0, 111, 18, 3, 58, 111, 18, 125, 58, 66, 96, 27, 59, 90, 101, 60, 59, 90,
            101, 68, 59, 10, 41, 95, 59, 111, 18, 125, 59, 111, 18, 3, 60, 68, 116, 19, 60 }
    
    // Get the amount of density
    new dens
    dens = ( 4 * iDensity )
    
    // The fog message
    message_begin( MSG_BROADCAST, get_user_msgid( "Fog" ), { 0,0,0 }, 0 )
    write_byte( iRed ) // Red
    write_byte( iGreen ) // Green
    write_byte( iBlue ) // Blue
    write_byte( fog_density[ dens ] ) // SD
    write_byte( fog_density[ dens + 1 ] ) // ED
    write_byte( fog_density[ dens + 2 ] ) // D1
    write_byte( fog_density[ dens + 3 ] ) // D2
    message_end( )
}
Its usage is simple:
For eg if you want red fog then do this:

Code: Select all

msg_create_fog( 255, 0, 0, 1 ) 
1st argument is the red color content of the fog [Must be a num between 1-255]
2nd argument is the green color content of the fog [Must be a num between 1-255]
3rd argument is the blue color content of the fog [Must be a num between 1-255]
4th argument is the amount of density of the fog [Must be a num between 1-9]

To disable fog you can do this:

Code: Select all

msg_create_fog( 0, 0, 0, 0 ) 
- For creating black fog [Sounds wierd] you can set all colors to 0
- The fog only works if you have set your cs display settings to OpenGL and you should set this cvar gl_fog to 1 in order to see the fog
- You can change the fog color and density in-game [Using cvars] without restarting the map
User avatar
Archive-Poster
Posts: 1267
Gender:
United States of America

Re: [STOCK] Creating customizable fog

Post by Archive-Poster »

You have fps drops when using this? (I get a drop from 101 to 40 when using smoke)

Change MSG_ALL to MSG_BROADCAST
Can u tell me whats the difference between them ?
MSG_ALL: tries to send a reliable msg to all players and wait for that

MSG_BROADCAST: send a msg and trust that it will be read by each player

Using MSG_ALL you can get some crash with a lagged server or a high cpu usage.
It's a client-side operation, server just informs that the player should have fog enabled and the client does the rest.

However, this only works on OpenGL, on D3D and software fog doesn't work.
User avatar
Archive-Poster
Posts: 1267
Gender:
United States of America

Re: [STOCK] Creating customizable fog

Post by Archive-Poster »

Here a better stock which is not hardcoded with predefined density value, and add more checks.
This way, you can put any value between 0.0001 and 0.25, so you can configure more finely.

I don't think it's the real limit but after some testing, below 0.0001 you don't see the difference and above 0.25 you don't see you hand/weapon anymore. So I guess it's enough.

Code: Select all

stock CreateFog ( const index = 0, const red = 127, const green = 127, const blue = 127, const Float:density_f = 0.001, bool:clear = false )
{
    static msgFog;
    
    if ( msgFog || ( msgFog = get_user_msgid( "Fog" ) ) )
    {
        new density = _:floatclamp( density_f, 0.0001, 0.25 ) * _:!clear;
        
        message_begin( index ? MSG_ONE_UNRELIABLE : MSG_BROADCAST, msgFog, .player = index );
        write_byte( clamp( red  , 0, 255 ) );
        write_byte( clamp( green, 0, 255 ) );
        write_byte( clamp( blue , 0, 255 ) );
        write_long( _:density );
        message_end();
    }
}
Examples :

Sending/Clearing for all players :

Code: Select all

CreateFog( 0, 133, 6, 6, 0.003 );

Code: Select all

CreateFog( 0, .clear = true );
Sending/Clearing for one player :

Code: Select all

CreateFog( id, 133, 6, 6, 0.003 );

Code: Select all

CreateFog( id, .clear = true );
User avatar
Archive-Poster
Posts: 1267
Gender:
United States of America

Re: [STOCK] Creating customizable fog

Post by Archive-Poster »

How is this way, beside that all player gets the fog?

Code: Select all

#include < amxmodx >
#include < fakemeta_util >

public plugin_precache( )
{
    new iEntity = fm_create_entity( "env_fog" );
    
    fm_set_kvd( iEntity, "density", "0.0005", "env_fog" );
    fm_set_kvd( iEntity, "rendercolor", "180 0 0", "env_fog" );
} 
You cant change the color density and color every round (if you want to) unlike this one which sets the color and density of the fog on map start only.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest