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

  1. /*
  2.  * @(#)TerrainInit.java
  3.  */
  4. package games.Battle.shared.comm;
  5.  
  6. import java.awt.Color;
  7. import games.Battle.shared.sys.*;
  8.  
  9. /**
  10.  * The TerrainInit packet is sent to each client prior to the 
  11.  * start of each game, and represents the terrain of the entire
  12.  * game board. This is somewhat unfortunate, since this means
  13.  * a rewritten client could take advantage of this information
  14.  * and always make the terrain visible. However, I have yet
  15.  * to be convinced that sending terrain information in every
  16.  * game turn is a good idea.
  17.  *
  18.  * @version 1.00 02/02/96
  19.  * @author Jay Steele
  20.  * @author Alex Nicolaou
  21.  */
  22. public class TerrainInit extends BattlePacket {
  23.     /**
  24.      * The board
  25.      */
  26.     Board board;
  27.  
  28.     /**
  29.      * Builds a terrain initializer for a particular board.
  30.      * @param board the game board from which the init data will be read/written
  31.      */
  32.     public TerrainInit(Board board) {
  33.         this.board = board;
  34.     }
  35.  
  36.     /**
  37.      * Produce an array of bytes representing a terrain init
  38.      */
  39.     public byte[] toBytes() {
  40.  
  41.         Board sb = board;
  42.  
  43.         byte[] result = new byte[Rules.rows*Rules.cols];
  44.         for (int r=0; r<Rules.rows; r++) {
  45.             for (int c=0; c<Rules.cols; c++) {
  46.                 int i = Common.RCtoIndex(r, c);
  47.                 Cell cell = sb.getCell(r, c);
  48.                 int occupancy = cell.getOccupancy();
  49.                 int terrain = cell.getTerrain();
  50.                 result[i] = Common.stuffOccupancyAndTerrain(occupancy, terrain);
  51.             }
  52.         }
  53.         return result;
  54.     }
  55.  
  56.     /**
  57.      * Fill a board with the terrain initialization data in the buffer.
  58.      * @param buffer the terrain initialization data
  59.      */
  60.     public void fromBytes(byte[] buffer) {
  61.  
  62.         Board cb = board;
  63.  
  64.         for (int r=0; r<Rules.rows; r++) {
  65.             for (int c=0; c<Rules.cols; c++) {
  66.                 int i = Common.RCtoIndex(r, c);
  67.                 byte b = buffer[i];
  68.                 int occupancy = Common.extractOccupancy(b);
  69.                 int terrain = Common.extractTerrain(b);
  70.                 Cell cell = cb.getCell(r, c);
  71.                 if (Common.isPlayer(occupancy)) {
  72.                     cell.setCity();
  73.                 }
  74.                 cell.setOccupancy(occupancy);
  75.                 cell.setTerrain(terrain);
  76.             }
  77.         }
  78.     }
  79. }
  80.