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. */
|
2008-08-14 18:42:47 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
#include "gameworld.h"
|
|
|
|
#include "entity.h"
|
|
|
|
#include "gamecontext.h"
|
2008-08-14 18:42:47 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////
|
|
|
|
// game world
|
|
|
|
//////////////////////////////////////////////////
|
2010-05-29 07:25:38 +00:00
|
|
|
CGameWorld::CGameWorld()
|
2008-08-14 18:42:47 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
m_pGameServer = 0x0;
|
|
|
|
m_pServer = 0x0;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
m_Paused = false;
|
|
|
|
m_ResetRequested = false;
|
2011-01-19 17:27:50 +00:00
|
|
|
for(int i = 0; i < NUM_ENTTYPES; i++)
|
2010-05-29 07:25:38 +00:00
|
|
|
m_apFirstEntityTypes[i] = 0;
|
2008-08-14 18:42:47 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
CGameWorld::~CGameWorld()
|
2008-08-14 18:42:47 +00:00
|
|
|
{
|
|
|
|
// delete all entities
|
2011-01-19 17:27:50 +00:00
|
|
|
for(int i = 0; i < NUM_ENTTYPES; i++)
|
|
|
|
while(m_apFirstEntityTypes[i])
|
|
|
|
delete m_apFirstEntityTypes[i];
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CGameWorld::SetGameServer(CGameContext *pGameServer)
|
|
|
|
{
|
|
|
|
m_pGameServer = pGameServer;
|
|
|
|
m_pServer = m_pGameServer->Server();
|
2008-08-14 18:42:47 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
CEntity *CGameWorld::FindFirst(int Type)
|
2008-08-14 18:42:47 +00:00
|
|
|
{
|
2011-01-19 17:27:50 +00:00
|
|
|
return Type < 0 || Type >= NUM_ENTTYPES ? 0 : m_apFirstEntityTypes[Type];
|
2008-08-14 18:42:47 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
int CGameWorld::FindEntities(vec2 Pos, float Radius, CEntity **ppEnts, int Max, int Type)
|
2008-08-14 18:42:47 +00:00
|
|
|
{
|
2011-01-19 17:27:50 +00:00
|
|
|
if(Type < 0 || Type >= NUM_ENTTYPES)
|
|
|
|
return 0;
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
int Num = 0;
|
2011-01-19 17:27:50 +00:00
|
|
|
for(CEntity *pEnt = m_apFirstEntityTypes[Type]; pEnt; pEnt = pEnt->m_pNextTypeEntity)
|
2008-08-14 18:42:47 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
if(distance(pEnt->m_Pos, Pos) < Radius+pEnt->m_ProximityRadius)
|
2008-08-14 18:42:47 +00:00
|
|
|
{
|
2011-02-10 11:05:55 +00:00
|
|
|
if(ppEnts)
|
|
|
|
ppEnts[Num] = pEnt;
|
2010-05-29 07:25:38 +00:00
|
|
|
Num++;
|
|
|
|
if(Num == Max)
|
2008-08-14 18:42:47 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
return Num;
|
2008-08-14 18:42:47 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CGameWorld::InsertEntity(CEntity *pEnt)
|
2008-08-14 18:42:47 +00:00
|
|
|
{
|
2011-01-19 17:27:50 +00:00
|
|
|
#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");
|
2011-01-19 17:27:50 +00:00
|
|
|
#endif
|
2008-08-14 18:42:47 +00:00
|
|
|
|
|
|
|
// insert it
|
2011-01-19 17:27:50 +00:00
|
|
|
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;
|
2011-01-19 17:27:50 +00:00
|
|
|
m_apFirstEntityTypes[pEnt->m_ObjType] = pEnt;
|
2008-08-14 18:42:47 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CGameWorld::DestroyEntity(CEntity *pEnt)
|
2008-08-14 18:42:47 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
pEnt->m_MarkedForDestroy = true;
|
2008-08-14 18:42:47 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CGameWorld::RemoveEntity(CEntity *pEnt)
|
2008-08-14 18:42:47 +00:00
|
|
|
{
|
|
|
|
// not in the list
|
2011-01-19 17:27:50 +00:00
|
|
|
if(!pEnt->m_pNextTypeEntity && !pEnt->m_pPrevTypeEntity && m_apFirstEntityTypes[pEnt->m_ObjType] != pEnt)
|
2008-08-14 18:42:47 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// remove
|
2010-05-29 07:25:38 +00:00
|
|
|
if(pEnt->m_pPrevTypeEntity)
|
|
|
|
pEnt->m_pPrevTypeEntity->m_pNextTypeEntity = pEnt->m_pNextTypeEntity;
|
2008-08-14 18:42:47 +00:00
|
|
|
else
|
2011-01-19 17:27:50 +00:00
|
|
|
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;
|
|
|
|
|
2011-01-19 14:31:42 +00:00
|
|
|
// keep list traversing valid
|
|
|
|
if(m_pNextTraverseEntity == pEnt)
|
2011-01-19 17:27:50 +00:00
|
|
|
m_pNextTraverseEntity = pEnt->m_pNextTypeEntity;
|
2011-01-19 14:31:42 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
pEnt->m_pNextTypeEntity = 0;
|
|
|
|
pEnt->m_pPrevTypeEntity = 0;
|
2008-08-14 18:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2010-05-29 07:25:38 +00:00
|
|
|
void CGameWorld::Snap(int SnappingClient)
|
2008-08-14 18:42:47 +00:00
|
|
|
{
|
2011-01-19 17:27:50 +00:00
|
|
|
for(int i = 0; i < NUM_ENTTYPES; i++)
|
|
|
|
for(CEntity *pEnt = m_apFirstEntityTypes[i]; pEnt; )
|
|
|
|
{
|
|
|
|
m_pNextTraverseEntity = pEnt->m_pNextTypeEntity;
|
|
|
|
pEnt->Snap(SnappingClient);
|
|
|
|
pEnt = m_pNextTraverseEntity;
|
|
|
|
}
|
2008-08-14 18:42:47 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CGameWorld::Reset()
|
2008-08-14 18:42:47 +00:00
|
|
|
{
|
|
|
|
// reset all entities
|
2011-01-19 17:27:50 +00:00
|
|
|
for(int i = 0; i < NUM_ENTTYPES; i++)
|
|
|
|
for(CEntity *pEnt = m_apFirstEntityTypes[i]; pEnt; )
|
|
|
|
{
|
|
|
|
m_pNextTraverseEntity = pEnt->m_pNextTypeEntity;
|
|
|
|
pEnt->Reset();
|
|
|
|
pEnt = m_pNextTraverseEntity;
|
|
|
|
}
|
2010-05-29 07:25:38 +00:00
|
|
|
RemoveEntities();
|
2008-08-14 18:42:47 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
GameServer()->m_pController->PostReset();
|
|
|
|
RemoveEntities();
|
2008-08-14 18:42:47 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
m_ResetRequested = false;
|
2008-08-14 18:42:47 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CGameWorld::RemoveEntities()
|
2008-08-14 18:42:47 +00:00
|
|
|
{
|
|
|
|
// destroy objects marked for destruction
|
2011-01-19 17:27:50 +00:00
|
|
|
for(int i = 0; i < NUM_ENTTYPES; i++)
|
|
|
|
for(CEntity *pEnt = m_apFirstEntityTypes[i]; pEnt; )
|
2008-08-14 18:42:47 +00:00
|
|
|
{
|
2011-01-19 17:27:50 +00:00
|
|
|
m_pNextTraverseEntity = pEnt->m_pNextTypeEntity;
|
|
|
|
if(pEnt->m_MarkedForDestroy)
|
|
|
|
{
|
|
|
|
RemoveEntity(pEnt);
|
|
|
|
pEnt->Destroy();
|
|
|
|
}
|
|
|
|
pEnt = m_pNextTraverseEntity;
|
2008-08-14 18:42:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CGameWorld::Tick()
|
2008-08-14 18:42:47 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
if(m_ResetRequested)
|
|
|
|
Reset();
|
2008-08-14 18:42:47 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(!m_Paused)
|
2008-08-14 18:42:47 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
if(GameServer()->m_pController->IsForceBalanced())
|
|
|
|
GameServer()->SendChat(-1, CGameContext::CHAT_ALL, "Teams have been balanced");
|
2008-08-14 18:42:47 +00:00
|
|
|
// update all objects
|
2011-01-19 17:27:50 +00:00
|
|
|
for(int i = 0; i < NUM_ENTTYPES; i++)
|
|
|
|
for(CEntity *pEnt = m_apFirstEntityTypes[i]; pEnt; )
|
|
|
|
{
|
|
|
|
m_pNextTraverseEntity = pEnt->m_pNextTypeEntity;
|
|
|
|
pEnt->Tick();
|
|
|
|
pEnt = m_pNextTraverseEntity;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2011-01-19 17:27:50 +00:00
|
|
|
for(int i = 0; i < NUM_ENTTYPES; i++)
|
|
|
|
for(CEntity *pEnt = m_apFirstEntityTypes[i]; pEnt; )
|
|
|
|
{
|
|
|
|
m_pNextTraverseEntity = pEnt->m_pNextTypeEntity;
|
|
|
|
pEnt->TickDefered();
|
|
|
|
pEnt = m_pNextTraverseEntity;
|
|
|
|
}
|
2008-08-14 18:42:47 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
RemoveEntities();
|
2008-08-14 18:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: should be more general
|
2011-04-09 06:41:31 +00:00
|
|
|
//CCharacter *CGameWorld::IntersectCharacter(vec2 Pos0, vec2 Pos1, float Radius, vec2& NewPos, CEntity *pNotThis)
|
2011-03-19 20:21:42 +00:00
|
|
|
CCharacter *CGameWorld::IntersectCharacter(vec2 Pos0, vec2 Pos1, float Radius, vec2& NewPos, CCharacter *pNotThis, int CollideWith, class CCharacter *pThisOnly)
|
2008-08-14 18:42:47 +00:00
|
|
|
{
|
|
|
|
// Find other players
|
2010-05-29 07:25:38 +00:00
|
|
|
float ClosestLen = distance(Pos0, Pos1) * 100.0f;
|
|
|
|
CCharacter *pClosest = 0;
|
2008-08-14 18:42:47 +00:00
|
|
|
|
2011-01-19 17:27:50 +00:00
|
|
|
CCharacter *p = (CCharacter *)FindFirst(ENTTYPE_CHARACTER);
|
2010-05-29 07:25:38 +00:00
|
|
|
for(; p; p = (CCharacter *)p->TypeNext())
|
2008-08-14 18:42:47 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
if(p == pNotThis)
|
2008-08-14 18:42:47 +00:00
|
|
|
continue;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2011-04-09 06:41:31 +00:00
|
|
|
if(CollideWith != -1 && !p->CanCollide(CollideWith))
|
|
|
|
continue;
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
vec2 IntersectPos = closest_point_on_line(Pos0, Pos1, p->m_Pos);
|
|
|
|
float Len = distance(p->m_Pos, IntersectPos);
|
|
|
|
if(Len < p->m_ProximityRadius+Radius)
|
2008-08-14 18:42:47 +00:00
|
|
|
{
|
2010-12-12 00:55:36 +00:00
|
|
|
Len = distance(Pos0, IntersectPos);
|
2010-05-29 07:25:38 +00:00
|
|
|
if(Len < ClosestLen)
|
2008-08-14 18:42:47 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
NewPos = IntersectPos;
|
|
|
|
ClosestLen = Len;
|
|
|
|
pClosest = p;
|
2008-08-14 18:42:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
return pClosest;
|
2008-08-14 18:42:47 +00:00
|
|
|
}
|
|
|
|
|
2011-01-06 03:46:10 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
CCharacter *CGameWorld::ClosestCharacter(vec2 Pos, float Radius, CEntity *pNotThis)
|
2008-08-14 18:42:47 +00:00
|
|
|
{
|
|
|
|
// Find other players
|
2010-05-29 07:25:38 +00:00
|
|
|
float ClosestRange = Radius*2;
|
|
|
|
CCharacter *pClosest = 0;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2011-01-19 17:27:50 +00:00
|
|
|
CCharacter *p = (CCharacter *)GameServer()->m_World.FindFirst(ENTTYPE_CHARACTER);
|
2010-05-29 07:25:38 +00:00
|
|
|
for(; p; p = (CCharacter *)p->TypeNext())
|
2008-08-14 18:42:47 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
if(p == pNotThis)
|
2008-08-14 18:42:47 +00:00
|
|
|
continue;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
float Len = distance(Pos, p->m_Pos);
|
|
|
|
if(Len < p->m_ProximityRadius+Radius)
|
2008-08-14 18:42:47 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
if(Len < ClosestRange)
|
2008-08-14 18:42:47 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
ClosestRange = Len;
|
|
|
|
pClosest = p;
|
2008-08-14 18:42:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
return pClosest;
|
2008-08-14 18:42:47 +00:00
|
|
|
}
|
2011-03-19 20:21:42 +00:00
|
|
|
|
2010-09-10 06:55:04 +00:00
|
|
|
std::list<class CCharacter *> CGameWorld::IntersectedCharacters(vec2 Pos0, vec2 Pos1, float Radius, class CEntity *pNotThis)
|
2010-08-31 11:01:46 +00:00
|
|
|
{
|
|
|
|
std::list< CCharacter * > listOfChars;
|
|
|
|
// Find other players
|
|
|
|
vec2 LineDir = normalize(Pos1-Pos0);
|
|
|
|
|
2011-01-29 00:59:50 +00:00
|
|
|
CCharacter *pChr = (CCharacter *)FindFirst(CGameWorld::ENTTYPE_CHARACTER);
|
|
|
|
for(; pChr; pChr = (CCharacter *)pChr->TypeNext())
|
2010-08-31 11:01:46 +00:00
|
|
|
{
|
2011-01-29 00:59:50 +00:00
|
|
|
if(pChr == pNotThis)
|
2010-08-31 11:01:46 +00:00
|
|
|
continue;
|
|
|
|
|
2011-01-29 00:59:50 +00:00
|
|
|
vec2 IntersectPos = closest_point_on_line(Pos0, Pos1, pChr->m_Pos);
|
|
|
|
float Len = distance(pChr->m_Pos, IntersectPos);
|
|
|
|
if(Len < pChr->m_ProximityRadius+Radius)
|
2010-08-31 11:01:46 +00:00
|
|
|
{
|
2011-01-29 00:59:50 +00:00
|
|
|
pChr->m_Intersection = IntersectPos;
|
|
|
|
listOfChars.push_back(pChr);
|
2010-08-31 11:01:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return listOfChars;
|
|
|
|
}
|
2010-09-11 09:42:35 +00:00
|
|
|
|
2011-02-13 05:35:13 +00:00
|
|
|
void CGameWorld::ReleaseHooked(int ClientID)
|
2010-09-11 00:01:15 +00:00
|
|
|
{
|
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();
|
2011-02-13 05:35:13 +00:00
|
|
|
if(Core->m_HookedPlayer == ClientID && !pChr->m_Super)
|
2010-09-11 00:01:15 +00:00
|
|
|
{
|
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;
|
2010-09-11 00:01:15 +00:00
|
|
|
}
|
2011-01-29 00:59:50 +00:00
|
|
|
}
|
2011-01-20 14:10:32 +00:00
|
|
|
}
|