Pages

Wednesday, March 28, 2012

Character State Management

   When making a game how should the program decide when the player should be walking? Well, maybe if they press the arrow keys. But, then what if the character is crouching and you would like the arrow keys to make them crawl. Also, what if you want the character to jump when the up button is pressed but only if the character is standing and not walking. One way is to have a variable for jump and another for walk and one for crouch, and by using a large combinations of if-statements you can determine the animation that should be played.

   Anyone that has followed this way of thinking before will know that eventually this if-statement will get large and the number of state related variables grows quickly. Once the number of variables becomes too large, handling the various exceptional cases becomes nearly impossible.

   When I started I too faced this problem, however I have since found a much more elegant solution. I have found that all of these states can be described as a finite state machine. Game developers can benefit from this approach by clearly defining which states can transition to others and by abstracting the transition logic away from keyboard inputs.

   To start a programmer needs to describe a simple generic finite state machine. A finite state machine consists of a graph of nodes representing states and directed edges representing actions. The state machine keeps track of its current state and when it receives an action it move to a new state only if it has an out going edge with the same action. Then, Once we have a generic finite state machine we simply draw whatever animation we want to associate with a particular state.

   One thing to note in the design of my state machines is that I never make the action refer to user inputs. This is because not all of the characters in my game are controlled by the player, so for non-player characters it does not sense to think of actions as user inputs. This also gives me the flexibility to change the inputs later if I want to run it on another platform, like the Xbox360 which has buttons and joysticks rather than a mouse and keyboard.

A partial character state diagram from my RPG game engine
   I have used this technique in my RPG game engine and have found it extremely elegant. I no longer need massive if statements for determining what character animation should be drawn. I have also found it has enabled me to have more complicated character actions, like action combos, that I used to find very difficult to program.

Monday, March 26, 2012

RPG Elements: Console Improvements

     Back in February I wrote a blog entry about an RPG game engine that I was working on called RPG Elements. I have been busy with university, however I have found some time for implementing interesting new features. I am most excited about some of the new features I have added to the map editing tools recently. These tools allow me to create maps much more quickly and even generate hundreds of simple maps in mere seconds.

     I mentioned in my last blog entry that the main editing tool is an in-game console with commands for adding new objects and creating new maps. As my engine has evolved many features had become merged with the console that really should not have been, for example the actual logic of the console commands in many cases needed to be separate functions. With all of the unrelated logic in one place, adding new commands became increasingly difficult. To fix this problem, I refactored the console by moving these features into more appropriate modules, I also separated the console commands into there own classes. Now the console module is complete independent of the commands which it runs.

     When I refactored the console I realized that I needed to redesign the settile hotkey. In my RPG engine, the map is represented by an array of tiles, the user can set the type of the tile their player is standing on by typing "settile <type>" into the console. Because this is such a common operation, it is not practical to make the user type this command in every time, so I had the console remember the last arguments passed to this command and then all the player would need to do was press the "w" key to execute the same command again. However, now I made the commands independent of the console I no longer wanted the console to be storing the arguments of a specific command. Well I was thinking about how to fix this I suddenly thought that it might be nice to have this hotkey feature for other commands or have hotkeys for different arguments. So I came up with a new console command that can assign any console command to any of the number keys. For example, "hotkey 1 settile;sand" sets the tile under the player to sand when the 1 key is pressed. Another advantage of this feature is that I can assign "sand" to the 1 key and "grass" to the 2 key, where before I would have to type in a console command every time I wanted to switch between the two tile types.

     For my next feature I wanted to be able to write a list of console commands in an external file and then have them executed sequentially. When I refactored my console I realized that this would not be very difficult to add and would help easily accomplish what I had originally designed my engine to do, generate a large amount of content with minimal high level user input. One difficult I was confronted with was that loading maps took a couple milliseconds to load and scripts executed in a separate thread. I solved this by adding a command to tell them to wait a certain number of milliseconds to give the map a chance to load. Another problem was that because I am executing hundreds of commands at once I more frequently come across some of the race conditions that I had noticed occasionally in the past. While at the moment these race conditions seem an annoyance, I think of this as an opportunity to find and fix these serious bugs that would not have been possible with manual testing. I am excited about how quickly I was able to add console scripting and I believe it will be very useful going forward.

     With the addition of console scripting I can now generate hundreds of maps and add doorways between them. I thought it would be interesting to visualize all of these maps and their connections, so I wrote a console command that would query the list of doors to identify the directed edges between the maps. The program then outputs a file with a list of these directed edges in a format that I can input into a graph making program called Graphviz. Well this graph is interesting to see now, it will be very useful for some of my planned features. One feature that will require a graph of directed edges is non-player character (NPC) path finding, eventually I want to program NPCs to walk between maps, and by analyzing graphs like this they will be able to identify the best path.

