diff --git a/buildings/Building.py b/buildings/Building.py index b42adc9..492054e 100644 --- a/buildings/Building.py +++ b/buildings/Building.py @@ -6,44 +6,53 @@ from buildings.Facade import Facade from buildings.Entrance import Entrance from buildings.Roof import Roof + class Building: - def __init__(self,rdata, positions : list[tuple[int,int,int]], matrice : list[list[int]], doors_direction : DIRECTION): - self.position = (0,0,0) - self.length, self.width, self.height = 0,0,0 + def __init__(self, rdata, positions: list[tuple[int, int, int]], matrice: list[list[int]], doors_direction: DIRECTION): + self.position = (0, 0, 0) + self.length, self.width, self.height = 0, 0, 0 self.matrice = matrice - + self.get_pos_and_size(positions) tile_size = self.gen_tile_size() - - self.foundations = Foundations(rdata["foundations"], (self.length,self.width), matrice, tile_size,) - self.facade = Facade(rdata["facade"], self.foundations.vertices, self.foundations.is_inner_or_outer) - self.entrance = Entrance(rdata, self.foundations.vertices, doors_direction, self.foundations.is_inner_or_outer) + + self.foundations = Foundations( + rdata["foundations"], (self.length, self.width), matrice, tile_size,) + self.facade = Facade( + rdata["facade"], self.foundations.vertices, self.foundations.is_inner_or_outer) + self.entrance = Entrance(rdata, self.foundations.vertices, + doors_direction, self.foundations.is_inner_or_outer) self.roof = Roof(rdata["roof"], self.foundations.polygon) - - def build(self, editor : Editor, materials : list[str]): - y=0 - while y < self.height: - with editor.pushTransform((self.position[0], y -1, self.position[1])): + + def build(self, editor: Editor, materials: list[str]): + y = self.position[1] + while y < self.position[1] + self.height: + with editor.pushTransform((self.position[0], y - 1, self.position[2])): self.foundations.build(editor, materials) - if y == 0: self.entrance.build(editor, materials) - else : self.facade.build(editor, materials) - y+=self.foundations.floor_height+1 - with editor.pushTransform((self.position[0], y -1, self.position[1])): self.roof.build(editor, materials) - + if y == 0: + self.entrance.build(editor, materials) + else: + self.facade.build(editor, materials) + y += self.foundations.floor_height+1 + with editor.pushTransform((self.position[0], y - 1, self.position[2])): + self.roof.build(editor, materials) + def gen_tile_size(self) -> int: # Tiles are constant square units different for each buildings smaller_side = min(self.length, self.width) - + # area is too small, will work but not very well - if smaller_side <= 5 : return smaller_side - if smaller_side <= 15 : return smaller_side // 5 - + if smaller_side <= 5: + return smaller_side + if smaller_side <= 15: + return smaller_side // 5 + return rd.randint(3, smaller_side // len(self.matrice)) - - def get_pos_and_size(self, pos : list[tuple[int,int,int]]) -> tuple[tuple[int,int],int,int]: + + def get_pos_and_size(self, pos: list[tuple[int, int, int]]) -> tuple[tuple[int, int], int, int]: pos1, pos2 = pos[0], pos[1] - self.position = (min(pos1[0], pos2[0]), min(pos1[1], pos2[1]), min(pos1[2], pos2[2])) + self.position = (min(pos1[0], pos2[0]), min( + pos1[1], pos2[1]), min(pos1[2], pos2[2])) self.length = abs(pos1[0] - pos2[0]) self.height = abs(pos1[1] - pos2[1]) self.width = abs(pos1[2] - pos2[2]) - \ No newline at end of file diff --git a/main.py b/main.py index 7e6688f..823e1c1 100644 --- a/main.py +++ b/main.py @@ -22,11 +22,15 @@ from buildings.Building import Building from utils.functions import * from utils.Enums import DIRECTION +import time def main(): + start_time_all = time.time() + start_time = time.time() rectangle_house_mountain, rectangle_building, skeleton_highway, skeleton_mountain, road_grid = world_maker() - + time_world_maker = time.time() - start_time + print(f"[TIME] World_maker {time_world_maker}") editor = Editor(buffering=True) buildArea = editor.getBuildArea() origin = ((buildArea.begin).x, (buildArea.begin).z) @@ -34,13 +38,23 @@ def main(): abs(buildArea.begin.z - buildArea.end.z) / 2) length_world = sqrt((center[0]*2) ** 2 + (center[1]*2) ** 2) - # remove_trees('./world_maker/data/heightmap.png', './world_maker/data/treemap.png', - # './world_maker/data/smooth_sobel_watermap.png') - # smooth_terrain('./world_maker/data/heightmap.png', - # './world_maker/data/heightmap_smooth.png', './world_maker/data/smooth_sobel_watermap.png') + start_time = time.time() + remove_trees('./world_maker/data/heightmap.png', './world_maker/data/treemap.png', + './world_maker/data/smooth_sobel_watermap.png') + time_remove_tree = time.time() - start_time + print(f"[TIME] Remove tree {time_remove_tree}") - # set_roads(skeleton_highway, origin) - # set_roads(skeleton_mountain, origin) + start_time = time.time() + smooth_terrain('./world_maker/data/heightmap.png', + './world_maker/data/heightmap_smooth.png', './world_maker/data/smooth_sobel_watermap.png') + time_smooth_terrain = time.time() - start_time + print(f"[TIME] Smooth terrain {time_smooth_terrain}") + + start_time = time.time() + set_roads(skeleton_highway, origin) + set_roads(skeleton_mountain, origin) + time_roads = time.time() - start_time + print(f"[TIME] Roads {time_roads}") # set_roads_grids(road_grid, origin) # roads.setRoads(skeleton_mountain) # roads.setRoads(skeleton_highway) @@ -75,29 +89,45 @@ def main(): # build it with your custom materials + start_time = time.time() for buildings in rectangle_building: height = get_height_building_from_center( center, (buildings[0][0], buildings[0][2]), length_world) start = (min(buildings[0][0], buildings[1][0]) + origin[0], buildings[0] - [1]+height, min(buildings[0][2], buildings[1][2]) + origin[1]) + [1], min(buildings[0][2], buildings[1][2]) + origin[1]) end = (max(buildings[0][0], buildings[1][0]) + origin[0], buildings[1] - [1], max(buildings[0][2], buildings[1][2]) + origin[1]) + [1]+height, max(buildings[0][2], buildings[1][2]) + origin[1]) - print("---", start, end) building = Building(random_data["buildings"], [ start, end], baseShape, DIRECTION.EAST) building.build(editor, ["stone_bricks", "glass_pane", "glass", "cobblestone_wall", "stone_brick_stairs", "oak_planks", "white_concrete", "cobblestone", "stone_brick_slab", "iron_bars"]) - # for buildings in rectangle_house_mountain: - # start = (buildings[0][0] + origin[0], buildings[0] - # [1], buildings[0][2] + origin[1]) - # end = (buildings[1][0] + origin[0], buildings[1] - # [1], buildings[1][2] + origin[1]) - # house = House(editor, start, end, - # entranceDirection[random.randint(0, 3)], blocks) - # house.build() + time_buildings = time.time() - start_time + print(f"[TIME] Buildings {time_buildings}") + + start_time = time.time() + for buildings in rectangle_house_mountain: + start = (buildings[0][0] + origin[0], buildings[0] + [1], buildings[0][2] + origin[1]) + end = (buildings[1][0] + origin[0], buildings[1] + [1], buildings[1][2] + origin[1]) + house = House(editor, start, end, + entranceDirection[random.randint(0, 3)], blocks) + house.build() + time_houses = time.time() - start_time + print(f"[TIME] Houses {time_houses}") + + print("[GDMC] Done!\n\n") + + print(f"[TIME] Total {time.time() - start_time_all}") + print(f"[TIME] World_maker {time_world_maker}") + print(f"[TIME] Remove tree {time_remove_tree}") + print(f"[TIME] Smooth terrain {time_smooth_terrain}") + print(f"[TIME] Roads {time_roads}") + print(f"[TIME] Buildings {time_buildings}") + print(f"[TIME] Houses {time_houses}") def get_height_building_from_center(center, position, length_world): @@ -133,7 +163,7 @@ def set_roads(skeleton: Skeleton, origin): xyz = transpose_form_heightmap('./world_maker/data/heightmap.png', skeleton.coordinates[skeleton.lines[i][j]], origin) heightmap_smooth = Image.open( - './world_maker/data/full_road_heightmap_smooth.png') + './world_maker/data/road_heightmap.png') skeleton.lines[i][j] = [xyz[0], heightmap_smooth.getpixel( (skeleton.coordinates[skeleton.lines[i][j]][0], skeleton.coordinates[skeleton.lines[i][j]][-1])), xyz[2]] diff --git a/networks/roads_2/Road.py b/networks/roads_2/Road.py index 57fbff1..da3d1c6 100644 --- a/networks/roads_2/Road.py +++ b/networks/roads_2/Road.py @@ -155,7 +155,6 @@ class Road: for j in range(len(circle_list)): if j != middle_lane_index and len(circle_list[j]) > 0: - print(len(circle_list[j]), circle_list[j]) circle_list[j] = circle_list[j][0].optimized_path( circle_list[j]) if len(circle_list[j]) != 1: diff --git a/world_maker/data/building.png b/world_maker/data/building.png index ff5d4a3..0d216d4 100644 Binary files a/world_maker/data/building.png and b/world_maker/data/building.png differ diff --git a/world_maker/data/building_moutain.png b/world_maker/data/building_moutain.png index 3166955..34d758d 100644 Binary files a/world_maker/data/building_moutain.png and b/world_maker/data/building_moutain.png differ diff --git a/world_maker/data/city_map.png b/world_maker/data/city_map.png index 63fbcbe..881bd6d 100644 Binary files a/world_maker/data/city_map.png and b/world_maker/data/city_map.png differ diff --git a/world_maker/data/district.png b/world_maker/data/district.png index 60dd8e7..a282982 100644 Binary files a/world_maker/data/district.png and b/world_maker/data/district.png differ diff --git a/world_maker/data/full_road.png b/world_maker/data/full_road.png index aca0905..22ca0e8 100644 Binary files a/world_maker/data/full_road.png and b/world_maker/data/full_road.png differ diff --git a/world_maker/data/full_road_heightmap.png b/world_maker/data/full_road_heightmap.png deleted file mode 100644 index bf794f4..0000000 Binary files a/world_maker/data/full_road_heightmap.png and /dev/null differ diff --git a/world_maker/data/full_road_heightmap_smooth.png b/world_maker/data/full_road_heightmap_smooth.png deleted file mode 100644 index eeed31c..0000000 Binary files a/world_maker/data/full_road_heightmap_smooth.png and /dev/null differ diff --git a/world_maker/data/heightmap.png b/world_maker/data/heightmap.png index 22adcdc..5462e0b 100644 Binary files a/world_maker/data/heightmap.png and b/world_maker/data/heightmap.png differ diff --git a/world_maker/data/heightmap_smooth.png b/world_maker/data/heightmap_smooth.png index e31fee8..7c15750 100644 Binary files a/world_maker/data/heightmap_smooth.png and b/world_maker/data/heightmap_smooth.png differ diff --git a/world_maker/data/heightmap_smooth_2.png b/world_maker/data/heightmap_smooth_2.png index 9af61c1..a45ac1f 100644 Binary files a/world_maker/data/heightmap_smooth_2.png and b/world_maker/data/heightmap_smooth_2.png differ diff --git a/world_maker/data/heightmap_with_building.png b/world_maker/data/heightmap_with_building.png index 2a0f40e..8841edd 100644 Binary files a/world_maker/data/heightmap_with_building.png and b/world_maker/data/heightmap_with_building.png differ diff --git a/world_maker/data/mountain_map.png b/world_maker/data/mountain_map.png index 5e0bf75..7d34f62 100644 Binary files a/world_maker/data/mountain_map.png and b/world_maker/data/mountain_map.png differ diff --git a/world_maker/data/removed_treesmap.png b/world_maker/data/removed_treesmap.png index 1d91fc2..807feef 100644 Binary files a/world_maker/data/removed_treesmap.png and b/world_maker/data/removed_treesmap.png differ diff --git a/world_maker/data/road_heightmap.png b/world_maker/data/road_heightmap.png index 3a590f5..c356acd 100644 Binary files a/world_maker/data/road_heightmap.png and b/world_maker/data/road_heightmap.png differ diff --git a/world_maker/data/roadmap.png b/world_maker/data/roadmap.png index f700c13..3dd8b4a 100644 Binary files a/world_maker/data/roadmap.png and b/world_maker/data/roadmap.png differ diff --git a/world_maker/data/skeleton_highway.png b/world_maker/data/skeleton_highway.png index 4065533..ae71841 100644 Binary files a/world_maker/data/skeleton_highway.png and b/world_maker/data/skeleton_highway.png differ diff --git a/world_maker/data/skeleton_highway_area.png b/world_maker/data/skeleton_highway_area.png index e02335c..947d412 100644 Binary files a/world_maker/data/skeleton_highway_area.png and b/world_maker/data/skeleton_highway_area.png differ diff --git a/world_maker/data/skeleton_mountain.png b/world_maker/data/skeleton_mountain.png index 489f968..8b079b0 100644 Binary files a/world_maker/data/skeleton_mountain.png and b/world_maker/data/skeleton_mountain.png differ diff --git a/world_maker/data/skeleton_mountain_area.png b/world_maker/data/skeleton_mountain_area.png index eaf6e02..726b7c9 100644 Binary files a/world_maker/data/skeleton_mountain_area.png and b/world_maker/data/skeleton_mountain_area.png differ diff --git a/world_maker/data/smooth_for_highway.png b/world_maker/data/smooth_for_highway.png index 6f4f073..d39efaa 100644 Binary files a/world_maker/data/smooth_for_highway.png and b/world_maker/data/smooth_for_highway.png differ diff --git a/world_maker/data/smooth_sobel_watermap.png b/world_maker/data/smooth_sobel_watermap.png index ce4ae43..4adddc8 100644 Binary files a/world_maker/data/smooth_sobel_watermap.png and b/world_maker/data/smooth_sobel_watermap.png differ diff --git a/world_maker/data/smooth_terrain_delta.png b/world_maker/data/smooth_terrain_delta.png index b5eed7b..26e1e68 100644 Binary files a/world_maker/data/smooth_terrain_delta.png and b/world_maker/data/smooth_terrain_delta.png differ diff --git a/world_maker/data/sobelmap.png b/world_maker/data/sobelmap.png index 93a42cc..aeb5592 100644 Binary files a/world_maker/data/sobelmap.png and b/world_maker/data/sobelmap.png differ diff --git a/world_maker/data/sobelmap_from_smooth.png b/world_maker/data/sobelmap_from_smooth.png index a4f773e..9aaaf2e 100644 Binary files a/world_maker/data/sobelmap_from_smooth.png and b/world_maker/data/sobelmap_from_smooth.png differ diff --git a/world_maker/data/treemap.png b/world_maker/data/treemap.png index db2d51c..9d03312 100644 Binary files a/world_maker/data/treemap.png and b/world_maker/data/treemap.png differ diff --git a/world_maker/data/watermap.png b/world_maker/data/watermap.png index df6e299..8051103 100644 Binary files a/world_maker/data/watermap.png and b/world_maker/data/watermap.png differ diff --git a/world_maker/pack_rectangle.py b/world_maker/pack_rectangle.py index 579a416..24cd6bf 100644 --- a/world_maker/pack_rectangle.py +++ b/world_maker/pack_rectangle.py @@ -67,6 +67,7 @@ def pack_rectangles(grid, min_width: int = 10, max_width: int = 25): bin = Bin(grid) while True: rectangle = generate_rectangle(min_width, max_width) + print(f"[Pack rectangles] Number of rectangles: {len(bin.rectangles)}") if not bin.place_rectangle(rectangle): break return bin.rectangles @@ -97,7 +98,7 @@ def area_of_rectangles(rectangles): def generate_building(image: str | Image.Image, heightmap: str | Image.Image, output: str = './world_maker/data/building.png', - number_of_try: int = 3, min_width: int = 10, max_width: int = 25): + number_of_try: int = 1, min_width: int = 10, max_width: int = 25): print("[Building] Start generating building position...") image = handle_import_image(image).convert('L') rectangles_output = [] diff --git a/world_maker/world_maker.py b/world_maker/world_maker.py index d954c3a..d37343f 100644 --- a/world_maker/world_maker.py +++ b/world_maker/world_maker.py @@ -46,7 +46,7 @@ def world_maker(): './world_maker/data/mountain_map.png').save('./world_maker/data/city_map.png') rectangle_building = generate_building( - './world_maker/data/city_map.png', './world_maker/data/heightmap.png', output='./world_maker/data/building.png', min_width=16, max_width=30) + './world_maker/data/city_map.png', './world_maker/data/heightmap.png', output='./world_maker/data/building.png', min_width=30, max_width=40) rectangle_building = rectangle_2D_to_3D(rectangle_building) # Houses