Difference between revisions of "Manual:DIL Manual/Making a Program"

From DikuMUD Wiki
Jump to navigation Jump to search
(XML import)
(rewritten against the compiler and interpreter; examples compile)
 
Line 1: Line 1:
−
<span id="making"></span>
+
A DIL program is written in a zone file, compiled with the zone by vmc, and run by the game server on a unit: a room, an object, an NPC or a player. This page goes through a whole program from nothing: where it is written, its parts, how it gets onto a unit, how it runs and waits, and how to compile it. A complete example is at the end.
  
−
<h3>'''Making a program:'''</h3>
+
== Templates ==
 +
A program is written once, as a '''template''', between [[Manual:DIL Manual/dilbegin|dilbegin]] and [[Manual:DIL Manual/dilend|dilend]]. Each unit that runs it gets its own '''copy''': the copy has its own variables and its own place in the code, while the code itself is shared. Ten guards with the same template are ten programs that know nothing of each other's variables.
  
−
  You define your DIL programs within your zone file. Each program you make is a
+
A template's full name is <code>name@zone</code>: its name (at most 15 characters) and the zone it is written in. Templates are also called by other templates, as procedures or as functions (see [[Manual:DIL Manual/Functions|Functions]]).
−
*template*. Such a template must be equipped with a unique name (for that zone).
 
−
Templates can either be defined in a new %dil section, just below the %zone
 
−
section in the zone file, or directly attached to units defined in your zonefile.
 
  
−
  If you define your DIL templates inside a unit definition, that unit is
+
== Where DIL is written ==
−
automatically assigned a program using that template. If you want to use an
+
* '''The <code>%dil</code> section''' of a zone, after <code>%zone</code> and before <code>%rooms</code>, holds the zone's templates. Any unit of any zone can use them by their full name.
−
already designed template, either in your own, or another zone, you use a
+
* '''Inside a unit's definition''': a <code>dilbegin ... dilend</code> written among a room's, object's or mobile's fields is a template of the zone too, and that unit gets a copy every time it is loaded. Such a template takes no arguments.
−
special function named "dilcopy", that takes the name of a template, and any
+
* '''<code>dilcopy name@zone(arguments);</code> in a unit's definition''' gives every unit loaded from that definition a copy of the template. The arguments are constants: numbers, strings, and lists in braces (<code>{"a", "b"}</code>, <code>{1, 2}</code>). A unit can have several <code>dilcopy</code> lines. The <code>%reset</code> section has no <code>dilcopy</code> of its own: the program comes with the definition of the unit the reset loads.
−
optional parameters in parenthesis. The parameters of a template called with
+
* '''At run time''', [[Manual:DIL Manual/dilcopy()|dilcopy()]] puts a copy on any unit: <code>dilcopy("name@zone(arg1,arg2)", u);</code>, with string and integer arguments only.
−
'dilcopy' may only be of types integer, strings and stringlists.
+
* '''As a player command''': a template named with <code>func = name@zone</code> in <code>vme/etc/commands.def</code> runs when the command is typed. It takes one string argument, the rest of the line, and <code>self</code> is the character who typed it. Spells are templates named the same way in <code>vme/etc/spells.def</code>.
  
−
'''Example:'''
+
The zone must start with <code>#include &lt;macros.h&gt;</code>: <code>dilend</code>, <code>pause</code> and <code>on_activation</code> are macros from it, and vmc needs the <code>^</code> that <code>dilend</code> adds to find the end of each program.
  
−
  dilcopy myfunc@myzone("say Hello, my friend!", 1, CMD_SAY,
+
== The parts of a template ==
−
);
+
  dilbegin [options] [type] name(arg : type, ...);
 +
external
 +
    other templates it calls
 +
var
 +
    its variables
 +
code
 +
{
 +
    statements
 +
}
 +
dilend
  
−
  DIL templates can always be reused. In fact, with version 2.0 of DIL, programs
