Friday, 31 December 2010

AI in Swords & Soldiers (part 1)

My blog has passed the 12,500 visitors mark! Awesome! And that in just the first four months! Thank you for visiting, and I hope my posts will remain interesting enough to visit here! ^-^

Creating good AI for a strategy game is a notoriously difficult task. AI design is of course never easy, but for strategy games, the complexity of options and situations is incredibly high. I think Swords & Soldiers is the first game for which we achieved really good AI in one of our games, so I figured it would be interesting to see how we got there.



When I was studying computer science, I had high expectations of how AIs worked. I expected there would be these awesome learning and analysis algorithms, and AIs would make incredibly smart decisions. Then I finally got to an actual AI course...

It turned out that the field of AI either solves really simple problems, like chess (yes, chess is incredibly simple in comparison to a realtime strategy game, let alone in comparison to the real world!), or only offers a huge number of nice and efficient helper algorithms, like pathfinding. Real decision making in complex situations is in essence an unsolved problem and as far as I can tell, it won't be solved any time soon.

Totally disappointed, I wondered how actual game AI was made. It turned out that most game AI is completely scripted. There is nothing intelligent about it. Just long rows of if-then-else-if-then-else. To spice it up and add some variation, some randomness may be put in, but that is not worth the term artificial intelligence either. Games where an AI can really make smart decisions, which were not explicitly scripted by an AI designer, are very rare (Black & White's Decision Trees spring to mind).

So, scripting. Swords & Soldiers is no exception. The trick to scripting an AI is to simply add as many specific situations as possible, and script a smart reaction to each of them. If you can define hundreds of different situations and their reactions, then you can achieve the quality of AI that is seen in most games today.

Now the trick is to script in a smart and manageable way. For a school project I have at some point scripted AI by programming long if-then-else structures in C++, but this was terrible. It takes a long time to program and it is incredibly difficult to have a good overview of this, so it is also very difficult to keep it free of bugs.



So for Ronimo's first official game as a company (which was cancelled, by the way), we wanted to do things differently, so we put in the LUA scripting language. Adding scripting to your games is all the hype and big games like World of Warcraft contain tons of script code like this. Scripting has the benefit that you don't have to compile any more, and that a game designer can write the AI scripts.

However, I have concluded that LUA is terrible for this purpose. Scripting complex AI in LUA is just as difficult as in C++, so you basically still need a good programmer to do it. Luckily, though, one of our designers has some serious scripting skills. But you still don't have any overview of the structure of your AI, and it is a lot of work to expose all the necessary data from C++ to LUA. As our enemies became more complex, it became more and more difficult to find and fix bugs in them. So that didn't work out either.

Around the time that I was totally fed up with LUA, I discovered a great article by Bungie on how they had built a Behaviour Tree system for Halo 2. At the time, Halo 2 was well known for its great AI, which would do things like flanking the player and working together as a team.



The basic idea of behaviour trees is that you have conditions and actions. If conditions are met, then certain actions are executed. In many ways, they are just a graphical representation of an if-then-else structure.

So, we set out to build a graphical behaviour tree editor. (Note that in this case, when I say "we built", I actually mean "an intern built". ;) )



We are really happy with using behaviour trees. Our designers make the coolest things with them, and as a programmer, I don't need to be involved in everything they want to make anymore. We are using them again in our Secret New Game, even though that is a game in a totally different genre than Swords & Soldiers!

Next week, I'm going to zoom in a bit further on how those Behaviour Trees work exactly in Swords & Soldiers and how they accidentally also became our story event scripting system.

Edit: here is part 2 of this blogpost: AI in Swords & Soldiers (part 2)

Thursday, 23 December 2010

The endless possibilities of art styles

Last Wednesday I did a five-minute presentation at Ignite Amsterdam 4. Since I care a lot about the point I was making there, I have created an online version of it. Enjoy!








































If you yourself are experimenting with unique styles, then please post a link below, since I would love to see them! ^-^

Ow, by the way, while sitting behind my computer I heard/hurt rumours that today is Christmas, so:

(Awesome image by Elsje Bakker)

Saturday, 18 December 2010

Designing levels without tools

One of the most interesting challenges I constantly meet when developing games, is to make cool things with a minimum effort. For Proun I am all alone and at Ronimo Games we have relatively too few coders to really work comfortably. So getting the maximum result out of a minimum effort is incredibly important.

