The Bitwise Challenge Post Mortem, Part 1

Hello!

If you are reading this, it’s probably because you either a) were directed here by a tweet about the Bitwise Challenge, or b) you used to read a political blog called SomeDisagree and you decided to see what’s up with it. If you are in that second group, I’ve got bad news for you. I haven’t been doing much political blogging lately (in case you hadn’t noticed) and I’m not really starting up again now. I’m using this blog to post some stuff about a game I work on called WildStar. On the Carbine forums (fora?) I am called Bitwise, hence the name of the challenge I’ll be talking about.

Okay, if you are in that first group, good news! You’re in the right place for information about the Bitwise Challenge, where I made a Tetris clone using the WildStar user interface engine in 4 hours on a dare. As the first paragraph alluded to, this site used to be a political blog, but after the 2012 election I lost all interest in political blogging and my fellow bloggers here pretty much did too, so this site has been almost entirely unused for the last 10 months. So I decided to use it to write up what I did with the challenge for those who expressed interest in it.

Now for the awesome disclaimers that everyone loves so much: THIS IS MY PERSONAL SITE. It is not associated with Carbine Studios and all the opinions expressed here are mine. I’m going to be talking about programming, not politics, but if you look through the previous postings you will see all sorts of things written by me and some of my friends which are very political. If that kind of thing might upset you, then don’t read the archives. If you’d rather stick with reading about this stuff via Carbine-approved sites, that’s cool, and some of this will probably get put on there eventually. I don’t apologize for anything I’ve posted on here, but it’s not going to be the focus moving forward. Probably. I dunno.

If you are here hoping to find out some new game information about WildStar, you (probably) won’t find much. All of that stuff is under tight control by our community relations peeps, and I won’t be (deliberately!) divulging any secrets here. In fact, this isn’t even going to be mostly about Apollo (the WildStar Addon Application Programming Interface) and Houston (an Addon Studio that will ship with the game) but the existence of those are already public knowledge. If you are curious about these aspects of WildStar and what it will be like to develop Addons for WildStar, you might find this interesting, but I won’t be revealing much new here about them. All of that stuff (Apollo, Houston, WildStar) is property of Carbine and we have an entire staff of people in charge of telling you all about it.

What this post (and others) WILL be about is the approach I take to programming a problem such as the one I tackled in the Bitwise Challenge. While the code here will be using the Apollo API, I won’t be going into any detail about that aspect of the code. Instead, I’ll be talking about reasons why I tackled certain logic problems in the way that I did, pointing out where I could have done things better, and stuff like that. Yes, you will see some of the Apollo API, and you may be able to glean some interesting info about it, but nothing that is a huge secret or anything. Most of the logic I will be talking about is API-agnostic, although not really language-agnostic, since Lua is so weird that it really affects the way you think about solving a problem.

Okay, on to the reason you are probably here, if you are still here: The Bitwise Challenge. For those who don’t remember, about 6-8 weeks ago someone tweeted a joke about Matt Krause spending all his time writing Tetris and I, in my usual humble manner, tweeted:

So… yeah, me and my big mouth. Anyway, that tweet got me to thinking. Could I really do it? So, the Bitwise Challenge was born. I talked to Troy at Carbine and he talked to the guys at Curse and we did this… thing… where I wrote a post about the challenge, and then I started at 5 PM and just started programming like a… something that programs… and tweeted about it and stuff and after 4 hours… I had a decent working version of Tetris:

So… success! Of course the challenge wasn’t ever about proving how awesome of a programmer I am (that’s just an added side benefit) but about how easy it is to create Addons for WildStar using Houston. With that in mind, I wanted to write up a post-mortem about the event that would illustrate some of the ways I went about solving the problem, along with the code. As some of you may remember, I had intended to do that over a weekend but was foiled when my younger son Jack decided to spill water all over my laptop, frying it. While the story of my poor laptop eventually had a happy ending thanks to Carbine IT, things were (and still are) pretty hectic at Carbine and I never did find time to write a post-mortem before my vacation.

So, yes, now I’m on vacation, and I’m choosing to spend at least some of that vacation on writing about programming games. Why? Because I love you guys, that’s why, assuming anyone is reading this. Also, I’m a huge nerd. Also, I like to point out that I wrote a Tetris clone in Houston in only 4 hours. Also… that’s enough alsos. Let’s get to the Addon.

