mirror of
https://github.com/nolen777/eagle0.git
synced 2026-07-29 08:35:42 +00:00
Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
df9be618bf | ||
|
|
7a15b1411c | ||
|
|
4dc18ab2c3 | ||
|
|
3ceabaa7d2 | ||
|
|
706931ad52 | ||
|
|
1e7e56523e |
@@ -99,13 +99,49 @@ void ShardokEngine::ApplyAndAddActionResults(const vector<ActionResultProto> &re
|
||||
void ShardokEngine::ApplyAndAddActionResult(const ActionResultProto &result) {
|
||||
MutatingApplyResult(gameState, result, settingsGetter);
|
||||
|
||||
// Invalidate cache since game state has changed
|
||||
InvalidateGameStateCache();
|
||||
|
||||
if (trackHistory) {
|
||||
actionHistory.emplace_back();
|
||||
*actionHistory.back().mutable_action_result() = result;
|
||||
*actionHistory.back().mutable_state_after_fb() = gameState.ToByteString();
|
||||
*actionHistory.back().mutable_state_after_fb() = GetCachedGameStateBytes();
|
||||
}
|
||||
}
|
||||
|
||||
auto ShardokEngine::GetCachedGameStateBytes() const -> const std::string & {
|
||||
// Generate a simple hash of the game state to detect changes
|
||||
// Using the game state pointer and a few key fields as a proxy for state change detection
|
||||
const auto *gs = gameState.Get();
|
||||
size_t stateHash = std::hash<const void *>{}(gs);
|
||||
|
||||
// Include key mutable fields that would indicate state changes
|
||||
if (gs->units() && gs->units()->size() > 0) {
|
||||
stateHash ^= std::hash<size_t>{}(gs->units()->size()) << 1;
|
||||
// Add hash of first and last unit if available for better change detection
|
||||
if (gs->units()->size() > 0) {
|
||||
stateHash ^= std::hash<uint32_t>{}(gs->units()->Get(0)->unit_id()) << 2;
|
||||
stateHash ^= std::hash<uint32_t>{}(gs->units()->Get(gs->units()->size() - 1)->unit_id())
|
||||
<< 3;
|
||||
}
|
||||
}
|
||||
|
||||
if (gs->hex_map() && gs->hex_map()->terrain()) {
|
||||
stateHash ^= std::hash<size_t>{}(gs->hex_map()->terrain()->size()) << 4;
|
||||
}
|
||||
|
||||
// Check if we have a cached version for this state
|
||||
if (cachedGameStateBytes && cachedGameStateBytes->first == stateHash) {
|
||||
return cachedGameStateBytes->second;
|
||||
}
|
||||
|
||||
// Cache miss or invalidated - serialize and cache the result
|
||||
std::string serializedBytes = gameState.ToByteString();
|
||||
cachedGameStateBytes = std::make_pair(stateHash, std::move(serializedBytes));
|
||||
|
||||
return cachedGameStateBytes->second;
|
||||
}
|
||||
|
||||
ShardokEngine::ShardokEngine(
|
||||
const GameSettingsSPtr &settings,
|
||||
const vector<ShardokActionWithResultingState> &history,
|
||||
|
||||
@@ -56,10 +56,19 @@ private:
|
||||
|
||||
mutable CommandListSPtr cachedAvailableCommands{};
|
||||
|
||||
// Game state serialization cache to avoid expensive ToByteString() calls
|
||||
mutable std::optional<std::pair<size_t, std::string>> cachedGameStateBytes;
|
||||
|
||||
void ApplyAndAddActionResult(const ActionResult &result);
|
||||
|
||||
void ApplyAndAddActionResults(const vector<ActionResult> &results);
|
||||
|
||||
// Helper method to invalidate game state cache when state changes
|
||||
void InvalidateGameStateCache() const { cachedGameStateBytes.reset(); }
|
||||
|
||||
// Helper method to get cached game state bytes with automatic caching
|
||||
[[nodiscard]] auto GetCachedGameStateBytes() const -> const std::string &;
|
||||
|
||||
[[nodiscard]] auto HandleUnitFallingIntoWater(
|
||||
const Terrain *terrain,
|
||||
const net::eagle0::shardok::storage::fb::Unit *unit,
|
||||
|
||||
Reference in New Issue
Block a user