home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / enxle1f6 / src / games / battle / server / geomorph / geotile.java < prev   
Encoding:
Java Source  |  1996-08-14  |  5.2 KB  |  225 lines

  1. /*
  2.  * @(#)GeoTile.java
  3.  */
  4. package games.Battle.server.GeoMorph;
  5.  
  6. import java.io.*;
  7. import java.util.*;
  8.  
  9. /**
  10.  * GeoTile implements one particular tile in a geomorph world.
  11.  *
  12.  * @version 1.00
  13.  * @author Alex Nicolaou
  14.  * @author Jay Steele
  15.  */
  16.  
  17. public class GeoTile implements Cloneable {
  18.     /**
  19.      * The number of squares in a particular geomorph tile
  20.      */
  21.     static final public int GEO_TILESIZE = 4;
  22.  
  23.     /**
  24.      * direction constant: north
  25.      */
  26.     static final public int NORTH = 0;
  27.     /**
  28.      * direction constant: east
  29.      */
  30.     static final public int EAST = 1;
  31.     /**
  32.      * direction constant: south
  33.      */
  34.     static final public int SOUTH = 2;
  35.     /**
  36.      * direction constant: west
  37.      */
  38.     static final public int WEST = 3;
  39.  
  40.     /**
  41.      * a unique number for this tile
  42.      */
  43.     int tileID;
  44.     /**
  45.      * the actual terrain in this tile
  46.      */
  47.     public int[][] tile;
  48.     /**
  49.      * the location of the city hint for this tile (place a city here, 
  50.      * if anywhere!)
  51.      */
  52.     public int cityX, cityY;
  53.     /**
  54.      * a flag to indicate if there is a city in this tile
  55.      */
  56.     boolean hasCity = false;
  57.  
  58.     /**
  59.      * an array of vectors, one for each direction. Each vector
  60.      * contains a list of valid tile neighbours in that direction.
  61.      * validity is determined by comparing the continuity of the
  62.      * terrain along that edge of the tile to avoid introducing 
  63.      * badly mismatched terrain.
  64.      */
  65.     public Vector[] validTiles; 
  66.  
  67.     /**
  68.      * construct a tile be reading it in from the data input stream.
  69.      * the id given to the tile is assumed to be unique.
  70.      * @param data an input stream that contains a tile data file
  71.      * @param id an id that is assumed to be unique to this tile
  72.      */
  73.     public GeoTile(DataInput data, int id) throws java.io.IOException {
  74.         tileID = id;
  75.  
  76.         validTiles = new Vector[4];
  77.         for (int dir = 0; dir < 4; dir++) {
  78.             validTiles[dir] = new Vector();
  79.         }
  80.  
  81.         tile = new int[GEO_TILESIZE][GEO_TILESIZE];
  82.  
  83.         for (int i = 0; i < GEO_TILESIZE; i++) {
  84.             for (int j = 0; j < GEO_TILESIZE; j++) {
  85.                 tile[i][j] = (int)data.readByte();
  86.             }
  87.         }
  88.  
  89.         cityX = (int)data.readByte();
  90.         cityY = (int)data.readByte();
  91.  
  92.         if (tile[cityX][cityY] == 0) {
  93.             tile[cityX][cityY] = 1;
  94.         }
  95.     }
  96.  
  97.     /**
  98.      * rotate this tile 90 degrees to create a "new" tile
  99.      */
  100.     public void rotate90() {
  101.         // shift the terrain around by 90 degrees
  102.         int newTerrain[][] = new int[GEO_TILESIZE][GEO_TILESIZE];
  103.  
  104.         for (int x = 0; x < GEO_TILESIZE; x++) {
  105.             for (int y = 0; y < GEO_TILESIZE; y++) {
  106.                 newTerrain[x][y] = tile[(GEO_TILESIZE - 1 - y)][x];
  107.             }
  108.         }
  109.  
  110.         for (int x = 0; x < GEO_TILESIZE; x++) {
  111.             for (int y = 0; y < GEO_TILESIZE; y++) {
  112.                 tile[x][y] = newTerrain[x][y];
  113.             }
  114.         }
  115.  
  116.         int tmp = cityX;
  117.         cityX = GEO_TILESIZE - 1 - cityY;
  118.         cityY = tmp;
  119.     }
  120.  
  121.     /**
  122.      * return an exact copy of this tile
  123.      */
  124.     public Object clone() {
  125.         try {
  126.             GeoTile newtile = (GeoTile)super.clone();
  127.  
  128.             newtile.tileID = tileID;
  129.             newtile.tile = new int[GEO_TILESIZE][GEO_TILESIZE];
  130.  
  131.             // Q: can't find a System.arraycopy for 2d arrays 
  132.             for (int i = 0; i < GEO_TILESIZE; i++) {
  133.                 for (int j = 0; j < GEO_TILESIZE; j++) {
  134.                     newtile.tile[i][j] = tile[i][j];
  135.                 }
  136.             }
  137.  
  138.             newtile.cityX = cityX;
  139.             newtile.cityY = cityY;
  140.             newtile.hasCity = hasCity;
  141.  
  142.             newtile.validTiles = new Vector[4];
  143.  
  144.             for (int d = 0; d < 4; d++) {
  145.                 newtile.validTiles[d] = (Vector)validTiles[d].clone();
  146.             }
  147.  
  148.             return  newtile;
  149.         }
  150.         catch (Exception e) {
  151.             throw new InternalError();
  152.         }
  153.     }
  154.  
  155.     /**
  156.      * compare us to the other tile and add the other tile to our legal
  157.      * neighbour lists in each direction
  158.      */
  159.     public boolean checkCompatibility(GeoTile other) {
  160.         int DIFF = 3;
  161.  
  162.         boolean north = true;
  163.         boolean east = true;
  164.         boolean south = true;
  165.         boolean west = true;
  166.  
  167.         for (int x = 0; x < GEO_TILESIZE; x++) {
  168.             if (Math.abs(tile[x][0] - other.tile[x][GEO_TILESIZE - 1]) > DIFF)
  169.                 north = false;
  170.             if (Math.abs(other.tile[x][0] - tile[x][GEO_TILESIZE - 1]) > DIFF)
  171.                 south = false;
  172.             if (Math.abs(tile[0][x] - other.tile[GEO_TILESIZE - 1][x]) > DIFF)
  173.                 west = false;
  174.             if (Math.abs(other.tile[0][x] - tile[GEO_TILESIZE - 1][x]) > DIFF)
  175.                 east = false;
  176.         }
  177.  
  178.         if (north)
  179.             validTiles[NORTH].addElement(other);
  180.         if (east)
  181.             validTiles[EAST].addElement(other);
  182.         if (south)
  183.             validTiles[SOUTH].addElement(other);
  184.         if (west)
  185.             validTiles[WEST].addElement(other);
  186.  
  187.         return north | south | east | west;
  188.     }
  189.  
  190.     /**
  191.      * given a vector of tiles set up our own compatibility lists
  192.      */
  193.     public void findAllCompatibleTiles(Vector tiles) {
  194.         Enumeration e = tiles.elements();
  195.  
  196.         while (e.hasMoreElements()) 
  197.             checkCompatibility((GeoTile)e.nextElement());
  198.     }
  199.  
  200.     /**
  201.      * produce debugging output for this tile
  202.      */
  203.     public void asciiDump() {
  204.         for (int i = 0; i < GEO_TILESIZE; i++) {
  205.             System.out.print("[");
  206.             for (int j = 0; j < GEO_TILESIZE; j++) {
  207.                 System.out.print(" " + tile[i][j]);
  208.             }
  209.             System.out.println(" ]");
  210.         }
  211.  
  212.         if (hasCity)
  213.             System.out.print("City at: ");
  214.         else
  215.             System.out.print("City potentially at: ");
  216.  
  217.         System.out.println("(" + cityX + ", " + cityY + ")");
  218.  
  219.         for (int dir = 0; dir < 4; dir++) {
  220.             System.out.println("Connected to " + validTiles[dir].size() +
  221.                                 " in d=" + dir);
  222.         }
  223.     }
  224. }
  225.