What to consider as game state when validating a simulated synchronization.

One really important thing when performing a synchronized simulation is having a way to check if all clients have the same simulation state. In order to do that, it must be clear which parts of the game state are important for the synchronized simulation and which parts aren’t. Obviously, this depends a lot on the game.

The idea of this blog post is to talk briefly about what is normally an important value for a game, and what isn't.

What is not important for the game state

Let's talk first about what is not important for your game. Even though it depends on the game, there are some common things that shouldn’t be considered as part of the important game state.

These things are normally all the stuff that doesn’t affect the game outcome, in other words, all the stuff that could be removed from the game and the game will be the same. For example, the smoke particles coming out from a bonfire, or even the current playtime of fire sound effect.

Also, things that could be derived from important gamestate even though they could affect the outcome in some way. For example, the units health bar current fill percentage could be derived from the unit health, that value could be used by the game logic but since it is directly derived from other values, it is not considered important.

What is important for the game state

All the things that could affect directly the game outcome are important for the game state. If you have a way to restore the game from a saved state, you have to be able to finish the game the same way each time you restore it.

For example, in a game like Doom, if you save and load you have to have the same health, armor, weapons and ammo, you should be in the same world position you were and enemies too, with same health, etc.

In Starcraft, what is important is your current vespene gas and your minerals since that decide what you can build or not, your current units with their health, your structures and their queued orders, the revealed map, etc. Things that are not so important include the blood in the ground when your units die (if you are playing Terran of course), your current units selection, the exact frame of a unit animation (unless the game depends on that to fire a skill).

If you are just saving the game state to be restored locally, you may want to store more stuff that you need since that shouldn’t matter. However, in the case of checking state synchronization between machines, having stuff that could differ and doesn’t affect the game outcome is not a good thing to have since you are not only wasting bandwidth but also risking the synchronization check.

Next time

Currently I am working on a way to represent the game state of a game I am working on and I want to have both features, a way to check synchronization between machines as well as having a way to save and restore the game state. I hope to talk about the benefits and problems of those features in the next posts, as well as having a more detailed information on the concepts of this blog post too.

Even though this blog post was a bit basic, I hope you liked it. I just wanted to talk a bit about the challenges I am dealing with right now.

VN:F [1.9.22_1171]
Rating: 5.0/5 (1 vote cast)

Understanding Unity Networking (Unet)

The objective of this blog post is to give you a first look at Unity Networking (Unet) based on my experience with it, and from my point of view.

Introduction

Unet is the Unity client/server architecture solution for networking, which provides the developer a wide range of options from high level logic like automatically synchronizing game objects state to low level like directly sending bytes through a network connection.

To have a quick understanding of how it normally works, here is a quick example. Imagine there are 3 machines connected, one server and two clients. Each machine is running the same game, that means the same code and the same Scene with same GameObjects. The code has a way to know if it is on a server or a client, and if has authority or not, and take action according to that. For example, for non player objects the server is the one with the authority and for player objects only one client has authority. Since it is a client/server architecture actions are processed on the server and then synchronized back to the clients.

note: I wrote normally because it is not always the case, you can have have objects created only on some clients, or on the server, etc.

Unet

The idea here is to give you a quick look of what I explored so far.

The NetworkManager is Unet point of entrance. It is the way to host or join a game. It is responsible for creating the player object for each client, etc. As a Unity behaviour, it allows configuring the player prefab, start positions, different scenes for being offline or online, or even advanced stuff like configuring connection timeouts, etc.

unity-networkmanager

All Unet network objects must have a NetworkIdentity behaviour, this is the way to identify an object is the same in different machines, used for synchronzation for example.

