char *direction_to_xwards (int direction)
{
static char buf[12];
if (direction == 0)
sprintf (buf, "Northwards: ");
else if (direction == 1)
sprintf (buf, "Eastwards: ");
else if (direction == 2)
sprintf (buf, "Southwards: ");
else if (direction == 3)
sprintf (buf, "Westwards: ");
else if (direction == 4)
sprintf (buf, "Upwards: ");
else
sprintf (buf, "Downwards: ");
return buf;
}
char * direction_to_xwards args( ( int direction ));
if (!str_cmp (arg1, "n") || !str_cmp (arg1, "north"))
static char buf[12];
byte 0 | 1 | 2 | 3 | 4 | 5
h | e | l | l | o | \0
char *direction_to_xwards( int direction )
{
static char buf[128];
switch( direction )
{
default: sprintf(buf,"Error, received invalid value: %d.",direction); break;
case 0: sprintf(buf,"Northwards: "); break;
case 1: sprintf(buf,"Eastwards: "); break;
case 2: sprintf(buf,"Southwards: "); break;
case 3: sprintf(buf,"Westwards: "); break;
case 4: sprintf(buf,"Upwards: "); break;
case 5: sprintf(buf,"Downwards: "); break;
}
return buf;
}
const char* foo(int input)
{
if (input < 5) return "hello";
else return "goodbye";
}
/* merh.h */
int str_to_direction args( ( const char *arg1) );
/* act_info.c */
int str_to_direction(const char *arg1)
{
if (!str_prefix (arg1, "north"))
return 0;
if (!str_prefix (arg1, "east"))
return 1;
if (!str_prefix (arg1, "south"))
return 2;
if (!str_prefix (arg1, "west"))
return 3;
if (!str_prefix (arg1, "up"))
return 4;
if (!str_prefix (arg1, "down"))
return 5;
else
return -1
}
/* merh.h */
const char* direction_to_xwards args( ( int direction ));
/* act_info.c */
const char* direction_to_xwards (int direction)
{
if (direction == 0) return "Northwards: ";
else if (direction == 1) return "Eastwards: ";
else if (direction == 2) return "Southwards: ";
else if (direction == 3) return "Westwards: ";
else if (direction == 4) return "Upwards: ";
else if (direction == 5) return "Downwards: ");
else
bug ("direction_to_xwards direction not found.", 0);
return;
}
#define DIR_NORTH 0
#define DIR_EAST 1
#define DIR_SOUTH 2
…
return "";
return NULL;
#define NUM_DIR 6 /* this should be in merc.h and really be used instead of 6 in all the code */
int get_dir(const char* txt){
int ct;
for(ct = 0; ct < NUM_DIR; ++ct)
if(!str_prefix(txt, dir_name[ct])
return ct;
return -1;
}
int get_dir(const char* txt);
/* merh.h */
int str_to_direction args( ( const char *arg1) );
/* act_info.c */
int str_to_direction(const char *arg1)
{
if (!str_prefix (arg1, "north")) return DIR_NORTH;
if (!str_prefix (arg1, "east")) return DIR_EAST;
if (!str_prefix (arg1, "south")) return DIR_SOUTH;
if (!str_prefix (arg1, "west")) return DIR_WEST;
if (!str_prefix (arg1, "up")) return DIR_UP;
if (!str_prefix (arg1, "down")) return DIR_DOWN;
else
return -1;
}
/* merh.h */
const char* direction_to_xwards args( ( int direction ) );
/* act_info.c */
const char* direction_to_xwards (int direction)
{
if (direction == DIR_NORTH) return "Northwards: ";
else if (direction == DIR_EAST) return "Eastwards: ";
else if (direction == DIR_SOUTH) return "Southwards: ";
else if (direction == DIR_WEST) return "Westwards: ";
else if (direction == DIR_UP) return "Upwards: ";
else if (direction == DIR_DOWN) return "Downwards: ";
else
bug ("direction_to_xwards direction not found.", 0);
return "ERROR";
}
/* act_move.c */
char *const dir_name[] = {
"north", "east", "south", "west", "up", "down"
};
/* merc.h */
int to_direction(const char* direction);
/* act_move.c */
int to_direction(const char* direction)
{
int td;
for(td = 0; td < DIR_MAX; ++td)
{
if(!str_prefix(direction, dir_name[td])
return td;
}
return -1;
}
/* merh.h */
int str_to_direction args( ( const char *arg1) );
/* act_info.c */
int str_to_direction(const char *arg1)
{
if (!str_prefix (arg1, "north")) return DIR_NORTH;
if (!str_prefix (arg1, "east")) return DIR_EAST;
if (!str_prefix (arg1, "south")) return DIR_SOUTH;
if (!str_prefix (arg1, "west")) return DIR_WEST;
if (!str_prefix (arg1, "up")) return DIR_UP;
if (!str_prefix (arg1, "down")) return DIR_DOWN;
else
return -1;
}
/* act_move.c */
int to_direction(const char* direction) {
register char i = 0;
while(i < DIR_MAX )
if (*direction == *dir_name[i++])
return i;
return -1;
}
/* act_move.c */
int to_direction(const char* direction) {
register char i = 0;
while(i < DIR_MAX )
if (*direction == *dir_name[i++])
return i;
return -1;
}
/* act_move.c */
int to_direction(const char* direction){
register char d = *direction;
register char i = -1;
while(i < DIR_DOWN)
if(d == *dir_name[++i])
return i;
return -1;
}
#define DIR_MAX 11
char const* dir_name[DIR_MAX] =
{
"north", "down", "east", NULL, NULL,
"south", NULL, "up", NULL, "west", NULL
};
int to_direction( char const* direction )
{
int i = direction[0] % DIR_MAX;
return (!dir_name[i] || strcmp(dir_name[i], direction)) ? -1 : i;
}
Start the sequence with if(), not else if().
db.c is primarily concerned with booting the mud from the files, if they wrote big words back the it would be called database.c. There are also miscellaneous functions near the end, but it would be saner to move them out rather than add more. You might put it in act_info, as the act files mostly contain player commands. Or, you could start your own file where you keep the stuff you've written yourself.
And yes, put it in merc.h. Note that RT wasn't as dogmatic about it, so there are things not included in that section. However, while it's true not everything needs to be in a header file, it sure is nice to have a big list of everything that's available for you to use.
David can be very helpful, but he's a firm believer in the "If you give a man a fish…" adage. You did a wonderful job of planning the code, and now you're well prepared to plan your next project. Keep taking his advice, and pretty soon you'll be able to do it all yourself.
(I know I wrote some code for you in another thread, but in that case people were going off in the wrong direction and you were getting confused. I finally decided writing something that did what you wanted was the only way to make my point.)