Merge remote-tracking branch 'origin/feat/graph-edition' into feat/graph-edition

# Conflicts:
#	src/editor.py
#	src/utils/.2024-06-27_21.01.45_Lycacraft_minecraft~overworld_day.png.icloud
#	src/utils/2024-06-27_21.01.45_Lycacraft_minecraft~overworld_day.png
This commit is contained in:
Louis Heredero 2024-07-05 22:24:59 +02:00
commit b2904c6b85
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
5 changed files with 538 additions and 159 deletions

262
save.txt Normal file
View File

@ -0,0 +1,262 @@
n -220 -374 3 Port Pickle
n -240 -519 3 Pickledill
n -266 -454 4 Pickledill Valley
n -125 -518 2 Pickledill Waterfall
n -224 -562 1 Yeti Cave
n -225 -352 1 Port Pickle Observatory
n -227 -364 1 Port Pickle Smithery
n -212 -372 1 Pickledill Ferry Port Pickle
n -212 -97 1 Pickledill Ferry Shopping District
n -70 -67 1 Witch Farm
n -70 -77 0
n -79 -77 0
n -86 -75 0
n -92 -71 0
n -108 -61 0
n -127 -55 0
n -151 -52 0
n -168 -55 0
n -188 -61 0
n -198 -71 0
n -208 -84 0
n -213 -376 0
n -208 -372 0
n -200 -371 0
n -193 -373 0
n -216 -379 0
n -222 -379 0
n -226 -372 0
n -222 -369 0
n -222 -364 0
n -221 -359 0
n -219 -353 0
n -220 -349 0
n -225 -348 0
n -257 -479 1 Pickledill Mill
n -383 -499 1 Pickledill Pass
n -398 -517 1 Pickledill Bridge
n -279 -524 1 Pickledill Portal
n -407 -17 1 Portail du Shopping District
n -390 -22 4 Shopping District
n -480 -56 3 Shopping District Ouest
n -396 -4 1 Slime Shop
n -387 -3 1 Slime Shop
n -383 -13 1 Sand Shop
n -394 -29 1 Magasin de Livres
n -407 -37 1 Magasin de la Mer
n -370 -10 1 Shop de Fer
n -362 -17 1 Living Is A Right
n -399 -17 3 Shopping District Centre
n -373 -77 1 Magasin de Glace
n -315 -91 1 Puits du Shopping District
n -335 -43 1 Nether Goods
n -680 -199 4 Lycapark
n -392 -10 0
n -397 -12 0
n -396 -5 0
n -401 -26 0
n -406 -33 0
n -412 -35 0
n -396 -23 0
n -392 -18 0
n -385 -17 0
n -375 -16 0
n -376 -6 0
n -374 -2 0
n -373 2 0
n -368 6 0
n -370 -16 0
n -367 -21 0
n -361 -24 0
n -354 -24 0
n -352 -31 0
n -348 -39 0
n -343 -44 0
n -350 -19 0
n -348 -14 0
n -341 -13 0
n -339 -17 0
n -336 -22 0
n -341 -25 0
n -345 -29 0
n -331 -16 0
n -328 -11 0
n -324 -7 0
n -320 -3 0
n -317 2 0
n -315 7 0
n -310 6 0
n -306 4 0
n -299 3 0
n -295 -4 0
n -296 -14 0
n -299 -23 0
n -304 -31 0
n -311 -28 0
n -319 -24 0
n -328 -22 0
n -299 -37 0
n -297 -41 0
n -297 -51 0
n -291 -61 0
n -296 -68 0
n -300 -64 0
n -304 -60 0
n -307 -58 0
n -315 -57 0
n -320 -53 0
n -324 -52 0
n -330 -53 0
n -337 -53 0
n -291 -72 0
n -286 -73 0
n -282 -76 0
n -279 -77 0
n -273 -83 0
n -269 -90 0
n -260 -92 0
n -253 -96 0
n -239 -96 0
n -231 -94 0
n -223 -97 0
n -419 -32 0
n -423 -29 0
n -428 -28 0
n -434 -26 0
n -435 -22 0
n -442 -17 0
n -446 -19 0
n -452 -21 0
n -457 -24 0
n -462 -26 0
n -465 -31 0
n -469 -37 0
n -471 -44 0
n -475 -51 0
n -286 -91 3 Shopping District Est
e 7 8 2
e 10 9 0
e 10 11 3
e 11 12 3
e 12 13 3
e 13 14 3
e 14 15 3
e 15 16 3
e 16 17 3
e 17 18 3
e 18 19 3
e 19 20 3
e 20 8 3
e 7 21 0
e 21 22 0
e 22 23 0
e 23 24 0
e 21 25 0
e 25 26 0
e 26 27 0
e 27 28 0
e 28 29 0
e 29 6 0
e 29 30 0
e 30 31 0
e 31 32 0
e 32 33 0
e 33 5 0
e 42 53 0
e 53 54 0
e 54 55 0
e 54 48 0
e 48 38 0
e 48 56 0
e 56 57 0
e 57 58 0
e 57 45 0
e 45 48 0
e 48 59 0
e 59 44 0
e 48 60 0
e 60 61 0
e 61 43 0
e 61 62 0
e 63 64 0
e 64 65 0
e 65 66 0
e 63 62 0
e 62 67 0
e 67 46 0
e 67 47 0
e 67 68 0
e 68 69 0
e 69 70 0
e 70 71 0
e 71 72 0
e 72 73 0
e 73 51 0
e 70 74 0
e 74 75 0
e 75 76 0
e 76 77 0
e 77 78 0
e 78 79 0
e 79 80 0
e 80 71 0
e 77 81 0
e 81 82 0
e 82 83 0
e 83 84 0
e 84 85 0
e 85 86 0
e 86 87 0
e 87 88 0
e 88 89 0
e 89 90 0
e 90 91 0
e 91 92 0
e 92 93 0
e 93 94 0
e 94 95 0
e 95 96 0
e 96 78 0
e 93 97 0
e 97 98 0
e 98 99 0
e 99 100 0
e 100 101 0
e 101 102 0
e 102 103 0
e 103 104 0
e 104 105 0
e 105 106 0
e 106 107 0
e 107 108 0
e 108 109 0
e 109 73 0
e 101 110 0
e 110 111 0
e 111 112 0
e 112 113 0
e 113 114 0
e 114 115 0
e 115 116 0
e 116 117 0
e 117 118 0
e 118 119 0
e 119 120 0
e 120 8 0
e 58 121 0
e 121 122 0
e 122 123 0
e 123 124 0
e 124 125 0
e 125 126 0
e 126 127 0
e 127 128 0
e 128 129 0
e 129 130 0
e 130 131 0
e 131 132 0
e 132 133 0
e 133 134 0
e 134 40 0
e 101 112 0

