1 /* $Id$ */
2 /***************************************************************************
3  *                   (C) Copyright 2003-2011 - Stendhal                    *
4  ***************************************************************************
5  ***************************************************************************
6  *                                                                         *
7  *   This program is free software; you can redistribute it and/or modify  *
8  *   it under the terms of the GNU General Public License as published by  *
9  *   the Free Software Foundation; either version 2 of the License, or     *
10  *   (at your option) any later version.                                   *
11  *                                                                         *
12  ***************************************************************************/
13 
14 /*
15  * MapConverter.java
16  *
17  * Created on 13. Oktober 2005, 18:24
18  *
19  */
20 package games.stendhal.tools;
21 
22 import java.io.File;
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26 
27 import org.apache.tools.ant.BuildException;
28 import org.apache.tools.ant.DirectoryScanner;
29 import org.apache.tools.ant.Task;
30 import org.apache.tools.ant.types.FileSet;
31 
32 import tiled.core.Map;
33 import tiled.core.MapLayer;
34 import tiled.core.Tile;
35 import tiled.core.TileLayer;
36 import tiled.core.TileSet;
37 import tiled.io.TMXMapReader;
38 import tiled.io.TMXMapWriter;
39 
40 /**
41  * Fix maps by loading and saving them.
42  *
43  * @author mtotz, miguel
44  */
45 public class MapUpdater extends Task {
46 	/** list of *.tmx files to convert. */
47 	private final List<FileSet> filesets = new ArrayList<FileSet>();
48 
49 	/* mostly copied from dialog/TilesetManager.java, except that
50 	   many things refuse to compile with the usual tiled.jar,
51 	   so rewrote it to work with more primitive interfaces. */
isUsedTileset(final Map map, final TileSet tileset)52 	private boolean isUsedTileset(final Map map, final TileSet tileset) {
53 		for (final Iterator< ? > tiles = tileset.iterator(); tiles.hasNext();) {
54 			final Tile tile = (Tile) tiles.next();
55 
56 			for (final MapLayer layer : map) {
57 				if ((layer instanceof TileLayer) && (((TileLayer) layer).isUsed(tile))) {
58 					return true;
59 				}
60 			}
61 		}
62 
63 		return false;
64 	}
65 
66 	/**
67 	 * Remove unused tilesets.
68 	 * @param map
69 	 */
removeUnusedTilesets(final Map map)70 	private void removeUnusedTilesets(final Map map) {
71 		for (final Iterator< ? > sets = map.getTileSets().iterator(); sets.hasNext();) {
72 			final TileSet tileset = (TileSet) sets.next();
73 
74 			if (!isUsedTileset(map, tileset)) {
75 				sets.remove();
76 			}
77 		}
78 	}
79 
80 	/**
81 	 * Remove unused roof layers.
82 	 *
83 	 * @param map
84 	 */
removeUnusedLayers(final Map map)85 	private void removeUnusedLayers(final Map map) {
86 		Iterator<MapLayer> iter = map.iterator();
87 		while (iter.hasNext()) {
88 			MapLayer layer = iter.next();
89 			if (layer.isEmpty()) {
90 				// Client merges floor layers, and removing anything there
91 				// prevents it doing that. Removing unused roof layers, however
92 				// saves drawing effort.
93 				if ("3_roof".equals(layer.getName())
94 						|| "4_roof_add".equals(layer.getName())) {
95 					iter.remove();
96 				}
97 			}
98 		}
99 	}
100 
101 	/** Converts the map files.
102 	 * @param tmxFile
103 	 * @throws Exception */
convert(final String tmxFile)104 	public void convert(final String tmxFile) throws Exception {
105 		final File file = new File(tmxFile);
106 
107 		final String filename = file.getAbsolutePath();
108 		final Map map = new TMXMapReader().readMap(filename);
109 		removeUnusedTilesets(map);
110 		removeUnusedLayers(map);
111 		new TMXMapWriter().writeMap(map, filename);
112 	}
113 
114 	/**
115 	 * Adds a set of files to copy.
116 	 *
117 	 * @param set
118 	 *            a set of files to copy
119 	 */
addFileset(final FileSet set)120 	public void addFileset(final FileSet set) {
121 		filesets.add(set);
122 	}
123 
124 	/**
125 	 * ants execute method.
126 	 */
127 	@Override
execute()128 	public void execute() {
129 		try {
130 			for (final FileSet fileset : filesets) {
131 				final DirectoryScanner ds = fileset.getDirectoryScanner(getProject());
132 				final String[] includedFiles = ds.getIncludedFiles();
133 				for (final String filename : includedFiles) {
134 					System.out.println(ds.getBasedir().getAbsolutePath()
135 							+ File.separator + filename);
136 					convert(ds.getBasedir().getAbsolutePath() + File.separator
137 							+ filename);
138 				}
139 			}
140 		} catch (final Exception e) {
141 			throw new BuildException(e);
142 		}
143 	}
144 
main(final String[] args)145 	public static void main(final String[] args) throws Exception {
146 		if (args.length < 1) {
147 			System.out.println("usage: java games.stendhal.tools.MapConverter <tmx file>");
148 			return;
149 		}
150 
151 		// do the job
152 		final MapUpdater converter = new MapUpdater();
153 		converter.convert(args[0]);
154 	}
155 }
156