Elevator Saga Help and API Documentation

About the game

This is a game of programming!
Your task is to program the movement of elevators, by writing a program in JavaScript.
This version is based on destination dispatch mode.

The goal is to transport people in an efficient manner. Depending on how well you do it, you can progress through the ever more difficult challenges. Only the very best programs will be able to complete all the challenges.

How to play

Enter your code in the input window below the game view, and press the Apply button to start the challenge. Press Apply with right button to simulate the code with some presets. You can increase or decrease the speed of time by pressing the and buttons. You can create custom challenges with your own options. To get current option, right press the button on the right of the challenge.

If your program contains an error, you can use the developer tools in your web browser to try and debug it. If you want to start over with the code, press the Reset button. This will revert the code to a simplistic implementation. If you have a favorite text editor, such as Sublime Text, feel free to edit the code there and paste it into the game editor. Your code is automatically saved in your local storage by default, right click Save button to toggle.

Basics

Your code must declare an object containing at least two functions called init and update. Like this:

{
    init: function(elevators, floors) {
        // Do stuff with the elevators and floors, which are both arrays of objects
    },
    update: function(dt, elevators, floors) {
        // Do more stuff with the elevators and floors
        // dt is the number of game seconds that passed since the last time update was called
    }
}

These functions will then be called by the game during the challenge. init will be called when the challenge starts, and update repeatedly during the challenge.

Normally you will put most of your code in the init function, to set up event listeners and logic.

Code examples

How to control an elevator

elevator.goToFloor(1);
Tell the elevator to move to floor 1 immediately, interrupting current task.
if(elevator.currentFloor > 2) { ... }
CurrentFloor gets the floor number that the elevator currently is on. Note that this is a rounded number and does not necessarily mean the elevator is in a stopped state.

Listening for events

It is possible to listen for events, like when stopping at a floor, or a button has been pressed.

elevator.on("idle", function() { elevator.goToFloor(0); });
Listen for the "idle" event issued by the elevator, when the task queue has been emptied and the elevator is doing nothing. In this example we tell it to move to floor 0.
elevator.on("floor_button_pressed", function(floorNum) { ... } );
Listen for the "floor_button_pressed" event, issued when a passenger pressed a button inside the elevator. This indicates that the passenger wants to go to that floor.
floor.on("call_button_pressed", function(destinationFloor) { ... } );
Listen for the "call_button_pressed" event, issued when a passenger pressed the call button on the floor they are waiting on. This indicates that the passenger wants to go to another floor.

API documentation

Elevator object

PropertyTypeExplanationExample
goToFloorFunctionRequest the elevator to go to specified floor number directly, and then go to any other queued floors.
elevator.goToFloor(3); // Do it before anything else
stopFunctionClear the destination queue and stop the elevator if it is moving. Note that you normally don't need to stop elevators - it is intended for advanced solutions with in-transit rescheduling logic. Also, note that the elevator will probably not stop at a floor, so passengers will not get out.
elevator.stop();
currentFloorNumberGet the (rounded) floor number that the elevator currently is on.
if(elevator.currentFloor === 0) {
    // Do something special?
}
getExactCurrentFloorFunctionGet exact floor number that the elevator currently is on.
if(elevator.getExactCurrentFloor() === 0) {
    // This elevator is probably stopped at floor 0
}
getExactFutureFloorIfStoppedFunctionGet exact floor number that the elevator will stop at if decelerates. Useful for fast moving elevators.
if(elevator.velocityY > 0 && elevator.getExactFutureFloorIfStopped() < 1) {
    // This elevator is suitable for stopping at floor 1
}
isApproachingFloorFunctionCheck if the elevator is moving closer to specified floor.
if(elevator.isApproachingFloor(0)) {
    // Do something special?
}
directionalIndicatorsBoolean arrayGet or set the going up indicator (index 0) or going down indicator (index 1), which will affect passenger behaviour when stopping at floors. Note that indicator changes are visually updated only when the elevator starts or stops.
if(elevator.directionalIndicators[0]) {
    elevator.directionalIndicators[1] = false;
}
serviceStatesBoolean arrayGet or set serviced floors in one-to-one correspondence. Passenger will enter the elevator only when the corresponding flag is true.
if(elevator.serviceStates[0]) {
    // This elevator is suitable for passengers going to floor 0
}
maxUsersNumberGet the maximum number of passengers that can occupy the elevator at the same time.
if(elevator.maxUsers >= 8) {
    // Use this elevator for something special, because it's big
}
loadFactorFunctionGet the load factor of the elevator. 0 means empty, 1 means full. Varies with passenger weights, which vary - not an exact measure. Note that sometimes the elevator may be overloaded - it does not matter.
if(elevator.loadFactor() < 0.5) {
    // Maybe use this elevator, since it's less than half full yet?
}
isFull
isEmpty
FunctionDetect if the elevator is full (empty) more simply.
if(elevator.isEmpty()) {
    // Equivalent to elevator.loadFactor() === 0
}
getMaxSpeedFunctionGet the maximum speed (floors per second) of the elevator.
if(elevator.getMaxSpeed() > 5) {
    // This elevator is rather fast
}
velocityYNumberGet the velocity of the elevator. A positive number means going up, and a negative one means going down.-
destinationQueueNumber arrayThe current destination queue, meaning the floor numbers the elevator is scheduled to go to. Can be modified and emptied if desired. Note that you need to call checkDestinationQueue() for the change to take effect immediately.
//C-LOOK algorithm
elevator.destinationQueue.sort();
elevator.checkDestinationQueue();
checkDestinationQueueFunctionCheck the destination queue for any new destinations to go to. Note that you only need to call this if you modify the destination queue explicitly.
getPressedFloorsFunctionGet the currently pressed floor numbers as an array sorted ascendingly.
if(elevator.getPressedFloors().length > 0) {
    // Maybe go to some chosen floor first?
}
pressFloorButtonFunctionPress the button of specified floor number. Note that you normally don't need to manually press buttons - it is needed only for testing.
elevator.pressFloorButton(0);
// Should trigger floor_button_pressed event for valid pressing
EventExplanationExample
idleTriggered when the elevator has completed all its tasks and is not doing anything.
elevator.on("idle", function() { ... });
floor_button_pressedTriggered when a passenger has pressed a button inside the elevator.
elevator.on("floor_button_pressed", function(floorNum) {
    // Maybe tell the elevator to go to that floor?
})
passing_floorTriggered slightly before the elevator will pass a floor. A good time to decide whether to stop at that floor. Note that this event is not triggered for the destination floor. Direction is the floor being passed subtracts current floor.
elevator.on("passing_floor", function(floorNum, direction) { ... });
stopped_at_floorTriggered when the elevator has arrived at a floor.
elevator.on("stopped_at_floor", function(floorNum) {
    // Maybe decide where to go next?
})

Floor object

PropertyTypeExplanationExample
levelNumberGet the floor number of the floor object.
if(floor.level > 3) { ... }
buttonStatesBoolean arrayGet call button states, in one-to-one correspondence, of specified floor.
if(floor.buttonStates[0]) {
    // Maybe call an elevator to pick up, then go to floor 0?
}
pressButtonFunctionPress the specified call button on the floor. Note that you normally don't need to manually press buttons - it is needed only for testing.
floor.pressButton(1);
// Should trigger call_button_pressed event for valid pressing
EventExplanationExample
call_button_pressedTriggered when someone has pressed a (destination) call button at a floor. Note that passengers will press the button again if they fail to enter an elevator.
floor.on("call_button_pressed", function(destinationFloor) {
    // Maybe call an elevator to go to this floor, then go to destination floor?
})