Saturday, 29 August 2015

How to exit a process on Windows

Reading Nethack 4's blog on this subject I wanted to be a bad person and tell how you can end a process in Windows (using Visual C++ of course). You should start the project with F5 to run in debug mode.

In case you get into a infinite loop you can exit the process with shift+F5 (debug - stop debugging in VC).

From programming perspective loops are something to watch out. It can be a typical programmer's idea to assume that a loop always has an exit condition. Just as it's difficult to control the index value of an array it's also sometimes hard to keep track of the exit condition. I'm always programming a panic exit logic in loops. It may sound like overdoing it, but it's a really good idea. Panic exit logic is a counter with max value which runs along the loop and exits when the max value is reached. For debug purposes it's also good to inform when panic exit was the result of the loop to find out the reason for the infinite condition.

Sometimes resource leaks in a loop can make the OS very slow. It happens in Windows, too. Programmers should be extra careful when there are resource management in a loop which I think sounds a bit odd, but then again I have more experience from C++ resource management which mostly happens in constructors and destructors - not in loops. This I think is another good thing about C++, because it's less likely to get into strange race conditions if the class hierarchy is a tree-like system where everything is released in a strict order.

This is getting into another topic, but C++ is not protected from certain type of leaks. When you think of dangerous things in C++ (which in C are typical everyday solutions) there are three things to be extra careful: passing and returning a new object and creating a clone of an object. In C++ returning a new object from a function is not a common style (and should be avoided), but passing object to another container is, as is creating a clone. You need to be careful to handle the ownership change (as it is called in C++) properly to avoid losing the object (and thus failing to release it).

Tuesday, 18 August 2015

Nethack 4.3 beta 1 notes

This version is old and there are new ones, but I wanted to write something about it since today I felt like playing something and found this one. NH4 is the other new version of Nethack, the another one is UnNethack.

NH4 opens a big screen full of information. The inventory screen is visible as side window. It's ok I guess, but can make the screen look more complicated mess of characters. The first problem I noticed was clipping of fonts. It must be a result of pseudo-console code, since real console never clips like that. Or it may, who knows. The programs looks a lot like console window, because you can set fonts as if using a console. Setting them smaller fixed it.

Keyboard commands in NH4 are somewhat different than in "my" classic Nethack (3.4.3). Kicking is 'n', but it doesn't even work. You can change all keys the way you want by opening Options, then hitting shift+I for keyboard command list. The default has hjlk-keys for movement which takes away 'k' as kick and others as well ('l' as loot). I really don't know why that should be default, because 95% of people who ever used hjkl-movement have died in old age by now.

I don't know why they changed AC to something else. In fact I didn't really pay attention to RPG system, have they changed it? The AC seems to be some kind of protection value with only positive numbers which is bananas.

Other than that it seems to be a regular Nethack, only with more bugs. Well I found one bug, but it was really strange. When hitting a floating eye I got this:

There was no save yet, since I had not saved the game. Maybe there is some kind of temp save and the final save game also went to... somewhere. Not in the game's directory. I hate when the program saves stuff to somewhere. In this case I didn't even find anything from AppData or those locations typical to Windows 7. Where the heck it saved the game? In registry?

It's really funny when people try to maintain the bullshit source code of Nethack and only manage to create more bugs. When will this end? What every Nethack variant programmer should do is completely restart the source code possibly not even using C or C++. The source code of Nethack is so bad and taking huge amount of time to fix bugs which keep increasing. The thing is that you need a better way to manage this kind of data and actions between them.

Not only that, I think NH4 also managed to make the user interface worse while developers should really concentrate to fix that shit if they want to create a better Nethack. For instance there should be a list of extended commands when you hit # and let player select from that list directly with a letter. Keyboard commands should be listed by category, so you don't have to scroll the entire list to find something. I could go on forever.

None of this would have happened if the Dev Team had not abandoned Nethack. I think it's time to let Nethack become history.

Monday, 10 August 2015

What happened to textattr?

