summaryrefslogtreecommitdiff
path: root/hercules/code/servertoclient/sprites.py
blob: 5dad9d1f1064a863242ae61a27a5b2f00ebe85d1 (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
# -*- coding: utf8 -*-
#
# Copyright (C) 2015  Evol Online
# Author: Andrei Karas (4144)

import array
import os
from PIL import Image

from code.fileutils import readInt8, readInt16, readInt32, makeDir, readFile, saveFile, skipData, readData, findFileIn

class ActClass:
    pass

class ActAnimationClass:
    pass

class ActSpriteClass:
    pass

class ActFrameClass:
    pass

class ActPivotClass:
    pass

class ActEventClass:
    pass

class SprClass:
    pass

class SprImageClass:
    pass

def readIndexedRLEImage(f, spr):
    spr.nimages = spr.indexedSpritesCount
    spr.images = dict()
    for imageN in range(0, spr.indexedSpritesCount):
        image = SprImageClass()
        spr.images[imageN] = image
        image.width = readInt16(f)
        image.height = readInt16(f)
        image.compressed = True
        data = array.array('B', (0 for _ in xrange(image.width * image.height)))
        image.data = data

        compressSize = readInt16(f)
        #uncompressedSize = image.width * image.height
        #print "uncompressed size=" + str(uncompressedSize)
        #print "compressed size=" + str(compressSize)

        idx = 0
        readCnt = 0
        while readCnt < compressSize:
            c = readInt8(f)
            readCnt = readCnt + 1
            data[idx] = c;
            #print "{0:4}: {1}".format(idx, c)
            idx = idx + 1
            if c == 0:
                cnt = readInt8(f)
                readCnt = readCnt + 1
                if cnt == 0:
                    data[idx] = cnt
                    #print "{0:4}: {1}".format(idx, cnt)
                    idx = idx + 1
                else:
                    for j in range(1, cnt):
                        data[idx] = c
                        #print "{0:4} ({1} to {2}): {3}".format(idx, j, cnt - 1, c)
                        idx = idx + 1
        #print "read bytes: " + str(readCnt)

def readIndexedImage(f, spr):
    pass

def readRgbaImage(f, spr):
    for imageN in range(0, spr.rgbaSpritesCount):
        image = SprImageClass()
        spr.images[imageN + spr.indexedSpritesCount] = image
        image.width = readInt16(f)
        image.height = readInt16(f)
        data = array.array('I', (0 for _ in xrange(image.width * image.height)))
        image.compressed = False
        image.data = data
        for idx in range(0, image.width * image.height):
            data[idx] = readInt32(f)


def readPalette(f, spr):
    palette = array.array('I', (0 for _ in xrange(256)))
    spr.palette = palette
    for col in range(0, 256):
        palette[col] = readInt32(f)

def getSignedNumber(number, bitLength):
    mask = (2 ** bitLength) - 1
    if number & (1 << (bitLength - 1)):
        return number | ~mask
    else:
        return number & mask

def decodeSprite(spr):
    palette = spr.palette
    spr.maxwidth = 0
    spr.maxheight = 0
    alpha = False
    for imageN in range(0, spr.indexedSpritesCount):
        image = spr.images[imageN]
        indexed = image.data
        data = array.array('I', (0 for _ in xrange(image.width * image.height)))
        for idx in range(0, image.width * image.height):
            col = indexed[idx]
            if col == 0:
                data[idx] = 0
            else:
                if palette[col] > 0x00ffffff:
                    data[idx] = palette[col]
                    alpha = True
                else:
                    data[idx] = palette[col] + 0xff000000
        image.data = data
        if image.width > spr.maxwidth:
            spr.maxwidth = image.width
        if image.height > spr.maxheight:
            spr.maxheight = image.height

        # for debug
#        png = Image.new('RGBA', (image.width, image.height))
#        png.putdata(image.data)
#        png.save("test{0}.png".format(imageN))

    if alpha:
        print "detected alpha"

    for imageN in range(0, spr.rgbaSpritesCount):
        image = spr.images[imageN + spr.indexedSpritesCount]
        if image.width > spr.maxwidth:
            spr.maxwidth = image.width
        if image.height > spr.maxheight:
            spr.maxheight = image.height



def saveSpriteImage(act, spr, spriteDir, spriteName):
#    for imageN in range(0, spr.rgbaSpritesCount):
#        image = spr.images[imageN + spr.indexedSpritesCount]
#        print "{0} x {1}".format(image.width, image.height)
#        png = Image.new('RGBA', (image.width, image.height))
#        png.putdata(image.data)
#        png.save("test{0}.png".format(imageN + spr.indexedSpritesCount))

#    numTiles = spr.indexedSpritesCount + spr.rgbaSpritesCount

    counted = 0
    for animN in range(0, act.nanimations):
        anim = act.animations[animN]
        for spriteN in range(0, anim.nsprites):
            sprite = anim.sprites[spriteN]
#            key = []
#            for frameN in range(0, sprite.nframes):
#                frame = sprite.frames[frameN]
#                idf = frame.frameIndex
#                if frame.mirror > 0:
#                    idf = - idf
#                key.append(idf)
            counted = counted + 1

    numTiles = counted
    #print "max: {0}x{1}".format(spr.maxwidth, spr.maxheight)

    # in row
    rowTiles = int(2048 / spr.maxwidth)
    colTiles = int(numTiles / rowTiles)
    if colTiles * rowTiles < numTiles:
        colTiles = colTiles + 1
    tilesetWidth = 2048
    tilesetHeight = colTiles * spr.maxheight
#    print "num {0} row {1}, col {2}".format(numTiles, rowTiles, colTiles)
#    print "size {0}x{1}".format(2048, colTiles * spr.maxheight)

#    tileset = array.array('I', (0 for _ in xrange(tilesetWidth * tilesetHeight)))
    tileset = Image.new('RGBA', (tilesetWidth, tilesetHeight))
#    png.putdata(tileset)
    #draw = ImageDraw.Draw(png)

    x = 0
    y = 0

    frameToIdx = dict()

    tile = 0
    for animN in range(0, act.nanimations):
        anim = act.animations[animN]
        for spriteN in range(0, anim.nsprites):
            sprite = anim.sprites[spriteN]
            frameToIdx[str(animN) + "_" + str(spriteN)] = tile
            for frameN in range(0, sprite.nframes):
                frame = sprite.frames[frameN]
                frm = frame.frameIndex
                if frame.mirror > 0:
                    frm = - frm

                if frm in frameToIdx:
                    continue

                if frame.frameIndex not in spr.images:
                    print "wrong frame index: {0}".format(frame.frameIndex)
                    continue
                image = spr.images[frame.frameIndex]
                png = Image.new('RGBA', (image.width, image.height))
                png.putdata(image.data)
                if frame.mirror > 0:
                    png = png.transpose(Image.FLIP_LEFT_RIGHT)
                offsetX = (spr.maxwidth - image.width) / 2
                offsetY = spr.maxheight - image.height
#                offsetX = (spr.maxwidth - image.width) / 2 + getSignedNumber(frame.offsetX, 32)
#                offsetY = spr.maxheight - image.height + getSignedNumber(frame.offsetY, 32)
                tileset.paste(png, (x + offsetX, y + offsetY))
            tile = tile + 1
            x = x + spr.maxwidth
            if x + spr.maxwidth > 2048:
                x = 0
                y = y + spr.maxheight

#    for imageN in range(0, spr.rgbaSpritesCount + spr.indexedSpritesCount):
#        image = spr.images[imageN]
#        png = Image.new('RGBA', (image.width, image.height))
#        png.putdata(image.data)
#        tileset.paste(png, (x, y))
#        x = x + spr.maxwidth
#        if x + spr.maxwidth > 2048:
#            x = 0
#            y = y + spr.maxheight

    spr.frameToIdx = frameToIdx
    spr.tilesetWidth = tilesetWidth
    spr.tilesetHeight = tilesetHeight
    makeDir(spriteDir)
    tileset.save(spriteDir + spriteName + ".png")

def extractSpriteAnimData(act, spr, actIndex, direction):
#    delay = anim.delay
    delay = 100
    data = "        <animation direction=\"" + direction + "\">\n"
    if actIndex not in act.animations:
        data = data + "            <frame index=\"{0}\" delay=\"{1}\" offsetX=\"{2}\" offsetY=\"{3}\"/>\n".format(
            0, delay, 0, 0)
        data = data + "        </animation>\n"
        return data

    anim = act.animations[actIndex]
    for spriteN in range(0, anim.nsprites):
        #sprite = anim.sprites[spriteN]
        #for frameN in range(0, sprite.nframes):
            #frame = sprite.frames[frameN]
            #frm = frame.frameIndex
            #if frame.mirror > 0:
            #    frm = -frm
            #if frm not in spr.frameToIdx:
            #    continue

        idx = spr.frameToIdx[str(actIndex) + "_" + str(spriteN)]
        offsetX = 0
        offsetY = 0
#        offsetX = frame.offsetX
#        offsetY = frame.offsetY
#        if offsetX > 4294900000:
#            offsetX = - (4294967296 - offsetX)
#        if offsetY > 4294000000:
#            offsetY = -(4294967296 - offsetY)

        data = data + "            <frame index=\"{0}\" delay=\"{1}\" offsetX=\"{2}\" offsetY=\"{3}\"/>\n".format(
            idx, delay, offsetX, offsetY)

    data = data + "        </animation>\n"
    return data

def extractSpriteDataAll(act, spr, actIndex, name):
    data = extractSpriteAnimData(act, spr, actIndex, "down")
    data = data + extractSpriteAnimData(act, spr, actIndex + 1, "downleft")
    data = data + extractSpriteAnimData(act, spr, actIndex + 2, "left")
    data = data + extractSpriteAnimData(act, spr, actIndex + 3, "upleft")
    data = data + extractSpriteAnimData(act, spr, actIndex + 4, "up")
    data = data + extractSpriteAnimData(act, spr, actIndex + 5, "upright")
    data = data + extractSpriteAnimData(act, spr, actIndex + 6, "right")
    data = data + extractSpriteAnimData(act, spr, actIndex + 6, "downright")
    return data

def saveSpriteXml(act, spr, spriteDir, spriteName):
    templatesDir = "templates/"
    dstFile = spriteDir + spriteName + ".xml"
    tpl = readFile(templatesDir + "sprite.xml")
    # 0, 8, 16, 24, 32, 40, 48
    # 0 - walk or attack or ?
    # 8 - walk
    # 16 - attack
    # 24 - dead
    # 32 - dead2 ?
    # no more
    standData = extractSpriteDataAll(act, spr, 0, "stand")
    walkData = extractSpriteDataAll(act, spr, 8, "walk")
    attackData = extractSpriteDataAll(act, spr, 16, "attack")
    deadData = extractSpriteDataAll(act, spr, 32, "dead")

    data = tpl.format(
        src = "graphics/sprites/sprites/" + spriteName + ".png",
        width = spr.maxwidth,
        height = spr.maxheight,
        stand = standData,
        walk = walkData,
        attack = attackData,
        dead = deadData
    )
    saveFile(dstFile, data)


def readAct(actFile):
    act = ActClass()
    with open(actFile, "r") as f:
        act.header = readInt16(f)
        if act.header != 17217:
            #print "Wrong header in file {0}".format(actFile)
            return None
        act.minorVersion = readInt8(f)
        act.majorVersion = readInt8(f)
        act.nanimations = readInt16(f)
        print "{0}, {1}.{2}, {3}".format(actFile, act.majorVersion, act.minorVersion, act.nanimations)
        #print " animations: " + str(act.nanimations)
        act.animations = dict()
        skipData(f, 10)
        for animN in range(0, act.nanimations):
            anim = ActAnimationClass()
            anim.delay = 30
            act.animations[animN] = anim
            anim.nsprites = readInt32(f)
            #print "  sprites: " + str(anim.nsprites)
            anim.sprites = dict()
            for spriteN in range(0, anim.nsprites):
                sprite = ActSpriteClass()
                anim.sprites[spriteN] = sprite
                sprite.left1 = readInt32(f)
                sprite.top1 = readInt32(f)
                sprite.right1 = readInt32(f)
                sprite.buttom1 = readInt32(f)
                sprite.left2 = readInt32(f)
                sprite.top2 = readInt32(f)
                sprite.right2 = readInt32(f)
                sprite.buttom2 = readInt32(f)
                sprite.nframes = readInt32(f)
                #print "sprite {0}, frames: {1}".format(spriteN, sprite.nframes)
                sprite.frames = dict()
                for frameN in range(0, sprite.nframes):
                    frame = ActFrameClass()
                    sprite.frames[frameN] = frame
                    frame.offsetX = readInt32(f)
                    frame.offsetY = readInt32(f)
                    frame.frameIndex = readInt32(f)
                    #print "   index: " + str(frame.frameIndex)
                    frame.mirror = readInt32(f)
                    if act.majorVersion >= 2:
                        frame.color = readInt32(f)
                        frame.scaleX = readInt32(f)
                        if act.majorVersion > 2 or (act.majorVersion == 2 and act.minorVersion >= 4):
                            frame.scaleY = readInt32(f)
                        else:
                            frame.scaleY = frame.scaleX
                        frame.angle = readInt32(f)
                        frame.spriteType = readInt32(f)
                        if act.majorVersion > 2 or (act.majorVersion == 2 and act.minorVersion >= 5):
                            frame.width = readInt32(f)
                            frame.height = readInt32(f)
                            #print "{0} x {1}".format(frame.width, frame.height)
                if act.majorVersion >= 2:
                    sprite.eventIndex = readInt32(f)
                    if act.majorVersion > 2 or (act.majorVersion == 2 and act.minorVersion >= 3):
                        sprite.npivots = readInt32(f)
                        sprite.pivots = dict()
                        #if sprite.npivots > 0:
                            #print "    pivotes: " + str(sprite.npivots)
                        for pivotN in range(0, sprite.npivots):
                            pivot = ActPivotClass()
                            sprite.pivots[pivotN] = pivot
                            pivot.unknown = readInt32(f)
                            pivot.centerX = readInt32(f)
                            pivot.centerY = readInt32(f)
                            pivot.nAttribute = readInt32(f)
        if act.majorVersion > 2 or (act.majorVersion == 2 and act.minorVersion >= 1):
            act.nevents = readInt32(f)
            act.events = dict()
            for eventN in range(0, act.nevents):
                event = ActEventClass()
                act.events[eventN] = event;
                event.name = readData(f, 40)
        if act.majorVersion > 2 or (act.majorVersion == 2 and act.minorVersion >= 2):
            for animN in range(0, act.nanimations):
                anim = act.animations[animN]
                anim.delay = readInt32(f)
    return act

def readSpr(sprFile):
    spr = SprClass()
    with open(sprFile, "r") as f:
        spr.header1 = readInt8(f)
        spr.header2 = readInt8(f)
        if spr.header1 != 0x53 or spr.header2 != 0x50:
            return None
        spr.minorVersion = readInt8(f)
        spr.majorVersion = readInt8(f)
        spr.indexedSpritesCount = readInt16(f)
        if spr.majorVersion > 1 or (spr.majorVersion == 1 and spr.minorVersion >= 1):
            spr.rgbaSpritesCount = readInt16(f)
        else:
            spr.rgbaSpritesCount = 0
        print "{0}, {1}.{2}, {3}, {4}".format(sprFile, spr.majorVersion, spr.minorVersion, spr.indexedSpritesCount, spr.rgbaSpritesCount)
        spr.frames = spr.indexedSpritesCount + spr.rgbaSpritesCount

        if spr.majorVersion > 2 or (spr.majorVersion == 2 and spr.minorVersion >= 1):
            readIndexedRLEImage(f, spr)
        else:
            readIndexedImage(f, spr)
        readRgbaImage(f, spr)

        if spr.majorVersion > 1 or (spr.majorVersion == 1 and spr.minorVersion >= 1):
            readPalette(f, spr)

    return spr

def convertSprite(spritePath, spriteName):
    actFile = "{0}.act".format(spritePath + spriteName)
    sprFile = "{0}.spr".format(spritePath + spriteName)
    if os.path.exists(actFile) == False or os.path.exists(sprFile) == False:
        return None
    act = readAct(actFile)
    spr = readSpr(sprFile)
    decodeSprite(spr)
    saveSpriteImage(act, spr, "clientdata/graphics/sprites/sprites/", spriteName)
    saveSpriteXml(act, spr, "clientdata/graphics/sprites/sprites/", spriteName)
#    if actFile.find("wolf") > 0:
#        exit(0)
#    exit(0)

def findSpritePath(spriteName):
    testName = spriteName + ".act"
    testName2 = spriteName.upper() + ".act"
    path = findFileIn((testName, testName2),
        ("rodata/data/sprite/ёуЅєЕН/",
        "rodata/data/sprite/npc/",
        "rodata/data/sprite/homun/"))
    return path

def convertSpritesNonFree(idtofile):
    processed = []
    for spriteid in idtofile:
        spriteName = idtofile[spriteid]
        if spriteName in processed:
            print "skipping " + spriteName
            continue

        path = findSpritePath(spriteName)
        if path is None:
            print "not found " + spriteName
            continue
        print spriteName
        convertSprite(path, spriteName)
        processed.append(spriteName)