+
* The '''header''' names the template and its arguments. The options are [[Manual:DIL Manual/aware|aware]], [[Manual:DIL Manual/recall|recall]], [[Manual:DIL Manual/unique|unique]] and [[Manual:DIL Manual/fnpri|fnpri]]. A type before the name makes the template a function that returns a value of that type. See [[Manual:DIL Manual/dilbegin|dilbegin]].
−
are no longer saved with equipment, but only a symbolic reference is used. That
+
* [[Manual:DIL Manual/external|external]] declares the templates this one calls, also those in the same zone.
−
way, if you make a change in your zone, ALL units using that program are changed.
+
* [[Manual:DIL Manual/var|var]] declares the variables, each with one of the eight [[Manual:DIL Manual/Data Types|data types]]. They start empty: 0, "", an empty list or null.
−
This however requires you keep the name. Upon loading, if a template is not
+
* [[Manual:DIL Manual/code|code]] holds the statements in one pair of braces. Labels for [[Manual:DIL Manual/goto|goto]] are written <code>:name:</code>.
−
found, the program is halted and rendered useless until such a reference can be
 
−
found during loading.
 
  
−
Technical note:
+
Every function, field and built-in variable of DIL is a reserved word, so <code>name</code>, <code>target</code>, <code>level</code>, <code>key</code> and many more can't be variable names. vmc then reports a syntax error with that word as the token.
  
−
  When you use several 'dilcopy' in your zonefile, only one instance is present
