Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public OnPlayerGiveDamage(playerid, damagedid, Float:amount, weaponid, bodypart)
- {
- if (!IsHighRateWeapon(weaponid)) {
- DebugMessage(playerid, "OnPlayerGiveDamage(%d gave %f to %d using %d on bodypart %d)", playerid, amount, damagedid, weaponid, bodypart);
- }
- // Nobody got damaged
- if (damagedid == INVALID_PLAYER_ID) {
- #if defined OnInvalidWeaponDamage
- OnInvalidWeaponDamage(playerid, damagedid, amount, weaponid, bodypart, WC_NO_DAMAGED, true);
- #endif
- AddRejectedHit(playerid, damagedid, HIT_NO_DAMAGEDID, weaponid);
- return 0;
- }
- if (s_IsDying[damagedid]) {
- AddRejectedHit(playerid, damagedid, HIT_DYING_PLAYER, weaponid);
- return 0;
- }
- if (!s_LagCompMode) {
- new npc = IsPlayerNPC(damagedid);
- if (weaponid == WEAPON_KNIFE && _:amount == _:0.0) {
- if (s_KnifeTimeout[damagedid] != -1) {
- KillTimer(s_KnifeTimeout[damagedid]);
- }
- s_KnifeTimeout[damagedid] = SetTimerEx("WC_SetSpawnForStreamedIn", 2500, false, "i", damagedid);
- }
- if (!npc) {
- return 0;
- }
- }
- // Ignore unreliable and invalid damage
- if (weaponid < 0 || weaponid >= sizeof(s_ValidDamageGiven) || !s_ValidDamageGiven[weaponid]) {
- // Fire is synced as taken damage (because it's not reliable as given), so no need to show a rejected hit.
- // Vehicle damage is also synced as taken, so no need to show that either.
- if (weaponid != WEAPON_FLAMETHROWER && weaponid != WEAPON_VEHICLE) {
- AddRejectedHit(playerid, damagedid, HIT_INVALID_WEAPON, weaponid);
- }
- return 0;
- }
- if (!IsPlayerSpawned(playerid)) {
- // Make sure the rejected hit wasn't added in OnPlayerWeaponShot
- if (!IsBulletWeapon(weaponid) || s_LastShot[playerid][e_Valid]) {
- AddRejectedHit(playerid, damagedid, HIT_NOT_SPAWNED, weaponid);
- }
- return 0;
- }
- new npc = IsPlayerNPC(damagedid);
- // From stealth knife, can be any weapon
- if (_:amount == _:1833.33154296875) {
- return 0;
- }
- if (weaponid == WEAPON_KNIFE) {
- if (_:amount == _:0.0) {
- // Resync without bothering the player being knifed
- if (npc || HasSameTeam(playerid, damagedid)) {
- if (s_KnifeTimeout[damagedid] != -1) {
- KillTimer(s_KnifeTimeout[damagedid]);
- }
- s_KnifeTimeout[damagedid] = SetTimerEx("WC_SpawnForStreamedIn", 150, false, "i", damagedid);
- ClearAnimations(playerid, 1);
- SetPlayerArmedWeapon(playerid, 0);
- return 0;
- }
- if (!OnPlayerDamage(damagedid, amount, playerid, weaponid, bodypart)) {
- if (s_KnifeTimeout[damagedid] != -1) {
- KillTimer(s_KnifeTimeout[damagedid]);
- }
- s_KnifeTimeout[damagedid] = SetTimerEx("WC_SpawnForStreamedIn", 150, false, "i", damagedid);
- ClearAnimations(playerid, 1);
- SetPlayerArmedWeapon(playerid, 0);
- return 0;
- }
- s_DamageDoneHealth[playerid] = s_PlayerHealth[playerid];
- s_DamageDoneArmour[playerid] = s_PlayerArmour[playerid];
- OnPlayerDamageDone(damagedid, s_PlayerHealth[damagedid] + s_PlayerArmour[damagedid], playerid, weaponid, bodypart);
- ClearAnimations(damagedid, 1);
- PlayerDeath(damagedid, "KNIFE", "KILL_Knife_Ped_Damage", _, 5200);
- SetTimerEx("WC_SecondKnifeAnim", 2200, false, "i", damagedid);
- #if defined WC_OnPlayerDeath
- WC_OnPlayerDeath(damagedid, playerid, weaponid);
- #endif
- DebugMessage(damagedid, "being knifed by %d", playerid);
- DebugMessage(playerid, "knifing %d", damagedid);
- new Float:x, Float:y, Float:z, Float:a;
- GetPlayerFacingAngle(damagedid, a);
- SetPlayerFacingAngle(playerid, a);
- PosInFront(damagedid, -1.0, x, y, z);
- SetPlayerVelocity(damagedid, 0.0, 0.0, 0.0);
- SetPlayerVelocity(playerid, 0.0, 0.0, 0.0);
- new forcesync = 2;
- if (747 < GetPlayerAnimationIndex(playerid) > 748) {
- DebugMessageRed(playerid, "applying knife anim for you too (current: %d)", GetPlayerAnimationIndex(playerid));
- forcesync = 1;
- }
- ApplyAnimation(playerid, "KNIFE", "KILL_Knife_Player", 4.1, 0, 1, 1, 0, 1800, forcesync);
- return 0;
- }
- }
- if (HasSameTeam(playerid, damagedid)) {
- AddRejectedHit(playerid, damagedid, HIT_SAME_TEAM, weaponid);
- return 0;
- }
- // Both players should see eachother
- if ((!IsPlayerStreamedIn(playerid, damagedid) && !IsPlayerPaused(damagedid)) || !IsPlayerStreamedIn(damagedid, playerid)) {
- AddRejectedHit(playerid, damagedid, HIT_UNSTREAMED, weaponid, damagedid);
- return 0;
- }
- new Float:bullets, err;
- if ((err = ProcessDamage(damagedid, playerid, amount, weaponid, bodypart, bullets))) {
- if (err == WC_INVALID_DAMAGE) {
- AddRejectedHit(playerid, damagedid, HIT_INVALID_DAMAGE, weaponid, _:amount);
- }
- if (err != WC_INVALID_DISTANCE) {
- #if defined OnInvalidWeaponDamage
- OnInvalidWeaponDamage(playerid, damagedid, amount, weaponid, bodypart, err, true);
- #endif
- }
- return 0;
- }
- new tick = GetTickCount();
- if (tick == 0) tick = 1;
- new idx = (s_LastHitIdx[playerid] + 1) % sizeof(s_LastHitTicks[]);
- // JIT plugin fix
- if (idx < 0) {
- idx += sizeof(s_LastHitTicks[]);
- }
- s_LastHitIdx[playerid] = idx;
- s_LastHitTicks[playerid][idx] = tick;
- s_LastHitWeapons[playerid][idx] = weaponid;
- s_HitsIssued[playerid] += 1;
- #if WC_DEBUG
- if (s_HitsIssued[playerid] > 1) {
- new prev_tick_idx = (idx - 1) % sizeof(s_LastHitTicks[]);
- // JIT plugin fix
- if (prev_tick_idx < 0) {
- prev_tick_idx += sizeof(s_LastHitTicks[]);
- }
- new prev_tick = s_LastHitTicks[playerid][prev_tick_idx];
- DebugMessage(playerid, "(hit) last: %d last 3: %d", tick - prev_tick, AverageHitRate(playerid, 3));
- }
- #endif
- new multiple_weapons;
- new avg_rate = AverageHitRate(playerid, s_MaxHitRateSamples, multiple_weapons);
- // Hit issue flood?
- // Could be either a cheat or just lag
- if (avg_rate != -1) {
- if (multiple_weapons) {
- if (avg_rate < 100) {
- AddRejectedHit(playerid, damagedid, HIT_RATE_TOO_FAST_MULTIPLE, weaponid, avg_rate, s_MaxHitRateSamples);
- return 0;
- }
- } else if (s_MaxWeaponShootRate[weaponid] - avg_rate > 20) {
- AddRejectedHit(playerid, damagedid, HIT_RATE_TOO_FAST, weaponid, avg_rate, s_MaxHitRateSamples, s_MaxWeaponShootRate[weaponid]);
- return 0;
- }
- }
- if (IsBulletWeapon(weaponid) && _:amount != _:2.6400001049041748046875 && !(IsPlayerInAnyVehicle(playerid) && GetPlayerVehicleSeat(playerid) == 0)) {
- new valid = true;
- if (!s_LastShot[playerid][e_Valid]) {
- //AddRejectedHit(playerid, damagedid, HIT_LAST_SHOT_INVALID, weaponid);
- valid = false;
- DebugMessageRed(playerid, "last shot not valid");
- } else if (WEAPON_SHOTGUN <= weaponid <= WEAPON_SHOTGSPA) {
- // Let's assume someone won't hit 3 players with 1 shotgun shot, and that one OnPlayerWeaponShot can be out of sync
- if (s_LastShot[playerid][e_Hits] >= 3) {
- valid = false;
- AddRejectedHit(playerid, damagedid, HIT_MULTIPLE_PLAYERS_SHOTGUN, weaponid, s_LastShot[playerid][e_Hits] + 1);
- }
- } else if (s_LastShot[playerid][e_Hits] > 0) {
- // Sniper doesn't always send OnPlayerWeaponShot
- if (s_LastShot[playerid][e_Hits] > 4 && weaponid != WEAPON_SNIPER) {
- valid = false;
- AddRejectedHit(playerid, damagedid, HIT_MULTIPLE_PLAYERS, weaponid, s_LastShot[playerid][e_Hits] + 1);
- } else {
- DebugMessageRed(playerid, "hit %d players with 1 shot", s_LastShot[playerid][e_Hits] + 1);
- }
- }
- s_LastShot[playerid][e_Hits] += 1;
- if (!valid) {
- return 0;
- }
- }
- if (npc) {
- OnPlayerDamageDone(damagedid, amount, playerid, weaponid, bodypart);
- } else {
- InflictDamage(damagedid, amount, playerid, weaponid, bodypart);
- }
- // Don't send OnPlayerGiveDamage to the rest of the script, since it should not be used
- return 0;
- }
- public OnPlayerTakeDamage(playerid, issuerid, Float: amount, weaponid, bodypart)
- {
- UpdateHealthBar(playerid, true);
- if (s_IsDying[playerid]) {
- return 0;
- }
- if (s_BeingResynced[playerid]) {
- return 0;
- }
- if (!IsHighRateWeapon(weaponid)) {
- DebugMessage(playerid, "OnPlayerTakeDamage(%d took %f from %d by %d on bodypart %d)", playerid, amount, issuerid, weaponid, bodypart);
- }
- // Ignore unreliable and invalid damage
- if (weaponid < 0 || weaponid >= sizeof(s_ValidDamageTaken) || !s_ValidDamageTaken[weaponid]) {
- return 0;
- }
- if (playerid == INVALID_PLAYER_ID || IsPlayerNPC(playerid)) {
- return 0;
- }
- // Carjack damage
- if (weaponid == 54 && _:amount == _:0.0) {
- return 0;
- }
- // From stealth knife, can be any weaponid
- if (_:amount == _:1833.33154296875) {
- return 0;
- }
- // Climb bug
- if (weaponid == WEAPON_COLLISION) {
- if (s_CustomFallDamage) {
- return 0;
- }
- new anim = GetPlayerAnimationIndex(playerid);
- if (1061 <= anim <= 1067) {
- DebugMessage(playerid, "climb bug prevented");
- return 0;
- }
- }
- // Being knifed client-side
- if (weaponid == WEAPON_KNIFE) {
- if (s_IsDying[playerid]) {
- return 0;
- }
- // With the plugin, this part is never actually used (it can't happen)
- if (_:amount == _:0.0) {
- if (s_KnifeTimeout[playerid] != -1) {
- KillTimer(s_KnifeTimeout[playerid]);
- s_KnifeTimeout[playerid] = -1;
- }
- if (issuerid == INVALID_PLAYER_ID || HasSameTeam(playerid, issuerid)) {
- ResyncPlayer(playerid);
- return 0;
- }
- if (!OnPlayerDamage(playerid, amount, issuerid, weaponid, bodypart)) {
- ResyncPlayer(playerid);
- return 0;
- }
- // Make sure the values were not modified
- weaponid = WEAPON_KNIFE;
- amount = 0.0;
- s_DamageDoneHealth[playerid] = s_PlayerHealth[playerid];
- s_DamageDoneArmour[playerid] = s_PlayerArmour[playerid];
- OnPlayerDamageDone(playerid, s_PlayerHealth[playerid] + s_PlayerArmour[playerid], issuerid, weaponid, bodypart);
- PlayerDeath(playerid, "KNIFE", "KILL_Knife_Ped_Die", _, 4000 - GetPlayerPing(playerid));
- #if defined WC_OnPlayerDeath
- WC_OnPlayerDeath(playerid, issuerid, weaponid);
- #endif
- SetPlayerHealth(playerid, Float:0x7f7fffff);
- DebugMessage(playerid, "being knifed by %d", issuerid);
- DebugMessage(issuerid, "knifing %d", playerid);
- new Float:x, Float:y, Float:z, Float:a;
- GetPlayerFacingAngle(playerid, a);
- SetPlayerFacingAngle(issuerid, a);
- PosInFront(playerid, -1.0, x, y, z);
- SetPlayerVelocity(playerid, 0.0, 0.0, 0.0);
- SetPlayerVelocity(issuerid, 0.0, 0.0, 0.0);
- new forcesync = 2;
- if (GetPlayerAnimationIndex(issuerid) != 747) {
- DebugMessageRed(issuerid, "applying knife anim for you too (current: %d)", GetPlayerAnimationIndex(issuerid));
- forcesync = 1;
- }
- ApplyAnimation(issuerid, "KNIFE", "KILL_Knife_Player", 4.1, 0, 1, 1, 0, 1800, forcesync);
- return 0;
- }
- }
- // If it's lagcomp, only allow damage that's valid for both modes
- if (s_LagCompMode && s_ValidDamageTaken[weaponid] != 2) {
- if (issuerid != INVALID_PLAYER_ID && IsPlayerInAnyVehicle(issuerid) && GetPlayerVehicleSeat(issuerid) == 0 && (weaponid == WEAPON_M4 || weaponid == WEAPON_MINIGUN)) {
- weaponid = weaponid == WEAPON_M4 ? WEAPON_VEHICLE_M4 : WEAPON_VEHICLE_MINIGUN;
- } else {
- return 0;
- }
- }
- // Should still be damaged by grenades or fire after someone has died
- if (issuerid != INVALID_PLAYER_ID) {
- if (HasSameTeam(playerid, issuerid)) {
- return 0;
- }
- if (s_IsDying[issuerid] && (IsBulletWeapon(weaponid) || IsMeleeWeapon(weaponid))) {
- DebugMessageRed(playerid, "shot/punched by dead player (%d)", issuerid);
- return 0;
- }
- if (s_BeingResynced[issuerid]) {
- return 0;
- }
- }
- new Float:bullets = 0.0, err;
- if ((err = ProcessDamage(playerid, issuerid, amount, weaponid, bodypart, bullets))) {
- if (err == WC_INVALID_DAMAGE) {
- AddRejectedHit(issuerid, playerid, HIT_INVALID_DAMAGE, weaponid, _:amount);
- }
- if (err != WC_INVALID_DISTANCE) {
- #if defined OnInvalidWeaponDamage
- OnInvalidWeaponDamage(issuerid, playerid, amount, weaponid, bodypart, err, false);
- #endif
- }
- return 0;
- }
- if (IsBulletWeapon(weaponid)) {
- new Float:x, Float:y, Float:z;
- GetPlayerPos(issuerid, x, y, z);
- new Float:dist = GetPlayerDistanceFromPoint(playerid, x, y, z);
- if (dist > s_WeaponRange[weaponid] + 2.0) {
- AddRejectedHit(issuerid, playerid, HIT_OUT_OF_RANGE, weaponid, _:dist, _:s_WeaponRange[weaponid]);
- return 0;
- }
- }
- InflictDamage(playerid, amount, issuerid, weaponid, bodypart);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment