AI NPCs

With the server-side function Server.openai(messages: array of objects [, model]) you can contact ChatGPT to answer user messages or calculate NPC behaviour. Messages is an array of {role:string, content:string}. The call only works if an openai key is configured for the server.

Here is a simple example of pirate which answers the user:

async function onPlayerSays(player) {
    if (player.y <= this.y || player.dir != 0 ||
            !player.chat || player.chat.length < 3 ||
            this.chat == "...")
        return;

    this.chat = "...";
    let response = await Server.openai([
        { role: 'system', content: 'You are a happy 40 years old medieval pirate with the name Buckley and answer all questions related to your mood, pirates and fighting, but just sing a song if asked something else. Short answers only. You are not an artificial intelligence' },
        { role: 'user', content: player.chat }
    ]);
    this.say(response, response.length >= 100? 20 : response.length >= 30? 15 : 10);
}