Non messy states

Started by
1 comment, last by Nathan Baum 18 years, 5 months ago
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.
--------------------------I present for tribute this haiku:Inane Ravings OfThe Haunting JubilationA Mad Engineer©Copyright 2005 ExtrariusAll Rights Reserved
Advertisement
Whoa, lots of spelling errors. I would fix them, but the forum will not let me. It gives me an error.
--------------------------I present for tribute this haiku:Inane Ravings OfThe Haunting JubilationA Mad Engineer©Copyright 2005 ExtrariusAll Rights Reserved
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.

This topic is closed to new replies.

Advertisement