Sheath/Draw

Posted by Shawncplus on Sat 14 Apr 2007 09:40 PM — 30 posts, 96,918 views.

USA #0
I have completely worked out all the code for Sheath and Draw accept two bits, actually moving the weapon from and to the container.

I have all the checks and special item types in place for the sheath itemtype.

This is the code I have now

void do_sheath( CHAR_DATA * ch, char *argument )
{
   OBJ_DATA *w_weapon;
   OBJ_DATA *w_sheath;
   if( ( w_weapon = get_obj_wear( ch, argument ) ) == NULL )
   {
      send_to_char( "Sheath what?\r\n", ch );
      return;
   }
   if( ( w_sheath = get_eq_char(ch, WEAR_SHEATH ) ) == NULL )
   {
      send_to_char("\nYou must have something to put the weapon into.\r\n", ch);
      return;
   }
   else
   {
   w_weapon = obj_to_obj( w_sheath, w_weapon );
    ch_printf(ch, "You dextrously slide %s into %s", w_weapon->short_descr, w_sheath->short_descr);
    return;  
   }
}

void do_draw( CHAR_DATA * ch, char *argument )
{
   OBJ_DATA *w_weapon;
   OBJ_DATA *w_sheath;
   if( ( w_weapon = get_obj_wear( ch, argument ) ) == NULL )
   {
      send_to_char( "Draw what?\r\n", ch );
      return;
   }
   if( ( w_sheath = get_eq_char(ch, WEAR_SHEATH ) ) == NULL )
   {
      send_to_char("\nYou don't even have a sheath to draw from.\r\n", ch);
      return;
   }
   else
   {       

   obj_from_obj( w_weapon );
   w_weapon = obj_to_char( w_weapon, ch );
   equip_char(ch, w_weapon, WEAR_WIELD);
    ch_printf(ch, "You quickly draw %s from %s", w_weapon->short_descr, w_sheath->short_descr);
    return;  
   }
}


This makes some major bugs the way it currently is.
It sometimes duplicates the sheath when the MUD is rebooted, also it creates an infinite bug loop stating the weapon has already been released.
I have no clue where to go from here short of throwing my laptop against the wall.
Any help would be appreciated

Shawncplus,
LazyCode.info
USA #1
I don't see the code where it would produce a message saying it was already released.

There are also a few things you should do in the code. You should first check if the argument actually exists, before making it check if the argument is a valid item the player has.
Amended on Sat 14 Apr 2007 09:47 PM by Zeno
USA #2
No, when I shut the mud down it gives me a log bug
like [BUG] Object 810 has already been released! or something like that.

This is one of the bugs it shot out, it core dumped and I don't have GDC so this is all I got.

Log: [*****] BUG: equip_char: obj not being carried by ch!
Amended on Sat 14 Apr 2007 09:52 PM by Shawncplus
USA #3
I don't see that in the stock source. What's the exact message?
USA #4
I posted the bug. The weird thing is that the messages go as usual but it's not actually putting the item in it's in_obj even though in the obj_to_obj function it already sets it.
USA #5
You need to do obj_from_obj, and then obj_to_char I think.
USA #6
I did that and it stopped the aforementioned bug but it still doesn't actually show in the list of items in the sheath. Also when I try to draw the item I get this bug.

Log: [*****] BUG: Obj_from_obj: null obj_from.
USA #7
What itemtype is the sheath? Container? If not, you need to make sure it's setup correctly to save objs inside of it.

But I'm a bit confused. Is sheath just a wear loc? Or is it a wear loc and an itemtype?
USA #8
I have the wear location WEAR_SHEATH, the itemtype ITEM_SHEATH, and all the necessary functions. You can also directly put and get items from the sheath as long as they are weapons, and the sheath shows up on the eq list when you type eq as a new item. I also have it set up so it correctly creates the type and wear loc with oset
USA #9
I don't think the obj_to_char function returns anything, by the way.

