Difference between revisions of "Manual:DIL Manual/interrupt()"

From DikuMUD Wiki
Jump to navigation Jump to search
(XML import)
(checked against the compiler and the interpreter; example compiles)
Line 1: Line 1:
−
<span id="bpinterr"></span>
+
= interrupt() =
−
integer interrupt( flags : integer, dilexp, label )
+
'''Function''' (integer). Sets up a jump to a label when a certain event wakes the program, whatever it is waiting for.
  
−
    Set up interrupt matching message classes matching "flags",
+
interrupt(flags : integer, condition, label) : integer
−
    for example "SFB_COM" or "SFB_COM | SFB_MSG".
 
  
−
    When the program is activated on either of the specified conditions
+
Registers an interrupt for the running template. From then on, every time the program is woken by an event of a kind in <code>flags</code>, the <code>condition</code> is evaluated before anything else; when it is true, the program leaves whatever it was doing (a <code>wait</code>, a <code>pause</code>) and goes on at <code>label</code>. The program is woken by those events even when its <code>wait</code> asks for other ones. Typical use: a long-running NPC program that should react to a message or to its own death at any moment.
−
    the 'dilexp' is evaluated. If true, then execution continues at 'label',
 
−
    otherwise the next interrupt is checked (if any).
 
  
−
    Interrupts are saved (restored), when 'recall' is set.
+
== Parameters ==
−
    Returns an integer which is used for clear() to clear an interrupt.
+
{| class="wikitable"
 +
! Name !! Type !! Meaning
 +
|-
 +
| flags || integer || <code>SFB_*</code> event kinds from <code>vme/include/vme.h</code>, joined with <code>|</code>: <code>SFB_CMD</code>, <code>SFB_MSG</code>, <code>SFB_TICK</code>, <code>SFB_DEAD</code>, <code>SFB_COM</code>, <code>SFB_DONE</code>... Write the constants themselves (see the notes).
 +
|-
 +
| condition || integer expression || Tested at each such event, with <code>activator</code>, <code>argument</code>, <code>command()</code> and the rest describing that event.
 +
|-
 +
| label || label || Where to go on when the condition is true, or <code>skip</code> to ignore the event: the program then stays where it was and the event goes no further in it.
 +
|}
  
−
'''Example:'''
+
== Returns ==
 +
The number of the interrupt, for [[Manual:DIL Manual/clear()|clear()]]. It can be used as a statement on its own (<code>interrupt(SFB_MSG, argument == "stop", stop_it);</code>) or assigned (<code>i := interrupt(...);</code>), but not inside an expression.
  
−
The following program shows, that the owner (self) of the program keeps
+
== Notes ==
−
snoring while sleeping. The on_activation ensures that the program
+
* The interrupt stays set until <code>clear()</code> or until the template returns: it fires every time, not once. Running the same <code>interrupt</code> statement again replaces it instead of adding a second one.
−
is only activated when the owner is sleeping. However, since the interrupt
+
* When several interrupts match an event, they are tried from the lowest number up, and the first whose condition is true wins.
−
precede the on_activation, these may still be intercepted before the
+
* An interrupt whose <code>flags</code> include <code>SFB_ACTIVATE</code> is tested at every wake, and is removed the first time it jumps to a label. <code>on_activation(cond, label)</code> is a macro in <code>vme/include/wmacros.h</code> for <code>interrupt(SFB_ACTIVATE, cond, label)</code>.
−
on_activation. The on_activation is just another type of interrupt that
+
* Interrupts belong to the template that set them: a template called from it doesn't see them, and they end when it returns. With <code>recall</code>, the main template's interrupts are saved and restored with the program.
−
reacts on all flags (without actually setting them so that the program
+
* The flags must be a constant expression such as <code>SFB_MSG | SFB_CMD</code>. A variable holding the flags is read as 0, and that interrupt never fires.
−
is activated).
+
* Unit variables are cleared at the wake as usual: at the label, find the units again or keep them with <code>secure</code>.
  
−
When the program receives the message "relief" the snoring stops briefly.
+
== Example ==
−
As used, "relief" may only be set once.
+
<pre>
 +
dilbegin guard_post();
 +
var
 +
  i : integer;
 +
code
 +
{
 +
  heartbeat := PULSE_SEC * 30;
 +
  i := interrupt(SFB_MSG, argument == "stand down", relieved);
  
−
When the program receives the message "cured", the snoring stops completely
+
  :loop:
−
(i.e. the program quits itself).
+
  wait(SFB_TICK, TRUE);
 +
  act("$1n looks around watchfully.", A_SOMEONE, self, null, null, TO_ROOM);
 +
  goto loop;
  
−
<nowiki>
+
  :relieved:
 +
  clear(i);
 +
  act("$1n relaxes and stops guarding.", A_SOMEONE, self, null, null, TO_ROOM);
 +
  quit;
 +
}
 +
dilend
 +
</pre>
 +
