Bookless spell systems in C: is this a good approach?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Therem Harth
    Knight
    • Jan 2008
    • 926

    Bookless spell systems in C: is this a good approach?

    I'm currently trying to implement a bookless caster class, the Necromancer, in Vanilla. The Necromancer (and other bookless casters) would have a small collection of powerful but relatively specialized spells - in this class's case, mostly offensive spells.

    My design for the spell system looks something like this...

    - For actually casting the spell, I have a cast_necro_spell function, which takes the index of a spell, and a direction. Spell indices are set with #define statements, as per book spells. Damages, radii, etc. are hardcoded into this function.

    - However, using tables or such for the info stuff struck me as annoying and maybe not so smart. Instead I figured I would use a struct, necro_infoholder, like this:

    Code:
    struct necro_infoholder {
        int initial_level;
        int mana_cost;
        int initial_fail_rate;
        char *desc;
    };
    And have a function to return a necro_infoholder struct, whose values could be used by other functions:

    Code:
    struct *necro_infoholder necro_spell_info(int spell) {
        struct necro_infoholder info;
    
        switch(spell) {
            case NECRO_DARK_BOLT:
            {
                info.initial_level = 1;
                info.mana_cost = 2;
                info.intial_fail_rate = 20;
                info.desc = "Fires a bolt of elemental darkness.";
            }
        /* ... etc. */
        }
        return info;
    }
    So for browsing necromantic spells, I could do

    Code:
    struct necro_infoholder *some_spell_info = necro_spell_info(NECRO_DARK_BOLT);
    /* blah blah code to print the info */
    ... Does this sound in any way sane, or am I just making more trouble for myself? Please be a little forgiving, I am quite new to C.
    Last edited by Therem Harth; November 27, 2012, 20:56. Reason: structs declarations end with semicolons. Have I mentioned I'm a newbie?
  • Therem Harth
    Knight
    • Jan 2008
    • 926

    #2
    Umm. The thought occurs to me that, if I intend to create several bookless caster classes with roughly similar spell systems, it would be better to have a struct spell_infoholder type, defined somewhere globally... That way all the bookless classes could use it.

    What can I say, I'm new to this design stuff too.

    (Luckily I haven't written much code yet!)

    Comment

    • Magnate
      Angband Devteam member
      • May 2007
      • 5110

      #3
      You really should ping Derakon. He's looking for someone to implement spells in pyrel ...
      "Been away so long I hardly knew the place, gee it's good to be back home" - The Beatles

      Comment

      • Therem Harth
        Knight
        • Jan 2008
        • 926

        #4
        Umm... Thank you very, very much?

        Alas, I have even less experience with Python than with C. Don't get me wrong, I'd love to help with Pyrel; I'm just not sure I could do a very good job of it (especially within Python's mostly object-oriented paradigm).

        Comment

        • Magnate
          Angband Devteam member
          • May 2007
          • 5110

          #5
          Originally posted by Therem Harth
          Alas, I have even less experience with Python than with C. Don't get me wrong, I'd love to help with Pyrel; I'm just not sure I could do a very good job of it (especially within Python's mostly object-oriented paradigm).
          If it's any consolation, I felt exactly the same way. Like so many *band devs I had no experience other than hacking on Angband in C, and none at all with OO coding. Python is easy to learn, Pyrel is really newbie-friendly and Derakon is a very patient instructor.
          "Been away so long I hardly knew the place, gee it's good to be back home" - The Beatles

          Comment

          • Therem Harth
            Knight
            • Jan 2008
            • 926

            #6
            Again, thanks, I'll see what he says.

            Re the OP, I think I should probably use a typedef for spell_infoholder...

            Comment

            • Derakon
              Prophet
              • Dec 2009
              • 9022

              #7
              Hm, Pyrel is going to have a completely different approach to spells compared to Vanilla. It won't look any different from the player's perspective, mind you. I already have the design more or less in my mind, though I've yet to articulate it. If you're interested in taking a stab at it, let me know and I can write it down.

              The main problem with changing things in Vanilla is understanding the codebase well enough to know what needs to be changed. If you have that knowledge, then there's any number of reasonable bookless-spellcasting designs that you could bolt onto it.

              Comment

              • Patashu
                Knight
                • Jan 2008
                • 528

                #8
                'Every spell is a LUA plugin' plz
                My Chiptune music, made in Famitracker: http://soundcloud.com/patashu

                Comment

                • Therem Harth
                  Knight
                  • Jan 2008
                  • 926

                  #9
                  Derakon: I'm definitely interested. Thanks.

                  Comment

                  • takkaria
                    Veteran
                    • Apr 2007
                    • 1951

                    #10
                    Originally posted by Therem Harth
                    - However, using tables or such for the info stuff struck me as annoying and maybe not so smart.
                    Can I ask why it strikes you as annoying/not so smart? To me it looks like the least annoying and smartest option. I don't see what the advantage of using a function is over using a table of the same information e.g.

                    Code:
                    struct necro_infoholder necro_spell_info[] = 
                    {
                      { 1, 2, 20, "Fires a bolt of elemental darkness." }
                    }
                    The only change to the rest of the code would be to use [] instead of (), e.g.

                    Code:
                    necro_spell_info[NECRO_DARK_BOLT]
                    vs
                    necro_spell_info(NECRO_DARK_BOLT)
                    and the advantage is that you can probably see every spell on your screen at once - and the disadvantage of having three numbers that don't have obvious meanings, but that's a tradeoff I'd gladly make.

                    Still, I would use the info files for this - some of this functionality is there already. You could use the B: line in p_class.txt for most of the info:

                    Code:
                    # B:spell number:level:mana:fail:exp
                    You could then access the info through 'p_ptr->class->magic->info[spell number].level' etc. You could use just a table for the descriptions then (because it looks like spell.txt really isn't designed for spells not in books).

                    Hope that's useful!
                    takkaria whispers something about options. -more-

                    Comment

                    • Therem Harth
                      Knight
                      • Jan 2008
                      • 926

                      #11
                      Bah, sorry about how long it took to respond on this. Real Life intruded, and I kind of gave up for a while.

                      takkaria - thanks for the input. Personally I'd rather hardcode things than use the info files, since V is easy enough to compile; and C is IMO easier to read than the info file format. I'm open to further suggestions though.

                      Here we go again...

                      Comment

                      • ekolis
                        Knight
                        • Apr 2007
                        • 921

                        #12
                        If you're designing your own variant, there's no reason you have to use the existing info file format, apart from "it's easy"... but even that argument is a bit specious, seeing as there are lots of freely available parsers for more readable formats like JSON...
                        You read the scroll labeled NOBIMUS UPSCOTI...
                        You are surrounded by a stasis field!
                        The tengu tries to teleport, but fails!

                        Comment

                        • Therem Harth
                          Knight
                          • Jan 2008
                          • 926

                          #13
                          I don't think I want to add dependencies though. And I hesitate to do anything that would require serious changes to the build scripts. :P

                          Comment

                          • Magnate
                            Angband Devteam member
                            • May 2007
                            • 5110

                            #14
                            Originally posted by Therem Harth
                            I don't think I want to add dependencies though. And I hesitate to do anything that would require serious changes to the build scripts. :P
                            I still think you're crazy to try this on a gnarly 20-year-old C codebase when there's a brand new clean python codebase just waiting for a spell system. Ho hum.
                            "Been away so long I hardly knew the place, gee it's good to be back home" - The Beatles

                            Comment

                            • LostTemplar
                              Knight
                              • Aug 2009
                              • 670

                              #15
                              20-year-old C
                              Old is good, it is well done and tested (not counting recent changes) code was written much better and accurately 20 years ago.

                              Comment

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