Server Details
The server is a single thread, which initializes the (empty) game universe, creates a socket for listening on port 9999, then drops into the main loop. The mail loop performs the following actions:
- Accept connections on the listening socket.
- Writes any buffered data to sockets that are ready to write.
- Reads any data on sockets that are ready to read, and sets status bits in the corresponding player structures.
- Checks the time that has elapsed since the last game logic update, and performs updates on a 5 millisecond basis. So if 15 milliseconds has elapsed, then the game logic will be updated three times.
- Checks the time that has elapsed since the last network update, and transmits update data to players on 20 millisecond intervals.
Socket Accept Details
The maximum number of players allowed is 64, so an array of connections is used to keep information on each player. The information that is kept is as follows:
- player mode
- position (X,Y) within the universe
- orientation of the ship
- velocity of the ship
- user command states: turn right, turn left, accelerate, decelerate, fire weapon 1, fire weapon 2
- ship health
- weapon status for each active weapon
- ship type
When a new connection is formed, an empty slot is found in the player array, and initialized to default values. The player will be put into "lobby" mode, and a list of available ships and their statistics will be transmitted to the client.
Socket Write Details
Each player has two buffers, a send buffer, and a receive buffer. Any updates that are to be sent to the player will be stored in the buffer, and the socket-write code will transmit the data.
Socket Read Details
When data is available to read from a socket, it will be appended to the read buffer. Immediately after reading, the server will try to interpret the data in the buffer. If there is not enough data to complete a command, the data will remain in the buffer until enough has been collected to execute the command.
When the player is in "lobby" mode, they can begin playing by transmitting a "ship selection" command. After the data is validated, the player will be given a position that will be randomly generated in such a way that they are as far as possible from other players. The player structure will be updated to reflect the position and ship statistics, and the player will be put into "playing" mode. An update is sent to all other players with the player id and nickname.
When the player is in "playing" mode, they can signify an action with an action packet, which contains a bitmask of the actions the player is requesting. These are merely stored in the player structure until the game logic can use them.
Game Logic Update Details
When the game logic is updated, each player is moved using the orientation and velocity of the ship. The orientation is updated using the action flags and the ship maximum turn rate. The velocity is updated using the ship acceleration and the action flags.
During the update, any collisions are resolved, including those between weapons and ships. Damage is assessed and applied. If any sound effects need to be played, a packet is transmitted to each player within range of the sound.
Player Update Details
If there are more than 1000 bytes of data still in the buffer when the update starts, the update will be skipped, in an effort to high latency from overfilling the buffer.
When it is time to send updates to a player, the update is built by finding all of the other players within range of the player. The first thing sent is a sequence number which, which allows the client to determine when network congestion is occurring. The player universe-position and orientation is transmitted, followed by the relative positions of the other players, their orientations, ship types, and id numbers.