Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUDs ➜ General ➜ Tiny MUD Server - version 2

Tiny MUD Server - version 2

Posting of new messages is disabled at present.

Refresh page


Pages: 1  2  3  4  5  6  7 8  

Posted by Nick Gammon   Australia  (23,122 posts)  Bio   Forum Administrator
Date Reply #90 on Wed 19 Aug 2009 09:10 PM (UTC)
Message
I found it somewhat hard to get the answer quickly, so I sympathize. :)

I couldn't find it in the manual, even after installing stl-manual.

Eventually a Google for "transform" gave the answer:

http://www.sgi.com/tech/stl/transform.html

Then I hit:


player.cpp: In member function ‘void tPlayer::Load()’:
player.cpp:82: error: ‘numeric_limits’ was not declared in this scope
player.cpp:82: error: expected primary-expression before ‘int’


I had to:


#include <limits>


into a couple of files.

Oh well, this is what happens with old code. The header files used to cross-reference each other, and someone tightened it up.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Hex   (1 post)  Bio
Date Reply #91 on Mon 16 Aug 2010 09:59 AM (UTC)
Message
I just got this to compile on g++ 4.5.0.

Changes needed:

#include <algorithm>
in several files.
#include <limits>
in several files.
#include <errno.h>
in several files.
Top

Posted by Nick Gammon   Australia  (23,122 posts)  Bio   Forum Administrator
Date Reply #92 on Mon 16 Aug 2010 11:16 AM (UTC)

Amended on Mon 16 Aug 2010 11:25 AM (UTC) by Nick Gammon

Message
Thanks Hex.

Sounds like I better do an improved version that works out of the box. :-)

That's not a criticism BTW, its just that compilers keep evolving, and things that used to work, suddenly don't.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,122 posts)  Bio   Forum Administrator
Date Reply #93 on Mon 16 Aug 2010 09:30 PM (UTC)
Message
The Tiny MUD server is now available for downloading from GitHub:

http://github.com/nickgammon/tinymudserver

The latest commit (f3cb4f0) fixes the problems with the missing include files.

It now compiles OK for me under:


gcc (Ubuntu 4.3.2-1ubuntu12) 4.3.2



You can clone a read-only version from GitHub like this:


git clone git://github.com/nickgammon/tinymudserver.git



- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Mudder   (7 posts)  Bio
Date Reply #94 on Sun 03 Oct 2010 09:17 PM (UTC)
Message
I'm having trouble creating a command that lists the current flags that a character has.

I'd rather just cycle through and spit out each flag, rather than checking for each flag like this:

if (HaveFlag (can_shutdown))
if (HaveFlag (can_goto))


That just seems unnecessary.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #95 on Sun 03 Oct 2010 10:08 PM (UTC)
Message
Why not something like:

for flag in flag list:
  if ch has flag:
    print "ch has flag '" + flag + "'"

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Nick Gammon   Australia  (23,122 posts)  Bio   Forum Administrator
Date Reply #96 on Sun 03 Oct 2010 10:43 PM (UTC)
Message
From the source, where it saves the player flags:


 copy (flags.begin (), flags.end (), ostream_iterator<string> (f, " "));


A variation on that should do it, as that was all it took to write them to the player file.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,122 posts)  Bio   Forum Administrator
Date Reply #97 on Sun 03 Oct 2010 11:14 PM (UTC)
Message
This worked for me, there might be an easier way:


void DoShowFlags (tPlayer * p, istream & sArgs)
{
  tPlayer * ptarget = p->GetPlayer (sArgs, "Usage: showflags <who>");  // who
  NoMore (p, sArgs);  // check no more input

  *p << "Flags for " << ptarget->playername << " : ";

  // loop through each flag  
  for (std::set<string, ciLess>::const_iterator it = ptarget->flags.begin ();
       it != ptarget->flags.end ();
       it++)
     *p << *it << " ";
  
  *p << "\n"; 
      
} // end of DoShowFlags


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Mudder   (7 posts)  Bio
Date Reply #98 on Mon 04 Oct 2010 12:30 AM (UTC)
Message
So I am experimenting with putting in color in various ways and since there was already a nice piece of code written to replace the %r with \n I thought I would play with it.


      messagemap [tolower (sMessageCode)] = FindAndReplace (sMessageText, "%r", "\r\n");

      messagemap [tolower (sMessageCode)] = FindAndReplace (messagemap [tolower (sMessageCode)], "[/color]", "\e[0m");


Someone already asked the question that gave me the part of the code that replaces [/color] - But what I don't quite understand is why the replacement works with the second piece of the code but if I try to use the first piece it doesn't.


      messagemap [tolower (sMessageCode)] = FindAndReplace (sMessageText, "%r", "\r\n");

      messagemap [tolower (sMessageCode)] = FindAndReplace (sMessageText, "[/color]", "\e[0m");


