File size: 4,141 Bytes
ed59cc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
HamSandWich function:
PHP Code:
Ham_TraceAttack 
Description:
Forward: This function is called when an entity is shot by another player (weapon, not grenade.)
Function: This function simulates a weapon attack on an entity.
You should use this function when you want to simulate an attack by a weapon from a player (like a gunshot!)

Parameters:
(ent, attacker, float:damage, Float:direction[3], trace, bits)
- ent is the entity that has been shot
- attacker is the entity that shoot the victim
- damage - this is the damage done by the shot but it does not have the hitgroup multiplication done (that is done before TakeDamage Call)
- direction - this parameter has a very important roll, it is a normalized vector that shows the direction of the bullets path
- trace - the trace handle that offers lots of useful information
- bits - the damage bits

Forward usage:
PHP Code:
#define SHOT_DISTANCE_POSSIBLE 2048.0

// plugin_init()
Register_Ham(Ham_TraceAttack, "func_breakable", "fw_traceatt_break")

public fw_traceatt_break(ent, attacker, float:damage, Float:direction[3], trace, bits)
{
    new Float:start[3], end[3], fraction, multi
    get_tr2(trace, TR_vecEndPos, end)
    get_tr2(trace, TR_flFraction, fraction)
    
    multi = fraction * SHOT_DISTANCE_POSSIBLE
    
    xs_vec_mul_scalar(direction, multi, start)
    
    // This is how to correctly get the start origin!
    xs_vec_add(start, end, start)
    
} 
Function true usage:
PHP Code:
#define SHOT_DISTANCE_POSSIBLE 2048.0

public Ham_Shoot(attacker, damage, bits, bool:can_penetrate)
{
    new Float:angles[3], Float:start[3], Float:end[3], Float:direction[3], Float:fakeend[3]
    
    // Turn the angles in a true vector
    pev(attacker, pev_angles, angles)
    angle_vector(angles, ANGLEVECTOR_FORWARD, direction)
    
    // We make this as a normal shot!
    xs_vec_mul_scalar(direction, SHOT_DISTANCE_POSSIBLE, fakeend)
    
    // Start origin
    pev(attacker, pev_origin, start)
    pev(attacker, pev_view_ofs, end)
    xs_vec_add(end, start, start)
    
    // Obtain the end shot origin
    xs_vec_add(start, fakeend, end)
    
    // From now this is how these variables will be used
    // origin - start place (will remain constant!)
    // end - end place (will remain constant!)
    // angles - no use
    // fakeend - dynamic start origin
    // direction - will be used in the forwards (will remain constant!)
    
    new ptr = create_tr2()
    
    // Trace to the first entity
    engfunc(EngFunc_TraceLine, start, end, DONT_IGNORE_MONSTERS, attacker, ptr)
    
    new Float:fraction, hit = get_tr2(ptr, TR_pHit)
    get_tr2(ptr, TR_flFraction, fraction)
    
    // Update the fake start origin
    get_tr2(ptr, TR_vecEndPos, fakeend)
    
    // This means that we hited sky!
    if (fraction != 1.0 && engfunc(EngFunc_PointContents, fakeend) == CONTENTS_SKY && hit == -1)
    {
        // Prepare the trace handle
        set_tr2(ptr, TR_pHit, 0)
        
        // Bullet trace!
        ExecuteHamB(Ham_TraceAttack, 0, attacker, damage, direction, ptr, DMG_NEVERGIB | DMG_BULLET)
        
        free_tr2(ptr)
        return -1
    }
    
    if (hit == -1)
    {
        hit = 0
        set_tr2(ptr, TR_pHit, hit)
    }
    
    ExecuteHamB(Ham_TraceAttack, hit, attacker, damage, direction, ptr, DMG_NEVERGIB | DMG_BULLET)
    
    if (!can_penetrate)
    {
        free_tr2(ptr)
        return 1
    }
    
    // Trace to the next entity
    engfunc(EngFunc_TraceLine, fakeend, end, DONT_IGNORE_MONSTERS, hit, ptr)
    
    hit = get_tr2(ptr, TR_pHit)
    get_tr2(ptr, TR_flFraction, fraction)
    
    // Update the fake start origin
    get_tr2(ptr, TR_vecEndPos, fakeend)
    
    if (hit == -1)
    {
        hit = 0
        set_tr2(ptr, TR_pHit, hit)
    }
    
    set_tr2(ptr, TR_flFraction, get_distance_f(start, fakeend) / SHOT_DISTANCE_POSSIBLE)
    
    ExecuteHamB(Ham_TraceAttack, hit, attacker, damage, direction, ptr, DMG_NEVERGIB | DMG_BULLET)
    
        
    free_tr2(ptr)
    return 1
}