Reading C code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • whiterhino123
    Rookie
    • Jul 2018
    • 4

    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
    Prophet
    • Dec 2009
    • 8820

    #2
    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.

    Comment

    • Pete Mack
      Prophet
      • Apr 2007
      • 6697

      #3
      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

      • fph
        Knight
        • Apr 2009
        • 956

        #4
        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.
        --
        Dive fast, die young, leave a high-CHA corpse.

        Comment

        • OmniNegro
          Scout
          • Jul 2014
          • 37

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

          Comment

          • whiterhino123
            Rookie
            • Jul 2018
            • 4

            #6
            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
            };
            these look very similar, which is why im thinking structs are very similar to objects

            Code:
            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
            Last edited by whiterhino123; July 12, 2018, 22:53.

            Comment

            • Pete Mack
              Prophet
              • Apr 2007
              • 6697

              #7
              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

              • whiterhino123
                Rookie
                • Jul 2018
                • 4

                #8
                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

                • Pete Mack
                  Prophet
                  • Apr 2007
                  • 6697

                  #9
                  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

                  • AnonymousHero
                    Veteran
                    • Jun 2007
                    • 1322

                    #10
                    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

                    • Pete Mack
                      Prophet
                      • Apr 2007
                      • 6697

                      #11
                      Originally posted by AnonymousHero
                      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.)
                      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

                      • Quirk
                        Swordsman
                        • Mar 2016
                        • 461

                        #12
                        Originally posted by AnonymousHero
                        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.)
                        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

                        • AnonymousHero
                          Veteran
                          • Jun 2007
                          • 1322

                          #13
                          Originally posted by Pete Mack
                          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.
                          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

                          • wobbly
                            Prophet
                            • May 2012
                            • 2576

                            #14
                            So I can find if/thens in the code but none apear to be nested. How is this generally handled?

                            Comment

                            • Quirk
                              Swordsman
                              • Mar 2016
                              • 461

                              #15
                              Originally posted by wobbly
                              So I can find if/thens in the code but none apear to be nested. How is this generally handled?
                              Mostly if you're checking two conditions that both have to be true you would use AND (&&) e.g.
                              Code:
                               
                              if (monster_is_awake && monster_sees_you)
                              {
                              }
                              however you would nest ifs in cases where you want to do one of two or more things when a condition is true e.g.
                              Code:
                               
                              if (monster_is_awake)
                              {
                                  if (monster_sees_you)
                                  {
                                      do_monster_attack();
                                  }
                                  else
                                  {
                                      do_monster_wander();
                                  }
                              }
                              To keep logic from getting too awkwardly nested, the inner logic may be moved into functions - so you might actually see the inner if moved into a do_monster_awake function in practice.

                              Comment

                              Working...
                              😀
                              😂
                              🥰
                              😘
                              🤢
                              😎
                              😞
                              😡
                              👍
                              👎