From 1e281bfce2fe525b10f4447537446383c9b4a729 Mon Sep 17 00:00:00 2001 From: Boondorl Date: Sun, 15 Jun 2025 15:40:11 -0400 Subject: [PATCH] Added player iterators Allows for easily iterating through players currently in the game. --- src/playsim/d_player.h | 3 ++ src/playsim/p_user.cpp | 35 +++++++++++++++++++ wadsrc/static/zscript/actors/player/player.zs | 3 ++ 3 files changed, 41 insertions(+) diff --git a/src/playsim/d_player.h b/src/playsim/d_player.h index 536da4aed..6e03436e0 100644 --- a/src/playsim/d_player.h +++ b/src/playsim/d_player.h @@ -477,6 +477,9 @@ public: bool HasWeaponsInSlot(int slot) const; bool Resurrect(); + static player_t* GetNextPlayer(player_t* p, bool noBots = false); + static int GetNextPlayerNumber(int pNum, bool noBots = false); + // Scaled angle adjustment info. Not for direct manipulation. DRotator angleOffsetTargets; }; diff --git a/src/playsim/p_user.cpp b/src/playsim/p_user.cpp index ccc75998e..1f109d808 100644 --- a/src/playsim/p_user.cpp +++ b/src/playsim/p_user.cpp @@ -746,6 +746,41 @@ DEFINE_ACTION_FUNCTION(_PlayerInfo, Resurrect) ACTION_RETURN_BOOL(self->Resurrect()); } +player_t* player_t::GetNextPlayer(player_t* p, bool noBots) +{ + int pNum = player_t::GetNextPlayerNumber(p == nullptr ? -1 : p - players); + return pNum != -1 ? &players[pNum] : nullptr; +} + +DEFINE_ACTION_FUNCTION_NATIVE(_PlayerInfo, GetNextPlayer, player_t::GetNextPlayer) +{ + PARAM_PROLOGUE; + PARAM_POINTER(p, player_t); + PARAM_BOOL(noBots); + + ACTION_RETURN_POINTER(player_t::GetNextPlayer(p, noBots)); +} + +int player_t::GetNextPlayerNumber(int pNum, bool noBots) +{ + int i = max(pNum + 1, 0); + for (; i < MaxClients; ++i) + { + if (playeringame[i] && (!noBots || players[i].Bot == nullptr)) + break; + } + + return i < MaxClients ? i : -1; +} + +DEFINE_ACTION_FUNCTION_NATIVE(_PlayerInfo, GetNextPlayerNumber, player_t::GetNextPlayerNumber) +{ + PARAM_PROLOGUE; + PARAM_INT(pNum); + PARAM_BOOL(noBots); + + ACTION_RETURN_INT(player_t::GetNextPlayerNumber(pNum, noBots)); +} DEFINE_ACTION_FUNCTION(_PlayerInfo, GetUserName) { diff --git a/wadsrc/static/zscript/actors/player/player.zs b/wadsrc/static/zscript/actors/player/player.zs index f6d12a81e..a64fca8f9 100644 --- a/wadsrc/static/zscript/actors/player/player.zs +++ b/wadsrc/static/zscript/actors/player/player.zs @@ -3004,6 +3004,9 @@ struct PlayerInfo native play // self is what internally is known as player_t native clearscope int GetAverageLatency() const; + native clearscope static PlayerInfo GetNextPlayer(PlayerInfo p, bool noBots = false); + native clearscope static int GetNextPlayerNumber(int pNum, bool noBots = false); + // The actual implementation is on PlayerPawn where it can be overridden. Use that directly in the future. deprecated("3.7", "MorphPlayer() should be used on a PlayerPawn object") bool MorphPlayer(PlayerInfo activator, class spawnType, int duration, EMorphFlags style, class enterFlash = "TeleportFog", class exitFlash = "TeleportFog") {