Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions libopenage/curve/container/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#pragma once

#include <algorithm>
#include <array>

#include "curve/container/iterator.h"
Expand Down Expand Up @@ -109,6 +110,23 @@ class Array : event::EventEntity {
*/
std::pair<time::time_t, T> next_frame(const time::time_t &t, const index_t index) const;


/**
* Insert a range of elements into the Array.
*
* @param t Time of insertion.
* @param begin_it iterator pointing to the first element in the container you wish to insert.
* @param end_it iterator pointing to one after the last element in the container you wish to insert.
* @param i Index of the array at which insertion will begin.
*
* @return Time-value pair of the first keyframe with time > t.
*/
void set_insert_range(const time::time_t &t, auto begin_it, auto end_it, index_t i = 0) {
ENSURE(std::distance(begin_it, end_it) <= Size - i,
"trying to insert more values than there are postions: max allowed = " << Size - i);
std::for_each(begin_it, end_it, [&](const T &val) { this->set_insert(t, i++, val); });
}

/**
* Insert a new keyframe value at time t.
*
Expand All @@ -118,7 +136,7 @@ class Array : event::EventEntity {
* @param index Index of the array element.
* @param value Keyframe value.
*/
void set_insert(const time::time_t &t, const index_t index, T value);
void set_insert(const time::time_t &t, const index_t index, const T &value);

/**
* Insert a new keyframe value at time t. Erase all other keyframes with elem->time > t.
Expand All @@ -127,7 +145,7 @@ class Array : event::EventEntity {
* @param index Index of the array element.
* @param value Keyframe value.
*/
void set_last(const time::time_t &t, const index_t index, T value);
void set_last(const time::time_t &t, const index_t index, const T &value);

/**
* Replace all keyframes at elem->time == t with a new keyframe value.
Expand All @@ -136,7 +154,7 @@ class Array : event::EventEntity {
* @param index Index of the array element.
* @param value Keyframe value.
*/
void set_replace(const time::time_t &t, const index_t index, T value);
void set_replace(const time::time_t &t, const index_t index, const T &value);

/**
* Copy keyframes from another container to this container.
Expand Down Expand Up @@ -324,7 +342,7 @@ consteval size_t Array<T, Size>::size() const {
template <typename T, size_t Size>
void Array<T, Size>::set_insert(const time::time_t &t,
const index_t index,
T value) {
const T &value) {
// find elem_ptr in container to get the last keyframe with time <= t
auto hint = this->last_elements[index];
auto e = this->containers.at(index).insert_after(Keyframe{t, value}, hint);
Expand All @@ -338,7 +356,7 @@ void Array<T, Size>::set_insert(const time::time_t &t,
template <typename T, size_t Size>
void Array<T, Size>::set_last(const time::time_t &t,
const index_t index,
T value) {
const T &value) {
// find elem_ptr in container to get the last keyframe with time <= t
auto hint = this->last_elements[index];
auto e = this->containers.at(index).last(t, hint);
Expand All @@ -363,7 +381,7 @@ void Array<T, Size>::set_last(const time::time_t &t,
template <typename T, size_t Size>
void Array<T, Size>::set_replace(const time::time_t &t,
const index_t index,
T value) {
const T &value) {
// find elem_ptr in container to get the last keyframe with time <= t
auto hint = this->last_elements[index];
auto e = this->containers.at(index).insert_overwrite(Keyframe{t, value}, hint);
Expand Down
21 changes: 17 additions & 4 deletions libopenage/curve/tests/container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,7 @@ void test_queue() {
}

void test_array() {
auto loop = std::make_shared<event::EventLoop>();

Array<int, 4> a(loop, 0);
Array<int, 4> a(nullptr, 2);
a.set_insert(1, 0, 0);
a.set_insert(1, 1, 1);
a.set_insert(1, 2, 2);
Expand All @@ -273,7 +271,7 @@ void test_array() {
TESTEQUALS(res.at(2), 0);
TESTEQUALS(res.at(3), 0);

Array<int, 4> other(loop, 0);
Array<int, 4> other(nullptr, 2);
other.set_last(0, 0, 999);
other.set_last(0, 1, 999);
other.set_last(0, 2, 999);
Expand Down Expand Up @@ -344,6 +342,21 @@ void test_array() {
TESTEQUALS(*it, 6);
++it;
TESTEQUALS(*it, 7);

// Test set_insert-range
std::vector<int> vec = {100, 200, 300};

a.set_insert_range(5, vec.begin(), vec.end(), 1);
// a = [[0:0, 1:4, 2:25, 3:35],[0:0, 1:5, 6:100],[0:0, 1:6, 6:200],[0:0, 1:7, 5:40, 6:300]]

it = a.begin(6);
TESTEQUALS(*it, 35);
++it;
TESTEQUALS(*it, 100);
++it;
TESTEQUALS(*it, 200);
++it;
TESTEQUALS(*it, 300);
}


Expand Down
7 changes: 3 additions & 4 deletions libopenage/gamestate/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@ namespace openage::gamestate {
Map::Map(const std::shared_ptr<GameState> &state,
const std::shared_ptr<Terrain> &terrain) :
terrain{terrain},
pathfinder{std::make_shared<path::Pathfinder>()},
pathfinder{std::make_shared<path::Pathfinder<path::SECTOR_SIZE>>()},
grid_lookup{} {
// Create a grid for each path type
// TODO: This is non-deterministic because of the unordered set. Is this a problem?
auto nyan_db = state->get_db_view();
std::unordered_set<nyan::fqon_t> path_types = nyan_db->get_obj_children_all("engine.util.path_type.PathType");
size_t grid_idx = 0;
auto chunk_size = this->terrain->get_chunk(0)->get_size();
auto side_length = std::max(chunk_size[0], chunk_size[1]);
auto grid_size = this->terrain->get_chunks_size();
for (const auto &path_type : path_types) {
auto grid = std::make_shared<path::Grid>(grid_idx, grid_size, side_length);
auto grid = std::make_shared<path::Grid<path::SECTOR_SIZE>>(grid_idx, grid_size);
this->pathfinder->add_grid(grid);

this->grid_lookup.emplace(path_type, grid_idx);
Expand Down Expand Up @@ -70,7 +69,7 @@ const std::shared_ptr<Terrain> &Map::get_terrain() const {
return this->terrain;
}

const std::shared_ptr<path::Pathfinder> &Map::get_pathfinder() const {
const std::shared_ptr<path::Pathfinder<path::SECTOR_SIZE>> &Map::get_pathfinder() const {
return this->pathfinder;
}

Expand Down
8 changes: 6 additions & 2 deletions libopenage/gamestate/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@

#include <nyan/nyan.h>

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

#include "util/vector.h"


namespace openage {
namespace path {

template <size_t N>
class Pathfinder;
} // namespace path

Expand Down Expand Up @@ -54,7 +58,7 @@ class Map {
*
* @return Pathfinder.
*/
const std::shared_ptr<path::Pathfinder> &get_pathfinder() const;
const std::shared_ptr<path::Pathfinder<path::SECTOR_SIZE>> &get_pathfinder() const;

/**
* Get the grid ID associated with a nyan path grid object.
Expand All @@ -74,7 +78,7 @@ class Map {
/**
* Pathfinder.
*/
std::shared_ptr<path::Pathfinder> pathfinder;
std::shared_ptr<path::Pathfinder<path::SECTOR_SIZE>> pathfinder;

/**
* Lookup table for mapping path grid objects in nyan to grid indices.
Expand Down
4 changes: 2 additions & 2 deletions libopenage/gamestate/system/move.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@
#include "gamestate/game_entity.h"
#include "gamestate/game_state.h"
#include "gamestate/map.h"
#include "pathfinding/definitions.h"
#include "pathfinding/path.h"
#include "pathfinding/pathfinder.h"
#include "util/fixed_point.h"


namespace openage::gamestate::system {


std::vector<coord::phys3> find_path(const std::shared_ptr<path::Pathfinder> &pathfinder,
std::vector<coord::phys3> find_path(const std::shared_ptr<path::Pathfinder<path::SECTOR_SIZE>> &pathfinder,
path::grid_id_t grid_id,
const coord::phys3 &start,
const coord::phys3 &end,
Expand Down
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
path.cpp
pathfinder.cpp
portal.cpp
portal_node.cpp
sector.cpp
tests.cpp
types.cpp
Expand Down
53 changes: 0 additions & 53 deletions libopenage/pathfinding/cost_field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,57 +11,4 @@

namespace openage::path {

CostField::CostField(size_t size) :
size{size},
valid_until{time::TIME_MIN},
cells(this->size * this->size, COST_MIN) {
log::log(DBG << "Created cost field with size " << this->size << "x" << this->size);
}

size_t CostField::get_size() const {
return this->size;
}

cost_t CostField::get_cost(const coord::tile_delta &pos) const {
return this->cells.at(pos.ne + pos.se * this->size);
}

cost_t CostField::get_cost(size_t x, size_t y) const {
return this->cells.at(x + y * this->size);
}

cost_t CostField::get_cost(size_t idx) const {
return this->cells.at(idx);
}

void CostField::set_cost(const coord::tile_delta &pos, cost_t cost, const time::time_t &valid_until) {
this->set_cost(pos.ne + pos.se * this->size, cost, valid_until);
}

void CostField::set_cost(size_t x, size_t y, cost_t cost, const time::time_t &valid_until) {
this->set_cost(x + y * this->size, cost, valid_until);
}

const std::vector<cost_t> &CostField::get_costs() const {
return this->cells;
}

void CostField::set_costs(std::vector<cost_t> &&cells, const time::time_t &valid_until) {
ENSURE(cells.size() == this->cells.size(),
"cells vector has wrong size: " << cells.size()
<< "; expected: "
<< this->cells.size());

this->cells = std::move(cells);
this->valid_until = valid_until;
}

bool CostField::is_dirty(const time::time_t &time) const {
return time >= this->valid_until;
}

void CostField::clear_dirty() {
this->valid_until = time::TIME_MAX;
}

} // namespace openage::path
Loading
Loading