View File

@ -24,6 +24,22 @@ class Editor:
ZOOMS: tuple[float] = tuple(2**p for p in range(-6, 7))
CROSSHAIR_SIZE: int = 10
EDGE_TYPE_KEYS: dict[int, str] = {
pygame.K_p: "path",
pygame.K_n: "narrow_path",
pygame.K_f: "ferry",
pygame.K_b: "boat",
pygame.K_r: "rails"
}
NODE_TYPE_KEYS: list[int] = [
pygame.K_0,
pygame.K_1,
pygame.K_2,
pygame.K_3,
pygame.K_4
]
def __init__(self):
pygame.init()
self.config: Config = Config(self.CONFIG_PATH)
@ -70,6 +86,11 @@ class Editor:
elif self.config.LAST_OPENED_FILE != "":
self.load(self.config.LAST_OPENED_FILE)
# =========================
# EVENTS
# =========================
def mainloop(self) -> None:
self.state = State.LOADING
while self.state != State.STOPPING:
@ -144,6 +165,10 @@ class Editor:
self.center = [0, 0]
elif event.key == pygame.K_F5:
self.reload()
elif event.key in self.EDGE_TYPE_KEYS.keys():
self.change_edge_types(self.EDGE_TYPE_KEYS[event.key])
elif event.key in self.NODE_TYPE_KEYS:
self.change_node_types(self.NODE_TYPE_KEYS.index(event.key))
elif event.type == pygame.KEYUP:
if event.key == pygame.K_m:
if self.original_move_pos is not None:
@ -203,6 +228,11 @@ class Editor:
if mbtns[1]:
self.mid_drag_pos = mpos
# =========================
# RENDERING
# =========================
def render(self) -> None:
self.win.fill((50, 50, 50))
off_x = (self.center[0] * self.zoom) % self.MAP_SIZE
@ -320,52 +350,43 @@ class Editor:
def render_graph(self) -> None:
self.render_edges()
hover_index, is_node = self.get_hover_object()
if is_node:
self.render_nodes()
if self.selection_rectangle is None:
self.render_hover_node(hover_index)
else:
if self.selection_rectangle is None:
self.render_hover_edge(hover_index)
self.render_nodes()
def render_edges(self) -> None:
hover_index, is_node = self.get_hover_object()
for edge in self.graph.edges:
node_1, node_2 = self.graph.get_edge_nodes(edge)
color = (0, 255, 255) if edge.index in self.selected_edges else (255, 0, 0)
color = self.graph.TYPE_COLORS[edge.type]
start = self.world_to_screen(node_1.x, node_1.z)
end = self.world_to_screen(node_2.x, node_2.z)
if not is_node and edge.index == hover_index:
pygame.draw.line(self.win, (0, 0, 0), start, end, self.edge_detect_radius)
elif edge.index in self.selected_edges:
pygame.draw.line(self.win, (255, 255, 255), start, end, self.edge_detect_radius)
pygame.draw.line(self.win, color, start, end, self.line_size)
def render_nodes(self) -> None:
hover_index, is_node = self.get_hover_object()
for node in self.graph.nodes:
blitpos = self.world_to_screen(node.x, node.z)
pygame.draw.circle(self.win, (255, 0, 0), (blitpos[0], blitpos[1]), self.node_radius)
for node_index in self.selected_nodes:
node = self.graph.nodes[node_index]
blitpos = self.world_to_screen(node.x, node.z)
pygame.draw.circle(self.win, (0, 255, 255), (blitpos[0], blitpos[1]), self.node_radius)
color = self.graph.TYPE_COLORS[node.type]
if is_node and node.index == hover_index:
self.render_hover_node(node.index)
elif node.index in self.selected_nodes:
pygame.draw.circle(self.win, (255, 255, 255), (blitpos[0], blitpos[1]), self.node_radius + self.line_size)
pygame.draw.circle(self.win, color, (blitpos[0], blitpos[1]), self.node_radius)
def render_hover_node(self, node_index):
if node_index != -1:
def render_hover_node(self, node_index: int) -> None:
node = self.graph.nodes[node_index]
txt = self.loading_font.render(node.name, True, (0, 0, 0))
txt = self.loading_font.render(node.name, True, (255, 255, 255))
node_pos = self.world_to_screen(node.x, node.z)
self.win.blit(txt, [node_pos[0] - txt.get_width(), node_pos[1] - txt.get_height()])
pygame.draw.circle(self.win, (0, 0, 0), (node_pos[0], node_pos[1]), self.node_radius, self.line_size)
xpos = node_pos[0] - txt.get_width() - self.node_radius * (2 ** -0.5)
ypos = node_pos[1] - txt.get_height() - self.node_radius * (2 ** -0.5)
pygame.draw.rect(self.win, (0, 0, 0), pygame.Rect(xpos, ypos, txt.get_width(), txt.get_height()))
self.win.blit(txt, [xpos, ypos])
pygame.draw.circle(self.win, (0, 0, 0), (node_pos[0], node_pos[1]), self.node_radius + self.line_size)
def render_hover_edge(self, edge_index):
if edge_index != -1:
node_1, node_2 = self.graph.get_edge_nodes(self.graph.edges[edge_index])
start = self.world_to_screen(node_1.x, node_1.z)
end = self.world_to_screen(node_2.x, node_2.z)
pygame.draw.line(self.win, (0, 0, 0), start, end, self.edge_detect_radius)
color = (0, 255, 255) if edge_index in self.selected_edges else (255, 0, 0)
pygame.draw.line(self.win, color, start, end, self.line_size)
def render_selection_rect(self):
def render_selection_rect(self) -> None:
rect = self.selection_rectangle
if rect is not None:
left = min(rect[0][0], rect[1][0])
@ -374,6 +395,11 @@ class Editor:
height = abs(rect[0][1] - rect[1][1])
pygame.draw.rect(self.win, (32, 32, 32), pygame.Rect(left, top, width, height), self.line_size)
# =========================
# ZOOMING
# =========================
def set_zoom(self, zoom_i: int) -> None:
self.zoom_i = max(0, min(len(self.ZOOMS) - 1, zoom_i))
self.zoom = self.ZOOMS[self.zoom_i]
@ -384,21 +410,10 @@ class Editor:
def zoom_out(self) -> None:
self.set_zoom(self.zoom_i - 1)
def screen_to_world(self, x: int, y: int) -> tuple[int, int]:
w2 = self.width / 2
h2 = self.height / 2
world_x = floor((x - w2) / self.zoom + self.center[0])
world_z = floor((y - h2) / self.zoom + self.center[1])
return int(world_x), int(world_z)
def world_to_screen(self, world_x: int, world_z: int) -> tuple[int, int]:
w2 = self.width / 2
h2 = self.height / 2
screen_x = (world_x - self.center[0]) * self.zoom + w2
screen_y = (world_z - self.center[1]) * self.zoom + h2
return int(screen_x), int(screen_y)
# =========================
# SELECTION
# =========================
def select_object(self, shifting: bool = False) -> None:
hover_index, is_node = self.get_hover_object()
@ -448,31 +463,51 @@ class Editor:
self.selected_edges = [] if edge == -1 else [edge]
self.selected_nodes = []
def link_nodes(self, node_1: int, node_2: int) -> None:
if not self.graph.edge_exists(node_1, node_2):
self.create_edge(node_1, node_2)
def clear_selection(self) -> None:
self.selected_nodes = []
self.selected_edges = []
def rename_nodes(self) -> None:
self.dirty = True
for node in self.selected_nodes:
self.graph.nodes[node].rename_node(self.typing_text)
self.typing_text = ""
self.is_renaming_node = False
def clear_node_selection(self) -> None:
self.selected_nodes = []
def create_node(self, pos: tuple[int, int], typing_text: str = "") -> None:
self.dirty = True
self.graph.add_node(pos[0], pos[1], typing_text)
if len(self.selected_nodes) == 1:
self.previously_created_nodes.append(self.selected_nodes[0])
self.select_node(self.graph.number_of_nodes() - 1)
def clear_edge_selection(self) -> None:
self.selected_edges = []
def create_edge(self, node_1: int, node_2: int) -> None:
self.dirty = True
self.graph.add_edge(node_1, node_2)
def create_selection_rect(self, shifting: bool = False):
if not shifting:
self.clear_selection()
self.previously_created_nodes = []
mouse_pos = pygame.mouse.get_pos()
self.selection_rectangle = [mouse_pos, mouse_pos]
def expand_selection_rect(self):
self.selection_rectangle[1] = pygame.mouse.get_pos()
def release_selection_rect(self, shifting: bool = False):
if not shifting:
self.clear_selection()
self.previously_created_nodes = []
rect = self.selection_rectangle
left = min(rect[0][0], rect[1][0])
top = min(rect[0][1], rect[1][1])
right = max(rect[0][0], rect[1][0])
bottom = max(rect[0][1], rect[1][1])
for node in self.graph.nodes:
pos = self.world_to_screen(node.x, node.z)
if left <= pos[0] <= right and top <= pos[1] <= bottom:
if node.index not in self.selected_nodes:
self.selected_nodes.append(node.index)
for edge in self.graph.edges:
pos = self.world_to_screen(*self.graph.get_edge_center(edge.index))
if left <= pos[0] <= right and top <= pos[1] <= bottom:
if edge.index not in self.selected_edges:
self.selected_edges.append(edge.index)
self.selection_rectangle = None
# =========================
# HOVERING
# =========================
def get_hovering_nodes(self) -> tuple[list[int], list[float]]:
hovering = []
@ -519,82 +554,53 @@ class Editor:
return -1, False
def get_edge_distance(self, edge_i: int, px: int, pz: int) -> float:
start_n, end_n = self.graph.get_edge_nodes(self.graph.edges[edge_i])
start_p = self.world_to_screen(start_n.x, start_n.z)
end_p = self.world_to_screen(end_n.x, end_n.z)
edge_vec = (end_p[0] - start_p[0], end_p[1] - start_p[1])
start_vec = (px - start_p[0], pz - start_p[1])
# =========================
# CREATION
# =========================
edge_vec_len = (edge_vec[0] ** 2 + edge_vec[1] ** 2) ** 0.5
def link_nodes(self, node_1: int, node_2: int) -> None:
if not self.graph.edge_exists(node_1, node_2):
self.create_edge(node_1, node_2)
if edge_vec_len == 0:
return self.get_node_distance(start_n.index, px, pz)
scal_prod = start_vec[0] * edge_vec[0] + start_vec[1] * edge_vec[1]
proj_len = scal_prod / edge_vec_len
if proj_len < 0:
return self.get_node_distance(start_n.index, px, pz)
if proj_len > edge_vec_len:
return self.get_node_distance(end_n.index, px, pz)
return abs((edge_vec[0] * start_vec[1] - edge_vec[1] * start_vec[0]) / edge_vec_len)
def get_node_distance(self, node_i: int, px: int, pz: int) -> float:
node = self.graph.nodes[node_i]
node_pos = self.world_to_screen(node.x, node.z)
return ((px - node_pos[0]) ** 2 + (pz - node_pos[1]) ** 2) ** 0.5
def deleted_selected_objects(self):
def create_node(self, pos: tuple[int, int], typing_text: str = "") -> None:
self.dirty = True
edges_to_delete = [self.graph.edges[i] for i in self.selected_edges]
nodes_to_delete = [self.graph.nodes[i] for i in self.selected_nodes]
for edge in edges_to_delete:
self.graph.delete_edge(edge)
for node in nodes_to_delete:
self.graph.delete_node(node)
self.clear_selection()
n = len(self.previously_created_nodes)
if n != 0:
self.selected_nodes.append(self.previously_created_nodes[n - 1])
self.previously_created_nodes.pop()
self.graph.add_node(pos[0], pos[1], typing_text, type=self.get_node_type())
if len(self.selected_nodes) == 1:
self.previously_created_nodes.append(self.selected_nodes[0])
self.select_node(self.graph.number_of_nodes() - 1)
def create_selection_rect(self, shifting: bool = False):
if not shifting:
self.clear_selection()
self.previously_created_nodes = []
mouse_pos = pygame.mouse.get_pos()
self.selection_rectangle = [mouse_pos, mouse_pos]
def create_edge(self, node_1: int, node_2: int) -> None:
self.dirty = True
self.graph.add_edge(node_1, node_2, type=self.get_edge_type())
def expand_selection_rect(self):
self.selection_rectangle[1] = pygame.mouse.get_pos()
def get_edge_type(self) -> str:
type = ""
for key, value in self.EDGE_TYPE_KEYS.items():
if pygame.key.get_pressed()[key]:
type = value
break
return type
def release_selection_rect(self, shifting: bool = False):
if not shifting:
self.clear_selection()
self.previously_created_nodes = []
rect = self.selection_rectangle
left = min(rect[0][0], rect[1][0])
top = min(rect[0][1], rect[1][1])
right = max(rect[0][0], rect[1][0])
bottom = max(rect[0][1], rect[1][1])
for node in self.graph.nodes:
pos = self.world_to_screen(node.x, node.z)
if left <= pos[0] <= right and top <= pos[1] <= bottom:
if node.index not in self.selected_nodes:
self.selected_nodes.append(node.index)
print(left, "<=", pos[0], "<=", right)
print(top, "<=", pos[1], "<=", bottom)
for edge in self.graph.edges:
pos = self.world_to_screen(*self.graph.get_edge_center(edge.index))
if left <= pos[0] <= right and top <= pos[1] <= bottom:
if edge.index not in self.selected_edges:
self.selected_edges.append(edge.index)
print(left, "<=", pos[0], "<=", right)
print(top, "<=", pos[1], "<=", bottom)
self.selection_rectangle = None
def get_node_type(self) -> int:
type = 0
for i in range(len(self.NODE_TYPE_KEYS)):
if pygame.key.get_pressed()[self.NODE_TYPE_KEYS[i]]:
type = i
break
return type
# =========================
# EDITION
# =========================
def rename_nodes(self) -> None:
self.dirty = True
for node in self.selected_nodes:
self.graph.nodes[node].rename_node(self.typing_text)
self.typing_text = ""
self.is_renaming_node = False
def start_moving(self):
self.move_old_poses = {}
@ -627,6 +633,84 @@ class Editor:
self.original_move_pos = None
self.move_old_poses = None
def change_edge_types(self, type: str = "path"):
self.dirty = True
for edge in self.selected_edges:
self.graph.set_edge_type(edge, type)
def change_node_types(self, type: int = 0):
self.dirty = True
for node_index in self.selected_nodes:
node = self.graph.nodes[node_index]
node.set_type(type)
def deleted_selected_objects(self):
self.dirty = True
edges_to_delete = [self.graph.edges[i] for i in self.selected_edges]
nodes_to_delete = [self.graph.nodes[i] for i in self.selected_nodes]
for edge in edges_to_delete:
self.graph.delete_edge(edge)
for node in nodes_to_delete:
self.graph.delete_node(node)
self.clear_selection()
n = len(self.previously_created_nodes)
if n != 0:
self.selected_nodes.append(self.previously_created_nodes[n - 1])
self.previously_created_nodes.pop()
# =========================
# CALCULATION
# =========================
def screen_to_world(self, x: int, y: int) -> tuple[int, int]:
w2 = self.width / 2
h2 = self.height / 2
world_x = floor((x - w2) / self.zoom + self.center[0])
world_z = floor((y - h2) / self.zoom + self.center[1])
return int(world_x), int(world_z)
def world_to_screen(self, world_x: int, world_z: int) -> tuple[int, int]:
w2 = self.width / 2
h2 = self.height / 2
screen_x = (world_x - self.center[0]) * self.zoom + w2
screen_y = (world_z - self.center[1]) * self.zoom + h2
return int(screen_x), int(screen_y)
def get_edge_distance(self, edge_i: int, px: int, pz: int) -> float:
start_n, end_n = self.graph.get_edge_nodes(self.graph.edges[edge_i])
start_p = self.world_to_screen(start_n.x, start_n.z)
end_p = self.world_to_screen(end_n.x, end_n.z)
edge_vec = (end_p[0] - start_p[0], end_p[1] - start_p[1])
start_vec = (px - start_p[0], pz - start_p[1])
edge_vec_len = (edge_vec[0] ** 2 + edge_vec[1] ** 2) ** 0.5
if edge_vec_len == 0:
return self.get_node_distance(start_n.index, px, pz)
scal_prod = start_vec[0] * edge_vec[0] + start_vec[1] * edge_vec[1]
proj_len = scal_prod / edge_vec_len
if proj_len < 0:
return self.get_node_distance(start_n.index, px, pz)
if proj_len > edge_vec_len:
return self.get_node_distance(end_n.index, px, pz)
return abs((edge_vec[0] * start_vec[1] - edge_vec[1] * start_vec[0]) / edge_vec_len)
def get_node_distance(self, node_i: int, px: int, pz: int) -> float:
node = self.graph.nodes[node_i]
node_pos = self.world_to_screen(node.x, node.z)
return ((px - node_pos[0]) ** 2 + (pz - node_pos[1]) ** 2) ** 0.5
# SAVING
def save(self, path: Optional[str] = None, save_config: bool = True) -> None:
last_path = self.config.LAST_OPENED_FILE
if path is None:

