• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Replay Detection Script

Status
Not open for further replies.
This script seems to be able to accurately detect replays. I haven't tested in Battle.net yet, or maybe I a forgetting something, so I'm posting here first.

I've tested:
  • Single Player
  • Multiplayer - LAN (1 player)
  • Multiplayer - LAN (2 players)
I was able to run code specifically in replays in each of those scenarios.

JASS:
library GameStatus requires PlayerUtils

//configuration
globals
    // Dummy unit is only created once, and removed directly after.
    private constant integer DUMMY_UNIT_ID = 'hfoo'
endglobals
//endconfig

globals
    constant integer GAME_STATUS_OFFLINE = 0
    constant integer GAME_STATUS_ONLINE  = 1
    constant integer GAME_STATUS_REPLAY  = 2
   
    private integer status = 0
endglobals

function GetGameStatus takes nothing returns integer
    return status
endfunction

private module GameStatusInit

    private static method onInit takes nothing returns nothing
        local player firstPlayer = User.fromPlaying(0).handle
        local unit u = CreateUnit(firstPlayer, DUMMY_UNIT_ID, 0, 0, 0)
        local boolean selected
       
        call SelectUnit(u, true)
        set selected = IsUnitSelected(u, firstPlayer)
        call RemoveUnit(u)
        set u = null
       
        if (selected) then
       
            if (User.AmountPlaying > 1) then
                set status = GAME_STATUS_REPLAY
                return
            endif
           
            if (ReloadGameCachesFromDisk()) then
                set status = GAME_STATUS_OFFLINE
            else
                set status = GAME_STATUS_REPLAY
            endif
           
        else
            set status = GAME_STATUS_ONLINE
        endif
       
    endmethod
   
endmodule

private struct GameStatus
    implement GameStatusInit
endstruct

endlibrary
Example:

JASS:
scope GameStatusExample initializer Init

    private function OnMapStart takes nothing returns nothing
        local integer status = GetGameStatus()
       
        if (status == GAME_STATUS_OFFLINE) then
            call BJDebugMsg("This game is offline.")
        elseif(status == GAME_STATUS_ONLINE) then
            call BJDebugMsg("This game is online.")
        elseif(status == GAME_STATUS_REPLAY) then
            call BJDebugMsg("You are viewing a replay")
        endif
    endfunction
   
    private function Init takes nothing returns nothing
        call TimerStart(CreateTimer(), 0.00, false, function OnMapStart)
    endfunction
   
endscope
 
Status
Not open for further replies.
Top