Advertisement

Another question about MUDs

Started by March 13, 2002 09:44 PM
3 comments, last by Vegadam 22 years, 9 months ago
Ok, here is one thing I dont understand how they do it. For every single command, like n, w, s, e, kick, punch ect ect.. How do They do all thoughs, is it just one big case, or an array? How do they arganize all of that stuff. Thank you.
I have seen a MUD source that used a large switch-case cascade to distinguish commands by the first character, then by the second character, and so on. However, I don''t think this is a good solution.

I would probably made up a sorted table or a hashtable of possible commands.

Have a nice day,



---
Never trust a Troglotroll.
---Never trust a Troglotroll.
Advertisement
Do you mean an Array?

Some MUDs use


  switch( input[0] ){  case ''a'':    if( !strcmp( input, "attack" ) ) do_attack()    else if ... // all commands in ''a''    break;  case ''b'':     // all commands in ''b''    break;  ...  default:    send_to_character( "huh ?" );}  


Others do


  typedef struct{  const char* word;  void (*function)();} command_t;command_t command_array[] ={   "attack", do_attack,   "attach", do_attach,   "break",  do_break,   ...   NULL, NULL};...while( command_array[index].word != NULL        && strcmp( input, command_array[index].word ) )          ++i;command_array[i].function;  
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
From the Circle 3.0 source:

  struct command_info {   const char *command;   byte minimum_position;   void	(*command_pointer)	   (struct char_data *ch, char *argument, int cmd, int subcmd);   sh_int minimum_level;   int	subcmd;};cpp_extern const struct command_info cmd_info[] = {  { "RESERVED", 0, 0, 0, 0 },	/* this must be first -- for specprocs */  /* directions must come before other commands but after RESERVED */  { "north"    , POS_STANDING, do_move     , 0, SCMD_NORTH },  { "east"     , POS_STANDING, do_move     , 0, SCMD_EAST },  { "south"    , POS_STANDING, do_move     , 0, SCMD_SOUTH },  { "west"     , POS_STANDING, do_move     , 0, SCMD_WEST },  { "up"       , POS_STANDING, do_move     , 0, SCMD_UP },  { "down"     , POS_STANDING, do_move     , 0, SCMD_DOWN },  
The array of structs above is ran through for every command, using strncmp to compare the two for a match.

    /* otherwise, find the command */  for (length = strlen(arg), cmd = 0; *cmd_info[cmd].command != '\n'; cmd++)    if (!strncmp(cmd_info[cmd].command, arg, length))      if (GET_LEVEL(ch) >= cmd_info[cmd].minimum_level)	break;  if (*cmd_info[cmd].command == '\n')    send_to_char("Huh?!?\r\n", ch);  
(interpreter.c, void command_interpreter(), line 600)
Mm... thanks Vegadam, for 'taking me back'... I was an imp on a Circle 2.2 94'-96'. Oh, good times...

[edited by - Tok on March 16, 2002 10:27:05 PM]
--------------------------~The Feature Creep of the Family~

This topic is closed to new replies.

Advertisement