home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / enxle1f6 / src / games / battle / shared / comm / command.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  4.0 KB  |  140 lines

  1. /*
  2.  * @(#)Command.java
  3.  */
  4.  
  5. package games.Battle.shared.comm;
  6.  
  7. /**
  8.  * A command is issued from the game client to the game server,
  9.  * and represents a request from a player to the game engine
  10.  * to modify his/her state on the game board. There are a variety
  11.  * of possible commands, which are detailed in the Battle.sys.Symbols
  12.  * class. When the game server receives a command from
  13.  * the client, it can choose to execute it, or to ignore it, if
  14.  * the command was not legal.
  15.  * 
  16.  * @version 1.00 02/02/96
  17.  * @author Jay Steele
  18.  * @author Alex Nicolaou
  19.  */
  20. public class Command extends BattlePacket {
  21.     byte player;
  22.     short id;
  23.     byte cell;
  24.     short mouse;
  25.  
  26.     /**
  27.      * convert to a string representation
  28.      */
  29.     public String toString() {
  30.         return getClass().getName() + "[player=" + player + ",id=" + id
  31.                 + ",r="+getRow()+",c="+getCol()+",x="+getX()+",y="+getY()
  32.                 + "]";
  33.     }
  34.  
  35.     /**
  36.      * Creates a default command which can then be filled using the
  37.      * readFrom(is) method. The server will probably be the only
  38.      * user of this method.
  39.      */
  40.     public Command() {
  41.         player = (byte)0;
  42.         id = (short)0;
  43.         cell = (byte)0;
  44.         mouse = (short)0;
  45.     }
  46.  
  47.     /**
  48.      * Creates a command based on the given data, which it can then
  49.      * send to a data stream using the writeTo(os) method. The client
  50.      * is probably the only user of this method.
  51.      * @param player who issued this command
  52.      * @param id what command does this represent
  53.      * @param row what row on the game board did this command come from
  54.      * @param col what column on the game board did this command come from
  55.      * @param x what x-position within the given cell did the command come from
  56.      * @param y what y-position within the given cell did the command come from
  57.      */
  58.     public Command(int player, short id, int row, int col, int x, int y) {
  59.         this.player = (byte)player;
  60.         this.id = id;
  61.         this.cell = (byte)(row << 4 | (col & 0x000f));
  62.         this.mouse = (short)(x << 8 | (y & 0x00ff));
  63.     }
  64.  
  65.     /**
  66.      * Dump a text version of this command, for debugging.
  67.      */
  68.     public void asciiDump() {
  69.         System.out.print("Command <");
  70.         System.out.print((int)player+", ");
  71.         System.out.print(id+", ");
  72.         System.out.print("("+getRow()+", "+getCol()+"), ");
  73.         System.out.print("("+getX()+", "+getY()+")>");
  74.         System.out.println();
  75.     }
  76.  
  77.     /**
  78.      * Return the player id for this command.
  79.      */
  80.     public final int getPlayer() { return (int)player & 0x000f; }
  81.  
  82.     /**
  83.      * Return the command id for this command.
  84.      */
  85.     public final short getId() { return id; }
  86.  
  87.     /**
  88.      * Return the row this command represents. This data may or may
  89.      * not be relevant for every command.
  90.      */
  91.     public final int getRow() { return (cell >> 4) & (byte)0x0f; }
  92.  
  93.     /**
  94.      * Return the column this command represents. This data may or
  95.      * may not be relevant for every command.
  96.      */
  97.     public final int getCol() { return cell & (byte)0x0f; }
  98.  
  99.     /**
  100.      * Return the x position within a cell this command originated from.
  101.      * This data may or may not be relevant for every command.
  102.      */
  103.     public final int getX() { return (mouse >> 8) & (short)0x00ff; }
  104.  
  105.     /**
  106.      * Return the y position within a cell this command originated from.
  107.      * This data may or may not be relevant for every command.
  108.      */
  109.     public final int getY() { return mouse & (short)0x00ff; }
  110.  
  111.     /**
  112.      * Convert this Command to an array of bytes for transmission.
  113.      */
  114.     protected byte[] toBytes() {
  115.         byte[] buffer = new byte[6];
  116.         buffer[0] = player;
  117.         buffer[1] = (byte)((id >> 4) & 0x000f);
  118.         buffer[2] = (byte)(id & 0x000f);
  119.         buffer[3] = cell;
  120.         buffer[4] = (byte)((mouse >> 8) & 0x00ff);
  121.         buffer[5] = (byte)(mouse & 0x00ff);
  122.         return buffer;
  123.     }
  124.  
  125.     /**
  126.      * With the given array of bytes, fill this command object
  127.      * with data.
  128.      * @param data the data to fill the command with.
  129.      */
  130.     protected void fromBytes(byte[] data) {
  131.         if (data.length != 5) {
  132.             // raise exception ?
  133.         }
  134.         player = data[0];
  135.         id = (short)(((int)data[1] << 4) | ((int)data[2] & 0x000f));
  136.         cell = data[3];
  137.         mouse = (short)(((int)data[4] << 8) | ((int)data[5] & 0x00ff));
  138.     }
  139. }
  140.