Difference between revisions of "Manual:DIL Examples"
m |
(rewritten against the compiler and interpreter; examples compile) |
||
| Line 1: | Line 1: | ||
| − | + | 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]]. | |
| − | |||
| − | |||
| − | = | + | == Where a program sits == |
| − | Every | + | Every program runs on a unit, which it calls <code>self</code>. What is around it depends on the kind of unit: |
| − | + | {| class="wikitable" | |
| − | + | ! Program on !! <code>self.outside</code> 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 [[Manual:DIL Manual/aware|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 <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] > 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 | ||
| + | </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>. | ||
| − | + | == A room that forbids a command == | |
| − | + | <pre> | |
| − | + | 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 | ||
| + | </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. | ||
| − | + | == Food that starts another program == | |
| − | + | <pre> | |
| + | 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 | |
| − | + | </pre> | |
| − | + | 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. | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| + | == Two NPCs working together == | ||
| + | <pre> | ||
| + | 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 | ||
| + | </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. | ||
| − | + | == A player command == | |
| + | <pre> | ||
| + | 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 | ||
| + | </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.
Contents
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.