Top Mud Sites Forum

Top Mud Sites Forum (http://www.topmudsites.com/forums/index.php)
-   MUD Coding (http://www.topmudsites.com/forums/forumdisplay.php?f=9)
-   -   Help With MUD Design In C (http://www.topmudsites.com/forums/showthread.php?t=7364)

Seratone 01-28-2015 02:01 PM

Help With MUD Design In C
 
Hi, I'm a beginner coder using C. I've managed to build a single player dungeon type game that I believe could be modified into a MUD at some point, but I'd like to see if I can organize things a little better.

Here is the basic lay out/structure for the game.


int e=1;
char exit1s[100];

while (e=1) //Each room operates on a while loop//

Room5:

{printf("\n\n ROOM DESCRIPTION EXIT S \n\n");


fgets (exit1s, 100, stdin);//Waits for command from player//

exit1s[strlen(exit1s)-1] = '\0';

if (strcasecmp(exit1s, "s") == 0)//If else statements allow for multiple commands for each room//

{printf("You start walking");
Sleep(500);
goto Room4;//Goto command is used to move between rooms.//

break;}


So here is my main question. Stacking up if else statements for each room is tedious. I'd like to add a "global command" that could work for every room. Any suggestions?

plamzi 01-28-2015 02:59 PM

Re: Help With MUD Design In C
 
Hi there,

A beginner in C should probably start by looking at existing code, and it should probably be something a lot simpler than a MUD server.

There's probably an ABC of game design in C somewhere, whose very first example will show you the classic main loop. There's a reason it is a classic.

There's a very limited number of use cases in C where "goto" does more good than harm. This is not one of them.

Putting closures on their own line is best practice. It improves readability even more than not using "goto".

If you are designing anything more than a 20-line experiment, you should not be hardcoding stuff like room links. "goto Room4;" is an example of hardcoding. You are already sensing that this is wrong when you say that "Stacking up if else statements for each room is tedious." What you need is an easily extendable structure for all rooms and their properties. Movement across any two rooms would then be handled by the same code.

Using "sleep" to introduce a delay is only OK for brief experiments. In normal games, you would have a way to move on to other things and return later when you want to show the user some output. In a main-loop kind of design, this usually involves creating pulse timers (something that runs each second e. g.).

There will be one place in your main loop where you consider user input. This is the place where you will be checking that input against a "global command" list. Because commands are naturally expected to be "global" and only sometimes expected to be location-specific, you should design with the latter as the exception, and the former as the rule.

dentin 01-29-2015 10:07 AM

Re: Help With MUD Design In C
 
Looking at other mud code is probably a bad idea. You don't really know enough C to make sense of it yet. This is a perfectly fine project to be learning how to code on.

To quote myself from 20 years ago when I was starting Alter Aeon in a similar fashion (minus goto and sleep()), "this sounds like a job for a structure!" Look up structures as a way to store multiple pieces of data together, then store your room information in an array of room structures with numbers representing how they connect to each other. That should get you one step farther, but there's still a long way to go.

Good luck!

Alter Aeon MUD

Seratone 01-29-2015 02:13 PM

Re: Help With MUD Design In C
 
Thanks for the feedback. So far I've managed to set up room navigation and a few other things on one main loop. I guess I can kiss those goto statements good bye!

It looks like structures will be the next big thing to learn.

As an aside, if anyone is interested, I'd be happy to message or post the source code for the game I designed.

Even if the coding isn't exactly elegant, its actually a pretty fun game to play!

Seshin 03-25-2015 12:26 AM

Re: Help With MUD Design In C
 
You've got your mind in the right place, but goto blocks are not recommended at all. Ever.

The first game I wrote was an interactive fiction game in a Borland C compiler on a very very old Pentium. I remember several methods at the time, the most popular one being something like this:

typedef struct {
int roomId;
char *shortDescription;
char *longDescription;
} room_t;

Then I created a bunch of room_t's based on the contents of some text files, each of which were named according to where they were on a 2d map. For example: imagine a 2d array of int variables, and then a folder of text files called x_y.txt.

I hope that helps with some of this.

Seshin 03-25-2015 12:28 AM

Re: Help With MUD Design In C
 
With respect to looking at how other people did it, I remember when I finally got a copy of John Carmack's code for Wolfenstein thinking to myself: "Wow, I don't know anything!" That definitely helped me to learn where my weak points were!


All times are GMT -4. The time now is 06:03 AM.

Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright Top Mud Sites.com 2022