File: MapNormalizer

package info (click to toggle)
crossfire-maps 1.75.0%2Bdfsg1-1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 275,656 kB
  • sloc: python: 7,711; sql: 92; sh: 73; makefile: 7
file content (118 lines) | stat: -rw-r--r-- 3,948 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?xml version="1.0" encoding="UTF-8"?>
<script>
  <name>MapNormalizer</name>
  <code><![CDATA[import java.io.File;
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.archetype.AttributeListUtils;
import net.sf.gridarta.model.direction.Direction;
import net.sf.gridarta.model.gameobject.GameObject;
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(File 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();
        }

        if (map.getMapModel().isModified()) {
            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);
}

String mapDefaultFolder = globalSettings.getMapsDirectory().getPath();
String rootDirectory = mapDefaultFolder + baseDirectory;
Iterator it = new RecursiveFileIterator(new File(rootDirectory));
while (it.hasNext()) {
    File file = it.next();
    String name = file.getName();
    String path = file.getPath();
    if (file.isFile()
    && path.startsWith(rootDirectory)
    && !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.contains("/Info/")
    && !path.contains("/editor/scripts/")) {
        normalizeMap(file, file.getPath().substring(mapDefaultFolder.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>