Login
User Name:

Password:



Register
Forgot your password?
Vote for Us!
auth_update crash
Dec 23, 2017, 10:15 pm
By Remcon
check_tumble
Dec 18, 2017, 7:21 pm
By Remcon
parse description bug
Dec 15, 2017, 10:08 pm
By Remcon
Couple bugs
Dec 12, 2017, 5:42 pm
By Remcon
Bug in disarm( )
Nov 12, 2017, 6:54 pm
By GatewaySysop
LoP 1.46
Author: Remcon
Submitted by: Remcon
LOP 1.45
Author: Remcon
Submitted by: Remcon
LOP Heroes Edition
Author: Vladaar
Submitted by: Vladaar
Heroes sound extras
Author: Vladaar
Submitted by: Vladaar
6Dragons 4.3
Author: Vladaar
Submitted by: Vladaar
Users Online
CommonCrawl, Yandex

Members: 0
Guests: 19
Stats
Files
Topics
Posts
Members
Newest Member
478
3,708
19,242
612
Jacki72H
Today's Birthdays
There are no member birthdays today.
Related Links
» SmaugMuds.org » General » Coding » pointer to a bitset?
Forum Rules | Mark all | Recent Posts

pointer to a bitset?
< Newer Topic :: Older Topic >

Pages:<< prev 1 next >>
Post is unread #1 Aug 21, 2009, 8:55 am
Go to the top of the page
Go to the bottom of the page

Andril
Magician
GroupMembers
Posts147
JoinedJun 9, 2009

I started feeling silly again so I decided to convert the bitvector system over to std::bitset. I would like to be able to just make a copy of flag_string so I can get a list of set flags in-game. flag_string takes a pointer to an int variable as its first argument, and a pointer to the array that contains the actual string names of the flags your running through. The issue though, is how do I pass a bitset to the new version of flag_string when I want to use it for all the places that, currently, are bitvectors, when I don't know how many elements are in the bitset that's getting passed to it?

It'd be simple if it was only for room flags as an example, then I could just do char *flag_string2( std::bitset<MAX_ROOM_FLAGS> *bitset, char *const flagarray[] ). But like I said, I'd like to use it for everything so what could I put in place of the MAX_ROOM_FLAGS in the above example?
       
Post is unread #2 Aug 21, 2009, 10:38 am
Go to the top of the page
Go to the bottom of the page

David Haley
Sorcerer
GroupMembers
Posts903
JoinedJan 29, 2007

Something like this (disclaimer: this is untested although I know the general idea works):

template <int SIZE>
const char* flag_string2(const std::bitset<SIZE>* bitset, const char** const flagarray) {
  ...
}


The compiler will be able to instantiate one copy of the flag_string2 function per bitset size. Since it's a template function, you will need to put its definition in .h files instead of just the declaration.
       
Post is unread #3 Aug 21, 2009, 10:55 am
Go to the top of the page
Go to the bottom of the page

Samson
Black Hand
GroupAdministrators
Posts3,639
JoinedJan 1, 2002

template < size_t N > char *bitset_string( bitset < N > bits, const char *flagarray[] )
{
   static char buf[MSL];
   size_t x;

   buf[0] = '\0';
   for( x = 0; x < bits.size(  ); ++x )
   {
      if( bits.test( x ) )
      {
         mudstrlcat( buf, flagarray[x], MSL );
         // don't catenate a blank if the last char is blank  --Gorog 
         if( buf[0] != '\0' && ' ' != buf[strlen( buf ) - 1] )
            mudstrlcat( buf, " ", MSL );
      }
   }

   if( ( x = strlen( buf ) ) > 0 )
      buf[--x] = '\0';

   return buf;
}


That should do the trick. If you don't have mudstrlcat, change it to strncat, the function is argument compatible.
       
Post is unread #4 Aug 21, 2009, 1:58 pm
Go to the top of the page
Go to the bottom of the page

Andril
Magician
GroupMembers
Posts147
JoinedJun 9, 2009

Ok, I threw the bitset_string guy into the bottom of mud.h, did a make clean and then make.

Was getting an error about there being no matching function call to "bitset_string(std::bitset<35u>&, char* const [])" from the compiler. So I changed the const char *flagarray[] to char *const flagarray[] and away she went. Just did a copyover, used the command I made to set all the proper bits based on what's currently set in all the rooms in an area and it's showing exactly what it's supposed to. :)

Now I get to deal with the rest of it.

Thanks for the help David and Samson.
       
Post is unread #5 Aug 21, 2009, 3:02 pm
Go to the top of the page
Go to the bottom of the page

