Skip to content

Pathfinder history #1748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
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
34 changes: 26 additions & 8 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 @@ -42,8 +43,8 @@ class Array : event::EventEntity {
* @param notifier Function to call when this curve changes.
* @param default_vals Default values for the array elements.
*/
Array(const std::shared_ptr<event::EventLoop> &loop,
size_t id,
Array(const std::shared_ptr<event::EventLoop> &loop = nullptr,
size_t id = 0,
Comment on lines +46 to +47
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a good idea, since it will lead to errors when the curve is used for events without initializing the loop.

const std::string &idstr = "",
const EventEntity::single_change_notifier &notifier = nullptr,
const std::array<T, Size> &default_vals = {}) :
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;
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;
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
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
137 changes: 137 additions & 0 deletions libopenage/pathfinding/cost_field.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@

#include <cstddef>
#include <vector>
#include <optional>

#include "curve/container/array.h"
#include "pathfinding/types.h"
#include "time/time.h"

#include "cost_field.h"

#include "error/error.h"
#include "log/log.h"

#include "coord/tile.h"
#include "pathfinding/definitions.h"

namespace openage {
namespace coord {
Expand All @@ -19,6 +28,8 @@ namespace path {
/**
* Cost field in the flow-field pathfinding algorithm.
*/

template <size_t N>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should actually use N for more than curve::Array. Otherwise you have a mismatch, e.g. of the size of cells and cell_cost_history.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also N should be side length like size passed to the constructor. If you don't do this, then this allows non-square fields which would break pathfinding.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it make more sense to have a curve::Vector here than an Array? size is not a compile time constant in Grid/Sector.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WillemKauf That's what my comment in #1748 (comment) is related to ^^

class CostField {
public:
/**
Expand Down Expand Up @@ -89,6 +100,7 @@ class CostField {
inline void set_cost(size_t idx, cost_t cost, const time::time_t &valid_until) {
this->cells[idx] = cost;
this->valid_until = valid_until;
this->cell_cost_history.set_insert(valid_until, idx, this->cells[idx]);
}

/**
Expand All @@ -106,6 +118,27 @@ class CostField {
*/
void set_costs(std::vector<cost_t> &&cells, const time::time_t &changed);

/**
* Stamp a cost field cell at a given time.
*
* @param idx Index of the cell.
* @param cost Cost to set.
* @param stamped_at Time at which the cost cell is to be stamped.
*
* @return True if the cell was successfully stamped, false if the cell was already stamped.
*/
bool stamp(size_t idx, cost_t cost, const time::time_t &stamped_at);

/**
* Unstamp a cost field cell at a given time.
*
* @param idx Index of the cell.
* @param unstamped_at Time at which the cost cell is to be unstamped.
*
* @return True if the cell was successfully unstamped, false if the cell was already not stamped.
*/
bool unstamp(size_t idx, const time::time_t &unstamped_at);

/**
* Check if the cost field is dirty at the specified time.
*
Expand Down Expand Up @@ -135,7 +168,111 @@ class CostField {
* Cost field values.
*/
std::vector<cost_t> cells;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can now also be an array of size N.


/**
* Cost stamp vector.
*/
std::vector<std::optional<cost_stamp_t>> cost_stamps;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here



/**
* Array curve recording cell cost history,
*/
curve::Array<cost_t, N> cell_cost_history;
};

template <size_t N>
CostField<N>::CostField(size_t size) :
size{size},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

size is no longer necessary due to templating

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

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

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

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

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

template <size_t N>
void CostField<N>::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);
}

template <size_t N>
void CostField<N>::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);
}

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

template <size_t N>
void CostField<N>::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;
this->cell_cost_history.set_insert_range(valid_until, this->cells.begin(), this->cells.end());
}

template <size_t N>
bool CostField<N>::stamp(size_t idx, cost_t cost, const time::time_t &stamped_at) {
if (this->cost_stamps[idx].has_value()) {
return false;
}
return false;

cost_t original_cost = this->get_cost(idx);
this->cost_stamps[idx]->original_cost = original_cost;
this->cost_stamps[idx]->stamp_time = stamped_at;

this->set_cost(idx, cost, stamped_at);
return true;
}

template <size_t N>
bool CostField<N>::unstamp(size_t idx, const time::time_t &unstamped_at) {
if (!this->cost_stamps[idx].has_value() or unstamped_at < this->cost_stamps[idx]->stamp_time) {
return false;
}
cost_t original_cost = cost_stamps[idx]->original_cost;

this->set_cost(idx, original_cost, unstamped_at);
this->cost_stamps[idx].reset();
return true;
}

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

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

} // namespace path
} // namespace openage
Loading
Loading