Skip to content

Commit

Permalink
Core: Add ->copy() helper to CBasicPacket
Browse files Browse the repository at this point in the history
  • Loading branch information
zach2good committed Jan 15, 2025
1 parent d7b3bc4 commit 80afa51
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/map/ai/states/ability_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ CAbilityState::CAbilityState(CBattleEntity* PEntity, uint16 targid, uint16 abili

if (!PTarget || m_errorMsg)
{
throw CStateInitException(std::move(m_errorMsg));
throw CStateInitException(m_errorMsg->copy());
}
SetTarget(PTarget->targid);
m_PAbility = std::make_unique<CAbility>(*PAbility);
Expand Down
2 changes: 1 addition & 1 deletion src/map/ai/states/attack_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ CAttackState::CAttackState(CBattleEntity* PEntity, uint16 targid)
if (!GetTarget() || m_errorMsg)
{
PEntity->SetBattleTargetID(0);
throw CStateInitException(std::move(m_errorMsg));
throw CStateInitException(m_errorMsg->copy());
}
if (PEntity->PAI->PathFind)
{
Expand Down
4 changes: 2 additions & 2 deletions src/map/ai/states/item_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ CItemState::CItemState(CCharEntity* PEntity, uint16 targid, uint8 loc, uint8 slo

if (!PTarget || m_errorMsg)
{
throw CStateInitException(std::move(m_errorMsg));
throw CStateInitException(m_errorMsg->copy());
}

auto [error, param, value] = luautils::OnItemCheck(PTarget, m_PItem, ITEMCHECK::NONE, m_PEntity);
Expand Down Expand Up @@ -294,7 +294,7 @@ void CItemState::InterruptItem(action_t& action)
actionTarget.messageID = 0;
actionTarget.knockback = 0;

m_PEntity->pushPacket(std::move(m_errorMsg));
m_PEntity->pushPacket(m_errorMsg->copy());
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/map/ai/states/magic_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ CMagicState::CMagicState(CBattleEntity* PEntity, uint16 targid, SpellID spellid,
auto* PTarget = m_PEntity->IsValidTarget(m_targid, m_PSpell->getValidTarget(), m_errorMsg);
if (!PTarget || m_errorMsg)
{
throw CStateInitException(std::move(m_errorMsg));
throw CStateInitException(m_errorMsg->copy());
}

if (!CanCastSpell(PTarget, false))
{
throw CStateInitException(std::move(m_errorMsg));
throw CStateInitException(m_errorMsg->copy());
}

auto errorMsg = luautils::OnMagicCastingCheck(m_PEntity, PTarget, GetSpell());
Expand Down
2 changes: 1 addition & 1 deletion src/map/ai/states/mobskill_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ CMobSkillState::CMobSkillState(CBattleEntity* PEntity, uint16 targid, uint16 wsi

if (!PTarget || m_errorMsg)
{
throw CStateInitException(std::move(m_errorMsg));
throw CStateInitException(m_errorMsg->copy());
}

m_PSkill = std::make_unique<CMobSkill>(*skill);
Expand Down
2 changes: 1 addition & 1 deletion src/map/ai/states/petskill_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ CPetSkillState::CPetSkillState(CPetEntity* PEntity, uint16 targid, uint16 wsid)

if (!PTarget || m_errorMsg)
{
throw CStateInitException(std::move(m_errorMsg));
throw CStateInitException(m_errorMsg->copy());
}

m_PSkill = std::make_unique<CPetSkill>(*skill);
Expand Down
8 changes: 4 additions & 4 deletions src/map/ai/states/range_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ CRangeState::CRangeState(CBattleEntity* PEntity, uint16 targid)

if (!PTarget || m_errorMsg)
{
throw CStateInitException(std::move(m_errorMsg));
throw CStateInitException(m_errorMsg->copy());
}

if (!CanUseRangedAttack(PTarget, false))
{
throw CStateInitException(std::move(m_errorMsg));
throw CStateInitException(m_errorMsg->copy());
}

if (distance(m_PEntity->loc.p, PTarget->loc.p) > 25)
{
m_errorMsg = std::make_unique<CMessageBasicPacket>(m_PEntity, PTarget, 0, 0, MSGBASIC_TOO_FAR_AWAY);
throw CStateInitException(std::move(m_errorMsg));
throw CStateInitException(m_errorMsg->copy());
}

auto delay = m_PEntity->GetRangedWeaponDelay(false);
Expand Down Expand Up @@ -124,7 +124,7 @@ bool CRangeState::Update(time_point tick)

if (auto* PChar = dynamic_cast<CCharEntity*>(m_PEntity))
{
PChar->pushPacket(std::move(m_errorMsg));
PChar->pushPacket(m_errorMsg->copy());
}
// reset aim time so interrupted players only have to wait the correct 2.7s until next shot
m_aimTime = std::chrono::seconds(0);
Expand Down
2 changes: 1 addition & 1 deletion src/map/ai/states/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ bool CState::HasErrorMsg() const

auto CState::GetErrorMsg() -> std::unique_ptr<CBasicPacket>
{
return std::move(m_errorMsg);
return m_errorMsg->copy();
}

bool CState::DoUpdate(time_point tick)
Expand Down
2 changes: 1 addition & 1 deletion src/map/ai/states/weaponskill_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ CWeaponSkillState::CWeaponSkillState(CBattleEntity* PEntity, uint16 targid, uint

if (!PTarget || m_errorMsg)
{
throw CStateInitException(std::move(m_errorMsg));
throw CStateInitException(m_errorMsg->copy());
}

if (!m_PEntity->CanSeeTarget(PTarget, false))
Expand Down
5 changes: 5 additions & 0 deletions src/map/packets/basic.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ class CBasicPacket
CBasicPacket& operator=(const CBasicPacket& other) = delete;
CBasicPacket& operator=(CBasicPacket&& other) noexcept = delete;

auto copy() -> std::unique_ptr<std::remove_pointer_t<decltype(this)>>
{
return std::make_unique<std::remove_pointer_t<decltype(this)>>(*this);
}

uint16 getType()
{
return ref<uint16>(0) & 0x1FF;
Expand Down

0 comments on commit 80afa51

Please sign in to comment.