summaryrefslogtreecommitdiff
path: root/hercules/code/clienttoserver/maps.py
blob: 995aca17f9a85a5011f2402474fbb69c03cd6e04 (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
# -*- coding: utf8 -*-
#
# Copyright (C) 2014  Evol Online
# Author: Andrei Karas (4144)

import array
import csv
import os
import zlib
import struct
import StringIO
from xml.dom import minidom

from code.fileutils import *

def getTmxFiles(srcDir):
    for name in os.listdir(srcDir):
        fileName = srcDir + name
        if os.path.isfile(fileName) == False:
            continue
        if name.endswith(".tmx") == False:
            continue
        yield fileName

def recreateMapCache():
    destDir = "../../server-data/db/re/"
    srcDir = "../../client-data/maps/"
    makeDir(destDir)
    sz = 0L
    mapsCount = 0
    with open(destDir + "map_cache.dat", "wb") as w:
        writeInt32(w, 0)  # file size
        writeInt16(w, 0)  # maps count
        writeInt16(w, 0)  # padding
        sz = sz + 8
        for fileName in getTmxFiles(srcDir):
            dom = minidom.parse(fileName)
            root = dom.documentElement
            firstgid = 0
            for tileset in root.getElementsByTagName("tileset"):
                try:
                    name = tileset.attributes["name"].value
                except:
                    name = ""
                if name == "Collision":
                    firstgid = int(tileset.attributes["firstgid"].value)
                    break

            for layer in root.getElementsByTagName("layer"):
                if layer.attributes["name"].value == "Collision":
                    data = layer.getElementsByTagName("data")
                    if data is None or len(data) != 1:
                        continue
                    data = data[0]
                    width = int(layer.attributes["width"].value)
                    height = int(layer.attributes["height"].value)
                    encoding = data.attributes["encoding"].value
                    try:
                        compression = data.attributes["compression"].value
                    except:
                        compression = ""

                    tiles = []
                    if encoding == "base64":
                        binData = data.childNodes[0].data.strip()
                        binData = binData.decode('base64')
                        if compression == "gzip":
                            dc = zlib.decompressobj(16 + zlib.MAX_WBITS)
                        else:
                            dc = zlib.decompressobj()
                        layerData = dc.decompress(binData)
                        arr = array.array("I")
                        arr.fromstring(layerData)
                        for tile in arr:
                            if tile == 0:
                                tileType = 0
                            else:
                                tileType = tile - firstgid;
                            if tileType == 0 or tileType == 4:
                                tiles.append(0)
                            else:
                                tiles.append(1)
                    elif encoding == "csv":
                        binData = data.childNodes[0].data.strip()
                        f = StringIO.StringIO(binData)
                        arr = list(csv.reader(f, delimiter=',', quotechar='|'))
                        for row in arr:
                            try:
                                for item in row:
                                    if item != "":
                                        tile = int(item)
                                        if tile == 0:
                                            tileType = 0
                                        else:
                                            tileType = tile - firstgid;
                                        if tileType == 0 or tileType == 4:
                                            tiles.append(0)
                                        else:
                                            tiles.append(1)
                            except:
                                None
                        f.close()
                    else:
                        print "map format not supported: " + fileName
                        continue

                    comp = zlib.compressobj()
                    binData = struct.pack(str(len(tiles))+"B", *tiles)
                    binData = zlib.compress(binData)
                    idx = fileName.rfind("/") + 1
                    mapName = fileName[idx:-4]
                    writeMapName(w, mapName)
                    writeInt16(w, width)
                    writeInt16(w, height)
                    writeInt32(w, len(binData))
                    writeData(w, binData)
                    print fileName
                    mapsCount = mapsCount + 1
                    sz = sz + 12 + 8 + len(binData)
                    break
        w.seek(0);
        writeInt32(w, sz)
        writeInt16(w, mapsCount)