Difference between revisions of "Manual:DIL Manual/Messages"

From DikuMUD Wiki
Jump to navigation Jump to search
(XML import)
(rewritten against the compiler and interpreter; examples compile)
 
Line 1: Line 1:
−
<span id="messies"></span>
+
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.
−
<h3>'''Messages:'''</h3>
 
  
−
  In DIL, a program attached to a unit gets activated when the program receives
+
== The kinds of event ==
−
a message. In order to save CPU usage, there are a number of different message
+
A program names the kinds it wants in [[Manual:DIL Manual/wait()|wait()]] or [[Manual:DIL Manual/interrupt()|interrupt()]], with the <code>SFB_</code> constants of <code>vme/include/vme.h</code> joined by <code>|</code>: <code>wait(SFB_CMD | SFB_MSG, ...)</code>.
−
categories which can cause activation of a program. The 'wait()' commands first
+
{| class="wikitable"
−
parameter is an integer, telling what message categories the program should
+
! Constant !! When !! Can be blocked
−
reactivate on. The second parameter is an expression, which also must evaluate
+
|-
−
to TRUE. 'pause' is just special instances of the 'wait()' command.
+
| [[Manual:DIL Manual/SFB CMD|SFB_CMD]] || A character near the unit types a command, before it runs. Also moves, unknown words, and commands given with <code>exec()</code>. || yes: the command doesn't run
 +
|-
 +
| [[Manual:DIL Manual/SFB DONE|SFB_DONE]] || A command has been carried out: something given, taken, dropped, a move, a spell cast, a room entered (<code>CMD_AUTO_ENTER</code>). || no, it has happened
 +
|-
 +
| [[Manual:DIL Manual/SFB PRE|SFB_PRE]] || A spell is about to take effect, or damage is about to be dealt (<code>CMD_AUTO_DAMAGE</code>). <code>power</code> can be changed. || yes: it doesn't happen
 +
|-
 +
| [[Manual:DIL Manual/SFB TICK|SFB_TICK]] || The program's own timer, every [[Manual:DIL Manual/heartbeat|heartbeat]]. <code>SFB_RANTIME</code> added makes the time vary. || no
 +
|-
 +
| [[Manual:DIL Manual/SFB MSG|SFB_MSG]] || Another program sent a message. || only from the programs after it
 +
|-
 +
| [[Manual:DIL Manual/SFB COM|SFB_COM]] || A character near the unit makes a melee attack. || no
 +
|-
 +
| [[Manual:DIL Manual/SFB DEAD|SFB_DEAD]] || A character near the unit dies (also the unit itself). || no
 +
|-
 +
| <code>SFB_EDIT</code> || A player closes the editor started with [[Manual:DIL Manual/beginedit()|beginedit()]]. ||
 +
|-
 +
| <code>SFB_PROMPT</code> || The player's prompt is being made. ||
 +
|}
 +
<code>pause</code> is <code>wait(SFB_TICK | SFB_RANTIME, TRUE)</code>. <code>SFB_SAVE</code> and <code>SFB_ACTIVATE</code> work only in <code>interrupt()</code> (the second is what [[Manual:DIL Manual/on activation()|on_activation()]] uses).
  
 +
== What the program sees ==
 +
When an event wakes a program, these built-in variables describe it:
 +
{| class="wikitable"
 +
! Variable !! Holds
 +
|-
 +
| [[Manual:DIL Manual/activator|activator]] || the character who caused it: who typed the command, who attacked or died, which unit sent the message; null for a tick
 +
|-
 +
| [[Manual:DIL Manual/argument|argument]] || the rest of the command line, or the message's text
 +
|-
 +
| [[Manual:DIL Manual/medium|medium]] || what was used, for some commands and spells: the item given, the weapon, the scroll
 +
|-
 +
| [[Manual:DIL Manual/target|target]] || who or what the command was done to
 +
|-
 +
| [[Manual:DIL Manual/power|power]] || a number: the amount of money given, the damage about to be dealt, the strength of a spell
 +
|-
 +
| [[Manual:DIL Manual/cmdstr|cmdstr]], [[Manual:DIL Manual/excmdstr|excmdstr]], [[Manual:DIL Manual/excmdstr case|excmdstr_case]] || the command's name, the word as typed in lower case, and exactly as typed
 +
|}
 +
[[Manual:DIL Manual/command()|<code>command("give")</code>]] is true when the event comes from that command; automatic events have numbers, such as <code>command(CMD_AUTO_ENTER)</code>, <code>command(CMD_AUTO_TICK)</code> or <code>command(CMD_AUTO_DAMAGE)</code>. What each value holds for each event is on the event's page; for <code>SFB_DONE</code> it is set by the command, in its call to [[Manual:DIL Manual/send done()|send_done()]].
  
−
<strong>Caveat Builder: </strong>
+
These values belong to the event that woke the program, and the next wait replaces them. At the same wake every <code>unitptr</code> and <code>extraptr</code> variable that isn't [[Manual:DIL Manual/secure()|secure]]d 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;
  
−
  Whenever you construct the arguments for the wait command, bear in mind that
+
== Who gets an event ==
−
ALL your tests will be executed EVERYTIME a message of the relevant kind comes
+
* '''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 <code>target</code> somewhere else gets it too. See [[Manual:DIL Manual/SFB CMD|SFB_CMD]].
−
through. Thus you should keep the length of the activation expression to a
+
* '''A tick''' goes only to the program whose timer it is.
−
reasonable minimum, and you should NEVER use the time-consuming findxxx-functions.
+
* '''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 <code>exec()</code>. Declare the template [[Manual:DIL Manual/aware|aware]] (<code>dilbegin aware name();</code>) to get those events. A unit's own death, and messages sent to it with <code>sendto()</code>, arrive without <code>aware</code>.
  
