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

I need variable conversion help...

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



Joined: 28 Jun 2013
Posts: 49

PostPosted: Sun Sep 18, 2016 7:35 pm    Post subject: I need variable conversion help... Reply with quote

So a little background into what I'm trying to do...a while back I posted a snippet for getting hex colors to work giving access to 1.6 million colors to use on DoT based muds. It has a few issues, but I'm trying to iron them out as I go, but it's solid, doesn't crash, and works pretty well.

I have recently converted saycolour to a string variable so that players may set their saycolours as hex colour values. It works fine except for one thing. When you have a hex colour set as your saycolour and you use sayto towards someone, the saycolour gets set correctly, but then the text of the say gets sent to translate_language() and that function adds the language to the beginning complete with `# and `^ codes around it. Problem is that `# and `^ and `& do not support hex colours, so I'm in the process of trying to get them to somehow.

My first attempt was an utter failure and completely lost me as to why exactly it failed because I think I have a fundamental misunderstanding of the stock code and in general the way that unsigned char variables can be fully utilized the way that they are. I attempted to convert the unsigned char variables in the COLOUR_MEMORY_TYPE struct directly to char * variables, and while the code I put in did actually correctly record hex colours with `#, it would not call them back properly, or rather what happened was that all instances of `^ or `& would call back the proper code, both single letters and hex colours, but instead of colouring the text as it should in the mud output, the colours would just be displayed with the text without a ` in front. When I added a ` to the code as a test, it printed that as well and I would get `Y for instance displaying instead of coloring it yellow.

Keep in mind that I am self-taught and have never taken classes or read books on coding, but here is my understanding of my failure. unsigned char variables are usually representative of numbers ranging from 0 to 255. When you set an unsigned char to a letter, such as 'G' it is set as the ascii value of that letter, not the letter itself. Converting from unsigned char to char * is relatively painless if I in fact did it in a manner that works...
Code:

 char *fred = FORMATF("%c",unsign);

I may be completely wrong doing it that way, but it SEEMED to work.
I may also be wrong for thinking that I need the answer to this question, because I may be going about it entirely wrong to begin with, but what is the best and most proper way to convert char * variables back to an unsigned char variable provided that there is safety in place to ensure that the char * does only contain a single letter?

All suggestions on how to do it, or even how to come at the problem in a completely different way are welcome.


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



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

PostPosted: Wed Sep 21, 2016 12:32 am    Post subject: Reply with quote

I applaud your effort in pioneering to do some complex work with the communication system; which is very complex. As you dig into it - you are digging into the language code, color code, et al and it can be a headache.

From what you are describing, your conversion from unsigned char to char * is creating some element of data loss; otherwise, the `# would be recalling the original color back properly.

What makes this complicated is it is a behavioral issue with the program; its not seg faulting so that makes identifying the problem a bit more taxing. Are there any other anomalies that you can address that may be affecting other parts of the program since implementing this?



_________________
- Daos

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



Joined: 28 Jun 2013
Posts: 49

PostPosted: Wed Sep 21, 2016 7:38 am    Post subject: Well Reply with quote

The only other anomaly in using hex colors other than `# and such not working with them is that `x does not reset hex colors to keep them from bleeding. Oddly, `w does, even though in our color table `x and `w have exactly identical entries.

I'm afraid that to get it all to work, I might have to rewrite the majority of the color code which I'm trying to avoid, I was just hoping there was an easier way that I might not be familiar with given my unfamiliarity with unsigned char variable types. I've done a little research online and found one or two methods that might help using strncpy and memcpy, but they seem sketchy to me. They may work though, we'll find out this weekend when I have a nice long block of time to try again.


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



Joined: 28 Jun 2013
Posts: 49

PostPosted: Fri Sep 23, 2016 6:54 am    Post subject: Hrmm Reply with quote

I've just had a revelation about this problem, I think I may have a solution, though it will be a major coding change to the color system. I'll keep you posted.


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



Joined: 28 Jun 2013
Posts: 49

PostPosted: Sun Sep 25, 2016 5:58 am    Post subject: I've solved the problem Reply with quote

Everything works beautifully now, it stores, restores, and rewinds both normal codes and hex color codes now using `# `^ and `&. As an added bonus, the new code and the solution to my problem also fixed the problem with `x not resetting to default color immediately after hex colors. Perseverance is great. Here's the small routine I used to convert back from char * to unsigned_char in the cases of single letter color codes and needing them to access the colour table, which still uses the unsigned char type...

Code:

unsigned char char2uc(char *string)
{
   unsigned char *end;
   end = static_cast<unsigned char *>(static_cast<void *>(string));
   return end[0];
}


Of course before I call this function I do check to make sure that it is in fact a single character in the string. String will only ever hold either a single letter, or a #FFFFFF style hex string. Its easy to tell the difference between them.


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



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

PostPosted: Tue Sep 27, 2016 9:11 pm    Post subject: Reply with quote

Well done! I am sorry I didn't reply sooner; I'm not getting new post notifications for some reason. Anyway, I am very happy for you. Nicely done!



_________________
- Daos

http://www.dawnoftime.org
Back to top
View user's profile Send private message Send e-mail Visit poster's website
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