• 🏆 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!

Modes mechanism

Status
Not open for further replies.
Level 5
Joined
Nov 27, 2007
Messages
85
Hey, recently I have added several modes to my map.
I wonder if there is a library or any sort of mechanism that includes following key mode features:


1. Initiation voting.
To enable a mode, one player writes "-mode_name" to start a voting to enable that mode.

2. Filtering votes.
Only the votes of players that meet specified requirements are counted.

3. Minimum number of votes needed (to enable a mode).
I used (k/2 + 1) where k is the number of players that can vote.

4. Mode initiating conditions that must be true for a mode to be enabled

5. (custom) "CanBeDisabled" flag for modes which means it is possible to disable that mode (similar to how it was enabled, via voting)

6. Minimum Runtime for modes - speaks for itself, so the mode isn't disabled 10 seconds after enabling.

Example:
1. Player writes "-dummy", voting for dummy mode starts (lasts 30 sec)
2. 3/5 players vote for dummy mode by writing "-dummy", when the 3rd one votes the mode is autoenabled.
3. After 10 seconds another player attempts to run disable voting by writing "-dummy". He fails (i.e. nothing happens) because the minimum runtime of that mode is 2 minutes.
4. After 2 minutes a player starts disable voting, 2/5 people what for disabling by writing "-dummy" but it's not enough for it to be disabled. Dummy for life!


