The Dawn Of Time
The Official Forums for the Dawn Of Time Mud Codebase
 
Log in Register FAQ Memberlist Search The Dawn Of Time Forum Index Goto the Official Dawn Website

frustrating bug in string.cpp

 
Post new topic   Reply to topic    The Dawn Of Time Forum Index » Coding
View previous topic :: View next topic  
Author Message
typhonjr



Joined: 11 Oct 2015
Posts: 11

PostPosted: Sun Oct 11, 2015 5:50 am    Post subject: frustrating bug in string.cpp Reply with quote

I know i'm new to the forums but I've been coding on DOT 1.69r on and off for a few years.

I don't want to say the code is heavily modified but its definitely not stock. Added in room and object progs (similar to mob progs), a profession system, customizable who format (like prompt), full html color support, and countless minor things.

By request one of the admins would like a automated way to add color in a repeated fashion. I thought to myself ok no problem. first i tried to add it to redit as an addition command and i couldnt get it to work quite right. it wasnt freeing the string correctly and adding garbage at the end of the string. i chewed on it a while and decided to drop the code in string.cpp as a new dot "." command.
Code:

   if ( !str_cmp( arg1, ".col" ) )
        {
            *ch->desc->pString = colorstring( ch, arg2 );
            ch->println("String formatted.");
            return;
        }

pretty straight forward or so I thought.

this is the code to confirm validity of the color codes 1 character at a time. (used in colorstring())
Code:

//check if a letter is a valid code. done using colourTable -typ
bool valid_color_code ( char ltr )
{
   int x;
   bool isfound = false;
   for(x=0; makeFullColourTableFrom[x].code != '\0'; x++)
   {
      if ( ltr == makeFullColourTableFrom[x].code && !makeFullColourTableFrom[x].special && makeFullColourTableFrom[x].dont_repeat )
         isfound = true;
   }
   return isfound;
}


and finally the code to colorize the string.
Code:

//custom color code to inject <color string> one letter at a time into the room description -typ
char *colorstring( char_data *ch, char * argument)
{
   if ( IS_NULLSTR(argument)){
      ch->println( "Syntax:  .col <color string>" );
      return *ch->desc->pString;
   }
   int len, x=0, y=0;
   char arg1 [MIL];
   char *newstr = NULL;
   char xbuf[HSL];
   newstr = str_dup(strip_colour(*ch->desc->pString));

   len=str_len(newstr);
//count proom->desc and triple it.
   if (len*3 > MSL-4)
   {
      ch->printlnf( "Description is too long. Max string length = %d, Length = %d, Colorized Length = %d", MSL-4, len, len *3 );
      return false;
   }

//if its not > than MSL, continue
//check for validity of color string

   if (str_len(argument) <= 1 )
   {
      ch->println( "Color string needs to be longer then 1 character" );
      return false;
   }
   while (argument[x] != '\0')
   {
      if (!valid_color_code( argument[x] ))
      {
         ch->printlnf( "Invalid color code found: %c", argument[x] );
         return false;
      }
      x++;
   }

   xbuf[0] = '\0';
   
   x=0;y=0;

   for (newstr; *newstr; newstr++)
   {
//only change color if desc is a letter.
      if (xbuf[x] == '`')
         x+=2;

      if (xbuf[x] != ' ')
      {
         xbuf[x]='`';
         x++;
         xbuf[x]=argument[y];
         x++; y++;
         if (argument[y]=='\0')
            y=0;
      }
      xbuf[x]=*newstr;
      x++;

   }
   xbuf[x]='\0';
//for beta testing: (might leave in later)
   ch->printlnf("Old Description:`1%s", ch->desc->pString);
   ch->printlnf("New Description:`1%s", xbuf);
   free_string( *ch->desc->pString);
   return xbuf;

}


now heres the glitchy part. the code works great (sort of)!
as you can see in the below picture Old Description has some garbage there:


but a .s will show the colored string:


even exiting redit and looking shows the correct color code:


however editing the description at all (.-, .ld 1, even just pressing enter) will cause the mud to crash immediately:


below is the debug log:



i'm about as frustrated as it gets with this and any help would be amazing.

thanks
-typ


