Skip to content

Add cost stamps #1745

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 14 commits into
base: master
Choose a base branch
from
15 changes: 8 additions & 7 deletions libopenage/pathfinding/cost_field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,25 @@ void CostField::set_costs(std::vector<cost_t> &&cells, const time::time_t &valid
}

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

cost_t original_cost = cost_stamps[idx].original_cost;
cost_t original_cost = cost_stamps[idx]->original_cost;

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

bool CostField::is_dirty(const time::time_t &time) const {
Expand Down
3 changes: 2 additions & 1 deletion libopenage/pathfinding/cost_field.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

#include "pathfinding/types.h"
#include "time/time.h"
Expand Down Expand Up @@ -160,7 +161,7 @@ class CostField {
/**
* Cost field values.
*/
std::unordered_map<size_t, cost_stamp_t> cost_stamps;
std::vector<std::optional<cost_stamp_t>> cost_stamps;
};

} // namespace path
Expand Down
Loading