Scheduler

There are many ways and strategies of scheduling game turns and/or actors. The ROT.Scheduler API is meant as a general scheduling abstraction; there are also three distinct implementations for commonly used scheduling approaches.

All available schedulers share these common methods:

Simple scheduler

The easiest scheduler is called ROT.Scheduler.Simple – it rotates all items evenly in a round-robin fashion.

var scheduler = new ROT.Scheduler.Simple(); /* generate some actors */ for (var i=0;i<4;i++) { scheduler.add(i+1, true); /* true = recurring actor */ } /* simulate several turns */ var turns = []; for (var i=0;i<20;i++) { var current = scheduler.next(); turns.push(current); } SHOW("\nGenerated order of actors:"); SHOW(turns.join(" ") + " ...");

Speed scheduler

In a more realistic game, your actors might want to act based on their speed. Due to the turn-based nature of the game, the speed is more accurately described as a frequency in which turns happen for a particular actor. ROT.Scheduler.Speed plans turns based on this fact: your actors must implement the getSpeed() method, which returns the relative actor speed (an actor with speed=100 will get twice as many turns as actor with speed=50).

var scheduler = new ROT.Scheduler.Speed(); /* generate some actors */ for (var i=0;i<4;i++) { var actor = { speed: ROT.RNG.getPercentage(), number: i+1, getSpeed: function() { return this.speed; } } scheduler.add(actor, true); SHOW(ROT.Util.format("Object #%s has speed %s.", actor.number, actor.speed)); } /* simulate several turns */ var turns = []; for (var i=0;i<40;i++) { var current = scheduler.next(); turns.push(current.number); } SHOW("\nGenerated order of turns:"); SHOW(turns.join(" ") + " ...");

Action-duration scheduler

The most complex scheduling strategy expects individual actors to perform time-dependent actions. Every action takes a different amount of time, so the "speed" of an actor varies from action to action. Every time an actor is picked, the scheduler needs to be told about the current action's duration – to properly schedule a next turn for this actor.

To use the ROT.Scheduler.Action, make sure you call its setDuration() method after calling next(): this sets the delay for current actor's next turn.

Finally, you can specify the initial scheduling delay as an optional third argument to add(). The default value is "1".

var scheduler = new ROT.Scheduler.Action(); /* generate some actors */ for (var i=0;i<4;i++) { scheduler.add(i+1, true, i); /* last argument - initial delay */ } /* simulate several turns */ var template = "Actor %s performing action for %s time units (current time: %s)"; for (var i=0;i<20;i++) { var current = scheduler.next(); var actionDuration = Math.ceil(ROT.RNG.getUniform() * 20); scheduler.setDuration(actionDuration); var padded = actionDuration.toString().padStart(2, "0"); SHOW(ROT.Util.format(template, current, padded, scheduler.getTime())) }