Skip to content

lua npc scripts

mtanksl edited this page Jun 23, 2024 · 9 revisions

Introduction

You can use Lua scripts to extend any funcionality you want.

Example

Let's attach a new Behaviour to all the npcs.

public class NpcScript : GameObjectScript<Npc>
{
    public override void Start(Npc npc)
    {
        DialoguePlugin dialoguePlugin = Context.Server.Plugins.GetDialoguePlugin(npc.Name);

        if (dialoguePlugin != null)
        {
            Context.Server.GameObjectComponents.AddComponent(npc, new NpcThinkBehaviour(dialoguePlugin, new RandomWalkStrategy(2) ) );
        }
    }

    public override void Stop(Npc npc)
    {

    }
}

Edit the file \mtanksl.OpenTibia.GameData\data\gameobjectscripts\config.lua. If the npc's name is not found in the registration list, the empty name is used instead.

gameobjectscripts = {
    ...
    npcs = {
        { name = "", filename = "OpenTibia.Game.GameObjectScripts.NpcScript" },
        ...
    },
    ...
}

The Context.Server.Plugins.GetDialoguePlugin initializes the LuaScriptingDialoguePlugin that loads \mtanksl.OpenTibia.GameData\data\lib.lua, \mtanksl.OpenTibia.GameData\data\plugins\lib.lua, \mtanksl.OpenTibia.GameData\data\plugins\npcs\lib.lua and \mtanksl.OpenTibia.GameData\data\plugins\npcs\{fileName}.lua files.

local say = topic:new()
say:add("name", "My name is {npcname}.")

local handler = npchandler:new( {
    greet = "Hello {playername}.",
    busy = "I'll talk to you soon {playername}.",
    say = say,
    farewell = "Bye {playername}.",
    disappear = "Bye."
} )

function shouldgreet(npc, player, message) return handler:shouldgreet(npc, player, message) end
function shouldfarewell(npc, player, message) return handler:shouldfarewell(npc, player, message) end
function ongreet(npc, player) handler:ongreet(npc, player) end
function onbusy(npc, player) handler:onbusy(npc, player) end
function onsay(npc, player, message) handler:onsay(npc, player, message) end
function onbuy(npc, player, item, type, count, price, ignoreCapacity, buyWithBackpacks) handler:onbuy(npc, player, item, type, count, price, ignoreCapacity, buyWithBackpacks) end
function onsell(npc, player, item, type, count, price, keepEquipped) handler:onsell(npc, player, item, type, count, price, keepEquipped) end
function onclosenpctrade(npc, player) handler:onclosenpctrade(npc, player) end
function onfarewell(npc, player) handler:onfarewell(npc, player) end
function ondisappear(npc, player) handler:ondisappear(npc, player) end
function onenqueue(npc, player) handler:onenqueue(npc, player) end
function ondequeue(npc, player) handler:ondequeue(npc, player) end

The NpcThinkBehaviour listen to the Player Say Event and dispatches the appropriate events to the script.

The script than processes the message and calls back the server with the appropriate actions using the command. proxy.

See Plugin for more examples.