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

  1. /*
  2.  * @(#)Cell.java
  3.  */
  4. package games.Battle.shared.sys;
  5.  
  6. /**
  7.  * A base class for an individual cell (square) in the game board. Both the
  8.  * client and server derive more specialized versions of cell to store the
  9.  * infomration that is of interest to them.
  10.  *
  11.  * @version 1.00
  12.  * @author  Jay Steele
  13.  * @author  Alex Nicolaou
  14.  */
  15.  
  16. public class Cell
  17. {
  18.     /**
  19.      * A symbols symbol is used to indicate who is in this cell.
  20.      */
  21.     int occupancy = Symbols.UNOCCUPIED;
  22.     /**
  23.      * An int is used to represent cities to allow for partial destruction
  24.      * of cities in the future.
  25.      */
  26.     int city = 0;
  27.     /**
  28.      * A boolean array that indicates the presence of pipes in each compass
  29.      * point direction.
  30.      */
  31.     boolean pipe[] = {false, false, false, false};
  32.     /**
  33.      * The height of the terrain in this square.
  34.      */
  35.     int terrain = 1;
  36.     /**
  37.      * The number of troops in the cell.
  38.      */
  39.     int troops = 0;
  40.     /**
  41.      * The row and column where this cell is located on the game board.
  42.      */
  43.     int row, col;
  44.     /**
  45.      * A flag that indicates if the contents of the cell have changed.
  46.      */
  47.     boolean modified = false;
  48.     /**
  49.      * A flag to indicate if the terrain in the cell has changed (not 
  50.      * yet used).
  51.      */
  52.     boolean terrainModified = false;
  53.  
  54.     /**
  55.      * construct a general battle game cell
  56.      * @param r the row
  57.      * @param c the column
  58.      */
  59.     public Cell(int r, int c) {
  60.         row = r;
  61.         col = c;
  62.     }
  63.  
  64.     /**
  65.      * set the occupancy
  66.      */
  67.     public final void setOccupancy(int occ) {
  68.         if (occupancy != occ) {
  69.             modified = true;
  70.             occupancy = occ; 
  71.         }
  72.     }
  73.     /**
  74.      * set the city construction amount 
  75.      * @param amt the amount of city that is here. Always complete 
  76.      * cities for now.
  77.      */
  78.     public final void setCity(int amt) { 
  79.         if (city != amt) {
  80.             terrainModified = true;
  81.             city = amt; 
  82.         }
  83.     }
  84.     /**
  85.      * set or clear a pipe in the given direction.
  86.      * @param dir the direction; 0 is north. defines are in Symbols
  87.      * @param state the state of the pipe, on or off.
  88.      */
  89.     public final void setPipe(int dir, boolean state) {
  90.         if (pipe[dir] != state) {
  91.             modified = true;
  92.             pipe[dir] = state; 
  93.         }
  94.     }
  95.  
  96.     /**
  97.      * clear all the pipes in this cell.
  98.      */
  99.     public final void clearPipes() { 
  100.         modified = true;
  101.         for (int dir = 0; dir < pipe.length; dir++)
  102.             pipe[dir] = false;
  103.     }
  104.  
  105.     /**
  106.      * set the height of the terrain in this cell.
  107.      * @param level the height of the land.
  108.      */
  109.     public final void setTerrain(int level)    { 
  110.         if (terrain != level) {
  111.             terrainModified = true;
  112.             terrain = level; 
  113.         }
  114.     }
  115.  
  116.     /**
  117.      * set how many troops are in this cell.
  118.      * @param  num the number of troops.
  119.      */
  120.     public final void setTroops(int num) { 
  121.         if (troops != num) {
  122.             modified = true;
  123.             troops = num; 
  124.         }
  125.     }
  126.  
  127.     /**
  128.      * special method for setting pipes. the lower four bits in 
  129.      * mask indicate which pipes are on and which are off. bits
  130.      * 3, 2, 1, and 0 represent the north, east, south and west
  131.      * pipes, respectively
  132.      * <P>
  133.      * This method is convenient because of the way data is transmitted
  134.      * between client and server.
  135.      * @param mask the bitmask of pipes.
  136.      */
  137.     public final void setPipes(int mask) {
  138.         modified = true;
  139.         for (int dir=0; dir<4; dir++) {
  140.             if ((mask & Symbols.PIPE_MASK[dir]) != 0x0000) {
  141.                 setPipe(dir, true);
  142.             } else {
  143.                 setPipe(dir, false);
  144.             }
  145.         }
  146.     }
  147.  
  148.     /**
  149.      * return the occupancy
  150.      */
  151.     public final int getOccupancy()                { return occupancy; }
  152.     /**
  153.      * return the city amount
  154.      */
  155.     public final int getCity()                    { return city; }
  156.     /**
  157.      * return the array of pipes.
  158.      */
  159.     public final boolean[] getPipes()            { return pipe; }
  160.     /**
  161.      * return the state of the pipe in the direction given 
  162.      * @param dir the direction
  163.      */
  164.     public final boolean getPipe(int dir)        { return pipe[dir]; }
  165.     /**
  166.      * build a pipe mask from the current pipe state. The lower four
  167.      * bits of the returned int represent the state of each pipe.
  168.      */
  169.     public final int getPipeMask() {
  170.         int result = 0;
  171.         for (int i=0; i<4; i++)
  172.             if (pipe[i]) 
  173.                 result |= Symbols.PIPE_MASK[i];
  174.         return result;
  175.     }
  176.     /**
  177.      * return the level of the terrain in this cell.
  178.      */
  179.     public final int getTerrain()                { return terrain; }
  180.     /**
  181.      * return the number of troops in this cell
  182.      */
  183.     public final int getTroops()                { return troops; }
  184.     /**
  185.      * return the row that this cell is in 
  186.      */
  187.     public final int getRow()                    { return row; }
  188.     /**
  189.      * return the column that this cell is in 
  190.      */
  191.     public final int getCol()                    { return col; }
  192.  
  193.     /**
  194.      * return true if this cell's contents have been modified
  195.      */
  196.     public final boolean isModified()             { return modified; }
  197.     /**
  198.      * return true if this cell's terrain has been modified
  199.      */
  200.     public final boolean isTerrainModified()    { return terrainModified; }
  201.  
  202.     /**
  203.      * clear the contents modified flag
  204.      */
  205.     public void clearModified()                    { modified = false; }
  206.     /**
  207.      * clear the terrain modified flag
  208.      */
  209.     public void clearTerrainModified()            { terrainModified = false; }
  210.  
  211.     /**
  212.      * set the content modified flag
  213.      */
  214.     public final void setModified()                { modified = true; }
  215.     /**
  216.      * set the terrain modified flag
  217.      */
  218.     public final void setTerrainModified()        { terrainModified = true; }
  219.  
  220.     /**
  221.      * return whether or not this cell is visible to a particuler player.
  222.      * this method should be overridden to create the effect of invisible
  223.      * cells.
  224.      */
  225.     public boolean isVisible(int player)     { return true; }
  226.  
  227.     /**
  228.      * Make this cell a functioning city (i.e. a city with the maximum
  229.      * number of city segments.
  230.      */
  231.     public void setCity() {
  232.         setCity(Symbols.MAX_CLIENT_TROOPS);
  233.     }
  234. }
  235.