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

Fix classicAttackSpeed makes server lag (#4676) #4677

Merged

Conversation

gesior
Copy link
Contributor

@gesior gesior commented May 10, 2024

classicAttackSpeed algorithm is bugged and may generate infinite number of scheduler (dispatcher) events.

  1. TFS calls Player::doAttacking every second for every player.
  2. It creates new event g_scheduler.addEvent(task);, which calls Player::doAttacking again with getAttackSpeed() delay. So there is repeating event every 0.2 sec (with attack speed 200) and TFS still calls Player::doAttacking every second creating new event.
    That way server can create infinite number of events for 1 player. 1 new event every second.

Often it all stops on if ((OTSYS_TIME() - lastAttack) >= getAttackSpeed()) { - player cannot attack as fast as these events execute, so if there is more than 1 dispatcher event for given player, it won't re-create it after execution (addEvent is inside that IF).
But if player uses melee weapon and his target is not within 1 sqm, his attack will never execute and lastAttack will never update, so it will be able to re-create events, even if there is more than 1 running in same time.

Fixed code limits number of scheduled doAttacking events to 1 per player.

@@ -3394,7 +3394,8 @@ void Player::doAttacking(uint32_t)
if (!classicSpeed) {
setNextActionTask(task, false);
} else {
g_scheduler.addEvent(task);
g_scheduler.stopEvent(classicAttackEvent);
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't you have to check if the classicAttackEvent is even valid before trying to stop it?

Copy link
Contributor Author

@gesior gesior May 10, 2024

Choose a reason for hiding this comment

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

Don't you have to check if the classicAttackEvent is even valid before trying to stop it?

No. If it's not set yet by script, it's set to 0 in player.h and stopEvent code checks, if it's not 0:

void Scheduler::stopEvent(uint32_t eventId)
{
	if (eventId == 0) {
		return;
	}
...

Even, if it's set to some invalid number, stopEvent code first checks, if event with given ID exists, so it's 100% safe.

@EPuncker EPuncker added enhancement Increase or improvement in quality, value, or extent bugfix labels May 10, 2024
@EPuncker EPuncker linked an issue May 10, 2024 that may be closed by this pull request
@EPuncker EPuncker removed the enhancement Increase or improvement in quality, value, or extent label May 10, 2024
@EvilHero90 EvilHero90 merged commit aefd870 into otland:master May 10, 2024
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

classicAttackSpeed makes server go 100% CPU
4 participants