On this page:
%cond
%unless
%define
8.18.0.13

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 ...]
Game versions: All

Arguments:
  • 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.

Execute a piece of code if a condition is fulfilled, with the option of specifying alternative conditions and pieces of code to execute.
  • 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.

Example: Manually scale relic count to map size.
#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)]))

Example: Replace the scout with a king when playing regicide.
#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))

Example: Set up a NOT conditional - place a cow when "infinite resources" is not true.
#lang aoe2-rms
 
<OBJECTS-GENERATION>
(%cond ['INFINITE_RESOURCES]
       [#f (create-object 'DLC_COW
                          (set-place-for-every-player)
                          (find-closest))])

Example: Check for various game versions.
#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 ...)

Same as:

(%cond [cond-label] [#f then-body ...])

Example: Set up a NOT conditional - place a cow when "infinite resources" is not true.
#lang aoe2-rms
 
<OBJECTS-GENERATION>
(%unless 'INFINITE_RESOURCES
         (create-object 'DLC_COW
                        (set-place-for-every-player)
                        (find-closest)))

procedure

(%define cond-label)  void?

  cond-label : any/c
Game versions: All

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.

Example: Set up seasonal variations for the base terrain.
#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)])