refactor: death, stun, buff and knockback game messages as structs

Converts the combat messages to NetGameMsgs in CombatMessages.{h,cpp}:
Die, Resurrect, SetResurrectRestoreValues, SetPlayerAllowedRespawn
(SendToClient, as before), Knockback, SetStunned, SetStunImmunity,
SetStatusImmunity, AddBuff, RemoveBuff, AddRunSpeedModifier,
RemoveRunSpeedModifier and DeactivateBubbleBuffFromServer, and the
received RequestDie, RequestSmashPlayer, RequestResurrect, Resurrect,
ActivateBubbleBuff and DeactivateBubbleBuff. Smash and UnSmash move
here from GameMessages.h (Smash's ghostCapacity is ghostOpacity, the
client's name) and gain a Deserialize.

The callers (DestroyableComponent, BuffComponent,
ControllablePhysicsComponent, QuickBuildComponent, RacingControl,
RailActivator and the scripts) build the structs. SendResurrect's
respawn timer moves to DestroyableComponent::Resurrect. The received
messages are registered in GameMessageHandler's map and their switch
cases are deleted, along with the unused HandleRequestDie overload and
the Send* functions nothing called (SendSmash, SendUnSmash, the run
speed modifiers, SendActivateBubbleBuffFromServer). Handler logic is
unchanged.

No wire change and no change in recipients. Verified byte for byte
against a frozen verbatim copy of the old functions over an input grid
(every optional field, bool patterns, empty and long strings), received
messages compared with the old handlers' read sequences and truncated
payloads rejected, hand computed golden bytes, round trips, and a
deliberate width mutation made the tests fail.

Known difference from the client, not changed here: DLU writes
SetStatusImmunity's nine flags in its own order; the client reads them
alphabetically (GameMessage::SetStatusImmunity::Serialize @ 00d8f140).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Aaron Kimbrell
2026-09-27 00:01:18 -05:00
parent fccc9aab62
commit 313f635543
38 changed files with 2002 additions and 808 deletions

View File

@@ -1,6 +1,7 @@
SET(DGAMEMESSAGES_TESTS
"ActivityMessagesTests.cpp"
"BuildingMessagesTests.cpp"
"CombatMessagesTests.cpp"
"GameMessageTests.cpp"
"GameMsgSplitTests.cpp"
"EffectsMessagesTests.cpp"

View File

@@ -0,0 +1,381 @@
#include "CombatMessages.h"
#include "GameDependencies.h"
#include "GameMessageTestUtils.h"
#include "Legacy/CombatMessagesLegacy.h"
#include "eKillType.h"
#include "eStateChangeType.h"
#include <array>
#include <functional>
#include <limits>
#include <string>
#include <vector>
#include <gtest/gtest.h>
using namespace GameMessageTestUtils;
namespace {
const std::vector<std::u16string> g_Strings = { u"", u"electro-shock-death", u"é中", std::u16string(300, u'x') };
const std::vector<LWOOBJID> g_Ids = { LWOOBJID_EMPTY, 0x1000000000000001LL, 0x0102030405060708LL };
const std::vector<float> g_Floats = { 0.0f, 90.0f, -1.5f };
PacketBytes Payload(const GameMessages::NetGameMsg& msg) {
RakNet::BitStream bitStream;
msg.Serialize(bitStream);
return FromBitStream(bitStream);
}
template<typename Result>
Result ReadWithLegacy(const GameMessages::NetGameMsg& msg, const std::function<Result(RakNet::BitStream&)>& read) {
RakNet::BitStream wire;
msg.Serialize(wire);
RakNet::BitStream legacyStream(wire.GetData(), wire.GetNumberOfBytesUsed(), false);
auto result = read(legacyStream);
EXPECT_EQ(legacyStream.GetReadOffset(), wire.GetNumberOfBitsUsed());
return result;
}
// For the old functions that always broadcast (SEND_PACKET_BROADCAST) whatever address they were given:
// the struct is sent with Send(UNASSIGNED_SYSTEM_ADDRESS) and must produce the same bytes and destination.
void ExpectSameAsLegacyBroadcast(const std::function<void(const SystemAddress&)>& legacySend, const GameMessages::NetGameMsg& msg) {
const auto legacyPackets = Capture([&] { legacySend(UNASSIGNED_SYSTEM_ADDRESS); });
const auto newPackets = Capture([&] { msg.Send(UNASSIGNED_SYSTEM_ADDRESS); });
ASSERT_EQ(legacyPackets.size(), 1);
ASSERT_EQ(newPackets.size(), 1);
EXPECT_PACKET_EQ(FromCapture(legacyPackets[0]), FromCapture(newPackets[0]));
EXPECT_TRUE(legacyPackets[0].broadcast);
EXPECT_TRUE(newPackets[0].broadcast);
EXPECT_EQ(legacyPackets[0].sysAddr, newPackets[0].sysAddr);
}
// Bit i of mask, for walking every combination of a message's bools.
bool Bit(uint32_t mask, int i) { return (mask >> i) & 1; }
}
class CombatMessagesTests : public GameDependenciesTest {
protected:
void SetUp() override { SetUpDependencies(); }
void TearDown() override { TearDownDependencies(); }
};
TEST_F(CombatMessagesTests, DieMatchesLegacy) {
for (const auto target : g_Targets) {
Entity entity(target, info);
for (const auto& deathType : g_Strings) {
for (const auto killType : { eKillType::VIOLENT, eKillType::SILENT }) {
for (const auto killer : g_Ids) {
for (const auto lootOwner : g_Ids) {
for (uint32_t mask = 0; mask < 4; mask++) {
for (const float value : g_Floats) {
GameMessages::Die msg;
msg.target = target;
msg.bClientDeath = Bit(mask, 0);
msg.bSpawnLoot = Bit(mask, 1);
msg.deathType = deathType;
msg.directionRelative_AngleXZ = value;
msg.directionRelative_AngleY = value * 2;
msg.directionRelative_Force = value * 3;
msg.killType = killType;
msg.killerID = killer;
msg.lootOwnerID = lootOwner;
ExpectSameAsLegacyBroadcast([&](const SystemAddress&) {
LegacyGameMessages::SendDie(&entity, killer, lootOwner, true, killType, deathType, value * 2, value, value * 3, Bit(mask, 0), Bit(mask, 1), 1.0f);
}, msg);
ExpectSameAsLegacyBroadcast([&](const SystemAddress&) {
LegacyGameMessages::SendDieNoImplCode(&entity, killer, lootOwner, killType, deathType, value * 2, value, value * 3, Bit(mask, 0), Bit(mask, 1));
}, msg);
const auto copy = RoundTrip(msg);
EXPECT_EQ(copy.deathType, deathType);
EXPECT_EQ(copy.killType, killType);
EXPECT_EQ(copy.lootOwnerID, lootOwner);
// RequestDie is read with the same layout.
GameMessages::RequestDie request;
request.bClientDeath = msg.bClientDeath;
request.bSpawnLoot = msg.bSpawnLoot;
request.deathType = deathType;
request.directionRelative_AngleXZ = value;
request.directionRelative_AngleY = value * 2;
request.directionRelative_Force = value * 3;
request.killType = killType;
request.killerID = killer;
request.lootOwnerID = lootOwner;
const auto legacy = ReadWithLegacy<LegacyGameMessages::LegacyRequestDie>(request, LegacyGameMessages::ReadRequestDie);
const auto requestCopy = RoundTrip(request);
EXPECT_EQ(requestCopy.bClientDeath, legacy.bClientDeath);
EXPECT_EQ(requestCopy.bSpawnLoot, legacy.bSpawnLoot);
EXPECT_EQ(requestCopy.deathType, legacy.deathType);
EXPECT_EQ(requestCopy.directionRelative_AngleXZ, legacy.directionRelativeAngleXZ);
EXPECT_EQ(requestCopy.directionRelative_AngleY, legacy.directionRelativeAngleY);
EXPECT_EQ(requestCopy.directionRelative_Force, legacy.directionRelativeForce);
EXPECT_EQ(requestCopy.killType, legacy.killType);
EXPECT_EQ(requestCopy.killerID, legacy.killerID);
EXPECT_EQ(requestCopy.lootOwnerID, legacy.lootOwnerID);
if (deathType.size() < 30 && value == 0.0f) ExpectTruncatedFails(request);
}
}
}
}
}
}
}
}
TEST_F(CombatMessagesTests, ResurrectAndRespawnMatchLegacy) {
for (const auto target : g_Targets) {
Entity entity(target, info);
GameMessages::Resurrect msg;
msg.target = target;
ExpectSameAsLegacyBroadcast([&](const SystemAddress&) { LegacyGameMessages::SendResurrect(&entity); }, msg);
for (const bool immediately : { false, true }) {
msg.bRezImmediately = immediately;
EXPECT_EQ(ReadWithLegacy<bool>(msg, LegacyGameMessages::ReadResurrect), immediately);
EXPECT_EQ(RoundTrip(msg).bRezImmediately, immediately);
ExpectTruncatedFails(msg);
GameMessages::SetPlayerAllowedRespawn respawn;
respawn.target = target;
respawn.dontPromptForRespawn = immediately;
ExpectSameAsLegacy([&](const SystemAddress& a) { LegacyGameMessages::SendPlayerAllowedRespawn(target, immediately, a); }, respawn, SendMode::SendToClient);
EXPECT_EQ(RoundTrip(respawn).dontPromptForRespawn, immediately);
}
for (const int32_t armor : { -1, 0, 5 }) {
for (const int32_t health : { -1, 0, 7 }) {
for (const int32_t imagination : { -1, 0, std::numeric_limits<int32_t>::max() }) {
GameMessages::SetResurrectRestoreValues restore;
restore.target = target;
restore.iArmorRestore = armor;
restore.iHealthRestore = health;
restore.iImaginationRestore = imagination;
ExpectSameAsLegacyBroadcast([&](const SystemAddress&) { LegacyGameMessages::SendSetResurrectRestoreValues(&entity, armor, health, imagination); }, restore);
const auto copy = RoundTrip(restore);
EXPECT_EQ(copy.iArmorRestore, armor);
EXPECT_EQ(copy.iImaginationRestore, imagination);
}
}
}
}
}
TEST_F(CombatMessagesTests, KnockbackAndSmashMatchLegacy) {
for (const auto target : g_Targets) {
Entity entity(target, info);
for (const auto caster : g_Ids) {
for (const auto originator : g_Ids) {
for (const int32_t time : { 0, 1000, -1 }) {
GameMessages::Knockback msg;
msg.target = target;
msg.Caster = caster;
msg.Originator = originator;
msg.iKnockBackTimeMS = time;
msg.vector = NiPoint3(-20.0f, 10.0f, 0.5f);
ExpectSameAsLegacyBroadcast([&](const SystemAddress&) { LegacyGameMessages::SendKnockback(target, caster, originator, time, msg.vector); }, msg);
const auto copy = RoundTrip(msg);
EXPECT_EQ(copy.Caster, caster);
EXPECT_EQ(copy.iKnockBackTimeMS, time);
EXPECT_EQ(copy.vector, msg.vector);
}
}
for (const float force : g_Floats) {
for (const bool ignore : { false, true }) {
GameMessages::Smash smash;
smash.target = target;
smash.bIgnoreObjectVisibility = ignore;
smash.force = force;
smash.ghostOpacity = force / 2;
smash.killerID = caster;
ExpectSameAsLegacyBroadcast([&](const SystemAddress&) { LegacyGameMessages::SendSmash(&entity, force, force / 2, caster, ignore); }, smash);
EXPECT_EQ(RoundTrip(smash).killerID, caster);
}
GameMessages::UnSmash unsmash;
unsmash.target = target;
unsmash.builderID = caster;
unsmash.duration = force == 0.0f ? 3.0f : force;
ExpectSameAsLegacyBroadcast([&](const SystemAddress&) { LegacyGameMessages::SendUnSmash(&entity, caster, unsmash.duration); }, unsmash);
}
}
}
}
TEST_F(CombatMessagesTests, StunsAndImmunitiesMatchLegacy) {
for (const auto target : g_Targets) {
for (const auto originator : g_Ids) {
for (const auto state : { eStateChangeType::PUSH, eStateChangeType::POP }) {
// 17 bools: walk each one alone, all of them, none, and alternating patterns.
std::vector<uint32_t> masks = { 0, 0x1FFFF, 0x0AAAA, 0x15555 };
for (int i = 0; i < 17; i++) masks.push_back(1u << i);
for (const auto mask : masks) {
GameMessages::SetStunned msg;
msg.target = target;
msg.Originator = originator;
msg.StateChangeType = state;
msg.bCantAttack = Bit(mask, 0);
msg.bCantEquip = Bit(mask, 1);
msg.bCantInteract = Bit(mask, 2);
msg.bCantJump = Bit(mask, 3);
msg.bCantMove = Bit(mask, 4);
msg.bCantTurn = Bit(mask, 5);
msg.bCantUseItem = Bit(mask, 6);
msg.bDontTerminateInteract = Bit(mask, 7);
msg.bIgnoreImmunity = Bit(mask, 8);
msg.bCantAttackOutChangeWasApplied = Bit(mask, 9);
msg.bCantEquipOutChangeWasApplied = Bit(mask, 10);
msg.bCantInteractOutChangeWasApplied = Bit(mask, 11);
msg.bCantJumpOutChangeWasApplied = Bit(mask, 12);
msg.bCantMoveOutChangeWasApplied = Bit(mask, 13);
msg.bCantTurnOutChangeWasApplied = Bit(mask, 14);
msg.bCantUseItemOutChangeWasApplied = Bit(mask, 15);
ExpectSameAsLegacy([&](const SystemAddress& a) {
LegacyGameMessages::SendSetStunned(target, state, a, originator, Bit(mask, 0), Bit(mask, 1), Bit(mask, 2), Bit(mask, 3), Bit(mask, 4), Bit(mask, 5), Bit(mask, 6), Bit(mask, 7), Bit(mask, 8),
Bit(mask, 9), Bit(mask, 10), Bit(mask, 11), Bit(mask, 12), Bit(mask, 13), Bit(mask, 14), Bit(mask, 15));
}, msg);
const auto copy = RoundTrip(msg);
EXPECT_EQ(copy.bCantUseItemOutChangeWasApplied, msg.bCantUseItemOutChangeWasApplied);
EXPECT_EQ(copy.Originator, originator);
GameMessages::SetStunImmunity immunity;
immunity.target = target;
immunity.Caster = originator;
immunity.StateChangeType = state;
immunity.bImmuneToStunAttack = Bit(mask, 0);
immunity.bImmuneToStunEquip = Bit(mask, 1);
immunity.bImmuneToStunInteract = Bit(mask, 2);
immunity.bImmuneToStunJump = Bit(mask, 3);
immunity.bImmuneToStunMove = Bit(mask, 4);
immunity.bImmuneToStunTurn = Bit(mask, 5);
immunity.bImmuneToStunUseItem = Bit(mask, 6);
ExpectSameAsLegacy([&](const SystemAddress& a) {
LegacyGameMessages::SendSetStunImmunity(target, state, a, originator, Bit(mask, 0), Bit(mask, 1), Bit(mask, 2), Bit(mask, 3), Bit(mask, 4), Bit(mask, 5), Bit(mask, 6));
}, immunity);
EXPECT_EQ(RoundTrip(immunity).bImmuneToStunUseItem, Bit(mask, 6));
GameMessages::SetStatusImmunity status;
status.target = target;
status.StateChangeType = state;
status.bImmuneToBasicAttack = Bit(mask, 0);
status.bImmuneToDOT = Bit(mask, 1);
status.bImmuneToKnockback = Bit(mask, 2);
status.bImmuneToInterrupt = Bit(mask, 3);
status.bImmuneToSpeed = Bit(mask, 4);
status.bImmuneToImaginationGain = Bit(mask, 5);
status.bImmuneToImaginationLoss = Bit(mask, 6);
status.bImmuneToQuickbuildInterrupt = Bit(mask, 7);
status.bImmuneToPullToPoint = Bit(mask, 8);
ExpectSameAsLegacy([&](const SystemAddress& a) {
LegacyGameMessages::SendSetStatusImmunity(target, state, a, Bit(mask, 0), Bit(mask, 1), Bit(mask, 2), Bit(mask, 3), Bit(mask, 4), Bit(mask, 5), Bit(mask, 6), Bit(mask, 7), Bit(mask, 8));
}, status);
EXPECT_EQ(RoundTrip(status).bImmuneToPullToPoint, Bit(mask, 8));
}
}
}
}
}
TEST_F(CombatMessagesTests, BuffsMatchLegacy) {
for (const auto target : g_Targets) {
Entity entity(target, info);
for (const auto caster : g_Ids) {
for (const uint32_t buffID : { 0u, 42u, 0xFFFFFFFFu }) {
for (const uint32_t duration : { 0u, 5000u }) {
for (uint32_t mask = 0; mask < (1u << 11); mask += 37) {
GameMessages::AddBuff msg;
msg.target = target;
msg.bAddedByTeammate = Bit(mask, 0);
msg.bApplyOnTeammates = Bit(mask, 1);
msg.bCancelOnDamaged = Bit(mask, 2);
msg.bCancelOnDeath = Bit(mask, 3);
msg.bCancelOnLogOut = Bit(mask, 4);
msg.bCancelOnRemoveBuff = Bit(mask, 5);
msg.bCancelOnUI = Bit(mask, 6);
msg.bCancelOnUnEquip = Bit(mask, 7);
msg.bCancelOnZone = Bit(mask, 8);
msg.bIsImmunity = Bit(mask, 9);
msg.i64AddedBy = caster;
msg.uiBuffID = buffID;
msg.uiDurationMS = duration;
auto objectID = target;
ExpectSameAsLegacy([&](const SystemAddress& a) {
LegacyGameMessages::SendAddBuff(objectID, caster, buffID, duration, Bit(mask, 9), Bit(mask, 2), Bit(mask, 3), Bit(mask, 4), Bit(mask, 5), Bit(mask, 6), Bit(mask, 7), Bit(mask, 8), Bit(mask, 0), Bit(mask, 1), a);
}, msg);
const auto copy = RoundTrip(msg);
EXPECT_EQ(copy.uiDurationMS, duration);
EXPECT_EQ(copy.i64AddedBy, caster);
}
}
for (uint32_t mask = 0; mask < 4; mask++) {
GameMessages::RemoveBuff remove;
remove.target = target;
remove.bFromUnEquip = Bit(mask, 0);
remove.bRemoveImmunity = Bit(mask, 1);
remove.uiBuffID = buffID;
ExpectSameAsLegacyBroadcast([&](const SystemAddress&) { LegacyGameMessages::SendRemoveBuff(&entity, Bit(mask, 0), Bit(mask, 1), buffID); }, remove);
EXPECT_EQ(RoundTrip(remove).uiBuffID, buffID);
}
}
for (const uint32_t modifier : { 500u, 0u, 750u }) {
GameMessages::AddRunSpeedModifier add;
add.target = target;
add.i64Caster = caster;
add.uiModifier = modifier;
ExpectSameAsLegacy([&](const SystemAddress& a) { LegacyGameMessages::SendAddRunSpeedModifier(target, caster, modifier, a); }, add);
EXPECT_EQ(RoundTrip(add).uiModifier, modifier);
GameMessages::RemoveRunSpeedModifier remove;
remove.target = target;
remove.uiModifier = modifier;
ExpectSameAsLegacy([&](const SystemAddress& a) { LegacyGameMessages::SendRemoveRunSpeedModifier(target, modifier, a); }, remove);
}
}
GameMessages::DeactivateBubbleBuffFromServer deactivate;
deactivate.target = target;
ExpectSameAsLegacy([&](const SystemAddress& a) { LegacyGameMessages::SendDeactivateBubbleBuffFromServer(target, a); }, deactivate);
}
for (const auto& type : { std::u16string(u""), std::u16string(u"skunk"), std::u16string(u"energy") }) {
for (const bool special : { false, true }) {
GameMessages::ActivateBubbleBuff activate;
activate.bSpecialAnims = special;
activate.wszType = type;
const auto legacy = ReadWithLegacy<LegacyGameMessages::LegacyActivateBubbleBuff>(activate, LegacyGameMessages::ReadActivateBubbleBuff);
EXPECT_TRUE(legacy.read);
EXPECT_EQ(legacy.specialAnimations, special);
EXPECT_EQ(legacy.type, type);
EXPECT_EQ(RoundTrip(activate).wszType, type);
ExpectTruncatedFails(activate);
}
}
RakNet::BitStream empty;
EXPECT_TRUE(GameMessages::DeactivateBubbleBuff().Deserialize(empty));
EXPECT_TRUE(GameMessages::RequestSmashPlayer().Deserialize(empty));
EXPECT_TRUE(GameMessages::RequestResurrect().Deserialize(empty));
}
// Independent of the legacy code: hand computed payloads (RakNet writes MSB first).
TEST_F(CombatMessagesTests, GoldenBytes) {
GameMessages::Die die;
die.killerID = 0x11;
die.deathType = u"A";
// bClientDeath 0, bSpawnLoot 1, u32 1, 'A' u16, three zero floats, killType flag 0, killer, loot owner flag 0.
EXPECT_PACKET_EQ(FromHex("40 40 00 00 10 40 00 00 00 00 00 00 00 00 00 00 00 00 02 20 00 00 00 00 00 00 00", 212), Payload(die));
GameMessages::Knockback knockback;
knockback.iKnockBackTimeMS = 1000;
knockback.vector = NiPoint3(1.0f, 0.0f, 0.0f);
EXPECT_PACKET_EQ(FromHex("3d 00 60 00 00 00 10 07 e0 00 00 00 00 00 00 00 00", 131), Payload(knockback));
GameMessages::SetStunned stun;
stun.StateChangeType = eStateChangeType::POP;
stun.bCantMove = true;
// originator flag 0, u32 1, 14 flags (bCantMove is the 9th), bDontTerminateInteract 0, bIgnoreImmunity 1.
EXPECT_PACKET_EQ(FromHex("00 80 00 00 00 40 80", 49), Payload(stun));
GameMessages::RemoveBuff remove;
remove.bRemoveImmunity = true;
remove.uiBuffID = 3;
EXPECT_PACKET_EQ(FromHex("20 60 00 00 00", 35), Payload(remove));
}

View File

@@ -0,0 +1,480 @@
#ifndef COMBATMESSAGESLEGACY_H
#define COMBATMESSAGESLEGACY_H
// FROZEN ORACLE - DO NOT EDIT.
// Verbatim copies of the hand written GameMessages functions that CombatMessages.h replaced
// (dGame/dGameMessages/GameMessages.cpp, branched from origin/main 129199e4). Only the namespace changed.
// The Read* functions are the read sequences of the replaced GameMessages::Handle* functions, verbatim up to
// the point where the handler starts using what it read.
#include "BitStreamUtils.h"
#include "dCommonVars.h"
#include "dServer.h"
#include "DestroyableComponent.h"
#include "eKillType.h"
#include "eStateChangeType.h"
#include "Entity.h"
#include "Game.h"
#include "GameMessages.h"
#include "GeneralUtils.h"
#include "LevelProgressionComponent.h"
#include "MessageType/Client.h"
#include "MessageType/Game.h"
#include "NiPoint3.h"
#include "ServiceType.h"
#include <string>
namespace LegacyGameMessages {
inline void SendPlayerAllowedRespawn(LWOOBJID entityID, bool doNotPromptRespawn, const SystemAddress& sysAddr) {
CBITSTREAM;
CMSGHEADER;
bitStream.Write(entityID);
bitStream.Write(MessageType::Game::SET_PLAYER_ALLOWED_RESPAWN);
bitStream.Write(doNotPromptRespawn);
SEND_PACKET;
}
inline void SendKnockback(const LWOOBJID& objectID, const LWOOBJID& caster, const LWOOBJID& originator, int knockBackTimeMS, const NiPoint3& vector) {
CBITSTREAM;
CMSGHEADER;
bitStream.Write(objectID);
bitStream.Write(MessageType::Game::KNOCKBACK);
bool casterFlag = caster != LWOOBJID_EMPTY;
bool originatorFlag = originator != LWOOBJID_EMPTY;
bool knockBackTimeMSFlag = knockBackTimeMS != 0;
bitStream.Write(casterFlag);
if (casterFlag) bitStream.Write(caster);
bitStream.Write(originatorFlag);
if (originatorFlag) bitStream.Write(originator);
bitStream.Write(knockBackTimeMSFlag);
if (knockBackTimeMSFlag) bitStream.Write(knockBackTimeMS);
bitStream.Write(vector);
SEND_PACKET_BROADCAST;
}
inline void SendDieNoImplCode(Entity* entity, const LWOOBJID& killerID, const LWOOBJID& lootOwnerID, eKillType killType, std::u16string deathType, float directionRelative_AngleY, float directionRelative_AngleXZ, float directionRelative_Force, bool bClientDeath, bool bSpawnLoot) {
CBITSTREAM;
CMSGHEADER;
bitStream.Write(entity->GetObjectID());
bitStream.Write(MessageType::Game::DIE);
bitStream.Write(bClientDeath);
bitStream.Write(bSpawnLoot);
bitStream.Write<uint32_t>(deathType.size());
bitStream.Write(deathType);
bitStream.Write(directionRelative_AngleXZ);
bitStream.Write(directionRelative_AngleY);
bitStream.Write(directionRelative_Force);
bitStream.Write(killType != eKillType::VIOLENT);
if (killType != eKillType::VIOLENT) bitStream.Write(killType);
bitStream.Write(killerID);
bitStream.Write(lootOwnerID != LWOOBJID_EMPTY);
if (lootOwnerID != LWOOBJID_EMPTY) {
bitStream.Write(lootOwnerID);
}
SEND_PACKET_BROADCAST;
}
inline void SendDie(Entity* entity, const LWOOBJID& killerID, const LWOOBJID& lootOwnerID, bool bDieAccepted, eKillType killType, std::u16string deathType, float directionRelative_AngleY, float directionRelative_AngleXZ, float directionRelative_Force, bool bClientDeath, bool bSpawnLoot, float coinSpawnTime) {
CBITSTREAM;
CMSGHEADER;
bitStream.Write(entity->GetObjectID());
bitStream.Write(MessageType::Game::DIE);
bitStream.Write(bClientDeath);
bitStream.Write(bSpawnLoot);
//bitStream.Write(coinSpawnTime != -1.0f);
//if (coinSpawnTime != -1.0f) bitStream.Write(coinSpawnTime);
uint32_t deathTypeLength = deathType.size();
bitStream.Write(deathTypeLength);
for (uint32_t k = 0; k < deathTypeLength; k++) {
bitStream.Write<uint16_t>(deathType[k]);
}
bitStream.Write(directionRelative_AngleXZ);
bitStream.Write(directionRelative_AngleY);
bitStream.Write(directionRelative_Force);
bitStream.Write(killType != eKillType::VIOLENT);
if (killType != eKillType::VIOLENT) bitStream.Write(killType);
bitStream.Write(killerID);
bitStream.Write(lootOwnerID != LWOOBJID_EMPTY);
if (lootOwnerID != LWOOBJID_EMPTY) {
bitStream.Write(lootOwnerID);
}
SEND_PACKET_BROADCAST;
}
inline void SendResurrect(Entity* entity) {
// Restore the players health after the animation for respawning has finished.
// This is when the health appered back in live, not immediately upon requesting respawn
// Add a half second in case someone decides to cheat and move during the death animation
// and just make sure the client has time to be ready.
constexpr float respawnTime = 3.66700005531311f + 0.5f;
entity->AddCallbackTimer(respawnTime, [=]() {
GameMessages::PlayerResurrectionFinished msg;
entity->NotifyPlayerResurrectionFinished(msg);
auto* destroyableComponent = entity->GetComponent<DestroyableComponent>();
if (destroyableComponent != nullptr && entity->GetLOT() == 1) {
destroyableComponent->SetIsDead(false);
auto* levelComponent = entity->GetComponent<LevelProgressionComponent>();
if (levelComponent) {
int32_t healthToRestore = levelComponent->GetLevel() >= 45 ? 8 : 4;
if (healthToRestore > destroyableComponent->GetMaxHealth()) healthToRestore = destroyableComponent->GetMaxHealth();
destroyableComponent->SetHealth(healthToRestore);
int32_t imaginationToRestore = levelComponent->GetLevel() >= 45 ? 20 : 6;
if (imaginationToRestore > destroyableComponent->GetMaxImagination()) imaginationToRestore = destroyableComponent->GetMaxImagination();
destroyableComponent->SetImagination(imaginationToRestore);
}
}
});
CBITSTREAM;
CMSGHEADER;
bool bRezImmediately = false;
bitStream.Write(entity->GetObjectID());
bitStream.Write(MessageType::Game::RESURRECT);
bitStream.Write(bRezImmediately);
SEND_PACKET_BROADCAST;
}
inline void SendSetStunned(LWOOBJID objectId, eStateChangeType stateChangeType, const SystemAddress& sysAddr,
LWOOBJID originator, bool bCantAttack, bool bCantEquip,
bool bCantInteract, bool bCantJump, bool bCantMove, bool bCantTurn,
bool bCantUseItem, bool bDontTerminateInteract, bool bIgnoreImmunity,
bool bCantAttackOutChangeWasApplied, bool bCantEquipOutChangeWasApplied,
bool bCantInteractOutChangeWasApplied, bool bCantJumpOutChangeWasApplied,
bool bCantMoveOutChangeWasApplied, bool bCantTurnOutChangeWasApplied,
bool bCantUseItemOutChangeWasApplied) {
CBITSTREAM;
CMSGHEADER;
bitStream.Write(objectId);
bitStream.Write(MessageType::Game::SET_STUNNED);
bitStream.Write(originator != LWOOBJID_EMPTY);
if (originator != LWOOBJID_EMPTY) bitStream.Write(originator);
bitStream.Write(stateChangeType);
bitStream.Write(bCantAttack);
bitStream.Write(bCantAttackOutChangeWasApplied);
bitStream.Write(bCantEquip);
bitStream.Write(bCantEquipOutChangeWasApplied);
bitStream.Write(bCantInteract);
bitStream.Write(bCantInteractOutChangeWasApplied);
bitStream.Write(bCantJump);
bitStream.Write(bCantJumpOutChangeWasApplied);
bitStream.Write(bCantMove);
bitStream.Write(bCantMoveOutChangeWasApplied);
bitStream.Write(bCantTurn);
bitStream.Write(bCantTurnOutChangeWasApplied);
bitStream.Write(bCantUseItem);
bitStream.Write(bCantUseItemOutChangeWasApplied);
bitStream.Write(bDontTerminateInteract);
bitStream.Write(bIgnoreImmunity);
if (sysAddr == UNASSIGNED_SYSTEM_ADDRESS) SEND_PACKET_BROADCAST;
SEND_PACKET;
}
inline void SendSetStunImmunity(LWOOBJID target, eStateChangeType state, const SystemAddress& sysAddr,
LWOOBJID originator,
bool bImmuneToStunAttack,
bool bImmuneToStunEquip,
bool bImmuneToStunInteract,
bool bImmuneToStunJump,
bool bImmuneToStunMove,
bool bImmuneToStunTurn,
bool bImmuneToStunUseItem) {
CBITSTREAM;
CMSGHEADER;
bitStream.Write(target);
bitStream.Write(MessageType::Game::SET_STUN_IMMUNITY);
bitStream.Write(originator != LWOOBJID_EMPTY);
if (originator != LWOOBJID_EMPTY) bitStream.Write(originator);
bitStream.Write(state);
bitStream.Write(bImmuneToStunAttack);
bitStream.Write(bImmuneToStunEquip);
bitStream.Write(bImmuneToStunInteract);
bitStream.Write(bImmuneToStunJump);
bitStream.Write(bImmuneToStunMove);
bitStream.Write(bImmuneToStunTurn);
bitStream.Write(bImmuneToStunUseItem);
if (sysAddr == UNASSIGNED_SYSTEM_ADDRESS) SEND_PACKET_BROADCAST;
SEND_PACKET;
}
inline void SendSetStatusImmunity(LWOOBJID objectId, eStateChangeType state, const SystemAddress& sysAddr,
bool bImmuneToBasicAttack,
bool bImmuneToDamageOverTime,
bool bImmuneToKnockback,
bool bImmuneToInterrupt,
bool bImmuneToSpeed,
bool bImmuneToImaginationGain,
bool bImmuneToImaginationLoss,
bool bImmuneToQuickbuildInterrupt,
bool bImmuneToPullToPoint) {
CBITSTREAM;
CMSGHEADER;
bitStream.Write(objectId);
bitStream.Write(MessageType::Game::SET_STATUS_IMMUNITY);
bitStream.Write(state);
bitStream.Write(bImmuneToBasicAttack);
bitStream.Write(bImmuneToDamageOverTime);
bitStream.Write(bImmuneToKnockback);
bitStream.Write(bImmuneToInterrupt);
bitStream.Write(bImmuneToSpeed);
bitStream.Write(bImmuneToImaginationGain);
bitStream.Write(bImmuneToImaginationLoss);
bitStream.Write(bImmuneToQuickbuildInterrupt);
bitStream.Write(bImmuneToPullToPoint);
if (sysAddr == UNASSIGNED_SYSTEM_ADDRESS) SEND_PACKET_BROADCAST;
SEND_PACKET;
}
inline void SendAddRunSpeedModifier(LWOOBJID objectId, LWOOBJID caster, uint32_t modifier, const SystemAddress& sysAddr) {
CBITSTREAM;
CMSGHEADER;
bitStream.Write(objectId);
bitStream.Write(MessageType::Game::ADD_RUN_SPEED_MODIFIER);
bitStream.Write(caster != LWOOBJID_EMPTY);
if (caster != LWOOBJID_EMPTY) bitStream.Write(caster);
bitStream.Write(modifier != 500);
if (modifier != 500) bitStream.Write(modifier);
if (sysAddr == UNASSIGNED_SYSTEM_ADDRESS) SEND_PACKET_BROADCAST;
SEND_PACKET;
}
inline void SendRemoveRunSpeedModifier(LWOOBJID objectId, uint32_t modifier, const SystemAddress& sysAddr) {
CBITSTREAM;
CMSGHEADER;
bitStream.Write(objectId);
bitStream.Write(MessageType::Game::REMOVE_RUN_SPEED_MODIFIER);
bitStream.Write(modifier != 500);
if (modifier != 500) bitStream.Write(modifier);
if (sysAddr == UNASSIGNED_SYSTEM_ADDRESS) SEND_PACKET_BROADCAST;
SEND_PACKET;
}
inline void SendRemoveBuff(Entity* entity, bool fromUnEquip, bool removeImmunity, uint32_t buffId) {
CBITSTREAM;
CMSGHEADER;
bitStream.Write(entity->GetObjectID());
bitStream.Write(MessageType::Game::REMOVE_BUFF);
bitStream.Write(false); // bFromRemoveBehavior but setting this to true makes the GM not do anything on the client?
bitStream.Write(fromUnEquip);
bitStream.Write(removeImmunity);
bitStream.Write(buffId);
SEND_PACKET_BROADCAST;
}
inline void SendSetResurrectRestoreValues(Entity* targetEntity, int32_t armorRestore, int32_t healthRestore, int32_t imaginationRestore) {
CBITSTREAM;
CMSGHEADER;
bitStream.Write(targetEntity->GetObjectID());
bitStream.Write(MessageType::Game::SET_RESURRECT_RESTORE_VALUES);
bitStream.Write(armorRestore != -1);
if (armorRestore != -1) bitStream.Write(armorRestore);
bitStream.Write(healthRestore != -1);
if (healthRestore != -1) bitStream.Write(healthRestore);
bitStream.Write(imaginationRestore != -1);
if (imaginationRestore != -1) bitStream.Write(imaginationRestore);
SEND_PACKET_BROADCAST;
}
inline void SendAddBuff(LWOOBJID& objectID, const LWOOBJID& casterID, uint32_t buffID, uint32_t msDuration,
bool addImmunity, bool cancelOnDamaged, bool cancelOnDeath, bool cancelOnLogout,
bool cancelOnRemoveBuff, bool cancelOnUi, bool cancelOnUnequip, bool cancelOnZone, bool addedByTeammate, bool applyOnTeammates,
const SystemAddress& sysAddr) {
CBITSTREAM;
CMSGHEADER;
bitStream.Write(objectID);
bitStream.Write(MessageType::Game::ADD_BUFF);
bitStream.Write(addedByTeammate); // Added by teammate
bitStream.Write(applyOnTeammates); // Apply on teammates
bitStream.Write(cancelOnDamaged);
bitStream.Write(cancelOnDeath);
bitStream.Write(cancelOnLogout);
bitStream.Write(false); // Cancel on move
bitStream.Write(cancelOnRemoveBuff);
bitStream.Write(cancelOnUi);
bitStream.Write(cancelOnUnequip);
bitStream.Write(cancelOnZone);
bitStream.Write(false); // Ignore immunities
bitStream.Write(addImmunity);
bitStream.Write(false); // Use ref count
bitStream.Write(casterID != LWOOBJID_EMPTY);
if (casterID != LWOOBJID_EMPTY) bitStream.Write(casterID);
bitStream.Write(buffID);
bitStream.Write(msDuration != 0);
if (msDuration != 0) bitStream.Write(msDuration);
if (sysAddr == UNASSIGNED_SYSTEM_ADDRESS) SEND_PACKET_BROADCAST;
SEND_PACKET;
}
inline void SendDeactivateBubbleBuffFromServer(LWOOBJID objectId, const SystemAddress& sysAddr) {
CBITSTREAM;
CMSGHEADER;
bitStream.Write(objectId);
bitStream.Write(MessageType::Game::DEACTIVATE_BUBBLE_BUFF_FROM_SERVER);
if (sysAddr == UNASSIGNED_SYSTEM_ADDRESS) SEND_PACKET_BROADCAST;
SEND_PACKET;
}
inline void SendSmash(Entity* entity, float force, float ghostOpacity, LWOOBJID killerID, bool ignoreObjectVisibility) {
CBITSTREAM;
CMSGHEADER;
bitStream.Write(entity->GetObjectID());
bitStream.Write(MessageType::Game::SMASH);
bitStream.Write(ignoreObjectVisibility);
bitStream.Write(force);
bitStream.Write(ghostOpacity);
bitStream.Write(killerID);
SEND_PACKET_BROADCAST;
}
inline void SendUnSmash(Entity* entity, LWOOBJID builderID, float duration) {
CBITSTREAM;
CMSGHEADER;
bitStream.Write(entity->GetObjectID());
bitStream.Write(MessageType::Game::UN_SMASH);
bitStream.Write(builderID != LWOOBJID_EMPTY);
if (builderID != LWOOBJID_EMPTY) bitStream.Write(builderID);
bitStream.Write(duration != 3.0f);
if (duration != 3.0f) bitStream.Write(duration);
SEND_PACKET_BROADCAST;
}
// GameMessages::HandleRequestDie (the three argument overload the switch called)
struct LegacyRequestDie { bool bClientDeath{}; bool bSpawnLoot{}; std::u16string deathType{}; float directionRelativeAngleXZ{}; float directionRelativeAngleY{}; float directionRelativeForce{}; eKillType killType{}; LWOOBJID killerID{}; LWOOBJID lootOwnerID{}; };
inline LegacyRequestDie ReadRequestDie(RakNet::BitStream& inStream) {
bool bClientDeath;
bool bSpawnLoot;
std::u16string deathType;
float directionRelativeAngleXZ;
float directionRelativeAngleY;
float directionRelativeForce;
eKillType killType = eKillType::VIOLENT;
LWOOBJID killerID;
LWOOBJID lootOwnerID = LWOOBJID_EMPTY;
bClientDeath = inStream.ReadBit();
bSpawnLoot = inStream.ReadBit();
uint32_t deathTypeLength = 0;
inStream.Read(deathTypeLength);
for (size_t i = 0; i < deathTypeLength; i++) {
char16_t character;
inStream.Read(character);
deathType.push_back(character);
}
inStream.Read(directionRelativeAngleXZ);
inStream.Read(directionRelativeAngleY);
inStream.Read(directionRelativeForce);
if (inStream.ReadBit()) {
inStream.Read(killType);
}
inStream.Read(killerID);
if (inStream.ReadBit()) {
inStream.Read(lootOwnerID);
}
return { bClientDeath, bSpawnLoot, deathType, directionRelativeAngleXZ, directionRelativeAngleY, directionRelativeForce, killType, killerID, lootOwnerID };
}
// GameMessages::HandleResurrect
inline bool ReadResurrect(RakNet::BitStream& inStream) {
bool immediate = inStream.ReadBit();
return immediate;
}
// GameMessages::HandleActivateBubbleBuff
struct LegacyActivateBubbleBuff { bool read{}; bool specialAnimations{}; std::u16string type{}; };
inline LegacyActivateBubbleBuff ReadActivateBubbleBuff(RakNet::BitStream& inStream) {
bool specialAnimations;
if (!inStream.Read(specialAnimations)) return {};
std::u16string type = GeneralUtils::ReadWString(inStream);
return { true, specialAnimations, type };
}
}
#endif // COMBATMESSAGESLEGACY_H

View File

@@ -1,4 +1,5 @@
#include "GameMessages.h"
#include "CombatMessages.h"
#include "GameDependencies.h"
#include "PacketTestUtils.h"