An example map graph generated using test data based on a popular classic RPG game. At this point all of the maps are empty, aside from doors (represented in the graph by directed edges)
     By improving the tools in these ways, along with some other minor improvements, I have greatly reduced the amount of work I need to do to create a map and can now focus on higher level design decisions. Well these tool improvements are very interesting, they are not the only new feature that I have implemented since February. In one of my next blog entries I will talk about some of the other new features. I am also hoping I will have time to upload another video to demonstrate some of these new features. In the meantime I am adding new feature to this project and the next big area I need to improve is NPC behavior and interaction. So stay tuned!

Friday, March 23, 2012

New SimCity Engine

     A couple days ago I wrote a blog entry about a simulation game that I had written to learn SDL and improve my C++ programming skills. I showed this project off at the game developers club and it reminded them about the new SimCity game that was just announced. The game was announced at a recent game developers conference where they also explained a bit about their new simulation engine called GlassBox. The algorithm that the engine uses has a particularly elegant design which I find very interesting.

     The system defines five basic object types: resources, units, maps, globals and agents. Resources are variables such as people, money, electricity. They are organized into bins that can store a limited amount of a resource. Units are physical objects in the world like houses. They are defined as a collection of resource bins. Maps are used to represent the distribution of resources across the environment. They are essentially an array of resource bins with one associated to each map tile. Globals are like units, since they are a set of resource bins, but do not represent physical objects. Lastly, are these things called agents that move around the environment distributing resources between resource bins. Game developers represent these different types of objects using rules which they define in scripts. [1]

    Game developers are able to simulate various natural phenomenon using these simple constructs (as can be seen in this video). For example, a map can be used to represent resources like underground minerals or areas covered by forests. Agents are used to represent the distribution of electricity, pollution or the movement of cars and pedestrians. According to the article the company that makes SimCity believes this engine could be applied to their other simulation games like, one of my favorites, SimTower. [1]

     Although this design seems very elegant I feel that there many some potential problems which have been left out. One aspect that may pose a problem is that there may be tens of thousands of independent agents running in parallel at any given time. Well this seems beneficial in the era of multi-core computing, managing these agents and ensuring mutual exclusion is non-trivial. Another thing to consider is if so few constructs can accurately represent everything needed in a simulation game with over simplifying some aspects.

     I believe that this engine was very elegantly designed. This level of elegance is what I often strive for in my own programs which is why I appreciate how difficult it is to achieve. To create such an elegant engine, software developers often need to iterate and rewrite the program, each time removing unnecessary or redundant features.

     I would very much like to try and implement a similar engine one day and this engine it was interesting to read about. Game development companies are often very secretive so when they reveal this kind of information it is very valuable.

References:
[1] Frank Cifaldi. "GDC 2012: Breaking down SimCity's Glassbox engine" Internet: http://www.gamasutra.com/view/news/164870/gdc_2012_breaking_down_simcitys_.php, March 7, 2012 [March 21, 2012].

Wednesday, March 21, 2012

