Add block rotation mappings for X and Z axis, optimize block search
This commit is contained in:
parent
0a164dd9b3
commit
323510856c
@ -288,35 +288,6 @@ class BlockTypeSet(object):
|
|||||||
|
|
||||||
return BlockType(ID, meta, self)
|
return BlockType(ID, meta, self)
|
||||||
|
|
||||||
def matchingState(self, internalName, stateDict):
|
|
||||||
"""
|
|
||||||
Find the first block with the given name whose state matches all of the keys
|
|
||||||
and values in stateDict.
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
----------
|
|
||||||
internalName : unicode
|
|
||||||
block's internal name
|
|
||||||
stateDict : dict
|
|
||||||
the keys and values that the returned state must match
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
|
|
||||||
block: BlockType
|
|
||||||
|
|
||||||
"""
|
|
||||||
for block in self:
|
|
||||||
if block.internalName == internalName:
|
|
||||||
bsd = block.stateDict
|
|
||||||
for k, v in stateDict.iteritems():
|
|
||||||
if bsd.get(k) != v:
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
return block
|
|
||||||
|
|
||||||
return None
|
|
||||||
|
|
||||||
def discardIDs(self, blockIDs):
|
def discardIDs(self, blockIDs):
|
||||||
blockIDs = set(blockIDs)
|
blockIDs = set(blockIDs)
|
||||||
blocktypes = [b for b in self.allBlocks if b.ID in blockIDs]
|
blocktypes = [b for b in self.allBlocks if b.ID in blockIDs]
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
"""
|
"""
|
||||||
from __future__ import absolute_import, division, print_function, unicode_literals
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
import logging
|
import logging
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
import numpy
|
import numpy
|
||||||
|
|
||||||
@ -19,15 +20,45 @@ def blankRotationTable():
|
|||||||
return table
|
return table
|
||||||
|
|
||||||
|
|
||||||
def yAxisTable(blocktypes):
|
class BlockRotations(object):
|
||||||
mapping = {
|
mappings = {
|
||||||
|
'y': {
|
||||||
'north': 'east',
|
'north': 'east',
|
||||||
'east': 'south',
|
'east': 'south',
|
||||||
'south': 'west',
|
'south': 'west',
|
||||||
'west': 'north',
|
'west': 'north',
|
||||||
|
},
|
||||||
|
'x': {
|
||||||
|
'up': 'south',
|
||||||
|
'south': 'down',
|
||||||
|
'down': 'north',
|
||||||
|
'north': 'up',
|
||||||
|
},
|
||||||
|
'z': {
|
||||||
|
'east': 'up',
|
||||||
|
'up': 'west',
|
||||||
|
'west': 'down',
|
||||||
|
'down': 'east',
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rail_shapes = {
|
axisMappings = {
|
||||||
|
'y': {
|
||||||
|
'x': 'z',
|
||||||
|
'z': 'x',
|
||||||
|
},
|
||||||
|
'x': {
|
||||||
|
'y': 'z',
|
||||||
|
'z': 'y',
|
||||||
|
},
|
||||||
|
'z': {
|
||||||
|
'x': 'y',
|
||||||
|
'y': 'x',
|
||||||
|
},
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
railShapes = {
|
||||||
'ascending_north': 'ascending_east',
|
'ascending_north': 'ascending_east',
|
||||||
'ascending_east': 'ascending_south',
|
'ascending_east': 'ascending_south',
|
||||||
'ascending_south': 'ascending_west',
|
'ascending_south': 'ascending_west',
|
||||||
@ -39,17 +70,28 @@ def yAxisTable(blocktypes):
|
|||||||
'north_west': 'north_east',
|
'north_west': 'north_east',
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def __init__(self, blocktypes):
|
||||||
|
self.blocktypes = blocktypes
|
||||||
|
|
||||||
|
self.blocksByInternalName = defaultdict(list)
|
||||||
|
|
||||||
|
for block in self.blocktypes:
|
||||||
|
self.blocksByInternalName[block.internalName].append(block)
|
||||||
|
|
||||||
|
self.rotateY90 = self.buildTable(axis='y')
|
||||||
|
self.rotateX90 = self.buildTable(axis='x')
|
||||||
|
self.rotateZ90 = self.buildTable(axis='z')
|
||||||
|
|
||||||
|
def buildTable(self, axis):
|
||||||
|
mapping = self.mappings[axis]
|
||||||
|
axisMapping = self.axisMappings[axis]
|
||||||
|
|
||||||
table = blankRotationTable()
|
table = blankRotationTable()
|
||||||
|
|
||||||
for block in blocktypes:
|
for block in self.blocktypes:
|
||||||
stateString = block.blockState
|
state = block.stateDict
|
||||||
if not len(stateString):
|
if not len(state):
|
||||||
continue
|
|
||||||
|
|
||||||
try:
|
|
||||||
state = parseBlockstate(stateString)
|
|
||||||
except:
|
|
||||||
log.exception("Error parsing blockstate: %s", stateString)
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# First pass: facing=north and similar
|
# First pass: facing=north and similar
|
||||||
@ -71,53 +113,74 @@ def yAxisTable(blocktypes):
|
|||||||
newState[k] = 'false'
|
newState[k] = 'false'
|
||||||
newState[v] = 'true'
|
newState[v] = 'true'
|
||||||
|
|
||||||
# For signs and banners: rotation=10 and similar
|
|
||||||
# y-axis only
|
|
||||||
|
|
||||||
state = newState
|
state = newState
|
||||||
|
|
||||||
|
if axis == 'y':
|
||||||
|
# For signs and banners: rotation=10 and similar
|
||||||
|
|
||||||
if 'rotation' in state:
|
if 'rotation' in state:
|
||||||
rotation = (int(state['rotation']) + 4) % 16
|
rotation = (int(state['rotation']) + 4) % 16
|
||||||
state['rotation'] = unicode(rotation)
|
state['rotation'] = unicode(rotation)
|
||||||
|
|
||||||
# For logs and such: axis=x and similar
|
|
||||||
|
|
||||||
if 'axis' in state:
|
|
||||||
axis = state['axis']
|
|
||||||
|
|
||||||
# y-axis only
|
|
||||||
if axis == 'x':
|
|
||||||
axis = 'z'
|
|
||||||
elif axis == 'z':
|
|
||||||
axis = 'x'
|
|
||||||
|
|
||||||
state['axis'] = axis
|
|
||||||
|
|
||||||
# For rails, powered rails, etc: shape=north_east
|
# For rails, powered rails, etc: shape=north_east
|
||||||
# y-axis only
|
|
||||||
|
|
||||||
if 'shape' in state:
|
if 'shape' in state:
|
||||||
shape = state['shape']
|
shape = state['shape']
|
||||||
|
|
||||||
newShape = rail_shapes.get(shape)
|
newShape = self.railShapes.get(shape)
|
||||||
if newShape:
|
if newShape:
|
||||||
state['shape'] = newShape
|
state['shape'] = newShape
|
||||||
|
|
||||||
|
# For logs and such: axis=x and similar
|
||||||
|
|
||||||
|
if 'axis' in state:
|
||||||
|
axis = state['axis']
|
||||||
|
axis = axisMapping.get(axis, axis)
|
||||||
|
state['axis'] = axis
|
||||||
|
|
||||||
#print("Changed %s \nto %s" % (stateString, newStateString))
|
#print("Changed %s \nto %s" % (stateString, newStateString))
|
||||||
|
|
||||||
newBlock = blocktypes.matchingState(block.internalName, state)
|
newBlock = self.matchingState(block.internalName, state)
|
||||||
if newBlock is block:
|
if newBlock is block:
|
||||||
pass
|
pass
|
||||||
elif newBlock is None:
|
# elif newBlock is None:
|
||||||
newStateString = joinBlockstate(state)
|
# newStateString = joinBlockstate(state)
|
||||||
# print("no mapping for %s%s" % (block.internalName, newStateString))
|
# print("no mapping for %s%s" % (block.internalName, newStateString))
|
||||||
else:
|
elif newBlock is not None:
|
||||||
# print("Changed %s \nto %s" % (block, newBlock))
|
# print("Changed %s \nto %s" % (block, newBlock))
|
||||||
table[block.ID, block.meta] = [newBlock.ID, newBlock.meta]
|
table[block.ID, block.meta] = [newBlock.ID, newBlock.meta]
|
||||||
|
|
||||||
return table
|
return table
|
||||||
|
|
||||||
|
def matchingState(self, internalName, stateDict):
|
||||||
|
"""
|
||||||
|
Find the first block with the given name whose state matches all of the keys
|
||||||
|
and values in stateDict.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
internalName : unicode
|
||||||
|
block's internal name
|
||||||
|
stateDict : dict
|
||||||
|
the keys and values that the returned state must match
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
|
||||||
|
block: BlockType
|
||||||
|
|
||||||
|
"""
|
||||||
|
for b in self.blocksByInternalName[internalName]:
|
||||||
|
bsd = b.stateDict
|
||||||
|
for k, v in stateDict.iteritems():
|
||||||
|
if bsd.get(k) != v:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
return b
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def xxxtest_yAxisTable():
|
def xxxtest_yAxisTable():
|
||||||
from . import PCBlockTypeSet
|
from . import PCBlockTypeSet
|
||||||
blocktypes = PCBlockTypeSet()
|
blocktypes = PCBlockTypeSet()
|
||||||
@ -143,7 +206,7 @@ def main():
|
|||||||
from timeit import timeit
|
from timeit import timeit
|
||||||
blocktypes = PCBlockTypeSet()
|
blocktypes = PCBlockTypeSet()
|
||||||
|
|
||||||
secs = timeit(lambda: yAxisTable(blocktypes), number=1)
|
secs = timeit(lambda: BlockRotations(blocktypes), number=1)
|
||||||
print("Time: %0.3f" % secs)
|
print("Time: %0.3f" % secs)
|
||||||
|
|
||||||
assert secs < 0.1
|
assert secs < 0.1
|
||||||
|
Reference in New Issue
Block a user