Category Archives: Programming

Building software.

Under 1,000

I spent pretty much the whole day working on the codebase.  Every error clobbered is becoming a pretty hard-fought battle.  Even so, I seem to be winning.  Of course, after the errors are done, that doesn’t mean the work is over — far from it.

The error count is now down to 949.

.Net XML Serialization

Most of the saving and loading of MUD data in Basterne 2 was handled by low-level C functions that used a custom text file format for each data type. That is just a bad idea.

Save and load functions were typically dozens or hundreds of lines of code with lots of room for error and data loss. If a word was off by one space or a character was miscapitalized, a file load would fail and nothing could be done. Players of Basternae 2 probably remember pfile corruption issues with anything but fondness.

Every time we changed a file format by adding another value to an object, we had to build a translator that would convert the old file to the new version. Usually this wasn’t too hard, but it did result in a LOT of extra code lying around where it didn’t belong.

.Net makes this a lot better by giving us XML serialization.

With XML serialization, data is dumped out in a neat little XML file that can be read not only by our MUD server, but by just about anything that can read XML, which these days is just about every application in existence. Gone are the days of low-level custom file formats and painful data corruption issues (I hope).

So, how do we do it?

For example, I just rewrote the saving and loading of fraglists to use XML serialization. The code started as 174 lines of custom textfile saving and loading with lots of recursive looping to save and read the arrays containing numbers on the frag list by race and class.

Now the code looks like:

public void Load()
{
XmlSerializer serializer = new XmlSerializer( this.GetType() );
Stream stream = new FileStream( Database.SYSTEM_DIR + Database.FRAG_FILE, FileMode.Open,
FileAccess.Read, FileShare.None );
fraglist = (frag_data)serializer.Deserialize(stream);
stream.Close();
}

public void Save()
{
XmlSerializer serializer = new XmlSerializer( this.GetType() );
Stream stream = new FileStream( Database.SYSTEM_DIR + Database.FRAG_FILE, FileMode.Create,
FileAccess.Write, FileShare.None );
serializer.Serialize( stream, this );
stream.Close();
}

Cleaner and better, just the way we like it. I still hate the way WordPress formats source code.

The compile error count is now down to 1,649.

Under 2,000

The compile error count is now down to 1,900. It feels like there will be a running codebase relatively soon.

Shiny New Logs

Today I replaced the old-school C-style logging routines with new C# code based on the System.Diagnostics.TraceListener class.  That may not mean much to you, but trust me — logging is way better now.

The compile error count is now down to 2,205.

Random Numbers

I finally got around to replacing the old outmoded random number generator that the MUD has been using for ages. It’s the Mitchell-Moore algorithm and is present in just about every free codebase.

I ended up building a class that lets you select one of three random number generators to run the MUD engine on.

The first is the plain-old slow, ineffecient, and repetitive .NET random number generator.

The second is the R250/521 shift-register sequence generator. There’s a paper on it here:
http://www.informs-cs.org/wsc97papers/0127.PDF

The third is the Mersenne Twister, a fairly new algorithm by Matsumora and Nishimura. More information is available here:
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html

The neat thing about how I’ve set up the random number system is that new algorithms can be dropped in easily. I’m sure there won’t be much cause to change random number generators, but it’s a pretty easy option if we feel like it.

We’re now down to 2,591 compiler errors.

More Progress

The compile error count is now down to 2,654.

I’ve also added a codebase section to FindMUD since it seems that codebase downloads are getting harder and harder to track down these days.  There are only a handful posted, but I expect that the list will grow as the site evolves.

Under 3,000

The compile error count is now down to 2,779.

Some of the remaining code rewrites remaining are pretty significant:

  • Telnet/Network Code.
  • Data File Saving/Loading (I’m seriously considering rewriting data storage as database-based rather than XML-based).
  • Low-level string processing routines.

If you think about it, those three things are all you need to build a MUD (well, that and a command interpreter). Even though progress has been good lately, there are a couple whole-weekend projects coming up. With me house shopping and the holiday season creeping up I couldn’t possibly imagine having a running server before the new year.

A New Website

I’ve spent the past day putting together a site called FindMUD. It’s pretty similar in concept to The MUD Connector, but done in my own way. I’m not sure the world needs another MUD listing site, but it was fun to create. Each listing can be rated by users, so as the site develops you’ll be able to get a pretty good picture of what the best MUDs are.

Feel free to visit the site, create a login, and add listings for your favorite MUD(s). Let me know what you think of the interface and whether I left anything important out of what is asked for when submitting a new MUD.

Since the site is brand new (only 6 listings so far!), I’m sure there will be plenty of room for improvement.

A Pretty Good Chunk

Compile errors are now down to 3,293.

It feels a little strange to re-implement some of the core C-language character functions in C#.  Things like isdigit(), isspace(), and isalpha() are easy enough to rewrite, but there’s just something a little surreal about it.

Only A Stupid Programmer Would Try To Be Clever

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”.

A Custom Client

I don’t believe I’ve mentioned it here yet, but around 2005-2007 wrote a custom MUD client using wxWidgets and SDL. It runs fairly well on Windows and tolerably on Linux (there are one or two glitches). I designed it to work with Duris and its prompt system. It shows maps for a zone if they’re available and keeps track of hits, mana, and moves via a meter system. The graphics are pretty simple, although I had planned to improve them a bit. This program was written from scratch and could form the basis for a custom hybrid 2-d/text-based Basternae client at some point.

Here’s a screenshot (while playing Duris):


(Click to enlarge)

Another Good Chunk

Down to 4,539 errors.

Most of the rest of the errors to be squashed are going to be pretty involved, so I expect slower progress for a while.  Even so, we’re not too far from zero now.

Just 100

The error count is down 100 to 5,704.

I spent this week sending out the second and last issue of my fantasy/sci-fi magazine, All Possible Worlds. Although I run a dozen or so other websites, I might be able to put more time into the Basternae code without the magazine stealing my time. It is/was a pretty cool little magazine, but it’s a massive drain on the time and finances to run.

A Tiny Update

The error count is now down to 5,804.

Working on the Basternae code is a lot of fun, but I don’t get as much time to do it as I would like.  Even so, at least progress is being made.