-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpxMap.py
376 lines (294 loc) · 8.75 KB
/
pxMap.py
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
import io, os, struct
import mmap
from ctypes import c_short
def writePixelString(fp, string):
length = len(string.encode("shift-jis"))
fp.write(struct.pack("<B", length))
fp.write(bytes(string.encode("shift-jis")))
def readPixelString(stream):
length = stream.read_byte()
if length >= 32: return ""
string = stream.read(length)
return string.decode("shift-jis")
def readInt(stream, length):
return int.from_bytes(stream.read(length), byteorder="little")
pxmapMagic = "pxMAP01\0"
class PxPackUnit:
def __init__(self, bits, code_char, param2, x, y, flag, string, id):
self.bits = bits
self.type1 = code_char
self.param2 = param2
self.x = x
self.y = y
self.flag = flag
self.string = string
self.id = id
class PxEve:
def __init__(self):
self.units = []
self._count = 0
def replace(self, ents):
for ent in ents:
for o in self.units:
if ent.id == o.id:
self.units[self.units.index(o)] = ent
def move(self, ids, xoffset, yoffset):
for num in ids:
for o in self.units:
if num == o.id:
o.x += xoffset
o.y += yoffset
def modify(self, ids, x=None, y=None, code_char=None, bits=None, param2=None, flag=None, string=None):
#TODO: is this really neccesary?
for num in ids:
for o in self.units:
if num == o.id:
if x != None: o.x = x
if y != None: o.y = y
if code_char != None: o.type1 = code_char
if bits != None: o.bits = bits
if param2 != None: o.param2 = param2
if flag != None: o.flag = flag
if string != None: o.string = string
break
def remove(self, ids):
for id in ids:
for o in self.units:
if o.id == id:
self.units.remove(o)
break
def add(self, x, y, code_char, bits=0, param2=0, flag=0, string=""):
o = PxPackUnit(bits|1, code_char, param2, x, y, flag, string, self._count)
self._count += 1
self.units.append(o)
return o
def saveToPack(self, stream):
pass
class PxPackLayer:
def __init__(self):
self.partsName = None
self.scrolltype = 0
self.visibility = 0
#max for an attr is 16*16
self.width = 16
self.height = 16
#TODO: compression
self.type = 0
self.tiles = [[0] * self.width] * self.height
def loadFromPack(self, stream):
self.tiles = []
#TODO: verify header minus numbers
stream.read(8) #PXMAP01
self.width = readInt(stream, 2)
self.height = readInt(stream, 2)
if self.width * self.height == 0: return True
self.type = readInt(stream, 1)
if self.type == 0:
for i in range(self.height):
byt = stream.read(self.width)
self.tiles.append([tile for tile in byt])
return True
def load(self, path, printError=True): #readEntities
try:
f = open(path, 'rb')
stream = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
except (OSError, IOError) as e:
if printError:
print("Error while opening {}: {}".format(path, e))
return False
return self.loadFromPack(stream)
def saveToPack(self, f):
f.write(bytes(pxmapMagic.encode("ascii")))
f.write(struct.pack("<H", self.width))
f.write(struct.pack("<H", self.height))
if self.width * self.height == 0: return
f.write(struct.pack("<B", self.type))
if self.type == 0:
for y in self.tiles:
f.write(bytes(y))
def save(self, path):
try:
f = open(path, 'wb')
except (OSError, IOError) as e:
print("Error while saving {}: {}".format(path, e))
return False
else:
self.saveToPack(f)
def modify(self, tiles):
#[[x,y], [x, y]]
for tile in tiles:
#convert a spritesheet tile index to its representation in the tile array
if tile[0][0] >= self.width: continue
if tile[0][1] >= self.height: continue
x = tile[1][0]
y = tile[1][1] * 16
self.tiles[tile[0][1]][tile[0][0]] = x+y
def resize(self, width, height):
if width <= 0: return
if height <= 0: return
tiles = []
for y in range(height):
if y > self.height-1:
tiles.append([0] * width)
else:
tiles.append( self.tiles[y][:width] + [0]*(width - self.width))
self.tiles = tiles
self.width = width
self.height = height
pxpackMagic = "PXPACK121127a**\0"
class PxPack:
def __init__(self):
self.description = None
self.left_field = None
self.right_field = None
self.up_field = None
self.down_field = None
self.spritesheet = None
self.area_x = None
self.area_y = None
self.area_no = None
self.bg_r = None
self.bg_g = None
self.bg_b = None
self.layers = []
self.eve = PxEve()
def load(self, path): #readEntities
try:
f = open(path, 'rb')
stream = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
except (OSError, IOError) as e:
print("Error while opening {}: {}".format(path, e))
return False
LAYER_COUNT = 3
#TODO: verify header
stream.seek(16)
self.description = readPixelString(stream)
self.left_field = readPixelString(stream)
self.right_field = readPixelString(stream)
self.up_field = readPixelString(stream)
self.down_field = readPixelString(stream)
self.spritesheet = readPixelString(stream)
self.area_x = readInt(stream, 2)
self.area_y = readInt(stream, 2)
self.area_no = readInt(stream, 1)
self.bg_r = readInt(stream, 1)
self.bg_g = readInt(stream, 1)
self.bg_b = readInt(stream, 1)
for i in range(LAYER_COUNT):
layer = PxPackLayer()
layer.partsName = readPixelString(stream)
layer.visibility = readInt(stream, 1)
layer.scrolltype = readInt(stream, 1)
self.layers.append(layer)
for i in range(LAYER_COUNT):
layer = self.layers[i]
layer.loadFromPack(stream)
entityCount = readInt(stream, 2)
for i in range(entityCount):
bits = readInt(stream, 1)
code_char = readInt(stream, 1)
param2 = readInt(stream, 1)
x = readInt(stream, 2)
y = readInt(stream, 2)
flag = readInt(stream, 2)
string = readPixelString(stream)
self.eve.units.append(PxPackUnit(bits,code_char,param2,x,y,flag,string, self.eve._count))
self.eve._count += 1
stream.close()
f.close()
return True
def save(self, path):
try:
f = open(path, 'wb')
except (OSError, IOError) as e:
print("Error while opening {}: {}".format(path, e))
return False
else:
f.write(bytes(pxpackMagic.encode("ascii")))
writePixelString(f, self.description)
writePixelString(f, self.left_field)
writePixelString(f, self.right_field)
writePixelString(f, self.up_field)
writePixelString(f, self.down_field)
writePixelString(f, self.spritesheet)
f.write(struct.pack("<H", self.area_x))
f.write(struct.pack("<H", self.area_y))
f.write(struct.pack("<B", self.area_no))
f.write(struct.pack("<B", self.bg_r))
f.write(struct.pack("<B", self.bg_g))
f.write(struct.pack("<B", self.bg_b))
LAYER_COUNT = 3
for i in range(LAYER_COUNT):
layer = self.layers[i]
writePixelString(f, layer.partsName)
f.write(struct.pack("<B", layer.visibility))
f.write(struct.pack("<B", layer.scrolltype))
for i in range(LAYER_COUNT):
self.layers[i].saveToPack(f)
f.write(struct.pack("<H", len(self.eve.units)))
for o in self.eve.units:
f.write(struct.pack("<B", o.bits))
f.write(struct.pack("<B", o.type1))
f.write(struct.pack("<B", o.param2))
f.write(struct.pack("<h", o.x))
f.write(struct.pack("<h", o.y))
f.write(struct.pack("<H", o.flag))
writePixelString(f, o.string)
f.close()
return True
class PxMapAttr: #use the same class for both
def __init__(self):
self.width = None
self.height = None
self.tiles = []
def load(self, path):
try:
f = open(path, 'rb')
data = f.read()
except (OSError, IOError) as e:
print("Error while opening {}: {}".format(path, e))
return False
self.width = int.from_bytes(data[0:2], byteorder='little')
self.height = int.from_bytes(data[2:4], byteorder='little')
for i in range(self.height):
self.tiles.append(list( data[4+i*self.width:\
4+(i*self.width)+self.width] ))
return True
def save(self, path):
try:
f = open(path, 'wb')
except (OSError, IOError) as e:
print("Error while opening {}: {}".format(path, e))
return False
else:
f.write(struct.pack("<h", self.width))
f.write(struct.pack("<h", self.height))
for y in self.tiles:
f.write(bytes(y))
f.close()
return True
def modify(self, tiles):
#[[x,y], [x, y]]
for tile in tiles:
#convert a spritesheet tile index to its representation in the tile array
if tile[0][0] >= self.width: continue
if tile[0][1] >= self.height: continue
x = tile[1][0]
y = tile[1][1] * 16
self.tiles[tile[0][1]][tile[0][0]] = x+y
def resize(self, width, height):
if width <= 0: return
if height <= 0: return
tiles = []
for y in range(height):
if y > self.height-1:
tiles.append([0] * width)
else:
tiles.append( self.tiles[y][:width] + [0]*(width - self.width))
self.tiles = tiles
self.width = width
self.height = height
def shift(self, x, y, wrap):
pass
def get(self):
return self.tiles[:]