Second draft of heuristic-based block rotation

This commit is contained in:
David Vierra 2016-05-05 23:04:22 -10:00
parent 55d66b8808
commit 3b647c7f93
3 changed files with 111 additions and 45 deletions

View File

@ -22,6 +22,7 @@ from mceditlib.blocktypes import BlockType
from mceditlib.cachefunc import lru_cache
from mceditlib.geometry import Vector
from mceditlib.selection import FloatBox
from mceditlib.blocktypes import splitInternalName
from libc.stdlib cimport malloc, free
from libc.string cimport memset
@ -247,7 +248,7 @@ cdef class BlockModels(object):
log.debug("Model for state %s previously loaded", nameAndState)
continue
internalName, blockState = blocktypes._splitInternalName(nameAndState)
internalName, blockState = splitInternalName(nameAndState)
block = blocktypes.get(internalName, None)
if block is None:
log.debug("No block found for block %s", internalName)

View File

@ -24,12 +24,37 @@ MCXDecreasing = 4 #west
MCXIncreasing = 5 #east
def parseBlockstate(state):
assert state[0] == '[' and state[-1] == ']'
state = state[1:-1]
return dict(pair.split('=') for pair in state.split(','))
def joinBlockstate(stateDict):
return '[' + ','.join(k + '=' + v for k, v in stateDict.items()) + ']'
def splitInternalName(nameAndState):
if '[' in nameAndState:
idx = nameAndState.index('[')
internalName, blockState = nameAndState[:idx], nameAndState[idx:]
else:
internalName = nameAndState
blockState = ""
return internalName, blockState
class BlockType(namedtuple("_BlockType", "ID meta blocktypeSet")):
"""
Value object representing an (id, data, blocktypeSet) tuple.
Accessing its attributes will return the corresponding elements
of its parent set's json arrays.
A blocktype set is initialized with one BlockType for each known
ID/meta combination. BlockTypes are also created as needed when the
set is asked for an unknown combination.
"""
def __repr__(self):
@ -52,6 +77,18 @@ class BlockType(namedtuple("_BlockType", "ID meta blocktypeSet")):
return cmp(key(self), key(other))
def __getattr__(self, attr):
if attr == "stateDict":
if attr in self.__dict__:
return self.__dict__[attr]
blockState = self.blockState
if not len(blockState):
self.__dict__[attr] = {}
else:
self.__dict__[attr] = parseBlockstate(blockState)
return self.__dict__[attr]
return self.blocktypeSet.getBlockTypeAttr(self, attr)
@ -93,13 +130,13 @@ class BlockTypeSet(object):
self.allBlocks = SortedSet()
self.blockJsons = {}
self.IDsByState = {} # internalName[blockstates] -> (id, meta)
self.statesByID = {} # (id, meta) -> internalName[blockstates]
self.IDsByState = {} # "internalName[blockstates]" -> (id, meta)
self.statesByID = {} # (id, meta) -> "internalName[blockstates]"
self.IDsByName = {} # internalName -> id
self.namesByID = {} # id -> internalName
self.defaultBlockstates = {} # internalName -> [blockstates]
self.defaultBlockstates = {} # internalName -> "[blockstates]"
self.defaults = {
'displayName': defaultName,
@ -172,11 +209,7 @@ class BlockTypeSet(object):
if attr == "nameAndState":
return nameAndState
if attr == "internalName":
return self._splitInternalName(nameAndState)[0]
if attr == "blockState":
return self._splitInternalName(nameAndState)[1]
try:
blockJson = self.blockJsons[nameAndState]
except KeyError:
@ -206,15 +239,6 @@ class BlockTypeSet(object):
def __iter__(self):
return iter(self.allBlocks)
def _splitInternalName(self, nameAndState):
if '[' in nameAndState:
idx = nameAndState.index('[')
internalName, blockState = nameAndState[:idx], nameAndState[idx:]
else:
internalName = nameAndState
blockState = ""
return internalName, blockState
def __getitem__(self, nameAndState):
"""
@ -265,6 +289,34 @@ class BlockTypeSet(object):
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):
blockIDs = set(blockIDs)
@ -291,7 +343,7 @@ class BlockTypeSet(object):
def addIDMappingFromJSON(self, entries):
for ID, meta, nameAndState in entries:
internalName, blockState = self._splitInternalName(nameAndState)
internalName, blockState = splitInternalName(nameAndState)
self.IDsByState[nameAndState] = ID, meta
self.IDsByName[internalName] = ID
self.statesByID = {v: k for (k, v) in self.IDsByState.iteritems()}
@ -346,8 +398,12 @@ class BlockTypeSet(object):
#log.debug("Adding block: \n%s", json.dumps(blockJson, indent=2))
nameAndState = blockJson.get("blockState", internalName)
internalName, blockState = self._splitInternalName(nameAndState)
blockState = blockJson.get("blockState", internalName)
_, blockState = splitInternalName(blockState)
# XXX raw jsons are created with 'blockState': <nameAndState>
blockJson['blockState'] = blockState
nameAndState = internalName + blockState
IDmeta = self.IDsByState.get(nameAndState)
if IDmeta is None:
@ -356,9 +412,9 @@ class BlockTypeSet(object):
ID, meta = IDmeta
self.allBlocks.add(BlockType(ID, meta, self))
oldJson = self.blockJsons.get(internalName + blockState)
oldJson = self.blockJsons.get(nameAndState)
if oldJson is None:
oldJson = self.blockJsons[internalName + blockState] = blockJson
oldJson = self.blockJsons[nameAndState] = blockJson
else:
oldJson.update(blockJson)

View File

@ -6,17 +6,17 @@ import logging
import numpy
from mceditlib.blocktypes import parseBlockstate, joinBlockstate, PCBlockTypeSet
log = logging.getLogger(__name__)
def parseBlockstate(state):
assert state[0] == '[' and state[-1] == ']'
state = state[1:-1]
return dict(pair.split('=') for pair in state.split(','))
def blankRotationTable():
table = numpy.indices((32768, 16))
def joinBlockstate(stateDict):
return '[' + ','.join(k + '=' + v for k, v in stateDict.items()) + ']'
# Roll array so table[x, y] returns [x, y]
table = numpy.rollaxis(numpy.rollaxis(table, 1), 2, 1)
return table
def yAxisTable(blocktypes):
@ -39,10 +39,7 @@ def yAxisTable(blocktypes):
'north_west': 'north_east',
}
table = numpy.indices((32768, 16))
# Roll array so table[x, y] returns [x, y]
table = numpy.rollaxis(numpy.rollaxis(table, 1), 2, 1)
table = blankRotationTable()
for block in blocktypes:
stateString = block.blockState
@ -91,7 +88,7 @@ def yAxisTable(blocktypes):
# y-axis only
if axis == 'x':
axis = 'z'
if axis == 'z':
elif axis == 'z':
axis = 'x'
state['axis'] = axis
@ -106,38 +103,50 @@ def yAxisTable(blocktypes):
if newShape:
state['shape'] = newShape
newStateString = joinBlockstate(state)
print("Changed %s \nto %s" % (stateString, newStateString))
#print("Changed %s \nto %s" % (stateString, newStateString))
try:
newBlock = blocktypes[block.internalName, newStateString]
except KeyError:
print("no mapping for %s%s" % (block.internalName, newStateString))
table[block.ID, block.meta] = [newBlock.ID, newBlock.meta]
newBlock = blocktypes.matchingState(block.internalName, state)
if newBlock is block:
pass
elif newBlock is None:
newStateString = joinBlockstate(state)
# print("no mapping for %s%s" % (block.internalName, newStateString))
else:
# print("Changed %s \nto %s" % (block, newBlock))
table[block.ID, block.meta] = [newBlock.ID, newBlock.meta]
return table
def test_yAxisTable():
def xxxtest_yAxisTable():
from . import PCBlockTypeSet
blocktypes = PCBlockTypeSet()
table = yAxisTable(blocktypes)
assert (table != blankRotationTable()).any(), "Table is blank"
changed = False
changedNames = set()
for i in range(32768):
for j in range(16):
e = table[i,j]
if e[0] != i and e[1] != j:
if e[0] != i or e[1] != j:
changed = True
name = blocktypes[i, j].internalName
if name not in changedNames:
print("%s is changed" % name)
# print("%s is changed" % name)
changedNames.add(name)
assert changed, "Table is unchanged"
def main():
from timeit import timeit
blocktypes = PCBlockTypeSet()
secs = timeit(lambda: yAxisTable(blocktypes), number=1)
print("Time: %0.3f" % secs)
assert secs < 0.1
if __name__ == '__main__':
main()