Skip to content
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

Pathfinder history #1748

Open
wants to merge 9 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
25 changes: 17 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 @@ -38,8 +39,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,
const std::string &idstr = "",
const EventEntity::single_change_notifier &notifier = nullptr,
const std::array<T, Size> &default_vals = {}) :
Expand Down Expand Up @@ -105,6 +106,14 @@ class Array : event::EventEntity {
*/
std::pair<time::time_t, T> next_frame(const time::time_t &t, const index_t index) const;


void set_insert_range(const time::time_t &t, auto begin_it, auto end_it) {
ENSURE(std::distance(begin_it, end_it) <= Size,
"trying to insert more values than there are postions: max allowed = " << Size);
index_t i = 0;
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 @@ -114,7 +123,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 @@ -123,7 +132,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 @@ -132,7 +141,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 @@ -321,7 +330,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 @@ -335,7 +344,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 @@ -360,7 +369,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
6 changes: 2 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
25 changes: 24 additions & 1 deletion libopenage/pathfinding/cost_field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ namespace openage::path {
CostField::CostField(size_t size) :
size{size},
valid_until{time::TIME_MIN},
cells(this->size * this->size, COST_MIN) {
cells(this->size * this->size, COST_MIN),
cell_cost_history() {
log::log(DBG << "Created cost field with size " << this->size << "x" << this->size);
}

Expand Down Expand Up @@ -54,6 +55,28 @@ void CostField::set_costs(std::vector<cost_t> &&cells, const time::time_t &valid

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());
}

bool CostField::stamp(size_t idx, cost_t cost, const time::time_t &stamped_at) {
if (this->cost_stamps[idx].has_value()) 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;
}

bool CostField::unstamp(size_t idx, const time::time_t &unstamped_at) {
if (!this->cost_stamps[idx].has_value() || 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;
}

bool CostField::is_dirty(const time::time_t &time) const {
Expand Down
37 changes: 37 additions & 0 deletions libopenage/pathfinding/cost_field.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

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

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

Expand All @@ -14,6 +16,8 @@ namespace coord {
struct tile_delta;
} // namespace coord

const unsigned int CHUNK_SIZE = 100;

namespace path {

/**
Expand Down Expand Up @@ -89,6 +93,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 +111,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,6 +161,17 @@ class CostField {
* Cost field values.
*/
std::vector<cost_t> cells;

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


/**
* Array curve recording cell cost history,
*/
curve::Array<cost_t, CHUNK_SIZE> cell_cost_history;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

How should cell_cost_history be exposed? A simple getter that returns a constant ref to the curve?

};

} // namespace path
Expand Down
18 changes: 17 additions & 1 deletion libopenage/pathfinding/types.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024-2024 the openage authors. See copying.md for legal info.
// Copyright 2024-2025 the openage authors. See copying.md for legal info.

#pragma once

Expand All @@ -7,6 +7,7 @@
#include <memory>
#include <utility>

#include "time/time.h"

namespace openage::path {

Expand Down Expand Up @@ -124,4 +125,19 @@ using cache_key_t = std::pair<portal_id_t, sector_id_t>;
*/
using field_cache_t = std::pair<std::shared_ptr<IntegrationField>, std::shared_ptr<FlowField>>;

/**
* Cost stamp for a given cost field cell.
*/
struct cost_stamp_t {
/**
* Original cost of the stamped cell.
*/
cost_t original_cost;

/**
* Time the cost field cell was stamped.
*/
time::time_t stamp_time;
};

} // namespace openage::path
Loading