Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ SMAUG ➜ SMAUG coding ➜ Adding new mob variable

Adding new mob variable

It is now over 60 days since the last post. This thread is closed.     Refresh page


Pages: 1 2  3  

Posted by Zeno   USA  (2,871 posts)  Bio
Date Sun 31 Aug 2003 04:18 AM (UTC)

Amended on Sun 31 Aug 2003 04:23 AM (UTC) by Zeno

Message
I'm having some trouble adding a new mob number variable. So far I have put it in mob_index_data and char_data (in mud.h). I can edit it, and seems to work fine, but when I savearea, and copyover(or reboot) its back to the original value, 0. I'm missing the place to save it at, and I'm not sure how or where it is. I've never added a mob variable yet, so I guess I need help.

Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #1 on Sun 31 Aug 2003 06:06 AM (UTC)
Message
You need to add it to both the save and load routines, in db.c I think it is. Be cautious how you do it, if you are not careful existing area files will not be able to be read, and also the area editor, or other offline editing tools will not read the area either.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Boborak   USA  (228 posts)  Bio
Date Reply #2 on Sun 31 Aug 2003 03:38 PM (UTC)
Message
The save section is in the function fold_area() which is usually found in build.c but may be in db.c as well. The load section is in the function load_mobiles() in db.c

Adding new variables to a mud can be a tricky situation. The problem is once you've added the 'load' code, the mud expects to find that variable no matter what. I personally have 2 methods of adding new variables into an area file.

The first method is a little simpler but a little easier to screw up too.

The first thing you should do is add the 'save' code to build.c so that your new variable will be written to the area file. For example:

A portion of my code in fold_area() looks like this:

fprintf( fpout, "%d 0\n",  pMobIndex->gold    );
fprintf( fpout, "%d %d %d\n",   pMobIndex->position,
                                pMobIndex->defposition,
                                pMobIndex->sex      );


I'm going to add my new code between the two for refrence.


fprintf( fpout, "%d 0\n",  pMobIndex->gold    );
fprintf( fpout, "%d\n",  pMobIndex->newvar    );
fprintf( fpout, "%d %d %d\n",   pMobIndex->position,
                                pMobIndex->defposition,
                                pMobIndex->sex      );


Now assuming all of the rest of the code related to this new var is complete (minus the load code!) I'm going to recompile and run my mud.

Now I run 'foldarea all' to ensure that all of my areas now have saved SOMETHING (even a 0 is fine) to that new location. This now ensures that when I add the load code, the area files have something in the spot the mud expects to find a variable.

Then you add your load code to db.c. Recompile and restart your mud. Since the area files have been preped you should be good to go.

When adding your load code you need to ensure it's in the exact spot as where you saved it. For example:

This is a chunck of code from my load_mobiles() in db.c:

pMobIndex->gold                 = fread_number( fp );
pMobIndex->exp                  = fread_number( fp );
pMobIndex->position             = fread_number( fp );
pMobIndex->defposition          = fread_number( fp );


That looks like its loading the variables that I used as refrence for saving. So I'll add my new variable in between.

pMobIndex->gold                 = fread_number( fp );
pMobIndex->newvar            = fread_number( fp );
pMobIndex->exp                  = fread_number( fp );
pMobIndex->position             = fread_number( fp );
pMobIndex->defposition          = fread_number( fp );


Now I should have all of the code installed to save/load my new variable. This is my preffered method for simple additions because it cuts down on code. The other method is to use an 'area version' for the area files and a series of if checks to ensure a section of code is only loaded if it's a compatible version. But the above example should suit most needs. Hope this helps.

-Bobo
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #3 on Sun 31 Aug 2003 04:19 PM (UTC)
Message
There is another way, which is what I meant by being careful. ;)

If you add writing the new variable to an *existing* line, you can then use sscanf to read it in, like this example from db.c:


            ln = fread_line( fp );
            x1=x2=x3=x4=x5=x6=x7=x8=0;
            sscanf( ln, "%d %d %d %d %d %d %d",
                &x1, &x2, &x3, &x4, &x5, &x6, &x7 );
            pMobIndex->race             = x1;
            pMobIndex->class            = x2;
            pMobIndex->height           = x3;
            pMobIndex->weight           = x4;
            pMobIndex->speaks           = x5;
            pMobIndex->speaking         = x6;
            pMobIndex->numattacks       = x7;



