01 Jul, 2009, Runter wrote in the 21st comment:
Votes: 0
Banner said:
How do you assign them to a slot in the array?


You're pretty close.

void do_randplanet( CHAR_DATA * ch, char *argument ) {
PLANET_DATA *planet;
AREA_DATA *area;
AREA_DATA *parea;
AREA_DATA **area_list; // an array of pointers to AREA_DATA
int count;

if( (planet = get_planet(argument)) == NULL ) {
send_to_char( "&RNo such planet.\n\r",ch );
return;
}

// I'm assuming that this is indeed how you iterate this list.
for( area = planet->first_area; area; area = area->next_on_planet )
count++;

area_list = (AREA_DATA **) malloc(sizeof(AREA_DATA*) * count);

for(area = planet->first_area;area;area = area->next_on_planet)
area_list[count–] = area;

parea = area_list[number_range(0, count-1)];

ch_printf( ch, "Planet: %s Selected Area:%s", planet->name, parea->name );
free(area_list);
return;
}



Assuming some of your code was right that I cannot confirm–That would indeed be correct.

edit: I did the population loop wrong. Fixed now.
02 Jul, 2009, Banner wrote in the 22nd comment:
Votes: 0
void do_randplanet( CHAR_DATA * ch, char *argument )
{
PLANET_DATA *planet;
AREA_DATA *area;
AREA_DATA *parea;
AREA_DATA **area_list; // an array of pointers to AREA_DATA
int count=0;

if( (planet = get_planet(argument)) == NULL )
{
send_to_char( "&RNo such planet.\n\r",ch );
return;
}
for( area = planet->first_area; area; area = area->next_on_planet )
count++;


area_list = (AREA_DATA **) malloc(sizeof(AREA_DATA*) * count);

for( area = planet->first_area; area; area = area->next_on_planet )
area_list[count–] = area;


parea = area_list[number_range(0, count-1)];


ch_printf( ch, "Planet: %s Selected Area:%s", planet->name, parea->name );
free(area_list);
return;
}


Thu Jul  2 00:08:29 2009 :: Log Banner: cedit randplanet create

