Skip to content

Fix random bots stuck in group on player logout; add leader transfer …#2130

Draft
zhangbo8418 wants to merge 19 commits intomod-playerbots:test-stagingfrom
zhangbo8418:Random-bots-to-fix-team-jams
Draft

Fix random bots stuck in group on player logout; add leader transfer …#2130
zhangbo8418 wants to merge 19 commits intomod-playerbots:test-stagingfrom
zhangbo8418:Random-bots-to-fix-team-jams

Conversation

@zhangbo8418
Copy link
Copy Markdown

@zhangbo8418 zhangbo8418 commented Feb 10, 2026

…and delayed leave

Summary

  • Prevents random bots from staying in a group when their master logs out, which made them uninvitable on re-login.
  • When a real player leaves a party that still has other real players, the party is kept: leader is transferred to the next real player and bots get a new master. The party only breaks when the last real player leaves.
  • When no real player remains in the party, random bots leave after a configurable delay (default 180s) so brief disconnects don’t disband the group.
  • After a server crash/restart, groups are restored from DB; if a random bot logs in and its group has no connected real player, that group is scheduled for delayed leave so bots don’t stay stuck.

Changes

  • OnPlayerLogout: For each random bot that had the logging-out player as master, either assign a new master (first connected real player in the group) and transfer party leader to that player, or schedule delayed leave and clear master. If the core did not remove the player on logout (LeaveGroupOnLogout=0), transfer leader when possible, then remove the player from the group.
  • Delayed leave: New config AiPlayerbot.BotLeaveGroupDelayWhenNoRealPlayer (default 180 seconds). Groups with no real player are scheduled; each tick ProcessScheduledGroupLeaves re-checks for a connected real player and, if still none, only random bots in that group are forced to leave (alt/addclass bots are not kicked). Delay 0 means leave on the next tick.
  • OnBotLoginInternal: When a random bot logs in and is in a group with no connected real player, schedule that group for delayed leave (avoids stuck/uninvitable bots after crash).
  • Scope: Only 5-man parties are handled; raid, LFG, and battleground are skipped. Logic applies only to random bots managed by RandomPlayerbotMgr.

Config

  • AiPlayerbot.BotLeaveGroupDelayWhenNoRealPlayer (default: 180) — seconds before random bots leave the group when no real player remains.

Notes

  • If LeaveGroupOnLogout is enabled in the core, the core removes the player and broadcasts first; bots may leave immediately via PartyCommandAction(PARTY_OP_LEAVE), so the delay does not apply in that case. When LeaveGroupOnLogout is disabled, we transfer leader and remove the player ourselves, and the delay applies.
  • Defensive checks added: FindFirstRealConnectedPlayerInGroup validates GetSession() and skips logout/invalid members to avoid crashes.

Pull Request

Describe what this change does and why it is needed...


Design Philosophy

We prioritize stability, performance, and predictability over behavioral realism.
Complex player-mimicking logic is intentionally limited due to its negative impact on scalability, maintainability, and
long-term robustness.

Excessive processing overhead can lead to server hiccups, increased CPU usage, and degraded performance for all
participants. Because every action and
decision tree is executed per bot and per trigger, even small increases in logic complexity can scale poorly and
negatively affect both players and
world (random) bots. Bots are not expected to behave perfectly, and perfect simulation of human decision-making is not a
project goal. Increased behavioral
realism often introduces disproportionate cost, reduced predictability, and significantly higher maintenance overhead.

Every additional branch of logic increases long-term responsibility. All decision paths must be tested, validated, and
maintained continuously as the system evolves.
If advanced or AI-intensive behavior is introduced, the default configuration must remain the lightweight decision
model
. More complex behavior should only be
available as an explicit opt-in option, clearly documented as having a measurable performance cost.

Principles:

  • Stability before intelligence
    A stable system is always preferred over a smarter one.

  • Performance is a shared resource
    Any increase in bot cost affects all players and all bots.

  • Simple logic scales better than smart logic
    Predictable behavior under load is more valuable than perfect decisions.

  • Complexity must justify itself
    If a feature cannot clearly explain its cost, it should not exist.

  • Defaults must be cheap
    Expensive behavior must always be optional and clearly communicated.

  • Bots should look reasonable, not perfect
    The goal is believable behavior, not human simulation.

Before submitting, confirm that this change aligns with those principles.


Feature Evaluation

Please answer the following:

  • Describe the minimum logic required to achieve the intended behavior?
  • Describe the cheapest implementation that produces an acceptable result?
  • Describe the runtime cost when this logic executes across many bots?

How to Test the Changes

  • Step-by-step instructions to test the change
  • Any required setup (e.g. multiple players, bots, specific configuration)
  • Expected behavior and how to verify it

Complexity & Impact

Does this change add new decision branches?

    • No
    • Yes (explain below)

Does this change increase per-bot or per-tick processing?

    • No
    • Yes (describe and justify impact)

Could this logic scale poorly under load?

    • No
    • Yes (explain why)

Defaults & Configuration

Does this change modify default bot behavior?

    • No
    • Yes (explain why)

If this introduces more advanced or AI-heavy logic:

    • Lightweight mode remains the default
    • More complex behavior is optional and thereby configurable

AI Assistance

Was AI assistance (e.g. ChatGPT or similar tools) used while working on this change?

    • No
    • Yes (explain below)

