import java.awt.*; import java.net.*; import java.util.*; import java.io.*; /** * This is the main thread running on each client that has connected to the gameserver. It will open a large frame and handle all interaction with the user as well as the communication with the server. * @author Torbjörn Nilsson * @author Nicklas Hjalmarsson * @version 2.4 */ public class ClientThread extends Thread { // GUI STUFF static final int FRAME_WIDTH = 1007; static final int FRAME_HEIGHT = 802; Frame f = new Frame("Flight of Dragons"); String castleURL = "http://www.lysator.liu.se/~nexus/FOD/PICS/fortress.jpg"; Image castlePic; String mapURL = "http://www.lysator.liu.se/~nexus/FOD/PICS/map4.jpg"; Image mapPic; String caveURL = "http://www.lysator.liu.se/~nexus/FOD/PICS/caveFalls2.jpg"; Image cavePic; String dragonURL1 = "http://www.lysator.liu.se/~nexus/FOD/PICS/dragonhead2.jpg"; Image dragonPic1; //single use as an Icon now!!!! String dragonURL2 = "http://www.lysator.liu.se/~nexus/FOD/PICS/brownHead.jpg"; Image dragonPic2; //single use as an Icon now!!!! String dragonURL3 = "http://www.lysator.liu.se/~nexus/FOD/PICS/dragonhead3.jpg"; Image dragonPic3; String dragonURL4 = "http://www.lysator.liu.se/~nexus/FOD/PICS/dragonhead4.jpg"; Image dragonPic4; String dragonURL5 = "http://www.lysator.liu.se/~nexus/FOD/PICS/dragonhead5.jpg"; Image dragonPic5; String dragonURL6 = "http://www.lysator.liu.se/~nexus/FOD/PICS/dragonhead6.jpg"; Image dragonPic6; String dragonURL7 = "http://www.lysator.liu.se/~nexus/FOD/PICS/dragonhead7.jpg"; Image dragonPic7; String dragonURL8 = "http://www.lysator.liu.se/~nexus/FOD/PICS/dragonhead8.jpg"; Image dragonPic8; String eggURL = "http://www.lysator.liu.se/~nexus/FOD/PICS/Eggchamber.jpg"; Image eggPic; String researchURL = "http://www.lysator.liu.se/~nexus/FOD/PICS/research.jpg"; Image researchPic; String spellRoomURL = "http://www.lysator.liu.se/~nexus/FOD/PICS/mage.jpg"; Image spellRoomPic; String gameOverURL = "http://www.lysator.liu.se/~nexus/FOD/PICS/deathhead.jpg"; Image gameOverPic; //terrain Image plainsPic; String plainsURL = "http://www.lysator.liu.se/~nexus/FOD/PICS/plains.jpg"; Image woodsPic; String woodsURL = "http://www.lysator.liu.se/~nexus/FOD/PICS/forest.jpg"; Image seaPic; String seaURL = "http://www.lysator.liu.se/~nexus/FOD/PICS/"; Image coastPic; String coastURL = "http://www.lysator.liu.se/~nexus/FOD/PICS/coast.jpg"; Image mountainsPic; String mountainsURL = "http://www.lysator.liu.se/~nexus/FOD/PICS/mountain2.jpg"; Image desertPic; String desertURL = "http://www.lysator.liu.se/~nexus/FOD/PICS/desert.jpg"; Image[] terrainPics = new Image[6]; ImageLoader imlo; Panel textPanel = new Panel(); TextArea textLines = new TextArea("Let the search for the parts of the Talisman begin...\n", 6,120); BkgPanel mainPanel; BkgPanel mapPanel; BkgPanel eggPanel; BkgPanel researchRoom; TextField researchTxt; ValueBar currMana; ValueBar currManaPerTurn; BkgPanel cavePanel; BkgPanel dragonPanel; BkgPanel zoomPanel; TextField defTxt; TextField devTxt; Panel lowPanel = new Panel(); TimerThread tt = new TimerThread(this); Panel IconPanel = new Panel(); Icon homeIcon; Icon mapIcon; Icon caveIcon; Icon eggIcon; Icon researchIcon; Icon spellRoomIcon; DragonList dragonList; BkgPanel spellRoom; BkgPanel gameOverPanel; Panel centerPanel; CardLayout centerLayout; Panel newTurnPanel; //Eggpanel knapp Button newegg; CheckboxGroup cbg = new CheckboxGroup(); Checkbox cb1 = new Checkbox("SEARCH", cbg, false); Checkbox cb2 = new Checkbox("CONQUER", cbg, false); Checkbox cb3 = new Checkbox("GOTO", cbg, true); Checkbox cb4 = new Checkbox("GUARD", cbg, false); // INTERNAL STUFF int screen = 1; //state int oldScreen = 1; boolean newTurn = true; //first round to do setup Point zoomPos; int X_SIZE = 20; int Y_SIZE = 11; int CITY_NR = 30; //FOR TESTING ONLY Area[][] map = new Area[X_SIZE][Y_SIZE]; Hashtable cities = new Hashtable(); Client client; ClientProcessing process; ValueBar stealthBar,courBar; // dragonPanel ValueBar strBar, heaBar,speBar, perBar, wisBar, ageBar, werBar; WhiteLabel wldName, wldGoal, wldMission, wldCLoc; ValueBar taxBar, uprBar, popBar, defBar, devBar; //zoomPanel static final int MAX_ZOOM_DRAGONS = 10; //max nr of dragons on zoomPanel WhiteLabel[] wlDragonNames; //the nameLabel objects WhiteLabel wlcName, wlaType, wldPresent, wlDefImp, wlDevImp, wlMoAvail; WhiteLabel wlResearchMoney, wlTreasury; WhiteArea Finances; //mainPanel WhiteLabel wlMoney, wlEggs, wlHome; WhiteLabel wlDragonNr, wlCityNr; ValueBar cityBar, dragonBar; City currentCity; Dragon zoomDragon = null; Point dragonArea = null; int dragonMarkCtr = 0; int requestname; //eggs String dragonNames; Banner banners; //animated banners on map screen boolean inCave = false; Button quitButton = new Button("Quit Game"); // PLAYER STUFF boolean gameOver=false; short playerNr; ValueBar mana = new ValueBar("Mana", 75, false); ValueBar manaPerTurn = new ValueBar("Mana recovery", 20, false); short money = 2000; short eggs = 20; String homeCityName; Hashtable dragons = new Hashtable(); Egg[] activeEggs = new Egg[4]; SpellBook spellBook = new SpellBook(); /////////////////////////////////////////////////// /** * This contructor is the only one you may use. You give one argument, a Client object, which is used to communicate with the gameserver. * This contructor will load all pictures, initialize all panels, and basically set up all GUI stuff. */ ClientThread(Client c) { client = c; process= new ClientProcessing(this); loadPictures(); mainPanel = new BkgPanel(castlePic, this, "main"); homeIcon = new Icon(castlePic,this,1); mapPanel = new BkgPanel(mapPic, this,"map"); mapIcon = new Icon(mapPic,this,2); cavePanel = new BkgPanel(cavePic); caveIcon = new Icon(cavePic,this,3); eggPanel = new BkgPanel(eggPic,this,"egg"); eggIcon = new Icon(eggPic,this,6); researchRoom = new BkgPanel(researchPic,this,"research"); researchIcon = new Icon(researchPic,this,9); zoomPanel = new BkgPanel(woodsPic, this, "zoom"); dragonPanel = new BkgPanel(dragonPic1); dragonList = new DragonList(4,false, this); spellRoom = new BkgPanel(spellRoomPic, this, "spellRoom"); spellRoomIcon = new Icon(spellRoomPic, this, 8); gameOverPanel = new BkgPanel(gameOverPic); newTurnPanel = new Panel(); newTurnPanel.add(new WhiteLabel("...and another month goes by...", 100,100)); centerPanel = new Panel(); cb1.setBackground(Color.white); cb2.setBackground(Color.white); cb3.setBackground(Color.white); cb4.setBackground(Color.white); f.resize(FRAME_WIDTH,FRAME_HEIGHT); f.setLayout(new BorderLayout()); f.setResizable(false); f.setCursor(Frame.DEFAULT_CURSOR); textPanel.add(textLines); //top textLines.setEditable(false); IconPanel.add(dragonList); IconPanel.add(homeIcon); IconPanel.add(mapIcon); // IconPanel.add(caveIcon); IconPanel.add(eggIcon); IconPanel.add(spellRoomIcon); IconPanel.add(researchIcon); lowPanel.setLayout(new BorderLayout()); //bottom lowPanel.add("West",tt); lowPanel.add("Center",IconPanel); mainPanel.setLayout(null); dragonPanel.setLayout(null); initDragonPanel(); zoomPanel.setLayout(null); initZoomPanel(); mainPanel.setLayout(null); initMainPanel(); eggPanel.setLayout(null); initEggPanel(); researchRoom.setLayout(null); initResearchRoom(); spellRoom.setLayout(null); initSpellRoom(); centerLayout = new CardLayout(); //set different screens centerPanel.setLayout(centerLayout); centerPanel.add("map",mapPanel); centerPanel.add("dragon",dragonPanel); centerPanel.add("zoom",zoomPanel); centerPanel.add("newTurn",newTurnPanel); centerPanel.add("main",mainPanel); centerPanel.add("egg",eggPanel); centerPanel.add("research",researchRoom); centerPanel.add("spells",spellRoom); centerPanel.add("gameOver",gameOverPanel); f.add("North",textPanel); f.add("South",lowPanel); f.add("Center",centerPanel); // System.out.println(toString()); } /** * This method contains the mainloop of the game on the clientside. First of all though, initial data is collected from the server. */ public void run(){ getBriefing(); //get initial gameworld wlHome.setText("Homecity: "+homeCityName); playerNr = ((City)cities.get(homeCityName)).Owner; // imlo.loadAllImages(); //do not show until all pics loaded! f.show(); dragonList.hide(); tt.kickStart(); terrainPics[0] = plainsPic; //initp TERRAIN pics terrainPics[1] = woodsPic; terrainPics[2] = seaPic; terrainPics[3] = coastPic; terrainPics[4] = mountainsPic; terrainPics[5] = desertPic; mapPanel.updateBanners(); //setup new cityBanners while(!gameOver) { getChanges(); updateScreen(); // System.out.print("before..."); try{ sleep(500); } catch(InterruptedException e){} switch(screen) { //anims!? case 1: System.out.print("1,"); break; case 2: markDragonOnMap(); // show area selected dragon is currently in System.out.print("2,"); break; case 3: System.out.print("3,"); break; case 4: System.out.print("4,"); break; case 5: System.out.print("5,"); break; case 6: System.out.print("6,"); break; case 7: System.out.print("7,"); break; case 8: System.out.print("8,"); break; case 9: System.out.print("9,"); break; default: System.out.println("Very BAD screen number: "+ screen); } System.out.flush(); // System.out.println(f.size()); } //Släng upp gameOver skärm tt.runner.stop(); centerLayout.show(centerPanel, "gameOver"); System.out.println("ovan"); unloadPictures(); System.out.println("under"); try{ sleep(5000); } catch(InterruptedException e){} System.out.println("ini"); try { client.close(); } catch(IOException e) {System.out.println("IOfel:"+e);} f.hide(); f.dispose(); System.out.println("stop"); this.stop(); } private void getChanges() { //check endofturn and screenupdates endOfTurn2(); setNewScreen2(); } private void updateScreen() { if (screen != oldScreen || newTurn) { // Runtime.getRuntime().gc(); // try { System.out.println("ENTER UPDATE_SCREEN "+oldScreen+" "+screen); System.out.flush(); Point p; // System.out.println("UPDATE--->"+mainPanel.size()+" " +mapPanel.size()); switch(oldScreen) { case 1: break; case 2: mapPanel.banner.suspend(); dragonList.hide(); /* System.out.println("f="+f); System.out.flush(); f.remove(mapPanel); */ break; case 3: //WE NEVER GET HERE BUT GO TO 5 INSTEAD!!!! // f.remove(cavePanel); break; case 4: if (currentCity != null) { System.out.println("currentCity is "+currentCity.getName()+" "+taxBar.getValue()); currentCity.Taxrate = (short)taxBar.getValue(); int nr = atoi(devTxt.getText()); //HERE WE SET MONEY-DEVELOPMENT EXCHANGE // System.out.println("devTxt --> "+nr); currentCity.developmentInvestment = nr; if (money < currentCity.developmentInvestment) { currentCity.developmentInvestment = 0; message("Not sufficient funds for the given development improvements on " + "the city of "+currentCity.getName()); } // System.out.println("HERE!!!!?"); nr = atoi(defTxt.getText()); // DEFENCE // System.out.println("defTxt --> "+nr); currentCity.defenceInvestment = nr; if (money < currentCity.defenceInvestment) { currentCity.defenceInvestment = 0; message("Not sufficient funds for the given defence improvements on " + "the city of "+currentCity.getName()); } // here could be another check to see if enough funds for both/all projects... } // f.remove(zoomPanel); zoomPanel.bkg = null; //no flashbacks from previous areas!!! break; case 5: zoomDragon.Mission = cbg.getCurrent().getLabel();; zoomDragon.Stealth = (short)stealthBar.getValue(); zoomDragon.Courage = (short)courBar.getValue(); inCave = false; /* f.remove(dragonPanel); */ break; case 6: break; case 7: break; case 8: break; case 9: int nr = atoi(researchTxt.getText()); System.out.println("ResearchTxt --> "+nr); spellBook.investment = nr; break; default: System.out.println("Very odd oldScreen-number!!!"); } System.out.println("INSIDE UPDATE SCREEN"); switch(screen) { case 1: // f.add("Center",mainPanel); setupMain(); centerLayout.show(centerPanel, "main"); break; case 2: /* f.add("Center",mapPanel); mapPanel.resize(mainPanel.size()); p = mainPanel.location(); mapPanel.move(p.x,p.y); */ System.out.println("HERE!!!"); setupMap(); centerLayout.show(centerPanel, "map"); mapPanel.paint(mapPanel.getGraphics()); //UGLY screenupdate!!! break; case 3: /* f.add("Center",cavePanel); cavePanel.resize(mainPanel.size()); p = mainPanel.location(); cavePanel.move(p.x,p.y); setupCave(); */ screen = 5; //SET EXIT TO THAT OF SCREEN 5 centerLayout.show(centerPanel, "dragon"); setupDragon(); break; case 4: /* f.add("Center",zoomPanel); zoomPanel.resize(mainPanel.size()); p = mainPanel.location(); zoomPanel.move(p.x,p.y); */ centerLayout.show(centerPanel, "zoom"); setupZoom(); break; case 5: /* f.add("Center",dragonPanel); dragonPanel.resize(mainPanel.size()); p = mainPanel.location(); dragonPanel.move(p.x,p.y); */ centerLayout.show(centerPanel, "dragon"); setupDragon(); break; case 6: /* f.add("Center",eggPanel); eggPanel.resize(mainPanel.size()); p = mainPanel.location(); eggPanel.move(p.x,p.y); setupEgg(); */ centerLayout.show(centerPanel,"egg"); setupEgg(); break; case 7: centerLayout.show(centerPanel, "newTurn"); break; case 8: centerLayout.show(centerPanel, "spells"); setupSpellRoom(); break; case 9: centerLayout.show(centerPanel, "research"); setupResearchRoom(); break; default: System.out.println("Very odd screen-number!!! "+ screen); } // System.out.println("LOCmap:"+mapPanel.location()); /* } catch (NullPointerException e) { System.out.println("Ett ATTANS fel:"+e); } */ oldScreen = screen; newTurn = false; System.out.println("LEAVING UPDATE SCREEN"); System.out.flush(); } } /** * Prints the given messsage in the top of the textpanel on the frame. */ public void message(String msg) { textLines.setText(msg +"\n"+textLines.getText()); textLines.invalidate(); } private void markDragonOnMap() { Graphics g = mapPanel.getGraphics(); if (g != null && dragonArea != null) { // System.out.println("g="+g); g.setXORMode(Color.blue); g.drawRect(dragonArea.x*50, dragonArea.y*50, 51,51); g.drawRect(dragonArea.x*50+1, dragonArea.y*50+1, 49,49); g.drawRect(dragonArea.x*50+2, dragonArea.y*50+2, 47,47); dragonMarkCtr++; // System.out.println("MARKED DRAGONS"); } } /** * Converts a string to an integer if possible, otherwise returns zero. */ public int atoi(String s) { int i; try { i = Integer.parseInt(s); } catch (NumberFormatException e) { i = 0; } return i; } ////////////////////////////////////////// SpellSchoolList schoolList; SpellList spellList; TextArea spellDescr; // SpellTargetList targetList; List targetList; Button castButton; ValueBar spellCostBar, availManaBar; /** * Initialise the spellroom panel */ private void initSpellRoom() { WhiteLabel wl = new WhiteLabel("Spell Schools", 20,20); spellRoom.add(wl); schoolList = new SpellSchoolList(6,false,this); schoolList.addItem("Gaias Lore"); schoolList.addItem("Illusions"); schoolList.addItem("Oracle"); schoolList.addItem("Necromancy"); schoolList.addItem("Evil Eye"); schoolList.addItem("Mentalism"); spellRoom.add(schoolList); schoolList.move(20,40); schoolList.resize(120,120); wl = new WhiteLabel("Spells", 150,20); spellRoom.add(wl); spellList = new SpellList(25, false, this); spellRoom.add(spellList); spellList.move(150,40); spellList.resize(200,200); //455 wl = new WhiteLabel("Spell Description", 20,380); spellRoom.add(wl); spellDescr = new TextArea(); spellRoom.add(spellDescr); spellDescr.move(20,400); spellDescr.resize(500,100); wl = new WhiteLabel("Spell Targets", 700,20); spellRoom.add(wl); // targetList = new SpellTargetList(5,false,this) targetList = new List(); targetList.setMultipleSelections(false); spellRoom.add(targetList); targetList.move(700,40); targetList.resize(200,200); //330); castButton = new Button("Cast Spell"); spellRoom.add(castButton); castButton.move(800,440); castButton.resize(100,40); availManaBar = new ValueBar("Mana available",0,false); spellCostBar = new ValueBar("Spellcasting cost",0,false); availManaBar.move(20,320); spellCostBar.move(20,350); spellRoom.add(availManaBar); spellRoom.add(spellCostBar); } /** * Set up the spellroom panel each time enters it. */ private void setupSpellRoom() { // clr all textfields but schoolList schoolList.deselect(schoolList.getSelectedIndex()); spellList.clear(); //clear all lists spellDescr.setText(""); targetList.clear(); availManaBar.setValue(mana.getValue()); spellCostBar.setValue(0); // spellCostBar.repaint(); } /** * Callbacked from spellSchoolList when a school has been selected */ public void spellSchoolSelect(Event event) { // update list with spells String school = schoolList.getItem(((Integer)event.arg).intValue()); System.out.println(school); spellList.clear(); //clear all lists spellDescr.setText(""); targetList.clear(); spellCostBar.setValue(0); spellCostBar.repaint(); Enumeration e = spellBook.spells.elements(); while(e.hasMoreElements()) { Spell s = (Spell)e.nextElement(); if ((s.getSchoolName()).equals(school)) { spellList.addItem(s.getSpellName()+" Lvl:"+s.getLevel()); } } } /** * Callbacked from spellList when a spell has been selected */ public void spellSelect(Event event) { // update list with targets and description String sName = spellList.getItem(((Integer)event.arg).intValue()); System.out.println("You chose the spell: "+sName); //update desc Spell spellObj = (Spell)spellBook.spells.get(sName.substring(0,sName.indexOf(" Lvl:"))); if (spellObj != null) { spellDescr.setText(spellBook.formatDescription(spellObj.getDescription())); spellCostBar.setValue(spellObj.getManaCost()); // spellCostBar.repaint(); // show possible targets targetList.clear(); String[] targets = spellObj.getTargetProfile(); for(int i=0;i"+sa.length); if (spell != null && target != null) { //MUST HAVE BOTH TARGET AND SPELL Spell spellObj = (Spell)spellBook.spells.get(spell); StringTokenizer st = new StringTokenizer(target, ": "); st.nextToken(); // spellObj.prepare(target.substring(target.indexOf(": ")+2), this); spellObj.prepare(st.nextToken(), this); //deselect spell and target if succesfully cast spellList.deselect(spellList.getSelectedIndex()); targetList.clear(); spellCostBar.setValue(0); // spellCostBar.repaint(); availManaBar.setValue(mana.getValue()); // availManaBar.repaint() } else if (spell != null && //MUST HAVE SPELL WITH NO TARGET (((Spell)spellBook.spells.get(spell)).getTargetProfile()).length == 0 ) { Spell spellObj = (Spell)spellBook.spells.get(spell); spellObj.prepare(" ", this); //deselect spell and target if succesfully cast spellList.deselect(spellList.getSelectedIndex()); targetList.clear(); spellCostBar.setValue(0); // spellCostBar.repaint(); availManaBar.setValue(mana.getValue()); // availManaBar.repaint() } // if we get here not all requirements were met - do nothing! Enumeration e = spellBook.spellCasts.elements(); while(e.hasMoreElements()) System.out.println(e.nextElement()); } /** * Callbacked from spellTargetList when a spell has been selected */ /* public void spellTargetSelect(Event event) { // update list with targets and description String data = targetList.getItem(((Integer)event.arg).intValue()); System.out.println(data); //insert spell into spellBook.spellCasts String tar = data.substring(data.indexOf(": ")+2); System.out.println(tar); // spellBook.spells.get } */ ////////////////////////////////////////// /** * Initialse the dragon panel, with mission and dragon settings. */ private void initDragonPanel() { strBar = new ValueBar("Strength", 0, false); dragonPanel.add(strBar); strBar.move(10,40); heaBar = new ValueBar("Health", 0 , false); dragonPanel.add(heaBar); heaBar.move(10,60); speBar = new ValueBar("Speed", 0 , false); dragonPanel.add(speBar); speBar.move(10,80); perBar = new ValueBar("Perception", 0 , false); dragonPanel.add(perBar); perBar.move(10,100); wisBar = new ValueBar("Wisdom", 0 , false); dragonPanel.add(wisBar); wisBar.move(10,120); ageBar = new ValueBar("Age", 0 , false); dragonPanel.add(ageBar); ageBar.move(10,140); werBar = new ValueBar("Weariness", 0 , false); dragonPanel.add(werBar); werBar.move(10,160); stealthBar = new ValueBar("Stealth", 0 , true); dragonPanel.add(stealthBar); stealthBar.move(10,200); courBar = new ValueBar("Courage", 50 , true); dragonPanel.add(courBar); courBar.move(10,220); // add mission radiobuttons dragonPanel.add(cb1); cb1.move(300,100); cb1.resize(100,30); dragonPanel.add(cb2); cb2.move(400,100); cb2.resize(100,30); dragonPanel.add(cb3); cb3.move(500,100); cb3.resize(100,30); dragonPanel.add(cb4); cb4.move(600,100); cb4.resize(100,30); wldName = new WhiteLabel("", 10,20); dragonPanel.add(wldName); wldName.resize(200,18); wldGoal = new WhiteLabel("",300,20); dragonPanel.add(wldGoal); wldGoal.resize(200,18); wldMission = new WhiteLabel("Mission:",300,80); dragonPanel.add(wldMission); wldCLoc = new WhiteLabel("",300,40); //curr loc dragonPanel.add(wldCLoc); wldCLoc.resize(200,18); } /** * Set up the dragon panel each time the player enters it. */ private void setupDragon() { // try{ sleep(100); } catch(InterruptedException e){} //avoid corrupt gfx // System.out.println("SEETING UP DRAGONs "+dragonPanel+(dragonPanel.getComponents()).length); // System.out.flush(); // dragonPanel.removeAll(); Graphics g = dragonPanel.getGraphics(); Dragon d = zoomDragon; if (g != null) { // System.out.println("SEETING UP DRAGONs1"); // System.out.flush(); // do some initial setup. if (inCave) { if (d.Mission.equals("") || d.Mission == null ) cbg.setCurrent(cb4); //GUARD } else { Area a = map[zoomPos.x][zoomPos.y]; d.X_destination = (short)zoomPos.x; // set the new destination d.Y_destination = (short)zoomPos.y; if (a.hasCity() && (((City)(cities.get(a.getCity()))).Owner == playerNr)) { d.MissionGoal = a.getCity(); cbg.setCurrent(cb4); //GUARD } else if (a.hasCity()) { d.MissionGoal = a.getCity(); cbg.setCurrent(cb2); //CONQUER } else { d.MissionGoal = a.getTerrain(); cbg.setCurrent(cb1); //SEARCH } } //set values stealthBar.setValue(d.Stealth); courBar.setValue(d.Courage); strBar.setValue(d.Strength); heaBar.setValue(d.Health); speBar.setValue(d.Speed); perBar.setValue(d.Perception); wisBar.setValue(d.Wisdom); ageBar.setValue(d.Age); werBar.setValue(d.Weariness); //mission? wldName.setText("Dragon is named: "+(d.name).toUpperCase()); wldGoal.setText("Goal of mission: "+d.MissionGoal); // where is dragon? Area b = map[d.X_position][d.Y_position]; if (b.hasCity()) wldCLoc.setText("Current location: "+b.getCity()); else wldCLoc.setText("Current location: "+b.getTerrain()); } } /** * Initialise the main panel. */ private void initMainPanel() { Finances = new WhiteArea(7,550,300); mainPanel.add(Finances); Finances.setText("Nothing yet!"); mainPanel.add(mana); mana.move(10,50); mainPanel.add(manaPerTurn); manaPerTurn.move(10,70); wlMoney = new WhiteLabel("", 10,100); mainPanel.add(wlMoney); wlEggs =new WhiteLabel("", 10,120); mainPanel.add(wlEggs); wlHome =new WhiteLabel("Homecity: "+homeCityName,10,140); //not known when called!!! mainPanel.add(wlHome); wlCityNr = new WhiteLabel("", 10,160); mainPanel.add(wlCityNr); cityBar = new ValueBar("City percent:",0, false); mainPanel.add(cityBar); cityBar.move(10,200); wlDragonNr = new WhiteLabel("", 10, 180); mainPanel.add(wlDragonNr); dragonBar = new ValueBar("Dragon percent:", 0, false); mainPanel.add(dragonBar); dragonBar.move(10,220); mainPanel.add(quitButton); quitButton.move(10,400); quitButton.resize(120,60); } /** * Set up the main panekl each time the player enters it. */ private void setupMain() { // try{ sleep(100); } catch(InterruptedException e){} //avoid corrupt gfx // mainPanel.removeAll(); // System.out.println("Start..."); wlMoney.setText("Money: "+money); wlEggs.setText("Eggs: "+eggs); // nr of cities owned int ownCtr= 0, allCtr = 0; for(int x=0;xIS OWN CITY!"); currentCity = c; uprBar.setValue(c.Uprising); taxBar.setValue(c.Taxrate); uprBar.resize(270,18); taxBar.resize(270,18); wlMoAvail.setText("Money available: "+money); defTxt.setText(Integer.toString(c.defenceInvestment)); devTxt.setText(Integer.toString(c.developmentInvestment)); wlMoAvail.resize(280,18); wlDefImp.resize(280,18); wlDevImp.resize(280,18); defTxt.resize(100,30); devTxt.resize(100,30); } else { //not this players city -> make these invisible! System.out.println("-->NOT OWN CITY!"); uprBar.resize(0,0); taxBar.resize(0,0); wlMoAvail.resize(0,0); wlDefImp.resize(0,0); wlDevImp.resize(0,0); defTxt.resize(0,0); devTxt.resize(0,0); System.out.println("-->NOT OWN CITY! END!!!"); } } else { //no city -> make these invisible! System.out.println(" HAS NO CITY!"); popBar.resize(0,0); defBar.resize(0,0); devBar.resize(0,0); wlcName.resize(0,0); uprBar.resize(0,0); taxBar.resize(0,0); wlMoAvail.resize(0,0); wlDefImp.resize(0,0); wlDevImp.resize(0,0); defTxt.resize(0,0); devTxt.resize(0,0); } System.out.println("Zoom1"); wlaType.setText("Terrain type in this area: "+a.getTerrain()); Enumeration e = a.getDragons(); // show dragons if (e.hasMoreElements()) wldPresent.resize(280,18); else wldPresent.resize(0,0); System.out.println("Zoom2"); for(int i=0;i= dragonImages.length) return null; else return dragonImages[i]; } private void loadPictures() { imlo = new ImageLoader(client.s.getInetAddress().getHostName()); castlePic = imlo.add(castleURL); mapPic = imlo.add(mapURL); cavePic = imlo.add(caveURL); eggPic = imlo.add(eggURL); researchPic = imlo.add(researchURL); spellRoomPic = imlo.add(spellRoomURL); gameOverPic = imlo.add(gameOverURL); dragonPic1 = imlo.add(dragonURL1); dragonPic2 = imlo.add(dragonURL2); dragonPic3 = imlo.add(dragonURL3); dragonPic4 = imlo.add(dragonURL4); dragonPic5 = imlo.add(dragonURL5); dragonPic6 = imlo.add(dragonURL6); dragonPic7 = imlo.add(dragonURL7); dragonPic8 = imlo.add(dragonURL8); plainsPic = imlo.add(plainsURL); woodsPic = imlo.add(woodsURL); seaPic = imlo.add(seaURL); coastPic = imlo.add(coastURL); mountainsPic = imlo.add(mountainsURL); desertPic = imlo.add(desertURL); } private void unloadPictures() { //NOT GAMEoVERpIC!!!! castlePic.flush(); mapPic.flush(); cavePic.flush(); eggPic.flush(); spellRoomPic.flush(); dragonPic1.flush(); dragonPic2.flush(); dragonPic3.flush(); dragonPic4.flush(); dragonPic5.flush(); dragonPic6.flush(); dragonPic7.flush(); dragonPic8.flush(); plainsPic.flush(); woodsPic.flush(); seaPic.flush(); coastPic.flush(); mountainsPic.flush(); desertPic.flush(); } /** * Called from the DragonList-class whenever an item is selected once. */ public void dragonSelect(Event event) { String data = dragonList.getItem(((Integer)event.arg).intValue()); String dname = (new StringTokenizer(data,":")).nextToken(); //assume dragonname!="" Dragon d = (Dragon)dragons.get(dname); if ((dragonMarkCtr%2) != 0) //erase old square! markDragonOnMap(); dragonArea = new Point(d.X_position,d.Y_position); dragonMarkCtr = 0; // System.out.println(dname); // System.out.println(d.X_position+" "+d.Y_position); } /** * Called from the DragonList-class whenever an item is selected TWICE */ public void dragonDualSelect(Event event) { /* String data = dragonList.getItem(((Integer)event.arg).intValue()); String dname = (new StringTokenizer(data,":")).nextToken(); //assume dragonname!="" zoomDragon = (Dragon)dragons.get(dname); */ zoomDragon = (Dragon)dragons.get( (new StringTokenizer((String)event.arg,":")).nextToken() ); if ((dragonMarkCtr%2) != 0) //erase old square! markDragonOnMap(); // System.out.println("Error DUAL-selecting dragon!"); inCave = true; setNewScreen(3); // System.out.println(dname); // System.out.println(d.X_position+" "+d.Y_position); } /** * Use this method to switch to another panel/screen. */ public void setNewScreen(int nr) { newNrOk = true; newNr = nr; } boolean newNrOk = false; int newNr = 0; private void setNewScreen2() { if (newNrOk) { System.out.println("NEW SCREEN DATA: "+newNrOk +" " + newNr); System.out.flush(); /* while(working) { //no change while setting up a screen System.out.println("WAITING..."+this.getName()+" "+getName()+" "+isAlive()); try {sleep(100);}catch (InterruptedException e){} } */ newNrOk = false; screen = newNr; } } /** * Called from TimerThread when one turn has passed. * Performs preprocessing before data is sent to server, as well as postprocessing after the new has been collected. */ public void endOfTurn() { endOfTurnVar = true; } boolean endOfTurnVar = false; // call this every loop in run() private void endOfTurn2() { if (endOfTurnVar) { // System.out.println("DOING EOT"); tt.runner.suspend(); message("-----====Next Turn====-----"); setNewScreen(7); setNewScreen2(); updateScreen(); try{ sleep(100); } catch(InterruptedException e){} //update screen process.preprocess(); sendInfo(); getBriefing(); process.postprocess(); // newTurn = true; //update screen setNewScreen(1); endOfTurnVar = false; tt.runner.resume(); mapPanel.updateBanners(); //setup new cityBanners // System.out.println("DONE EOT"); } } /** * This method is called from the panel whenever someone has clicked on "the map". */ public void mapEvent(int x, int y) { zoomPos = new Point(x/50,y/50); //this is the area that was clicked! String d = dragonList.getSelectedItem(); if (d != null && !d.equals("")) { StringTokenizer st = new StringTokenizer(d,":"); zoomDragon = (Dragon)dragons.get(st.nextElement()); setNewScreen(5); //the dragon screen } else { setNewScreen(4); // the map zoom screen } } /** * Sends information to the gameserver */ void sendInfo(){ client.send("DRAGON"); Enumeration d = dragons.elements(); while(d.hasMoreElements()){ Dragon dragon = (Dragon)d.nextElement(); if(dragon.Owner == playerNr){ client.send(dragon.Toform()); } } client.send("END"); client.send("CITY"); Enumeration c = cities.elements(); while(c.hasMoreElements()){ City city = (City)c.nextElement(); //Skicka bara de Egna städerna if(city.Owner == playerNr) client.send(city.Toform()); } client.send("END"); client.send("SPELL"); Enumeration s = spellBook.spellCasts.elements(); while(s.hasMoreElements()) client.send((String)s.nextElement()); client.send("END"); client.send("PLAYER"); client.send(homeCityName); //Sänd hemstaden client.send(""+requestname); client.send("END"); client.send("ENDTRANS"); //Nollställ anhållan om nya namn requestname = 0; } /** * Get information from the gameserver via the serverthread */ void getBriefing(){ try { boolean done = false; String str = client.in.readLine(); System.out.println("Client shall be briefed\r"); while (!done){ if (str.trim().equals("DRAGON")){ str = client.in.readLine(); while (!str.equals("END")){ Dragon d = new Dragon(str); dragons.put(d.name,d); System.out.println("Got: "+d); str = client.in.readLine(); } } else if (str.trim().equals("CITY")){ str = client.in.readLine(); while (!str.equals("END")){ System.out.println("Got : "+str); City c = new City(str); cities.put(c.Name,c); str = client.in.readLine(); } } else if (str.trim().equals("GAMEOVER")){ str = client.in.readLine(); //messages... //...format StringTokenizer stok = new StringTokenizer(str,"|@"); while(stok.hasMoreTokens()) message(stok.nextToken()); /* while(str.indexOf("|@|") != -1) //...format str = str.substring(0,str.indexOf("|@|"))+"\n"+ str.substring(str.indexOf("|@|")+3); message(str); */ //läs bort END gameOver = true; client.in.readLine(); } else if (str.trim().equals("PLAYER")){ str = client.in.readLine(); //messages... //...format StringTokenizer stok = new StringTokenizer(str,"|@"); while(stok.hasMoreTokens()) message(stok.nextToken()); /* //System.out.println("......str="+str); while(str.indexOf("|@|") != -1) //...format str = str.substring(0,str.indexOf("|@|"))+"\n"+ str.substring(str.indexOf("|@|")+3); //System.out.println("......str="+str); message(str); */ //Show theese after postproc str = client.in.readLine(); System.out.println("Homecity:"+str+":"); homeCityName = str; //Hemstaden str = client.in.readLine(); System.out.println("Dragnames:"+str+":"); dragonNames = str; //client.in.readLine(); //Names for hatclings str = client.in.readLine(); System.out.println("STR:"+str+":"); while (!str.equals("END")){ //Areas Area a = new Area(str); // System.out.println("Got: "+a); // lägg till den uppdaterade Arean till kartan Area old = a.put(map); //Ta bort eventuella Döda städer. if (old !=null && old.hasCity() && !a.hasCity()) cities.remove(old.getCity()); str = client.in.readLine(); } } else if (str.trim().equals("ENDTRANS")) break; sleep(100); str = client.in.readLine(); } System.out.println("Client briefed\r"); } catch (Exception e) { System.out.println("Exception caught in getBriefing: "+e); } } //////////////////////////////////////////// /** * Start up the ClientThread. Takes as an argument the name of the host where the StartupServer is running. Uses port 8282 to communicate through. */ public static void main(String[] a) { try { Client c = new Client(a[0],8282); ClientThread ct = new ClientThread(c); ct.start(); // ct.spellBook.spellCasts.addElement((new Grow1()).toForm()); // ct.HomeCityName = "Gondor"; } catch (IOException e) {System.out.println(e);} } }