summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/exporter.py80
1 files changed, 64 insertions, 16 deletions
diff --git a/src/exporter.py b/src/exporter.py
index 4026929..f706a38 100644
--- a/src/exporter.py
+++ b/src/exporter.py
@@ -3,6 +3,12 @@ import array
import sys
from ctypes import create_string_buffer
+# Useful Stuff
+u32 = struct.Struct('>I')
+u16 = struct.Struct('>H')
+zero32 = u32.pack(0)
+
+
def RGB5A3Encode(tex):
tex = tex.toImage()
w, h = tex.width(), tex.height()
@@ -54,7 +60,8 @@ class KPMapExporter:
class LayerExporter:
def __init__(self, layer):
self.layer = layer
-
+
+ class TileLayerExporter(LayerExporter):
def buildSectors(self, sectors, indices):
# we'll use the cache held by the layer: why reinvent the wheel?
layer = self.layer
@@ -121,18 +128,45 @@ class KPMapExporter:
self.sectorBounds = (sectorLeft, sectorTop, sectorRight, sectorBottom)
self.sectorMap = sectorMap
+ class DoodadLayerExporter(LayerExporter):
+ pass
+
+ class PathLayerExporter(LayerExporter):
+ pass
+
def __init__(self, mapObj):
self.map = mapObj
- self.layers = map(KPMapExporter.LayerExporter, self.map.layers)
+ self.tileAssociates = {}
+ self.doodadAssociates = {}
- def build(self):
- u32 = struct.Struct('>I')
- u16 = struct.Struct('>H')
- zero32 = u32.pack(0)
+ output = []
+ for layer in self.map.layers:
+ if isinstance(layer, KPTileLayer) and len(layer.objects) > 0:
+ output.append(KPMapExporter.TileLayerExporter(layer))
+
+ elif isinstance(layer, KPDoodadLayer) and len(layer.objects) > 0:
+ output.append(KPMapExporter.DoodadLayerExporter(layer))
+
+ elif isinstance(layer, KPPathLayer):
+ for iLayer in self.map.associateLayers:
+ if len(iLayer.objects) > 0:
+ tl = KPMapExporter.TileLayerExporter(iLayer)
+ self.tileAssociates[iLayer.associate] = tl
+ output.append(tl)
+
+ if len(iLayer.doodads) > 0:
+ dl = KPMapExporter.DoodadLayerExporter(iLayer)
+ self.doodadAssociates[iLayer.associate] = dl
+ output.append(dl)
+
+ output.append(KPMapExporter.PathLayerExporter(layer))
+ self.layers = output
+
+ def build(self):
requiredFixUps = []
stringsToAdd = set()
textures = set()
@@ -143,7 +177,7 @@ class KPMapExporter:
sectorIndices = {}
for layer in self.layers:
- if isinstance(layer.layer, KPTileLayer):
+ if isinstance(layer, self.TileLayerExporter):
layer.buildSectors(sectors, sectorIndices)
sectorData = self._packSectorData(sectors)
@@ -165,7 +199,7 @@ class KPMapExporter:
offsets[eLayer] = len(data)
offsets[layer] = len(data)
- if isinstance(layer, KPTileLayer):
+ if isinstance(eLayer, self.TileLayerExporter):
data += u32.pack(0)
# tileset name
@@ -180,17 +214,22 @@ class KPMapExporter:
pad = (4 - (len(data) & 3)) % 4
data += ('\0' * pad)
- elif isinstance(layer, KPDoodadLayer):
+ elif isinstance(eLayer, self.DoodadLayerExporter):
data += u32.pack(1)
# doodad list
- data += u32.pack(len(layer.objects))
- for doodad in layer.objects:
+ try:
+ doodadList = layer.doodads
+ except AttributeError:
+ doodadList = layer.objects
+
+ data += u32.pack(len(doodadList))
+ for doodad in doodadList:
requiredFixUps.append((len(data), doodad))
data += zero32
# now pack them ...
- for doodad in layer.objects:
+ for doodad in doodadList:
offsets[doodad] = len(data)
x, y = doodad.position
@@ -209,7 +248,7 @@ class KPMapExporter:
typeid = self.ANIM_TYPES.index(rType)
data += struct.pack('>iififf', loopid, curveid, rFrames, typeid, rStart, rEnd)
- elif isinstance(layer, KPPathLayer):
+ elif isinstance(eLayer, self.PathLayerExporter):
data += u32.pack(2)
# lists
@@ -232,7 +271,7 @@ class KPMapExporter:
x, y = node.position
current = len(data)
- data += struct.pack('>hhiiii', x, y, 0, 0, 0, 0)
+ data += struct.pack('>hhiiiiii', x, y, 0, 0, 0, 0, 0, 0)
exits = node.exits + [None,None,None,None] # TODO
requiredFixUps.append((current+4, exits[0]))
@@ -240,6 +279,11 @@ class KPMapExporter:
requiredFixUps.append((current+12, exits[2]))
requiredFixUps.append((current+16, exits[3]))
+ if node in self.tileAssociates:
+ requiredFixUps.append((current+20, self.tileAssociates[node]))
+ if node in self.doodadAssociates:
+ requiredFixUps.append((current+24, self.doodadAssociates[node]))
+
if node.isStop():
if node.level:
level1, level2 = node.level
@@ -269,8 +313,12 @@ class KPMapExporter:
requiredFixUps.append((current, start))
requiredFixUps.append((current+4, end))
- requiredFixUps.append((current+8, path.linkedLayer))
- data += (zero32 * 3)
+ if path in self.tileAssociates:
+ requiredFixUps.append((current+8, self.tileAssociates[path]))
+ if path in self.doodadAssociates:
+ requiredFixUps.append((current+12, self.doodadAssociates[path]))
+
+ data += (zero32 * 4)
data += struct.pack('>fi', path.movementSpeed, path.animation)