Getting Input from a command

 

Getting input from a command is very easy, since a string pointer is passed through all do_functions.  But let’s go back to that command we just made, do_showstats, at the beging add the lines

 

char arg[MAX_INPUT_LENGTH];

 

argument = one_argument(argument, arg);

 

If you say that last line out loud you may sound like a pirate to those around (stupid joke).  Now you should know what the first line is, MAX_INPUT_LENGTH is in mud.h.  but the second line, is similar to ones you’ll see all throughout the code.  What it does is it takes the string, argument, takes out the next word, puts that word into the arg pointer, and returns a pointer to the next word in the argument function.  For example (cus I know I rambled there a bit)

 

argument = “this is fun”, you pass this through that function and this is what you get in return.

 

argument = “is fun”

arg = “this”

 

one_argument will also parse anything inside quotes as a word and ignore leading white space  so if

argument = “    ‘this is fun’ yeah!”

 

after you pass it through one_argument

arg = “this is fun”

argument = “yeah!”

 

Now add the following code after the one_argument call

 

if(!str_cmp(arg, “str”) {

            send_to_char(“You only asked for strength.\n\r”, ch);

            ch_printf(ch, “Str: %d\n\r”, get_curr_str(ch));

            return;

}

 

The str_cmp function, is similar to the ANSI C function strcmp, except str_cmp ignores case, so “This” == “tHIS”.

The send_to_char function, takes a string pointer, and a CHAR_DATA pointer as parameters, and will show the character the string.  Recompile and reboot.  Now if you type showstats str, it will show this:

 

You only asked for strength.

Str: 18

 

If you noticed, the one_argument function, isn’t really necessary here, since the original argument pointer already points to the first word.  But I thought I’d add it in for practice.

 

Now your command looks like almost every other command in the SMAUG code, though somewhat smaller.

 

Say you want to make it so that only players (not mobs) halfway to avatar or under and immortals, to be able to use this command.  You would add this code, to preferable just after the declartion of variables.

 

if(IS_NPC(ch)) {

            send_to_char(“Mobs may not use this command.\n\r”, ch);

            return;

}

 

if(get_trust(ch) > LEVEL_AVATAR / 2 && !IS_IMMORTAL(ch)) {

            send_to_char(“Cannot be more than halfway to avatar to use this command.\n\r”, ch);

            return;

}

 

You are probably thinking these are some very stupid restrictions to a piece of code.  And you’re right, but that’s not the point.  The first check should be obvious once you look at it.  It checks if the character is an NPC, and returns TRUE if it is.  This can be very important in some commands.  if you have a command that accesses pcdata, like channels for instance, You must return before that data is accessed, otherwise the mud will crash, because mobs don’t have pcdata.  Because of the trust attribute that a person can have, I usually use it instead of ch->level.  Although since mortals probably don’t have a trust, You could put ch->level instead of get-Trust.  But get_trust returns ch->level if there is no trust.  The IS_IMMORTAL check should, like IS_NPC, be obvious, it returns TRUE if the player is an immortal.  So basically if you typed in the command on a stock mud you would have to meet the following requirements:

 

Be level 25 or less, or be level 101 or more And you would have to be a character, so if you ordered a MOB to do it, it wouldn’t work.