Sign in to follow this  
Witchcraven

Non messy states

Recommended Posts

Witchcraven    564
So I wrote a mud, and it uses lua scripting. I want this mud to be lua programmable by non programmers, and I believe lua is simple enough to acheive that. The mud is event based, and calls certain lua functions when certain things happen, like inputEvent. Muds are basically state ased, and keeping track of them seems a bit messy. You wind up with crap like: if state == 0 //do login // more ifs if state == 1 //do account creation etc.. Basically, it looks like a nasty mess of ifs and conditions and state routing around. I want to make it cleaner and less intimidating to non programmers. One of my ideas was the mud would still track states and substates, but it would call functions instead of using ifs or switching. For example: state = 1 substate = 5 doStates() --this would call a function called state1_5() I am sure there are better ways for state managment as well, but the this style seems well suited to muds. However I am very very open to other options.

Share this post


Link to post
Share on other sites
Nathan Baum    1027
I'd use classes, if the language the mud is written in supports them.

For the simple example you gave, in C++:

class Session
{
SessionManager * sessionManager
Player * player;
public:
Session (Socket * socket)
: player(player)
{
sessionManager = new Greeter (this);
}
};

class SessionManager
{
public:
virtual void HandleInputChar (char c)
{
// ... build up a line and pass it to HandleInputLine ... // ;
}
virtual void HandleInputLine (std::string);
};

class Greeter: public SessionManager
{
Session * session;
public:
virtual Greeter (Session * session)
: session(session), player(0)
{
session->SendText("Welcome to blah blah..., what's your name, like?");
}
virtual void HandleInputLine (std::string line)
{
if (player)
{
if (player->CheckPassword(line))
{
session->SendText("Welcome back!");
session->sessionManager = new MudManager;
delete this;
}
else
{
session->SendText("Password invalid. Try again. Password: ");
}
}
else
{
if (session->player = Player::FindByName(line))
{
session->SendText("Password: ");
}
else
{
session->SendText("New player, eh?");
session->sessionManager = new Registrator;
delete this;
}
}
}
};

class MudManager: public SessionManager
{
public:
virtual void HandleInputLine (std::string)
{
...;
}
};

class Registrator: public SessionManager
{
public:
virtual void HandleInputLine (std::string)
{
...;
}
};



Using this, or something similar but better designed, you can add new kinds of session interaction without needing to do anything particularly invasive in the source, or even as a Lua script.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this