Marauroa Core API

From Arianne
Jump to navigation Jump to search



Marauroa exposes a very simplified and reduced API so you can easily develop your own games. A nightly build JavaDoc API Documentation is available, too.

Content

Class Diagram of RP* classes

The main entities you should know about are:

  • Attributes
  • RPAction
  • RPObject
  • RPSlot
  • RPClass
  • IRPZone interface
  • IRPRuleProcessor interface
  • RPWorld

To read about these elements please see RolePlayingDesign and GameDesign.

Or if you would prefer to use Python for developing the game rules read about:

  • PythonRP
  • PythonWorld

The Python API can be found at PyArianneAPIDefinition and an example of its use at PyArianneAPIExample. You can read more about using Python for developing in our game tutorial HowToWriteGamesUsingArianne. Please note that Python support is dormant.

Attributes

Attributes are pairs of data stored in a list. Each attribute comprises of a name and value element. Attributes are the standard way of storing data in Arianne. An example of an attribute is your age, e.g. name="age" and value=21

Each attribute also has a type associated with it that is defined in a RPClass. An attributes is an instance of a RPClass (see next section of this document to read more on RPClass.

There are two special entries in attributes:

  • type: defines the name of the RPClass that the object belongs to.
  • id: defines a unique identifier

The methods exposed by the Attributes class to the game developer are shown below. These are the functions you should use to modify your attributes.

Setting the value of an attribute:

    public void put(String name, String value)
    public void put(String name, int value)

Add a quantity to the value element of an attribute that MUST already exist:

    public void add(String name, int quantity)

Returns the value of an attribute:

    public String get(String name)
    public int get(String name)

Removes an attribute entry from the list of attributes:

    public void remove(String name)

Returns True if an attribute exists:

    public boolean has(String name)

Example:

    Attributes test=new Attributes();
    test.put("name","Test attribute");
    test.put("hp",100);

    if(test.has("hp")) {
        test.add("hp",10);
    }

    test.remove("name");

RPClass

This class is a key concept of Arianne. An RPClass is much like a Java Class but for Arianne RPObjects: it defines the type (string, int, boolean, ...) and the visibility (hidden, private or visible) of the attributes that make up an object (an object is a collection of attributes, for example for a human, age, height etc).

RPClass is made up of five different parts:

A data type definition:

  • String: a string of up to 2^32 bytes
  • Short String: a string of 255 bytes
  • Integer: a 32 bits integer
  • Short: a 16 bits integer
  • Byte: a 8 bits integer
  • Flag: a binary flag.

The Visibility of the data:

  • Visible: if the attribute can be seen by clients.
  • Hidden: if the attribute is not visible to clients.
  • Private: if the attribute is only visible to the client the object is associated with (the player object controlled by this client)

Creation methods of the RPClass: (These are part of each RPClass instance, see example below for usage)

    public void add(String name, byte type)
    public void add(String name, byte type, byte visibility)
    public void isA(String parent)
    public void isA(RPClass parent)

These methods add attributes to the RPClass and set their type and visibility. You can also set the parent of this class. Of course classes without parents are possible, too.

Once the class has been filled, you can query the data using these methods. These methods allow you to get the class name of the class, the the type of an attribute, determine if an attribute exists and to know if that RPClass is a subclass of another.

Query:

    public String getName()
    public byte getType(String name)
    public byte getVisibility(String name)
    public boolean hasAttribute(String name)
    public boolean subclassOf(String parentclass)

You can query the system classes using these methods. Note that it is not a good idea to modify them once you are running. The class definitions are send to the client on connect hence if you change the class definitions in the middle of a game you will crash your clients!

Class wide query:

    boolean hasRPClass(String name)
    RPClass getRPClass(String name)


When writting a class definition you can skip the id and type attributes as they are automatically determined by RPClass.

Example:

    // a general entity with a position
    RPClass objclass = new RPClass("entity");
    objclass.add("x", RPClass.BYTE);
    objclass.add("y", RPClass.BYTE);

    // an entity specialized in players
    objclass = RPClass("player");
    objclass.isA("entity");
    objclass.add("name", RPClass.SHORT_STRING);
    objclass.add("direction", RPClass.SHORT_STRING);
    objclass.add("score", RPClass.INT);
    objclass.add("super", RPClass.BYTE);
    objclass.add("!vdir", RPClass.STRING,RPClass.HIDDEN);
    objclass.add("!hdir", RPClass.STRING,RPClass.HIDDEN);
    objclass.add("?kill", RPClass.FLAG);

Each time you create a new RPClass, as long as you give it a name (ie: entity, player, ... ), it will be added to the system list of classes.

Notice in the example that the hidden attribute must be specified if you want the RPClass to have this property otherwise by default an attribute is visible.

Note: You can choose not to use RPClasses in your application but that increases network bandwidth usage.

RPAction

RPAction is an object used to represent actions that a player wants to do. It is up to you to create these when you design your game as they are specific to each game ( you should also define a RPClass for these ).

RPObject and RPSlot

RPObjects are the containters of data in Arianne. An RPObject is an Attributes element with a list of RPSlots attached.

RPSlots are slots owned by an RPObject into which other RPObjects can be placed (like items in a backpack)

The methods of the RPObject to modify slots are:

    public void addSlot(RPSlot slot)
    public RPSlot getSlot(String name)
    public void removeSlot(String name)
    public boolean hasSlot(String name)
    public Iterator slotIterator()

The above methods are used to add a slot to the object, retrieve it, remove it and test if the slot exists. Finally the slot iterator is used to visit all the slots in the object.

    public RPObject.ID getID()

This is a helper method to get the unique ID of the object.

RPSlot has a simple API:

    public void add(RPObject object)
    public RPObject get(RPObject.ID id)
    public boolean has(RPObject.ID id)
    public RPObject remove(RPObject.ID id)
    public void clear()

These methods modify objects in the RPSlot. The clear() method removes all the objects in the slot.

   public Iterator iterator()

It is used to visit all the objects of the slot.

  // create an object of RPClass player and set some attribute values
  RPObject object = new RPObject("player");
  object.put("name", "example");
  object.put("score", 0);

  // create a slot called backpack
  RPSlot slot = new RPSlot("backpack");
  object.addSlot(slot);

  // create an object of RPClass "coin" and put it into the slot
  RPObject coin = new RPObject("coin");
  slot.add(coin);

Now for the complex part. Where it all becomes a little nuts!: IRPZone and IRPRuleProcessor interfaces

RPWorld

This class is just a container of zones.

RPWorld provides several methods to make easier the handling of RPZones.

onInit and onFinish are called on server startup and finalization. You need to subclass RPWorld to give proper behaviour to them.

    public void onInit() throws Exception
    public void onFinish() throws Exception

Some helper methods to add zones and iterate them.

    public void addRPZone(IRPZone zone)
    public IRPZone getRPZone(IRPZone.ID zoneid)
    public IRPZone getRPZone(RPObject.ID objectid)
    public Iterator<IRPZone> iterator()
    public int size()

Methods to add, get, test existence, remove and modify objects. modify() is as you know for delta^2 algorithm.

    public void add(RPObject object) throws NoRPZoneException, RPObjectInvalidException  
    public RPObject get(RPObject.ID id) throws NoRPZoneException, RPObjectInvalidException  
    public boolean has(RPObject.ID id) throws NoRPZoneException, RPObjectInvalidException  
    public RPObject remove(RPObject.ID id) throws NoRPZoneException, RPObjectNotFoundException  
    public void modify(RPObject object) throws NoRPZoneException

These are helper methods for changing the zone of an object. Use them instead of doing it by hand.

    public void changeZone(IRPZone.ID oldzoneid, IRPZone.ID newzoneid, RPObject object) throws NoRPZoneException
    public void changeZone(String oldzone, String newzone, RPObject object) throws NoRPZoneException

IRPZone

IRPZone is the interface that handles the world content and the perceptions. In most cases you should use the implementation MarauroaRPZone and extend it.

The methods are:

  /** This method is called when the zone is created to populate it */
  public void onInit() throws Exception;

  /** This method is called when the server finish to save the content of the zone */
  public void onFinish() throws Exception;

  /** This method adds an object to the Zone */
  public void add(RPObject object) throws RPObjectInvalidException;

  /** This method tag an object of the Zone as modified */
  public void modify(RPObject object) throws RPObjectInvalidException;

  /** This method removed an object of the Zone and return it.*/
  public RPObject remove(RPObject.ID id) throws RPObjectNotFoundException;

  /** This method returns an object of the Zone */
  public RPObject get(RPObject.ID id) throws RPObjectNotFoundException;

  /** This method returns true if the object exists in the Zone */
  public boolean has(RPObject.ID id);

  /** This method create a new RPObject with a valid id */
  public RPObject create();

  /** Iterates over the elements of the zone */
  public Iterator iterator();

  /** Returns the number of elements of the zone */
  public long size();

  /** This method return the perception of a zone for a player */
  public Perception getPerception(RPObject.ID id, byte type);

  /** This method is called to take zone to the next turn */
  public void nextTurn();

  /** Method to create the map to send to player's client */
  public java.util.List buildMapObjectsList(RPObject.ID id);

In most of the cases all you will wish to modify are:

  • onInit
  • onFinish
  • buildMapObjectsList

IRPRuleProcessor

This class must be implemented fully, but it is a childs toy compared to IRPZone :). This is where you code all your games rules.
The API is as follows:

  /** Set the context where the actions are executed.
   *  @param zone The zone where actions happens.  */
  public void setContext(IRPZone zone);

  /** Pass the whole list of actions so that it can approve or deny the actions in it.
   *  @param id the id of the object owner of the actions.
   *  @param actionList the list of actions that the player wants to execute. */
  public void approvedActions(RPObject.ID id, RPActionList actionList);

  /** Execute an action in the name of a player.
   *  @param id the id of the object owner of the actions.
   *  @param action the action to execute
   *  @return the action status, that can be Success, Fail or incomplete, please
   *      refer to Actions Explained for more info. */
  public RPAction.Status execute(RPObject.ID id, RPAction action);

  /** Notify it when a new turn happens */
  public void nextTurn();

  /** Callback method called when a new player enters in the game
   *  @param object the new player that enters in the game. */
  public boolean onInit(RPObject object) throws RPObjectInvalidException;

  /** Callback method called when a new player exits the game
   *  @param id the new player id that exits the game.
   *  @return true to update the player on database. */
  public boolean onExit(RPObject.ID id) throws RPObjectNotFoundException;

  /** Callback method called when a new player time out
   *  @param id the new player id that timeouts. */
  public boolean onTimeout(RPObject.ID id) throws RPObjectNotFoundException;

{{#breadcrumbs: Marauroa | Using | Core API}}