[MUD-Dev] i got's a question for yall on the best way to do something..
Jim Clark
somewhere at sprint.ca
Thu Jun 17 14:43:29 CEST 1999
You still want a state machine, but that horrible 1000 line switch is not
a good way to implement a state machine. The first step to make a decent
deterministic state machine would be to write down a list of states:
connect -> login -> new char -> new char passwd -> ect..
|
-----> old char -> get old char password -> playing
|
--> bad password
After you have a decent map of state transitions, you can get down
to the implementation. (It's been a few years since I did any large
state machines so bear with me)
First create a nice list of constants, one for each state.
const int STATE_CONNECT = 0;
const int STATE_LOGIN = 1;
const int STATE_OLD_CHAR = 2;
const int STATE_NEW_CHAR = 3;
ect..
const int MAX_STATE = 20; // Or whatever
Then make a large table which maps old state vs. next state.
With function pointers
const STATEHANDER STATE_TABLE[MAX_STATE][MAX_STATE] =
{
// Next State (the one we are moving to)
// Connect Login Old Char NewChar
/* Connect */ { NULL, Login, NULL, NULL },
/* Login */ { NULL, NULL, OldChar, NewChar },
/* Old Char */ { NULL, NULL, NULL, NULL },
/* New Char */ { NULL, NULL, NULL, NULL }
};
This table defines the links between states, as well as where to go next.
Some function such as this will handle that.
void HandleState( Ch* pCh, const String& strIn )
{
// I can't rember if this is the right member function
// syntax, and I'm to lazy to dig it up
// And you should of course check for NULL's
pCh->*( STATE_TABLE[pCh->m_nLastState][pCh->m_nCurrState] )( strIn );
}
And lastly an example of a STATEHANDLER, in psuado code
void Ch::Login( const String& strIn )
{
if( strIn IS AN old player )
m_nLastState = m_nCurrState;
m_nCurrState = STATE_OLD_CHAR;
Out( "some message about entering a password" );
}
else
{
m_nLastState = m_nCurrState;
m_nCurrState = STATE_NEW_CHAR;
Out( "some message about new characters and some instructions about
entering a new password" );
}
}
-Jim
----- Original Message -----
From: <PartyG2816 at aol.com>
To: <mud-dev at kanga.nu>
Sent: Thursday, June 17, 1999 2:55 AM
Subject: [MUD-Dev] i got's a question for yall on the best way to do
something..
> Ok, any of yall familiar with the ROM code base? Tis a text based mud.
Well,
> they handle character creation via this big, ugly, confusing state
machine..
> Hard as hell to find stuff in, and just a bitch to look at. Well, I've
> finally got around to starting my own mud in C++, tired of modifying ROM's
> and Oblivions yadda yadda, and gotten to the point where I need to figure
out
> a way to handle this.. The only way I know of doing it is via a big ugly
> state machine like ROM and most of the other's that I've seen use.. Mainly
> the Diku derivatives. What are some other ways a person might go about
doing
> it? Any input would be greatly appreciated.. And if you need more info on
> what I'm talking about, just ask and I'll try to supply ;-)
>
> Thanx,
> JD
_______________________________________________
MUD-Dev maillist - MUD-Dev at kanga.nu
http://www.kanga.nu/lists/listinfo/mud-dev
More information about the mud-dev-archive
mailing list