Difference between revisions of "Manual:DIL Examples"

From DikuMUD Wiki
Jump to navigation Jump to search
m
(rewritten against the compiler and interpreter; examples compile)
 
Line 1: Line 1:
−
= Intro =
+
Short DIL programs that run in the game, taken from the public zones in <code>vme/zone/</code> (lightly tidied), with what each shows. [[Manual:DIL Manual/Making a Program|Making a Program]] explains the parts of a program; each keyword links to its own page in the [[Manual:DIL Manual|DIL Manual]].
−
I put this section in here because while many programmers already know most of this inherently, many casual mudders have the curiosity and want to start building but don't know where to start with dil and the manual can be a bit overwhelming.
 
−
So this is a compilation of the most frequently asked questions and overall stumbling blocks we've seen for new coders. Hopefully it helps.
 
  
−
= Construction =
+
== Where a program sits ==
−
Every DIL program has the following structure:<br>
+
Every program runs on a unit, which it calls <code>self</code>. What is around it depends on the kind of unit:
−
<nowiki>
+
{| class="wikitable"
−
dilbegin programname();
+
! Program on !! <code>self.outside</code> is !! It hears
−
var
+
|-
−
code
+
| an object || the room it lies in, the character carrying or wearing it, or the container it is in || the commands of the character carrying it; when it lies in a room or is worn, also those of the others in the room
−
{}
+
|-
−
dilend
+
| an NPC || the room it stands in || the commands of the others in the room; its own only with [[Manual:DIL Manual/aware|aware]]
−
</nowiki><br>
+
|-
 +
| a room || usually null (a room can lie inside another) || the commands typed in the room
 +
|-
 +
| a player || the room the player is in || the same as an NPC; the player's own commands only with <code>aware</code>
 +
|}
 +
<code>self.inside</code> is the first unit inside <code>self</code>: what a room holds, a character's inventory, a container's contents.
  
 +
== An object that answers a look ==
 +
<pre>
 +
dilbegin tobacco_pouch();
 +
code
 +
{
 +
  :start:
 +
  wait(SFB_DONE, command("look") and (self == target));
 +
  if (self.value[1] &gt; 0)
 +
      sendtext("It currently contains enough tobacco for " + itoa(self.value[1]) +
 +
              " fillings of the pipe.&lt;br/&gt;", activator);
 +
  else
 +
      sendtext("It is empty.&lt;br/&gt;", activator);
 +
  goto start;
 +
}
 +
dilend
 +
</pre>
 +
From udgaard.zon, on the tobacco pouches. After someone has looked at the pouch (<code>SFB_DONE</code>, with the pouch as <code>target</code>), it tells them how much tobacco is left, kept in the object's <code>value[1]</code>.
  
−
Every dil must run on an object. That object is always going to be 'self' to the program.
+
== A room that forbids a command ==
−
It will be running from one of the following things:<br>
+
<pre>
−
* A room
+
dilbegin no_magic();
−
* A mobile (npc)
+
code
−
* A player
+
{
−
* An object (thing)<br>
+
  :start:
 +
  wait(SFB_CMD, command("cast") and ("door" in argument));
 +
  act("You are not allowed to cast magic on the door.",
 +
      A_ALWAYS, activator, null, null, TO_CHAR);
 +
  block;
 +
  goto start;
 +
}
 +
dilend
 +
</pre>
 +
From ratswarf.zon (and bounty.zon), on rooms with a magically sealed door. It wakes before a <code>cast</code> runs (<code>SFB_CMD</code>), and when the spell is aimed at the door it answers and [[Manual:DIL Manual/block|block]]s the command, so the spell is never cast.
  
−
Keeping this in mind will help with your program's construction. You can also use dot notation to reference objects relative to the dil or the objects it interacts with.
+
== Food that starts another program ==
−
Take <code>self.outside</code> and we'll look at it from each of the object types.
+
<pre>
 +
dilbegin herb(s : string);
 +
code
 +
{
 +
  :start:
 +
  wait(SFB_CMD, command("eat") and (activator.position &gt;= POSITION_RESTING));
 +
  if (findunit(activator, argument, FIND_UNIT_INVEN, null) != self)
 +
      goto start;
  
−
=== Dil On an Object ===
+
  act("You eat $2n.", A_HIDEINV, activator, self, null, TO_CHAR);
−
So let's say this particular DIL lives on a gem. The gem says nice things to you when it's held.
+
  act("$1n eats $2n.", A_HIDEINV, activator, self, null, TO_ROOM);
−
Considerations:<br>
+
  dilcopy(s, activator);
−
Does it speak when you're incapacitated? Meditating? Fighting?
+
  block;
−
Does it speak when you're invisible?
+
  destroy(self);
−
Can other people hear it?
+
}
−
How often can it speak?
+
dilend
−
+
</pre>
−
<nowiki>dilbegin gem_speak();
+
From midgaard.zon, on the nightshade berries: <code>dilcopy herb@midgaard("ns_berries@midgaard");</code>. When the berries are eaten, it takes over the <code>eat</code> command, puts a copy of the template named in its argument on the eater with [[Manual:DIL Manual/dilcopy()|dilcopy()]] (there, <code>ns_berries</code> makes the eater feel ill for a while), and destroys the berries. The same <code>herb</code> template serves any herb: only the argument changes. [[Manual:DIL Manual/findunit()|findunit()]] checks that the word typed means this very object.
−
          var
 
