Manual:Zone Manual/General Compiler Information/The VMC pre-processor

From DikuMUD Wiki
< Manual:Zone Manual‎ | General Compiler Information
Revision as of 06:19, 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

The VMC pre-processor

Before vmc reads a zone, its pre-processor goes through the file. It works like a search and replace tool: it removes comments, replaces macros with what they stand for, and pastes in include files. It works like the C pre-processor (cpp); if you know that one, skim this page for the differences at the end.

To see what the pre-processor made of your zone, run vmc with -p. It prints the zone after pre-processing and compiles nothing:

../bin/vmc -p -d ../etc/ -I../include/ swamp.zon

Comments

Comments are notes for you and other builders; vmc never sees them. Write them between /* and */ (they can span several lines), or after // to the end of the line:

/*
   The following ten rooms are the swamp,
   there are 97 rooms in the zone.
*/

// Zone first written in 1994

A comment can go anywhere outside a string, even inside a field:

// I am going to make a title now
title /* the keyword first */ "A mud hut" /* then the title */

To vmc this is the same as title "A mud hut". Inside a string, /* and // are ordinary text: title "A hut /* old */" gives the hut that whole title.

Use /* */ for a comment of several lines and // for a single line. Comments matter most in DIL programs, but a note on why a room is set up the way it is helps whoever keeps your zone working later.

Macros

Some things you write many times. Say fifty rooms of your zone are huts with the same flags. Instead of writing

flags {UNIT_FL_NO_WEATHER, UNIT_FL_INDOORS}

fifty times, define a macro once, near the top of the zone:

#define HUTFLAGS flags {UNIT_FL_NO_WEATHER, UNIT_FL_INDOORS}

and write HUTFLAGS in each room. If you later want the huts to be safe from teleporting too, change only the macro, and all fifty rooms change the next time you compile:

#define HUTFLAGS flags {UNIT_FL_NO_WEATHER, UNIT_FL_INDOORS, UNIT_FL_NO_TELEPORT}

A macro can take arguments. In a swamp every direction looks the same, so the exits can share one description:

#define SEXIT(direction, place) direction to place descr \
"You see the swamp stretch out for miles.";

Then each exit is one short line:

SEXIT(east, swamp1)
SEXIT(north, swamp2)
SEXIT(south, swamp3)

Some rules:

  • In the #define, the ( must follow the macro's name with no space. #define SEXIT (direction, place) ... defines a macro without arguments whose text starts with (direction, place). Where you use the macro, a space is allowed: SEXIT (east, swamp1) works.
  • A #define is one line. To go on to the next line, end the line with \.
  • A comma inside braces still separates arguments, so NAMED({"wooden box", "box"}) passes two broken arguments. Define the list as a macro of its own and pass that:
#define BOX_NAMES {"wooden box", "box"}
...
names BOX_NAMES
  • Write your macros in capitals, so that they stand out from the fields. The headers do the same, with a few exceptions: the directions north ... southwest, up and down are macros (north becomes exit[NORTH]), and so are dilend, pause and on_activation. Don't use these words as names of your own.

Macros can use other macros:

#define HUTFLAGS flags {UNIT_FL_NO_WEATHER, UNIT_FL_INDOORS, UNIT_FL_NO_TELEPORT}
#define MUDHUT movement SECT_INSIDE \
   HUTFLAGS

Most of the macros you will use are already written: the headers define a macro for every kind of weapon, armour, container, drink and money, and for hundreds of NPC races and professions. The room, object and NPC chapters show them.

Including other files

#include pastes another file into your zone. Every zone starts with

#include <macros.h>

macros.h includes the other headers you need: values.h and vme.h (the constants), wmacros.h (the object and DIL macros), liquid.h (drinks), and the NPC templates in mhumanoid.h, chumanoid.h, monster.h, fantasy.h, plant.h and aquatic.h. The headers are in vme/include/ (on GitHub); read them to see what a macro does. Nothing is included unless you ask for it.

vmc looks for #include <file> in the directories given with -I. It looks for #include "file" next to the zone file first, then in the -I directories. You can write your own header, for example with the macros shared by several of your zones, and include it the same way.

Put the includes and your #defines at the top of the file: a macro must be defined before it is used.

Conditions

#if, #ifdef, #ifndef, #elif, #else and #endif keep or drop parts of the file, and #undef removes a macro. #error "message" stops the compile with that message (the quotes are needed). The guild zones use conditions to read different parts of one file: a teacher does #define _ABILITY, then #include "guilddef.h", whose parts start with #if defined(_ABILITY), #elif defined(_SKILL) and so on, and then #undef _ABILITY.

Calculations

Wherever a field takes a number, you can write a sum: +, -, *, / and parentheses work, with whole numbers (/ drops the remainder). vmc, not the pre-processor, does the sum, so a macro can pass its arguments into one:

#define CHEST(size) \
   weight 5 + (size) / 4 \
   capacity (size)

CHEST(40) becomes weight 5 + (40) / 4 capacity (40): a weight of 15 and a capacity of 40. Put parentheses around each argument, as above. The pre-processor only pastes text, so without them CHEST(20+20) would give weight 5 + 20+20 / 4, which is 30.

The NPC headers work this way. bottom.h sets an NPC's height from its level:

#define SETHEIGHT(MLVL,minh,maxh) \
height (((((maxh-minh)+1)*((MLVL*100)/199))/100)+minh)

An example

This zone uses everything above. It compiles as swamp.zon:

#include <macros.h>

/*
   The swamp: a mud hut and the swamp around it.
*/

%zone swamp
lifespan 20
reset RESET_NOT
creators {"manual"}

#define HUTFLAGS flags {UNIT_FL_NO_WEATHER, UNIT_FL_INDOORS, UNIT_FL_NO_TELEPORT}

#define MUDHUT movement SECT_INSIDE \
   HUTFLAGS

#define SEXIT(direction, place) direction to place descr \
"You see the swamp stretch out for miles.";

#define CHEST(size) \
   weight 5 + (size) / 4 \
   capacity (size)

#define BOX_NAMES {"wooden box", "box"}

%rooms

hut
// I am going to make a title now
title /* the keyword first */ "A mud hut" /* then the title */
descr "A low hut of mud and reeds."
MUDHUT
east to swamp1 descr "The swamp.";
end

swamp1
title "In the swamp"
descr "Mud, reeds and more mud."
movement SECT_SWAMP
west to hut descr "A mud hut.";
SEXIT(north, swamp2)
end

swamp2
title "Deeper in the swamp"
descr "The mud reaches your knees."
movement SECT_SWAMP
SEXIT(south, swamp1)
end

%objects

chest
names {"oak chest", "chest"}
title "an oak chest"
descr "An oak chest stands in the corner."
CHEST(40)
end

box
names BOX_NAMES
title "a wooden box"
descr "A wooden box lies here."
end

%end

Differences from cpp

The pre-processor is not GNU cpp, but most of what cpp does works: #define with arguments, # and ## in macros, #include, #if and its relatives, #undef and #error. Directives may be indented. Macros with a variable number of arguments (...) don't work, and #error wants its message in quotes. Its own manual is vme/doc/pp.pdf in the repository.

Previous: General Compiler Information
Next: Zone Source File