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

Add cost stamps #1745

Open
wants to merge 10 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;
dmwever marked this conversation as resolved.
Show resolved Hide resolved

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;
Comment on lines +65 to +66
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this code will work, i.e. it could segfault because the optional value is not initialized. You have to explicitely assign it a cost_stamp_t struct before you can access the members directly. So you have to write something like

this->cost_stamps[idx] = cost_stamp_t{
    orginal_cost,
    stamped_at,
};

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. Learning the ropes of c++ here.


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;
dmwever marked this conversation as resolved.
Show resolved Hide resolved

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.
dmwever marked this conversation as resolved.
Show resolved Hide resolved
*/
std::unordered_map<size_t, cost_stamp_t> cost_stamps;
std::vector<std::optional<cost_stamp_t>> cost_stamps;
Copy link
Contributor

Choose a reason for hiding this comment

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

you need to make sure cost_stamps is initialised in the constructor with the correct size

};

} // namespace path
Expand Down
Loading