Fix position calculation modifying negative values

- For #8
 - Remove all modulo operations in Pos.getAsInt()
 - Change MapInterface.get_node and MapInterface.set_node to apply the modulo themselves
 - Be a little bit more explicit about invalid coordinates whenever a MapBlock receives one
This commit is contained in:
LeMagnesium 2016-04-06 21:52:07 +02:00
parent 0669a41c77
commit 5486478ad7
2 changed files with 4 additions and 4 deletions

View File

@ -163,7 +163,7 @@ class MapBlock:
def check_pos(self, mapblockpos):
if mapblockpos < 0 or mapblockpos >= 4096:
raise OutOfBordersCoordinates
raise OutOfBordersCoordinates("Invalid position : " + str(mapblockpos))
def get_node(self, mapblockpos):
self.check_pos(mapblockpos)
@ -638,7 +638,7 @@ class MapInterface:
if not self.check_for_pos(mapblockpos):
return Node("ignore", pos = pos)
return self.mapblocks[mapblockpos].get_node(pos.getAsInt())
return self.mapblocks[mapblockpos].get_node(Pos(pos.x % 16, pos.y % 16, pos.z % 16).getAsInt())
def set_node(self, pos, node):
mapblock = determineMapBlock(pos)
@ -649,7 +649,7 @@ class MapInterface:
node.pos = pos
self.flag_mod(mapblockpos)
return self.mapblocks[mapblockpos].set_node(pos.getAsInt(), node)
return self.mapblocks[mapblockpos].set_node(Pos(pos.x % 16, pos.y % 16, pos.z % 16).getAsInt(), node)
def remove_node(self, pos):
return self.set_node(self, pos, Node("air"))

View File

@ -71,7 +71,7 @@ class Pos:
return self.x == other.x and self.y == other.y and self.z == other.z
def getAsInt(self, max_val = 16):
return int64((self.z % max_val) * max_val * max_val + (self.y % max_val) * max_val + (self.x % max_val))
return self.z * max_val * max_val + self.y * max_val + self.x
def getAsTuple(self):
return (self.x, self.y, self.z)