An NPC that keeps watch every 30 seconds until another program sends it the message "stand down" (<code>sendto("stand down", guard)</code>).
  
−
dilbegin
+
== See also ==
−
 
+
* [[Manual:DIL Manual/clear()|clear()]]
−
var
+
* [[Manual:DIL Manual/wait()|wait()]]
−
    i : integer;
+
* [[Manual:DIL Manual/skip()|skip()]]
−
 
+
* [[Manual:DIL Manual/sendto()|sendto()]]
−
code
+
* [[Manual:DIL Manual/recall|recall]]
−
dilend</nowiki>
 
−
 
 
−
---~---~---~---~---~---~---~---~---
 
−
<span id="bpinterr"></span>
 
−
integer interrupt( flags : integer, dilexp, label )
 
−
 
 
−
    Set up interrupt matching message classes matching "flags",
 
−
    for example "SFB_COM" or "SFB_COM | SFB_MSG".
 
−
 
 
−
    When the program is activated on either of the specified conditions
 
−
    the 'dilexp' is evaluated. If true, then execution continues at 'label',
 
−
    otherwise the next interrupt is checked (if any).
 
−
 
 
−
    Interrupts are saved (restored), when 'recall' is set.
 
−
    Returns an integer which is used for clear() to clear an interrupt.
 
−
 
 
−
'''Example:'''
 
−
 
 
−
The following program shows, that the owner (self) of the program keeps
 
−
snoring while sleeping. The on_activation ensures that the program
 
−
is only activated when the owner is sleeping. However, since the interrupt
 
−
precede the on_activation, these may still be intercepted before the
 
−
on_activation. The on_activation is just another type of interrupt that
 
−
reacts on all flags (without actually setting them so that the program
 
−
is activated).
 
−
 
 
−
When the program receives the message "relief" the snoring stops briefly.
 
−
As used, "relief" may only be set once.
 
−
 
 
−
When the program receives the message "cured", the snoring stops completely
 
−
(i.e. the program quits itself).
 
−
 
 
−
<nowiki>
 
−
 
 
−
dilbegin
 
−
 
 
−
var
 
−
    i : integer;
 
−
 
 
−
code
 
−
dilend</nowiki>
 
−
 
 
−
---~---~---~---~---~---~---~---~---
 

Revision as of 23:17, 14 November 2026

interrupt()

Function (integer). Sets up a jump to a label when a certain event wakes the program, whatever it is waiting for.

interrupt(flags : integer, condition, label) : integer

Registers an interrupt for the running template. From then on, every time the program is woken by an event of a kind in flags, the condition is evaluated before anything else; when it is true, the program leaves whatever it was doing (a wait, a pause) and goes on at label. The program is woken by those events even when its wait asks for other ones. Typical use: a long-running NPC program that should react to a message or to its own death at any moment.

Parameters

Name Type Meaning
flags integer : SFB_CMD, SFB_MSG, SFB_TICK, SFB_DEAD, SFB_COM, SFB_DONE... Write the constants themselves (see the notes).
condition integer expression Tested at each such event, with activator, argument, command() and the rest describing that event.
label label Where to go on when the condition is true, or skip to ignore the event: the program then stays where it was and the event goes no further in it.

Returns

The number of the interrupt, for clear(). It can be used as a statement on its own (interrupt(SFB_MSG, argument == "stop", stop_it);) or assigned (i := interrupt(...);), but not inside an expression.

Notes

  • The interrupt stays set until clear() or until the template returns: it fires every time, not once. Running the same interrupt statement again replaces it instead of adding a second one.
  • When several interrupts match an event, they are tried from the lowest number up, and the first whose condition is true wins.
  • An interrupt whose flags include SFB_ACTIVATE is tested at every wake, and is removed the first time it jumps to a label. on_activation(cond, label) is a macro in vme/include/wmacros.h for interrupt(SFB_ACTIVATE, cond, label).
  • Interrupts belong to the template that set them: a template called from it doesn't see them, and they end when it returns. With recall, the main template's interrupts are saved and restored with the program.
  • The flags must be a constant expression such as SFB_MSG | SFB_CMD. A variable holding the flags is read as 0, and that interrupt never fires.
  • Unit variables are cleared at the wake as usual: at the label, find the units again or keep them with secure.

Example

dilbegin guard_post();
var
   i : integer;
code
{
   heartbeat := PULSE_SEC * 30;
   i := interrupt(SFB_MSG, argument == "stand down", relieved);

   :loop:
   wait(SFB_TICK, TRUE);
   act("$1n looks around watchfully.", A_SOMEONE, self, null, null, TO_ROOM);
   goto loop;

   :relieved:
   clear(i);
   act("$1n relaxes and stops guarding.", A_SOMEONE, self, null, null, TO_ROOM);
   quit;
}
dilend

An NPC that keeps watch every 30 seconds until another program sends it the message "stand down" (sendto("stand down", guard)).

See also