Personally I made something like a "base mode" for all of my other modes but it's problematic to change it. (I have to modify all exisitng modes to include a new mode feature.)
I've thought of a solution to avoid it, it's something with using game cache (I don't know hashtables) for better abstraction & unification.
I haven't implemented this solution yet - it looks like there is a lot of work to do. So I'm just checking if someone has already made "Advanced Customizable Mode System" or something.


JASS:
library ModesLib
    globals    
        constant integer MODE_STATUS_DISABLED       = 1
        constant integer MODE_STATUS_ENABLED        = 2
        constant integer MODE_STATUS_VOTING         = 3
        
        constant integer MODE_ACTION_DO_NOTHING     = 0
        constant integer MODE_ACTION_ENABLE         = 1
        constant integer MODE_ACTION_DISABLE        = 2
        constant integer MODE_ACTION_VOTE_YES       = 3
        constant integer MODE_ACTION_VOTE_NO        = 4
        
        constant integer MODE_VOTING_TIME_STANDARD  = 30
    endglobals
    
    public function StandardInitiatingConditions takes nothing returns boolean
        bool b1 = udg_RoundCheck >= 0 and udg_RoundCheck <= 2
        bool b2 = true
        bool b3 = true
        bool b4 = true

        return b1 and b2 and b3 and b4
    endfunction
    
    public function StandardVotingConditions takes player p returns boolean
        bool b1 = IsPlayerStatePlaying(p)
        bool b2 = IsControllerUser(p)
        bool b3 = true
        //bool b3 = not IsPlayerInForce(p, Observers)
        bool b4 = true
        
        return b1 and b2 and b3 and b4
    endfunction
    
    private function GetAllVotesFilter takes nothing returns boolean
        return StandardVotingConditions(GetFilterPlayer())
    endfunction

    public function GetAllVotesStandard takes nothing returns int
        force f = GetPlayersMatching(Condition(function GetAllVotesFilter))
        int n = CountPlayersInForceBJ(f)
        
        return n
    endfunction
    
    function IsModeEnabled takes int status returns boolean
        return status == MODE_STATUS_ENABLED
    endfunction
    
    function IsModeDisabled takes int status returns boolean
        return status == MODE_STATUS_DISABLED
    endfunction
    
    function IsModeInVoting takes int status returns boolean
        return status == MODE_STATUS_VOTING
    endfunction
    
    public function EnableIfPossibleAbstracted takes bool EarlyRun, bool InitConditions, \
                                int votes_yes, int votes_needed, int votes_all returns int // status_new
        
        int mode = MODE_STATUS_VOTING
        string output = I2S(votes_yes)+"/"+I2S(votes_all)+": "
        
        if (votes_yes >= votes_needed) && InitConditions
            set mode = MODE_STATUS_ENABLED // !
            output += "enabled."
            echo(output)
        endif
        
        if EarlyRun || IsModeEnabled(mode)
            return mode
        endif
        
    // Final actions [error msg, no enable]
        if not InitConditions
            output += "mode cannot be enabled."
        elseif votes_yes < votes_needed
            output += "not enough votes ("+I2S(votes_needed)+")."
        endif
        
        set mode = MODE_STATUS_DISABLED // !
        echo(output)
        
        return mode
    endfunction
endlibrary



[JASS="Dummy Mode"]
globals
int Mode_Dummy = MODE_STATUS_DISABLED
force Mode_Dummy_Voters = CreateForce()
constant integer Mode_Dummy_VotingTime = MODE_VOTING_TIME_STANDARD

string Mode_Dummy_MsgOnEnable = "Now you can do specific dummy stuff."
string Mode_Dummy_VotingStartMsg = "Voting for Dummy Mode started. Type |cffffcc00-dummy|r to vote."

timer Mode_Dummy_Runtime_Timer = CreateTimer()
endglobals

function Trig_Mode_Dummy_ActionsOnEnable takes nothing returns nothing
// do specific mode stuff
endfunction

function Trig_Mode_Dummy_GetNeededVotes takes int votes_all returns int
if votes_all <= 2
return votes_all
endif

int k = votes_all/2
return k + 1
endfunction

function Trig_Mode_Dummy_CustomInitiatingConditions takes nothing returns boolean
bool b1 = true
bool b2 = true
bool b3 = true
bool b4 = true

return b1 and b2 and b3 and b4
endfunction

function Trig_Mode_Dummy_CustomVotingConditions takes player p returns boolean
bool b1 = true
bool b2 = true
bool b3 = true
bool b4 = true
//bool b5 = not IsPlayerInForce(p, Observers)

return b1 and b2 and b3 and b4 //and b5
endfunction

function Trig_Mode_Dummy_InitiatingConditions takes nothing returns boolean
if ModesLib_StandardInitiatingConditions() && Trig_Mode_Dummy_CustomInitiatingConditions()
return true
endif

return false
endfunction

function Trig_Mode_Dummy_VotingConditions takes player p returns boolean
if ModesLib_StandardVotingConditions(p) && Trig_Mode_Dummy_CustomVotingConditions(p)
return true
endif

return false
endfunction

function Trig_Mode_Dummy_GetYesVotes takes nothing returns integer
return CountPlayersInForceBJ(Mode_Dummy_Voters)
endfunction

function Trig_Mode_Dummy_GetAllVotesFilter takes nothing returns boolean
return Trig_Mode_Dummy_VotingConditions(GetFilterPlayer())
endfunction

function Trig_Mode_Dummy_GetAllVotes takes nothing returns integer
force f = GetPlayersMatching(Condition(function Trig_Mode_Dummy_GetAllVotesFilter))
int n = CountPlayersInForceBJ(f)

return n
endfunction

function Trig_Mode_Dummy_EnableIfPossible takes boolean EarlyRun returns nothing
int votes_all = Trig_Mode_Dummy_GetAllVotes()
int votes_needed = Trig_Mode_Dummy_GetNeededVotes(votes_all)
int votes_yes = Trig_Mode_Dummy_GetYesVotes()

bool InitConditions = Trig_Mode_Dummy_InitiatingConditions()

set Mode_Dummy = ModesLib_EnableIfPossibleAbstracted( EarlyRun, InitConditions, \
votes_yes, votes_needed, votes_all)

if IsModeEnabled(Mode_Dummy)
echo(Mode_Dummy_MsgOnEnable)
call Trig_Mode_Dummy_ActionsOnEnable()
// Do not clear force here
return
endif
endfunction

function Trig_Mode_Dummy_Actions takes nothing returns nothing
player p = GetTriggerPlayer()
int mode

if not EventStringExactMatch() || Mode_Dummy == MODE_STATUS_ENABLED
return
endif

if Mode_Dummy == MODE_STATUS_VOTING then
set mode = MODE_ACTION_VOTE_YES

elseif Mode_Dummy == MODE_STATUS_DISABLED then
set mode = MODE_ACTION_ENABLE
else
return
endif

if mode == MODE_ACTION_VOTE_YES
// add to force & check conditions, func
// if enough ppl disable voting and enable mode

if Trig_Mode_Dummy_VotingConditions(p) then
ForceAddPlayer(Mode_Dummy_Voters ,p)
pecho(p, "Vote counted.")
endif

Trig_Mode_Dummy_EnableIfPossible(true)

elseif mode == MODE_ACTION_ENABLE
// if can be enabled, initiate a voting

if not Trig_Mode_Dummy_InitiatingConditions()
return
endif

set Mode_Dummy = MODE_STATUS_VOTING
echo(Mode_Dummy_VotingStartMsg)

if InTesting
Sleep(5)
else
call Sleep(Mode_Dummy_VotingTime)
endif

if Mode_Dummy != MODE_STATUS_ENABLED then
Trig_Mode_Dummy_EnableIfPossible(false)
endif

call ForceClear(Mode_Dummy_Voters)
endif

endfunction

//===========================================================================
function InitTrig_Mode_Dummy takes nothing returns nothing
set gg_trg_Mode_Dummy = CreateTrigger( )
call TriggerRegisterAnyPlayerChatEvent( gg_trg_Mode_Dummy, "-dummy", false )
call TriggerAddAction( gg_trg_Mode_Dummy, function Trig_Mode_Dummy_Actions )
endfunction


[/code]
 
Status
Not open for further replies.
Top