import java.util.*; /** * @author Nicklas Hjalmarsson * @author Torbjörn Nilsson * @version 1.1 * * Datatype for a City */ public class City{ private static String[] cityNames = {"Pelargir","Gondor","Harad","Zir","Bengad","Werchi","Varpe","Katqe","Prokke","Lamad", "Severi","Olorin","Snulk","Menlad","Tann","Verreni","Nelvi","Ashla","Krell","Erech", "Bopnom","Ligver","Jhere","Hoista","Bruinen","Derive","Snizs","Quires","Balao","Melao", "Omovil","Truk","Agonau"}; private static int cityNameCtr = -1; String Name; short Population; short Defence; short Taxrate; short Development; short Uprising; short Time_to_Defence; //Count down each turn and then reset //Defence_growth short Defence_growth; //How much the Defence rises/turn short Time_to_Development; //Count down each turn and then reset //Development_growth short Development_growth; //How much the Development rises/turn short Owner; short X_position; short Y_position; int developmentInvestment = 0; int defenceInvestment = 0; /** * Default constructor. Do NOT use! */ public City(){ Name = getNewName(); Population = 5; Defence = 2; Taxrate = 25; Development = 0; Uprising = 10; Time_to_Defence = 10; Defence_growth = 1; Time_to_Development = 0; Development_growth = 0; Owner = -1; X_position = -1; Y_position = -1; } /** * This is the normally used contructor. */ public City(Area a){ this(); X_position = a.x; Y_position = a.y; a.addCity(this.Name); } /** * Use this contructor if you know both where to put the city and what to name it. */ public City(Area a, String name){ this(); Name = name; cityNameCtr--; X_position = a.x; Y_position = a.y; a.cityName = name; } private String getNewName(){ cityNameCtr++; if (cityNameCtr < cityNames.length) return cityNames[cityNameCtr]; else return "City-"+cityNameCtr; } public String getName() { return Name; } /** * @return the attackvalue of the city */ public int attack(){ return (Defence*2+Population+Development*2)/2; } /** * @return the defencevalue of the city */ public int defence(){ return (int)((Defence*4 +Population+Development)/2.5); } /** * Use to collect tax and check Uprising */ public int tax(){ if(Uprising<99){ int Up = Uprising + (Taxrate-25)+(Population/5-10); if(Up>99) Uprising = 99; else Uprising = (short)Up; } else Uprising +=(Taxrate-25)+(Population/5-10); return (int)((0.01*(float)Taxrate)*(4*Population+Development-Defence)); } /** * Use each turn so the cities grow. */ public void increasePopulation(){ int popexpansion; if(Population>5) popexpansion = 150/(Population+Uprising) ; // System.out.println("Pop:"+Population+" Upr:"+Uprising+" Exp:"+popexpansion); else popexpansion = 5; increasePopulation(popexpansion); } /** * Use to alter population */ public void increasePopulation(int ppl){ Population += ppl; if(Population > 100) Population = 100; if(Population < 1) Population = 1; } /** * Call when a city is conquered, takes as argument the conquering players name. */ public void conquer(short playernumber){ incDev(-5); incDef(-5); Owner = playernumber; Taxrate = 25; } /** * @returns true if the city is someones HomeCity */ public boolean isHomeCity(Playerinfo[] players){ boolean home = false; for(int i= 1;i 100) Defence = 100; } /** * use to alter development value */ public void incDev(int x){ Development += x; if(Development < 0) Development = 0; if(Development > 100) Development = 100; } /** * use to correct eventual malformed values */ public void tuneValues(){ if(Population < 0) Population = 0; if(Population > 100) Population = 100; if(Taxrate < 0) Taxrate = 0; if(Taxrate > 100) Taxrate = 100; if(Uprising < 0) Uprising = 0; if(Uprising > 100) Uprising = 100; if(Time_to_Defence < 0) Time_to_Defence = 0; if(Time_to_Development < 0) Time_to_Development = 0; if(Defence_growth < 0) Defence = 0; if(Population > 100) Population = 100; } /** * Constructor to make a City from a form * do not use for creating new Citys */ public City(String str){ StringTokenizer tok = new StringTokenizer(str,"|"); //System.out.println("Tokenize to "+tok.countTokens()); Name = tok.nextToken(); Population = (short)Integer.parseInt(tok.nextToken()); Defence = (short)Integer.parseInt(tok.nextToken()); Taxrate = (short)Integer.parseInt(tok.nextToken()); Development = (short)Integer.parseInt(tok.nextToken()); Uprising = (short)Integer.parseInt(tok.nextToken()); Time_to_Defence = (short)Integer.parseInt(tok.nextToken()); Defence_growth = (short)Integer.parseInt(tok.nextToken()); Time_to_Development = (short)Integer.parseInt(tok.nextToken()); Development_growth = (short)Integer.parseInt(tok.nextToken()); Owner = (short)Integer.parseInt(tok.nextToken()); X_position = (short)Integer.parseInt(tok.nextToken()); Y_position = (short)Integer.parseInt(tok.nextToken()); } /** * Use to get a form to pass through sockets from a City */ public String Toform(){ return Name+"|"+Population+"|"+Defence+"|"+Taxrate+"|"+Development+"|"+Uprising+"|"+ Time_to_Defence+"|"+Defence_growth+"|"+ Time_to_Development+"|"+ Development_growth+"|"+Owner+"|"+X_position+"|"+Y_position+"|"; } /** * @return The nr of the owner of this city. */ public int getOwner() { return (int)Owner; } /** * @return true if no player owns the city */ public boolean noOwner(){ return (Owner == -1); } /** * Set a new current owner. */ public void setOwner(int own) { Owner = (short)own; } /** * Given a map, the area in which the city is situated is returned. */ public Area getArea(Area[][] map) { return map[X_position][Y_position]; } /** * This method is used when a city grows beyond its capacity */ public City colonize(Area[][] map){ System.out.println("In i colonize "+this); Area newCitySite = this.getArea(map).getBorderingArea(map); City c = new City(newCitySite); c.Population = (short)((float)Population * 0.25); c.Owner = Owner; c.Time_to_Defence = 0; c.Defence_growth = 0; Population = (short)((float)Population * 0.75); System.out.println("the new city: "+c); return c; } /* * A nice function for debugging */ public String toString(){ return "Name:"+Name+" Own:"+Owner+" Pop:"+Population+" Defence:"+Defence+ " Xpos:"+X_position+" Ypos:"+Y_position;; } /** * USe for test only */ public static void main(String[] args){ /* for(int i=0;i<10;i++) System.out.println(new City()); } */ City c = new City(); c.Uprising = 40; for(int i=0;i<40;i++) c.increasePopulation(); } }