Manual:DIL Examples

From DikuMUD Wiki
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

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