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 "gamecontext.h"
|
2008-08-14 18:42:47 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////
|
|
|
|
// Event handler
|
|
|
|
//////////////////////////////////////////////////
|
2010-05-29 07:25:38 +00:00
|
|
|
CEventHandler::CEventHandler()
|
2008-08-14 18:42:47 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
m_pGameServer = 0;
|
|
|
|
Clear();
|
2008-08-14 18:42:47 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CEventHandler::SetGameServer(CGameContext *pGameServer)
|
2008-08-14 18:42:47 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
m_pGameServer = pGameServer;
|
|
|
|
}
|
|
|
|
|
2020-07-08 14:59:32 +00:00
|
|
|
void *CEventHandler::Create(int Type, int Size, int64 Mask)
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
|
|
|
if(m_NumEvents == MAX_EVENTS)
|
2008-08-14 18:42:47 +00:00
|
|
|
return 0;
|
2020-09-26 19:41:58 +00:00
|
|
|
if(m_CurrentOffset + Size >= MAX_DATASIZE)
|
2008-08-14 18:42:47 +00:00
|
|
|
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++;
|
2008-08-14 18:42:47 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CEventHandler::Clear()
|
2008-08-14 18:42:47 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
m_NumEvents = 0;
|
|
|
|
m_CurrentOffset = 0;
|
2008-08-14 18:42:47 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CEventHandler::Snap(int SnappingClient)
|
2008-08-14 18:42:47 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
for(int i = 0; i < m_NumEvents; i++)
|
2008-08-14 18:42:47 +00:00
|
|
|
{
|
2011-12-31 10:06:34 +00:00
|
|
|
if(SnappingClient == -1 || CmaskIsSet(m_aClientMasks[i], SnappingClient))
|
2008-08-14 18:42:47 +00:00
|
|
|
{
|
2011-06-01 17:43:48 +00:00
|
|
|
CNetEvent_Common *ev = (CNetEvent_Common *)&m_aData[m_aOffsets[i]];
|
2011-12-31 10:06:34 +00:00
|
|
|
if(SnappingClient == -1 || distance(GameServer()->m_apPlayers[SnappingClient]->m_ViewPos, vec2(ev->m_X, ev->m_Y)) < 1500.0f)
|
2008-08-14 18:42:47 +00:00
|
|
|
{
|
2020-06-10 20:27:23 +00:00
|
|
|
int Type = m_aTypes[i];
|
|
|
|
int Size = m_aSizes[i];
|
|
|
|
const char *Data = &m_aData[m_aOffsets[i]];
|
|
|
|
if(GameServer()->Server()->IsSixup(SnappingClient))
|
|
|
|
EventToSixup(&Type, &Size, &Data);
|
|
|
|
|
|
|
|
void *d = GameServer()->Server()->SnapNewItem(Type, i, Size);
|
2010-12-16 02:29:08 +00:00
|
|
|
if(d)
|
2020-06-10 20:27:23 +00:00
|
|
|
mem_copy(d, Data, Size);
|
2008-08-14 18:42:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-10 20:27:23 +00:00
|
|
|
|
|
|
|
void CEventHandler::EventToSixup(int *Type, int *Size, const char **pData)
|
|
|
|
{
|
|
|
|
static char s_aEventStore[128];
|
|
|
|
if(*Type == NETEVENTTYPE_DAMAGEIND)
|
|
|
|
{
|
|
|
|
const CNetEvent_DamageInd *pEvent = (const CNetEvent_DamageInd *)(*pData);
|
|
|
|
protocol7::CNetEvent_Damage *pEvent7 = (protocol7::CNetEvent_Damage *)s_aEventStore;
|
2020-06-12 19:22:54 +00:00
|
|
|
*Type = -protocol7::NETEVENTTYPE_DAMAGE;
|
2020-06-10 20:27:23 +00:00
|
|
|
*Size = sizeof(*pEvent7);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2020-06-14 10:19:36 +00:00
|
|
|
*pData = s_aEventStore;
|
|
|
|
}
|
|
|
|
else if(*Type == NETEVENTTYPE_SOUNDGLOBAL) // No more global sounds for the server
|
|
|
|
{
|
|
|
|
const CNetEvent_SoundGlobal *pEvent = (const CNetEvent_SoundGlobal *)(*pData);
|
|
|
|
protocol7::CNetEvent_SoundWorld *pEvent7 = (protocol7::CNetEvent_SoundWorld *)s_aEventStore;
|
|
|
|
|
|
|
|
*Type = -protocol7::NETEVENTTYPE_SOUNDWORLD;
|
|
|
|
*Size = sizeof(*pEvent7);
|
|
|
|
|
|
|
|
pEvent7->m_SoundID = pEvent->m_SoundID;
|
|
|
|
pEvent7->m_X = pEvent->m_X;
|
|
|
|
pEvent7->m_Y = pEvent->m_Y;
|
|
|
|
|
2020-06-10 20:27:23 +00:00
|
|
|
*pData = s_aEventStore;
|
|
|
|
}
|
|
|
|
}
|