Skip to content

Commit

Permalink
fix condition constant and small things
Browse files Browse the repository at this point in the history
  • Loading branch information
MillhioreBT committed Apr 28, 2024
1 parent 9bc2bc8 commit 4670bbe
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 27 deletions.
65 changes: 56 additions & 9 deletions src/condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ bool Condition::unserializeProp(ConditionAttr_t attr, PropStream& propStream)
return true;
}

case CONDITIONATTR_ENDTIME: {
return propStream.read<int64_t>(endTime);
}

case CONDITIONATTR_END:
return true;

Expand Down Expand Up @@ -158,13 +162,26 @@ void Condition::serialize(PropWriteStream& propWriteStream)
propWriteStream.write<uint8_t>(aggressive);

propWriteStream.write<uint8_t>(CONDITIONATTR_CONSTANT);
propWriteStream.write<uint8_t>(constant ? 1 : 0);
propWriteStream.write<uint8_t>(constant);

if (constant) {
propWriteStream.write<uint8_t>(CONDITIONATTR_ENDTIME);
propWriteStream.write<int64_t>(endTime);
}
}

void Condition::setEndTime(int64_t newEndTime)
{
if (endTime == 0) {
endTime = newEndTime;
}
}

void Condition::setTicks(int32_t newTicks)
{
ticks = newTicks;
endTime = ticks + OTSYS_TIME();

setEndTime(ticks + OTSYS_TIME());
}

bool Condition::executeCondition(Creature*, int32_t interval)
Expand Down Expand Up @@ -235,7 +252,7 @@ Condition* Condition::createCondition(ConditionId_t id, ConditionType_t type, in
}
}

Condition* Condition::createCondition(PropStream& propStream)
Condition_ptr Condition::createCondition(PropStream& propStream)
{
uint8_t attr;
if (!propStream.read<uint8_t>(attr) || attr != CONDITIONATTR_TYPE) {
Expand Down Expand Up @@ -292,14 +309,44 @@ Condition* Condition::createCondition(PropStream& propStream)
return nullptr;
}

return createCondition(static_cast<ConditionId_t>(id), static_cast<ConditionType_t>(type), ticks, 0, buff != 0,
subId, aggressive);
if (!propStream.read<uint8_t>(attr) || attr != CONDITIONATTR_CONSTANT) {
return nullptr;
}

uint8_t constant;
if (!propStream.read<uint8_t>(constant)) {
return nullptr;
}

auto condition = Condition_ptr(createCondition(static_cast<ConditionId_t>(id), static_cast<ConditionType_t>(type),
ticks, 0, buff != 0, subId, aggressive));

if (!condition) {
return nullptr;
}

condition->setConstant(constant != 0);

if (condition->isConstant()) {
if (!propStream.read<uint8_t>(attr) || attr != CONDITIONATTR_ENDTIME) {
return nullptr;
}

int64_t endTime;
if (!propStream.read<int64_t>(endTime)) {
return nullptr;
}

condition->setEndTime(endTime);
}

return condition;
}

bool Condition::startCondition(Creature*)
{
if (ticks > 0) {
endTime = ticks + OTSYS_TIME();
setEndTime(ticks + OTSYS_TIME());
}
return true;
}
Expand Down Expand Up @@ -489,7 +536,7 @@ void ConditionAttributes::updatePercentStats(Player* player)
}
}

void ConditionAttributes::updateStats(Player* player)
void ConditionAttributes::updateStats(Player* player) const
{
bool needUpdateStats = false;

Expand Down Expand Up @@ -518,7 +565,7 @@ void ConditionAttributes::updatePercentSkills(Player* player)
}
}

void ConditionAttributes::updateSkills(Player* player)
void ConditionAttributes::updateSkills(Player* player) const
{
bool needUpdateSkills = false;

Expand All @@ -541,7 +588,7 @@ void ConditionAttributes::updateSkills(Player* player)
}
}

void ConditionAttributes::updateExperienceRate(Player* player)
void ConditionAttributes::updateExperienceRate(Player* player) const
{
for (uint8_t i = static_cast<size_t>(ExperienceRateType::BASE);
i <= static_cast<size_t>(ExperienceRateType::STAMINA); ++i) {
Expand Down
13 changes: 9 additions & 4 deletions src/condition.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ enum ConditionAttr_t
CONDITIONATTR_HEALTHGAINPERCENT,
CONDITIONATTR_MANAGAINPERCENT,
CONDITIONATTR_CONSTANT,
CONDITIONATTR_ENDTIME, // only use if the condition is constant

// reserved for serialization
CONDITIONATTR_END = 254,
Expand All @@ -59,6 +60,9 @@ struct IntervalInfo
int32_t interval;
};

class Condition;
using Condition_ptr = std::unique_ptr<Condition>;

class Condition
{
public:
Expand Down Expand Up @@ -87,13 +91,14 @@ class Condition

ConditionType_t getType() const { return conditionType; }
int64_t getEndTime() const { return endTime; }
void setEndTime(int64_t newEndTime);
int32_t getTicks() const { return ticks; }
void setTicks(int32_t newTicks);
bool isAggressive() const { return aggressive; }

static 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);
static Condition* createCondition(PropStream& propStream);
static Condition_ptr createCondition(PropStream& propStream);

virtual bool setParam(ConditionParam_t param, int32_t value);
virtual int32_t getParam(ConditionParam_t param) const;
Expand Down Expand Up @@ -177,10 +182,10 @@ class ConditionAttributes final : public ConditionGeneric
bool disableDefense = false;

void updatePercentStats(Player* player);
void updateStats(Player* player);
void updateStats(Player* player) const;
void updatePercentSkills(Player* player);
void updateSkills(Player* player);
void updateExperienceRate(Player* player);
void updateSkills(Player* player) const;
void updateExperienceRate(Player* player) const;
};