Back to top
View user's profile Send private message
typhonjr



Joined: 11 Oct 2015
Posts: 11

PostPosted: Sun Oct 11, 2015 6:14 am    Post subject: Reply with quote

well...

tinkering around i changed the code to:
Code:

void colorstring( char_data *ch, char * argument)
{
   if ( IS_NULLSTR(argument)){
      ch->println( "Syntax:  .col <color string>" );
      return ;
   }
   int len, x=0, y=0;
   char arg1 [MIL];
   char *newstr = NULL;
   char xbuf[HSL];
//   newstr = str_dup(*ch->desc->pString);

   newstr = strip_colour(*ch->desc->pString);
   len=str_len(newstr);
//count proom->desc and triple it.
   if (len*3 > MSL-4)
   {
      ch->printlnf( "Description is too long. Max string length = %d, Length = %d, Colorized Length = %d", MSL-4, len, len *3 );
      return ;
   }

//if its not > than MSL, continue
//check for validity of color string

   if (str_len(argument) <= 1 )
   {
      ch->println( "Color string needs to be longer then 1 character" );
      return ;
   }
   while (argument[x] != '\0')
   {
      if (!valid_color_code( argument[x] ))
      {
         ch->printlnf( "Invalid color code found: %c", argument[x] );
         return ;
      }
      x++;
   }

   xbuf[0] = '\0';
   
   x=0;y=0;

   for (newstr; *newstr; newstr++)
   {
//only change color if desc is a letter.
      if (xbuf[x] == '`')
         x+=2;

      if (xbuf[x] != ' ')
      {
         xbuf[x]='`';
         x++;
         xbuf[x]=argument[y];
         x++; y++;
         if (argument[y]=='\0')
            y=0;
      }
      xbuf[x]=*newstr;
      x++;

   }
   xbuf[x]='\0';
//for beta testing:
   ch->printlnf("Old Description:`1%s", ch->desc->pString);
   ch->printlnf("New Description:`1%s", xbuf);
   free_string( *ch->desc->pString);
    *ch->desc->pString = str_dup( xbuf );
 
   return ;

}


and now it works like a charm. not sure i really understand why it got tripped up. simplification works best sometimes i guess.

i still get the odd garbage after old description:

and:




Back to top
View user's profile Send private message
typhonjr



Joined: 11 Oct 2015
Posts: 11

PostPosted: Sun Oct 11, 2015 6:22 am    Post subject: Reply with quote

welll...

figured out that one too lol.

Code:

ch->printlnf("Old Description:`1%s", ch->desc->pString);


to

Code:

ch->printlnf("Old Description:`1%s", *ch->desc->pString);


cheers,
-typ


Back to top
View user's profile Send private message
Daos



Joined: 29 Jan 2003
Posts: 1219
Location: United States

PostPosted: Sun Oct 11, 2015 12:57 pm    Post subject: Reply with quote

Very interesting and creative; though how do you all plan to effectively use that? Perhaps sparingly? Reason I ask is because any type of long description with a constant changing of random colors per letter can make it extremely hard to read.



_________________
- Daos

http://www.dawnoftime.org
Back to top
View user's profile Send private message Send e-mail Visit poster's website
typhonjr



Joined: 11 Oct 2015
Posts: 11

PostPosted: Mon Oct 12, 2015 7:45 am    Post subject: Reply with quote

Sparingly is good. It is challenging to read and i'm hoping people group similar colors together if they decide to use it.


Back to top
View user's profile Send private message
Trell



Joined: 28 Jun 2013
Posts: 49

PostPosted: Thu Oct 22, 2015 3:11 pm    Post subject: Sparingly indeed Reply with quote

We've had a similar ability on our mud for a long while now and it works pretty well. I've recently added it as a "." command, since before it was just a normal redit command, but I had to limit the "." command to imms only, since we didn't want people overusing it for anything they could do in the desc editor... descriptions, notes, etc... Most of our areas that have been created since it went in, use this coloring method almost exclusively, and while some builders have a good eye for which colors and patterns work well together, others can give you a headache reading them.


Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    The Dawn Of Time Forum Index » Coding All times are GMT + 13 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001-2005 phpBB Group
Theme created by Vjacheslav Trushkin