Program received signal SIGSEGV, Segmentation fault.
0xb7e3961b in strlen () from /lib/libc.so.6
(gdb) bt
#0 0xb7e3961b in strlen () from /lib/libc.so.6
#1 0xb7e093ec in vfprintf () from /lib/libc.so.6
#2 0xb7e2ba64 in vsnprintf () from /lib/libc.so.6
#3 0x08103448 in ch_printf (ch=0x8f20450, fmt=0x82b3221 "Planet: %s Selected Area:%s") at color.c:1398
#4 0x081956dd in do_randplanet (ch=0x8f20450, argument=0xbff7ebeb "Coruscant") at misc.c:5769
#5 0x08166758 in interpret (ch=0x8f20450, argument=0xbff7ebeb "Coruscant") at interp.c:505
#6 0x08108866 in game_loop () at comm.c:787
#7 0x08107551 in main (argc=2, argv=0xbff7f0d4) at comm.c:302
(gdb) frame 3
#3 0x08103448 in ch_printf (ch=0x8f20450, fmt=0x82b3221 "Planet: %s Selected Area:%s") at color.c:1398
1398 vsnprintf( buf, MAX_STRING_LENGTH * 2, fmt, args );
(gdb) frame 4
#4 0x081956dd in do_randplanet (ch=0x8f20450, argument=0xbff7ebeb "Coruscant") at misc.c:5769
5769 ch_printf( ch, "Planet: %s Selected Area:%s", planet->name, parea->name );
(gdb) print parea->name
$1 = 0x99 <Address 0x99 out of bounds>
(gdb) print parea
$2 = (AREA_DATA *) 0x8341c00
(gdb) print *parea
$3 = {next = 0xb7f03190, prev = 0x21, next_sort = 0x0, prev_sort = 0x20647261, first_reset = 0x7661656c, last_reset = 0x65207365, planet =
0x2e747361, next_on_planet = 0xd0a,
prev_on_planet = 0x0, name = 0x99 <Address 0x99 out of bounds>,
filename = 0xb7f03220 "\0302ð·\0302ð· \0344\b \0344\b(2ð·
(2ð·02ð·02ð·82ð·82ð·@2ð·@2ð·H2ð·H2ð·P2ð·P2ð·X2ð·X2ð·`2ð·`2ð·h2ð·h2ð·p2ð·p2ð·x2ð·x2ð·\2002ð·\2002ð·\2102ð·
\2102ð·\2202ð·\2202ð·\2302ð·
\2302ð· 2ð· 2ð·¨2ð·¨2ð·°2ð·°2ð·¸2ð·¸2ð·À2ð·À2ð·È2ð·È2ð·Ð2ð·Ð2ð·Ø2ð·Ø2ð·"…, flags = -1208995296,
status = 16, age = 0, nplayer = 136, reset_frequency = 0,
low_r_vnum = 137632960, hi_r_vnum = 150075232, low_o_vnum = 0, hi_o_vnum = 0, low_m_vnum = 0, hi_m_vnum = 0, low_soft_range = 0,
hi_soft_range = 0, low_hard_range = 0,
hi_hard_range = 167772160, author = 0x20000000 <Address 0x20000000 out of bounds>, resetmsg = 0x5b000000 <Address 0x5b000000 out of


bounds>, last_mob_reset = 0x2d000000,
last_obj_reset = 0x31000000, max_players = 0, mkills = 754974720, mdeaths = 822083584, pkills = 1526726656, pdeaths = 754974720,
gold_looted = 0, illegal_pk = 0,
high_economy = -1090519040, low_economy = -1090519040}
(gdb)



:(


Edit to fix board breaking lines.
02 Jul, 2009, David Haley wrote in the 23rd comment:
Votes: 0
I think that count– should be –count. If count is one, you want to be assigning to slot 0, not slot 1.
02 Jul, 2009, Banner wrote in the 24th comment:
Votes: 0
David Haley said:
I think that count– should be –count. If count is one, you want to be assigning to slot 0, not slot 1.


[Force]: 30014/30014 [Align]: evil
[Health]: 95344/100000 [Move]: 30000/30000 [$$]:[0]|>
randplanet coruscant
Planet: Coruscant Selected Area:Coruscant - Menari Spaceport
[Force]: 30014/30014 [Align]: evil
[Health]: 95344/100000 [Move]: 30000/30000 [$$]:[0]|>
randplanet coruscant
Planet: Coruscant Selected Area:Coruscant - Menari Spaceport
[Force]: 30014/30014 [Align]: evil
[Health]: 95344/100000 [Move]: 30000/30000 [$$]:[0]|>
randplanet coruscant
Planet: Coruscant Selected Area:Coruscant - Menari Spaceport
[Force]: 30014/30014 [Align]: evil
[Health]: 95344/100000 [Move]: 30000/30000 [$$]:[0]|>
randplanet coruscant
Planet: Coruscant Selected Area:Coruscant - Menari Spaceport
[Force]: 30014/30014 [Align]: evil
[Health]: 95344/100000 [Move]: 30000/30000 [$$]:[0]|>
randplanet coruscant
Planet: Coruscant Selected Area:Coruscant - Menari Spaceport
[Force]: 30014/30014 [Align]: evil
[Health]: 95344/100000 [Move]: 30000/30000 [$$]:[0]|>
randplanet coruscant
Planet: Coruscant Selected Area:Coruscant - Menari Spaceport
[Force]: 30014/30014 [Align]: evil
[Health]: 95344/100000 [Move]: 30000/30000 [$$]:[0]|>
randplanet coruscant
Planet: Coruscant Selected Area:Coruscant - Menari Spaceport
[Force]: 30014/30014 [Align]: evil
[Health]: 95344/100000 [Move]: 30000/30000 [$$]:[0]|>
randplanet coruscant
Planet: Coruscant Selected Area:Coruscant - Menari Spaceport
[Force]: 30014/30014 [Align]: evil
[Health]: 95344/100000 [Move]: 30000/30000 [$$]:[0]|>
randplanet coruscant
Planet: Coruscant Selected Area:Coruscant - Menari Spaceport
[Force]: 30014/30014 [Align]: evil
[Health]: 95344/100000 [Move]: 30000/30000 [$$]:[0]|>
randplanet coruscant
Planet: Coruscant Selected Area:Coruscant - Menari Spaceport
[Force]: 30014/30014 [Align]: evil
[Health]: 95344/100000 [Move]: 30000/30000 [$$]:[0]|>
randplanet coruscant
Planet: Coruscant Selected Area:Coruscant - Menari Spaceport
02 Jul, 2009, Runter wrote in the 25th comment:
Votes: 0
That's because count is being decreased. You need to use the actual number of areas found in the number_range call.
02 Jul, 2009, Runter wrote in the 26th comment:
Votes: 0
void do_randplanet( CHAR_DATA * ch, char *argument )
{
PLANET_DATA *planet;
AREA_DATA *area;
AREA_DATA *parea;
AREA_DATA **area_list; // an array of pointers to AREA_DATA
int count = 0, count2=0;

if( (planet = get_planet(argument)) == NULL )
{
send_to_char( "&RNo such planet.\n\r",ch );
return;
}
for( area = planet->first_area; area; area = area->next_on_planet )
count++;

count2 = count;
area_list = (AREA_DATA **) malloc(sizeof(AREA_DATA*) * count);

for( area = planet->first_area; area; area = area->next_on_planet )
area_list[–count] = area;


parea = area_list[number_range(0, count2-1)];


ch_printf( ch, "Planet: %s Selected Area:%s", planet->name, parea->name );
free(area_list);
return;
}
02 Jul, 2009, Banner wrote in the 27th comment:
Votes: 0
Runter said:
That's because count is being decreased. You need to use the actual number of areas found in the number_range call.


What do you mean? Do I need another integer variable to count the areas or something?
02 Jul, 2009, Runter wrote in the 28th comment:
Votes: 0
Banner said:
Runter said:
That's because count is being decreased. You need to use the actual number of areas found in the number_range call.


What do you mean? Do I need another integer variable to count the areas or something?


The code I posted should fix it. I added a new variable to store the count even when the other is decreased.
02 Jul, 2009, Banner wrote in the 29th comment:
Votes: 0
Runter said:
Banner said:
Runter said:
That's because count is being decreased. You need to use the actual number of areas found in the number_range call.


What do you mean? Do I need another integer variable to count the areas or something?


The code I posted should fix it. I added a new variable to store the count even when the other is decreased.


Oh, I didn't see that. :redface:

EDIT: And it works like a charm! Much thanks Runter and David. :)
02 Jul, 2009, Runter wrote in the 30th comment:
Votes: 0
No problem. I'd advice putting that in a function of its own. If you need any help with selecting the random room let me know. For equal distribution you should do it the same way. (Not using random number selection.)
02 Jul, 2009, Banner wrote in the 31st comment:
Votes: 0
I dropped it into a function and will convert it into a random room one too. I tested it in use in the skill I'd intended it for and it works flawlessly.
02 Jul, 2009, Sharmair wrote in the 32nd comment:
Votes: 0
Or, you could forget about the double walking of the list, and having to worry
about memory management and just pick your random area as you walk the
list one time by replacing:
for( area = planet->first_area; area; area = area->next_on_planet )
count++;

count2 = count;
area_list = (AREA_DATA **) malloc(sizeof(AREA_DATA*) * count);

for( area = planet->first_area; area; area = area->next_on_planet )
area_list[–count] = area;


parea = area_list[number_range(0, count2-1)];


ch_printf( ch, "Planet: %s Selected Area:%s", planet->name, parea->name );
free(area_list);

With:
parea = NULL;
count = 0;
for(area = planet->first_area; area; area = area->next_on_planet){
if(number_range(0, count) == 0)
parea = area;
++count;
}
ch_printf(ch, "Planet: %s Selected Area:%s", planet->name, parea->name);

In your function shown here. You can also get rid of some now unused variables. You
can also put this in a function of it's own. Note that it will return NULL if there is no
areas in the list, but not have any problems with such a case (unlike your original code
here). Note also that you would not have to set count to zero twice, I put that line
here so all the code is in one place.
02 Jul, 2009, Runter wrote in the 33rd comment:
Votes: 0
Your algorithm does have equal distribution and a bit more efficient, but at this point I think he's just concerned with getting the basics down. Either way, you won't see a serious difference in overall application performance and both solutions work pretty well.
20.0/33