First of all, I decided to make a Tetris clone with a WildStar theme, hence Loftis. For those who don’t know, on Nexus there are these weird crystals that float because [REDACTED] and they have some other cool properties like [REDACTED]. They’re called Loftite. So I decided my version of Tetris would be called Loftis and the blocks would float up rather than fall down. Also, coincidentally, a lot of the look of WildStar was created by a concept artist named Cory Loftis, who has since moved on to Disney, so it was kind of cool for that reason as well. Anyway, here’s a zip file that is Loftis as it was when the challenge ended:

Loftis Post Challenge source.

For those of you currently in beta, you can just copy the contents of that zip file into a folder named “Loftis” in your Addons (%appdata%\ncsoft\wildstar\Addons) folder. Login to the game and type “/loftis” on the chat entry line to invoke Loftis. It works pretty well, there are some bugs, but it keeps score and ends the game when you can’t place a new block anymore. I didn’t get the “Next Block” feature working.

I spent the first 25 minutes or so of the challenge just editing the form that would eventually be the Loftis UI. This included making all the controls for the game, and registering/creating the event handlers to handle the game controls. The scheme I came up with was “A” and “D” to move blocks left and right, and “Q” and “E” to rotate them. “W” can be used to quickly force a rising block to go immediately to its next position. Once I had finished the form, I ran the game and invoked Loftis, and sent this tweet with a screenshot:

As the tweet says, that’s Loftis running in game, without a single line of new Lua code. Once I did start writing code, here’s what I wrote first:

-- block definitions
local karBlocks = {
 {'    ', 
  '    ', 
  '****', 
  '    '},

 {'    ', 
  '   *', 
  ' ***', 
  '    '},

 {'    ', 
  ' *  ', 
  ' ***', 
  '    '},

 {'    ',
  ' ** ',
  ' ** ',
  '    '},

 {'    ',
  '**  ',
  ' ** ',
  '    '},

 {'    ',
  ' ** ',
  ' ** ',
  '    '},

 {'    ',
  '  * ',
  ' ***',
  '    '},
}

These represent the 7 different kinds of blocks that are used in Tetris, er, Loftis. I chose to represent the definitions as strings for the sole purpose that it made it much easier to enter them into the source code. It was purely a coding-speed decision, which is very different than a performance-speed decision. If I was purely interested in performance, I’d probably take the coding time to represent these blocks as booleans rather than strings that need to be parsed. However, the other thing to consider is readability, and the strings are much easier to read than a table of booleans; you can look at those blocks of text and sort of see the Tetris blocks. Since the conversion time from a string to boolean is not a concern (see below) I’d probably still use strings to define these blocks, even if I wasn’t under a 4-hour time constraint.

Anyway, in the game simulation, I represented the current block as an array of boolean variables, so I also had to write a converter:

function InitBlock(tBlockDef)
 local tBlock = {}

 for i = 1,4 do
   tBlock[i] = {}
   for x = 1,4 do 
     tBlock[i][x] = false
     if string.sub(tBlockDef[i], x, x) == '*' then
       tBlock[i][x] = true
     end
   end
 end
 return tBlock
end

This function takes one of those block definitions and returns a new 4×4 table of booleans with true representing a block and false representing empty space. Whenever it is time to place a new block, I first use math.random(1, 7) to choose which element of karBlocks to send to InitBlock:

local iBlock = math.random(1, 7)
local tBlock = InitBlock(karBlocks[iBlock])

Now I have a tBlock object that represents one of the seven Tetris blocks. What will I do with it? You’ll have to wait until tomorrow(ish) to find out! Hey, I’m on vacation.

Bitwise out.

UPDATE: In case this wasn’t obvious, NONE of this code is production-quality code. This was a speed programming challenge. There are all sorts of things (magic numbers, inefficient algorithms) that I would absolutely NOT do if I were writing this code to last. I’ll be pointing these things out as I write about them, with examples of what I might do instead in a production environment. Showing someone else your bad code is an act of trust; don’t get hung up on the ugliness.

About Wiesman

Husband, father, video game developer, liberal, and perpetual Underdog.
This entry was posted in Videogames, WildStar. Bookmark the permalink.

6 Responses to The Bitwise Challenge Post Mortem, Part 1

  1. Pingback: The Bitwise Challenge Post-Mortem, Part 2 | Some Disagree

  2. Pingback: The Bitwise Challenge Post-Mortem, Part 3 | Some Disagree

  3. Pingback: The Bitwise Challenge Post-Mortem, Part 4 | Some Disagree

  4. it’s nice, bt i want to make templates….

  5. Packetdancer says:

    Incidentally, Bitwise… thank you so much for posting these. I’ve been able to point a lot of aspiring addon developers who want to know what they should look into (and what WildStar addons look like) to this post over the past few months.

  6. Pingback: Introduction to Pixies - Nexus Addon Standards Association

Disagree?

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s