Improve interfaces by exposing SwapClients in CEntities

This commit is contained in:
def 2021-12-14 10:35:00 +01:00
parent e83a7ff96f
commit ddf04c1ff2
8 changed files with 79 additions and 54 deletions

View file

@ -285,3 +285,8 @@ void CLaser::Snap(int SnappingClient)
pObj->m_FromY = (int)m_From.y; pObj->m_FromY = (int)m_From.y;
pObj->m_StartTick = m_EvalTick; pObj->m_StartTick = m_EvalTick;
} }
void CLaser::SwapClients(int Client1, int Client2)
{
m_Owner = m_Owner == Client1 ? Client2 : m_Owner == Client2 ? Client1 : m_Owner;
}

View file

@ -7,15 +7,14 @@
class CLaser : public CEntity class CLaser : public CEntity
{ {
friend class CGameTeams;
public: public:
CLaser(CGameWorld *pGameWorld, vec2 Pos, vec2 Direction, float StartEnergy, int Owner, int Type); CLaser(CGameWorld *pGameWorld, vec2 Pos, vec2 Direction, float StartEnergy, int Owner, int Type);
virtual void Reset(); virtual void Reset() override;
virtual void Tick(); virtual void Tick() override;
virtual void TickPaused(); virtual void TickPaused() override;
virtual void Snap(int SnappingClient); virtual void Snap(int SnappingClient) override;
virtual void SwapClients(int Client1, int Client2) override;
protected: protected:
bool HitCharacter(vec2 From, vec2 To); bool HitCharacter(vec2 From, vec2 To);

View file

@ -357,6 +357,11 @@ void CProjectile::Snap(int SnappingClient)
} }
} }
void CProjectile::SwapClients(int Client1, int Client2)
{
m_Owner = m_Owner == Client1 ? Client2 : m_Owner == Client2 ? Client1 : m_Owner;
}
// DDRace // DDRace
void CProjectile::SetBouncing(int Value) void CProjectile::SetBouncing(int Value)

View file

