Separating state out of the location

This commit is contained in:
Vladislav Khorev committed 2026-10-30 14:56:24 +06:00
1 parent 5e9a663812
commit 297bd7377f
45 files changed
+2114 -778

No files matched your search

+129
View File
@@ -0,0 +1,129 @@
cmake_minimum_required(VERSION 3.16)
project(GameServer LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(GAME_ROOT "${CMAKE_CURRENT_LIST_DIR}/..")
find_package(Threads REQUIRED)
find_package(Eigen3 3.3 CONFIG QUIET)
if(NOT TARGET Eigen3::Eigen)
set(EIGEN_BUNDLED_DIR "${GAME_ROOT}/thirdparty/eigen-5.0.0")
if(NOT EXISTS "${EIGEN_BUNDLED_DIR}/Eigen/Core")
message(FATAL_ERROR
"Eigen3 was not found. Install Eigen3 or configure the graphical "
"project once to populate thirdparty/eigen-5.0.0.")
endif()
add_library(game_server_eigen INTERFACE)
target_include_directories(game_server_eigen INTERFACE "${EIGEN_BUNDLED_DIR}")
add_library(Eigen3::Eigen ALIAS game_server_eigen)
endif()
add_library(game_server_core STATIC
"${GAME_ROOT}/src/Campaign.cpp"
"${GAME_ROOT}/src/Campaign.h"
"${GAME_ROOT}/src/CharacterState.cpp"
"${GAME_ROOT}/src/CharacterState.h"
"${GAME_ROOT}/src/GameState.cpp"
"${GAME_ROOT}/src/GameState.h"
"${GAME_ROOT}/src/ISaveable.h"
"${GAME_ROOT}/src/Localization.cpp"
"${GAME_ROOT}/src/Localization.h"
"${GAME_ROOT}/src/LocationGameState.cpp"
"${GAME_ROOT}/src/LocationGameState.h"
"${GAME_ROOT}/src/LocationState.cpp"
"${GAME_ROOT}/src/LocationState.h"
"${GAME_ROOT}/src/NpcCar.cpp"
"${GAME_ROOT}/src/NpcCar.h"
"${GAME_ROOT}/src/content/LocationDefinition.cpp"
"${GAME_ROOT}/src/content/LocationDefinition.h"
"${GAME_ROOT}/src/game_server/IGameServer.h"
"${GAME_ROOT}/src/game_server/IGameServerLifecycle.h"
"${GAME_ROOT}/src/game_server/InternalGameServer.cpp"
"${GAME_ROOT}/src/game_server/InternalGameServer.h"
"${GAME_ROOT}/src/items/InteractiveObjectState.cpp"
"${GAME_ROOT}/src/items/InteractiveObjectState.h"
"${GAME_ROOT}/src/items/Inventory.cpp"
"${GAME_ROOT}/src/items/Item.h"
"${GAME_ROOT}/src/navigation/PathFinder.cpp"
"${GAME_ROOT}/src/navigation/PathFinder.h"
"${GAME_ROOT}/src/quest/QuestJournal.cpp"
"${GAME_ROOT}/src/quest/QuestJournal.h"
"${GAME_ROOT}/src/quest/QuestTypes.h"
"${GAME_ROOT}/src/utils/HeadlessUtils.cpp"
"${GAME_ROOT}/src/utils/Utils.h"
)
source_group(TREE "${GAME_ROOT}/src" PREFIX "Source" FILES
"${GAME_ROOT}/src/Campaign.cpp"
"${GAME_ROOT}/src/Campaign.h"
"${GAME_ROOT}/src/CharacterState.cpp"
"${GAME_ROOT}/src/CharacterState.h"
"${GAME_ROOT}/src/GameState.cpp"
"${GAME_ROOT}/src/GameState.h"
"${GAME_ROOT}/src/ISaveable.h"
"${GAME_ROOT}/src/Localization.cpp"
"${GAME_ROOT}/src/Localization.h"
"${GAME_ROOT}/src/LocationGameState.cpp"
"${GAME_ROOT}/src/LocationGameState.h"
"${GAME_ROOT}/src/LocationState.cpp"
"${GAME_ROOT}/src/LocationState.h"
"${GAME_ROOT}/src/NpcCar.cpp"
"${GAME_ROOT}/src/NpcCar.h"
"${GAME_ROOT}/src/content/LocationDefinition.cpp"
"${GAME_ROOT}/src/content/LocationDefinition.h"
"${GAME_ROOT}/src/game_server/IGameServer.h"
"${GAME_ROOT}/src/game_server/IGameServerLifecycle.h"
"${GAME_ROOT}/src/game_server/InternalGameServer.cpp"
"${GAME_ROOT}/src/game_server/InternalGameServer.h"
"${GAME_ROOT}/src/items/InteractiveObjectState.cpp"
"${GAME_ROOT}/src/items/InteractiveObjectState.h"
"${GAME_ROOT}/src/items/Inventory.cpp"
"${GAME_ROOT}/src/items/Item.h"
"${GAME_ROOT}/src/navigation/PathFinder.cpp"
"${GAME_ROOT}/src/navigation/PathFinder.h"
"${GAME_ROOT}/src/quest/QuestJournal.cpp"
"${GAME_ROOT}/src/quest/QuestJournal.h"
"${GAME_ROOT}/src/quest/QuestTypes.h"
"${GAME_ROOT}/src/utils/HeadlessUtils.cpp"
"${GAME_ROOT}/src/utils/Utils.h"
)
target_include_directories(game_server_core PUBLIC "${GAME_ROOT}/src")
target_link_libraries(game_server_core PUBLIC Eigen3::Eigen Threads::Threads)
target_compile_definitions(game_server_core PRIVATE FRG_HEADLESS_SERVER=1)
if(MSVC)
target_compile_options(game_server_core PRIVATE /W4 /permissive-)
else()
target_compile_options(game_server_core PRIVATE -Wall -Wextra -Wpedantic)
endif()
add_executable(GameServer src/main.cpp README.md)
source_group("Server" FILES src/main.cpp README.md)
target_link_libraries(GameServer PRIVATE game_server_core)
include(CTest)
if(BUILD_TESTING)
add_executable(
GameServerStateTests tests/LocationGameStateTests.cpp)
target_link_libraries(GameServerStateTests PRIVATE game_server_core)
source_group(
"Server\\Tests" FILES tests/LocationGameStateTests.cpp)
add_test(
NAME GameServer.LocationState
COMMAND GameServerStateTests)
add_test(
NAME GameServer.IdleSmoke
COMMAND GameServer --ticks 2 --no-sleep)
add_test(
NAME GameServer.BattleZoneZeroPlayers
COMMAND GameServer --campaign battle_zone --location uni_exterior
--ticks 2 --no-sleep)
set_tests_properties(
GameServer.BattleZoneZeroPlayers
PROPERTIES WORKING_DIRECTORY "${GAME_ROOT}")
endif()
+77
View File
@@ -0,0 +1,77 @@
# Headless GameServer
This target runs the authoritative `InternalGameServer` without SDL, OpenGL,
audio, Lua, or rendering dependencies. It can prepare campaign/location data
headlessly; networking is not implemented yet.
## Build
Windows:
```powershell
cmake -S server -B server/build
cmake --build server/build --config Release --target GameServer
ctest --test-dir server/build -C Release --output-on-failure
```
Linux:
```bash
cmake -S server -B server/build -DCMAKE_BUILD_TYPE=Release
cmake --build server/build --target GameServer --parallel
ctest --test-dir server/build --output-on-failure
```
Eigen3 must either be installed or be available at
`thirdparty/eigen-5.0.0`. The headless target reads ordinary filesystem resources
only; ZIP-backed campaign loading belongs to the later content-loading
milestone.
Run until interrupted:
```bash
./server/build/GameServer
```
For a multi-configuration Windows build, the executable is normally
`server/build/Release/GameServer.exe`.
For smoke tests, `--ticks N` limits execution and `--no-sleep` skips the
real-time delay.
Load a location with no players:
```bash
./server/build/GameServer --campaign battle_zone --location uni_exterior
```
If `--location` is omitted, the campaign's configured start location is used.
`--campaigns PATH` selects a different campaign registry. Content paths are
resolved from the process working directory, so run from the repository root
when using the bundled `resources/` tree.
Available startup options:
```text
--campaign ID
--location ID
--campaigns PATH
--tick-ms N
--ticks N
--no-sleep
```
The graphical client executes Lua/dialogue behavior after building its runtime
`Location`; the headless content pass loads authoritative navigation, NPC,
interactive-object, trigger, teleport, taxi, and animation metadata only.
The standalone lifecycle intentionally creates neither the local `"player"` nor
the battle-zone test bot. Those are opt-ins used by the graphical host.
## Tests
CTest covers:
- repeated `LocationGameState::setup()` preserving mutable runtime state;
- ticking a prepared location with zero players;
- idle server startup;
- headless loading of `battle_zone/uni_exterior`.
+123
View File
@@ -0,0 +1,123 @@
#include "game_server/InternalGameServer.h"
#include "Campaign.h"
#include <atomic>
#include <chrono>
#include <csignal>
#include <cstdlib>
#include <exception>
#include <iostream>
#include <limits>
#include <string>
#include <thread>
namespace {
std::atomic_bool stopRequested{false};
void requestStop(int)
{
stopRequested.store(true);
}
std::int64_t parsePositiveNumber(const char* value, const char* option)
{
try {
const std::string text(value);
std::size_t consumed = 0;
const long long parsed = std::stoll(text, &consumed);
if (consumed != text.size() || parsed <= 0)
throw std::invalid_argument("not positive");
return static_cast<std::int64_t>(parsed);
} catch (const std::exception&) {
throw std::runtime_error(
std::string("Invalid value for ") + option + ": " + value);
}
}
} // namespace
int main(int argc, char** argv)
{
std::int64_t tickMs = 50;
std::int64_t maxTicks = (std::numeric_limits<std::int64_t>::max)();
bool sleepBetweenTicks = true;
std::string campaignId;
std::string locationId;
std::string campaignsPath = "resources/config/campaigns.json";
try {
for (int index = 1; index < argc; ++index) {
const std::string option(argv[index]);
if (option == "--ticks" && index + 1 < argc) {
maxTicks = parsePositiveNumber(argv[++index], "--ticks");
} else if (option == "--tick-ms" && index + 1 < argc) {
tickMs = parsePositiveNumber(argv[++index], "--tick-ms");
} else if (option == "--no-sleep") {
sleepBetweenTicks = false;
} else if (option == "--campaign" && index + 1 < argc) {
campaignId = argv[++index];
} else if (option == "--location" && index + 1 < argc) {
locationId = argv[++index];
} else if (option == "--campaigns" && index + 1 < argc) {
campaignsPath = argv[++index];
} else if (option == "--help") {
std::cout
<< "Usage: GameServer [--ticks N] [--tick-ms N] "
"[--no-sleep] [--campaign ID] [--location ID] "
"[--campaigns PATH]\n";
return EXIT_SUCCESS;
} else {
throw std::runtime_error("Unknown or incomplete option: " + option);
}
}
} catch (const std::exception& error) {
std::cerr << error.what() << '\n';
return EXIT_FAILURE;
}
std::signal(SIGINT, requestStop);
std::signal(SIGTERM, requestStop);
FRG::InternalGameServer server;
if (!campaignId.empty()) {
try {
FRG::CampaignRegistry::instance().loadFromJson(campaignsPath);
const FRG::CampaignConfig* campaign =
FRG::CampaignRegistry::instance().find(campaignId);
if (!campaign)
throw std::runtime_error(
"Unknown campaign '" + campaignId + "'");
const std::string selectedLocation = locationId.empty()
? campaign->startLocation : locationId;
const FRG::LocationConfig* location =
campaign->findLocation(selectedLocation);
if (!location)
throw std::runtime_error(
"Campaign '" + campaignId +
"' has no location '" + selectedLocation + "'");
server.startCampaign(*campaign, std::string(), false, false);
server.prepareLocation(*location);
server.activateLocation(selectedLocation);
std::cout << "Loaded campaign '" << campaignId
<< "', location '" << selectedLocation
<< "' with zero players\n";
} catch (const std::exception& error) {
std::cerr << "Server content setup failed: "
<< error.what() << '\n';
return EXIT_FAILURE;
}
}
const auto tickDuration = std::chrono::milliseconds(tickMs);
auto nextTick = std::chrono::steady_clock::now();
std::cout << "GameServer started (idle headless mode, "
<< tickMs << " ms tick)\n";
for (std::int64_t tick = 0;
tick < maxTicks && !stopRequested.load(); ++tick) {
nextTick += tickDuration;
server.update(tickMs);
if (sleepBetweenTicks) std::this_thread::sleep_until(nextTick);
}
std::cout << "GameServer stopped\n";
return EXIT_SUCCESS;
}
+67
View File
@@ -0,0 +1,67 @@
#include "LocationGameState.h"
#include <cstdlib>
#include <iostream>
namespace {
int fail(const char* message)
{
std::cerr << message << '\n';
return EXIT_FAILURE;
}
} // namespace
int main()
{
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);
return EXIT_SUCCESS;
}