Manual:Zone Manual/The NPC Section/DIL functions for NPCs

From DikuMUD Wiki
< Manual:Zone Manual‎ | The NPC Section
Revision as of 06:17, 15 November 2026 by Agent-mud (talk | contribs) (checked against the current code (wiki review 2026-11-15))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

DIL functions for NPCs

DIL is the language for making rooms, NPCs and objects do things. This manual doesn't teach you to write DIL; the DIL manual does. But DikuMUD3 comes with many ready-made DIL functions (templates) that you can put on an NPC without writing any DIL yourself. This page describes the ones for NPCs.

You put a template on an NPC with dilcopy in its definition:

dilcopy aggressive@function(0, -1, 0, 0, 3, PULSE_SEC*5, 0, "ANY", {"$1n attacks $3n!", "$1n attacks you!"});
  • The name is template@zone. Most of these live in vme/zone/function.zon; a few in other zones, such as guilds.zon and midgaard.zon.
  • The arguments are numbers (integer), strings in double quotes (string), string lists in braces such as {"a", "b"} (stringlist), number lists such as {4, 23} (intlist), and null for an empty list. Macros from the headers work in them, for example PULSE_SEC*10 or RACE_DWARF.
  • The line ends with ;.
  • vmc checks neither the name nor the arguments. A misspelt template or a wrong argument compiles fine, and shows up only at boot or when the NPC acts, as an error in vme/bin/vme.log. Before you use a template, read its dilbegin line, and the comment above it, in the zone file on GitHub.
  • Times are often in ticks: 4 ticks are one second. PULSE_SEC (4) and WAIT_SEC (4) are macros for it.
  • In messages, $1n, $2n and $3n are replaced by names of the units involved; each function below says which is which. See act().

You can give an NPC as many of these as you like; the janitor in A more complex set of NPCs has two. In the game, wstat <symbol>@<zone> func lists the DIL programs an NPC runs.

Many NPC templates in macros.h already add some of these functions; for example the black dragon templates add aggressive and combat_mag. See NPC macros.

Fighting

aggressive

Makes the NPC attack players who come into its room, or do something there, under the conditions you give. It attacks only players it can see, never gods, and only while it is awake and not already fighting.

dilbegin aggressive (sx : integer, rce : integer, opp : integer,
                     levl : integer, sanc : integer, tme : integer,
                     tar : integer, align : string, attack : stringlist);
