Skip to content

Commit

Permalink
improve: move Condition to smart ptrs
Browse files Browse the repository at this point in the history
  • Loading branch information
luan committed Sep 21, 2023
1 parent 60fd43f commit ff4321a
Show file tree
Hide file tree
Showing 27 changed files with 236 additions and 237 deletions.
4 changes: 2 additions & 2 deletions src/creatures/combat/combat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ void Combat::CombatConditionFunc(std::shared_ptr<Creature> caster, std::shared_p
}

if (caster == target || target && !target->isImmune(condition->getType())) {
Condition* conditionCopy = condition->clone();
auto conditionCopy = condition->clone();
if (caster) {
conditionCopy->setParam(CONDITION_PARAM_OWNER, caster->getID());
conditionCopy->setPositionParam(CONDITION_PARAM_CASTER_POSITION, caster->getPosition());
Expand Down Expand Up @@ -2054,7 +2054,7 @@ void MagicField::onStepInField(const std::shared_ptr<Creature> &creature) {

const ItemType &it = items[getID()];
if (it.conditionDamage) {
Condition* conditionCopy = it.conditionDamage->clone();
auto conditionCopy = it.conditionDamage->clone();
auto ownerId = getAttribute<uint32_t>(ItemAttribute_t::OWNER);
if (ownerId) {
bool harmfulField = true;
Expand Down
4 changes: 2 additions & 2 deletions src/creatures/combat/combat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class ChainPickerCallback final : public CallBack {
};

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

std::unique_ptr<ValueCallback> valueCallback;
std::unique_ptr<TileCallback> tileCallback;
Expand Down Expand Up @@ -296,7 +296,7 @@ class Combat {
bool hasArea() const {
return area != nullptr;
}
void addCondition(const Condition* condition) {
void addCondition(const std::shared_ptr<Condition> condition) {
params.conditionList.emplace_front(condition);
}
void setPlayerCombatValues(formulaType_t formulaType, double mina, double minb, double maxa, double maxb);
Expand Down
158 changes: 79 additions & 79 deletions src/creatures/combat/condition.cpp

Large diffs are not rendered by default.

90 changes: 45 additions & 45 deletions src/creatures/combat/condition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Player;
class PropStream;
class PropWriteStream;

class Condition {
class Condition : public SharedObject {
public:
Condition() = default;
Condition(ConditionId_t initId, ConditionType_t initType, int32_t initTicks, bool initBuff = false, uint32_t initSubId = 0) :
Expand All @@ -27,7 +27,7 @@ class Condition {
virtual bool startCondition(std::shared_ptr<Creature> creature);
virtual bool executeCondition(std::shared_ptr<Creature> creature, int32_t interval);
virtual void endCondition(std::shared_ptr<Creature> creature) = 0;
virtual void addCondition(std::shared_ptr<Creature> creature, const Condition* condition) = 0;
virtual void addCondition(std::shared_ptr<Creature> creature, const std::shared_ptr<Condition> condition) = 0;
virtual uint32_t getIcons() const;
ConditionId_t getId() const {
return id;
Expand All @@ -36,7 +36,7 @@ class Condition {
return subId;
}

virtual Condition* clone() const = 0;
virtual std::shared_ptr<Condition> clone() const = 0;

ConditionType_t getType() const {
return conditionType;
Expand All @@ -49,8 +49,8 @@ class Condition {
}
void setTicks(int32_t newTicks);

static Condition* createCondition(ConditionId_t id, ConditionType_t type, int32_t ticks, int32_t param = 0, bool buff = false, uint32_t subId = 0);
static Condition* createCondition(PropStream &propStream);
static std::shared_ptr<Condition> createCondition(ConditionId_t id, ConditionType_t type, int32_t ticks, int32_t param = 0, bool buff = false, uint32_t subId = 0);
static std::shared_ptr<Condition> createCondition(PropStream &propStream);

virtual bool setParam(ConditionParam_t param, int32_t value);
virtual bool setPositionParam(ConditionParam_t param, const Position &pos);
Expand All @@ -72,7 +72,7 @@ class Condition {
ConditionId_t id;
bool isBuff;

virtual bool updateCondition(const Condition* addCondition);
virtual bool updateCondition(const std::shared_ptr<Condition> addCondition);

private:
SoundEffect_t tickSound = SoundEffect_t::SILENCE;
Expand All @@ -90,11 +90,11 @@ class ConditionGeneric : public Condition {
bool startCondition(std::shared_ptr<Creature> creature) override;
bool executeCondition(std::shared_ptr<Creature> creature, int32_t interval) override;
void endCondition(std::shared_ptr<Creature> creature) override;
void addCondition(std::shared_ptr<Creature> creature, const Condition* condition) override;
void addCondition(std::shared_ptr<Creature> creature, const std::shared_ptr<Condition> condition) override;
uint32_t getIcons() const override;

ConditionGeneric* clone() const override {
return new ConditionGeneric(*this);
std::shared_ptr<Condition> clone() const override {
return std::make_shared<ConditionGeneric>(*this);
}
};

Expand All @@ -106,12 +106,12 @@ class ConditionAttributes final : public ConditionGeneric {
bool startCondition(std::shared_ptr<Creature> creature) final;
bool executeCondition(std::shared_ptr<Creature> creature, int32_t interval) final;
void endCondition(std::shared_ptr<Creature> creature) final;
void addCondition(std::shared_ptr<Creature> creature, const Condition* condition) final;
void addCondition(std::shared_ptr<Creature> creature, const std::shared_ptr<Condition> condition) final;

bool setParam(ConditionParam_t param, int32_t value) final;

ConditionAttributes* clone() const final {
return new ConditionAttributes(*this);
std::shared_ptr<Condition> clone() const final {
return std::make_shared<ConditionAttributes>(*this);
}

// serialization
Expand Down Expand Up @@ -172,16 +172,16 @@ class ConditionRegeneration final : public ConditionGeneric {

bool startCondition(std::shared_ptr<Creature> creature) override;
void endCondition(std::shared_ptr<Creature> creature) override;
void addCondition(std::shared_ptr<Creature> creature, const Condition* addCondition) override;
void addCondition(std::shared_ptr<Creature> creature, const std::shared_ptr<Condition> addCondition) override;
bool executeCondition(std::shared_ptr<Creature> creature, int32_t interval) override;

bool setParam(ConditionParam_t param, int32_t value) override;

uint32_t getHealthTicks(std::shared_ptr<Creature> creature) const;
uint32_t getManaTicks(std::shared_ptr<Creature> creature) const;

ConditionRegeneration* clone() const override {
return new ConditionRegeneration(*this);
std::shared_ptr<Condition> clone() const override {
return std::make_shared<ConditionRegeneration>(*this);
}

// serialization
Expand All @@ -205,13 +205,13 @@ class ConditionManaShield final : public Condition {

bool startCondition(std::shared_ptr<Creature> creature) override;
void endCondition(std::shared_ptr<Creature> creature) override;
void addCondition(std::shared_ptr<Creature> creature, const Condition* addCondition) override;
void addCondition(std::shared_ptr<Creature> creature, const std::shared_ptr<Condition> addCondition) override;
uint32_t getIcons() const override;

bool setParam(ConditionParam_t param, int32_t value) override;

ConditionManaShield* clone() const override {
return new ConditionManaShield(*this);
std::shared_ptr<Condition> clone() const override {
return std::make_shared<ConditionManaShield>(*this);
}

// serialization
Expand All @@ -227,13 +227,13 @@ class ConditionSoul final : public ConditionGeneric {
ConditionSoul(ConditionId_t initId, ConditionType_t initType, int32_t iniTicks, bool initBuff = false, uint32_t initSubId = 0) :
ConditionGeneric(initId, initType, iniTicks, initBuff, initSubId) { }

void addCondition(std::shared_ptr<Creature> creature, const Condition* addCondition) override;
void addCondition(std::shared_ptr<Creature> creature, const std::shared_ptr<Condition> addCondition) override;
bool executeCondition(std::shared_ptr<Creature> creature, int32_t interval) override;

bool setParam(ConditionParam_t param, int32_t value) override;

ConditionSoul* clone() const override {
return new ConditionSoul(*this);
std::shared_ptr<Condition> clone() const override {
return std::make_shared<ConditionSoul>(*this);
}

// serialization
Expand All @@ -254,8 +254,8 @@ class ConditionInvisible final : public ConditionGeneric {
bool startCondition(std::shared_ptr<Creature> creature) override;
void endCondition(std::shared_ptr<Creature> creature) override;

ConditionInvisible* clone() const override {
return new ConditionInvisible(*this);
std::shared_ptr<Condition> clone() const override {
return std::make_shared<ConditionInvisible>(*this);
}
};

Expand All @@ -270,11 +270,11 @@ class ConditionDamage final : public Condition {
bool startCondition(std::shared_ptr<Creature> creature) override;
bool executeCondition(std::shared_ptr<Creature> creature, int32_t interval) override;
void endCondition(std::shared_ptr<Creature> creature) override;
void addCondition(std::shared_ptr<Creature> creature, const Condition* condition) override;
void addCondition(std::shared_ptr<Creature> creature, const std::shared_ptr<Condition> condition) override;
uint32_t getIcons() const override;

ConditionDamage* clone() const override {
return new ConditionDamage(*this);
std::shared_ptr<Condition> clone() const override {
return std::make_shared<ConditionDamage>(*this);
}

bool setParam(ConditionParam_t param, int32_t value) override;
Expand Down Expand Up @@ -309,7 +309,7 @@ class ConditionDamage final : public Condition {
bool getNextDamage(int32_t &damage);
bool doDamage(std::shared_ptr<Creature> creature, int32_t healthChange);

bool updateCondition(const Condition* addCondition) override;
bool updateCondition(const std::shared_ptr<Condition> addCondition) override;
};

class ConditionFeared final : public Condition {
Expand All @@ -321,11 +321,11 @@ class ConditionFeared final : public Condition {
bool startCondition(std::shared_ptr<Creature> creature) override;
bool executeCondition(std::shared_ptr<Creature> creature, int32_t interval) override;
void endCondition(std::shared_ptr<Creature> creature) override;
void addCondition(std::shared_ptr<Creature> creature, const Condition* condition) override;
void addCondition(std::shared_ptr<Creature> creature, const std::shared_ptr<Condition> condition) override;
uint32_t getIcons() const override;

ConditionFeared* clone() const override {
return new ConditionFeared(*this);
std::shared_ptr<Condition> clone() const override {
return std::make_shared<ConditionFeared>(*this);
}

bool setPositionParam(ConditionParam_t param, const Position &pos) override;
Expand Down Expand Up @@ -360,11 +360,11 @@ class ConditionSpeed final : public Condition {
bool startCondition(std::shared_ptr<Creature> creature) override;
bool executeCondition(std::shared_ptr<Creature> creature, int32_t interval) override;
void endCondition(std::shared_ptr<Creature> creature) override;
void addCondition(std::shared_ptr<Creature> creature, const Condition* condition) override;
void addCondition(std::shared_ptr<Creature> creature, const std::shared_ptr<Condition> condition) override;
uint32_t getIcons() const override;

ConditionSpeed* clone() const override {
return new ConditionSpeed(*this);
std::shared_ptr<Condition> clone() const override {
return std::make_shared<ConditionSpeed>(*this);
}

bool setParam(ConditionParam_t param, int32_t value) override;
Expand Down Expand Up @@ -395,10 +395,10 @@ class ConditionOutfit final : public Condition {
bool startCondition(std::shared_ptr<Creature> creature) override;
bool executeCondition(std::shared_ptr<Creature> creature, int32_t interval) override;
void endCondition(std::shared_ptr<Creature> creature) override;
void addCondition(std::shared_ptr<Creature> creature, const Condition* condition) override;
void addCondition(std::shared_ptr<Creature> creature, const std::shared_ptr<Condition> condition) override;

ConditionOutfit* clone() const override {
return new ConditionOutfit(*this);
std::shared_ptr<Condition> clone() const override {
return std::make_shared<ConditionOutfit>(*this);
}

void setOutfit(const Outfit_t &outfit);
Expand All @@ -421,10 +421,10 @@ class ConditionLight final : public Condition {
bool startCondition(std::shared_ptr<Creature> creature) override;
bool executeCondition(std::shared_ptr<Creature> creature, int32_t interval) override;
void endCondition(std::shared_ptr<Creature> creature) override;
void addCondition(std::shared_ptr<Creature> creature, const Condition* addCondition) override;
void addCondition(std::shared_ptr<Creature> creature, const std::shared_ptr<Condition> addCondition) override;

ConditionLight* clone() const override {
return new ConditionLight(*this);
std::shared_ptr<Condition> clone() const override {
return std::make_shared<ConditionLight>(*this);
}

bool setParam(ConditionParam_t param, int32_t value) override;
Expand All @@ -445,10 +445,10 @@ class ConditionSpellCooldown final : public ConditionGeneric {
ConditionGeneric(initId, initType, initTicks, initBuff, initSubId) { }

bool startCondition(std::shared_ptr<Creature> creature) override;
void addCondition(std::shared_ptr<Creature> creature, const Condition* condition) override;
void addCondition(std::shared_ptr<Creature> creature, const std::shared_ptr<Condition> condition) override;

ConditionSpellCooldown* clone() const override {
return new ConditionSpellCooldown(*this);
std::shared_ptr<Condition> clone() const override {
return std::make_shared<ConditionSpellCooldown>(*this);
}
};

Expand All @@ -458,9 +458,9 @@ class ConditionSpellGroupCooldown final : public ConditionGeneric {
ConditionGeneric(initId, initType, initTicks, initBuff, initSubId) { }

bool startCondition(std::shared_ptr<Creature> creature) override;
void addCondition(std::shared_ptr<Creature> creature, const Condition* condition) override;
void addCondition(std::shared_ptr<Creature> creature, const std::shared_ptr<Condition> condition) override;

ConditionSpellGroupCooldown* clone() const override {
return new ConditionSpellGroupCooldown(*this);
std::shared_ptr<Condition> clone() const override {
return std::make_shared<ConditionSpellGroupCooldown>(*this);
}
};
6 changes: 3 additions & 3 deletions src/creatures/combat/spells.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ void Spell::applyCooldownConditions(std::shared_ptr<Player> player) const {
spellCooldown -= getWheelOfDestinyBoost(WheelSpellBoost_t::COOLDOWN, spellGrade);
}
if (spellCooldown > 0) {
Condition* condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_SPELLCOOLDOWN, spellCooldown / rate_cooldown, 0, false, spellId);
std::shared_ptr<Condition> condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_SPELLCOOLDOWN, spellCooldown / rate_cooldown, 0, false, spellId);
player->addCondition(condition);
}
}
Expand All @@ -627,7 +627,7 @@ void Spell::applyCooldownConditions(std::shared_ptr<Player> player) const {
spellGroupCooldown -= getWheelOfDestinyBoost(WheelSpellBoost_t::GROUP_COOLDOWN, spellGrade);
}
if (spellGroupCooldown > 0) {
Condition* condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_SPELLGROUPCOOLDOWN, spellGroupCooldown / rate_cooldown, 0, false, group);
std::shared_ptr<Condition> condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_SPELLGROUPCOOLDOWN, spellGroupCooldown / rate_cooldown, 0, false, group);
player->addCondition(condition);
}
}
Expand All @@ -638,7 +638,7 @@ void Spell::applyCooldownConditions(std::shared_ptr<Player> player) const {
spellSecondaryGroupCooldown -= getWheelOfDestinyBoost(WheelSpellBoost_t::SECONDARY_GROUP_COOLDOWN, spellGrade);
}
if (spellSecondaryGroupCooldown > 0) {
Condition* condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_SPELLGROUPCOOLDOWN, spellSecondaryGroupCooldown / rate_cooldown, 0, false, secondaryGroup);
std::shared_ptr<Condition> condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_SPELLGROUPCOOLDOWN, spellSecondaryGroupCooldown / rate_cooldown, 0, false, secondaryGroup);
player->addCondition(condition);
}
}
Expand Down
Loading

0 comments on commit ff4321a

Please sign in to comment.