msg00357.html

1999Q1/msg00357.html · 9.0 KB

From the archive 1999Q1.zip

Download the archive   « Back to the archive contents

<!-- MHonArc v2.4.4 -->
<!--X-Subject: [MUD&#45;Dev] Re: Question on c++ switch optimization, and parsers in general. -->
<!--X-From-R13: Oqnz Ivttvaf <nqnzNnatry.pbz> -->
<!--X-Date: Mon, 8 Feb 1999 11:47:57 &#45;0800 -->
<!--X-Message-Id: Pine.SGI.3.96.990208111536.930G&#45;100000#zazu,angel.com -->
<!--X-Content-Type: text/plain -->
<!--X-Reference: 36BE7019.241FB974#cyberhighway,net -->
<!--X-Head-End-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title>MUD-Dev message, [MUD-Dev] Re: Question on c++ switch optimization, and parsers</title>
<!-- meta name="robots" content="noindex,nofollow" -->
<link rev="made" href="mailto:adam#angel,com">
</head>
<body background="/backgrounds/paperback.gif" bgcolor="#ffffff"
      text="#000000" link="#0000FF" alink="#FF0000" vlink="#006000">

  <font size="+4" color="#804040">
    <strong><em>MUD-Dev<br>mailing list archive</em></strong>
  </font>
      
<br>
[&nbsp;<a href="../">Other Periods</a>
&nbsp;|&nbsp;<a href="../../">Other mailing lists</a>
&nbsp;|&nbsp;<a href="/search.php3">Search</a>
&nbsp;]
<br clear=all><hr>
<!--X-Body-Begin-->
<!--X-User-Header-->
<!--X-User-Header-End-->
<!--X-TopPNI-->

Date:&nbsp;
[&nbsp;<a href="msg00356.html">Previous</a>
&nbsp;|&nbsp;<a href="msg00358.html">Next</a>
&nbsp;]
&nbsp;&nbsp;&nbsp;&nbsp;
Thread:&nbsp;
[&nbsp;<a href="msg00365.html">Previous</a>
&nbsp;|&nbsp;<a href="msg00358.html">Next</a>
&nbsp;]
&nbsp;&nbsp;&nbsp;&nbsp;
Index:&nbsp;
[&nbsp;<A HREF="author.html#00357">Author</A>
&nbsp;|&nbsp;<A HREF="#00357">Date</A>
&nbsp;|&nbsp;<A HREF="thread.html#00357">Thread</A>
&nbsp;]

<!--X-TopPNI-End-->
<!--X-MsgBody-->
<!--X-Subject-Header-Begin-->
<H1>[MUD-Dev] Re: Question on c++ switch optimization, and parsers in general.</H1>
<HR>
<!--X-Subject-Header-End-->
<!--X-Head-of-Message-->
<UL>
<LI><em>To</em>: <A HREF="mailto:mud-dev#kanga,nu">mud-dev#kanga,nu</A></LI>
<LI><em>Subject</em>: [MUD-Dev] Re: Question on c++ switch optimization, and parsers in general.</LI>
<LI><em>From</em>: Adam Wiggins &lt;<A HREF="mailto:adam#angel,com">adam#angel,com</A>&gt;</LI>
<LI><em>Date</em>: Mon, 8 Feb 1999 11:47:31 -0800 (PST)</LI>
<LI><em>Reply-To</em>: <A HREF="mailto:mud-dev#kanga,nu">mud-dev#kanga,nu</A></LI>
</UL>
<!--X-Head-of-Message-End-->
<!--X-Head-Body-Sep-Begin-->
<HR>
<!--X-Head-Body-Sep-End-->
<!--X-Body-of-Message-->
<PRE>
On Sun, 7 Feb 1999, Ben Greear wrote:
&gt; Basically, I'll have a bunch of classes hashed into an array
&gt; that will contain the keywords mapped to an enum.
&gt; Now, I get the enum, and then I need to call the various commands
&gt; that the enum maps to.
&gt; Currently, I'm using a huge (~400 cases) switch statement.

I've always been fond of the simplicity of the "big array with everything
you need to know about a given command" method.  Apparently some folks in
the diku world are as well: I know that stock CircleMUD handles commands
this way.

Basically, you make something like:

typedef void (*CommandMethod)(Character *, char *);

struct CommandInfo
{
   char *Name;
   int MinimumLevel;
   int SkillNeeded;
   int LimbsNeeded;
   CommandMethod Method;
};

Then you just fill out your array as you like:

CommandMethod Smile, Track, Purge /* ... */;

CommandInfo CommandList[] = 
{
  { "smile", LEVEL_MIN, SKILL_NOTHING, SPEACH, Smile },
  { "track", LEVEL_MIN, SKILL_TRACK,   EYES,   Track },
  { "purge", LEVEL_GOD, SKILL_NOTHING, 0,      Purge },

  { NULL, 0, 0, 0, NULL }
};


And your command parsing code looks like:

bool ParseCommand(Character *c, char *arg)
{
  int i;
  for (i = 0; CommandList[i].Name != NULL; i++)
    if (strcasecmp(CommandList[i].Name, arg) == 0 &amp;&amp;
        c-&gt;Level &gt;= CommandList[i].MinimumLevel &amp;&amp;
        c-&gt;Skill[CommandList[i].SkillNeeded] &gt; 0)
      break;

  if (CommandList[i].Name == NULL)
  {
    InformCharacter(c, "No such command.");
    return false;
  }

  if (CommandList[i].LimbsNeeded)
  {
    if (CommandList[i].LimbsNeeded &amp; SPEACH &amp;&amp; !CanSpeak(c))
    {
      InformCharacter(c, "You can't speak!");
      return false;
    }
    if (CommandList[i].LimbsNeeded &amp; EYES &amp;&amp; !CanSee(c))
    {
      InformCharacter(c, "You can't see a thing!");
      return false;
    }
    if (CommandList[i].LimbsNeeded &amp; HAND &amp;&amp; NumHands(c) &lt; 1)
    {
      InformCharacter(c, "You don't have any hands!");
      return false;
    }
    /* ... */
  }

  CommandList[i].Method(c, arg);
  return true;
} 

Finally, you need to define the methods Smile, Track, Purge, and
whatever else commands you want in whatever module you want.

This was just off the top of my head, btw, so code errors are quite
possible.  Check out the circle mud sources for a working version
of this sort of thing.

Adam W.




</PRE>

<!--X-Body-of-Message-End-->
<!--X-MsgBody-End-->
<!--X-Follow-Ups-->
<HR>
<ul compact><li><strong>Follow-Ups</strong>:
<ul>
<li><strong><A NAME="00359" HREF="msg00359.html">[MUD-Dev] Re: Question on c++ switch optimization, and parsers in general.</A></strong>
<ul compact><li><em>From:</em> Oliver Jowett &lt;icecube#ihug,co.nz&gt;</li></ul>
<li><strong><A NAME="00358" HREF="msg00358.html">[MUD-Dev] Re: Question on c++ switch optimization, and parsers in general.</A></strong>
<ul compact><li><em>From:</em> Mark Gritter &lt;mark#erdos,Stanford.EDU&gt;</li></ul>
</UL></LI></UL>
<!--X-Follow-Ups-End-->
<!--X-References-->
<UL><LI><STRONG>References</STRONG>:
<UL>
<LI><STRONG><A NAME="00353" HREF="msg00353.html">[MUD-Dev] Question on c++ switch optimization, and parsers in general.</A></STRONG>
<UL><LI><EM>From:</EM> Ben Greear &lt;greear#cyberhighway,net&gt;</LI></UL></LI>
</UL></LI></UL>
<!--X-References-End-->
<!--X-BotPNI-->
<UL>
<LI>Prev by Date:
<STRONG><A HREF="msg00356.html">[MUD-Dev] Re: Question on c++ switch optimization, and parsers in general.</A></STRONG>
</LI>
<LI>Next by Date:
<STRONG><A HREF="msg00358.html">[MUD-Dev] Re: Question on c++ switch optimization, and parsers in general.</A></STRONG>
</LI>
<LI>Prev by thread:
<STRONG><A HREF="msg00365.html">[MUD-Dev] Re: Question on c++ switch optimization, and parsers in  general.</A></STRONG>
</LI>
<LI>Next by thread:
<STRONG><A HREF="msg00358.html">[MUD-Dev] Re: Question on c++ switch optimization, and parsers in general.</A></STRONG>
</LI>
<LI>Index(es):
<UL>
<LI><A HREF="index.html#00357"><STRONG>Date</STRONG></A></LI>
<LI><A HREF="thread.html#00357"><STRONG>Thread</STRONG></A></LI>
</UL>
</LI>
</UL>

<!--X-BotPNI-End-->
<!--X-User-Footer-->
<!--X-User-Footer-End-->
<ul><li>Thread context:
<BLOCKQUOTE><UL>
<LI><STRONG>[MUD-Dev] Re: Question on c++ switch optimization, and parsers i</STRONG>, <EM>(continued)</EM>
<ul compact>
<LI><strong><A NAME="00354" HREF="msg00354.html">[MUD-Dev] Re: Question on c++ switch optimization, and parsers i</A></strong>, 
Jon A. Lambert <a href="mailto:jlsysinc#ix,netcom.com">jlsysinc#ix,netcom.com</a>, Mon 08 Feb 1999, 05:56 GMT
<UL>
<LI><strong><A NAME="00364" HREF="msg00364.html">[MUD-Dev] Re: Question on c++ switch optimization, and parsers i</A></strong>, 
Ben Greear <a href="mailto:greear#cyberhighway,net">greear#cyberhighway,net</a>, Tue 09 Feb 1999, 02:46 GMT
</LI>
</UL>
</LI>
<LI><strong><A NAME="00356" HREF="msg00356.html">[MUD-Dev] Re: Question on c++ switch optimization, and parsers in general.</A></strong>, 
Ola Fosheim Grøstad <a href="mailto:olag#ifi,uio.no">olag#ifi,uio.no</a>, Mon 08 Feb 1999, 16:53 GMT
<UL>
<LI><strong><A NAME="00365" HREF="msg00365.html">[MUD-Dev] Re: Question on c++ switch optimization, and parsers in  general.</A></strong>, 
Ben Greear <a href="mailto:greear#cyberhighway,net">greear#cyberhighway,net</a>, Tue 09 Feb 1999, 02:54 GMT
</LI>
</UL>
</LI>
<LI><strong><A NAME="00357" HREF="msg00357.html">[MUD-Dev] Re: Question on c++ switch optimization, and parsers in general.</A></strong>, 
Adam Wiggins <a href="mailto:adam#angel,com">adam#angel,com</a>, Mon 08 Feb 1999, 19:47 GMT
<UL>
<LI><strong><A NAME="00358" HREF="msg00358.html">[MUD-Dev] Re: Question on c++ switch optimization, and parsers in general.</A></strong>, 
Mark Gritter <a href="mailto:mark#erdos,Stanford.EDU">mark#erdos,Stanford.EDU</a>, Mon 08 Feb 1999, 21:18 GMT
</LI>
<LI><strong><A NAME="00359" HREF="msg00359.html">[MUD-Dev] Re: Question on c++ switch optimization, and parsers in general.</A></strong>, 
Oliver Jowett <a href="mailto:icecube#ihug,co.nz">icecube#ihug,co.nz</a>, Mon 08 Feb 1999, 21:23 GMT
<UL>
<LI><strong><A NAME="00360" HREF="msg00360.html">[MUD-Dev] Re: Question on c++ switch optimization, and parsers in general.</A></strong>, 
Oliver Jowett <a href="mailto:icecube#ihug,co.nz">icecube#ihug,co.nz</a>, Mon 08 Feb 1999, 21:30 GMT
</LI>
</UL>
</LI>
</UL>
</LI>
<LI><strong><A NAME="00361" HREF="msg00361.html">[MUD-Dev] Re: Question on c++ switch optimization, and parsers in general.</A></strong>, 
Ola Fosheim Grøstad <a href="mailto:olag#ifi,uio.no">olag#ifi,uio.no</a>, Tue 09 Feb 1999, 00:16 GMT
</LI>
</ul>
</LI>
</UL></BLOCKQUOTE>

</ul>
<hr>
<center>
[&nbsp;<a href="../">Other Periods</a>
&nbsp;|&nbsp;<a href="../../">Other mailing lists</a>
&nbsp;|&nbsp;<a href="/search.php3">Search</a>
&nbsp;]
</center>
<hr>
</body>
</html>

Site Stats

Snippets: 1791
Codebases: 571