{"id":13,"date":"2013-03-15T10:36:33","date_gmt":"2013-03-15T10:36:33","guid":{"rendered":"http:\/\/www.kamremake.com\/devblog\/?p=13"},"modified":"2013-05-13T10:45:24","modified_gmt":"2013-05-13T10:45:24","slug":"pascal-the-best-of-both-worlds","status":"publish","type":"post","link":"https:\/\/www.kamremake.com\/devblog\/pascal-the-best-of-both-worlds\/","title":{"rendered":"Pascal: The best of both worlds"},"content":{"rendered":"<p>We regularly get emails saying <em>&#8220;Why aren&#8217;t you using a normal language like C++?<\/em>&#8220;. That&#8217;s often followed by <em>&#8220;If you want to move to C++ I can help you&#8221;,<\/em> 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).<\/p>\n<p><!--more-->This is not a &#8220;why Pascal is better than C&#8221; article, but instead I will explain why Pascal is an appropriate language for the KaM Remake project.<\/p>\n<p>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.<\/p>\n<p>Note: When I refer to Pascal in this article I mean Object Pascal (Delphi and Lazarus), not plain Pascal which doesn&#8217;t have objects\/classes.<\/p>\n<h3><strong>Requirements<\/strong><\/h3>\n<p>What are the language requirements of a project like the KaM Remake? We don&#8217;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&#215;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.<\/p>\n<h3><strong>High level features<br \/>\n<\/strong><\/h3>\n<p>To me Pascal seems like a good compromise between higher level language features and performance. It compiles straight to machine code and doesn&#8217;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&#8217;m not saying any of these are bad features, but they do have a\u00c2\u00a0performance 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++.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Object oriented<\/strong><\/span><br \/>\nThe 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&#8217;ve had to work with Java a lot, and I&#8217;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\u00c2\u00a0 efficiency that can compete with a low level language like C++.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Sets, arrays and strings<br \/>\n<\/strong><\/span>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.<\/p>\n<p>Strings are built into the language and can be used much like you would expect in a high level language.<\/p>\n<p>Arrays are also a pleasure to use. If you don&#8217;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[2]).<\/p>\n<h3><strong>Performance<\/strong><\/h3>\n<p>Pascal has good performance. I&#8217;ve never tested it myself but from what I&#8217;ve read both Delphi and FPC are comparable with C++, and certainly well ahead of higher level languages like C# and Java.<\/p>\n<p>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.<\/p>\n<h3><strong>Cross platform support<\/strong><\/h3>\n<p>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&#8217;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&#8217;t put effort into making and maintaining a Linux build because all our Linux users say it runs perfectly under Wine (Windows Emulator). There&#8217;s been very little interest in a Mac port, so we haven&#8217;t experimented there either. We&#8217;ve ported the server to Linux, that was very easy as it doesn&#8217;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&#8217;s a whole different CPU architecture after all).<\/p>\n<h3><strong>Popularity<\/strong><\/h3>\n<p>Pascal is certainly an uncommon language, although mostly thanks to Free Pascal and Lazarus there&#8217;s still an active community. There&#8217;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&#8217;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.<\/p>\n<p>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&#8217;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&#8217;s not the language that&#8217;s the limiting factor, just a lack of motivated developers.<\/p>\n<p>It&#8217;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.<\/p>\n<h3><strong>Libraries<\/strong><\/h3>\n<p>We haven&#8217;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.<\/p>\n<h3><strong>Conclusion<\/strong><\/h3>\n<p>I&#8217;m not saying Pascal is the perfect language for everything, if efficiency doesn&#8217;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&#8217;t have\u00c2\u00a0anything 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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We regularly get emails saying &#8220;Why aren&#8217;t you using a normal language like C++?&#8220;. That&#8217;s often followed by &#8220;If you want to move to C++ I can help you&#8221;, as if they are offering us salvation from Pascal. The reasons &hellip; <a href=\"https:\/\/www.kamremake.com\/devblog\/pascal-the-best-of-both-worlds\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/www.kamremake.com\/devblog\/wp-json\/wp\/v2\/posts\/13"}],"collection":[{"href":"https:\/\/www.kamremake.com\/devblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kamremake.com\/devblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kamremake.com\/devblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kamremake.com\/devblog\/wp-json\/wp\/v2\/comments?post=13"}],"version-history":[{"count":38,"href":"https:\/\/www.kamremake.com\/devblog\/wp-json\/wp\/v2\/posts\/13\/revisions"}],"predecessor-version":[{"id":61,"href":"https:\/\/www.kamremake.com\/devblog\/wp-json\/wp\/v2\/posts\/13\/revisions\/61"}],"wp:attachment":[{"href":"https:\/\/www.kamremake.com\/devblog\/wp-json\/wp\/v2\/media?parent=13"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kamremake.com\/devblog\/wp-json\/wp\/v2\/categories?post=13"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kamremake.com\/devblog\/wp-json\/wp\/v2\/tags?post=13"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}