Updates to upgrades

This commit is contained in:
wincent 2024-06-06 09:37:24 +02:00
parent 364bcf822a
commit ac1b00fdaa
7 changed files with 209 additions and 3 deletions

View File

@ -91,6 +91,8 @@ void nejlika::NejlikaHooks::InstallHooks()
auto& upgradeTemplate = *upgradeTemplateOpt.value(); auto& upgradeTemplate = *upgradeTemplateOpt.value();
entityData.AddUpgradeItem(item->GetId()); entityData.AddUpgradeItem(item->GetId());
entityData.TriggerUpgradeItems(UpgradeTriggerType::Equip);
}; };
EntityManager::OnEntityCreated += [](Entity* entity) { EntityManager::OnEntityCreated += [](Entity* entity) {
@ -134,6 +136,8 @@ void nejlika::NejlikaHooks::InstallHooks()
additionalData.AddUpgradeItem(id); additionalData.AddUpgradeItem(id);
} }
} }
additionalData.TriggerUpgradeItems(UpgradeTriggerType::Equip);
}; };
EntityManager::OnEntityDestroyed += [](Entity* entity) { EntityManager::OnEntityDestroyed += [](Entity* entity) {
@ -192,6 +196,8 @@ void nejlika::NejlikaHooks::InstallHooks()
auto& entityData = *entityDataOpt.value(); auto& entityData = *entityDataOpt.value();
entityData.TriggerUpgradeItems(UpgradeTriggerType::Equip);
entityData.ApplyToEntity(); entityData.ApplyToEntity();
const auto itemDataOpt = GetAdditionalItemData(item->GetId()); const auto itemDataOpt = GetAdditionalItemData(item->GetId());
@ -232,6 +238,8 @@ void nejlika::NejlikaHooks::InstallHooks()
auto& entityData = *entityDataOpt.value(); auto& entityData = *entityDataOpt.value();
entityData.TriggerUpgradeItems(UpgradeTriggerType::UnEquip);
entityData.ApplyToEntity(); entityData.ApplyToEntity();
}; };

View File

