Angband Forums

Angband Forums (
-   Development (
-   -   Reading C code (

whiterhino123 July 11, 2018 17:11

Reading C code
So i've read

but I was actually hoping for something more involved?

Whats a good C++ source (book probably?) that can help me understand the logic behind the source code of angband?

is there a more indepth guide to understanding angbands code?

Derakon July 11, 2018 23:09

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.

Pete Mack July 11, 2018 23:41

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.)

fph July 12, 2018 09:38

In any case, look for a book on C, not C++. C++ is a rabbit hole that goes as deep as the center of the Earth.

OmniNegro July 12, 2018 21:46

While this is for C++, if you want to learn, this site is probably one of the best I know of. (And free.)

whiterhino123 July 12, 2018 23:47

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


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

these look very similar, which is why im thinking structs are very similar to objects


bool feat_is_magma(int feat)
        return tf_has(f_info[feat].flags, TF_MAGMA);

so this is a bool function that can only return true or false.

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 src

Pete Mack July 13, 2018 01:40

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.)

whiterhino123 July 21, 2018 17:53

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?

Pete Mack July 21, 2018 18:18

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.
$ <edit> bar.c
or the Windows equivalent.

AnonymousHero July 21, 2018 21:23

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.)

All times are GMT +1. The time now is 13:57.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.