-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
base: master
Are you sure you want to change the base?
Pathfinder history #1748
Changes from all commits
f453ca0
ffeeefb
79beb3f
c637f4a
d68a40d
50d7272
4ae4de0
96024ff
4615e8d
b390440
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -19,6 +28,8 @@ namespace path { | |
/** | ||
* Cost field in the flow-field pathfinding algorithm. | ||
*/ | ||
|
||
template <size_t N> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should actually use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't it make more sense to have a There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
/** | ||
|
@@ -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]); | ||
} | ||
|
||
/** | ||
|
@@ -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. | ||
* | ||
|
@@ -135,7 +168,111 @@ class CostField { | |
* Cost field values. | ||
*/ | ||
std::vector<cost_t> cells; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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 |
There was a problem hiding this comment.
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.