@ -7,8 +7,6 @@
class CProjectile : public CEntity class CProjectile : public CEntity
{ {
friend class CGameTeams;
public: public:
CProjectile( CProjectile(
CGameWorld *pGameWorld, CGameWorld *pGameWorld,
@ -27,10 +25,11 @@ public:
vec2 GetPos(float Time); vec2 GetPos(float Time);
void FillInfo(CNetObj_Projectile *pProj); void FillInfo(CNetObj_Projectile *pProj);
virtual void Reset(); virtual void Reset() override;
virtual void Tick(); virtual void Tick() override;
virtual void TickPaused(); virtual void TickPaused() override;
virtual void Snap(int SnappingClient); virtual void Snap(int SnappingClient) override;
virtual void SwapClients(int Client1, int Client2) override;
private: private:
vec2 m_Direction; vec2 m_Direction;

View file

@ -114,6 +114,16 @@ public:
*/ */
virtual void Snap(int SnappingClient) {} virtual void Snap(int SnappingClient) {}
/*
Function: SwapClients
Called when two players have swapped their client ids.
Arguments:
Client1 - First client ID
Client2 - Second client ID
*/
virtual void SwapClients(int Client1, int Client2) {}
/* /*
Function: NetworkClipped Function: NetworkClipped
Performs a series of test to see if a client can see the Performs a series of test to see if a client can see the

View file

@ -306,6 +306,18 @@ void CGameWorld::Tick()
} }
} }
void CGameWorld::SwapClients(int Client1, int Client2)
{
// update all objects
for(auto *pEnt : m_apFirstEntityTypes)
for(; pEnt;)
{
m_pNextTraverseEntity = pEnt->m_pNextTypeEntity;
pEnt->SwapClients(Client1, Client2);
pEnt = m_pNextTraverseEntity;
}
}
// TODO: should be more general // 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, CEntity *pNotThis)
CCharacter *CGameWorld::IntersectCharacter(vec2 Pos0, vec2 Pos1, float Radius, vec2 &NewPos, CCharacter *pNotThis, int CollideWith, class CCharacter *pThisOnly) CCharacter *CGameWorld::IntersectCharacter(vec2 Pos0, vec2 Pos1, float Radius, vec2 &NewPos, CCharacter *pNotThis, int CollideWith, class CCharacter *pThisOnly)

View file

@ -58,16 +58,16 @@ public:
CEntity *FindFirst(int Type); CEntity *FindFirst(int Type);
/* /*
Function: find_entities Function: FindEntities
Finds entities close to a position and returns them in a list. Finds entities close to a position and returns them in a list.
Arguments: Arguments:
pos - Position. Pos - Position.
radius - How close the entities have to be. Radius - How close the entities have to be.
ents - Pointer to a list that should be filled with the pointers ppEnts - Pointer to a list that should be filled with the pointers
to the entities. to the entities.
max - Number of entities that fits into the ents array. Max - Number of entities that fits into the ents array.
type - Type of the entities to find. Type - Type of the entities to find.
Returns: Returns:
Number of entities found and added to the ents array. Number of entities found and added to the ents array.
@ -79,11 +79,11 @@ public:
Finds the CCharacters that intersects the line. // made for types lasers=1 and doors=0 Finds the CCharacters that intersects the line. // made for types lasers=1 and doors=0
Arguments: Arguments:
pos0 - Start position Pos0 - Start position
pos2 - End position Pos1 - End position
radius - How for from the line the CCharacter is allowed to be. Radius - How for from the line the CCharacter is allowed to be.
new_pos - Intersection position NewPos - Intersection position
notthis - Entity to ignore intersecting with pNotThis - Entity to ignore intersecting with
Returns: Returns:
Returns a pointer to the closest hit or NULL of there is no intersection. Returns a pointer to the closest hit or NULL of there is no intersection.
@ -91,13 +91,13 @@ public:
//class CCharacter *IntersectCharacter(vec2 Pos0, vec2 Pos1, float Radius, vec2 &NewPos, class CEntity *pNotThis = 0); //class CCharacter *IntersectCharacter(vec2 Pos0, vec2 Pos1, float Radius, vec2 &NewPos, class CEntity *pNotThis = 0);
class CCharacter *IntersectCharacter(vec2 Pos0, vec2 Pos1, float Radius, vec2 &NewPos, class CCharacter *pNotThis = 0, int CollideWith = -1, class CCharacter *pThisOnly = 0); class CCharacter *IntersectCharacter(vec2 Pos0, vec2 Pos1, float Radius, vec2 &NewPos, class CCharacter *pNotThis = 0, int CollideWith = -1, class CCharacter *pThisOnly = 0);
/* /*
Function: closest_CCharacter Function: ClosestCharacter
Finds the closest CCharacter to a specific point. Finds the closest CCharacter to a specific point.
Arguments: Arguments:
pos - The center position. Pos - The center position.
radius - How far off the CCharacter is allowed to be Radius - How far off the CCharacter is allowed to be
notthis - Entity to ignore ppNotThis - Entity to ignore
Returns: Returns:
Returns a pointer to the closest CCharacter or NULL if no CCharacter is close enough. Returns a pointer to the closest CCharacter or NULL if no CCharacter is close enough.
@ -105,55 +105,60 @@ public:
class CCharacter *ClosestCharacter(vec2 Pos, float Radius, CEntity *ppNotThis); class CCharacter *ClosestCharacter(vec2 Pos, float Radius, CEntity *ppNotThis);
/* /*
Function: insert_entity Function: InsertEntity
Adds an entity to the world. Adds an entity to the world.
Arguments: Arguments:
entity - Entity to add pEntity - Entity to add
*/ */
void InsertEntity(CEntity *pEntity); void InsertEntity(CEntity *pEntity);
/* /*
Function: remove_entity Function: RemoveEntity
Removes an entity from the world. Removes an entity from the world.
Arguments: Arguments:
entity - Entity to remove pEntity - Entity to remove
*/ */
void RemoveEntity(CEntity *pEntity); void RemoveEntity(CEntity *pEntity);
/* /*
Function: snap Function: Snap
Calls snap on all the entities in the world to create Calls Snap on all the entities in the world to create
the snapshot. the snapshot.
Arguments: Arguments:
snapping_client - ID of the client which snapshot SnappingClient - ID of the client which snapshot
is being created. is being created.
*/ */
void Snap(int SnappingClient); void Snap(int SnappingClient);
/* /*
Function: tick Function: Tick
Calls tick on all the entities in the world to progress Calls Tick on all the entities in the world to progress
the world to the next tick. the world to the next tick.
*/ */
void Tick(); void Tick();
/*
Function: SwapClients
Calls SwapClients on all the entities in the world to ensure that /swap
command is handled safely.
*/
void SwapClients(int Client1, int Client2);
// DDRace // DDRace
void ReleaseHooked(int ClientID); void ReleaseHooked(int ClientID);
/* /*
Function: interserct_CCharacters Function: IntersectedCharacters
Finds all CCharacters that intersect the line. Finds all CCharacters that intersect the line.
Arguments: Arguments:
pos0 - Start position Pos0 - Start position
pos2 - End position Pos1 - End position
radius - How for from the line the CCharacter is allowed to be. Radius - How for from the line the CCharacter is allowed to be.
new_pos - Intersection position pNotThis - Entity to ignore intersecting with
notthis - Entity to ignore intersecting with
Returns: Returns:
Returns list with all Characters on line. Returns list with all Characters on line.

View file

@ -891,17 +891,7 @@ void CGameTeams::SwapTeamCharacters(CPlayer *pPlayer, CPlayer *pTargetPlayer, in
swap(m_TeeStarted[pPlayer->GetCID()], m_TeeStarted[pTargetPlayer->GetCID()]); swap(m_TeeStarted[pPlayer->GetCID()], m_TeeStarted[pTargetPlayer->GetCID()]);
swap(m_TeeFinished[pPlayer->GetCID()], m_TeeFinished[pTargetPlayer->GetCID()]); swap(m_TeeFinished[pPlayer->GetCID()], m_TeeFinished[pTargetPlayer->GetCID()]);
for(CProjectile *pProj = (CProjectile *)GameServer()->m_World.FindFirst(CGameWorld::ENTTYPE_PROJECTILE); pProj; pProj = (CProjectile *)pProj->TypeNext()) GameServer()->m_World.SwapClients(pPlayer->GetCID(), pTargetPlayer->GetCID());
{
pProj->m_Owner = pProj->m_Owner == pPlayer->GetCID() ? pTargetPlayer->GetCID() : pProj->m_Owner == pTargetPlayer->GetCID() ? pPlayer->GetCID() : pProj->m_Owner;
}
for(CEntity *pEnt = GameServer()->m_World.FindFirst(CGameWorld::ENTTYPE_LASER); pEnt; pEnt = pEnt->TypeNext())
{
if(auto *const pLaser = dynamic_cast<CLaser *>(pEnt))
{
pLaser->m_Owner = pLaser->m_Owner == pPlayer->GetCID() ? pTargetPlayer->GetCID() : pLaser->m_Owner == pTargetPlayer->GetCID() ? pPlayer->GetCID() : pLaser->m_Owner;
}
}
str_format(aBuf, sizeof(aBuf), str_format(aBuf, sizeof(aBuf),
"%s has swapped with %s.", "%s has swapped with %s.",