Refactor: Added print for the console

This commit is contained in:
NichiHachi
2024-06-13 16:38:51 +02:00
parent 528c0a2a30
commit 18ce4872dd
4 changed files with 22 additions and 18 deletions

View File

@@ -93,14 +93,12 @@ class City:
""" """
Loop the expansion of all districts in the city until all districts are fully expanded. Loop the expansion of all districts in the city until all districts are fully expanded.
""" """
loop_count = 0 print("[City] Start expanding districts...")
while not self.is_expend_finished(): while not self.is_expend_finished():
self.update_expend_district() self.update_expend_district()
loop_count += 1 print("[City] Finished expanding districts.")
if loop_count % 100 == 0:
print("[City] Loop count: ", loop_count)
def custom_district_draw_map(self): def district_draw_map(self):
""" """
Draw the map of the city with different colors for each district. Draw the map of the city with different colors for each district.
""" """
@@ -116,12 +114,13 @@ class City:
else: else:
img.putpixel((x, y), colors[self.map_data[y][x]]) img.putpixel((x, y), colors[self.map_data[y][x]])
img.save('./data/custom_district.png') img.save('./data/district.png')
print("[City] District map created.")
if __name__ == '__main__': if __name__ == '__main__':
city = City() city = City()
for i in range(10): for i in range(10):
city.add_district(Position(random.randint(0, 400), random.randint(0, 400))) city.add_district(Position(random.randint(0, 800), random.randint(0, 800)))
city.loop_expend_district() city.loop_expend_district()
city.custom_district_draw_map() city.district_draw_map()

View File

@@ -18,6 +18,7 @@ class Skeleton:
self.set_skeleton(data) self.set_skeleton(data)
def set_skeleton(self, data: np.ndarray): def set_skeleton(self, data: np.ndarray):
print("[Skeleton] Start skeletonization...")
binary_skeleton = skeletonize(data, method="lee") binary_skeleton = skeletonize(data, method="lee")
graph, coordinates = skeleton_to_csgraph(binary_skeleton) graph, coordinates = skeleton_to_csgraph(binary_skeleton)
@@ -33,6 +34,7 @@ class Skeleton:
for i in range(len(coordinates[0])): for i in range(len(coordinates[0])):
# print((coordinates[0][i], coordinates[1][i], coordinates[2][i])) # print((coordinates[0][i], coordinates[1][i], coordinates[2][i]))
self.coordinates.append((coordinates[0][i], coordinates[1][i], coordinates[2][i])) self.coordinates.append((coordinates[0][i], coordinates[1][i], coordinates[2][i]))
print("[Skeleton] Skeletonization completed.")
def find_next_elements(self, key: str) -> list: def find_next_elements(self, key: str) -> list:
"""Find the very nearest elements""" """Find the very nearest elements"""
@@ -85,6 +87,7 @@ class Skeleton:
return line return line
def parse_graph(self, parse_orphan: bool = False): def parse_graph(self, parse_orphan: bool = False):
print("[Skeleton] Start parsing the graph", ("with orphans" if parse_orphan else "") + "...")
for key, value in sorted( for key, value in sorted(
Counter(self.graph.row).items(), key=lambda kv: kv[1], reverse=True Counter(self.graph.row).items(), key=lambda kv: kv[1], reverse=True
): ):
@@ -136,6 +139,7 @@ class Skeleton:
if not already_inside: if not already_inside:
self.lines.append(line) self.lines.append(line)
print("[Skeleton] Graph parsing completed.")
def map(self) -> Image: def map(self) -> Image:
""" """
@@ -145,6 +149,7 @@ class Skeleton:
Returns: Returns:
image: 2D path of the skeleton on top of the heightmap. image: 2D path of the skeleton on top of the heightmap.
""" """
print("[Skeleton] Start mapping the skeleton...")
# editor = Editor() # editor = Editor()
# buildArea = editor.getBuildArea() # buildArea = editor.getBuildArea()
@@ -206,5 +211,5 @@ class Skeleton:
# (int(self.intersections[i][2]), int(self.intersections[i][0])), # (int(self.intersections[i][2]), int(self.intersections[i][0])),
# (255, 0, 255), # (255, 0, 255),
# ) # )
print("[Skeleton] Mapping completed.")
return heightmap # , roadsArea return heightmap # , roadsArea

View File