The NetworkBehaviour is the base class for behaviours that want to know about networking. Subclasses of this class can override easy methods to be aware of networking events, automatically synchronize fields, invoke methods on the server or on the clients, and to know current code has authority over the object or not (since normally all objects are created in each machine). Here is a code sample containing some of these concepts:

 public class NetworkExampleClass : NetworkBehaviour
 {
     [SyncVar]
     public int health;
 
     int maxHealth = 100;
 
     public Color myColor;
 
     public GameObject bombPrefab;
 
     public void Start()
     {
         if (isLocalPlayer) {
             myColor = Color.blue;
         } else {
             myColor = Color.red;
         }
     
         if (isServer) {
             Debug.Log ("Object started on server");
         }
     }
 
     // only want to process this Unity callback on clients (avoid server update)
     [ClientCallback]
     public void Update()
     {
         // don't want to process on the client that is not the object owner, this works only
         // for the player object I believe.
         if (!isLocalPlayer)
             return;
 
         if (Input.GetMouseButtonUp (0)) {
             CmdTakePotion(10);
         }
 
         if (Input.GetMouseButtonUp (1)) {
             CmdSpawnBomb(UnityEngine.Random.insideUnitCircle * 100);
         }
     }
 
     // send command to server to perform authoritative logic
     [Command]
     public void CmdTakePotion(int hitpoints)
     {
         health += hitpoints;
         if (health > maxHealth)
             health = maxHealth;
         RpcShowPotionFeedback (hitpoints);
     }
 
     // send command to clients to perform visual logic
     [ClientRpc]
     public void RpcShowPotionFeedback(int hitpoints)
     {
         CreatePotionFeedback ();
     }
 
     public void CreatePotionFeedback(int hitpoints)
     {
         // TODO: create visual feedback for taking a potion
     }
 
     [Command]
     public void CmdSpawnBomb(Vector2 position)
     {
         GameObject bombObject = GameObject.Instantiate (bombPrefab);
         bombObject.transform.position = position;
 
         // this spawns the object on all clients and syncrhonizes them with server object (identity, etc)
         NetworkServer.Spawn (bombObject);
     }
 
 }

(note: this code may not compile, it wasn't tested)

And here is a quick explanation of the example concepts:

  • isServer: this property lets you know if you are on the server or not.
  • hasAuthority: this property lets you know if you have authority over the game object or not.
  • isLocalPlayer: this property tells you if the gameobject represents the player on the local machine, to avoid for example moving another players gameobjects when processing the input.
  • [SyncVar]: this field annotation it is a way to specify you need a field to be synchronized over the same game object running in each machine (the object identified by the same network id). For example, the health was decreased from 10 to 5 in the server, then all clients must show the change in the health bar.
  • [Command]: this method annotation it is used on methods that want to be invoked from a client to run in the server, their names must start with Cmd prefix. For example, if you want to do damage over a unit you send that to the server since the server is the authority here.
  • [ClientRpc]: this method annotation it is used on methods that want to be invoked from a server to run in all clients, their names must start with Rpc prefix. For example, the server detected some unit died (after it received damage), then it invokes a RpcUnitDeath(), so each client show the unit death animation.
  • Other methods annotations like [ClientCallback] and [ServerCallback] are an easier way to not having to check isServer property in the code, it is used for Unity automatic gameobject callbacks like Update or Start methods.
  • NetworkServer.Spawn(): it is the way to synchronize server creation of objects on clients, that means creating a game object on each client with the same network id.

For automatically synchronizing state there are already some behaviours like the NetworkTransform. These behaviours not only provide an automatic way to synchronize a GameObject common state but, in some cases, also a way to configure interpolation strategies. Here is a picture showing the NetworkTransform configuration:

unity-networktransform

Note: in my tests I couldn’t find a way to make the transform interpolation to look smooth. Even looking at other developers examples, they normally create their own interpolation code to fix that.

Here is a video showing a quick example I did using Unet, there are two clients, the left one is also the server. Each player can control one hexagon by clicking where to move it, the movement logic is performed in each client machine and automatically synchronized using a NetworkTransform.

Conclusion

The Unet high level concepts are a way to get, as fast as possible, a multiplayer game working, and depending the kind of game you are making, it may fit perfectly your needs. The low level access may give you a real control of what is happening behind the scenes in case you need to optimize your game networking usage.

As a developer, being flexible to use from high to low level stuff is always an advantage, and that is a really good point of Unet. Also, even though it wasn’t so easy to understand for me in my first tests, I believe the HLAPI is not hard to use and gives a good start point, and, like other Unity stuff, it is really good for quick prototyping.

Another really good point is that it is open source which means I can take a look at implementation details if I want, or understand how something work behind the scenes, or even propose bug fixes.

The bad part is that the documentation is not good, but that happens for all the Unity stuff, when you need to understand something in detail the documentation says nothing or it is even wrong. Also, the first Unet tutorial results wasn’t what I expected, the movement was really choppy and it didn’t work so well (I believe they updated it, I tried the one for Unity 5.3.x), that was something that made me think about Unet not being the solution I wanted for my games, but in the end it was just a really bad example of Unet features.

Also, if you are making an action game, I don't know if Unet provides good support for client side prediction, server reconciliation or lag compensation but it shouldn't be a problem to implement what you need over Unet since it is very flexible.

Right now I am following another aproach of using low level send messages API since for my game I am trying to reduce the bandwidth cost by sending only player actioons and performing a synchornized simulation, something I will try to write about in a future blog post.

References

Some references in no specific order.

Tutorial videos for making a survival cooperative game

Unet overview

Unity forums - Multiplayer networking

Unity forums - Collection of multiplayer samples

Unity networking source code at Bitbucket

A simple implementation of authoritative movement on top of unet

VN:F [1.9.22_1171]
Rating: 5.0/5 (2 votes cast)

Exploring Remote Multiplayer

Some time ago I’ve started to prototype a multiplayer game, it is some kind of super simplified RTS game for mobile devices where macro decisions are encapsulated into one action through a button.

First versions were prototyped in a multiplayer hot seat fashion by using only one device (a tablet or a phone), that allowed me to quickly iterate between different ideas and find fun as soon as possible. That was a successful approach since the idea of the game proved that it was indeed fun to play with friends, so the next step was to go remote multiplayer with two or more devices.

Since networking is a whole new world for me, I started by reading lots of different articles about making multiplayer games.

The first thing to know is that the common solutions around depend on each kind of game, and knowing that beforehand helps you deciding the best solution for your game, and how your game must be adapted to support it.

In my case, I am developing a really simplified version of a RTS game for mobile, similar to Clash Royale. It is a 1v1 game where each player controls multiple units by giving high level orders like “everyone attack!” or “build new supply depot”.

The common networking approach for this kind of games, where synchronizing the whole world is a bit heavy given there are tons of units, is to do a synchronized simulation where only the main actions are transmitted (to save bandwidth) and each machine/device perform the same simulation. This implies the game should be deterministic in order to avoid desynchronization between players, so given one start state of the game and a list of actions over time, the final state should be always the same.

Making a game deterministic is a really hard problem, but it also has its rewards, like being able to replay the game given the actions of each player over time. This is a great debug tool for developers and at the same time a great freature for players since it is used to see other strategies or to share a great victory with your friends, everything with almost no storage cost.

The idea is to start writing my findings in the remote multiplayer journey to share all the lessons learned.

References

These are some of the articles I read:

What every programmer needs to know about game networking

1500 Archers on a 28.8: Network Programming in Age of Empires and Beyond

Fast-Paced Multiplayer

Why adding multiplayer makes game coding twice as much work

Core network structures for games

Making Fast-Paced Multiplayer Networked Games is Hard

How to create an online multiplayer game with Unity

Networking in Unity

Creating a Cross-Platform Multiplayer Game in Unity

Understanding Fighting Game Networking

Desyncs and FPU synchronization

Cross platform RTS synchronization and floating point indeterminism

Minimizing the Pain of Lockstep Multiplayer

The Tech of Planetary Annihilation: ChronoCam

Synchronous RTS Engines and a Tale of Desyncs

Don’t Store That in a Float

 

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)

