canary icon indicating copy to clipboard operation
canary copied to clipboard

fix: stats after transcendance effect

Open luanluciano93 opened this issue 9 months ago • 2 comments

close: #2595

luanluciano93 avatar May 02 '24 07:05 luanluciano93

In spell remove this... ( --)

FernandoDiroz1 avatar May 02 '24 21:05 FernandoDiroz1

void Player::triggerTranscendance() {
	if (wheel()->getOnThinkTimer(WheelOnThink_t::AVATAR_FORGE) > OTSYS_TIME()) {
		return;
	}

	auto item = getInventoryItem(CONST_SLOT_LEGS);
	if (item == nullptr) {
		return;
	}

	double_t chance = item->getTranscendenceChance();
	double_t randomChance = uniform_random(0, 10000) / 100.;
	if (getZoneType() != ZONE_PROTECTION && checkLastAggressiveActionWithin(2000) && ((OTSYS_TIME() / 1000) % 2) == 0 && chance > 0 && randomChance < chance) {
		int64_t duration = g_configManager().getNumber(TRANSCENDANCE_AVATAR_DURATION, __FUNCTION__);
		auto outfitCondition = Condition::createCondition(CONDITIONID_COMBAT, CONDITION_OUTFIT, duration, 0)->static_self_cast<ConditionOutfit>();
		Outfit_t outfit;
		outfit.lookType = getVocation()->getAvatarLookType();
		outfitCondition->setOutfit(outfit);
		addCondition(outfitCondition);
		wheel()->setOnThinkTimer(WheelOnThink_t::AVATAR_FORGE, OTSYS_TIME() + duration);
		g_game().addMagicEffect(getPosition(), CONST_ME_AVATAR_APPEAR);

		sendSkills();
		sendStats();
		sendBasicData();

		sendTextMessage(MESSAGE_ATTENTION, "Transcendance was triggered.");

		// Send player data after transcendance timer expire
		const auto &task = createPlayerTask(
			std::max<uint32_t>(SCHEDULER_MINTICKS, duration),
			[playerId = getID()] {
				auto player = g_game().getPlayerByID(playerId);
				if (player) {
					player->sendSkills();
					player->sendStats();
					player->sendBasicData();
				}
			}, "Player::updateStatusAfterTranscendance"
		);
		g_dispatcher().scheduleEvent(task);

		wheel()->sendGiftOfLifeCooldown();
		g_game().reloadCreature(getPlayer());
	}
}

This refactoring of the triggerTranscendance() function significantly improves the modularity and clarity of the player's transformation process. By explicitly scheduling future updates and decoupling them from the immediate logic, the code becomes not only more readable but also more flexible for future expansions or modifications.

Additionally, the revised approach simplifies the complexity of the include file by eliminating unnecessary pointers and variables when the system is on cooldown, which enhances performance. By checking the cooldown status at the beginning and exiting early if it's active, the system avoids creating and managing resources that would not be used. This efficient handling significantly reduces the overhead during cooldown periods, leading to better overall performance.

Please test to ensure everything is functioning as expected. Note that my review was solely based on the code, so there may be aspects that require further verification.

dudantas avatar May 08 '24 17:05 dudantas

updated, thanks @dudantas

luanluciano93 avatar May 09 '24 02:05 luanluciano93