While I was away from console programming they have removed textattr() function from conio.h. Reading some random explanations for why it's a DOS function. No, it's actually a console function. They (reptilians?) did keep printf and even cprintf which I vaguely remember is actually the color version of printf. When you look at conio.h it's not even that big and Microsoft has added bunch of irrelevant "secure" versions of functions in there. They could have kept textattr for sure, because WinAPI does have color actions, they are just more annoying with that WinAPI style.

I've had fun writing the tiny GUI for console. My plan is avoid (most) problems I've previously had. For example the output is strictly limited to Display_Matrix class which defines a rectangular area of CHAR_INFO (Microsoft's struct for letter and color data). It's a simple container where you can write characters and strings with color data. That rectangle is then output to actual console. With Display_Matrix each screen or window is its own area and I'm also including update flag in the Display_Matrix: when the matrix is changed the flag for updating is set. It prevents duplicate or more updates per turn in low level manner.

The usual problem in class programming is that you may start to add some stuff to class which clearly doesn't belong to it. In small projects it does reduce the amount of classes needed, but it's still less modular way to think programming. Classes should always be as modular as possible and do one task using various types of source data.

Saturday, 8 August 2015

Console font size

I've programmed a gui class for console, but setting the font size seems to be difficult. I guess it's supposed not to set by the program, but by user as you indeed can do for Windows console. Not only that I noticed the Code::Blocks gcc version doesn't even have SetCurrentConsoleFont declared. Maybe I was foolish to try the project with gcc's windows.h implementation which really does not have all stuff included.

Should I start the project again with VC2010. Or not to change the font but let the user do it. Maybe I'll go with that and think about the font size later.

Friday, 7 August 2015

Console programming in Windows

I'm planning a new roguelike with ascii text in a console window. I guess curses is the only library available as cross-compiler solution and for some reason I didn't want to go there. Rather than using any libraries I started to investigate the possiblity to use Windows native console API.

I've only taken the first step and found out that you actually can set the console windows size other than default 80x25 from within the program. It's an important step, because I plan to make the size static, possibly smaller than 80x25.

The next step is a gui class to restrict all that ugly Windows API code in one class if there ever is need to change that to something else or port the game to other platforms. My prediction is that creating the gui is going to take some time because WinAPI, but once it's done it will be easier than working with graphical roguelikes as I've done this far.

This time I'm also going to use external script files, unlike in my current projects with only C++ data structures. In scripting I don't see any other advantages but "default" values which means you only have to write the data that is different from default type. Some people say it's good that you don't have to recompile the project to change data, but I think it's a non-issue in a modern IDE where only files that were changed are compiled.

The gameplay idea I have for this project is quite simple, so we are not talking about a major roguelike. If I can get the gui and parser for data done before abandoning the project it may be finished some day.

Wednesday, 5 August 2015

My new vesuri

I was planning to get a machete (or jungle knife as we call it) for gardening work, but it may give an idea for police officers to shoot you if they see someone running around with a machete. Then I found something which is less weapon-like. We call it a vesuri, it's a type of billhook. If you google search vesuri you know how the traditional "grandpa" model looks. It's much bigger than billhooks in some other countries. But the one I found is smaller:

It's so cute! The blade is 21cm (8 inches) long. If you wonder what 'vesuri' means it's an example of agglutinative structure of finnish language. 'Vesa' means "young" or "offspring" (although less used in these days, it's an old finnish word) and from that is formed 'vesakko' which means a bunch of young trees or shoots of plants. Also from that comes 'vesuri' in where the -ri means some kind of object (or sometimes a person) having some kind of function. Another example is 'imeƤ' which means "to suck" and from that 'imuri' which is a vacuum cleaner ("sucker"). Simple, isn't it.

The reason I got this for gardening is that I think scissors (large gardening ones) and pruners are more strenuous for forearm muscles. It's easier to cut small branches and shoots with a vesuri. You can even cut grass and other smaller plants which is not practical with small pruners or large, cumbersomely heavy garden scissors.

This vesuri is from Marttiini, a finnish company more known from their knives. The price was 48 euros which I think is very affordable, because it's made in Finland. The sheath is also nice, but I don't know how long that lock mechanism is going to last. I guess this kind of tool is less common in modern gardening, probably because it's considered to be an "ancient" tool (which it is). But when you work in a garden and start to think how you could make things easier then you rediscover a vesuri.