From 15858e59e544cfc2653a467c0d80b602e736dabe Mon Sep 17 00:00:00 2001 From: dmwever <56411717+dmwever@users.noreply.github.com> Date: Tue, 21 Jan 2025 19:04:01 -0600 Subject: [PATCH 01/13] Add cost stamps --- libopenage/pathfinding/cost_field.cpp | 21 +++++++++++++++++++++ libopenage/pathfinding/cost_field.h | 26 ++++++++++++++++++++++++++ libopenage/pathfinding/types.h | 15 +++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/libopenage/pathfinding/cost_field.cpp b/libopenage/pathfinding/cost_field.cpp index 12889663eb..6c29e24f59 100644 --- a/libopenage/pathfinding/cost_field.cpp +++ b/libopenage/pathfinding/cost_field.cpp @@ -56,6 +56,27 @@ void CostField::set_costs(std::vector &&cells, const time::time_t &valid this->valid_until = valid_until; } +bool CostField::stamp(size_t idx, cost_t cost, const time::time_t &stamped_at) { + if (this->cost_stamps.contains(idx)) 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.contains(idx)) return false; + if (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); + return this->cost_stamps.erase(idx) != 0; +} + bool CostField::is_dirty(const time::time_t &time) const { return time >= this->valid_until; } diff --git a/libopenage/pathfinding/cost_field.h b/libopenage/pathfinding/cost_field.h index c03b494a84..b9a794f66f 100644 --- a/libopenage/pathfinding/cost_field.h +++ b/libopenage/pathfinding/cost_field.h @@ -106,6 +106,27 @@ class CostField { */ void set_costs(std::vector &&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,6 +156,11 @@ class CostField { * Cost field values. */ std::vector cells; + + /** + * Cost field values. + */ + std::unordered_map cost_stamps; }; } // namespace path diff --git a/libopenage/pathfinding/types.h b/libopenage/pathfinding/types.h index 3229010a91..d04d45b183 100644 --- a/libopenage/pathfinding/types.h +++ b/libopenage/pathfinding/types.h @@ -124,4 +124,19 @@ using cache_key_t = std::pair; */ using field_cache_t = std::pair, std::shared_ptr>; +/** + * 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 From 2730cdb57f4439c2d73df4c93335803d3c9a88ce Mon Sep 17 00:00:00 2001 From: dmwever <56411717+dmwever@users.noreply.github.com> Date: Tue, 21 Jan 2025 19:19:15 -0600 Subject: [PATCH 02/13] Update types.h --- libopenage/pathfinding/types.h | 1 + 1 file changed, 1 insertion(+) diff --git a/libopenage/pathfinding/types.h b/libopenage/pathfinding/types.h index d04d45b183..a300250935 100644 --- a/libopenage/pathfinding/types.h +++ b/libopenage/pathfinding/types.h @@ -7,6 +7,7 @@ #include #include +#include "time/time.h" namespace openage::path { From f959d2feea3a4ed4f73867449edf86589b065c7b Mon Sep 17 00:00:00 2001 From: dmwever <56411717+dmwever@users.noreply.github.com> Date: Tue, 21 Jan 2025 19:26:04 -0600 Subject: [PATCH 03/13] sanity checks --- libopenage/pathfinding/cost_field.h | 8 ++++---- libopenage/pathfinding/types.h | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/libopenage/pathfinding/cost_field.h b/libopenage/pathfinding/cost_field.h index b9a794f66f..decff980df 100644 --- a/libopenage/pathfinding/cost_field.h +++ b/libopenage/pathfinding/cost_field.h @@ -108,21 +108,21 @@ class CostField { /** * 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); diff --git a/libopenage/pathfinding/types.h b/libopenage/pathfinding/types.h index a300250935..86f7ad6d69 100644 --- a/libopenage/pathfinding/types.h +++ b/libopenage/pathfinding/types.h @@ -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 @@ -130,7 +130,7 @@ using field_cache_t = std::pair, std::shared_p */ struct cost_stamp_t { /** - * Original cost of the stamped cell. + * Original cost of the stamped cell. */ cost_t original_cost; From 342efb9e14261e4612f27ef7937ffa5a49c0e7d2 Mon Sep 17 00:00:00 2001 From: dmwever <56411717+dmwever@users.noreply.github.com> Date: Thu, 23 Jan 2025 16:16:33 -0600 Subject: [PATCH 04/13] use vector of optional cost_stamp_t's --- libopenage/pathfinding/cost_field.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libopenage/pathfinding/cost_field.h b/libopenage/pathfinding/cost_field.h index decff980df..3750ed0680 100644 --- a/libopenage/pathfinding/cost_field.h +++ b/libopenage/pathfinding/cost_field.h @@ -160,7 +160,7 @@ class CostField { /** * Cost field values. */ - std::unordered_map cost_stamps; + std::vector> cost_stamps; }; } // namespace path From 30a8baaef241c54a9ad91b94cf10b78ea751cd01 Mon Sep 17 00:00:00 2001 From: dmwever <56411717+dmwever@users.noreply.github.com> Date: Thu, 23 Jan 2025 16:58:50 -0600 Subject: [PATCH 05/13] fix issues with optional --- libopenage/pathfinding/cost_field.cpp | 15 ++++++++------- libopenage/pathfinding/cost_field.h | 1 + 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/libopenage/pathfinding/cost_field.cpp b/libopenage/pathfinding/cost_field.cpp index 6c29e24f59..92afea4b9b 100644 --- a/libopenage/pathfinding/cost_field.cpp +++ b/libopenage/pathfinding/cost_field.cpp @@ -57,24 +57,25 @@ void CostField::set_costs(std::vector &&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 { diff --git a/libopenage/pathfinding/cost_field.h b/libopenage/pathfinding/cost_field.h index 3750ed0680..b00a49f6fb 100644 --- a/libopenage/pathfinding/cost_field.h +++ b/libopenage/pathfinding/cost_field.h @@ -4,6 +4,7 @@ #include #include +#include #include "pathfinding/types.h" #include "time/time.h" From b0beecac922cdde377d528fca6191f5e41466620 Mon Sep 17 00:00:00 2001 From: dmwever Date: Thu, 23 Jan 2025 17:30:50 -0600 Subject: [PATCH 06/13] Update libopenage/pathfinding/cost_field.cpp Co-authored-by: jere8184 --- libopenage/pathfinding/cost_field.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libopenage/pathfinding/cost_field.cpp b/libopenage/pathfinding/cost_field.cpp index 92afea4b9b..708f4f0682 100644 --- a/libopenage/pathfinding/cost_field.cpp +++ b/libopenage/pathfinding/cost_field.cpp @@ -68,8 +68,7 @@ bool CostField::stamp(size_t idx, cost_t cost, const time::time_t &stamped_at) { } bool CostField::unstamp(size_t idx, const time::time_t &unstamped_at) { - if (!this->cost_stamps[idx].has_value()) return false; - if (unstamped_at < this->cost_stamps[idx]->stamp_time) return false; + 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; From c2d5cdba19ea935c735d716ee220eb71c1c08a64 Mon Sep 17 00:00:00 2001 From: dmwever <56411717+dmwever@users.noreply.github.com> Date: Thu, 23 Jan 2025 17:38:22 -0600 Subject: [PATCH 07/13] Update cost_field.h --- libopenage/pathfinding/cost_field.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libopenage/pathfinding/cost_field.h b/libopenage/pathfinding/cost_field.h index b00a49f6fb..eb72780076 100644 --- a/libopenage/pathfinding/cost_field.h +++ b/libopenage/pathfinding/cost_field.h @@ -159,7 +159,7 @@ class CostField { std::vector cells; /** - * Cost field values. + * Cost stamp vector. */ std::vector> cost_stamps; }; From 6f1eaeb243b926ea6b7b294bbbfd4cfc2650fee3 Mon Sep 17 00:00:00 2001 From: dmwever Date: Wed, 29 Jan 2025 07:58:45 -0600 Subject: [PATCH 08/13] Update libopenage/pathfinding/cost_field.cpp Co-authored-by: Christoph Heine <6852422+heinezen@users.noreply.github.com> --- libopenage/pathfinding/cost_field.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libopenage/pathfinding/cost_field.cpp b/libopenage/pathfinding/cost_field.cpp index 708f4f0682..fc2ab402f5 100644 --- a/libopenage/pathfinding/cost_field.cpp +++ b/libopenage/pathfinding/cost_field.cpp @@ -57,7 +57,9 @@ void CostField::set_costs(std::vector &&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[idx].has_value()) 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; From 9e7aaa7730c903435bf5cba457056b46dd51a954 Mon Sep 17 00:00:00 2001 From: dmwever Date: Wed, 29 Jan 2025 07:59:00 -0600 Subject: [PATCH 09/13] Update libopenage/pathfinding/cost_field.cpp Co-authored-by: Christoph Heine <6852422+heinezen@users.noreply.github.com> --- libopenage/pathfinding/cost_field.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libopenage/pathfinding/cost_field.cpp b/libopenage/pathfinding/cost_field.cpp index fc2ab402f5..ae7e20ea40 100644 --- a/libopenage/pathfinding/cost_field.cpp +++ b/libopenage/pathfinding/cost_field.cpp @@ -70,7 +70,9 @@ bool CostField::stamp(size_t idx, cost_t cost, const time::time_t &stamped_at) { } 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; + if (not 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; From a6cadf418971657b4a551d7456ab2ee6c5230078 Mon Sep 17 00:00:00 2001 From: dmwever Date: Mon, 10 Feb 2025 10:34:08 -0600 Subject: [PATCH 10/13] Update cost_field.cpp --- libopenage/pathfinding/cost_field.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/libopenage/pathfinding/cost_field.cpp b/libopenage/pathfinding/cost_field.cpp index ae7e20ea40..cc97b38daf 100644 --- a/libopenage/pathfinding/cost_field.cpp +++ b/libopenage/pathfinding/cost_field.cpp @@ -14,6 +14,7 @@ namespace openage::path { CostField::CostField(size_t size) : size{size}, valid_until{time::TIME_MIN}, + cost_stamps(this->size * this->size, COST_MIN), cells(this->size * this->size, COST_MIN) { log::log(DBG << "Created cost field with size " << this->size << "x" << this->size); } From 077d406c42e957281bddb5f9a7a0cef28f86b8c5 Mon Sep 17 00:00:00 2001 From: dmwever Date: Mon, 10 Feb 2025 10:35:34 -0600 Subject: [PATCH 11/13] stamp explicit initialization --- libopenage/pathfinding/cost_field.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/libopenage/pathfinding/cost_field.cpp b/libopenage/pathfinding/cost_field.cpp index cc97b38daf..df5e60bcf9 100644 --- a/libopenage/pathfinding/cost_field.cpp +++ b/libopenage/pathfinding/cost_field.cpp @@ -63,8 +63,10 @@ bool CostField::stamp(size_t idx, cost_t cost, const time::time_t &stamped_at) { } 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] = cost_stamp_t{ + orginal_cost, + stamped_at, + }; this->set_cost(idx, cost, stamped_at); return true; From be9ba8e2c8c0c688bcc27c0c31c5d5a8ca6daa55 Mon Sep 17 00:00:00 2001 From: dmwever Date: Mon, 10 Feb 2025 10:41:49 -0600 Subject: [PATCH 12/13] assign nullopt instead of time_t --- libopenage/pathfinding/cost_field.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libopenage/pathfinding/cost_field.cpp b/libopenage/pathfinding/cost_field.cpp index df5e60bcf9..522aaa2b1d 100644 --- a/libopenage/pathfinding/cost_field.cpp +++ b/libopenage/pathfinding/cost_field.cpp @@ -14,10 +14,10 @@ namespace openage::path { CostField::CostField(size_t size) : size{size}, valid_until{time::TIME_MIN}, - cost_stamps(this->size * this->size, COST_MIN), cells(this->size * this->size, COST_MIN) { - log::log(DBG << "Created cost field with size " << this->size << "x" << this->size); -} + log::log(DBG << "Created cost field with size " << this->size << "x" << this->size); + }, + cost_stamps(this->size * this->size, std::nullopt) size_t CostField::get_size() const { return this->size; From 8420d47ed52dfff5c228e078b76a5d557c891ece Mon Sep 17 00:00:00 2001 From: dmwever Date: Mon, 10 Feb 2025 10:44:05 -0600 Subject: [PATCH 13/13] typo --- libopenage/pathfinding/cost_field.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libopenage/pathfinding/cost_field.cpp b/libopenage/pathfinding/cost_field.cpp index 522aaa2b1d..9850c45d13 100644 --- a/libopenage/pathfinding/cost_field.cpp +++ b/libopenage/pathfinding/cost_field.cpp @@ -14,10 +14,10 @@ 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), + cost_stamps(this->size * this->size, std::nullopt) { log::log(DBG << "Created cost field with size " << this->size << "x" << this->size); - }, - cost_stamps(this->size * this->size, std::nullopt) + } size_t CostField::get_size() const { return this->size; @@ -64,7 +64,7 @@ bool CostField::stamp(size_t idx, cost_t cost, const time::time_t &stamped_at) { cost_t original_cost = this->get_cost(idx); this->cost_stamps[idx] = cost_stamp_t{ - orginal_cost, + original_cost, stamped_at, };