[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  SMAUG
. -> [Folder]  SMAUG coding
. . -> [Subject]  Saving spells with slots rather than sn's
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Saving spells with slots rather than sn's

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


Pages: 1 2  3  

Posted by Orik   USA  (182 posts)  [Biography] bio
Date Mon 02 Jul 2007 04:20 AM (UTC)

Amended on Mon 02 Jul 2007 04:27 AM (UTC) by Orik

Message
Ok, I have put in a spell memorization system in my code. It all works fine and dandy but the one problem is that it saves the sn's of the spells to the pfile. This works great until I add a new skill/spell and it resorts the sn's. So when my next character that had a spell memorized logs in, he'll have a different spell in his memory because the new spell bumped that spell out of it's old sn.

Example:

I mem heal which is sn #90. 

I see heal when I type mem to show memorized list. 

I log off and have sn 90 saved in my pfile. 

A new spell named "haver" is made, and sorted. I hotboot mud, skill table is resorted haver goes into sn 90, pushing heal to 91.

I log back in my player and type mem to show list and it now says haver instead of heal.


Here's where it's saving as sn:


int
set_spell_mem(CHAR_DATA * ch, int sn)
{
     int i;

     for (i = 0, i < MAX_MEM_SPELLS; i++)
          if (ch->pcdata->memorizing[ i] == 0)
          {
             ch->pcdata->memorizing[ i] = sn;
             return TRUE;
          }
     return FALSE;
}


so it's setting ch->pcdata->memorizing[ i] to sn and I want it set to the slot number. I tried this:

ch->pcdata->memorizing[ i] = skill_table[sn]->slot;

but it didn't work. Any thoughts? The problem is I'm converting this from a envy codebase where there is no sorting on the skills/spells so it never took that into consideration when it saved sn's.
[Go to top] top

Posted by Meerclar   USA  (733 posts)  [Biography] bio
Date Reply #1 on Mon 02 Jul 2007 04:55 AM (UTC)
Message
My suggestion would be to save the actual spell name rather than the spell number. You'd need to rework the code a bit to read and store the spell name from input instead of the sn table.

Meerclar - Lord of Cats
Coder, Builder, and Tormenter of Mortals
Stormbringer: Rebirth
storm-bringer.org:4500
www.storm-bringer.org
[Go to top] top

Posted by Orik   USA  (182 posts)  [Biography] bio
Date Reply #2 on Mon 02 Jul 2007 04:59 AM (UTC)

Amended on Mon 02 Jul 2007 05:07 AM (UTC) by Orik

Message
I had thought of that. But I didn't know if I'd be able to figure it all out. How would I set up the for loops to pick out the name rather than the sn?

***********EDIT**************

Also what about spells with two or more words?

IE quantum spike or protection from scrying
[Go to top] top

Posted by Orik   USA  (182 posts)  [Biography] bio
Date Reply #3 on Mon 02 Jul 2007 05:40 AM (UTC)
Message
I'd rather just go with slots numbers. I'm not using any stock spells, so I'm just starting with my own new spells and I can easily put in slot numbers when I add new spells in.

I think it would be easiest to just have it save slot numbers. Save space in pfiles as well. I just need to figure out how to get it to find the slot number through the sn of the spell.
[Go to top] top

Posted by Meerclar   USA  (733 posts)  [Biography] bio
Date Reply #4 on Mon 02 Jul 2007 05:45 AM (UTC)
Message
They wont have more than 1 word as the function name so thats one option. You could also use a char * value for storage and read the spell input as one arg with ' delimiters since players have to use them for spells with more than 1 word anyway.

Meerclar - Lord of Cats
Coder, Builder, and Tormenter of Mortals
Stormbringer: Rebirth
storm-bringer.org:4500
www.storm-bringer.org
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #5 on Mon 02 Jul 2007 05:46 AM (UTC)
Message
I would also recommend going with spell names because it is more robust in the long run.

But if you really want to use slots, to find a slot from an sn, isn't there a 'slot' field in the skill structure?

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Orik   USA  (182 posts)  [Biography] bio
Date Reply #6 on Mon 02 Jul 2007 02:05 PM (UTC)

Amended on Mon 02 Jul 2007 02:14 PM (UTC) by Orik

Message
I don't have my code in front of me, but I'm guessing I could do something like

ch->pcdata->memorizing[ i] = skill->slot;

What would I do about the sn? Whenever it's called it's like

set_spell_mem(ch, sn)

could I put it

set_spell_mem(ch, skill->slot)

or I could put skill->slot = sn?

*******EDIT********

If I go by names, how would I set it up to look for the arguments? If that's the better way then I'll do it like that.
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #7 on Mon 02 Jul 2007 05:32 PM (UTC)
Message
Well, you'd have to be very careful to use SNs anywhere the code expects an SN. I think there is a function called slot_lookup that will translate from slot to SN, which is what you'd want to use. You would set up your data representation and functions however you want, but when you talk to the rest of the code you would need to use the conventions they have. Basically, pick an internal convention and stick with it, and then make sure you do the right thing when talking to the existing functions.

One problem of slots and skill numbers is that they aren't type-safe. Meaning that they can be used interchangeably by accident, even though they really aren't interchangeable at all.

Quote:
If I go by names, how would I set it up to look for the arguments?


I'm not sure I understand what you mean. :-)

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Orik   USA  (182 posts)  [Biography] bio
Date Reply #8 on Mon 02 Jul 2007 05:46 PM (UTC)
Message
You said it'd be better to use spell names. How would I set up a char * to be able to use the names to write the names to files? Would it be something like


struct char * ch

for (sn = 0, sn < max_skill, sn++)
  sn = skill_table[sn]->name;
return skill_table[sn]->name;


Thats not real, just a psuedo code, but I'm not really sure how to set it to read and write the names to memorization.
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #9 on Mon 02 Jul 2007 09:03 PM (UTC)
Message
No, you would use sn's in memory, but save/load the spell names. It's like how spell effects work currently on, e.g., pfiles: you store names, and convert them to numbers when you read.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Orik   USA  (182 posts)  [Biography] bio
Date Reply #10 on Mon 02 Jul 2007 10:23 PM (UTC)
Message
Ok, here's the fwrite_char in save.c


   for( sn = 1; sn < top_sn; sn++ )
   {
      if( skill_table[sn]->name && ch->pcdata->learned[sn] > 0 )
         switch ( skill_table[sn]->type )
         {
            default:
               fprintf( fp, "Skill        %d '%s'\n", ch->pcdata->learned[sn], skill_table[sn]->name );
               break;
            case SKILL_SPELL:
               fprintf( fp, "Spell        %d '%s'\n", ch->pcdata->learned[sn], skill_table[sn]->name );
               break;
            case SKILL_WEAPON:
               fprintf( fp, "Weapon       %d '%s'\n", ch->pcdata->learned[sn], skill_table[sn]->name );
               break;
            case SKILL_TONGUE:
               fprintf( fp, "Tongue       %d '%s'\n", ch->pcdata->learned[sn], skill_table[sn]->name );
               break;
         }
   }


Here's fread_char


if( !strcmp( word, "Spell" ) )
            {
               int sn, value;

               if( preload )
                  word = "End";
               else
               {
                  value = fread_number( fp );

                  sn = bsearch_skill_exact( fread_word( fp ), gsn_first_spell, gsn_first_skill - 1 );
                  if( sn < 0 )
                     bug( "%s", "Fread_char: unknown spell." );
                  else
                  {
                     ch->pcdata->learned[sn] = value;
                     if( ch->level < LEVEL_IMMORTAL )
                        if( skill_table[sn]->skill_level[ch->Class] >= LEVEL_IMMORTAL )
                        {
                           ch->pcdata->learned[sn] = 0;
                           ch->practice++;
                        }
                  }
                  fMatch = TRUE;
                  break;
               }
            }


So I need to go off this. I'll think on it a minute and then repost something.
[Go to top] top

Posted by Orik   USA  (182 posts)  [Biography] bio
Date Reply #11 on Mon 02 Jul 2007 11:41 PM (UTC)

Amended on Mon 02 Jul 2007 11:44 PM (UTC) by Orik

Message
Here's how I save my spells in fwrite and fread char


                /* Spell Memorization */
        fprintf( fp, "Memtime     %d\n",
        ch->mem_time);

    fprintf( fp, "Memorized     ");
    for (j = 0; j < MAX_MEM_SPELLS; j++)
        {
        fprintf(fp, " %d",ch->pcdata->memorized[j]);
        }
    fprintf(fp, "\n");

    fprintf( fp, "Memorizing    ");    
for (j = 0; j < MAX_MEM_SPELLS; j++)
        {
        fprintf(fp, " %d",ch->pcdata->memorizing[j]);
        }
   fprintf(fp, "\n");



fread:


                if (!str_cmp(word,"Memorized"))
                {
                for (j = 0; j < MAX_MEM_SPELLS; j++)
                        ch->pcdata->memorized[j] = fread_number(fp);
                fMatch   = TRUE;
                break;
                }
                if (!str_cmp(word,"Memorizing"))
                {
                for (j = 0; j < MAX_MEM_SPELLS; j++)
                        ch->pcdata->memorizing[j] = fread_number(fp);
                fMatch   = TRUE;
                break;
                }


Here's in mud.h

        int                     memorized               [ MAX_MEM_SPELLS ];
    int                     memorizing              [ MAX_MEM_SPELLS ];


********EDIT********

So it saves like this in the pfile:


Memorized      165 0 0 52 0 90 90 90 90 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Memorizing     90 90 90 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0


So what you're memorizing gets put up into memorized after your done meming it, onc eyou cast it goes back to memorizing and have to remem it.


[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #12 on Mon 02 Jul 2007 11:44 PM (UTC)
Message
Some questions:

- You are using numbers. Are they slot numbers or skill numbers? If they are skill numbers, how are you accounting for the fact that they might change when you add skills and reboot?

- Where do you initialize the memory arrays? You might want to set them all to zero to avoid getting garbage the first time you save (and hence subsequent times you load).

- You might want to only write out elements that actually exist, but that will mean you have to adapt your load function.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Orik   USA  (182 posts)  [Biography] bio
Date Reply #13 on Tue 03 Jul 2007 12:03 AM (UTC)
Message
Quote:

You are using numbers. Are they slot numbers or skill numbers? If they are skill numbers, how are you accounting for the fact that they might change when you add skills and reboot?


That's why I'd rather save them by something else. They are saving as skill numbers right now. The reason being once I added a new skill/spell all numbers would be off in the mem list.

Quote:

Where do you initialize the memory arrays? You might want to set them all to zero to avoid getting garbage the first time you save (and hence subsequent times you load).


I believe it just writes out 100 0's when you first create your character

Quote:

You might want to only write out elements that actually exist, but that will mean you have to adapt your load function.


I'd like to do this. It would probably be smoother.
[Go to top] top

Posted by Orik   USA  (182 posts)  [Biography] bio
Date Reply #14 on Tue 03 Jul 2007 12:08 AM (UTC)
Message
When you save I believe this is what writes out the 100 0's because max_mem_spell is 100, they can't mem anymore then that.


                /* Spell Memorization */
        fprintf( fp, "Memtime     %d\n",
        ch->mem_time);

    fprintf( fp, "Memorized     ");
    for (j = 0; j < MAX_MEM_SPELLS; j++)
        {
        fprintf(fp, " %d",ch->pcdata->memorized[j]);
        }
    fprintf(fp, "\n");

    fprintf( fp, "Memorizing    ");    
for (j = 0; j < MAX_MEM_SPELLS; j++)
        {
        fprintf(fp, " %d",ch->pcdata->memorizing[j]);
        }
   fprintf(fp, "\n");


[Go to top] 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.


22,141 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] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]