Global Game Jam 2017

Finally took part in the Global Game Jam after so many years just visiting the local game jam site as a visitor! ūüėĬ†It was a dream come true to see so many people enjoying the game-making craft and working hard to make amazing games over one packed weekend. The quality achieved in these little gems was great and I’m glad to be part of a global game-making community. Sharing my passion with so many like-minded individuals¬†felt like¬†being home.

This year’s theme was waves. The¬†game¬†we created is¬†a local-multiplayer 1v1 game where your character claps in 3 different ways in order to send sound waves towards a particular, currently topical, person’s toupee. These sound waves make said person’s toupee fly off if done enough times. You can counter your opponent’s waves in a rock-paper-scissors fashion in order to untilt your opponent’s tilt of the toupe thus tilting it in your favour. The person who manages to tilt the toupee far enough sends it flying towards their opponent. Doing so wins the game!

Game Jam Theme: Waves

Team name: Potbelly Vikings and A Gozitan (don’t ask lol)

  • Programming: Alan Duca, Dylan Abela, Mark Anthony Cassar and me
  • Art: Mark Anthony Cassar
  • Mexican Wave Programming: Anthony Demanuele

Game name:¬†TTT (Trigonometry Trumps Toupees)¬†(don’t ask 2 lol)

Some screenies:

Game page: http://globalgamejam.org/2017/games/ttt-trigonometry-trumps-toupees

Controls:

  • Character Selection
    Red Player: W/S/A/D keys to move your selection, Space key to select, Ctrl key to unselect
    Blue Player: Cursor keys to move your selection, Enter key to select, Backspace key to unselect
  • In-Game
    Red Player: A/S/D keys correspond to Sin (x), Cos (x), Tan (x) respectively
    Blue Player: J/K/L keys correspond to Sin (x), Cos (x), Tan (x) respectively

Play the game here:

We came in 3rd locally by popular vote! Not bad for a first time try! ūüėÄ

Using Unity’s Postprocessor to “compile” strings

Tl;dr: String parameters are not type-safe. Using Unity’s Postprocessor replace them with automatically generated enums and detect errors at compile-time instead of run-time.

Recently, while working with a Unity plugin, I came across an API which makes use of string parameters. Personally, I am not a huge fan of them but I must admit that¬†they are¬†sometimes a necessary evil. This happens¬†when an item is not available at compile-time but rather is determined at run-time. Their positive aspect is that since they are untyped they allow you to pass in virtually anything you want and it would allow you to compile without any problems. The caveat? If that string refers to a resource that is not found you have no way of knowing this until you happen to do a call to that API. If that happens the results could be unpleasant or even worse¬†they could go through testing¬†undetected until they are in the hands of a player possibly causing unwanted behaviour.¬†So the questions are: Is there a way one can make one’s¬†code a bit more strict by somehow introducing types in strings without losing the flexibility that the latter¬†provide? Can we know at compile time whether our resource is well-referenced so that no (possibly hidden) problems crop up later on at run-time? ¬†As it turns out in my specific case:¬†yes! To be completely honest¬†I am not sure it would work out in more cases and whether it can be in fact generalised but I am sharing it here just in case someone else finds it useful.

In my specific case the plugin is an audio one which, via a string parameter in their API calls, allows me to play a sound clip. In this case the string parameter refers to the sound clip name. Since this is a string which might be used in multiple places a common good practice is to make it a constant string. This latter constant string is then used instead of the actual string literal. In this way we gain a very small but useful advantage: if, for some reason, the sound clip name changes I have to change only one central place instead of having to potentially change more than one file. During development change is the only constant as they say, so being able to do quick changes like this significantly aids development by reducing the number of manual changes one has to do and preventing programmer errors which cannot be detected by the compiler.

So OK: I can actually now do the change manually in one place and my strings are a bit safer, however:

  1. I can still make a mistake in the new name I enter which error won’t be detected until possibly much later on.¬†Moreover, someone might decide to change a sound¬†clip’s name for whatever reason and inadvertently cause havoc in the game.
  2. I have to actually manually change/add/remove different constants for every sound file I rename/add to/remove from the project. We already automated one aspect by using constants. Can we improve upon this so that we automatically update the code?

Fixing the first problem above, the one related to detection, can be achieved¬†by means of Unity’s own asset¬†post-processor¬†methods. More specifically in our case we can make use of:

AssetPostprocessor.OnPostprocessAllAssets

This static method is called by Unity when changes are done in the project’s Asset folder. Therefore, adding/removing/renaming sound files would cause a call to this method to be made by Unity. This is the perfect place to do what we want to do, that is: detect what sound clips we have available when a change is made to the Asset folder. This can be achieved by means of the following code:

var filesPath = string.Format(
    "{0}/Sounds/", 
    Application.dataPath);
var filePaths = Directory
    .GetFiles(filesPath, "*.*", SearchOption.AllDirectories)
    .Where(file =>
        file.ToLower().EndsWith("wav") || 
        file.ToLower().EndsWith("mp3")
    )
    .ToList();
var soundFileNames = filePaths
    .Select(filePath => 
        Path.GetFileNameWithoutExtension(filePath))
    .ToList();

The first line simply generates the path where our sound files are. By using¬†Unity’s dataPath we are simply generating the path “<ProjectPath><ProjectName>/Assets/Sounds”. The second line does a search¬†for all the file paths in this directory¬†and its subdirectories which end with “wav” or “mp3”. In this way we obtain a list of all the file paths we care about. (Other formats can be added with ease in a similar way.) ¬†The third and final line iterates through all the file paths and obtains just their name.

Having now¬†detected all of the file names one might ask: what are we going to do with them? My answer is to use the file names to generate an enumeration which represents the sound file names and hence “typify” our strings! Basically, we are going to do a very simplistic form of code-generation within Unity! This will allow us to automatically update our code without our intervention. We can achieve this simply as follows:

var enumValueSB = new StringBuilder();
foreach (var soundFileName in soundFileNames)
{
    enumValueSB.AppendFormat("\t{0},\n", soundFileName);
}
var fileContents = string.Format(
    "public enum SoundNames\n{{\n{0}}}", 
    enumValueSB.ToString());
File.WriteAllText(
    "Assets/Scripts/Sounds/SoundNames.cs", 
    fileContents);

Here we are basically iterating through all of the file names and generating their corresponding enumeration member. (We are also adding some tabs in order to preserve some code indentation.) Once we have done this we encapsulate all of these into our “SoundNames” enumeration. All of this generates our code which is then dumped into a sound file in the path of our choice. In my case they will go under “Scripts/Sounds/” which I have created forehand. (Of course, generating these folders automatically through code can be done too if required.)

Now all we need to do is actually call the two code snippets above from within “OnPostProcessAllAssets”. From now whenever we do an asset change, Unity will check whether the files in our “Sound” folder¬†have changed and if so it will generate the required enumeration for us automatically! In my case if I add “sound_file_001.mp3”,¬†“sound_file_002.mp3” and¬†“sound_file_003.mp3” the generated script is:

public enum SoundNames
{
    sound_file_001,
    sound_file_002,
    sound_file_003,
}

Now whenever I need to use a particular sound name I can do, for example:

SoundNames.sound_file_001.ToString()

I sincerely believe that while adding ToString() looks a bit ugly at least we are now sure that our code is safer and less prone to human¬†errors by leveraging the compiler’s power. Something to note is that now whenever someone makes a change to our sound files, our project might not even compile. Let us say I rename “sound_file_001” to “sound_file_001b”. Now wherever we have the above code example¬†becomes invalid since our code regenerates the enumeration accordingly. Ideally this is automated as well but for now at least our code is more safe because the compiler will from now on take care of any sound file changes for us.

I have attached the above code in its most basic form here (zip format):

SoundNamesImporter

Unzip it, drop it into your Unity project,¬†create the relevant folders and you are good to go. Feel free to improve upon this code (there are a million ways it can be improved). I hope that this post helps someone speed up their development or think of new ways of¬†exploiting Unity’s PostProcessor.¬†Feel free to share any mistakes and/or improvements by adding comments below.

Working with C++ and Cocos2dx

Recently I’ve started my foray into the C++ world proper. I have always toyed a bit with the language and its predecessor C but I never really got into it fully, preferring instead languages such as C# and Haskell. However, due to the nature of the industry I work in, and due to how C and C++ make you think more about memory-efficient implementations, I thought it was high time I delved deeper into it.¬†So far, personally I prefer the simpler C and languages such as C# and Haskell but hopefully this will change as I gather more knowledge about the language itself.

Just reading C++ tutorials and books is a bit boring though and learning is often more ‘fun’ when it is applied. Especially when it is applied to games! Hence, I’ve decided once again to try re-writing Space Generals. (Yes, yes, I’ve said this before). This time I opted for mobile and using the Cocos2dx engine.

Setting up Cocos2dx was not that hard but finding good up-to-date information online is hard since the engine is going through constant iterations. I am hoping the following set of steps help anyone who is looking to setup their Windows environment to be able to develop for Windows and for the Android platforms. They are very terse on purpose but should work. Let me know if you need more information and I’ll try dig it out for you.

Disclaimer 1: Cocos2d and Cocos2dx are¬†in constant development so bear in mind that the information I have below might have already become outdated. Let me know and if I have some time I’ll update it accordingly.

