Skip to content

Commit 274b0a1

Browse files
fix NPCEntity
1 parent c5fe8e1 commit 274b0a1

6 files changed

Lines changed: 61 additions & 56 deletions

File tree

include/database/Backend.hpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
/**
2-
* @brief Abstract Database Backend Interface
3-
*
4-
* Provides a unified interface for database operations with support for
5-
* both PostgreSQL and Citus (distributed PostgreSQL) backends.
6-
*/
1+
#include <nlohmann/json.hpp>
2+
3+
#include "logging/Logger.hpp"
4+
75
class DatabaseBackend {
86
public:
97
virtual ~DatabaseBackend() = default;

include/game/InventorySystem.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,4 @@ class InventorySystem {
107107
// Database operations
108108
bool LoadFromDatabase(uint64_t playerId);
109109
bool SaveToDatabase(uint64_t playerId);
110-
};
110+
};

include/game/NPCEntity.hpp

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -339,46 +339,6 @@ class NPCEntity : public GameEntity {
339339
void SetBehaviorState(NPCAIState st) { ai_state_ = st; };
340340
void MoveTo(const glm::vec3& destination, float speed_multiplier = 1.0f);
341341

342-
private:
343-
NPCType npc_type_;
344-
NPCRarity rarity_;
345-
NPCFaction faction_;
346-
NPCAIState ai_state_;
347-
348-
NPCStats npc_stats_;
349-
NPCAIProfile ai_profile_;
350-
NPCLootTable loot_table_;
351-
NPCDialogue dialogue_;
352-
353-
// Targeting
354-
uint64_t target_id_ = 0;
355-
std::vector<uint64_t> hate_list_; // Ordered by hate/damage dealt
356-
std::unordered_map<uint64_t, float> damage_taken_; // Damage taken from each attacker
357-
358-
// AI state tracking
359-
float state_timer_ = 0.0f;
360-
float idle_timer_ = 0.0f;
361-
float patrol_index_ = 0.0f;
362-
bool patrol_direction_ = true; // true = forward, false = backward
363-
364-
// Combat tracking
365-
float attack_cooldown_ = 0.0f;
366-
float stun_timer_ = 0.0f;
367-
float flee_timer_ = 0.0f;
368-
float summon_cooldown_ = 0.0f;
369-
370-
// Patrol and movement
371-
glm::vec3 spawn_position_;
372-
std::queue<glm::vec3> patrol_queue_;
373-
374-
// Quests and trade
375-
std::vector<std::string> quests_;
376-
std::unordered_map<std::string, int> trade_items_;
377-
378-
// Special abilities
379-
std::vector<std::string> abilities_;
380-
std::unordered_map<std::string, float> ability_cooldowns_;
381-
382342
// AI decision making
383343
void UpdateIdle(float delta_time);
384344
void UpdatePatrol(float delta_time);
@@ -432,6 +392,46 @@ class NPCEntity : public GameEntity {
432392
void SaveTradeItemsToJson(nlohmann::json& json) const;
433393
void LoadTradeItemsFromJson(const nlohmann::json& json);
434394

395+
private:
396+
NPCType npc_type_;
397+
NPCRarity rarity_;
398+
NPCFaction faction_;
399+
NPCAIState ai_state_;
400+
401+
NPCStats npc_stats_;
402+
NPCAIProfile ai_profile_;
403+
NPCLootTable loot_table_;
404+
NPCDialogue dialogue_;
405+
406+
// Targeting
407+
uint64_t target_id_ = 0;
408+
std::vector<uint64_t> hate_list_; // Ordered by hate/damage dealt
409+
std::unordered_map<uint64_t, float> damage_taken_; // Damage taken from each attacker
410+
411+
// AI state tracking
412+
float state_timer_ = 0.0f;
413+
float idle_timer_ = 0.0f;
414+
float patrol_index_ = 0.0f;
415+
bool patrol_direction_ = true; // true = forward, false = backward
416+
417+
// Combat tracking
418+
float attack_cooldown_ = 0.0f;
419+
float stun_timer_ = 0.0f;
420+
float flee_timer_ = 0.0f;
421+
float summon_cooldown_ = 0.0f;
422+
423+
// Patrol and movement
424+
glm::vec3 spawn_position_;
425+
std::queue<glm::vec3> patrol_queue_;
426+
427+
// Quests and trade
428+
std::vector<std::string> quests_;
429+
std::unordered_map<std::string, int> trade_items_;
430+
431+
// Special abilities
432+
std::vector<std::string> abilities_;
433+
std::unordered_map<std::string, float> ability_cooldowns_;
434+
435435
// Constants
436436
static constexpr float ATTACK_COOLDOWN_BASE = 2.0f;
437437
static constexpr float STUN_DURATION = 1.0f;

src/game/CollisionSystem.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,18 @@
1010
static constexpr float EPSILON = 1e-6f;
1111
static constexpr float INV_EPSILON = 1e6f;
1212

13+
float distance_between_vec3(glm::vec3 pos0, glm::vec3 pos1)
14+
{
15+
glm::vec3 delta = pos0 - pos1;
16+
return glm::dot(delta, delta);
17+
}
18+
1319
// BoundingSphere implementation
1420
bool BoundingSphere::Intersects(const BoundingSphere& other) const {
1521
if (!IsValid() || !other.IsValid()) return false;
1622

17-
float distanceSquared = glm::distance2(center, other.center);
23+
//float distanceSquared = glm::distance2(center, other.center);
24+
float distanceSquared = distance_between_vec3(center, other.center);
1825
float radiusSum = radius + other.radius;
1926
return distanceSquared <= (radiusSum * radiusSum);
2027
}
@@ -717,7 +724,7 @@ bool CollisionSystem::TestSphereTriangle(const glm::vec3& sphereCenter, float ra
717724
float t = glm::dot(ap, ab) / glm::dot(ab, ab);
718725
t = std::clamp(t, 0.0f, 1.0f);
719726
glm::vec3 closest = a + ab * t;
720-
float distSq = glm::distance2(sphereCenter, closest);
727+
float distSq = distance_between_vec3(sphereCenter, closest);
721728
if (distSq < minDistanceSquared) {
722729
minDistanceSquared = distSq;
723730
closestPoint = closest;
@@ -730,7 +737,7 @@ bool CollisionSystem::TestSphereTriangle(const glm::vec3& sphereCenter, float ra
730737

731738
// Check vertices
732739
auto checkVertex = [&](const glm::vec3& vertex) {
733-
float distSq = glm::distance2(sphereCenter, vertex);
740+
float distSq = distance_between_vec3(sphereCenter, vertex);
734741
if (distSq < minDistanceSquared) {
735742
minDistanceSquared = distSq;
736743
closestPoint = vertex;
@@ -829,7 +836,7 @@ bool CollisionSystem::TestRayTriangle(const glm::vec3& origin, const glm::vec3&
829836

830837
bool CollisionSystem::ValidatePosition(const glm::vec3& position) const {
831838
// Check for NaN and infinite values
832-
if (!glm::all(glm::isfinite(position))) {
839+
if (!std::isfinite(position.x) || !std::isfinite(position.y) || !std::isfinite(position.z)) {
833840
return false;
834841
}
835842

src/game/MobSystem.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,11 @@ void MobSystem::OnMobDeath(uint64_t mobId, uint64_t killerId) {
144144
MobDeathInfo deathInfo;
145145
deathInfo.mobId = mobId;
146146
deathInfo.killerId = killerId;
147-
deathInfo.mobType = mob->GetType();
147+
deathInfo.mobType = mob->GetNPCType();
148148
deathInfo.deathPosition = mob->GetPosition();
149149
deathInfo.deathTime = std::chrono::steady_clock::now();
150150
deathInfo.level = 1; // TODO: Get level from mob
151-
deathInfo.lootTableId = GetLootTableIdForMob(mob->GetType(), zoneName);
151+
deathInfo.lootTableId = GetLootTableIdForMob(mob->GetNPCType(), zoneName);
152152

153153
// Award experience
154154
float experience = GetExperienceReward(deathInfo.mobType, deathInfo.level);
@@ -183,7 +183,7 @@ void MobSystem::OnMobDeath(uint64_t mobId, uint64_t killerId) {
183183
gameLogic.FirePythonEvent("mob_death", {
184184
{"mobId", mobId},
185185
{"killerId", killerId},
186-
{"mobType", static_cast<int>(mob->GetType())},
186+
{"mobType", static_cast<int>(mob->GetNPCType())},
187187
{"level", deathInfo.level},
188188
{"experience", experience},
189189
{"deathPosition", {

src/game/NPCSystem.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ void NPCManager::ProcessNPCAI(NPCEntity* npc, float deltaTime) {
8585
}
8686

8787
void NPCManager::HandleCombat(NPCEntity* npc, float deltaTime) {
88-
if (npc->GetBehaviorState() == NPCAIState::COMBAT) {
88+
if (npc->GetAIState() == NPCAIState::COMBAT) {
8989
// Check if target is in attack range
9090
// If yes, attack
9191
// If no, chase
92-
npc->Attack();
92+
npc->PerformAttack();
9393
}
9494
}
9595

0 commit comments

Comments
 (0)