−
          code
 
−
          {
 
−
            heartbeat := 90*PULSE_SEC; //every 90 seconds.  
 
−
            :start:
 
−
            wait(SFB_TICK, TRUE and self.outside.type == UNIT_ST_PC and self.equip);
 
−
            act("$1n says 'You look lovely today!'", A_ALWAYS, self.outside, self, null, TO_CHAR);
 
−
            goto start;
 
−
          }
 
−
          dilend</nowiki>
 
−
In this example:<br>
 
−
* self.outside varies by where the object is. If it's in a bag, self.outside = the bag.
 
−
** self. outside could also be an NPC if it's inside a mob. It could be a player. Remember, people can go in containers too!
 
−
* self.outside.outside.inside is a even a possibility!
 
  
 +
== Two NPCs working together ==
 +
<pre>
 +
dilbegin goblin1prg();
 +
var
 +
  s : string;
 +
code
 +
{
 +
  heartbeat := PULSE_SEC * 2;
  
−
=== DIL on an NPC ===
+
  :start:
−
So this particular DIL will live on a NPC or mobile. Perhaps it will say "Greetings" every time an actual player enters the room.
+
  wait(SFB_CMD, activator.type == UNIT_ST_PC);
 +
  if (self.position &lt; POSITION_STANDING)
 +
      goto start;
 +
  s := activator.name;
 +
  pause;
 +
  act("$1n whispers 'ready?'", A_SOMEONE, self, null, null, TO_ALL);
 +
  sendto(s, findsymbolic("goblin2@goblin_cave"));
 +
  pause;
 +
  pause;
 +
  exec("kill " + s, self);
 +
  while (self.position == POSITION_FIGHTING)
 +
      pause;
 +
  goto start;
 +
}
 +
dilend
  
−
<nowiki>dilbegin hi();
+
dilbegin goblin2prg();
−
        var
+
code
−
        code
+
{
−
        {
+
  heartbeat := PULSE_SEC * 2;
−
          :start:  
 
−
          wait(SFB_DONE, command(CMD_AUTO_ENTER) and activator.type == UNIT_ST_PC);
 
−
          exec("say Hello, "+ activator.name +"!", self);
 
−
          goto start;
 
−
        }
 
−
        dilend</nowiki>
 
  
−
In this example:<br>
+
  :start:
−
* self.outside = the room the NPC is in.
+
  wait(SFB_MSG, (activator.nameidx == "goblin1") and (activator.zoneidx == "goblin_cave"));
−
* self.inside = the NPC's inventory.
+
  pause;
−
=== Dil on a PC ===
+
  act("$1n whispers 'yes... kill... KILL!'", A_SOMEONE, self, null, null, TO_ALL);
 +
  pause;
 +
  pause;
 +
  exec("kill " + argument, self);
 +
  pause;
 +
  goto start;
 +
}
 +
dilend
 +
</pre>
 +
From goblin_cave.zon, where each is written inside its goblin's definition. When a player does anything near the first goblin, it keeps the player's name in a string (unit variables are cleared at each <code>pause</code>; strings are kept), [[Manual:DIL Manual/sendto()|sends]] the name to the second goblin, and attacks. The second wakes with the message in <code>argument</code>, checks that the sender (<code>activator</code>) really is the first goblin, and joins in.
  
−
It's unlikely that you would begin anywhere near a dil on a player. Something to keep in mind if you are writing one is how it will react in a number of circumstances. ie: When they die? What if it's a room with $no_spam? What if they're in nightmare? What if they're in a no-tele zone? What if they're fighting? Who are they fighting? Will it provoke an attack? These are just a few things to keep in mind.
+
== A player command ==
 +
<pre>
 +
dilbegin do_motd(arg : string);
 +
var
 +
  err : integer;
 +
  motd : string;
 +
code
 +
{
 +
  err := loadstr("motd", motd);
 +
  if (err &gt; 0)
 +
  {
 +
      motd := "&lt;br/&gt;" + textformat(motd);
 +
      pagestring(motd, self);
 +
  }
 +
  send_done("motd", self, null, null, 0, arg, null, CMD_AUTO_NONE);
 +
  quit;
 +
}
 +
dilend
 +
</pre>
 +
From commands.zon: the <code>motd</code> command, named in <code>vme/etc/commands.def</code> with <code>func = do_motd@commands</code>. <code>self</code> is the player who typed it and <code>arg</code> the rest of the line. It reads a text file with [[Manual:DIL Manual/loadstr()|loadstr()]], shows it a page at a time, and ends with [[Manual:DIL Manual/send done()|send_done()]] so that other programs can react to the command as they do to built-in ones.
 +
 
 +
== See also ==
 +
* [[Manual:DIL Manual|DIL Manual]]
 +
