Developer Time: Bit Switching Swingers
State of the Project Address
Well, it's been a tumultuous month here. Between computer problems for both of us, financial woes and a grumpy server, things got kinda down. Still, there has been a lot of progress for July, with many parts of the mail and forum system coming together, the light at the end of the tunnel seems to be getting nearer. The Dev list for our alpha invite release is still quite a laundry list, but things are being crossed off one by one.
However, the biggest blow so far has been the loss of my partner's computer last week, on which were many completed pieces of content and plot documents. We have managed to save everything we can from the hulk, but the blow has set back the development timeline somewhat, as she has not been able to do many of the pieces of art we're waiting on. However, things are looking up and we should have a new workstation for her on the first of next month.
Today I thought I'd go a bit more in depth into some of the inner workings of the game's mechanics. I'll surround this in a block quote for those of you who aren't interested, but it should be a interesting discussion anyway.
I thought today I'd talk about one of the things that I find most interesting about the way we've set up the engine, and that's something called Bit Switching.
In pointless, most of what you do is tracked through a series of flags, with most conversations having between one and three flags associated to them. These flags are simple, being represented in a "on " and a "off" state, or "true" and "false" if you prefer. There are many ways that these could be represented, and I found that the simplest way was to simply store them as a single unsigned integer.
Q1: Unsigned Integer? What?
A1: A unsigned integer is, quite simply, storage for a number between the values of 0 and 4,294,967,295. It can't hold a negative number, but for this application all I care about is how many bits it can hold, which is 32.
Q2: Ok, I sorta get what a unsigned integer is. What is Bit Switching?
A2:When I bit switch, I'm simply taking the integer above and converting it to binary. For example 4,294,967,295 would be represented as:
11111111111111111111111111111111
For completeness, this is 0:
00000000000000000000000000000000
Now, lets say the game is running, and you end a conversation. If I were to track that(For this example lets use the first flag), it would look like this:
10000000000000000000000000000000
^The flag that's tracked.
So, I can imagine now you're saying "Hah! How will you track multiple options? What if I pick number 3?!". To which I respond, it's simple. If there's more then two options, we concern ourselves with more then two flags. Lets say there's four options - This is the bits we would be concerned with:
1 00 00000000000000000000000000000
^These Right here...
So, our selection of 3 would look like 01. For selection 4, it would be represented as 11. Here's the completed field:
10100000000000000000000000000000
Q3:So, Mr. Programmer, how would you do this? It can't be done!
A3: Actually, it can, and in PHP it's really simple.
For this example, let's say I wanted to track if you selected option 3 from the previous example. First, I would have to determine which flag I wanted to test for. This is quite simple, and I'm not going to go too in depth to how we keep track of flags to be tested, but let me assure you - it's trivial.
Well, now I know which section I'm looking for. A little bit of math lets me know the bounds I'm looking for, which in this case I'm testing if 4 is set in the field. The easiest way to do this is:
if($test & 4){
//whatever you want it to do
}
So, what is this? This is called Bitwise Comparison, and is simply testing if the number 4(represented 001) is active in the last variable. Since, in this example, the Variable is set to 5, the test passes. Here's a example of the math in action:
101(5)
&001(4)
-------
001-True(if it was 000, it would be false)
If you want to learn more about Bitwise Calculations, Jim Plush has a great tutorial over at litfuel.net. --Here--
That's enough rambling for now - I'll be back at the end of the week with another chaotic, mechanics filled post. Leave us a messsage if there's anything in particular you'd like us to cover or leave a replay!
What would you like to hear about?