Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revise PVP Block and Hit Recovery handling #7097

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 44 additions & 45 deletions Source/missiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "levels/trigs.h"
#include "lighting.h"
#include "monster.h"
#include "player.h"
#include "spells.h"
#include "utils/str_cat.hpp"

Expand Down Expand Up @@ -280,9 +281,24 @@ bool MonsterMHit(const Player &player, int monsterId, int mindam, int maxdam, in
return true;
}

bool Plr2PlrMHit(const Player &player, Player &target, int mindam, int maxdam, int dist, MissileID mtype, DamageType damageType, bool shift, bool *blocked)
int8_t GetTargetResistance(const Player &target, DamageType damageType)
{
if (sgGameInitInfo.bFriendlyFire == 0 && player.friendlyMode)
switch (damageType) {
case DamageType::Fire:
return target._pFireResist;
case DamageType::Lightning:
return target._pLghtResist;
case DamageType::Magic:
case DamageType::Acid:
return target._pMagResist;
default:
return 0;
}
}

bool Plr2PlrMHit(const Player &attacker, const Player &target, int mindam, int maxdam, int dist, MissileID mtype, DamageType damageType, bool shift, bool *blocked)
{
if (sgGameInitInfo.bFriendlyFire == 0 && attacker.friendlyMode)
return false;

*blocked = false;
Expand All @@ -300,81 +316,64 @@ bool Plr2PlrMHit(const Player &player, Player &target, int mindam, int maxdam, i

const MissileData &missileData = GetMissileData(mtype);

if (HasAnyOf(target._pSpellFlags, SpellFlag::Etherealize) && missileData.isArrow()) {
return false;
}

int8_t resper;
switch (damageType) {
case DamageType::Fire:
resper = target._pFireResist;
break;
case DamageType::Lightning:
resper = target._pLghtResist;
break;
case DamageType::Magic:
case DamageType::Acid:
resper = target._pMagResist;
break;
default:
resper = 0;
break;
}
// Get resistances
int8_t resper = GetTargetResistance(target, damageType);

int hper = GenerateRnd(100);
// Hit check
int hit = GenerateRnd(100);

int hit;
int hper;
if (missileData.isArrow()) {
hit = player.GetRangedToHit()
hper = attacker.GetRangedToHit()
- (dist * dist / 2)
- target.GetArmor();
} else {
hit = player.GetMagicToHit()
hper = attacker.GetMagicToHit()
- (target.getCharacterLevel() * 2)
- dist;
}

hit = std::clamp(hit, 5, 95);
hper = std::clamp(hper, 5, 95);

if (hper >= hit) {
if (hit >= hper) {
return false;
}

int blkper = 100;
if (!shift && (target._pmode == PM_STAND || target._pmode == PM_ATTACK) && target._pBlockFlag) {
blkper = GenerateRnd(100);
}

int blk = target.GetBlockChance() - (player.getCharacterLevel() * 2);
blk = std::clamp(blk, 0, 100);

// Calculate damage
int dam;
if (mtype == MissileID::BoneSpirit) {
dam = target._pHitPoints / 3;
} else {
dam = mindam + GenerateRnd(maxdam - mindam + 1);
if (missileData.isArrow() && damageType == DamageType::Physical)
dam += player._pIBonusDamMod + player._pDamageMod + dam * player._pIBonusDam / 100;
dam = RandomIntBetween(mindam, maxdam);
if (missileData.isArrow() && damageType == DamageType::Physical) {
dam += dam * attacker._pIBonusDam / 100;
dam += attacker._pIBonusDamMod + attacker._pDamageMod;
}
if (!shift)
dam <<= 6;
}

// Halve arrow damage
if (!missileData.isArrow())
dam /= 2;

// Resistance damage reduction
if (resper > 0) {
dam -= (dam * resper) / 100;
if (&player == MyPlayer)
if (&attacker == MyPlayer)
NetSendCmdDamage(true, target, dam, damageType);
target.Say(HeroSpeech::ArghClang);
return true;
}

if (blkper < blk) {
StartPlrBlock(target, GetDirection(target.position.tile, player.position.tile));
// Block check
if (TryPlayerBlock(attacker, target, shift)) {
*blocked = true;
} else {
if (&player == MyPlayer)
NetSendCmdDamage(true, target, dam, damageType);
StartPlrHit(target, dam, false);
if (&attacker == MyPlayer) {
NetSendCmdDamage(true, target, dam, damageType); // Damage target
NetSendCmdParam2(true, CMD_PLRINFLICTRECOVERY, target.getId(), dam); // Inflict hit recovery
}
}

return true;
Expand Down
37 changes: 37 additions & 0 deletions Source/msg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1867,6 +1867,39 @@ size_t OnPlayerDamage(const TCmd *pCmd, Player &player)
return sizeof(message);
}

size_t OnPlayerInflictBlock(const TCmd *pCmd, Player &player)
{
const auto &message = *reinterpret_cast<const TCmdParam1 *>(pCmd);
const uint16_t pid = SDL_SwapLE16(message.wParam1);

Player &target = Players[pid];
if (leveltype != DTYPE_TOWN && gbBufferMsgs != 1) {
if (player.isOnActiveLevel() && target._pHitPoints >> 6 > 0) {
Direction dir = GetDirection(target.position.tile, player.position.tile);
StartPlrBlock(target, dir);
}
}

return sizeof(message);
}

size_t OnPlayerInflictRecovery(const TCmd *pCmd, Player &player)
{
const auto &message = *reinterpret_cast<const TCmdParam2 *>(pCmd);
const uint16_t pid = SDL_SwapLE16(message.wParam1);
const uint16_t dam = SDL_SwapLE16(message.wParam2);

Player &target = Players[pid];
if (leveltype != DTYPE_TOWN && gbBufferMsgs != 1) {
if (player.isOnActiveLevel() && target._pHitPoints >> 6 > 0) {
Direction dir = GetDirection(target.position.tile, player.position.tile);
StartPlrHit(target, dam, false);
}
}

return sizeof(message);
}

size_t OnOperateObject(const TCmd &pCmd, Player &player)
{
const auto &message = reinterpret_cast<const TCmdLoc &>(pCmd);
Expand Down Expand Up @@ -3295,6 +3328,10 @@ size_t ParseCmd(uint8_t pnum, const TCmd *pCmd)
return OnPlayerDeath(pCmd, player);
case CMD_PLRDAMAGE:
return OnPlayerDamage(pCmd, player);
case CMD_PLRINFLICTBLOCK:
return OnPlayerInflictBlock(pCmd, player);
case CMD_PLRINFLICTRECOVERY:
return OnPlayerInflictRecovery(pCmd, player);
case CMD_OPENDOOR:
case CMD_CLOSEDOOR:
case CMD_OPERATEOBJ:
Expand Down
8 changes: 8 additions & 0 deletions Source/msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,14 @@ enum _cmd_id : uint8_t {
//
// body (TCmdDamage)
CMD_PLRDAMAGE,
// Make target player block.
//
// body (TCmdParam1)
CMD_PLRINFLICTBLOCK,
// Make target player enter hit recovery.
//
// body (TCmdParam1)
CMD_PLRINFLICTRECOVERY,
// Set player level.
//
// body (TCmdParam1):
Expand Down
62 changes: 38 additions & 24 deletions Source/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -697,53 +697,45 @@ bool PlrHitMonst(Player &player, Monster &monster, bool adjacentDamage = false)
return true;
}

bool PlrHitPlr(Player &attacker, Player &target)
bool PlrHitPlr(Player &attacker, const Player &target)
{
if (target._pInvincible) {
return false;
}

if (HasAnyOf(target._pSpellFlags, SpellFlag::Etherealize)) {
return false;
}

// Hit check
int hit = GenerateRnd(100);

int hper = attacker.GetMeleeToHit() - target.GetArmor();
hper = std::clamp(hper, 5, 95);

int blk = 100;
if ((target._pmode == PM_STAND || target._pmode == PM_ATTACK) && target._pBlockFlag) {
blk = GenerateRnd(100);
}

int blkper = target.GetBlockChance() - (attacker.getCharacterLevel() * 2);
blkper = std::clamp(blkper, 0, 100);

if (hit >= hper) {
return false;
}

if (blk < blkper) {
Direction dir = GetDirection(target.position.tile, attacker.position.tile);
StartPlrBlock(target, dir);
return true;
// Block check
if (&attacker == MyPlayer) {
if (TryPlayerBlock(attacker, target))
return true;
}

int mind = attacker._pIMinDam;
int maxd = attacker._pIMaxDam;
int dam = GenerateRnd(maxd - mind + 1) + mind;
// Calculate damage
int dam = RandomIntBetween(attacker._pIMinDam, attacker._pIMaxDam);
dam += (dam * attacker._pIBonusDam) / 100;
dam += attacker._pIBonusDamMod + attacker._pDamageMod;

// Critical hit check
if (attacker._pClass == HeroClass::Warrior || attacker._pClass == HeroClass::Barbarian) {
if (GenerateRnd(100) < attacker.getCharacterLevel()) {
dam *= 2;
}
}
int skdam = dam << 6;

dam <<= 6;

// Undead Crown life steal check
if (HasAnyOf(attacker._pIFlags, ItemSpecialEffect::RandomStealLife)) {
int tac = GenerateRnd(skdam / 8);
int tac = GenerateRnd(dam / 8);
attacker._pHitPoints += tac;
if (attacker._pHitPoints > attacker._pMaxHP) {
attacker._pHitPoints = attacker._pMaxHP;
Expand All @@ -754,10 +746,11 @@ bool PlrHitPlr(Player &attacker, Player &target)
}
RedrawComponent(PanelDrawComponent::Health);
}

if (&attacker == MyPlayer) {
NetSendCmdDamage(true, target, skdam, DamageType::Physical);
NetSendCmdDamage(true, target, dam, DamageType::Physical); // Damage target
NetSendCmdParam2(true, CMD_PLRINFLICTRECOVERY, target.getId(), dam); // Inflict hit recovery
}
StartPlrHit(target, skdam, false);

return true;
}
Expand Down Expand Up @@ -3456,6 +3449,27 @@ void PlayDungMsgs()
}
}

bool TryPlayerBlock(const Player &attacker, const Player &target, bool shift /*= false*/)
{
if (!target._pBlockFlag || IsNoneOf(target._pmode, PM_STAND, PM_ATTACK) || shift)
return false;

int blk = 100;

if ((target._pmode == PM_STAND || target._pmode == PM_ATTACK) && target._pBlockFlag)
blk = GenerateRnd(100);

int blkper = target.GetBlockChance() - (attacker.getCharacterLevel() * 2);

blkper = std::clamp(blkper, 0, 100);

if (blk < blkper) {
NetSendCmdParam1(true, CMD_PLRINFLICTBLOCK, target.getId());
return true;
}
return false;
}

#ifdef BUILD_TESTING
bool TestPlayerDoGotHit(Player &player)
{
Expand Down
1 change: 1 addition & 0 deletions Source/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -984,5 +984,6 @@ void SetPlrDex(Player &player, int v);
void SetPlrVit(Player &player, int v);
void InitDungMsgs(Player &player);
void PlayDungMsgs();
bool TryPlayerBlock(const Player &attacker, const Player &target, bool shift = false);

} // namespace devilution
Loading