<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>luminance</title>
	
	<link>http://www.luminance.org</link>
	<description>Programming and Game Development - Kevin Gadd's Personal Blog</description>
	<lastBuildDate>Thu, 17 Sep 2009 08:11:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/luminance/OVxC" type="application/rss+xml" /><feedburner:feedFlare href="http://add.my.yahoo.com/rss?url=http%3A%2F%2Ffeeds.feedburner.com%2Fluminance%2FOVxC" src="http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif">Subscribe with My Yahoo!</feedburner:feedFlare><feedburner:feedFlare href="http://www.newsgator.com/ngs/subscriber/subext.aspx?url=http%3A%2F%2Ffeeds.feedburner.com%2Fluminance%2FOVxC" src="http://www.newsgator.com/images/ngsub1.gif">Subscribe with NewsGator</feedburner:feedFlare><feedburner:feedFlare href="http://www.bloglines.com/sub/http://feeds.feedburner.com/luminance/OVxC" src="http://www.bloglines.com/images/sub_modern11.gif">Subscribe with Bloglines</feedburner:feedFlare><feedburner:feedFlare href="http://fusion.google.com/add?feedurl=http%3A%2F%2Ffeeds.feedburner.com%2Fluminance%2FOVxC" src="http://buttons.googlesyndication.com/fusion/add.gif">Subscribe with Google</feedburner:feedFlare><feedburner:feedFlare href="http://www.pageflakes.com/subscribe.aspx?url=http%3A%2F%2Ffeeds.feedburner.com%2Fluminance%2FOVxC" src="http://www.pageflakes.com/ImageFile.ashx?instanceId=Static_4&amp;fileName=ATP_blu_91x17.gif">Subscribe with Pageflakes</feedburner:feedFlare><feedburner:feedFlare href="http://www.live.com/?add=http%3A%2F%2Ffeeds.feedburner.com%2Fluminance%2FOVxC" src="http://tkfiles.storage.msn.com/x1piYkpqHC_35nIp1gLE68-wvzLZO8iXl_JMledmJQXP-XTBOLfmQv4zhj4MhcWEJh_GtoBIiAl1Mjh-ndp9k47If7hTaFno0mxW9_i3p_5qQw">Subscribe with Live.com</feedburner:feedFlare><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
		<title>Level Up 2009</title>
		<link>http://feedproxy.google.com/~r/luminance/OVxC/~3/nSwf7rXa7Dc/level-up-2009</link>
		<comments>http://www.luminance.org/games/2009/09/17/level-up-2009#comments</comments>
		<pubDate>Thu, 17 Sep 2009 08:08:50 +0000</pubDate>
		<dc:creator>Kael</dc:creator>
				<category><![CDATA[Games]]></category>
		<category><![CDATA[austin]]></category>
		<category><![CDATA[contest]]></category>
		<category><![CDATA[gdc]]></category>
		<category><![CDATA[Inferus]]></category>
		<category><![CDATA[intel]]></category>
		<category><![CDATA[platformer]]></category>

		<guid isPermaLink="false">http://www.luminance.org/?p=837</guid>
		<description><![CDATA[I&#8217;ve been spending the past few days down in Austin, TX at the Austin Game Developers Conference, and having a pretty good time. For anyone who&#8217;s ever been on the fence about going to GDC (either in San Francisco, or in Austin) I wholeheartedly recommend going if you can find a way &#8211; it&#8217;s a [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been spending the past few days down in Austin, TX at the <a href="http://www.gdcaustin.com/">Austin Game Developers Conference</a>, and having a pretty good time. For anyone who&#8217;s ever been on the fence about going to GDC (either in San Francisco, or in Austin) I wholeheartedly recommend going if you can find a way &#8211; it&#8217;s a bit expensive, but an amazing experience.</p>
<p>On a related note, I can now announce that <a href="http://luminance.org/inferusgame"><b>Inferus</b></a> was selected as a winner of <a href="http://software.intel.com/en-us/contests/levelup2009/contests.php">Intel&#8217;s <b>Level Up 2009</b> game competition</a>. I&#8217;m quite pleased with how it turned out and very grateful to Intel for running the contest. You can <a href="http://software.intel.com/en-us/articles/level-up-2009-winners-announced/">check out the list of winners here</a> &#8211; there are some pretty interesting games!</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/luminance/OVxC?a=nSwf7rXa7Dc:pdS_Aqxdyb4:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/luminance/OVxC?i=nSwf7rXa7Dc:pdS_Aqxdyb4:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/luminance/OVxC?a=nSwf7rXa7Dc:pdS_Aqxdyb4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/luminance/OVxC?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/luminance/OVxC/~4/nSwf7rXa7Dc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.luminance.org/games/2009/09/17/level-up-2009/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.luminance.org/games/2009/09/17/level-up-2009</feedburner:origLink></item>
		<item>
		<title>Achievements and player data II</title>
		<link>http://feedproxy.google.com/~r/luminance/OVxC/~3/bXcTdpT4SqU/achievements-and-player-data-ii</link>
		<comments>http://www.luminance.org/gruedorf/2009/09/03/achievements-and-player-data-ii#comments</comments>
		<pubDate>Thu, 03 Sep 2009 12:51:37 +0000</pubDate>
		<dc:creator>Kael</dc:creator>
				<category><![CDATA[Gruedorf]]></category>
		<category><![CDATA[achievements]]></category>
		<category><![CDATA[analysis]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[heatmap]]></category>
		<category><![CDATA[xna]]></category>

		<guid isPermaLink="false">http://www.luminance.org/?p=824</guid>
		<description><![CDATA[In my previous post I gave an introduction to achievements and player data collection. In this post, I&#8217;ll cover the remaining two significant pieces: Services and front-ends. Unfortunately neither of these will be quite as complete as the coverage in the previous post, since I haven&#8217;t completely finished implementing these parts of my achievement system. [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.luminance.org/gruedorf/2009/08/28/achievements-and-player-data-i">In my previous post</a> I gave an introduction to achievements and player data collection. In this post, I&#8217;ll cover the remaining two significant pieces: Services and front-ends. Unfortunately neither of these will be quite as complete as the coverage in the previous post, since I haven&#8217;t completely finished implementing these parts of my achievement system. Whoops!</p>
<h2>Services</h2>
<p>Boy, that&#8217;s a generic heading. Anyway, so your game is collecting important data on gameplay events, and it&#8217;s reporting that data periodically to a web service on your server. Unfortunately, since you haven&#8217;t actually written that service yet, it doesn&#8217;t seem to be working. Odd how that goes, isn&#8217;t it?</p>
<p>The primary issue you have to deal with when building a service to recieve collected data from your game is storing the data. There are other incidental issues &#8211; security, performance, serialization, etc &#8211; but none of them are of any significance if you can&#8217;t find a way to reliably store and access the data. This isn&#8217;t a simple problem, but there are ways to handle it.</p>
<p>Essentially, the biggest issue you have to confront here is that you are going to have a lot of data. You may not have a lot of data now, but you will eventually. On one hand, you could spend weeks and weeks trying to design the perfect solution for storing all of this data &#8211; but doing that doesn&#8217;t actually get your game any closer to shipping, and doesn&#8217;t actually guarantee that you won&#8217;t have scalability issues down the road. On the other hand, if you completely ignore the problem, you risk losing some of the data you&#8217;ve already gathered by discovering significant design issues after you ship. In practice, you probably want to prioritize shipping over the longevity of your data, since (assuming you ship and your game isn&#8217;t terrible) you can always get more data.</p>
<p>As a starting point, I decided to build my services on Google App Engine, since it was relatively easy to get up and running and provides fairly generous quotas for free.</p>
<p><span id="more-824"></span></p>
<p>I built my services in Python, using the provided datastore and JSON modules to handle most of the work. For those unfamiliar with how App Engine works, essentially your process is this:</p>
<ul>
<li>Modify your app.yaml file to specify the URLs of your services and the python scripts that implement them.</li>
<li>Create one or more Python classes to be used for storing your data in the GAE datastore.</li>
<li>Implement your services by creating python WSGI modules to process incoming requests and store the resulting data in the datastore.</li>
</ul>
<p>For those more familiar with traditional databases, working with the App Engine datastore can be a challenge &#8211; its write performance borders on terrible, and the documentation is somewhat sparse. However, the actual API is very easy to use and since you&#8217;re using Python, most of the problems you&#8217;re dealing with are easy to solve. It helps that most of Django is included with the GAE APIs, since that means you have access to all sorts of useful tools like their HTML template module.</p>
<p>So, for the event format I showed in my previous post, the Python class looks like this:</p>
<pre>class Event(db.Expando):
  playerId = db.IntegerProperty()
  sessionId = db.IntegerProperty()
  timeMs = db.IntegerProperty(indexed=False)
  eventType = db.StringProperty()</pre>
<p>Fairly straightforward. Note that I&#8217;m deriving from <strong>Expando</strong> and not <strong>Model</strong> &#8211; to get an idea of what this means you&#8217;ll need to read the GAE documentation, but essentially this lets me add additional, unindexed fields to my instances as necessary. Since I only plan to do most queries based on event type and player ID, this is perfect.</p>
<p>Given that class definition, in my service, I can simply unpack the JSON I recieve from a game client and convert it into one or more Event instances, ready to send to the datastore:</p>
<pre>      playerId = int(body["PlayerId"])
      sessionId = int(body["SessionId"])
      events = body["Events"]

      put_list = []

      for event in events:
        eventData = event["Data"]
        extraArgs = {}

        while eventData:
          if isinstance(eventData, types.DictType):
            d = eventData
            eventData = None
            for k in d.iterkeys():
              value = d[k]
              if k == "Data":
                eventData = value
              elif isinstance(value, types.DictType):
                continue
              else:
                key = ("%s" % (k,)).encode("ascii")
                extraArgs[key] = value
          else:
            extraArgs["eventData"] = eventData
            eventData = None

        evt = Event(
          playerId=playerId,
          sessionId=sessionId,
          timeMs=event["Time"],
          eventType=event["Type"],
          **extraArgs
        )
        put_list.append(evt)

      db.put(put_list)</pre>
<p>The resulting data format doesn&#8217;t allow you to answer every question you could possibly have, but does leave you with a relatively easy to query volume of data in the google datastore. It&#8217;s also very simple, which makes it relatively straightforward to debug failures and make changes.</p>
<p>As mentioned before, the datastore&#8217;s write performance borders on terrible. If you attempt to perform a <strong>put</strong> on multiple objects in sequence, its write performance goes from bad to earth-shatteringly horrible. However, the datastore API allows you to hand it a list of objects, and it will attempt to put them all at once, which significantly improves your performance so that it&#8217;s at least tolerable. If I were using a relational database like MySQL this would be somewhat equivalent to trying to insert all my rows in a single multi-line query to reduce round-trip time.</p>
<p>Also of note are the somewhat ridiculous contortions I do here to convert event data into fields for the Event instance. Essentially, if the incoming event&#8217;s Data field is just one value, I shove it into a single field inside the Event &#8211; but if it&#8217;s a dictionary, I dig through it looking for values and store them into corresponding fields on the Event instance. This ensures that all the values attached to the event end up with corresponding columns in the datastore.</p>
<p>This also illustrates some slightly interesting design decisions: Events don&#8217;t have any global timestamp, nor do they have a sequence number &#8211; all they have is a player ID, session ID, and timestamp relative to the beginning of the session *in game time*. This is mostly a pragmatic decision; I could provide a global timestamp and get some benefits out of it, but the costs associated with that are quite significant, since accurate timestamps interact badly with batching and the general problem of internet latency.</p>
<p>One particularly nasty sticking point is that GAE has fairly rigid limits on how much CPU and &#8216;API CPU&#8217; time you can consume in a given request, or in a given minute, or in a given hour, or in a given day. These limits are <strong>extremely</strong> easy to hit if you are using the datastore in the manner I am &#8211; so it&#8217;s important to make sure that your game isn&#8217;t sending large batches, or your requests will time out and google will become very angry with you for wasting precious CPU milliseconds. In practice, I&#8217;ve found that batch sizes between 6 and 12 reduce network traffic without hitting the built-in GAE limits. Your mileage may vary based on the nature of your data.</p>
<p>It&#8217;s also worth pointing out that the HistoryBlock approach I described in the previous approach is a bit problematic here: You&#8217;re basically forced to store all those position values within a single event, because turning them into multiple events would absolutely destroy the datastore. Unfortunately, it appears that storing a long list of values within a single event <strong>also</strong> absolutely destroys the datastore. Oh well, what can you do? (Other than switch to another hosting provider)</p>
<p>Also note that this doesn&#8217;t account for security, which means you probably can&#8217;t deploy it in production, because people like breaking your services. Oh well!</p>
<p>Now that we&#8217;re all familiar with how frustrating web service development is, let&#8217;s venture into the comforting realm of data analysis:</p>
<h2>Presenting Your Data (Front-ends)</h2>
<p>Mmm, much better.</p>
<p>Now that you&#8217;ve got all this data sitting around in the GAE datastore, what are you going to do with it?</p>
<p>Among the many things you can do, here are a few good ideas:</p>
<ul>
<li>You can perform high level queries against your event data to generate general &#8216;gameplay statistics&#8217; for player profiles.</li>
<li>You can perform statistical analysis of your event data to detect patterns in usage and player behavior.</li>
<li>You can take the position values attached to your event data, and use it to generate heatmaps representing certain events or behaviors &#8211; player movement, player death, creature death, etc.</li>
</ul>
<p>Since I&#8217;m uneducated, I&#8217;m going to skip the statistical analysis and describe the other two.</p>
<p>Creating gameplay statistics from your data is actually relatively easy, as long as you&#8217;re not worried about performance (and right now, you probably shouldn&#8217;t be). Even if performance is an issue, you can often solve this easily by track counters and statistics in a summary table, either by maintaining a count in your service when storing events, or by writing a <a href="http://en.wikipedia.org/wiki/Cron">cron job</a> to periodically update your summaries from the most recently stored events. Doing this will allow you to maintain statistics without having to query your entire event history.</p>
<p>For example, given my event format, I can determine how many times a given player has killed the Drowned Queen by querying the datastore like so:</p>
<pre>player_events = db.Query(Event).filter(
  'playerId =', playerId
)
drowned_queen_kill_events = player_events.filter(
  'eventType =', 'PlayerKilledCreature'
).filter(
  'CreatureName =', 'DrownedQueen'
)
drowned_queen_kill_count = drowned_queen_kill_events.count()</pre>
<p>Essentially, what I&#8217;m doing here is taking my entire event data set, and iteratively filtering it down &#8211; first I start with the player ID, then I filter by the event type. Both of these columns are indexed, and each of these filters will eliminate a huge number of rows. After this I can filter by CreatureName; as long as I don&#8217;t have thousands of PlayerKilledCreature events this will be relatively cheap to do. After that it&#8217;s simple to ask the datastore how many events match my filter.</p>
<p>Given statistics like these, if you choose you can then come up with achievements and other information to display on a player&#8217;s profile page. Players tend to love these things, even though they&#8217;re not directly integral to a game experience. They give players a way to gauge their own skill at a game versus others, and also provide them with interesting &#8216;carrots&#8217; to run after, whether it&#8217;s finding all the secrets in a game or defeating a boss without taking any damage.</p>
<p>So! Now for <a href="http://en.wikipedia.org/wiki/Heat_map">heat maps</a>. If you&#8217;re not familiar with them, you might want to glance at the linked Wikipedia page, but they&#8217;re fairly easy to understand, at least conceptually. You essentially take a large volume of data (in our case, events) and use it to generate a bitmap that you can overlay on an image of a space that you care about. To give a real-world example, if you had a list of recent car accidents, you could generate a heat map from that list and overlay it on a map of your neighborhood, and possibly see clusters around particularly dangerous intersections. You can&#8217;t always rely on a heat map for drawing conclusions, but they make it much easier to spot patterns and trends because our brains are great at analyzing data when it&#8217;s presented in this format.</p>
<p>In my case, I want to take all that HistoryBlock data and convert it into a heat map, so I can figure out where players are spending most of their time. To do this, first I needed to get images of all my levels &#8211; this took some work, since I hadn&#8217;t previously done any offline rendering of my game content. In some cases you can do this sort of thing offline during your build process, but in my case since my renderer relies on Direct3D and XNA, I simply start up an instance of the game engine in a &#8216;batch processing mode&#8217;, and it scans through a level, using the 3D card to generate screen-size &#8217;tiles&#8217; of the level, one at a time, and save them out to disk. After that I scan through the tiles and scale them down to create a &#8216;minimap&#8217; that represents the level at a given magnification.</p>
<p>Once I&#8217;ve got images for each of the levels, I can begin to generate a heat map for them. First, I query a service that pulls down all of the HistoryBlock events from the server and returns them as JSON. This sort of thing is likely to have scalability issues, so in practice you might want to only pull down a subset of your events &#8211; the most recent ones, or a random sampling &#8211; and use them instead. Right now my dataset is small enough that this isn&#8217;t a problem.</p>
<p>Once I&#8217;ve got the list of positions, I can scan over them and create the heat map. Essentially, what you want to do is create a 2D bitmap that is roughly the same size as your minimap, or some even multiple of it. Then, you can through all your positions and map them to points within that bitmap. Every time you successfully map a position to a point, you increment the value at that point, so that the most frequently reported locations have higher values.</p>
<p>Once you&#8217;ve done that, you can compute a minimum, maximum, etc. for the heat map, and use that to translate those &#8216;heat&#8217; values into colors at each point. Then, all you have to do layer the resulting color bitmap onto your mini map, and you have a heat map. Here&#8217;s a few ones I just generated, based on the events I&#8217;ve been recording while testing out my services and achievement system. You may need to click on them to view them at full size:</p>
<p style="text-align: center;"><a href="http://www.luminance.org/wp-content/uploads/2009/09/troupe.png"><img class="aligncenter size-full wp-image-831" title="troupe" src="http://www.luminance.org/wp-content/uploads/2009/09/troupe.png" alt="troupe" width="778" height="185" /></a></p>
<p style="text-align: center;">This heat map was generated from a dozen or so play-throughs of the first level in the Level Up 2009 demo.</p>
<p style="text-align: left;">There are a few interesting conclusions you can draw from this heatmap &#8211; for example, you can easily spot the locations of switches/cranks, because those points are orange or red (from the player tending to stand in front of them to manipulate the object). Because of the short number of playthroughs, you can actually see faint traces of me dying by falling into the water.</p>
<p style="text-align: left;">The data also gives you a general idea of typical paths through the level, and shows you where players spend more time &#8211; for example, the ledge on the right side of the water is much brighter, because the player tends to stand there for a while fighting the monsters that are spawned there.</p>
<p style="text-align: left;">This presents some good opportunities to improve the flow of the level, address difficult platforming challenges, or make it easier for the player to find objectives (keen-eyed observers will note that there&#8217;s a secret passage in this level that I did not enter in any of my playthroughs. (: )</p>
<p style="text-align: center;"><a href="http://www.luminance.org/wp-content/uploads/2009/09/drowned_queen_arena.png"><img class="aligncenter size-full wp-image-830" title="drowned_queen_arena" src="http://www.luminance.org/wp-content/uploads/2009/09/drowned_queen_arena.png" alt="drowned_queen_arena" width="332" height="332" /></a></p>
<p style="text-align: left;">This one was generated from the small arena in which the player fights the boss from the demo, the Drowned Queen.</p>
<p style="text-align: left;">Even a quick glance at this heat map reveals some interesting patterns, despite the fact that it is based on a small amount of data.</p>
<p style="text-align: left;">I rarely died from falling into the water below, which indicates that the water is not particularly effective as a hazard (or that I&#8217;m particularly adept at avoiding it).</p>
<p style="text-align: left;">The extremely bright clusters to the left side indicate that I rarely moved around the arena while fighting the Queen, tending to instead dart back and forth within the same area. This might indicate problems with the design of the level or the boss, since you would usually expect players to be running around in an arena like this while fighting a well-designed foe.</p>
<p style="text-align: left;">The big bright red point up in the top left is aligned perfectly with one of the arena&#8217;s grip plates. While you&#8217;re supposed to use the grip plates to avoid one of the Queen&#8217;s special attacks, the huge amount of time spent there suggests that the boss&#8217;s AI is leading me to remain on the plate for a long period of time, looking for a safe opportunity to drop down, instead of actively fighting her and having a good time.</p>
<p style="text-align: left;">Keen-eyed viewers who&#8217;ve played the demo will also note that there are yellowish regions at each of the locations where a dialogue sequence occurs. This indicates that I tend to stand still while progressing through the dialogue sequence, despite the fact that the player is allowed to move freely. Interesting!</p>
<p style="text-align: center;"><a href="http://www.luminance.org/wp-content/uploads/2009/09/aqueduct.png"><img class="aligncenter size-full wp-image-829" title="aqueduct" src="http://www.luminance.org/wp-content/uploads/2009/09/aqueduct.png" alt="aqueduct" width="695" height="302" /></a></p>
<p style="text-align: center;">This final one is generated from the second level in the demo, the aqueducts.</p>
<p style="text-align: left;">Rather embarassingly, this demonstrates the perils of trying to draw conclusions from player data: Almost all the points in this heatmap are barely visible.</p>
<p style="text-align: left;">Why? Well, most likely, I left the game running with the player standing in that position for a long time, which resulted in that point being far, far brighter than the rest of the level.</p>
<p style="text-align: left;">Of course, there are ways to address these sorts of problems with your data &#8211; but if you&#8217;re not careful, they may influence your conclusions without you even realizing it. In this case I would probably want to discard or ignore any points that have values vastly out of whack compared to the rest of the heat map, so that the heat map will demonstrate general trends instead of only showing outliers.</p>
<p style="text-align: left;">
<p style="text-align: left;">I hope these posts have given you an idea of how you can go about gathering player data in your games, and how you can put that data to work &#8211; both towards improving your game, and giving your players access to data they want.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/luminance/OVxC?a=bXcTdpT4SqU:8RngodWKgLc:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/luminance/OVxC?i=bXcTdpT4SqU:8RngodWKgLc:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/luminance/OVxC?a=bXcTdpT4SqU:8RngodWKgLc:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/luminance/OVxC?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/luminance/OVxC/~4/bXcTdpT4SqU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.luminance.org/gruedorf/2009/09/03/achievements-and-player-data-ii/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.luminance.org/gruedorf/2009/09/03/achievements-and-player-data-ii</feedburner:origLink></item>
		<item>
		<title>Achievements and player data I</title>
		<link>http://feedproxy.google.com/~r/luminance/OVxC/~3/pkhH5cQbbjo/achievements-and-player-data-i</link>
		<comments>http://www.luminance.org/gruedorf/2009/08/28/achievements-and-player-data-i#comments</comments>
		<pubDate>Fri, 28 Aug 2009 09:35:37 +0000</pubDate>
		<dc:creator>Kael</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Gruedorf]]></category>
		<category><![CDATA[achievements]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[xna]]></category>

		<guid isPermaLink="false">http://www.luminance.org/?p=814</guid>
		<description><![CDATA[One of the things I&#8217;ve been working on lately is a way to collect and store data on play sessions, so I can track achievements for players and track where players spend the most time in a level, or where they die the most. There are lots of details to get right for a system [...]]]></description>
			<content:encoded><![CDATA[<p>One of the things I&#8217;ve been working on lately is a way to collect and store data on play sessions, so I can track achievements for players and track where players spend the most time in a level, or where they die the most. There are lots of details to get right for a system like this, but I&#8217;ve at least gotten a prototype working and started experimenting with ways to visualize the data.</p>
<p>For a system like this you have a few important pieces:</p>
<ul>
<li>Your game needs to collect data during play sessions &#8211; in my case, I track important events like player/creature death, and periodically sample the player&#8217;s position to get a general idea of where players are in a given level during their session.</li>
<li>You need a way for your game to periodically report collected data to a remote server. You have to handle lots of edge cases here &#8211; for example, it&#8217;s likely some people will play without an active internet connection or suffer temporary loss of connection, so you need to batch up events to report later in this case &#8211; you definitely don&#8217;t want the game to fall over and choke without access to the internet, and if possible you want to avoid losing data too.</li>
<li>You need to build a set of services to collect data from the game. This typically means you also need services to handle things like uniquely identifying individual play sessions and computers, so that you can track achievements for individual players and perform analysis on individual sessions instead of only on a player&#8217;s entire play history.</li>
<li>You need to build an in-game frontend, to expose collected data to a player. This means turning your event data into user-friendly statistics &#8211; like a kill counter, or an achievement for killing a particular boss. One interesting challenge here is that you probably want to expose this information online, too, so that players can share their profiles and achievements.</li>
</ul>
<p>In this post, I&#8217;m going to cover the first two.</p>
<p><span id="more-814"></span></p>
<h2>Collecting Data</h2>
<p>So, first, you need a way to collect important player data. For it to be useful, you need to collect it in as consistent a format as possible; you don&#8217;t want every piece of data to have a unique set of parameters attached to it, for example, because that would make it impossible to perform any generic analysis of your data. Likewise, you want to try and keep your data &#8216;denormalized&#8217;, by storing as much relevant data together as possible. If you achieve both of these goals, you end up with a stream of &#8216;events&#8217; that represent the things you care about in a form that lets you inspect them individually or analyze them in large groups.</p>
<p>In my case, this step was actually quite straightforward. I already had a simple &#8216;event bus&#8217; integrated into the game, that I was previously using to synchronize animation and sound effects with gameplay. Given this, it was simple to create a data collector that attaches to the event bus and listens for events that it wants to collect.</p>
<p>I did, however, have to make some changes &#8211; many of my events didn&#8217;t expose the information necessary to be useful; for example, the event representing a player death didn&#8217;t contain any information to tell you who (or what) killed the player, so I had to tweak the game code to attach that to the event. Likewise, many other events didn&#8217;t include position information, which made it hard to get meaningful information about them &#8211; knowing that the player grabbed onto a ledge isn&#8217;t particularly useful if you don&#8217;t know *where* or *which ledge*. In some cases, the data collector attached important parameters into the events so that game objects wouldn&#8217;t have to; for example any event coming from the Player automatically has PlayerX and PlayerY parameters attached to it (since the Player is the source of the event, it is simple to extract a position and attach it to the event).</p>
<p>One thing that required some special treatment as well was recording the player&#8217;s position over time &#8211; while attaching position information to events gives you a general idea of the player&#8217;s location, it&#8217;s not very useful for figuring out where players are getting stuck or confused. To address this, I built a simple class that periodically samples the player&#8217;s position and adds it to a list. Every time the list reaches a certain size, its contents are batched up into a single event &#8211; a &#8216;HistoryBatch&#8217; &#8211; and sent to the data collector. This allows me to get fairly high-resolution position data without having to report hundreds of individual events every minute (which would have contained lots and lots of redundant information).</p>
<h2>Reporting Data</h2>
<p>Once you have your player data, you need to report it to your server (and probably store some of it locally, too). I honestly haven&#8217;t tackled the latter part yet, but here&#8217;s how I handled the former:</p>
<p>To handle periodically reporting events, I created a simple &#8216;Event Reporter&#8217; class that collaborates with the data collector. Essentially, the event reporter maintains a queue of pending events, and runs a thread that is responsible for sending batches of events to the remote server once the queue gets large enough. It automatically waits a certain amount of time between requests, and attempts to batch events into groups instead of reporting them one at a time as they occur. This allows me to get relatively low latency (if I want it) but still remain relatively efficient in my use of the network. Whenever the data collector gets an event, it adds it to the event reporter&#8217;s queue.</p>
<p>Once I have a batch of events ready to send to the server, I pack them together into a class that&#8217;s laid out optimally for network transmission. I then use .NET 3.5&#8217;s built in JSON serializer to convert the batch into a blob of JSON data ready to send to my remote server. A typical blob looks like this:</p>
<pre>{
  "PlayerId":xxxx,
  "SessionId":yyyy,
  "Events":[
    {"Type":"ActiveControllerChanged","Time":0,"Data":{
      "IsKeyboard":true,
      "ConnectedGamepads":0,
      "ActiveController":0
    }},
    {"Type":"LevelLoaded","Time":5436,"Data":"troupe"},
    {"Type":"HistoryBlock","Time":7788,"Data":{
      "LevelName":"troupe",
      "PlayerX":[-136,-74,35,192,209,209],
      "PlayerY":[588,875,920,920,920,920]
    }}
  ]
}</pre>
<p>As you can see, events are small when possible &#8211; they simply store the event type and the timestamp at which the event occurred, along with their associated information. In cases where an event has lots of information attached, I send a dictionary, but in some cases I can just send a single value. In the case of a history block, I make sure to send the X and Y coordinates as individual lists, so that I don&#8217;t have to waste time encoding &#8216;PlayerX&#8217; and &#8216;PlayerY&#8217; along with every individual coordinate. A batch of events has a player ID and a session ID attached, which allows me to analyze individual play sessions and track achievements. (Note that both of these IDs are randomly generated on the server, so they&#8217;re anonymous.)</p>
<p>The reporter starts an HTTP POST to send the JSON to the server, and goes to sleep until it&#8217;s finished. If the send fails, the events are left on the queue and the thread sleeps for a while so it can retry sending them later. Once a send completes, the reporter checks to see if the queue is empty &#8211; if the queue is now empty, it goes to sleep until the queue contains items again. If the queue still contains items, it sleeps for a short while and prepares to send another batch &#8211; this allows the event reporter to spend the majority of its time asleep, but still handle large bursts of events without sending a huge blob of events at the server in one POST (since that would be very likely to fail or time out.)</p>
<p>One important detail is that the reporter needs to be able to correctly report events even if the player quits. In my case, the player can quit at any time, and I can end up with a very large number of events in the queue at the time. To deal with this, once the game has finished tearing down, the event reporter is given around 30 seconds to report any remaining events to the server. This usually allows events to get through, but prevents the game from hanging indefinitely on exit (in the event that there&#8217;s some sort of network failure, or the event queue is <strong>really</strong> full). Since this occurs after teardown, the game window is gone so the player doesn&#8217;t get the impression that the game has hung, and since the reporter spends most of its time sleeping, they won&#8217;t see any significant CPU usage either.</p>
<p>The reporter also runs in its own isolated thread and uses its own task scheduler, instead of sharing resources with the game. This increases the likelihood that the game will be able to successfully report events in the case of a bug or crash, and also means that if the reporter fails for some reason &#8211; probably a network outage &#8211; it won&#8217;t take the game down with it.</p>
<p>In my next post, I&#8217;ll cover the remaining two items and show you some of the things you can do once you&#8217;ve gathered player data. In the interim, you can <a href="http://inferus-data.luminance.org/player/view?id=1115">take a look at my player profile</a>!</p>
<hr />
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/luminance/OVxC?a=pkhH5cQbbjo:fDL4zOS2AKQ:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/luminance/OVxC?i=pkhH5cQbbjo:fDL4zOS2AKQ:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/luminance/OVxC?a=pkhH5cQbbjo:fDL4zOS2AKQ:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/luminance/OVxC?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/luminance/OVxC/~4/pkhH5cQbbjo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.luminance.org/gruedorf/2009/08/28/achievements-and-player-data-i/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.luminance.org/gruedorf/2009/08/28/achievements-and-player-data-i</feedburner:origLink></item>
		<item>
		<title>Updating Onscreen Objects / Profiling</title>
		<link>http://feedproxy.google.com/~r/luminance/OVxC/~3/ne51pW7HnKo/updating-onscreen-objects-profiling</link>
		<comments>http://www.luminance.org/gruedorf/2009/08/20/updating-onscreen-objects-profiling#comments</comments>
		<pubDate>Fri, 21 Aug 2009 04:24:39 +0000</pubDate>
		<dc:creator>Kael</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Gruedorf]]></category>
		<category><![CDATA[360]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[profiler]]></category>
		<category><![CDATA[xna]]></category>

		<guid isPermaLink="false">http://www.luminance.org/?p=742</guid>
		<description><![CDATA[One of the problems I started to run into while polishing things up for my contest entry builds was that as my levels grew larger, the game&#8217;s CPU utilization on the 360 steadily grew with them. While PC builds of my game ran smooth on basically all the machines I had access to, on the [...]]]></description>
			<content:encoded><![CDATA[<p>One of the problems I started to run into while polishing things up for my contest entry builds was that as my levels grew larger, the game&#8217;s CPU utilization on the 360 steadily grew with them. While PC builds of my game ran smooth on basically all the machines I had access to, on the 360 the cost of updating all the level&#8217;s objects and entities became quite significant &#8211; most likely due to the 360&#8217;s feeble floating-point performance and lack of out-of-order execution.</p>
<p>The solution to this was, at least to me, relatively obvious: My levels were much larger than the camera, so it didn&#8217;t really make sense to update the entire level every frame.</p>
<p>The first thing I tried to verify this theory was simply hacking it in: Do a check before updating each object to see if it was onscreen. Interestingly, this didn&#8217;t make the game any faster on the 360. Depending on your point of view, this either confirmed or denied my hypothesis: If the problem was simply the cost of all the floating point operations, the cost of doing the onscreen check for each object (since the camera and object bounds were both expressed in floating-point) could have been making the problem worse. Clearly, I didn&#8217;t have enough data to be sure about the right choice to make.</p>
<p>So, I spent a day or so rigging up the necessary infrastructure to be able to profile my game on the 360. Since you can&#8217;t use tools like CLR Profiler or NProf on the 360, I ended up building a very simple frame timing system, and adding an overlay to the game that would show timing data. This let me get a good idea of how much time each subsystem in the game was using, and then I could compare the costs of individual subsystems, and try making changes and seeing how the profile data changed.</p>
<p>Once I had the profiler up and running on the 360, a clear pattern emerged.</p>
<p><a href="http://www.luminance.org/wp-content/uploads/2009/08/01.png"><img class="aligncenter size-full wp-image-745" title="01" src="http://www.luminance.org/wp-content/uploads/2009/08/01.png" alt="01" width="500" height="550" /></a></p>
<p>Updates were consuming a huge amount of CPU time on the 360. While on my desktop, updates basically accounted for no more than 1% of CPU time, on the 360 they actually accounted for more CPU time than rendering &#8211; this was actually a bit of a surprise to me since rendering was definitely the bottleneck at one point on the 360. It seems that at some point along the way, I solved my rendering performance issues on the 360, but didn&#8217;t notice because I had made updates so much more expensive &#8211; one mistake I plan not to repeat was that I went a week or two without testing the game on the 360, since my 360 was not hooked up at the time. During that span of time I made a lot of changes that drastically altered the game&#8217;s performance characteristics, so it was hard to tell what had caused things to degrade.</p>
<p><span id="more-742"></span></p>
<p>Now that I knew updates were expensive, I decided to try and narrow down which object types were the problem. I added profiling markers around specific types of objects, to try and figure out which ones cost the most CPU time to update. After doing that and deploying a few builds to the 360, I found one of my culprits: Water.</p>
<p><a href="http://www.luminance.org/wp-content/uploads/2009/08/04.png"></a><a href="http://www.luminance.org/wp-content/uploads/2009/08/05.png"><img class="aligncenter size-full wp-image-749" title="05" src="http://www.luminance.org/wp-content/uploads/2009/08/05.png" alt="05" width="500" height="550" /></a></p>
<p>While I had suspected that water might be a problem, I was still somewhat surprised by the results; A similar piece of in-game geometry, zones, used similar update code but turned out to have virtually zero update cost at runtime, while water cost a tremendous amount of CPU for very little gameplay impact. The problem turned out to be a subtle difference in how they were implemented.</p>
<p>Zones were inefficient in that every frame, each zone did an obstruction test to see if any entities were inside &#8211; in most cases a zone would be empty, so this was wasted effort. This would have been better implemented by having every entity check for nearby zones, since there are typically far less entities than there are zones. However, in practice this didn&#8217;t turn out to be the problem. The problem was that water built on this logic, and then used it to perform additional work: It did an obstruction test to determine how far the water should fall, and then did another obstruction test to locate any entities inside the water and apply &#8216;flow force&#8217; to them so that the flowing water would push them in a given direction. These two obstruction tests each ended up accounting for a significant portion of the time spent updating the level.</p>
<p>To solve this, I took two steps. First, I reworked zones and water so that they both operated the way I described &#8211; each entity does a check to locate all the zones it&#8217;s within, in a single obstruction test. This reduced the number of obstruction tests I was running every frame by a large amount, and helped reduce CPU usage for water. However, that still wasn&#8217;t enough.</p>
<p>The biggest improvement came from reworking things so that only onscreen objects get updated every frame. Instead of performing a test against every object to see if it&#8217;s onscreen, I decided to build on the partitioning scheme I use for rendering to get a list of onscreen objects, and use that as my list of objects to update. Once I had that working, my performance improved drastically and the game easily ran at 60fps in every part of my levels with CPU to spare.</p>
<p>Of course, doing this introduced bugs. One of the biggest issues is that often you will have important objects just outside the edge of the screen that need to keep updating, like moving platforms or enemies. To solve this, I added two mechanisms:</p>
<p>First, I added a margin around the screen within which objects still continued to update. This meant that an object just barely offscreen would keep updating, and solved the problem of a moving platform or enemy getting left behind as soon as he dropped offscreen.</p>
<p>Second, I built a simple &#8216;update manager&#8217; that maintains a list of all the objects that are currently onscreen. When an object leaves the screen, instead of removing it immediately, the update manager instead sets a timeout, which causes the object to become &#8216;asleep&#8217; within a certain number of frames. As a result, once an object leaves the screen it has a second or two to return to the screen before it falls asleep, which helps with things like moving platforms that are going to move on and off the screen regularly &#8211; since they don&#8217;t spend very long offscreen, they never have a chance to fall asleep.</p>
<p>The update manager also gives me the ability to exclude some objects from updates entirely, by flagging them as &#8216;unable to wake&#8217;, so the update manager knows to never remove them from sleep status. Likewise, it gives me the option to flag objects as &#8216;unable to sleep&#8217; so that they always stay active &#8211; for example, I do this to the player character and his companion to avoid any unintentional bugs that might result from the player remaining offscreen too long (say, during a cinematic).</p>
<p>The update manager has some other benefits, too. Here&#8217;s what it looks like:</p>
<pre>    public class UpdateManager&lt;T&gt;
        where T : class, IUpdateable, IHasBounds {

        public struct Entry {
            public readonly T Object;
            public int WakeCounter;

            public Entry (T obj, int wakeCounter) {
                Object = obj;
                WakeCounter = wakeCounter;
            }
        }

        protected Dictionary&lt;T, bool&gt; _VisibleObjects = new Dictionary&lt;T, bool&gt;(new ReferenceComparer&lt;T&gt;());
        protected UnorderedList&lt;Entry&gt; _Entries = new UnorderedList&lt;Entry&gt;();

        public int SleepTimeout = 30;
        public readonly SpatialCollection&lt;T&gt; Collection;

        public UpdateManager (SpatialCollection&lt;T&gt; collection) {
            Collection = collection;
        }

        public void Update (Bounds liveRegion) {
            using (var e = Collection.GetItemsFromBounds(liveRegion, true))
            while (e.MoveNext()) {
                _VisibleObjects[e.Current.Item] = false;
            }

            Entry currentItem;

            using (var e = _Entries.GetEnumerator())
            while (e.GetNext(out currentItem)) {
                if (!_VisibleObjects.Remove(currentItem.Object)) {
                    // Object not visible

                    if (!currentItem.Object.AllowSleep) {
                        // Object cannot fall asleep
                    } else if (--currentItem.WakeCounter == 0) {
                        // Object fell asleep
                        e.RemoveCurrent();
                        continue;
                    } else {
                        // Object still awake
                        e.SetCurrent(ref currentItem);
                    }
                } else {
                    // Object visible

                    currentItem.WakeCounter = SleepTimeout;
                    e.SetCurrent(ref currentItem);
                }

                currentItem.Object.Update();
            }

            foreach (var newObject in _VisibleObjects.Keys) {
                if (newObject.AllowWake) {
                    _Entries.Add(new Entry(newObject, SleepTimeout));

                    newObject.Update();
                }
            }

            _VisibleObjects.Clear();
        }
    }</pre>
<p>One of the nice things it does in addition to improving performance is that it simplifies my game code &#8211; previously, I had hand-written logic to step through the various object lists (geometry, entities, etc) and update them every frame, and that code differed in subtle ways. The update manager unifies all that, so it kills a lot of duplicated code. Also, by maintaining a unique &#8216;awake objects&#8217; list, it allows me to remove objects from the geometry/entity lists while performing an update, instead of having to wait until the end of the frame, which simplifies some of my entity code as well.</p>
<p>And despite the amount of problems it solves, it ends up actually being quite simple and easy to use. All I have to do to rig up an update manager is point it at a collection of objects and hand it the current camera boundaries every frame.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/luminance/OVxC?a=ne51pW7HnKo:pn_mA7xyC2w:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/luminance/OVxC?i=ne51pW7HnKo:pn_mA7xyC2w:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/luminance/OVxC?a=ne51pW7HnKo:pn_mA7xyC2w:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/luminance/OVxC?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/luminance/OVxC/~4/ne51pW7HnKo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.luminance.org/gruedorf/2009/08/20/updating-onscreen-objects-profiling/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.luminance.org/gruedorf/2009/08/20/updating-onscreen-objects-profiling</feedburner:origLink></item>
		<item>
		<title>Supporting alternate keyboard layouts in XNA games</title>
		<link>http://feedproxy.google.com/~r/luminance/OVxC/~3/S6fXGNtZWJ8/supporting-alternate-keyboard-layouts-in-xna-games</link>
		<comments>http://www.luminance.org/gruedorf/2009/08/14/supporting-alternate-keyboard-layouts-in-xna-games#comments</comments>
		<pubDate>Fri, 14 Aug 2009 20:00:59 +0000</pubDate>
		<dc:creator>Kael</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Gruedorf]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[input]]></category>
		<category><![CDATA[keyboard]]></category>
		<category><![CDATA[xna]]></category>

		<guid isPermaLink="false">http://www.luminance.org/?p=733</guid>
		<description><![CDATA[If you support keyboard input in your XNA game, one issue you need to be aware of is alternate keyboard layouts. Due to the way the XNA Framework handles keyboard input, if you create your game while using the QWERTY keyboard layout, and a customer runs your game while using the DVORAK keyboard layout, he [...]]]></description>
			<content:encoded><![CDATA[<p>If you support keyboard input in your XNA game, one issue you need to be aware of is <a href="http://msdn.microsoft.com/en-us/goglobal/bb964651.aspx">alternate keyboard layouts</a>. Due to the way the XNA Framework handles keyboard input, if you create your game while using the QWERTY keyboard layout, and a customer runs your game while using the DVORAK keyboard layout, he will have to press different keys on his keyboard. At first, this might seem logical &#8211; but consider the typical case:</p>
<p>A customer downloads and installs your game. He&#8217;s running it on a Windows PC. He has his keyboard layout set to DVORAK, because he heard it helps reduce wrist strain. His keyboard still has QWERTY labels printed on it, so he presses &#8216;S&#8217; to type an &#8216;O&#8217;.</p>
<p>He starts your game up, and let&#8217;s say the splash screen says &#8216;Press S to continue&#8217;. He presses S on his keyboard.</p>
<p>Nothing happens.</p>
<p>This is because the OS is remapping the S keystroke into an O. For your XNA game to see an S keystroke, he will have to press the semicolon key (;:) instead.</p>
<p>While users with alternate keyboard layouts are a comparative minority compared to people using the QWERTY keyboard layout, that&#8217;s no excuse for ignoring them. As it turns out, the solution to this problem is pretty straightforward.</p>
<p>What you need to do is find a way to map a keystroke in <strong>your</strong> keyboard layout &#8211; let&#8217;s say QWERTY &#8211; to whatever keyboard layout your customer is running at the time. The Win32 API actually makes this quite simple, so as long as you&#8217;re willing to include some P/Invoke code in windows builds of your game, you can accomplish this in a couple dozen lines of code.</p>
<p>To accomplish this, I use the following helper struct:</p>
<pre>uusing System;
using Microsoft.Xna.Framework.Input;
using System.Runtime.InteropServices;

namespace Labyrinth.Framework {
    public struct LocalizedKeyboardState {
        internal enum MAPVK : uint {
            VK_TO_VSC = 0,
            VSC_TO_VK = 1,
            VK_TO_CHAR = 2
        }

        [DllImport("user32.dll", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)]
        internal extern static uint MapVirtualKeyEx (uint key, MAPVK mappingType, IntPtr keyboardLayout);
        [DllImport("user32.dll", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)]
        internal extern static IntPtr LoadKeyboardLayout (string keyboardLayoutID, uint flags);
        [DllImport("user32.dll", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)]
        internal extern static bool UnloadKeyboardLayout (IntPtr handle);
        [DllImport("user32.dll", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)]
        internal extern static IntPtr GetKeyboardLayout (IntPtr threadId);

        internal const uint KLF_NOTELLSHELL = 0x00000080;

        public struct KeyboardLayout : IDisposable {
            public readonly IntPtr Handle;

            public KeyboardLayout (IntPtr handle) : this() {
                Handle = handle;
            }

            public KeyboardLayout (string keyboardLayoutID)
                : this(LoadKeyboardLayout(keyboardLayoutID, KLF_NOTELLSHELL)) {
            }

            public bool IsDisposed {
                get;
                private set;
            }

            public void Dispose () {
                if (IsDisposed)
                    return;

                UnloadKeyboardLayout(Handle);
                IsDisposed = true;
            }

            public static KeyboardLayout US_English = new KeyboardLayout("00000409");

            public static KeyboardLayout Active {
                get {
                    return new KeyboardLayout(GetKeyboardLayout(IntPtr.Zero));
                }
            }
        }

        public readonly KeyboardState Native;

        public LocalizedKeyboardState (KeyboardState keyboardState) {
            Native = keyboardState;
        }

        public bool IsKeyDown (Keys key, bool isLocalKey) {
            if (!isLocalKey)
                key = USEnglishToLocal(key);

            return Native.IsKeyDown(key);
        }

        public bool IsKeyUp (Keys key, bool isLocalKey) {
            if (!isLocalKey)
                key = USEnglishToLocal(key);

            return Native.IsKeyDown(key);
        }

        public bool IsKeyDown (Keys key) {
            return IsKeyDown(key, false);
        }

        public bool IsKeyUp (Keys key) {
            return IsKeyDown(key, false);
        }

        // Maps a localized character like 'S' to the virtual scan code
        //  for that key on the user's keyboard ('O' in dvorak, for example)
        public static Keys USEnglishToLocal (Keys key) {
            var activeScanCode = MapVirtualKeyEx((uint)key, MAPVK.VK_TO_VSC, KeyboardLayout.US_English.Handle);
            var nativeVirtualCode = MapVirtualKeyEx(activeScanCode, MAPVK.VSC_TO_VK, KeyboardLayout.Active.Handle);

            return (Keys)nativeVirtualCode;
        }
    }
}</pre>
<p>Here&#8217;s how it works: First, at startup, we ask the Win32 API to load up a specific keyboard layout; US English QWERTY. From then on, we can ask the Win32 API to convert a &#8216;virtual key&#8217; from that keyboard layout into a scan code. Once we have a scan code, we can then ask the Win32 API to convert that scan code into the equivalent virtual key for the end-user&#8217;s keyboard layout. Since the XNA Framework uses virtual keys (the Keys enumeration contains virtual key values), this allows us to apply this technique to existing keyboard input code without any significant changes.</p>
<p>So, with this helper struct, you can add support for alternate keyboard layouts like DVORAK with only a couple changes:</p>
<ul>
<li>First, add the helper struct to your game code somewhere so you have access to it.</li>
<li>Replace any uses of the XNA KeyboardState struct with the LocalizedKeyboardState helper struct. If you use some of the more obscure KeyboardState helper methods, you may need to do some work to add them to LocalizedKeyboardState; it only provides IsKeyUp and IsKeyDown.</li>
<li>Figure out whether any of the keys you&#8217;re using should not be remapped based on the current keyboard layout. For example, it&#8217;s common practice for some kinds of games to expose a &#8216;console&#8217; that allows the user to view log messages and enter commands. In a lot of games, you press the tilde (<strong>~</strong>) key to open the console. This is a convenient key since it&#8217;s near the top left corner of a QWERTY keyboard. However, if you allow the keystroke to be remapped to the current layout, non-QWERTY typists will find they have to press some random key on their keyboard to open the console. In this case, you&#8217;ll want to pass a value of <strong><tt>true</tt></strong> for the second, optional parameter to the IsKeyDown/IsKeyUp helper methods:</li>
</ul>
<pre>if (ks.IsKeyDown(Keys.OemTilde, true))
    ShowConsole();</pre>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/luminance/OVxC?a=S6fXGNtZWJ8:QnqIh6TeYjA:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/luminance/OVxC?i=S6fXGNtZWJ8:QnqIh6TeYjA:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/luminance/OVxC?a=S6fXGNtZWJ8:QnqIh6TeYjA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/luminance/OVxC?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/luminance/OVxC/~4/S6fXGNtZWJ8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.luminance.org/gruedorf/2009/08/14/supporting-alternate-keyboard-layouts-in-xna-games/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.luminance.org/gruedorf/2009/08/14/supporting-alternate-keyboard-layouts-in-xna-games</feedburner:origLink></item>
		<item>
		<title>Constant Binding</title>
		<link>http://feedproxy.google.com/~r/luminance/OVxC/~3/q-FwFzqOvIU/constant-binding</link>
		<comments>http://www.luminance.org/gruedorf/2009/08/13/constant-binding#comments</comments>
		<pubDate>Thu, 13 Aug 2009 10:31:28 +0000</pubDate>
		<dc:creator>Kael</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Gruedorf]]></category>
		<category><![CDATA[constants]]></category>
		<category><![CDATA[csharp]]></category>

		<guid isPermaLink="false">http://www.luminance.org/?p=729</guid>
		<description><![CDATA[One of the changes I made in the weeks leading up to my contest deadlines was to pull some of the player-specific combat logic, like that for attack chains, combos, and flinching, out into their own objects. Doing this let me apply that same combat logic to monsters and other entities in the game world, [...]]]></description>
			<content:encoded><![CDATA[<p>One of the changes I made in the weeks leading up to my contest deadlines was to pull some of the player-specific combat logic, like that for attack chains, combos, and flinching, out into their own objects. Doing this let me apply that same combat logic to monsters and other entities in the game world, which cut down on duplication considerably.</p>
<p>However, doing this made it clear that I had some architectural issues to tackle: All of these mechanics were heavily dependent on the <a href="http://www.luminance.org/gruedorf/2009/03/30/changing-constants-at-runtime">tunable constants</a> for the creature in question, which meant I couldn&#8217;t just pull methods and variables out of my entity classes into classes of their own.</p>
<p>To solve the problem of accessing an object&#8217;s constants, I came up with a solution based on reflection. I can define a helper object designed to handle an aspect of an entity&#8217;s mechanics &#8211; for example, a HealthPool object to manage the creature&#8217;s health, along with associated aspects like regeneration. The helper object can define instance variables for the constants it needs access to, like so:</p>
<pre>public class HealthPool {
    public Constant&lt;float&gt; HealthMax = null;
    public Constant&lt;float&gt; HealthPassiveRegen = null;
    public Constant&lt;float&gt; HealthRegenDelayTime = null;
    public Constant&lt;float&gt; HealthRegenRampTime = null;
    public Constant&lt;float&gt; FlinchThreshold = null;
    public Constant&lt;float&gt; FlinchThresholdDecay = null;

    public readonly RuntimeEntity Entity;
    public readonly ITimeProvider TimeProvider;
    public float Health = 0.0f;</pre>
<p>Note that these variables are the same name and type as the actual <a href="http://www.luminance.org/gruedorf/2009/03/30/changing-constants-at-runtime">tunable constants</a> &#8211; the difference is that instead of being static, they&#8217;re instance variables. Doing this allows me to pull a function out of an entity&#8217;s source code without needing to change the way it references particular constants, since the constants have the exact same names as before.</p>
<p>Of course, since these variables default to null, we need some way to fill them in with references to the actual tunable constants we want to use. To do that, we apply reflection:</p>
<pre>public static void BindConstants (object destination, params Type[] sourceTypes) {
    var genericConstant = typeof(Constant&lt;&gt;);
    var destinationType = destination.GetType();
    var destinationFields = new Dictionary&lt;string, FieldInfo&gt;();

    foreach (var field in
        destinationType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy)
    ) {
        var fieldName = field.Name;
        var fieldType = field.FieldType;

        if (!fieldType.IsGenericType || fieldType.GetGenericTypeDefinition() != genericConstant)
            continue;

        destinationFields[fieldName] = field;
    }

    foreach (var sourceType in sourceTypes) {
        foreach (var field in
            sourceType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.FlattenHierarchy)
        ) {
            var fieldName = field.Name;
            var fieldType = field.FieldType;

            if (!fieldType.IsGenericType || fieldType.GetGenericTypeDefinition() != genericConstant)
                continue;

            FieldInfo destinationField = null;
            if (destinationFields.TryGetValue(fieldName, out destinationField)) {
                destinationField.SetValue(destination, field.GetValue(null));
                destinationFields.Remove(fieldName);
            }
        }
    }

    if (destinationFields.Count &gt; 0) {
        var constants = String.Join(", ", (from key in destinationFields.Keys select key).ToArray());
        var types = String.Join(", ", (from type in sourceTypes select type.Name).ToArray());

        throw new InvalidDataException(String.Format("Type(s) {0} do not declare the following constants:\n{1}", types, constants));
    }
}</pre>
<p>What we&#8217;re doing here is pretty simple: We accept a reference to an object that has constants requiring binding, and a list of source types to retrieve constants from. The function operates in two stages: First, we enumerate all the instance variables defined in the target object, and build a list of all the tunable constants it has that need to be bound. After that, we enumerate all the static fields of the provided source types, looking for constants that have names matching those of the instance variables on the target object, binding them where appropriate. After this, we can simply check to see if our list of constants is empty or not &#8211; if it&#8217;s empty, we successfully bound all our constants, and if it&#8217;s not, we know that one or more of the desired constants was missing.</p>
<p>We get a few useful things out of this: First, accepting a list of types allows us to do simple inheritance of constants. If we first check the most-derived type and then the base type of an entity, that allows us to define a &#8216;default value&#8217; for a particular constant, like &#8216;Maximum Health&#8217;, in a base class, and then define a new constant with the same name in the derived type. This also allows us to create &#8216;global defaults&#8217; for a given constant &#8211; for example, if we always put Game at the end of the type list, we can have global game-wide constants for things like physics parameters, and only override them in specific classes if necessary.</p>
<p>Finally, to wire things up, we just need to do a little work in the constructor for our helper object:</p>
<pre>    public HealthPool (RuntimeEntity entity, ITimeProvider timeProvider) {
        Entity = entity;
        TimeProvider = timeProvider;

        ConstantManager.BindConstants(this, entity.GetType(), entity.Game.GetType());

        Health = HealthMax;
    }</pre>
<p>In this case, we&#8217;re initializing the HealthPool using the constants defined in the entity, and falling back to any constants defined in the Game when the entity doesn&#8217;t specify them. If a necessary constant is missing, we&#8217;ll get an exception thrown when constructing our helper object. Once we&#8217;ve bound the constants, we can just use them like we would otherwise &#8211; in this case, the HealthPool automatically initializes itself based on the HealthMax constant.</p>
<p>This is definitely preferable to the approach I used to use for exposing an entity&#8217;s constants &#8211; previously, for important constants like an entity&#8217;s bounding box size, I&#8217;d define an abstract property in a base class, and override it in each derived type to return the value of the constant. Now, I don&#8217;t need to use any abstract members or interfaces; I can just bind to the constants once when I initialize my helper objects.</p>
<p>One thing of note is that since this technique uses reflection, you might run into performance issues if you&#8217;re binding constants repeatedly. This is pretty trivial to solve, however; you can just cache the results of a constant binding operation based on the destination and source types, since those aren&#8217;t going to change at runtime.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/luminance/OVxC?a=q-FwFzqOvIU:Q4Ox-VZQ3r4:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/luminance/OVxC?i=q-FwFzqOvIU:Q4Ox-VZQ3r4:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/luminance/OVxC?a=q-FwFzqOvIU:Q4Ox-VZQ3r4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/luminance/OVxC?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/luminance/OVxC/~4/q-FwFzqOvIU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.luminance.org/gruedorf/2009/08/13/constant-binding/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.luminance.org/gruedorf/2009/08/13/constant-binding</feedburner:origLink></item>
		<item>
		<title>One down</title>
		<link>http://feedproxy.google.com/~r/luminance/OVxC/~3/tA1Ge4fjbqo/one-down</link>
		<comments>http://www.luminance.org/gruedorf/2009/08/06/one-down#comments</comments>
		<pubDate>Fri, 07 Aug 2009 06:44:41 +0000</pubDate>
		<dc:creator>Kael</dc:creator>
				<category><![CDATA[Gruedorf]]></category>
		<category><![CDATA[360]]></category>
		<category><![CDATA[dbp2009]]></category>
		<category><![CDATA[lame]]></category>
		<category><![CDATA[xna]]></category>

		<guid isPermaLink="false">http://www.luminance.org/?p=679</guid>
		<description><![CDATA[One to go.
Thanks to some especially hard work by Troupe and Ian, we got a relatively decent build of the game in for the Dream-Build-Play 2009 deadline. Next is the Intel Level Up 2009 competion, only a few days from now.
I&#8217;m too lazy to write a large blog post this time, since I&#8217;ve been up [...]]]></description>
			<content:encoded><![CDATA[<p>One to go.</p>
<p>Thanks to some especially hard work by Troupe and Ian, we got a relatively decent build of the game in for the Dream-Build-Play 2009 deadline. Next is the Intel Level Up 2009 competion, only a few days from now.</p>
<p>I&#8217;m too lazy to write a large blog post this time, since I&#8217;ve been up for about 48 hours. Instead, enjoy this <a href="http://www.luminance.org/inferusgame">conveniently placed link</a> that allows you to download a build of the game and try it out. Feel free to mess around with the level editor, too.</p>
<p>Biggest things of note from the SVN logs this week:</p>
<ul>
<li>Added a boss fight!</li>
<li>Overhauled my physics system to address some floating point accuracy issues.</li>
<li>Overhauled the combat system to try and make it more fun. Only slightly successful.</li>
<li>Finally implemented the player character&#8217;s companion, and added support for talking to her.</li>
<li>Significantly reduced garbage generation, which means less stuttering on the 360. Hooray!</li>
</ul>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/luminance/OVxC?a=tA1Ge4fjbqo:C2pNlBPpoVE:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/luminance/OVxC?i=tA1Ge4fjbqo:C2pNlBPpoVE:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/luminance/OVxC?a=tA1Ge4fjbqo:C2pNlBPpoVE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/luminance/OVxC?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/luminance/OVxC/~4/tA1Ge4fjbqo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.luminance.org/gruedorf/2009/08/06/one-down/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.luminance.org/gruedorf/2009/08/06/one-down</feedburner:origLink></item>
		<item>
		<title>Home stretch</title>
		<link>http://feedproxy.google.com/~r/luminance/OVxC/~3/IEh9BtQmByc/home-stretch</link>
		<comments>http://www.luminance.org/gruedorf/2009/07/30/home-stretch#comments</comments>
		<pubDate>Fri, 31 Jul 2009 06:21:50 +0000</pubDate>
		<dc:creator>Kael</dc:creator>
				<category><![CDATA[Gruedorf]]></category>
		<category><![CDATA[audio]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[xna]]></category>

		<guid isPermaLink="false">http://www.luminance.org/?p=645</guid>
		<description><![CDATA[In the next two weeks I have deadlines for two different contests coming up, so things are getting pretty hectic. Lots of things changed in the ~100 or so commits since the last blog post, so I&#8217;ll pick a few to describe.

Combo System &#38; Attack Chains
Instance Limiting Overhaul
Active Controller Detection




Combo System &#38; Attack Chains
Previously, combat [...]]]></description>
			<content:encoded><![CDATA[<p>In the next two weeks I have deadlines for two different contests coming up, so things are getting pretty hectic. Lots of things changed in the ~100 or so commits since the last blog post, so I&#8217;ll pick a few to describe.</p>
<ul>
<li><a href="http://www.luminance.org/gruedorf/2009/07/30/home-stretch#section1">Combo System &amp; Attack Chains</a></li>
<li><a href="http://www.luminance.org/gruedorf/2009/07/30/home-stretch#section2">Instance Limiting Overhaul</a></li>
<li><a href="http://www.luminance.org/gruedorf/2009/07/30/home-stretch#section3">Active Controller Detection</a></li>
</ul>
<div class="video"><object width="720" height="430" style="width:720px;"><param name="movie" value="http://www.youtube-nocookie.com/v/8UJ6-5EHRsw&#038;hl=en&#038;fs=1&#038;rel=0&#038;color1=0x2b405b&#038;color2=0x6b8ab6&#038;hd=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube-nocookie.com/v/8UJ6-5EHRsw&#038;hl=en&#038;fs=1&#038;rel=0&#038;color1=0x2b405b&#038;color2=0x6b8ab6&#038;hd=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="720" height="430" style="width:720px;"></embed></object></div>
<p><span id="more-645"></span></p>
<p><a name="section1"></a><br />
<h2>Combo System &amp; Attack Chains</h2>
<p>Previously, combat basically consisted of hitting the punch button over and over to kill monsters. This sucked.</p>
<p>I took a few major steps to address this:</p>
<p>First, I added a second attack type bound to the button I was previously going to use for the grappling hook. This attack is slower and hits heavier, with a wider attack arc, and gives you a nice alternative to the fast, lighter-hitting punch attack.</p>
<p>After the addition of the second attack type, I built on that to add &#8216;combo&#8217; variations of each attack (punch combo, slash combo). The player can combo these attacks onto a previous attack by properly timing another button press near the end of the previous attack animation. Missing the timing (either by pressing too early or too late) &#8216;botches&#8217; the combo and causes the delay before they can perform another attack to be longer than it would be otherwise. The size of the time window in which you can successfully combo an attack decreases every time you combo, so over time it gets harder. This means that simply attacking once will be slow and inefficient, but you are also prevented from comboing attacks indefinitely, which strikes a good balance. The fact that a failed combo has a longer delay than a normal attack means that a player won&#8217;t be punished for choosing to simply combo once or twice and then attack again, since the effectiveness ends up being nearly the same.</p>
<p>Finally, I added a &#8216;attack chain&#8217; system that tracks the number of hits you land on a foe in rapid succession. This allows me to delay the &#8216;flinching&#8217; animation normally played when a creature recieves damage, so that the creature stays within reach of your attacks, allowing you to combo. Each successive hit extends the chain for a short period of time, allowing you to continue landing blows, and when the chain &#8216;breaks&#8217;, all the hits you landed deal their damage to the creature at once, knocking it back and possibly killing it. Chains can span across multiple creatures as well, allowing you to keep multiple enemies &#8216;locked&#8217; by your chain at once. Right now it&#8217;s a bit overpowered, but I think some careful tuning will maintain most of the positive aspects without making the game too easy.</p>
<p><a name="section2"></a><br />
<h2>Instance Limiting Overhaul</h2>
<p>Recently, while working on audio improvements, Troupe ran across a bug in XACT. PC builds of the game ran perfectly without any significant CPU usage problems, and the audio sounded great &#8211; but on the 360, as soon as enough channels of audio started playing, the game&#8217;s framerate tanked to around 15FPS and stayed there indefinitely. This was despite the fact that the game already issues most of its audio calls on a background thread to work around the prohibitively high cost of XACT&#8217;s API calls.</p>
<p>The problem turned out to be that despite the fact I was using XACT&#8217;s built in instance limiting support to control the number of instances playing at once, XACT was struggling to handle the number of cues I was asking it to start at once. Essentially, I was starting all the ambient loop cues for my level at once &#8211; around 30 &#8211; and expecting it to pick the 8 loudest ones to play at any given time based on the instance limit. On PC this worked perfectly without any performance issues, but for whatever reason, not so on the 360.</p>
<p>As a result I basically tore out all the existing code that relied on XACT instance limiting, and reimplemented limiting inside my engine. Luckily this only required changes to about 500 lines of code, but it was still rather frustrating to have to reimplement it when it worked perfectly on PC. On the bright side, now I have more control over how instance limiting behaves, so I at least got something out of it.</p>
<p><a name="section3"></a><br />
<h2>Active Controller Detection</h2>
<p>While doing some testing on my 360 I realized that my approach to handling the 360 controller was incorrect. While I assumed that the player might want to play with any of their four connected controllers, I neglected to notice that most of the XNA Guide APIs (storage device selection, etc) are designed to only respond to input from a single controller. This meant that I needed to detect which controller the player was currently using and make sure to only show XNA dialog boxes using that controller, and that I also needed to detect if that controller became unplugged so that I wouldn&#8217;t attempt to show a dialog box the player was unable to close.</p>
<p>I ended up spending a while changing my input framework so that it would automatically detect disconnected controllers, and inform the game code when a controller had been reconnected. I also did some work to automatically track the active controller, while still handling keyboard input correctly on the PC.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/luminance/OVxC?a=IEh9BtQmByc:tjT64uao5VI:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/luminance/OVxC?i=IEh9BtQmByc:tjT64uao5VI:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/luminance/OVxC?a=IEh9BtQmByc:tjT64uao5VI:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/luminance/OVxC?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/luminance/OVxC/~4/IEh9BtQmByc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.luminance.org/gruedorf/2009/07/30/home-stretch/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.luminance.org/gruedorf/2009/07/30/home-stretch</feedburner:origLink></item>
		<item>
		<title>Event-driven audio</title>
		<link>http://feedproxy.google.com/~r/luminance/OVxC/~3/HK-v-TdpF4Y/event-driven-audio</link>
		<comments>http://www.luminance.org/gruedorf/2009/07/24/event-driven-audio#comments</comments>
		<pubDate>Fri, 24 Jul 2009 19:49:39 +0000</pubDate>
		<dc:creator>Kael</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Gruedorf]]></category>
		<category><![CDATA[audio]]></category>
		<category><![CDATA[content pipeline]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[editor]]></category>
		<category><![CDATA[xact]]></category>
		<category><![CDATA[xna]]></category>

		<guid isPermaLink="false">http://www.luminance.org/?p=625</guid>
		<description><![CDATA[One of the older items on my to-do list was to give my sound designer a way to change the game&#8217;s audio without having to recompile the game in Visual Studio and start it up. Based on some of the improvements I made recently, I was finally able to knock that item off my to-do [...]]]></description>
			<content:encoded><![CDATA[<p>One of the older items on my to-do list was to give my sound designer a way to change the game&#8217;s audio without having to recompile the game in Visual Studio and start it up. Based on some of the improvements I made recently, I was finally able to knock that item off my to-do list.</p>
<p>Below, you can see a short annotated video walkthrough where I demonstrate the technique and show how it integrates with XACT.</p>
<div class="video"><object width="720" height="480" data="http://www.youtube-nocookie.com/v/faHdvcz45lU&amp;hl=en&amp;fs=1&amp;rel=0&amp;color1=0x2b405b&amp;color2=0x6b8ab6&amp;hd=1;hq=1" type="application/x-shockwave-flash" style="width: 720px"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube-nocookie.com/v/faHdvcz45lU&amp;hl=en&amp;fs=1&amp;rel=0&amp;color1=0x2b405b&amp;color2=0x6b8ab6&amp;hd=1;hq=1" /><param name="allowfullscreen" value="true" /></object></div>
<hr />
<p>There are a few key pieces necessary for this to work.</p>
<p><span id="more-625"></span></p>
<p>First, I need a way to get updated audio into the game. It turns out this is pretty simple &#8211; if you change the settings in your XACT project, you can get it to build output files into a folder of your choice. At that point, all you need to do is change your game&#8217;s XACT code to be able to load from that location.</p>
<p>Second, I need a way to pull information out of the XACT datafiles that I can use to attach cues to events. Since the XACT API provided by the XNA Framework is basically useless for this purpose, I ended up solving this problem by loading up the raw datafiles and pulling the names of my cues out of the datafiles. Yes, it&#8217;s disgusting. But it works! Luckily, the cue names are right at the end of the data files, and they&#8217;re null-terminated, so it&#8217;s not difficult to read them. It&#8217;s beyond my understanding why the framework developers opted not to provide the information.</p>
<p>Third, I needed a way to broadcast events from objects in my game world. The solution I ended up building for this was essentially an improved version of the event framework that I previously used for handling input events, like button presses. The framework allows me to &#8217;subscribe&#8217; to various types of events, either for a specific object, or for all objects that can broadcast that event. The ability to subscribe to all objects inexpensively gives me a straightforward way to say things like &#8216;whenever any creature gets hit by a rocket, play an explosion sound&#8217;, which is important.</p>
<p>Fourth, I needed a way to expose event information in an understandable manner, so that it would be easy to figure out what events are occurring in-game and how to attach sounds to them. I solved this by creating a simple &#8216;event overlay&#8217; that shows a list of the most recently fired events, and highlights objects when they broadcast events. This allows you to simply play the game with the overlay open and look for things that are missing sound effects &#8211; once you find something, just look at the log to find out the name of the event.</p>
<hr />
<p>If you&#8217;re interested, you can check out the source code and automated tests for the event system <a href="http://code.google.com/p/fracture/source/browse/trunk/Squared/Util/EventBus.cs">here, on Google Code</a>. In the future I will be releasing the rest of my audio framework as open-source for people to use. Please don&#8217;t hesitate to leave a comment if you have any questions; I&#8217;d be glad to help explain more about how this technique works.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/luminance/OVxC?a=HK-v-TdpF4Y:DHNwUSBaLtk:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/luminance/OVxC?i=HK-v-TdpF4Y:DHNwUSBaLtk:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/luminance/OVxC?a=HK-v-TdpF4Y:DHNwUSBaLtk:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/luminance/OVxC?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/luminance/OVxC/~4/HK-v-TdpF4Y" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.luminance.org/gruedorf/2009/07/24/event-driven-audio/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.luminance.org/gruedorf/2009/07/24/event-driven-audio</feedburner:origLink></item>
		<item>
		<title>Simple Dynamic Music with XACT</title>
		<link>http://feedproxy.google.com/~r/luminance/OVxC/~3/xFnfqpsXgKY/simple-dynamic-music-with-xact</link>
		<comments>http://www.luminance.org/code/2009/07/23/simple-dynamic-music-with-xact#comments</comments>
		<pubDate>Thu, 23 Jul 2009 08:16:26 +0000</pubDate>
		<dc:creator>Kael</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[audio]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[music]]></category>
		<category><![CDATA[xact]]></category>
		<category><![CDATA[xna]]></category>

		<guid isPermaLink="false">http://www.luminance.org/?p=604</guid>
		<description><![CDATA[Good music adds a lot to a game, but only if it fits the action occurring on screen. Badly chosen music that doesn&#8217;t match the feel of your game, or is inappropriate for the tone of the gameplay, won&#8217;t do you much good. Dynamic music is one way to help make sure your music is [...]]]></description>
			<content:encoded><![CDATA[<p>Good music adds a lot to a game, but only if it fits the action occurring on screen. Badly chosen music that doesn&#8217;t match the feel of your game, or is inappropriate for the tone of the gameplay, won&#8217;t do you much good. Dynamic music is one way to help make sure your music is always a good fit for the action occurring on-screen &#8211; it lets you adapt the music the player is hearing based on what&#8217;s happening.</p>
<p>There are various approaches for building dynamic music &#8211; some games go so far as to create hundreds of short music &#8217;snippets&#8217; and string them together to create longer, more dynamic songs that never sound the same to the player once. Unfortunately, this is pretty complicated to do, especially if you&#8217;re just working with XNA and XACT. Luckily, there are some simpler techniques you can use to build dynamic music.</p>
<p>Below, you can see a short video clip showing one of my experiments with dynamic music. I&#8217;ll explain how it works in this post, so you can apply the technique to your games. In this video clip, I have two music tracks playing, and control the volume of one of them depending on whether or not the player character is within the blue rectangle. You can hear the track fading in and out as he moves.</p>
<p></p>
<div class="video"><object width="640" height="385" data="http://www.youtube-nocookie.com/v/7RUG7tDMUL8&amp;hl=en&amp;fs=1&amp;rel=0&amp;color1=0x2b405b&amp;color2=0x6b8ab6&amp;hd=1" type="application/x-shockwave-flash"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube-nocookie.com/v/7RUG7tDMUL8&amp;hl=en&amp;fs=1&amp;rel=0&amp;color1=0x2b405b&amp;color2=0x6b8ab6&amp;hd=1" /><param name="allowfullscreen" value="true" /></object></div>
<p><center><i>(The track used in this example is &#8216;<a href="http://ftp.df.lth.se/pub/scene.org/music/groups/mephtik/mephtik_2/skyline-corn_fields.it">Corn Fields</a>&#8216; by <a href="http://www.traxinspace.com/profile/skyline">Skyline</a>.)</i></center></p>
<p></p>
<p>The  approach I&#8217;m using works like this: You take a song that you intend to use in your game, and in an audio tool, split it out into multiple wave files, one for each &#8216;track&#8217; that you want to have control over. Depending on the complexity of your song, you might just want to split it out into a pair of tracks, and fade one of them in when you want the music to get more intense. If your music is more sophisticated and your gameplay has lots of variation, you may want lots of tracks that you can control individually based on what&#8217;s happening &#8211; fade in some anxious-sounding strings when powerful monsters are on screen, and temporarily fade in some ominous-sounding bass notes when the player is injured, for example.</p>
<p><span id="more-604"></span></p>
<hr />
<p>In this article I&#8217;m going to be using two simple tracks I exported from a song using ModPlug tracker. This is about as simple as you can get, since I just wanted to grab a song I had lying around that I knew I could use without violating any copyright laws. To create the tracks, I went into MPT and loaded up the song. Then, I went into the pattern editor and muted part of the channels, and exported the song to a WAV. After that, I unmuted those channels and muted the remaining channels, and exported them to a second WAV file. This gives me two WAV files of equal length, each of which composes a portion of the song.</p>
<p>Now, to actually wire it up, you have to go into XACT and load up your audio project. You&#8217;re going to want to find the wave bank you keep your music in, and open it up. Once it&#8217;s open, select the tracks for your song and drag-drop them into the wave bank. Afterwards, you should have one entry in the wave bank for each track, as you see below.</p>
<p><a href="http://www.luminance.org/wp-content/uploads/2009/07/xactmusic_01.png"><img class="aligncenter size-full wp-image-605" title="xactmusic_01" src="http://www.luminance.org/wp-content/uploads/2009/07/xactmusic_01.png" alt="xactmusic_01" width="750" height="213" /></a></p>
<p>Next, open up the sound bank that contains your music cues. This probably has the same name as your wave bank for music, but if it doesn&#8217;t, no big deal.</p>
<p><a href="http://www.luminance.org/wp-content/uploads/2009/07/xactmusic_02.png"><img class="aligncenter size-full wp-image-606" title="xactmusic_02" src="http://www.luminance.org/wp-content/uploads/2009/07/xactmusic_02.png" alt="xactmusic_02" width="727" height="494" /></a></p>
<p>Now, drag one of your tracks out of your music wave bank, and drop it into the bottom portion of your sound bank, where the cues are listed. This will create a cue and associated sound for that track.</p>
<p><a href="http://www.luminance.org/wp-content/uploads/2009/07/xactmusic_03.png"><img class="aligncenter size-full wp-image-607" title="xactmusic_03" src="http://www.luminance.org/wp-content/uploads/2009/07/xactmusic_03.png" alt="xactmusic_03" width="544" height="561" /></a></p>
<p>The cue and sound are automatically named after the track, which probably isn&#8217;t ideal, so you can rename both of them to something easier to remember &#8211; this will make it easier to work with them in your game code. After you&#8217;ve done that, select the sound for your music up in the top section of the sound bank. You should see your track on the right side of the window, listed as &#8216;Track 1&#8242;.</p>
<p><a href="http://www.luminance.org/wp-content/uploads/2009/07/xactmusic_04.png"><img class="aligncenter size-full wp-image-608" title="xactmusic_04" src="http://www.luminance.org/wp-content/uploads/2009/07/xactmusic_04.png" alt="xactmusic_04" width="539" height="353" /></a></p>
<p>Next, you want to drag your remaining tracks out of your wave bank, and drop them directly onto the empty space below your track, in the top right portion of the sound bank window. This will create additional tracks for them, as a part of the sound.</p>
<p><a href="http://www.luminance.org/wp-content/uploads/2009/07/xactmusic_05.png"><img class="aligncenter size-full wp-image-609" title="xactmusic_05" src="http://www.luminance.org/wp-content/uploads/2009/07/xactmusic_05.png" alt="xactmusic_05" width="601" height="387" /></a></p>
<p>You&#8217;re all done here, so go ahead and close your wave and sound banks. Next, you need to create a variable to control the volume of your tracks. Go to the treeview on the left side of the XACT window, and right click the &#8216;Global&#8217; section under &#8216;Variables&#8217;. Select &#8216;New Global Variable&#8217;.</p>
<p><a href="http://www.luminance.org/wp-content/uploads/2009/07/xactmusic_06.png"><img class="aligncenter size-full wp-image-610" title="xactmusic_06" src="http://www.luminance.org/wp-content/uploads/2009/07/xactmusic_06.png" alt="xactmusic_06" width="250" height="172" /></a></p>
<p>In the window that opens, choose a name for your variable &#8211; I chose &#8216;MusicTrackVolume&#8217; &#8211; and then change its &#8216;Control&#8217; to &#8216;Monitored&#8217;. Go ahead and close the variable settings window after this.</p>
<p><a href="http://www.luminance.org/wp-content/uploads/2009/07/xactmusic_07.png"><img class="aligncenter size-medium wp-image-611" title="xactmusic_07" src="http://www.luminance.org/wp-content/uploads/2009/07/xactmusic_07-300x141.png" alt="xactmusic_07" width="300" height="141" /></a></p>
<p>Next, right click the &#8216;RPC Presets&#8217; section in the left side of the window, and select &#8216;New RPC Preset&#8217;.</p>
<p><a href="http://www.luminance.org/wp-content/uploads/2009/07/xactmusic_08.png"><img class="aligncenter size-full wp-image-612" title="xactmusic_08" src="http://www.luminance.org/wp-content/uploads/2009/07/xactmusic_08.png" alt="xactmusic_08" width="221" height="200" /></a></p>
<p>A rather intimidating window will open up. The first few things you want to do in this window are:</p>
<ul>
<li>Select your variable in the &#8216;Variable&#8217; dropdown at the top.</li>
<li>Then, in the same row, change the &#8216;Object&#8217; dropdown to point at the track for which you wish to control volume.</li>
<li>Finally, change the &#8216;Parameter&#8217; dropdown to &#8216;Volume&#8217;.</li>
</ul>
<p><a href="http://www.luminance.org/wp-content/uploads/2009/07/xactmusic_09.png"><img class="aligncenter size-full wp-image-613" title="xactmusic_09" src="http://www.luminance.org/wp-content/uploads/2009/07/xactmusic_09.png" alt="xactmusic_09" width="779" height="150" /></a></p>
<p>Once this is done, you need to set up the volume ramp, so that XACT knows how to adjust the volume of your track based on the value of the variable. To keep things simple, go ahead and set two points for the ramp:</p>
<ul>
<li><strong>MusicTrackVolume</strong> = <tt>0.00</tt>, <strong>Volume</strong> = <tt>-96.00</tt> (dB)</li>
<li><strong>MusicTrackVolume</strong> = <tt>1.00</tt>, <strong>Volume</strong> = <tt>0.00</tt> (dB)</li>
</ul>
<p>This specifies that when your variable is set to 0.0, the volume will be set to -96.0 dB, basically silent, and when it is set to 1.0, the volume will be set to 0 dB &#8211; full volume.</p>
<p><a href="http://www.luminance.org/wp-content/uploads/2009/07/xactmusic_10.png"><img src="http://www.luminance.org/wp-content/uploads/2009/07/xactmusic_10.png" alt="xactmusic_10" title="xactmusic_10" width="770" height="348" class="aligncenter size-full wp-image-614" /></a></p>
<p>Once this is done, you can close the RPC Preset window, and rename your new preset to something easier to identify, like &#8216;Music Volume Preset&#8217;. After that, open your sound bank back up. Drag your RPC preset out of the left side of the XACT window, and drop it directly onto the entry for your music in the top left portion of the sound bank window (the sounds portion, not the cues portion). </p>
<p>After you do this, your sound should be highlighted, and the bottom left portion of the XACT window (the properties viewer) should show your preset under &#8216;Attached Objects&#8217;. We&#8217;re done in XACT now, so you can save everything and close it if you like.</p>
<hr />
<p>Once you have everything set up in XACT, you can use your cue as you normally would in-game. By default, you will only hear one of your tracks, because your variable&#8217;s value defaults to zero.</p>
<p>To make it so the player can hear the track, all you have to do is provide a value for your variable. You do this by using the SetGlobalVariable method of the XACT AudioEngine class, like so:</p>
<pre>AudioEngine.SetGlobalVariable("MusicIntensity", 0.8f);</pre>
<p>Given this, it&#8217;s straightforward to set up your game code so that XACT variables change in response to in-game events. Note that variables change immediately after you set them, so your volume changes will be abrupt and somewhat disconcerting. The best way to solve this is to interpolate your variable changes, something like this:</p>
<pre>float fadeElapsedTime = (gameTime.TotalGameTime.TotalSeconds - FadeStartedWhen);
float musicIntensity = MathHelper.Clamp(fadeElapsedTime / FadeDuration, 0.0f, 1.0f);
AudioEngine.SetGlobalVariable("MusicIntensity", musicIntensity);</pre>
<p>Doing that every frame will give you smooth transitions between values, so that the player hears tracks fade in and out instead of immediately stopping and starting.</p>
<p>It&#8217;s also important to note that since the relationship between volume and decibels (dB) is logarithmic, your tracks will sound like they are fading in very quickly. The best way to solve this is by creating a more advanced curve for your RPC Preset, so that the fading sounds smoother. Here&#8217;s the one I used in my example video clip above:</p>
<p><a href="http://www.luminance.org/wp-content/uploads/2009/07/xactmusic_12.png"><img src="http://www.luminance.org/wp-content/uploads/2009/07/xactmusic_12.png" alt="xactmusic_12" title="xactmusic_12" width="696" height="343" class="aligncenter size-full wp-image-620" /></a></p>
<p>As you can see, I partially counteract the logarithmic nature of the dB scale by making the volume ramp up rapidly from <tt>-96</tt>dB to <tt>-32</tt>dB in the first quarter of the scale, and then ramp up more slowly from there to <tt>0</tt>dB. For your needs, a more complex curve might be appropriate.</p>
<p>I hope this post has given you some ideas about how to integrate dynamic music into your own XNA games. Please don&#8217;t hesitate to comment if you have any questions!</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/luminance/OVxC?a=xFnfqpsXgKY:99m3UPqBH24:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/luminance/OVxC?i=xFnfqpsXgKY:99m3UPqBH24:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/luminance/OVxC?a=xFnfqpsXgKY:99m3UPqBH24:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/luminance/OVxC?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/luminance/OVxC/~4/xFnfqpsXgKY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.luminance.org/code/2009/07/23/simple-dynamic-music-with-xact/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://www.luminance.org/code/2009/07/23/simple-dynamic-music-with-xact</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic page generated in 2.485 seconds. --><!-- Cached page generated by WP-Super-Cache on 2009-11-09 14:32:34 -->
