[Home] [Downloads] [Search] [Help/forum]

Gammon Software Solutions forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  SMAUG
. -> [Folder]  SMAUG coding
. . -> [Subject]  Problems with code i created
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Problems with code i created

It is now over 60 days since the last post. This thread is closed.   [New subject]  Start a new subject   [Refresh] Refresh page


Pages: 1 2  

Posted by Jason   (109 posts)  [Biography] bio
Date Fri 05 Mar 2004 10:08 AM (UTC)  quote  ]
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;
}
[Go to top] top

Posted by Jason   (109 posts)  [Biography] bio
Date Reply #1 on Fri 05 Mar 2004 10:10 AM (UTC)  quote  ]
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
[Go to top] top

Posted by Meerclar   USA  (600 posts)  [Biography] bio
Date Reply #2 on Fri 05 Mar 2004 10:37 AM (UTC)  quote  ]
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
[Go to top] top

Posted by Nick Gammon   Australia  (19,613 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Fri 05 Mar 2004 08:52 PM (UTC)  quote  ]
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
[Go to top] top

Posted by Jason   (109 posts)  [Biography] bio
Date Reply #4 on Sat 06 Mar 2004 01:16 AM (UTC)  quote  ]
Message
well The SUB_TIMER_DO_ABORT does work. everything after that won't
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio   Moderator
Date Reply #5 on Sat 06 Mar 2004 02:08 AM (UTC)  quote  ]
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
[Go to top] top

Posted by Jason   (109 posts)  [Biography] bio
Date Reply #6 on Sat 06 Mar 2004 02:13 AM (UTC)  quote  ]
Message
Well might i go about fixing it... I'm not good with timers
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio   Moderator
Date Reply #7 on Sat 06 Mar 2004 03:55 AM (UTC)  quote  ]
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
[Go to top] top

Posted by Jason   (109 posts)  [Biography] bio
Date Reply #8 on Sat 06 Mar 2004 05:02 AM (UTC)  quote  ]
Message
ok well where did i mess up? I can't find it
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio   Moderator
Date Reply #9 on Sat 06 Mar 2004 06:27 AM (UTC)  quote  ]
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
[Go to top] top

Posted by Jason   (109 posts)  [Biography] bio
Date Reply #10 on Sat 06 Mar 2004 08:30 AM (UTC)  quote  ]
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?
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio   Moderator
Date Reply #11 on Sat 06 Mar 2004 09:48 AM (UTC)  quote  ]
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
[Go to top] top

Posted by Jason   (109 posts)  [Biography] bio
Date Reply #12 on Sat 06 Mar 2004 09:55 AM (UTC)  quote  ]
Message
sorta..... but i am having problems with them still What would i have to do to fix it?
[Go to top] top

Posted by Jason   (109 posts)  [Biography] bio
Date Reply #13 on Sat 06 Mar 2004 09:57 AM (UTC)  quote  ]
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
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio   Moderator
Date Reply #14 on Sat 06 Mar 2004 10:14 AM (UTC)  quote  ]
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
[Go to top] 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.


5,607 views.

This is page 1, subject is 2 pages long: 1 2  [Next page]

It is now over 60 days since the last post. This thread is closed.   [New subject]  Start a new subject   [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

[Home]

Written by Nick Gammon - 5K

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( http://www.gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Web site powered by FutureQuest.Net]