Skip to content

Commit

Permalink
Revert "bugfix for non-deterministic behavior in XML output"
Browse files Browse the repository at this point in the history
This reverts commit f104408.
  • Loading branch information
Zamiell committed Mar 22, 2022
1 parent f104408 commit 32fc14d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 29 deletions.
18 changes: 6 additions & 12 deletions src/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,33 +374,27 @@ def getPrefix(self):
return Room.getDesc(self.info, self.name, self.difficulty, self.weight)

class _SpawnIter:
"""
This is the old iterator that does not return a sorted list. It is currently
unused.
"""

def __init__(self, gridSpawns, dimensions):
def __init__(self, gridSpawns, dims):
self.idx = -1
self.spawns = gridSpawns
self.width, self.height = dimensions
self.width, self.height = dims

def __iter__(self):
return self

def __next__(self):
entityStack = None
stack = None
while True:
self.idx += 1
if self.idx >= self.width * self.height or self.idx >= len(self.spawns):
raise StopIteration

entityStack = self.spawns[self.idx]
if entityStack:
stack = self.spawns[self.idx]
if stack:
break

x = int(self.idx % self.width)
y = int(self.idx / self.width)
return (entityStack, x, y)
return (stack, x, y)

def spawns(self):
return Room._SpawnIter(self.gridSpawns, self.info.dims)
Expand Down
25 changes: 8 additions & 17 deletions src/roomconvert.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,21 +178,12 @@ def flattenXml(d):

output.append(f'\t\t<door exists="{exists}" x="{x - 1}" y="{y - 1}"/>\n')

for entityStack, x, y in room.spawns():
for stack, x, y in room.spawns():
output.append(f'\t\t<spawn x="{x - 1}" y="{y - 1}">\n')

# Basement Renovator will print out the spawns in a random sort
# This leads to problems when diffing XML files
# Thus, sort the spawns so that they will always be printed in the same
# deterministic order
# We sort by entity type, variant, and then sub-type
entityStack.sort(key=lambda entity: entity.Type)
entityStack.sort(key=lambda entity: entity.Variant)
entityStack.sort(key=lambda entity: entity.Subtype)

for entity in entityStack:
for ent in stack:
output.append(
f'\t\t\t<entity type="{entity.Type}" variant="{entity.Variant}" subtype="{entity.Subtype}" weight="{entity.weight}"{flattenXml(entity.xmlProps)}/>\n'
f'\t\t\t<entity type="{ent.Type}" variant="{ent.Variant}" subtype="{ent.Subtype}" weight="{ent.weight}"{flattenXml(ent.xmlProps)}/>\n'
)

output.append("\t\t</spawn>\n")
Expand Down Expand Up @@ -297,8 +288,8 @@ def commonToSTBRB(path, rooms):
totalBytes += len(room.name)
totalBytes += doorHeaderPacker.size + doorPacker.size * len(room.info.doors)
totalBytes += room.getSpawnCount() * stackPacker.size
for entityStack, x, y in room.spawns():
totalBytes += len(entityStack) * entPacker.size
for stack, x, y in room.spawns():
totalBytes += len(stack) * entPacker.size

out = bytearray(totalBytes)
off = 0
Expand All @@ -325,12 +316,12 @@ def commonToSTBRB(path, rooms):
doorPacker.pack_into(out, off, door[0] - 1, door[1] - 1, door[2])
off += doorPacker.size

for entityStack, x, y in room.spawns():
numEnts = len(entityStack)
for stack, x, y in room.spawns():
numEnts = len(stack)
stackPacker.pack_into(out, off, x - 1, y - 1, numEnts)
off += stackPacker.size

for entity in entityStack:
for entity in stack:
entPacker.pack_into(
out, off, entity.Type, entity.Variant, entity.Subtype, entity.weight
)
Expand Down

0 comments on commit 32fc14d

Please sign in to comment.