Now if you wanted to read another one you could do this:


            ln = fread_line( fp );
            x1=x2=x3=x4=x5=x6=x7=x8=0;
            sscanf( ln, "%d %d %d %d %d %d %d %d",
                &x1, &x2, &x3, &x4, &x5, &x6, &x7, &x8 );
            pMobIndex->race             = x1;
            pMobIndex->class            = x2;
            pMobIndex->height           = x3;
            pMobIndex->weight           = x4;
            pMobIndex->speaks           = x5;
            pMobIndex->speaking         = x6;
            pMobIndex->numattacks       = x7;
            pMobIndex->my_new_variable       = x8;
       


The advantage of this method is that it will cope with existing area files because the non-present variable will be taken to be zero.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Boborak   USA  (228 posts)  Bio
Date Reply #4 on Sun 31 Aug 2003 06:39 PM (UTC)
Message
True. But I was trying to demonstrate a way that could be used with strings too. ;-)
Top

Posted by Zeno   USA  (2,871 posts)  Bio
Date Reply #5 on Sun 31 Aug 2003 08:21 PM (UTC)
Message
Ok I followed Nick's advice, because I've done this before and was getting errors in mobs because the variable wasn't created in the mobs.
In db.c

            ln = fread_line( fp );
            x1=x2=x3=x4=x5=x6=x7=x8=0;
            sscanf( ln, "%d %d %d %d %d %d %d %d",
                &x1, &x2, &x3, &x4, &x5, &x6, &x7, &x8 );
            pMobIndex->race             = x1;
            pMobIndex->class            = x2;
            pMobIndex->height           = x3;
            pMobIndex->weight           = x4;
            pMobIndex->speaks           = x5;
            pMobIndex->speaking         = x6;
            pMobIndex->numattacks       = x7;
            pMobIndex->mvr              = x8;

Looks ok, right? Well, I loadarea, mset the mobs new variable to 1, savearea, copyover/reboot, loadarea, and stat the mob. The variable is back to 0. Maybe I did the mset wrong.
In build.c

    if ( !str_cmp( arg2, "mvr" ) )
    {
        if ( !IS_NPC(victim) )
          return;
        victim->mvr = value2;
        if ( IS_NPC(victim) && xIS_SET(victim->act, ACT_PROTOTYPE) )
          victim->pIndexData->mvr = value2;
        return;
    }

By the way, value2 is "unsigned long long" and is defined as

 value2 = is_number( arg3 ) ? atof( arg3 ) : -1;

What am I doing wrong?

Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org
Top

Posted by Boborak   USA  (228 posts)  Bio
Date Reply #6 on Mon 01 Sep 2003 03:33 AM (UTC)
Message
You're loading it ok. But where are you saving it at? Have you added your new variable to fold_area in build.c? It should look something like this:


fprintf( fpout, "%d 0 %d %d %d %d %d\n",
                                        pMobIndex->race,
                                        pMobIndex->height,
                                        pMobIndex->weight,
                                        pMobIndex->speaks,
                                        pMobIndex->speaking,
                                        pMobIndex->numattacks,
                                        pMobIndex->mvr );
Top

Posted by Zeno   USA  (2,871 posts)  Bio
Date Reply #7 on Mon 01 Sep 2003 04:41 AM (UTC)

Amended on Mon 01 Sep 2003 04:42 AM (UTC) by Zeno

Message
Well, I tried that and didn't work.

          fprintf( fpout, "%d 0 %d %d %d %d %d %d\n",
                                        pMobIndex->race,
                                        pMobIndex->class,
                                        pMobIndex->height,
                                        pMobIndex->weight,
                                        pMobIndex->speaks,
                                        pMobIndex->speaking,
                                        pMobIndex->numattacks,
                                        pMobIndex->mvr );

Why a zero? And I'm not foldarea, I'm savearea, copyover/reboot, loadarea. I want the variable to stay when its prototype.

Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org
Top

Posted by Boborak   USA  (228 posts)  Bio
Date Reply #8 on Mon 01 Sep 2003 09:46 AM (UTC)
Message
The 0 is there because stock smaug saves the mob's class even though mobs don't have classes (fighter, cleric, ect.)

I realize you are using savearea but the fold_area function is used by savearea to actually save the file itself. If you used Nick's example correctly and my example correctly you should be set. How are you modifing the variable itself? Did you setup something in mset or a seperate command?
Top

