Bay 12 Games Forum

Other Projects => Curses => Topic started by: zaroth on May 09, 2014, 01:10:29 am

Title: LCS code refactoring effort
Post by: zaroth on May 09, 2014, 01:10:29 am
While in the Github thread (http://www.bay12forums.com/smf/index.php?topic=138426.0) I posted about the more general stuff concerning further LCS development, here I'd like to keep things pretty specific. I'm talking about refactoring the source code.

Me and Lasica, while surfing through LCS code, found some pretty good reasons for some refactoring work to be done:

In order to learn what was done in this direction, we've skimmed the forum, commit logs, code etc. and found what follows:


If you know about other important architecture discussions or documents that I've missed, please link them here, as we certainly do not wish to reinvent the wheel. Of course, it would be best if the developers themselves could comment on these issues (especially Toady One, Jonathan S. Fox, Addie MacGruer, blomkvist, grundee and yetisyny - who seem to be veterans, having fought many battles in code for the liberal cause), but we'll take what we can :) Also, on a bit unrelated note, we haven't looked at the portability / pdcurses etc. part of code, if someone could shed some light on how/why things are done there, it'd be great.

Since refactoring isn't easy work, we'll probably mostly plan it / do it in small steps for now, but here are some possibilities that we are considering:
Title: Re: LCS code refactoring effort
Post by: AddieMacGruer on May 09, 2014, 10:56:23 am
Hi Zaroth

Thanks very much to the invite to the forums.  I hadn't realised you guys had paid so much attention to my android port of the game; it's made my weekend and will be raising a glass to you all later.

I'd been going through a quiet period at work -- having just got my first android phone, an S2 -- and was looking for a project to occupy my time.  The android store certainly doesn't need another fart app, so was looking for something reasonably chunky and open-source that I could play with.  I'm a massive DF fan, and had been looking through the rest of Toady's work.  I'd played a few games of Liberal Crime Squad in DosBox, and loved the concept, despite being rubbish at it.  (I still am.  Bug reports from people who are very much better at it than me and who have found a crash further than I can get still cause me much grief.)

Some of the primary issues encountered when translating anything to Android are:

Being a bit of a porting novice, my translation of LCS to android went as follows:

I wrote a python script that reads a class file, identifies all of the class variables and method names, and spits out a java file with an appropriate class name, instance variables left at the top, and method names with the correct names and all of the code included but commented out, and with a 'throw new UnsupportedOperationException();' at the top.

Starting with the main() function, look through the code a line at a time, and change it to Java.  Changing the C code to Java mostly involves removing the comments; int x=3 is the same in both languages, for instance.  Changing the C++ idioms (for instance, loops), required a bit of rewriting.

Once I've completed a function or two, run it, and see where it crashes.  If it's on a UOE, then great, work on that function next.  If not, fix the code and try again.  Repeat repeat repeat.

The curses routines are already factorised out in LCS.  Writing a few stub functions for java that send it to logfile lets you play through LCS/Android under the debugger, and you can fix up output to the screen using the message passing functions a bit at a time.  Repeat till done.

There were two big issues I had with the Android port:
However, I did have a lot of fun putting it all together, and am glad that so many people have enjoyed it.  I hadn't really been intending to release it at all -- hence the lack of forum posting, or anything similar -- but when it was done I thought it might be a chuckle.  Sent Toady an email asking if that would be okay; he said it would, as long as the code stayed freely available, and here we are today.  Hope Mr Fox isn't too upset about me playing with his baby; I'd not appreciated the work he'd put into it, not having much experience of forums.

Regarding Zaroth's goals:
Quote
splitting the huge functions/files into more easily consumable blocks
grouping some of the functionality into classes, encapsulate and all the good stuff

I can't speak for how easy this is in C / C++ (not my area of expertise), but this is fairly straightforward in Eclipse / Java.  I've done quite a lot of splitting things out and rearranging for the Java port, mostly so that I could get my head round it.  On the other hand, Java is very easy to refactor (as these things go), and gives you plenty of warnings about things that are inaccessible, uninitialised local variables and so on.  Also, plenty of static code analysis tools (FindBugs, Unnecessary Code Detector) to pick out the 'obvious' mistakes.  I'd begun commenting on all the functions as well, but it was taking ages, and perfect being the enemy of good enough, I decided to just get it out there.

