1- // Copyright 2024-2024 the openage authors. See copying.md for legal info.
1+ // Copyright 2024-2025 the openage authors. See copying.md for legal info.
22
33#include " demo_1.h"
44
@@ -30,29 +30,42 @@ void path_demo_1(const util::Path &path) {
3030 // Initialize the cost field for each sector.
3131 for (auto §or : grid->get_sectors ()) {
3232 auto cost_field = sector->get_cost_field ();
33- std::vector<cost_t > sector_cost = sectors_cost.at (sector->get_id ());
34- cost_field->set_costs (std::move (sector_cost));
33+
34+ // Read the data from the preconfigured table
35+ std::vector<cost_t > sector_cost = SECTORS_COST .at (sector->get_id ());
36+
37+ // Set the cost field for the sector
38+ cost_field->set_costs (std::move (sector_cost), time::TIME_MAX );
3539 }
3640
37- // Initialize portals between sectors.
41+ // Initialize portals between sectors
3842 util::Vector2s grid_size = grid->get_size ();
3943 portal_id_t portal_id = 0 ;
4044 for (size_t y = 0 ; y < grid_size[1 ]; y++) {
4145 for (size_t x = 0 ; x < grid_size[0 ]; x++) {
46+ // For each sector on the grid, we will seatch for portals to the east and south
47+ // sectors and connect them
48+
4249 auto sector = grid->get_sector (x, y);
4350
4451 if (x < grid_size[0 ] - 1 ) {
52+ // Check the sector to the east
4553 auto neighbor = grid->get_sector (x + 1 , y);
4654 auto portals = sector->find_portals (neighbor, PortalDirection::EAST_WEST , portal_id);
55+
56+ // Add the portals to the sector and the neighbor
4757 for (auto &portal : portals) {
4858 sector->add_portal (portal);
4959 neighbor->add_portal (portal);
5060 }
5161 portal_id += portals.size ();
5262 }
5363 if (y < grid_size[1 ] - 1 ) {
64+ // Check the sector to the south
5465 auto neighbor = grid->get_sector (x, y + 1 );
5566 auto portals = sector->find_portals (neighbor, PortalDirection::NORTH_SOUTH , portal_id);
67+
68+ // Add the portals to the sector and the neighbor
5669 for (auto &portal : portals) {
5770 sector->add_portal (portal);
5871 neighbor->add_portal (portal);
@@ -62,7 +75,8 @@ void path_demo_1(const util::Path &path) {
6275 }
6376 }
6477
65- // Connect portals inside sectors.
78+ // Connect portals that can mutually reach each other
79+ // within the same sector
6680 for (auto sector : grid->get_sectors ()) {
6781 sector->connect_exits ();
6882
@@ -81,19 +95,25 @@ void path_demo_1(const util::Path &path) {
8195 auto pathfinder = std::make_shared<path::Pathfinder>();
8296 pathfinder->add_grid (grid);
8397
98+ // Add a timer to measure the pathfinding speed
8499 util::Timer timer;
85100
86- // Create a path request and get the path
101+ // Create a path request from one end of the grid to the other
87102 coord::tile start{2 , 26 };
88103 coord::tile target{36 , 2 };
89-
90104 PathRequest path_request{
91105 grid->get_id (),
92106 start,
93107 target,
108+ time::TIME_ZERO ,
94109 };
110+
111+ // Initialize the portal nodes of the grid
112+ // This is used for the A* pathfinding search at the beginning
95113 grid->init_portal_nodes ();
114+
96115 timer.start ();
116+ // Let the pathfinder search for a path
97117 Path path_result = pathfinder->get_path (path_request);
98118 timer.stop ();
99119
@@ -109,8 +129,11 @@ void path_demo_1(const util::Path &path) {
109129 auto render_manager = std::make_shared<RenderManager1>(qtapp, window, path, grid);
110130 log::log (INFO << " Created render manager for pathfinding demo" );
111131
132+ // Callbacks for mouse button events
133+ // Used to set the start and target cells for pathfinding
112134 window->add_mouse_button_callback ([&](const QMouseEvent &ev) {
113135 if (ev.type () == QEvent::MouseButtonRelease) {
136+ // From the mouse position, calculate the position/cell on the grid
114137 auto cell_count_x = grid->get_size ()[0 ] * grid->get_sector_size ();
115138 auto cell_count_y = grid->get_size ()[1 ] * grid->get_sector_size ();
116139 auto window_size = window->get_size ();
@@ -127,6 +150,7 @@ void path_demo_1(const util::Path &path) {
127150 grid->get_id (),
128151 start,
129152 target,
153+ time::TIME_ZERO ,
130154 };
131155
132156 timer.reset ();
@@ -147,6 +171,7 @@ void path_demo_1(const util::Path &path) {
147171 grid->get_id (),
148172 start,
149173 target,
174+ time::TIME_ZERO ,
150175 };
151176
152177 timer.reset ();
0 commit comments