Samson
Black Hand
GroupAdministrators
Posts3,639
JoinedJan 1, 2002

I'm sure I had a reason to have it as "const char* flagarray[]" but I don't remember now. Likely just the way I went about solving const stuff way back when. Before the current crop of const errors even.
       
Post is unread #6 Aug 21, 2009, 3:32 pm   Last edited Aug 21, 2009, 4:20 pm by Andril
Go to the top of the page
Go to the bottom of the page

Andril
Magician
GroupMembers
Posts147
JoinedJun 9, 2009

My guess would be how the list of flag names were set up in build.c and whatnot.

Got another question for you. How to load in the area files now that I've saved everything over to a long string of 1's and 0's?
Here's what I tried.
In load_rooms in db.c
at the top
char *flagstring;

then later on down
ln = fread_line( fp );
sscanf( ln, "%s %d %d %d %d", flagstring, &x3, &x4, &x5, &x6 ); 
pRoomIndex->sector_type = x3; 
pRoomIndex->tele_delay = x4;
pRoomIndex->tele_vnum = x5; 
pRoomIndex->tunnel = x6;
			
pRoomIndex->room_flags.reset();
for( int i = 0; i < MAX_ROOM_FLAGS; i++ )
    if ( atoi( flagstring++ ) == 1 )
        xTOGGLE_BITx( pRoomIndex->room_flags, i );

Needless to say, it's not working.

Edit:
Maybe I should just have room flags on their own line and use fread_word.

Edit 2:
Ok, using flagstring = fread_word( fp ); before the line = fread_line( fp ); seems to work fine.

Edit 3:
Ok, it's definitely saving the stuff properly. But for some reason it's not reading the string into char *flagstring correctly. The only thing that's not getting set to 0, regardless of what's in the area file is, for some reason, the nomob flag.
       
Post is unread #7 Aug 21, 2009, 4:49 pm
Go to the top of the page
Go to the bottom of the page

Samson
Black Hand
GroupAdministrators
Posts3,639
JoinedJan 1, 2002

You should be using fread_flagstring() to read a string of flags, assuming they were saved with the terminating tilde. That's a specialized version of fread_string that doesn't allocate the string to the hash or to the heap.

But before going any further - which specific codebase are you working with? One of the FUSS bases or something else?
       
Post is unread #8 Aug 21, 2009, 4:55 pm   Last edited Aug 21, 2009, 5:04 pm by Andril
Go to the top of the page
Go to the bottom of the page

Andril
Magician
GroupMembers
Posts147
JoinedJun 9, 2009

SWR 1.0 with most of the FUSS fixes.
Also, the way I've currently got the saving set up for this is just a string of 1's and 0's. 000100000000000000000000000000000000 8 would be the flags and then the sector.
       
Post is unread #9 Aug 22, 2009, 3:54 pm
Go to the top of the page
Go to the bottom of the page

David Haley
Sorcerer
GroupMembers
Posts903
JoinedJan 29, 2007