And you say that bug message printed out in an infinite loop? I don't see how it could cause an infinite loop.
USA #10
It's not anymore but I wanna break down why it's not actually placing the weapon inside the sheath. Because obj_to_char actually puts the item in the inventory. I tried a work around with obj_to_char then using equip_char(ch, w_weapon, WEAR_WIELD); but that didn't work either.
USA #11
Looking over my sheath/draw code, you're missing things:
separate_obj()
obj_from_char()
if char_died() [to be safe]

Oh and obj_to_char does return the obj, my bad.

I would also assume you should check if the item is valid to be sheathed.
Amended on Sat 14 Apr 2007 10:26 PM by Zeno
USA #12
This is the new code for sheath after the checks but it's still giving the same problem.

I'll add the died check later

   separate_obj(w_weapon);
   obj_from_char( w_weapon );
   w_weapon = obj_to_obj( w_sheath, w_weapon );
    ch_printf(ch, "You dextrously slide %s into %s", w_weapon->short_descr, w_sheath->short_descr);
    return;  



It's still not actually placing the item in the sheath. It did originally and I can't remember what the crap I changed.

EDIT: I was able to reproduce the error and this is it.

BUG: extract_obj: obj 1000 already extracted
!
Amended on Sat 14 Apr 2007 10:58 PM by Shawncplus
USA #13
Did you do obj_from_char as well for sheathing?
USA #14
That was the code for sheathing, I'm going to debug sheath before draw.


void do_sheath( CHAR_DATA * ch, char *argument )
{
   OBJ_DATA *w_weapon;
   OBJ_DATA *w_sheath;
   if( ( w_weapon = get_obj_wear( ch, argument ) ) == NULL )
   {
      send_to_char( "Sheath what?\r\n", ch );
      return;
   }
   if( ( w_sheath = get_eq_char(ch, WEAR_SHEATH ) ) == NULL )
   {
      send_to_char("\nYou must have something to put the weapon into.\r\n", ch);
      return;
   }
   else
   {

   separate_obj(w_weapon);
   obj_from_char( w_weapon );
   w_weapon = obj_to_obj( w_weapon, w_sheath );
    ch_printf(ch, "You dextrously slide %s into %s", w_weapon->short_descr, w_sheath->short_descr);
    return;  
   }
}


I can't figure out for the life of me what is going wrong here. I though it was because I didn't do save_char_obj but that just game me more and more bugs.
USA #15
What is obj 1000? Sheath or weapon? And that's the error you're still getting?
USA #16
In do_sheath, you probably want to actually unequip the weapon, and not just do obj_from_char.
USA #17
I just tested that and it seem to be working so now I just have to work on Draw. The thing I need to fix is that it's checking the wrong thing. It should be doing
w_weapon = get_obj_list( ch, argument, w_sheath->first_content ) 

instead of checking to see if it's equipped.

This is what I have for Draw


void do_draw( CHAR_DATA * ch, char *argument )
{
   OBJ_DATA *w_weapon;
   OBJ_DATA *w_sheath;
   w_weapon = get_obj_list( ch, argument, w_sheath->first_content);
   if( !w_weapon )
   {
      send_to_char( "Draw what?\r\n", ch );
      return;
   }
   if( ( w_sheath = get_eq_char(ch, WEAR_SHEATH ) ) == NULL )
   {
      send_to_char("\nYou don't even have a sheath to draw from.\r\n", ch);
      return;
   }
   else
   {
   get_obj( ch, w_weapon, w_sheath );
   obj_to_char( w_weapon, ch ); 
   equip_char( ch, w_weapon, WEAR_WIELD );
   ch_printf(ch, "You quickly draw %s from %s", w_weapon->short_descr, w_sheath->short_descr);
   
    return;  
   }
}


This is giving me a core dump and I can't figure out why (though it's probably fairly obvious)
USA #18
You could use gdb to track down at least what line is the problem.
USA #19
Yeah I have GDC and I'm not sure how to use it with Cygwin.
Any tips?
USA #20
There is a guide to gdb right here on Gammon.
http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=3653
USA #21
OK, I've got it working without GDC. It works fine without bugs but it gives me the get message along with the draw message because I use get_obj() Here is the code:


