Fixing bugs with pathfinding, bump version

This commit is contained in:
Vladislav Khorev committed 2026-11-15 21:46:30 +06:00
1 parent 8e2b019c92
commit f1cc28ab31
22 files changed
+1119 -759

No files matched your search

+3 -3
View File
@@ -152,9 +152,9 @@ int main(int argc, char** argv)
"' is missing from the initial state");
}
compatibilityFingerprint =
campaign->networkCompatibility.empty()
? "lockstep-v2:" + campaignId
: campaign->networkCompatibility;
FRG::makeNetworkCompatibilityFingerprint(*campaign);
std::cout << "[server] Compatibility fingerprint: "
<< compatibilityFingerprint << '\n';
if (!campaign->tutorial) {
server.submit(FRG::SetTutorialStepCommand{
FRG::TutorialStep::Completed});
+60 -121
View File
@@ -1,5 +1,6 @@
#include "LocationGameState.h"
#include "game_server/InternalGameServer.h"
#include <algorithm>
#include <cstdlib>
#include <filesystem>
#include <iostream>
@@ -12,6 +13,19 @@ int fail(const char* message)
return EXIT_FAILURE;
}
FRG::Vec3fp point(float x, float y, float z)
{
return {
static_cast<FRG::fp>(x),
static_cast<FRG::fp>(y),
static_cast<FRG::fp>(z)};
}
bool samePoint(const FRG::Vec3fp& left, const FRG::Vec3fp& right)
{
return left.x == right.x && left.y == right.y && left.z == right.z;
}
} // namespace
int main()
@@ -74,17 +88,34 @@ int main()
!singlePlayerBattleZone || !multiplayerBattleZoneConfig) {
return fail("Expected three campaigns including both Battle Zone modes");
}
if (singlePlayerBattleZone->startStatePath !=
multiplayerBattleZoneConfig->startStatePath) {
return fail("Battle Zone modes do not share their initial state");
if (singlePlayerBattleZone->startStatePath.empty() ||
multiplayerBattleZoneConfig->startStatePath.empty()) {
return fail("Battle Zone modes are missing initial state paths");
}
if (multiplayerBattleZoneConfig->networkPlayerSlots.size() != 2 ||
if (multiplayerBattleZoneConfig->networkPlayerSlots.size() != 4 ||
multiplayerBattleZoneConfig->networkPlayerSlots[0] != "player1" ||
multiplayerBattleZoneConfig->networkPlayerSlots[1] !=
"player2" ||
multiplayerBattleZoneConfig->networkPlayerSlots[2] != "player3" ||
multiplayerBattleZoneConfig->networkPlayerSlots[3] != "player4" ||
multiplayerBattleZoneConfig->networkCompatibility.empty()) {
return fail("Multiplayer player slots were not loaded");
}
const std::string compatibilityFingerprint =
FRG::makeNetworkCompatibilityFingerprint(
*multiplayerBattleZoneConfig);
FRG::CampaignConfig reorderedContent = *multiplayerBattleZoneConfig;
if (reorderedContent.locations.empty() ||
reorderedContent.locations.front().navigationJsonPaths.size() < 2) {
return fail("Expected multiple navigation maps for fingerprint test");
}
std::reverse(
reorderedContent.locations.front().navigationJsonPaths.begin(),
reorderedContent.locations.front().navigationJsonPaths.end());
if (compatibilityFingerprint ==
FRG::makeNetworkCompatibilityFingerprint(reorderedContent)) {
return fail("Compatibility fingerprint ignored navigation-map order");
}
FRG::LocationDefinition definition;
definition.config.id = "test_location";
@@ -152,18 +183,18 @@ int main()
hostileNpc.enabled = true;
hostileNpc.canAttack = true;
hostileNpc.hp = hostileNpc.initialHp = 75.f;
hostileNpc.position = Eigen::Vector3f::Zero();
hostileNpc.homePosition = {5.f, 0.f, 0.f};
hostileNpc.position = point(0.f, 0.f, 0.f);
hostileNpc.homePosition = point(5.f, 0.f, 0.f);
hostileNpc.stopInPlace();
auto playerOne = std::make_shared<FRG::CharacterState>();
playerOne->configureAsPlayer();
playerOne->enabled = true;
playerOne->position = {3.f, 0.f, 0.f};
playerOne->position = point(3.f, 0.f, 0.f);
auto playerTwo = std::make_shared<FRG::CharacterState>();
playerTwo->configureAsPlayer();
playerTwo->enabled = true;
playerTwo->position = {2.f, 0.f, 0.f};
playerTwo->position = point(2.f, 0.f, 0.f);
FRG::PlayerStateMap combatPlayers{
{"player1", playerOne}, {"player2", playerTwo}};
@@ -172,8 +203,8 @@ int main()
return fail("Hostile NPC did not select the nearest player");
}
playerOne->position = {1.f, 0.f, 0.f};
playerTwo->position = {4.f, 0.f, 0.f};
playerOne->position = point(1.f, 0.f, 0.f);
playerTwo->position = point(4.f, 0.f, 0.f);
state.update(100, combatPlayers, false);
if (hostileNpc.attackTargetId != "player2") {
return fail("Hostile NPC did not retain its living target lock");
@@ -189,64 +220,12 @@ int main()
state.update(100, combatPlayers, false);
if (!hostileNpc.attackTargetId.empty() ||
hostileNpc.battle_state != 0 ||
(hostileNpc.requestedWalkTarget - hostileNpc.homePosition).norm() >
FRG::CharacterState::kWalkThreshold) {
(hostileNpc.requestedWalkTarget -
hostileNpc.homePosition).normSquared() >
FRG::CharacterState::kWalkThreshold_SQUARED()) {
return fail("Hostile NPC did not return home after losing its target");
}
auto blocker = std::make_shared<FRG::CharacterState>();
blocker->enabled = true;
blocker->hp = 50.f;
blocker->position = {0.8f, 0.f, 0.f};
state.npcs["blocker"] = blocker;
hostileNpc.position = Eigen::Vector3f::Zero();
hostileNpc.homePosition = {5.f, 0.f, 0.f};
hostileNpc.attackTargetId = "player1";
hostileNpc.requestedWalkTarget = playerOne->position;
hostileNpc.walkTarget = playerOne->position;
hostileNpc.pathWaypoints = {playerOne->position};
hostileNpc.currentWaypointIndex = 0;
hostileNpc.movementBlockedTicks = 0;
playerOne->enabled = true;
playerOne->hp = 75.f;
playerOne->position = {4.f, 0.f, 0.f};
playerTwo->enabled = true;
playerTwo->hp = 75.f;
playerTwo->position = {2.f, 0.f, 0.f};
bool blockedRetryRequested = false;
for (int tick = 0; tick < 10 && !blockedRetryRequested; ++tick) {
const FRG::LocationSimulationEvents events =
state.update(100, combatPlayers, false);
for (const auto& event : events.characters) {
if (event.characterId == "npc" &&
event.events.forceNavigationReplan) {
blockedRetryRequested = true;
if (!state.replanCharacter(
"npc", combatPlayers,
event.events.blockingCharacterId)) {
return fail("Blocked NPC replan was rejected");
}
}
}
}
if (!blockedRetryRequested ||
hostileNpc.attackTargetId != "player2" ||
(hostileNpc.requestedWalkTarget - playerTwo->position).norm() >
FRG::CharacterState::kWalkThreshold) {
return fail("Blocked NPC did not switch to the nearest player");
}
playerOne->enabled = false;
playerTwo->enabled = false;
if (!state.replanCharacter("npc", combatPlayers, "blocker") ||
!hostileNpc.attackTargetId.empty() ||
hostileNpc.battle_state != 0 ||
(hostileNpc.requestedWalkTarget - hostileNpc.homePosition).norm() >
FRG::CharacterState::kWalkThreshold) {
return fail("Blocked NPC did not return home without an eligible target");
}
FRG::CampaignConfig battleZone;
battleZone.id = "battle_zone";
battleZone.questsPath = "resources/config/quests_battle_zone.json";
@@ -295,8 +274,9 @@ int main()
return fail(
"Loaded multiplayer slot did not restore animation metadata");
}
if (!loadedNetworkBot->second.state.position.isApprox(
Eigen::Vector3f(4.f, 0.f, 5.f))) {
if (!samePoint(
loadedNetworkBot->second.state.position,
point(4.f, 0.f, 5.f))) {
return fail("Loaded multiplayer slot position was overwritten");
}
if (loadedNetworkBot->second.state.enabled)
@@ -304,19 +284,19 @@ int main()
const std::string locationId = loadedNetworkSnapshot->currentLocationId;
networkServer.submit(FRG::SetCharacterPositionCommand{
locationId, "player2", Eigen::Vector3f(7.f, 0.f, 8.f), true});
locationId, "player2", point(7.f, 0.f, 8.f), true});
networkServer.submit(FRG::SetCharacterFacingAngleCommand{
locationId, "player2", 1.5f, true});
locationId, "player2", FRG::fp(1.5f), true});
networkServer.submit(FRG::SetCharacterFaceTargetCommand{
locationId, "player2", "player1"});
networkServer.submit(FRG::SetCharacterEnabledCommand{
locationId, "player2", false});
const auto commandSnapshot = networkServer.snapshot();
const auto commandCharacter = commandSnapshot->players.at("player2").state;
if (!commandCharacter.position.isApprox(Eigen::Vector3f(7.f, 0.f, 8.f)) ||
!commandCharacter.homePosition.isApprox(Eigen::Vector3f(7.f, 0.f, 8.f)) ||
commandCharacter.facingAngle != 1.5f ||
commandCharacter.targetFacingAngle != 1.5f ||
if (!samePoint(commandCharacter.position, point(7.f, 0.f, 8.f)) ||
!samePoint(commandCharacter.homePosition, point(7.f, 0.f, 8.f)) ||
commandCharacter.facingAngle != FRG::fp(1.5f) ||
commandCharacter.targetFacingAngle != FRG::fp(1.5f) ||
!commandCharacter.faceTargetId.empty() ||
commandCharacter.enabled) {
return fail("Semantic character commands were not applied");
@@ -330,61 +310,20 @@ int main()
}
FRG::CharacterState guardedMover;
guardedMover.configureAsPlayer();
guardedMover.enabled = true;
guardedMover.position = Eigen::Vector3f::Zero();
guardedMover.walkTarget = Eigen::Vector3f(2.f, 0.f, 0.f);
guardedMover.requestedWalkTarget = guardedMover.walkTarget;
guardedMover.pathWaypoints = {guardedMover.walkTarget};
const std::vector<FRG::CharacterMovementObstacle> movementObstacles{
{"mover", Eigen::Vector3f::Zero(), 0.45f},
{"blocker", Eigen::Vector3f(0.8f, 0.f, 0.f), 0.45f}};
FRG::CharacterUpdateContext guardedContext;
guardedContext.characterId = "mover";
guardedContext.movementObstacles = &movementObstacles;
FRG::CharacterUpdateEvents blockedEvents;
for (int i = 0; i < 3; ++i)
blockedEvents = guardedMover.update(100, guardedContext);
if (!guardedMover.position.isZero() ||
blockedEvents.forceNavigationReplan ||
!guardedMover.pathWaypoints.empty()) {
return fail("Movement guard did not cancel the blocked player action");
}
nlohmann::json guardedJson;
guardedMover.velocity = {
FRG::fp::from_raw_value(12345),
FRG::fp::from_raw_value(-6789)};
guardedMover.save(guardedJson);
FRG::CharacterState restoredGuardedMover;
restoredGuardedMover.load(guardedJson);
if (restoredGuardedMover.movementBlockedTicks !=
guardedMover.movementBlockedTicks ||
restoredGuardedMover.navigationRetryTicks !=
guardedMover.navigationRetryTicks) {
return fail("Movement blockage state did not round trip");
}
FRG::LocationGameState occupancyState;
auto stationary = std::make_shared<FRG::CharacterState>();
stationary->enabled = true;
stationary->hp = 10.f;
stationary->position = Eigen::Vector3f(4.f, 0.f, 0.f);
auto moving = std::make_shared<FRG::CharacterState>();
moving->enabled = true;
moving->hp = 10.f;
moving->pathWaypoints = {Eigen::Vector3f(8.f, 0.f, 0.f)};
moving->movedLastTick = true;
moving->attackTargetId = "stationary";
FRG::PlayerStateMap occupancyPlayers{
{"stationary", stationary}, {"moving", moving}};
if (occupancyState.collectPathObstacles(
"requester", occupancyPlayers).size() != 1 ||
occupancyState.collectPathObstacles(
"requester", occupancyPlayers, "moving").size() != 2 ||
!occupancyState.collectPathObstacles(
"stationary", occupancyPlayers).empty() ||
!occupancyState.collectPathObstacles(
"moving", occupancyPlayers).empty() ||
occupancyState.collectPathObstacles(
"moving", occupancyPlayers, "stationary").size() != 1) {
return fail("Stationary obstacle collection policy is incorrect");
guardedMover.navigationRetryTicks ||
restoredGuardedMover.velocity.x.raw_value() != 12345 ||
restoredGuardedMover.velocity.y.raw_value() != -6789) {
return fail("Character velocity did not round trip");
}
return EXIT_SUCCESS;
+14 -4
View File
@@ -13,6 +13,16 @@ void require(bool condition, const char* message)
std::exit(EXIT_FAILURE);
}
FRG::Vec3fp point(float x, float y, float z)
{
return {FRG::fp(x), FRG::fp(y), FRG::fp(z)};
}
bool samePoint(const FRG::Vec3fp& left, const FRG::Vec3fp& right)
{
return left.x == right.x && left.y == right.y && left.z == right.z;
}
} // namespace
int main()
@@ -49,7 +59,7 @@ int main()
FRG::DestinationInputMessage input;
input.clientSequence = 7;
input.target = Eigen::Vector3f(1.234f, 0.f, -5.678f);
input.target = point(1.234f, 0.f, -5.678f);
FRG::DestinationInputMessage parsedInput;
require(
FRG::parseDestinationInputMessage(
@@ -57,7 +67,7 @@ int main()
"destination input did not round trip");
require(
parsedInput.clientSequence == 7 &&
parsedInput.target.isApprox(Eigen::Vector3f(1.23f, 0.f, -5.68f)),
samePoint(parsedInput.target, input.target),
"destination input was not normalized");
FRG::CharacterActionInputMessage actionInput{
@@ -147,9 +157,9 @@ int main()
FRG::LockstepTimeline timeline(2);
const std::uint64_t firstSequence =
timeline.schedule("player1", Eigen::Vector3f(1.f, 0.f, 1.f));
timeline.schedule("player1", point(1.f, 0.f, 1.f));
const std::uint64_t secondSequence =
timeline.schedule("player2", Eigen::Vector3f(2.f, 0.f, 2.f));
timeline.schedule("player2", point(2.f, 0.f, 2.f));
const FRG::TickMessage emptyTick = timeline.advance();
require(
emptyTick.tickId == 1 && emptyTick.commands.empty(),
+105 -16
View File
@@ -16,6 +16,16 @@ void require(bool condition, const char* message)
std::exit(EXIT_FAILURE);
}
FRG::Vec3fp point(float x, float y, float z)
{
return {FRG::fp(x), FRG::fp(y), FRG::fp(z)};
}
bool samePoint(const FRG::Vec3fp& left, const FRG::Vec3fp& right)
{
return left.x == right.x && left.y == right.y && left.z == right.z;
}
template <class Predicate>
bool waitFor(Predicate predicate)
{
@@ -28,6 +38,15 @@ bool waitFor(Predicate predicate)
return false;
}
bool sameMovement(
const FRG::CharacterState& left, const FRG::CharacterState& right)
{
return left.position.x.raw_value() == right.position.x.raw_value() &&
left.position.z.raw_value() == right.position.z.raw_value() &&
left.velocity.x.raw_value() == right.velocity.x.raw_value() &&
left.velocity.y.raw_value() == right.velocity.y.raw_value();
}
} // namespace
int main()
@@ -70,7 +89,22 @@ int main()
auto hostileNpc = std::make_shared<FRG::CharacterState>();
hostileNpc->enabled = true;
hostileNpc->hp = 200.f;
hostileNpc->position = point(-1.f, 0.f, 0.f);
hostileNpc->walkTarget = point(4.f, 0.f, 0.f);
hostileNpc->requestedWalkTarget = hostileNpc->walkTarget;
hostileNpc->pathWaypoints = {hostileNpc->walkTarget};
hostileNpc->velocity = {FRG::fp(1.5f), FRG::fp(0.f)};
testLocation.npcs.emplace("hostile_npc", std::move(hostileNpc));
auto secondHostileNpc = std::make_shared<FRG::CharacterState>();
secondHostileNpc->enabled = true;
secondHostileNpc->hp = 200.f;
secondHostileNpc->position = point(1.f, 0.f, 0.f);
secondHostileNpc->walkTarget = point(-4.f, 0.f, 0.f);
secondHostileNpc->requestedWalkTarget = secondHostileNpc->walkTarget;
secondHostileNpc->pathWaypoints = {secondHostileNpc->walkTarget};
secondHostileNpc->velocity = {FRG::fp(-1.5f), FRG::fp(0.f)};
testLocation.npcs.emplace(
"hostile_npc_2", std::move(secondHostileNpc));
nlohmann::json testLocationState;
testLocation.save(testLocationState);
initialState["currentLocationId"] = "test_location";
@@ -93,6 +127,22 @@ int main()
"client did not receive initial state");
require(client->localPlayerId() == "player1", "wrong player slot assigned");
// Advance avoidance before the second client joins. Its initial snapshot must
// carry previous-tick velocity or its very next ORCA result will diverge.
server.update();
require(
waitFor([&]() {
client->update(0);
const auto first = client->snapshot();
const auto host = authoritative.snapshot();
return sameMovement(
first->locations.at("test_location").npcs.at(
"hostile_npc").state,
host->locations.at("test_location").npcs.at(
"hostile_npc").state);
}),
"first client did not apply the pre-join movement tick");
auto secondClient = std::make_unique<FRG::WebsocketGameClient>(
"127.0.0.1", std::to_string(server.port()), "/");
require(
@@ -126,10 +176,34 @@ int main()
authoritative.snapshot()->players.at("player2").state.enabled,
"connection presence was not broadcast to all replicas");
for (int tick = 0; tick < 6; ++tick) server.update();
require(
waitFor([&]() {
client->update(0);
secondClient->update(0);
const auto first = client->snapshot();
const auto second = secondClient->snapshot();
const auto host = authoritative.snapshot();
for (const char* id : {"hostile_npc", "hostile_npc_2"}) {
const auto& hostState =
host->locations.at("test_location").npcs.at(id).state;
if (!sameMovement(
first->locations.at("test_location").npcs.at(id).state,
hostState) ||
!sameMovement(
second->locations.at("test_location").npcs.at(id).state,
hostState)) {
return false;
}
}
return true;
}),
"late-joining client diverged during character avoidance");
client->submit(FRG::SetCharacterDestinationCommand{
{}, "spoofed", Eigen::Vector3f(1.234f, 0.f, 2.345f), "ignored"});
{}, "spoofed", point(1.234f, 0.f, 2.345f), "ignored"});
secondClient->submit(FRG::SetCharacterDestinationCommand{
{}, "spoofed", Eigen::Vector3f(-1.234f, 0.f, 3.456f), "ignored"});
{}, "spoofed", point(-1.234f, 0.f, 3.456f), "ignored"});
std::this_thread::sleep_for(std::chrono::milliseconds(50));
server.update();
server.update();
@@ -140,17 +214,23 @@ int main()
const auto first = client->snapshot();
const auto second = secondClient->snapshot();
const auto host = authoritative.snapshot();
return first->players.at("player1").state.requestedWalkTarget.isApprox(
Eigen::Vector3f(1.23f, 0.f, 2.35f)) &&
second->players.at("player2").state.requestedWalkTarget.isApprox(
Eigen::Vector3f(-1.23f, 0.f, 3.46f)) &&
first->players.at("player2").state.requestedWalkTarget.isApprox(
return samePoint(
first->players.at("player1").state.requestedWalkTarget,
point(1.234f, 0.f, 2.345f)) &&
samePoint(
second->players.at("player2").state.requestedWalkTarget,
point(-1.234f, 0.f, 3.456f)) &&
samePoint(
first->players.at("player2").state.requestedWalkTarget,
second->players.at("player2").state.requestedWalkTarget) &&
second->players.at("player1").state.requestedWalkTarget.isApprox(
samePoint(
second->players.at("player1").state.requestedWalkTarget,
first->players.at("player1").state.requestedWalkTarget) &&
host->players.at("player1").state.requestedWalkTarget.isApprox(
samePoint(
host->players.at("player1").state.requestedWalkTarget,
first->players.at("player1").state.requestedWalkTarget) &&
host->players.at("player2").state.requestedWalkTarget.isApprox(
samePoint(
host->players.at("player2").state.requestedWalkTarget,
second->players.at("player2").state.requestedWalkTarget);
});
if (!destinationsConverged) {
@@ -160,13 +240,13 @@ int main()
std::cerr << "destination revisions " << first->revision << ", "
<< second->revision << ", " << host->revision << "\n"
<< "player targets "
<< first->players.at("player1").state.requestedWalkTarget.transpose()
<< first->players.at("player1").state.requestedWalkTarget.toEigen().transpose()
<< " / "
<< host->players.at("player1").state.requestedWalkTarget.transpose()
<< host->players.at("player1").state.requestedWalkTarget.toEigen().transpose()
<< "\nplayer2 targets "
<< second->players.at("player2").state.requestedWalkTarget.transpose()
<< second->players.at("player2").state.requestedWalkTarget.toEigen().transpose()
<< " / "
<< host->players.at("player2").state.requestedWalkTarget.transpose()
<< host->players.at("player2").state.requestedWalkTarget.toEigen().transpose()
<< std::endl;
}
require(
@@ -218,8 +298,17 @@ int main()
waitFor([&]() {
client->update(0);
secondClient->update(0);
return client->snapshot()->revision ==
secondClient->snapshot()->revision;
const auto host = authoritative.snapshot();
return sameMovement(
client->snapshot()->locations.at("test_location").npcs.at(
"hostile_npc").state,
host->locations.at("test_location").npcs.at(
"hostile_npc").state) &&
sameMovement(
secondClient->snapshot()->locations.at(
"test_location").npcs.at("hostile_npc").state,
host->locations.at("test_location").npcs.at(
"hostile_npc").state);
}),
"attack combo command was not rebroadcast in lockstep");