+
== How a program runs ==
−
game-time, thus saving tremendous amounts of memory.  It is similar to a shared
+
A copy starts at the first statement of <code>code</code> soon after its unit is loaded (or, for <code>dilcopy()</code>, at the unit's next tick). It runs until it waits, and waits most of its life:
−
library, in which code is shared  but variables are not.
 
  
−
You may use your templates to define both procedure and functions for your other
+
* [[Manual:DIL Manual/wait()|wait(events, condition)]] stops the program until an event of one of the given kinds comes for which the condition is true. The events are the <code>SFB_</code> values: a command typed nearby (<code>SFB_CMD</code>), a command done (<code>SFB_DONE</code>), a message from another program (<code>SFB_MSG</code>), the program's timer (<code>SFB_TICK</code>), and others; see [[Manual:DIL Manual/Messages|Messages]].
−
templates to call.
+
* <code>pause;</code> waits for the program's next tick. The tick comes every [[Manual:DIL Manual/heartbeat|heartbeat]], which the program sets: <code>heartbeat := PULSE_SEC * 30;</code> is every 30 seconds.
 +
* [[Manual:DIL Manual/interrupt()|interrupt()]] and [[Manual:DIL Manual/on activation()|on_activation()]] make the program react to some events whatever it is waiting for.
  
−
A template is defined by beginning with 'dilbegin' and ending with 'dilend'.
+
When an event wakes the program, the built-in variables [[Manual:DIL Manual/activator|activator]], [[Manual:DIL Manual/argument|argument]], [[Manual:DIL Manual/medium|medium]], [[Manual:DIL Manual/target|target]] and [[Manual:DIL Manual/power|power]] describe it, and <code>command(...)</code> tells which command it was. [[Manual:DIL Manual/self|self]] is always the program's own unit.
−
Inside the template your program section is defined, marked by the keyword 'code',
 
−
followed by the program itself;
 
  
−
<nowiki>
+
Two rules make DIL different from most languages:
 +
* '''At every wake, every <code>unitptr</code> and <code>extraptr</code> variable becomes null''', unless the unit was kept with [[Manual:DIL Manual/secure()|secure()]]. The unit may have left or been destroyed while the program slept. Copy what you need right after the <code>wait</code>, and find units again after a <code>pause</code>. Integers, strings and lists keep their values.
 +
* '''A program can't run forever without waiting.''' After about 52,000 statements without a wait (about 17,000 rounds of a one-line <code>while</code> loop) the server stops it and writes "had endless loop" to the zone's log.
  
−
dilbegin myprogram();
+
The usual shape is a loop: a label, a wait, the work, and <code>goto</code> back to the label. A program ends for good with [[Manual:DIL Manual/quit|quit]], or when its unit is destroyed. One that runs past the closing brace of <code>code</code> starts again from the top, with its variables as they were.
−
var
 
−
  i : integer;
 
−
  j : integer;
 
  
−
code
+
Programs on a player, and on the things a player carries, are saved with the player. Their integer, string and list variables are saved too; with [[Manual:DIL Manual/recall|recall]] the program also goes on from where it waited, instead of starting again.
−
dilend</nowiki>
 
  
−
  This simple template does nothing but making its owner say 'Hello world' once
+
== Compiling ==
−
every 5 seconds. The template is called 'myprogram' and takes no arguments. The
+
Zone files are in <code>vme/zone/</code>. Compile a zone with vmc, the zone compiler:
−
'pause' command waits for the program to receive a timer message, whose interval
+
  cd vme/zone
−
  is set by the 'inline' variable 'heartbeat'. The 'self' variable is a unitptr
+
  ../bin/vmc -m -d ../etc/ -I../include/ myzone.zon
−
  referring to the unit owning the DIL program. Other inline variables will be
 
−
explained later.
 
  
−
  For a DIL program to work, it must be part of a *unit*: a room, player, non-player
+
<code>-m</code> compiles only zones that have changed; <code>-s</code> checks the syntax without writing any files. vmc writes <code>myzone.data</code> and <code>myzone.reset</code>, which the server reads, and <code>myzone.dh</code>, a list of the zone's templates ready to copy into another template's <code>external</code> section. An error is reported with the line number and the token where vmc stopped:
−
or object. The program uses *messages* to operate.  Your program gets activated
+
  30: syntax error
−
when it receives a message that some command is being executed, or a certain amount
+
    Token: 'name'
−
of time has passed. You decide yourself in the program, what to wait for, before
 
−
  the program continues executing.
 
  
−
  Supposing you want your program to contain variables, you have to put them in a
+
A new zone is listed in <code>vme/etc/zonelist</code>. The server reads zones only when it starts, so restart it after compiling. Errors while a program runs, such as a template that isn't found, a type error or an endless loop, are written to the zone's log in <code>vme/bin/vme.log</code>.
−
section before the 'code' section marked 'var'. The variables are declared by
 
−
their type and their name, separated by a ':' and ended by a ';'
 
  
−
  For an example, see the program above. Variables of type 'string', 'stringlist'
+
== A complete example ==
−
and 'integer' are saved, if the unit the program is attached to is saved with
+
<pre>
−
player inventory. Variables of type 'unitpr' and 'extraptr' are 'volatile'. This
+
#include &lt;macros.h&gt;
−
means that they are cleared whenever there is a chance that their contents may
 
−
have been rendered non usable.  This ensures that you do not have any 'loose'
 
−
pointers. However it is possible to 'secure' the contents of a 'unitptr' type
 
−
variable of a unit located in your local environment (the units directly 'visible'
 
−
to the unit who owns the DIL program) (see the secure / unsecure functions).
 
−
<span id="making"></span>
 
  
−
<h3>'''Making a program:'''</h3>
+
%zone mytown
 +
lifespan 20
 +
reset RESET_ANYHOW
 +
creators {"manual"}
  
−
  You define your DIL programs within your zone file. Each program you make is a
+
%dil
−
*template*. Such a template must be equipped with a unique name (for that zone).
 
−
Templates can either be defined in a new %dil section, just below the %zone
 
−
section in the zone file, or directly attached to units defined in your zonefile.
 
  
−
  If you define your DIL templates inside a unit definition, that unit is
+
dilbegin town_crier(news : stringlist);
−
automatically assigned a program using that template. If you want to use an
+
var
−
already designed template, either in your own, or another zone, you use a
+
   i : integer;
−
special function named "dilcopy", that takes the name of a template, and any
+
code
−
optional parameters in parenthesis. The parameters of a template called with
+
{
−
'dilcopy' may only be of types integer, strings and stringlists.
+
  heartbeat := PULSE_SEC * 60;
−
 
 
−
'''Example:'''
 
−
 
 
−
dilcopy myfunc@myzone("say Hello, my friend!", 1, CMD_SAY,
 
−
);
 
−
 
 
−
   DIL templates can always be reused. In fact, with version 2.0 of DIL, programs
 
−
are no longer saved with equipment, but only a symbolic reference is used. That
 
−
way, if you make a change in your zone, ALL units using that program are changed.
 
−
This however requires you keep the name. Upon loading, if a template is not
 
−
found, the program is halted and rendered useless until such a reference can be
 
−
found during loading.
 
  
−
Technical note:
+
  :start:
 +
  wait(SFB_TICK | SFB_CMD, command(CMD_AUTO_TICK) or
 +
        (command("say") and (activator.type == UNIT_ST_PC)
 +
        and ("news" in argument)));
  
−
   When you use several 'dilcopy' in your zonefile, only one instance is present
+
   if (length(news) == 0)
−
game-time, thus saving tremendous amounts of memory.  It is similar to a shared
+
      quit;
−
library, in which code is shared  but variables are not.
+
  if (i &gt;= length(news))
 +
      i := 0;
 +
  exec("say Hear ye! " + news.[i], self);
 +
  i := i + 1;
 +
  goto start;
 +
}
 +
dilend
  
−
You may use your templates to define both procedure and functions for your other
+
%rooms
−
templates to call.
 
  
−
A template is defined by beginning with 'dilbegin' and ending with 'dilend'.
+
square
−
Inside the template your program section is defined, marked by the keyword 'code',
+
title "The Town Square"
−
followed by the program itself;
+
descr "Cobbled streets meet here around an old well."
 +
dilbegin no_sleep();
 +
code
 +
{
 +
  :start:
 +
  wait(SFB_CMD, command("sleep") and (activator.type == UNIT_ST_PC));
 +
  act("Not in the middle of the square, please.",
 +
      A_ALWAYS, activator, null, null, TO_CHAR);
 +
  block;
 +
  goto start;
 +
}
 +
dilend
 +
end
  
−
<nowiki>
+
%mobiles
  
−
dilbegin myprogram();
+
crier
−
var
+
names {"town crier", "crier"}
−
  i : integer;
+
title "the town crier"
−
  j : integer;
+
descr "The town crier stands by the well with a big bell."
 +
M_AVG_HUMAN(5, SEX_MALE)
 +
dilcopy town_crier@mytown({"The bridge is mended.", "Taxes are due on Friday."});
 +
end
  
−
code
+
%reset
−
dilend</nowiki>
 
  
−
  This simple template does nothing but making its owner say 'Hello world' once
+
load crier into square max 1
−
every 5 seconds. The template is called 'myprogram' and takes no arguments. The
 
−
'pause' command waits for the program to receive a timer message, whose interval
 
−
is set by the 'inline' variable 'heartbeat'. The 'self' variable is a unitptr
 
−
referring to the unit owning the DIL program. Other inline variables will be
 
−
explained later.
 
  
−
  For a DIL program to work, it must be part of a *unit*: a room, player, non-player
+
%end
−
or object. The program uses *messages* to operate. Your program gets activated
+
</pre>
−
when it receives a message that some command is being executed, or a certain amount
+
* <code>town_crier</code> is a template in <code>%dil</code> that takes a list of news. The crier's definition copies it onto every crier with two pieces of news. Every minute, and whenever a player says something with "news" in it, the crier says the next piece. The wait wakes for two kinds of event (<code>SFB_TICK | SFB_CMD</code>), and the condition lets ticks through with <code>command(CMD_AUTO_TICK)</code> and commands only when a player says "news". The crier doesn't hear its own <code>say</code>: a program isn't woken by its own unit's commands unless it is [[Manual:DIL Manual/aware|aware]].
−
of time has passed. You decide yourself in the program, what to wait for, before
+
* <code>no_sleep</code> is written inside the room, so the room runs it. It wakes before a player's <code>sleep</code> command runs, answers, and [[Manual:DIL Manual/block|block]]s the command so that it never happens.
−
the program continues executing.
+
* The reset loads one crier into the square.
  
−
  Supposing you want your program to contain variables, you have to put them in a
+
To try it, save it as <code>vme/zone/mytown.zon</code>, compile it, add <code>mytown</code> to <code>vme/etc/zonelist</code> and restart the server; a god can then <code>goto square@mytown</code>. Started as <code>vme -w</code>, the server lets the ticks pass without waiting (see [[Manual:DIL Manual/Ending Notes|Ending Notes]]).
−
section before the 'code' section marked 'var'. The variables are declared by
 
−
their type and their name, separated by a ':' and ended by a ';'
 
  
−
  For an example, see the program above. Variables of type 'string', 'stringlist'
+
== See also ==
−
and 'integer' are saved, if the unit the program is attached to is saved with
+
* [[Manual:DIL Manual/Data Types|Data Types]]
−
player inventory. Variables of type 'unitpr' and 'extraptr' are 'volatile'. This
+
* [[Manual:DIL Manual/DIL Constructs|DIL Constructs]]
−
means that they are cleared whenever there is a chance that their contents may
+
* [[Manual:DIL Manual/Messages|Messages]]
−
have been rendered non usable.  This ensures that you do not have any 'loose'
+
* [[Manual:DIL Manual/Functions|Functions]]
−
pointers. However it is possible to 'secure' the contents of a 'unitptr' type
+
* [[Manual:DIL Manual/dilbegin|dilbegin]]
−
variable of a unit located in your local environment (the units directly 'visible'
+
* [[Manual:DIL Manual/dilcopy()|dilcopy()]]
−
to the unit who owns the DIL program) (see the secure / unsecure functions).
+
* [[Manual:DIL Manual/wait()|wait()]]
 +
* [[Manual:DIL Manual/secure()|secure()]]

Latest revision as of 23:54, 14 November 2026

A DIL program is written in a zone file, compiled with the zone by vmc, and run by the game server on a unit: a room, an object, an NPC or a player. This page goes through a whole program from nothing: where it is written, its parts, how it gets onto a unit, how it runs and waits, and how to compile it. A complete example is at the end.

Templates

A program is written once, as a template, between dilbegin and dilend. Each unit that runs it gets its own copy: the copy has its own variables and its own place in the code, while the code itself is shared. Ten guards with the same template are ten programs that know nothing of each other's variables.

A template's full name is name@zone: its name (at most 15 characters) and the zone it is written in. Templates are also called by other templates, as procedures or as functions (see Functions).

Where DIL is written

  • The %dil section of a zone, after %zone and before %rooms, holds the zone's templates. Any unit of any zone can use them by their full name.
  • Inside a unit's definition: a dilbegin ... dilend written among a room's, object's or mobile's fields is a template of the zone too, and that unit gets a copy every time it is loaded. Such a template takes no arguments.
  • dilcopy name@zone(arguments); in a unit's definition gives every unit loaded from that definition a copy of the template. The arguments are constants: numbers, strings, and lists in braces ({"a", "b"}, {1, 2}). A unit can have several dilcopy lines. The %reset section has no dilcopy of its own: the program comes with the definition of the unit the reset loads.
  • At run time, dilcopy() puts a copy on any unit: dilcopy("name@zone(arg1,arg2)", u);, with string and integer arguments only.
  • As a player command: a template named with func = name@zone in vme/etc/commands.def runs when the command is typed. It takes one string argument, the rest of the line, and self is the character who typed it. Spells are templates named the same way in vme/etc/spells.def.

The zone must start with #include <macros.h>: dilend, pause and on_activation are macros from it, and vmc needs the ^ that dilend adds to find the end of each program.

The parts of a template

dilbegin [options] [type] name(arg : type, ...);
external
   other templates it calls
var
   its variables
code
{
   statements
}
dilend
  • The header names the template and its arguments. The options are aware, recall, unique and fnpri. A type before the name makes the template a function that returns a value of that type. See dilbegin.
  • external declares the templates this one calls, also those in the same zone.
  • var declares the variables, each with one of the eight data types. They start empty: 0, "", an empty list or null.
  • code holds the statements in one pair of braces. Labels for goto are written :name:.

Every function, field and built-in variable of DIL is a reserved word, so name, target, level, key and many more can't be variable names. vmc then reports a syntax error with that word as the token.

How a program runs

A copy starts at the first statement of code soon after its unit is loaded (or, for dilcopy(), at the unit's next tick). It runs until it waits, and waits most of its life:

  • wait(events, condition) stops the program until an event of one of the given kinds comes for which the condition is true. The events are the SFB_ values: a command typed nearby (SFB_CMD), a command done (SFB_DONE), a message from another program (SFB_MSG), the program's timer (SFB_TICK), and others; see Messages.
  • pause; waits for the program's next tick. The tick comes every heartbeat, which the program sets: heartbeat := PULSE_SEC * 30; is every 30 seconds.
  • interrupt() and on_activation() make the program react to some events whatever it is waiting for.

When an event wakes the program, the built-in variables activator, argument, medium, target and power describe it, and command(...) tells which command it was. self is always the program's own unit.

Two rules make DIL different from most languages:

  • At every wake, every unitptr and extraptr variable becomes null, unless the unit was kept with secure(). The unit may have left or been destroyed while the program slept. Copy what you need right after the wait, and find units again after a pause. Integers, strings and lists keep their values.
  • A program can't run forever without waiting. After about 52,000 statements without a wait (about 17,000 rounds of a one-line while loop) the server stops it and writes "had endless loop" to the zone's log.

The usual shape is a loop: a label, a wait, the work, and goto back to the label. A program ends for good with quit, or when its unit is destroyed. One that runs past the closing brace of code starts again from the top, with its variables as they were.

Programs on a player, and on the things a player carries, are saved with the player. Their integer, string and list variables are saved too; with recall the program also goes on from where it waited, instead of starting again.

Compiling

Zone files are in vme/zone/. Compile a zone with vmc, the zone compiler:

cd vme/zone
../bin/vmc -m -d ../etc/ -I../include/ myzone.zon

-m compiles only zones that have changed; -s checks the syntax without writing any files. vmc writes myzone.data and myzone.reset, which the server reads, and myzone.dh, a list of the zone's templates ready to copy into another template's external section. An error is reported with the line number and the token where vmc stopped:

30: syntax error
    Token: 'name'

A new zone is listed in vme/etc/zonelist. The server reads zones only when it starts, so restart it after compiling. Errors while a program runs, such as a template that isn't found, a type error or an endless loop, are written to the zone's log in vme/bin/vme.log.

A complete example

#include <macros.h>

%zone mytown
lifespan 20
reset RESET_ANYHOW
creators {"manual"}

%dil

dilbegin town_crier(news : stringlist);
var
   i : integer;
code
{
   heartbeat := PULSE_SEC * 60;

   :start:
   wait(SFB_TICK | SFB_CMD, command(CMD_AUTO_TICK) or
        (command("say") and (activator.type == UNIT_ST_PC)
         and ("news" in argument)));

   if (length(news) == 0)
      quit;
   if (i >= length(news))
      i := 0;
   exec("say Hear ye! " + news.[i], self);
   i := i + 1;
   goto start;
}
dilend

%rooms

square
title "The Town Square"
descr "Cobbled streets meet here around an old well."
dilbegin no_sleep();
code
{
   :start:
   wait(SFB_CMD, command("sleep") and (activator.type == UNIT_ST_PC));
   act("Not in the middle of the square, please.",
       A_ALWAYS, activator, null, null, TO_CHAR);
   block;
   goto start;
}
dilend
end

%mobiles

crier
names {"town crier", "crier"}
title "the town crier"
descr "The town crier stands by the well with a big bell."
M_AVG_HUMAN(5, SEX_MALE)
dilcopy town_crier@mytown({"The bridge is mended.", "Taxes are due on Friday."});
end

%reset

load crier into square max 1

%end
  • town_crier is a template in %dil that takes a list of news. The crier's definition copies it onto every crier with two pieces of news. Every minute, and whenever a player says something with "news" in it, the crier says the next piece. The wait wakes for two kinds of event (SFB_TICK | SFB_CMD), and the condition lets ticks through with command(CMD_AUTO_TICK) and commands only when a player says "news". The crier doesn't hear its own say: a program isn't woken by its own unit's commands unless it is aware.
  • no_sleep is written inside the room, so the room runs it. It wakes before a player's sleep command runs, answers, and blocks the command so that it never happens.
  • The reset loads one crier into the square.

To try it, save it as vme/zone/mytown.zon, compile it, add mytown to vme/etc/zonelist and restart the server; a god can then goto square@mytown. Started as vme -w, the server lets the ticks pass without waiting (see Ending Notes).

See also