Skip to content

Commit

Permalink
path: Separate grid into sectors.
Browse files Browse the repository at this point in the history
  • Loading branch information
heinezen committed Mar 17, 2024
1 parent 9981a7a commit 6b71548
Show file tree
Hide file tree
Showing 8 changed files with 341 additions and 55 deletions.
1 change: 1 addition & 0 deletions libopenage/pathfinding/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ add_sources(libopenage
integrator.cpp
path.cpp
portal.cpp
sector.cpp
tests.cpp
types.cpp
)
Expand Down
30 changes: 25 additions & 5 deletions libopenage/pathfinding/grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,39 @@

#include "grid.h"

#include "pathfinding/sector.h"


namespace openage::path {

CostGrid::CostGrid(size_t width, size_t height, size_t field_size) :
width{width}, height{height}, fields{width * height, CostField{field_size}} {
Grid::Grid(size_t width, size_t height, size_t sector_size) :
width{width},
height{height} {
for (size_t y = 0; y < height; y++) {
for (size_t x = 0; x < width; x++) {
this->sectors.push_back(std::make_shared<Sector>(x + y * width, sector_size));
}
}
}

Grid::Grid(size_t width,
size_t height,
std::vector<std::shared_ptr<Sector>> &&sectors) :
width{width},
height{height},
sectors{std::move(sectors)} {
}

std::pair<size_t, size_t> CostGrid::get_size() const {
std::pair<size_t, size_t> Grid::get_size() const {
return {this->width, this->height};
}

CostField &CostGrid::get_field(size_t x, size_t y) {
return this->fields[y * this->width + x];
const std::shared_ptr<Sector> &Grid::get_sector(size_t x, size_t y) {
return this->sectors.at(x + y * this->width);
}

const std::shared_ptr<Sector> &Grid::get_sector(sector_id_t id) const {
return this->sectors.at(id);
}

} // namespace openage::path
49 changes: 36 additions & 13 deletions libopenage/pathfinding/grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,42 @@
#pragma once

#include <cstddef>
#include <memory>
#include <utility>
#include <vector>

#include "pathfinding/cost_field.h"
#include "pathfinding/types.h"


namespace openage::path {
class Sector;

/**
* Grid of cost fields for flow field pathfinding.
* Grid for flow field pathfinding.
*/
class CostGrid {
class Grid {
public:
/**
* Create a grid with a specified size and field size.
* Create a new empty grid of width x height sectors with a specified size.
*
* @param width Width of the grid.
* @param height Height of the grid.
* @param field_size Size of the cost fields.
* @param sector_size Side length of each sector.
*/
CostGrid(size_t width,
size_t height,
size_t field_size);
Grid(size_t width,
size_t height,
size_t sector_size);

/**
* Create a grid of width x height sectors from a list of existing sectors.
*
* @param width Width of the grid.
* @param height Height of the grid.
* @param sectors Existing sectors.
*/
Grid(size_t width,
size_t height,
std::vector<std::shared_ptr<Sector>> &&sectors);

/**
* Get the size of the grid.
Expand All @@ -35,13 +48,23 @@ class CostGrid {
std::pair<size_t, size_t> get_size() const;

/**
* Get the cost field at a specified position.
* Get the sector at a specified position.
*
* @param x X coordinate.
* @param y Y coordinate.
* @return Cost field at the specified position.
*
* @return Sector at the specified position.
*/
const std::shared_ptr<Sector> &get_sector(size_t x, size_t y);

/**
* Get the sector with a specified ID
*
* @param id ID of the sector.
*
* @return Sector with the specified ID.
*/
CostField &get_field(size_t x, size_t y);
const std::shared_ptr<Sector> &get_sector(sector_id_t id) const;

private:
/**
Expand All @@ -55,9 +78,9 @@ class CostGrid {
size_t height;

/**
* Cost fields.
* Sectors of the grid.
*/
std::vector<CostField> fields;
std::vector<std::shared_ptr<Sector>> sectors;
};


Expand Down
33 changes: 21 additions & 12 deletions libopenage/pathfinding/portal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,25 @@

namespace openage::path {

Portal::Portal(std::shared_ptr<CostField> sector0,
std::shared_ptr<CostField> sector1,
std::vector<std::shared_ptr<Portal>> sector0_exits,
std::vector<std::shared_ptr<Portal>> sector1_exits,
Portal::Portal(sector_id_t sector0,
sector_id_t sector1,
PortalDirection direction,
size_t cell_start_x,
size_t cell_start_y,
size_t cell_end_x,
size_t cell_end_y) :
sector0{sector0},
sector1{sector1},
sector0_exits{sector0_exits},
sector1_exits{sector1_exits},
sector0_exits{},
sector1_exits{},
direction{direction},
cell_start_x{cell_start_x},
cell_start_y{cell_start_y},
cell_end_x{cell_end_x},
cell_end_y{cell_end_y} {
}

const std::vector<std::shared_ptr<Portal>> &Portal::get_exits(const std::shared_ptr<CostField> &entry_sector) const {
const std::vector<std::shared_ptr<Portal>> &Portal::get_exits(sector_id_t entry_sector) const {
ENSURE(entry_sector == this->sector0 || entry_sector == this->sector1, "Invalid entry sector");

if (entry_sector == this->sector0) {
Expand All @@ -36,7 +34,18 @@ const std::vector<std::shared_ptr<Portal>> &Portal::get_exits(const std::shared_
return this->sector0_exits;
}

const std::shared_ptr<CostField> &Portal::get_exit_sector(const std::shared_ptr<CostField> &entry_sector) const {
void Portal::set_exits(sector_id_t sector, const std::vector<std::shared_ptr<Portal>> &exits) {
ENSURE(sector == this->sector0 || sector == this->sector1, "Portal does not connect to sector");

if (sector == this->sector0) {
this->sector0_exits = exits;
}
else {
this->sector1_exits = exits;
}
}

sector_id_t Portal::get_exit_sector(sector_id_t entry_sector) const {
ENSURE(entry_sector == this->sector0 || entry_sector == this->sector1, "Invalid entry sector");

if (entry_sector == this->sector0) {
Expand All @@ -45,7 +54,7 @@ const std::shared_ptr<CostField> &Portal::get_exit_sector(const std::shared_ptr<
return this->sector0;
}

std::pair<size_t, size_t> Portal::get_entry_start(const std::shared_ptr<CostField> &entry_sector) const {
std::pair<size_t, size_t> Portal::get_entry_start(sector_id_t entry_sector) const {
ENSURE(entry_sector == this->sector0 || entry_sector == this->sector1, "Invalid entry sector");

if (entry_sector == this->sector0) {
Expand All @@ -55,7 +64,7 @@ std::pair<size_t, size_t> Portal::get_entry_start(const std::shared_ptr<CostFiel
return this->get_sector1_start();
}

std::pair<size_t, size_t> Portal::get_entry_end(const std::shared_ptr<CostField> &entry_sector) const {
std::pair<size_t, size_t> Portal::get_entry_end(sector_id_t entry_sector) const {
ENSURE(entry_sector == this->sector0 || entry_sector == this->sector1, "Invalid entry sector");

if (entry_sector == this->sector0) {
Expand All @@ -65,7 +74,7 @@ std::pair<size_t, size_t> Portal::get_entry_end(const std::shared_ptr<CostField>
return this->get_sector1_end();
}

std::pair<size_t, size_t> Portal::get_exit_start(const std::shared_ptr<CostField> &entry_sector) const {
std::pair<size_t, size_t> Portal::get_exit_start(sector_id_t entry_sector) const {
ENSURE(entry_sector == this->sector0 || entry_sector == this->sector1, "Invalid entry sector");

if (entry_sector == this->sector0) {
Expand All @@ -75,7 +84,7 @@ std::pair<size_t, size_t> Portal::get_exit_start(const std::shared_ptr<CostField
return this->get_sector0_start();
}

std::pair<size_t, size_t> Portal::get_exit_end(const std::shared_ptr<CostField> &entry_sector) const {
std::pair<size_t, size_t> Portal::get_exit_end(sector_id_t entry_sector) const {
ENSURE(entry_sector == this->sector0 || entry_sector == this->sector1, "Invalid entry sector");

if (entry_sector == this->sector0) {
Expand Down
55 changes: 30 additions & 25 deletions libopenage/pathfinding/portal.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

#pragma once


#include <memory>
#include <utility>
#include <vector>

#include "pathfinding/cost_field.h"
#include "pathfinding/types.h"


namespace openage::path {
Expand Down Expand Up @@ -45,18 +46,14 @@ class Portal {
* Must be north or east on the grid in relation to sector 1.
* @param sector1 Second sector connected by the portal.
* Must be south or west on the grid in relation to sector 0.
* @param sector0_exits Portals reachable from this portal in sector 0.
* @param sector1_exits Portals reachable from this portal in sector 1.
* @param direction Direction of the portal from sector 0 to sector 1.
* @param cell_start_x Start cell x coordinate in sector 0.
* @param cell_start_y Start cell y coordinate in sector 0.
* @param cell_end_x End cell x coordinate in sector 0.
* @param cell_end_y End cell y coordinate in sector 0.
*/
Portal(std::shared_ptr<CostField> sector0,
std::shared_ptr<CostField> sector1,
std::vector<std::shared_ptr<Portal>> sector0_exits,
std::vector<std::shared_ptr<Portal>> sector1_exits,
Portal(sector_id_t sector0,
sector_id_t sector1,
PortalDirection direction,
size_t cell_start_x,
size_t cell_start_y,
Expand All @@ -72,7 +69,15 @@ class Portal {
*
* @return Exit portals reachable from the portal.
*/
const std::vector<std::shared_ptr<Portal>> &get_exits(const std::shared_ptr<CostField> &entry_sector) const;
const std::vector<std::shared_ptr<Portal>> &get_exits(sector_id_t entry_sector) const;

/**
* Set the exit portals reachable for a specified sector.
*
* @param sector Sector for which the exit portals are set.
* @param exits Exit portals reachable from the portal.
*/
void set_exits(sector_id_t sector, const std::vector<std::shared_ptr<Portal>> &exits);

/**
* Get the cost field of the sector where the portal is exited.
Expand All @@ -81,7 +86,7 @@ class Portal {
*
* @return Cost field of the sector where the portal is exited.
*/
const std::shared_ptr<CostField> &get_exit_sector(const std::shared_ptr<CostField> &entry_sector) const;
sector_id_t get_exit_sector(sector_id_t entry_sector) const;

/**
* Get the cost field of the sector from which the portal is entered.
Expand All @@ -90,7 +95,7 @@ class Portal {
*
* @return Cost field of the sector from which the portal is entered.
*/
std::pair<size_t, size_t> get_entry_start(const std::shared_ptr<CostField> &entry_sector) const;
std::pair<size_t, size_t> get_entry_start(sector_id_t entry_sector) const;

/**
* Get the cell coordinates of the start of the portal in the entry sector.
Expand All @@ -99,7 +104,7 @@ class Portal {
*
* @return Cell coordinates of the start of the portal in the entry sector.
*/
std::pair<size_t, size_t> get_entry_end(const std::shared_ptr<CostField> &entry_sector) const;
std::pair<size_t, size_t> get_entry_end(sector_id_t entry_sector) const;

/**
* Get the cell coordinates of the start of the portal in the exit sector.
Expand All @@ -108,7 +113,7 @@ class Portal {
*
* @return Cell coordinates of the start of the portal in the exit sector.
*/
std::pair<size_t, size_t> get_exit_start(const std::shared_ptr<CostField> &entry_sector) const;
std::pair<size_t, size_t> get_exit_start(sector_id_t entry_sector) const;

/**
* Get the cell coordinates of the end of the portal in the exit sector.
Expand All @@ -117,7 +122,7 @@ class Portal {
*
* @return Cell coordinates of the end of the portal in the exit sector.
*/
std::pair<size_t, size_t> get_exit_end(const std::shared_ptr<CostField> &entry_sector) const;
std::pair<size_t, size_t> get_exit_end(sector_id_t entry_sector) const;

private:
/**
Expand Down Expand Up @@ -149,47 +154,47 @@ class Portal {
std::pair<size_t, size_t> get_sector1_end() const;

/**
* Sector 0
* First sector connected by the portal.
*/
std::shared_ptr<CostField> sector0;
sector_id_t sector0;

/**
* Sector 1
* Second sector connected by the portal.
*/
std::shared_ptr<CostField> sector1;
sector_id_t sector1;

/**
* Exits from sector 0
* Exits in sector 0 reachable from the portal.
*/
std::vector<std::shared_ptr<Portal>> sector0_exits;

/**
* Exits from sector 1
* Exits in sector 1 reachable from the portal.
*/
std::vector<std::shared_ptr<Portal>> sector1_exits;

/**
* Direction of the portal
* Direction of the portal from sector 0 to sector 1.
*/
PortalDirection direction;

/**
* Start cell x coordinate
* Start cell x coordinate.
*/
size_t cell_start_x;

/**
* Start cell y coordinate
* Start cell y coordinate.
*/
size_t cell_start_y;

/**
* End cell x coordinate
* End cell x coordinate.
*/
size_t cell_end_x;

/**
* End cell y coordinate
* End cell y coordinate.
*/
size_t cell_end_y;
};
Expand Down

0 comments on commit 6b71548

Please sign in to comment.