Skip to content
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

Conditions handled by std::shared_ptr #4829

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ void BedItem::regeneratePlayer(Player* player) const
{
const uint32_t sleptTime = time(nullptr) - sleepStart;

Condition* condition = player->getCondition(CONDITION_REGENERATION, CONDITIONID_DEFAULT);
std::shared_ptr<Condition> condition = player->getCondition(CONDITION_REGENERATION, CONDITIONID_DEFAULT);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

auto here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

auto here?

image

You are literally contradicting yourself... I don't understand. Either you want the explicit type declared for clarity, or you don't. I understand that "getCondition" would imply that its returning a condition of some sort, raw, ref, shared, we don't know.. but that is implied, which means its implicit... not explicit... so which exactly is your preference?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Read it again, until the dot

if (condition) {
uint32_t regen;
if (condition->getTicks() != -1) {
Expand Down
10 changes: 5 additions & 5 deletions src/combat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ void Combat::doCombat(Creature* caster, Creature* target) const
if (params.origin != ORIGIN_MELEE) {
for (const auto& condition : params.conditionList) {
if (caster == target || !target->isImmune(condition->getType())) {
Condition* conditionCopy = condition->clone();
std::shared_ptr<Condition> conditionCopy = condition->clone();
conditionCopy->setParam(CONDITION_PARAM_OWNER, caster->getID());
target->addCombatCondition(conditionCopy);
}
Expand Down Expand Up @@ -756,7 +756,7 @@ void Combat::doCombat(Creature* caster, const Position& position) const
(caster != creature && Combat::canDoCombat(caster, creature) == RETURNVALUE_NOERROR)) {
for (const auto& condition : params.conditionList) {
if (caster == creature || !creature->isImmune(condition->getType())) {
Condition* conditionCopy = condition->clone();
std::shared_ptr<Condition> conditionCopy = condition->clone();
if (caster) {
conditionCopy->setParam(CONDITION_PARAM_OWNER, caster->getID());
}
Expand Down Expand Up @@ -829,7 +829,7 @@ void Combat::doTargetCombat(Creature* caster, Creature* target, CombatDamage& da
if (damage.blockType == BLOCK_NONE || damage.blockType == BLOCK_ARMOR) {
for (const auto& condition : params.conditionList) {
if (caster == target || !target->isImmune(condition->getType())) {
Condition* conditionCopy = condition->clone();
std::shared_ptr<Condition> conditionCopy = condition->clone();
if (caster) {
conditionCopy->setParam(CONDITION_PARAM_OWNER, caster->getID());
}
Expand Down Expand Up @@ -997,7 +997,7 @@ void Combat::doAreaCombat(Creature* caster, const Position& position, const Area
if (damage.blockType == BLOCK_NONE || damage.blockType == BLOCK_ARMOR) {
for (const auto& condition : params.conditionList) {
if (caster == creature || !creature->isImmune(condition->getType())) {
Condition* conditionCopy = condition->clone();
std::shared_ptr<Condition> conditionCopy = condition->clone();
if (caster) {
conditionCopy->setParam(CONDITION_PARAM_OWNER, caster->getID());
}
Expand Down Expand Up @@ -1397,7 +1397,7 @@ void MagicField::onStepInField(Creature* creature)

const ItemType& it = items[getID()];
if (it.conditionDamage) {
Condition* conditionCopy = it.conditionDamage->clone();
std::shared_ptr<Condition> conditionCopy = it.conditionDamage->clone();
uint32_t ownerId = getOwner();
if (ownerId) {
bool harmfulField = true;
Expand Down
7 changes: 5 additions & 2 deletions src/combat.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class TargetCallback final : public CallBack

struct CombatParams
{
std::forward_list<std::unique_ptr<const Condition>> conditionList = {};
std::forward_list<std::shared_ptr<const Condition>> conditionList = {};

std::unique_ptr<ValueCallback> valueCallback = nullptr;
std::unique_ptr<TileCallback> tileCallback = nullptr;
Expand Down Expand Up @@ -115,7 +115,10 @@ class Combat

void setArea(AreaCombat* area);
bool hasArea() const { return area != nullptr; }
void addCondition(const Condition* condition) { params.conditionList.emplace_front(condition); }
void addCondition(const std::shared_ptr<const Condition>& condition)
{
params.conditionList.emplace_front(condition);
}
void clearConditions() { params.conditionList.clear(); }
void setPlayerCombatValues(formulaType_t formulaType, double mina, double minb, double maxa, double maxb);
void postCombatEffects(Creature* caster, const Position& pos) const { postCombatEffects(caster, pos, params); }
Expand Down
70 changes: 36 additions & 34 deletions src/condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,9 @@ bool Condition::executeCondition(Creature*, int32_t interval)
return getEndTime() >= OTSYS_TIME();
}

Condition* Condition::createCondition(ConditionId_t id, ConditionType_t type, int32_t ticks, int32_t param /* = 0*/,
bool buff /* = false*/, uint32_t subId /* = 0*/, bool aggressive /* = false */)
std::shared_ptr<Condition> Condition::createCondition(ConditionId_t id, ConditionType_t type, int32_t ticks,
int32_t param /* = 0*/, bool buff /* = false*/,
uint32_t subId /* = 0*/, bool aggressive /* = false */)
{
switch (type) {
case CONDITION_POISON:
Expand All @@ -177,38 +178,39 @@ Condition* Condition::createCondition(ConditionId_t id, ConditionType_t type, in
case CONDITION_DAZZLED:
case CONDITION_CURSED:
case CONDITION_BLEEDING:
return new ConditionDamage(id, type, buff, subId, aggressive);
return std::make_shared<ConditionDamage>(id, type, buff, subId, aggressive);

case CONDITION_HASTE:
case CONDITION_PARALYZE:
return new ConditionSpeed(id, type, ticks, buff, subId, param, aggressive);
return std::make_shared<ConditionSpeed>(id, type, ticks, buff, subId, param, aggressive);

case CONDITION_INVISIBLE:
return new ConditionInvisible(id, type, ticks, buff, subId, aggressive);
return std::make_shared<ConditionInvisible>(id, type, ticks, buff, subId, aggressive);

case CONDITION_OUTFIT:
return new ConditionOutfit(id, type, ticks, buff, subId, aggressive);
return std::make_shared<ConditionOutfit>(id, type, ticks, buff, subId, aggressive);

case CONDITION_LIGHT:
return new ConditionLight(id, type, ticks, buff, subId, param & 0xFF, (param & 0xFF00) >> 8, aggressive);
return std::make_shared<ConditionLight>(id, type, ticks, buff, subId, param & 0xFF, (param & 0xFF00) >> 8,
aggressive);

case CONDITION_REGENERATION:
return new ConditionRegeneration(id, type, ticks, buff, subId, aggressive);
return std::make_shared<ConditionRegeneration>(id, type, ticks, buff, subId, aggressive);

case CONDITION_SOUL:
return new ConditionSoul(id, type, ticks, buff, subId, aggressive);
return std::make_shared<ConditionSoul>(id, type, ticks, buff, subId, aggressive);

case CONDITION_ATTRIBUTES:
return new ConditionAttributes(id, type, ticks, buff, subId, aggressive);
return std::make_shared<ConditionAttributes>(id, type, ticks, buff, subId, aggressive);

case CONDITION_SPELLCOOLDOWN:
return new ConditionSpellCooldown(id, type, ticks, buff, subId, aggressive);
return std::make_shared<ConditionSpellCooldown>(id, type, ticks, buff, subId, aggressive);

case CONDITION_SPELLGROUPCOOLDOWN:
return new ConditionSpellGroupCooldown(id, type, ticks, buff, subId, aggressive);
return std::make_shared<ConditionSpellGroupCooldown>(id, type, ticks, buff, subId, aggressive);

case CONDITION_DRUNK:
return new ConditionDrunk(id, type, ticks, buff, subId, param, aggressive);
return std::make_shared<ConditionDrunk>(id, type, ticks, buff, subId, param, aggressive);

case CONDITION_INFIGHT:
case CONDITION_EXHAUST_WEAPON:
Expand All @@ -219,18 +221,18 @@ Condition* Condition::createCondition(ConditionId_t id, ConditionType_t type, in
case CONDITION_YELLTICKS:
case CONDITION_PACIFIED:
case CONDITION_MANASHIELD:
return new ConditionGeneric(id, type, ticks, buff, subId, aggressive);
return std::make_shared<ConditionGeneric>(id, type, ticks, buff, subId, aggressive);
case CONDITION_ROOT:
return new ConditionGeneric(id, type, ticks, buff, subId, aggressive);
return std::make_shared<ConditionGeneric>(id, type, ticks, buff, subId, aggressive);
case CONDITION_MANASHIELD_BREAKABLE:
return new ConditionManaShield(id, type, ticks, buff, subId);
return std::make_shared<ConditionManaShield>(id, type, ticks, buff, subId);

default:
return nullptr;
}
}

Condition* Condition::createCondition(PropStream& propStream)
std::shared_ptr<Condition> Condition::createCondition(PropStream& propStream)
{
uint8_t attr;
if (!propStream.read<uint8_t>(attr) || attr != CONDITIONATTR_TYPE) {
Expand Down Expand Up @@ -314,7 +316,7 @@ bool Condition::isPersistent() const

uint32_t Condition::getIcons() const { return isBuff ? ICON_PARTY_BUFF : 0; }

bool Condition::updateCondition(const Condition* addCondition)
bool Condition::updateCondition(const std::shared_ptr<const Condition>& addCondition)
{
if (conditionType != addCondition->getType()) {
return false;
Expand Down Expand Up @@ -343,7 +345,7 @@ void ConditionGeneric::endCondition(Creature*)
//
}

void ConditionGeneric::addCondition(Creature*, const Condition* condition)
void ConditionGeneric::addCondition(Creature*, const std::shared_ptr<const Condition>& condition)
{
if (updateCondition(condition)) {
setTicks(condition->getTicks());
Expand Down Expand Up @@ -374,7 +376,7 @@ uint32_t ConditionGeneric::getIcons() const
return icons;
}

void ConditionAttributes::addCondition(Creature* creature, const Condition* condition)
void ConditionAttributes::addCondition(Creature* creature, const std::shared_ptr<const Condition>& condition)
{
if (updateCondition(condition)) {
setTicks(condition->getTicks());
Expand Down Expand Up @@ -832,7 +834,7 @@ int32_t ConditionAttributes::getParam(ConditionParam_t param)
}
}

void ConditionRegeneration::addCondition(Creature*, const Condition* condition)
void ConditionRegeneration::addCondition(Creature*, const std::shared_ptr<const Condition>& condition)
{
if (updateCondition(condition)) {
setTicks(condition->getTicks());
Expand Down Expand Up @@ -1002,7 +1004,7 @@ int32_t ConditionRegeneration::getParam(ConditionParam_t param)
}
}

void ConditionSoul::addCondition(Creature*, const Condition* condition)
void ConditionSoul::addCondition(Creature*, const std::shared_ptr<const Condition>& condition)
{
if (updateCondition(condition)) {
setTicks(condition->getTicks());
Expand Down Expand Up @@ -1210,7 +1212,7 @@ void ConditionDamage::serialize(PropWriteStream& propWriteStream)
}
}

bool ConditionDamage::updateCondition(const Condition* addCondition)
bool ConditionDamage::updateCondition(const std::shared_ptr<const Condition>& addCondition)
{
const ConditionDamage& conditionDamage = static_cast<const ConditionDamage&>(*addCondition);
if (conditionDamage.doForceUpdate()) {
Expand Down Expand Up @@ -1395,7 +1397,7 @@ void ConditionDamage::endCondition(Creature*)
//
}

void ConditionDamage::addCondition(Creature* creature, const Condition* condition)
void ConditionDamage::addCondition(Creature* creature, const std::shared_ptr<const Condition>& condition)
{
if (condition->getType() != conditionType) {
return;
Expand Down Expand Up @@ -1616,7 +1618,7 @@ bool ConditionSpeed::executeCondition(Creature* creature, int32_t interval)

void ConditionSpeed::endCondition(Creature* creature) { g_game.changeSpeed(creature, -speedDelta); }

void ConditionSpeed::addCondition(Creature* creature, const Condition* condition)
void ConditionSpeed::addCondition(Creature* creature, const std::shared_ptr<const Condition>& condition)
{
if (conditionType != condition->getType()) {
return;
Expand Down Expand Up @@ -1722,7 +1724,7 @@ void ConditionOutfit::endCondition(Creature* creature)
g_game.internalCreatureChangeOutfit(creature, creature->getDefaultOutfit());
}

void ConditionOutfit::addCondition(Creature* creature, const Condition* condition)
void ConditionOutfit::addCondition(Creature* creature, const std::shared_ptr<const Condition>& condition)
{
if (updateCondition(condition)) {
setTicks(condition->getTicks());
Expand Down Expand Up @@ -1771,7 +1773,7 @@ void ConditionLight::endCondition(Creature* creature)
g_game.changeLight(creature);
}

void ConditionLight::addCondition(Creature* creature, const Condition* condition)
void ConditionLight::addCondition(Creature* creature, const std::shared_ptr<const Condition>& condition)
{
if (updateCondition(condition)) {
setTicks(condition->getTicks());
Expand Down Expand Up @@ -1871,7 +1873,7 @@ void ConditionLight::serialize(PropWriteStream& propWriteStream)
propWriteStream.write<uint32_t>(lightChangeInterval);
}

void ConditionSpellCooldown::addCondition(Creature* creature, const Condition* condition)
void ConditionSpellCooldown::addCondition(Creature* creature, const std::shared_ptr<const Condition>& condition)
{
if (updateCondition(condition)) {
setTicks(condition->getTicks());
Expand Down Expand Up @@ -1900,7 +1902,7 @@ bool ConditionSpellCooldown::startCondition(Creature* creature)
return true;
}

void ConditionSpellGroupCooldown::addCondition(Creature* creature, const Condition* condition)
void ConditionSpellGroupCooldown::addCondition(Creature* creature, const std::shared_ptr<const Condition>& condition)
{
if (updateCondition(condition)) {
setTicks(condition->getTicks());
Expand Down Expand Up @@ -1939,19 +1941,19 @@ bool ConditionDrunk::startCondition(Creature* creature)
return true;
}

bool ConditionDrunk::updateCondition(const Condition* addCondition)
bool ConditionDrunk::updateCondition(const std::shared_ptr<const Condition>& addCondition)
{
const ConditionDrunk* conditionDrunk = static_cast<const ConditionDrunk*>(addCondition);
const auto& conditionDrunk = std::static_pointer_cast<const ConditionDrunk>(addCondition);
return conditionDrunk->drunkenness > drunkenness;
}

void ConditionDrunk::addCondition(Creature* creature, const Condition* condition)
void ConditionDrunk::addCondition(Creature* creature, const std::shared_ptr<const Condition>& condition)
{
if (!updateCondition(condition)) {
return;
}

const ConditionDrunk* conditionDrunk = static_cast<const ConditionDrunk*>(condition);
const auto& conditionDrunk = std::static_pointer_cast<const ConditionDrunk>(condition);
setTicks(conditionDrunk->getTicks());
creature->setDrunkenness(conditionDrunk->drunkenness);
}
Expand Down Expand Up @@ -1999,7 +2001,7 @@ void ConditionManaShield::endCondition(Creature* creature)
}
}

void ConditionManaShield::addCondition(Creature* creature, const Condition* addCondition)
void ConditionManaShield::addCondition(Creature* creature, const std::shared_ptr<const Condition>& addCondition)
{
if (Player* player = creature->getPlayer()) {
endCondition(player);
Expand Down
Loading
Loading