View Single Post
hellmonja
Senior Member
Join Date: Oct 2015
Old 10-04-2019 , 03:34   Re: [HELP] Emit Sound on Coordinates
Reply With Quote #9

Hello again, guys! After much fiddling, I've decided to go with Arkshines codes since it seems most convenient. So what's left is establishing the coordinates where the sounds will be emitted, which I've done as well with a test plugin. The problem is I'm not sure if my method is the most efficient way of doing this.

What I intend to do is when a player fires a gun, 4 points forming a cross relative to his coordinates will be the origin of the emitted sound. Height is fixed by the way, just +100.0 of the players coord. All I did was duplicate the code four times with 4 different combinations of coordinates:
PHP Code:
#include <amxmodx>
#include <engine>
#include <fakemeta> 

#define PLUGIN "Distant Sounds Test"
#define VERSION "1.0"

#define DIST         300.0
#define SND_DIST     1230.0
#define SND_HEIGHT     100.0

new const Sound[] = "misc/killChicken.wav";
new 
SoundIndex;
new 
cvar_distsndxcvar_distsndycvar_distsndz;

#define clamp_byte(%1)     ( clamp( %1, 0, 255 ) ) 
#define write_coord_f(%1)  ( engfunc( EngFunc_WriteCoord, %1 ) )
#define null_vector        ( Float:{ 0.0, 0.0, 0.0 } )

public plugin_init()
{
    
register_plugin(PLUGINVERSION"Arkshine & hellmonja");
    
    
register_concmd("debug""Debug");
    
    
cvar_distsndx register_cvar("ds_x""0.0");
    
cvar_distsndy register_cvar("ds_y""0.0");
    
cvar_distsndz register_cvar("ds_z""0.0");
}

public 
plugin_precache()
{
    
SoundIndex precache_soundSound );
    
precache_model("models/chick.mdl");
}

public 
Debug(user)
{
    new 
Float:pl_origin[3];
    new 
Float:snd_origin[3];
    new 
ent
    
///////////////////////////// 1 ///////////////////////////////////////////////////    
    
pev(userpev_originpl_origin); 
    
    
snd_origin[0] = pl_origin[0] + DIST;
    
snd_origin[1] = pl_origin[1];
    
snd_origin[2] = pl_origin[2] + SND_HEIGHT;
    
    
ent create_entity("info_target")
    
entity_set_origin(ent snd_origin)
    
entity_set_model(ent,"models/chick.mdl");
    
    
spawnStaticSound(0snd_originSoundIndexVOL_NORMATTN_NORMPITCH_NORM, .flags 0);

    
set_task(0.5"remove_ent"ent);
///////////////////////////////////////////////////////////////////////////////////

///////////////////////////// 2 ///////////////////////////////////////////////////    
    
pev(userpev_originpl_origin); 
    
    
snd_origin[0] = pl_origin[0] - DIST;
    
snd_origin[1] = pl_origin[1];
    
snd_origin[2] = pl_origin[2] + SND_HEIGHT;
    
    
ent create_entity("info_target")
    
entity_set_origin(ent snd_origin)
    
entity_set_model(ent,"models/chick.mdl");
    
    
spawnStaticSound(0snd_originSoundIndexVOL_NORMATTN_NORMPITCH_NORM, .flags 0);

    
set_task(0.5"remove_ent"ent);
///////////////////////////////////////////////////////////////////////////////////

///////////////////////////// 3 ///////////////////////////////////////////////////    
    
pev(userpev_originpl_origin); 
    
    
snd_origin[0] = pl_origin[0];
    
snd_origin[1] = pl_origin[1] + DIST;
    
snd_origin[2] = pl_origin[2] + SND_HEIGHT;
    
    
ent create_entity("info_target")
    
entity_set_origin(ent snd_origin)
    
entity_set_model(ent,"models/chick.mdl");
    
    
spawnStaticSound(0snd_originSoundIndexVOL_NORMATTN_NORMPITCH_NORM, .flags 0);

    
set_task(0.5"remove_ent"ent);
///////////////////////////////////////////////////////////////////////////////////

///////////////////////////// 4 ///////////////////////////////////////////////////    
    
pev(userpev_originpl_origin); 
    
    
snd_origin[0] = pl_origin[0];
    
snd_origin[1] = pl_origin[1] - DIST;
    
snd_origin[2] = pl_origin[2] + SND_HEIGHT;
    
    
ent create_entity("info_target")
    
entity_set_origin(ent snd_origin)
    
entity_set_model(ent,"models/chick.mdl");
    
    
spawnStaticSound(0snd_originSoundIndexVOL_NORMATTN_NORMPITCH_NORM, .flags 0);

    
set_task(0.5"remove_ent"ent);
///////////////////////////////////////////////////////////////////////////////////
    
    //    new Float:dist = entity_range(user, ent);
    //    client_print(user, print_chat, "DIST: %f", dist);

}

public 
remove_ent(ent)
{
    
remove_entity(ent);
}

stock spawnStaticSound( const index, const Float:origin[3], const soundIndex, const Float:vol, const Float:atten, const pitch, const flags 

    
message_beginindex MSG_ONE MSG_ALLSVC_SPAWNSTATICSOUND, .player index );
    {
        
write_coord_forigin[0] ); 
        
write_coord_forigin[1] ); 
        
write_coord_forigin[2] );
        
write_shortsoundIndex );
        
write_byteclamp_bytefloatroundvol 255 ) ) );
        
write_byteclamp_bytefloatroundatten 64 ) ) );
        
write_shortindex );        
        
write_bytepitch ); 
        
write_byteflags );   
    }
    
message_end();

Please don't mind the set_task function. It's just there so I have time to see where the chickens appear...
__________________
hellmonja is offline