Why doesn't this work?
Top

Posted by Mudder   (7 posts)  Bio
Date Reply #99 on Mon 04 Oct 2010 12:48 AM (UTC)
Message
Thank you for the DoShowFlags piece of code, I had given up for the time being.

I hope I'm not coming off as if I'm asking questions before experimenting and using some google. Because that certainly is not the case.

I'm trying to get a feel for this base and I think I mostly have it. The rest, I think, will be easy. ;)
Top

Posted by Worstje   Netherlands  (899 posts)  Bio
Date Reply #100 on Mon 04 Oct 2010 01:25 AM (UTC)
Message
Mudder said:

... But what I don't quite understand is why the replacement works with the second piece of the code but if I try to use the first piece it doesn't.


      messagemap [tolower (sMessageCode)] = FindAndReplace (sMessageText, "%r", "\r\n");

      messagemap [tolower (sMessageCode)] = FindAndReplace (sMessageText, "[/color]", "\e[0m");


Why doesn't this work?


Because strings are immutable, and what you are assigning on the left side is not the same thing as you are using on the right side. When programming, do not forget that it is a 'timeline' from top to bottom; it is not math where everything is a statement of truth that is supposed to happen at the very same time.

To put it in a bit more trivial example without all the extra syntax cruft...

my_lucky_number = 42                      -- MY lucky number is 42.
your_lucky_number = my_lucky_number + 1   -- YOUR lucky number is mine plus one (43).
your_lucky_number = my_lucky_number - 10  -- YOUR lucky number is mine minus ten (32).
my_lucky_number = 1                       -- MY lucky number is 1.


You changing your mind on your lucky number does not change the fact that I still trust in the Hitchhiker's Guide of the Galaxy upto that point... until I get immersed into numerology. Then I choose one. But you still are stuck with 32 as your lucky number, since that is what you decided at that point in time.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #101 on Mon 04 Oct 2010 01:32 AM (UTC)
Message
Quote:
Because strings are immutable

Not in all languages, and in particular, not in C++. I think Mudder knows that program flow is linear. I don't know why it's not working since there is missing context and I don't know what the expected result is.

Mudder, you said that the first one doesn't work but the second one does, but then you ask why the second one doesn't work -- could you be more precise about which one is the one you expect to work but doesn't?

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Worstje   Netherlands  (899 posts)  Bio
Date Reply #102 on Mon 04 Oct 2010 01:54 AM (UTC)

Amended on Mon 04 Oct 2010 01:55 AM (UTC) by Worstje

Message
David Haley said:

Quote:
Because strings are immutable

Not in all languages, and in particular, not in C++.


Oops. Seems I am totally guilty of misinformation here. For some reason, I saw the code snippet, and after reading other topics involving Lua, I assumed it was Lua. My bad. :)

Still, assuming FindAndReplace does not modify the string in-place (which would be weird given the fact it returns stuff), the rest of my post holds.
Top

Posted by Mudder   (7 posts)  Bio
Date Reply #103 on Mon 04 Oct 2010 02:38 AM (UTC)

Amended on Mon 04 Oct 2010 03:09 AM (UTC) by Nick Gammon

Message
I suppose I wasn't entirely clear. Worstje did actually help me a little.

I've been away from coding for awhile so I was forgetting some basic things. I assumed tolower was modifying the original, instead of returning another. Newbie mistake!

Originally I couldn't see why the code wasn't working. I posted a working copy (the first code snip)

First Snip


messagemap [tolower (sMessageCode)] = FindAndReplace (sMessageText, "%r", "\r\n");

messagemap [tolower (sMessageCode)] = FindAndReplace (messagemap [tolower (sMessageCode)], "[/color]", "\e[0m");



then I posted a second snip and hoped for an explanation as to why it didn't work as the first did.

Second Snip


messagemap [tolower (sMessageCode)] = FindAndReplace (sMessageText, "%r", "\r\n");

messagemap [tolower (sMessageCode)] = FindAndReplace (sMessageText, "[/color]", "\e[0m");


The second part of both snips was where the question was.

The answer, of course, was that the original string wasn't being modified so I just kept reassigning values to the messagemap.

Since then I've been working to find a way to write this in a better way, slimming it down as much as possible. This is what I have.