@ -2,6 +2,7 @@
#include "GeneralUtils.h" #include "GeneralUtils.h"
#include "GameMessages.h" #include "GameMessages.h"
#include "InventoryComponent.h"
#include <magic_enum.hpp> #include <magic_enum.hpp>
#include <iostream> #include <iostream>
@ -48,6 +49,28 @@ nlohmann::json nejlika::UpgradeEffect::ToJson() const
json["effect-type"] = effectType; json["effect-type"] = effectType;
} }
if (!conditions.empty()) {
nlohmann::json conditionsJson = nlohmann::json::array();
for (const auto& condition : conditions) {
conditionsJson.push_back(magic_enum::enum_name(condition));
}
json["conditions"] = conditionsJson;
}
if (equipSkillID != 0) {
json["grant-skill-id"] = equipSkillID;
}
if (equipSkillSlot != BehaviorSlot::Invalid) {
json["grant-skill-slot"] = magic_enum::enum_name(equipSkillSlot);
}
if (unequipSkill) {
json["unequip-skill"] = true;
}
return json; return json;
} }
@ -70,10 +93,12 @@ void nejlika::UpgradeEffect::Load(const nlohmann::json& json)
modifiers.clear(); modifiers.clear();
if (json.contains("modifiers")){
for (const auto& modifier : json["modifiers"]) { for (const auto& modifier : json["modifiers"]) {
ModifierTemplate effect(modifier); ModifierTemplate effect(modifier);
modifiers.push_back(effect); modifiers.push_back(effect);
} }
}
if (json.contains("chance")) { if (json.contains("chance")) {
chance.clear(); chance.clear();
@ -93,6 +118,26 @@ void nejlika::UpgradeEffect::Load(const nlohmann::json& json)
if (json.contains("effect-type")) { if (json.contains("effect-type")) {
effectType = json["effect-type"].get<std::string>(); effectType = json["effect-type"].get<std::string>();
} }
if (json.contains("conditions")) {
conditions.clear();
for (const auto& condition : json["conditions"]) {
conditions.push_back(magic_enum::enum_cast<UpgradeTriggerCondition>(condition.get<std::string>()).value_or(UpgradeTriggerCondition::None));
}
}
if (json.contains("grant-skill-id")) {
equipSkillID = json["grant-skill-id"].get<int32_t>();
}
if (json.contains("grant-skill-slot")) {
equipSkillSlot = magic_enum::enum_cast<BehaviorSlot>(json["grant-skill-slot"].get<std::string>()).value_or(BehaviorSlot::Invalid);
}
if (json.contains("unequip-skill")) {
unequipSkill = json["unequip-skill"].get<bool>();
}
} }
float nejlika::UpgradeEffect::CalculateChance(int32_t level) const { float nejlika::UpgradeEffect::CalculateChance(int32_t level) const {
@ -112,6 +157,88 @@ float nejlika::UpgradeEffect::CalculateChance(int32_t level) const {
return value; return value;
} }
bool nejlika::UpgradeEffect::CheckConditions(LWOOBJID origin) const {
auto* entity = Game::entityManager->GetEntity(origin);
if (!entity) {
return false;
}
auto* inventory = entity->GetComponent<InventoryComponent>();
if (!inventory) {
return false;
}
const auto& skills = inventory->GetSkills();
const auto& equipped = inventory->GetEquippedItems();
for (const auto& condition : conditions) {
switch (condition) {
case UpgradeTriggerCondition::None:
break;
case UpgradeTriggerCondition::Unarmed:
if (equipped.contains("special_r")) {
return false;
}
break;
case UpgradeTriggerCondition::Melee:
if (!equipped.contains("special_r")) {
return false;
}
break;
case UpgradeTriggerCondition::TwoHanded:
{
if (!equipped.contains("special_r")) {
return false;
}
const auto& weaponLot = equipped.at("special_r").lot;
const auto& info = Inventory::FindItemComponent(weaponLot);
if (!info.isTwoHanded) {
return false;
}
break;
}
case UpgradeTriggerCondition::Shield:
if (!equipped.contains("special_l")) {
return false;
}
break;
default:
break;
}
}
return true;
}
void nejlika::UpgradeEffect::OnTrigger(LWOOBJID origin) const {
auto* entity = Game::entityManager->GetEntity(origin);
if (!entity) {
return;
}
auto* inventory = entity->GetComponent<InventoryComponent>();
if (!inventory) {
return;
}
if (equipSkillID != 0) {
std::cout << "Granting skill: " << equipSkillID << " to entity: " << origin << " in slot: " << magic_enum::enum_name(equipSkillSlot) << std::endl;
inventory->SetSkill(equipSkillSlot, equipSkillID);
}
if (unequipSkill) {
std::cout << "Unequipping skill from entity: " << origin << " in slot: " << magic_enum::enum_name(equipSkillSlot) << std::endl;
inventory->ResetSkill(equipSkillSlot);
}
}
std::vector<ModifierInstance> nejlika::UpgradeEffect::Trigger(const std::vector<UpgradeEffect>& modifiers, int32_t level, UpgradeTriggerType triggerType, LWOOBJID origin) { std::vector<ModifierInstance> nejlika::UpgradeEffect::Trigger(const std::vector<UpgradeEffect>& modifiers, int32_t level, UpgradeTriggerType triggerType, LWOOBJID origin) {
std::vector<ModifierInstance> result; std::vector<ModifierInstance> result;
@ -120,16 +247,28 @@ std::vector<ModifierInstance> nejlika::UpgradeEffect::Trigger(const std::vector<
continue; continue;
} }
if (!modifier.CheckConditions(origin)) {
continue;
}
float chanceRoll = GeneralUtils::GenerateRandomNumber<float>(0, 1); float chanceRoll = GeneralUtils::GenerateRandomNumber<float>(0, 1);
if (chanceRoll > modifier.CalculateChance(level)) { if (chanceRoll > modifier.CalculateChance(level)) {
continue; continue;
} }
std::cout << "Triggering effect trigger type: " << magic_enum::enum_name(triggerType) << std::endl;
modifier.OnTrigger(origin);
auto instances = modifier.GenerateModifiers(level); auto instances = modifier.GenerateModifiers(level);
result.insert(result.end(), instances.begin(), instances.end()); result.insert(result.end(), instances.begin(), instances.end());
if (modifier.effectID == 0) {
continue;
}
GameMessages::SendPlayFXEffect( GameMessages::SendPlayFXEffect(
origin, origin,
modifier.effectID, modifier.effectID,

View File

@ -2,6 +2,8 @@
#include "ModifierTemplate.h" #include "ModifierTemplate.h"
#include "UpgradeTriggerType.h" #include "UpgradeTriggerType.h"
#include "UpgradeTriggerCondition.h"
#include <InventoryComponent.h>
#include <dCommonVars.h> #include <dCommonVars.h>
@ -21,6 +23,10 @@ public:
float CalculateChance(int32_t level) const; float CalculateChance(int32_t level) const;
bool CheckConditions(LWOOBJID origin) const;
void OnTrigger(LWOOBJID origin) const;
static std::vector<ModifierInstance> Trigger(const std::vector<UpgradeEffect>& modifiers, int32_t level, UpgradeTriggerType triggerType, LWOOBJID origin); static std::vector<ModifierInstance> Trigger(const std::vector<UpgradeEffect>& modifiers, int32_t level, UpgradeTriggerType triggerType, LWOOBJID origin);
// Getters // Getters
@ -37,7 +43,11 @@ private:
}; };
std::vector<UpgradeScale> chance; std::vector<UpgradeScale> chance;
std::vector<UpgradeTriggerCondition> conditions;
UpgradeTriggerType triggerType; UpgradeTriggerType triggerType;
int32_t equipSkillID = 0;
bool unequipSkill = false;
BehaviorSlot equipSkillSlot = BehaviorSlot::Invalid;
std::vector<ModifierTemplate> modifiers; std::vector<ModifierTemplate> modifiers;
int32_t effectID = 0; int32_t effectID = 0;
std::string effectType = ""; std::string effectType = "";

View File

@ -0,0 +1,17 @@
#pragma once
#include <cstdint>
namespace nejlika
{
enum class UpgradeTriggerCondition
{
None,
Unarmed,
Melee,
TwoHanded,
Shield
};
}

View File

@ -8,6 +8,8 @@ namespace nejlika
enum class UpgradeTriggerType enum class UpgradeTriggerType
{ {
OnHit, OnHit,
Equip,
UnEquip,
}; };
} }