If yes, please specify:

  • AI tool or model used (e.g. ChatGPT, GPT-4, Claude, etc.)
  • Purpose of usage (e.g. brainstorming, refactoring, documentation, code generation)
  • Which parts of the change were influenced or generated
  • Whether the result was manually reviewed and adapted

AI assistance is allowed, but all submitted code must be fully understood, reviewed, and owned by the contributor.
Any AI-influenced changes must be verified against existing CORE and PB logic. We expect contributors to be honest
about what they do and do not understand.


Final Checklist

    • Stability is not compromised
    • Performance impact is understood, tested, and acceptable
    • Added logic complexity is justified and explained
    • Documentation updated if needed

Notes for Reviewers

Anything that significantly improves realism at the cost of stability or performance should be carefully discussed
before merging.

…and delayed leave

## Summary
- Prevents random bots from staying in a group when their master logs out, which made them uninvitable on re-login.
- When a real player leaves a party that still has other real players, the party is kept: leader is transferred to the next real player and bots get a new master. The party only breaks when the last real player leaves.
- When no real player remains in the party, random bots leave after a configurable delay (default 180s) so brief disconnects don’t disband the group.
- After a server crash/restart, groups are restored from DB; if a random bot logs in and its group has no connected real player, that group is scheduled for delayed leave so bots don’t stay stuck.

## Changes
- **OnPlayerLogout**: For each random bot that had the logging-out player as master, either assign a new master (first connected real player in the group) and transfer party leader to that player, or schedule delayed leave and clear master. If the core did not remove the player on logout (`LeaveGroupOnLogout=0`), transfer leader when possible, then remove the player from the group.
- **Delayed leave**: New config `AiPlayerbot.BotLeaveGroupDelayWhenNoRealPlayer` (default 180 seconds). Groups with no real player are scheduled; each tick `ProcessScheduledGroupLeaves` re-checks for a connected real player and, if still none, only **random bots** in that group are forced to leave (alt/addclass bots are not kicked). Delay 0 means leave on the next tick.
- **OnBotLoginInternal**: When a random bot logs in and is in a group with no connected real player, schedule that group for delayed leave (avoids stuck/uninvitable bots after crash).
- **Scope**: Only 5-man parties are handled; raid, LFG, and battleground are skipped. Logic applies only to random bots managed by RandomPlayerbotMgr.

## Config
- `AiPlayerbot.BotLeaveGroupDelayWhenNoRealPlayer` (default: 180) — seconds before random bots leave the group when no real player remains.

## Notes
- If `LeaveGroupOnLogout` is enabled in the core, the core removes the player and broadcasts first; bots may leave immediately via `PartyCommandAction(PARTY_OP_LEAVE)`, so the delay does not apply in that case. When `LeaveGroupOnLogout` is disabled, we transfer leader and remove the player ourselves, and the delay applies.
- Defensive checks added: `FindFirstRealConnectedPlayerInGroup` validates `GetSession()` and skips logout/invalid members to avoid crashes.
@NoxMax
Copy link
Copy Markdown
Contributor

NoxMax commented Feb 10, 2026

Please make sure your PR targets the test-staging branch, not master.

@Celandriel Celandriel changed the base branch from master to test-staging February 10, 2026 16:26
@Celandriel
Copy link
Copy Markdown
Collaborator

Can that be selected when submitting PRs?

@NoxMax
Copy link
Copy Markdown
Contributor

NoxMax commented Feb 10, 2026

Yeah of course, it just defaults to the default branch.

Untitled

@NoxMax
Copy link
Copy Markdown
Contributor

NoxMax commented Feb 10, 2026

Okay, about the PR itself. The core issue at hand "Fix random bots stuck in group on player logout"

How does that happen to you right now? Unless I am misunderstanding the intent here, RND bot groups already disband when human master leaves, no? Things are handled in LeaveGroupAction

This is screenshot is for my bot group, taken at the exact moment I clicked logout
Untitled

@Celandriel
Copy link
Copy Markdown
Collaborator

Celandriel commented Feb 10, 2026

So on logout they wont leave and just wait around forever for you to come back.

Edit: doing a quick read through, im not sure that I agree fully with the scheduling leaving group this way. Need to think about it more.

@zhangbo8418
Copy link
Copy Markdown
Author

So on logout they wont leave and just wait around forever for you to come back.

Edit: doing a quick read through, im not sure that I agree fully with the scheduling leaving group this way. Need to think about it more.

If you don't need to wait, you can set AiPlayerbot.BotLeaveGroupDelayWhenNoRealPlayer to 0.

@hermensbas
Copy link
Copy Markdown
Collaborator

hermensbas commented Feb 10, 2026

No offense but try to fill in the PR form, esp when using AI in this extend.

@yqsy2025
Copy link
Copy Markdown

The code has multiple thread safety issues and potential crash hazards.

// When CONFIG_LEAVE_GROUP_ON_LOGOUT is true, the core removes the player from the group before this hook
// and sends group updates; bots may then leave immediately in LeaveGroupAction::PartyCommandAction (PARTY_OP_LEAVE).
// When it is false, we transfer leader / remove player ourselves below and set bot master to nullptr before
// RemoveMember, so bots do not leave in PartyCommandAction and the scheduled delay applies.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This code taking into consideration AiPlayerbot.KeepAltsInGroup config?

@kadeshar kadeshar marked this pull request as draft March 11, 2026 16:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants