[General] Track timestamps for spells in SpellsActive packets

This makes it possible to remove a specific effect in effect stacks by checking its timestamp.
pull/593/head
David Cernat 4 years ago
parent 6b45a48116
commit 294f64d12d

@ -45,13 +45,15 @@ namespace MWMechanics
Whenever a player loses an active spell, send an ID_PLAYER_SPELLS_ACTIVE packet to the server with it
*/
bool isStackingSpell = !MWBase::Environment::get().getWorld()->getStore().get<ESM::Spell>().search(iter->first);
if (this == &MWMechanics::getPlayer().getClass().getCreatureStats(MWMechanics::getPlayer()).getActiveSpells())
{
mwmp::Main::get().getLocalPlayer()->sendSpellsActiveRemoval(iter->first);
mwmp::Main::get().getLocalPlayer()->sendSpellsActiveRemoval(iter->first, isStackingSpell, iter->second.mTimeStamp);
}
else if (mwmp::Main::get().getCellController()->isLocalActor(MechanicsHelper::getCurrentActor()))
{
mwmp::Main::get().getCellController()->getLocalActor(MechanicsHelper::getCurrentActor())->sendSpellsActiveRemoval(iter->first);
mwmp::Main::get().getCellController()->getLocalActor(MechanicsHelper::getCurrentActor())->sendSpellsActiveRemoval(iter->first, isStackingSpell, iter->second.mTimeStamp);
}
/*
End of tes3mp addition
@ -173,8 +175,16 @@ namespace MWMechanics
return mSpells;
}
/*
Start of tes3mp change (major)
Add a timestamp argument so spells received from other clients can have the same timestamps they had there
*/
void ActiveSpells::addSpell(const std::string &id, bool stack, std::vector<ActiveEffect> effects,
const std::string &displayName, int casterActorId)
const std::string &displayName, int casterActorId, MWWorld::TimeStamp timestamp)
/*
End of tes3mp change (major)
*/
{
TContainer::iterator it(mSpells.find(id));
@ -183,6 +193,16 @@ namespace MWMechanics
params.mDisplayName = displayName;
params.mCasterActorId = casterActorId;
/*
Start of tes3mp addition
Track the timestamp of this active spell so that, if spells are stacked, the correct one can be removed
*/
params.mTimeStamp = timestamp;
/*
End of tes3mp addition
*/
if (it == end() || stack)
{
mSpells.insert(std::make_pair(id, params));
@ -211,11 +231,11 @@ namespace MWMechanics
if (this == &MWMechanics::getPlayer().getClass().getCreatureStats(MWMechanics::getPlayer()).getActiveSpells())
{
mwmp::Main::get().getLocalPlayer()->sendSpellsActiveAddition(id, isStackingSpell, esmParams);
mwmp::Main::get().getLocalPlayer()->sendSpellsActiveAddition(id, isStackingSpell, esmParams, params.mTimeStamp);
}
else if (mwmp::Main::get().getCellController()->isLocalActor(MechanicsHelper::getCurrentActor()))
{
mwmp::Main::get().getCellController()->getLocalActor(MechanicsHelper::getCurrentActor())->sendSpellsActiveAddition(id, isStackingSpell, esmParams);
mwmp::Main::get().getCellController()->getLocalActor(MechanicsHelper::getCurrentActor())->sendSpellsActiveAddition(id, isStackingSpell, esmParams, params.mTimeStamp);
}
/*
End of tes3mp addition
@ -224,6 +244,23 @@ namespace MWMechanics
mSpellsChanged = true;
}
/*
Start of tes3mp addition
Declare addSpell() without the timestamp argument and make it call the version with that argument,
using the current time for the timestamp
*/
void ActiveSpells::addSpell(const std::string& id, bool stack, std::vector<ActiveEffect> effects,
const std::string& displayName, int casterActorId)
{
MWWorld::TimeStamp timestamp = MWBase::Environment::get().getWorld()->getTimeStamp();
addSpell(id, stack, effects, displayName, casterActorId, timestamp);
}
/*
End of tes3mp addition
*/
void ActiveSpells::mergeEffects(std::vector<ActiveEffect>& addTo, const std::vector<ActiveEffect>& from)
{
for (std::vector<ActiveEffect>::const_iterator effect(from.begin()); effect != from.end(); ++effect)
@ -257,6 +294,31 @@ namespace MWMechanics
}
}
/*
Start of tes3mp addition
Remove the spell with a certain ID and a certain timestamp, useful
when there are stacked spells with the same ID
*/
void ActiveSpells::removeSpellByTimestamp(const std::string& id, MWWorld::TimeStamp timestamp)
{
for (TContainer::iterator spell = mSpells.begin(); spell != mSpells.end(); ++spell)
{
if (spell->first == id)
{
if (spell->second.mTimeStamp == timestamp)
{
spell->second.mEffects.clear();
mSpellsChanged = true;
break;
}
}
}
}
/*
End of tes3mp addition
*/
void ActiveSpells::visitEffectSources(EffectSourceVisitor &visitor) const
{
for (TContainer::const_iterator it = begin(); it != end(); ++it)

@ -77,9 +77,31 @@ namespace MWMechanics
void addSpell (const std::string& id, bool stack, std::vector<ActiveEffect> effects,
const std::string& displayName, int casterActorId);
/*
Start of tes3mp addition
Add a separate addSpell() with a timestamp argument
*/
void addSpell (const std::string& id, bool stack, std::vector<ActiveEffect> effects,
const std::string& displayName, int casterActorId, MWWorld::TimeStamp timestamp);
/*
End of tes3mp addition
*/
/// Removes the active effects from this spell/potion/.. with \a id
void removeEffects (const std::string& id);
/*
Start of tes3mp addition
Remove the spell with a certain ID and a certain timestamp, useful
when there are stacked spells with the same ID
*/
void removeSpellByTimestamp(const std::string& id, MWWorld::TimeStamp timestamp);
/*
End of tes3mp addition
*/
/// Remove all active effects with this effect id
void purgeEffect (short effectId);

@ -365,8 +365,10 @@ void DedicatedActor::addSpellsActive()
for (const auto& activeSpell : spellsActiveChanges.activeSpells)
{
MWWorld::TimeStamp timestamp = MWWorld::TimeStamp(activeSpell.timestampHour, activeSpell.timestampDay);
// Don't do a check for a spell's existence, because active effects from potions need to be applied here too
activeSpells.addSpell(activeSpell.id, activeSpell.isStackingSpell, activeSpell.params.mEffects, activeSpell.params.mDisplayName, 1);
activeSpells.addSpell(activeSpell.id, activeSpell.isStackingSpell, activeSpell.params.mEffects, activeSpell.params.mDisplayName, 1, timestamp);
}
reloadPtr();
@ -377,10 +379,19 @@ void DedicatedActor::removeSpellsActive()
MWMechanics::ActiveSpells& activeSpells = getPtr().getClass().getCreatureStats(getPtr()).getActiveSpells();
for (const auto& activeSpell : spellsActiveChanges.activeSpells)
{
// Remove stacking spells based on their timestamps
if (activeSpell.isStackingSpell)
{
MWWorld::TimeStamp timestamp = MWWorld::TimeStamp(activeSpell.timestampHour, activeSpell.timestampDay);
activeSpells.removeSpellByTimestamp(activeSpell.id, timestamp);
}
else
{
activeSpells.removeEffects(activeSpell.id);
}
}
}
void DedicatedActor::setSpellsActive()
{

@ -490,8 +490,10 @@ void DedicatedPlayer::addSpellsActive()
for (const auto& activeSpell : spellsActiveChanges.activeSpells)
{
MWWorld::TimeStamp timestamp = MWWorld::TimeStamp(activeSpell.timestampHour, activeSpell.timestampDay);
// Don't do a check for a spell's existence, because active effects from potions need to be applied here too
activeSpells.addSpell(activeSpell.id, activeSpell.isStackingSpell, activeSpell.params.mEffects, activeSpell.params.mDisplayName, 1);
activeSpells.addSpell(activeSpell.id, activeSpell.isStackingSpell, activeSpell.params.mEffects, activeSpell.params.mDisplayName, 1, timestamp);
}
}
@ -500,10 +502,19 @@ void DedicatedPlayer::removeSpellsActive()
MWMechanics::ActiveSpells& activeSpells = getPtr().getClass().getCreatureStats(getPtr()).getActiveSpells();
for (const auto& activeSpell : spellsActiveChanges.activeSpells)
{
// Remove stacking spells based on their timestamps
if (activeSpell.isStackingSpell)
{
MWWorld::TimeStamp timestamp = MWWorld::TimeStamp(activeSpell.timestampHour, activeSpell.timestampDay);
activeSpells.removeSpellByTimestamp(activeSpell.id, timestamp);
}
else
{
activeSpells.removeEffects(activeSpell.id);
}
}
}
void DedicatedPlayer::setSpellsActive()
{

@ -285,7 +285,7 @@ void LocalActor::sendEquipment()
Main::get().getNetworking()->getActorPacket(ID_ACTOR_EQUIPMENT)->Send();
}
void LocalActor::sendSpellsActiveAddition(const std::string id, bool isStackingSpell, ESM::ActiveSpells::ActiveSpellParams params)
void LocalActor::sendSpellsActiveAddition(const std::string id, bool isStackingSpell, ESM::ActiveSpells::ActiveSpellParams params, MWWorld::TimeStamp timestamp)
{
// Skip any bugged spells that somehow have clientside-only dynamic IDs
if (id.find("$dynamic") != std::string::npos)
@ -296,6 +296,8 @@ void LocalActor::sendSpellsActiveAddition(const std::string id, bool isStackingS
mwmp::ActiveSpell spell;
spell.id = id;
spell.isStackingSpell = isStackingSpell;
spell.timestampDay = timestamp.getDay();
spell.timestampHour = timestamp.getHour();
spell.params = params;
spellsActiveChanges.activeSpells.push_back(spell);
@ -308,7 +310,7 @@ void LocalActor::sendSpellsActiveAddition(const std::string id, bool isStackingS
Main::get().getNetworking()->getActorPacket(ID_ACTOR_SPELLS_ACTIVE)->Send();
}
void LocalActor::sendSpellsActiveRemoval(const std::string id)
void LocalActor::sendSpellsActiveRemoval(const std::string id, bool isStackingSpell, MWWorld::TimeStamp timestamp)
{
// Skip any bugged spells that somehow have clientside-only dynamic IDs
if (id.find("$dynamic") != std::string::npos)
@ -318,6 +320,9 @@ void LocalActor::sendSpellsActiveRemoval(const std::string id)
mwmp::ActiveSpell spell;
spell.id = id;
spell.isStackingSpell = isStackingSpell;
spell.timestampDay = timestamp.getDay();
spell.timestampHour = timestamp.getHour();
spellsActiveChanges.activeSpells.push_back(spell);
spellsActiveChanges.action = mwmp::SpellsActiveChanges::REMOVE;

@ -4,6 +4,7 @@
#include <components/openmw-mp/Base/BaseActor.hpp>
#include "../mwmechanics/creaturestats.hpp"
#include "../mwworld/manualref.hpp"
#include "../mwworld/timestamp.hpp"
namespace mwmp
{
@ -26,8 +27,8 @@ namespace mwmp
void updateAttackOrCast();
void sendEquipment();
void sendSpellsActiveAddition(const std::string id, bool isStackingSpell, ESM::ActiveSpells::ActiveSpellParams params);
void sendSpellsActiveRemoval(const std::string id);
void sendSpellsActiveAddition(const std::string id, bool isStackingSpell, ESM::ActiveSpells::ActiveSpellParams params, MWWorld::TimeStamp timestamp);
void sendSpellsActiveRemoval(const std::string id, bool isStackingSpell, MWWorld::TimeStamp timestamp);
void sendDeath(char newDeathState);
MWWorld::Ptr getPtr();

@ -718,8 +718,10 @@ void LocalPlayer::addSpellsActive()
for (const auto& activeSpell : spellsActiveChanges.activeSpells)
{
MWWorld::TimeStamp timestamp = MWWorld::TimeStamp(activeSpell.timestampHour, activeSpell.timestampDay);
// Don't do a check for a spell's existence, because active effects from potions need to be applied here too
activeSpells.addSpell(activeSpell.id, activeSpell.isStackingSpell, activeSpell.params.mEffects, activeSpell.params.mDisplayName, 1);
activeSpells.addSpell(activeSpell.id, activeSpell.isStackingSpell, activeSpell.params.mEffects, activeSpell.params.mDisplayName, 1, timestamp);
}
}
@ -820,10 +822,19 @@ void LocalPlayer::removeSpellsActive()
MWMechanics::ActiveSpells& activeSpells = ptrPlayer.getClass().getCreatureStats(ptrPlayer).getActiveSpells();
for (const auto& activeSpell : spellsActiveChanges.activeSpells)
{
// Remove stacking spells based on their timestamps
if (activeSpell.isStackingSpell)
{
MWWorld::TimeStamp timestamp = MWWorld::TimeStamp(activeSpell.timestampHour, activeSpell.timestampDay);
activeSpells.removeSpellByTimestamp(activeSpell.id, timestamp);
}
else
{
activeSpells.removeEffects(activeSpell.id);
}
}
}
void LocalPlayer::die()
{
@ -1589,7 +1600,7 @@ void LocalPlayer::sendSpellsActive()
getNetworking()->getPlayerPacket(ID_PLAYER_SPELLS_ACTIVE)->Send();
}
void LocalPlayer::sendSpellsActiveAddition(const std::string id, bool isStackingSpell, ESM::ActiveSpells::ActiveSpellParams params)
void LocalPlayer::sendSpellsActiveAddition(const std::string id, bool isStackingSpell, ESM::ActiveSpells::ActiveSpellParams params, MWWorld::TimeStamp timestamp)
{
// Skip any bugged spells that somehow have clientside-only dynamic IDs
if (id.find("$dynamic") != std::string::npos)
@ -1600,15 +1611,20 @@ void LocalPlayer::sendSpellsActiveAddition(const std::string id, bool isStacking
mwmp::ActiveSpell spell;
spell.id = id;
spell.isStackingSpell = isStackingSpell;
spell.timestampDay = timestamp.getDay();
spell.timestampHour = timestamp.getHour();
spell.params = params;
spellsActiveChanges.activeSpells.push_back(spell);
LOG_MESSAGE_SIMPLE(TimedLog::LOG_INFO, "Sending active spell addition with stacking %s, timestamp %i %f",
spell.isStackingSpell ? "true" : "false", spell.timestampDay, spell.timestampHour);
spellsActiveChanges.action = mwmp::SpellsActiveChanges::ADD;
getNetworking()->getPlayerPacket(ID_PLAYER_SPELLS_ACTIVE)->setPlayer(this);
getNetworking()->getPlayerPacket(ID_PLAYER_SPELLS_ACTIVE)->Send();
}
void LocalPlayer::sendSpellsActiveRemoval(const std::string id)
void LocalPlayer::sendSpellsActiveRemoval(const std::string id, bool isStackingSpell, MWWorld::TimeStamp timestamp)
{
// Skip any bugged spells that somehow have clientside-only dynamic IDs
if (id.find("$dynamic") != std::string::npos)
@ -1618,8 +1634,14 @@ void LocalPlayer::sendSpellsActiveRemoval(const std::string id)
mwmp::ActiveSpell spell;
spell.id = id;
spell.isStackingSpell = isStackingSpell;
spell.timestampDay = timestamp.getDay();
spell.timestampHour = timestamp.getHour();
spellsActiveChanges.activeSpells.push_back(spell);
LOG_MESSAGE_SIMPLE(TimedLog::LOG_INFO, "Sending active spell removal with stacking %s, timestamp %i %f",
spell.isStackingSpell ? "true" : "false", spell.timestampDay, spell.timestampHour);
spellsActiveChanges.action = mwmp::SpellsActiveChanges::REMOVE;
getNetworking()->getPlayerPacket(ID_PLAYER_SPELLS_ACTIVE)->setPlayer(this);
getNetworking()->getPlayerPacket(ID_PLAYER_SPELLS_ACTIVE)->Send();

@ -3,6 +3,7 @@
#include <components/openmw-mp/Base/BasePlayer.hpp>
#include "../mwworld/ptr.hpp"
#include "../mwworld/timestamp.hpp"
#include <RakNetTypes.h>
namespace mwmp
@ -91,8 +92,8 @@ namespace mwmp
void sendSpellbook();
void sendSpellChange(std::string id, unsigned int action);
void sendSpellsActive();
void sendSpellsActiveAddition(const std::string id, bool isStackingSpell, ESM::ActiveSpells::ActiveSpellParams params);
void sendSpellsActiveRemoval(const std::string id);
void sendSpellsActiveAddition(const std::string id, bool isStackingSpell, ESM::ActiveSpells::ActiveSpellParams params, MWWorld::TimeStamp timestamp);
void sendSpellsActiveRemoval(const std::string id, bool isStackingSpell, MWWorld::TimeStamp timestamp);
void sendQuickKey(unsigned short slot, int type, const std::string& itemId = "");
void sendJournalEntry(const std::string& quest, int index, const MWWorld::Ptr& actor);
void sendJournalIndex(const std::string& quest, int index);

@ -161,6 +161,8 @@ namespace mwmp
{
std::string id;
bool isStackingSpell;
int timestampDay;
double timestampHour;
ESM::ActiveSpells::ActiveSpellParams params;
};

@ -30,6 +30,8 @@ void PacketActorSpellsActive::Actor(BaseActor &actor, bool send)
{
RW(activeSpell.id, send, true);
RW(activeSpell.isStackingSpell, send);
RW(activeSpell.timestampDay, send);
RW(activeSpell.timestampHour, send);
RW(activeSpell.params.mDisplayName, send, true);
uint32_t effectCount;

@ -31,6 +31,8 @@ void PacketPlayerSpellsActive::Packet(RakNet::BitStream *newBitstream, bool send
{
RW(activeSpell.id, send, true);
RW(activeSpell.isStackingSpell, send);
RW(activeSpell.timestampDay, send);
RW(activeSpell.timestampHour, send);
RW(activeSpell.params.mDisplayName, send, true);
uint32_t effectCount;

Loading…
Cancel
Save