Game Developers Club

     Back in January I wrote a blog entry about how I volunteered at clubs week to promote the game developers club. Since then we have held several meetings and had some interesting discussions. Since I have been a member for a while I am also looking for ways to get more involved.

     One of the things that I really enjoy about the game developers club is learning about all kinds of different technology that I might not otherwise have heard about. Usually when I am working independently on my own game projects I am so focused on the way that I am currently doing things that I do not stop to consider new or alternative technologies. By discussing ideas with other people I feel that I can learn more than I can individually.

     For example last week at the game developers club we discussed game engine called Unity. Some of the other members have used Unity to create very interesting games. Unity developers also have the choice of embedding there game in a web browser or deploying it to a mobile device. At one of our upcoming meetings one of the members might give a brief tutorial on how to use Unity because many people seem interested.


     I have really enjoyed being a part of the game developers club and often try to find ways to get more involved. As I already mentioned I volunteered at clubs week but I have also been looking for ways to help organize the meetings and other club activities. Over the past couple semesters some of the prominent founding members of the club graduated and there has been a gap in leadership. Last week one of the former club presidents drop by during the Burnaby meeting and we talked about some of the things we could do to try to improve the club. One of the problems is a lack of focus that we have had at our meetings lately, to fix this I will try to spend some time during the week before the meetings coming up with some more topics for discussion.

     Another thing that needed fixing up was the club website. Many of the pages were out of date, especially the projects page. I started by talking to some of the executives and gathered a list of the projects that they had worked on. I will also need to find out this week if any of the other members have projects they want to post on the website. The project page on the club website is a valuable feature for promotion of the club and its members. The former club president I was talking to was saying game development companies often visit the website and one of the first pages that they want to look at is the projects page.

Monday, March 19, 2012

Book Review: The Animators Survival Kit

     Back when I was working on my animation project, one of my group members recommended a book to me. Unfortunately, he recommended it to me near the end of the project when the deadline was fast approaching and I did not have time to read it. I remembered that book the other day while I was at the university library researching for a history project. I searched on the library's website and found that they had a copy of the book so I decided to read some of it.

    The book is called The Animators Survival Kit and was written by Richard Williams, the animator behind the film "Who Framed Roger Rabit?"[1]

    I found this book interesting because it explained some of the techniques used by traditional animators. Many of these techniques, such as tweening, are now done automatically by computer animation software. Even though these processes are now automated, I feel that understanding the workflow and techniques of traditional animators can be useful to improve digital animations and make them more expressive.

    One process digital animators often take for granted is called tweening. In traditional animation, tweening was the process of creating the frames in between the major actions. In digital animation software the in-between frames are inferred using mathematical interpolation. Often beginner animators will rely too heavily on the animation software to generate the correct in-between frames and as a result there animations look artificial.

    One thing that I never realized about tweening is that traditional animators would draw the in-between frames halfway between two frames they had already drawn. This is because it is easier for animators to imagine what the frame should look like if it is exactly half way rather than a third of the way to the next frame. Then they would draw further in-between frames to make the animation slower or smoother.[1]

    The book also explains about how to make characters more expressive and have display their personality through there actions. The most fundamental action that many animators overlook is how the character walks. The book probably has around one hundred pages just dealing with developing the walk cycle.

    Another technique the book explains to make the characters more expressive is to design their actions to have three phases. In the first phase, called the anticipation, we want the character to make a motion that sets the scene for the major action. In the second phase the character preforms the major action. The final phase is the result where we see the outcome of the major action.[1] For example consider a cartoon character hitting another character with an over sized hammer. The anticipation would be the character lifting the hammer over their head, the action would be them quickly slamming it down and the result would be the character lifting the hammer to reveal the other character has been flattened like a pancake.

    I have enjoyed reading this book and hope to try some of these techniques when I get some free time. I definitely recommend it to anyone interested in learning more about animation and the workflow of traditional animators.

References
[1] Richard Williams, Animators Survival Kit, Expanded ed. London, Faber And Faber Ltd, 2009.

Saturday, March 17, 2012