Quick prototyping and sharing FTW!!

Since the beginning of times (of Gemserk), I had one or more local projects where I could test stuff related to the libraries we were using (Slick2D, LibGDX, Artemis), graphical effects, game ideas or just algorithms. One example is the lighting bolt effect I talked about in a previous post, I have also made some videos of some other tests but never uploaded the code.

As these kind of prototypes could be useful for other developers we have started a new project named prototypes (yeah, super creative name) where we (at least I) are going to add some of the new tests and prototypes we make.

To be easily tested by anyone, we created a Webstart and an Android application with a launcher to select which test or prototype you want to run.

Hope you like it.

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)

A game prototype : Mad Jetpack

The previous weekend I started a game prototype named Mad Jetpack, it is a Jetpac remake with Box2D physics and probably more features.

Jetpac is an arcade game where the main idea was to recover parts of your ship and assemble them to fix your ship while different monsters (from each planet you visit I suppose) attack you.

Game prototype is uploaded and it has already some features implemented. As I don't know if I will continue with it or not I want to share the current state with you.

Objective: pick the small circles(ship parts or fuel) and bring them to the greater circle (your ship), avoid the moving circles (enemies).

Instructions: move the character with WASD, aim with the mouse and shoot with the left button, press 1 to restart the game.

PLAY with your imagination

Hope you enjoy it.

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)

Super Flying Thing - Update 01

A new version of Super Flying Thing was released, here is a screenshot:

Super Flying Thing - Screenshot

And here a list of changes:

  • Improved a bit controls to be less sensible (keeps being a bit hard on Android though).
  • Added six simple levels on Challenge mode, created using Inkscape.
  • Changed the obstacles to be filled now, as you see on the screenshot, also the planets.
  • Added in game basic instructions.
  • Added a pause screen where you can restart level or ask to show instructions again
  • Added a game over screen shown when you die playing Challenge mode, not 100% sure about how it stops the gameplay

Some stuff for debugging purposes:

  • Toggle controls customization window by pressing 0, this window lets you customize ship's movement values like acceleration, etc.
  • Take screenshots by pressing 9 (saved on your tmp folder).
  • Toggle FPS by pressing 8
  • Toggle Box2D debug renderer by pressing 7

