-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(skymp5-server): add onReadBook event (#2212)
- Loading branch information
Showing
3 changed files
with
103 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
skymp5-server/cpp/server_guest_lib/gamemode_events/ReadBookEvent.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include "ReadBookEvent.h" | ||
|
||
#include "MpActor.h" | ||
#include "WorldState.h" | ||
#include <string> | ||
#include <unordered_set> | ||
#include <vector> | ||
|
||
ReadBookEvent::ReadBookEvent(MpActor* actor_, uint32_t baseId_) | ||
: actor(actor_) | ||
, baseId(baseId_) | ||
{ | ||
} | ||
|
||
const char* ReadBookEvent::GetName() const | ||
{ | ||
return "onReadBook"; | ||
} | ||
|
||
std::string ReadBookEvent::GetArgumentsJsonArray() const | ||
{ | ||
std::string result; | ||
result += "["; | ||
result += std::to_string(actor->GetFormId()); | ||
result += ","; | ||
result += std::to_string(baseId); | ||
result += "]"; | ||
return result; | ||
} | ||
|
||
bool ReadBookEvent::SpellLearned() const | ||
{ | ||
return spellLearned; | ||
} | ||
|
||
void ReadBookEvent::OnFireSuccess(WorldState* worldState) | ||
{ | ||
auto& loader = actor->GetParent()->GetEspm(); | ||
auto bookLookupResult = loader.GetBrowser().LookupById(baseId); | ||
|
||
if (!bookLookupResult.rec) { | ||
spdlog::error("ReadBookEvent::OnFireSuccess - Actor {:x} reading book: No " | ||
"book form {:x}", | ||
actor->GetFormId(), baseId); | ||
return; | ||
} | ||
|
||
const auto bookData = espm::GetData<espm::BOOK>(baseId, actor->GetParent()); | ||
const auto spellOrSkillFormId = | ||
bookLookupResult.ToGlobalId(bookData.spellOrSkillFormId); | ||
|
||
if (bookData.IsFlagSet(espm::BOOK::Flags::TeachesSpell)) { | ||
if (actor->IsSpellLearned(spellOrSkillFormId)) { | ||
spdlog::info("ReadBookEvent::OnFireSuccess - Actor {:x} reading book: " | ||
"Spell already learned " | ||
"{:x}, not spending the book", | ||
actor->GetFormId(), spellOrSkillFormId); | ||
return; | ||
} | ||
actor->AddSpell(spellOrSkillFormId); | ||
spellLearned = true; | ||
return; | ||
} else if (bookData.IsFlagSet(espm::BOOK::Flags::TeachesSkill)) { | ||
spdlog::info( | ||
"ReadBookEvent::OnFireSuccess - Actor {:x} reading skill book {:x} " | ||
"detected, not implemented", | ||
actor->GetFormId(), baseId); | ||
return; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
skymp5-server/cpp/server_guest_lib/gamemode_events/ReadBookEvent.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#pragma once | ||
#include "GameModeEvent.h" | ||
|
||
class MpActor; | ||
|
||
class ReadBookEvent : public GameModeEvent | ||
{ | ||
public: | ||
ReadBookEvent(MpActor* actor_, uint32_t baseId_); | ||
|
||
const char* GetName() const override; | ||
|
||
std::string GetArgumentsJsonArray() const override; | ||
|
||
bool SpellLearned() const; | ||
|
||
private: | ||
void OnFireSuccess(WorldState* worldState) override; | ||
|
||
// event arguments | ||
MpActor* actor = nullptr; | ||
uint32_t baseId = 0; | ||
|
||
// OnFireSuccess-specific arguments | ||
// ... | ||
|
||
// Results | ||
bool spellLearned = false; | ||
}; |