Summer in Innovation

     I mentioned I have already mentioned that since the courses offered at my university are very sparse I have been looking for alternatives. Initially I thought I should try to find another co-op job, however I have already done 4 semester of co-op and would really like to concentrate on graduating so that I can get a full time position.

     Then I heard about a program that my university is offering called the Summer in Innovation. The program is targeted at upper division computer science, business and interactive arts and technology (IAT) students. Twenty-four students, eight students from each of those three disciplines, will be chosen and the program will have a focus toward working with and learning cross disciplinary concepts to develop software applications.

     The theme for the summer is all about designing mobile applications. From what I understand the course will involve several mobile applications developed as part of smaller groups and a couple developed as a large group. There will also be several industry experts that will come to discuss innovative ideas in mobile applications to share their thoughts and get feedback from the students.

     This program would count toward my graduation as a 15 credit course which would fulfill the 14 credits that I still need to graduate. I would also fulfill the honours research credit and if I decided to I could take 4 more courses in the fall to graduate with honours. So in terms of program requirements this seems to meet my need perfectly.

     Considering that this is a 15 credit course I assume that it will be very intensive. It will be the only course I will be taking and will probably take at least as much time as working on co-op. I do not believe this will be a problem, since I have done co-op and have worked on intensive group projects like my animation project.

     I am also very interested in working with mobile application and feel that this would be valuable experience to have given their growing popularity. It would also give me more opportunities for possible careers after graduation at are not directly related to games.

     Since I found this program interesting I decided to apply. They liked my application and I was accepted into the program. So this is what I will be doing this summer I am very excited.

Thursday, March 15, 2012

Creativity in Games

     I have already talked about one of the suggestions that I received from my previous job interview. Today I would like to talk about another thing they suggested.

     During the interview they asked me questions about the kinds of video games that I had played and what I liked about them. I had some trouble coming up with reasons because I had not given it much serious thought. I believe that understanding why one likes something is important to think about, not only with respect to games but also, even more generally, with any kind of software. To help me understand why I like the things that I like, I will try to explain some of the aspects of games I like using specific examples of games that demonstrate this aspect well.

     I have liked playing video games all my life and have many reasons I prefer some games over others. Its very difficult to identify a single common element that I like in games but some themes that I like to see in games are creativity, exploration, simulation, expansion and progression. I wont address all of these in this blog but instead will focus on the first and then continue with the others in a future post.

     Unfortunately, since I began university I have played fewer and fewer games. The biggest reason for this is that I have been spending much more of my time working on university projects. Even still I do occasionally play games but I feel that I do not enjoy them as much as I did when I was younger. Maybe I have different interests now then I used to? I think part of the reason I have not played many games lately is that I enjoy making my own games. Making games is a much more creative activity and it is hard to find games that allow one to be creative.

   A perfect exception to this is Minecraft which gives players complete freedom to be creative. Minecraft has no objective and simply encourages the player to gather resources and use those resources to build things. Other games that I have really enjoyed are simulation games like Rollercoaster Tycoon and The Sims. Both of these games require the player to design a virtual world.

    One aspect I like about of these games that promotes creativity is the lack of a well defined objective. All three of these games have no end condition, the player just keeps creating until they decide to stop. In games with a clearly defined objective, game developers would design the features of a game as tools that the player can use to get them to the goal. In games where there is no goal it is often unclear what kinds of features the player will require. Game developers often find designing these kinds of games particularly difficult because they must create features that have many possible uses.

    A good example of a feature with many uses is the piston blocks in Minecraft. The piston block is a block the player can activate remotely and when activated will extend causing any block in its path to be moved. The piston can also be modified so that when it is deactivated it retracts and pulls the block back to its original position. Using this people have created elevators, automatic doors and even giant moving faces in the sides of mountains.

    While a lack of objective has in someway helped these games I often feel like there needs to be some kind of goal in order to validate the quality of the players design. Business simulation games like Rollercoaster Tycoon allow the player to gauge their skill based on how much money they make and how happy the people are that ride their roller coasters. However, Minecraft and The Sims seem to be lacking a validating goal and occasionally I wish there was some kind of goal that I could optionally try to achieve. For example, in Minecraft I built a castle but then realized that there was nothing worth defending the castle against and no way to prove that my castle was designed well.

     Creativity is not only limited to simulation games. I believe that players want to be creative in whatever game they are playing. There are countless examples on the internet of players finding situations where they can be creative in almost any game. Therefore, my suggestion to game developers is to always keep players creativity in mind.

     Well those are my thoughts on creativity. I find this kind of subject very hard to write about because I have many ideas but it is very hard to boil them down into a single argument. I had to rewrite this post several times because I kept going off on a tangent. I plan to think about these ideas over the next couple weeks and hopefully by writing and thinking about them I will further understand and solidify them in my mind.