Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ SMAUG
➜ SMAUG coding
➜ Problems with code i created
|
Problems with code i created
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
Pages: 1 2
| Posted by
| Jason
(109 posts) Bio
|
| Date
| Fri 05 Mar 2004 10:08 AM (UTC) |
| Message
| ok i made this code in order for people to get there percentage of a skill up a little faster than what the stock code does. What this code does is allows a player to "research" a skill at a library. I had the code working great before i put a timer on it and made it to a skill. if you could please show me what i did wrong with the timer.
void do_research( CHAR_DATA *ch, char *argument )
{
int sn;
int adept = 75;
char arg[MAX_INPUT_LENGTH];
int chance;
strcpy( arg, argument );
switch( ch->substate )
{
default:
if ( IS_NPC(ch) )
return;
if ( argument[0] == '\0' )
{
send_to_char("&RWhat skill would you like to research?\n\r", ch);
return;
}
{
bool can_prac = TRUE;
if ( !IS_AWAKE(ch) )
{
send_to_char( "In your dreams, or what?\n\r", ch );
return;
}
if(!(xIS_SET(ch->in_room->room_flags, ROOM_LIBRARY)))
{
send_to_char( "You need to be in a library to research skills.\n\r", ch );
return;
}
sn = skill_lookup( argument );
if ( sn == -1 )
{
send_to_char("&RYou can't seem to find that skill in any books.\n\r", ch);
return;
}
if ( skill_table[sn]->guild < 0 || skill_table[sn]->guild >= MAX_ABILITY )
{
send_to_char("&RYou cannot learn that skill.\n\r", ch);
return;
}
if ( can_prac && !IS_NPC(ch)
&& ch->skill_level[skill_table[sn]->guild] < skill_table[sn]->min_level )
{
send_to_char("&RYour not ready to research that yet.\n\r", ch);
return;
}
if ( is_name( skill_tname[skill_table[sn]->type], CANT_PRAC ) )
{
send_to_char ("&RYou can't find that in any books.\n\r'", ch);
return;
}
chance = IS_NPC(ch) ? ch->top_level
: (int) (ch->pcdata->learned[gsn_research]);
if ( number_percent( ) < chance )
{
ch->dest_buf = str_dup(arg);
send_to_char( "&GYou you begin the long proccess of researching a skill.\n\r", ch );
add_timer ( ch , TIMER_DO_FUN , 0 , do_research , 1 );
return;
}
send_to_char("&RYou're not quite sure how to do it...\n\r",ch);
learn_from_failure( ch, gsn_research );
return;
case 1:
if ( !ch->dest_buf )
return;
strcpy(arg, ch->dest_buf);
DISPOSE( ch->dest_buf);
break; /*EVERYTHING WORKED RIGHT UP TO THIS POINT THEN THE CODE STOPPED*/
case SUB_TIMER_DO_ABORT:
ch->substate = SUB_NONE;
send_to_char("&RYou fail to complete your research.\n\r", ch);
return;
}
ch->substate = SUB_NONE;
if ( number_percent( ) > chance )
{
send_to_char( "&RAfter much study you fail to learn anything about your skill.\n\r", ch);
learn_from_failure( ch, gsn_research );
return;
}
if ( number_percent( ) < chance )
{
if ( ch->pcdata->learned[sn] >= adept )
{
send_to_char("You've learned everything you can from the books,\n\r", ch);
send_to_char("you must practice it on your own now.\n\r", ch);
}
else
{
ch->pcdata->learned[sn] += 5;
act( AT_ACTION, "You gain some knowledge in the skill of $T.",
ch, NULL, skill_table[sn]->name, TO_CHAR );
}
}
}
return;
}
| | Top |
|
| Posted by
| Jason
(109 posts) Bio
|
| Date
| Reply #1 on Fri 05 Mar 2004 10:10 AM (UTC) |
| Message
| Oh and the way i tested to see where it stopped working was:
send_to_char ("test.\n\r" , ch);
There for it put the word test if it work at that point | | Top |
|
| Posted by
| Meerclar
USA (733 posts) Bio
|
| Date
| Reply #2 on Fri 05 Mar 2004 10:37 AM (UTC) |
| Message
| | It doesnt look like you ever actually exit case 1 for your timer case to ever be evaluated. I'd have to do some testing to be sure (and its after 4am and Ive not slept) but I believe the break you put at the end of case 1 is going to be your problem. Might try finding Nick's GDB guide and putting in some breakpoints if you have a *nix setup. |
Meerclar - Lord of Cats
Coder, Builder, and Tormenter of Mortals
Stormbringer: Rebirth
storm-bringer.org:4500
www.storm-bringer.org | | Top |
|
| Posted by
| Nick Gammon
Australia (23,169 posts) Bio
Forum Administrator |
| Date
| Reply #3 on Fri 05 Mar 2004 08:52 PM (UTC) |
| Message
| Your nesting is wrong. You can prove this in vi by putting the cursor over the braces and hitting "%" (go to matching brace), or pasting into the MUSHclient notepad, and choosing "select to matching brace".
Either way, this brace here:
if ( !ch->dest_buf )
return;
strcpy(arg, ch->dest_buf);
DISPOSE( ch->dest_buf);
break; /*EVERYTHING WORKED RIGHT UP TO THIS POINT THEN THE CODE STOPPED*/
case SUB_TIMER_DO_ABORT:
ch->substate = SUB_NONE;
send_to_char("&RYou fail to complete your research.\n\r", ch);
return;
} // <--- this one
... is matched with one much further up under "default:". Thus the "case SUB_TIMER_DO_ABORT:" is an orphan, not really part of the switch.
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | | Top |
|
| Posted by
| Jason
(109 posts) Bio
|
| Date
| Reply #4 on Sat 06 Mar 2004 01:16 AM (UTC) |
| Message
| | well The SUB_TIMER_DO_ABORT does work. everything after that won't | | Top |
|
| Posted by
| David Haley
USA (3,881 posts) Bio
|
| Date
| Reply #5 on Sat 06 Mar 2004 02:08 AM (UTC) |
| Message
| The nesting is nonetheless incorrect.
This is why if you ever take a programming class, you'll get indentation indented into your skull... no pun intended. :-)
Seriously though, fix your indentation and braces, and life will be good. |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | | Top |
|
| Posted by
| Jason
(109 posts) Bio
|
| Date
| Reply #6 on Sat 06 Mar 2004 02:13 AM (UTC) |
| Message
| | Well might i go about fixing it... I'm not good with timers | | Top |
|
| Posted by
| David Haley
USA (3,881 posts) Bio
|
| Date
| Reply #7 on Sat 06 Mar 2004 03:55 AM (UTC) |
| Message
| It's not a timer problem.
It's a brace matching problem, caused by very poor code indentation. |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | | Top |
|
| Posted by
| Jason
(109 posts) Bio
|
| Date
| Reply #8 on Sat 06 Mar 2004 05:02 AM (UTC) |
| Message
| ok well where did i mess up? I can't find it
| | Top |
|
| Posted by
| David Haley
USA (3,881 posts) Bio
|
| Date
| Reply #9 on Sat 06 Mar 2004 06:27 AM (UTC) |
| Message
| Indent your code, and you'll see where the problem is.
Indenting means to insert tabs (or spaces), like so:
if ( something )
whatever;
else
{
if ( something else )
{
whatever;
}
}
This is the badly indented but equivalent expression:
if ( something )
whatever;
else
{
if ( something else )
{
whatever;
}
}
Which one is easier to read? Which one can you spot errors more easily with? |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | | Top |
|
| Posted by
| Jason
(109 posts) Bio
|
| Date
| Reply #10 on Sat 06 Mar 2004 08:30 AM (UTC) |
| Message
| ok I did what you said and fix the indents... but i still don't see the problem in this dang code... it is about to drive me nuts. Here is the code with the new indents:
void do_research( CHAR_DATA *ch, char *argument )
{
int sn;
int adept = 75;
char arg[MAX_INPUT_LENGTH];
int chance;
strcpy( arg, argument );
switch( ch->substate )
{
default:
if ( IS_NPC(ch) )
return;
if ( argument[0] == '\0' )
{
send_to_char("&RWhat skill would you like to research?\n\r", ch);
return;
}
{
bool can_prac = TRUE;
if ( !IS_AWAKE(ch) )
{
send_to_char( "In your dreams, or what?\n\r", ch );
return;
}
if(!(xIS_SET(ch->in_room->room_flags, ROOM_LIBRARY)))
{
send_to_char( "You need to be in a library to research skills.\n\r", ch );
return;
}
sn = skill_lookup( argument );
if ( sn == -1 )
{
send_to_char("&RYou can't seem to find that skill in any books.\n\r", ch);
return;
}
if ( skill_table[sn]->guild < 0 || skill_table[sn]->guild >= MAX_ABILITY )
{
send_to_char("&RYou cannot learn that skill.\n\r", ch);
return;
}
if ( can_prac && !IS_NPC(ch)
&& ch->skill_level[skill_table[sn]->guild] < skill_table[sn]->min_level )
{
send_to_char("&RYour not ready to research that yet.\n\r", ch);
return;
}
if ( is_name( skill_tname[skill_table[sn]->type], CANT_PRAC ) )
{
send_to_char ("&RYou can't find that in any books.\n\r'", ch);
return;
}
chance = IS_NPC(ch) ? ch->top_level
: (int) (ch->pcdata->learned[gsn_research]);
if ( number_percent( ) < chance )
{
ch->dest_buf = str_dup(arg);
send_to_char( "&GYou you begin the long proccess of researching a skill.\n\r", ch );
add_timer ( ch , TIMER_DO_FUN , 0 , do_research , 1 );
return;
}
send_to_char("&RYou're not quite sure how to do it...\n\r",ch);
learn_from_failure( ch, gsn_research );
return;
case 1:
if ( !ch->dest_buf )
return;
strcpy(arg, ch->dest_buf);
DISPOSE( ch->dest_buf);
break;
case SUB_TIMER_DO_ABORT:
ch->substate = SUB_NONE;
send_to_char("&RYou fail to complete your research.\n\r", ch);
return;
}
ch->substate = SUB_NONE;
if ( number_percent( ) > chance )
{
send_to_char( "&RAfter much study you fail to learn anything about your skill.\n\r", ch);
learn_from_failure( ch, gsn_research );
return;
}
if ( number_percent( ) < chance )
{
if ( ch->pcdata->learned[sn] >= adept )
{
send_to_char("You've learned everything you can from the books,\n\r", ch);
send_to_char("you must practice it on your own now.\n\r", ch);
}
else
{
ch->pcdata->learned[sn] += 5;
act( AT_ACTION, "You gain some knowledge in the skill of $T.",
ch, NULL, skill_table[sn]->name, TO_CHAR );
}
}
}
return;
}
do you see the problem?
| | Top |
|
| Posted by
| David Haley
USA (3,881 posts) Bio
|
| Date
| Reply #11 on Sat 06 Mar 2004 09:48 AM (UTC) |
| Message
| Well, your indents are not quite right... but, well, anyways.
And for the problem, yes, as a matter of fact, I CAN see it... and Nick already pointed it out to you:
Quote:Either way, this brace here:
if ( !ch->dest_buf )
return;
strcpy(arg, ch->dest_buf);
DISPOSE( ch->dest_buf);
break; /*EVERYTHING WORKED RIGHT UP TO THIS POINT THEN THE CODE STOPPED*/
case SUB_TIMER_DO_ABORT:
ch->substate = SUB_NONE;
send_to_char("&RYou fail to complete your research.\n\r", ch);
return;
} // <--- this one
... is matched with one much further up under "default:". Thus the "case SUB_TIMER_DO_ABORT:" is an orphan, not really part of the switch.
Not to be discouraging or anything, but do you know how a switch statement works? |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | | Top |
|
| Posted by
| Jason
(109 posts) Bio
|
| Date
| Reply #12 on Sat 06 Mar 2004 09:55 AM (UTC) |
| Message
| | sorta..... but i am having problems with them still What would i have to do to fix it? | | Top |
|
| Posted by
| Jason
(109 posts) Bio
|
| Date
| Reply #13 on Sat 06 Mar 2004 09:57 AM (UTC) |
| Message
| | Oh... And i'm sure you can tell... I am a VERY new coder still i have much to learn.... so all your help is apprieciated | | Top |
|
| Posted by
| David Haley
USA (3,881 posts) Bio
|
| Date
| Reply #14 on Sat 06 Mar 2004 10:14 AM (UTC) |
| Message
| To be perfectly honest with you I think you should pick up a book on C or C++ programming, and just learn the language. It might sound pretty harsh, but trust me in the end you'll save yourself a lot of frustration and time. Really, I'm not trying to put you down, I'm just saying you should learn the language before trying to code in it, or you'll be met with only failure and disillusionment. Save yourself the misery and spend a few days learning C first.
A 30 second Google search reveals some nice starting points for free material...
http://www.gustavo.net/programming/c__tutorials.shtml
(Lots of links on that page)
http://www.physics.drexel.edu/courses/Comp_Phys/General/C_basics/c_tutorial.html
(seems to be a more comprehensive manual)
Take my word for it, I know from experience that it's much more enjoyable to do something when you actually know what you're doing. :) |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
57,157 views.
This is page 1, subject is 2 pages long: 1 2
It is now over 60 days since the last post. This thread is closed.
Refresh page
top