Archive for the 'Uncategorized' Category

Sorting The Easy Way

Tuesday, April 22nd, 2008

If you have a list of objects, say List<Widget> it’s mighty easy to to sort them using .NET.

First, derive the Widget class from IComparable:

public class Widget : IComparable

Then create the CompareTo method. In this case, we’re sorting the list of Widgets by name:

public int CompareTo(object obj)
{
  if( !(obj is SynthProfile ))
  {
    return 0; // We could throw an exception here if we wanted to.
  }
  return this.Name.CompareTo(((SynthProfile)obj).Name);
}

Once that’s done all you have to do is call:

WidgetList.Sort();

Easy.

Technology Always Fails

Thursday, April 10th, 2008

There’s only so many times I can see a message like:

“Error loading operating system”

Before it starts to get frustrating.

Won’t be much progress today.  I love computers.

Upgrading to Vista Ultimate

Thursday, March 27th, 2008

I hate updating software.  Many years of painful upgrade experiences have taught me not to upgrade a piece of software unless it’s horribly broken or lacks the ability to get anything done.

That’s why I was dreadfully, terribly afraid of upgrading from Windows XP to Vista on my home desktop.  Sure I’ve been running Vista since summer 2007 at work, but I don’t do much other than software development on my work PC.

Knowing full well that a clean install is the only safe way to go and that Windows always needs more RAM, I picked up a new hard drive and an extra gigabyte of RAM (memory sure is cheap these days!)  It also doesn’t help that I was running an original Windows XP Pro install from late 2001 (which has gone through half a dozen hardware upgrades on the same install).  It was tired and in need of a clean slate.

The install was remarkably, stunningly painless.  There were all sorts of things that just worked without the installer wizard having to ask me dumb questions.  It was so smooth — even to the point that it auto-capitalized the registration key while I was typing so I didn’t have to worry about whether I had caps lock on.

I almost wish I had something bad to say, but nothing went wrong and everything worked.  This is nothing like setting up an NT 4.0 system, which had a way of running differently every time you installed it on the exact same hardware configuration.

The folks who fear Vista, think it’s broken or too evil to use:  They’re idiots.  It works, and it works better than any Microsoft OS yet.  If you have the hardware to run it you probably ought to.  Now that it’s SP1 and most software developers have worked out their compatibility glitches it’s mature enough to use regularly.

My only complaints:  Folder views defaulting to music (it’s a folder full of DLLs you idiot, show me the size, date, and version — not artist, album, and rating!); kind of annoying navigating in Windows Explorer (it’s an extra click or two to get where I’m going); the folks at Winamp still haven’t gotten their act together for Vista (crash, crash, crash!)

Memory Usage

Saturday, March 15th, 2008

When booting up the MUD engine with only my two zones (the Thri-Kreen hometown and the Kobold Village), Basternae 3 takes up 22,256 kilobytes of RAM.

That somehow manages to drop to 20,096 kilobytes of RAM when I connect with a single player.  I think it was probably just timed to coincide with a garbage collection.  Moving around for a while brings it back up to 20,500.

A boot up with no zones loaded starts at 22,116 kilobytes of RAM.

So, at this point the MUD engine takes about 22 megabytes of RAM plus however many zones are loaded.  I can’t imagine players taking more than 8 kilobytes, and with zones taking up about 1 megabyte per 3000 rooms, we’ve got plenty of room to grow.

With 32 megabytes available, we could support:

27,000 rooms
128 simultaneous players

No problem.

The entry-level VPN servers I’ve been looking at start at 256, but that’s for OS plus applications.  It’ll be running Linux so I’ll have about 128 megs for OS and 128 for apps.  We could run 300,000 rooms and nearly 800 players before needing to upgrade.

I definitely don’t miss the “bad old days”.  When I first started writing MUD code it was normal to only have 32 MB in an entire system.

HTML Help Workshop = Epic Fail.

Wednesday, March 12th, 2008

Anyone who has wondered why the help systems in Windows applications are typically so abysmal has to look no further than Microsoft’s HTML Help Workshop. Not only is rotting tripe more pleasant to deal with, it’s more likely to help you get the job done.

You’d think that maybe an application designed for editing help files would have a decent help system. Not so.

After trying to figure out the interface to HTML Help Workshop and failing (after getting screens full of incomprehensible errors) I went in search of an application with a less counterintuitive interface. I found HelpNDoc. It looks a lot prettier, but it’s also completely incomprehensible. It might not be all that bad, but after my HHW-induced brain meltdown I just didn’t have the patience to decrypt the interface.

I guess if you were meant to be able to get useful information out of the help files for an application they would have made it easy. Failures like Microsoft HHW are why the software training industry is a massive cash cow.

I ended up falling back on the manual method — creating the index, table of contents, and individual topic files by hand. This is the same method I used when creating the help files for PixelSwapper. To put it in perspective, having to do so is just as retarded as writing a letter to your congresscritter using a hex editor because your word processing program is too terrible to use.

Luckily the file formats are well-documented. Not so much by Microsoft, but by the folks who made the Win32 API usable — wxWidgets. I’ve used wxWidgets extensively, and if you’re going to write an app for Windows without using .NET it’s the only sane way to go. Down the path of pure Win32 lies madness. Yes, the Windows API was designed by Cthulhu. Luckily he has since moved on to the world of embedded design and development (cellphones mostly), so post-Win32 Windows programming is no longer painful.

Rebuilding An Engine

Sunday, December 30th, 2007

Most of the work done over the past few days has been boring, grueling engine rewrite stuff.  There’s not much exciting that can be said about reorganizing variables, changing the way that lists are processed, and cleaning up object reference handling.  It’s a lot of code rewriting that will have little or no visible effect on the way the game is played.  It will, however, make it far easier to add to and update the code with our planned changes.

IT App Monitor Robot

Wednesday, December 19th, 2007

This post on The Daily WTF reminds me of the startup script we had for Basternae 2 in the early days:

http://thedailywtf.com/Articles/ITAPPMONROBOT.aspx

Since the codebase would tend to hang or crash, we would have to watch for the process and restart it if it was missing. If the CPU usage was above a certain amount for more than about a minute it also told us that the code was in and endless loop, so the process would have to be killed and restarted. It wasn’t not quite as intricate as the WTF setup, but it was still pretty kludged.

Bought a House

Friday, December 7th, 2007

I just bought a new house, so chances are I won’t be able to devote as much time to Basternae over the next two months.

Should I learn C first? Or C++?

Monday, November 12th, 2007

Every once in a while, a non-programmer interested in game programming will ask me where they should start learning. They’ll usually know that most modern programming languages are derived from or related to C and that most games are developed in C++, so the first question I hear will usually be:

Should I learn C first? Or C++?

My answer is: neither. I recommend learning C# first.

There are so many bad habits and terrible practices you can develop by starting with C or C++ first that it’s a bad road to go down. By starting with C# first you’ll be forced to learn how to develop object-oriented code instead of being able to mix an ugly blend of procedural and object code with no method to the madness.

The transition from C to C++ can be very easy — almost any code written in C will run unmodified in C++, with the biggest danger being that you might have used reserved keywords as variable names.

That’s very dangerous.

In C++ there are a lot of things that can be done that just shouldn’t be — especially global variables and constants, un-encapsulated data and procedural code, etc. The C++ compiler doesn’t care how badly-written your code is as long as you follow proper syntax. Some people spend years trying to “unlearn” C programming once they transition to C++. I know I had a hard time with it, and entire books have been written on the subject.

Instead, if you eventually end up making the transition from C# to C++, you’ll already be used to the idea of being able to use library functions and be less-inclined to “roll your own” version of things when you can find a library to take care of it for you. This will result in faster development — after all, it’s often far easier to plug something in or look up what’s available in the standard library than it is to write and debug new code, which C trains a programmer to do more often. Remember: an architect doesn’t reinvent the nail every time he designs a house.

Some other reasons to learn C# first:

  • C# is also good as a first language because it’s a very easy to make a transition to Java. Some people refer to C# as a “child of C++ and Java”.
  • With Microsoft’s Silverlight (designed as a Flash-killer), C# is also becoming more relevant for developing web-based games.
  • Every person I know that has tried to learn programming and given up has died at the same point: pointers. With C# you don’t have such a mind-boggling mess of things and are far less likely to have weird pointer-based crashes and glitches. You never have to learn the abstract insanity that is pointer arithmetic and memory manipulation. (void ** pointers still make me wake up screaming sometimes.

Only A Stupid Programmer Would Try To Be Clever

Friday, November 2nd, 2007

Huh?

It takes an insecure jerk to write a clever line of code like this:

x?x=y=z=0:x=y=z+2;

What does it say? If X is true, set X, Y, and Z to 0, but if X is false, set X and Y to Z+2. It’s a shortcut written by someone who thought they were being clever. To an experienced programmer it’s readable, but still a little uncomfortable to deal with if you’re trying to trace a bug. The longhand version would be something like (note descriptive variable names):

if( LeftPosition != 0 )
{
LeftPosition = 0;
MiddlePosition = 0;
RightPosition = 0;
}
else
{
LeftPosition = RightPosition + 2;
MiddlePosition = RightPosition +2;
}

(Irritating failure to indent courtesy of Wordpress.)

It’s longer to type, but a casual reader is more likely to be able to understand the code at first glance without having to re-read or decipher it.

If you read through any sizeable codebase you’ll find a ton of things done just for the sake of being “clever”. A lot of these are shorthand or obfuscation that has no real purpose since the compiler will generate the same executable code in most cases.

I think it boils down to insecurity. Programmers who don’t have much confidence in their abilities try to be clever and show off. Skilled programmers know a fundamental point about writing code:

Not everyone is as smart or skilled as I am, so I need to make this as simple and readable as possible.

Keeping this point in mind often results in code that is easier to debug, better-commented, and simpler to read.

Cleverness is quite possibly the number one reason why people prefer to scrap and rewrite a codebase rather than fix the problems in it. Code is easier to write than it is to read, and if it’s made all that more unreadable by an ineloquent programmer, then it’s less likely to stand the test of time.

Be a real programmer. Write simple, clear, understandable code. You will have to read your own code someday. Do you really want blood to shoot out of your eyes when you try to read it?

For further reading, check out the infamous “How to Write Unmaintainable Code”.