Difference between revisions of "Manual:Zone Manual/General Compiler Information/The VMC pre-processor"
(checked against the current code (wiki review 2026-11-15)) |
|||
| Line 1: | Line 1: | ||
=== The VMC pre-processor === | === 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 <code>-p</code>. It prints the zone after pre-processing and compiles nothing: | |
| − | + | <pre> | |
| − | + | ../bin/vmc -p -d ../etc/ -I../include/ swamp.zon | |
| − | + | </pre> | |
| − | |||
| − | |||
| − | + | ==== Comments ==== | |
| − | + | Comments are notes for you and other builders; vmc never sees them. Write them between <code>/*</code> and <code>*/</code> (they can span several lines), or after <code>//</code> to the end of the line: | |
| + | <pre> | ||
| + | /* | ||
| + | The following ten rooms are the swamp, | ||
| + | there are 97 rooms in the zone. | ||
| + | */ | ||
| − | + | // Zone first written in 1994 | |
| − | + | </pre> | |
| − | + | A comment can go anywhere outside a string, even inside a field: | |
| − | + | <pre> | |
| − | + | // I am going to make a title now | |
| − | + | title /* the keyword first */ "A mud hut" /* then the title */ | |
| − | + | </pre> | |
| − | + | To vmc this is the same as <code>title "A mud hut"</code>. Inside a string, <code>/*</code> and <code>//</code> are ordinary text: <code>title "A hut /* old */"</code> gives the hut that whole title. | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | Use <code>/* */</code> for a comment of several lines and <code>//</code> 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 | |
| − | + | <pre> | |
| − | + | flags {UNIT_FL_NO_WEATHER, UNIT_FL_INDOORS} | |
| + | </pre> | ||
| + | fifty times, define a macro once, near the top of the zone: | ||
| + | <pre> | ||
| + | #define HUTFLAGS flags {UNIT_FL_NO_WEATHER, UNIT_FL_INDOORS} | ||
| + | </pre> | ||
| + | and write <code>HUTFLAGS</code> 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: | ||
| + | <pre> | ||
| + | #define HUTFLAGS flags {UNIT_FL_NO_WEATHER, UNIT_FL_INDOORS, UNIT_FL_NO_TELEPORT} | ||
| + | </pre> | ||
| − | + | A macro can take arguments. In a swamp every direction looks the same, so the exits can share one description: | |
| − | + | <pre> | |
| + | #define SEXIT(direction, place) direction to place descr \ | ||
| + | "You see the swamp stretch out for miles."; | ||
| + | </pre> | ||
| + | Then each exit is one short line: | ||
| + | <pre> | ||
| + | SEXIT(east, swamp1) | ||
| + | SEXIT(north, swamp2) | ||
| + | SEXIT(south, swamp3) | ||
| + | </pre> | ||
| − | + | Some rules: | |
| − | + | * In the <code>#define</code>, the <code>(</code> must follow the macro's name with no space. <code>#define SEXIT (direction, place) ...</code> defines a macro without arguments whose text starts with <code>(direction, place)</code>. Where you use the macro, a space is allowed: <code>SEXIT (east, swamp1)</code> works. | |
| − | + | * A <code>#define</code> is one line. To go on to the next line, end the line with <code>\</code>. | |
| − | + | * A comma inside braces still separates arguments, so <code>NAMED({"wooden box", "box"})</code> passes two broken arguments. Define the list as a macro of its own and pass that: | |
| − | + | <pre> | |
| − | + | #define BOX_NAMES {"wooden box", "box"} | |
| − | + | ... | |
| − | + | names BOX_NAMES | |
| + | </pre> | ||
| + | * Write your macros in capitals, so that they stand out from the fields. The headers do the same, with a few exceptions: the directions <code>north</code> ... <code>southwest</code>, <code>up</code> and <code>down</code> are macros (<code>north</code> becomes <code>exit[NORTH]</code>), and so are <code>dilend</code>, <code>pause</code> and <code>on_activation</code>. Don't use these words as names of your own. | ||
| − | + | Macros can use other macros: | |
| − | + | <pre> | |
| − | + | #define HUTFLAGS flags {UNIT_FL_NO_WEATHER, UNIT_FL_INDOORS, UNIT_FL_NO_TELEPORT} | |
| − | + | #define MUDHUT movement SECT_INSIDE \ | |
| − | + | HUTFLAGS | |
| − | + | </pre> | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | 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 ==== | |
| − | + | <code>#include</code> pastes another file into your zone. Every zone starts with | |
| − | + | <pre> | |
| − | + | #include <macros.h> | |
| − | + | </pre> | |
| − | + | <code>macros.h</code> includes the other headers you need: <code>values.h</code> and <code>vme.h</code> (the constants), <code>wmacros.h</code> (the object and DIL macros), <code>liquid.h</code> (drinks), and the NPC templates in <code>mhumanoid.h</code>, <code>chumanoid.h</code>, <code>monster.h</code>, <code>fantasy.h</code>, <code>plant.h</code> and <code>aquatic.h</code>. The headers are in <code>vme/include/</code> ([https://github.com/Seifert69/DikuMUD3/tree/master/vme/include on GitHub]); read them to see what a macro does. Nothing is included unless you ask for it. | |
| − | |||
| − | |||
| − | |||
| − | + | vmc looks for <code>#include <file></code> in the directories given with <code>-I</code>. It looks for <code>#include "file"</code> next to the zone file first, then in the <code>-I</code> 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 <code>#define</code>s at the top of the file: a macro must be defined before it is used. | |
| − | |||
| − | + | ==== Conditions ==== | |
| − | + | <code>#if</code>, <code>#ifdef</code>, <code>#ifndef</code>, <code>#elif</code>, <code>#else</code> and <code>#endif</code> keep or drop parts of the file, and <code>#undef</code> removes a macro. <code>#error "message"</code> 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 <code>#define _ABILITY</code>, then <code>#include "guilddef.h"</code>, whose parts start with <code>#if defined(_ABILITY)</code>, <code>#elif defined(_SKILL)</code> and so on, and then <code>#undef _ABILITY</code>. | |
| − | + | ||
| − | + | ==== Calculations ==== | |
| − | + | Wherever a field takes a number, you can write a sum: <code>+</code>, <code>-</code>, <code>*</code>, <code>/</code> and parentheses work, with whole numbers (<code>/</code> drops the remainder). vmc, not the pre-processor, does the sum, so a macro can pass its arguments into one: | |
| − | + | <pre> | |
| − | + | #define CHEST(size) \ | |
| − | + | weight 5 + (size) / 4 \ | |
| − | This | + | capacity (size) |
| − | + | </pre> | |
| − | + | <code>CHEST(40)</code> becomes <code>weight 5 + (40) / 4 capacity (40)</code>: 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 <code>CHEST(20+20)</code> would give <code>weight 5 + 20+20 / 4</code>, which is 30. | |
| − | + | ||
| − | + | The NPC headers work this way. <code>bottom.h</code> sets an NPC's height from its level: | |
| − | + | <pre> | |
| − | + | #define SETHEIGHT(MLVL,minh,maxh) \ | |
| − | + | height (((((maxh-minh)+1)*((MLVL*100)/199))/100)+minh) | |
| − | + | </pre> | |
| + | |||
| + | ==== An example ==== | ||
| + | This zone uses everything above. It compiles as <code>swamp.zon</code>: | ||
| + | <pre> | ||
| + | #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 | ||
| + | </pre> | ||
| + | |||
| + | ==== Differences from cpp ==== | ||
| + | The pre-processor is not GNU cpp, but most of what cpp does works: <code>#define</code> with arguments, <code>#</code> and <code>##</code> in macros, <code>#include</code>, <code>#if</code> and its relatives, <code>#undef</code> and <code>#error</code>. Directives may be indented. Macros with a variable number of arguments (<code>...</code>) don't work, and <code>#error</code> wants its message in quotes. Its own manual is <code>vme/doc/pp.pdf</code> in the repository. | ||
<div style='padding-top: 30px; padding-bottom: 20px; text-align: left;float:left;width:50%;'>[[Manual:Zone_Manual/General_Compiler_Information|Previous: General Compiler Information]]</div> | <div style='padding-top: 30px; padding-bottom: 20px; text-align: left;float:left;width:50%;'>[[Manual:Zone_Manual/General_Compiler_Information|Previous: General Compiler Information]]</div> | ||
<div style='padding-top: 30px; padding-bottom: 20px; text-align: right;float:right;width:50%;'>[[Manual:Zone_Manual/Zone_Source_File|Next: Zone Source File]]</div> | <div style='padding-top: 30px; padding-bottom: 20px; text-align: right;float:right;width:50%;'>[[Manual:Zone_Manual/Zone_Source_File|Next: Zone Source File]]</div> | ||
Latest revision as of 06:19, 15 November 2026
Contents
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
#defineis 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,upanddownare macros (northbecomesexit[NORTH]), and so aredilend,pauseandon_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.