ddnet/src/game/server/gameworld.cpp

402 lines
9.7 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 "gameworld.h"
#include "entities/character.h"
2010-05-29 07:25:38 +00:00
#include "entity.h"
#include "gamecontext.h"
#include "player.h"
2013-12-31 05:13:57 +00:00
#include <algorithm>
#include <engine/shared/config.h>
#include <utility>
//////////////////////////////////////////////////
// game world
//////////////////////////////////////////////////
2010-05-29 07:25:38 +00:00
CGameWorld::CGameWorld()
{
2010-05-29 07:25:38 +00:00
m_pGameServer = 0x0;
m_pConfig = 0x0;
2010-05-29 07:25:38 +00:00
m_pServer = 0x0;
2010-05-29 07:25:38 +00:00
m_Paused = false;
m_ResetRequested = false;
2020-10-26 14:14:07 +00:00
for(auto &pFirstEntityType : m_apFirstEntityTypes)
pFirstEntityType = 0;
}
2010-05-29 07:25:38 +00:00
CGameWorld::~CGameWorld()
{
// delete all entities
2020-10-26 14:14:07 +00:00
for(auto &pFirstEntityType : m_apFirstEntityTypes)
while(pFirstEntityType)
delete pFirstEntityType;
2010-05-29 07:25:38 +00:00
}
void CGameWorld::SetGameServer(CGameContext *pGameServer)
{
m_pGameServer = pGameServer;
m_pConfig = m_pGameServer->Config();
2010-05-29 07:25:38 +00:00
m_pServer = m_pGameServer->Server();
}
2010-05-29 07:25:38 +00:00
CEntity *CGameWorld::FindFirst(int Type)
{
return Type < 0 || Type >= NUM_ENTTYPES ? 0 : m_apFirstEntityTypes[Type];
}
2010-05-29 07:25:38 +00:00
int CGameWorld::FindEntities(vec2 Pos, float Radius, CEntity **ppEnts, int Max, int Type)
{
if(Type < 0 || Type >= NUM_ENTTYPES)
return 0;
2010-05-29 07:25:38 +00:00
int Num = 0;
for(CEntity *pEnt = m_apFirstEntityTypes[Type]; pEnt; pEnt = pEnt->m_pNextTypeEntity)
{
if(distance(pEnt->m_Pos, Pos) < Radius + pEnt->m_ProximityRadius)
{
if(ppEnts)
ppEnts[Num] = pEnt;
2010-05-29 07:25:38 +00:00
Num++;
if(Num == Max)
break;
}
}
2010-05-29 07:25:38 +00:00
return Num;
}
2010-05-29 07:25:38 +00:00
void CGameWorld::InsertEntity(CEntity *pEnt)
{
#ifdef CONF_DEBUG
2011-01-20 00:35:21 +00:00
for(CEntity *pCur = m_apFirstEntityTypes[pEnt->m_ObjType]; pCur; pCur = pCur->m_pNextTypeEntity)
2010-05-29 07:25:38 +00:00
dbg_assert(pCur != pEnt, "err");
#endif
// insert it
if(m_apFirstEntityTypes[pEnt->m_ObjType])
m_apFirstEntityTypes[pEnt->m_ObjType]->m_pPrevTypeEntity = pEnt;
pEnt->m_pNextTypeEntity = m_apFirstEntityTypes[pEnt->m_ObjType];
2010-05-29 07:25:38 +00:00
pEnt->m_pPrevTypeEntity = 0x0;
m_apFirstEntityTypes[pEnt->m_ObjType] = pEnt;
}
2010-05-29 07:25:38 +00:00
void CGameWorld::RemoveEntity(CEntity *pEnt)
{
// not in the list
if(!pEnt->m_pNextTypeEntity && !pEnt->m_pPrevTypeEntity && m_apFirstEntityTypes[pEnt->m_ObjType] != pEnt)
return;
// remove
2010-05-29 07:25:38 +00:00
if(pEnt->m_pPrevTypeEntity)
pEnt->m_pPrevTypeEntity->m_pNextTypeEntity = pEnt->m_pNextTypeEntity;
else
m_apFirstEntityTypes[pEnt->m_ObjType] = pEnt->m_pNextTypeEntity;
2010-05-29 07:25:38 +00:00
if(pEnt->m_pNextTypeEntity)
pEnt->m_pNextTypeEntity->m_pPrevTypeEntity = pEnt->m_pPrevTypeEntity;
// keep list traversing valid
if(m_pNextTraverseEntity == pEnt)
m_pNextTraverseEntity = pEnt->m_pNextTypeEntity;
2010-05-29 07:25:38 +00:00
pEnt->m_pNextTypeEntity = 0;
pEnt->m_pPrevTypeEntity = 0;
}
//
2010-05-29 07:25:38 +00:00
void CGameWorld::Snap(int SnappingClient)
{
for(auto *pEnt : m_apFirstEntityTypes)
for(; pEnt;)
{
m_pNextTraverseEntity = pEnt->m_pNextTypeEntity;
pEnt->Snap(SnappingClient);
pEnt = m_pNextTraverseEntity;
}
}
2010-05-29 07:25:38 +00:00
void CGameWorld::Reset()
{
// reset all entities
for(auto *pEnt : m_apFirstEntityTypes)
for(; pEnt;)
{
m_pNextTraverseEntity = pEnt->m_pNextTypeEntity;
pEnt->Reset();
pEnt = m_pNextTraverseEntity;
}
2010-05-29 07:25:38 +00:00
RemoveEntities();
GameServer()->m_pController->OnReset();
2010-05-29 07:25:38 +00:00
RemoveEntities();
2010-05-29 07:25:38 +00:00
m_ResetRequested = false;
}
2010-05-29 07:25:38 +00:00
void CGameWorld::RemoveEntities()
{
// destroy objects marked for destruction
for(auto *pEnt : m_apFirstEntityTypes)
for(; pEnt;)
{
m_pNextTraverseEntity = pEnt->m_pNextTypeEntity;
if(pEnt->m_MarkedForDestroy)
{
RemoveEntity(pEnt);
pEnt->Destroy();
}
pEnt = m_pNextTraverseEntity;
}
}
bool distCompare(std::pair<float, int> a, std::pair<float, int> b)
2013-12-31 05:13:57 +00:00
{
return (a.first < b.first);
}
void CGameWorld::UpdatePlayerMaps()
{
if(Server()->Tick() % g_Config.m_SvMapUpdateRate != 0)
return;
2013-12-31 05:13:57 +00:00
std::pair<float, int> Dist[MAX_CLIENTS];
for(int i = 0; i < MAX_CLIENTS; i++)
2013-12-31 05:13:57 +00:00
{
if(!Server()->ClientIngame(i))
continue;
2017-03-21 10:24:44 +00:00
int *pMap = Server()->GetIdMap(i);
2013-12-31 05:13:57 +00:00
// compute distances
for(int j = 0; j < MAX_CLIENTS; j++)
2013-12-31 05:13:57 +00:00
{
2017-03-21 10:24:44 +00:00
Dist[j].second = j;
if(!Server()->ClientIngame(j) || !GameServer()->m_apPlayers[j])
2014-01-02 01:47:06 +00:00
{
2017-03-21 10:24:44 +00:00
Dist[j].first = 1e10;
2013-12-31 05:13:57 +00:00
continue;
2014-01-02 01:47:06 +00:00
}
CCharacter *ch = GameServer()->m_apPlayers[j]->GetCharacter();
if(!ch)
2014-01-02 01:47:06 +00:00
{
2017-03-21 10:24:44 +00:00
Dist[j].first = 1e9;
2013-12-31 05:13:57 +00:00
continue;
2014-01-02 01:47:06 +00:00
}
2013-12-31 05:13:57 +00:00
// copypasted chunk from character.cpp Snap() follows
CCharacter *SnapChar = GameServer()->GetPlayerChar(i);
2013-12-31 05:13:57 +00:00
if(SnapChar && !SnapChar->m_Super &&
2017-04-08 22:20:41 +00:00
!GameServer()->m_apPlayers[i]->IsPaused() && GameServer()->m_apPlayers[i]->GetTeam() != -1 &&
!ch->CanCollide(i) &&
(!GameServer()->m_apPlayers[i] ||
GameServer()->m_apPlayers[i]->GetClientVersion() == VERSION_VANILLA ||
(GameServer()->m_apPlayers[i]->GetClientVersion() >= VERSION_DDRACE &&
2020-09-21 13:31:55 +00:00
(GameServer()->m_apPlayers[i]->m_ShowOthers == 0 ||
(GameServer()->m_apPlayers[i]->m_ShowOthers == 2 && !GameServer()->m_apPlayers[i]->GetCharacter()->SameTeam(j))))))
2017-03-21 10:24:44 +00:00
Dist[j].first = 1e8;
2014-01-11 22:38:13 +00:00
else
2017-03-21 10:24:44 +00:00
Dist[j].first = 0;
2013-12-31 05:13:57 +00:00
2017-03-21 10:24:44 +00:00
Dist[j].first += distance(GameServer()->m_apPlayers[i]->m_ViewPos, GameServer()->m_apPlayers[j]->GetCharacter()->m_Pos);
2013-12-31 05:13:57 +00:00
}
// always send the player himself
2017-03-21 10:24:44 +00:00
Dist[i].first = 0;
2013-12-31 05:13:57 +00:00
// compute reverse map
int rMap[MAX_CLIENTS];
for(int &j : rMap)
2013-12-31 05:13:57 +00:00
{
j = -1;
2013-12-31 05:13:57 +00:00
}
for(int j = 0; j < VANILLA_MAX_CLIENTS; j++)
2013-12-31 05:13:57 +00:00
{
if(pMap[j] == -1)
continue;
if(Dist[pMap[j]].first > 5e9)
pMap[j] = -1;
else
rMap[pMap[j]] = j;
2013-12-31 05:13:57 +00:00
}
2017-03-21 10:24:44 +00:00
std::nth_element(&Dist[0], &Dist[VANILLA_MAX_CLIENTS - 1], &Dist[MAX_CLIENTS], distCompare);
2013-12-31 05:13:57 +00:00
2017-03-21 10:24:44 +00:00
int Mapc = 0;
int Demand = 0;
for(int j = 0; j < VANILLA_MAX_CLIENTS - 1; j++)
2013-12-31 05:13:57 +00:00
{
2017-03-21 10:24:44 +00:00
int k = Dist[j].second;
if(rMap[k] != -1 || Dist[j].first > 5e9)
continue;
while(Mapc < VANILLA_MAX_CLIENTS && pMap[Mapc] != -1)
Mapc++;
if(Mapc < VANILLA_MAX_CLIENTS - 1)
2017-03-21 10:24:44 +00:00
pMap[Mapc] = k;
2014-01-02 01:24:49 +00:00
else
2017-03-21 10:24:44 +00:00
Demand++;
2013-12-31 05:13:57 +00:00
}
for(int j = MAX_CLIENTS - 1; j > VANILLA_MAX_CLIENTS - 2; j--)
2013-12-31 05:13:57 +00:00
{
2017-03-21 10:24:44 +00:00
int k = Dist[j].second;
if(rMap[k] != -1 && Demand-- > 0)
2017-03-21 10:24:44 +00:00
pMap[rMap[k]] = -1;
2013-12-31 05:13:57 +00:00
}
2017-03-21 10:24:44 +00:00
pMap[VANILLA_MAX_CLIENTS - 1] = -1; // player with empty name to say chat msgs
2013-12-31 05:13:57 +00:00
}
}
2010-05-29 07:25:38 +00:00
void CGameWorld::Tick()
{
2010-05-29 07:25:38 +00:00
if(m_ResetRequested)
Reset();
2010-05-29 07:25:38 +00:00
if(!m_Paused)
{
2010-05-29 07:25:38 +00:00
if(GameServer()->m_pController->IsForceBalanced())
GameServer()->SendChat(-1, CGameContext::CHAT_ALL, "Teams have been balanced");
// update all objects
for(auto *pEnt : m_apFirstEntityTypes)
for(; pEnt;)
{
m_pNextTraverseEntity = pEnt->m_pNextTypeEntity;
pEnt->Tick();
pEnt = m_pNextTraverseEntity;
}
for(auto *pEnt : m_apFirstEntityTypes)
for(; pEnt;)
{
m_pNextTraverseEntity = pEnt->m_pNextTypeEntity;
pEnt->TickDefered();
pEnt = m_pNextTraverseEntity;
}
}
2012-01-09 23:49:31 +00:00
else
{
// update all objects
for(auto *pEnt : m_apFirstEntityTypes)
for(; pEnt;)
2012-01-09 23:49:31 +00:00
{
m_pNextTraverseEntity = pEnt->m_pNextTypeEntity;
pEnt->TickPaused();
pEnt = m_pNextTraverseEntity;
}
}
2010-05-29 07:25:38 +00:00
RemoveEntities();
2013-12-31 05:13:57 +00:00
UpdatePlayerMaps();
2019-05-01 22:34:20 +00:00
// find the characters' strong/weak id
int StrongWeakID = 0;
for(CCharacter *pChar = (CCharacter *)FindFirst(ENTTYPE_CHARACTER); pChar; pChar = (CCharacter *)pChar->TypeNext())
2019-05-01 22:34:20 +00:00
{
pChar->m_StrongWeakID = StrongWeakID;
StrongWeakID++;
}
2013-12-31 05:13:57 +00:00
}
// TODO: should be more general
//CCharacter *CGameWorld::IntersectCharacter(vec2 Pos0, vec2 Pos1, float Radius, vec2& NewPos, CEntity *pNotThis)
CCharacter *CGameWorld::IntersectCharacter(vec2 Pos0, vec2 Pos1, float Radius, vec2 &NewPos, CCharacter *pNotThis, int CollideWith, class CCharacter *pThisOnly)
{
// Find other players
float ClosestLen = distance(Pos0, Pos1) * 100.0f;
2010-05-29 07:25:38 +00:00
CCharacter *pClosest = 0;
CCharacter *p = (CCharacter *)FindFirst(ENTTYPE_CHARACTER);
2010-05-29 07:25:38 +00:00
for(; p; p = (CCharacter *)p->TypeNext())
2015-07-09 00:08:14 +00:00
{
2010-05-29 07:25:38 +00:00
if(p == pNotThis)
continue;
if(pThisOnly && p != pThisOnly)
continue;
if(CollideWith != -1 && !p->CanCollide(CollideWith))
continue;
vec2 IntersectPos;
if(closest_point_on_line(Pos0, Pos1, p->m_Pos, IntersectPos))
{
float Len = distance(p->m_Pos, IntersectPos);
if(Len < p->m_ProximityRadius + Radius)
{
Len = distance(Pos0, IntersectPos);
if(Len < ClosestLen)
{
NewPos = IntersectPos;
ClosestLen = Len;
pClosest = p;
}
}
}
}
2010-05-29 07:25:38 +00:00
return pClosest;
}
2010-05-29 07:25:38 +00:00
CCharacter *CGameWorld::ClosestCharacter(vec2 Pos, float Radius, CEntity *pNotThis)
{
// Find other players
float ClosestRange = Radius * 2;
2010-05-29 07:25:38 +00:00
CCharacter *pClosest = 0;
CCharacter *p = (CCharacter *)GameServer()->m_World.FindFirst(ENTTYPE_CHARACTER);
2010-05-29 07:25:38 +00:00
for(; p; p = (CCharacter *)p->TypeNext())
2015-07-09 00:08:14 +00:00
{
2010-05-29 07:25:38 +00:00
if(p == pNotThis)
continue;
2010-05-29 07:25:38 +00:00
float Len = distance(Pos, p->m_Pos);
if(Len < p->m_ProximityRadius + Radius)
{
2010-05-29 07:25:38 +00:00
if(Len < ClosestRange)
{
2010-05-29 07:25:38 +00:00
ClosestRange = Len;
pClosest = p;
}
}
}
2010-05-29 07:25:38 +00:00
return pClosest;
}
std::list<class CCharacter *> CGameWorld::IntersectedCharacters(vec2 Pos0, vec2 Pos1, float Radius, class CEntity *pNotThis)
{
std::list<CCharacter *> listOfChars;
2011-01-29 00:59:50 +00:00
CCharacter *pChr = (CCharacter *)FindFirst(CGameWorld::ENTTYPE_CHARACTER);
for(; pChr; pChr = (CCharacter *)pChr->TypeNext())
2015-07-09 00:08:14 +00:00
{
2011-01-29 00:59:50 +00:00
if(pChr == pNotThis)
continue;
vec2 IntersectPos;
if(closest_point_on_line(Pos0, Pos1, pChr->m_Pos, IntersectPos))
{
float Len = distance(pChr->m_Pos, IntersectPos);
if(Len < pChr->m_ProximityRadius + Radius)
{
pChr->m_Intersection = IntersectPos;
listOfChars.push_back(pChr);
}
}
}
return listOfChars;
}
2010-09-11 09:42:35 +00:00
void CGameWorld::ReleaseHooked(int ClientID)
{
2011-01-29 00:59:50 +00:00
CCharacter *pChr = (CCharacter *)CGameWorld::FindFirst(CGameWorld::ENTTYPE_CHARACTER);
for(; pChr; pChr = (CCharacter *)pChr->TypeNext())
{
CCharacterCore *Core = pChr->Core();
if(Core->m_HookedPlayer == ClientID && !pChr->m_Super)
2011-01-29 00:59:50 +00:00
{
Core->m_HookedPlayer = -1;
Core->m_HookState = HOOK_RETRACTED;
Core->m_TriggeredEvents |= COREEVENT_HOOK_RETRACT;
Core->m_HookState = HOOK_RETRACTED;
2011-01-29 00:59:50 +00:00
}
}
}