MudBytes
Pages: << prev 1, 2, 3 next >>
Area Loop
Banner
Sorcerer






Group: Members
Posts: 381
Joined: Jul 14, 2006

Go to the bottom of the page Go to the top of the page
#16 Posted Jul 1, 2009, 9:00 am

Code (text):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 
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;
   }
   for( area = planet->first_area; area; area = area->next_on_planet )
      count++;
 
   area_list = (AREA_DATA **) malloc(sizeof(AREA_DATA*) * count);
 
   parea = area_list[number_range(0, count-1)];
 
 
   ch_printf( ch, "Planet: %s Selected Area:%s", planet->name, parea->name );
   free(area_list);
   return;
}
 


Code (text):
1
2
3
4
5
6
7
8
9
10
11
12
13
 
Wed Jul  1 12:54:40 2009 :: Log Banner: cedit randplanet create
 
Program received signal SIGSEGV, Segmentation fault.
0x08195677 in do_randplanet (ch=0x8f11ac8, argument=0xbfae9f3b "Coruscant")
    at misc.c:5761
5761       parea = area_list[number_range(0, count-1)];
(gdb) print area_list
$1 = (AREA_DATA **) 0x0
(gdb) 
 
.........................
sudo apt-get sandwich

Directions on a slingshot I bought(seriously):
No in the mouth. No to wind. No aim peoples.

David Haley
Wizard






Group: Members
Posts: 5,730
Joined: Jun 30, 2007

Go to the bottom of the page Go to the top of the page
#17 Posted Jul 1, 2009, 9:31 am

