summaryrefslogtreecommitdiff
path: root/tmx_converter.py
blob: 24506c31646c03b065249c16511b8166094de5ff (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#!/usr/bin/env python
# -*- encoding: utf-8 -*-

##    tmx_converter.py - Extract walkmap, warp, and spawn information from maps.
##
##    Copyright © 2012 Ben Longbons <b.r.longbons@gmail.com>
##
##    This file is part of The Mana World
##
##    This program is free software: you can redistribute it and/or modify
##    it under the terms of the GNU General Public License as published by
##    the Free Software Foundation, either version 2 of the License, or
##    (at your option) any later version.
##
##    This program is distributed in the hope that it will be useful,
##    but WITHOUT ANY WARRANTY; without even the implied warranty of
##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##    GNU General Public License for more details.
##
##    You should have received a copy of the GNU General Public License
##    along with this program.  If not, see <http://www.gnu.org/licenses/>.


from __future__ import print_function

import sys
import os
import posixpath
import struct
import xml.sax
import base64
import zlib

dump_all = False # wall of text
check_mobs = True # mob_db.txt
create_switches_files = False # creates .switches.txt with template npcs for objects of the class switch (only needed for devs when creating quests)

# lower case versions of everything except 'spawn' and 'warp'
other_object_types = set([
    'particle_effect',
    'npc', # not interpreted by client
    'script', # for ManaServ
    'fixme', # flag for things that didn't have a type before
    'music',
])

# Somebody has put ManaServ fields in our data!
other_spawn_fields = (
    'spawn_rate',
)
other_warp_fields = (
)

TILESIZE = 32
SEPARATOR = '|'
MESSAGE = 'This file is generated automatically. All manually added changes will be removed when running the Converter.'
CLIENT_MAPS = 'maps'
SERVER_WLK = 'data'
SERVER_NPCS = 'npc'
TMWA_MAP_CONF = 'conf/tmwa-map.conf'
NPC_MOBS = '_mobs.txt'
NPC_NODES = '_nodes.txt'
NPC_SWITCHES = '.switches.txt'
NPC_WARPS = '_warps.txt'
NPC_IMPORTS = '_import.txt'
NPC_MASTER_IMPORTS = NPC_IMPORTS

this_map_npc_dir = ''

class State(object):
    pass
State.INITIAL = State()
State.LAYER = State()
State.DATA = State()
State.FINAL = State()

class Object(object):
    __slots__ = (
        'name',
        #'map',
        'x', 'y',
        'w', 'h',
        'ignore',
    )

class Mob(Object):
    __slots__ = (
        'monster_id',
        'max_beings',
        'spawn',
        'death',
        'ea_spawn',
        'ea_death',
        'npc',
    ) + other_spawn_fields
    def __init__(self):
        self.max_beings = 1
        self.spawn = 0
        self.death = 0
        self.ea_spawn = 0
        self.ea_death = 0

class Node(Object):
    __slots__ = (
        'subtype'
    )
    def __init__(self):
        self.subtype = 0

class Switch(Object):
    __slots__ = (
        'subtype'
    )
    def __init__(self):
        self.subtype = 0

class Warp(Object):
    __slots__ = (
        'dest_map',
        'dest_x',
        'dest_y',
        'dest_tile_x',
        'dest_tile_y',
    ) + other_warp_fields

class ContentHandler(xml.sax.ContentHandler):
    global this_map_npc_dir
    __slots__ = (
        'locator',  # keeps track of location in document
        'out',      # open file handle to .wlk
        'state',    # state of collision info
        'tilesets', # first gid of each tileset
        'buffer',   # characters within a section
        'encoding', # encoding of layer data
        'compression', # compression of layer data
        'width',    # width of the collision layer
        'height',   # height of the collision layer
        'base',     # base name of current map
        'npc_dir',  # world/map/npc/<base>
        'mobs',     # open file to _mobs.txt
        'nodes',     # open file to _nodes.txt
        'warps',    # open file to _warps.txt
        'imports',  # open file to _import.txt
        'name',     # name property of the current map
        'object',   # stores properties of the latest <object> tag
        'mob_ids',  # set of all mob types that spawn here
        'node_types', # set of all node types that appear here
        'node_objs', # set of all node objects that appear here
        'switch_objs', # set of all switch objects that appear here
    )
    def __init__(self, out, npc_dir, mobs, warps, imports, nodes):
        xml.sax.ContentHandler.__init__(self)
        self.locator = None
        self.out = open(out, 'w')
        self.state = State.INITIAL
        self.tilesets = set([0]) # consider the null tile as its own tileset
        self.buffer = bytearray()
        self.encoding = None
        self.compression = None
        self.width = None
        self.height = None
        self.base = posixpath.basename(npc_dir)
        self.npc_dir = npc_dir
        self.mobs = mobs
        self.nodes = nodes
        self.warps = warps
        self.imports = imports
        self.object = None
        self.mob_ids = set()
        self.node_types = set()
        self.node_objs = set()
        self.switch_objs = set()

    def setDocumentLocator(self, loc):
        self.locator = loc

    # this method randomly cuts in the middle of a line; thus funky logic
    def characters(self, s):
        if not s.strip():
            return
        if self.state is State.DATA:
            self.buffer += s.encode('ascii')

    def startDocument(self):
        pass

    def startElement(self, name, attr):
        if dump_all:
            attrs = ' '.join('%s="%s"' % (k,v) for k,v in attr.items())
            if attrs:
                print('<%s %s>' % (name, attrs))
            else:
                print('<%s>' % name)

        if self.state is State.INITIAL:
            if name == u'property' and attr[u'name'].lower() == u'name':
                self.name = attr[u'value']
                self.mobs.write('// %s\n' % MESSAGE)
                self.mobs.write('// %s mobs\n\n' % self.name)
                self.warps.write('// %s\n' % MESSAGE)
                self.warps.write('// %s warps\n\n' % self.name)
                self.nodes.write('// %s\n' % MESSAGE)
                self.nodes.write('// %s nodes\n\n' % self.name)

            if name == u'tileset':
                self.tilesets.add(int(attr[u'firstgid']))

            if name == u'layer' and attr[u'name'].lower().startswith(u'collision'):
                self.width = int(attr[u'width'])
                self.height = int(attr[u'height'])
                self.out.write(struct.pack('<HH', self.width, self.height))
                self.state = State.LAYER
        elif self.state is State.LAYER:
            if name == u'data':
                if attr.get(u'encoding','') not in (u'', u'csv', u'base64', u'xml'):
                    print('Bad encoding:', attr.get(u'encoding',''))
                    return
                self.encoding = attr.get(u'encoding','')
                if attr.get(u'compression','') not in (u'', u'none', u'zlib', u'gzip'):
                    print('Bad compression:', attr.get(u'compression',''))
                    return
                self.compression = attr.get(u'compression','')
                self.state = State.DATA
        elif self.state is State.DATA:
            self.out.write(chr(int(attr.get(u'gid',0)) not in self.tilesets))
        elif self.state is State.FINAL:
            if name == u'object':
                try:
                    obj_type = attr[u'type'].lower()
                except KeyError:
                    obj_type = attr[u'class'].lower()
                x = int(attr[u'x']) / TILESIZE;
                y = int(attr[u'y']) / TILESIZE;
                w = int(attr.get(u'width', 0)) / TILESIZE;
                h = int(attr.get(u'height', 0)) / TILESIZE;
                # I'm not sure exactly what the w/h shrinking is for,
                # I just copied it out of the old converter.
                # I know that the x += w/2 is to get centers, though.
                if obj_type == 'spawn':
                    self.object = Mob()
                    if w > 1:
                        w -= 1
                    if h > 1:
                        h -= 1
                    x += w/2
                    y += h/2
                elif obj_type == 'warp':
                    self.object = Warp()
                    x += w/2
                    y += h/2
                    w -= 2
                    h -= 2
                elif obj_type == 'node':
                    self.object = Node()
                    w += x - 1
                    h += y - 1
                    if w == x and h == y:
                        w = 0
                        h = 0
                elif obj_type == 'switch':
                    self.object = Switch()
                else:
                    if obj_type not in other_object_types:
                        print('Unknown object type:', obj_type, file=sys.stderr)
                    self.object = None
                    return
                obj = self.object
                obj.x = x
                obj.y = y
                obj.w = w
                obj.h = h
                obj.name = attr[u'name']
            elif name == u'property':
                obj = self.object
                if obj is None:
                    return
                key = attr[u'name'].lower()
                value = attr[u'value']
                # Not true due to defaulting
                #assert not hasattr(obj, key)
                try:
                    value = int(value)
                except ValueError:
                    pass
                setattr(obj, key, value)

    def add_warp_line(self, line):
        self.warps.write(line)

    def endElement(self, name):
        if dump_all:
            print('</%s>' % name)

        if name == u'object':
            if hasattr(self.object, 'ignore'):
                return;
            obj = self.object

            if isinstance(obj, Mob):
                mob_id = obj.monster_id
                if mob_id < 1000:
                    mob_id += 1002
                if check_mobs:
                    try:
                        name = mob_names[mob_id]
                    except KeyError:
                        print('Warning: unknown mob ID: %d (%s)' % (mob_id, obj.name))
                    else:
                        if name != obj.name:
                            print('Warning: wrong mob name: %s (!= %s)' % (obj.name, name))
                            obj.name = name
                if hasattr(obj, 'spawn'):
                    spawn = obj.spawn
                else:
                    spawn = obj.ea_spawn
                if hasattr(obj, 'death'):
                    death = obj.death
                else:
                    death = obj.ea_death
                self.mob_ids.add(mob_id)
                self.mobs.write(
                    SEPARATOR.join([
                        '%s,%d,%d,%d,%d' % (self.base, obj.x, obj.y, obj.w, obj.h),
                        'monster',
                        obj.name,
                        '%d,%d,%dms,%dms' % (mob_id, obj.max_beings, spawn, death),
                    ])
                )
                if hasattr(obj, 'npc'):
                    self.mobs.write(',%s\n' % obj.npc)
                else:
                    self.mobs.write('\n')

            elif isinstance(obj, Warp):
                if hasattr(obj, 'dest_x'):
                    dest_x = obj.dest_x
                else:
                    dest_x = obj.dest_tile_x
                if hasattr(obj, 'dest_y'):
                    dest_y = obj.dest_y
                else:
                    dest_y = obj.dest_tile_y
                self.warps.write(
                    SEPARATOR.join([
                        '%s,%d,%d' % (self.base, obj.x, obj.y),
                        'warp',
                        '%d,%d,%s,%d,%d\n' % (obj.w, obj.h, obj.dest_map, dest_x, dest_y),
                    ])
                )

            elif isinstance(obj, Switch):
                self.switch_objs.add(obj)

            elif isinstance(obj, Node):
                self.node_types.add(obj.name)
                self.node_objs.add(obj)

        if name == u'data':
            if self.state is State.DATA:
                if self.encoding == u'csv':
                    for x in self.buffer.split(','):
                        self.out.write(chr(int(x) not in self.tilesets))
                elif self.encoding == u'base64':
                    data = base64.b64decode(str(self.buffer))
                    if self.compression == u'zlib':
                        data = zlib.decompress(data)
                    elif self.compression == u'gzip':
                        data = zlib.decompressobj().decompress('x\x9c' + data[10:-8])
                    for i in range(self.width*self.height):
                        self.out.write(chr(int(struct.unpack('<I',data[i*4:i*4+4])[0]) not in self.tilesets))
                self.state = State.FINAL

    def endDocument(self):
        if len(self.node_types) > 0:
            self.nodes.write('%s,0,0,0|script|Node%s|32767\n{\n    end;\nOnInit:\n' % (self.base, self.base))
            for ntype in sorted(self.node_types):
                self.nodes.write('    setarray .m$, "_N-%s"' % (ntype))
                for nm in self.node_objs:
                    self.nodes.write(', "%s"' % (self.base))
                self.nodes.write(';\n    setarray .x1, "_N-%s"' % (ntype))
                for nx in self.node_objs:
                    self.nodes.write(', %d' % (nx.x))
                self.nodes.write(';\n    setarray .y1, "_N-%s"' % (ntype))
                for ny in self.node_objs:
                    self.nodes.write(', %d' % (ny.y))
                self.nodes.write(';\n    setarray .x2, "_N-%s"' % (ntype))
                for nw in self.node_objs:
                    self.nodes.write(', %d' % (nw.w))
                self.nodes.write(';\n    setarray .y2, "_N-%s"' % (ntype))
                for nh in self.node_objs:
                    self.nodes.write(', %d' % (nh.h))
                self.nodes.write(';\n    setarray .id, "_N-%s"' % (ntype))
                for nt in self.node_objs:
                    self.nodes.write(', %d' % (nt.subtype))
                self.nodes.write(';\n')
                # TODO don't make .id, .x2, .y2 when they are empty
                # TODO custom properties
                self.nodes.write('    donpcevent "_N-%s::OnMaybeStart";\n' % (ntype))
            self.nodes.write('    destroy;\n}\n')
        else:
            self.nodes.write('// (no nodes)\n')

        # had to move it here else it writes zero size files not sure if its better to check if a written file has zero size and delete it after
        if create_switches_files:
            if len(self.switch_objs) > 0:
                with open(posixpath.join(this_map_npc_dir, NPC_SWITCHES), 'w') as switches:
                    switches.write('// %s\n' % MESSAGE)
                    switches.write('// %s switches\n\n' % self.name)
                    for sobjs in sorted(self.switch_objs):
                        obj_name = "#%s_%s_%s" % (self.base, sobjs.x, sobjs.y)
                        switches.write(
                            SEPARATOR.join([
                                '%s,%d,%d,0|script|%s|422\n{\n// REPLACE ME\n}\n' % (self.base, sobjs.x, sobjs.y, obj_name),
                            ])
                        )

        self.imports.write('// Map %s: %s\n' % (self.base, self.name))
        self.imports.write('// %s\n' % MESSAGE)
        self.imports.write('map: %s\n' % self.base)

        npcs = os.listdir(self.npc_dir)
        npcs.sort()
        for x in npcs:
            if x == NPC_IMPORTS:
                continue
            if x.startswith('.'):
                continue
            if x.endswith('.txt'):
                self.imports.write('npc: %s\n' % posixpath.join(SERVER_NPCS, self.base, x))
        pass

def main(argv):
    global this_map_npc_dir
    _, client_data, server_data = argv
    tmx_dir = posixpath.join(client_data, CLIENT_MAPS)
    wlk_dir = posixpath.join(server_data, SERVER_WLK)
    npc_dir = posixpath.join(server_data, SERVER_NPCS)
    if check_mobs:
        global mob_names
        mob_names = {}
        with open(posixpath.join(server_data, TMWA_MAP_CONF)) as mob_dbs:
            for mob_db_line in mob_dbs:
                if mob_db_line.startswith('mob_db:'):
                    with open(posixpath.join(server_data, mob_db_line.split(':')[1].strip())) as mob_db:
                        for line in mob_db:
                            if not line.strip():
                                continue
                            if line.startswith('//'):
                                continue
                            k, v, _ = line.split(',', 2)
                            mob_names[int(k)] = v.strip()

    npc_master = []
    map_basenames = []

    for arg in os.listdir(tmx_dir):
        base, ext = posixpath.splitext(arg)

        if ext == '.tmx':
            map_basenames.append(base)
            tmx = posixpath.join(tmx_dir, arg)
            wlk = posixpath.join(wlk_dir, base + '.wlk')
            this_map_npc_dir = posixpath.join(npc_dir, base)
            os.path.isdir(this_map_npc_dir) or os.mkdir(this_map_npc_dir)
            print('Converting %s to %s' % (tmx, wlk))
            with open(posixpath.join(this_map_npc_dir, NPC_MOBS), 'w') as mobs:
                with open(posixpath.join(this_map_npc_dir, NPC_WARPS), 'w') as warps:
                    with open(posixpath.join(this_map_npc_dir, NPC_IMPORTS), 'w') as imports:
                        with open(posixpath.join(this_map_npc_dir, NPC_NODES), 'w') as nodes:
                            xml.sax.parse(tmx, ContentHandler(wlk, this_map_npc_dir, mobs, warps, imports, nodes))
            npc_master.append('import: %s\n' % posixpath.join(SERVER_NPCS, base, NPC_IMPORTS))

    with open(posixpath.join(wlk_dir, 'resnametable.txt'), 'w') as resname:
        for base in sorted(map_basenames):
            resname.write('%s#%s.wlk#\n' % (base, base))
    with open(posixpath.join(npc_dir, NPC_MASTER_IMPORTS), 'w') as out:
        out.write('// %s\n\n' % MESSAGE)
        npc_master.sort()
        for line in npc_master:
            out.write(line)

if __name__ == '__main__':
    main(sys.argv)