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

[TUT] Properly Detect Weapon Shot (Every Shot) [UPDATED]


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Author Message
WATCH_D0GS UNITED
Senior Member
Join Date: Jan 2023
Old 04-04-2023 , 00:12   [TUT] Properly Detect Weapon Shot (Every Shot) [UPDATED]
Reply With Quote #1

This is for hooking every gun shot.

It hooks the practically exact moment when a bullet is fired. Shots are detected in a extremely short time.

You do not need to check if player is pressing button.

You do not need to check if weapon has clip bullets.

Much more accurate and reliable than FM_PlaybackEvent, which counts every round start as shooting and misses the first bullet fired when using bools. (...)

This shot hook detects every of the shots which go through walls, so when you shoot with weapons which shots are capable to go through walls, ALL the crossing sections are counted as shot.

The debugger will inform you when you are shooting (TRUE),
when you are not shooting (FALSE),
and how many shots you fired (SHOTS).

*Every shot crossing walls is counted/detected as one different shot.
*Every shotgun pellet is counted/detected as one different shot.


UPDATE!
PHP Code:
Apr 092023

Now using FM_CmdEnd instead of CurWeapon to detect the shots in an even shorter period of time

DEBUG VERSION

PHP Code:
#include <amxmodx>
#include <fakemeta>

#define valid(id) (id == clamp(id,1,33))  // Checks if the player is in the range of 1 to max players
#define NOSHOT (1<<0 | 1<<4 | 1<<6 | 1<<9 | 1<<25 | 1<<29)  // (CSW_NONE | CSW_HEGRENADE | CSW_C4 | CSW_SMOKEGRENADE | CSW_FLASHBANG | CSW_KNIFE)
#define ALIVE is_user_alive
#define WEAPON get_user_weapon
#define m_flLastFired get_pdata_int(id,220)
#define GUN_FIRED (last_shot_b[id] != last_shot_a[id])

static last_shot_a[33],last_shot_b[33],bool:SHOOTING[33],shot_count;

public 
plugin_init() {
    
register_plugin("Shot Hook","1.0","WATCH_DOGS UNITED")
    
register_forward(FM_TraceLine,"fwTraceLine",1)
    
register_forward(FM_CmdEnd,"Refresh")
}

public 
Refresh(id) {
    if(!
valid(id))
        return
    
last_shot_a[id] = m_flLastFired
}

public 
fwTraceLine(const Float:start[3],Float:dest[3],HIT_PARAM,id,ptr) {
    if(!
valid(id))
        return
    
last_shot_b[id] = m_flLastFired
    
if(ALIVE(id) && !(NOSHOT & (1<<WEAPON(id))) && GUN_FIRED) {

        
// Gun fired
        
SHOOTING[id] = true
        shot_count
++
    }
    else
        
SHOOTING[id] = false

    
// REAL-TIME DEBUG

    
if(SHOOTING[id] == true)
        
client_print(id,print_chat,"TRUE")
    else
        
client_print(id,print_chat,"FALSE")
    
client_print(id,print_chat,"SHOTS: %d",shot_count)
}

/*
public Your_Stuff(id) {
    if(SHOOTING[id] == true) {
        Make something
    }
}
*/ 

SIMPLIFIED

PHP Code:
#include <amxmodx>
#include <fakemeta>

#define valid(id) (id == clamp(id,1,33))
#define NOSHOT (1<<0 | 1<<4 | 1<<6 | 1<<9 | 1<<25 | 1<<29)

static last_shot_a[33],last_shot_b[33],bool:SHOOTING[33];

public 
plugin_init() {
    
register_plugin("Shot Hook","1.0","WATCH_DOGS UNITED")
    
register_forward(FM_TraceLine,"fwTraceLine",1)
    
register_forward(FM_CmdEnd,"Refresh")
}

public 
Refresh(id) {
    if(!
valid(id))
        return
    
last_shot_a[id] = get_pdata_int(id,220)
}

public 
fwTraceLine(const Float:start[3],Float:dest[3],HIT_PARAM,id,ptr) {
    if(!
valid(id))
        return
    
last_shot_b[id] = get_pdata_int(id,220)
    if(
is_user_alive(id) && !(NOSHOT & (1<<get_user_weapon(id))) && last_shot_b[id] != last_shot_a[id]) {
        
// Gun fired
        
SHOOTING[id] = true
    
}
    else
        
SHOOTING[id] = false



We will release the new version of Aim Realism plugin soon with this shot hook.

EDIT

Aim Realism v2.3.9 released.
https://gamebanana.com/mods/311932

Last edited by WATCH_D0GS UNITED; 04-09-2023 at 20:48. Reason: Updated
WATCH_D0GS UNITED is offline
 



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 14:29.


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