Difference between revisions of "Manual:DIL Manual/on activation()"

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="bpona"></span>
+
= on_activation() =
−
integer on_activation ( dilexp , label )
+
'''Function''' (integer).
−
    dilexp  : A boolean DIL expression.
 
−
    label  : Label to jump to - OR the reserved keyword SKIP.
 
−
    returns : The index to the interrupt handing the on_activation.
 
  
−
    result: Sets up an interrupt that is executed before every activation
+
on_activation(condition, label) : integer
−
            of the DIL program. This is for example useful to catch
+
on_activation(condition, skip) : integer
−
            situations where your NPC has fallen asleep or is injured.
 
−
            If 'dilexp' evaluates to TRUE then the program jumps to 'label'.
 
−
            If 'label' is 'skip' then the program is simply not activated.
 
−
            When the on_activation evaluates to true, and jumps to a label
 
−
            other than skip, the condition is automatically cleared. If the
 
−
            dilexp evaluates to false, or if the label is skip, the activation
 
−
            remains active. Use the clear() to remove the on_activation.
 
  
−
    '''Example:'''
+
Sets a check that runs every time the program is woken, by any event, before the event reaches the program's <code>wait()</code> or <code>pause</code>. When the condition is true, the program either ignores the event (<code>skip</code>) or jumps to <code>label</code>. The usual use is to make a monster deaf while it sleeps or fights: <code>on_activation(self.position == POSITION_FIGHTING, skip);</code>.
−
            on_activation(self.position <= POSITION_SLEEPING, skip);
 
−
              or
 
−
            on_activation(self.position > POSITION_SLEEPING, let_me_sleep);
 
  
−
---~---~---~---~---~---~---~---~---
+
It is a macro in wmacros.h for <code>interrupt(SFB_ACTIVATE, condition, label)</code>, so the zone needs <code>#include &lt;macros.h&gt;</code> (as every zone has).
−
<span id="bpona"></span>
 
−
integer on_activation ( dilexp , label )
 
−
    dilexp  : A boolean DIL expression.
 
−
    label  : Label to jump to - OR the reserved keyword SKIP.
 
−
    returns : The index to the interrupt handing the on_activation.
 
  
−
    result: Sets up an interrupt that is executed before every activation
+
== Parameters ==
−
            of the DIL program. This is for example useful to catch
+
{| class="wikitable"
−
            situations where your NPC has fallen asleep or is injured.
+
! Name !! Type !! Meaning
−
            If 'dilexp' evaluates to TRUE then the program jumps to 'label'.
+
|-
−
            If 'label' is 'skip' then the program is simply not activated.
+
| condition || integer || an expression, checked at every wake
−
            When the on_activation evaluates to true, and jumps to a label
+
|-
−
            other than skip, the condition is automatically cleared. If the
+
| label || label || where to jump when it is true; or <code>skip</code> to ignore the event
−
            dilexp evaluates to false, or if the label is skip, the activation
+
|}
−
            remains active. Use the clear() to remove the on_activation.
 
  
−
    '''Example:'''
+
== Returns ==
−
            on_activation(self.position <= POSITION_SLEEPING, skip);
+
The number of the interrupt, for <code>clear()</code>. Most programs don't keep it.
−
              or
 
−
            on_activation(self.position > POSITION_SLEEPING, let_me_sleep);
 
  
−
---~---~---~---~---~---~---~---~---
+
== Notes ==
 +
* With <code>skip</code>: while the condition is true, every event is dropped. The program stays where it was, the event isn't kept for later, and the program can't <code>block</code> it. The check stays set.
 +
* With a label: when the condition is true the program jumps to the label and the check is removed. Call <code>on_activation()</code> again after the label to set it again.
 +
* Running the same <code>on_activation()</code> line again replaces the check it set before; it doesn't add a second one.
 +
* The checks belong to the template that set them: they end when that template returns or quits.
 +
* The checks and the <code>interrupt()</code>s of a template are tried in the order of their numbers; the first one that is true wins.
 +
* The check runs after the program's unit and extra variables have been cleared for the new wake (see <code>secure()</code>), so use <code>self</code>, <code>activator</code> and secured variables in the condition.
 +
* <code>clear()</code> with the returned number removes the check. Removing one moves the numbers of the checks set after it down by one.
 +
 
 +
