Skip to content

Commit 47d7766

Browse files
fix QuestManager
1 parent c55f7f7 commit 47d7766

4 files changed

Lines changed: 35 additions & 13 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ set(LOGIC_CORE_SOURCES
111111
src/game/EntityManager.cpp
112112
src/game/Player.cpp
113113
src/game/PlayerManager.cpp
114+
src/game/QuestManager.cpp
114115
src/game/NPCEntity.cpp
115116
)
116117

include/game/LogicCore.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
//#include "game/RAIIThread.hpp"
2525
#include "game/LogicWorld.hpp"
2626
#include "game/LogicEntity.hpp"
27-
//#include "game/PlayerManager.hpp"
27+
#include "game/PlayerManager.hpp"
2828

2929
class PythonScripting;
3030
class ScriptHotReloader;

include/game/QuestManager.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "logging/Logger.hpp"
2020
#include "config/ConfigManager.hpp"
21+
#include "game/LogicCore.hpp"
2122

2223
// =============== Enums ===============
2324

@@ -222,7 +223,7 @@ class QuestManager {
222223

223224
// --- Entity Quest Management ---
224225
bool CanStartQuest(uint64_t entity_id, uint64_t quest_id) const;
225-
void StartQuest(uint64_t entity_id, uint64_t quest_id);
226+
bool StartQuest(uint64_t entity_id, uint64_t quest_id);
226227
bool AbandonQuest(uint64_t entity_id, uint64_t quest_id);
227228
bool CanCompleteQuest(uint64_t entity_id, uint64_t quest_id) const;
228229
bool CompleteQuest(uint64_t entity_id, uint64_t quest_id);

src/game/QuestManager.cpp

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -365,17 +365,17 @@ bool QuestManager::CanStartQuest(uint64_t entity_id, uint64_t quest_id) const {
365365
return true;
366366
}
367367

368-
void QuestManager::StartQuest(uint64_t entity_id, uint64_t quest_id) {
368+
bool QuestManager::StartQuest(uint64_t entity_id, uint64_t quest_id) {
369369
if (!CanStartQuest(entity_id, quest_id)) {
370370
Logger::Warn("QuestManager: Entity {} cannot start quest {}", entity_id, quest_id);
371-
return;
371+
return false;
372372
}
373373

374374
std::unique_lock<std::shared_mutex> lock(mutex_);
375375
auto& data = entity_quests_[entity_id];
376376
if (data.active_quests.size() >= MAX_ACTIVE_QUESTS) {
377377
Logger::Warn("QuestManager: Entity {} has too many active quests", entity_id);
378-
return;
378+
return false;
379379
}
380380

381381
const auto& def = quest_definitions_[quest_id];
@@ -399,6 +399,7 @@ void QuestManager::StartQuest(uint64_t entity_id, uint64_t quest_id) {
399399
lock.unlock();
400400
FireQuestEvent("quest_started", entity_id, quest_id);
401401
Logger::Debug("QuestManager: Entity {} started quest {}", entity_id, quest_id);
402+
return true;
402403
}
403404

404405
bool QuestManager::AbandonQuest(uint64_t entity_id, uint64_t quest_id) {
@@ -655,17 +656,25 @@ void QuestManager::OnItemCollected(uint64_t entity_id, const std::string& item_i
655656
}
656657

657658
void QuestManager::OnNPCTalkedTo(uint64_t entity_id, uint64_t npc_id) {
659+
auto to_uint64 = [](const std::string& s) -> uint64_t {
660+
try { return std::stoull(s); } catch (...) { return 0; }
661+
};
662+
658663
std::vector<std::pair<uint64_t, std::string>> updates;
659664
{
660665
std::shared_lock<std::shared_mutex> lock(mutex_);
661666
auto entity_it = entity_quests_.find(entity_id);
662667
if (entity_it == entity_quests_.end()) return;
668+
663669
for (const auto& [qid, progress] : entity_it->second.active_quests) {
664670
if (progress.state != QuestState::IN_PROGRESS) continue;
665671
const auto& def = quest_definitions_.at(qid);
666672
for (const auto& obj : def.objectives) {
667-
if (obj.type == ObjectiveType::TALK_TO_NPC && obj.target == npc_id) {
668-
updates.emplace_back(qid, obj.id);
673+
if (obj.type == ObjectiveType::TALK_TO_NPC) {
674+
// Convert the objective's target (string) to uint64_t and compare
675+
if (to_uint64(obj.target) == npc_id) {
676+
updates.emplace_back(qid, obj.id);
677+
}
669678
}
670679
}
671680
}
@@ -674,14 +683,16 @@ void QuestManager::OnNPCTalkedTo(uint64_t entity_id, uint64_t npc_id) {
674683
UpdateObjective(entity_id, qid, obj_id, 1);
675684
}
676685

677-
// Also handle quest discovery from NPC
686+
// Quest discovery from NPC
678687
std::vector<uint64_t> possible_quests;
679688
{
680689
std::shared_lock<std::shared_mutex> lock(mutex_);
681690
for (const auto& [qid, def] : quest_definitions_) {
682-
if (def.giver_npc_id == npc_id && def.is_discoverable &&
683-
!HasQuest(entity_id, qid) && CanStartQuest(entity_id, qid)) {
684-
possible_quests.push_back(qid);
691+
if (def.is_discoverable && !HasQuest(entity_id, qid) && CanStartQuest(entity_id, qid)) {
692+
// Convert giver_npc_id (string) to uint64_t and compare
693+
if (to_uint64(def.giver_npc_id) == npc_id) {
694+
possible_quests.push_back(qid);
695+
}
685696
}
686697
}
687698
}
@@ -1034,25 +1045,34 @@ uint64_t QuestManager::GetTrackedQuest(uint64_t entity_id) const {
10341045
// =============== NPC Interaction ===============
10351046

10361047
std::vector<uint64_t> QuestManager::GetQuestsFromNPC(uint64_t entity_id, uint64_t npc_id) const {
1048+
auto to_uint64 = [](const std::string& s) -> uint64_t {
1049+
try { return std::stoull(s); } catch (...) { return 0; }
1050+
};
1051+
10371052
std::vector<uint64_t> result;
10381053
std::shared_lock<std::shared_mutex> lock(mutex_);
10391054
for (const auto& [qid, def] : quest_definitions_) {
1040-
if (def.giver_npc_id == npc_id && !HasQuest(entity_id, qid) && CanStartQuest(entity_id, qid)) {
1055+
if (to_uint64(def.giver_npc_id) == npc_id && !HasQuest(entity_id, qid) && CanStartQuest(entity_id, qid)) {
10411056
result.push_back(qid);
10421057
}
10431058
}
10441059
return result;
10451060
}
10461061

10471062
std::vector<uint64_t> QuestManager::GetQuestsToTurnIn(uint64_t entity_id, uint64_t npc_id) const {
1063+
auto to_uint64 = [](const std::string& s) -> uint64_t {
1064+
try { return std::stoull(s); } catch (...) { return 0; }
1065+
};
1066+
10481067
std::vector<uint64_t> result;
10491068
std::shared_lock<std::shared_mutex> lock(mutex_);
10501069
auto it = entity_quests_.find(entity_id);
10511070
if (it == entity_quests_.end()) return result;
1071+
10521072
for (const auto& [qid, progress] : it->second.active_quests) {
10531073
if (progress.state == QuestState::COMPLETED) {
10541074
auto def_it = quest_definitions_.find(qid);
1055-
if (def_it != quest_definitions_.end() && def_it->second.turn_in_npc_id == npc_id) {
1075+
if (def_it != quest_definitions_.end() && to_uint64(def_it->second.turn_in_npc_id) == npc_id) {
10561076
result.push_back(qid);
10571077
}
10581078
}

0 commit comments

Comments
 (0)