Fractal Softworks Forum

Please login or register.

Login with username, password and session length
Advanced search  

News:

Starsector 0.97a is out! (02/02/24); New blog post: Simulator Enhancements (03/13/24)

Author Topic: Timer, timer...  (Read 3299 times)

xenoargh

  • Admiral
  • *****
  • Posts: 5078
  • naively breaking things!
    • View Profile
Timer, timer...
« on: March 17, 2014, 12:08:07 PM »

Hey, all... my brain is not working very well for Java today, I'm thinking in two other languages and a bunch of other stuff is rattling around my skull.

Basically, I want to build a timer, A, that will trigger a timer, B. 

While timer B is active, A is not active; while A is active, B is not active.  Timer A has a long cycle time, B has a short cycle time.

Pseudocode:

WHILE-->Engine is running
IF Timer B condition == FALSE, Advance Timer A, do not advance Timer B
IF Timer A interval has elapsed, trigger Timer B condition to TRUE
IF Timer B has not elapsed and timer A is not active, Do Stuff...
WHEN Timer B elapses, Timer B condition = FALSE

Does that make sense?  This is a concept I want to use for various randomized behaviors where the cycle-time of B is pretty constant or isn't even using a timer at all, in the SS sense (strict frame-counting, rather, to ensure complete operational cycles even if framerate drops) but where A is going to be random.
Logged
Please check out my SS projects :)
Xeno's Mod Pack

TJJ

  • Admiral
  • *****
  • Posts: 1905
    • View Profile
Re: Timer, timer...
« Reply #1 on: March 17, 2014, 12:29:20 PM »

It'd be far simpler if you organised it as states, not separate timers.

In the description above, it seems to me like you have two states; an ACTIVE state, and a SLEEP state.
ACTIVE state is a fixed duration, SLEEP state is a random duration.

so:

Code

final int ACTIVE_STATE_DURATION = 1000;
final int SLEEP_STATE_MIN_DURATION = 100;
final int SLEEP_STATE_MAX_DURATION = 1000;

final int ACTIVE_STATE = 0;
final int SLEEP_STATE = 1;

int state = SLEEP_STATE;
int nextStateChangeInMs = 0;

void advance(....) {
nextStateChangeInMs-=deltaTime;

if(nextStateChangeInMs<=0) {
   if(state==SLEEP_STATE) {
      nextStateChangeInMs = ACTIVE_STATE_DURATION;
      state = ACTIVE_STATE;
   }
   else { //
      nextStateChangeInMs = SLEEP_STATE_MIN_DURATION + (int)(Math.random() * (SLEEP_STATE_MAX_DURATION-SLEEP_STATE_MIN_DURATION));
      state = SLEEP_STATE
   }
}

}

Disclaimer, the above is off top of my head & is not intended to compile.

:edit:

Though I'd expect the engine to provide a robust callback mechanism for scheduling event triggers & the like... does it not? (Can you tell I've done almost zero SS modding  ;)).
« Last Edit: March 17, 2014, 12:32:53 PM by TJJ »
Logged

xenoargh

  • Admiral
  • *****
  • Posts: 5078
  • naively breaking things!
    • View Profile
Re: Timer, timer...
« Reply #2 on: March 17, 2014, 01:26:51 PM »

Event trigger timing is (usually) handled via IntervalUtil objects declared when the class is built (it doesn't have to be, blah blah).  

Example:
Code: java
private final IntervalUtil myInterval = new IntervalUtil(0.5f,2.5f);

advance(float amount){
  myInterval.advance(amount);

  if(myInterval.intervalElapsed()){
   //DO STUFF
  }
}
Anyhow, what I (think) I want is to use an IntervalUtil for the long random loop A, then an integer counter timer for B, so that A is loose in regards to time elapsed but B is strictly regulated by gameframe updates.  A is "game time", but B needs to be strict, as I'm planning on using it to regulate a graphics process, where time-sync is not important but having it "skip" frames might be an issue :)

Anyhow, yeah, I know it's not really a "hard" problem... brain is just not doing it today, lol.
Logged
Please check out my SS projects :)
Xeno's Mod Pack

silentstormpt

  • Admiral
  • *****
  • Posts: 1060
    • View Profile
Re: Timer, timer...
« Reply #3 on: March 17, 2014, 05:47:08 PM »

Unless you want a timer to stop at a certain time so it can be used again at that time, i dont see why not using a boolean and reset/re-use the same timer
Logged

dmaiski

  • Captain
  • ****
  • Posts: 422
  • resistance is futile
    • View Profile
Re: Timer, timer...
« Reply #4 on: March 28, 2014, 02:45:19 PM »

just off the cuff
Code: java
intervalUtil timer1 ()
intervalUtil timer2 ()

boolean switch1 = true

if(engine.isPaused) {
    return
    }

if (switch1) {
    timer1.advance(amount)

    if (timer1.Elapsed) {
        switch1=false

        //code on timer1 end
        return
        }

    //code while timer1 is working

    }

else {
    timer2.advance(amount)

    if (timer2.Elapsed) {
        switch1=true

        //code on timer2 end
        return
    }

    //code while timer2 is working

}


just off the cuff quick code that should do what you want (look at the api for details, im writink it from memory using my phones web browser...)

*edit* improved it a bit
« Last Edit: March 29, 2014, 05:05:40 AM by dmaiski »
Logged
BISO
(WIP) lots of shiny new weapons ( :-[ i have more weapons then sprites :-[ )

i got a cat pad
its like a mouse pad but better!