void do_draw( CHAR_DATA * ch, char *argument )
{
   OBJ_DATA *w_weapon;
   OBJ_DATA *w_sheath;
   
   if( argument == '\0' )
   {
      send_to_char( "Draw what?\r\n", ch );
      return;
   }
   if( ( w_sheath = get_eq_char(ch, WEAR_SHEATH ) ) == NULL )
   {
      send_to_char("\nYou don't even have a sheath to draw from.\r\n", ch);
      return;
   }
   else
   {

   w_weapon = get_obj_list( ch, argument, w_sheath->first_content);
   if( !w_weapon )
         {
            act( AT_PLAIN, "There is no $T sheathed.", ch, NULL, argument, TO_CHAR );
            return;
         }
   get_obj( ch, w_weapon, w_sheath );
   obj_to_char( w_weapon, ch ); 
   equip_char( ch, w_weapon, WEAR_WIELD );
   ch_printf(ch, "You quickly draw %s from %s", w_weapon->short_descr, w_sheath->short_descr);
   
    return;  
   }
}



If you guys/gals know a good substitution for get_obj()
please let me know, Zeno you've been a huge help

EDIT: I replaced get_obj() with obj_from_obj() and it _seems_ to be working bug free (keyword seems).
Amended on Sun 15 Apr 2007 03:53 AM by Shawncplus
USA #22
Yes, obj_from_obj is what you want to be using, not get_obj. Typically a good strategy is to look at how another function is implemented, like the get command, and imitate it; for example, get calls get_obj which then uses obj_from_obj.

By the way, it's gdb, not gdc.
USA #23
GDB is GNU DeBugger, GDC is GNU Debugger for Cygwin
USA #24
Are you using a non-standard Cygwin distribution? Even under Cygwin the package is named gdb:

http://www.cygwin.com/packages/

The gnu-gdc package is a compiler for the D programming language.
USA #25
Either way it's done and this is a log of it in action.

<wielded> a shortsword named "Snakebite"
<worn as sheath> (PROTO) a newly created sheath

You dextrously slide a shortsword named "Snakebite" into a newly created sheath

You are using:
<worn as sheath> (PROTO) a newly created sheath (a shortsword named "Snakebite")

You quickly draw a shortsword named "Snakebite" from a newly created sheath

You are using:
<wielded> a shortsword named "Snakebite"
<worn as sheath> (PROTO) a newly created sheath
USA #26
Well, I was just curious, because I'd never heard of gdb being specialized for Cygwin, and if it had been, that'd be pretty neat. I couldn't find it in the package list, so I wasn't sure what you were referring to.

That code is pretty neat. One thing you might want to check for is making sure the sheath/scabbard doesn't have something in it already. And if you want multiple things in the sheath, also test to see that things still work if I take a weapon, sheathe it, then take another, sheathe it, and so forth. And also make sure that things don't break if you draw when something is sheathed. And finally, if you have dual-wield, make sure that works as expected too.
USA #27
Well the entire sheath/draw system was just to test my abilities so I probably won't do anything with dual wield unless I plan on making it a more advanced system. Then again Dual wield is taken from a different iWear so who knows :P.
I am definitely adding a check to see if there is already something in it. Thanks for all the help guys and I might release it if I don't feel lazy and actually get motivated enough to find all the tiny little changes that were required to do the things I wanted it to.
Amended on Mon 16 Apr 2007 05:59 PM by Shawncplus
USA #28
If you have a version of your code before these changes, the diff tool is an excellent and easy way to generate a comprehensive list of changes.
USA #29
one thing you need to do is make sure they aren't already wielding something when drawing something from their sheath. as it is they will draw it out of the sheath regardless but not have them wield it, in fact i'm not too sure what will happen to the item, just that the player will get mixed messages that they dextrously pulled it or whatever and that they couldn't wield it.