[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  SMAUG
. -> [Folder]  Lua
. . -> [Subject]  LUA Handlers

LUA Handlers

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Muthasys   (3 posts)  [Biography] bio
Date Sun 12 Dec 2021 11:22 AM (UTC)
Message
Hi,

I have read the messages on how to add LUA to the smaugfuss (it works) but some things are not clear to me probably due to my little knowledge of LUA.

When the mud starts, the "startup_mud.lua" file is loaded (which loads tprint.lua, utilities.lua, reset.lua).

When any PC comes into play, the "startup.lua" file will be loaded for it (which loads tprint, utilities, tasks, tasklist, taskevents, taskhints, whereis, whereis_destination).

What I don't understand now is how future additions should be structured. I'll explain.

In task.lua, for example, Nick has inserted a "looking" function that is activated when the PC is looking and certain conditions are validated.

function looking (vnum)
 - need to know about this player
 char_info = mud.character_info ()

  - we need to know if quest-giver or quest-receiver is here
  find_mobs_in_room ()
<cut>
end


This function trig as the following appears in act_info.c:

call_lua (ch, "looking", NULL);



If I add a "test.lua" file with a second function that must be activated by "look":

function looking (arg)
send ("looking funcion 2")
end


... how should I go about getting both of them working?

I would like to understand how the handler array works and how it is used:

- install handlers into handlers table AFTER this code
- (this code below creates the handlers table)
for _, name in ipairs
{
  "new_player", "entered_game",
  "speech",
<cut>


In "task.lua" I notice that Nick has entered:

table.insert (handlers.looking, looking) - check for tasks in this room


I think he add "handlers.looking" table the element "looking".

Just to try I tried to put in "test.lua":

table.insert (handlers.looking, looking_2)


and then use:

function looking_2 (arg)
send ("looking funcion 2")
end


But of course it didn't work.

They will be banalities but I didn't understand how to work on the LUA side.

Thanks!
Bye
[Go to top] top

Posted by Muthasys   (3 posts)  [Biography] bio
Date Reply #1 on Sun 12 Dec 2021 04:32 PM (UTC)
Message
I think I figured it out, but confirmation would help.

Inside "startup.lua" file we have:

for _, name in ipairs {
  "new_player",
  "entered_game",
  "speech",
<cut>


These are the nominal references of the "hooks" present in the SmaugFuss C code (ex: call_lua (ch, "looking", NULL);).

If we added on MUD side a new "hook", for example inside violence_update function (call_lua(ch, "violence_update", NULL);) ... we should add "violence_update" to that list.

A new test.lua modules to be added must have the form:

module (..., package.seeall)

function violence_update()
print("TEST: violence_update event")
end

table.insert (handlers.violence_update, violence_update)



while in startup.lua one will put the debt require:

test = require ("test")


I don't think there is anything else to do. It's correct?

In the next message I report for example how I did the insertion of the "wait.lua" module.

Bye
[Go to top] top

Posted by Muthasys   (3 posts)  [Biography] bio
Date Reply #2 on Sun 12 Dec 2021 05:07 PM (UTC)

Amended on Sun 12 Dec 2021 10:53 PM (UTC) by Muthasys

Message
This is my "wait.lua" file:

module (..., package.seeall)

local threads = {}
function violence_update ()
  -- for each active thread, see if the time is up
  for k, v in pairs (threads) do
    if os.time () >= v then
      threads [k] = nil  -- delete from table now
      assert (coroutine.resume (k))
    end
  end
end

function wpause (seconds)
  threads [assert (coroutine.running (), "Must be in coroutine")] = os.time () + (seconds or 1)
  return coroutine.yield ()
end

function make (f)
  assert (type (f) == "function", "wait.make requires a function")
  coroutine.wrap (f) () -- make coroutine, resume it
end -- make

-- violence_update handler insert
table.insert (handlers.violence_update, violence_update)


Inside startup.lua add "violence_update" and require:


for _, name in ipairs {
  "new_player", 
  "entered_game",
  "speech",
  "entered_room",
  "killed_mob",
  "got_object",
  "lost_object",
  "looking",
  "char_update",
  "act",
  "give",
  "bribe",
  "buy",
  "wear",
  "drop",
  "repair",
  "advance_level",
  "use",
  "violence_update" -- <----------- HERE
  } do
  _G [name] = MakeHandler (name)  -- make a function handler
  handlers [name] = {}  -- make empty handlers table
end -- for loop

pause = require("wait").wpause   -- insert and rename wait.wpause(vaue) to pause(value)


Once this is done we could write a function like:

function function_with_pause()
print( "Function_with_pause: start" )
      wait.make( function ()
      pause(3)
      print( "MSG part1" )
      pause(3)
      print( "MSG part2" )
      end
      )
print( "Function_with_pause: end" )
end


I point out two completely independent things.

(1) I believe it is necessary to take care of the player_name.lua files when a character is deleted or renamed. A new character with the same name could access the old character's .lua data.

(2) for EXT_BV in lua_scripting.c it seems to me it works to do for example:

#define EXIT_EXT_BV_ITEM (arg) \
  lua_pushstring (L, print_bitvector (& pexit-> arg)); \
  lua_setfield (L, -2, #arg)


...so you can then use: EXIT_EXT_BV_ITEM (exit_info);
[Go to top] top

Posted by Nick Gammon   Australia  (23,001 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Sun 12 Dec 2021 10:11 PM (UTC)
Message

This thread is referring to the concepts described here.


- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (23,001 posts)  [Biography] bio   Forum Administrator
Date Reply #4 on Sun 12 Dec 2021 10:22 PM (UTC)
Message
It's been almost 10 years since I wrote that, so I don't totally remember how it works.

Judging by what you posted, the loop you mention is just setting up empty handlers for all the hooks in the SMAUG code.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


8,910 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]