Friday, October 26, 2012

The Bits, Nibbles, and Bytes of Life

Many new developers struggle with the idea of bits and bytes at a low level, and why they are useful to us as software developers since we have high level languages that abstract it away from us. Most of us are aware that a computers language is in 1s and 0s. Glorious 1s and 0s reign supreme in the digital world. Analog signals such as the human voice are analyzed and converted into a static on or off signal. Sequences of bits come together for a common purpose: to represent and execute information at the programmers whim. Sometimes, being in charge of development can give someone a God complex. We have all the control. Even though there are occasions where the computer (doing exactly what we told it to do, no less) is seemingly rebelling against our authority, we can eventually tame the beast into submission.

Still, the emphasis on the underlying beauty of the digital machine isn't as prominent as I feel that it should be. Consider this simple program in C++:
int a = 0, b = 0, c = 0;
a = 5;
b = 5;
c = a + b;
It's so simple. Even the people who have never seen a line of code could reasonably guess at what's happening here. We are declaring three variables, a, b, and c, and we are assigning the constant value of 5 to both a and b, and finally, storing the result of a + b into c. How could a computer not understand something so simple? The truth of the matter is that this 4 line program gets converted into, with collective optimism, a 17 line program in the computer language not including a lot of the setup and finalizing "stuff". Here's an extracted segment of just the 4 lines we see above in assembly:
; 4    : int a = 0, b = 0, c = 0;
mov DWORD PTR _a$[ebp], 0
mov DWORD PTR _b$[ebp], 0
mov DWORD PTR _c$[ebp], 0
; 5    :
; 6    : a = 5;
mov DWORD PTR _a$[ebp], 5
; 7    : b = 5;
mov DWORD PTR _b$[ebp], 5
; 8    :
; 9    : c = a + b;
mov eax, DWORD PTR _a$[ebp]
add eax, DWORD PTR _b$[ebp]
mov DWORD PTR _c$[ebp], eax

The first three lines store the constant 0 in the storage locations of a, b, and c. Remember, that these names are meaningless to a computer. Each variable has a memory address. This code is still an abstraction which is translated to the computers CPU language. The commands like mov and add will be translated to a binary op code such as "0110", which in turn is translated to an electrical signal.

Before I get too excited, there is a point to my madness. How does this make us better developers? How does knowing what code is translated to at the raw, naughty level of inside the machine useful?

The simple answer is this: Everything we learn can be applied in places we would least expect.

There's another reason, though. I'm going to use C# and the .NET CLR (Common Language Runtime) as an example. The + sign can be used to add numbers, but it can also be used to add Strings. In addition, there's an explicit function called String.Concat. What happens at the lower level? How are these different? Are they different? Consider this C# Snippet:
String str = Console.ReadLine();
String str2 = Console.ReadLine();
String test = str + str2;
String test2 = String.Concat("hi", "test2");
I read two strings from the user and store them in str and str2. I then explicitly call String.Concat with two compile time constant Strings. (Compile time meaning these are known at the time the code is compiled, as opposed to str and str2 which are known at run time because the user has to enter them). So what happens? What do you think? They're the same! Here's the code compiled into the intermediate language of the above snippet:
.locals init ([0] string str,//Declare the local variables by index
[1] string str2,
[2] string test,
[3] string test2)
IL_0000: nop //No meaningful operation is performed
IL_0001: call string [mscorlib]System.Console::ReadLine()
IL_0006: stloc.0 //This pops the item off the input buffer and into the variable str
IL_0007: call string [mscorlib]System.Console::ReadLine()
IL_000c: stloc.1 //This pops the item off the input buffer and into the variable str2
IL_000d: ldloc.0 //The next two lines push the local variables back onto the evaluation stack to be passed as parameters to concat
IL_000e: ldloc.1
IL_000f: call string [mscorlib]System.String::Concat(string,string)
IL_0014: stloc.2
IL_0015: ldstr "hi" //Load a string onto the stack directly
IL_001a: ldstr "test2"
IL_001f: call string [mscorlib]System.String::Concat(string,string)
IL_0024: stloc.3
IL_0025: ret //Ends the function
But wait! There are two calls to String.Concat? The compiler will convert the + sign between two String types into a Concat call. So at run time, there is no difference. There's also a reason I used "ReadLine" to get input from the user. If we use compile time strings like str = "hi" and str2 = "test2" and do str + str2, the compiler will be clever and combine the strings directly into the IL code instead of making a String.Concat call. This saves CPU cycles at run time. This isn't apparently useful, but to extend this concept is an important lesson. The reality is, in typical business development, it probably won't be necessary to think in these terms. In game development, however, this can make or break some performance factors. Game developers are always thinking of how they can save precious CPU cycles. Although I am far from experienced, this is a lesson I learned while working on Armor Potions.

Consider another example. When we think of the remainder of two numbers, what do we think of? Bonus points for anyone who successfully mentioned division. Well, sometimes a compiler is very clever. Using .NET as an example, and this is what originally got me interested in looking at the compiled code, is that if we take the remainder of dividing by 2, the .NET compiler uses no division at all! In fact, it's very clever while using the binary representations of the integers. Division, to a computer, is often compound subtraction which wastes cycles. So this optimization by the compiler is an interesting point. However, there are other solutions for getting the remainder of a number when dividing by two, one that saves even more cycles.

Consider the binary representation of the number 15, which is 1111. For those who don't know, binary is a base 2 number system. This means that, from left to right, the digits represent a power of two. The above number can be constructed as 8 * 1 + 4 * 1 + 2 * 1 + 1 * 1 = 15. This knowledge helps us in some cases, and because it's the computers language, we will use it to our advantage. Note that this is an odd number. What is the definition of an odd number? It's 2n + 1. Note the 1! An even number is simply 2n. 2 times any number n is an even number. In this case, if we add 1 to an even number, it becomes odd. The rightmost digit in a binary number represents 1. So if that digit is 1, then the number is odd. If that digit is 0, it is even. So any number ANDed with 1, and if it equals 1,  it is odd. In C++, it would look like this:
15 & 1 == 1; 
12 & 1 == 0
and so on. This works because with an AND 1, we're comparing the right most digit to compare the value. It should be noted that these are not that much faster than normal methods. The compiler is sometimes clever and does the optimizations for us. We shouldn't be spending all of our valuable time looking for these subtleties. My point is that these exist, and in some cases, such as my example with collision in a previous post, it can actually be a solution. Things like using bit strings to represent the collision behavior of a tile is an oft used mechanic. It's important to understand that these can be very useful in the developer tool kit. We never know when they will come in handy.

Tuesday, October 23, 2012

Ughhhhhhh

I've been working on the Map System a bunch and I've realized how incredibly much I need to fix, Working that all out will take more time then i had originally planned.

Plus with all the work in real life I have had over the last couple weeks... I haven't had the time nor energy to actually do any of it. 

I figured out how to make the system I wanted to make work so now its just a matter of finding time to do it. and figuring out how to integrate it back into the game as it literally breaks the old map system entirely.(and it only breaks it because I took out all the shortcuts)

Tuesday, October 16, 2012

Momentum

My apologies to anyone that does read this for our lack of frequent posts. I decided to post this as an interim idea rather than waiting for the next big break with Armor Potions.

John will be working on the new map system, but if he wants my help I'm glad to offer it. My part will be more likely fixing bugs and coding the AI for the new enemies that he comes up with (FYI, anyone interesting in doing graphics for this game is strongly encouraged to contact us as it's our weak point). The graphics will be the last part of the game to be finished. Before that is gameplay and the progression itself.

Right now, our map system deals with a hierarchy of sub classes from the Tile base class. Anyone who is savvy to software development will probably be cringing at the thought. Even I do, and I was part of the decision due to time constraints. John and I sat down and, well, John is the one that thought of it pretty much. We'll be removing the sub classes and using a single Tile class to operate on all the tiles. To be honest, I'm still not completely clear on how it works, so I'll leave John to describe it. We'll basically be adding an additional layer to the map where currently we have the bottom layer which is non-collidables such as floor tiles and such. The second layer was originally anything collidable (switches, etc). The switches as a tile though means that we're forced to make the switches the same size as any other tile. This has many drawbacks. For a game like Zelda, it didn't make much of a difference because the tile resolution was very small for the game boy screens. So everything being in unison size was the least of their worries.

Since we have a much larger tile size, we need to be able to say that the switch can be other sizes. Just imagine a floor button switch the same size as the wall Tile. Yeah. Although that might be humorous in some parts, it's not ideal in all situations.

Eventually, I'll be creating a link on the side bar here for updates to our release build that will contain a read me with instructions on how to play since the controls have been updated slightly (I am not particularly fond of them, so any insight here would be appreciated).

The rest of this is going to be slightly off topic from the entry, but more on par with the title of this entry.

Many people struggle with motivation, and I am no exception. There is no golden rule to suddenly become motivated. However, many don't realize the power of momentum.

I try to learn something everyday. Mostly within my field, but I am pretty lenient with myself as long as it's something interesting. So, regardless of motivation status, here is my challenge:

Take 5-30 minutes of your time every day, and learn something. It's preferable that it's something you're passionate about, or within your field of interest. I say 5 minutes because it doesn't usually take as long as 30 minutes. Take the first thought of something you're not sure about, Google it, and absorb the knowledge. The best part about this, is you can even come back with more questions! Keep this momentum!

I personally am happy to talk with you about what you've learned, discuss it, analyze it, or look up and solve a question with you. My point is, do this every day. I don't stop at one thing, but that's because I have months of momentum behind me. It can take as little as 5 minutes of time to learn something beneficial. I shouldn't have to say this, but anything related to the latest movie, celebrity, politics, or anything of that nature does not qualify as beneficial. If you can tell me what the technology or history behind a current [thoughtful] movie, then I'm all for that. Something as simple as the making of snow white is absolutely baffling.

The reason this is so helpful is because you feel better about what you did for the day. You're on track to your passion, and you know something you didn't know before which, to me, is invaluable. I hope that some of you will come to me with what you've learned so that we may enjoy a cup of tea and an intellectual discussion.

As an example, here is what I recorded of what I learned one day:
  • Magnetic materials in sub freezing temperatures can store a digital 1 and 0 in a space as small as 12 atoms, and are even showing signs of quantum mechanics http://www.nytimes.com/2012/01/13/science/smaller-magnetic-materials-push-boundaries-of-nanotechnology.html?_r=1&
  • The bus and CPU cycles times are closely related. Things sent across the bus, such as CPU interrupts, are determined by the bus speed (such as 400 MHz), and execution of instructions are determined by CPU type (Such as 2 GHz)
  • The stack plays a fundamental role in not only programming languages with function calls, but within the CPU directly as well because when interrupt handlers are called, it must push the function onto the stack and pop it to return to the original program that was suspended. To put it into perspective, every keyboard press triggers an interrupt, stops the current program executing, handles the interrupt, and then finally resumes.

This took me about an hour of research, but all I am asking for is 5 minutes of your time. Be productive, and learn something. Some of you might be fortunate to have jobs that allow you to learn fascinating things everyday, but for those who don't, this is even more necessary. And don't say you can't, because you all have smart phones and probably have what I call "wait states" that you could quickly do something. This is meant to be a work out for the brain as opposed to a work out of the body.

This is something I challenge everyone to do. 

Saturday, October 13, 2012

Energy Drinks!

Hey there! I'm hyped up on Caffeine!

So Today was kinda productive on my end, and I apologize to Jared for being sidetracked.

I got 2 of the maps done! the first of 2 is a small map that has some enemies and some doors that are opened with a new type of switch. one that is activated with the Space/A button not like the other switches that are elemental. The second is a bit bigger when compared to the first but still only contains Switches.

These only contain regular switches that have to  be manually activated because you start with ONLY the sword in the finished game. the first item you get is the Fire Eball which activates fire switches. so I theoretically COULD change the switches to fire switches in the second dungeon... but BLAH whatever.

I also started working on finishing the Code for the map maker so that it loads the map, cause I hate starting from scratch every time. i'm still writing the long number parsing for the switches so until that's done the maps are basically the 3 we have.

This weekend me and Jared are going to be going over Interact-able things like wall switches, Chests and NPC's(maybe? not set in stone, but might be a help.)  we're also going to sit down and fix the event system with Switches and doors. cause I suck at programming and that was the easiest way to get it done, so now we can come up with some ingenious way of doing tiles.

That's the Shpeal with Armor Potions. I'm also kinda working on a side project. if you want to read more its after the break: