import java.util.*; /** * @author Torbjörn Nilsson & Nicklas Hjalmarsson * @version 1.0 * Datatype for an area. */ public class Area{ static final int XMAPSIZE = 20; static final int YMAPSIZE = 11; static final short PLAINS = 0 ; static final short WOODS = 1 ; static final short SEA = 2; static final short COAST = 3; static final short MOUNTAINS = 4; static final short DESERT = 5; short x; short y; short terrain; Vector dragons = new Vector(2,3); //names of dragons String cityName = null; boolean talisman = false; //is there a part of the talisman in this area? /** * Default constructor. */ public Area(int x, int y){ terrain = (short)Dice.rand(5); this.x = (short)x; this.y = (short)y; } /** * Use this contructor ONLY when you have received data on the toForm-form */ public Area(String data) { StringTokenizer st = new StringTokenizer(data, ": "); String type = st.nextToken(); String xt = st.nextToken(); x = (short)Integer.parseInt(st.nextToken()); String yt = st.nextToken(); y = (short)Integer.parseInt(st.nextToken()); String terr = st.nextToken(); terrain = (short)Integer.parseInt(st.nextToken()); String city = st.nextToken(); cityName = st.nextToken(); if (cityName.equals("null")) cityName = null; String tali = st.nextToken(); talisman = (new Boolean(st.nextToken())).booleanValue(); String drag = st.nextToken(); while(st.hasMoreElements()) dragons.addElement(st.nextToken()); } /** * Get all dragons in this area as an Enumeration. */ public Enumeration getDragons() { return dragons.elements(); } /** * Turn the Area object into something sendable by sockets. */ public String toForm() { Enumeration e = dragons.elements(); String dnames =""; while(e.hasMoreElements()) dnames += " "+ e.nextElement(); return "AREA x:"+x+" y:"+y+" terr:"+terrain + " city:"+cityName+ " tali:"+talisman+" drag:"+dnames; } public String toString(){ return toForm(); } /** * * @return If the city was accepted, ie the first in this area, True is returned. Otherwize the city is not installed in this area, and False is returned. */ public boolean addCity(String name) { if(cityName == null) { cityName = name; return true; } else return false; } public void removeCity() { if(cityName == null) System.out.println("No city to remove from this Area!"); else cityName = null; } /** * @return If there is already a dragon in this Area with the given name then True is returned, nothing was added. Success returns False */ public boolean addDragon(String dragon) { boolean inside = dragons.contains(dragon); if (!inside) dragons.addElement(dragon); return inside; } public void removeDragon(String dragon) { dragons.removeElement(dragon); } /** * Marks that this area contains a part of the Talisman. */ public void setTalisman() { talisman = true; } /** * Is a part of the Talisman in this area? */ public boolean gotTalisman(){ return talisman; } /** * Is there a city in this area? */ public boolean hasCity() { // if(cityName != null) // System.out.println("City at"+x+" "+y); return (cityName != null); } /** * Returns the name of the city in this area, if any. */ public String getCity() { return cityName; } public String getTerrain() { switch(terrain){ case PLAINS: return "Plain"; case WOODS: return "Woods"; case SEA: return "Sea"; case COAST: return "Coast"; case MOUNTAINS: return "Mountains"; case DESERT: return "Desert"; } return "Unknown"; } public Area put(Area[][] map) { Area oldArea = map[x][y]; map[x][y] = this; return oldArea; } public Area getBorderingArea(Area[][] map){ Vector as = new Vector(); Area newCitySite= null; if(x >0) if(!map[x-1][y].hasCity()) as.addElement(map[x-1][y]); if(x < XMAPSIZE-1) if(!map[x+1][y].hasCity()) as.addElement(map[x+1][y]); if(y >0) if(!map[x][y-1].hasCity()) as.addElement(map[x][y-1]); if(y < YMAPSIZE-1) if(!map[x][y+1].hasCity()) as.addElement(map[x][y+1]); if(x>0 && y>0) if(!map[x-1][y-1].hasCity()) as.addElement(map[x-1][y-1]); if(x0) if(!map[x+1][y-1].hasCity()) as.addElement(map[x+1][y-1]); if(x>0 && y0){//om det finns några lediga rutor System.out.println(as.size()+" areas to choose from"); newCitySite = (Area)as.elementAt(Dice.rand(as.size()-1)); }else{ //Om all rutor kring staden är upptagna slumpas en ruta ut. boolean found = false; while(!found){ Area sa = map[Dice.rand(XMAPSIZE-1)][Dice.rand(YMAPSIZE-1)]; if(!sa.hasCity()){ found = true; newCitySite = sa; } } } return newCitySite; } /** * This method will return the immediate surrounding to the given coord according tho the map, and nr suggests the number of areas to return, 4 or 9 or ... * The method assures that the returned areas are ON the map! */ public static Vector getSurroundings(Area[][] map, int x, int y, int nr) { Vector loc = new Vector(4,9); int[] off4 = {0,0, 0,1, 1,0, 1,1}; int[] off9 = {-1,-1, -1,0, -1,1, 0,-1, 0,0, 0,1, 1,-1, 1,0, 1,1}; switch(nr) { case 4: for(int i=0;iADDED area: "+i); } } break; case 9: for(int i=0;iADDED area: "+i); } } break; } loc.trimToSize(); return loc; } private static boolean onMap(int x, int y, int offx, int offy){ return (x+offx)>=0 && (x+offx)=0 && (y+offy)