Assignment 4: Game Networking

In this assignment, you will design and implement a framework for implementing client/server multi-player games. This will increase your practical understanding of:

  • the client/server game architecture
  • the use of dead reckoning to reduce animation delay
  • consistency maintainence in networked games
  • the Network Transport API of the Unity game engine

Overview

You are to develop a multi-player game environment allowing two people using two separate computers to walk through an environment.

Sample Software

I have provided you with a stripped down Unity project implementing client/server communication. You can download this code from OnQ. The code consists of the following scripts:

  • ServerNetworkScript: Implements the networking code run on the server. The server awaits connections from the clients. It assigns each client a player Id and an avatar. It relays movement messages from one client to other clients.

  • ClientNetworkScript: Implements the networking code run on the client. The client establishes connection with the server. It passes information about avatar movement to the server. When information about remote avatar movement is received, it updates the remote avatar on this client.

  • LocalAvatarScript: This implements the player's avatar on the client. It accepts movement commands - "W" moves forward; "A" rotates counter-clockwise; "D" rotates clockwise. It updates the avatar's position and rotation and the animation being played.

  • RemoteAvatarScript: This implements updates to the position, rotation and animation of the remote avatar. It implements the dead reckoning and smooth corrections algorithms (see parts 3 and 4 below.)

In addition, there is a utility class shared by the ServerNetworkScript and the ClientNetworkScript:

  • Messages: This class provides utilities for creating network messages (byte arrays) and for extracting the contents of network messages.

To test this project, you will want to be able to run two instances of Unity on a single computer. One way of achieving this is to to build the client. Then run the server in Unity, and run two instances of the client using a command line.

You will be using Unity's Transport Layer API to send messages between server and client. The relevant Unity documentation is:

You may also find this useful for converting between byte arrays and standard data types:

Part 1: Basic Client-Server Movement

Starting from the provided code, create a client-server program allowing up to two players to control avatars in a simple virtual world. Each client should show both players' avatars - i.e., the client should show the player's own avatar and the avatar of the other player. We will call these the local and remote avatars respectively. Players should be able to move their avatar using the commands "W" to move forward, "A" to rotate counter-clockwise, and "D" to rotate counter-clockwise. Avatar movement commands should be sent to the server; the server should broadcast those commands to other clients. Updates should be sent at a frequency determined by the slider in the user interface, ranging by increments of 200 ms from 0 ms to 1,000 ms.

The code should support client connection, disconnection and reconnection.

For part 1, you need only support the "none" algorithm available in the client user interface. I.e., you do not need to support dead reckoning or smooth corrections.

Your code should build on the sample code that I have provided. Read through all of the code carefully to understand where it is necessary to add code. A particular issue will be the design of the format of the message used to transmit avatar movement information. Use the helper functions provided in the Messages class. Ensure that your message contains only the minimum number of bytes required to convey the avatar's position, rotation and movement state.

Part 2: Dead Reckoning

Add dead reckoning to the client for the display of remote avatars. Selecting "Dead Reckoning" in the user interface of the client should enable the algorithm.

Part 3: Smooth Corrections

When positional updates for the remote avatar arrive, if the correction is large, use smooth corrections to fix it. The correction is considered large if the difference in position between the new position and the currently saved position is above some threshold. When correcting, the remote avatar should be set to move at double its usual speed.

Part 4: Questions

Provide brief answers to the following questions.

  1. Document the message flow between the client and server, showing the steps from a client requesting a connection, through to the first position update message being sent by the client and that position update being received by another client. Use a UML sequence diagram. (If you need a refresher, look at the example in lecture 03-3.) Provide approximately one paragraph of text with the diagram to explain its contents.

  2. Explain how you would extend this solution to handle more than two avatars. In what ways does the current solution embed knowledge that exactly two avatars are being used? What specific parts of the code would you need to change?

  3. You have used the simplest form of smooth corrections in this assignment. Describe how this algorithm sometimes leads to unaesthetic character movement. Explain how you might elaborate this system to provide more realistic smooth corrections.

  4. Imagine you were to extend this "game". Objects appear on the ground, and the first player to walk over the object collects it. (After an object is collected, it disappears.) What is the primary implementation challenge with this game feature? Briefly sketch how you would use messages to implement this functionality. What new messages would you introduce? (Specify the names and parameters of the new messages, and explain how they are used.) What two quality attributes do you need to trade off in the implementation of this game feature?

  5. This solution uses two channels: a lossy channel and a lossless channel. Explain the difference between these types of channels (i.e., what do lossy and lossless mean). Explain what kinds of messages the lossless channel is used for. Explain what kinds of messages the lossy channel is used for. Explain why it is useful to have both types of channel.

To Hand In

Using OnQ, provide a handin consisting of two parts: (1) your code for part 1-3, and (2) a report answering the questions from part 4.

You can attach your code as a zip file in your assignment submission in OnQ. Your code should be in a folder called networking-yourLastName. In your code, include comments to show where changes have been made. If your code does not work completely, give a clear explanation of what features work and what features do not work.

Your report should be in PDF format. It should include your name and a title (e.g., Assignment 4 Report). Use 11 pt type with at least 1" margins. Diagrams should be included in the body of the report (not raw datafiles from UMLet.)

Evaluation

This assignment will be graded using the rubric posted on OnQ. It is advisable to review this rubric in advance of submitting your work.

Optional Extensions

You may consider a number of optional extensions. These are not for credit, but would make fun projects for you once the term is done.

  • Extend the code to allow creation of any number of remote avatars.
  • The walking animations have a slippery feel to them. Fix the animations so that they move at a speed closer to movement speed. This will require you to edit the animations themselves in Unity's animaton editor.
  • Currently movement of the remote avatar is smoothed using dead reckoning, but rotation is not. Modify the code so that rotation is also smooth.
© 2019 Nicholas Graham