Some cleanup

This commit is contained in:
Vladislav Khorev committed 2026-11-17 20:29:20 +06:00
1 parent 4e40a1825a
commit c1be26023b
20 files changed
+451 -1330

No files matched your search

+39 -304
View File
@@ -11,9 +11,7 @@
#include <memory>
#include <cfloat>
#include <limits>
#include <random>
#include <stdexcept>
#include <unordered_set>
#include "GameConstants.h"
#include "Character.h"
#include "game_server/IGameServer.h"
@@ -24,35 +22,10 @@ namespace FRG
{
extern const char* CONST_ZIP_FILE;
extern float x;
constexpr float CONST_TAXI_CAR_SCALE = 0.6f;
static constexpr float CAMERA_FOV_Y = 1.0f / 1.5f;
// How close the player needs to be to a peaceful NPC before the
// on_npc_interact callback fires and the conversation begins.
static constexpr float NPC_TALK_DISTANCE = 1.35f;
static constexpr float RENDER_DISTRANCE = 50.f;
static float distancePointToSegmentXZ(const Eigen::Vector3f& p,
const Eigen::Vector3f& a,
const Eigen::Vector3f& b)
{
const Eigen::Vector2f p2(p.x(), p.z());
const Eigen::Vector2f a2(a.x(), a.z());
const Eigen::Vector2f b2(b.x(), b.z());
const Eigen::Vector2f ab = b2 - a2;
const float abLenSq = ab.squaredNorm();
if (abLenSq <= 1e-8f) {
return (p2 - a2).norm();
}
const float t = std::clamp((p2 - a2).dot(ab) / abLenSq, 0.0f, 1.0f);
const Eigen::Vector2f closest = a2 + ab * t;
return (p2 - closest).norm();
}
Location::Location(Renderer& iRenderer, IGameServer& server,
std::string locationId)
: renderer(iRenderer)
@@ -151,11 +124,6 @@ namespace FRG
locationId_, character.entityId(), std::move(targetId)});
}
void Location::replanCharacter(Character& character)
{
gameServer_.submit(ReplanCharacterCommand{locationId_, character.entityId()});
}
void Location::resetCharacterAfterDeath(Character& character)
{
gameServer_.submit(
@@ -589,188 +557,37 @@ namespace FRG
}
}
void Location::loadTeleportZones(const std::string& jsonPath, const char* zipFile)
void Location::prepareCharacterSparks(const Eigen::Matrix4f& cameraViewMatrix)
{
if (jsonPath.empty()) return;
std::string content;
try {
if (!zipFile || zipFile[0] == '\0') {
content = readTextFile(jsonPath);
} else {
auto buf = readFileFromZIP(jsonPath, zipFile);
if (buf.empty()) {
std::cerr << "[TELEPORT] Failed to read " << jsonPath << " from zip" << std::endl;
return;
}
content.assign(buf.begin(), buf.end());
}
} catch (const std::exception& e) {
std::cerr << "[TELEPORT] Failed to open " << jsonPath << ": " << e.what() << std::endl;
return;
}
using json = nlohmann::json;
json j;
try {
j = json::parse(content);
} catch (const std::exception& e) {
std::cerr << "[TELEPORT] JSON parse error in " << jsonPath << ": " << e.what() << std::endl;
return;
}
if (!j.contains("teleports") || !j["teleports"].is_array()) return;
auto activeTex = renderer.textureManager.LoadFromPng("resources/w/star.png", zipFile ? zipFile : "");
auto inactiveTex = renderer.textureManager.LoadFromPng("resources/w/star_red.png", zipFile ? zipFile : "");
std::unordered_set<std::string> ids;
for (const auto& item : j["teleports"]) {
TeleportZone tz;
tz.id = item.at("id").get<std::string>();
if (tz.id.empty() || !ids.insert(tz.id).second)
throw std::runtime_error(
"Missing or duplicate teleport id in " + jsonPath);
tz.active = item.value("active", false);
tz.position = Eigen::Vector3f(
item.value("positionX", 0.0f),
item.value("positionY", 0.0f),
item.value("positionZ", 0.0f)
);
tz.radius = item.value("radius", 0.0f);
tz.automatic = item.value("automatic", false);
tz.automaticRadius = item.value("automaticRadius", tz.radius);
tz.destinationLocation = item.value("destinationLocation", "");
tz.destinationPosition = Eigen::Vector3f(
item.value("destinationPositionX", 0.0f),
item.value("destinationPositionY", 0.0f),
item.value("destinationPositionZ", 0.0f)
);
tz.destinationRotationY = item.value("destinationRotationY", 0.0f);
tz.initSparks(activeTex, inactiveTex);
teleportZones.push_back(std::move(tz));
}
logger() << "[TELEPORT] Loaded " << teleportZones.size() << " teleport(s) from " << jsonPath << std::endl;
if (player) player->prepareHitSparksForDraw(cameraViewMatrix);
for (auto& [id, remote] : remotePlayers_)
if (remote) remote->prepareHitSparksForDraw(cameraViewMatrix);
for (auto& npc : npcs) npc->prepareHitSparksForDraw(cameraViewMatrix);
}
void Location::loadTriggerZones(const std::string& jsonPath, const char* zipFile)
void Location::drawCharacters()
{
if (jsonPath.empty()) return;
std::string content;
try {
if (!zipFile || zipFile[0] == '\0') {
content = readTextFile(jsonPath);
} else {
auto buf = readFileFromZIP(jsonPath, zipFile);
if (buf.empty()) {
std::cerr << "[TRIGGER] Failed to read " << jsonPath << " from zip" << std::endl;
return;
}
content.assign(buf.begin(), buf.end());
}
} catch (const std::exception& e) {
std::cerr << "[TRIGGER] Failed to open " << jsonPath << ": " << e.what() << std::endl;
return;
}
using json = nlohmann::json;
json j;
try {
j = json::parse(content);
} catch (const std::exception& e) {
std::cerr << "[TRIGGER] JSON parse error in " << jsonPath << ": " << e.what() << std::endl;
return;
}
if (!j.contains("trigger_zones") || !j["trigger_zones"].is_array()) return;
std::unordered_set<std::string> ids;
for (const auto& item : j["trigger_zones"]) {
TriggerZone tz;
tz.id = item.at("id").get<std::string>();
if (tz.id.empty() || !ids.insert(tz.id).second)
throw std::runtime_error(
"Missing or duplicate trigger-zone id in " + jsonPath);
tz.enabled = item.value("enabled", true);
tz.radius = item.value("radius", 1.5f);
tz.hysteresis = item.value("hysteresis", 0.3f);
tz.position = Eigen::Vector3f(
item.value("positionX", 0.0f),
item.value("positionY", 0.0f),
item.value("positionZ", 0.0f)
);
triggerZones.push_back(std::move(tz));
}
logger() << "[TRIGGER] Loaded " << triggerZones.size() << " trigger zone(s) from " << jsonPath << std::endl;
if (player) player->draw(renderer);
for (auto& [id, remote] : remotePlayers_)
if (remote) remote->draw(renderer);
for (auto& npc : npcs) npc->draw(renderer);
}
void Location::loadPointLights(const std::string& jsonPath, const char* zipFile)
void Location::drawCharacterOverlays(const Eigen::Matrix4f& cameraViewMatrix)
{
if (jsonPath.empty()) return;
std::string content;
try {
if (!zipFile || zipFile[0] == '\0') {
content = readTextFile(jsonPath);
} else {
auto buf = readFileFromZIP(jsonPath, zipFile);
if (buf.empty()) {
std::cerr << "[LIGHTS] Failed to read " << jsonPath << " from zip" << std::endl;
return;
}
content.assign(buf.begin(), buf.end());
}
} catch (const std::exception& e) {
std::cerr << "[LIGHTS] Failed to open " << jsonPath << ": " << e.what() << std::endl;
return;
if (!npcNameText) return;
const Eigen::Matrix4f proj = MakePerspectiveMatrix(1.0f / 1.5f,
static_cast<float>(Environment::width) / static_cast<float>(Environment::height),
Environment::CONST_Z_NEAR, Environment::CONST_Z_FAR);
drawRemotePlayerNames(cameraViewMatrix, proj);
for (auto& npc : npcs) {
if (npc) npc->drawName(*npcNameText, cameraViewMatrix, proj);
}
using json = nlohmann::json;
json j;
try {
j = json::parse(content);
} catch (const std::exception& e) {
std::cerr << "[LIGHTS] JSON parse error in " << jsonPath << ": " << e.what() << std::endl;
return;
if (player) player->drawHealthBar(renderer, cameraViewMatrix, proj);
for (auto& npc : npcs) {
if (npc) npc->drawHealthBar(renderer, cameraViewMatrix, proj);
}
if (!j.contains("lights") || !j["lights"].is_array()) return;
for (const auto& item : j["lights"]) {
PointLight pl;
pl.id = item.value("id", "");
pl.autoLight = item.value("autoLight", false);
pl.autoLightDistance = item.value("autoLightDistance", -1.0f);
pl.limitX = item.value("limitX", 99999.f);
pl.limitY = item.value("limitY", 99999.f);
pl.autoLightLimitX = item.value("autoLightLimitX", -1.0f);
pl.autoLightLimitY = item.value("autoLightLimitY", -1.0f);
pl.position = Eigen::Vector3f(
item.value("positionX", 0.0f),
item.value("positionY", 0.0f),
item.value("positionZ", 0.0f)
);
pl.direction = Eigen::Vector3f(
item.value("directionX", 0.0f),
item.value("directionY", -1.0f),
item.value("directionZ", 0.0f)
);
pl.color = Eigen::Vector3f(
item.value("colorR", 1.0f),
item.value("colorG", 1.0f),
item.value("colorB", 1.0f)
);
pointLights.push_back(std::move(pl));
}
logger() << "[LIGHTS] Loaded " << pointLights.size() << " light(s) from " << jsonPath << std::endl;
}
void Location::updateTriggerZones(const Eigen::Vector3f& playerPos)
{
for (int i = 0; i < static_cast<int>(triggerZones.size()); ++i) {
@@ -1019,11 +836,6 @@ namespace FRG
renderer.shaderManager.PopShader();
}
void Location::setPathVisualizationEnabled(bool enabled)
{
pathVisualization.setEnabled(enabled);
}
void Location::drawGame()
{
auto locationState = stateSnapshot();
@@ -1076,16 +888,10 @@ namespace FRG
}
renderer.RenderUniform1f("uAlpha", 1.0f);
const Eigen::Matrix4f currentView = renderer.GetCurrentModelViewMatrix();
if (player) player->prepareHitSparksForDraw(currentView);
for (auto& [id, remote] : remotePlayers_)
if (remote) remote->prepareHitSparksForDraw(currentView);
for (auto& npc : npcs) npc->prepareHitSparksForDraw(currentView);
prepareCharacterSparks(currentView);
for (auto& tz : teleportZones) tz.prepareForDraw(currentView);
if (player) player->draw(renderer);
for (auto& [id, remote] : remotePlayers_)
if (remote) remote->draw(renderer);
for (auto& npc : npcs) npc->draw(renderer);
drawCharacters();
drawNpcCar();
@@ -1106,19 +912,7 @@ namespace FRG
renderer.shaderManager.PopShader();
if (npcNameText) {
Eigen::Matrix4f proj = MakePerspectiveMatrix(1.0f / 1.5f,
static_cast<float>(Environment::width) / static_cast<float>(Environment::height),
Environment::CONST_Z_NEAR, Environment::CONST_Z_FAR);
drawRemotePlayerNames(currentView, proj);
for (auto& npc : npcs) {
if (npc) npc->drawName(*npcNameText, currentView, proj);
}
if (player) player->drawHealthBar(renderer, currentView, proj);
for (auto& npc : npcs) {
if (npc) npc->drawHealthBar(renderer, currentView, proj);
}
}
drawCharacterOverlays(currentView);
}
void Location::drawShadowDepthPass()
@@ -1202,6 +996,7 @@ namespace FRG
glClearColor(0.53, 0.81, 0.92, 1.0f);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
Eigen::Matrix4f cameraViewMatrix = Eigen::Matrix4f::Identity();
#ifdef DEBUG_LIGHT
// Debug mode: render from the light's point of view using the plain
// textured shader so we can see what the shadow map "sees".
@@ -1293,24 +1088,15 @@ namespace FRG
// but from the light's viewpoint — projection/view already on stack).
{
const Eigen::Matrix4f currentView = renderer.GetCurrentModelViewMatrix();
if (player) player->prepareHitSparksForDraw(currentView);
for (auto& [id, remote] : remotePlayers_)
if (remote) remote->prepareHitSparksForDraw(currentView);
for (auto& npc : npcs) npc->prepareHitSparksForDraw(currentView);
prepareCharacterSparks(currentView);
}
if (player) player->draw(renderer);
for (auto& [id, remote] : remotePlayers_)
if (remote) remote->draw(renderer);
for (auto& npc : npcs) npc->draw(renderer);
drawCharacters();
drawNpcCar();
#else
// Characters use their own shadow-aware shaders
CheckGlError(__FILE__, __LINE__);
if (player) player->prepareHitSparksForDraw(cameraViewMatrix);
for (auto& [id, remote] : remotePlayers_)
if (remote) remote->prepareHitSparksForDraw(cameraViewMatrix);
for (auto& npc : npcs) npc->prepareHitSparksForDraw(cameraViewMatrix);
prepareCharacterSparks(cameraViewMatrix);
for (auto& tz : teleportZones) tz.prepareForDraw(cameraViewMatrix);
if (player) player->drawWithShadow(renderer, lightFromCamera, shadowMap->getDepthTexture(), lightDirCamera);
@@ -1341,19 +1127,7 @@ namespace FRG
renderer.PopProjectionMatrix();
renderer.shaderManager.PopShader();
if (npcNameText) {
Eigen::Matrix4f proj = MakePerspectiveMatrix(1.0f / 1.5f,
static_cast<float>(Environment::width) / static_cast<float>(Environment::height),
Environment::CONST_Z_NEAR, Environment::CONST_Z_FAR);
drawRemotePlayerNames(cameraViewMatrix, proj);
for (auto& npc : npcs) {
if (npc) npc->drawName(*npcNameText, cameraViewMatrix, proj);
}
if (player) player->drawHealthBar(renderer, cameraViewMatrix, proj);
for (auto& npc : npcs) {
if (npc) npc->drawHealthBar(renderer, cameraViewMatrix, proj);
}
}
drawCharacterOverlays(cameraViewMatrix);
}
void Location::drawGameDarklands()
@@ -1403,16 +1177,10 @@ namespace FRG
}
renderer.RenderUniform1f("uAlpha", 1.0f);
const Eigen::Matrix4f currentView = renderer.GetCurrentModelViewMatrix();
if (player) player->prepareHitSparksForDraw(currentView);
for (auto& [id, remote] : remotePlayers_)
if (remote) remote->prepareHitSparksForDraw(currentView);
for (auto& npc : npcs) npc->prepareHitSparksForDraw(currentView);
prepareCharacterSparks(currentView);
for (auto& tz : teleportZones) tz.prepareForDraw(currentView);
if (player) player->draw(renderer);
for (auto& [id, remote] : remotePlayers_)
if (remote) remote->draw(renderer);
for (auto& npc : npcs) npc->draw(renderer);
drawCharacters();
//drawNpcCar();
@@ -1431,19 +1199,7 @@ namespace FRG
renderer.PopProjectionMatrix();
renderer.shaderManager.PopShader();
if (npcNameText) {
Eigen::Matrix4f proj = MakePerspectiveMatrix(1.0f / 1.5f,
static_cast<float>(Environment::width) / static_cast<float>(Environment::height),
Environment::CONST_Z_NEAR, Environment::CONST_Z_FAR);
drawRemotePlayerNames(currentView, proj);
for (auto& npc : npcs) {
if (npc) npc->drawName(*npcNameText, currentView, proj);
}
if (player) player->drawHealthBar(renderer, currentView, proj);
for (auto& npc : npcs) {
if (npc) npc->drawHealthBar(renderer, currentView, proj);
}
}
drawCharacterOverlays(currentView);
}
void Location::drawNightShadowDepthPass(const PointLight& light)
@@ -1551,7 +1307,7 @@ namespace FRG
interpolatedNpcCarPresentation(locationState->taxiCar);
renderer.PushMatrix();
renderer.TranslateMatrix(car.position);
renderer.TranslateMatrix({ 0, 0.7f - 0.25f +x, 0 });
renderer.TranslateMatrix({ 0, 0.7f - 0.25f, 0 });
renderer.RotateMatrix(Eigen::Quaternionf(Eigen::AngleAxisf(car.rotation, Eigen::Vector3f::UnitY())).toRotationMatrix());
glBindTexture(GL_TEXTURE_2D, carTexture->getTexID());
renderer.DrawVertexRenderStruct(carMesh);
@@ -1687,7 +1443,7 @@ namespace FRG
renderer.TranslateMatrix({ -camTarget.x(), -camTarget.y(), -camTarget.z() });
renderer.TranslateMatrix({ 0, -1.f, 0 });
cameraViewMatrix = renderer.GetCurrentModelViewMatrix();
const Eigen::Matrix4f cameraViewMatrix = renderer.GetCurrentModelViewMatrix();
// Transform active spot lights to eye space and build uniform arrays
float plPosEye[MAX_POINT_LIGHTS * 3] = {};
@@ -1772,10 +1528,7 @@ namespace FRG
}
renderer.RenderUniform1f("uAlpha", 1.0f);
if (player) player->prepareHitSparksForDraw(cameraViewMatrix);
for (auto& [id, remote] : remotePlayers_)
if (remote) remote->prepareHitSparksForDraw(cameraViewMatrix);
for (auto& npc : npcs) npc->prepareHitSparksForDraw(cameraViewMatrix);
prepareCharacterSparks(cameraViewMatrix);
for (auto& tz : teleportZones) tz.prepareForDraw(cameraViewMatrix);
if (hasShadows) {
@@ -1818,19 +1571,7 @@ namespace FRG
renderer.PopProjectionMatrix();
renderer.shaderManager.PopShader();
if (npcNameText) {
Eigen::Matrix4f proj = MakePerspectiveMatrix(1.0f / 1.5f,
static_cast<float>(Environment::width) / static_cast<float>(Environment::height),
Environment::CONST_Z_NEAR, Environment::CONST_Z_FAR);
drawRemotePlayerNames(cameraViewMatrix, proj);
for (auto& npc : npcs) {
if (npc) npc->drawName(*npcNameText, cameraViewMatrix, proj);
}
if (player) player->drawHealthBar(renderer, cameraViewMatrix, proj);
for (auto& npc : npcs) {
if (npc) npc->drawHealthBar(renderer, cameraViewMatrix, proj);
}
}
drawCharacterOverlays(cameraViewMatrix);
}
bool Location::setNavigationAreaAvailable(const std::string& areaName, bool available)
@@ -1896,7 +1637,6 @@ namespace FRG
};
for (const CharacterSimulationEvent& event : events.characters) {
Character* character = findRuntimeCharacter(event.characterId);
if (!event.events.arrivedCallbackName.empty()) {
try {
scriptEngine.callFunction(event.events.arrivedCallbackName);
@@ -2124,7 +1864,7 @@ namespace FRG
gameServer_.submit(SetLocationTargetsCommand{
locationId_, std::string(), std::string(), std::string()});
if (distance >= Character::kAttackRange) {
if (distance >= kAttackRange) {
setCharacterTarget(*player, clickedNpcState->position);
pathVisualization.start(
*player,
@@ -2146,7 +1886,7 @@ namespace FRG
// Peaceful NPC: walk to them, fire on_npc_interact when within talk distance.
clearCharacterAttackTarget(*player);
if (distance <= NPC_TALK_DISTANCE) {
if (distance <= kNpcTalkDistance) {
// Already in talk range — fire immediately, stop, and face the player.
//logger() << "[CLICK] *** SUCCESS: Clicked on NPC index: " << npcIndex << " (in range) ***" << std::endl;
setCharacterTarget(*player, playerState->position);
@@ -2159,7 +1899,7 @@ namespace FRG
}
else {
//logger() << "[CLICK] NPC " << npcIndex << " out of talk range (distance " << distance
// << " > " << NPC_TALK_DISTANCE << "); walking to NPC..." << std::endl;
// << " > " << kNpcTalkDistance << "); walking to NPC..." << std::endl;
setCharacterTarget(*player, clickedNpcState->position);
pathVisualization.start(
*player, clickedNpcState->position.toEigen(),
@@ -2267,11 +2007,6 @@ namespace FRG
return dialogueSystem.startCutscene(cutsceneId);
}
void Location::setOnCutsceneFinished(std::function<void(const std::string&)> cb)
{
dialogueSystem.setOnCutsceneFinished(std::move(cb));
}
void Location::setDialogueFlag(const std::string& flag, int value)
{
dialogueSystem.setFlag(flag, value);