Reading C code
Collapse
X
-
Tags: None
-
First off, just to clarify: Angband is written in C, not C++.
Second, what kinds of things are you having trouble with? Do you know how to code? If not, that's where you should start. If you want to know where something specific is (e.g. "where are spells defined" or "how do I change the monster AI") then just go ahead and ask your more specific questions.
If you want to figure out overall how the program works, then my advice would be to come up with some of those more specific questions, and try to figure them out. The usual way I do this is to find some in-game text that seems to be close to what I want to work on, and then search the code for that text, or a snippet of it. That should get me close to where I want to be, and then I just start tracing the program logic from there. -
Kernighan and Richie is of course the classic. For basic programming design, it's hard to beat Design Patterns. (Some of them are used in Angband, and it really helps if you can recognize the general technique. There are some patterns that are not in the book, e.g. 'group join', which is used in the knowledge menu.)Comment
-
While this is for C++, if you want to learn, this site is probably one of the best I know of. (And free.)
Comment
-
i have a hard time understanding why the folders are there and what they do in the games source code
all i know about programming i know from some javascript
i know functions,assignment operators,loops,objects
and i think struct in c/ c++ is just a way to create an object?
thats just my guessing
Code:struct account { int account_number; char *first_name; char *last_name; float balance; }; var account = { account: undefined, first_name: undefined, last_name: undefined, balance: undefined };
Code:bool feat_is_magma(int feat) { return tf_has(f_info[feat].flags, TF_MAGMA); }
and this function is returning another function, taking the parameters of the first function (feat) and passing it and TF_MAGMA, into the inner function.
but Id like to be able to see where the tf_has function is defined, but I dont know which folder to find it in, say, lib, or scripts or srcLast edited by whiterhino123; July 12, 2018, 22:53.Comment
-
What OS are you on? You can find the definition for this function via grep on Linux, or on Windows if you have the unix tools installed (mingw or cygwin.) Windows PowerShell has it too, though the command is different. The function is declared in one or another .h file. The C file will have the corresponding definition. And you really need to look up how C works. I strongly recommend Kernighan and Ritchie. It is short and gets all the basics. Warning: C is Not object oriented. All functions are static. You can create "true" objects only with function pointers, which is done in a number of locations in Angband (Notably, in menus, spells, and commands.)Comment
-
thanks Pete, Could you explain how I'd find the definition with those tools?
I can search in windows for tf_has function, but that still pulls up a lot of files that have the function, meaning I still have to filter through all the files to see where it is declared, rather than called.
Would grep or mingw do something differently?Comment
-
Look at the .h files only. That is where functions are "declared", which makes them accessible from other files. So if function foo is defined in bar.c, it will be declared in bar.h
To find it:
$ grep -l "foo" *.h #note: grep -l just lists files where it is present.
bar.h
$ <edit> bar.c
or the Windows equivalent.Comment
-
I would suggest trying QtCreator or a similar IDE. Then you can Ctrl-click to "go through" any function/typedef/etc. you see.
(I mean Pete's method sort-of works, but it's incredibly cumbersome for commonly used "names" and it's pointlessly tedious.)Comment
-
Sure an (any) IDE works better. So do syntax-aware editors. But if you just want to dip your toes, it's not worth setting up an environment.Comment
-
I would note here as an aside that, working on a decent sized codebase in my day job, Visual Studio Intellisense is vastly slower at finding function references than Find in Files - i.e. grep routinely outperforms the IDE features. Mileage may vary here depending on usage.Comment
-
Any difficulty effort in getting Angband into an IDE seems to be because they seem to want to stick with the utter brain-damage that is auto{conf,make} + Makefiles. In ToME 2.x-ah it's something like 5 clicks or thereabouts and you're done.Comment
-
Code:if (monster_is_awake && monster_sees_you) { }
Code:if (monster_is_awake) { if (monster_sees_you) { do_monster_attack(); } else { do_monster_wander(); } }
Comment
Comment