Skip to content

Commit aa9c669

Browse files
committed
use N inplace of sector_size
1 parent cd8a371 commit aa9c669

File tree

7 files changed

+59
-76
lines changed

7 files changed

+59
-76
lines changed

libopenage/curve/tests/container.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ void test_queue() {
244244
}
245245

246246
void test_array() {
247-
Array<int, 4> a;
247+
Array<int, 4> a(nullptr, 2);
248248
a.set_insert(1, 0, 0);
249249
a.set_insert(1, 1, 1);
250250
a.set_insert(1, 2, 2);
@@ -271,7 +271,7 @@ void test_array() {
271271
TESTEQUALS(res.at(2), 0);
272272
TESTEQUALS(res.at(3), 0);
273273

274-
Array<int, 4> other;
274+
Array<int, 4> other(nullptr, 2);
275275
other.set_last(0, 0, 999);
276276
other.set_last(0, 1, 999);
277277
other.set_last(0, 2, 999);

libopenage/gamestate/map.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ Map::Map(const std::shared_ptr<GameState> &state,
2626
std::unordered_set<nyan::fqon_t> path_types = nyan_db->get_obj_children_all("engine.util.path_type.PathType");
2727
size_t grid_idx = 0;
2828
auto chunk_size = this->terrain->get_chunk(0)->get_size();
29-
auto side_length = std::max(chunk_size[0], chunk_size[1]);
3029
auto grid_size = this->terrain->get_chunks_size();
3130
for (const auto &path_type : path_types) {
32-
auto grid = std::make_shared<path::Grid<path::SECTOR_SIZE>>(grid_idx, grid_size, side_length);
31+
auto grid = std::make_shared<path::Grid<path::SECTOR_SIZE>>(grid_idx, grid_size);
3332
this->pathfinder->add_grid(grid);
3433

3534
this->grid_lookup.emplace(path_type, grid_idx);

libopenage/pathfinding/cost_field.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ class CostField {
3535
/**
3636
* Create a square cost field.
3737
*/
38-
CostField();
38+
CostField(const std::shared_ptr<event::EventLoop> &loop = nullptr, size_t id = 0);
3939

4040
/**
4141
* Get the size of the cost field.
4242
*
4343
* @return Size of the cost field.
4444
*/
45-
size_t get_size() const;
45+
constexpr size_t get_size() const;
4646

4747
/**
4848
* Get the cost at a specified position.
@@ -151,6 +151,9 @@ class CostField {
151151
*/
152152
void clear_dirty();
153153

154+
155+
const curve::Array<cost_t, N> &get_cost_history() const;
156+
154157
private:
155158
/**
156159
* Side length of the field.
@@ -180,15 +183,15 @@ class CostField {
180183
};
181184

182185
template <size_t N>
183-
CostField<N>::CostField() :
186+
CostField<N>::CostField(const std::shared_ptr<event::EventLoop> &loop, size_t id) :
184187
valid_until{time::TIME_MIN},
185188
cells(N * N, COST_MIN),
186-
cell_cost_history() {
189+
cell_cost_history(loop, id) {
187190
log::log(DBG << "Created cost field with size " << N << "x" << N);
188191
}
189192

190193
template <size_t N>
191-
size_t CostField<N>::get_size() const {
194+
constexpr size_t CostField<N>::get_size() const {
192195
return N;
193196
}
194197

@@ -271,5 +274,10 @@ void CostField<N>::clear_dirty() {
271274
this->valid_until = time::TIME_MAX;
272275
}
273276

277+
template <size_t N>
278+
const curve::Array<cost_t, N> &CostField<N>::get_cost_history() const {
279+
return this->cell_cost_history;
280+
};
281+
274282
} // namespace path
275283
} // namespace openage

libopenage/pathfinding/demo/demo_1.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
namespace openage::path::tests {
2626

2727
void path_demo_1(const util::Path &path) {
28-
auto grid = std::make_shared<Grid<path::SECTOR_SIZE>>(0, util::Vector2s{4, 3}, 10);
28+
auto grid = std::make_shared<Grid<path::SECTOR_SIZE>>(0, util::Vector2s{4, 3});
2929

3030
// Initialize the cost field for each sector.
3131
for (auto &sector : grid->get_sectors()) {
@@ -134,8 +134,8 @@ void path_demo_1(const util::Path &path) {
134134
window->add_mouse_button_callback([&](const QMouseEvent &ev) {
135135
if (ev.type() == QEvent::MouseButtonRelease) {
136136
// From the mouse position, calculate the position/cell on the grid
137-
auto cell_count_x = grid->get_size()[0] * grid->get_sector_size();
138-
auto cell_count_y = grid->get_size()[1] * grid->get_sector_size();
137+
auto cell_count_x = grid->get_size()[0] * path::SECTOR_SIZE;
138+
auto cell_count_y = grid->get_size()[1] * path::SECTOR_SIZE;
139139
auto window_size = window->get_size();
140140

141141
double cell_size_x = static_cast<double>(window_size[0]) / cell_count_x;
@@ -301,8 +301,8 @@ void RenderManager1::init_passes() {
301301
// Create object for the grid
302302
auto model = Eigen::Affine3f::Identity();
303303
model.prescale(Eigen::Vector3f{
304-
2.0f / (this->grid->get_size()[0] * this->grid->get_sector_size()),
305-
2.0f / (this->grid->get_size()[1] * this->grid->get_sector_size()),
304+
2.0f / (this->grid->get_size()[0] * path::SECTOR_SIZE),
305+
2.0f / (this->grid->get_size()[1] * path::SECTOR_SIZE),
306306
1.0f});
307307
model.pretranslate(Eigen::Vector3f{-1.0f, -1.0f, 0.0f});
308308
auto grid_unifs = grid_shader->new_uniform_input(
@@ -413,8 +413,8 @@ renderer::resources::MeshData RenderManager1::get_grid_mesh(const std::shared_pt
413413
// increase by 1 in every dimension because to get the vertex length
414414
// of each dimension
415415
util::Vector2s size{
416-
grid->get_size()[0] * grid->get_sector_size() + 1,
417-
grid->get_size()[1] * grid->get_sector_size() + 1};
416+
grid->get_size()[0] * path::SECTOR_SIZE + 1,
417+
grid->get_size()[1] * path::SECTOR_SIZE + 1};
418418

419419
// add vertices for the cells of the grid
420420
std::vector<float> verts{};
@@ -467,7 +467,7 @@ renderer::resources::MeshData RenderManager1::get_grid_mesh(const std::shared_pt
467467
void RenderManager1::create_impassible_tiles(const std::shared_ptr<path::Grid<path::SECTOR_SIZE>> &grid) {
468468
auto width = grid->get_size()[0];
469469
auto height = grid->get_size()[1];
470-
auto sector_size = grid->get_sector_size();
470+
auto sector_size = path::SECTOR_SIZE;
471471

472472
float tile_offset_width = 2.0f / (width * sector_size);
473473
float tile_offset_height = 2.0f / (height * sector_size);
@@ -529,7 +529,7 @@ void RenderManager1::create_impassible_tiles(const std::shared_ptr<path::Grid<pa
529529
void RenderManager1::create_portal_tiles(const std::shared_ptr<path::Grid<path::SECTOR_SIZE>> &grid) {
530530
auto width = grid->get_size()[0];
531531
auto height = grid->get_size()[1];
532-
auto sector_size = grid->get_sector_size();
532+
auto sector_size = path::SECTOR_SIZE;
533533

534534
float tile_offset_width = 2.0f / (width * sector_size);
535535
float tile_offset_height = 2.0f / (height * sector_size);
@@ -606,7 +606,7 @@ void RenderManager1::create_portal_tiles(const std::shared_ptr<path::Grid<path::
606606
void RenderManager1::create_waypoint_tiles(const Path &path) {
607607
auto width = grid->get_size()[0];
608608
auto height = grid->get_size()[1];
609-
auto sector_size = grid->get_sector_size();
609+
auto sector_size = path::SECTOR_SIZE;
610610

611611
float tile_offset_width = 2.0f / (width * sector_size);
612612
float tile_offset_height = 2.0f / (height * sector_size);

libopenage/pathfinding/flow_field.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class FlowField {
5151
*
5252
* @return Size of the flow field.
5353
*/
54-
size_t get_size() const;
54+
constexpr size_t get_size() const;
5555

5656
/**
5757
* Get the flow field value at a specified position.
@@ -194,7 +194,7 @@ FlowField<N>::FlowField(const std::shared_ptr<IntegrationField<N>> &integration_
194194
}
195195

196196
template <size_t N>
197-
size_t FlowField<N>::get_size() const {
197+
constexpr size_t FlowField<N>::get_size() const {
198198
return N;
199199
}
200200

libopenage/pathfinding/grid.h

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@ class Grid {
2929
*
3030
* @param id ID of the grid.
3131
* @param size Size of the grid in sectors (width x height).
32-
* @param sector_size Side length of each sector.
3332
*/
3433
Grid(grid_id_t id,
35-
const util::Vector2s &size,
36-
size_t sector_size);
34+
const util::Vector2s &size);
3735

3836
/**
3937
* Create a grid of width x height sectors from a list of existing sectors.
@@ -60,12 +58,6 @@ class Grid {
6058
*/
6159
const util::Vector2s &get_size() const;
6260

63-
/**
64-
* Get the side length of the sectors on the grid (in number of cells).
65-
*
66-
* @return Sector side length (in number of cells).
67-
*/
68-
size_t get_sector_size() const;
6961

7062
/**
7163
* Get the sector at a specified position.
@@ -121,10 +113,6 @@ class Grid {
121113
*/
122114
util::Vector2s size;
123115

124-
/**
125-
* Side length of the grid sectors.
126-
*/
127-
size_t sector_size;
128116

129117
/**
130118
* Sectors of the grid.
@@ -141,11 +129,9 @@ class Grid {
141129

142130
template <size_t N>
143131
Grid<N>::Grid(grid_id_t id,
144-
const util::Vector2s &size,
145-
size_t sector_size) :
132+
const util::Vector2s &size) :
146133
id{id},
147-
size{size},
148-
sector_size{sector_size} {
134+
size{size} {
149135
for (size_t y = 0; y < size[1]; y++) {
150136
for (size_t x = 0; x < size[0]; x++) {
151137
this->sectors.push_back(
@@ -165,8 +151,6 @@ Grid<N>::Grid(grid_id_t id,
165151
ENSURE(this->sectors.size() == size[0] * size[1],
166152
"Grid has size " << size[0] << "x" << size[1] << " (" << size[0] * size[1] << " sectors), "
167153
<< "but only " << this->sectors.size() << " sectors were provided");
168-
169-
this->sector_size = sectors.at(0)->get_cost_field()->get_size();
170154
}
171155

172156
template <size_t N>
@@ -179,11 +163,6 @@ const util::Vector2s &Grid<N>::get_size() const {
179163
return this->size;
180164
}
181165

182-
template <size_t N>
183-
size_t Grid<N>::get_sector_size() const {
184-
return this->sector_size;
185-
}
186-
187166
template <size_t N>
188167
const std::shared_ptr<Sector<N>> &Grid<N>::get_sector(size_t x, size_t y) {
189168
return this->sectors.at(x + y * this->size[0]);

0 commit comments

Comments
 (0)