View File

@ -1617,3 +1617,31 @@ bool InventoryComponent::SetSkill(BehaviorSlot slot, uint32_t skillId) {
m_Skills.insert_or_assign(slot, skillId); m_Skills.insert_or_assign(slot, skillId);
return true; return true;
} }
void InventoryComponent::UnsetSkill(BehaviorSlot slot) {
const auto index = m_Skills.find(slot);
if (index == m_Skills.end()) return;
const auto old = index->second;
GameMessages::SendRemoveSkill(m_Parent, old);
m_Skills.erase(slot);
}
void InventoryComponent::ResetSkill(BehaviorSlot slot) {
const auto index = m_Skills.find(slot);
if (index == m_Skills.end()) return;
const auto old = index->second;
GameMessages::SendRemoveSkill(m_Parent, old);
m_Skills.erase(slot);
// Loop through all equipped items and find the first item that can be equipped in the slot
for (const auto& pair : m_Equipped) {
const auto item = pair.second;
const auto info = Inventory::FindItemComponent(item.lot);
const auto behaviorSlot = FindBehaviorSlot(static_cast<eItemType>(info.itemType));
if (behaviorSlot == slot) {
SetSkill(slot, FindSkill(item.lot));
return;
}
}
}

View File

@ -372,6 +372,8 @@ public:
bool SetSkill(int slot, uint32_t skillId); bool SetSkill(int slot, uint32_t skillId);
bool SetSkill(BehaviorSlot slot, uint32_t skillId); bool SetSkill(BehaviorSlot slot, uint32_t skillId);
void UnsetSkill(BehaviorSlot slot);
void ResetSkill(BehaviorSlot slot);
~InventoryComponent() override; ~InventoryComponent() override;