Top Mud Sites Forum Return to TopMudSites.com
Go Back   Top Mud Sites Forum > Mud Development and Administration > MUD Coding
Click here to Register

Reply
 
Thread Tools
Old 01-28-2015, 02:01 PM   #1
Seratone
New Member
 
Join Date: Jan 2015
Posts: 2
Seratone is on a distinguished road
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?
Seratone is offline   Reply With Quote
Old 01-28-2015, 02:59 PM   #2
plamzi
Senior Member
 
plamzi's Avatar
 
Join Date: Nov 2009
Home MUD: bedlam.mudportal.com:9000
Home MUD: www.mudportal.com
Posts: 292
plamzi is on a distinguished road
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.

Last edited by plamzi : 01-28-2015 at 03:15 PM.
plamzi is offline   Reply With Quote
Old 01-29-2015, 10:07 AM   #3
dentin
Member
 
Join Date: Apr 2008
Home MUD: Alter Aeon
Posts: 245
dentin is on a distinguished road
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
http://www.alteraeon.com
dentin is offline   Reply With Quote
Old 01-29-2015, 02:13 PM   #4
Seratone
New Member
 
Join Date: Jan 2015
Posts: 2
Seratone is on a distinguished road
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!
Seratone is offline   Reply With Quote
Old 03-25-2015, 12:26 AM   #5
Seshin
New Member
 
Join Date: Mar 2015
Posts: 2
Seshin is on a distinguished road
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 is offline   Reply With Quote
Old 03-25-2015, 12:28 AM   #6
Seshin
New Member
 
Join Date: Mar 2015
Posts: 2
Seshin is on a distinguished road
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!
Seshin is offline   Reply With Quote
Reply


Thread Tools


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off

All times are GMT -4. The time now is 03:10 PM.


Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Style based on a design by Essilor
Copyright Top Mud Sites.com 2014