When we built Swords & Soldiers, we didn't have a level editor yet. We have our own 2D engine at Ronimo Games, the RoniTech, but the RoniTech 1 that is used in Swords & Soldiers was still very bare bones at the time. We are currently working with and on the RoniTech 2 and have lots of cool in-game level editing and graphics editing tools, but for Swords & Soldiers we had to come up with some smart workarounds to design level graphics without having a real tool to do it with.

The core information of a level is where goldmines and towers are, what the height of the landscape is, and what type of ground it is (rock, snow, grass, etc.). We made designing this possible in a crude and simple way: in a Notepad file, each line contains information about one of these categories. Since the game essentially plays on a line, this is actually very simple to understand and edit and worked really quick for our designers.



To give our artists some room to create places of interest in the level, I gave them the possibility to define the exact position of props as well. They had a couple more lines for this and a prop sheet with a character per prop. By typing these characters in Notepad, they placed props in the level. This has an incredibly hardcore hacking feel to it, but I simply didn't have the time to create anything better (creating an entire console game with only one full-time programmer is, euhm, interesting...). It did give our artists enough freedom to make some fun areas, though, so the results were good.



The final part of the levels is the backgrounds (and foregrounds). We have lots of parallax layers and each layer is at a different distance from the camera. Also, the background layers often contain loads of objects per level. Since perspective is involved here, Notepad was out of the question. And without a graphical tool to design that, we had to choose a different solution here.

We came up with a procedural system where artists would create background elements for different layers, and the game would place these elements at random positions in their layers. Artists described to me which layers they wanted (nearby trees waving in the wind, medium distance hills, far away mountains, clouds, etc.) and I created code to place them. I did give our artists the possibility to remove layers or make them extremely dense, so that they could create deserts, mountain areas and forests.



The obvious downside of this approach is that it is very difficult to create unique places per level. With a real graphics level editor, artists could have placed specific eye-catchers in certain levels, like a city in the distance, or a palace just behind the field of war. This was impossible with the lack of tools for Swords & Soldiers.

Since we have a limited amount of background elements to generate levels from, repetition looms. So the artists requested two forms of control over that: one is simply a background gradient per level. The other is an atmospheric fog gradient, also per level, that is overlayed over the objects that are further in the background.



Despite the limitations, we managed to create a game with rich graphics that got great review scores. The smart choices described above made it possible to achieve that with only one programmer, which I am really proud of. Obviously, though, the real credit goes to our art team, who made some really compelling artwork. To quote IGN: "One of the best-looking games on Wii."

(Did I already mention I feel very proud of Swords & Soldiers? ;) )



PS. For those interested in some of the most hardcore programming topics: Jelle van der Beek, lead engine programmer at Vanguard Games (formerly known as W! Games) made a cool tool to analyse memory allocations and memory fragmentation. Although I have to admit that none of the problems he solves with this tool are relevant for the scale of games that we make at Ronimo (even though we do make real multiplatform console games), he shows some clear examples of the complexities of the topic, making this a very interesting presentation indeed for hardcore coders: Heap Inspector Demo video

Saturday, 11 December 2010

All the settings

An important part of being a programmer at a game development company, is creating things in such a way that game designers can do their job. They get to fiddle around with the gameplay, while programmers build the technology. At Ronimo Games we have built a whole suite of tools for designers to work with, and I personally think the settings system is the most important. The nice part is that it was also the easiest to build.

At its core, this is a very simple thing. We have a text file with lots of values that our designers can tweak. For example, for each unit, the designers can set the health, price, walking speed, weapon's range, and a lot more. In Swords & Soldiers, we have some 600 gameplay settings like that.



A programmer creates these settings, and then a designer experiments with them to choose the right value for each of them. To do that, the designer needs to see the results of his changes quickly. Now I would like our designers to work with that as fluently as possible, so I built the feature that by pressing F5 in the middle of the game, they can reload all the settings. So they can constantly switch between settings and game using alt+tab, and use F5 to reload on the fly. Usually there is no need to restart the game all the time.

Technically, this is very simple to do. The key aspect is to use a struct for the settings and keep pointing to that. Never copy a value from the struct, because the copy won't change if the struct does. Thus when F5 changes the values in the struct, all the gameplay immediately uses the new values.

Depending on your needs, lots of variations to such a settings system can be chosen. In Proun I don't have the F5 mechanism, but I do have the possibility to change all the settings per level. So in the third track, I could easily make boosts last twice as long as in the other tracks, which happened to fit the track design better.

One of my first implementations of this system was De Blob. There we even implemented sliders in the game to allow designers to quickly modify properties, but we learned that as the number of settings explodes, these sliders didn't work that well. Since they were also a lot of work to build, we have simply used Notepad to modify settings ever since.