void LoadHelps ()
{
  // format is: <code> <message>
  //  eg. motd Hi there!
  // Imbedded %r sequences will becomes new lines.
  ifstream fHelps (HELPS_FILE, ios::in);
  if (!fHelps)
    {
    cerr << "Could not open helps file: " << HELPS_FILE << endl;
    return;
    }

  while (!(fHelps.eof ()))
    {
    string sMessageCode, sMessageText;
    fHelps >> sMessageCode >> ws;
    getline (fHelps, sMessageText);
    if (!(sMessageCode.empty ()))
      sMessageText = FindAndReplace (sMessageText, "%r", "\r\n");
      sMessageText = FindAndReplace (sMessageText, "[/color]", "\e[0m");
      sMessageText = FindAndReplace (sMessageText, "[red]", "\e[0;31m");
      sMessageText = FindAndReplace (sMessageText, "[green]", "\e[0;32m");
      sMessageText = FindAndReplace (sMessageText, "[yellow]",  "\e[0;33m");
      sMessageText = FindAndReplace (sMessageText, "[blue]", "\e[0;34m");
      sMessageText = FindAndReplace (sMessageText, "[magenta]", "\e[0;35m");
      sMessageText = FindAndReplace (sMessageText, "[cyan]", "\e[0;36m");
      sMessageText = FindAndReplace (sMessageText, "[white]", "\e[0;37m");
      sMessageText = FindAndReplace (sMessageText, "[grey]", "\e[1;30m");
      sMessageText = FindAndReplace (sMessageText, "[RED]", "\e[1;31m");
      sMessageText = FindAndReplace (sMessageText, "[GREEN]", "\e[1;32m");
      sMessageText = FindAndReplace (sMessageText, "[YELLOW]", "\e[1;33m");
      sMessageText = FindAndReplace (sMessageText, "[BLUE]", "\e[1;34m");
      sMessageText = FindAndReplace (sMessageText, "[MAGENTA]", "\e[1;35m");
      sMessageText = FindAndReplace (sMessageText, "[CYAN]", "\e[1;36m");
      sMessageText = FindAndReplace (sMessageText, "[WHITE]", "\e[1;37m");
      messagemap [tolower (sMessageCode)] = sMessageText;

    } // end of read loop
} // end of LoadHelps


The above shows the context, the other snips were simply the bottom half.

[EDIT] Edited by Nick to put in some backslashes where required.
Top

Posted by Nick Gammon   Australia  (23,122 posts)  Bio   Forum Administrator
Date Reply #104 on Mon 04 Oct 2010 03:06 AM (UTC)

Amended on Mon 04 Oct 2010 03:29 AM (UTC) by Nick Gammon

Message
Mudder said:

I'm having trouble creating a command that lists the current flags that a character has.


Here is an improved version. It took a bit of research to work out how to write an output iterator for a player, but I managed it. :)



// put this in player.h

template <typename T>
class player_output_iterator : public std::iterator <std::output_iterator_tag, void, void, void, void>
{
 protected:
   tPlayer &    player_;  // who we are outputting to
   const char * delim_;   // delimiter between each item
   
 public:
  // constructor
  player_output_iterator (tPlayer & p, const char* d = "")
    : player_ (p), delim_ (d) {}
  
  // copy constructor
  player_output_iterator (const player_output_iterator<T>& rhs)
   : player_ (rhs.player_), delim_ (rhs.delim_) {}
  
  // assignment
  player_output_iterator<T>& operator= (const T& rhs)
    { 
    player_ << rhs << delim_;  
    return *this;
    }
  
  // dereference - no operation, returns reference to itself
  player_output_iterator<T>& operator* ()     { return *this; }
  // increment   - no operation, returns reference to itself
  player_output_iterator<T>& operator++ ()    { return *this; }
  // increment   -  no operation, returns reference to itself
  player_output_iterator<T>& operator++ (int) { return *this; }
};  // end of player_output_iterator


// this is in commands.cpp

void DoShowFlags (tPlayer * p, istream & sArgs)
{
  tPlayer * ptarget = p->GetPlayer (sArgs, "Usage: showflags <who>");  // who
  NoMore (p, sArgs);  // check no more input
  *p << "Flags: for " << ptarget->playername << " : ";
  copy (ptarget->flags.begin (), ptarget->flags.end (), player_output_iterator<string> (*p, " "));
  *p << "\n"; 
      
} // end of DoShowFlags



Basically the output iterator is a class that gets instantiated in the copy call (line in bold). It remembers in its internal variable which player we want (the target of the output), and then implements operator= for assignment, so that each time the copy algorithm copies a string to the iterator, it then gets copied the the player, with the delimiter after it.

This might be lengthier now, but lets you iterate over other things, and copy other batches of stuff (inventory maybe) to the player, so it is a neater end-result.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


344,714 views.

This is page 7, subject is 8 pages long:  [Previous page]  1  2  3  4  5  6  7 8  [Next page]

Posting of new messages is disabled at present.

Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.