How does Spaceteam work?
For those who don't know, Spaceteam is a very popular and very fun multiplayer game for iOS.
This allows real-time gameplay between multiple devices on an ad hoc Wifi network - how to do that?
Are there published libraries describing how to build protocols on top of ad-hoc networking libraries? Is this iOS specific, or would it be possible to create different apps on different platforms?
Quickly, answer before you hit the asteroid!
source to share
In particular, what aspect are you interested in? There is nothing special about mobile devices or ad hoc Wi-Fi networks (except for ad hoc networks, not all devices can communicate with each other, so some mesh networks can help, but unnecessarily complicate things for the usual case).
I'll answer a broader question first, because it's more interesting. In my experience, there are several basic considerations:
- Server / client or peer-to-peer? By this I mean whether there is a "master" deciding the true state of the world and communicating it to all clients. Avara is the only game I know, it is "peer-to-peer" in that sense (peer commands send commands to all other peers; this has proven to be a lot of bandwidth for modem users in 6 player games). I don't know of games that use more complex network topologies to communicate game state (e.g. only sending data to one client on each LAN).
- What are you doing with the delay? Avara is the only game I know of that is localized locally with "latency tolerance" to get a consistent state of the world, which was terrible if someone was on a modem (disabling compression helped a lot). There are various ways to "compensate for latency" (eg in Half-Life / CS), some of which can also work in peer-to-peer, peer-to-peer games.
- Time synchronization? For client-server games, you at least need to worry about changing the RTT. For peer-to-peer gaming, I think you also want to agree on timing that minimizes the effective maximum latency.
- What if customers disagree with the state of the world? Avara simply lets colleagues decide their own state of the world (and displays "reality fragmentation" if it detects a mismatch, which could be due to dropped packets or too low an "acceptable" wait).
- What if a player leaves? For a P2P game, you may need a consistent game state (for example, if a player was disconnected after sending commands to a subset of other peers). For client-server play, you may need to select a new master.
And now, after watching the Spaceteam trailer:
I have no idea how this works since I have not redesigned the protocol. However, it's pretty easy to do something that works well enough:
- Use some kind of P2P discovery to find players (like Bonjour, there should be a lot of documents and samples in there).
- Connect with peers. I did this with GameKit around iOS 3/4 (I'm not sure if it works over Wi-Fi).
- Choose a master. It can be as easy as the one who hits the "ready" last attempts to become a master. In some cases with edges, you may need a waiver.
- Let the master decide everything. Spaceteam is not latent sensitive; Wi-Fi latency is typically no more than a few milliseconds, and no one really will notice if one device is 100ms slower (as long as the UI is fast enough).
source to share