You probably don't want to be converting the entire string of 0s and 1s at a time. It's much more likely that you want to be acting on a character at a time, and testing against '0' or '1'. (You don't really need to convert the 0/1 to an integer.)
       
Post is unread #10 Aug 22, 2009, 4:54 pm
Go to the top of the page
Go to the bottom of the page

Andril
Magician
GroupMembers
Posts147
JoinedJun 9, 2009

*slaps himself on the forehead* You're right.

Hmm, because char *flagstring isn't an array, flagstring points to a single element in whatever string it's pointed at, starting at the first element, right?
Okay, I changed that for loop in load_rooms to the following and it appears to work correctly, more or less.
for( int i = 0; i < MAX_ROOM_FLAGS; i++, flagstring++ )
    if ( *flagstring == '1' )
        xTOGGLE_BITx( pRoomIndex->room_flags, i );


For some odd reason though, every time I copyover the nomob flag is being set on and off. Think I'll need to take a closer look at what I'm doing in fold_area.
       
Post is unread #11 Aug 22, 2009, 5:27 pm   Last edited Aug 22, 2009, 8:43 pm by Andril
Go to the top of the page
Go to the bottom of the page

Andril
Magician
GroupMembers
Posts147
JoinedJun 9, 2009

Will it never end? :sad:

Can someone please tell me why I'm getting an out of range error thrown when checking position 35 of room_flags when the declaration for room_flags in room_index_data is bitset<MAX_ROOM_FLAGS - 1> room_flags; and MAX_ROOM_FLAGS, which is the last member of the enumerator holding room flags is number 36?

Here's the backtrace in gdb.
(gdb) bt
#0 0x00b97402 in __kernel_vsyscall ()
#1 0x002408a0 in raise (sig=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
#2 0x00242271 in abort () at abort.c:88
#3 0x00d60ba0 in __gnu_cxx::__verbose_terminate_handler () from /usr/lib/libstdc++.so.6
#4 0x00d5e685 in ?? () from /usr/lib/libstdc++.so.6
#5 0x00d5e6c2 in std::terminate () from /usr/lib/libstdc++.so.6
#6 0x00d5e7fa in __cxa_throw () from /usr/lib/libstdc++.so.6
#7 0x00cf507f in std::__throw_out_of_range () from /usr/lib/libstdc++.so.6
#8 0x080738e0 in std::bitset<35u>::test (this=0x8f4af90, __position=35) at /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bitset:1090
#9 0x080c1bf1 in fold_area (tarea=0x8f440d8, filename=0x8f44198 "limbo.are", install=false, backup=false) at build.c:1159
#10 0x080c2e7d in do_foldarea (ch=0x8face20, argument=0x820e6f6 "";) at build.c:3375
#11 0x08120ba8 in do_saveall (ch=0x8face20, argument=0x8201337 " ";) at graddons.c:187
#12 0x080f0031 in do_copyover (ch=0x8face20, argument=0xbfda951e "now";) at comm.c:3819
#13 0x0813603b in interpret (ch=0x8face20, argument=0xbfda951e "now";) at interp.c:332
#14 0x080f41a2 in game_loop () at comm.c:850
#15 0x080f49f1 in main (argc=4, argv=0xbfdb9604) at comm.c:561

From what I can tell, there are 35 elements in that bitset, which is what I'd expect, according to what's shown for frame 8.

Edit:
Meh, just removed the - 1 from where room_flags is in room_index_data. Still only saves the right stuff so no extra 0's in the area file for room flags.
       
Post is unread #12 Aug 22, 2009, 11:11 pm
Go to the top of the page
Go to the bottom of the page

David Haley
Sorcerer
GroupMembers
Posts903
JoinedJan 29, 2007

MAX_ROOM_FLAGS is 36, so if you declare it as being of size MAX-1, you only have 35 elements. Therefore index 35 is one beyond the last valid index.
       
Post is unread #13 Aug 23, 2009, 1:01 am   Last edited Aug 23, 2009, 1:05 am by Andril
Go to the top of the page
Go to the bottom of the page

Andril
Magician
GroupMembers
Posts147
JoinedJun 9, 2009

Hmm. 0-35 is 36 elements. I have 35 flags so, you're absolutely correct. Wait, I actually have 36, cause 35 is the last one, starting from 0. Meh, I really need to pull my head out of my rear and think about this stuff a little more thoroughly. I guess that means that the array of chars I use to store the flags prior to saving needs to be declared with MAX_ROOM_FLAGS + 1, for that NULL terminating character. Hmm, I wonder if this might not be part of the problem with the nomob flag being constantly toggled on/off during area folding?

Thanks for pointing that out to me. :)

Edit:
Yup, that seems to have fixed the problem!
Guess it's time to write a general use function for the string used to store the stuff for saving, and another for loading and then get to work actually converting the other stuff.
       
Post is unread #14 Aug 24, 2009, 5:29 am
Go to the top of the page
Go to the bottom of the page

David Haley
Sorcerer
GroupMembers
Posts903
JoinedJan 29, 2007

Zero-based indexing is confusing at first, because if you declare something to be of size X, the valid indices are 0..(X-1), and index X in particular is invalid -- even if there are that many entries in the array. And it gets more confusing when you start doing index math, like going to the end of the array and stepping backwards -- you need to account for occasional +1 here, -1 there... It takes a little getting used to, but eventually it'll become second-nature. :smile: Good that you got your problem worked out.
       
Post is unread #15 Aug 24, 2009, 7:49 am
Go to the top of the page
Go to the bottom of the page

Andril
Magician
GroupMembers
Posts147
JoinedJun 9, 2009

Yeah, usually I don't have any problems keeping how arrays work in mind when it comes to how many elements there are. I think it was because I was concentrating mostly on the char array for the saving stuff, and having to remind myself that I needed an extra element for the '\0', MAX_ROOM_FLAGS + 1, at the end that threw me off. :)

And it turns out that the deal with the constantly flipping nomob flag was in the fix_exits function. We added in OLC turbolifts, based on Gavin and Grevan's OLC Shuttle code, which was apparently confusing the hell out of it heh.
       
Pages:<< prev 1 next >>