From b0139421c506acefd9d0b258c521f507f671562b Mon Sep 17 00:00:00 2001 From: Faramour <44729057+Faramour@users.noreply.github.com> Date: Wed, 18 Sep 2024 00:34:07 +0200 Subject: [PATCH 1/3] Add !spec command --- src/commands.cpp | 80 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/src/commands.cpp b/src/commands.cpp index e3395de7..6fe67403 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -479,6 +479,86 @@ CON_COMMAND_CHAT(help, "- Display list of commands in console") ClientPrint(player, HUD_PRINTCONSOLE, "! can be replaced with / for a silent chat command, or c_ for console usage"); } +CON_COMMAND_CHAT(spec, " - Spectate another player") +{ + if (!player) + { + ClientPrint(player, HUD_PRINTCONSOLE, CHAT_PREFIX "You cannot use this command from the server console."); + return; + } + + if (args.ArgC() < 2) + { + ClientPrint(player, HUD_PRINTTALK, CHAT_PREFIX "Usage: !spec "); + return; + } + + int iCommandPlayer = player ? player->GetPlayerSlot() : -1; + int iNumClients = 0; + int pSlot[MAXPLAYERS]; + + if (g_playerManager->TargetPlayerString(iCommandPlayer, args[1], iNumClients, pSlot) > ETargetType::PLAYER) + { + ClientPrint(player, HUD_PRINTTALK, CHAT_PREFIX "You can only spectate a specific player."); + return; + } + + if (!iNumClients) + { + ClientPrint(player, HUD_PRINTTALK, CHAT_PREFIX "Target not found."); + return; + } + + if (iNumClients > 1) + { + ClientPrint(player, HUD_PRINTTALK, CHAT_PREFIX "More than one player matched."); + return; + } + + CCSPlayerController* pTarget = CCSPlayerController::FromSlot(pSlot[0]); + + if (!pTarget) + return; + + if (pTarget == player) + { + ClientPrint(player, HUD_PRINTTALK, CHAT_PREFIX "You cannot spectate yourself."); + return; + } + + if (pTarget->m_iTeamNum() == CS_TEAM_SPECTATOR) + { + ClientPrint(player, HUD_PRINTTALK, CHAT_PREFIX "Target is a spectator."); + return; + } + + if (!pTarget->m_bPawnIsAlive()) + { + ClientPrint(player, HUD_PRINTTALK, CHAT_PREFIX "Target is not alive."); + return; + } + + if (player->m_iTeamNum() != CS_TEAM_SPECTATOR) + player->SwitchTeam(CS_TEAM_SPECTATOR); + + // 1 frame delay as observer services will be null on same frame as spectator team switch + CHandle hPlayer = player->GetHandle(); + CHandle hTarget = pTarget->GetHandle(); + new CTimer(0.0f, false, false, [hPlayer, hTarget](){ + CCSPlayerController* pPlayer = hPlayer.Get(); + CCSPlayerController* pTargetPlayer = hTarget.Get(); + if (!pPlayer || !pTargetPlayer) + return -1.0f; + CPlayer_ObserverServices* pObserverServices = pPlayer->GetPawn()->m_pObserverServices(); + if (!pObserverServices) + return -1.0f; + pObserverServices->m_iObserverMode.Set(OBS_MODE_IN_EYE); + pObserverServices->m_iObserverLastMode.Set(OBS_MODE_ROAMING); + pObserverServices->m_hObserverTarget.Set(pTargetPlayer->GetPawn()); + ClientPrint(pPlayer, HUD_PRINTTALK, CHAT_PREFIX "Spectating player %s.", pTargetPlayer->GetPlayerName()); + return -1.0f; + }); +} #if _DEBUG CON_COMMAND_CHAT(myuid, "- test") From addef9ead96ddec90af7d43e0ac11cde4aa4dca6 Mon Sep 17 00:00:00 2001 From: Vauff Date: Wed, 2 Oct 2024 15:02:59 -0400 Subject: [PATCH 2/3] Update to new targeting --- src/commands.cpp | 35 +---------------------------------- 1 file changed, 1 insertion(+), 34 deletions(-) diff --git a/src/commands.cpp b/src/commands.cpp index 1beae85f..716b95bf 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -493,47 +493,14 @@ CON_COMMAND_CHAT(spec, " - Spectate another player") int iNumClients = 0; int pSlot[MAXPLAYERS]; - if (g_playerManager->TargetPlayerString(iCommandPlayer, args[1], iNumClients, pSlot) > ETargetType::PLAYER) - { - ClientPrint(player, HUD_PRINTTALK, CHAT_PREFIX "You can only spectate a specific player."); - return; - } - - if (!iNumClients) - { - ClientPrint(player, HUD_PRINTTALK, CHAT_PREFIX "Target not found."); + if (!g_playerManager->CanTargetPlayers(player, args[1], iNumClients, pSlot, NO_MULTIPLE | NO_SELF | NO_DEAD | NO_SPECTATOR)) return; - } - - if (iNumClients > 1) - { - ClientPrint(player, HUD_PRINTTALK, CHAT_PREFIX "More than one player matched."); - return; - } CCSPlayerController* pTarget = CCSPlayerController::FromSlot(pSlot[0]); if (!pTarget) return; - if (pTarget == player) - { - ClientPrint(player, HUD_PRINTTALK, CHAT_PREFIX "You cannot spectate yourself."); - return; - } - - if (pTarget->m_iTeamNum() == CS_TEAM_SPECTATOR) - { - ClientPrint(player, HUD_PRINTTALK, CHAT_PREFIX "Target is a spectator."); - return; - } - - if (!pTarget->m_bPawnIsAlive()) - { - ClientPrint(player, HUD_PRINTTALK, CHAT_PREFIX "Target is not alive."); - return; - } - if (player->m_iTeamNum() != CS_TEAM_SPECTATOR) player->SwitchTeam(CS_TEAM_SPECTATOR); From 0589ad34008ad8019dbb3384d559782a9265f43c Mon Sep 17 00:00:00 2001 From: Faramour <44729057+Faramour@users.noreply.github.com> Date: Thu, 3 Oct 2024 00:49:42 +0200 Subject: [PATCH 3/3] move to spec without target --- src/commands.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/commands.cpp b/src/commands.cpp index 716b95bf..06817ba1 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -475,7 +475,7 @@ CON_COMMAND_CHAT(help, "- Display list of commands in console") ClientPrint(player, HUD_PRINTCONSOLE, "! can be replaced with / for a silent chat command, or c_ for console usage"); } -CON_COMMAND_CHAT(spec, " - Spectate another player") +CON_COMMAND_CHAT(spec, "[name] - Spectate another player or join spectators") { if (!player) { @@ -485,7 +485,15 @@ CON_COMMAND_CHAT(spec, " - Spectate another player") if (args.ArgC() < 2) { - ClientPrint(player, HUD_PRINTTALK, CHAT_PREFIX "Usage: !spec "); + if (player->m_iTeamNum() == CS_TEAM_SPECTATOR) + { + ClientPrint(player, HUD_PRINTTALK, CHAT_PREFIX "Already spectating."); + } + else + { + player->SwitchTeam(CS_TEAM_SPECTATOR); + ClientPrint(player, HUD_PRINTTALK, CHAT_PREFIX "Moved to spectators."); + } return; }