We regularly get emails saying “Why aren’t you using a normal language like C++?“. That’s often followed by “If you want to move to C++ I can help you”, as if they are offering us salvation from Pascal. The reasons people give for C++ being a better choice than Pascal are usually cross platform support, performance and being widely used. This is something I feel quite strongly about so it seems like a good article to start off the blog (and from now on when we get those emails I can just send people a link to this article :P).
This is not a “why Pascal is better than C” article, but instead I will explain why Pascal is an appropriate language for the KaM Remake project.
Pascal is an old language published in 1970. It was made popular in the 1990s by a commercial product called Delphi (which is still being developed and sold today) and more recently by Free Pascal and Lazarus which offer a free and open source alternative to Delphi.
Note: When I refer to Pascal in this article I mean Object Pascal (Delphi and Lazarus), not plain Pascal which doesn’t have objects/classes.
What are the language requirements of a project like the KaM Remake? We don’t require blisteringly fast performance and manual memory management that you might need on something like a modern first person shooter rendering engine, but we do need fairly good performance because we are dealing with a real-time multiplayer game on large maps (256×256 = 65,536 tiles) and possibly thousands of units existing at once, all of which require pathfinding and other CPU intensive operations. We also want it to run on fairly low end PCs. This rules out a lot of high level languages for performance reasons.
High level features
To me Pascal seems like a good compromise between higher level language features and performance. It compiles straight to machine code and doesn’t have the features that often slow down very high level languages such as virtual machines/interpreted code, fully automated garbage collection, implicit typing, etc. I’m not saying any of these are bad features, but they do have a performance cost. However, Pascal does support some high level features such as inheritance, abstract classes, overloading, properties, sets, easy to use strings and arrays, etc. which make it easier to use than low level languages like C++.
The object oriented features of Pascal have allowed us keep the project maintainable and easy to add new features as it continues to grow in size. Through my University course I’ve had to work with Java a lot, and I’ve been impressed (and sometimes frustrated) by the multitude of object oriented features it supports. However, in my opinion Object Pascal supports most of the really useful object oriented features that Java has whilst still having efficiency that can compete with a low level language like C++.
Sets, arrays and strings
I think sets are one of the really interesting features of Pascal. There are number of places in our code where it would be painful to use anything else. Sets are not a low level concept (consider their importance in mathematics) and yet they can be compiled into very efficient machine code using bitmasks and bitwise operations.
Strings are built into the language and can be used much like you would expect in a high level language.
Arrays are also a pleasure to use. If you don’t need to change the size you can use a static array (which allows the compiler to perform more optimisation), and if you do need to change the size you can use a dynamic array. Multidimensional arrays are straight forward. You can also index static arrays by enumerators which is very useful to make lookup tables and avoid using meaningless numbers (for example Land[X,Y].WalkConnect[wcRoad] is much more meaningful than Land[X,Y].WalkConnect).
Pascal has good performance. I’ve never tested it myself but from what I’ve read both Delphi and FPC are comparable with C++, and certainly well ahead of higher level languages like C# and Java.
I like the fact that you can still use low level features like pointers and embedded assembly code, but you rarely ever want to or need to. For example you can treat arrays and strings as blocks of memory like you would in C/C++, which is useful for loading, saving or transmitting them. This also allows gives you more control when optimising your code.
Cross platform support
The Free Pascal Compiler (FPC) compiles for Windows, Linux, Mac, even Raspberry Pi and Android. Lazarus can be used as a graphical front end to Free Pascal, like Delphi but free. We have made our code compile on Linux without much difficulty, but we still need to sort out some libraries and system calls to get everything working smoothly (you’d have this problem in other compiled languages such as C++ too, porting always takes a bit of effort because libraries and system calls vary between operating systems). So far we haven’t put effort into making and maintaining a Linux build because all our Linux users say it runs perfectly under Wine (Windows Emulator). There’s been very little interest in a Mac port, so we haven’t experimented there either. We’ve ported the server to Linux, that was very easy as it doesn’t use many libraries (just networking really). Somebody even compiled the Linux version of the server to Raspberry Pi (without any code changes required) which I think is pretty cool (that’s a whole different CPU architecture after all).
Pascal is certainly an uncommon language, although mostly thanks to Free Pascal and Lazarus there’s still an active community. There’s plenty of documentation and example code available on the internet. Pascal is an easy to learn language and the structures are similar to other object oriented languages. Translating code between Pascal and C# or Java is not hard, we’ve done this for certain algorithms that we borrowed which were written in another language. Being a good programmer is not about knowing the syntax of a language, but about knowing concepts and being able to apply them regardless of the language.
Using a more popular language does not necessarily mean that we would have more developers willing to assist us. When people have contacted us saying they are interested in helping us with development but do not want to learn Pascal, we have suggested they make tools/utilities that are needed or write DLLs in their language of choice, yet no one has taken us up on these offers. We’ve also had various Pascal developers join us for a short time, but they all lost motivation and disappeared quite quickly. So it seems like it’s not the language that’s the limiting factor, just a lack of motivated developers.
It’s worth noting that some widely used software such as Skype and InnoSetup were written in Pascal, both of which are successful and well known.
We haven’t had any problems finding libraries to use, in fact there are some really excellent libraries written for Pascal. For example our crash reporting library madExcept is astonishingly good, it has almost zero overhead yet provides a complete stack trace (including line numbers!) as well as lots of other useful information, and uploads this along with any other files we choose to include such as a replay of the game to our server as a ZIP file. This has made fixing crashes extremely easy for us and greatly sped up our community beta testing process. PascalScript is another excellent library that we use for dynamic mission scripts. It compiles and runs Pascal code written by map authors to allow missions to be dynamic and complex in ways which were never possible before. It was very easy for us to use and seems to run very efficiently. We also had no problems finding up to date wrappers for well known libraries such as OpenGL, OpenAL and zlib.
I’m not saying Pascal is the perfect language for everything, if efficiency doesn’t matter so much then there are higher level languages that have much better cross platform support and higher level abstractions/features. I also don’t have anything against low level languages like C++, sometimes you need the extra control and speed of a lower level language. If I was writing a high performance computation algorithm which required extreme optimisation and manual memory management, C++ would be a better choice. But for a project like ours Pascal seems like a good compromise. It has the efficiency of low level languages yet some of the features of high level languages which make development faster and easier to maintain. In my opinion it really does give us the best of both worlds.