Posted by Greven   Canada  (835 posts)  Bio
Date Reply #9 on Mon 01 Sep 2003 05:03 PM (UTC)
Message
well, your using an unsigned long long, right? I don't think it can be written in with %d, as you are using in fold_area, but you would need %lu, you would need to change it to:

          fprintf( fpout, "%d 0 %d %d %d %d %d %lu\n",
                                        pMobIndex->race,
                                        pMobIndex->class,
                                        pMobIndex->height,
                                        pMobIndex->weight,
                                        pMobIndex->speaks,
                                        pMobIndex->speaking,
                                        pMobIndex->numattacks,
                                        pMobIndex->mvr );


Now, I might be wrong, but I beleive that %lu would make it print in the proper format. If I am wrong, someone please correct me, as I was able to find that %lu is for unsigned longs, but I don't know if it would make a difference with unsigned long longs

Nobody ever expects the spanish inquisition!

darkwarriors.net:4848
http://darkwarriors.net
Top

Posted by Zeno   USA  (2,871 posts)  Bio
Date Reply #10 on Mon 01 Sep 2003 05:40 PM (UTC)

Amended on Mon 01 Sep 2003 05:45 PM (UTC) by Zeno

Message
Uh, my original code was


	  fprintf( fpout, "%d %d %d %d %d %d %d\n",
					pMobIndex->race,
					pMobIndex->class,
					pMobIndex->height,
					pMobIndex->weight,
					pMobIndex->speaks,
					pMobIndex->speaking,
					pMobIndex->numattacks );


There wasn't a 0. So you want me to add it?

Oh, and Grevan, yeah, good thought. Except its %llu.

Boborak, your example, do you mean your first example? I've added Nick's careful way, and then your way to save the variable. Should I have added your first example too? And btw, I used mset to change the variable. I posted the code in my above post.

Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org
Top

Posted by Boborak   USA  (228 posts)  Bio
Date Reply #11 on Mon 01 Sep 2003 05:46 PM (UTC)
Message
Ah. I stand corrected. The 0 is not stock SMAUG. I was using an example from my code. Leave it at %d.
Top

Posted by Greven   Canada  (835 posts)  Bio
Date Reply #12 on Mon 01 Sep 2003 05:46 PM (UTC)
Message
My apoligies, I copied the wrong part, and thanks, wasn't sure about %llu.

Nobody ever expects the spanish inquisition!

darkwarriors.net:4848
http://darkwarriors.net
Top

Posted by Zeno   USA  (2,871 posts)  Bio
Date Reply #13 on Mon 01 Sep 2003 05:57 PM (UTC)
Message
Alright, its now...

          fprintf( fpout, "%d %d %d %d %d %d %d %llu\n",
                                        pMobIndex->race,
                                        pMobIndex->class,
                                        pMobIndex->height,
                                        pMobIndex->weight,
                                        pMobIndex->speaks,
                                        pMobIndex->speaking,
                                        pMobIndex->numattacks,
                                        pMobIndex->mvr );


Still reseting to 0 on copyover/reboot. The above would be my savearea code, this is the mset code

    if ( !str_cmp( arg2, "mvr" ) )
    {
        if ( !IS_NPC(victim) )
          return;
        victim->mvr = value2;
        if ( IS_NPC(victim) && xIS_SET(victim->act, ACT_PROTOTYPE) )
          victim->pIndexData->mvr = value2;
        return;
    }

value2 allows me to mset it higher than 2billion, the code is

value2 = is_number( arg3 ) ? atof( arg3 ) : -1;

And then in db.c

            sscanf( ln, "%d %d %d %d %d %d %d %llu",
                &x1, &x2, &x3, &x4, &x5, &x6, &x7, &x8 );
            pMobIndex->race             = x1;
            pMobIndex->class            = x2;
            pMobIndex->height           = x3;
            pMobIndex->weight           = x4;
            pMobIndex->speaks           = x5;
            pMobIndex->speaking         = x6;
            pMobIndex->numattacks       = x7;
            pMobIndex->mvr              = x8;

Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org
Top

Posted by Greven   Canada  (835 posts)  Bio
Date Reply #14 on Mon 01 Sep 2003 06:05 PM (UTC)

Amended on Mon 01 Sep 2003 06:06 PM (UTC) by Greven

Message
do you have mvr in your structure also set to unsigned long long, as well as value2? Also, make sure that x8 is also an unsigned long long

Nobody ever expects the spanish inquisition!

darkwarriors.net:4848
http://darkwarriors.net
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


78,765 views.

This is page 1, subject is 3 pages long: 1 2  3  [Next page]

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.