Manual:DIL Manual/Messages

From DikuMUD Wiki
< Manual:DIL Manual
Revision as of 23:54, 14 November 2026 by Agent-mud (talk | contribs) (rewritten against the compiler and interpreter; examples compile)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

A DIL program sleeps until something happens near it: a command typed, a command done, a blow struck, a death, a message from another program, or its own timer. The server calls each of these an event (also message), and wakes the programs that wait for that kind.

The kinds of event

A program names the kinds it wants in wait() or interrupt(), with the SFB_ constants of vme/include/vme.h joined by |: wait(SFB_CMD | SFB_MSG, ...).

Constant When Can be blocked
SFB_CMD A character near the unit types a command, before it runs. Also moves, unknown words, and commands given with exec(). yes: the command doesn't run
SFB_DONE A command has been carried out: something given, taken, dropped, a move, a spell cast, a room entered (CMD_AUTO_ENTER). no, it has happened
SFB_PRE A spell is about to take effect, or damage is about to be dealt (CMD_AUTO_DAMAGE). power can be changed. yes: it doesn't happen
SFB_TICK The program's own timer, every heartbeat. SFB_RANTIME added makes the time vary. no
SFB_MSG Another program sent a message. only from the programs after it
SFB_COM A character near the unit makes a melee attack. no
SFB_DEAD A character near the unit dies (also the unit itself). no
SFB_EDIT A player closes the editor started with beginedit().
SFB_PROMPT The player's prompt is being made.

pause is wait(SFB_TICK | SFB_RANTIME, TRUE). SFB_SAVE and SFB_ACTIVATE work only in interrupt() (the second is what on_activation() uses).

What the program sees

When an event wakes a program, these built-in variables describe it:

Variable Holds
activator the character who caused it: who typed the command, who attacked or died, which unit sent the message; null for a tick
argument the rest of the command line, or the message's text
medium what was used, for some commands and spells: the item given, the weapon, the scroll
target who or what the command was done to
power a number: the amount of money given, the damage about to be dealt, the strength of a spell
cmdstr, excmdstr, excmdstr_case the command's name, the word as typed in lower case, and exactly as typed

command("give") is true when the event comes from that command; automatic events have numbers, such as command(CMD_AUTO_ENTER), command(CMD_AUTO_TICK) or command(CMD_AUTO_DAMAGE). What each value holds for each event is on the event's page; for SFB_DONE it is set by the command, in its call to send_done().

These values belong to the event that woke the program, and the next wait replaces them. At the same wake every unitptr and extraptr variable that isn't secured becomes null. So the first statements after a wait copy what the program needs:

wait(SFB_CMD, command("say") and (activator.type == UNIT_ST_PC));
pc := activator;
said := argument;

Who gets an event

  • Commands, done, pre, combat and death events go to the units around the character who caused them: first the room's programs, then the things the character carries and wears, then the other units in the room (the characters, what they wear but not what they carry, and what is in open transparent units). A target somewhere else gets it too. See SFB_CMD.
  • A tick goes only to the program whose timer it is.
  • Messages go where the sender sends them (below).
  • A program doesn't hear what its own unit does: when the activator is the program's own unit, the event passes it by. A program on a player doesn't see what the player types, and an NPC's program doesn't see the NPC's own exec(). Declare the template aware (dilbegin aware name();) to get those events. A unit's own death, and messages sent to it with sendto(), arrive without aware.

Every event of a kind the program waits for wakes it, also one its condition then turns down, so a wide wait near a busy place runs often. Keep the condition short. While a program is running (it sent a message and the receivers haven't finished, say) it can't be woken, and events are not queued: what comes while a program waits for something else, or is busy, is lost to it.

The order of programs on a unit

A unit can run several programs. They get each event one after another, in the order of their fnpri, lowest number first (FN_PRI_BLOCK 25 before the default FN_PRI_CHORES 40); among equals the oldest first.

  • block stops the event: the command doesn't run, or the spell or damage doesn't happen, and no program after this one, on this unit or on the units after it, gets the event. A program that should stop a command before another program acts on it needs the lower fnpri.
  • priority keeps the programs after this one on the same unit from getting any event at all, until nopriority.
  • on_activation(condition, skip) makes a program ignore every event while the condition holds; the other programs still get them.

Sending messages

Programs talk to each other with messages: a text that wakes the programs waiting for SFB_MSG, with the text in argument and the sending unit in activator.

Procedure Reaches
send(msg) the programs around the sender's unit, as a command does; the sender's own other programs only when they are aware
sendto(msg, u) every program on the unit u, wherever it is, the sender's own unit included
sendtoall(msg, "sym@zone") the programs on every unit in the game made from that definition; medium is the sender too
sendtoalldil(msg, "tmpl@zone") every running copy of that template in the game; target is the receiver's own unit
  • The receivers run at once, before the send returns. The sending program itself never gets its own message.
  • A program that isn't waiting for SFB_MSG at that moment misses it. To catch messages while waiting for something else, use interrupt(SFB_MSG, condition, label).
  • After the send, the sender's secures are tested: a secured unit that a receiver moved away sends the sender to its label.
  • Messages are plain text. Agree on the words, compare them with == (which ignores case), or pack several values with itoa() and take them apart with getword().

Events can also be made: send_done() sends an SFB_DONE as a command does when it has succeeded, and send_pre() an SFB_PRE.

Example

dilbegin bell_rope();
code
{
   :start:
   wait(SFB_CMD, command("pull") and ("rope" in argument));
   block;
   act("You pull the rope, and far above a bell rings.",
       A_ALWAYS, activator, null, null, TO_CHAR);
   act("$1n pulls the rope, and far above a bell rings.",
       A_ALWAYS, activator, null, null, TO_ROOM);
   sendtoalldil("bell", "bell_ringer@mytown");
   goto start;
}
dilend

dilbegin bell_ringer();
var
   n : integer;
code
{
   heartbeat := PULSE_SEC * 30;
   :start:
   wait(SFB_MSG | SFB_TICK, (command(CMD_AUTO_MSG) and (argument == "bell"))
        or command(CMD_AUTO_TICK));
   if (command(CMD_AUTO_TICK))
   {
      n := 0;
      goto start;
   }
   n := n + 1;
   if (n > 3)
      goto start;
   exec("say The bell! Someone is at the gate!", self);
   goto start;
}
dilend

bell_rope sits in a room: it makes up the command "pull rope" (the game has no pull, so it must block, or the server answers that the command isn't known) and sends a message. Every NPC running bell_ringer, anywhere in the game, wakes with the message and answers, at most three times in a row; its own tick resets the count.

See also