Home » Stars! Clones, Extensions, Modding » FreeStars » some questions about coding
some questions about coding |
Wed, 09 March 2005 03:33 |
|
ConstB | | Crewman 1st Class | Messages: 27
Registered: March 2005 Location: Tomsk, Russia | |
|
Just checked out this fun piece of code and found it to be a bit strange.
1.
Let's see, every time you have to report some error and finish execution you are writing something like:
if (somethingWentWrong) {
Message * mess = TheGame->AddMessage("Error: Invalid FreeStarsVersion");
mess->AddItem(File);
mess->AddItem(GetString(node->FirstChild("FreeSta rsVersion")));
return false;
}
And if you have nested function calls this makes you code one huge error processing. Why don't you use c++ exceptions? they work quite fine even in your aged msvc6.
2.
Looks like you're writing freestars only for english speaking players. As I haven't seen a word about use of unicode or at least utf-8.
3.
You're creating Game object as a single "Game * TheGame" declaration an then export it for other modules. Quite simple but why not make it singleton?
4.
By the way, images in "FreeStars Classes.RTF" are not shown in my ms word 2000. :-(
Report message to a moderator
|
|
| | | |
Re: some questions about coding |
Wed, 09 March 2005 12:08 |
|
ConstB | | Crewman 1st Class | Messages: 27
Registered: March 2005 Location: Tomsk, Russia | |
|
LEit wrote on Wed, 09 March 2005 21:51 | 1. I don't like to use excpetions for expected problems. I also find that when reading other code, that exceptions are hard to follow, hard to debug, and makes the code look very cluttered.
|
Hmm.. In my humble opinion exceptions help avoid repeating this huge error reporting code, making it shorter and cleaner. Throwing is like another way of returning from function unambiguously treated as an error condition by calling function.
Debugging isn't a problem at all. MSVC6's debugger correctly follows to the catch clause.
And by the way, why aren't you using smart pointers for dynamic objects? They would help you avoid code like Game::~Game() and be sure objects are always correctly deleted.
LEit wrote on Wed, 09 March 2005 21:51 | 2. The server and the data files are in english, the client has to do the translation, the message strings can be thought of as message IDs, although they are readable in the data files if you want to read them by hand.
|
Ah, but this isn't enough! You forget about in-game messages sent by players (what encoding do they use?) and about ship and bases design names.
Consider two russian gamers playing freestars, one from windows, another under linux. First gives name to a ship desing in russian letters and what do another see? Garbage. Surprise, surprise, ;-) windows and linux use different encodings for russian (cp1251 in win and koi8-r in *nix)! But of course one of them may appear to be a programmer and he sends a patch for linux client to its author to automatically convert strings to koi, author won't accept it since it will break correct behaviour for another languages. So it seems like client author is going to have to add an additional option for all character conversions that users may require (not only russian language has more than one character encoding).
If you don't solve this problem in the freestars design, you shift this problem to client author's (and user's) shoulders. :-(
LEit wrote on Wed, 09 March 2005 21:51 | 3. I'm not sure how, or why to make it a singleton. A quick check seems like there isn't much code, so I'd be willing to change it.
|
Good template-based implementation of Singletons is provided by Loki library (loki-lib at sourceforge as far as I remember). It also contains good smart pointers implementation.
Singleton makes you sure that neither you nor another developer participating in writing code won't create another instance of Game by mistake, through inadvertence or through misunderstanding.
And by the way, what do Password field do in *m? files? To be readable by other players? ;-) And why it's missing in *.x? files? ;-)))
Oh, I've just checked out gstars code. Ehhh, is that the only way to create dialogs with wxWindows? I'm afraid it'll take a lot of time to make this work finished. ;-) Maybe there's any visual dialog editor for it?
And I checked your RTF-file. Pictures are linked to external files not present in CVS.
Report message to a moderator
|
|
|
Re: some questions about coding |
Wed, 09 March 2005 13:01 |
|
LEit | | Lt. Commander | Messages: 879
Registered: April 2003 Location: CT | |
|
By way of background, I've been programming for a while now and I leared C (not C++, it wasn't an option back then) in college, and C++ at work over the years. I'm not familiar with all the newer features and options, so I program with what I know, I am willing to learn new things if there is a good arguement for them however.
If you know how to fix the encoding, please let me know.
Passwords arn't working yet, right now they are just placeholders.
I may have the other files for the RTF at home, if so I'll check them in tonight, if not, I may just delete the RTF from CVS.
If you can get on IRC and go to the #FreeStars channel, we could have a conversation there.
- LEitReport message to a moderator
|
|
|
Re: some questions about coding |
Wed, 09 March 2005 13:39 |
|
ConstB | | Crewman 1st Class | Messages: 27
Registered: March 2005 Location: Tomsk, Russia | |
|
LEit wrote on Thu, 10 March 2005 00:01 | I program with what I know, I am willing to learn new things if there is a good arguement for them however.
|
Well, I'm not a C++ guru too, but I like experimenting. :-)
Quote: | If you know how to fix the encoding, please let me know.
|
As for me, I use Unicode whenever possible. But you should prefer UTF-8. First of all because it is default character encoding for TinyXML and I'm not sure whether Unicode XML is a common thing. And UTF-8 is more common in Linux if you are going to port. You just should notice that with UTF-8 character size isn't always 1 byte. And possibly explicitly declaring character set in XML files would be a good idea.
And windows client writers will have to add code to convert it before displaying to current charset. In Linux UTF-8 is supported natively.
Quote: | Passwords arn't working yet, right now they are just placeholders.
|
I see, but anyway they should reside in *.x files not *.m.
Quote: | I may have the other files for the RTF at home, if so I'll check them in tonight, if not, I may just delete the RTF from CVS.
|
Looks like it's something of *.EMF type...
Quote: | If you can get on IRC and go to the #FreeStars channel, we could have a conversation there.
|
Well, I'm there now. But not for a long time, it's already night here. :-)
Report message to a moderator
|
|
| | | | | |
Re: some questions about coding |
Thu, 10 March 2005 12:40 |
|
ConstB | | Crewman 1st Class | Messages: 27
Registered: March 2005 Location: Tomsk, Russia | |
|
haven't you discovered this yet?
exact algo of planet value calculation from original stars is:
{some buggy code deleted by me}
[Updated on: Fri, 11 March 2005 05:14] Report message to a moderator
|
|
| | | |
Re: some questions about coding |
Fri, 11 March 2005 05:18 |
|
ConstB | | Crewman 1st Class | Messages: 27
Registered: March 2005 Location: Tomsk, Russia | |
|
fixed
DWORD planetValueCalc(BYTE* planetHabData)
{
DWORD planetValuePoints=0,redValue=0,ideality=10000;
WORD i=0,
planetHab,habCenter,habUpper,habLower,
vE,habRadius,v12,v14,tmp;
for (i=0; i<3; i++)
{
planetHab = planetHabData[i];
habCenter = player.centerHab[i];
habLower = player.lowerHab[i];
habUpper = player.upperHab[i];
if (IMMUNE(habUpper))
planetValuePoints += 10000;
else
{
if (habLower <= planetHab && habUpper >= planetHab)
{
/* green planet */
vE = abs(planetHab-habCenter)*100;
if (habCenter > planetHab)
{
habRadius = habCenter-habLower;
vE /= habRadius;
tmp = habCenter-planetHab;
}
else
{
habRadius = habUpper-habCenter;
vE /= habRadius;
tmp = planetHab-habCenter;
}
v12 = ((tmp)*2)-habRadius;
vE = 100 - vE;
planetValuePoints += vE*vE;
if (v12>0)
{
ideality *= habRadius*2 - v12;
ideality /= habRadius*2;
}
}
else
{
/* red planet */
if (habLower <= planetHab) v14=planetHab-habUpper;
else v14=habLower-planetHab;
if (v14>15) v14=15;
redValue+=v14;
}
}
}
if (redValue!=0) return -redValue;
planetValuePoints = sqrt((double)planetValuePoints/3)+0.9;
planetValuePoints = planetValuePoints * ideality/10000;
return planetValuePoints;
}
this one works fine :-)
Report message to a moderator
|
|
| |
Re: some questions about coding |
Fri, 11 March 2005 05:58 |
|
mazda | | Lieutenant | Messages: 655
Registered: April 2003 Location: Reading, UK | |
|
ConstB wrote on Fri, 11 March 2005 10:18 | fixed
tmp = planetHab-habCenter;
v12 = ((tmp)*2)-habRadius;
if (v12>0)
{
ideality *= habRadius*2 - v12;
ideality /= habRadius*2;
}
|
Agreed.
v12 now has "units" of radius, which is what it needs for the ideality expression (which is what I tried to do above, I had the right units but the wrong expression).
Your earlier value for v12 was "unitless".
This also gives 14% for the (1,0,0) position.
However you have introduced the (if x < 0.5) term that "our" best fit formula also has. So it looks good.
Be interesting to see whether it fits all available data.
[Updated on: Fri, 11 March 2005 06:57] Report message to a moderator
|
|
| | | |
Re: some questions about coding |
Fri, 11 March 2005 08:40 |
|
m.a@stars | | Commander | Messages: 2765
Registered: October 2004 Location: Third star to the left | |
|
ConstB wrote on Fri, 11 March 2005 14:24 |
Well it's just three bytes with planet data and global "player" structure with three arrays for hab center, lower and upper values (-1 if immune).
|
Guessed that, even to the -1 I was thinking what kind of bytes. Looks like integers in the [0..100] range, AKA "clicks", right? If so, I'll need to convert from planet data to clicks :-/
As for assembly, well, I'm too lazy to locate by myself the relevant chunks of code, but if you already have them isolated and need only an accurate translation to "human readable", I'd be glad to help.
Never used a debugger for Windows, though. Never found a good reason
So many Stars, so few Missiles!
In space no one can hear you scheme! Report message to a moderator
|
|
| |
Re: some questions about coding |
Fri, 11 March 2005 12:02 |
|
ConstB | | Crewman 1st Class | Messages: 27
Registered: March 2005 Location: Tomsk, Russia | |
|
m.a@stars wrote on Fri, 11 March 2005 19:40 | Guessed that, even to the -1 :) I was thinking what kind of bytes. Looks like integers in the [0..100] range, AKA "clicks", right?
|
Exactly.
m.a@stars wrote on Fri, 11 March 2005 19:40 | As for assembly, well, I'm too lazy to locate by myself the relevant chunks of code, but if you already have them isolated and need only an accurate translation to "human readable", I'd be glad to help.
|
Ah. forgot about that! I had same problem and here is a solution:
char tmps[1024];
const char* gravityFromGravityPoints(int grav)
{
int result, tmp = abs(grav-50);
if (tmp<=25) result = (tmp+25)*4;
else result = tmp*24-400;
if (grav<50) result = 10000/result;
sprintf(tmps,"%d.%02dg",result/100,result%100);
return tmps;
}
const char* tempFromTempPoints(int temp)
{
int result;
result = (temp-50)*4;
sprintf(tmps,"%dC",result);
return tmps;
}
I used these to generate some kind of value tables to look at, just was too lazy to invent backward translation.
ofstream of ("gravlist.txt");
for (int i=0; i<=100; i++)
of << i << "\t" << gravityFromGravityPoints(i) << endl;
ofstream of ("templist.txt");
for (int i=0; i<=100; i++)
of << i << "\t" << tempFromTempPoints(i) << endl;
Report message to a moderator
|
|
|
Goto Forum:
Current Time: Mon Jun 10 07:09:49 EDT 2024
|