−
Valid example (That prohibits a player from removing an object):
+
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.
  
−
:glue:
+
== 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 [[Manual:DIL Manual/fnpri|fnpri]], lowest number first (<code>FN_PRI_BLOCK</code> 25 before the default <code>FN_PRI_CHORES</code> 40); among equals the oldest first.
 +
* [[Manual:DIL Manual/block|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 <code>fnpri</code>.
 +
* [[Manual:DIL Manual/priority|priority]] keeps the programs after this one on the same unit from getting any event at all, until [[Manual:DIL Manual/nopriority|nopriority]].
 +
* [[Manual:DIL Manual/on activation()|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 <code>SFB_MSG</code>, with the text in <code>argument</code> and the sending unit in <code>activator</code>.
 +
{| class="wikitable"
 +
! Procedure !! Reaches
 +
|-
 +
| [[Manual:DIL Manual/send()|send(msg)]] || the programs around the sender's unit, as a command does; the sender's own other programs only when they are <code>aware</code>
 +
|-
 +
| [[Manual:DIL Manual/sendto()|sendto(msg, u)]] || every program on the unit <code>u</code>, wherever it is, the sender's own unit included
 +
|-
 +
| [[Manual:DIL Manual/sendtoall()|sendtoall(msg, "sym@zone")]] || the programs on every unit in the game made from that definition; <code>medium</code> is the sender too
 +
|-
 +
| [[Manual:DIL Manual/sendtoalldil()|sendtoalldil(msg, "tmpl@zone")]] || every running copy of that template in the game; <code>target</code> 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 <code>SFB_MSG</code> at that moment misses it. To catch messages while waiting for something else, use <code>interrupt(SFB_MSG, condition, label)</code>.
 +
* 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 <code>==</code> (which ignores case), or pack several values with <code>itoa()</code> and take them apart with [[Manual:DIL Manual/getword()|getword()]].
  
−
wait(SFB_CMD,command(CMD_REMOVE));
+
Events can also be made: [[Manual:DIL Manual/send done()|send_done()]] sends an <code>SFB_DONE</code> as a command does when it has succeeded, and [[Manual:DIL Manual/send pre()|send_pre()]] an <code>SFB_PRE</code>.
−
u := findunit(activator,argument,FIND_UNIT_IN_ME,null );
 
−
if (u != self)
 
−
act("You can't remove $2n, it's sticky.",
 
−
  A_SOMEONE,activator,self,null,TO_CHAR););
 
−
block;
 
−
goto glue;
 
  
−
'''See Also:'''
+
== Example ==
−
[[#findunit.html|Dil and Findunit()]]
+
<pre>
 +
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
  
−
The message categories are as follows:
+
dilbegin bell_ringer();
−
---~---~---~---~---~---~---~---~---
+
var
−
<span id="messies"></span>
+
  n : integer;
−
<h3>'''Messages:'''</h3>
+
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 &gt; 3)
 +
      goto start;
 +
  exec("say The bell! Someone is at the gate!", self);
 +
  goto start;
 +
}
 +
dilend
 +
</pre>
 +
<code>bell_rope</code> sits in a room: it makes up the command "pull rope" (the game has no <code>pull</code>, so it must <code>block</code>, or the server answers that the command isn't known) and sends a message. Every NPC running <code>bell_ringer</code>, anywhere in the game, wakes with the message and answers, at most three times in a row; its own tick resets the count.
  
−
  In DIL, a program attached to a unit gets activated when the program receives
+
== See also ==
−
a message. In order to save CPU usage, there are a number of different message
+
* [[Manual:DIL Manual/wait()|wait()]]
−
categories which can cause activation of a program.  The 'wait()' commands first
+
* [[Manual:DIL Manual/interrupt()|interrupt()]]
−
parameter is an integer, telling what message categories the program should
+
* [[Manual:DIL Manual/block|block]]
−
reactivate on.  The second parameter is an expression, which also must evaluate
+
* [[Manual:DIL Manual/fnpri|fnpri]]
−
to TRUE. 'pause' is just special instances of the 'wait()' command.
+
* [[Manual:DIL Manual/aware|aware]]
−
 
+
* [[Manual:DIL Manual/DIL Constructs|DIL Constructs]]
−
 
 
−
<strong>Caveat Builder: </strong>
 
−
 
 
−
  Whenever you construct the arguments for the wait command, bear in mind that
 
−
ALL your tests will be executed EVERYTIME a message of the relevant kind comes
 
−
through.  Thus you should keep the length of the activation expression to a
 
−
reasonable minimum, and you should NEVER use the time-consuming findxxx-functions.
 
−
 
 
−
Valid example (That prohibits a player from removing an object):
 
−
 
 
−
:glue:
 
−
 
 
−
 
 
−
wait(SFB_CMD,command(CMD_REMOVE));
 
−
u := findunit(activator,argument,FIND_UNIT_IN_ME,null );
 
−
if (u != self)
 
−
act("You can't remove $2n, it's sticky.",
 
−
  A_SOMEONE,activator,self,null,TO_CHAR););
 
−
block;
 
−
goto glue;
 
−
 
 
−
'''See Also:'''
 
−
[[#findunit.html|Dil and Findunit()]]
 
−
 
 
−
The message categories are as follows:
 
−
---~---~---~---~---~---~---~---~---
 

Latest revision as of 23:54, 14 November 2026

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