forked from jensnt/io_import_build_map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildmap_format.py
657 lines (572 loc) · 34.2 KB
/
buildmap_format.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
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
# Blender Import BUILD Map format Add-on
# Copyright (C) 2023 Jens Neitzel
# ##### BEGIN GPL LICENSE BLOCK #####
#
# 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 3
# 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 <https://www.gnu.org/licenses/>.
#
# ##### END GPL LICENSE BLOCK #####
from typing import List
from mathutils import Vector
from enum import Enum
import math
import struct
from collections import namedtuple
import os
import logging
log = logging.getLogger(__name__)
class BuildMap:
class Level(Enum):
FLOOR = 0
CEILING = 1
class WallType(Enum):
WHITE = 0 ## simple white wall (wall that is not connecting to another sector)
REDBOT = 1 ## bottom portion of red wall (bottom portion of wall that is connecting to another sector)
REDTOP = 2 ## top portion of red wall (top portion of wall that is connecting to another sector)
class _MapData:
def __init__(self):
self.mapversion = None
self.posx = None
self.posy = None
self.posz = None
self.ang = None
self.cursectnum = None
self.numsectors = None
self.numwalls = None
self.numsprites = None
def __init__(self, mapFilePath, ignoreErrors=False):
self.ignoreErrors = ignoreErrors
if isinstance(mapFilePath, str) and os.path.isfile(mapFilePath):
log.debug("Opening file: %s" % mapFilePath)
with open(mapFilePath, "rb") as mapFile:
self.data = BuildMap._MapData()
self.data.mapversion = struct.unpack('<i', mapFile.read(4))[0]
if (self.data.mapversion == 7) or (self.data.mapversion == 8):
self.data.posx = struct.unpack('<i', mapFile.read(4))[0]
self.data.posy = struct.unpack('<i', mapFile.read(4))[0]
self.data.posz = struct.unpack('<i', mapFile.read(4))[0]
self.data.ang = struct.unpack('<h', mapFile.read(2))[0]
self.data.cursectnum = struct.unpack('<h', mapFile.read(2))[0]
self.data.numsectors = struct.unpack('<H', mapFile.read(2))[0]
self.sectors: List[BuildMap.BuildSector] = list()
self.walls: List[BuildMap.BuildWall] = list()
self.sprites: List[BuildMap.BuildSprite] = list()
log.debug("mapversion: %s" % self.data.mapversion)
log.debug("posx: %s" % self.data.posx)
log.debug("posy: %s" % self.data.posy)
log.debug("posz: %s" % self.data.posz)
log.debug("ang: %s" % self.data.ang)
log.debug("cursectnum: %s" % self.data.cursectnum)
log.debug("numsectors: %s" % self.data.numsectors)
self.spawnAngle = BuildMap.calculateAngle(self.data.ang)
## Read Sectors
for i in range(self.data.numsectors):
self.sectors.append(self.BuildSector(mapFile, self, i))
self.data.numwalls = struct.unpack('<H', mapFile.read(2))[0]
log.debug("numwalls: %s" % self.data.numwalls)
## Sanity Check: Number of walls in Secors has to match absolute number of walls
numberOfWallsInAllSectors = 0
for sect in self.sectors:
numberOfWallsInAllSectors = numberOfWallsInAllSectors + sect.data.wallnum
if numberOfWallsInAllSectors != self.data.numwalls:
if not self.ignoreErrors:
mapFile.close()
self.handleError(ignorable=True, errorMsg="Number of walls found in Sectors %s does not match given absolute number of walls: %s !" % (numberOfWallsInAllSectors, self.data.numwalls))
## Read Walls
for i in range(self.data.numwalls):
self.walls.append(self.BuildWall(mapFile, self, i))
## Read Sprites
self.data.numsprites = struct.unpack('<H', mapFile.read(2))[0]
log.debug("numsprites: %s" % self.data.numsprites)
for i in range(self.data.numsprites):
self.sprites.append(self.BuildSprite(mapFile, self, i))
mapFile.close()
self.posxScal = float(self.data.posx) / 512
self.posyScal = float(self.data.posy) / 512
self.poszScal = float(self.data.posz) / 8192
## Find Wall Loops
log.debug("Start Finding Wall Loops")
for sect in self.sectors:
sectorWallLastIdx = sect.data.wallptr+sect.data.wallnum-1
while len(sect.walls) < sect.data.wallnum:
wallLoop = list()
if len(sect.walls) == 0:
## First loop starts with sect.data.wallptr
firstWallInLoop = self.getWall(sect.data.wallptr)
if firstWallInLoop == None:
sect.corrupted = True
self.handleError(ignorable=True, errorMsg="Unable to find next wall for first loop for sector %s" % sect.sectorIndex)
break
else:
## To find the first wall of the next loop,
## search for the next wall that is not yet assigned
## starting from the Sectors first wall.
findNextIdx = sect.data.wallptr
while True:
wall = self.getWall(findNextIdx)
if wall == None:
sect.corrupted = True
self.handleError(ignorable=True, errorMsg="Unable to find next wall for next loop for sector %s" % sect.sectorIndex)
break
if wall not in sect.walls:
firstWallInLoop = wall
break
findNextIdx += 1
if sect.corrupted:
break
## Create the next wall loop
currentWall = firstWallInLoop
while True:
sect.walls.append(currentWall)
wallLoop.append(currentWall)
if (currentWall.data.point2 < sect.data.wallptr) or (currentWall.data.point2 > sectorWallLastIdx):
sect.corrupted = True
self.handleError(ignorable=True, errorMsg="Wall loop extends outside sectors range! wall.data.point2 %s not in range of sector walls! %s to %s for sector %s" % (currentWall.data.point2, sect.data.wallptr, sectorWallLastIdx, sect.sectorIndex))
currentWall = self.getWall(currentWall.data.point2)
if (currentWall == None) or (currentWall in wallLoop) or ((self.ignoreErrors == False) and (len(sect.walls) >= sect.data.wallnum)):
if currentWall == None:
sect.corrupted = True
self.handleError(ignorable=True, errorMsg="Unable to find next wall for loop of sector %s ! Wall is outside of map range." % sect.sectorIndex)
elif currentWall != firstWallInLoop:
sect.corrupted = True
self.handleError(ignorable=True, errorMsg="Wall Loop did not end on first wall in loop in sector %s !" % sect.sectorIndex)
if len(sect.walls) > sect.data.wallnum:
sect.corrupted = True
log.error("Walls in loop exceed number of walls in sector %s !" % sect.sectorIndex)
break
sect.wallLoops.append(wallLoop)
log.debug("Finished Finding Wall Loops")
## Link Walls to Sectors
for sect in self.getSectors():
wallIndexInSect = 0
for loop in sect.wallLoops:
for wall in loop:
wall.indexInSector = wallIndexInSect
wall.sector = sect
wallIndexInSect += 1
## Postprocessing: Find wall and sector neighbors of walls
self.find_wall_neighbors()
## Postprocessing: Calculate Wall vectors and angles with now known basic properties
for wall in self.walls:
if (wall.sector == None) or (wall.sector.corrupted):
log.warning("Wall %s is not used or sector is corrupted!" % wall.indexInMap)
else:
wall.__post_init__()
## Postprocessing: Calculate slope for x and y directions separately
for sect in self.getSectors():
for lvl in self.Level:
sect.slopeVector[lvl.name] = Vector(( (math.sin(sect.walls[0].angle) * sect.slopeAbs[lvl.name]),
(math.cos(sect.walls[0].angle)*-1 * sect.slopeAbs[lvl.name]) ))
log.debug("Finished parsing file: %s" % mapFilePath)
else:
self.handleError(ignorable=False, errorMsg="Unsupported file! Only BUILD Maps in version 7 or 8 are supported.")
else:
self.handleError(ignorable=False, errorMsg="File not found: %s" % mapFilePath)
def getWallListString(self, wall_list):
if len(wall_list) == 0:
return ""
wallNames = wall_list[0].getName()
for i in range(1, len(wall_list)):
wallNames += "; " + wall_list[i].getName()
return wallNames
def find_wall_neighbors(self):
## Find wall and sector neighbors of walls using Coordinates.
## This has in some cases shown more trustworthy results than relying on the walls nextwall and nextsector fields.
## This can be improved by taking z coordinates into account as a step to support TROR
walls_dict = dict()
for wall in self.getWalls():
nextWall = wall.getPoint2Wall()
if nextWall != None:
key = tuple(sorted([(wall.data.x, wall.data.y), (nextWall.data.x, nextWall.data.y)]))
if walls_dict.get(key, None) == None:
walls_dict[key] = list()
walls_dict[key].append(wall)
for wall_list in walls_dict.values():
if len(wall_list) < 2:
continue ## This wall has no neighbors
if len(wall_list) > 2:
log.warning("More than 2 neighboring walls found: %s" % self.getWallListString(wall_list))
if wall_list[0].sector.sectorIndex == wall_list[1].sector.sectorIndex:
log.warning("Two walls in same sector found with same coordinates: %s" % self.getWallListString(wall_list))
continue ## Walls in the same sector can't be neighbors!
wall_list[0].neighborSectorIndex = wall_list[1].sector.sectorIndex
wall_list[0].neighborWallIndexInSector = wall_list[1].indexInSector
wall_list[1].neighborSectorIndex = wall_list[0].sector.sectorIndex
wall_list[1].neighborWallIndexInSector = wall_list[0].indexInSector
def calculateAngle(buildAngle):
return ((float(buildAngle) * math.pi) / 1024) * -1 ## 2048 = 360 deg = 2 PI
def getSectors(self):
return [sect for sect in self.sectors if not sect.corrupted]
def getWall(self, index):
if (index < 0) or (index >= len(self.walls)):
return None
return self.walls[index]
def getWalls(self):
return [wall for wall in self.walls if ((wall.sector != None) and (wall.sector.corrupted == False) and (wall.getPoint2Wall() != None))]
def handleError(self, ignorable=False, errorMsg="Unknown Error!"):
log.error(errorMsg)
if (ignorable == False) or (self.ignoreErrors == False):
raise ValueError(errorMsg)
class BuildSector:
sectorDataNames = namedtuple('SectorData', ['wallptr', 'wallnum', 'ceilingz', 'floorz', 'ceilingstat', 'floorstat',
'ceilingpicnum', 'ceilingheinum', 'ceilingshade', 'ceilingpal',
'ceilingxpanning', 'ceilingypanning', 'floorpicnum', 'floorheinum',
'floorshade', 'floorpal', 'floorxpanning', 'floorypanning',
'visibility', 'filler', 'lotag', 'hitag', 'extra'])
sectorDataFormat = '<hhii4hbBBBhhb5Bhhh'
def __init__(self, mapFile, parentBuildMap, index):
raw = mapFile.read(struct.calcsize(self.sectorDataFormat))
self.data = self.sectorDataNames._make(struct.unpack(self.sectorDataFormat, raw))
self.bmap = parentBuildMap
self.sectorIndex = index
self.walls: List[BuildMap.BuildWall] = list()
self.sprites: List[BuildMap.BuildSprite] = list()
self.wallLoops = list() ## List of list of walls that form loops
self.zScal = dict() ## Z-coordinate (height) of floor or ceiling at first point of sector
self.slopeAbs = dict()
self.slopeVector = dict() ## slope values (float 1 = 45 degrees)
self.corrupted = False
self.zScal[self.bmap.Level.FLOOR.name] = float(self.data.floorz) / 8192
self.zScal[self.bmap.Level.CEILING.name] = float(self.data.ceilingz) / 8192
for lvl in self.bmap.Level:
self.slopeAbs[lvl.name] = 0.0
self.slopeVector[lvl.name] = Vector((0.0, 0.0))
if self.data.floorstat&2 != 0:
self.slopeAbs[self.bmap.Level.FLOOR.name] = float(self.data.floorheinum) / 4096
if self.data.ceilingstat&2 != 0:
self.slopeAbs[self.bmap.Level.CEILING.name] = float(self.data.ceilingheinum) / 4096
def getPicNum(self, level):
if level==self.bmap.Level.FLOOR:
return self.data.floorpicnum
else:
return self.data.ceilingpicnum
def getTexPanning(self, level):
if level==self.bmap.Level.FLOOR:
return float(self.data.floorxpanning) / 256, float(self.data.floorypanning) / 256 * -1
else:
return float(self.data.ceilingxpanning) / 256, float(self.data.ceilingypanning) / 256 * -1
def getTexSwapXY(self, level):
## mapster32: F flip texture
if level==self.bmap.Level.FLOOR:
return bool((self.data.floorstat>>2)&1)
else:
return bool((self.data.ceilingstat>>2)&1)
def getTexExpansion(self, level):
## mapster32: E toggle sector texture expansion
if level==self.bmap.Level.FLOOR:
return float(((self.data.floorstat>>3)&1)+1)
else:
return float(((self.data.ceilingstat>>3)&1)+1)
def getTexFlipX(self, level):
## mapster32: F flip texture
if level==self.bmap.Level.FLOOR:
return bool((self.data.floorstat>>4)&1)
else:
return bool((self.data.ceilingstat>>4)&1)
def getTexFlipY(self, level):
## mapster32: F flip texture
if level==self.bmap.Level.FLOOR:
return bool((self.data.floorstat>>5)&1)
else:
return bool((self.data.ceilingstat>>5)&1)
def isTexAlignToFirstWall(self, level):
## mapster32: R toggle sector texture relativity alignment
if level==self.bmap.Level.FLOOR:
return bool((self.data.floorstat>>6)&1)
else:
return bool((self.data.ceilingstat>>6)&1)
def getTexFlipXFactor(self, level):
if self.getTexSwapXY(level) == self.getTexFlipX(level):
return 1
else:
return -1
def getTexFlipYFactor(self, level):
if self.getTexSwapXY(level) == self.getTexFlipY(level):
return 1
else:
return -1
def getPolyLines(self):
polylines = list()
for wallLoop in self.wallLoops:
polyline = list()
for wall in wallLoop:
polyline.append(Vector((wall.xScal, wall.yScal)))
polylines.append(polyline)
return polylines
def getHeightAtPos(self, xPos, yPos, level, respectEffectors=False): ## TODO respectEffectors is experimental for now
slopeX = self.slopeVector[level.name].x
slopeY = self.slopeVector[level.name].y
zScal = self.zScal[level.name]
zC9Sprite = None
if respectEffectors:
for sprite in self.sprites:
if sprite.data.lotag == 13: ## C-9 Explosive Sprite
zC9Sprite = sprite.zScal
break
if (zC9Sprite!=None) and (level==self.bmap.Level.FLOOR):
zScal = zC9Sprite
return (self.walls[0].xScal - xPos)*slopeX + (self.walls[0].yScal - yPos)*slopeY + zScal
def isParallaxing(self, level):
## mapster32: P toggle parallax
if level==self.bmap.Level.FLOOR:
return bool(self.data.floorstat&1)
else:
return bool(self.data.ceilingstat&1)
def getName(self, sky=False, prefix=""):
if sky:
return "%sSector_%03d_Sky" % (prefix, self.sectorIndex)
else:
return "%sSector_%03d" % (prefix, self.sectorIndex)
def getSpritesString(self):
string = ""
for sprite in self.sprites:
string += "%s " % sprite.spriteIndex
return string.rstrip()
class BuildWall:
wallDataNames = namedtuple('SectorData', ['x', 'y', 'point2', 'nextwall', 'nextsector', 'cstat', 'picnum',
'overpicnum', 'shade', 'pal', 'xrepeat', 'yrepeat', 'xpanning',
'ypanning', 'lotag', 'hitag', 'extra'])
wallDataFormat = '<ii6hb5Bhhh'
def __init__(self, mapFile, parentBuildMap, indexInMap):
raw = mapFile.read(struct.calcsize(self.wallDataFormat))
self.data = self.wallDataNames._make(struct.unpack(self.wallDataFormat, raw))
self.bmap = parentBuildMap
self.indexInMap = indexInMap
self.indexInSector = None
self.sector = None
self.xScal = float(self.data.x) / 512
self.yScal = float(self.data.y) / 512
self.neighborSectorIndex = -1 ## This has in some cases shown more trustworthy results than relying on the walls nextwall and nextsector fields.
self.neighborWallIndexInSector = -1 ## This has in some cases shown more trustworthy results than relying on the walls nextwall and nextsector fields.
self.wallParts: List[BuildWall.WallPart] = list()
def __post_init__(self):
self.startVect = Vector((self.xScal, self.yScal*-1))
self.endVect = Vector((self.getPoint2Wall().xScal, self.getPoint2Wall().yScal*-1))
self.wallVect = self.endVect - self.startVect
self.angle = Vector((1,0)).angle_signed(self.wallVect)
self.length = self.wallVect.length
def getPoint2Wall(self):
if (self.data.point2 < self.sector.data.wallptr) or (self.data.point2 >= self.bmap.data.numwalls) or (self.data.point2 >= (self.sector.data.wallptr + self.sector.data.wallnum)):
return None
else:
return self.bmap.getWall(self.data.point2)
def getNeighborSector(self):
if (self.neighborSectorIndex < 0) or (self.neighborSectorIndex >= self.bmap.data.numsectors):
return None
if self.bmap.sectors[self.neighborSectorIndex].corrupted:
return None
return self.bmap.sectors[self.neighborSectorIndex]
def getNeighborWall(self):
neighborSect = self.getNeighborSector()
if (neighborSect == None) or (self.neighborWallIndexInSector < 0) or (self.neighborWallIndexInSector >= neighborSect.data.wallnum):
return None
else:
return neighborSect.walls[self.neighborWallIndexInSector]
def getTexBottomSwap(self):
return bool((self.data.cstat>>1)&1)
def getTexAlignFlag(self):
return bool((self.data.cstat>>2)&1)
def getTexRotate(self):
## mapster32: R
return bool((self.data.cstat>>12)&1)
def getTexFlipXFactor(self):
return float(1) - float((self.data.cstat>>3)&1)*2
def getTexFlipYFactor(self):
return float(1) - float((self.data.cstat>>8)&1)*2
def getName(self, useIndexInMap=False, prefix=""):
if useIndexInMap:
return "%sSector_%03d_MapWall_%03d" % (prefix, self.sector.sectorIndex, self.indexInMap)
else:
return "%sSector_%03d_SctWall_%03d" % (prefix, self.sector.sectorIndex, self.indexInSector)
def getWallParts(self):
if len(self.wallParts) > 0:
return self.wallParts
else:
neighborSector = self.getNeighborSector()
## Create a first Wall Part.
## In case there is no neighbor Sector, this will remain the only one and it will be a "white" wall.
self.wallParts.append(self.WallPart(self, neighborSector, isRedTopWall=False))
if neighborSector != None:
## In case a neighbor sector exists we must create a second wall part.
## A Wall that connects to another Sector is a "red" wall that has a bottom and a top part.
self.wallParts.append(self.WallPart(self, neighborSector, isRedTopWall=True))
return self.wallParts
class WallPart:
def __init__(self, parentWall, neighborSector, isRedTopWall):
self.wall = parentWall
self.bmap = self.wall.bmap
self.vertices = list()
self.wallType = None
self.sectBot = None
self.sectBotLevel = None
self.sectTop = None
self.sectTopLevel = None
self.alignTexZ = 0
self.neighborSector = neighborSector
self.zBottom = None
if not isRedTopWall:
self.sectBot = self.wall.sector
self.sectBotLevel = self.bmap.Level.FLOOR
if self.neighborSector == None:
## Case 1 (simple white wall)
self.wallType = self.bmap.WallType.WHITE
self.sectTop = self.wall.sector
self.sectTopLevel = self.bmap.Level.CEILING
if self.wall.getTexAlignFlag():
self.alignTexZ = self.wall.sector.zScal[self.bmap.Level.FLOOR.name] ## Flags = 4: Aligned to floor of own sector
else:
self.alignTexZ = self.wall.sector.zScal[self.bmap.Level.CEILING.name] ## Flags = 0: Aligned to ceiling of own sector
else:
## Case 2 (bottom portion of red wall)
self.wallType = self.bmap.WallType.REDBOT
self.sectTop = self.neighborSector
self.sectTopLevel = self.bmap.Level.FLOOR
if self.wall.getTexAlignFlag():
self.alignTexZ = self.wall.sector.zScal[self.bmap.Level.CEILING.name] ## Flags = 4: Aligned to ceiling of own sector
else:
self.alignTexZ = self.neighborSector.zScal[self.bmap.Level.FLOOR.name] ## Flags = 0: Aligned to floor of neighbor sector (upper edge of lower wall portion)
else:
## Case 3 (top portion of red wall)
self.wallType = self.bmap.WallType.REDTOP
self.sectBot = self.neighborSector
self.sectBotLevel = self.bmap.Level.CEILING
self.sectTop = self.wall.sector
self.sectTopLevel = self.bmap.Level.CEILING
if self.wall.getTexAlignFlag():
self.alignTexZ = self.wall.sector.zScal[self.bmap.Level.CEILING.name] ## Flags = 4: Aligned to ceiling of own sector
else:
self.alignTexZ = self.neighborSector.zScal[self.bmap.Level.CEILING.name] ## Flags = 0: Aligned to ceiling of neighbor sector (lower edge of upper wall portion)
self.zBottom = self.sectBot.zScal[self.sectBotLevel.name]
nextWall = self.wall.getPoint2Wall()
if nextWall != None:
self.vertices.append(Vector(( self.wall.xScal, self.wall.yScal, self.sectBot.getHeightAtPos(self.wall.xScal, self.wall.yScal, self.sectBotLevel) )))
self.vertices.append(Vector(( nextWall.xScal, nextWall.yScal, self.sectBot.getHeightAtPos( nextWall.xScal, nextWall.yScal, self.sectBotLevel) )))
self.vertices.append(Vector(( nextWall.xScal, nextWall.yScal, self.sectTop.getHeightAtPos( nextWall.xScal, nextWall.yScal, self.sectTopLevel) )))
self.vertices.append(Vector(( self.wall.xScal, self.wall.yScal, self.sectTop.getHeightAtPos(self.wall.xScal, self.wall.yScal, self.sectTopLevel) )))
def getClippedVertices(self):
cverts = list()
if len(self.vertices) != 4:
return cverts
a, b, c, d = self.vertices
wallHeight = d.z*-1 - a.z*-1 ## z height on this wall (sectTop - sectBot)
nextWallHeight = c.z*-1 - b.z*-1 ## z height on next wall (sectTop - sectBot)
diffHeight = (wallHeight - nextWallHeight)
if diffHeight != 0:
ratio = wallHeight / diffHeight
clippedVert = Vector(( ((b.x-a.x)*ratio + a.x),
((b.y-a.y)*ratio + a.y),
((b.z-a.z)*ratio + a.z) ))
if wallHeight > 0:
cverts.append(a)
if nextWallHeight > 0:
cverts.append(b)
cverts.append(c)
else:
cverts.append(clippedVert)
cverts.append(d)
else:
if nextWallHeight > 0:
cverts.append(clippedVert)
cverts.append(b)
cverts.append(c)
#else: ## skip for walls with no surface.
return cverts
def isSky(self):
if self.wallType == self.bmap.WallType.REDBOT:
checkLevel = self.bmap.Level.FLOOR
elif self.wallType == self.bmap.WallType.REDTOP:
checkLevel = self.bmap.Level.CEILING
else:
return False
return self.wall.sector.isParallaxing(level=checkLevel) and self.neighborSector.isParallaxing(level=checkLevel)
def getPicNum(self):
## Get picnum, taking swapped textures for bottom walls into account
neighborWall = self.wall.getNeighborWall()
if (self.wallType == self.bmap.WallType.REDBOT) and self.wall.getTexBottomSwap() and (neighborWall != None):
return neighborWall.data.picnum
else:
return self.wall.data.picnum
def getName(self, useIndexInMap=False, prefix=""):
if self.wallType == self.bmap.WallType.REDBOT:
return self.wall.getName(useIndexInMap, prefix)+"_Bot"
elif self.wallType == self.bmap.WallType.REDTOP:
return self.wall.getName(useIndexInMap, prefix)+"_Top"
else:
return self.wall.getName(useIndexInMap, prefix)
class BuildSprite:
spriteDataNames = namedtuple('SectorData', ['x', 'y', 'z', 'cstat', 'picnum', 'shade', 'pal', 'clipdist', 'filler',
'xrepeat', 'yrepeat', 'xoffset', 'yoffset', 'sectnum', 'statnum', 'ang',
'owner', 'xvel', 'yvel', 'zvel', 'lotag', 'hitag', 'extra'])
spriteDataFormat = '<iiihhb5Bbb10h'
def __init__(self, mapFile, parentBuildMap, index):
raw = mapFile.read(struct.calcsize(self.spriteDataFormat))
self.data = self.spriteDataNames._make(struct.unpack(self.spriteDataFormat, raw))
self.bmap = parentBuildMap
self.spriteIndex = index
self.xScal = float(self.data.x) / 512
self.yScal = float(self.data.y) / 512
self.zScal = float(self.data.z) / 8192
self.angle = BuildMap.calculateAngle(self.data.ang)
if self.data.sectnum >= 0 and self.data.sectnum < self.bmap.data.numsectors:
self.bmap.sectors[self.data.sectnum].sprites.append(self)
else:
log.warning("Sprite %s sectnum is not in range of maps number of sectors: %s" % (self.spriteIndex, self.bmap.data.numsectors))
def isFlippedX(self):
## cstat bit 2: 1 = x-flipped, 0 = normal
return self.data.cstat&4 != 0
def isFlippedY(self):
## cstat bit 3: 1 = y-flipped, 0 = normal
return self.data.cstat&8 != 0
def isFaceSprite(self):
## cstat bits 5-4: 00 = FACE sprite (default)
return ((self.data.cstat>>4)&3) == 0
def isWallSprite(self):
## cstat bits 5-4: 01 = WALL sprite (like masked walls)
return ((self.data.cstat>>4)&3) == 1
def isFloorSprite(self):
## cstat bits 5-4: 10 = FLOOR sprite (parallel to ceilings&floors)
return ((self.data.cstat>>4)&3) == 2
def isRealCentered(self):
## cstat bit 7: 1 = Real centered centering, 0 = foot center
return self.data.cstat&128 != 0
def getDataKey(self):
## This must return a key that is individual for every aspect of a Sprite
## that makes it neccessary to have a separate Datablock.
## So that when used for a dictionary we can reuse existing datablocks when they make no difference to the sprite.
return (self.data.picnum, self.isFlippedX(), self.isFlippedY(), self.isFloorSprite(), self.isRealCentered())
def isEffectSprite(self):
## https://wiki.eduke32.com/wiki/Special_Tile_Reference_Guide
## https://wiki.eduke32.com/wiki/Sector_effectors
## https://wiki.eduke32.com/wiki/Tilenum
## https://wiki.eduke32.com/wiki/Actor
return (self.data.picnum >= 1) and (self.data.picnum <= 10)
def isGunAmmo(self):
return self.data.picnum in [21, 22, 23, 24, 25, 26, 27, 28, 29, 32, 37, 40, 41, 42, 44, 45, 46, 47, 49]
def isHealthEquipment(self):
return self.data.picnum in [51, 52, 53, 54, 55, 56, 57, 59, 60, 61, 100]
def getScale(self, like_in_game=True):
## Return normalized Scale with 64 as 1
if self.isFloorSprite():
scale = ((self.data.yrepeat/64), (self.data.xrepeat/64), (self.data.xrepeat/64))
else:
scale = ((self.data.xrepeat/64), (self.data.xrepeat/64), (self.data.yrepeat/64))
if like_in_game and (self.isGunAmmo() or self.isHealthEquipment()):
if self.data.picnum == 26: ## HEAVYHBOMB
return (0.125, 0.125, 0.125)
if self.data.picnum == 40: ## AMMO
return (0.25, 0.25, 0.25)
return (0.5, 0.5, 0.5)
else:
return scale
def getName(self, prefix=""):
return "%sSprite_%03d" % (prefix, self.spriteIndex)