Disclaimer 2: I work with a combination of Visual Studio 2013 and Eclipse Juno. If your target is simply Android, Eclipse should be enough. I assume you have some basic knowledge of how these two IDEs work. Having some knowledge of Android development would also be an asset.

Creating a Cocos2d-x project for Windows

Download and install

  1. Download latest v2.0 python 2.7.9. (Not 3.0): https://www.python.org/downloads/release/python-279/
  2. Download latest Cocos2dx (at the time of writing this is Cocos2d-x-3.4.zip): http://www.cocos2d-x.org/
  3. Install Python to your drive, e.g.: C:\Program Files\Python\2.7.9
  4. Make sure you add Python to your PATH environment variable.
  5. Unzip Cocos2dx to your hard-drive. For the sake of these steps I will use: C:\SDKs

Creating the project

  1. In command-line:
    1. Goto to the Cocos2dx folder: C:\SDKs\Cocos2d-x-3.4\tools\cocos2d-console\bin
    2. Create the project via> cocos.py new AwesomeGame -p com.awesomestudio.awesomegame -l cpp -d C:\GameDevelopment
    3. Run the project for the first time via> cocos run -s “C:\GameDevelopment\AwesomeGame” -p win32

Creating a Cocos2d-x project for Android

The steps below assume the above section has been completed successfully.

Download and extract

  1. Download and install JRE
  2. Download the Android NDK: https://developer.android.com/tools/sdk/ndk/index.html
  3. Download the Android SDK: http://developer.android.com/sdk/installing/index.html
  4. Download Apache ANT: http://ant.apache.org/bindownload.cgi
  5. Unzip Android NDK to C:\SDKs
  6. Unzip Android SDK to C:\SDKs
  7. Unzip Apache AND to C:\SDKs

Setup for deployment

  1. Run setup.py found in C:\SDKs\Cocos2d-x-3.4
  2. Supply path for NDK_ROOT, e.g.: C:\SDKs\android-ndk-r10d
  3. Supply path for ANDROID_SDK_ROOT, e.g.: C:\SDKs\adt-bundle-windows-x86_64-20140702\sdk
  4. Supply path for ANT_ROOT, e.g.: C:\SDKs\apache-ant-1.9.4-bin\apache-ant-1.9.4\bin
  5. A restart of the machine might be necessary at this point.

Compiling and running the solution

  1. In command-line:
    1. Goto to your android project folder, e.g.: C:\GameDevelopment\AwesomeGame\proj.android
    2. Run build_native.py.
  2. In SDKManager, update to latest toolkit.
  3. Open eclipse:
    1. Use any workspace. I used: C:\EclipseWorkspace
    2. Import solution into workspace, no need to copy in. You need to import cocos2dxlib and your game project.
    3. Remove libcocos2dx as a dependency. This was one of my major headaches while trying to configure my setup.
    4. Remove the default project dependencies (again something I didn’t know I had to do).
    5. Another problem I encountered was with my .cproject file. If that happens try recreating the project via cocos.py and then build_native.py.

Running and debugging

In Eclipse, assuming you have an Android virtual device setup, hitting run should make your game work using the default Cocos2dx-generated application. Unfortunately I had some problems with this due to a known issue related to my system configuration. Sometimes you cannot debug your application due to your graphics card not supporting a particular version of OpenGL which android supports. I am not a 100% sure why as I only did a quick Google search. There is help out there though if you encounter this problem. I hope you have more luck than me with this. If so feel free to comment.

In the end I resorted to coding and debugging my game in Visual Studio and then building it in Eclipse to create the apk which I then manually move to my Android device.

Hope that helps anyone trying their hand with Cocos2dx!

Space Generals v1?

Lately, due to work and personal interest I have been experimenting with Unity. It is a very nice and simple 3D engine with a lot to offer in terms of features and capabilities. I am hoping that in the following months I will be able to use it in order to perform a re-write of Space Generals. While Space Generals (version 0) met its requirements as one of ¬†my Master’s case-studies, it is in a dire need of a bottom-up overhaul. This will probably mean having to make do without a Haskell back-end and a Google Web Toolkit-based front-end. If Unity allows me to call external executables I might re-use the Haskell back-end as is and simply use Unity instead of GWT for the front-end. More on this as I make more progress…

In the meantime I am back to studying vectors and matrices with the hope that this allows me to refresh my memory and to better understand the various functions provided by the Unity API. In the end, while the API does a fantastic job to simplify and abstract, I feel that a basic knowledge of the underlying mathematics is a good plus. (Now I wish that my brain had conveniently kept its knowledge of this subject from pure maths classes, but apparently mathematics topics are like languages for me, they become rusty if not used from time to time.) The next step after this would be to go through various tutorials in order gain some experience and insight on how to best design and implement Space Generals using Unity. Finally, it will be my turn to get my hands dirty and give it a good shot.

