import java.io.*; import java.net.*; import java.util.*; /** * @author Nicklas Hjalmarsson * @version 1.0 * * The game serverclass, here is all the processing between turns done. */ public class Fod extends Thread{ Hashtable dragons= new Hashtable(); Hashtable cities= new Hashtable(); SpellBook spells = new SpellBook(); Vector playerThreads; Playerinfo[] players; Area[][] Map; Battle war; World world; Timeout timeout; /** * Called to initialize the game after all players have connected */ public void init(Vector threads){ playerThreads = threads; players = new Playerinfo[threads.size()+1]; Map = new Area[10][10]; Enumeration e = threads.elements(); while(e.hasMoreElements()){ ServerThread st = ((ServerThread)e.nextElement()); //Create playes players[st.counter] = new Playerinfo(); System.out.println(st.counter+":th spawn"); // st.out.println("Contact with FOD"); // st.out.println("ON FODDIE"); } world = new World(this); Map = world.createWorld(); world.divideCities(players); //Skapa Nonplayer-player players[0] = new Playerinfo(); //Skapa spells spells.init(); //Make sure server disappears after game... 6 hours timeout! timeout = new Timeout(60*60*6); // 60 (sec) * 60 (min) * 6 hours timeout.start(); } /** * Call this function to end the game do not call stop use this instead */ public void endGame(){ Enumeration e = playerThreads.elements(); while(e.hasMoreElements()){ ServerThread st = ((ServerThread)e.nextElement()); try{st.incoming.close(); st.stop();} catch (Exception evt){System.out.println("EOG:"+evt);} } timeout.stop(); this.stop(); } /** * Use to startup the game */ public void Go(){ //Scanna in kartan for(int playerNr=1;playerNr counter){ Moredragons = true; moveUnit(unit); } } counter++; } } private void moveUnit(Dragon dragon){ //Flytta draken diagonalt mot målet och sedan //rakt mot Map[dragon.X_position][dragon.Y_position].removeDragon(dragon.name); if(dragon.X_position < dragon.X_destination){ dragon.X_position++; dragon.incWea(5); // System.out.println("MOVE X: "+dragon.name+" "+dragon.Weariness); } if(dragon.Y_position < dragon.Y_destination){ dragon.Y_position++; dragon.incWea(5); } if(dragon.X_position > dragon.X_destination){ dragon.X_position--; dragon.incWea(5); } if(dragon.Y_position > dragon.Y_destination){ dragon.Y_position--; dragon.incWea(5); } Map[dragon.X_position][dragon.Y_position].addDragon(dragon.name); checkForCombat(dragon); } private void checkForCombat(Dragon dragon){ Enumeration drags = dragon.getArea(Map).getDragons(); while(drags.hasMoreElements()){ Dragon opponent = (Dragon)(dragons.get((String)drags.nextElement())); // System.out.println("O:"+opponent.name+"D:"+dragon.name); if(opponent.Owner != dragon.Owner){//Dragon of the same master do not fight //Check if they spot each other and wanto fight each other if((opponent.Perception-dragon.Stealth+50 > Dice.roll() && !opponent.flee(dragon)) || (dragon.Perception-opponent.Stealth+50 > Dice.roll() && !dragon.flee(opponent))){ System.out.println("Fight between "+opponent.name+" & "+dragon.name); war.fight(dragon,opponent); } } } } /** * Let the dragon do its mission, conquer cities, search for talisman or whatever */ public void executeMissions(Hashtable u){ Enumeration units = u.elements(); while(units.hasMoreElements()){ Dragon unit = (Dragon)units.nextElement(); //Mission is to conquer city XXX if(unit.Mission.equals("CONQUER")){ if(unit.getArea(Map).hasCity() ){ City c = (City)(cities.get(unit.getArea(Map).getCity())); if(unit.MissionGoal.equals(c.Name)) if(c.Owner == unit.Owner) unit.Mission = "GUARD"; else{ // Slåss mot guarddrakar boolean coward = false; //Om draken förlorar en strid sätts lcoward till true Enumeration drags = c.getArea(Map).getDragons(); if(drags.hasMoreElements()){ Dragon opponent = (Dragon)dragons.get((String)drags.nextElement()); if(opponent.Mission.equals("GUARD")) if(!war.fight(unit,opponent)){ System.out.println(unit.name+" LOST BIG TIME!!"); coward = true;} } if(!coward){ war.conquer(unit,c); unit.Mission = "GUARD"; unit.MissionGoal = ""; } } } } if(unit.Mission.equals("GOTO")){ if(unit.getArea(Map) == unit.getDestination(Map)) unit.Mission = ""; } if(unit.Mission.equals("GUARD")){ if(unit.getArea(Map).gotTalisman()){ System.out.println("Guard tal"+unit.getArea(Map).x+":"+unit.getArea(Map).y); players[unit.Owner].increaseTalismans(unit.getArea(Map)); } } if(unit.Mission.equals("SEARCH")){ if(unit.getArea(Map) == unit.getDestination(Map)){ int roll = Dice.roll(); System.out.print("\nSearch roll:"+roll+" Perc:"+unit.Perception); if(roll < unit.Perception && unit.getArea(Map).talisman){ System.out.println(" Talisman found"); players[unit.Owner].messages += "|@|A talisman was found by "+unit.name; unit.Mission = "GUARD"; }else{ System.out.println(" Talisman wasn't found"); } unit.Mission = ""; } } } } /** * Wait for the clients to send data. */ public void getInfo(){ boolean ready = false; boolean dead = false; while(!ready){ try{sleep(100);} catch(InterruptedException e){System.out.println(e);} ready = true; dead = true; for(int i = 1; i < players.length;i++){ if(players[i].ready){ System.out.println("Got info from player "+i); ready = ready && true; }else ready = false; if(players[i].dead) dead = dead && true; else dead = false; } } if(dead){ System.out.println("All players have left"); endGame(); } } /** * Used to give a unique name to new dragons */ public void baptizeHatchlings(){ //Send dragonnames for hatchlings for(int i= 1;i 0){ Dragon d = new Dragon(); dragons.put(d.name,d); ((City)cities.get(players[i].homeCity)).getArea(Map).addDragon(d.name); players[i].dragonNames += d.name+"|"; players[i].requestname--; } } } /** * This function checks if a city is about to revolt or if it should colonize a new area */ public void serverProcess(){ Enumeration c = cities.elements(); while(c.hasMoreElements()){ City city = (City)c.nextElement(); //Check for Revolting city if(city.Uprising >= 100){ boolean guarded = false; //If dragon watches town they dont dare to rebel Enumeration ds = city.getArea(Map).getDragons(); while(ds.hasMoreElements()){ Dragon d = (Dragon)dragons.get((String)ds.nextElement()); if(d.Owner == city.Owner) guarded = true; } if(guarded) //Draken måste ha ihjäl några för att lugna stämmningen city.increasePopulation(-5); else{ players[city.Owner].messages += "|@|"+city.Name+" have proclaimed it's freedom"; city.Owner = -1; //Set city owner to noOwner city.Uprising = 50; } } else if(city.Uprising >80){ players[city.Owner].messages += "|@|The people of "+city.Name+" are about to start a rebellion"; } else if(city.Uprising < 0){ city.increasePopulation(1); city.Uprising = 0; } //The other cities pop is increased on the client side if(city.noOwner()) city.increasePopulation(); //If cities have grown big move out ppl if(city.Population >= 100){ City newcity = city.colonize(Map); cities.put(newcity.Name,newcity); if(!city.noOwner()){ players[newcity.Owner].messages += "|@|People of "+city.Name+" have founded "+newcity.Name+"."; Area a = newcity.getArea(Map); if(!players[newcity.Owner].scannedareas.contains(a)) players[newcity.Owner].scannedareas.addElement(a); } }else if(city.Population > 65){ if(Dice.roll() < (city.Population-55)){ City newcity = city.colonize(Map); cities.put(newcity.Name,newcity); if(!city.noOwner()){ players[newcity.Owner].messages += "|@|People of "+city.Name+" have founded "+newcity.Name+"."; Area a = newcity.getArea(Map); if(!players[newcity.Owner].scannedareas.contains(a)) players[newcity.Owner].scannedareas.addElement(a); } } }else{ } } } /** * Check if a player is dead or a player has found all talismans */ public void gameOverCheck(){ for(int i = 1;i