-
-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add SweetPieSpellDamageFormula for spell damage modification #2247
Merged
Pospelove
merged 10 commits into
skyrim-multiplayer:main
from
Pospelove:spelldamageformula
Dec 28, 2024
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
262de48
.
Pospelove 212ef61
.
Pospelove b044043
.
Pospelove 39d575c
.
Pospelove f39aad3
.
Pospelove 3bc0a37
.
Pospelove bf93881
Update SweetPieSpellDamageFormula.h
Pospelove ca626ff
allow 0 in spellDamageFormula entry & support player-to-npc mult
Pospelove 7a75444
./
Pospelove 2014bc7
fix
Pospelove File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
123 changes: 123 additions & 0 deletions
123
skymp5-server/cpp/server_guest_lib/formulas/SweetPieSpellDamageFormula.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,123 @@ | ||
|
||
#include "SweetPieSpellDamageFormula.h" | ||
|
||
#include "archives/JsonInputArchive.h" | ||
#include <limits> | ||
#include <sstream> | ||
|
||
namespace SweetPieSpellDamageFormulaPrivate { | ||
|
||
bool IsPlayerBaseId(const MpActor& actor) | ||
{ | ||
return actor.GetBaseId() == 0x7; | ||
} | ||
|
||
float SelectRespectiveMult( | ||
const MpActor& aggressor, const MpActor& target, | ||
const SweetPieSpellDamageFormulaSettingsEntry& entry) | ||
{ | ||
const bool isPlayerAggressor = IsPlayerBaseId(aggressor); | ||
const bool isPlayerTarget = IsPlayerBaseId(target); | ||
|
||
if (isPlayerAggressor && isPlayerTarget) { | ||
return entry.multPlayerHitsPlayer.has_value() ? *entry.multPlayerHitsPlayer | ||
: 1.f; | ||
} | ||
|
||
if (isPlayerAggressor && !isPlayerTarget) { | ||
return entry.multPlayerHitsNpc.has_value() ? *entry.multPlayerHitsNpc | ||
: 1.f; | ||
} | ||
|
||
return 1.f; | ||
} | ||
|
||
template <class T> | ||
T Clamp(T value, T minValue, T maxValue) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This implements the same value clamping logic as the existing
|
||
{ | ||
if (value < minValue) { | ||
return minValue; | ||
} else if (value > maxValue) { | ||
return maxValue; | ||
} else { | ||
return value; | ||
} | ||
} | ||
} | ||
|
||
SweetPieSpellDamageFormulaSettings | ||
SweetPieSpellDamageFormulaSettings::FromJson(const nlohmann::json& j) | ||
{ | ||
JsonInputArchive ar(j); | ||
SweetPieSpellDamageFormulaSettings res; | ||
res.Serialize(ar); | ||
return res; | ||
} | ||
|
||
SweetPieSpellDamageFormula::SweetPieSpellDamageFormula( | ||
std::unique_ptr<IDamageFormula> baseFormula_, const nlohmann::json& config) | ||
: baseFormula(std::move(baseFormula_)) | ||
, settings(std::nullopt) | ||
{ | ||
if (config.is_object()) { | ||
settings = ParseConfig(config); | ||
} | ||
} | ||
|
||
float SweetPieSpellDamageFormula::CalculateDamage(const MpActor& aggressor, | ||
const MpActor& target, | ||
const HitData& hitData) const | ||
{ | ||
return baseFormula->CalculateDamage(aggressor, target, hitData); | ||
} | ||
|
||
float SweetPieSpellDamageFormula::CalculateDamage( | ||
const MpActor& aggressor, const MpActor& target, | ||
const SpellCastData& spellCastData) const | ||
{ | ||
const float baseDamage = | ||
baseFormula->CalculateDamage(aggressor, target, spellCastData); | ||
|
||
if (!settings) { | ||
return baseDamage; | ||
} | ||
|
||
float biggestMult = -1; | ||
float defaultMult = 1.f; | ||
|
||
for (auto& entry : settings->entries) { | ||
const std::string& itemId = entry.itemId; | ||
const float mult = SweetPieSpellDamageFormulaPrivate::Clamp( | ||
SweetPieSpellDamageFormulaPrivate::SelectRespectiveMult(aggressor, | ||
target, entry), | ||
0.f, std::numeric_limits<float>::infinity()); | ||
|
||
uint32_t itemIdParsed = 0; | ||
|
||
if (itemId.find("0x") == 0 || itemId.find("0X") == 0) { | ||
std::stringstream ss; | ||
ss << std::hex << itemId.substr(2); // Skip "0x" | ||
ss >> itemIdParsed; | ||
} else { | ||
std::stringstream ss(itemId); | ||
ss >> itemIdParsed; | ||
} | ||
|
||
if (itemIdParsed == 0) { | ||
defaultMult = mult; | ||
} | ||
|
||
if (aggressor.GetInventory().GetItemCount(itemIdParsed) > 0) { | ||
biggestMult = std::max(biggestMult, mult); | ||
} | ||
} | ||
|
||
return biggestMult >= 0 ? biggestMult * baseDamage | ||
: defaultMult * baseDamage; | ||
} | ||
|
||
SweetPieSpellDamageFormulaSettings SweetPieSpellDamageFormula::ParseConfig( | ||
const nlohmann::json& config) const | ||
{ | ||
return SweetPieSpellDamageFormulaSettings::FromJson(config); | ||
} |
64 changes: 64 additions & 0 deletions
64
skymp5-server/cpp/server_guest_lib/formulas/SweetPieSpellDamageFormula.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,64 @@ | ||
#pragma once | ||
|
||
#include "IDamageFormula.h" | ||
#include "MpActor.h" | ||
|
||
#include <memory> | ||
#include <optional> | ||
#include <vector> | ||
|
||
#include <nlohmann/json_fwd.hpp> | ||
|
||
// Modifies vanilla damage for spells | ||
|
||
struct SweetPieSpellDamageFormulaSettingsEntry | ||
{ | ||
template <class Archive> | ||
void Serialize(Archive& archive) | ||
{ | ||
archive.Serialize("itemId", itemId) | ||
.Serialize("multPlayerHitsPlayer", multPlayerHitsPlayer) | ||
.Serialize("multPlayerHitsNpc", multPlayerHitsNpc); | ||
} | ||
|
||
std::string itemId; // supports hex and decimal ids: "0x000feef1", "0" | ||
std::optional<float> multPlayerHitsPlayer; | ||
std::optional<float> multPlayerHitsNpc; | ||
}; | ||
|
||
struct SweetPieSpellDamageFormulaSettings | ||
{ | ||
|
||
template <class Archive> | ||
void Serialize(Archive& archive) | ||
{ | ||
archive.Serialize("entries", entries); | ||
} | ||
|
||
static SweetPieSpellDamageFormulaSettings FromJson(const nlohmann::json& j); | ||
|
||
std::vector<SweetPieSpellDamageFormulaSettingsEntry> entries; | ||
}; | ||
|
||
class SweetPieSpellDamageFormula : public IDamageFormula | ||
{ | ||
public: | ||
SweetPieSpellDamageFormula(std::unique_ptr<IDamageFormula> baseFormula_, | ||
const nlohmann::json& config); | ||
|
||
[[nodiscard]] float CalculateDamage(const MpActor& aggressor, | ||
const MpActor& target, | ||
const HitData& hitData) const override; | ||
|
||
[[nodiscard]] float CalculateDamage( | ||
const MpActor& aggressor, const MpActor& target, | ||
const SpellCastData& spellCastData) const override; | ||
|
||
private: | ||
SweetPieSpellDamageFormulaSettings ParseConfig( | ||
const nlohmann::json& config) const; | ||
|
||
private: | ||
std::unique_ptr<IDamageFormula> baseFormula; | ||
std::optional<SweetPieSpellDamageFormulaSettings> settings; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anonymous namespace?