Ruby Gems and Warhammer

The other day I wrote about my attempts to get back into Warhammer. Today I wanted to touch upon a slightly different aspect of the hobby. The open secret of war gaming community is that models are technically optional.

This may sound counter intuitive at first, seeing how the entire “hobby” aspect of war games revolves around models: collecting, painting, converting, trading, etc.. Undeniably, they are very important and without them the companies that release and market such games would have no profits. But when you just want to play a quick game with a friend, it can be done without models.

Unlike strategy games that are played over the network via keyboard and mouse under strict rules enforced by a soulless machine, tabletop war games are gentlemanly affairs conducted between consenting man-children. In essence, when you play Warhammer you are on the honor system – there is no game engine to make sure rules are followed, and no referee to ban someone for cheating. You simply have to agree to follow the rules and conduct yourself in a sportsman-like way because otherwise the game ceases to be fun.

Because of this, you can technically play a game without any models. Or rather you can substitute actual models with just about anything: lego figurines, old plastic army men, poker chips or even paper cut-outs. When you play Wahrammer Fantasy you typically deal with square or rectangular units that all move as one. Models are typically mounted on square bases that are 20mm on the side (or 25x50mm for mounted cavalry). If you are really lazy all you need to do is to draw a grid on paper, cut it out and you have a unit. When the models “die” instead of taking them off, you can either cross them off, or rip them out of your paper cut-out.

This is more or less how we have been playing lately. None of the participating mentlegen had a complete army – at least not by the rules as they stand today. We all had some old models at our disposal, and so we decided to just create the types of armies we would hope to one day assemble, and simply use paper cut-outs for all the units we didn’t own.

Unfortunately drawing grids is actually not that easy when you can’t find a metric system ruler (or any ruler for that matter) in the house, and you are forced to trace model bases on notebook paper to create crooked grids that line up only in theory. After scribbling down a dozen units like this I decided there ought to be a better way of doing this. And being the lone programmer in the group, I was uniquely equipped with the know-how on how to accomplish such a thing.

The next day I sat down, rolled up my sleeves and created MovementTray – a quick and dirty Ruby script that spits out printable grids that can be used as stand-in Warhammer Fantasy units, or as movement trays (pieces of paper you put underneath the models so that they are easier to move as a unit).

Screenshot

Screenshot

Before you give me any credit for it, let me explain: this took about 20 minutes and was absolutely trivial. I put it online mostly just to make it downloadable. The core functionality is roughly 20 lines of code, and the rest is just fluff, error checking and user prompts. It was much, much easier to write than I expected, primarily because of Prawn.

Prawn is an amazing Ruby Gem that lets you generate PDF files on the fly. It is feature rich, ridiculously comprehensive and super easy to use. Let me give you a dumb simple hello world example:

1
2
3
pdf = Prawn::Document.new
pdf.text "Hello World!"
pdf.render_file = "hello.pdf"

Yep, it is that simple. Drawing custom shapes is just as easy – Prawn defines a whole array of shape drawing methods. So to generate my grids all I had to do was to repeatedly call the rectangle method (which took 3 arguments: starting coordinates, width and height) in a loop. Here is an example of the document my little script generates:

Output of mt.rb

Output of mt.rb

If you ever need to programatically generate PDF files, Prawn is definitely the way to go. I highly recommend, especially seeing how it has a strong team, a lot of contributors and sees a lot of activity on github.

The other really nifty gem I discovered was Trollop. What does it do? It simplifies parsing of command line arguments. I stumbled upon it almost accidentally, but now that I know about it I wonder how I ever created command line tools without it.

If you think about it, it is both baffling and staggering how much code typically gets written to parse command line arguments. Simply grabbing one or two values from ARGV is easy and quick, but when you want to have a unix-like command line switches, some of which are optional toggles while others take sub-arguments the problem of parsing them suddenly becomes non-trivial. Especially if you want the user to be able to specify the arguments in random order, and be able to leave out arguments providing sane default fall-backs.

In my particular case, I was looking at a program that generates PDF files in 20 lines, and requires 100+ lines to parse the arguments which seemed absolutely ridiculous. Trollop allowed me to reduce all that mess into more or less this:

1
2
3
4
5
6
opts = Trollop::options do
    opt :base, "Base size: standard, large, monster, cavalry", :short => "-b", :default => "standard"
    opt :rows, "Number of rows", :short => "-r", :type => :int, :required => true
    opt :cols, "Number of collumns", :short => "-c", :type => :int, :required => true
    opt :file, "Output file", :short => "-f", :type => :string, :required => true
end

Basically you need a single line per command line switch. The snippet above defines both long (double-dash) and short option switches and checks for their presence if they are required. It also automagically defines –help and -h options that will display all the options along with their descriptions.

I don’t know what it is about Ruby, but regardless of what I set out to do, there is usually an amazingly designed, well maintained and meticulously documented gem that firs my needs exactly. Half the time it just feels effortless.

This entry was posted in programming and tagged , . Bookmark the permalink.



4 Responses to Ruby Gems and Warhammer

  1. I don’t know what it is about Ruby, but regardless of what I set out to do, there is usually an amazingly designed, well maintained and meticulously documented gem that firs my needs exactly. Half the time it just feels effortless.

    Well.. thats exactly what i associate with python. And obviously i’m not the only one who thinks so. :D

    Reply  |  Quote
  2. This is pretty slick! Nice work. At the top of the post I was wondering how you would get the scale right. Drawing rectangles is easy, but getting them out of a printer at exactly the right dimensions isn’t. PDF seems like a great solution for that.

    Reply  |  Quote
  3. Jason *StDoodle* Wood UNITED STATES Google Chrome Windows says:

    Aw, the one time that I, as a professional CAD Monkey, might have been able to contribute something useful… :(

    Reply  |  Quote
  4. Luke Maciak UNITED STATES Google Chrome Linux Terminalist says:

    @ Dr. Azrael Tod:

    Oh, yeah – Python definitely has that thing going for it too.

    @ Chris Wellons:

    Yeah, I knew right away I needed to go straight to PDF. Anything else would be a nightmare to keep in proper scale.

    @ Jason *StDoodle* Wood:

    Oh man… Sorry, CAD tools are sort of out of my scope so I tend to forget they even exist. The most AutoCad was relevant to my life was when someone used it to explain the infinite chocolate bar illusion on reddit :P

    Reply  |  Quote

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>