Code Duplication    Length = 75-75 lines in 2 locations

examples/samples_old.py 1 location

@@ 983-1057 (lines=75) @@
980
        x += 1
981
982
# the class building the dungeon from the bsp nodes
983
def traverse_node(node, *dat):
984
    global bsp_map
985
    if libtcod.bsp_is_leaf(node):
986
        # calculate the room size
987
        minx = node.x + 1
988
        maxx = node.x + node.w - 1
989
        miny = node.y + 1
990
        maxy = node.y + node.h - 1
991
        if not bsp_room_walls:
992
            if minx > 1:
993
                minx -= 1
994
            if miny > 1:
995
                miny -=1
996
        if maxx == SAMPLE_SCREEN_WIDTH - 1:
997
            maxx -= 1
998
        if maxy == SAMPLE_SCREEN_HEIGHT - 1:
999
            maxy -= 1
1000
        if bsp_random_room:
1001
            minx = libtcod.random_get_int(None, minx, maxx - bsp_min_room_size + 1)
1002
            miny = libtcod.random_get_int(None, miny, maxy - bsp_min_room_size + 1)
1003
            maxx = libtcod.random_get_int(None, minx + bsp_min_room_size - 1, maxx)
1004
            maxy = libtcod.random_get_int(None, miny + bsp_min_room_size - 1, maxy)
1005
        # resize the node to fit the room
1006
        node.x = minx
1007
        node.y = miny
1008
        node.w = maxx-minx + 1
1009
        node.h = maxy-miny + 1
1010
        # dig the room
1011
        for x in range(minx, maxx + 1):
1012
            for y in range(miny, maxy + 1):
1013
                bsp_map[x][y] = True
1014
    else:
1015
        # resize the node to fit its sons
1016
        left = libtcod.bsp_left(node)
1017
        right = libtcod.bsp_right(node)
1018
        node.x = min(left.x, right.x)
1019
        node.y = min(left.y, right.y)
1020
        node.w = max(left.x + left.w, right.x + right.w) - node.x
1021
        node.h = max(left.y + left.h, right.y + right.h) - node.y
1022
        # create a corridor between the two lower nodes
1023
        if node.horizontal:
1024
            # vertical corridor
1025
            if left.x + left.w - 1 < right.x or right.x + right.w - 1 < left.x:
1026
                # no overlapping zone. we need a Z shaped corridor
1027
                x1 = libtcod.random_get_int(None, left.x, left.x + left.w - 1)
1028
                x2 = libtcod.random_get_int(None, right.x, right.x + right.w - 1)
1029
                y = libtcod.random_get_int(None, left.y + left.h, right.y)
1030
                vline_up(bsp_map, x1, y - 1)
1031
                hline(bsp_map, x1, y, x2)
1032
                vline_down(bsp_map, x2, y + 1)
1033
            else:
1034
                # straight vertical corridor
1035
                minx = max(left.x, right.x)
1036
                maxx = min(left.x + left.w - 1, right.x + right.w - 1)
1037
                x = libtcod.random_get_int(None, minx, maxx)
1038
                vline_down(bsp_map, x, right.y)
1039
                vline_up(bsp_map, x, right.y - 1)
1040
        else:
1041
            # horizontal corridor
1042
            if left.y + left.h - 1 < right.y or right.y + right.h - 1 < left.y:
1043
                # no overlapping zone. we need a Z shaped corridor
1044
                y1 = libtcod.random_get_int(None, left.y, left.y + left.h - 1)
1045
                y2 = libtcod.random_get_int(None, right.y, right.y + right.h - 1)
1046
                x = libtcod.random_get_int(None, left.x + left.w, right.x)
1047
                hline_left(bsp_map, x - 1, y1)
1048
                vline(bsp_map, x, y1, y2)
1049
                hline_right(bsp_map, x + 1, y2)
1050
            else:
1051
                # straight horizontal corridor
1052
                miny = max(left.y, right.y)
1053
                maxy = min(left.y + left.h - 1, right.y + right.h - 1)
1054
                y = libtcod.random_get_int(None, miny, maxy)
1055
                hline_left(bsp_map, right.x - 1, y)
