def test_bsp(): """ commented out statements work in libtcod-cffi """ bsp = libtcodpy.bsp_new_with_size(0, 0, 64, 64) repr(bsp) # test __repr__ on leaf libtcodpy.bsp_resize(bsp, 0, 0, 32, 32) assert bsp != None # test getter/setters bsp.x = bsp.x bsp.y = bsp.y bsp.w = bsp.w bsp.h = bsp.h bsp.position = bsp.position bsp.horizontal = bsp.horizontal bsp.level = bsp.level # cover functions on leaf #self.assertFalse(libtcodpy.bsp_left(bsp)) #self.assertFalse(libtcodpy.bsp_right(bsp)) #self.assertFalse(libtcodpy.bsp_father(bsp)) assert libtcodpy.bsp_is_leaf(bsp) assert libtcodpy.bsp_contains(bsp, 1, 1) #self.assertFalse(libtcodpy.bsp_contains(bsp, -1, -1)) #self.assertEqual(libtcodpy.bsp_find_node(bsp, 1, 1), bsp) #self.assertFalse(libtcodpy.bsp_find_node(bsp, -1, -1)) libtcodpy.bsp_split_once(bsp, False, 4) repr(bsp) # test __repr__ with parent libtcodpy.bsp_split_once(bsp, True, 4) repr(bsp) # cover functions on parent assert libtcodpy.bsp_left(bsp) assert libtcodpy.bsp_right(bsp) #self.assertFalse(libtcodpy.bsp_father(bsp)) assert not libtcodpy.bsp_is_leaf(bsp) #self.assertEqual(libtcodpy.bsp_father(libtcodpy.bsp_left(bsp)), bsp) #self.assertEqual(libtcodpy.bsp_father(libtcodpy.bsp_right(bsp)), bsp) libtcodpy.bsp_split_recursive(bsp, None, 4, 2, 2, 1.0, 1.0) # cover bsp_traverse def traverse(node, user_data): return True libtcodpy.bsp_traverse_pre_order(bsp, traverse) libtcodpy.bsp_traverse_in_order(bsp, traverse) libtcodpy.bsp_traverse_post_order(bsp, traverse) libtcodpy.bsp_traverse_level_order(bsp, traverse) libtcodpy.bsp_traverse_inverted_level_order(bsp, traverse) # test __repr__ on deleted node son = libtcodpy.bsp_left(bsp) libtcodpy.bsp_remove_sons(bsp) repr(son) libtcodpy.bsp_delete(bsp)
def test_bsp(): """ commented out statements work in libtcod-cffi """ bsp = libtcodpy.bsp_new_with_size(0, 0, 64, 64) repr(bsp) # test __repr__ on leaf libtcodpy.bsp_resize(bsp, 0, 0, 32, 32) assert bsp != None # test getter/setters bsp.x = bsp.x bsp.y = bsp.y bsp.w = bsp.w bsp.h = bsp.h bsp.position = bsp.position bsp.horizontal = bsp.horizontal bsp.level = bsp.level # cover functions on leaf #self.assertFalse(libtcodpy.bsp_left(bsp)) #self.assertFalse(libtcodpy.bsp_right(bsp)) #self.assertFalse(libtcodpy.bsp_father(bsp)) assert libtcodpy.bsp_is_leaf(bsp) assert libtcodpy.bsp_contains(bsp, 1, 1) #self.assertFalse(libtcodpy.bsp_contains(bsp, -1, -1)) #self.assertEqual(libtcodpy.bsp_find_node(bsp, 1, 1), bsp) #self.assertFalse(libtcodpy.bsp_find_node(bsp, -1, -1)) libtcodpy.bsp_split_once(bsp, False, 4) repr(bsp) # test __repr__ with parent libtcodpy.bsp_split_once(bsp, True, 4) repr(bsp) # cover functions on parent assert libtcodpy.bsp_left(bsp) assert libtcodpy.bsp_right(bsp) #self.assertFalse(libtcodpy.bsp_father(bsp)) assert not libtcodpy.bsp_is_leaf(bsp) #self.assertEqual(libtcodpy.bsp_father(libtcodpy.bsp_left(bsp)), bsp) #self.assertEqual(libtcodpy.bsp_father(libtcodpy.bsp_right(bsp)), bsp) libtcodpy.bsp_split_recursive(bsp, None, 4, 2, 2, 1.0, 1.0) # cover bsp_traverse def traverse(node, user_data): return True libtcodpy.bsp_traverse_pre_order(bsp, traverse) libtcodpy.bsp_traverse_in_order(bsp, traverse) libtcodpy.bsp_traverse_post_order(bsp, traverse) libtcodpy.bsp_traverse_level_order(bsp, traverse) libtcodpy.bsp_traverse_inverted_level_order(bsp, traverse) # test __repr__ on deleted node son = libtcodpy.bsp_left(bsp) libtcodpy.bsp_remove_sons(bsp) repr(son) libtcodpy.bsp_delete(bsp)
def travers_node(self, node, dat): if libtcod.bsp_is_leaf(node): minx = node.x + 1 maxx = node.x + node.w - 1 miny = node.y + 1 maxy = node.y + node.h - 1 if maxx == self.width - 1: maxx -= 1 if maxy == self.height - 1: maxy -= 1 if BspConstants.FULL_ROOMS == False: minx = libtcod.random_get_int(None, minx, maxx - BspConstants.MIN_SIZE + 1) miny = libtcod.random_get_int(None, miny, maxy - BspConstants.MIN_SIZE + 1) maxx = libtcod.random_get_int(None, minx + BspConstants.MIN_SIZE - 2, maxx) maxy = libtcod.random_get_int(None, miny + BspConstants.MIN_SIZE - 2, maxy) node.x = minx node.y = miny node.w = maxx - minx + 1 node.h = maxy - miny + 1 for x in range(minx, maxx + 1): for y in range(miny, maxy + 1): self.tiles[x][y].blocked = False self.tiles[x][y].block_sight = False self.bsp_rooms.append((int( (minx + maxx) / 2), int((miny + maxy) / 2))) else: left = libtcod.bsp_left(node) right = libtcod.bsp_right(node) node.x = min(left.x, right.x) node.y = min(left.y, right.y) node.w = max(left.x + left.w, right.x + right.w) - node.x node.h = max(left.y + left.h, right.y + right.h) - node.y if node.horizontal: if left.x + left.w - 1 < right.x or right.x + right.w - 1 < left.x: x1 = libtcod.random_get_int(None, left.x, left.x + left.w - 1) x2 = libtcod.random_get_int(None, right.x, right.x + right.w - 1) y = libtcod.random_get_int(None, left.y + left.h, right.y) self.vline_up(x1, y - 1) self.hline(x1, y, x2) self.vline_down(x2, y + 1) else: minx = max(left.x, right.x) maxx = min(left.x + left.w - 1, right.x + right.w - 1) x = libtcod.random_get_int(None, minx, maxx) while x > self.width - 1: x -= 1 self.vline_down(x, right.x) self.vline_up(x, right.y - 1) else: if left.x + left.h - 1 < right.y or right.x + right.h - 1 < left.y: y1 = libtcod.random_get_int(None, left.y, left.y + left.h - 1) y2 = libtcod.random_get_int(None, right.y, right.y + right.h - 1) x = libtcod.random_get_int(None, left.x + left.w, right.x) self.hline_left(x - 1, y1) self.vline(x, y1, y2) self.hline_right(x + 1, y2) else: miny = max(left.y, right.y) maxy = min(left.y + left.h - 1, right.y + right.h - 1) y = libtcod.random_get_int(None, miny, maxy) while y > self.height - 1: y -= 1 self.hline_left(right.x - 1, y) self.hline_right(right.x, y) return True
def traverse_node(self, node, dat): #Create rooms if libtcod.bsp_is_leaf(node): minx = node.x + 1 maxx = node.x + node.w - 1 miny = node.y + 1 maxy = node.y + node.h - 1 if maxx == self.MAP_WIDTH - 1: maxx -= 1 if maxy == self.MAP_HEIGHT - 1: maxy -= 1 if self.FULL_ROOMS == False: minx = libtcod.random_get_int(None, minx, maxx - self.MIN_SIZE + 1) miny = libtcod.random_get_int(None, miny, maxy - self.MIN_SIZE + 1) maxx = libtcod.random_get_int(None, minx + self.MIN_SIZE - 2, maxx) maxy = libtcod.random_get_int(None, miny + self.MIN_SIZE - 2, maxy) node.x = minx node.y = miny node.w = maxx - minx + 1 node.h = maxy - miny + 1 #Dig room coords = [] for x in range(minx, maxx + 1): # random 0-10 spaces, if > minx and < maxx + 1 # > miny and < maxy + 1 for y in range(miny, maxy + 1): if (x > minx and x < maxx + 1 and y > miny and y < maxy + 1): coords.append([x, y]) self.game.msg('clearspace', [x, y]) numWalls = randint(0, self.wall_peppering) wallCoords = [] for n in range(0, numWalls): randCoord = coords[randint(0, len(coords) - 1)] wallCoords.append(randCoord) for wallCoord in wallCoords: #self.game.msg('blockoff', wallCoord) tile = self.style[6] blocksight = tile['blocksight'] name = 'wall' if 'name' in tile.keys(): name = tile['name'] self.worldMap.makeWall(wallCoord[0], wallCoord[1], 6, blocksight, name) else: for i in range(randint(1, 3)): left = libtcod.bsp_left(node) right = libtcod.bsp_right(node) node.x = min(left.x, right.x) node.y = min(left.y, right.y) node.w = max(left.x + left.w, right.x + right.w) - node.x node.h = max(left.y + left.h, right.y + right.h) - node.y if node.horizontal: if left.x + left.w - 1 < right.x or right.x + right.w - 1 < left.x: x1 = libtcod.random_get_int(None, left.x, left.x + left.w - 1) x2 = libtcod.random_get_int(None, right.x, right.x + right.w - 1) y = libtcod.random_get_int(None, left.y + left.h, right.y) self.vline_up(x1, y - 1) self.hline(x1, y, x2) self.vline_down(x2, y + 1) else: minx = max(left.x, right.x) maxx = min(left.x + left.w - 1, right.x + right.w - 1) x = libtcod.random_get_int(None, minx, maxx) # catch out-of-bounds attempts while x > self.MAP_WIDTH - 3: x -= 1 self.vline_down(x, right.y) self.vline_up(x, right.y - 1) else: if left.y + left.h - 1 < right.y or right.y + right.h - 1 < left.y: y1 = libtcod.random_get_int(None, left.y, left.y + left.h - 1) y2 = libtcod.random_get_int(None, right.y, right.y + right.h - 1) x = libtcod.random_get_int(None, left.x + left.w, right.x) self.hline_left(x - 1, y1) self.vline(x, y1, y2) self.hline_right(x + 1, y2) else: miny = max(left.y, right.y) maxy = min(left.y + left.h - 1, right.y + right.h - 1) y = libtcod.random_get_int(None, miny, maxy) # catch out-of-bounds attempts while y > self.MAP_HEIGHT - 2: y -= 1 self.hline_left(right.x - 1, y) self.hline_right(right.x, y) return True
def traverse_node(self, node, _, entities, mmpr, mipr, min_size): # global rooms if tcod.bsp_is_leaf(node): minx = node.x + 1 maxx = node.x + node.w - 1 miny = node.y + 1 maxy = node.y + node.h - 1 if maxx == self.width - 1: maxx -= 1 if maxy == self.height - 1: maxy -= 1 if self.full_rooms is False: minx = tcod.random_get_int(None, minx, maxx - min_size + 1) miny = tcod.random_get_int(None, miny, maxy - min_size + 1) maxx = tcod.random_get_int(None, minx + min_size - 2, maxx) maxy = tcod.random_get_int(None, miny + min_size - 2, maxy) node.x = minx node.y = miny node.w = maxx - minx + 1 node.h = maxy - miny + 1 new_room = Room(node.x, node.y, node.w, node.h) for x in range(minx, maxx + 1): for y in range(miny, maxy + 1): self.tiles[x][y].blocked = False self.tiles[x][y].block_sight = False new_room.place_entities(entities, mmpr, mipr) # new_room = Rect(node.x, node.y, node.w, node.h) rooms.append(new_room) # print(new_room.room_type) # rooms.append(((minx + maxx) / 2, (miny + maxy) / 2)) else: left = tcod.bsp_left(node) right = tcod.bsp_right(node) node.x = min(left.x, right.x) node.y = min(left.y, right.y) node.w = max(left.x + left.w, right.x + right.w) - node.x node.h = max(left.y + left.h, right.y + right.h) - node.y if node.horizontal: if (left.x + left.w - 1 < right.x or right.x + right.w - 1 < left.x): x1 = tcod.random_get_int(None, left.x, left.x + left.w - 1) x2 = tcod.random_get_int(None, right.x, right.x + right.w - 1) y = tcod.random_get_int(None, left.y + left.h, right.y) self.vline_up(x1, y - 1) self.hline(x1, y, x2) self.vline_down(x2, y + 1) else: minx = max(left.x, right.x) maxx = min(left.x + left.w - 1, right.x + right.w - 1) x = tcod.random_get_int(None, minx, maxx) while x > self.width - 1: x -= 1 self.vline_down(x, right.y) self.vline_up(x, right.y - 1) else: if (left.y + left.h - 1 < right.y or right.y + right.h - 1 < left.y): y1 = tcod.random_get_int(None, left.y, left.y + left.h - 1) y2 = tcod.random_get_int(None, right.y, right.y + right.h - 1) x = tcod.random_get_int(None, left.x + left.w, right.x) self.hline_left(x - 1, y1) self.vline(x, y1, y2) self.hline_right(x + 1, y2) else: miny = max(left.y, right.y) maxy = min(left.y + left.h - 1, right.y + right.h - 1) y = tcod.random_get_int(None, miny, maxy) while y > self.height - 1: y -= 1 self.hline_left(right.x - 1, y) self.hline_right(right.x, y) return True