Once more I am hoping that this blog will be a way for me to share what I have learned and to keep a journal of the game’s process.

4Blocks Source Code

Today I decided to try my hand at releasing a cabal package with the 4Blocks code. I finally managed: http://hackage.haskell.org/package/4Blocks-0.2. (I made a newbie mistake in 0.1 so please ignore that release).

Unfortunately the library can be compiled only with, as far as i know:

  • GHC 6.8.3
  • Gtk2hs 0.9.13

The reasons why are in the README file in the cabal package, copy/pasted here:

Some notes:
– The game currently works only with GHC 6.8.3 due to its use of Gtk2hs 0.9.13.
– I haven’t tested with anything later but it is likely to fail due the fact that¬†later versions of Gtk2hs have a different system of handling events.
– To make this project compatible with later versions of GHC and Gtk2hs two changes are required:
– Remove the function “permutations” which was copied from a later GHC base library
– Alter key-event handling to the version used by later Gtk2hs: some functions (in CommandKeys.hs)¬†were simply introduced in order to disallow some of the keys used in the game (namely rotation) to trigger¬†continuously when a key is held pressed. I believe this kind of behaviour can be managed automatically¬†with Gtk2hs’ new event handling mechanism, however I haven’t had time to recode accordingly myself.
– I hope to write a patch for this in the near future.

So yeah, fairly old now, but I thought, I’d share the code all the same as some people have requested it earlier. Hopefully I will release the code with my AI later on after my project is over and done with in summer and also a patch to make it work with newer versions of GHC and Gtk2hs.

I have to say I really enjoyed coding the game in Haskell and if you have any comments for me, regarding better ways to code stuff, silly things I did, or anything else please let me know by leaving a comment!

Space Generals

Hi all!

It’s been more than 4 months since my last post! I’ve been really busy lately creating a website for a game called Space Generals which we are going to use for our research in game AI. The game itself is actually a turn-based one much in the style of Risk with a little twist which will allow us to study hierarchical AI and domain-specific embedded scripting languages. The website’s front-end is developed in Java using the GWT framework while its back-end, which carries out turn-processing and game-logic, is developed in Haskell. Here are some screenshots of the game:

More information about the game and how to play can be found in the User Manual.

Feel free to register and play the game. You need at least one more friend to play a game and up to 5 players can play together. ¬†Also, if possible report any errors and provide suggestions using the game’s:

Email Address

Twitter

Facebook Group

Also, it would be of great interest to us for you to share your game-playing strategies using the above methods as we shall be using them to create different AIs using the DSEL we are working on at the moment.

Thanks! We hope you enjoy the game!

P.S. Currently only Mozilla Firefox and Google Chrome are supported.

4Blocks with AI

I’ve been wanting to write another blog post on the series on DSELs I’ve been planning, but I’ve been really swamped with work lately. So once more I’ll post a video or two on something I’ve been working on. It’s the 4blocks game again, this time playing on its own with an AI I’ve written for it. Here are the videos:


On the whole these were two good runs. I’ve had much better and much worse runs of course.

The AI is by no means perfect but it uses the DSEL techniques in Haskell. I have two languages acting here:
1. A Tetris DSEL
2. Haskell acting as a meta-language over the Tetris DSEL.

What is happening is that for every new brick the game state is queried by Haskell for certain information such as occupied blocks or number of lines. Using this information, by means of Haskell’s case statement I simply trigger one of a possible number of strategies. These strategies acquire certain information from the game state and generate a DSEL script. This script is then carried out by a DSEL script interpreter.

There are numerous problems with the AI and its mostly because I haven’t had time to write more strategies or a more intelligent way to select what according to the selected strategy is the best final brick destination.

That’s about it for now. Let me know what you think.

4Blocks in Haskell!

This week, instead of a post on DSELs, I’m going to show what I’ve been working on. It’s my first, real foray into Haskell: a game implementation. Some day soon I hope to make my implementation available on the Haskell libraries repository (Hackage).

Thanks goes to the folk at the Haskell irc channel #haskell who helped me when I got stuck. Special mention goes to Saizan for his everlasting patience.

Edit:
Some more details:
The game is being played by me in single player mode. The other parts shown are WIP. The graphics library used if anyone is interested is Gtk2hs.

Edit 2:
In light of a comment by one of the visitors of the blog (which was also a nagging worry of mine), and some searching on my side I decided to alter the game’s name and certain minor features to prevent copyright problems. I have uploaded a new video which reflects these changes.

Edit 3:
4Blocks code released! Check here and here for more info! ūüôā