#include "LocationGameState.h" #include "game_server/InternalGameServer.h" #include #include #include namespace { int fail(const char* message) { std::cerr << message << '\n'; return EXIT_FAILURE; } } // namespace int main() { FRG::ClientLocationState localState; auto camera = localState.getOrCreate("test_location"); camera->cameraAzimuth = 0.75f; camera->cameraInclination = 1.25f; nlohmann::json localJson; localState.save(localJson); FRG::ClientLocationState restoredLocalState; restoredLocalState.load(localJson); const auto restoredCamera = restoredLocalState.getOrCreate("test_location"); if (restoredCamera->cameraAzimuth != 0.75f || restoredCamera->cameraInclination != 1.25f) { return fail("Local camera state did not round trip"); } FRG::LocationNetworkState networkLocationState; nlohmann::json networkLocationJson; networkLocationState.save(networkLocationJson); if (networkLocationJson.contains("cameraAzimuth") || networkLocationJson.contains("cameraInclination")) { return fail("Network location state contains local camera fields"); } const nlohmann::json authoritativeJson{ {"schemaVersion", FRG::GameState::kSchemaVersion}, {"campaignId", "test"} }; nlohmann::json persistenceJson; FRG::saveSinglePlayerPersistence( authoritativeJson, localState, persistenceJson); nlohmann::json restoredAuthoritativeJson; FRG::ClientLocationState restoredPersistenceLocalState; FRG::loadSinglePlayerPersistence( persistenceJson, restoredAuthoritativeJson, restoredPersistenceLocalState); if (restoredAuthoritativeJson != authoritativeJson || restoredPersistenceLocalState.getOrCreate("test_location") ->cameraAzimuth != 0.75f) { return fail("Composed single-player state did not round trip"); } if (persistenceJson.at("networkState").contains("localState")) { return fail("Local state leaked into authoritative state"); } const std::filesystem::path repositoryRoot = std::filesystem::path(__FILE__).parent_path().parent_path().parent_path(); std::filesystem::current_path(repositoryRoot); FRG::CampaignRegistry::instance().loadFromJson( "resources/config/campaigns.json"); const FRG::CampaignConfig* singlePlayerBattleZone = FRG::CampaignRegistry::instance().find("battle_zone"); const FRG::CampaignConfig* multiplayerBattleZoneConfig = FRG::CampaignRegistry::instance().find("battle_zone_multiplayer"); if (FRG::CampaignRegistry::instance().all().size() != 3 || !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"); } FRG::LocationDefinition definition; definition.config.id = "test_location"; FRG::NpcData npc; npc.id = "npc"; npc.name = "Test NPC"; npc.hp = 75.f; definition.npcs.push_back(npc); FRG::InteractiveObjectData object; object.id = "object"; object.base.name = "Test Object"; object.base.meshPath = "unused.mesh"; object.isActive = true; definition.interactiveObjects.push_back(object); FRG::TriggerZoneDefinition trigger; trigger.id = "trigger"; trigger.enabled = true; definition.triggerZones.push_back(trigger); FRG::TeleportDefinition teleport; teleport.id = "teleport"; teleport.active = false; definition.teleports.push_back(teleport); FRG::LocationGameState state; if (!state.setup(definition, {})) return fail("First setup must initialize runtime fields"); if (state.npcs.size() != 1 || state.interactiveObjects.size() != 1) return fail("Definitions were not applied"); state.npcs.at("npc")->hp = 12.f; state.interactiveObjects.at("object")->isActive = false; state.triggerZones.at("trigger").enabled = false; state.teleportZones.at("teleport") = true; state.hasPersistentState = true; if (state.setup(definition, {})) return fail("Persistent setup must not reinitialize runtime fields"); if (state.npcs.at("npc")->hp != 12.f || state.interactiveObjects.at("object")->isActive || state.triggerZones.at("trigger").enabled || !state.teleportZones.at("teleport")) { return fail("Repeated setup did not preserve runtime state"); } FRG::PlayerStateMap noPlayers; state.update(50, noPlayers, false); FRG::CampaignConfig battleZone; battleZone.id = "battle_zone"; battleZone.questsPath = "resources/config/quests_battle_zone.json"; FRG::InternalGameServer server; server.startCampaign(battleZone, {}, false, true); const auto snapshot = server.snapshot(); const auto bot = snapshot->players.find("battle_zone_test_bot"); if (bot == snapshot->players.end()) return fail("Battle Zone test bot was not created"); const auto stand = static_cast(FRG::AnimationState::STAND); const auto walk = static_cast(FRG::AnimationState::WALK); if (bot->second.state.animationFrameCounts[stand] <= 1 || bot->second.state.animationFrameCounts[walk] <= 1) { return fail("Battle Zone test bot animation metadata was not loaded"); } FRG::InternalGameServer networkServer; FRG::CampaignConfig multiplayerBattleZone = battleZone; multiplayerBattleZone.id = "battle_zone_multiplayer"; networkServer.startCampaign(multiplayerBattleZone, {}, true, false); const auto networkSnapshot = networkServer.snapshot(); if (networkSnapshot->campaignId != "battle_zone_multiplayer") return fail("Network campaign ID was not retained"); if (networkSnapshot->players.count("player") != 1) return fail("Network campaign shared player was not created"); if (networkSnapshot->players.count("battle_zone_test_bot") != 0) return fail("Network campaign unexpectedly created the test bot"); return EXIT_SUCCESS; }