A4 v1.2.2 Engine Documentation

The A4 Engine is a small JavaScript engine for 2D action RPG games created by Santiago Ontañón (2014). It was created specifically to test procedural content generation techniques, and thus, games are completely defined as xml files that are loaded by the game engine for execution.

To create a game, the following set of files are needed:
The engine ships with an example (Aventura 4), that contains all of these files. Specifically, the game definition file is called aventura4.xml, which includes references to all the other files.

Game Definition File

The Game definition file has the following structure:

<A4Game name="My Game Title">
  <titleImage>title.png</titleImage>
  <story>
    <line> ... </line>
    ...
    <line> ... </line>
  </story>
  <ending>
    <line> ... </line>
    ...
    <line> ... </line>
  </ending>
  <tiles sourcewidth="32" sourceheight="32" targetwidth="32" targetheight="32">
    <types file="graphics.png">
    ...
    </types>
    <seeThrough file="graphics.png">
    ...
    </seeThrough>
    <canDig file="graphics.png">
    ...
    </canDig>
    <animation name="coinpurse" dx="1" dy="1" period="4" looping="false" file="graphics2x.png">9</animation>
    <animation name="curious" dx="1" dy="1" period="8" looping="true" file="graphics2x.png">74,-1</animation>
    <animation name="scared" dx="1" dy="1" period="8" looping="true" file="graphics2x.png">84,-1</animation>
    <animation name="angry" dx="1" dy="1" period="8" looping="true" file="graphics2x.png">94,-1</animation>
    <animation name="tired" dx="1" dy="1" period="8" looping="true" file="graphics2x.png">104,-1</animation>
    <animation name="happy" dx="1" dy="1" period="8" looping="true" file="graphics2x.png">75,-1</animation>
    <animation name="magic missile" dx="1" dy="1" period="8" looping="false" file="graphics2x.png">114</animation>
    <animation name="fireball" dx="1" dy="1" period="8" looping="false" file="graphics2x.png">124</animation>
    <animation name="incinerate" dx="1" dy="1" period="8" looping="false" file="graphics2x.png">134</animation>  

  </tiles>

  <characterDefinition file="characters1.xml"/>
  ...
  <characterDefinition file="charactersn.xml"/>

  <objectDefinition file="objects1.xml"/>
  ...
  <objectDefinition file="objectsn.xml"/>

  <map file="map1.tmx"/>
  ...
  <map file="map1.tmx"/>

  <player class="PLayerClass1" x="12" y="14" map="0"/>
  ...
  <player class="PLayerClassn" x="12" y="14" map="0"/>

  <onStart>
  ... [script] ... 
  <onStart/>

  ... [story state and event rules] ...

</A4Game>
      
The tags have the following meaning:
Additional story state rules and event rules can be added, which will be checked at each frame of the game during gameplay.

back to top

Character Definition Files

Character definition files are used to define player characters, NPCs and enemies. Each file has the following structure:

<Characters>
  ...
  <CharacterClass class="MyCharacterClass" super="SuperClass" name="My Character Name">
    <animation name="animation-name" dx="1" dy="1" period="4" looping="false" file="graphics.png">animation sequence</animation>
    ...
    <animation name="animation-name" dx="1" dy="1" period="4" looping="false" file="graphics.png">animation sequence</animation>

    <attribute name="attribute name" value="attribute value"/>
    ...
    <attribute name="attribute name" value="attribute value"/>

    <item>JavaScript code to create item</item>
    ...
    <item>JavaScript code to create item</item>

    ... [story state, event conversation and trade rules] ...

    <onStart>
    ... [script] ... 
    <onStart/>
    <onEnd>
    ... [script] ... 
    <onEnd/>
  </CharacterClass>
        
The tags have the following meaning:
Additional story state rules, event rules, conversation rules and trade rules can be added, which will be checked at each frame of the game during gameplay. These define how the characters behave in front of story events, or when other characters talk to them.

back to top

Map Definition Files

A4 expects the maps to be in the XML format used by the TILED map editor (TMX). When creating a map, make sure to add a property called "name" that takes the name of the map as a value.

Maps are organized in "layers". It is recommended that you defined two layers in a map (but you can define more if you want). The first layer will be used for the background tiles and objects (e.g. floor, grass, water, etc.), and the second layer for the foreground objects (walls, trees, etc.).
Each layer is defined as:

    <layer name="Tile Layer 1" width="WIDTH" height="HEIGHT">
    <data>
      <tile gid="TILEID"/>
      ...
      <tile gid="TILEID"/>
    </data>
    </layer>
    
Where each "tile" line corresponds to one of the WIDTH*HEIGHT tiles in the map (starting from the top-left corner, and proceeding line by line). The TILEID is the number of the tile in the graphics file (Starting from the top left). Everything that does not have any behavior (floor, walls, trees, rivers, etc.) is defined in the tile layers. Everything that has a behavior (enemies, items that can be picked-up, doors that can be open, etc.) does in the object layers. After you define the tile layers, you need to add the object layers. An object layer is defined as:

    <objectgroup name="LAYER NAME" width="WIDTH" height="HEIGHT">
      ...
      <object name="OBJECT_DEFINITION" type="TYPE" x="X" y="Y" width="W" height="H"/>
      ...
    </objectgroup>
    
