Speed Issues Resolved

I made use of a profiling tool to identify the major bottlenecks in my code. Two sections were causing most of my problems. The first has to do with my generic Map class.

The Map<T> class is used to represent all the 2-D structures in the game for example the dungeon, visibility map and light maps. For ease of use I had the following code to fill a map structure with an instance of a specific type.

        public void FillMap(Type tileType)


            // Set each tile to the instance of the default tile

            foreach (var location in Locations)

                this[location] = (T)Activator.CreateInstance(tileType);


Now the above code worked nicely, but is dog slow due to the reflection. As a rule I don’t do any premature optimisation, but the general slowness of my AI required me to address this slowness.

The solution was to use an anonymous delegate to pass in the actual new statement of the type to the function. The new code looks as follows

        public void FillMap(Func<T> cellFactory)


            // Set each tile to the instance of the default tile

            foreach (var location in Locations)

                this[location] = cellFactory();


The new function is used as follows

            // Fill the lightmap with black tiles

            FillMap(() => new LightMapTile());

This change gained a 50% speed improvement over the old code. Not too bad.

The next real bottleneck was in the actual AI logic. I have a piece of code that is responsible for identifying threats. The original code made use of a LINQ statement which was incorrect to begin with (the actual logic not the LINQ). The new code simplifies the threat logic into the following.

            // Find all actors that are visible threats

            return (from actor in Dungeon.Actors where IsThreat(actor) && VisibilityMap[actor.Location].IsVisible select actor)


The new code flies and I now have multiple actors running around the dungeon finding each other. Pretty cool.

The next step is to add some evasion logic which should yield actors chasing each other around the map. Thereafter I should look at some simple combat and tweaking the main loop to defer decision making to the player when it’s his turn.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s