3.7 Conditionals
Conditionals are pieces of code that will be executed based on whether a specified condition is fulfilled. The game has predefined most lobby settings as conditions. See full set of conditions available.
You can detect other game versions, as well as total conversion mods by exploiting differences in the predefined constants within random_map.def of the respective game versions.
syntax
(%cond cond-clause ...)
cond-clause = [cond-label then-body ...] | [#f then-body ...]
cond-label - either a predefined condition or a custom condition created using %define.
then-body - anything that will become body thats executed when given condition is valid.
if the condition is true will stop checking further conditions.
"#f" represents else condition, when specified, it must be the last cond-clause.
Can be used within blocks or around arbitrary code segments.
Conditionals can be nested.
BUG (AoC/HD): Comments in dead branches are not ignored. Do not include any conditional syntax in such comments. Be especially careful with "if" since it is easy to inadvertently type it in a comment! For more information, see this external article: Parser Pitfalls
Note that unlike cond, all branches are always executed during code generation.
#lang aoe2-rms <OBJECTS-GENERATION> (create-object 'RELIC (min-distance-to-players 25) (%cond ['TINY_MAP (number-of-objects 5) (temp-min-distance-group-placement 35)] ['SMALL_MAP (number-of-objects 5) (temp-min-distance-group-placement 38)] ['MEDIUM_MAP (number-of-objects 5) (temp-min-distance-group-placement 38)] ['LARGE_MAP (number-of-objects 7) (temp-min-distance-group-placement 48)] ['HUGE_MAP (number-of-objects 8) (temp-min-distance-group-placement 52)] [#f (number-of-objects 999) (temp-min-distance-group-placement 52)]))
#lang aoe2-rms <OBJECTS-GENERATION> (%cond ['REGICIDE (%const 'HERO 'KING)] [#f (%const 'HERO 'SCOUT)]) (create-object 'HERO (set-place-for-every-player) (min-distance-to-players 7) (max-distance-to-players 9))
#lang aoe2-rms <OBJECTS-GENERATION> (%cond ['INFINITE_RESOURCES] [#f (create-object 'DLC_COW (set-place-for-every-player) (find-closest))])
#lang aoe2-rms <OBJECTS-GENERATION> (%cond ['DE_AVAILABLE] ['DLC_TIGER (%cond ['UP_EXTENSION (%define 'WOLOLOKINGDOMS)] [#f (%define 'HD_DLC)])] ['DLC_COW (%define 'HD_BASE)] ['UP_EXTENSION] ['UP_AVAILABLE] [#f (%define 'CONQUERORS_CD)])
syntax
(%unless cond-label then-body ...)
#lang aoe2-rms <OBJECTS-GENERATION> (%unless 'INFINITE_RESOURCES (create-object 'DLC_COW (set-place-for-every-player) (find-closest)))
Arguments:
cond-label - text
AoC/UP - max length is 99 characters
ANY characters are valid; convention is to use uppercase letters and underscores
Define your own condition labels, to refer to at a later point. Do not use any predefined constants.
#lang aoe2-rms (%random [20 (%define 'WINTER)] [20 (%define 'AUTUMN)]) <LAND-GENERATION> (%cond ['WINTER (base-terrain 'SNOW)] ['AUTUMN (base-terrain 'LEAVES)] [#f (base-terrain 'DIRT)])