Argument Type Meaning
sx integer The victim's sex (not the SEX_ values): 0 any, 1 the opposite of the NPC's own (not for a neutral NPC), 2 male, 3 female, 4 neutral.
rce integer A player race, RACE_HUMAN (0) to RACE_DARK_ELF (10), or -1 for any race.
opp integer What to do with rce: 0 ignore it, 1 attack only that race, 2 attack every race but that one.
levl integer 0: any level. 30: players of level 30 and above. -30: players of level 30 and below. Allowed: -49 to 50.
sanc integer Whether the NPC spares protected players: 0 no, 1 those with sanctuary, 2 those with soothe, 3 both.
tme integer How long it waits before attacking, in ticks, 0 to 400 (100 seconds). The real wait is random, between about half and one and a half times this.
tar integer Which of the players who qualify it picks: -2 the last to enter the room, -1 the weakest, 0 a random one, 1 the strongest, 2 the first to enter.
align string The victim's alignment: "ANY", "GOOD", "EVIL", "NEUTRAL", "OPPOSITE" (the opposite of the NPC's; not for a neutral NPC), "SALIGN" (the same as the NPC's) or "DALIGN" (any other than the NPC's).
attack stringlist The attack messages: {"to the others", "to the victim"}. $1n is the NPC, $3n the victim. With one string, everyone in the room sees it (don't use $3n then); with {} or {""} there is no message.

If an argument is out of range the function logs "AGGRO ERROR" in vme.log and stops.

A level 40 goblin who hates evil female dwarves of level 20 and above, spares players with sanctuary (but not soothe), and waits about 10 seconds before attacking:

dilcopy aggressive@function(3, RACE_DWARF, 1, 20, 1, PULSE_SEC*10, 0, "EVIL",
                            {"$1n savagely attacks $3n with his big axe!",
                             "$1n attacks you!"});

A monster that attacks anyone, after about 20 seconds, with one message for all:

dilcopy aggressive@function(0, -1, 0, 0, 3, PULSE_SEC*20, 0, "ANY",
                            {"Big monster attacks!"});
combat_mag

Makes the NPC cast spells while it fights: an attack spell, and a defence spell (such as a heal on itself) when its hit points run low.

dilbegin fnpri(FN_PRI_COMBAT) combat_mag(atk_spl : string, def_spl : string, def_pct : integer,
                    spd: integer);
Argument Type Meaning
atk_spl string The attack spell, as a player types it after cast, for example "fireball", or "" for none.
def_spl string The defence spell, for example "heal", or "" for none.
def_pct integer When the NPC's hit points are at or below this percentage of its maximum, it casts the defence spell.
spd integer How often it casts the attack spell: 1 every combat round, 2 every second round, and so on. 2 is a good choice.

The defence spell comes first whenever the hit points are low. With a defence spell, the NPC casts its attack spell only while it has 40 mana or more, so that some is left for the defence; without one, 16 or more. The spell names are in vme/etc/spells.def.

dilcopy combat_mag@function("harm", "heal", 25, 2);
comb_poison and comb_acid

In a fight, about half of the NPC's hits also poison the victim (comb_poison) or burn them with acid (comb_acid). The two messages go to the victim and to the others in the room; $1n is the NPC, $3n the victim.

dilbegin aware comb_poison(toact : string, restact : string);
dilbegin aware comb_acid(toact : string, restact : string);
dilcopy comb_poison@function("You feel very ill as $1n bites you!",
                             "$1n sinks $1s fangs deep into $3n!");
teamwork

The NPC joins the fight when one of the NPCs it protects is attacked, if they are in the same room.

dilbegin aware fnpri(FN_PRI_RESCUE) teamwork(lst: string);

lst lists the names of the NPCs to help, separated by /. A name matches one of the names in the other NPC's names.

dilcopy teamwork@function("guard/captain");

Note: at present the function skips NPCs of its own zone, so it helps only NPCs from other zones.

rescue

The NPC rescues one of the NPCs it protects when that NPC is fighting in the same room and is down to half its hit points or less. Both must come from the same zone. It uses the rescue skill, so it can fail.

dilbegin aware fnpri(FN_PRI_RESCUE) rescue(lst: string);

lst is as for teamwork. NPCs often have both:

dilcopy teamwork@function("guard/captain");
dilcopy rescue@function("guard/captain");

Moving around

wander_zones

The NPC walks through random exits, but only into rooms of the zones you list, and never into a room with UNIT_FL_NO_MOB. It stands up if it is sitting or resting, and does nothing while asleep. If it finds itself outside its zones, it waits five minutes and tries again.

dilbegin wander_zones(zones : string, spd : integer, doors : integer, lckd_doors : integer);
Argument Type Meaning
zones string Zone names separated by spaces. @loadzone in the list stands for the zone the NPC is in when it starts.
spd integer Seconds between steps; less than 5 counts as 5.
doors integer 1: it opens closed doors to go through, and closes them behind it. 0: it doesn't.
lckd_doors integer 1: it also goes through locked doors, without a key, and locks them again behind it. 0: it doesn't.
dilcopy wander_zones@function("haon_dor @loadzone", 30, 1, 0);

An NPC with an extra named $block wander stops wandering.

global_wander

Like wander_zones, but the NPC may wander into any zone.

dilbegin global_wander(spd : integer, doors : integer, lckd_doors : integer);

The arguments are those of wander_zones without zones; a speed below 2 counts as 5 seconds.

dilcopy global_wander@function(60, 1, 0);

Guards

These guards block only in the room where the NPC was when the function started, normally the room it was loaded into. Moved away, it doesn't block until it is back.

guard_dir

The NPC stops characters from leaving in one direction, except those you name.

dilbegin fnpri(FN_PRI_MISSION) guard_dir(direction : string,
                   excludepc : stringlist,
                   excludenpc : stringlist,
                   stopdil : string);
Argument Type Meaning
direction string The direction to block: "north", "south"... "up", "down", "northeast"...
excludepc stringlist The players allowed through, by name, or null.
excludenpc stringlist The NPCs allowed through, by name, or null.
stopdil string "" for the normal messages ("<guard> stops you from going south!"). Otherwise the name of a DIL procedure that takes the blocked character and the direction, (u : unitptr, dir : string), and is called instead; this needs DIL of your own.
dilcopy guard_dir@function("south", {"papi", "whistler"}, {"guard", "captain"}, "");
dilcopy guard_dir@function("east", {"rejji"}, null, "");
guard_door

The NPC stops everyone from opening, closing, locking, unlocking or picking the door in one direction, or casting a spell at it.

dilbegin fnpri(FN_PRI_MISSION) guard_door(dir:integer,as:string,ao:string);

dir is a direction constant: NORTH, EAST, SOUTH, WEST, UP, DOWN... as is the message to the character who tried, ao the message to the others; $1n is the guard, $3n the character.

dilcopy guard_door@function(WEST, "$1n stops you.",
                            "$1n prevents $3n from opening the grating.");
guard_unit

The same for an object in the room, such as a chest or a safe: nobody may open, close, lock, unlock or pick it, or cast at it. The first argument is the object's full symbolic name, symbol@zone.

dilbegin fnpri(FN_PRI_MISSION) guard_unit(us:string,as:string,ao:string);
dilcopy guard_unit@function("safe@midgaard", "$1n stops you.",
                            "$1n prevents $3n from opening the safe.");

Cleaning up

fido

The NPC eats NPC corpses and food that lie in its room, every 10 to 30 seconds. When it eats a corpse, what the corpse held drops to the floor. It doesn't eat player corpses or buried things.

dilbegin fido(txt1:string,txt2:string);
Argument Type Meaning
txt1 string The message when it eats a corpse. "" gives "<npc> savagely devours a corpse.", "stop" means it doesn't eat corpses.
txt2 string The message when it eats food. "" gives "<npc> hungrily devours <food>.", "stop" means it doesn't eat food.

In the messages $1n is the NPC and $2n what it eats.

dilcopy fido@function("$1n slowly devours $2n, crunching the bones.",
                      "$1n grabs $2n and hungrily munches it.");
janitors

This one is in midgaard.zon, so it is janitors@midgaard. The NPC tidies its room: it picks up things it can carry and puts them in a trash bag (which it gets when the function starts), buries NPC corpses after taking what is in them, tells a player where their corpse lies, gives away food and drink, and throws out slime. It leaves containers alone, and does nothing in rooms where burying isn't allowed (UNIT_FL_NO_BURY). If it loses its trash bag, it stops wandering and sings a song instead.

dilbegin fnpri(FN_PRI_MISSION) janitors(rate: integer);

rate is the number of seconds between two rounds of tidying. The janitor doesn't move by itself; give it wander_zones or global_wander too.

dilcopy janitors@midgaard(15);
dilcopy wander_zones@function("@loadzone", 20, 1, 0);

Shops and services

shopkeepernew

The NPC runs a shop. Players can list what it sells, buy, sell, value (what it would pay) and request (what it asks); see Help: Shops. It restocks once a mud day, and buys from players only while it has money left for the day.

dilbegin aware shopkeepernew(prod       : stringlist,
                             custom_acts : stringlist,
                             itemtypes  : intlist,
                             sellprofit : integer,    buyprofit   : integer,
                             maxcash    : integer, shophome : integer);
Argument Type Meaning
prod stringlist What it sells. Each entry is "symbol@zone daily max": the object, how many come into stock each mud day, and the most it keeps. Both numbers must be more than 0.
custom_acts stringlist Its messages, in the order below. An empty or missing message gets the default.
itemtypes intlist The kinds of object it buys from players, as ITEM_ constants (from vme/include/vme.h), for example {ITEM_FOOD, ITEM_DRINKCON}.
sellprofit integer Its selling price, as a percentage of the object's cost: 110 means 10% more than the cost.
buyprofit integer What it pays players, as a percentage of the cost: 50 means half.
maxcash integer The money it has each mud day to buy things from players, in money units (an iron piece is 10; 2*PLATINUM_MULT is two platinum pieces).
shophome integer TRUE: it serves only in the room where it was loaded, and walks back there if moved. FALSE: a wandering trader.

The messages, in order. $1n is the shopkeeper, $2n the object and $3n the player.

  1. The player asks for something it doesn't have.
  2. The player tries to sell or value something they don't have.
  3. The player tries to sell something of a kind it doesn't buy.
  4. The player can't afford it.
  5. After a purchase. (Not shown at present: it says "That cost you ..." instead.)
  6. After a sale. (Not shown at present: it says "I paid you ..." instead.)
  7. It hasn't got as many as the player asked for.
  8. The object can't be sold (it has an extra named $no-sell).
  9. It has no money left to buy with.

shopkeepernew has no opening hours. For those, add opening_hours (below).

Here is Hogan, a bartender. The lists are made into macros with #define, at the top of the zone file, to keep the NPC readable; a \ at the end of a line continues the macro on the next line.

#define TAVERN_PROD {"tuborg@udgaard 5 20", "beer_bottle@midgaard 10 30"}

#define TAVERN_MSG \
{"$1n says, 'This is a tavern, I don't sell such an item as that!'", \
 "$1n says, '$3n, you don't even have that!'", \
 "$1n says, 'I don't trade in things such as $2n! Just buy a beer!'", \
 "$1n says, '$3n, you can't afford $2n.'", \
 "$1n says, 'Here you are.'", \
 "$1n says, 'Thank you, $3n.'", \
 "$1n says, 'I don't have that many $2Ns in stock.'", \
 "$1n says, 'I haven't got a use for $2n.'", \
 "$1n says, 'I'd like to buy it, but I can't afford it, sorry.'"}

#define TAVERN_TYPES {ITEM_DRINKCON, ITEM_FOOD}

%mobiles

bartender
names {"Hogan", "goblin", "bartender"}
title "Hogan"
descr "Hogan stands behind the bar, waiting to take your order."
extra {}
"He looks back at you, patiently waiting for your order."
M_SHOP_KEEPER(30, SEX_MALE, RACE_GOBLIN)
money 2 SILVER_PIECE, 2 COPPER_PIECE

dilcopy opening_hours@function({8, 23}, {"list", "buy", "sell", "value", "request"},
   "say Sorry, we're closed.",
   {"stand", "say The bar is open!"},
   {"say The bar is closed, come back at eight.", "rest"});

dilcopy shopkeepernew@function(TAVERN_PROD, TAVERN_MSG, TAVERN_TYPES,
                               110, 50, 2*PLATINUM_MULT, TRUE);
end

Hogan sells Tuborgs from Udgaard and bottles of beer from Midgaard at 110% of their cost, buys drinks and food from players at half their cost, and has two platinum pieces a day for that.

opening_hours

Closes a shop (or anything else) outside its opening hours: it blocks the commands you list, and the NPC does what you give it when it opens and closes.

dilbegin fnpri(FN_PRI_BLOCK-1) opening_hours(shophours  : intlist,
                                             blockcmds  : stringlist,
                                             blockexec  : string,
                                             opencmds   : stringlist,
                                             closecmds  : stringlist);
Argument Type Meaning
shophours intlist Pairs of mud hours, open from the first to the second, both included: {8, 23}, or {4, 10, 12, 20} for two periods. A period can't cross midnight: write {22, 23, 0, 6}, not {22, 6}.
blockcmds stringlist The commands it refuses while closed.
blockexec string The command the NPC does when it refuses one, for example "say We're closed.".
opencmds stringlist Commands the NPC does when it opens.
closecmds stringlist Commands the NPC does when it closes.

See Hogan above for an example.

shopkeeper

The older form of the shop, still used by many shops in the public zones. It passes its arguments on to shopkeepernew; new shops should use shopkeepernew directly.

dilbegin aware shopkeeper(prod       : stringlist, custom_acts : stringlist,
                          opentimes  : stringlist, itemtype    : string,
                          sellprofit : integer,    buyprofit   : integer,
                          maxcash    : integer,
                          closedil   : string,     dilparams   : string );

The differences: custom_acts has ten messages, with "the shop is closed" as the eighth; itemtype is a string of numbers, such as "17 19"; and opentimes, closedil and dilparams are ignored, so the shop is always open unless you add opening_hours. The shopkeeper always stays in its shop. At present, from the seventh message on, the messages come out in the wrong places; another reason to use shopkeepernew.

evaluate

For a fee, the NPC tells a player about a weapon, armour or shield they carry: the kind of weapon, armour or shield, and how good its craftsmanship is. The player types evaluate <item>.

dilbegin aware evaluate (amt: integer);

amt is the fee in iron pieces; 64 is one silver piece.

dilcopy evaluate@function(64);
mercenary_hire

The NPC can be hired to kill a player. offer <player> tells you the price, contract <player> pays it. The mercenary then sneaks to the victim, fights until the victim is dead, takes the loot and goes back home. It refuses players of level 20 or below and gods; only gods can hire it against an NPC. The price is 20 platinum pieces plus a little per level of the victim.

dilbegin mercenary_hire();
dilcopy mercenary_hire@function();

Pets and followers

obey

The NPC obeys the character it follows. When that character types tell <npc> <command> in the same room, the NPC does the command, for example tell familiar cast heal papi. Others are ignored.

dilbegin obey();
dilcopy obey@function();
obey_animal

obey for animals: the animal obeys only simple commands (moving, kill, get, drop, give, follow, flee, look, sit, sleep... but no spells or talking). Don't add obey as well; obey_animal adds it itself.

dilcopy obey_animal@function();

Teachers

Teachers themselves are made with special SFUN_TEACH_INIT, described in A more complex set of NPCs. These functions limit who may practise with them.

teach_q_block

Only players who have done a quest may practice. Others are refused, and the NPC does the command s.

dilbegin fnpri(FN_PRI_BLOCK) teach_q_block(q : string, s : string);

q is the quest as the player has it, for a finished quest usually "<name> Complete".

dilcopy teach_q_block@function("Mary's Pot Complete", "say Buggar ye off!");

shop_q_block@function takes the same arguments and does the same for buy and sell at a shop.

TrainMemberChk

In guilds.zon: only members of the guild may practice. The second argument is the command the NPC does when it refuses a player, or "" for "say You're not a member of our guild, go speak with the guild master."

dilbegin fnpri(FN_PRI_BLOCK+1) TrainMemberChk(sGuild : string, sMsg : string);
dilcopy TrainMemberChk@guilds(GUILD_UDG_FIGHTER, "");

The guild masters' functions (MasterJoin, SetTitles, MurderBanish) are also in A more complex set of NPCs.

More

function.zon holds more than this page shows: restrictions for objects (abi_restrict, guild_restrict, ...), functions for rooms (death_room, force_move, safe_room, climb), ships and others. The comments above each dilbegin explain them. The quest helpers for NPCs are in quests.zon.

Previous: Compiling and debugging your first NPC
Next: A more complex set of NPCs