-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 dirty flag and integrate #1725
base: master
Are you sure you want to change the base?
Changes from all commits
9ea8b58
a12b347
73647d4
5a17e4e
22e594f
ce77c9f
d33d4f7
3adb802
6251a72
fd72608
b9f6bd1
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 |
---|---|---|
|
@@ -11,6 +11,8 @@ | |
#include "pathfinding/portal.h" | ||
#include "pathfinding/sector.h" | ||
#include "util/timer.h" | ||
#include "time/time_loop.h" | ||
#include "time/clock.h" | ||
|
||
#include "renderer/gui/integration/public/gui_application_with_logger.h" | ||
#include "renderer/opengl/window.h" | ||
|
@@ -25,13 +27,17 @@ | |
namespace openage::path::tests { | ||
|
||
void path_demo_1(const util::Path &path) { | ||
auto time_loop = std::make_shared<time::TimeLoop>(); | ||
time_loop->run(); | ||
auto clock = time_loop->get_clock(); | ||
Comment on lines
+30
to
+32
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. What are you intending to use the time loop and clock for? The changes you made to the demo code do not make it very clear and I don't understand why we need a clock at all in this demo. Also, the demo will deadlock in line 31 currently because |
||
auto grid = std::make_shared<Grid>(0, util::Vector2s{4, 3}, 10); | ||
|
||
const time::time_t time = clock->get_time(); | ||
// Initialize the cost field for each sector. | ||
for (auto §or : grid->get_sectors()) { | ||
auto cost_field = sector->get_cost_field(); | ||
std::vector<cost_t> sector_cost = sectors_cost.at(sector->get_id()); | ||
cost_field->set_costs(std::move(sector_cost)); | ||
cost_field->set_costs(std::move(sector_cost), time); | ||
} | ||
|
||
// Initialize portals between sectors. | ||
|
@@ -87,16 +93,20 @@ void path_demo_1(const util::Path &path) { | |
coord::tile start{2, 26}; | ||
coord::tile target{36, 2}; | ||
|
||
const time::time_t request_time = clock->get_time(); | ||
|
||
PathRequest path_request{ | ||
grid->get_id(), | ||
start, | ||
target, | ||
request_time | ||
}; | ||
|
||
grid->init_portal_nodes(); | ||
timer.start(); | ||
Path path_result = pathfinder->get_path(path_request); | ||
timer.stop(); | ||
|
||
log::log(INFO << "Pathfinding request at " << request_time); | ||
log::log(INFO << "Pathfinding took " << timer.getval() / 1000 << " us"); | ||
|
||
// Create a renderer to display the grid and path | ||
|
@@ -127,6 +137,7 @@ void path_demo_1(const util::Path &path) { | |
grid->get_id(), | ||
start, | ||
target, | ||
clock->get_time() | ||
}; | ||
|
||
timer.reset(); | ||
|
@@ -147,6 +158,7 @@ void path_demo_1(const util::Path &path) { | |
grid->get_id(), | ||
start, | ||
target, | ||
clock->get_time() | ||
}; | ||
|
||
timer.reset(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2024-2024 the openage authors. See copying.md for legal info. | ||
|
||
#include "field_cache.h" | ||
|
||
namespace openage::path { | ||
|
||
void FieldCache::add(cache_key_t cache_key, | ||
field_cache_t cache_entry) { | ||
this->cache[cache_key] = cache_entry; | ||
} | ||
|
||
void FieldCache::evict(cache_key_t cache_key) { | ||
this->cache.erase(cache_key); | ||
} | ||
|
||
bool FieldCache::is_cached(cache_key_t cache_key) { | ||
return this->cache.contains(cache_key); | ||
} | ||
|
||
std::shared_ptr<IntegrationField> FieldCache::get_integration_field(cache_key_t cache_key) { | ||
auto cached = this->cache.find(cache_key); | ||
return cached->second.first; | ||
} | ||
|
||
std::shared_ptr<FlowField> FieldCache::get_flow_field(cache_key_t cache_key) { | ||
auto cached = this->cache.find(cache_key); | ||
return cached->second.second; | ||
} | ||
|
||
} // namespace openage::path |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2024-2024 the openage authors. See copying.md for legal info. | ||
|
||
#pragma once | ||
|
||
#include <memory> | ||
#include <unordered_map> | ||
|
||
#include "pathfinding/types.h" | ||
#include "util/hash.h" | ||
|
||
namespace openage { | ||
namespace coord { | ||
struct tile_delta; | ||
} // namespace coord | ||
|
||
namespace path { | ||
class IntegrationField; | ||
class FlowField; | ||
|
||
/** | ||
* Cache to store already calculated flow and integration fields for the pathfinding algorithm. | ||
*/ | ||
class FieldCache { | ||
public: | ||
FieldCache() = default; | ||
~FieldCache() = default; | ||
|
||
/** | ||
* Adds a new field cache entry to the cache with a given portal and sector cache key. | ||
*/ | ||
void add(cache_key_t cache_key, | ||
field_cache_t cache_entry); | ||
|
||
/** | ||
* Evicts a given field cache entry from the cache at the given cache key. | ||
*/ | ||
void evict(cache_key_t cache_key); | ||
|
||
/** | ||
* Checks if there is a cached entry at a specific cache key. | ||
*/ | ||
bool is_cached(cache_key_t cache_key); | ||
|
||
/** | ||
* Gets the integration field from a given cache entry. | ||
*/ | ||
std::shared_ptr<IntegrationField> get_integration_field(cache_key_t cache_key); | ||
|
||
/** | ||
* Gets the flow field from a given cache entry. | ||
*/ | ||
std::shared_ptr<FlowField> get_flow_field(cache_key_t cache_key); | ||
|
||
private: | ||
/** | ||
* Hash function for the field cache. | ||
*/ | ||
struct pair_hash { | ||
template <class T1, class T2> | ||
std::size_t operator()(const std::pair<T1, T2> &pair) const { | ||
return util::hash_combine(std::hash<T1>{}(pair.first), std::hash<T2>{}(pair.second)); | ||
} | ||
}; | ||
|
||
/** | ||
* Cache for already computed fields. | ||
* | ||
* Key is the portal ID and the sector ID from which the field was entered. Fields that are cached are | ||
* cleared of dynamic flags, i.e. wavefront or LOS flags. These have to be recalculated | ||
* when the field is reused. | ||
*/ | ||
std::unordered_map<cache_key_t, | ||
field_cache_t, | ||
pair_hash> | ||
cache; | ||
}; | ||
|
||
} // 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 method should be called
clear_dirty