area_list is a NULL pointer. What is count equal to? If count is zero, malloc will probably return a NULL pointer (don't remember off the top of my head).

There's also the problem that you create the array but don't actually put anything into it.

You could also count the areas in one pass, and then skip the array business and just make a random number of hops through the linked list; it'd give you the same effect without the extra allocations.
.........................
-- d.c.h --
BabbleMUD Project (custom codebase)
Legends of the Darkstone (head coder)
http://david.the-haleys.org
.........................

Banner
Sorcerer






Group: Members
Posts: 381
Joined: Jul 14, 2006

Go to the bottom of the page Go to the top of the page
#18 Posted Jul 1, 2009, 9:38 am

David Haley said:
area_list is a NULL pointer. What is count equal to? If count is zero, malloc will probably return a NULL pointer (don't remember off the top of my head).

Count should be equal to 6.

David Haley said:
There's also the problem that you create the array but don't actually put anything into it.

I did it how Runter said. I've never created arrays before so beyond that I don't know what to do.

DavidHaley said:
You could also count the areas in one pass, and then skip the array business and just make a random number of hops through the linked list; it'd give you the same effect without the extra allocations.

Could you be more specific on how to actually achieve that?
.........................
sudo apt-get sandwich

Directions on a slingshot I bought(seriously):
No in the mouth. No to wind. No aim peoples.

David Haley
Wizard






Group: Members
Posts: 5,730
Joined: Jun 30, 2007

Go to the bottom of the page Go to the top of the page
#19 Posted Jul 1, 2009, 9:51 am

Well, when stuff doesn't work, you need to stop making assumptions about what things should or shouldn't be and check in the debugger. :wink: Obviously something is going wrong because area_list is coming back as a null pointer.

As for the array: well, an array is just a sequence of slots. What you've done is create that sequence of slots. But you still need to put things into the slots, by looping over the areas again and assigning them to the next slot.

As for the list: you first get the count, then you start at the first area. You pick a random number, and you move forward in the linked list that many times by taking area->next_on_planet (or whatever the linked list uses) to step forward in the list. It's just a list traversal, except that you count how many nodes you traverse.
.........................
-- d.c.h --
BabbleMUD Project (custom codebase)
Legends of the Darkstone (head coder)
http://david.the-haleys.org
.........................

Banner
Sorcerer






Group: Members
Posts: 381
Joined: Jul 14, 2006

Go to the bottom of the page Go to the top of the page
#20 Posted Jul 1, 2009, 9:53 am

How do you assign them to a slot in the array?
.........................
sudo apt-get sandwich

Directions on a slingshot I bought(seriously):
No in the mouth. No to wind. No aim peoples.

Runter
Wizard






Group: Members
Posts: 1,074
Joined: Jun 1, 2006

Go to the bottom of the page Go to the top of the page
#21 Posted Jul 1, 2009, 12:00 pm

Banner said:
How do you assign them to a slot in the array?


You're pretty close. 

Code (text):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 
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.
.........................
-Heath

For once you have tasted flight Ruby you will walk the earth with your eyes turned skywards,
for there you have been and there you will long to return. --
                                              Leonardo Da Vinci Yukihiro Matsumoto

Last edited Jul 1, 2009, 12:10 pm by Runter
Banner
Sorcerer






Group: Members
Posts: 381
Joined: Jul 14, 2006

Go to the bottom of the page Go to the top of the page
#22 Posted Jul 1, 2009, 8:11 pm

Code (text):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 
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;
}
 


Code (text):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
 
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.
.........................
sudo apt-get sandwich

Directions on a slingshot I bought(seriously):
No in the mouth. No to wind. No aim peoples.

Last edited Jul 1, 2009, 8:15 pm by Banner
David Haley
Wizard






Group: Members
Posts: 5,730
Joined: Jun 30, 2007

Go to the bottom of the page Go to the top of the page
#23 Posted Jul 1, 2009, 10:55 pm

I think that count-- should be --count. If count is one, you want to be assigning to slot 0, not slot 1.
.........................
-- d.c.h --
BabbleMUD Project (custom codebase)
Legends of the Darkstone (head coder)
http://david.the-haleys.org
.........................

Banner
Sorcerer






Group: Members
Posts: 381
Joined: Jul 14, 2006

Go to the bottom of the page Go to the top of the page
#24 Posted Jul 1, 2009, 11:14 pm


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.


Code (text):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 
[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
 
.........................
sudo apt-get sandwich

Directions on a slingshot I bought(seriously):
No in the mouth. No to wind. No aim peoples.

Runter
Wizard






Group: Members
Posts: 1,074
Joined: Jun 1, 2006

Go to the bottom of the page Go to the top of the page
#25 Posted Jul 1, 2009, 11:17 pm

That's because count is being decreased.  You need to use the actual number of areas found in the number_range call.
.........................
-Heath

For once you have tasted flight Ruby you will walk the earth with your eyes turned skywards,
for there you have been and there you will long to return. --
                                              Leonardo Da Vinci Yukihiro Matsumoto

Runter
Wizard






Group: Members
Posts: 1,074
Joined: Jun 1, 2006

Go to the bottom of the page Go to the top of the page
#26 Posted Jul 1, 2009, 11:18 pm

Code (text):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 
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;
}
 
.........................
-Heath

For once you have tasted flight Ruby you will walk the earth with your eyes turned skywards,
for there you have been and there you will long to return. --
                                              Leonardo Da Vinci Yukihiro Matsumoto

Last edited Jul 1, 2009, 11:19 pm by Runter
Banner
Sorcerer






Group: Members
Posts: 381
Joined: Jul 14, 2006

Go to the bottom of the page Go to the top of the page
#27 Posted Jul 1, 2009, 11:20 pm


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?
.........................
sudo apt-get sandwich

Directions on a slingshot I bought(seriously):
No in the mouth. No to wind. No aim peoples.

Runter
Wizard






Group: Members
Posts: 1,074
Joined: Jun 1, 2006

Go to the bottom of the page Go to the top of the page
#28 Posted Jul 1, 2009, 11:23 pm


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.
.........................
-Heath

For once you have tasted flight Ruby you will walk the earth with your eyes turned skywards,
for there you have been and there you will long to return. --
                                              Leonardo Da Vinci Yukihiro Matsumoto

Banner
Sorcerer






Group: Members
Posts: 381
Joined: Jul 14, 2006

Go to the bottom of the page Go to the top of the page
#29 Posted Jul 1, 2009, 11:24 pm

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. :)
.........................
sudo apt-get sandwich

Directions on a slingshot I bought(seriously):
No in the mouth. No to wind. No aim peoples.

Last edited Jul 1, 2009, 11:28 pm by Banner
Runter
Wizard






Group: Members
Posts: 1,074
Joined: Jun 1, 2006

Go to the bottom of the page Go to the top of the page
#30 Posted Jul 2, 2009, 12:16 am

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.)
.........................
-Heath

For once you have tasted flight Ruby you will walk the earth with your eyes turned skywards,
for there you have been and there you will long to return. --
                                              Leonardo Da Vinci Yukihiro Matsumoto

Pages:<< prev 1, 2, 3 next >>

Valid XHTML 1.1! Valid CSS!