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

  1. /*
  2.  * @(#)GeoMorph.java
  3.  */
  4. package games.Battle.server.GeoMorph;
  5.  
  6. import java.awt.*;
  7. import java.io.*;
  8. import java.util.*;
  9.  
  10. /**
  11.  * GeoMorph creates worlds based on pre-constructed 4x4 tiles of terrain. Since
  12.  * each tile has been constructed by hand, it leads to "strategically 
  13.  * interesting" terrain, instead of totally random terrain. Also, each tile
  14.  * can only contain one city which helps keeps the cities spaced apart, and
  15.  * the city goes on a particular square that "makes sense" for the particular
  16.  * tile it is in.
  17.  *
  18.  * @version 1.00
  19.  * @author Alex Nicolaou
  20.  * @author Jay Steele
  21.  */
  22.  
  23. public class GeoMorph {
  24.     /**
  25.      * a vector of all the tiles we have to choose from
  26.      */
  27.     Vector tiles;
  28.  
  29.     /**
  30.      *  the size of the world (in tiles) that we're going to build
  31.      */
  32.     public int WORLDSIZE = 4;
  33.  
  34.     /**
  35.      * an array to hold the world tiles.
  36.      */
  37.     GeoTile[][] world;
  38.  
  39.     /**
  40.      * Construct a GeoMorph world. Load the tile files from the file base
  41.      * given in basename, with the tile number appended onto it. Create a 
  42.      * world that is numTiles by numTiles in size. The tile files are loaded,
  43.      * and from each tile 4 tiles are generated, one for the original 
  44.      * orientation and 3 for each 90 degree rotation. Mirror images are not
  45.      * used.
  46.      *
  47.      * @param basename the prefix of all the tile filenames
  48.      * @param numTiles the size of the world in tiles: 4 means 4x4 tiles
  49.      */
  50.     public GeoMorph(String basename, int numTiles) {
  51.         WORLDSIZE = numTiles;
  52.         tiles = new Vector(32);
  53.  
  54.         int tileNum = 0;
  55.         File infile = new File(basename + tileNum);
  56.         while (infile.exists()) {
  57.             try {
  58.                 FileInputStream file = new FileInputStream(infile);
  59.                 DataInputStream in = new DataInputStream(file);
  60.                 GeoTile tile = new GeoTile(in, tileNum);
  61.  
  62.                 tiles.addElement(tile);
  63.                 tile = (GeoTile)tile.clone();
  64.                 tile.rotate90();
  65.                 tiles.addElement(tile);
  66.                 tile = (GeoTile)tile.clone();
  67.                 tile.rotate90();
  68.                 tiles.addElement(tile);
  69.                 tile = (GeoTile)tile.clone();
  70.                 tile.rotate90();
  71.                 tiles.addElement(tile);
  72.             }
  73.             catch (Exception e) {
  74.                 System.out.println("error " + e);
  75.             }
  76.  
  77.             tileNum++;
  78.             infile = new File(basename + tileNum);
  79.         }
  80.  
  81.         Enumeration e = tiles.elements();
  82.         while (e.hasMoreElements()) {
  83.             GeoTile tile = (GeoTile)e.nextElement();
  84.             tile.findAllCompatibleTiles(tiles);
  85.         }
  86.  
  87.         world = new GeoTile[WORLDSIZE][WORLDSIZE];
  88.  
  89.         Random rand = new Random();
  90.         int startTile = Math.abs(rand.nextInt()) % tiles.size();
  91.         world[0][0] = (GeoTile)tiles.elementAt(startTile);
  92.         for (int i = 1; i < WORLDSIZE; i++) {
  93.             Vector validTiles = world[i - 1][0].validTiles[1];
  94.             int tnum = Math.abs(rand.nextInt()) % validTiles.size();
  95.             world[i][0] = (GeoTile)validTiles.elementAt(tnum);
  96.         }
  97.         for (int y = 1; y < WORLDSIZE; y++) {
  98.             for (int i = 0; i < WORLDSIZE; i++) {
  99.                 Vector restrictFromNorth = world[i][y-1].validTiles[2];
  100.  
  101.                 Vector restrictFromWest = null;
  102.                 if (i > 0) {
  103.                     restrictFromWest = world[i - 1][y].validTiles[1];
  104.  
  105.                     Vector validTiles = new Vector();
  106.                     Enumeration north = restrictFromNorth.elements();
  107.                     while (north.hasMoreElements()) {
  108.                         Object tile = north.nextElement();
  109.                         if (restrictFromWest.contains(tile))
  110.                             validTiles.addElement(tile);
  111.                     }
  112.  
  113.                     if (validTiles.size() > 0) {
  114.                         int tnum = Math.abs(rand.nextInt());
  115.                         tnum = tnum % validTiles.size();
  116.                         world[i][y] = (GeoTile)validTiles.elementAt(tnum);
  117.                     }
  118.                     else {
  119.                         int tnum = Math.abs(rand.nextInt());
  120.                         tnum = tnum % restrictFromNorth.size();
  121.                         world[i][y] = (GeoTile)restrictFromNorth.elementAt(tnum);
  122.                     }
  123.                 }
  124.                 else {
  125.                     int tnum = Math.abs(rand.nextInt());
  126.                     tnum = tnum % restrictFromNorth.size();
  127.                     world[i][y] = (GeoTile)restrictFromNorth.elementAt(tnum);
  128.                 }
  129.             }
  130.         }
  131.  
  132.     }
  133.  
  134.     /**
  135.      * return the level of terrain at a particular board location
  136.      * @param bx the x location on the actual world
  137.      * @param by the y location on the actual world
  138.      */
  139.     public int terrain(int bx, int by) {
  140.         int tx = bx / 4;
  141.         int ty = by / 4;
  142.         int offx = bx % 4;
  143.         int offy = by % 4;
  144.  
  145.         return world[tx][ty].tile[offx][offy];
  146.     }
  147.  
  148.     /**
  149.      * chooses a tile at random and returns that tile's city hint in world
  150.      * co-ordinates.
  151.      */
  152.     public Point getRandomCity() {
  153.         Random rand = new Random();
  154.         int tx = Math.abs(rand.nextInt()) % 4;
  155.         int ty = Math.abs(rand.nextInt()) % 4;
  156.  
  157.         return new Point(tx * 4 + world[tx][ty].cityX, ty * 4 + world[tx][ty].cityY);
  158.     }
  159. }
  160.