Cellular Textures

Cellular textures are another form of procedural content generation. In fact it's probably one of the easier ones out there as there is no smoothing, blurring, etc. All we do is create a bunch of random points within the space, determine which one each pixel is closest to, and based on it's distance to that point we change the color. What this ends up doing is, well this:

As you can see it creates what can best be described as cells. The lines are created where two or more points of interest border each other. As always, I'm certain that you're interested in the code:

Cellular.zip (1.09 kb)

Note that as always this is done in C# but can be converted into another language fairly easily. Anyway, this algorithm is great for various tasks including bump mapping, creating certain textures (scales or a rock path for instance), special effects, etc. There's even uses in AI, world creation, etc. So it's definitely an algorithm to take a look at. As always, download, leave feedback, etc.

kick it on DotNetKicks.com   Shout it
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkListEmail

Posted by: James Craig
Posted on: 3/25/2008 at 10:39 AM
Tags: ,
Categories: Game Programming | Procedural Content
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

Fault Formation

In case you're curious, I'm not talking about seg faults or the ability of wives to create things that are your fault. I'm referring to faults on a landscape where two tectonic plates meet. And yes, this is another one of my procedural content posts. Previously I talked about Perlin Noise a bit and unlike that algorithm, this one is more of a brute force type algorithm. All you do is pick two points along the edge of the height map, draw a line, and pick one side of that line and increase or decrease the values by a given amount. You just continue to do this for a couple thousand iterations and voila, you have some terrain:

As you can see the final image doesn't quite look as good as the Perlin Noise. At the same time it has a tendency to take an extremely long time to create an item due to the number of iterations involved. However if we put the height map through a Gaussian Blur or something along those lines, it usually fixes the "look" issue to some extent. It does however usually create something that looks more like rolling hills when compared to Perlin Noise.

Fault.zip (1.37 kb)

Anyway, here is the basic code to create your own faults. Play around with it though and definately see if you can improve my implementation definately has some flaws and feedback is always appreciated.

kick it on DotNetKicks.com   Shout it
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkListEmail

Posted by: James Craig
Posted on: 3/22/2008 at 11:20 AM
Tags: ,
Categories: C# | Game Programming | Procedural Content
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

Getting Free/Busy Data from an Exchange Server

Ever wanted to know when someone was busy so you could plan a meeting? In outlook you'll notice a nice graph that shows that information when creating a meeting. How can we get that same information on our website? This is actually pretty easy to do and uses OWA to accomplish it. All we need to do is query the Exchange server, get an XML doc that contains that information, parse said XML doc, and voilà we have our info. Without further ado, here's the code:

        public static byte[] GetFreeBusy(string People, DateTime StartTime, DateTime EndTime, int Interval, string UserName, string Password)
        {
            string EmailAddresses = "&u=SMTP:" + People;

            //Make sure to switch this to point to your exchange server
            string Url = "exchangeserver/public/?cmd=freebusy&start=" + StartTime.Date.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss") + "-05:00&end=" + EndTime.AddDays(1).Date.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss") + "-05:00&interval=" + Interval.ToString() + EmailAddresses;

            System.Net.HttpWebRequest Request;
            System.Net.WebResponse Response;
            System.Net.CredentialCache MyCredentialCache;

            byte[] bytes = null;
            System.IO.Stream ResponseStream = null;
            XmlDocument ResponseXmlDoc = null;
            XmlNodeList DisplayNameNodes = null;

            try
            {
                MyCredentialCache = new System.Net.CredentialCache();
                MyCredentialCache.Add(new System.Uri(Url),
                   "NTLM",
                   new System.Net.NetworkCredential(UserName, Password, "")
                   );

                Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(Url);

                Request.Credentials = MyCredentialCache;

                Request.Method = "GET";
                Request.ContentType = "text/xml; encoding='utf-8'";
                Request.UserAgent = "Mozilla/4.0(compatible;MSIE 6.0; " +
                    "Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1)";
                Request.KeepAlive = true;
                Request.AllowAutoRedirect = false;

                Response = (HttpWebResponse)Request.GetResponse();
                ResponseStream = Response.GetResponseStream();

                ResponseXmlDoc = new XmlDocument();
                ResponseXmlDoc.Load(ResponseStream);

                DisplayNameNodes = ResponseXmlDoc.GetElementsByTagName("a:item");

                if (DisplayNameNodes.Count > 0)
                {
                    for (int i = 0; i < DisplayNameNodes.Count; i++)
                    {
                        XmlNodeList Nodes = DisplayNameNodes[i].ChildNodes;
                        foreach (XmlNode Node in Nodes)
                        {
                            if (Node.Name == "a:fbdata")
                            {
                                bytes = new byte[Node.InnerText.Length];
                                for (int x = 0; x < Node.InnerText.Length; ++x)
                                {
                                    bytes[x] = byte.Parse(Node.InnerText[x].ToString());
                                }
                            }
                        }
                    }
                }
                else
                {
                    throw new Exception("Could not get free/busy data from the exchange server");
                }
                ResponseStream.Close();
                Response.Close();
                return bytes;

            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }

 And that's all there is to it. This function will query the Exchange server and return a byte array containing the information. A 0 means not busy, 2 means it doesn't know, and anything else pretty much means busy. Also note that the array is divided into minutes based on the interval you give. So if the start and end time are an hour away and you put it at 10 minute intervals, you'll get an array with the size of 6. Anyway, test out the code, leave feedback, etc.

kick it on DotNetKicks.com   Shout it
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkListEmail

Posted by: James Craig
Posted on: 3/21/2008 at 11:47 AM
Tags: ,
Categories: ASP.Net | C# | Web Design
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed