Procedural Land Creation with JRuby, Slick2D and Perlin Noise

For something different, I decided I would try my hand at doing some game development programming. I was incredibly inspired by many of the indie games I watch being actively developed, and I figured it would be a interesting way to learn some brand new things totally outside of my usual purview.

I wanted to do some more work with JRuby, as I had a good hack with it at work, but haven't touched it in a while, so I hunted down which framework for doing a game. I've decided I wanted to do a desktop based 2D something (no idea what still), so settled on Slick2D as my framework of choice.

I've starting leaning towards doing something open world-ish, which meant I needed to generate out a large chunk of landscape without having to hand build a map, and yet also make sure it was the same every time a user came back to a particular world. That lead me to start investigating Perlin Noise, and I settled on the perlin_noise ruby library for generating that, and so far it's been very good.

It's been interesting too, having JRuby as the platform. It's a tough choice deciding whether to use a Java library or a Ruby one. With Slick2D it was easy – it was the best there was, and it hooks straight into OpenGL. With something like a Perlin Noise library, ease of use was the primary concern, and managing gems with Bundler and having a direct line to a nice Ruby syntax based API was the deal clincher. Although if it ever becomes a performance concern, I may look to go back to straight Java.

So here is a video of what I have so far. It's still very raw, but basically it's a circular big world, that if you go one way for long enough, you come back right where you started.

 

I'm extending Slick2D's Shape class to create my world object (basically a circle that I modify with my generated perlin noise). I've already set it up so the world has a "coordinate", and that controls what the world looks like. In theory, if this ever becomes a real game, you could give someone else your world coordinates and they would see the same world you do.

