summaryrefslogtreecommitdiff
path: root/tmwcon/src/converter/Process.java
blob: ab2eb8018da7a36b3643a0e5ed3d33c6e45bdf98 (plain) (blame)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 * TMWServ to eAthena Converter (c) 2008, 2011 Jared Adams
 * License: GPL, v2 or later
 */

package converter;

import java.awt.*;
import java.io.*;
import java.util.Iterator;
import java.util.Properties;
import java.util.TreeSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;

import tiled.core.*;
import tiled.plugins.tmw.*;

public class Process {
    private static final String mobFile = "_mobs.txt";
    private static final String warpFile = "_warps.txt";
    private static final String importFile = "_import.txt";
    private static File server_data;
    private static File script_directory;
    private static File wlkFolder;

    private static WLKInterface wlk = null;

    public static void setServerData(File folder) {
        server_data = folder;
        script_directory = new File(server_data, "npc/");
        wlkFolder = new File(server_data, "data/");
        wlk = new WLKInterface();
    }

    private static String getProp(Properties props, String name, String def) {
        if (name == null) return def;
        for (java.util.Map.Entry<Object, Object> entry : props.entrySet()) {
            if (name.equalsIgnoreCase(entry.getKey().toString())) {
                return entry.getValue().toString();
            }
        }
        return def;
    }

    private static int getProp(Properties props, String name, int def) {
        if (name == null) return def;
        try {
            return Integer.parseInt(getProp(props, name, "?"));
        } catch (Exception e) {}
        return def;
    }

    private static int[] resolveBounds(Rectangle in, boolean warp) {
        int x = in.x / 32;
        int y = in.y / 32;
        int width = in.width / 32;
        int height = in.height / 32;
        if (!warp) {
            if (width > 1) --width;
            if (height > 1) --height;
        }
        x += width / 2;
        y += height / 2;
        if (warp) {
            width -= 2;
            height -= 2;
        }
        return new int[]{x, y, width, height};
    }

    private static void handleWarp(PrintWriter out, String map, String name, Rectangle bounds, Properties props) {
        if (out == null) return;
        String dest = getProp(props, "dest_map", null);
        if (dest == null) return;
        int x = getProp(props, "dest_x", -1);
        if (x < 0) return;
        int y = getProp(props, "dest_y", -1);
        if (y < 0) return;
        int[] shape = resolveBounds(bounds, true);
        System.out.printf("Usable warp found: %s\n", name);
        out.printf("%s.gat,%d,%d\twarp\t%s\t%d,%d,%s.gat,%d,%d\n",
                   map, shape[0], shape[1], name, shape[2], shape[3], dest, x / 32, y / 32);
    }

    private static int handleMob(PrintWriter out, String map, String name, Rectangle bounds, Properties props) {
        if (out == null) return -1;
        int mob = getProp(props, "monster_id", -1);
        if (mob < 0) return -1;
        mob += 1002;
        int max = getProp(props, "max_beings", 1);
        int time1 = getProp(props, "eA_spawn", 0);
        int time2 = getProp(props, "eA_death", 0);
        int[] shape = resolveBounds(bounds, false);
        System.out.printf("Usable mob found: %s (%d)\n", name, mob);
        out.printf("%s.gat,%d,%d,%d,%d\tmonster\t%s\t%d,%d,%d,%d,Mob%s::On%d\n",
                   map, shape[0], shape[1], shape[2], shape[3], name, mob, max, time1, time2, map, mob);
        return mob;
    }

    private static void processObject(MapObject mo, String map, PrintWriter warpOut, PrintWriter mobOut, TreeSet<Integer> mobs) {
        if (mo == null) return;
        String name = mo.getName();
        String type = mo.getType();
        Rectangle bounds = new Rectangle(mo.getBounds());
        Properties props = mo.getProperties();

        if (type.equalsIgnoreCase("warp")) {
            handleWarp(warpOut, map, name, bounds, props);
        } else if (type.equalsIgnoreCase("spawn")) {
            mobs.add(handleMob(mobOut, map, name, bounds, props));
        }
    }

    private static void processObjects(Iterator<MapObject> objs, String map, PrintWriter warpOut, PrintWriter mobOut, TreeSet<Integer> mobs) {
        MapObject mo;
        while (objs.hasNext()) {
            mo = objs.next();
            if (mo == null) continue;
            processObject(mo, map, warpOut, mobOut, mobs);
        }
    }

    private static void processFiles(File folder, List<String> out) {
        for (File f : folder.listFiles()) {
            if (f.isDirectory()) {
                processFiles(folder, out);
            } else if (!f.getName().equals(importFile)) {
                out.add("npc: " + f.getPath().substring(server_data.getPath().length() + 1));
            }
        }
    }

    private static void makeInclude(String name, String title, File folder) {
        File _import = new File(folder, importFile);
        List<String> output_elements = new ArrayList<String>();
        processFiles(folder, output_elements);
        PrintWriter importOut = Main.getWriter(_import);
        importOut.printf("// Map %s: %s\n", name, title);
        importOut.printf("// This file is generated automatically. All manually changes will be removed when running the Converter.\n");
        importOut.printf("map: %s.gat\n", name);
        Collections.sort(output_elements);
        for (String s : output_elements)
            importOut.println(s);
        importOut.flush();
        importOut.close();
    }

    public static String processMap(String name, Map map, File mapFile, PrintWriter summary) {
        if (name == null) return null;
        if (map == null) return null;

        Properties props = map.getProperties();
        String title = getProp(props, "name", "");

        String folderName =  "npc/" + name;

        File folder = new File(script_directory, name);

        System.out.println(title);

        File wlkFile = new File(wlkFolder, name + ".wlk");

        if (wlkFile.exists() && mapFile.lastModified() < wlkFile.lastModified()) {
            System.out.println("Up to date, skipping");
            makeInclude(name, title, folder);
            return folderName;
        }

        if (summary != null) {
            summary.printf("\tName: %s: '%s'\n", name, title);
            summary.printf("\tMusic: '%s'\n", getProp(props, "music", ""));
            summary.printf("\tMinimap: '%s'\n", getProp(props, "minimap", ""));
        }

        if (wlk != null) wlk.write(name, map, wlkFile);

        PrintWriter warpOut = Main.getWriter(new File(folder, warpFile));
        PrintWriter mobOut = Main.getWriter(new File(folder, mobFile));

        warpOut.printf("// This file is generated automatically. All manually changes will be removed when running the Converter.\n// %s warps\n\n", title);
        mobOut.printf("// This file is generated automatically. All manually changes will be removed when running the Converter.\n// %s mobs\n\n", title);

        TreeSet<Integer> mobs = new TreeSet<Integer>();
        processObjects(map.getObjects(), name, warpOut, mobOut, mobs);
        for (MapLayer layer : map) {
            if (layer instanceof ObjectGroup) {
                processObjects(((ObjectGroup) layer).getObjects(), name, warpOut, mobOut, mobs);
            }
        }

        warpOut.flush();
        warpOut.close();

        System.out.println("Starting mob points");
        mobOut.printf("\n\n%s.gat,0,0,0\tscript\tMob%1$s\t-1,{\n", name);
        for (int mob : mobs) {
            if (mob == -1) continue;
            mobOut.printf("On%d:\n\tset @mobID, %d;\n\tcallfunc \"MobPoints\";\n\tbreak;\n\n", mob, mob);
        }
        mobOut.printf("\tend;\n}\n");
        System.out.println("Finished mob points");

        mobOut.flush();
        mobOut.close();

        makeInclude(name, title, folder);

        return folderName;
    }

    public static void writeMasterImport(ArrayList<String> folders) {
        File master = new File(script_directory, importFile);
        PrintWriter out = Main.getWriter(master);
        if (out == null) return;

        List<String> output_elements = new ArrayList<String>();

        output_elements.add("// This file is generated automatically. All manually changes will be removed when running the Converter.\n");
        for (String folder : folders) {
            if (folder == null) continue;
            output_elements.add("import: " + folder + "/_import.txt");
        }

        Collections.sort(output_elements);
        for (String s : output_elements)
            out.println(s);

        out.flush();
        out.close();
    }
}