class ConditionRegeneration final : public ConditionGeneric
Expand Down
6 changes: 2 additions & 4 deletions src/iologindata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,10 @@ bool IOLoginData::loadPlayer(Player* player, DBResult_ptr result)
PropStream propStream;
propStream.init(conditions.data(), conditions.size());

Condition* condition = Condition::createCondition(propStream);
Condition_ptr condition = Condition::createCondition(propStream);
while (condition) {
if (condition->unserialize(propStream)) {
player->storedConditionList.push_front(condition);
} else {
delete condition;
player->storedConditionList.push_front(condition.release());
}
condition = Condition::createCondition(propStream);
}
Expand Down
2 changes: 1 addition & 1 deletion src/luacondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ int luaConditionCreate(lua_State* L)
ConditionType_t conditionType = getInteger<ConditionType_t>(L, 2);
ConditionId_t conditionId = getInteger<ConditionId_t>(L, 3, CONDITIONID_COMBAT);

Condition* condition = Condition::createCondition(conditionId, conditionType, 0, 0);
auto condition = Condition::createCondition(conditionId, conditionType, 0, 0);
if (condition) {
pushUserdata<Condition>(L, condition);
setMetatable(L, -1, "Condition");
Expand Down
17 changes: 9 additions & 8 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ extern Events* g_events;
MuteCountMap Player::muteCountMap;

uint32_t Player::playerAutoID = 0x10000000;
std::forward_list<Condition*> Player::storedConditionList;

Player::Player(ProtocolGame_ptr p) : Creature(), lastPing(OTSYS_TIME()), lastPong(lastPing), client(std::move(p))
{
Expand Down Expand Up @@ -979,7 +980,7 @@ void Player::onCreatureAppear(Creature* creature, bool isLogin)
}
}

for (Condition* condition : storedConditionList) {
for (auto condition : storedConditionList) {
addCondition(condition);
}
storedConditionList.clear();
Expand Down Expand Up @@ -1200,7 +1201,7 @@ void Player::onCreatureMove(Creature* creature, const Tile* newTile, const Posit
if (teleport || oldPos.z != newPos.z) {
const int64_t ticks = g_config[ConfigKeysInteger::STAIRHOP_DELAY];
if (ticks > 0) {
if (Condition* condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_PACIFIED, ticks, 0)) {
if (auto condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_PACIFIED, ticks, 0)) {
addCondition(condition);
}
}
Expand Down Expand Up @@ -1421,7 +1422,7 @@ void Player::removeMessageBuffer()

uint32_t muteTime = 5 * muteCount * muteCount;
muteCountMap[guid] = muteCount + 1;
Condition* condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_MUTED, muteTime * 1000, 0);
auto condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_MUTED, muteTime * 1000, 0);
addCondition(condition);

sendTextMessage(MESSAGE_STATUS_SMALL, fmt::format("You are muted for {:d} seconds.", muteTime));
Expand Down Expand Up @@ -2052,13 +2053,13 @@ Item* Player::getCorpse(Creature* lastHitCreature, Creature* mostDamageCreature)

void Player::addCombatExhaust(uint32_t ticks)
{
Condition* condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_EXHAUST_COMBAT, ticks, 0);
auto condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_EXHAUST_COMBAT, ticks, 0);
addCondition(condition);
}

void Player::addHealExhaust(uint32_t ticks)
{
Condition* condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_EXHAUST_HEAL, ticks, 0);
auto condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_EXHAUST_HEAL, ticks, 0);
addCondition(condition);
}

Expand All @@ -2072,7 +2073,7 @@ void Player::addInFightTicks(bool pzlock /*= false*/)
pzLocked = true;
}

Condition* condition =
auto condition =
Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_INFIGHT, g_config[ConfigKeysInteger::PZ_LOCKED], 0);
addCondition(condition);
}
Expand Down Expand Up @@ -3482,8 +3483,8 @@ bool Player::onKilledCreature(Creature* target, bool lastHit /* = true*/)

if (lastHit && hasCondition(CONDITION_INFIGHT)) {
pzLocked = true;
Condition* condition = Condition::createCondition(
CONDITIONID_DEFAULT, CONDITION_INFIGHT, g_config[ConfigKeysInteger::WHITE_SKULL_TIME] * 1000, 0);
auto condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_INFIGHT,
g_config[ConfigKeysInteger::WHITE_SKULL_TIME] * 1000, 0);
addCondition(condition);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,8 @@ class Player final : public Creature, public Cylinder
std::forward_list<Party*> invitePartyList;
std::forward_list<uint32_t> modalWindows;
std::forward_list<std::string> learnedInstantSpellList;
std::forward_list<Condition*>

static std::forward_list<Condition*>
storedConditionList; // TODO: This variable is only temporarily used when logging in, get rid of it somehow

std::string name;
Expand Down

0 comments on commit 4670bbe

Please sign in to comment.