Day-Night

The day-night effect, also called day-night-cycle, is changing the color and brightness of the game to simulate day and night: at night everything is darker and lamps are lightened up.

In the game this is configured with the ambient variables:

To change the colors throught the day, an NPC is calculating the new ambient color each few seconds and sends it to all players and also to players who just logged in:

NPC Daynight example:

function onUpdated() {
    this.onCreated();
}
function onCreated() {
    this.name = "";
    this.name = "DaynightControl"
    this.dayLength = 4*3600;
    this.dayOffset = 0;
    if (!this.ambient)
        this.ambient = [1,1,1];
    this.times = [
        {hour: 0,   ambient: [0.4,  0.4,  0.8]},
        {hour: 5,   ambient: [0.45, 0.45, 0.8]},
        {hour: 6,   ambient: [0.55, 0.5,  0.8]},
        {hour: 7,   ambient: [0.8,  0.7,  0.8]},
        {hour: 8,   ambient: [1,    1,    1]},
        {hour:16,   ambient: [0.95, 1,    1]},
        {hour:17,   ambient: [0.8,  0.9,  1]},
        {hour:18,   ambient: [0.65, 0.65, 0.95]},
        {hour:19,   ambient: [0.6,  0.6,  0.9]},
        {hour:20,   ambient: [0.5,  0.5,  0.8]},
        {hour:21,   ambient: [0.4,  0.4,  0.8]}
    ];
    this.onTimeout();
}
function onTimeout() {
    this.calculateAmbientColor();
    this.settimeout(5);
}
function calculateAmbientColor() {
    let currentTime = ((new Date().getTime() / 1000 + this.dayOffset) % (24*3600)) % this.dayLength;
    let hour = Math.floor(48 * currentTime / this.dayLength) / 2;
    this.ambient = [1,1,1];
    for (let i = 0; i < this.times.length; i++) {
        if (hour >= this.times[i].hour)
            this.ambient = this.times[i].ambient;
        else
            break;
    }
    Server.setambient(this.ambient[0], this.ambient[1], this.ambient[2]);
    Server.hour = Math.floor(hour); // for other scripts
}
function onPlayerLogin(pl) {
    // Called from player class
    pl.setambient(this.ambient[0], this.ambient[1], this.ambient[2]);
}

players class: In order to keep the day-night script running, we need to trigger the script for the player when they login, using the player.js file

function onPlayerLogin(pl) {
    let npc = Server.searchnpcs({name:"DaynightControl"})[0];
    npc.scheduleevent(0, "playerlogin", pl);
}