[Snippet] Single Player Detector

Scripts

13 years
edited 8 years
Description


The purpose of this library is to offer a single function which checks if the game is played in single player mode or multiplayer.
Important: This function must be used only to define if the player is in single player or in Bnet or lan game. This function will fail in replays (it will show that you're in a LAN game by default).

Credits to Nestharus for the awesome solution to this problem :D

Requirements


- None

Actual Code


Code (jass) Select
/******************************************************************
*                   SINGLE PLAYER DETECTOR V3.0                   *
*                           By moyack                             *
*                              2012                               *
*              ===================================                *
*              Exclusive resource from wc3jass.com                *
*              ===================================                *
******************************************************************/
library SinglePlayerDetector
/*The purpose of this library is to offer a single function which checks if
  the game is played in single player mode or multiplayer. It offers only
  one function:
 
  isSinglePlayer: takes nothing and returns a boolean.
 
  If true, the game is being run in single player mode, else, in multiplayer
  CREDITS TO NESTHARUS for the improved and nice solution to this problem.
  It passed from a long code to a simple line function.
  Credits to Magtheridon96 for the library optimization
*/

globals
    private boolean sp = ReloadGameCachesFromDisk()
endglobals

function IsSinglePlayer takes nothing returns boolean
    return sp
endfunction

endlibrary


Example


Code (jass) Select
scope test initializer init

private function init takes nothing returns nothing
    if IsSinglePlayer() then
        call DisplayTimedTextFromPlayer(Player(0), 0,0,10, "You're playing alone, forever, happy. You can cheat")
    else
        call DisplayTimedTextFromPlayer(Player(0), 0,0,10, "You're playing multiplayer. You can't cheat")
    endif
endfunction

endscope
13 years
Uhhm, can you explain the:

" This function cannot be run at map initialization, otherwise it won't offer a right answer. It relies in "greedisgood" cheat to do the verification. "

What does it mean? Does it mean you have to put cheat first before the library run?
13 years
uhm...

Code (jass) Select

function IsSinglePlayer takes nothing returns boolean
    return ReloadGameCachesFromDisk()
endfunction


the above is the standard way to do this check...
13 years
Quote from: nestharus on November 05, 2012, 06:02:39 AM
uhm...

Code (jass) Select

function IsSinglePlayer takes nothing returns boolean
    return ReloadGameCachesFromDisk()
endfunction


the above is the standard way to do this check...

I... just... didn't... know... about .... this... AWESOME FEATURE!!!!! I burned a lot of brain cells thinking in one solution for this problem and you came with this :)

Awesome!!! I'll update this library, is offensively simple but it¿s worth bolding the feature of this function.


Quote from: sonofjay on November 05, 2012, 12:15:09 AM
Uhhm, can you explain the:

" This function cannot be run at map initialization, otherwise it won't offer a right answer. It relies in "greedisgood" cheat to do the verification. "

What does it mean? Does it mean you have to put cheat first before the library run?
In version  yes, but now with the outstanding solution from Nestharus, it's not necessary and works at map init!!!
13 years
Code (jass) Select
library IsSinglePlayer

    globals
        private boolean sp = ReloadGameCachesFromDisk()
    endglobals

    function IsSinglePlayer takes nothing returns boolean
        return sp
    endfunction

endlibrary


Also, it should be IsSinglePlayer, because FunctionNamesAreWrittenLikeThis ;P
13 years
Im quite sure that replays will screw this.
13 years
Quote from: Magtheridon96 on November 05, 2012, 02:51:18 PM
Code (jass) Select
library IsSinglePlayer

    globals
        private boolean sp = ReloadGameCachesFromDisk()
    endglobals

    function IsSinglePlayer takes nothing returns boolean
        return sp
    endfunction

endlibrary


Also, it should be IsSinglePlayer, because FunctionNamesAreWrittenLikeThis ;P
Hmmmm.... And I just haven't noticed that :P Your approach is good too.

Quote from: Troll-Brain on November 05, 2012, 04:56:51 PM
Im quite sure that replays will screw this.
I have to test it :)

13 years
http://www.wc3c.net/showthread.php?t=104921&page=3

That has a bit of a discussion about it. That one is about it being online though. So it really depends on what definition of single player you're after.

So you "might" have to revert to the cheat method, it depends on what you're trying to do. You should probably test it anyway just in case. :)
13 years
Quote from: Purgeandfire on November 06, 2012, 09:51:45 PM
http://www.wc3c.net/showthread.php?t=104921&page=3

That has a bit of a discussion about it. That one is about it being online though. So it really depends on what definition of single player you're after.

So you "might" have to revert to the cheat method, it depends on what you're trying to do. You should probably test it anyway just in case. :)

The purpose of this script is to detect if you're playing in mode "single player" AKA you can cheat or if you're playing on LAN or B.net.
13 years
edited 13 years
Code (jass) Select
library Detector initializer init

    globals
        public constant real DELAY = 0.3
        private boolean Is_multi = false
        private timer Tim
    endglobals
   
    // these 2 functions can only be used only when the timer has expired or after the TSA in the DoInit2 function
   
    function IsMulti takes nothing returns boolean
        return Is_multi
    endfunction
   
    function IsSolo takes nothing returns boolean
        return not Is_multi
    endfunction
   
    private function EndOfTimer takes nothing returns nothing
        set Is_multi = true
    endfunction
   
    private function DoInit2 takes nothing returns nothing
        local diag = DialogCreate()
        call DialogDisplay(GetLocalPlayer(),diag,true)
        call TriggerSleepAction(DELAY)
        call PauseTimer(Tim) // just in case
        call DestroyTimer(Tim)
        set Tim = null
        call DialogDestroy(diag)
        set diag = null
    endfunction

    private function DoInit takes nothing returns nothing
        call ExecuteFunc("DoInit2")
        call TimerStart(Tim,DELAY,false,function EndOfTimer)
    endfunction
   
    private function init takes nothing returns nothing
        set Tim = CreateTimer()
        call TimerStart(Tim,0,false,function DoInit)
    endfunction
   
endlibrary


This is just a proof of concept i don't know if it works, and yes it can be improved, i will eventually do it if it works and the ReloadGame... bug is confirmed on replays.

EDIT :

Or if Diod's way work 100 % i suppose it doesn't worth it, especially because it would be even more noticeable than his way.
Now maybe the select unit trick mentionned also works and then it would be much better because much faster, in fact could be used just after a Timer(0) and not really noticeable ...

Note, that i don't know what happens if there is a noticeable delay or not.

EDIT 2 :

And just to be clear, no i don't care to test it, and yes you're free to use any part, all, or nothing of this code without any credit required (ofc it's sthe same about the wc3c's post.

And btw Nestharus shouldn't be credited for the Reload... way, he has not dicovered it, even if yeah i suppose we are just re-discovering the same things again and again.
But here i'm just sure that someone told him.
13 years
never said it came from me

TriggerHappy was the first person I saw to post it, but it's really obvious given that gamecache doesn't work in single player ;p.
13 years
Updated, and as Trollbrain said, it will fail in replays. I've added a warning in the first post.
13 years
A warning. Good enough for me :p

Approved.