⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mapeditor.java

📁 java 开源游戏源码 RISK 联机对战 战棋类
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			}
			else if (disjoin.isSelected()) {

				editPanel.setMode(MapEditorPanel.MODE_DISJOIN);

			}
			else if (draw.isSelected()) {

				editPanel.setMode(MapEditorPanel.MODE_DRAW);

			}
			else {

				throw new RuntimeException("unknown mode");

			}

		}
		else if (a.getActionCommand().equals("zoomin")) {

			zoom(true);


		}
		else if (a.getActionCommand().equals("zoomout")) {

			zoom(false);

		}
		else if (a.getActionCommand().equals("fix")) {

			removeBadMapColors();

		}
		else {

			throw new RuntimeException("unknown command: "+a.getActionCommand());

		}


	}

	private void removeBadMapColors() {

		Map updateMap = new HashMap();

		// go though ALL the colors that can be in the image map
		for (int c=0;c<256;c++) {

			if (myMap.getCountryInt(c)!=null) {

				updateMap.put(new Integer(c),new Integer(c));

			}
			else {

				updateMap.put(new Integer(c),new Integer(255));

			}
		}

		editPanel.update(updateMap);

		editPanel.repaint();

	}

	private BufferedImage makeRGBImage(BufferedImage INipic) {

		BufferedImage ipic = new BufferedImage(INipic.getWidth(), INipic.getHeight(), BufferedImage.TYPE_INT_BGR);

		Graphics g1 = ipic.getGraphics();

		g1.setColor( Color.WHITE );

		g1.fillRect(0,0,INipic.getWidth(), INipic.getHeight());

		g1.drawImage(INipic,0,0,this);

		g1.dispose();

		return ipic;

	}

	public void zoom(boolean in) {

		setZoom( (in)?(zoomint+1):(zoomint-1) );

	}

	private void setZoom(int a) {

		if (a<ZOOM_MIN || a>ZOOM_MAX) { return; }

		zoomint = a;

		zoomout.setEnabled( !(a==ZOOM_MIN) );

		zoomin.setEnabled( !(a==ZOOM_MAX) );

		zoom.setText(zoomint+"x");

		editPanel.zoom(a);

	}

	public void showError(Throwable ex) {

		JOptionPane.showMessageDialog(this, "Error: "+ex.toString(), "ERROR!", JOptionPane.ERROR_MESSAGE);
		ex.printStackTrace();

	}

	public BufferedImage getImageMap() {

		return editPanel.getImageMap();

	}


	public BufferedImage getImagePic() {

		return editPanel.getImagePic();

	}

	public String getStringForContinent(Continent c) {

		if (c == null) {

			return "0";

		}
		if (c == RiskGame.ANY_CONTINENT) {

			return "*";

		}

		Continent[] continents = myMap.getContinents();

		for (int i = 0; i < continents.length; i++) {

			if (continents[i] == c) {

				return String.valueOf(i+1);

			}

		}

		throw new RuntimeException();

	}

	public boolean checkMap() {

		String errors="";

		if (myMap.getNoCountries() < 6) {

			errors = errors + "\n* Less then 6 countries on this map.";

		}
		else {

			Vector t = new Vector(Arrays.asList(myMap.getCountries()));
			Vector a = new Vector();

			Country country = ((Country)t.remove(0));
			a.add( country );

			myMap.getConnectedEmpire(
				t,
				a,
				country.getNeighbours(),
				null
			);

			if (a.size() != myMap.getNoCountries()) {

				errors = errors + "\n* Some countries are isolated from the rest: "+t;

			}

		}

		Continent[] continents = myMap.getContinents();

		for (int c=0;c<continents.length;c++) {

			if (continents[c].getTerritoriesContained().size() == 0) {

				errors = errors + "\n* The continent \""+continents[c]+"\" is empty.";

			}

		}


		BufferedImage map = editPanel.getImageMap();
		//if (map.getWidth()!=PicturePanel.PP_X || map.getHeight()!=PicturePanel.PP_Y) {
		//	errors = errors + "\n* Image Map is not a standard size of "+PicturePanel.PP_X+"x"+PicturePanel.PP_Y+".";
		//}

		BufferedImage pic = editPanel.getImagePic();
		if (pic.getWidth()!=map.getWidth() || pic.getHeight()!=map.getHeight()) {

			errors = errors + "\n* ImagePic and ImageMap are not the same size.";

		}


		int[] pixels = map.getRGB(0,0,map.getWidth(),map.getHeight(),null,0,map.getWidth());

		int color,noc = myMap.getNoCountries();
		HashSet bad = new HashSet();
		HashSet good = new HashSet( Arrays.asList(myMap.getCountries()) );

		for (int c=0;c<pixels.length;c++) {

			color = pixels[c] & 0xff;

			if (color == 255) {

				// ignore

			}
			else if (color == 0 || color > noc) {

				bad.add( new Integer(color) );

			}
			else {

				good.remove( myMap.getCountryInt(color) );

			}
		}

		if (bad.size() > 0) {

			errors = errors + "\n* Image Map uses colors that do not match any country: "+bad;

		}
		if (good.size() > 0) {

			errors = errors + "\n* Image Map does not contain areas for some countries: "+good;

		}

		// missions checks:

		Vector missions = myMap.getMissions();

		if (missions.size()>0 && missions.size() <6) {

			errors = errors + "\n* You have chosen to have missions but you have less then is needed for a game with 6 players.";

		}

		for (int i = 0; i < missions.size(); i++) {

			Mission m = (Mission)missions.elementAt(i);

			Player p = m.getPlayer();

			if (p !=null && m.getDiscription().indexOf( p.getName() ) == -1) {

				errors = errors + "\n* You have a mission that is to destroy "+p.getName()+", yet you do NOT have the text \""+p.getName()+"\" in the description.";

			}

		}

		if (errors.length() >0) {

			JOptionPane.showMessageDialog(this,"There are errors in this map that need to be fixed before it can be used:"+errors);

			return false;

		}

		return true;

	}

	public void saveMap(String mapName) throws Exception {


	    String name = mapName.substring(0, mapName.lastIndexOf('.') );

	    String cardsName = name + "." + RiskFileFilter.RISK_CARDS_FILES;
	    String imageMapName = name+"_map."+IMAGE_MAP_EXTENSION;
	    String imagePicName = name+"_pic."+IMAGE_PIC_EXTENSION;

	    File mapFile = new File( new URI(RiskUtil.mapsdir.toString()+"/"+mapName) );
	    File cardsFile = new File( new URI(RiskUtil.mapsdir.toString()+"/"+cardsName) );
	    File imageMapFile = new File( new URI(RiskUtil.mapsdir.toString()+"/"+imageMapName) );
	    File imagePicFile = new File( new URI(RiskUtil.mapsdir.toString()+"/"+imagePicName) );

	    if (mapFile.exists() || cardsFile.exists() || imageMapFile.exists() || imagePicFile.exists()) {

		int result = JOptionPane.showConfirmDialog(this, "Are you sure you want to replace:\n"+
		(mapFile.exists()?mapFile+"\n":"")+
		(cardsFile.exists()?cardsFile+"\n":"")+
		(imageMapFile.exists()?imageMapFile+"\n":"")+
		(imagePicFile.exists()?imagePicFile+"\n":""), "Replace?", JOptionPane.YES_NO_OPTION);

		if (result != JOptionPane.YES_OPTION) {

			return;

		}

	    }

	    String n = System.getProperty("line.separator");

	    // ####################################################### MAKE CARDS FILE

	    StringBuffer cardsBuffer = new StringBuffer();

	    cardsBuffer.append("; cards: ");
	    cardsBuffer.append(cardsName);
	    cardsBuffer.append(n);
	    cardsBuffer.append("; cards made with the map maker");
	    cardsBuffer.append(n);
	    cardsBuffer.append("; yura.net Risk ");
	    cardsBuffer.append( Risk.RISK_VERSION );
	    cardsBuffer.append(n);

	    cardsBuffer.append(n);
	    cardsBuffer.append("[cards]");
	    cardsBuffer.append(n);

	    Vector cards = myMap.getCards();

            for (int i = 0; i < cards.size(); i++) {

                Card c = (Card)cards.elementAt(i);

		cardsBuffer.append(c.getName());

		if (c.getCountry()!=null) {

			int color = c.getCountry().getColor();

			if (strictcards && color != (i+1)) { throw new Exception("cards missmatch with pos/id/color: "+c); }

			cardsBuffer.append(" ");
			cardsBuffer.append( String.valueOf(color) );

		}

		cardsBuffer.append(n);

	    }

	    Vector missions = myMap.getMissions();

	    if (missions.size()>0) {

	    	cardsBuffer.append(n);
	    	cardsBuffer.append("; destroy x occupy x x continents x x x");
	    	cardsBuffer.append(n);
	    	cardsBuffer.append("; destroy (Player) occupy (int int) continents (Continent Continent Continent)");
	    	cardsBuffer.append(n);
	    	cardsBuffer.append("[missions]");
	    	cardsBuffer.append(n);

		for (int i = 0; i < missions.size(); i++) {

			Mission m = (Mission)missions.elementAt(i);

			if (m.getPlayer()!=null) {

				cardsBuffer.append( m.getPlayer().getName().substring(6,7) ); // PLAYER1

			}
			else {

				cardsBuffer.append("0");

			}

			cardsBuffer.append("\t");
			cardsBuffer.append(String.valueOf( m.getNoofcountries() ));
			cardsBuffer.append(" ");
			cardsBuffer.append(String.valueOf( m.getNoofarmies() ));
			cardsBuffer.append("\t");
			cardsBuffer.append( getStringForContinent(m.getContinent1()) );
			cardsBuffer.append(" ");
			cardsBuffer.append( getStringForContinent(m.getContinent2()) );
			cardsBuffer.append(" ");
			cardsBuffer.append( getStringForContinent(m.getContinent3()) );
			cardsBuffer.append("\t");
			cardsBuffer.append(m.getDiscription());
			cardsBuffer.append(n);
		}

	    }

	    // ####################################################### MAKE MAP FILE

	    StringBuffer buffer = new StringBuffer();

	    buffer.append("; map: ");
	    buffer.append(mapName);
	    buffer.append(n);
	    buffer.append("; map made with the map maker");
	    buffer.append(n);
	    buffer.append("; yura.net Risk ");
	    buffer.append( Risk.RISK_VERSION );
	    buffer.append(n);

	    buffer.append(n);
	    buffer.append("[files]");
	    buffer.append(n);

	    buffer.append("pic ");
	    buffer.append(imagePicName);
	    buffer.append(n);
	    buffer.append("map ");
	    buffer.append(imageMapName);
	    buffer.append(n);
	    buffer.append("crd ");
	    buffer.append(cardsName);
	    buffer.append(n);

	    buffer.append(n);
	    buffer.append("[continents]");
	    buffer.append(n);

	    Continent[] continents = myMap.getContinents();

            for (int i = 0; i < continents.length; i++) {

                Continent c = continents[i];

		buffer.append(c.getIdString());
		buffer.append(" ");
		buffer.append(c.getArmyValue());
		buffer.append(" ");
		buffer.append( RiskUtil.getStringForColor( c.getColor() ) );
		buffer.append(n);

	    }

	    buffer.append(n);
	    buffer.append("[countries]");
	    buffer.append(n);

	    Country[] countries = myMap.getCountries();

            for (int i = 0; i < countries.length; i++) {

                Country c = countries[i];

		int color = c.getColor();

		if (color != (i+1)) { throw new Exception("country missmatch with pos/id/color: "+c); }

		buffer.append(String.valueOf(color));
		buffer.append(" ");
		buffer.append(c.getIdString());
		buffer.append(" ");
		buffer.append( getStringForContinent( c.getContinent() ) );
		buffer.append(" ");
		buffer.append( c.getX() );
		buffer.append(" ");
		buffer.append( c.getY() );
		buffer.append(n);

	    }


	    buffer.append(n);
	    buffer.append("[borders]");
	    buffer.append(n);


            for (int i = 0; i < countries.length; i++) {

                Country c = countries[i];

		buffer.append(String.valueOf(i+1));


                List ney = c.getNeighbours();
                for (int j = 0; j < ney.size(); j++) {

                    Country n1 = (Country)ney.get(j);

		    buffer.append(" ");
		    buffer.append(String.valueOf( n1.getColor() ) );
		}

		buffer.append(n);

	    }


	    Writer output = null;
	    try {

		output = new BufferedWriter( new FileWriter(mapFile) );
		output.write( buffer.toString() );

	    }
	    finally {

		if (output != null) output.close();
	    }

	    try {

		output = new BufferedWriter( new FileWriter(cardsFile) );
		output.write( cardsBuffer.toString() );

	    }
	    finally {

		if (output != null) output.close();
	    }

	    if (

		!ImageIO.write( editPanel.getImageMap() , IMAGE_MAP_EXTENSION , imageMapFile ) |

		!ImageIO.write( editPanel.getImagePic() , IMAGE_PIC_EXTENSION , imagePicFile )

	    ) {

		// this should NEVER happen
		throw new Exception("unable to save image files!\nPlease email yura@yura.net and tell!");

	    }
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -