Thursday, March 22, 2012

My AD&D 2nd Ed. Character Editor from 1995

Well met!  If there was ever any doubt about my nerdy ways - past or present - this should clear it up.  This is my AD&D 2nd Edition "Character Creation System"... a.k.a. The CHreator (version 1.00B for beta).  :-)

According to the file dates, I compiled the final version of this at 12:42am, Sept 20th, 1995. 

Many such editors existed, but they were hard to distribute and come by.  Apparently I found it easier to write my own than download one from the local BBS over dial-up. Anyone remember Legend of the Red Dragon BBS game?

Anyway, this bad boy only runs in DOS, and won't run in Windows 7, so I've posted screenshots (as if you would have downloaded it anyway).  It does run in DOSBox just fine though (which I happened to already have for all of my other nostalgic software).

Most similar character editors used the text console based interfaces... which is fine and all, but it wasn't good enough for me.  I wanted to go VGA!  This was before the days of ubiquitous (read "free") component frameworks.  I just had a plain old Borland C compiler (I'm sure it was a legal copy, courtesy of my high school) and a basic graphics API.  Every screen, line, box and control had to be hand coded.  

I even had to make the cursor blink myself (the cursor was a red blinking asterisk character). 

I made my own controls for text input, combo boxes (see the alignment field in the 3rd screen -- these were proper combo boxes, with text entry) and auto-populating look-up tables for attribute modifiers.

It stored the characters in some strange binary format with a *.D&D extension (creative!), and printed a 2 page character sheet spaced perfectly on old-school dot-matrix printers.  

Then Windows 95 came out, DOS died and this went nowhere.  Too bad I didn't write it four years later.. I'm sure I could have found a venture capitalist to throw a few million into this thing... :-)

** Special thanks to Colin for buying better hard drives than I do... I lost this many years ago in a drive crash, along with the source and all of my other DOS apps.  I don't suppose anyone has a copy of my old VGA DOS menu?  Remember DOS menus?  Man... DOS was fun. 











And if you read this far, you deserve a bonus from 1995... good times.  Look! That guy is using a netbook in 1995!



Sunday, November 13, 2011

Multiplayer PVP Starship Battles in Flash and Plain Old HTTP

Click to Play Starship!

Look, I'm early for my annual blog post!  I suppose I've always preferred coding to writing.  But here I hope to share some of my experiences in building this game with you.

While I hope this game is fun for at least 3 - 5 minutes, this is more of a tech demo.  This is what I do for fun, and I promised myself I would finish it before opening Skyrim, which is sitting next to my XBox 360 unopened as I write this.

Edit:  I've added a section on CPU usage.
Edit: Made asteroids cause damage on impact.
Edit: Allowed asteroids to be destroyed while only the first (red ship) exists while waiting, to celebrate Ed Logg's recent award:  http://www.wired.com/gamelife/2011/11/ed-logg-pioneer-award/

Why Flash? Isn't it dead?

First and foremost... why Flash in this HTML5 era? Isn't Flash dead?  Well, I don't want to get too political, but I'll just say it isn't yet. I'm pretty certain that HTML5 will eventually evolve into a platform that allows for pretty much everything Flash can do, and possibly more, and maybe even better.  With the momentum behind HTML5, it's hard to bet against it.  But it's not there yet.

This game is Flash. I actually believe it would be quite a challenge to build this game in HTML5 today due to some of the unique characteristics... see below for more (in particular the graphics and sound sections).

For my next side project I plan on porting the little retro RPG engine I made to HTML5 and JavaScript.  It's a perfect match for the capabilities of HTML5 today.

The Game: "Starship" (super original)


You
  • You play the role of a starship, which is probably actually manned by a crew of 2 - 4. 
  • It is armed with a combination of a "phaser-like" energy weapon and projectiles.
  • Your defenses include both shields and thick armor.
  • Energy weapons do more damage to shields, and projectiles do more damage to armor.  Effective use of both is required to defeat the other starships.
  • You fly around at pretty rapid speeds using thrusters, retro-thrusters and maneuvering jets.
The Environment
  • The galaxy is endless... not like Asteroids endless where it just wraps around the screen... this is a massive pixelspace that you fly around in a spherical route. A math savvy friend of mine tells me that it's actually a torus, but I don't believe so.  Not exactly.  It's not a true sphere, but given how this is implemented it's not a torus either... some simple origami will settle the debate!  Regardless... you won't find boundaries here.
  • The galaxy is riddled with indestructible asteroids!  But worry not, your Starship is equipped with advanced equipment to keep you from slamming into them. In other words, I couldn't decide on an adequate game mechanic: Should asteroids be ultimately deadly, destructible, or what?  So I went with "galactic nuisance".  I've now upgraded the asteroids from galactic nuisance to legitimate danger.  They will damage your shields and armor, large ones do more damage.  Asteroids can be destroyed with projectiles while only the first (red) ship is active (while you're waiting for opponents -- to give you something to do). :-)  
The Enemy
  • Up to four other players man Starships of equal power, but different in color.
  • Even though I just described the galaxy as massive and endless, it's not big enough for the five of you.
  • So shoot them.
  • Or you can bump into them to disrupt them, which will trigger their ships collision avoidance system and you'll "bounce" around a bit.
  • When you destroy an enemy (or are destroyed yourself), along comes your backup in the form of a respawned ship, good as new (which reminds me, I should probably reset the shields and armor on the winner's ship)
The Controls
  • The controls are laptop friendly and for lefties and normal people.  :-)
  • WASD -or- OKL; -or- Arrow Keys will move you.
    • Up and Down control your thrusters. 
    • Right and left rotate the ship with your maneuvering jets.
  • Space Bar or either Control (CTRL) key will fire your energy weapons.
  • Either Shift key will fire your projectiles.
  • Your HUD encircles your ship
    • The blue circle represents your shield status
    • The red circle represents your armor status
  • Your HUD also includes a radar that surrounds the game window.  Asteroids are tracked as grey semi-circles, and ships are tracked as larger colored semi-circles that match the ship.
  • Upon opening the game, or if the flash player or browser loses focus, these instructions will appear on the title screen.
  • If the controls aren't responding, just click in the Flash Player to give it focus.
The Game Lifecycle
  • The game will fill up "rooms" with people as they arrive.  I'm sure there will be millions of people playing this game, so you shouldn't have problems finding an opponent.... but really you should BYOF (Bring Your Own Friends).
  • If you're the first person to arrive, the game will wait indefinitely (well actually 30 minutes... but if you stick around for longer than 30 minutes shooting at indestructible asteroids....).
  • Once at least one more ship joins, then the room will only wait for another 30 seconds before closing the doors to new opponents. 
  • Currently this approach might make it hard to get together a party if this does become popular, but I was erring on the side of it not becoming the next Angry Birds.
  • If you really have no friends, you can just refresh your browser a couple of times and you'll see the room spawn a new ship each time in the room you're in.  So you have some orphaned ships to shoot at (note:  IE9 doesn't refresh Flash state -- which is actually good if you're watching a video -- but for gaming, you'll want to close the browser and reopen it).
Profit. 

Technical Notes

Okay, now that you know how the game works and have spent 23 seconds playing it... here's what makes it cool IMHO...

HTTP

This is pretty responsive, real-time-ish, PVP multiplayer in a browser via HTTP.  Now for a lot of my friends who are really good at what they do, this may not seem too impressive.  
"Pffft. Some comet, some streaming, and some other stuff etc." ~friend
But really... how many real-time-ish (not turn based) in-browser, HTTP based, PVP combat games have you seen online?  I was really curious myself to see how well it would work out. With HTML5 and WebSockets, this world should get a lot better.  But right now, this game employs some pretty heavy abuse of plain old HTTP.  A persistent HTTP connection is used for receiving the remote game states, while individual requests are used for updating the status.  

I didn't use any frameworks for this.  I really wanted to learn how this stuff works.  So the client connector, protocol and server are all hand-coded.

Syncing

Each player's ship sends its commands (intentions) and current state (according to this one client) to the server.  I didn't go too crazy with anti-cheat detection.  That part seemed relatively easy to me, since the entire set of game logic could be run on the server side, to ensure nobody is warping their ships around etc.  Until people start playing this for real money (or virtual Zynga goods), I don't think I'll worry too much (but more on this later in the collisions discussion).

The asteroids are synced differently, based on a simple time protocol.  Every asteroid you see in one game window should be in relatively the same place as any other game window in that same room.  But each asteroid's starting position, speed and direction are all randomly set.  The server simply sends back the age of the room, and the current location of the asteroid is calculated based on that synced time, so even latecomers to the room will see asteroids in the proper position.  

Visually, every single star, asteroid, and ship should be placed exactly with only minor errors for latency -- but even that is predicted and calculated out as best as I could.  Projectiles are not synced, but given that everything else is, it's a pretty safe bet.  There's a remote possibility of the odd extra projectile slipping by, and if there were more serious consequences (like if you had paid real money for your Starship), I would probably sync those too.

Latency is tested and used to help avoid too much stuttering in the game, which I think worked out pretty well.  During regular game play, there's next to no jittering (except on collisions... but more on that later).

Remember, this is HTTP syncing... not UDP over a dedicated socket. 

Collisions: Detection vs. Reaction

Flash has built-in support for collision detection, but according to many, it's entry level at best. It may have been good enough for this game, but I figured I might as well invest in it a bit.  So in invested 30 seconds in Google Search and came up with Corey O'Neil's cool little MIT Licensed Collision Detection Kit (CDK).  It had a pretty nice API for detecting collisions in a field of potential objects (like ships, asteroids, projectiles and energy weapons).  So collision detection became fairly simple. 

Projectiles, energy weapons, damage and all that stuff was easy.

However, I soon learned that I should have attended a few more of Mr. Lajoie's Physics classes -- because ship-to-ship and ship-to-asteroid collisions were difficult.  The detection was easy, but the reaction to a collision detection became a bit of a challenge.  

For smoothness the game has to predict events and play them through, potentially before it receives such state from the server.  For general movement this isn't too bad.  But collisions have so much going on, and there can even be multiple parties involved in the collision -- all with different state arriving at different times.  So the net result of the collision reactions was absolute chaos.  

Looking at just one screen at a time, it's only moderately bad.  But if you watch more than one at a time (DON'T -- okay now you will), you will witness a total sync meltdown.  If people are shooting and such during these events, I'm sure those phantom projectiles will not be so uncommon anymore. 

I'm sure the big game studios know exactly what to do and how to deal with this.  Short of having the server decide upon it all, and just send the state to the client, I can't think of a magical solution. Since I have not implemented the server-side physics, you will see these anomalies.

Unfortunately the asteroids were one of the last things I added, and the problem was not so severe without them.  In hindsight, if I were to ever do this again, I'd go 100% server side "decision", with only client side "intention" -- which is probably the "right" way to do all of this anyway.  My learning experience was more client focused though, so the server side is light (I've been doing server side for 13 years-ish). 

Graphics

I don't own Flash Professional, nor do I know how to use it, nor am I an artist.  I used the free Adobe MXML compiler that comes with the Flex SDK -- but there is no Flex here... The main class is just a Sprite, and it's all Sprites and domain classes from there.

All of the animation and movement is hand-coded.  It worked out pretty smooth and clean... but network latency and syncing can make it jitter from time to time (not usually noticeable with regular game play). 

The game uses a combination of vector and bitmap graphics.  Vector is used for the stars, radar, projectiles and energy weapons.  Bitmaps (PNG sourced) are used for the ship, asteroids, explosions.

This is one area I wonder about with HTML5. The ability to use both bitmap and vector graphics was important here. I wanted bitmap style artwork (although the current ship art doesn't warrant it, the asteroids and explosions totally do).  In HTML5 you have SVG and Canvas.  Two completely separate specs, standards and APIs... Can the canvas be transparent and on top of an SVG container?  Or vice versa?  I used vector for projectiles and bitmap for ships though... and collision detection works great between them.  I hardly doubt the same will be true in HTML5.

So why use vector graphics at all?  Well...  The equivalent size of the starfield in this game is the effective equivalent of 225,000,000 pixels.  With a 32 bit pixel (4 bytes), that's 900MB of bitmap data -- a little too much to be working with in a browser or across a wire.  But vector graphics don't need to store all of that.  They are what is called a "retained mode" format, which means it just knows where stuff should be and what it should look like based on some instructions, calculations, filters and rules (to put it simply).  

Why use bitmap graphics at all?  Well, pure vector graphics lack detail that bitmap can provide.  While it may not show in the ship in this game, it certainly shows in the asteroids and the explosions.  I'll try to find a better ship to show it off.

The combination of both was indeed powerful.  Of course it's not impossible to make this work purely with bitmaps using a tile-based approach (think Google Maps), it will certainly be far more work and still more memory intensive.  Plus, remember that the star field itself is randomly generated and parallaxing.  So with bitmap you'd actually need to generate (on the fly) two layers of bitmaps, doubling the data size. 

Music

My favorite part of how this game turned out is the music. Sure, it would be simple to play MP3 in Flash or even with the audio tag in HTML5 (when it works... many HTML5 games -- including Google Pac-Man and the more recent Google guitar fall back to Flash for audio). 

But this entire game is 686KB.  That includes all of the code, art assets AND the music.  If you sit with the game open for a bit, you'll find that there's about 8 minutes of music made up of 5 tracks, randomly ordered each time you play.  So the music doesn't get too repetitive.  8 minutes of MP3 even at a low quality would be many times the size of the entire game.  

Sure, we all have broadband, and we can stream the MP3 to avoid having to download it all at once.  But this  entire game is smaller than the Facebook homepage (just the HTML). Am I the only one that finds that awesome?  Maybe I'm just old and remember floppy disks that couldn't store the Facebook homepage and a time when screenshots of a game were larger than the game.  :-)  

In any case, for the music I use something I found in the trash bin of one famous and fully respect-worthy Andre Michelle.  If you don't know who he is, Google him.  This guy's trash was my treasure in the form of one 8BitBoy MOD player. 

If you're unfamiliar with MOD and related formats... see The Mod Archive.

CPU Usage

One of the biggest criticisms of Flash is that it is a CPU hog.  This is largely due to the fact that Flash doesn't use the graphics card.  The reason is that by limiting it to CPU, Flash can ensure a more consistent user experience (not different depending on your graphics hardware), but this comes at the cost of performance.   Adobe has released Stage3D which has hardware accelleration support, and it's pretty impressive.  I have not tried it yet.

In a nutshell though, on my i5 that I developed this on, it uses about 30% of  one core (Flash is also single threaded).  On my wife's ultra-portable, which has an Intel SU7300 - a 10W ULV processor slower than the old Macbook Airs - it uses about 60%.  Both of these tests were done with Chrome.

Interestingly, Firefox utilizes a little more more CPU, because it seems like its HTTP client is less efficient than Chromes.  More surprisingly is that Internet Explorer 9 uses even less than Chrome, because its HTTP client is even faster than that of Chrome.  But the Flash cost is about the same in all three.

These were quick smoke tests though, nothing too scientific.  If you test yourself, be sure to have a CPU monitor to ensure that your CPU isn't underclocking itself (stepping down).  For example, if your laptop steps down your CPU to half its normal speed, then the process may be registered as using a higher percentage of CPU cycles than it actually is. My computer is a laptop so it often steps down from 2700 to 2000 MHz, and thus the usage jumps up to around 40% (this usually happens only when the window doesn't have focus).

The use of vector graphics as well as the MOD format music contribute to a bit more CPU usage in exchange for a small package.  MP3 would (surprisingly) be faster as would pure bitmaps -- depending upon an adequate implementation to achieve the same effect as I mentioned above.

Summary


Overall this was a great development experience.  I'd really like to go back and refactor the code and rethink some of the natural designs that emerged... which weren't always great.  This experience, along with the retro RPG engine has convinced me that game development is both awesome and hard.  It has to be one of the most rewarding kinds of development.  It's a mixture of art, science and engineering at its best.

I'm currently reading through four books related to various HTML5 topics, and I look forward to seeing what it can do.  It has big shoes to fill though, as Flash is a hard act to follow.  Simply being "not a plug-in" won't cut it!

Cheers,
Clinton


Credits

Game Design and Programming
  • Clinton Begin
Art
  • The original ship is from Devian Art under a CC license by Aquilar Nava.
  • The explosion sprite sheet was created using a generator by Positech Games.  
  • The asteroids are from Mr Booth, and a forum post, but not sure of the source (if you know it or own it, let me know).
Music
Sound
  • All sound effects for thrusters, energy weapons, projectiles and explosions are from Freesound.org
Fonts
Libraries

Saturday, January 1, 2011

An Enterprise Java Developer in a Flash Game Developer's Court

I am no game developer...

...yet.  But is it not the dream of every software developer to build games?  Thinking back to my childhood and my early interest in computers, I can say for certain that it may not be.  Indeed, my early computers could not play games -- nor could I afford them.  I therefore spent far more time sitting in the aisles of the public library, reading outdated operating systems books, hardware books, tinkering with Linux and programming crude windowing and application user interface systems with Borland's C and Pascal compilers (I remember having to make my own cursor blink).

It is not until now - in my early 30's - that I attempt to build a game.  I think it's more of an adult dream than it ever was a childhood dream.

The advantage of this is that I need not worry about how much money I can make as a game developer, or whether I'm wasting my time learning how to build games. That said, in my short time spent building a game, I have learned of new challenges, patterns and software development techniques.  This is wonderful, as I've been shoveling bits from one database to another in "Enterprise" software development for 12 years now, so it's rare that I come across anything really new and interesting in that space.

Perhaps the dream is not of the game itself, but what the game can teach me as a software developer.  I've always been a practical learner, so I purposefully read no books about game development -- I've just purchased my first, now having written a significant portion of the game.  I suppose I want to see what I naturally got "right", and what "I didn't know that I didn't know".

Being a fan of RPGs, and currently only possessing the graphics programming skills to build a 1980's style NES Zelda type game... that's what I targeted.  While they can be simple on the graphics side, RPGs are known to be quite complicated in many other respects -- and in total, are probably the toughest type of game to build.


The User Interface

Some of the most interesting challenges obviously come in the user interface.  A game built with Swing, Flex or WinForms would probably suck.  :-)

For someone like me, it's not every day that I get to calculate the positions, viewable areas and movable areas within a virtual map etc.  But that might not be entirely unique to game development.  Anyone doing Google Maps style work or any GIS project may experience this kind of challenge.  But I took a very raw approach to building this first game of mine.  I used no frameworks or libraries to help.  I really wanted to know what went into such things.

The math wasn't rocket science by any means.  But the OO design decisions were definitely interesting.  Should the map know about the character position?  Should the character know about its position on the map?  Both?  Neither?  Furthermore, what should decide what is visible to the player?  The map is bigger than the available screen space, so a scrollable window is necessary.  What decides what is viewable in that window?

It sounds so simple.. but I think I wrote that part about 12 times before coming up with something I was happy with.

Movement and Zone Detection

A typical RPG doesn't really have a lot of complex collision detection, but what it does have is places where you can walk, and where you cannot walk.  It also needs hot spots for loading maps, starting encounters, finding treasure, triggering story lines etc. I had nightmares about calculating polygon shapes and sizes, or using bitmasks with complex bitwise operations for determining the characteristics of a certain part of the map.  Programmatically it's not that hard to implement something like that.  But where it becomes complex is in actually creating a map.  Thus special tools become necessary to create a map.  I didn't want my game development experience to begin with having to build a map editor.  I wanted to just paint up a map with Paint.NET and be done with it.  The approach I took might be a bit grunt, but at the end of the day, I think it works quite well.  I chose to use a color mask, where the colors can be configured (by their easily readable HTML hex code).  For example, here is the first map, with 4 zones:


I could say 0x000000 (black) is the no-walk zone, 0xFF0000 (red) loads the cave map, and 0x0000FF (blue) loads the map to the east.  The default zone is 0xFFFFFF, which is walkable and does nothing else (but could -- definable on a per map basis).  This approach allows me to use any simple paint program and a text editor to create my maps.  There are more than enough colors definable that I won't run out of easily identifiable colors on any single map (I can't see using more than 12 or so on any one map).  Furthermore, it is also pretty easy to create a custom map editor when the time comes.  But it's not required to get me past the point of map creation and into the development of the game.

I am also not an artist...


Now that I have a couple of screenshots up, I can point out that I am not an artist.  First, I didn't create any of the above art.  Of course I created the map and the mask, but not the elements of the map, like the trees and cave etc.  And while the art seems simple, I think it's great.  Both the map assets and the characters were found online with friendly licensing like Creative Commons.  It's a good thing, because my backup plan was stick men.

As simple as these graphics are, I have to give full respect to the creators.  Something like those little 2 frame character animations are actually an incredible amount of work to create -- at least for me.  I'm sure there are people out there who can crank out a full set of 8 direction, 2 frame character sprites (total 16 images) in an hour.  But to get them all matching and looking like they belong in the same game -- that's entirely different.  It's not just pixel pushing, it's art.

For sound, I took a similar approach.  There were plenty of Creative Commons or at least friendly licensing terms for simple sound effects and background music.

Java as a web game platform


I have 15 years of Java development experience, and therefore defaulted to Java as a target platform.  Even though I have never built an Applet that was actually used for anything (has anyone?).   I also had this sinking feeling that there must be a reason that I've never seen a serious game build with Java on the web.

Without question Flash dominates this space.  But I hypothesized that the reason for this was simply due to the heritage of the two languages.  Java has foundations primarily in enterprise computing, business software, web applications, web services, and the odd successful desktop software (usually development tools).  Flash on the other hand has a heritage in graphics, animation, and rich media on the web.  I'm sure the multimedia programs taught at colleges around the world target Flash in their curriculum and not Java.

So I figured that there's no reason why Java couldn't be a decent platform for game development -- for a Java developer.

Unfortunately this is not the case. The sad thing is that it isn't due to the libraries, the language or other aspects of software development.  Although, Flash has some clear advantages in the tools and libraries available.

The big problem for Java though, was the end user experience.


The JVM is a brilliant piece of software.  But as a browser plugin and an end user runtime environment, it is very large (at least 5x the size of the Flash Player), and its security policies are outrageous.  In addition, even something as simple as the Java logo during the initialization of the JVM has a significant impact on the end user experience -- especially when we're talking about a game.  Games are about tone, environment, feel and player immersion.  Having the player's first experience be a security policy dialog box and a Java logo is a pretty terrible consequence for choosing the Java platform.  Furthermore, the Java Runtime Environment is not as ubiquitous as Flash, and only recently has it had an updater that could be trusted to keep it up-to-date (around Java 1.5 and 1.6).

Now, let's not give up just yet.
  • The security policy issue can be improved a couple of ways.  First, simply don't use any features that require special security permissions.  My game doesn't need access to the disk, or arbitrary network access to servers other than the one it was served from.  No, the only thing I wanted to do in my applet was utilize modern practices for mapping rich domain classes to an HTTP friendly data format like XML or YAML.  Thus I wanted to use reflection to serialize and deserialize my domain classes to these text based formats. However, reflection is restricted in the Applet sandbox, and thus requires special permission.  Signing the JAR at least allows the user to see what they're accepting and from who... but it is a hideous limitation of the JRE.  I understand why it was necessary, but it is a flaw of the runtime environment.  Reflection upon my own classes should be perfectly allowable, and only reflection upon JRE or perhaps even 3rd party JARs should be restricted.  
  • The Java splash screen is required, but the image can be replaced by one that you specify.  It's a minor improvement, but the only reason such a loading screen is required must be that the JRE is slow to load, and/or it needs to finish dealing with the above security policies before it can move beyond the loading screen.  And thus the issues are compounded.
  • As for the Java version, there's not much you can do about that one.  Most players of a game such as mine would not know what Java or Flash are.  They just know it's a game that magically works in their browser.  And Flash has long been masterful at keeping this magic transparent, invisible, fast and simple for users.  As of this writing, Flash player 10 is available on 98% of computers running various operating systems.  That's greater penetration than the Windows OS itself.  By contrast, the JRE has about an 80% penetration rate, for all versions -- I have no idea what percentage of which versions are available where... Thus one's only recourse is to put a big note: "Hey you require Java 1.6.  If the game doesn't work, go to ORACLE (a pinnacle of end-user friendliness) for an upgrade."

Credit for what it's worth...

So the end-user experience was the show stopper for me.  It was truly unfortunate, because when it came to developing the game, I was mostly happy.  Like all Java APIs, the Java 2D graphics libraries are overcomplicated and often require chains of various classes and approaches to do something relatively simple.  But that said, I was able to do everything I needed, even if I had to look up a lot of examples from the web.

  • As a side note,  I also built this same game for Android and I found the graphics APIs for Android to be superior to Java 2D.  It's clear why Google did not want to use the plain Java 2D APIs for Android.   And oops, they're getting sued for it.  But I digress...

I was able to easily implement double-buffered scrolling and sprite animations. I was able to use Swing components seamlessly underneath the game view.  Also, there were a lot of good methods for drawing polygons, filled, with alpha transparency and other treats that ultimately allowed me to do exactly what I wanted, even if it took a few extra lines of code and some arm waving to achieve it.

Probably my favourite part of using Java was its ability to play MIDI files.  Although I was ultimately disappointed by excessive complexity and ultimately the inability to control the volume properly (a known issue).  But I love MIDI and think it's totally underutilized today.  It's a very small file (because it uses hardware sound banks) and yet the sound can be very good.  A large reason why it's probably not used in games is that it can be inconsistent.  One computer might sound like a live orchestra, whereas another might sound like a C64. But I love it nevertheless, and being a retro game, the C64 consequence could be a perk for some in this case.

The biggest annoyance, as mentioned, was the graphics APIs.  Between Image, BufferedImage, ImageObservers, Graphics, Graphics2D, Rectangle, Rectangle2D.... it was all too much.  There's at least two of any class that you would expect, and it becomes a chore trying to figure out which is compatible with which, or which you need to do any one thing. It's not unlike other Java APIs that have multiple implementations, like collections and dates to name a couple.


Sidebar: My Learning Goals

Experts recommend that anyone serious about software development should learn one new language per year.  I've generally succeeded at that, although for me, most of them have been cast aside, leaving only Ruby, Java and C# as relevant.  This is not to say that those other languages aren't awesome and great, I just had no need for them.  As hard as I tried, I could not find a place for Groovy, Scala, Go or D in my world (but I'm still trying for D!). That said, learning each new language teaches us not only the language, but the motivations behind it and new techniques for solving problems that we hadn't thought about before.  Learning Ruby made me a better Java developer.

That said, learning a new language, while useful and important, is only part of the story.  Doing the same thing with a different language doesn't teach you as much as you'd think.  It teaches you new ways to solve the same problems, but it doesn't teach you any new problems.  And that's where the excitement and larger value comes in -- at least in my opinion.

Ultimately I decided I wanted to build a game after I watched the entire end credits for Assassin's Creed 2 scroll by.  And then the credits screen for Fallout 3.  I was blown away.  The sheer number of people and amount of effort that goes into a game is astonishing.  The largest enterprise Java project I can think of does not come close.

I wanted to know that.  I don't care what language I have to learn to do it.  

Limited Options

Once I decided against Java, my options were somewhat limited.  Use Flash, HTML5 Canvas + JavaScript... or build a desktop game with Microsoft XNA in C#.  I really wanted a web accessible game though, and HTML5 is really fragmented and young at the moment (here come the emails).

So Flash was it.  

I already had experience with Flex and ActionScript 3.  So the language really was not all that new.  I already knew that I preferred AS3  to Java in almost every respect.  I missed nothing about the Java language, AS3 is cleaner, more modern and enjoyable.  The things I miss were largely in the libraries... like having equals and hashCode available on all classes, and having more advanced collections APIs.   

My first approach is probably pretty predictable... I tried to start with a Flex project.  That is, I figured I could utilize Flex components to lay out the game, use it for the control panel interface and then just slap a canvas on the component tree and draw on it -- much the same approach I took with the Java Applet -- which was a JApplet with Swing components and a JPanel that I drew the game view on.

Unfortunately I was stopped short in my tracks.  Flex dominates the Flash player in such a way that there is little left of actual Flash behavior.  The Stage is overtaken, keyboard inputs swallowed, the rendering pipeline is completely bastardized and redraw regions are completely out of control.  So without any way to use reliable keyboard input or to draw the a specific zone on the screen easily (which I did solve), I decided to abandon Flex altogether. 

Goodbye Application, hello Sprite.

This is where it got scary... and exciting.  Learning was about to happen.  I had to dispense altogether with everything I knew and was comfortable with.  Suddenly I had only a Sprite class and a rendering lifecycle that I had no knowledge of.  I made good use of Google. 

Keep in mind, that I do not own a license for Flash Pro, or Adobe CS.  I was simply coding from scratch, and compiling with the free Flex SDK.  All of my graphics were bitmaps or programmatically created with the vector drawing APIs. This was fine with me, as Flash Pro leans toward the artistic side, and in my hands it would be like watching someone paint with oven mitts on.

I was able to reuse my domain model classes, and in general I kept the same application design. The biggest difference was the graphics APIs and the lifecycle.  

I have to say that while I was not thrilled with the Java graphics libraries, I was also not impressed by the Flash APIs.  For a platform that specializes in this area, I found them cumbersome and limiting.  That said... I know I was developing with Flash a little differently than most people would.  I'm sure typical Flash devs would use Flash Pro to create the graphics and animations ... rather than code them (thus I am not doing as the Romans do in Rome).  Fair enough.  But still... the APIs are pretty disappointing.   I was expecting something more slick and consistent.  Java 2D had a method to draw arbitrary point, filled polygons with alpha transparency.  I could find no such beast in the Flash drawing APIs. I used that to draw conversation bubbles above my characters. But I may have missed it somewhere.  

The other big departure was the control over the rendering lifecycle.  In Java I controlled the main thread (or the main game loop).  In there I accept input, update my game, update the graphics etc.   In Flash, I had to hook into the existing rendering pipeline through an ENTER_FRAME event. This did save me having to manage the thread myself.  But at the same time, I felt like I was hacking into something by latching onto this one event. It was fine, I'm probably just demonstrating discomfort with something new, but it was not how I expected it.  At the end of the day, this is a pretty small part of the code anyway.  However, it was also one of the main keys to performance, which I'll talk to later.

Event Driven Development

But by far the biggest leap that I had to make moving from Java to Flash was from a synchronous programming style to an asynchronous one.  Flash is heavily event driven.  As mentioned above, the very first thing you do to hook into the rendering pipeline is create an event listener for the ENTER_FRAME event.  And that continues for absolutely everything.  You cannot just simply make a web request for an image, wait for the image to load, and then use it.  The second you make the web request, your application continues on and the asset is loaded asynchronously --and fires an event to notify you when it's done.  Yeah, I've coded like this before, of course.  Every rich user interface on earth, be it Swing, WinForms or Flex uses an event based component model.  And they all require management of concurrent activities so that you don't freeze up the application while waiting for DB or network IO, usually with thread, a monitor, a progress bar and a cancel button. 

But this is different.  When building a Flash application, you're always in a state of responding to an event.  Or in double-negative speak:  you're never not in an event handler.  It's event after event, chained together.  After a while it feels like Tarzan swinging from vine-to-vine.  

I'm not complaining though.  Somehow this approach seems right.  After all, requesting an image from the web synchronously and waiting for it would probably be bad for game playability... or any kind of animated application for that matter.  The Flash environment seems to be focused on always moving, and never stopping for anything, ever.  And thinking about the domain, that sounds like exactly what it should do.  

This is what I mean about learning new languages vs. learning new domains... I knew AS3 via Flex already.  But it was the same problem space. Now I knew AS3 and Flash in a whole new way...

That said, I have a whole new appreciation for Flash developers.  To think in these terms is actually quite complex.  You can't think linearly anymore, in terms of doing step A, then B, then C.  You have to be able to start A, do B, and do something interesting before starting C after A is done.  This was mostly felt during the loading of assets.  In the Java version I simply loaded each resource in order, waited for each and then moved on when everything was loaded.  With Flash there was no way to wait, and no way to know what would load first, or when if ever. 

The End Result

Overall I was thrilled with building the game with AS3 and targeting the Flash VM.  The end user experience was better, the language was more fun to work with, the libraries were a trade-off (no winners there).  I feel like this is the right way to build a game, and that I've learned a lot about how games should be built. 


My biggest challenge in Flash came with the control panel that is seen at the bottom of the game window here.  Since Flex was impossible to integrate with the Flash, I thought that I could perhaps use the Flash Controls instead. Yes, Flash has another whole set of buttons, drop lists and other UI components that are not Flex. It's not unlike the Java's AWT controls vs Swing controls.  Unfortunately the Flash controls don't come as part of the free Flex SDK that I was using.  And they are included as FLA binaries, which are not compatible with the command line compiler.  So I was out of luck there. I ultimately found a simple set of open source components online though, called Minimal Comps.  Time will tell if they're the right long term solution.  But for now, they're perfect.

In addition, I had to switch to MP3 for the music, as MIDI is not supported by Flash.  But as I said, I think I'm alone in the MIDI space... I don't think anyone is investing new solutions there.  MP3 is obviously great, but it's also big... which compounds that whole asynchronous asset loading issue.

Performance

Flash does not kill browsers.  Flash developers do.  I should know, as I got pretty good at killing the Flash Player when I first started out. Creating bitmaps with negative sizes, and various other tricks.  That said, I was disappointed a little in that. Sure, it was a programming error... show the error and move on.  But the whole player just tanks on some pretty basic programming errors common during development. 

However, I never experienced a player crash that I did not cause through a programming error.  So blame whomever you want... yes the Flash player should deal with these better.  But bugs are bugs too...

As for performance, I learned very quickly that the key to Flash performance is managing framerate -- or the number of times per second that the ENTER_FRAME event fires. Flash has an upper bounds for framerate that you can set on the Stage.  In addition to that though, each object drawn to the screen will likely need separate framerates depending on the number of frames they have.  For example, my little character sprites have only 2 frames, so 30 FPS will have them moving like Scooby-Doo and Shaggy on the run. So they need about 10 FPS to look right.  But other animations might need 30... or 60 if it's really high quality.  You can always limit downwards, but the upper bounds will be whatever is set on the stage.

The framerate combined with what your code does upon each cycle is the difference between 100% CPU usage, and 5% CPU usage.  If you've ever hit a Flash site that uses 100% of one of your CPU cores, don't blame Adobe... 

With both the Java and Flash versions I could easily pin the CPU.  But given the same framerate settings, I was able to keep them both under 5% CPU usage on my i5.  

Java

Flash

So there you have it.

That's my early experience with game development.  I'm having a blast and learning a lot.  I hope to release this game in some capacity, at some point.  I'm trying to keep the engine flexible to allow for better graphics and animations with more than 2 frames.  In the mean time, I encourage software developers to not just learn new languages, but learn to do new and different things with them.

Cheers,
Clinton

Saturday, January 17, 2009

Gordon Ramsay's Kitchens are Agile

Software Development is a practice in its infancy, so we often find ourselves looking to older practices for direction.

Any analogy is at risk of being flawed, irrelevant or even ridiculous. Yet, analogies are not without their value in helping to explain software development concepts.

This is especially true of Agile Methods. It's very hard to explain to someone why Agile might be better.

For years software has been compared to everything from building buildings or bridges, to automobiles. Furthermore, debates have raged around whether software development is an engineering discipline, a science or simply art.

So what's one more analogy then?

Like cooking, I don't believe software development is an art or a science exclusively -- it's a little of both. Also, if you've ever seen the chaos that is a restaurant kitchen, you could easily agree that the team constantly deals with new requirements, changes and problems. And like food, software is very much subject to a great deal of personal preference, perception and opinion -- taste.

Let's consider the roles in a restaurant and how they compare with a software project respectively:

  • General Manager -> Project Manager: Greet and seat the customers, to manage scope (how many people are seated at a time given the amount of staff and complexity of the food or requirements). Also a point of escalation when things go wrong.
  • Service Staff -> Analysts: Take orders from the customers, or in other words, gather requirements. They also ensure requirements are met to the satisfaction of the customer.
  • Cooks -> Developers: Implement the requirements based on the specifications (order/ticket) provided by the analyst. They taste the food to make sure it's what they expect (unit test?).
  • Head Chef -> Practice Lead: Ensures that processes are followed and that high standards are met. Watches food on the way out to make sure it looks good and meets the specifications. Also keeps the team energized and motivated. In some software organizations this is an Architect or a Q/A Lead. In others it might be the Technical Lead or Senior Developer.
The Agile Restaurant

  • General Manager greets Customer at the door, and seats them if a table is available.
  • Once seated, the Server collects the Customer's order.
  • The Server places the order on a ticket queue (a Story Wall!)
  • The Head Chef calls out the order and prioritizes them to ensure entire tables are served at once and on time, but also identifies any opportunities to improve efficiency.
  • The Cooks implement the orders as they're called out and may refer to the ticket as necessary. They taste (test) their food, to ensure that it turns out the way they intended. They report back regularly to the Head Chef with time remaining on the order.
  • When the order has been prepared, the Head Chef inspects it quickly for quality.
  • The Server (who is also able to make a quick sanity check that the order at least looks right) delivers the order to the Customer
  • In the event that the Customer is not happy, the Server can return the order to the kitchen, at which point the Head Chef can re-prioritize and correct the order.
  • In certain cases it may be required to involve the General Manager if an issue is unresolvable.



Does this sound familiar? It should if you've ever been on an Agile project. Customers are served in order of priority (arrival time in this case), there are a number of immediate check-points, and issues are resolved and reprioritized in a timely fashion. The key is that the feedback loops are short and tight.

How would a restaurant run with Waterfall?

  • 5pm - 6pm: Seat all the customers.
  • 6pm - 7pm: Collect all orders.
  • 7pm - 8pm: Head Chef prioritizes and calls out all orders.
  • 8pm - 10pm: Cooks prepare all of the orders.
  • 10pm - 11pm: Head Chef checks every order, sends back all of the wrong orders.
  • 11pm - 12pm: Cooks fix all of the wrong orders.
  • 12pm - 1am: Analysts deliver all of the orders to the customers,
  • and return all of the incorrect orders (probably cold from sitting there during the previous check/fix cycle -- stale requirements!)
  • 1am - 2am: Developers err.... Cooks flail around trying to correct old orders originally received hours ago, for customers that have been waiting for hours.
One could argue that even in the Agile case, customers could end up waiting and errors will be made. But there's certainly no case for Waterfall making the situation any better. There are some teams with dysfunctions that Agile cannot correct. But for teams that are disciplined or well organized (or can be improved to be so), Agile allows teams to satisfy customers sooner and deal with problems closer to the point at which they were created.




Tuesday, July 29, 2008

Open Source, Forks, Incompatibilities and Misconceptions

Today I read a blog post about JavaFX and how it's going to take over the world because it works well on Linux... I'll leave that alone.

What was more interesting were comments from James Ward, Technical Evangelist for Flex at Adobe and Adobe’s JCP representative. Or "RIA Cowboy" for short. His comments were as follows:

Adobe continues to try and find the tough balance between more openness and what our developers want. Most of our developers tell us not to open source the Flash Player because it would lead to forks and incompatibilities in the run time ~ James Ward
And then...
[Open Source] certainly doesn't prevent them. I use and love Linux which is full of forks and incompatibilities. ~ James Ward

Unfortunately I think James is falling into the same mental trap that a lot of people do when it comes to open source and licensing. That is, that Open Source means Linux and GPL. 


It does not.

Let's look at the key aspects of a software license, or at least 4 that we care about for the purposes of this discussion. I'll avoid legalese (because I'm not a lawyer) and talk in plain English:

* Open vs. Closed Source
* Free vs. Pay
* Extensible, Reusable vs. Fixed, Controlled
* Copyright


Open vs. Closed
Source


This means nothing more than that developers and others are allowed to see the code. Period. It does not necessarily allow them to change, modify or use freely the software or its source code. Many commercial products are open sourced and yet locked down in every other conceivable way.


Free vs. Pay


...is pretty obviously whether or not customers/users need to pay before using the software and/or having the source available to them. Many companies offer source code as an option at a cost.


Extensible, Reusable vs. Fixed, Controlled.


This is the important point that I think James and others may not be aware of. Licenses like GPL and Apache (although very different) both explicitly allow for extension and reuse in software and binary form. Apache only requires that you give credit where credit is due, whereas the GPL adds a few extra encumbrances, such as you have to also GPL your contributions and make your source available (the LGPL lightens this up for libraries and frameworks).

-- So if Adobe were to open source AIR, they could easily do so by creating a Free, Open Source license with specific restrictions around extensions and code reuse. Sun and even Microsoft (!) have similar licenses.


Finally, Copyright.

The ONLY reason I believe Adobe doesn't release their code is that they're worried Microsoft and Sun would then be able to read it and steal their IP. My first thought on that is that it's BS... big companies reverse engineer stuff whether the source is available or not. Second, this is not an issue of open source, it's an issue of licensing and IP protection -- a copyright. If they blatantly steal code then Adobe can sue them. Similarly, if it's a very unique and valuable approach (an innovation), then a patent can protect them -- although we all know how broken the patent system is in the US and Canada, so I'm not recommending it...

Key point: Don't use such things as an excuse for not open sourcing the Flash/Flex/Air platform. There are plenty of licensing options.

Homework: Without opening Google, can you name
name just one more open source project [other than Linux] that is rife with forks and incompatibilities... ?

If we need a counter example to Linux, look at the Apache Web Server. It's open source, and more freely so (not GPL, but Apache), and yet how many forks of the Apache Web Server are there? I know of not a single one. In the case of Linux I believe forks are a part of their unique culture. If we look at the broad spectrum of open source software, I think we'll find Linux is a unique example of a project that forks almost uncontrollably and unnecessarily.

I think you'd be hard pressed to find another example that's even relevant (like a smaller framework or tool like similar to Adobe Flash/Flex/AIR).

Conclusion

Forks and incompatibilities have nothing to do with open source. They are more an issue of culture and licensing terms.

Would Adobe Flash/Flex/AIR fork or become incompatible with a proper open source strategy and an intelligent license?

No.

Tuesday, July 15, 2008

Dwemthy’s Array in Java

Dwemthy's Array is an uber-geeky text based adventure game with a specific coding challenge built in, and is particularly suited to implementation with a dynamic language such as Ruby. What caught my attention was by Adrian Kuhn's implementation in Java. Despite my love-hate (or like-hate) relationship with Java, I'm always up for a coding challenge, especially when it's Java vs. Ruby...(continued)

See my full implementation at:
=>
http://clintonbegin.com/dwemthy/

Unfortunately Blogger isn't very good for posting code the way I needed to.

Tuesday, July 8, 2008

JSR 166

It's been a while since I've posted, so I thought I'd say something nice. While I know I have a lot of criticism for Sun and the JCP, I always try to give credit where credit is due.

Sun/JCP has finally done something right...

http://gee.cs.oswego.edu/dl/jsr166/dist/jsr166ydocs/jsr166y/Phaser.html

They've given us a Phaser class so we can kill ourselves in the event we actually have to use Java 7. ;-)

Cheers,
Clinton