Quote
writing some unit tests to make sure nothing breaks during our refactoring and to be useful later on

I can only suggest that this would be an excellent idea.  Assuming of course you understand how the damage calculation code is supposed to work :-)

Quote
abstract the curses calls to allow for a different UI (e.g. SDL) in the future - super long term and work consuming, probably won't be done, but it's nice to dream :)

Actually, this is okay, depending on what your goals are.  The curses code is already factorised out and in one place.  If you're wanting to create an OpenGl port that just draws the characters on-screen where they would be in DOS mode (like, for instance, Dwarf Fortress does) then it shouldn't take more than a few hours.  Caveat: says me, that doesn't know a great deal about SDL programming.

Any questions on anything, please feel free to ask, will do my best.  Otherwise, cheers everyone, and keep pursuing that Liberal agenda,

Addiexxx
Title: Re: LCS code refactoring effort
Post by: BigD145 on May 10, 2014, 11:15:50 am
Last I saw Fox was thrilled to see LCS expanded onto another platform. LCS is perfect for Android. You can stop at any time and pick up at almost any time. It's largely just a better game than 99% of what's on the google marketplace. I look forward to the next update.
Title: Re: LCS code refactoring effort
Post by: Sorro196 on May 10, 2014, 07:04:04 pm
Oh, it's great to see this game's not dead. I thought nobody would read my crash reports (which are happening less often now that I avoid crashy things). Really, there's only two big bugs I've noticed: One of my Liberals lost his head and was pretty much fine after a two month clinic visit, and if somehow you send Liberals to a safe house that is not seen by the game as a safe house, you don't see any sieges. 0% heat, and none is generated. There are smaller bugs with stats too, one the health bug where heavily injured Liberals heal and get way too much health (not sure if it affects gameplay, would it change defense or blood levels?) and beating Conservatives raises Wisdom- a lot. (This one has crashed my phone before, but only because I'm curious. I had to let the hostage escape after like 2600 Wisdom.)
Title: Re: LCS code refactoring effort
Post by: Carlos Gustavos on May 14, 2014, 04:16:10 pm
Hi, I'm blomkvist on Sourceforge and, yes, as you've pointed out, the source code could certainly be improved by any brave enough to endure.
Title: Re: LCS code refactoring effort
Post by: Liberal Elitist on June 23, 2014, 05:01:24 am
Hi I'm yetisyny on SourceForge and also pleased to see these efforts. I hope to work together with you on this. I've made a number of changes to the code on SourceForge recently... I hope we can merge our work together. I like your ideas on making the code more object-oriented and portable and not having ridiculously long functions, and making it easier for it to use something other than curses for display. Curses, and console mode, have plenty of limitations. Also I think the Android port is really awesome, and also quite an effort since it's a different language. I've been working on getting more concise code, among other things... as far as the really long length of includes.h goes, that's both a blessing and a curse... it's kinda nice being able to look up almost any function declaration all in one place, but the file is definitely too long. But if you increase the number of header or code files, you need to add them to the makefiles and the project files for the various IDEs. I've done plenty of code cleanup... before I started on the game, it had a very long list of compiler warnings... I fixed all of that and now it never generates any warnings, that's just 1 example. Recently I changed the compiler options for MinGW-GCC in the Code::Blocks project to use the C++98 standard and be "pedantic", and had to change a bunch of things that were, well, non-standard. This ensures that all the code is now standard code that ought to work on any compiler on any platform as long as it's a C++ compiler. I've also been changing from C-strings to std::strings gradually, since std::strings are safer and don't have the buffer overflow problems and are more object oriented, not based on that old-fashioned pointer/array code. I also hope to change the savegame format to be pure XML that is fully editable in any text editor, eventually. This would have the advantage that small changes to the game that happen periodically would no longer break savegame compatibility, and it would be great for debugging too.