View File

@ -1,6 +1,8 @@
class Edge:
def __init__(self, start: int, end: int, length: float, index: int):
def __init__(self, start: int, end: int, length: float, index: int, type: int):
self.length: float = length
self.start: int = start
self.end: int = end
self.index: int = index
self.type: int = type

View File

@ -7,19 +7,37 @@ from src.graph.edge import Edge
class Graph:
EDGE_TYPES: list[str] = [
"path",
"narrow_path",
"ferry",
"boat",
"rails"
]
TYPE_COLORS: list[tuple[int, int, int]] = [
(255, 0, 0),
(255, 0, 255),
(0, 255, 0),
(255, 255, 0),
(0, 0, 255)
]
def __init__(self):
self.edges: list[Edge] = []
self.nodes: list[Node] = []
def add_node(self, x: int, z: int, name: str = "") -> None:
self.nodes.append(Node(x, z, len(self.nodes), name))
def add_node(self, x: int, z: int, name: str = "", type: int = 0) -> None:
self.nodes.append(Node(x, z, len(self.nodes), name, type))
def add_edge(self, start_index: int, end_index: int, auto_length: bool = True) -> None:
def add_edge(self, start_index: int, end_index: int, type: str = "path", auto_length: bool = True) -> None:
length = 0
if auto_length:
n1, n2 = self.nodes[start_index], self.nodes[end_index]
length = sqrt((n1.x - n2.x)**2 + (n1.z - n2.z)**2)
self.edges.append(Edge(start_index, end_index, length, len(self.edges)))
type_n = 0 if type not in self.EDGE_TYPES else self.EDGE_TYPES.index(type)
self.edges.append(Edge(start_index, end_index, length, len(self.edges), type_n))
def delete_edge(self, edge: Edge) -> None:
self.edges.remove(edge)
@ -72,6 +90,10 @@ class Graph:
def edge_exists(self, node_1: int, node_2: int) -> bool:
return self.get_edge(node_1, node_2) != -1
def set_edge_type(self, edge_index: int, type: str = "path") -> None:
edge = self.edges[edge_index]
edge.type = 0 if type not in self.EDGE_TYPES else self.EDGE_TYPES.index(type)
def dijkstra(self, source_index: int, target_index: int) -> Optional[list[int]]:
n = len(self.nodes)
@ -111,10 +133,10 @@ class Graph:
def save(self, path: str) -> None:
with open(path, "w") as f:
for node in self.nodes:
f.write(f"n {node.x} {node.z} {node.name}\n")
f.write(f"n {node.x} {node.z} {node.type} {node.name}\n")
f.write("\n")
for edge in self.edges:
f.write(f"e {edge.start} {edge.end}\n")
f.write(f"e {edge.start} {edge.end} {edge.type}\n")
@staticmethod
def load(path: str) -> Graph:
@ -127,13 +149,13 @@ class Graph:
entry_type, values = line.split(" ", 1)
if entry_type == "n":
x, z, name = values.split(" ", 2)
x, z = int(x), int(z)
graph.add_node(x, z, name)
x, z, type, name = values.split(" ", 3)
x, z, type = int(x), int(z), int(type)
graph.add_node(x, z, name, type)
elif entry_type == "e":
start, end = values.split(" ", 2)
start, end = int(start), int(end)
graph.add_edge(start, end, False)
start, end, type = values.split(" ", 2)
start, end, type = int(start), int(end), int(type)
graph.add_edge(start, end, auto_length=False, type=graph.EDGE_TYPES[type])
graph.recompute_lengths()
return graph

View File

@ -1,9 +1,18 @@
class Node:
def __init__(self, x: int, z: int, index: int, name: str = ""):
def __init__(self, x: int, z: int, index: int, name: str = "", type: int = 0):
self.x: int = x
self.z: int = z
self.index: int = index
self.name: str = name
self.type: int = (0 if name == "" else type)
def rename_node(self, name: str):
def rename_node(self, name: str) -> None:
if self.name == "" and name != "":
self.type = 1
self.name = name
if name == "":
self.type = 0
def set_type(self, type: int = 0) -> None:
if type != 0:
self.type = 0 if self.name == "" else type