From d358a4a6727e65b4983d7a24ada4410030c7f8e4 Mon Sep 17 00:00:00 2001 From: Nico <122193236+Nico8340@users.noreply.github.com> Date: Sat, 30 Mar 2024 19:28:19 +0100 Subject: [PATCH 1/5] Events for server-side --- Server/mods/deathmatch/logic/CGame.cpp | 2 + .../logic/CStaticFunctionDefinitions.cpp | 43 ++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/Server/mods/deathmatch/logic/CGame.cpp b/Server/mods/deathmatch/logic/CGame.cpp index c2c8485562..bf1e36f06b 100644 --- a/Server/mods/deathmatch/logic/CGame.cpp +++ b/Server/mods/deathmatch/logic/CGame.cpp @@ -1608,6 +1608,8 @@ void CGame::AddBuiltInEvents() m_Events.AddEvent("onElementModelChange", "oldModel, newModel", NULL, false); m_Events.AddEvent("onElementDimensionChange", "oldDimension, newDimension", nullptr, false); m_Events.AddEvent("onElementInteriorChange", "oldInterior, newInterior", nullptr, false); + m_Events.AddEvent("onElementAttach", "attachSource, attachOffsetX, attachOffsetY, attachOffsetZ, attachOffsetRX, attachOffsetRY, attachOffsetRZ", nullptr, false); + m_Events.AddEvent("onElementDetach", "detachSource, detachWorldX, detachWorldY, detachWorldZ, detachWorldRX, detachWorldRY, detachWorldRZ", nullptr, false); // Radar area events diff --git a/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp b/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp index 0542dce5fd..6f0d043121 100644 --- a/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp +++ b/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp @@ -1518,8 +1518,24 @@ bool CStaticFunctionDefinitions::AttachElements(CElement* pElement, CElement* pA if (pElement->IsAttachToable() && pAttachedToElement->IsAttachable() && !pAttachedToElement->IsAttachedToElement(pElement) && pElement->GetDimension() == pAttachedToElement->GetDimension()) { - pElement->SetAttachedOffsets(vecPosition, vecRotation); + CLuaArguments Arguments; + Arguments.PushElement(pAttachedToElement); + Arguments.PushNumber(vecPosition.fX); + Arguments.PushNumber(vecPosition.fY); + Arguments.PushNumber(vecPosition.fZ); + Arguments.PushNumber(vecRotation.fX); + Arguments.PushNumber(vecRotation.fY); + Arguments.PushNumber(vecRotation.fZ); + bool bContinue = pElement->CallEvent("onElementAttach", Arguments); + + if (!bContinue) + { + return false; + } + ConvertDegreesToRadians(vecRotation); + + pElement->SetAttachedOffsets(vecPosition, vecRotation); pElement->AttachTo(pAttachedToElement); CBitStream BitStream; @@ -1547,9 +1563,29 @@ bool CStaticFunctionDefinitions::DetachElements(CElement* pElement, CElement* pA { if (pAttachedToElement == NULL || pActualAttachedToElement == pAttachedToElement) { + CVector vecPosition = pElement->GetPosition(); + CVector vecRotation; + + pElement->GetRotation(vecRotation); + ConvertRadiansToDegrees(vecRotation); + + CLuaArguments Arguments; + Arguments.PushElement(pActualAttachedToElement); + Arguments.PushNumber(vecPosition.fX); + Arguments.PushNumber(vecPosition.fY); + Arguments.PushNumber(vecPosition.fZ); + Arguments.PushNumber(vecRotation.fX); + Arguments.PushNumber(vecRotation.fY); + Arguments.PushNumber(vecRotation.fZ); + bool bContinue = pElement->CallEvent("onElementDetach", Arguments); + + if (!bContinue) + { + return false; + } + // Detach it. Also generate a new time context to prevent sync screwup from // old packes arriving. - CVector vecPosition = pElement->GetPosition(); pElement->AttachTo(NULL); pElement->GenerateSyncTimeContext(); @@ -1558,6 +1594,9 @@ bool CStaticFunctionDefinitions::DetachElements(CElement* pElement, CElement* pA BitStream.pBitStream->Write(vecPosition.fX); BitStream.pBitStream->Write(vecPosition.fY); BitStream.pBitStream->Write(vecPosition.fZ); + BitStream.pBitStream->Write(vecRotation.fX); + BitStream.pBitStream->Write(vecRotation.fY); + BitStream.pBitStream->Write(vecRotation.fZ); m_pPlayerManager->BroadcastOnlyJoined(CElementRPCPacket(pElement, DETACH_ELEMENTS, *BitStream.pBitStream)); return true; From 91ad6d1568ece563fc831126b0b36d61f5204211 Mon Sep 17 00:00:00 2001 From: Nico <122193236+Nico8340@users.noreply.github.com> Date: Sat, 30 Mar 2024 19:28:29 +0100 Subject: [PATCH 2/5] Events for client-side --- Client/mods/deathmatch/logic/CClientGame.cpp | 2 + .../logic/CStaticFunctionDefinitions.cpp | 35 +++++++++++ .../deathmatch/logic/rpc/CElementRPCs.cpp | 62 +++++++++++++++++-- 3 files changed, 93 insertions(+), 6 deletions(-) diff --git a/Client/mods/deathmatch/logic/CClientGame.cpp b/Client/mods/deathmatch/logic/CClientGame.cpp index 63bd6174be..30594474a4 100644 --- a/Client/mods/deathmatch/logic/CClientGame.cpp +++ b/Client/mods/deathmatch/logic/CClientGame.cpp @@ -2637,6 +2637,8 @@ void CClientGame::AddBuiltInEvents() m_Events.AddEvent("onClientElementModelChange", "oldModel, newModel", nullptr, false); m_Events.AddEvent("onClientElementDimensionChange", "oldDimension, newDimension", nullptr, false); m_Events.AddEvent("onClientElementInteriorChange", "oldInterior, newInterior", nullptr, false); + m_Events.AddEvent("onClientElementAttach", "attachSource, attachOffsetX, attachOffsetY, attachOffsetZ, attachOffsetRX, attachOffsetRY, attachOffsetRZ", nullptr, false); + m_Events.AddEvent("onClientElementDetach", "detachSource, detachWorldX, detachWorldY, detachWorldZ, detachWorldRX, detachWorldRY, detachWorldRZ", nullptr, false); // Player events m_Events.AddEvent("onClientPlayerJoin", "", NULL, false); diff --git a/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp b/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp index be0e47a076..99959cda70 100644 --- a/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp +++ b/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp @@ -1367,6 +1367,20 @@ bool CStaticFunctionDefinitions::AttachElements(CClientEntity& Entity, CClientEn if (Entity.IsAttachToable() && AttachedToEntity.IsAttachable() && !AttachedToEntity.IsAttachedToElement(&Entity) && Entity.GetDimension() == AttachedToEntity.GetDimension()) { + CLuaArguments Arguments; + Arguments.PushElement(&AttachedToEntity); + Arguments.PushNumber(vecPosition.fX); + Arguments.PushNumber(vecPosition.fY); + Arguments.PushNumber(vecPosition.fZ); + Arguments.PushNumber(vecRotation.fX); + Arguments.PushNumber(vecRotation.fY); + Arguments.PushNumber(vecRotation.fZ); + bool bContinue = Entity.CallEvent("onClientElementAttach", Arguments, true); + + if (!bContinue) { + return false; + } + ConvertDegreesToRadians(vecRotation); Entity.SetAttachedOffsets(vecPosition, vecRotation); @@ -1387,6 +1401,27 @@ bool CStaticFunctionDefinitions::DetachElements(CClientEntity& Entity, CClientEn { if (pAttachedToEntity == NULL || pActualAttachedToEntity == pAttachedToEntity) { + CVector vecPosition; + CVector vecRotation; + + Entity.GetPosition(vecPosition); + Entity.GetRotationDegrees(vecRotation); + + CLuaArguments Arguments; + Arguments.PushElement(pActualAttachedToEntity); + Arguments.PushNumber(vecPosition.fX); + Arguments.PushNumber(vecPosition.fY); + Arguments.PushNumber(vecPosition.fZ); + Arguments.PushNumber(vecRotation.fX); + Arguments.PushNumber(vecRotation.fY); + Arguments.PushNumber(vecRotation.fZ); + bool bContinue = Entity.CallEvent("onClientElementDetach", Arguments, true); + + if (!bContinue) + { + return false; + } + Entity.AttachTo(NULL); return true; } diff --git a/Client/mods/deathmatch/logic/rpc/CElementRPCs.cpp b/Client/mods/deathmatch/logic/rpc/CElementRPCs.cpp index 1b5da5c780..fad923f6fa 100644 --- a/Client/mods/deathmatch/logic/rpc/CElementRPCs.cpp +++ b/Client/mods/deathmatch/logic/rpc/CElementRPCs.cpp @@ -321,8 +321,25 @@ void CElementRPCs::AttachElements(CClientEntity* pSource, NetBitStreamInterface& CClientEntity* pAttachedToEntity = CElementIDs::GetElement(usAttachedToID); if (pAttachedToEntity) { - pSource->SetAttachedOffsets(vecPosition, vecRotation); - pSource->AttachTo(pAttachedToEntity); + ConvertRadiansToDegrees(vecRotation); + + CLuaArguments Arguments; + Arguments.PushElement(pAttachedToEntity); + Arguments.PushNumber(vecPosition.fX); + Arguments.PushNumber(vecPosition.fY); + Arguments.PushNumber(vecPosition.fZ); + Arguments.PushNumber(vecRotation.fX); + Arguments.PushNumber(vecRotation.fY); + Arguments.PushNumber(vecRotation.fZ); + bool bContinue = pSource->CallEvent("onClientElementAttach", Arguments, true); + + if (bContinue) + { + ConvertDegreesToRadians(vecRotation); + + pSource->SetAttachedOffsets(vecPosition, vecRotation); + pSource->AttachTo(pAttachedToEntity); + } } } } @@ -332,13 +349,46 @@ void CElementRPCs::DetachElements(CClientEntity* pSource, NetBitStreamInterface& unsigned char ucTimeContext; if (bitStream.Read(ucTimeContext)) { - pSource->SetSyncTimeContext(ucTimeContext); - pSource->AttachTo(NULL); + ElementID usAttachedToID; + CClientEntity* pAttachedToEntity = CElementIDs::GetElement(usAttachedToID); CVector vecPosition; - if (bitStream.Read(vecPosition.fX) && bitStream.Read(vecPosition.fY) && bitStream.Read(vecPosition.fZ)) + CVector vecRotation; + + bitStream.Read(vecPosition.fX); + bitStream.Read(vecPosition.fY); + bitStream.Read(vecPosition.fZ); + + bitStream.Read(vecRotation.fX); + bitStream.Read(vecRotation.fY); + bitStream.Read(vecRotation.fZ); + + CLuaArguments Arguments; + Arguments.PushElement(pAttachedToEntity); + Arguments.PushNumber(vecPosition.fX); + Arguments.PushNumber(vecPosition.fY); + Arguments.PushNumber(vecPosition.fZ); + + Arguments.PushNumber(vecRotation.fX); + Arguments.PushNumber(vecRotation.fY); + Arguments.PushNumber(vecRotation.fZ); + + bool bContinue = pSource->CallEvent("onClientElementDetach", Arguments, true); + + if (bContinue) { - pSource->SetPosition(vecPosition); + pSource->SetSyncTimeContext(ucTimeContext); + pSource->AttachTo(NULL); + + if (vecPosition.fX != 0.0f || vecPosition.fY != 0.0f || vecPosition.fZ != 0.0f) + { + pSource->SetPosition(vecPosition); + } + + if (vecRotation.fX != 0.0f || vecRotation.fY != 0.0f || vecRotation.fZ != 0.0f) + { + pSource->SetRotationDegrees(vecRotation); + } } } } From c5d9a9cc2b36037eb776807768d75d22523ac275 Mon Sep 17 00:00:00 2001 From: Nico <122193236+Nico8340@users.noreply.github.com> Date: Wed, 3 Apr 2024 20:17:42 +0200 Subject: [PATCH 3/5] Modify returns --- .../mods/deathmatch/logic/CStaticFunctionDefinitions.cpp | 9 ++------- Client/mods/deathmatch/logic/rpc/CElementRPCs.cpp | 7 ++----- .../mods/deathmatch/logic/CStaticFunctionDefinitions.cpp | 3 +-- 3 files changed, 5 insertions(+), 14 deletions(-) diff --git a/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp b/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp index 99959cda70..237b75af0f 100644 --- a/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp +++ b/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp @@ -1375,11 +1375,9 @@ bool CStaticFunctionDefinitions::AttachElements(CClientEntity& Entity, CClientEn Arguments.PushNumber(vecRotation.fX); Arguments.PushNumber(vecRotation.fY); Arguments.PushNumber(vecRotation.fZ); - bool bContinue = Entity.CallEvent("onClientElementAttach", Arguments, true); - if (!bContinue) { + if (!Entity.CallEvent("onClientElementAttach", Arguments, true)) return false; - } ConvertDegreesToRadians(vecRotation); @@ -1415,12 +1413,9 @@ bool CStaticFunctionDefinitions::DetachElements(CClientEntity& Entity, CClientEn Arguments.PushNumber(vecRotation.fX); Arguments.PushNumber(vecRotation.fY); Arguments.PushNumber(vecRotation.fZ); - bool bContinue = Entity.CallEvent("onClientElementDetach", Arguments, true); - if (!bContinue) - { + if (!Entity.CallEvent("onClientElementDetach", Arguments, true)) return false; - } Entity.AttachTo(NULL); return true; diff --git a/Client/mods/deathmatch/logic/rpc/CElementRPCs.cpp b/Client/mods/deathmatch/logic/rpc/CElementRPCs.cpp index fad923f6fa..a73739535b 100644 --- a/Client/mods/deathmatch/logic/rpc/CElementRPCs.cpp +++ b/Client/mods/deathmatch/logic/rpc/CElementRPCs.cpp @@ -331,9 +331,8 @@ void CElementRPCs::AttachElements(CClientEntity* pSource, NetBitStreamInterface& Arguments.PushNumber(vecRotation.fX); Arguments.PushNumber(vecRotation.fY); Arguments.PushNumber(vecRotation.fZ); - bool bContinue = pSource->CallEvent("onClientElementAttach", Arguments, true); - if (bContinue) + if (pSource->CallEvent("onClientElementAttach", Arguments, true)) { ConvertDegreesToRadians(vecRotation); @@ -373,9 +372,7 @@ void CElementRPCs::DetachElements(CClientEntity* pSource, NetBitStreamInterface& Arguments.PushNumber(vecRotation.fY); Arguments.PushNumber(vecRotation.fZ); - bool bContinue = pSource->CallEvent("onClientElementDetach", Arguments, true); - - if (bContinue) + if (pSource->CallEvent("onClientElementDetach", Arguments, true)) { pSource->SetSyncTimeContext(ucTimeContext); pSource->AttachTo(NULL); diff --git a/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp b/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp index 6f0d043121..9b73c938f1 100644 --- a/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp +++ b/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp @@ -1577,9 +1577,8 @@ bool CStaticFunctionDefinitions::DetachElements(CElement* pElement, CElement* pA Arguments.PushNumber(vecRotation.fX); Arguments.PushNumber(vecRotation.fY); Arguments.PushNumber(vecRotation.fZ); - bool bContinue = pElement->CallEvent("onElementDetach", Arguments); - if (!bContinue) + if (!pElement->CallEvent("onElementDetach", Arguments)) { return false; } From 463a385d46807af993feb41d70e91c42676cc037 Mon Sep 17 00:00:00 2001 From: Nico <122193236+Nico8340@users.noreply.github.com> Date: Wed, 3 Apr 2024 21:10:35 +0200 Subject: [PATCH 4/5] Modify BitStream version --- Client/mods/deathmatch/logic/rpc/CElementRPCs.cpp | 15 ++++++++++++--- Shared/sdk/net/bitstream.h | 4 ++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Client/mods/deathmatch/logic/rpc/CElementRPCs.cpp b/Client/mods/deathmatch/logic/rpc/CElementRPCs.cpp index a73739535b..dbba00af26 100644 --- a/Client/mods/deathmatch/logic/rpc/CElementRPCs.cpp +++ b/Client/mods/deathmatch/logic/rpc/CElementRPCs.cpp @@ -358,9 +358,18 @@ void CElementRPCs::DetachElements(CClientEntity* pSource, NetBitStreamInterface& bitStream.Read(vecPosition.fY); bitStream.Read(vecPosition.fZ); - bitStream.Read(vecRotation.fX); - bitStream.Read(vecRotation.fY); - bitStream.Read(vecRotation.fZ); + if (g_pNet->CanServerBitStream((eBitStreamVersion::DetachElementsRotation))) + { + bitStream.Read(vecRotation.fX); + bitStream.Read(vecRotation.fY); + bitStream.Read(vecRotation.fZ); + } + else + { + vecRotation.fX = 0; + vecRotation.fY = 0; + vecRotation.fZ = 0; + } CLuaArguments Arguments; Arguments.PushElement(pAttachedToEntity); diff --git a/Shared/sdk/net/bitstream.h b/Shared/sdk/net/bitstream.h index 3924ebbf16..8620af6a3e 100644 --- a/Shared/sdk/net/bitstream.h +++ b/Shared/sdk/net/bitstream.h @@ -540,6 +540,10 @@ enum class eBitStreamVersion : unsigned short // 2023-10-12 CPlayerJoinCompletePacket_ServerName, + // Send rotation on detachElements + // 2024-04-12 + DetachElementsRotation, + // This allows us to automatically increment the BitStreamVersion when things are added to this enum. // Make sure you only add things above this comment. Next, From 1b85d9268f1efd35682a980d7de58784a50eddeb Mon Sep 17 00:00:00 2001 From: Nico <122193236+Nico8340@users.noreply.github.com> Date: Wed, 3 Apr 2024 21:51:06 +0200 Subject: [PATCH 5/5] Format & early returns --- .../logic/CStaticFunctionDefinitions.cpp | 81 +++++----- .../deathmatch/logic/rpc/CElementRPCs.cpp | 138 ++++++++++-------- .../logic/CStaticFunctionDefinitions.cpp | 130 ++++++++--------- 3 files changed, 178 insertions(+), 171 deletions(-) diff --git a/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp b/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp index 237b75af0f..d181a4422a 100644 --- a/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp +++ b/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp @@ -1364,30 +1364,30 @@ bool CStaticFunctionDefinitions::AttachElements(CClientEntity& Entity, CClientEn RUN_CHILDREN(AttachElements(**iter, AttachedToEntity, vecPosition, vecRotation)) // Can these elements be attached? - if (Entity.IsAttachToable() && AttachedToEntity.IsAttachable() && !AttachedToEntity.IsAttachedToElement(&Entity) && - Entity.GetDimension() == AttachedToEntity.GetDimension()) + if (!Entity.IsAttachToable() || !AttachedToEntity.IsAttachable() || AttachedToEntity.IsAttachedToElement(&Entity) || + Entity.GetDimension() != AttachedToEntity.GetDimension()) { - CLuaArguments Arguments; - Arguments.PushElement(&AttachedToEntity); - Arguments.PushNumber(vecPosition.fX); - Arguments.PushNumber(vecPosition.fY); - Arguments.PushNumber(vecPosition.fZ); - Arguments.PushNumber(vecRotation.fX); - Arguments.PushNumber(vecRotation.fY); - Arguments.PushNumber(vecRotation.fZ); - - if (!Entity.CallEvent("onClientElementAttach", Arguments, true)) - return false; + return false; + } - ConvertDegreesToRadians(vecRotation); + CLuaArguments Arguments; + Arguments.PushElement(&AttachedToEntity); + Arguments.PushNumber(vecPosition.fX); + Arguments.PushNumber(vecPosition.fY); + Arguments.PushNumber(vecPosition.fZ); + Arguments.PushNumber(vecRotation.fX); + Arguments.PushNumber(vecRotation.fY); + Arguments.PushNumber(vecRotation.fZ); + + if (!Entity.CallEvent("onClientElementAttach", Arguments, true)) + return false; - Entity.SetAttachedOffsets(vecPosition, vecRotation); - Entity.AttachTo(&AttachedToEntity); + ConvertDegreesToRadians(vecRotation); - return true; - } + Entity.SetAttachedOffsets(vecPosition, vecRotation); + Entity.AttachTo(&AttachedToEntity); - return false; + return true; } bool CStaticFunctionDefinitions::DetachElements(CClientEntity& Entity, CClientEntity* pAttachedToEntity) @@ -1395,34 +1395,33 @@ bool CStaticFunctionDefinitions::DetachElements(CClientEntity& Entity, CClientEn RUN_CHILDREN(DetachElements(**iter, pAttachedToEntity)) CClientEntity* pActualAttachedToEntity = Entity.GetAttachedTo(); - if (pActualAttachedToEntity) + if (!pActualAttachedToEntity || (pAttachedToEntity && pActualAttachedToEntity != pAttachedToEntity)) { - if (pAttachedToEntity == NULL || pActualAttachedToEntity == pAttachedToEntity) - { - CVector vecPosition; - CVector vecRotation; + return false; + } - Entity.GetPosition(vecPosition); - Entity.GetRotationDegrees(vecRotation); + CVector vecPosition; + CVector vecRotation; - CLuaArguments Arguments; - Arguments.PushElement(pActualAttachedToEntity); - Arguments.PushNumber(vecPosition.fX); - Arguments.PushNumber(vecPosition.fY); - Arguments.PushNumber(vecPosition.fZ); - Arguments.PushNumber(vecRotation.fX); - Arguments.PushNumber(vecRotation.fY); - Arguments.PushNumber(vecRotation.fZ); - - if (!Entity.CallEvent("onClientElementDetach", Arguments, true)) - return false; + Entity.GetPosition(vecPosition); + Entity.GetRotationDegrees(vecRotation); - Entity.AttachTo(NULL); - return true; - } + CLuaArguments Arguments; + Arguments.PushElement(pActualAttachedToEntity); + Arguments.PushNumber(vecPosition.fX); + Arguments.PushNumber(vecPosition.fY); + Arguments.PushNumber(vecPosition.fZ); + Arguments.PushNumber(vecRotation.fX); + Arguments.PushNumber(vecRotation.fY); + Arguments.PushNumber(vecRotation.fZ); + + if (!Entity.CallEvent("onClientElementDetach", Arguments, true)) + { + return false; } - return false; + Entity.AttachTo(NULL); + return true; } bool CStaticFunctionDefinitions::SetElementAttachedOffsets(CClientEntity& Entity, CVector& vecPosition, CVector& vecRotation) diff --git a/Client/mods/deathmatch/logic/rpc/CElementRPCs.cpp b/Client/mods/deathmatch/logic/rpc/CElementRPCs.cpp index dbba00af26..10ed920b98 100644 --- a/Client/mods/deathmatch/logic/rpc/CElementRPCs.cpp +++ b/Client/mods/deathmatch/logic/rpc/CElementRPCs.cpp @@ -314,88 +314,100 @@ void CElementRPCs::SetElementDimension(CClientEntity* pSource, NetBitStreamInter void CElementRPCs::AttachElements(CClientEntity* pSource, NetBitStreamInterface& bitStream) { ElementID usAttachedToID; - CVector vecPosition, vecRotation; - if (bitStream.Read(usAttachedToID) && bitStream.Read(vecPosition.fX) && bitStream.Read(vecPosition.fY) && bitStream.Read(vecPosition.fZ) && - bitStream.Read(vecRotation.fX) && bitStream.Read(vecRotation.fY) && bitStream.Read(vecRotation.fZ)) + + CVector vecPosition; + CVector vecRotation; + + if (!(bitStream.Read(usAttachedToID) && bitStream.Read(vecPosition.fX) && bitStream.Read(vecPosition.fY) && bitStream.Read(vecPosition.fZ) && + bitStream.Read(vecRotation.fX) && bitStream.Read(vecRotation.fY) && bitStream.Read(vecRotation.fZ))) { - CClientEntity* pAttachedToEntity = CElementIDs::GetElement(usAttachedToID); - if (pAttachedToEntity) - { - ConvertRadiansToDegrees(vecRotation); + return; + } - CLuaArguments Arguments; - Arguments.PushElement(pAttachedToEntity); - Arguments.PushNumber(vecPosition.fX); - Arguments.PushNumber(vecPosition.fY); - Arguments.PushNumber(vecPosition.fZ); - Arguments.PushNumber(vecRotation.fX); - Arguments.PushNumber(vecRotation.fY); - Arguments.PushNumber(vecRotation.fZ); + CClientEntity* pAttachedToEntity = CElementIDs::GetElement(usAttachedToID); + if (!pAttachedToEntity) + { + return; + } - if (pSource->CallEvent("onClientElementAttach", Arguments, true)) - { - ConvertDegreesToRadians(vecRotation); + ConvertRadiansToDegrees(vecRotation); - pSource->SetAttachedOffsets(vecPosition, vecRotation); - pSource->AttachTo(pAttachedToEntity); - } - } + CLuaArguments Arguments; + Arguments.PushElement(pAttachedToEntity); + Arguments.PushNumber(vecPosition.fX); + Arguments.PushNumber(vecPosition.fY); + Arguments.PushNumber(vecPosition.fZ); + Arguments.PushNumber(vecRotation.fX); + Arguments.PushNumber(vecRotation.fY); + Arguments.PushNumber(vecRotation.fZ); + + if (!pSource->CallEvent("onClientElementAttach", Arguments, true)) + { + return; } + + ConvertDegreesToRadians(vecRotation); + + pSource->SetAttachedOffsets(vecPosition, vecRotation); + pSource->AttachTo(pAttachedToEntity); } void CElementRPCs::DetachElements(CClientEntity* pSource, NetBitStreamInterface& bitStream) { unsigned char ucTimeContext; - if (bitStream.Read(ucTimeContext)) + if (!bitStream.Read(ucTimeContext)) { - ElementID usAttachedToID; - CClientEntity* pAttachedToEntity = CElementIDs::GetElement(usAttachedToID); + return; + } - CVector vecPosition; - CVector vecRotation; + ElementID usAttachedToID; + CClientEntity* pAttachedToEntity = CElementIDs::GetElement(usAttachedToID); - bitStream.Read(vecPosition.fX); - bitStream.Read(vecPosition.fY); - bitStream.Read(vecPosition.fZ); + CVector vecPosition; + CVector vecRotation; - if (g_pNet->CanServerBitStream((eBitStreamVersion::DetachElementsRotation))) - { - bitStream.Read(vecRotation.fX); - bitStream.Read(vecRotation.fY); - bitStream.Read(vecRotation.fZ); - } - else - { - vecRotation.fX = 0; - vecRotation.fY = 0; - vecRotation.fZ = 0; - } + bitStream.Read(vecPosition.fX); + bitStream.Read(vecPosition.fY); + bitStream.Read(vecPosition.fZ); - CLuaArguments Arguments; - Arguments.PushElement(pAttachedToEntity); - Arguments.PushNumber(vecPosition.fX); - Arguments.PushNumber(vecPosition.fY); - Arguments.PushNumber(vecPosition.fZ); + if (g_pNet->CanServerBitStream((eBitStreamVersion::DetachElementsRotation))) + { + bitStream.Read(vecRotation.fX); + bitStream.Read(vecRotation.fY); + bitStream.Read(vecRotation.fZ); + } + else + { + vecRotation.fX = 0; + vecRotation.fY = 0; + vecRotation.fZ = 0; + } - Arguments.PushNumber(vecRotation.fX); - Arguments.PushNumber(vecRotation.fY); - Arguments.PushNumber(vecRotation.fZ); + CLuaArguments Arguments; + Arguments.PushElement(pAttachedToEntity); + Arguments.PushNumber(vecPosition.fX); + Arguments.PushNumber(vecPosition.fY); + Arguments.PushNumber(vecPosition.fZ); + Arguments.PushNumber(vecRotation.fX); + Arguments.PushNumber(vecRotation.fY); + Arguments.PushNumber(vecRotation.fZ); - if (pSource->CallEvent("onClientElementDetach", Arguments, true)) - { - pSource->SetSyncTimeContext(ucTimeContext); - pSource->AttachTo(NULL); + if (!pSource->CallEvent("onClientElementDetach", Arguments, true)) + { + return; + } - if (vecPosition.fX != 0.0f || vecPosition.fY != 0.0f || vecPosition.fZ != 0.0f) - { - pSource->SetPosition(vecPosition); - } + pSource->SetSyncTimeContext(ucTimeContext); + pSource->AttachTo(NULL); - if (vecRotation.fX != 0.0f || vecRotation.fY != 0.0f || vecRotation.fZ != 0.0f) - { - pSource->SetRotationDegrees(vecRotation); - } - } + if (vecPosition.fX != 0.0f || vecPosition.fY != 0.0f || vecPosition.fZ != 0.0f) + { + pSource->SetPosition(vecPosition); + } + + if (vecRotation.fX != 0.0f || vecRotation.fY != 0.0f || vecRotation.fZ != 0.0f) + { + pSource->SetRotationDegrees(vecRotation); } } diff --git a/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp b/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp index 9b73c938f1..bb889ad82d 100644 --- a/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp +++ b/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp @@ -1515,43 +1515,42 @@ bool CStaticFunctionDefinitions::AttachElements(CElement* pElement, CElement* pA assert(pElement); assert(pAttachedToElement); - if (pElement->IsAttachToable() && pAttachedToElement->IsAttachable() && !pAttachedToElement->IsAttachedToElement(pElement) && - pElement->GetDimension() == pAttachedToElement->GetDimension()) + if (!pElement->IsAttachToable() || !pAttachedToElement->IsAttachable() || pAttachedToElement->IsAttachedToElement(pElement) || + pElement->GetDimension() != pAttachedToElement->GetDimension()) { - CLuaArguments Arguments; - Arguments.PushElement(pAttachedToElement); - Arguments.PushNumber(vecPosition.fX); - Arguments.PushNumber(vecPosition.fY); - Arguments.PushNumber(vecPosition.fZ); - Arguments.PushNumber(vecRotation.fX); - Arguments.PushNumber(vecRotation.fY); - Arguments.PushNumber(vecRotation.fZ); - bool bContinue = pElement->CallEvent("onElementAttach", Arguments); + return false; + } - if (!bContinue) - { - return false; - } + CLuaArguments Arguments; + Arguments.PushElement(pAttachedToElement); + Arguments.PushNumber(vecPosition.fX); + Arguments.PushNumber(vecPosition.fY); + Arguments.PushNumber(vecPosition.fZ); + Arguments.PushNumber(vecRotation.fX); + Arguments.PushNumber(vecRotation.fY); + Arguments.PushNumber(vecRotation.fZ); - ConvertDegreesToRadians(vecRotation); + if (!pElement->CallEvent("onElementAttach", Arguments)) + { + return false; + } - pElement->SetAttachedOffsets(vecPosition, vecRotation); - pElement->AttachTo(pAttachedToElement); + ConvertDegreesToRadians(vecRotation); - CBitStream BitStream; - BitStream.pBitStream->Write(pAttachedToElement->GetID()); - BitStream.pBitStream->Write(vecPosition.fX); - BitStream.pBitStream->Write(vecPosition.fY); - BitStream.pBitStream->Write(vecPosition.fZ); - BitStream.pBitStream->Write(vecRotation.fX); - BitStream.pBitStream->Write(vecRotation.fY); - BitStream.pBitStream->Write(vecRotation.fZ); - m_pPlayerManager->BroadcastOnlyJoined(CElementRPCPacket(pElement, ATTACH_ELEMENTS, *BitStream.pBitStream)); + pElement->SetAttachedOffsets(vecPosition, vecRotation); + pElement->AttachTo(pAttachedToElement); - return true; - } + CBitStream BitStream; + BitStream.pBitStream->Write(pAttachedToElement->GetID()); + BitStream.pBitStream->Write(vecPosition.fX); + BitStream.pBitStream->Write(vecPosition.fY); + BitStream.pBitStream->Write(vecPosition.fZ); + BitStream.pBitStream->Write(vecRotation.fX); + BitStream.pBitStream->Write(vecRotation.fY); + BitStream.pBitStream->Write(vecRotation.fZ); + m_pPlayerManager->BroadcastOnlyJoined(CElementRPCPacket(pElement, ATTACH_ELEMENTS, *BitStream.pBitStream)); - return false; + return true; } bool CStaticFunctionDefinitions::DetachElements(CElement* pElement, CElement* pAttachedToElement) @@ -1559,50 +1558,47 @@ bool CStaticFunctionDefinitions::DetachElements(CElement* pElement, CElement* pA assert(pElement); CElement* pActualAttachedToElement = pElement->GetAttachedToElement(); - if (pActualAttachedToElement) + if (!pActualAttachedToElement || (pAttachedToElement && pActualAttachedToElement != pAttachedToElement)) { - if (pAttachedToElement == NULL || pActualAttachedToElement == pAttachedToElement) - { - CVector vecPosition = pElement->GetPosition(); - CVector vecRotation; - - pElement->GetRotation(vecRotation); - ConvertRadiansToDegrees(vecRotation); - - CLuaArguments Arguments; - Arguments.PushElement(pActualAttachedToElement); - Arguments.PushNumber(vecPosition.fX); - Arguments.PushNumber(vecPosition.fY); - Arguments.PushNumber(vecPosition.fZ); - Arguments.PushNumber(vecRotation.fX); - Arguments.PushNumber(vecRotation.fY); - Arguments.PushNumber(vecRotation.fZ); + return false; + } - if (!pElement->CallEvent("onElementDetach", Arguments)) - { - return false; - } + CVector vecPosition = pElement->GetPosition(); + CVector vecRotation; - // Detach it. Also generate a new time context to prevent sync screwup from - // old packes arriving. - pElement->AttachTo(NULL); - pElement->GenerateSyncTimeContext(); + pElement->GetRotation(vecRotation); + ConvertRadiansToDegrees(vecRotation); - CBitStream BitStream; - BitStream.pBitStream->Write(pElement->GetSyncTimeContext()); - BitStream.pBitStream->Write(vecPosition.fX); - BitStream.pBitStream->Write(vecPosition.fY); - BitStream.pBitStream->Write(vecPosition.fZ); - BitStream.pBitStream->Write(vecRotation.fX); - BitStream.pBitStream->Write(vecRotation.fY); - BitStream.pBitStream->Write(vecRotation.fZ); - m_pPlayerManager->BroadcastOnlyJoined(CElementRPCPacket(pElement, DETACH_ELEMENTS, *BitStream.pBitStream)); + CLuaArguments Arguments; + Arguments.PushElement(pActualAttachedToElement); + Arguments.PushNumber(vecPosition.fX); + Arguments.PushNumber(vecPosition.fY); + Arguments.PushNumber(vecPosition.fZ); + Arguments.PushNumber(vecRotation.fX); + Arguments.PushNumber(vecRotation.fY); + Arguments.PushNumber(vecRotation.fZ); - return true; - } + if (!pElement->CallEvent("onElementDetach", Arguments)) + { + return false; } - return false; + // Detach it. Also generate a new time context to prevent sync screwup from + // old packets arriving. + pElement->AttachTo(NULL); + pElement->GenerateSyncTimeContext(); + + CBitStream BitStream; + BitStream.pBitStream->Write(pElement->GetSyncTimeContext()); + BitStream.pBitStream->Write(vecPosition.fX); + BitStream.pBitStream->Write(vecPosition.fY); + BitStream.pBitStream->Write(vecPosition.fZ); + BitStream.pBitStream->Write(vecRotation.fX); + BitStream.pBitStream->Write(vecRotation.fY); + BitStream.pBitStream->Write(vecRotation.fZ); + m_pPlayerManager->BroadcastOnlyJoined(CElementRPCPacket(pElement, DETACH_ELEMENTS, *BitStream.pBitStream)); + + return true; } bool CStaticFunctionDefinitions::SetElementAlpha(CElement* pElement, unsigned char ucAlpha)