|
| 1 | +--- |
| 2 | +id: multiplayer-overview |
| 3 | +title: Multiplayer Overview |
| 4 | +--- |
| 5 | + |
| 6 | +Some notes on the current state of multiplayer development, as well as some quick what-you-need-to-know to get started with developing for multiplayer |
| 7 | + |
| 8 | +## What can I do in multiplayer? |
| 9 | +- Jump seamlessly between single and multiplayer |
| 10 | +- Switch my avatar, either from an avatar in the world or from the menus |
| 11 | +- Speak with my voice |
| 12 | +- Switch my voice pack and voice engine |
| 13 | +- Have my character's mouth respond to my voice with mouth movement |
| 14 | +- Change my name, and see other user's names as a nameplate over their head |
| 15 | +- Chat, and have my character speak whatever I chat |
| 16 | +- Drop items into the world and use them |
| 17 | +- Drop pets and vehicles into the world and activate them |
| 18 | +- Pick up weapons and damage mobs |
| 19 | +- Die and respawn at current world spawn point |
| 20 | + |
| 21 | +## Important Files |
| 22 | +The important, high-touch documents for multiplayer are: |
| 23 | +- avatars/avatars.js |
| 24 | +- app-manager.js |
| 25 | +- character-controller.js |
| 26 | +- chat-manager.js |
| 27 | +- players-manager.js |
| 28 | +- webaverse.js |
| 29 | +- universe.js |
| 30 | +- world.js |
| 31 | + |
| 32 | +## Where do I start? |
| 33 | +The main entrypoint to the Webaverse app is the Webaverse class in webaverse.js -- this is where everything gets set up and the main loop starts. The world, local and remote players are updated from here. |
| 34 | + |
| 35 | +Multiplayer starts from enterWorld in **universe.js** |
| 36 | + |
| 37 | +## Key Concepts |
| 38 | + |
| 39 | +#### CRDTs |
| 40 | +Webaverse is a peer-authoratative system. Objects in the world can be moved and manipulated by peers, and these changes will be reflected by the server out to other peers. It is simple in theory, but in practice this can potentially lead to conflicts where peers don't agree on where something is, or *what* it is. |
| 41 | + |
| 42 | +**Conflict-free replicated data types** or CRDTs are a set of data structures which can be merged in such a way that any inconsistencies are eventually resolved, and all peers will have the same document state. Additional reading can be found here: |
| 43 | +[Conflict-free_replicated_data_type](https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type) |
| 44 | + |
| 45 | +#### Websockets, WebRTC and WSRTC |
| 46 | +Previously, Webaverse used a server-authoritative WebRTC implementation for voice and data. However, this has significant scalability issues and was generally not fun to work with. The current implementation uses pure WebSockets. In the future we will adopt WebTransport and turn on AV1 and WebCodecs once they are well-supported in Safari and Firefox. |
| 47 | + |
| 48 | +## Multiplayer-Related Packages |
| 49 | + |
| 50 | +#### zjs |
| 51 | +zjs is a purpose-built API-compatible clone of yjs for multiplayer realtime games. Most of the concepts and documentation for yjs applies, so please read before attempting to work on the multiplayer. |
| 52 | + |
| 53 | +The TL;DR is that zjs is a CRDT-backed document store. When changes to the Doc object occur, zjs will handle any conflicts and then send change events with the final state at that moment to all connected clients. |
| 54 | + |
| 55 | +Any object in Webaverse which has a bound state -- players, app managers and tracked apps -- will be subscribed to these change events. This is done using the "observer" pattern, and zjs maps and arrays are observable objects. |
| 56 | + |
| 57 | +#### wsrtc |
| 58 | +Actual socket communication is handled by wsrtc, another webaverse project, which moves app data and voice packets over websockets. wsrtc is heavily bound to zjs. Messages received from peers are passed into the server's own zjs instance, where conflicts are handled and final state is passed back out to peers. |
| 59 | + |
| 60 | +An important note about wsrtc is that this will eventually be replaced with redis or a similar distribute key-value store and possibly phased out entirely. |
| 61 | + |
| 62 | +#### totum / metaversefile |
| 63 | +Totum (formerly metaversefile, and still called this everywhere) is an API for Webaverse to load composable apps and for said apps to communicate with the Webaverse core. The metaversefileApi contains useful helper functions for initializing and interacting with metaverse apps, or for getting important core data into those apps. A good example would be accessing the local player's app manager, or checking if the scene is loaded. |
| 64 | + |
| 65 | +## Development |
| 66 | +#### Player Controllers |
| 67 | +The character-controller.js file holds the LocalPlayer, RemotePlayer and the Player base class. Everything related to character state, including transform, movement, actions, wearing, etc is here. NPCs are also a type of player |
| 68 | + |
| 69 | +The actual visual avatar display is largely handled by the Avatars class in the avatars/avatars.js file. The character controller sets the velocity of it's avatar class instance, which then handles the animation update. |
| 70 | + |
| 71 | +Player objects are held in the PlayersManager class in players-manager.js, this is initialized when the client connects to a room. The PlayersManager object is a zjs map of player objects, which are also zjs maps, containing component information that might change. Players sync most things via "actions", but also have information like name, instance ID and their chosen voice engine and voice pack. |
| 72 | + |
| 73 | +Player objects have a playerId, and are stored in the playersArray of the PlayersManager class instance |
| 74 | + |
| 75 | +The instanceId of the avatar can be found inside the player map's avatar key, or through calling CharacterController.getAvatarState() |
| 76 | + |
| 77 | +#### Apps |
| 78 | +The AppManager class (app-manager.js) handles setting up, tearing down and updating apps. Apps can be avatars, wearables, buildings, anything that lives in the world with the player. All AppManagers are bound to state, which can be local or networked. world.connectState sets up all of the AppManager bindings locally, while world.connectRoom sets up the bindings globally. |
| 79 | + |
| 80 | +When an app is loaded, it is bound to a map or array in zjs. Each app has an instanceId, which is a 5-digit alphanumeric hash. The instanceId of an object on one client should match the next. |
| 81 | + |
| 82 | +The instanceId is a useful key for getting the app. Each app has a list of components, a contentId and an instanceId (which should be unique). |
0 commit comments