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