-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathcost_field.cpp
More file actions
88 lines (64 loc) 路 2.43 KB
/
Copy pathcost_field.cpp
File metadata and controls
88 lines (64 loc) 路 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2024-2025 the openage authors. See copying.md for legal info.
#include "cost_field.h"
#include "error/error.h"
#include "log/log.h"
#include "coord/tile.h"
#include "pathfinding/definitions.h"
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::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 {
return time >= this->valid_until;
}
void CostField::clear_dirty() {
this->valid_until = time::TIME_MAX;
}
} // namespace openage::path