1056
                hline_right(bsp_map, right.x, y)
1057
    return True
1058
1059
bsp = None
1060
bsp_generate = True

examples/samples_py.py 1 location

@@ 825-899 (lines=75) @@
822
        x += 1
823
824
# the class building the dungeon from the bsp nodes
825
def traverse_node(node, *dat):
826
    global bsp_map
827
    if libtcod.bsp_is_leaf(node):
828
        # calculate the room size
829
        minx = node.x + 1
830
        maxx = node.x + node.w - 1
831
        miny = node.y + 1
832
        maxy = node.y + node.h - 1
833
        if not bsp_room_walls:
834
            if minx > 1:
835
                minx -= 1
836
            if miny > 1:
837
                miny -=1
838
        if maxx == SAMPLE_SCREEN_WIDTH - 1:
839
            maxx -= 1
840
        if maxy == SAMPLE_SCREEN_HEIGHT - 1:
841
            maxy -= 1
842
        if bsp_random_room:
843
            minx = libtcod.random_get_int(None, minx, maxx - bsp_min_room_size + 1)
844
            miny = libtcod.random_get_int(None, miny, maxy - bsp_min_room_size + 1)
845
            maxx = libtcod.random_get_int(None, minx + bsp_min_room_size - 1, maxx)
846
            maxy = libtcod.random_get_int(None, miny + bsp_min_room_size - 1, maxy)
847
        # resize the node to fit the room
848
        node.x = minx
849
        node.y = miny
850
        node.w = maxx-minx + 1
851
        node.h = maxy-miny + 1
852
        # dig the room
853
        for x in range(minx, maxx + 1):
854
            for y in range(miny, maxy + 1):
855
                bsp_map[x][y] = True
856
    else:
857
        # resize the node to fit its sons
858
        left = libtcod.bsp_left(node)
859
        right = libtcod.bsp_right(node)
860
        node.x = min(left.x, right.x)
861
        node.y = min(left.y, right.y)
862
        node.w = max(left.x + left.w, right.x + right.w) - node.x
863
        node.h = max(left.y + left.h, right.y + right.h) - node.y
864
        # create a corridor between the two lower nodes
865
        if node.horizontal:
866
            # vertical corridor
867
            if left.x + left.w - 1 < right.x or right.x + right.w - 1 < left.x:
868
                # no overlapping zone. we need a Z shaped corridor
869
                x1 = libtcod.random_get_int(None, left.x, left.x + left.w - 1)
870
                x2 = libtcod.random_get_int(None, right.x, right.x + right.w - 1)
871
                y = libtcod.random_get_int(None, left.y + left.h, right.y)
872
                vline_up(bsp_map, x1, y - 1)
873
                hline(bsp_map, x1, y, x2)
874
                vline_down(bsp_map, x2, y + 1)
875
            else:
876
                # straight vertical corridor
877
                minx = max(left.x, right.x)
878
                maxx = min(left.x + left.w - 1, right.x + right.w - 1)
879
                x = libtcod.random_get_int(None, minx, maxx)
880
                vline_down(bsp_map, x, right.y)
881
                vline_up(bsp_map, x, right.y - 1)
882
        else:
883
            # horizontal corridor
884
            if left.y + left.h - 1 < right.y or right.y + right.h - 1 < left.y:
885
                # no overlapping zone. we need a Z shaped corridor
886
                y1 = libtcod.random_get_int(None, left.y, left.y + left.h - 1)
887
                y2 = libtcod.random_get_int(None, right.y, right.y + right.h - 1)
888
                x = libtcod.random_get_int(None, left.x + left.w, right.x)
889
                hline_left(bsp_map, x - 1, y1)
890
                vline(bsp_map, x, y1, y2)
891
                hline_right(bsp_map, x + 1, y2)
892
            else:
893
                # straight horizontal corridor
894
                miny = max(left.y, right.y)
895
                maxy = min(left.y + left.h - 1, right.y + right.h - 1)
896
                y = libtcod.random_get_int(None, miny, maxy)
897
                hline_left(bsp_map, right.x - 1, y)
898
                hline_right(bsp_map, right.x, y)
899
    return True
900
901
bsp = None
902
bsp_generate = True