• Check out the results of the Techtree Contest #19!
  • Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.
  • Create a void inspired texture for Warcraft 3 and enter Hive's 34th Texturing Contest: Void! Click here to enter!
  • The Hive's 22nd Icon Contest: Creep Abilities is now concluded, time to vote for your favourite set of icons! Click here to vote!

Replay POV Detection

This bundle is marked as pending. It has not been reviewed by a staff member yet.
  • Like
Reactions: deepstrasz
A small snippet for detecting which POV is selected during a replay.

After finding that DisplayTextToPlayer acts only for the player for which the UI is currently rendering, sotzaii_shuen set out to find 2 more natives that share the same logic; IsLeaderboardDisplayed and DisplayTimedTextToPlayer. IsLeaderboardDisplayed is the only candidate that can be used for figuring out which player is currently being observed in a replay, considering it is the only one that returns something.

The logic is as follows:
- for each player
- set leaderboard to that player (SetPlayerLeaderboard - incidentally it automatically displays it)
- check if leaderboard is displayed (IsLeaderboardDisplayed)
- if true -> that's the player that is currently being observed by replayer


vJASS:
library ReplayPOVDetection initializer init requires /**
  
    v1.0.0 by InsanityAI
    -------------------------------------------------------
    This snippet is used to detect which player perspective is the replay spectator currently viewing, by abusing how leaderboards work.
    In short, IsLeaderboardDisplayed will only return true for your current spectator / player

    Credits to sotzaii_shuen for finding this function (among others) with this behaviour
    -------------------------------------------------------
    1. Installation
    - Copy this script to your map
    - Put the following library in your map
    **/ GameStatus /** https://www.hiveworkshop.com/threads/gamestatus-replay-detection.293176/
    - Save map
    -------------------------------------------------------
    2. API
    // function GetReplayPlayer takes nothing returns player
    -  - returns the player that the replay spectator is currently observing
    -  - returns local player if it was either
    -  -  - a. unable to determine which POV was observed
    -  -  - b. not a replay (but an ongoing game)
    -------------------------------------------------------**/

    globals
        private leaderboard lb
        private leaderboard tempLeaderboard = null
        private player povPlayer = null
        private integer i = 0
        private boolean wasDisplayed = false
        private boolean somethingWrong = false
    endglobals

    // Will return whatever player the replay spectator is currently viewing, otherwise, if GameStatus determines that it is not a replay, or if it is
    // unable to determine the replay player, will return local player instead. It is advised to store the resulting player in a variable if it were to
    // be used multiple times in same scope/instance, since it does have quite a few native calls happening during a replay
    function GetReplayPlayer takes nothing returns player
        if GetGameStatus() != GAME_STATUS_REPLAY then
            return GetLocalPlayer()
        endif

        set somethingWrong = true
        set i = 0
        loop
            set povPlayer = Player(i)

            //record player's current leaderboard
            set tempLeaderboard = PlayerGetLeaderboard(povPlayer)
            if (tempLeaderboard != null) then
                set wasDisplayed = IsLeaderboardDisplayed(tempLeaderboard)
            else
                set wasDisplayed = false
            endif

            //Check if player is being spectated
            call PlayerSetLeaderboard(povPlayer, lb)
            if IsLeaderboardDisplayed(lb) then
                set somethingWrong = false
                set i = bj_MAX_PLAYER_SLOTS //force exit after cleanup is done
            endif

            //restore player's leaderboard, if any
            if (tempLeaderboard != null) then
                call PlayerSetLeaderboard(povPlayer, tempLeaderboard)
                call LeaderboardDisplay(tempLeaderboard, wasDisplayed)
            else
                call PlayerSetLeaderboard(povPlayer, null)
            endif

            set i = i + 1
            exitwhen i >= bj_MAX_PLAYER_SLOTS
        endloop
        call LeaderboardDisplay(lb, false)

        //In case it couldn't determine observed player
        if (somethingWrong) then
            return GetLocalPlayer()
        endif

        return povPlayer
    endfunction

    private function init takes nothing returns nothing
        set lb = CreateLeaderboard()
    endfunction
endlibrary
Lua:
if Debug then Debug.beginFile "ReplayPOVDetection" end
OnInit.global("ReplayPOVDetection", function(require) --[[

    v1.0.0 by InsanityAI
    -------------------------------------------------------
    This snippet is used to detect which player perspective is the replay spectator currently viewing, by abusing how leaderboards work.
    In short, IsLeaderboardDisplayed will only return true for your current spectator / player

    Credits to sotzaii_shuen for finding this function (among others) with this behaviour
    -------------------------------------------------------
    1. Installation
     - Copy this script to your map
     - Put the following libraries in your map
        TotalInitialization - https://www.hiveworkshop.com/threads/total-initialization.317099/post-3641920 --]]
    require "GameStatus" --[[ https://www.hiveworkshop.com/threads/gamestatus-replay-detection.293176/
     - Save map
    -------------------------------------------------------
    2. API
    // function GetReplayPlayer(): player
        returns the player that the replay spectator is currently observing
        returns local player if it was either
            a. unable to determine which POV was observed
            b. not a replay (but an ongoing game)
    -------------------------------------------------------]]

    local lb = CreateLeaderboard()
    local tempLeaderboard = nil ---@type leaderboard?
    local povPlayer = nil ---@type player
    local i = 0
    local wasDisplayed = false
    local somethingWrong = false

    -- Will return whatever player the replay spectator is currently viewing, otherwise, if GameStatus determines that it is not a replay, or if it is
    -- unable to determine the replay player, will return local player instead. It is advised to store the resulting player in a variable if it were to
    -- be used multiple times in same scope/instance, since it does have quite a few native calls happening during a replay
    ---@return player replayPlayer
    function GetReplayPlayer()
        if GetGameStatus() ~= GameStatus.REPLAY then
            return GetLocalPlayer()
        end

        somethingWrong = true
        i = 0
        repeat
            povPlayer = Player(i)

            --record player's current leaderboard
            tempLeaderboard = PlayerGetLeaderboard(povPlayer)
            if (tempLeaderboard ~= nil) then
                wasDisplayed = IsLeaderboardDisplayed(tempLeaderboard)
            else
                wasDisplayed = false
            end

            --Check if player is being spectated
            PlayerSetLeaderboard(povPlayer, lb)
            if IsLeaderboardDisplayed(lb) then
                somethingWrong = false
                i = bj_MAX_PLAYER_SLOTS --force exit after cleanup is done
            end

            --restore player's leaderboard, if any
            if (tempLeaderboard ~= nil) then
                PlayerSetLeaderboard(povPlayer, tempLeaderboard)
                LeaderboardDisplay(tempLeaderboard, wasDisplayed)
            else
                PlayerSetLeaderboard(povPlayer, nil)
            end

            i = i + 1
        until (i >= bj_MAX_PLAYER_SLOTS)
        LeaderboardDisplay(lb, false)

        -- In case it couldn't determine observed player
        if somethingWrong then
            return GetLocalPlayer()
        end

        return povPlayer
    end
end)
Contents

Replay POV Detection Lua Demo (Map)

Replay POV Detection vJASS Demo (Map)

Back
Top