Fixing bugs with pathfinding, bump version
This commit is contained in:
1 parent
8e2b019c92
commit
f1cc28ab31
22 files changed
+1119
-759
No files matched your search
@@ -1,13 +1,87 @@
|
||||
#include "Campaign.h"
|
||||
#include "utils/Utils.h"
|
||||
#include "external/nlohmann/json.hpp"
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <cstdint>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
|
||||
|
||||
namespace FRG {
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
namespace {
|
||||
|
||||
void hashBytes(std::uint64_t& hash, const void* data, std::size_t size)
|
||||
{
|
||||
const auto* bytes = static_cast<const unsigned char*>(data);
|
||||
for (std::size_t i = 0; i < size; ++i) {
|
||||
hash ^= bytes[i];
|
||||
hash *= 1099511628211ull;
|
||||
}
|
||||
}
|
||||
|
||||
void hashString(std::uint64_t& hash, const std::string& value)
|
||||
{
|
||||
const std::uint64_t size = static_cast<std::uint64_t>(value.size());
|
||||
hashBytes(hash, &size, sizeof(size));
|
||||
hashBytes(hash, value.data(), value.size());
|
||||
}
|
||||
|
||||
bool hasAnimationExtension(const std::string& value)
|
||||
{
|
||||
if (value.size() < 5) return false;
|
||||
std::string extension = value.substr(value.size() - 5);
|
||||
std::transform(
|
||||
extension.begin(), extension.end(), extension.begin(),
|
||||
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
|
||||
return extension == ".anim";
|
||||
}
|
||||
|
||||
void collectAnimationPaths(
|
||||
const json& value, std::set<std::string>& animationPaths)
|
||||
{
|
||||
if (value.is_string()) {
|
||||
const std::string path = value.get<std::string>();
|
||||
if (hasAnimationExtension(path)) animationPaths.insert(path);
|
||||
return;
|
||||
}
|
||||
if (value.is_array()) {
|
||||
for (const auto& child : value)
|
||||
collectAnimationPaths(child, animationPaths);
|
||||
return;
|
||||
}
|
||||
if (value.is_object()) {
|
||||
for (const auto& [key, child] : value.items()) {
|
||||
(void)key;
|
||||
collectAnimationPaths(child, animationPaths);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<char> readCompatibilityResource(
|
||||
const std::string& path, const std::string& zipFile)
|
||||
{
|
||||
const std::vector<char> bytes =
|
||||
zipFile.empty() ? readFile(path) : readFileFromZIP(path, zipFile);
|
||||
if (bytes.empty())
|
||||
throw std::runtime_error(
|
||||
"Cannot fingerprint missing or empty resource '" + path + "'");
|
||||
return bytes;
|
||||
}
|
||||
|
||||
void addPath(std::set<std::string>& paths, const std::string& path)
|
||||
{
|
||||
if (!path.empty()) paths.insert(path);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
static bool parseJsonFile(const std::string& path, const std::string& zipFile, json& out) {
|
||||
const std::string content = readLocalizedConfigFile(path, zipFile);
|
||||
if (content.empty()) {
|
||||
@@ -91,6 +165,111 @@ namespace FRG {
|
||||
return (loc && !loc->loadingScreenPath.empty()) ? loc->loadingScreenPath : loadingScreenPath;
|
||||
}
|
||||
|
||||
std::string makeNetworkCompatibilityFingerprint(
|
||||
const CampaignConfig& campaign, const std::string& zipFile)
|
||||
{
|
||||
const std::string versionLabel = campaign.networkCompatibility.empty()
|
||||
? "lockstep-v2:" + campaign.id
|
||||
: campaign.networkCompatibility;
|
||||
|
||||
json manifest{
|
||||
{"simulationVersion", kDeterministicSimulationVersion},
|
||||
{"versionLabel", versionLabel},
|
||||
{"campaignId", campaign.id},
|
||||
{"startLocation", campaign.startLocation},
|
||||
{"networkPlayerSlots", campaign.networkPlayerSlots},
|
||||
{"startStatePath", campaign.startStatePath},
|
||||
{"questsPath", campaign.questsPath},
|
||||
{"locations", json::array()}
|
||||
};
|
||||
|
||||
std::set<std::string> resourcePaths;
|
||||
addPath(resourcePaths, campaign.startStatePath);
|
||||
addPath(resourcePaths, campaign.questsPath);
|
||||
for (std::size_t i = 0; i < campaign.locations.size(); ++i) {
|
||||
const LocationConfig& location = campaign.locations[i];
|
||||
const std::string configPath =
|
||||
i < campaign.locationConfigPaths.size()
|
||||
? campaign.locationConfigPaths[i] : std::string();
|
||||
addPath(resourcePaths, configPath);
|
||||
addPath(resourcePaths, location.gameObjectsJsonPath);
|
||||
addPath(resourcePaths, location.interactiveObjectsJsonPath);
|
||||
addPath(resourcePaths, location.npcsJsonPath);
|
||||
addPath(resourcePaths, location.teleportsJsonPath);
|
||||
addPath(resourcePaths, location.triggerZonesJsonPath);
|
||||
for (const std::string& path : location.navigationJsonPaths)
|
||||
addPath(resourcePaths, path);
|
||||
|
||||
json locationManifest{
|
||||
{"id", location.id},
|
||||
{"configPath", configPath},
|
||||
{"gameObjects", location.gameObjectsJsonPath},
|
||||
{"interactiveObjects", location.interactiveObjectsJsonPath},
|
||||
{"npcs", location.npcsJsonPath},
|
||||
{"navigation", location.navigationJsonPaths},
|
||||
{"teleports", location.teleportsJsonPath},
|
||||
{"triggerZones", location.triggerZonesJsonPath},
|
||||
{"playerPosition", {
|
||||
location.playerPosition.x.raw_value(),
|
||||
location.playerPosition.y.raw_value(),
|
||||
location.playerPosition.z.raw_value()}},
|
||||
{"lockInteractiveObjectsDuringTutorial",
|
||||
location.lockInteractiveObjectsDuringTutorial}
|
||||
};
|
||||
if (location.taxi) {
|
||||
const LocationConfig::Taxi& taxi = *location.taxi;
|
||||
locationManifest["taxi"] = {
|
||||
{"triggerPosition", {
|
||||
taxi.triggerPosition.x(), taxi.triggerPosition.y(),
|
||||
taxi.triggerPosition.z()}},
|
||||
{"waypoints", json::array()},
|
||||
{"initialPosition", {
|
||||
taxi.initialPosition.x(), taxi.initialPosition.y(),
|
||||
taxi.initialPosition.z()}},
|
||||
{"initialRotation", taxi.initialRotation},
|
||||
{"waypointReachRadius", taxi.waypointReachRadius}
|
||||
};
|
||||
for (const Eigen::Vector3f& waypoint : taxi.waypoints) {
|
||||
locationManifest["taxi"]["waypoints"].push_back({
|
||||
waypoint.x(), waypoint.y(), waypoint.z()});
|
||||
}
|
||||
}
|
||||
manifest["locations"].push_back(std::move(locationManifest));
|
||||
}
|
||||
|
||||
std::uint64_t hash = 1469598103934665603ull;
|
||||
hashString(hash, manifest.dump());
|
||||
std::set<std::string> animationPaths;
|
||||
for (const std::string& path : resourcePaths) {
|
||||
const std::vector<char> bytes =
|
||||
readCompatibilityResource(path, zipFile);
|
||||
hashString(hash, path);
|
||||
hashBytes(hash, bytes.data(), bytes.size());
|
||||
if (path.size() >= 5 &&
|
||||
path.substr(path.size() - 5) == ".json") {
|
||||
try {
|
||||
collectAnimationPaths(
|
||||
json::parse(bytes.begin(), bytes.end()), animationPaths);
|
||||
} catch (const std::exception& error) {
|
||||
throw std::runtime_error(
|
||||
"Cannot fingerprint invalid JSON resource '" + path +
|
||||
"': " + error.what());
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const std::string& path : animationPaths) {
|
||||
const std::vector<char> bytes =
|
||||
readCompatibilityResource(path, zipFile);
|
||||
hashString(hash, path);
|
||||
hashBytes(hash, bytes.data(), bytes.size());
|
||||
}
|
||||
|
||||
std::ostringstream output;
|
||||
output << versionLabel << ":sim" << kDeterministicSimulationVersion
|
||||
<< ':' << std::hex << std::setfill('0') << std::setw(16) << hash;
|
||||
return output.str();
|
||||
}
|
||||
|
||||
CampaignRegistry& CampaignRegistry::instance() {
|
||||
static CampaignRegistry reg;
|
||||
return reg;
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
|
||||
namespace FRG {
|
||||
|
||||
inline constexpr int kDeterministicSimulationVersion = 1;
|
||||
|
||||
// One location of a campaign, loaded from resources/config/locations/<name>.json.
|
||||
// Optional paths may be left empty; Location::setup skips them.
|
||||
struct LocationConfig {
|
||||
@@ -72,6 +74,13 @@ namespace FRG {
|
||||
std::string loadingScreenFor(const std::string& locationId) const;
|
||||
};
|
||||
|
||||
// Combines the deterministic simulation version with the exact authoritative
|
||||
// resource bytes used by a campaign. Matching manual version labels alone are
|
||||
// not sufficient to establish lockstep compatibility.
|
||||
std::string makeNetworkCompatibilityFingerprint(
|
||||
const CampaignConfig& campaign,
|
||||
const std::string& zipFile = std::string());
|
||||
|
||||
// Campaigns listed in resources/config/campaigns.json, in display order.
|
||||
class CampaignRegistry {
|
||||
public:
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "LocationState.h"
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace FRG {
|
||||
|
||||
@@ -499,6 +500,8 @@ void CharacterState::save(nlohmann::json& out) const
|
||||
out["hp"] = hp;
|
||||
out["enabled"] = enabled;
|
||||
out["battle_state"] = battle_state;
|
||||
out["attack"] = attack;
|
||||
out["resetAnim"] = resetAnim;
|
||||
out["currentState"] = static_cast<int>(currentState);
|
||||
out["attack_cooldown"] = attack_cooldown;
|
||||
out["showWeapon"] = showWeapon;
|
||||
@@ -517,6 +520,10 @@ void CharacterState::save(nlohmann::json& out) const
|
||||
out["movementBlockedTicks"] = movementBlockedTicks;
|
||||
out["navigationRetryTicks"] = navigationRetryTicks;
|
||||
out["movedLastTick"] = movedLastTick;
|
||||
out["velocity"] = {
|
||||
velocity.x.raw_value(),
|
||||
velocity.y.raw_value()
|
||||
};
|
||||
out["navigationRetryCooldownTicks"] = navigationRetryCooldownTicks;
|
||||
out["navigationRetryCooldownIndex"] = navigationRetryCooldownIndex;
|
||||
|
||||
@@ -549,6 +556,8 @@ void CharacterState::load(const nlohmann::json& in)
|
||||
visibleRange = static_cast<fp>(in.value("visibleRange", static_cast<double>(visibleRange)));
|
||||
enabled = in.value("enabled", enabled);
|
||||
battle_state = in.value("battle_state", battle_state);
|
||||
attack = in.at("attack").get<int>();
|
||||
resetAnim = in.at("resetAnim").get<bool>();
|
||||
currentState = static_cast<AnimationState>(in.value("currentState", static_cast<int>(currentState)));
|
||||
attack_cooldown = in.value("attack_cooldown", attack_cooldown);
|
||||
showWeapon = in.value("showWeapon", showWeapon);
|
||||
@@ -568,6 +577,16 @@ void CharacterState::load(const nlohmann::json& in)
|
||||
movementBlockedTicks = in.value("movementBlockedTicks", movementBlockedTicks);
|
||||
navigationRetryTicks = in.value("navigationRetryTicks", navigationRetryTicks);
|
||||
movedLastTick = in.value("movedLastTick", movedLastTick);
|
||||
const auto& velocityValue = in.at("velocity");
|
||||
if (!velocityValue.is_array() || velocityValue.size() != 2 ||
|
||||
!velocityValue[0].is_number_integer() ||
|
||||
!velocityValue[1].is_number_integer()) {
|
||||
throw std::runtime_error(
|
||||
"Character velocity must contain two fixed-point raw integers");
|
||||
}
|
||||
velocity = Vec2fp(
|
||||
fp::from_raw_value(velocityValue[0].get<std::int32_t>()),
|
||||
fp::from_raw_value(velocityValue[1].get<std::int32_t>()));
|
||||
navigationRetryCooldownTicks = in.value(
|
||||
"navigationRetryCooldownTicks", navigationRetryCooldownTicks);
|
||||
navigationRetryCooldownIndex = in.value(
|
||||
|
||||
@@ -72,8 +72,8 @@ public:
|
||||
|
||||
// --- Position / orientation ---
|
||||
Vec3fp position;
|
||||
fp facingAngle;
|
||||
fp targetFacingAngle;
|
||||
fp facingAngle{0};
|
||||
fp targetFacingAngle{0};
|
||||
|
||||
// --- Health ---
|
||||
float hp = 200.f;
|
||||
|
||||
+1
-1
@@ -33,7 +33,7 @@ struct StoredChatMessage {
|
||||
};
|
||||
|
||||
struct GameState : public ISaveable {
|
||||
static constexpr int kSchemaVersion = 5;
|
||||
static constexpr int kSchemaVersion = 6;
|
||||
|
||||
// --- World ---
|
||||
// Campaign being played (see CampaignRegistry).
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "WebsocketGameClient.h"
|
||||
#include "../Campaign.h"
|
||||
#include "../Location.h"
|
||||
#include "../utils/Utils.h"
|
||||
#include <stdexcept>
|
||||
|
||||
namespace FRG {
|
||||
@@ -20,10 +21,11 @@ void RemoteGameServerHost::initializeCampaign(
|
||||
CampaignRegistry::instance().find(campaignId);
|
||||
if (!campaign)
|
||||
throw std::runtime_error("Unknown remote campaign '" + campaignId + "'");
|
||||
client_.expectCompatibility(
|
||||
campaign->networkCompatibility.empty()
|
||||
? "lockstep-v2:" + campaignId
|
||||
: campaign->networkCompatibility);
|
||||
const std::string fingerprint =
|
||||
makeNetworkCompatibilityFingerprint(*campaign, zipFile);
|
||||
logger() << "[network] Client compatibility fingerprint: "
|
||||
<< fingerprint << std::endl;
|
||||
client_.expectCompatibility(fingerprint);
|
||||
client_.replica_.startCampaign(*campaign, zipFile, true, false);
|
||||
}
|
||||
|
||||
|
||||
@@ -617,12 +617,12 @@ void WebsocketGameClient::update(std::int64_t)
|
||||
if (!tick.stateHash.empty()) {
|
||||
nlohmann::json state;
|
||||
replica_.save(state);
|
||||
/*const std::string localHash = canonicalStateHash(std::move(state));
|
||||
const std::string localHash = canonicalStateHash(std::move(state));
|
||||
if (localHash != tick.stateHash) {
|
||||
std::cerr << "[network] Lockstep divergence at tick "
|
||||
<< tick.tickId << ": server=" << tick.stateHash
|
||||
<< ", client=" << localHash << '\n';
|
||||
}*/
|
||||
}
|
||||
}
|
||||
++nextExpectedTick_;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user