@@ -111,7 +111,7 @@ class World:
buildRect = buildArea.toRect() buildRect = buildArea.toRect()
xzStart = buildRect.begin xzStart = buildRect.begin
print(xzStart, "xzStart") print("[World]", '('+str(xzStart[0])+', '+str(xzStart[1])+')', "xzStart")
xzDistance = (max(buildRect.end[0], buildRect.begin[0]) - min(buildRect.end[0], buildRect.begin[0]), xzDistance = (max(buildRect.end[0], buildRect.begin[0]) - min(buildRect.end[0], buildRect.begin[0]),
max(buildRect.end[1], buildRect.begin[1]) - min(buildRect.end[1], buildRect.begin[1])) max(buildRect.end[1], buildRect.begin[1]) - min(buildRect.end[1], buildRect.begin[1]))
watermap = Image.new("L", xzDistance, 0) watermap = Image.new("L", xzDistance, 0)
@@ -125,12 +125,9 @@ class World:
for x in range(0, xzDistance[0]): for x in range(0, xzDistance[0]):
for z in range(0, xzDistance[1]): for z in range(0, xzDistance[1]):
y = heightmapData[x][z] - 1 y = heightmapData[x][z] - 1
yTree = treesmapData[x][z] - 1 yTree = treesmapData[x][z] - 1
print('getData', xzStart[0] + x, y, xzStart[1] + z)
biome = slice.getBiome((x, y, z)) biome = slice.getBiome((x, y, z))
block = slice.getBlock((x, y, z)) block = slice.getBlock((x, y, z))
maybeATree = slice.getBlock((x, yTree, z)) maybeATree = slice.getBlock((x, yTree, z))
@@ -149,7 +146,7 @@ class World:
if (0 <= x + i < xzDistance[0]) and (0 <= z + j < xzDistance[1]): if (0 <= x + i < xzDistance[0]) and (0 <= z + j < xzDistance[1]):
k = heightmapData[x + i][z + j] - 1 k = heightmapData[x + i][z + j] - 1
print('getData for tree', xzStart[0] + x + i, k, xzStart[1] + z + j) # print('getData for tree', xzStart[0] + x + i, k, xzStart[1] + z + j)
blockNeighbor = slice.getBlock((x + i, k, z + j)) blockNeighbor = slice.getBlock((x + i, k, z + j))
if blockNeighbor.id not in lookup.TREES: if blockNeighbor.id not in lookup.TREES:
@@ -157,7 +154,7 @@ class World:
number += 1 number += 1
if number != 0: if number != 0:
average = round(height / number) average = round(height / number)
print(average, "average") # print(average, "average")
heightmap.putpixel((x, z), (average, average, average)) heightmap.putpixel((x, z), (average, average, average))
if (biome in waterBiomes) or (block.id in waterBlocks): if (biome in waterBiomes) or (block.id in waterBlocks):
@@ -170,7 +167,6 @@ class World:
return heightmap, watermap, treesmap return heightmap, watermap, treesmap
def propagate(self, coordinates, scanned=[]): def propagate(self, coordinates, scanned=[]):
print('propagate', coordinates)
i = 0 i = 0
editor = Editor(buffering=True) editor = Editor(buffering=True)
if self.isInVolume(coordinates): if self.isInVolume(coordinates):

View File

@@ -7,10 +7,12 @@ from typing import Union
def get_data(world: World): def get_data(world: World):
print("[Data Analysis] Generating data...")
heightmap, watermap, treemap = world.getData() heightmap, watermap, treemap = world.getData()
heightmap.save('./data/heightmap.png') heightmap.save('./data/heightmap.png')
watermap.save('./data/watermap.png') watermap.save('./data/watermap.png')
treemap.save('./data/treemap.png') treemap.save('./data/treemap.png')
print("[Data Analysis] Data generated.")
return heightmap, watermap, treemap return heightmap, watermap, treemap
@@ -176,6 +178,7 @@ def filter_remove_details(image: Union[str, Image], n: int = 20) -> Image:
def highway_map() -> Image: def highway_map() -> Image:
print("[Data Analysis] Generating highway map...")
smooth_sobel = filter_smooth("./data/sobelmap.png", 1) smooth_sobel = filter_smooth("./data/sobelmap.png", 1)
negative_smooth_sobel = filter_negative(smooth_sobel) negative_smooth_sobel = filter_negative(smooth_sobel)
negative_smooth_sobel_water = subtract_map(negative_smooth_sobel, './data/watermap.png') negative_smooth_sobel_water = subtract_map(negative_smooth_sobel, './data/watermap.png')
@@ -188,6 +191,7 @@ def highway_map() -> Image:
image = Image.fromarray(array_sobel_water) image = Image.fromarray(array_sobel_water)
image_no_details = filter_remove_details(image, 15) image_no_details = filter_remove_details(image, 15)
image_no_details.save('./data/highwaymap.png') image_no_details.save('./data/highwaymap.png')
print("[Data Analysis] Highway map generated.")
return image_no_details return image_no_details
@@ -211,8 +215,8 @@ def convert_2D_to_3D(image: Union[str, Image], make_it_flat: bool = False) -> np
return volume return volume
def skeleton_highway_map(map: Union[str, Image]): def skeleton_highway_map(image: Union[str, Image] = './data/highwaymap.png'):
image_array = convert_2D_to_3D(map, True) image_array = convert_2D_to_3D(image, True)
skeleton = Skeleton(image_array) skeleton = Skeleton(image_array)
skeleton.parse_graph(True) skeleton.parse_graph(True)
heightmap_skeleton = skeleton.map() heightmap_skeleton = skeleton.map()