On Mon, 6 May 2002, Matt Foltz wrote:

> I've got a command I'm working on, modify, to
> basically add hp, mana, etc to an object.  It's
> supposed to check if the object already has hp/etc
> applied to it, and if it does then it combines the
> current and added values to the one apply.  The
> problem I'm getting is that if the object doesn't load
> with, say mana, on it then it won't combine the
> values.  But if it DOES load with mana on it, then it
> will.  Any suggestions would be appreciated.
> -Matt Foltz
> P.S.  Please send replies directly to me - I'm not
> subscribed to the list.


>     for (paf = obj->pIndexData->affected; paf != NULL; paf = paf->next)
>     {
>           if ( paf->location == enchant_type )
>           {
>               paf->type = sn;
>               paf->modifier += enchant_value;
>               paf->level = ch->level;
>               send_to_char("Ok.\n\r",ch );
>               return;
>           }
>           else
>           {
>              AFFECT_DATA *af_new;
>              af_new = new_affect();
>
>              af_new->next = obj->affected;
>              obj->affected = af_new;
>
>              af_new->where       = paf->where;
>              af_new->type        = UMAX(0,paf->type);
>              af_new->level       = paf->level;
>              af_new->duration    = paf->duration;
>              af_new->location    = enchant_type;
>              af_new->modifier    = enchant_value;
>              af_new->bitvector   = paf->bitvector;
>           }
>     }

You've got a couple of problems going on here.

One problem has already been pointed out by someone else:  There is a big
difference in obj->affected and obj->pIndexData->affected.
obj->pIndexData->affect is the list of affects on every single object
of that type in the game.  I'm assuming that you only want to change one
particular object, not the entire type.  You only want to modify the
obj->affected list, which is the affects that apply to only one particular
instance of an object.

You need to copy all the affects from the obj index to the particular
object, then modify the copies.  There's a function in ROM that copies
the affects for you.  Before the for loop above, add:
        affect_enchant ( obj );
Then change the for loop to start at obj->affected.


The second major problem you have is that your logic is completely wrong.

for (each affect on the object)
        if ( this affect is the right one )
                modify it
                return
        else
                create a new affect and add it to the object

The code as is will work correctly in exactly one case: the obj index
has one affect, and it is not the one you're changing.  In all other
cases, it will do the wrong thing.

You should not be creating a new affect for each non-match you see.  You
should create exactly one new affect if no matching affect was found.

Since your for loop returns if it finds the right one, the code after
the for loop will never get run if the affect is found.  This is where
you want the code to create a new affect.


Dennis



Reply via email to