maps/editor/scripts/MapNormalizer

118 lines
3.9 KiB
XML

<?xml version="1.0" encoding="UTF-8"?>
<script>
<name>MapNormalizer</name>
<code><![CDATA[import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.util.Iterator;
import net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeUtils;
import net.sf.gridarta.map.mapmodel.MapModel;
import net.sf.gridarta.map.mapmodel.MapSquare;
import net.sf.gridarta.model.direction.Direction;
import net.sf.gridarta.model.gameobject.GameObject;
import net.sf.gridarta.model.io.AttributeListUtils;
import net.sf.gridarta.model.io.RecursiveFileIterator;
void normalizeGameObject(GameObject gameObject) {
Iterator it = gameObject.iterator();
while (it.hasNext()) {
normalizeGameObject(it.next());
}
gameObject.setObjectText(AttributeListUtils.diffArchTextValues(gameObject.getArchetype(), gameObject.getObjectText()));
}
void normalizeMap(Path mapFile, String mapPath) {
print(mapPath);
try {
map = mapManager.openMapFile(mapFile, false);
} catch (IOException ex) {
print("Cannot load map '" + mapFile + "': " + ex.getMessage());
return;
}
try {
mapModel = map.getMapModel();
mapModel.beginTransaction("Normalize");
try {
if (!mapPath.startsWith("/styles")
&& !mapPath.startsWith("/editor/pickmaps")
&& !mapPath.startsWith("/editor/walls")) {
mapArchObject = mapModel.getMapArchObject();
int shrinkFlags = 0;
if (mapArchObject.getTilePath(Direction.NORTH).length() <= 0 && mapArchObject.getTilePath(Direction.SOUTH).length() <= 0) {
shrinkFlags |= ShrinkMapSizeUtils.SHRINK_EAST;
}
if (mapArchObject.getTilePath(Direction.EAST).length() <= 0 && mapArchObject.getTilePath(Direction.WEST).length() <= 0) {
shrinkFlags |= ShrinkMapSizeUtils.SHRINK_SOUTH;
}
ShrinkMapSizeUtils.shrinkMap(mapModel, shrinkFlags);
}
Iterator it = mapModel.iterator();
while (it.hasNext()) {
Iterator it2 = it.next().iterator();
while (it2.hasNext()) {
normalizeGameObject(it2.next());
}
}
} finally {
mapModel.endTransaction();
}
map.save();
} finally {
mapManager.release(map);
}
}
if (baseDirectory == null || baseDirectory.length() <= 0) {
baseDirectory = "/";
}
print("Normalizing maps below " + baseDirectory + "...");
if (baseDirectory.endsWith("/")) {
baseDirectory = baseDirectory.substring(0, baseDirectory.length() - 1);
}
Path mapDefaultFolder = globalSettings.getMapsDirectory();
Path rootDirectory = mapDefaultFolder.resolve(baseDirectory);
Iterator it = new RecursiveFileIterator(rootDirectory);
while (it.hasNext()) {
Path path = it.next();
String name = path.getFileName().toString();
if (Files.isRegularFile(path, new LinkOption[0])
&& path.toString().startsWith(rootDirectory.toString())
&& !name.equalsIgnoreCase("README")
&& !name.endsWith(".msg")
&& !name.endsWith(".py")
&& !name.endsWith(".png")
&& !name.endsWith(".ppm")
&& !name.endsWith(".quests")
&& !name.endsWith(".animation")
&& !name.equals("pshop_copier")
&& !name.equals("pshops_changelog")
&& !name.equals(".emergency")
&& !name.equals("ChangeLog")
&& !name.equals("COPYING")
&& !path.toString().contains("/.git/")
&& !path.toString().contains("/Info/")
&& !path.toString().contains("/editor/scripts/")) {
normalizeMap(path, path.toString().substring(mapDefaultFolder.toString().length()));
}
}
print("Done.");]]></code>
<mode>
<autoboot>false</autoboot>
<bash>true</bash>
<filter>false</filter>
</mode>
<parameter>
<name>baseDirectory</name>
<description>Base Directory</description>
<type>MapPathParameter</type>
<value>/</value>
</parameter>
</script>