Now, if you want to play it, links again:

Play on PC

or play it on Android:

Play on Android

We will be trying some new stuff on the next days like moving obstacles and others.

That's all for now, hope you like the game and enjoy playing it.

VN:F [1.9.22_1171]
Rating: 5.0/5 (1 vote cast)

Super Flying Thing With No Name - New game prototype

We are working on a prototype for a new game with no defined name, graphics and sounds yet. But, game mechanics are, and they are fun, at least for me (arielsan).

In a simple description, the game is about a flying thing (probably a ship) which should travel from one safe point to another through a series of difficult paths.

Super Flying Thing - Screenshot
(note: I wanted to post a video but I had problems recording it)

There are going to be two game objectives, a main objective of reaching the destination alive and a second objective of taking all the stuff (coins, stars, diamonds, box2d polygons, something) on the level.

Game mechanics are not fully decided yet, the idea is to make simple levels and don't allow you to receive a single hit on the ship, else you die. However, one option is to have some kind of shield and if you reach the destination with full shields then you have a Perfect score.

Also, game controls are a bit too sensible for now, but game levels are not defined so we will improve control sensibility when we have some levels.

We have some ideas in mind like having a level editor and let players share levels and stuff, lets see what happens.

Finally, if you want to try the game, there is a Webstart and also an APK to download, it will be on Android market when we have a name decided, or maybe with Super Flying thing name.

Play on PC

Instructions:

  • LEFT key to rotate ship left
  • RIGHT key to rotate ship right
  • SPACE to release the ship
  • R to reset the level and generate a new one on random and practice modes

or play it on Android:

Play on Android

Instructions:

  • Touch LEFT half of the screen to rotate left
  • Touch RIGHT half of the screen to rotate right
  • Touch screen to release the ship
  • MENU key to reset the level on random and practice modes

That's all for now, hope you like the game and enjoy playing it.

UPDATE: added the missing screenshot

VN:F [1.9.22_1171]
Rating: 4.0/5 (1 vote cast)

Archers Vs Zombies - Dev Log - 01

I am working on a new game for Android and PC (Windows, Mac and Linux) platforms. The name of the game is not clear yet, for now is Archer Vs Zombies. It is being developed using Libgdx (using its box2d port for the physics), Artemis Entity System Framework and Animation4j. I want to be blogging about the game development progress, maybe some game design decisions and how I am using the libraries, stuff like that.

Quick Description

An archer has to defend something (could be a castle, a house, people) from enemies (zombies, vampires, etc) which are coming to steal/destroy/kill/eat brains (yeah, they are bad) by throwing arrows to them. There will be waves of enemies and different levels with different landscapes.

Here is a video of the current status of the game, so you can visualize the concept:

note: something happened with the video, it should be finished after second 20.

Here are the features I will concentrate for now:

  • There are enemies which comes to kill/steal/destroy.
    • Add a movable target as the first enemy prototype.
  • The player can control the bow power.
    • Add feedback of the current bow power (probably add an arrow in the bow when charging which moves depending on the charge).
    • Restrict bow power.

I already have some ideas to add more depth to the game but I want to explain them in further posts.

The game is open source (for now?) with the codename of archervsworld. Also I have a link here if you want to test the latest PC stable version, and here to test the latest Android stable version.

Hope you like it.

VN:F [1.9.22_1171]
Rating: 5.0/5 (1 vote cast)

Face Hunt - Introduction

We are developing an Android game using Libgdx named Face Hunt. It is only a test to learn a bit about Libgdx and Android's world. Right now, the game will work best on 480x800 devices.

The game is about killing smilies by touching them :D, here is a video:

And here is the qr code for the link of the game if you want to download the game in your android and test it:

Btw, if you want to download the apk, you can touch the image. Don't rely on that link, it could be down in any time, the idea is to put our games on android market directly.

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)

Animation4j Project Introduction

Some time ago I started an internal project to simplify working with animations and transitions when making games in Java.

It was used in some of our games, for example ZombieRockers to make different kind of effects like fade in/out of screens or the points messages when scoring, or in JSnakes to make camera effects like zoom in/out, camera movement, etc.

After using it on our games, it became more clear what kind of things are possible with it.

Now that we are using Github for our source code, we started a dedicated project named animation4j with all the stuff related with animations.

This video shows some working examples of the project:

You can download a runnable jar to test the features shown in the video.

The project is on development, so right now there are a lot of things to improve starting by making easy to understand examples.

I plan to add more posts explaining some design decisions and showing how to use the library.

Hope Java developers could find some use for it.

VN:F [1.9.22_1171]
Rating: 4.0/5 (2 votes cast)