AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Reproduce sound every third murder. (https://forums.alliedmods.net/showthread.php?t=193733)

Sutar 08-22-2012 10:33

Reproduce sound every third murder.
 
Here there is a plugin (I wrote myself) it reproduces sounds: multikill, megakill and other.

For example:
I killed 3 players and play sound multikill.
I killed 3 more players and play sound megakill.
I killed 3 more players and play sound ultrakill.
I killed 3 more players and play sound godlike.

But the problem is that if I killed 3 players, and then a further 3 plays sound godlike. ie passes 2 audio, megakill and ultrakill.

What's the problem?

Code:

#include <amxmodx>
#include <csx>
#include <cstrike>

new g_streakKills[33][1]

new g_Sounds[10][] =
{
        "multikill",
        "megakill",
        "ultrakill",
        "godlike",
        "rampage",
        "wickedsick",
        "killingspree",
        "unstoppable",
        "monsterkill",
        "ludacrisskill"
}

public plugin_init()
{
        register_plugin("Music Stats", "2.0", "Sutar")
}

public client_putinserver(id)
        g_streakKills[id] = {0}

public client_death(killer, victim, wpnindex, hitplace, TK)
{
        if(wpnindex == CSW_C4)
                return

        new headshot = (hitplace == HIT_HEAD) ? 1 : 0;
        new selfkill = (killer == victim) ? 1 : 0;

        if(!TK && !selfkill && killer)
        {
                g_streakKills[victim][0] = 0;
                g_streakKills[killer][0] += 1;
               
                new a = g_streakKills[killer][0] - 3;
               
                if((a > -1) && !(a % 3))
                {
                        if(a > 9)
                                a = 5;
                       
                        new file[32]
                        format(file, 31, "misc/%s", g_Sounds[a])
                        play_sound(file)
                        return
                }
        }

        return
}

public play_sound(sound[])
{
        new players[32], pnum
        get_players(players, pnum, "c")

        for(new i = 0; i < pnum; i++)
        {
                if(is_user_connecting(players[i]))
                        continue
               
                client_cmd(players[i], "spk %s", sound)
        }
}


Liverwiz 08-22-2012 11:02

Re: Reproduce sound every third murder.
 
There's a few things i have to say.
1. new g_streakKills[33][1] :arrow: new g_streakKills[33] Its a simple array, you don't need to say it only has one row; that is implied when created. And confused me a bit when i first read it.
2. all your sounds need to be precached and have a filetype extension (.wav .mp3 etc)
3. sounds need to be in the sounds folder. not misc (i'm pretty sure) -correct me if i'm wrong
4. There's a better way to do the check. Here it is...
Code:

        new headshot = (hitplace == HIT_HEAD) ? 1 : 0;
        new selfkill = (killer == victim) ? 1 : 0;

        if(!TK && !selfkill && killer)
        {
                g_streakKills[victim][0] = 0;
                g_streakKills[killer][0] += 1;
               
                new a = g_streakKills[killer][0] - 3;
               
                if((a > -1) && !(a % 3))
                {
                        if(a > 9)
                                a = 5;
                       
                        new file[32]
                        format(file, 31, "misc/%s", g_Sounds[a])
                        play_sound(file)
                        return
                }
        }

:arrow:
PHP Code:

        g_streakKills[victim] = 0;
        new 
g_streakKills[killer]++;    // Immediately store your increment into the temp var
        
        
if( == )     // all you need to worry about is if it is the third kill 
        
{
            if(
a/= 9)
                
5    // int division by 3 will get you what index you want
            
            
new file[32]
            
formatex(file31"misc/%s"g_Sounds[a])    // formatex is faster than format
            
play_sound(file)
            return
        } 

5. client_cmd(0, "spk %s", sound) Will work just fine without the loop. It'll be faster too and you won't need to check if user is alive. All around win.
6. You should also declare g_sounds as const new const g_sounds[][] = { Its just good habbit, because that never changes. But I'd suggest putting the entire file path into those strings just to make things a bit easier. (i.e. "sounds/multikill.wav", ) That way you can loop through g_sounds in plugin_precache and not need to add anything. Nor will you need to add anything later. All speeding up your code.

Sutar 08-22-2012 11:32

Re: Reproduce sound every third murder.
 
The best code will be like this?

Code:

#include <cstrike>

new g_streakKills[33]

new g_Sounds[10][] =
{
        "multikill",
        "megakill",
        "ultrakill",
        "godlike",
        "rampage",
        "wickedsick",
        "killingspree",
        "unstoppable",
        "monsterkill",
        "ludacrisskill"
}

public plugin_init()
{
        register_plugin("Music Stats", "2.0", "Sutar")
}

public client_putinserver(id)
        g_streakKills[id] = {0}

public client_death(killer, victim, wpnindex, hitplace, TK)
{
        if(wpnindex == CSW_C4)
                return

        new headshot = (hitplace == HIT_HEAD) ? 1 : 0;
        new selfkill = (killer == victim) ? 1 : 0;

        if(!TK && !selfkill && killer)
        {
                g_streakKills[victim] = 0;
                      new a = g_streakKills[killer]++;
       
                if(a % 3 == 0)
                {
                        if(a/= 3 > 9)
                                a = 5
           
                        client_cmd(0, "spk misc/%s", g_Sounds[a])
                        return
                }
        }

        return
}

I need to play a sound to the player, every 3 of his murder.

For example:
I killed 2 players - no sound.
I Killed 3 - there was a sound (multikill).
Following the sound, kill 3 more - 2 sound (megakill).

Liverwiz 08-22-2012 11:41

Re: Reproduce sound every third murder.
 
I'd say so. but you still need to prechace all the sound files and give them a file extension. see #2. 3. 6

EDIT: and i'm not sure the efficiency of client_death versus event_death But that isn't much of a problem to be honest....unless you really want to make the best code possible.

Exolent[jNr] 08-22-2012 11:51

Re: Reproduce sound every third murder.
 
Quote:

Originally Posted by Liverwiz (Post 1778882)
3. sounds need to be in the sounds folder. not misc (i'm pretty sure) -correct me if i'm wrong

First, when using precache_sound() and the "spk" command, the "sound/" folder is implied.
The "misc/" folder in front will transform to "sound/misc/" for both precache and the "spk" command, so you don't need to put it there.
Quote:

Originally Posted by Liverwiz (Post 1778882)
4. There's a better way to do the check. Here it is...
PHP Code:

        new g_streakKills[killer]++;    // Immediately store your increment into the temp var 


Actually, that will store the value before you add 1 to it, because the ++ is after the variable which makes it a POST increment.
This is the valid way:
PHP Code:

        new = ++g_streakKills[killer];    // Immediately store your increment into the temp var 

This way it increases the value in the streak kills array, and then stores that value into the "a" variable.
Quote:

Originally Posted by Liverwiz (Post 1778882)
PHP Code:

            if(a/= 9


That is just ugly and confusing. Just put it on separate lines:
PHP Code:

            a /= 3;
            if(
9

Quote:

Originally Posted by Liverwiz (Post 1778882)
6. ... But I'd suggest putting the entire file path into those strings just to make things a bit easier. (i.e. "sounds/multikill.wav", ) ...

Again, "sound" folder not needed.

Sutar 08-22-2012 11:51

Re: Reproduce sound every third murder.
 
Code:

//AMXXPC compile.exe
// by the AMX Mod X Dev Team

//// miscstats.sma
// D:\Server\test_minimap1\cstrike\addons\amxmodx\scripting\miscstats.sma(44) :
warning 213: tag mismatch
// Header size:            312 bytes
// Code size:            1292 bytes
// Data size:            1908 bytes
// Stack/heap size:      16384 bytes; estimated max. usage=44 cells (176 bytes)
// Total requirements:  19896 bytes
//
// 2 Warnings.
// Done.
//
// Compilation Time: 0,14 sec
// ----------------------------------------

Press enter to exit ...

And the plugin does not play sounds in order for three murders.

Liverwiz 08-22-2012 12:13

Re: Reproduce sound every third murder.
 
Quote:

Originally Posted by Exolent[jNr] (Post 1778957)
Actually, that will store the value before you add 1 to it, because the ++ is after the variable which makes it a POST increment.
This is the valid way:
PHP Code:

        new = ++g_streakKills[killer];    // Immediately store your increment into the temp var 

This way it increases the value in the streak kills array, and then stores that value into the "a" variable.

Oh, right. :oops: my bad.

As for the implication of sound.....lol, forgot that too. I've been told that before. I just never remember. -_-


Quote:

Originally Posted by Sutar (Post 1778958)
Code:

//AMXXPC compile.exe
// by the AMX Mod X Dev Team

//// miscstats.sma
// D:\Server\test_minimap1\cstrike\addons\amxmodx\scripting\miscstats.sma(44) :
warning 213: tag mismatch
// Header size:            312 bytes
// Code size:            1292 bytes
// Data size:            1908 bytes
// Stack/heap size:      16384 bytes; estimated max. usage=44 cells (176 bytes)
// Total requirements:  19896 bytes
//
// 2 Warnings.
// Done.
//
// Compilation Time: 0,14 sec
// ----------------------------------------

Press enter to exit ...

And the plugin does not play sounds in order for three murders.

follow exolent's instructions
PHP Code:

          new = ++g_streakKills[killer];
        
        if(
== 0)
        {
            
a/= 3;
            if(
9)
                


The increment was in the improper order. This made it so you were dealing with old data, not the updated one. If exolent didn't explain it well enough here is a long, drawn out example....
Code:
// new a = g_streakKills[killer]++;    // this is the same as..... new a = g_streakKills[killer]; g_streakKills[killer] = g_streakKills[killer] + 1; // new a = ++g_streakKills[killer]    // this is the same as..... g_streakKills[killer] = g_streakKills[killer] + 1; new a = g_streakKills[killer];

Sutar 08-23-2012 03:59

Re: Reproduce sound every third murder.
 
The problem is, that does not play the first sound (multikill)

3 / 3 = 1

May need to take away from g_streakKills[killer] - 3?

For example:
Quote:

g_streakKills[killer] += 1;

new a = g_streakKills[killer] - 3;

if(a > 0 && !(a % 3))
{
a/= 3;
if(a > 9)
a = 5
And I'm doing this:

Quote:

if(headshot)
g_streakKills[killer][0] += 2;
else
g_streakKills[killer][0] += 1;
the killed in head += 2
Just killed += 1

But the problem is that if a player kills 2 times in the head, no sound.

For example:
Quote:

g_streakKills[killer] += 2 //one killed in head
g_streakKills[killer] += 2 //two killed in head (g_streakKills[killer] = 4)

new a = g_streakKills[killer] - 3;

if(a > 0 && !(a % 3)) //a = 1 and the function does not work on...
{
...

Exolent[jNr] 08-23-2012 09:48

Re: Reproduce sound every third murder.
 
Try this:
PHP Code:

        new = ++g_streakKills[killer] - 3;    // Immediately store your increment into the temp var 


Liverwiz 08-23-2012 10:48

Re: Reproduce sound every third murder.
 
Quote:

Originally Posted by Exolent[jNr] (Post 1780150)
Try this:
PHP Code:

        new = ++g_streakKills[killer] - 3;    // Immediately store your increment into the temp var 


I was wondering why he subtracted 3 from that originally....
I apologize, Sutar, you were -mostly- right all along. Just needed a few tweaks.
Aren't we lucky to have such a smart guy like Exolent around to correct me when i'm being a dumbass? rukia


All times are GMT -4. The time now is 05:52.

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