Core Game Loop
The worldserver executable has three primary phases: initialization, game loop, shutdown and then additionally a crash handler procedure. The execution flow of the primary phases are relatively straightforward, while the crash handler is a special procedure that is only called when something goes wrong.
Initialization
The initialization is handled through primarily two functions, main
and World::SetInitialWorldSettings
, which themselves call tons of other smaller functions for initializing specific subsystems.
- int main: Starts up threads, freeze detector and other handles
- World::SetInitialWorldSettings: Loads DBC data, static database data, scripts and initializes gameplay-related systems.
- World::LoadConfigSettings: Loads configuration files (worldserver.conf) into the Configuration Manager.
- World::SetInitialWorldSettings: Loads DBC data, static database data, scripts and initializes gameplay-related systems.
Game Loop
The main game loop is the phase that handles what happens to the world as players enter it, reacting to network packets, moving entities around, executing combat and so on.
The game loop is itself divided into three primary phases:
- Early Updates: Various global things like cleanup, who lists etc.
- Map Updates: Updates individual maps (Kalimdor, Eastern Kingdoms, Outland, Instances, Battlegrounds etc)
- Primary Map Updates: Multithreaded, where almost all game logic happens
- Delayed Map Updates: Single-threaded, handles minor things like moving transports and cross-map spells
- Late Updates: Handles various things like groups, guilds and channels.
Callstack outline
This is an outline of some of the most important functions involved in the game loop. The call stack is not exact, but is mostly meant to allow easily visualize roughly in what orders things happen in the server.
- main - Main function of the program
- WorldUpdateLoop - Self-sustaining loop that handles update time differences and ensures updates don’t fire too frequently.
- World::Update - Root function for a updating a single tick of the game world. Aside from map updates and occasional use of worker threads, this function is single-threaded.
- Early Updates:
- who list
- calendar events
- auctions,
- MapManager::Update: Updates all maps loaded into the game
- Normal Phase: (optionally) multithreaded map updates
- Map::Update: Primary function used to update a single map, may run in isolated threads
- Unit::Update: Generic update function for all units
- Player::Update: Update function specific to players
- Creature::Update: Update function specific to non-player units
- Map::MoveAllCreaturesInMoveList: Handles creature movement
- Map::MoveAllGameObjectsInMoveList: Handles transport (boats, zeppelins) movement
- Map::SendObjectUpdates: Broadcasts ‘UpdateData’ packets. One of the slowest functions in the map update.
- Map::ProcessRelocationNotifies: Main function for handling visibility updates
- Map::Update: Primary function used to update a single map, may run in isolated threads
- Delayed Phase: Map update logic that needs to run on the main thread (not multithreaded)
- Map::DelayedUpdate: Updates a single map on the main thread
- Handles cross-map spells
- Moves transports between maps
- Removes objects
- Map::DelayedUpdate: Updates a single map on the main thread
- Normal Phase: (optionally) multithreaded map updates
- Late Updates
- BattlegroundMgr::Update: Updates all battlegrounds
- Battleground::Update: Updates a single battleground
- OutdoorPvPMgr::Update: Updates logic for all outdoor pvp (tower captures etc)
- OutdoorPvP::Update: Updates a single OutdoorPvP script
- BattlefieldMgr::Update: Updates battlefields like wintergrasp
- Battlefield::Update: Updates a single battlefield
- GroupMgr::Update: Updates all partys and raids
- LfgMgr::Update: Updates the LFG queue
- GameEventMgr::Update: Updates game and holiday events like the fishing derby or brewfest.
- World::ProcessCliCommands: Handle input to the worldserver console window
- BattlegroundMgr::Update: Updates all battlegrounds
- Early Updates:
- World::Update - Root function for a updating a single tick of the game world. Aside from map updates and occasional use of worker threads, this function is single-threaded.
- WorldUpdateLoop - Self-sustaining loop that handles update time differences and ensures updates don’t fire too frequently.
Multithreading
The primary use of multithreading that is relevant to most developers is the worker threads handling map updates, but these are not the only instances of threading in the core.
Threads are also used to handle various other things in parallel to the primary game loop such as:
- Network connections / Packet handlers
- Database queries and utility
- Freeze detection
- Crash handling
Developers usually do not need to worry about these the way they need to worry about threaded map updates unless they stumble into them, primarily relating to database connections.