Skip to content

Attach events & rotation fix #3353

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

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions Client/mods/deathmatch/logic/CClientGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
35 changes: 35 additions & 0 deletions Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down
62 changes: 56 additions & 6 deletions Client/mods/deathmatch/logic/rpc/CElementRPCs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
Expand All @@ -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);
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions Server/mods/deathmatch/logic/CGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
43 changes: 41 additions & 2 deletions Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand All @@ -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;
Expand Down