Notice that the x, y, width, and height of objects is defined in pixels. The OBJECT_DEFINITION determines the object to be created. Several object types exist in the A4Engine (see the examples provided and the A4 source code to see the latest set of available objects). But for example, if you want to create a door, you can create it by specifying OBJECT_DEFINITION as:

    new Door("CENTERKEY",true,Animation.fromTile(49,"graphics2x.png"),null)
    
This creates a door that will be open with the key named "CENTERKEY", which can be created like this:

    new Key("Small key", "CENTERKEY", Animation.fromTile(28,"graphics2x.png"))
    
Or alternatively, you might want the door to just be open by activating a lever, like this:

    new Lever("CENTERKEY",true,Animation.fromTile(66,"graphics2x.png"),Animation.fromTile(65,"graphics2x.png"))
    
For each "object" that you add to an object layer, you must use one of the following TYPES: For each object defined in any object layers, additional event/story/trade rules, scripts, items, etc. can be added in the same way as in the object and character definition files.

Finally, after the map has been saved from TILED, you can add additional
story state rules, event rules, and an onStart tag like in the Game definition file.

back to top

Items

A4 includes a set of default item and object classes that can be used in any game. Specifically, A4 incorporates the following default item/object classes: Additionally, you can specify new items and objects via the use of an object definition file, that has the following structure:

<Objects>
  <ObjectClass class="MyObjectClass" super="SuperClass" name="MyObjectName">
    <animation name="animation-name" dx="1" dy="1" period="4" looping="false" file="graphics.png">animation sequence</animation>
    ...
    <animation name="animation-name" dx="1" dy="1" period="4" looping="false" file="graphics.png">animation sequence</animation>

    <attribute name="attribute name" value="attribute value"/>
    ...
    <attribute name="attribute name" value="attribute value"/>

    ... [story state and event rules] ...
  </ObjectClass>
  ...
</Objects>  
    
The tags have the following meaning:
Additional story state rules, event rules can be added, which will be checked at each frame of the game during gameplay. Vehicles allow characters that have "canSwim=false" to move through water (if the vehicle has canSwim=true) and characters that have "canWalk=false" more throuh land (if the vehicle has "canWalk=true").

back to top

Scripts

A4 allows the specification of scripts for many tasks. A scripp in A4 is a sequence of actions, where each action is defined in the following way:

<actionName ...action attributes...>
  ... sub-actions ...
</actionName>  
    
The list of actions that A4 supports is the following:
All of these actions return "true" or "false" if they could or not be executed successfully, and thus, all can be used as part of the condition of an if-then-else. An if-then-else returns the value of the last action executed internally.

back to top

Story State Rules

Story state rules are rules that are checked at each execution cycle, and that check the "story state" to see if any script needs to be triggered. The "story state" in A4 is defined at three different levels: each character and map has a set of "story state variables", and the game itself has another global set. You can set the values of these variables with the script action "storyState". As the story state changes, story state rules can be fired to trigger game events. A story state rule is defined as follows:

<storyStateRule scope="SCOPE" variable="VARIABLENAME" value="VALUE" once="true/false">
    ... script ...
</storyStateRule>
      
When the variable "variable" in the scope "scope" takes the value "value", the script will be executed. You can specify whether this rule has to be executed only once, or each time this value is observed with the attribute "once" (most likely you want once="true" for all your rules).

back to top

Event Rules

Event rules are rules that are checked at each execution cycle, triggered by certain events. An event rule is defined as follows:

<eventRule event="EVENT" once="true/false">
    ... script ...
</eventRule>
      
When the event "event" is triggered, this rule will be executed. Currently, the engine supports the following events: You can specify whether this rule has to be executed only once, or each time this value is observed with the attribute "once" (default is "false").

back to top

Conversation Rules

Conversation rules determine how characters (e.g. NPCs) react when asked about different conversation topics.

<conversationRule topic="TOPIC">
    ... script ...
</conversationRule>
      
When a character is asked about the topic "TOPIC", it will execute the specified script. Notice that conversation rules can be updated via the "updateConversationRule" script action, to make characters behave differently after some game events happened.

back to top

Trade Rules

Trade rules determine how characters (e.g. NPCs) react when they receive an item (given or sold) or when they sell you an item.

<tradeRule event="GIVE/SELL/RECEIVE" item="NAME">
    ... script ...
</tradeRule>
      
When a character gives/sells/receives an item with name "NAME", it will execute the specified script. Notice that give/Sell/Receive rules can be updated via the "updateTradeRule" script actions, to make characters behave differently after some game events happened.

back to top

Behaviors

NPCs and Enemies can have multiple "behaviors" attached to them. Behaviors are executed each time a character can execute an action (i.e. they will not be executed while a character is moving or attacking until the move or attack is done). Each behavior can return a desired action. If multiple behaviors return actions, ties are resolved by the priority of each behavior.

Most of these behaviors make use of the "working memory" that NPCs and Enemies have. The working memory is divided into two parts: short-term and long-term. Each of them is a list of "WMEs" (Working Memory Elements). A WME is a sctructure of the form "type(parameter1, ..., parametern)". WMEs in the short-term memory disappear after a while, WMEs in the long-term memory stay there forever. If a WME stays in the short-term memory for a very long time, it will be moved to the long-term memory.

A4 comes built-in with the following behaviors (but you can add more, by just adding additional JavaScript files to your project):

back to top