ddnet/src/game/server/eventhandler.cpp

106 lines
2.9 KiB
C++
Raw Normal View History

2010-11-20 10:37:14 +00:00
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
/* If you are missing that file, acquire a complete release at teeworlds.com. */
2010-05-29 07:25:38 +00:00
#include "eventhandler.h"
#include "entity.h"
2010-05-29 07:25:38 +00:00
#include "gamecontext.h"
2022-05-29 15:31:40 +00:00
#include <base/system.h>
#include <base/vmath.h>
//////////////////////////////////////////////////
// Event handler
//////////////////////////////////////////////////
2010-05-29 07:25:38 +00:00
CEventHandler::CEventHandler()
{
2010-05-29 07:25:38 +00:00
m_pGameServer = 0;
Clear();
}
2010-05-29 07:25:38 +00:00
void CEventHandler::SetGameServer(CGameContext *pGameServer)
{
2010-05-29 07:25:38 +00:00
m_pGameServer = pGameServer;
}
2021-06-23 05:05:49 +00:00
void *CEventHandler::Create(int Type, int Size, int64_t Mask)
2010-05-29 07:25:38 +00:00
{
if(m_NumEvents == MAX_EVENTS)
return 0;
if(m_CurrentOffset + Size >= MAX_DATASIZE)
return 0;
2010-05-29 07:25:38 +00:00
void *p = &m_aData[m_CurrentOffset];
m_aOffsets[m_NumEvents] = m_CurrentOffset;
m_aTypes[m_NumEvents] = Type;
m_aSizes[m_NumEvents] = Size;
m_aClientMasks[m_NumEvents] = Mask;
m_CurrentOffset += Size;
m_NumEvents++;
return p;
}
2010-05-29 07:25:38 +00:00
void CEventHandler::Clear()
{
2010-05-29 07:25:38 +00:00
m_NumEvents = 0;
m_CurrentOffset = 0;
}
2010-05-29 07:25:38 +00:00
void CEventHandler::Snap(int SnappingClient)
{
2010-05-29 07:25:38 +00:00
for(int i = 0; i < m_NumEvents; i++)
{
if(SnappingClient == SERVER_DEMO_CLIENT || CmaskIsSet(m_aClientMasks[i], SnappingClient))
{
CNetEvent_Common *pEvent = (CNetEvent_Common *)&m_aData[m_aOffsets[i]];
if(!NetworkClipped(GameServer(), SnappingClient, vec2(pEvent->m_X, pEvent->m_Y)))
{
2020-06-10 20:27:23 +00:00
int Type = m_aTypes[i];
int Size = m_aSizes[i];
const char *pData = &m_aData[m_aOffsets[i]];
2020-06-10 20:27:23 +00:00
if(GameServer()->Server()->IsSixup(SnappingClient))
EventToSixup(&Type, &Size, &pData);
2020-06-10 20:27:23 +00:00
void *pItem = GameServer()->Server()->SnapNewItem(Type, i, Size);
if(pItem)
mem_copy(pItem, pData, Size);
}
}
}
}
2020-06-10 20:27:23 +00:00
void CEventHandler::EventToSixup(int *pType, int *pSize, const char **ppData)
2020-06-10 20:27:23 +00:00
{
static char s_aEventStore[128];
if(*pType == NETEVENTTYPE_DAMAGEIND)
2020-06-10 20:27:23 +00:00
{
const CNetEvent_DamageInd *pEvent = (const CNetEvent_DamageInd *)(*ppData);
2020-06-10 20:27:23 +00:00
protocol7::CNetEvent_Damage *pEvent7 = (protocol7::CNetEvent_Damage *)s_aEventStore;
*pType = -protocol7::NETEVENTTYPE_DAMAGE;
*pSize = sizeof(*pEvent7);
2020-06-10 20:27:23 +00:00
pEvent7->m_X = pEvent->m_X;
pEvent7->m_Y = pEvent->m_Y;
// This will need some work, perhaps an event wrapper for damageind,
// a scan of the event array to merge multiple damageinds
// or a separate array of "damage ind" events that's added in while snapping
pEvent7->m_HealthAmount = 1;
*ppData = s_aEventStore;
2020-06-14 10:19:36 +00:00
}
else if(*pType == NETEVENTTYPE_SOUNDGLOBAL) // No more global sounds for the server
2020-06-14 10:19:36 +00:00
{
const CNetEvent_SoundGlobal *pEvent = (const CNetEvent_SoundGlobal *)(*ppData);
2020-06-14 10:19:36 +00:00
protocol7::CNetEvent_SoundWorld *pEvent7 = (protocol7::CNetEvent_SoundWorld *)s_aEventStore;
*pType = -protocol7::NETEVENTTYPE_SOUNDWORLD;
*pSize = sizeof(*pEvent7);
2020-06-14 10:19:36 +00:00
pEvent7->m_SoundID = pEvent->m_SoundID;
pEvent7->m_X = pEvent->m_X;
pEvent7->m_Y = pEvent->m_Y;
*ppData = s_aEventStore;
2020-06-10 20:27:23 +00:00
}
}