Class MarauroaRPZone

java.lang.Object
marauroa.server.game.rp.MarauroaRPZone
All Implemented Interfaces:
Iterable<RPObject>, IRPZone
Direct Known Subclasses:
StendhalRPZone

public class MarauroaRPZone extends Object implements IRPZone
Default implementation of IRPZone. This class implements the Delta^2 algorithm to save bandwidth.

The idea behind the DPA is to avoid sending ALL the objects to a client each time, but only those that have been modified. Imagine that we have 1000 objects, and only Object 1 and Object 505 are active objects that are modified each turn.

   The Traditional method:

   - Get objects that our player should see ( 1000 objects )
   - Send them to player ( 1000 objects )
   - Next turn
   - Get objects that our player should see ( 1000 objects )
   - Send them to player
   - Next turn
   ...
 
I hope you see the problem... we are sending objects that haven't changed each turn.
   The delta perception algorithm:

   - Get objects that our player should see ( 1000 objects )
   - Reduce the list to the modified ones ( 1000 objects )
   - Store also the objects that are not longer visible ( 0 objects )
   - Send them to player ( 1000 objects )
   - Next turn
   - Get objects that our player should see ( 1000 objects )
   - Reduce the list to the modified ones ( 2 objects )
   - Store also the objects that are not longer visible ( 0 objects )
   - Send them to player ( 2 objects )
   - Next turn
   ...
 
The next step of the delta perception algorithm is pretty clear: delta2
The idea is to send only what changes of the objects that changed. This way we save even more bandwidth, making perceptions around 20% of the original delta perception size.

The delta2 algorithm is based on four containers:

  • List of added objects
  • List of modified added attributes of objects
  • List of modified deleted attributes of objects
  • List of deleted objects
To make perceptions work, it is important to call the modify method in RPZone, so this way objects modified are stored in the modified list.
Author:
miguel
  • Field Details

    • zoneid

      protected IRPZone.ID zoneid
      Name of the zone
    • objects

      protected Map<RPObject.ID,​RPObject> objects
      Objects contained by the zone indexed by its id.
  • Constructor Details

    • MarauroaRPZone

      public MarauroaRPZone(String zoneid)
      Creates a new MarauroaRPZone
      Parameters:
      zoneid - name of zone
  • Method Details

    • getID

      public IRPZone.ID getID()
      Returns the zoneid
      Specified by:
      getID in interface IRPZone
      Returns:
      zone id
    • onFinish

      public void onFinish() throws Exception
      Description copied from interface: IRPZone
      This method is called when the server finish to save the content of the zone
      Specified by:
      onFinish in interface IRPZone
      Throws:
      Exception - if there has been any problem loading zone
    • storeToDatabase

      public void storeToDatabase()
      Store objects that has been tagged as storable to database asynchronously. Note: This methods returns before the saving is completed.
    • onInit

      public void onInit() throws Exception
      Load objects in database for this zone that were stored and waits for the database operation to complete.
      Specified by:
      onInit in interface IRPZone
      Throws:
      Exception - if there has been any problem loading zone
    • add

      public void add(RPObject object) throws RPObjectInvalidException
      This method adds an object to this zone.
      Specified by:
      add in interface IRPZone
      Parameters:
      object - object to add.
      Throws:
      RPObjectInvalidException - if it lacks of mandatory attributes.
    • modify

      public void modify(RPObject object) throws RPObjectInvalidException
      This method notify zone that the object has been modified. You should call it only once per turn, even if inside the turn you modify it several times.
      Specified by:
      modify in interface IRPZone
      Parameters:
      object - object to modify.
      Throws:
      RPObjectInvalidException - if it lacks of mandatory attributes.
    • remove

      public RPObject remove(RPObject.ID id)
      Removes the object from zone.
      Specified by:
      remove in interface IRPZone
      Parameters:
      id - identified of the removed object
      Returns:
      the removed object
    • hide

      public void hide(RPObject object)
      Hide an object from the perceptions, but it doesn't remove it from world. Any further calls to modify will be ignored.
      Specified by:
      hide in interface IRPZone
      Parameters:
      object - the object to hide.
    • unhide

      public void unhide(RPObject object)
      Makes a hidden object to be visible again. It will appear on the perception as an added object.
      Specified by:
      unhide in interface IRPZone
      Parameters:
      object - the object to unhide.
    • get

      public RPObject get(RPObject.ID id)
      Returns the object which id is id.
      Specified by:
      get in interface IRPZone
      Parameters:
      id - identified of the removed object
      Returns:
      the object
    • has

      public boolean has(RPObject.ID id)
      Returns true if the zone has that object.
      Specified by:
      has in interface IRPZone
      Parameters:
      id - identified of the removed object
      Returns:
      true if object exists.
    • assignRPObjectID

      public void assignRPObjectID(RPObject object)
      This method assigns a valid id to the object.
      Specified by:
      assignRPObjectID in interface IRPZone
      Parameters:
      object - the object that is going to obtain a new id
    • iterator

      public Iterator<RPObject> iterator()
      Iterates over all the objects in the zone.
      Specified by:
      iterator in interface IRPZone
      Specified by:
      iterator in interface Iterable<RPObject>
      Returns:
      an iterator
    • getPerception

      public Perception getPerception(RPObject player, byte type)
      Returns the perception of given type for that object.
      Specified by:
      getPerception in interface IRPZone
      Parameters:
      player - object whose perception we are going to build
      type - the type of perception:
      • SYNC
      • DELTA
      Returns:
      the perception
    • reset

      protected void reset()
      This methods resets the delta^2 information of objects.
    • size

      public long size()
      This method returns the amount of objects in the zone.
      Specified by:
      size in interface IRPZone
      Returns:
      amount of objects.
    • print

      public void print(PrintStream out)
      This method prints the whole zone. Handle it with care.
      Parameters:
      out - the PrintStream where zone is printed.
    • nextTurn

      public void nextTurn()
      This method moves zone from this turn to the next turn. It is called by RPWorld.
      Specified by:
      nextTurn in interface IRPZone