Skip to content

Commit

Permalink
Fix suicide logic (again) (#787)
Browse files Browse the repository at this point in the history
Add bomb parameter to player death event
Make attacker and weapon nullable on player death event
Add self to array of victims for grenades
  • Loading branch information
nickdnk authored Jul 25, 2022
1 parent 6920d3d commit c8fe43d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 25 deletions.
19 changes: 14 additions & 5 deletions documentation/docs/event_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,9 @@ paths:
event:
enum:
- player_death
bomb:
type: boolean
description: Indicates if the player died from the bomb explosion. `Bomb` in SourceMod.
headshot:
type: boolean
description: Indicates if the player died from a headshot. `Headshot`
Expand Down Expand Up @@ -619,7 +622,11 @@ paths:
description: Indicates if the player died from friendly fire. `FriendlyFire`
in SourceMod.
attacker:
"$ref": "#/components/schemas/Get5Player"
allOf:
- type: object
nullable: true
title: Get5Player
- "$ref": "#/components/schemas/Get5Player"
assist:
"$ref": "#/components/schemas/Get5AssisterObject"
responses: { }
Expand Down Expand Up @@ -995,6 +1002,7 @@ components:
description: Describes a player. `Player` in SourceMod (or `Attacker` on `Get5PlayerDeathEvent`).
Get5Weapon:
type: object
nullable: true
properties:
name:
type: string
Expand All @@ -1007,7 +1015,7 @@ components:
example: 27
description: The weapon ID used. See https://sm.alliedmods.net/new-api/cstrike/CSWeaponID
`Id` in SourceMod.
description: Describes a weapon. `Weapon` in SourceMod.
description: Describes a weapon. `Weapon` in SourceMod. Use `HasWeapon()` to determine if `null` on `Get5PlayerDeathEvent`.
Get5AssisterObject:
type: object
nullable: true
Expand All @@ -1022,9 +1030,10 @@ components:
type: boolean
description: Indicates if the assist was a flash assist. `FlashAssist` in
SourceMod.
description: 'Describes an assist to a kill. `null` if no assister. `Assist`
in SourceMod. **Note: Use `HasAssist()` in SourceMod to determine if the property
exists before accessing it.**'
description: |
Describes an assist to a kill. `null` if no assister. `Assist`
in SourceMod. Use `HasAssist()` in SourceMod to determine if the property
exists before accessing it.
Get5GrenadeVictim:
type: object
properties:
Expand Down
40 changes: 26 additions & 14 deletions scripting/get5/stats.sp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public Action HandlePlayerDamage(int victim, int &attacker, int &inflictor, floa
return Plugin_Continue;
}

if (attacker == victim || !IsValidClient(attacker) || !IsValidClient(victim)) {
if (!IsValidClient(attacker) || !IsValidClient(victim)) {
return Plugin_Continue;
}

Expand Down Expand Up @@ -673,7 +673,7 @@ public Action Stats_PlayerDeathEvent(Event event, const char[] name, bool dontBr

bool validAttacker = IsValidClient(attacker);
bool validVictim = IsValidClient(victim);
bool validAssister = assister > 0 && IsValidClient(assister);
bool validAssister = IsValidClient(assister);

if (!validVictim) {
return Plugin_Continue; // Not sure how this would happen, but it's not something we care
Expand Down Expand Up @@ -701,9 +701,18 @@ public Action Stats_PlayerDeathEvent(Event event, const char[] name, bool dontBr
char weapon[32];
event.GetString("weapon", weapon, sizeof(weapon));

int attackerTeam = 0; // 0 until we know attacker is valid.
CSWeaponID weaponId = CS_AliasToWeaponID(weapon);

int attackerTeam = validAttacker ? GetClientTeam(attacker) : 0;
int victimTeam = GetClientTeam(victim);
bool isSuicide = false;

// suicide (kill console) is attacker == victim, weapon id 0, weapon "world"
// fall damage is weapon id 0, attacker 0, weapon "worldspawn"
// falling from vertigo is attacker 0, weapon id 0, weapon "trigger_hurt"
// c4 is attacker 0, weapon id 0, weapon planted_c4
// with those in mind, we can determine suicide from this:
bool killedByBomb = StrEqual("planted_c4", weapon, true);
bool isSuicide = attacker == victim || (view_as<int>(weaponId) == 0 && !killedByBomb);

IncrementPlayerStat(victim, STAT_DEATHS);
// used for calculating round KAST
Expand All @@ -715,12 +724,7 @@ public Action Stats_PlayerDeathEvent(Event event, const char[] name, bool dontBr
(victimTeam == CS_TEAM_CT) ? STAT_FIRSTDEATH_CT : STAT_FIRSTDEATH_T);
}

CSWeaponID weaponId = CS_AliasToWeaponID(weapon);

if (!validAttacker || attacker == victim) {
isSuicide = true;
} else {
attackerTeam = GetClientTeam(attacker);
if (!isSuicide) {
if (attackerTeam != victimTeam) {
if (!g_TeamFirstKillDone[attackerTeam]) {
g_TeamFirstKillDone[attackerTeam] = true;
Expand Down Expand Up @@ -755,10 +759,18 @@ public Action Stats_PlayerDeathEvent(Event event, const char[] name, bool dontBr
}

Get5PlayerDeathEvent playerDeathEvent = new Get5PlayerDeathEvent(
g_MatchID, g_MapNumber, g_RoundNumber, GetRoundTime(), new Get5Weapon(weapon, weaponId),
g_MatchID, g_MapNumber, g_RoundNumber, GetRoundTime(),
GetPlayerObject(victim), headshot, validAttacker ? attackerTeam == victimTeam : false,
GetPlayerObject(attacker), event.GetBool("thrusmoke"), event.GetBool("noscope"),
event.GetBool("attackerblind"), isSuicide, event.GetInt("penetrated"));
event.GetBool("thrusmoke"), event.GetBool("noscope"), event.GetBool("attackerblind"),
isSuicide, event.GetInt("penetrated"), killedByBomb);

if (validAttacker) {
playerDeathEvent.Attacker = GetPlayerObject(attacker);
}

if (view_as<int>(weaponId) > 0) {
playerDeathEvent.Weapon = new Get5Weapon(weapon, weaponId);
}

if (validAssister) {
bool assistedFlash = event.GetBool("assistedflash");
Expand Down Expand Up @@ -897,7 +909,7 @@ public Action Stats_PlayerBlindEvent(Event event, const char[] name, bool dontBr
int victim = GetClientOfUserId(event.GetInt("userid"));
int attacker = GetClientOfUserId(event.GetInt("attacker"));

if (attacker == victim || !IsValidClient(attacker) || !IsValidClient(victim)) {
if (!IsValidClient(attacker) || !IsValidClient(victim)) {
return Plugin_Continue;
}

Expand Down
32 changes: 26 additions & 6 deletions scripting/include/get5.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,16 @@ methodmap Get5PlayerWeaponEvent < Get5PlayerTimedRoundEvent {

methodmap Get5PlayerDeathEvent < Get5PlayerWeaponEvent {

property bool Bomb {
public get() {
return this.GetBool("bomb");
}

public set(bool bomb) {
this.SetBool("bomb", bomb);
}
}

property bool Headshot {
public get() {
return this.GetBool("headshot");
Expand Down Expand Up @@ -1291,21 +1301,30 @@ methodmap Get5PlayerDeathEvent < Get5PlayerWeaponEvent {
return this.GetObject("assist") != null;
}

// Use before accessing "Attacker", as its getter will raise an exception if null.
public bool HasAttacker() {
return this.GetObject("attacker") != null;
}

// Use before accessing "Weapon", as its getter will raise an exception if null.
public bool HasWeapon() {
return this.GetObject("weapon") != null;
}

public Get5PlayerDeathEvent(
const char[] matchId,
const int mapNumber,
const int roundNumber,
const int roundTime,
const Get5Weapon weapon,
const Get5Player victim,
const bool headshot,
const bool friendlyFire,
const Get5Player attacker,
const bool thruSmoke,
const bool noScope,
const bool attackerBlind,
const bool suicide,
const int penetrated
const int penetrated,
const bool bomb
) {

Get5PlayerDeathEvent self = view_as<Get5PlayerDeathEvent>(new JSON_Object());
Expand All @@ -1314,19 +1333,20 @@ methodmap Get5PlayerDeathEvent < Get5PlayerWeaponEvent {
self.MapNumber = mapNumber;
self.RoundNumber = roundNumber;
self.RoundTime = roundTime;
self.Weapon = weapon;
self.Player = victim;
self.Headshot = headshot;
self.FriendlyFire = friendlyFire;
self.Attacker = attacker;
self.ThruSmoke = thruSmoke;
self.NoScope = noScope;
self.AttackerBlind = attackerBlind;
self.Suicide = suicide;
self.Penetrated = penetrated;
self.Bomb = bomb;

// set assist to null initially
// set nullables to null initially
self.SetObject("assist", null);
self.SetObject("attacker", null);
self.SetObject("weapon", null);
return self;

}
Expand Down

0 comments on commit c8fe43d

Please sign in to comment.