== Example ==
 +
<pre>
 +
dilbegin answer();
 +
code
 +
{
 +
  on_activation((self.position <= POSITION_SLEEPING) or
 +
                (self.position == POSITION_FIGHTING), skip);
 +
 
 +
  heartbeat := PULSE_SEC * 3;
 +
 
 +
  :start:
 +
  wait(SFB_CMD, command("say") and ("hello" in argument));
 +
  exec("say Hello, " + activator.name + ".", self);
 +
  goto start;
 +
}
 +
dilend
 +
 
 +
dilbegin wakeup();
 +
var
 +
  i : integer;
 +
code
 +
{
 +
  heartbeat := PULSE_SEC * 5;
 +
 
 +
  :watch:
 +
  i := on_activation(self.position == POSITION_SLEEPING, asleep);
 +
 
 +
  :start:
 +
  pause;
 +
  goto start;
 +
 
 +
  :asleep:
 +
  exec("emote snores loudly.", self);
 +
  wait(SFB_TICK, self.position != POSITION_SLEEPING);
 +
  exec("emote yawns.", self);
 +
  goto watch;
 +
}
 +
dilend
 +
</pre>
 +
answer greets players but not while asleep or fighting, as many monsters in udgaard.zon do; wakeup jumps to a label when its monster falls asleep and sets the check again after waking.
 +
 
 +
== See also ==
 +
* [[Manual:DIL Manual/interrupt()|interrupt()]]
 +
* [[Manual:DIL Manual/clear()|clear()]]
 +
* [[Manual:DIL Manual/skip()|skip]]
 +
* [[Manual:DIL Manual/wait()|wait()]]
 +
* [[Manual:DIL Manual/secure()|secure()]]
 +
* [[Manual:DIL Manual/position|position]]

Revision as of 23:25, 14 November 2026

on_activation()

Function (integer).

on_activation(condition, label) : integer
on_activation(condition, skip) : integer

Sets a check that runs every time the program is woken, by any event, before the event reaches the program's wait() or pause. When the condition is true, the program either ignores the event (skip) or jumps to label. The usual use is to make a monster deaf while it sleeps or fights: on_activation(self.position == POSITION_FIGHTING, skip);.

It is a macro in wmacros.h for interrupt(SFB_ACTIVATE, condition, label), so the zone needs #include <macros.h> (as every zone has).

Parameters

Name Type Meaning
condition integer an expression, checked at every wake
label label where to jump when it is true; or skip to ignore the event

Returns

The number of the interrupt, for clear(). Most programs don't keep it.

Notes

  • With skip: while the condition is true, every event is dropped. The program stays where it was, the event isn't kept for later, and the program can't block it. The check stays set.
  • With a label: when the condition is true the program jumps to the label and the check is removed. Call on_activation() again after the label to set it again.
  • Running the same on_activation() line again replaces the check it set before; it doesn't add a second one.
  • The checks belong to the template that set them: they end when that template returns or quits.
  • The checks and the interrupt()s of a template are tried in the order of their numbers; the first one that is true wins.
  • The check runs after the program's unit and extra variables have been cleared for the new wake (see secure()), so use self, activator and secured variables in the condition.
  • clear() with the returned number removes the check. Removing one moves the numbers of the checks set after it down by one.

Example

dilbegin answer();
code
{
   on_activation((self.position <= POSITION_SLEEPING) or
                 (self.position == POSITION_FIGHTING), skip);

   heartbeat := PULSE_SEC * 3;

   :start:
   wait(SFB_CMD, command("say") and ("hello" in argument));
   exec("say Hello, " + activator.name + ".", self);
   goto start;
}
dilend

dilbegin wakeup();
var
   i : integer;
code
{
   heartbeat := PULSE_SEC * 5;

   :watch:
   i := on_activation(self.position == POSITION_SLEEPING, asleep);

   :start:
   pause;
   goto start;

   :asleep:
   exec("emote snores loudly.", self);
   wait(SFB_TICK, self.position != POSITION_SLEEPING);
   exec("emote yawns.", self);
   goto watch;
}
dilend

answer greets players but not while asleep or fighting, as many monsters in udgaard.zon do; wakeup jumps to a label when its monster falls asleep and sets the check again after waking.

See also