I would like to also talk about Ronimo's Secret New Game here now, since we have a much more advanced settings system for that, but that will be for another day. After all, the game is still Secret (tm) right now.

A settings system like this is so simple to build, that I think you should even create something like it for the smallest student games. It is also something that you can build once and then reuse in all your games after that. In my opinion, fast gameplay tweaking options are an absolute requirement for good game design.

Saturday, 4 December 2010

The horror that is PC development

Swords & Soldiers is now out on PC and Mac on Steam! Buy it from SwordsAndSoldiers.com and use the coupon-code "sinterklaas" before December 5th to get a 30% discount!

In the past two years I have released my first games as a programmer on the Playstation 3 and Nintendo Wii. As I was told beforehand, the most evil part of console development are the certification requirements. Sony, Nintendo and Microsoft have long lists with hundreds of demands that you must meet in order to be allowed to release your game on their platform. These are all purely technical and are about topics like: "Pause the game if the controller runs out of batteries", or: "Show this message when the harddisc is corrupted."

Going through those lists and implementing all of it is incredibly boring work and has little to do with making cool games. However, last week I learned that PC development is actually a lot worse! On consoles, at least all of the requirements make sense, and since you know exactly on what hardware your game will run (all Playstation 3s are essentially the same), it is quite easy to test for. How unlike PC development...

Last week we launched Swords & Soldiers on the PC and the amount of things that can go wrong on all these different combinations of videocards, drivers, Windows versions and user settings is just terrible. Knowing the hardware on consoles suddenly makes them seem incredibly simple to develop for!



The main culprit on PC is the videocard, so I figured it would be interesting to have a little look at the kind of issues we came across during development of various games.

One thing most graphics programmers know, is that you should use power of two (PoT) textures, so resolutions should be like 128, 256, 512, 1024 and 2048. However, this is only relevant for pretty old videocards, since all modern videocards can handle other resolutions as well. Because of our animation system, we often have to round values up, so an animation sheet of 600*512 pixels becomes 1024*512 pixels. That is a huge waste! So to save a lot of space, we don't use power of two textures in HD. Since older computers also need to run Swords & Soldiers, we added a low-res SD mode where all textures are power of two.

Now I would like to detect whether the videocard can run in HD, and then hide that setting if the videocard cannot handle non power of two textures. But how do we detect that? There are some OpenGL extensions related to it, so I tried checking for those. However, some videocards don't have these extensions, yet they can run in HD anyway. I wasn't able to find a definite way to detect this (although I guess greater minds will know one), so I ended up allowing everyone to select HD and showing a message that tells the user to play in SD if the game looks broken otherwise. Pretty lame, but at least it is a clear solution!



A weird issue I came accross while working on Proun, is that certain really old onboard Intel videocards cannot handle objects with more than 65536 vertices. This only happens on some very specific old Intel cards and is okay anywhere else. Since Proun is a hobby project and splitting meshes for this would be quite some work, I ended up not fixing this one.

One of the simpler hardware differences to work with is shader versions. Vertex and pixel shaders can have all kinds of features, depending on the videocard and the DirectX version. However, programming for these isn't that difficult, because shader versions are always backwards compatible. So if for example you would write a shader for version 2.0, then it will definitely also run on 2.x, 3.0 and 4.0. There are no weird combinations here!



However, there are those videocards who think it is okay to lie. While developing De Blob, I learned that there are videocards that claim to be able to do shader 2.0, while they really don't. So I asked that videocard, "Can you do shader 2.0?" and the videocard said: "Yeah, sure, no problem!" I then tried to load the shader, and the videocard simply refused. How is one to program for that kind of weirdness?



The final example I would like to discuss here is one that I am a bit ashamed of, because Swords & Soldiers actually launched with this issue and I had to release a patch On Steam yesterday to fix it. Swords & Soldiers has a dynamic font rendering system, which means that we only load those characters that the game actually uses into video memory. This way we can handle the enormous number of characters needed for languages like Japanese and Chinese. Now we only implemented this after we did extensive tests on all kinds of videocards and we totally forgot to test again, so it turned out about 1% of users could not see any texts in the game at all. Pretty lame.

I haven't been able to figure out why this happened on those videocards, but I do know that the OpenGL function glTexSubImage2D was the cause. So in yesterday's patch I built a workaround that doesn't use that function. For more technical details on this problem, have a look here.

So, I conclude that PC development is actually more difficult than console development!

If you have any fun or interesting examples of rare hardware/driver/OS differences that break games, then please share them by commenting beneath this post. I'd love to hear about your experiences! ^-^