* [[Manual:DIL Manual/Making a Program|Making a Program]]
 +
* [[Manual:DIL Manual/Messages|Messages]]
 +
* [[Manual:DIL Manual/Ending Notes|Ending Notes]]
  
 
[[Category:Manual Title Pages]]
 
[[Category:Manual Title Pages]]

Latest revision as of 23:54, 14 November 2026

Short DIL programs that run in the game, taken from the public zones in vme/zone/ (lightly tidied), with what each shows. Making a Program explains the parts of a program; each keyword links to its own page in the DIL Manual.

Where a program sits

Every program runs on a unit, which it calls self. What is around it depends on the kind of unit:

Program on self.outside is It hears
an object the room it lies in, the character carrying or wearing it, or the container it is in the commands of the character carrying it; when it lies in a room or is worn, also those of the others in the room
an NPC the room it stands in the commands of the others in the room; its own only with aware
a room usually null (a room can lie inside another) the commands typed in the room
a player the room the player is in the same as an NPC; the player's own commands only with aware

self.inside is the first unit inside self: what a room holds, a character's inventory, a container's contents.

An object that answers a look

dilbegin tobacco_pouch();
code
{
   :start:
   wait(SFB_DONE, command("look") and (self == target));
   if (self.value[1] > 0)
      sendtext("It currently contains enough tobacco for " + itoa(self.value[1]) +
               " fillings of the pipe.<br/>", activator);
   else
      sendtext("It is empty.<br/>", activator);
   goto start;
}
dilend

From udgaard.zon, on the tobacco pouches. After someone has looked at the pouch (SFB_DONE, with the pouch as target), it tells them how much tobacco is left, kept in the object's value[1].

A room that forbids a command

dilbegin no_magic();
code
{
   :start:
   wait(SFB_CMD, command("cast") and ("door" in argument));
   act("You are not allowed to cast magic on the door.",
       A_ALWAYS, activator, null, null, TO_CHAR);
   block;
   goto start;
}
dilend

From ratswarf.zon (and bounty.zon), on rooms with a magically sealed door. It wakes before a cast runs (SFB_CMD), and when the spell is aimed at the door it answers and blocks the command, so the spell is never cast.

Food that starts another program

dilbegin herb(s : string);
code
{
   :start:
   wait(SFB_CMD, command("eat") and (activator.position >= POSITION_RESTING));
   if (findunit(activator, argument, FIND_UNIT_INVEN, null) != self)
      goto start;

   act("You eat $2n.", A_HIDEINV, activator, self, null, TO_CHAR);
   act("$1n eats $2n.", A_HIDEINV, activator, self, null, TO_ROOM);
   dilcopy(s, activator);
   block;
   destroy(self);
}
dilend

From midgaard.zon, on the nightshade berries: dilcopy herb@midgaard("ns_berries@midgaard");. When the berries are eaten, it takes over the eat command, puts a copy of the template named in its argument on the eater with dilcopy() (there, ns_berries makes the eater feel ill for a while), and destroys the berries. The same herb template serves any herb: only the argument changes. findunit() checks that the word typed means this very object.

Two NPCs working together

dilbegin goblin1prg();
var
   s : string;
code
{
   heartbeat := PULSE_SEC * 2;

   :start:
   wait(SFB_CMD, activator.type == UNIT_ST_PC);
   if (self.position < POSITION_STANDING)
      goto start;
   s := activator.name;
   pause;
   act("$1n whispers 'ready?'", A_SOMEONE, self, null, null, TO_ALL);
   sendto(s, findsymbolic("goblin2@goblin_cave"));
   pause;
   pause;
   exec("kill " + s, self);
   while (self.position == POSITION_FIGHTING)
      pause;
   goto start;
}
dilend

dilbegin goblin2prg();
code
{
   heartbeat := PULSE_SEC * 2;

   :start:
   wait(SFB_MSG, (activator.nameidx == "goblin1") and (activator.zoneidx == "goblin_cave"));
   pause;
   act("$1n whispers 'yes... kill... KILL!'", A_SOMEONE, self, null, null, TO_ALL);
   pause;
   pause;
   exec("kill " + argument, self);
   pause;
   goto start;
}
dilend

From goblin_cave.zon, where each is written inside its goblin's definition. When a player does anything near the first goblin, it keeps the player's name in a string (unit variables are cleared at each pause; strings are kept), sends the name to the second goblin, and attacks. The second wakes with the message in argument, checks that the sender (activator) really is the first goblin, and joins in.

A player command

dilbegin do_motd(arg : string);
var
   err : integer;
   motd : string;
code
{
   err := loadstr("motd", motd);
   if (err > 0)
   {
      motd := "<br/>" + textformat(motd);
      pagestring(motd, self);
   }
   send_done("motd", self, null, null, 0, arg, null, CMD_AUTO_NONE);
   quit;
}
dilend

From commands.zon: the motd command, named in vme/etc/commands.def with func = do_motd@commands. self is the player who typed it and arg the rest of the line. It reads a text file with loadstr(), shows it a page at a time, and ends with send_done() so that other programs can react to the command as they do to built-in ones.

See also