Before anyone asks, I've not decided what this is going to be, or if I'll open source it, or what (it's a bit of a sandpit at the moment), but I'll keep writing a diary here of what I've been doing with it, with appropriate links as long as it keeps staying interesting.

Got a few ideas on where to go next (zoom in and out, dynamic texturing of the landscape, etc), but haven't committed yet. This stuff is pretty interesting, and very different from the sort of stuff I code on a day to day basis.

Let me know if you find this interesting, and I'll keep writing about it!

Simplify Threaded Processing with FutureThreadedWorker

Having worked with both Groovy, (J)Ruby and, Java and (to a much smaller degree) Clojure, I can pretty easily claim that ColdFusion's threading and distributed processing capabilities.. leave a little to be desired. (Okay, actually a whole lot).

There are solutions though – using a job queuing system like Amazon SQS, IronMQ, or if you are looking for a something more threaded, dig into Marc Esher's awesome cfconcurrent project.

But sometimes you just want to take an array of values and run them through some sort of processing in a threaded manner, without having to jump onto any other projects or infrastructures, and without having to write that nasty <cfthread> create and join pattern for the millionth time.

So I wrote a quick CFC I've called 'FutureThreadedWorker', which does exactly that, without half the code you would otherwise need. (Note: this was written for ColdFusion 9. It would be a bit nicer if I could use closures)


<cfscript>
   //create, passing in an array for the array of structures (which are the arguments passed to the workerMethod)
   //then pass it the worker to call the worker method on
   //tell it how many thread it's should use
   //finally, tell it the name of the method to invoke.
   var future = new services.util.FutureThreadedWorker(queue, this, 5, "doWork");

   //start the running

   future.run();

   //if I want the results, wait for them.

   var results = future.get();

   //if i want to check for errors

   var errors = resuls.getErrors();
</cfscript>

Bam. That's it.  The FutureThreadedWorker now loops around the passed in array, and passed in the arguments to the method specified on the worker without you having to worry about creating threads or joining them back up again. I think you'll find that a lot easier than using <cfthread>.

cf.Objective(ANZ) is only a Month Away!

Wow, time flies!

Seems like only yesterday I was doing the closing remarks on the last cf.Objective(ANZ), and now it's already around the corner.

This year is going to be great once again, with lots of top notch speakers and a program that is going to be really interesting as well!

The sessions that I'm personally looking forward to are:

Functional Programming with Clojure & CFML – the best of both worlds?
I'm personally getting more and more into functional programming (or even just writing my OO code with more of a functional style). It's definitely something that is picking up steam all over the place, so I'm keen to see what Kai has to say.

Railo CLI
I'm doing a lot of (J)Ruby work these days, and I'm pretty much living in the console with the great tooling it has in this department. I'm really keen to see what advancements that Railo has made in this area, and see if there are opportunities there for cross pollination.

Building the Olympics
This should have some very interesting war stories of a site that is truly web scale.

Using nginx With ColdFusion
I've always been intrigued by nginx as I've seen it used in many places, but never looked into it itself. I know people that swear by it, so I'll be interested to learn more about nginx.

The Ugly Truth about Frameworks
This is just going to be fun 🙂

What sessions are you guys looking forward to?

Git Workflow: The Case of the Lost Refactor

This has happened to me more than once, so I've been thinking about how to organise my branching and merging to allow for this scenario.

  1. I get my ticket to do some new feature on the application I'm working on.
  2. I create a feature branch, usually in the format of feature/(ticketNumber)-(title) (Yeah, we follow A successful Git branching model)
  3. I think to myself "I've got some time to get this done, I'll take the opportunity to refactor this code so it's nicer, in the process of getting this new feature done" (Insert "Upgrade library/framework" here as well).
  4. So I start refactoring, and continue on working on my new feature…
  5. Until I get a: "Hey Mark, we have changing priorities/emergency/we have a better idea/never-mind, so backburner/kill/delay/switch to working on this totally new thing over here".
  6. So I go off and start a whole new feature, and get that done instead.
  7. Later on, I come to do something else in the area I was working in previously, and realise the refactoring work I was doing previously would be super useful. However all of it is tied in with other half finished code in my previous feature branch. That sucks, because it would make what I'm working on a million times easier.

I call it the "Lost Refactor", and it drives me nuts, as you know you could be working on better code, but it's all attached to other code that you would have to manually pull apart.

So what do you do? I have two thoughts on the matter:

  1. Separate the refactoring into a separate ticket, and build a separate feature branch for it.
  2. Create a refactor/(ticketNumber)-(title) branch, specifically for the refactoring work, and merge that into my feature branch.
  3. Make sure my commits are clean enough so that I can cherry pick out which ones are pure refactor, and which ones are the partial implementations of the new feature.

I've yet to try out option #2, but it seems to me that that would be the best approach.

Technically you could claim that the refactoring would be a totally separate ticket – but that seems like more trouble than it's worth from a pragmatic standpoint. However, the refactoring is a separate effort than the ticket, which means it's nice to split off to it's own branch, in case you want to integrate that work separately. Having the refactor/ branch be the same as the feature/ branch name, it also gives you a really good idea of what the original impetus for the refactoring came from.

Option #3 is not a bad one – and probably the easiest from the development workflow (not having to switch branches around), but the idea of cherry picking commits makes me shudder a bit. I guess it depends how often you end up in a "lost refactor" type scenario, that if it was rare enough, doing the occasional cherry pick is not the end of the world.

What does everyone else do? Do you find yourself in this situation often? What approach do you take?

ColdFusion 10 with Ubuntu Upstart

Setting up ColdFusion 10 on an Ubuntu Server, and as per usual, the installation option to start up ColdFusion when the server starts does not work on Linux.

So I wondered how hard it would be to set this up using Upstart, and it was surprisingly easy with a few bit of trial and error.

This is what I would eventually came up with:

Unfortunately, I would like this to respawn if it somehow dies, and it doesn't look like this style of setting up will work for that.

Anyone else tried this? What did you come up with?

cf.Objective() 2012 Slide Decks

In case anyone would like access to them, here are my slide decks for the two presentations that I gave at cf.Objective() this year.

The first one is Getting closure on Closures – this can be run directly from ColdFusion 10, as a html slide deck which includes the running code.

The second one, A/B Testing with Squabble can also be found online. I made this pubic because Sean Corfield asked me to, but in all honesty, if you weren't at the presentation, it probably won't make much sense to you. Pictures of Storm Troopers in front of computer screens are not particularly descriptive.

Overall cf.Objective() was a great confidence, yet again, much kudos to those that organised the event!

ColdFusion 10 and Closures: The Sesame Library

Writing my presentation for cf.Objective(), Getting closure on Closures, I found I was writing a few common utility functions for use with Closures to use and show in my talk, and as general utilities for coding.

I wanted to bundle these functions into a project, and put it on Github so that we could all share various utility functions that would be useful now that ColdFusion has got Closures available to us. I went looking for an existing project that was already doing this, but since I couldn't find one, I figured I would start one, and see what happens from there!

The project's name is Sesame (much thanks to Ben Koshy for the name), and currently only has a few functions which can be cfincluded into your code, but they were the ones I found most useful when doing some recent work with Groovy, and wanted to have them on hand when doing similar things in ColdFusion.

Here are a few examples:

From the collections include, the _collect functions allows you collect values from one array or struct, by iterating a closure over the passed in collection, and collecting the results returned from that closure.

For example, if I wanted to get all the names from an array of Employees, I could do the following:


var employees = listEmployees();
var names = _collect(employees, function(employee) { return employee.getName(); } );

This would then give me an array of all the employees name in the list.

My other favourite, is _groupBy(), which gives you an easy way to group an array or struct of items into a structure of items that have been grouped by a value returned from a closure.

For example, I could easily group an array of employees by their salary, like so:


var employees = listEmployees();
var grouped = _groupBy(employees, function(employee)
              {
                 if(employee.getSalary() < 100)
                 { return "Less Than 100"; }
                 else if(employee.getSalary() > 100 && employee.getSalary() < 200)
                 { return "Greater than 100, Less than 200"; }
                 else
                 { return "Greater than 200"; }
              });

From there, I can now get at all the employees that earn less than 100, by referenceing them as grouped["Less Than 100"], and so on.

From the numbers include, I really like the _times() function. Simply put, it executes a closure a certain number of times!

_times(6, function() { writeOutput("Write me 6 times!"); });

Nothing too complicated there, but it can be quite useful.

From the concurrency include, there is a function that was directly inspired by the GPars Groovy library (and awesome concurrency library for Groovy). _eachParrallel() will execute the closure for each item in an array or struct, but execute the closure in its own cfthread implementation, allowing for an easy way to parallel process data. When it is done, it joins all the threads back up again, so there is no need for you to do deal with managing the threads yourself.

For example, summing up each item in an array, each in it's own thread:


var values = [1, 2, 3, 4, 5];
var total = 0;
_eachParallel(values, function(it) { total += it; });

Gives us a total of 15.

If you find this interesting, please feel free to download, use and also contribute. I know there are a million more functions that could yet be implemented, and I've already implemented the automatic documentation generator – so as long as your functions have appropriate hinting, it becomes super easy to regenerate the README.md file! So no hard work there.

I'll also be showing this library off as part of my cf.Objective() talk on Closures – so please pop by if you want to see more about closures in ColdFusion 10, or just about closures in general.

JavaLoader 1.1, and a Move to GitHub

A couple of new items in this release of JavaLoader:

  • A few bug fixes in the NetworkClassLoader that allowed the Tika library to work.
  • A new function for switching out the ThreadContextClassLoader.

The new function, swithThreadContextClassLoader() is useful as it is often required when dealing with libraries, such as dom4j, that use the ClassLoader to define singletons or search for implementing classes, and don't allow you to overwrite what ClassLoader they use. Check out the wiki for more details on why this is neccessary, and how easy it is to now do, thanks to our new function!

Also, JavaLoader has been move to GitHub, along with the all the documentation. This should make collaboration much easier moving forward!

Otherwise, make sure to sign up to the mailing list, and enjoy loading your Java.

Speaking at cf.Objective() 2012!

I'm delighted to say that I'm speaking at cf.Objective() again this year!

I've got three sessions I'm presenting this year, two of which on my own, and one in which I'm paired with with Luis Majano.

cf.Objective()

I'll be talking about:

A/B Testing with Squabble

In which I extol the virtue of A/B testing, and why you should be doing it in your business, and introduce people to the A/B testing framework Squabble that we developed to use in the team I work with.

Getting closure on Closures

Where we will look at Closures, which are coming in ColdFusion Zeus. You probably already use these, and don't even know it. I'll explain what closures are, and how you can use them in simple ways, as well as more interesting use cases as well.

AOP Demystified!

This is the session I will be doing with Luis Majano, where we will talk about Aspect Oriented Programming, and give you examples with the two primary AOP frameworks – ColdSpring and Wirebox. Should be good stuff!

All up, it looks like it's shaping up to be a great conference program, so I look forward to seeing everyone there!

Custom Schemas in Coldspring 2

One of the most powerful new features in Coldspring 2 is the ability to create custom xml schemas that
can be used within your XML configuration.

Rather than go into the details of how to write and configure custom schemas, here is an example of one that comes
bundled with CS2 out of the box.

In Coldspring 1.x, if you ever wanted to create a standalone structure inside your XML configuration, you would quite
commonly create it through a MapFactoryBean like
so:


<bean id="myMap" class="coldspring.beans.factory.config.MapFactoryBean">
<property name="sourceMap">
    <map>
        <entry key="keyA" value="keyA value"
/>
        <entry key="keyB" value="keyB value"
/>
        <entry key="keyC" value="keyC value"
/>
    </map>
</property>
</bean>

This is all well and good, but the issue here is that you specifically need to know the api of the MapFactoryBean.
 If you get any part if the configuration of the MapFactoryBean wrong, you tend to get errors that may not be
clear, or potentially no error at all, simply a result that is not desirable.

For example, if you misspell the sourceMap property, Coldspring will attempt to set the property, but it doesn't
throw an error when it fails.


<bean id="myMap" class="coldspring.beans.factory.config.MapFactoryBean">
<property name="sourcMap">
    <map>
        <entry key="keyA" value="keyA value"
/>
        <entry key="keyB" value="keyB value"
/>
        <entry key="keyC" value="keyC value"
/>
    </map>
</property>
</bean>

Therefore, all you will get back is an empty struct, and quite possibly a headache trying to debug exactly why this
is happening in your code.

In ColdSpring 2, there is a nice, convenient util
 custom schema
, that exists to do things like create arrays, lists and other data structures for you.
 It essentially is a simple wrapper around things like the MapFactoryBean, but the custom schema gives you a
code completion and hinting when using an XML editor (that support XML Schemas), as well as better error messages
when things go wrong.

So, for example, if I wanted to do exactly the same configuration as above, I would add the xml namespace for util in
the head of my xml file, like so:


<beans xmlns="http://www.coldspringframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.coldspringframework.org/schema/util"

        xsi:schemaLocation="http://www.coldspringframework.org/schema/beans
http://coldspringframework.org/schema/coldspring-beans-2.0.xsd

        http://www.coldspringframework.org/schema/util
http://coldspringframework.org/schema/coldspring-util-2.0.xsd ">

Which would enable the <util:> namespace in my XML editor, and I could quickly type out:


<util:map id="myMap">
        <entry key="keyA" value="keyA value"
/>
        <entry key="keyB" value="keyB value"
/>
        <entry key="keyC" value="keyC value"
/>
</util:map>

And we would have the same result as above, but if something went wrong with your syntax, the IDE should show you
what it was, and if it didn’t, you would get a nice error from the XML validation letting you know exactly
what it was.

Of course, the whole point of this post, is due to the extensible nature of ColdSpring, you can write your own custom
XML namespaces, and register them with your XmlBeanFactory, to do things in very concise and easy to use manner
inside your IDEs as well.

This is a very simple example of what can be done with Custom xml schemas. You can drastically change the nature of
the BeanFactory as well as the contained beans through this mechanism, but this should hopefully give you a little
bit of a taste of what can be done with this new functionality.