• 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.

Implementing a local global wait

Status
Not open for further replies.
Level 15
Joined
Aug 7, 2013
Messages
1,338
Hi,

Suppose people enter a room in random order. After entering the room, the person cannot leave.

The first person who enters the room starts a timer for N seconds.

Everyone else who enters the room afterwards has to wait whatever time is remaining on the first person's timer.

When the first person's timer finishes, the door to the room locks, and nobody can enter.

Here is my simple implementation of this "local" global wait (local since it's only true for one room--there could be many rooms with this property). Are there any problems with my set up?

JASS:
		local timer t
		local timer globalTimer
		if timerTable.timer[this] == null then
			set timerTable.timer[this] = CreateTimer()
			set globalTimer = timerTable.timer[this]
			set eventHandleTable[GetHandleId(globalTimer)] = this
			set eventHandleTable.real[GetHandleId(globalTimer)] = pid
			call TimerStart(globalTimer, duration, false, function after)
		else //someone's already started this global timer
			set t = CreateTimer()
			set eventHandleTable[GetHandleId(t)] = this
			set eventHandleTable.real[GetHandleId(t)] = pid
			call TimerStart(t, TimerGetRemaining(timerTable.timer[this]), false, function EventGlobalWait.afterWait)
		endif
 
I just might be dumb but.. my mind can't contain whole description in such a way to understand it :)

Lets assume that is goes as follows:

-we have a random room A (entered via some region in other part of map)
-when random dude enters A, he triggers a timer B (like a bomb' one). After expiration of such timer.. BOOM. Door is locked forvever and none can enter A.
-however, as long as B is expiring other folks are free to enter the room.
-their enter-event is omited, afterall B has already been triggered

Considering those we got:
JASS:
globals
    private timer clock = CreateTimer()
    private boolean enables = true
    private real duration = 604800
endglobals

// pseudo:
private function onCallback takes nothing returns nothing
    call PauseTimer(clock)
    set enabled = false
    // BOOM - total devastation and fireworks
endfunction

private function onEvent takes nothing returns nothing
    if enabled then
        if ( TimerGetElapsed(clock) == 0 ) then
            call TimerStart(clock, duration, false, function onCallback)
        endif
        // move folk into room area
    endif
endfunction

// private onInit takes .. returns
//     register enter event and connect it with out execution code
// end..
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,255
Since both t and globalTimer are not used at the same time it might be good to merge them into the same variables.

In any case I do not even see the need for complex handle Id related things for this task.

You define each "room" as a structure instance with a timer. When anyone enters the room and the timer is not running it starts the timer. If you want to keep track of anyone who is in the room, use a group to hold their units or a force to hold the players. If you want to keep handle overhead down then break instances apart so that the timer and any groups are created only when someone enters and starts it and is destroyed when it expires. It really is that simple.

The only time handle related stuff may be required is to determine which room was entered and from the timer which room it represented.
 
Status
Not open for further replies.
Top