Home World Forum
Stars! AutoHost web forums

Jump to Stars! AutoHost


 
 
Home » Stars! Clones, Extensions, Modding » FreeStars » some questions about coding
some questions about coding Wed, 09 March 2005 03:33 Go to next message
ConstB is currently offline 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 10:51 Go to previous messageGo to next message
LEit is currently offline LEit

 
Lt. Commander

Messages: 879
Registered: April 2003
Location: CT
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.

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.

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.



- LEit

Report message to a moderator

Re: some questions about coding Wed, 09 March 2005 10:57 Go to previous messageGo to next message
PricklyPea is currently offline PricklyPea

 
Lieutenant

Messages: 534
Registered: February 2005
LEit wrote on Wed, 09 March 2005 10: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.



I guess the simplest would be to simply pass the message ID along with paramenters and let the client handle it e.g. 01 35 02 25 (all bytes for simplicity) might be:

01 Unload Iron command
35 Fleet ID
02 Planet ID
25 Quantity

Client to translate as:

"Medium Freighter #35 has unloaded 25kt of Ironium to Planet X".

My coding question was why XML?!?! Razz

Report message to a moderator

Re: some questions about coding Wed, 09 March 2005 11:01 Go to previous messageGo to next message
LEit is currently offline LEit

 
Lt. Commander

Messages: 879
Registered: April 2003
Location: CT
It is coded, I just use strings instead of numbers.
So instead of 01 you see 'Unload iron'.

It takes slightly more space in the data files, but after compression, it shouldn't be that big of a hit, and will be much easier to read the files in the meantime. I suppose if the files size becomes a problem, we'll have to go back and change it, but I doubt it will become a problem.

The alternative to XML at the time was a text file format that I would have to come up with, and write a parser for...



- LEit

Report message to a moderator

Re: some questions about coding Wed, 09 March 2005 12:08 Go to previous messageGo to next message
ConstB is currently offline 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 Go to previous messageGo to next message
LEit is currently offline 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.



- LEit

Report message to a moderator

Re: some questions about coding Wed, 09 March 2005 13:39 Go to previous messageGo to next message
ConstB is currently offline 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 Wed, 09 March 2005 14:32 Go to previous messageGo to next message
PricklyPea is currently offline PricklyPea

 
Lieutenant

Messages: 534
Registered: February 2005
UTF-8, compression, XML.

All these have had recent exploits due to complexity. Since you're writing a server based app, consideration should also be given to security issues.

Report message to a moderator

Re: some questions about coding Wed, 09 March 2005 14:35 Go to previous messageGo to next message
ConstB is currently offline ConstB

 
Crewman 1st Class

Messages: 27
Registered: March 2005
Location: Tomsk, Russia
Found mistype in Galaxy::DoBombing - excess semicolon in first if. You should pay more attention to warnings from compiler. ;-)

Report message to a moderator

Re: some questions about coding Wed, 09 March 2005 14:51 Go to previous messageGo to next message
ConstB is currently offline ConstB

 
Crewman 1st Class

Messages: 27
Registered: March 2005
Location: Tomsk, Russia
And two more issues. <typeinfo> must be included in FSServer.h to compile those hackish dynamic_cast's. And I was to comment out "min" and "max" redefenitions...

And you shouldn't define TIXML_USE_STL in TinyXmlPlus.cpp as it is already defined in project settings.

Report message to a moderator

Re: some questions about coding Wed, 09 March 2005 15:05 Go to previous messageGo to next message
ConstB is currently offline ConstB

 
Crewman 1st Class

Messages: 27
Registered: March 2005
Location: Tomsk, Russia
Bugs, bugs, bugs...

Variables in Bombing::Resolve are used without being initialized...

In MessLoc::ToString "m += mLoc->GetPosX();" causes return value to be autoconverted to char and than added to m as a char... And this way 6 times through this method. Programming in Visual Basic style?.. ;-)

Report message to a moderator

Re: some questions about coding Wed, 09 March 2005 21:42 Go to previous messageGo to next message
LEit is currently offline LEit

 
Lt. Commander

Messages: 879
Registered: April 2003
Location: CT
Passwords and encryption will be considered, after the game is (mostly) working. Note that whoever has access to the host program will have access to everything, so we will either need to trust the host or use something like AH.

Because the source is available, a malicious host could recomplie the program to see what everyone is doing, if the server program can see the data, then the host can too.

Bombing is not compete yet, I've told the person working on it about that ; and about the strings. TIXML_USE_STL is defined in a file because MSVC and other C++ use different project files. It should be in a h file not a cpp file however. Or just defined in every project file.



- LEit

Report message to a moderator

Re: some questions about coding Thu, 10 March 2005 12:40 Go to previous messageGo to next message
ConstB is currently offline 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 Thu, 10 March 2005 13:54 Go to previous messageGo to next message
PricklyPea is currently offline PricklyPea

 
Lieutenant

Messages: 534
Registered: February 2005
Another Russian reverse engineer is detected! Wink

Report message to a moderator

Re: some questions about coding Thu, 10 March 2005 19:46 Go to previous messageGo to next message
Downsider is currently offline Downsider

 
Crewman 1st Class

Messages: 35
Registered: June 2003
Location: Derbyshire, England
I'm not sure that IS the exact planet value calculation algorithm. It doesn't seem to work.

Using a the race hab setting G 20-80 (0.31g - 3.2g), T 20-80 (-120C - 120C), R 20-80 and the planet hab of 62 (1.48g), 46 (-16C), 73, I get a value of 60%.
The planet value calculator I use gives 46% (+/- 1%).

I think you need to go back and work that one again Very Happy



"Violence is the last resort of the incompetent" - Salvor Hardin

Report message to a moderator

Re: some questions about coding Fri, 11 March 2005 05:04 Go to previous messageGo to next message
ConstB is currently offline ConstB

 
Crewman 1st Class

Messages: 27
Registered: March 2005
Location: Tomsk, Russia
Downsider wrote on Fri, 11 March 2005 06:46


Using a the race hab setting G 20-80 (0.31g - 3.2g), T 20-80 (-120C - 120C), R 20-80 and the planet hab of 62 (1.48g), 46 (-16C), 73, I get a value of 60%.
The planet value calculator I use gives 46% (+/- 1%).


you're right. seems like i missed something.

Report message to a moderator

Re: some questions about coding Fri, 11 March 2005 05:18 Go to previous messageGo to next message
ConstB is currently offline 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:48 Go to previous messageGo to next message
mazda is currently offline mazda

 
Lieutenant

Messages: 655
Registered: April 2003
Location: Reading, UK
Downsider wrote on Fri, 11 March 2005 00:46


I think you need to go back and work that one again Very Happy

Try changing this line :-
v12 = ((planetHab-habCenter)*2) / habRadius
to
v12 = ((planetHab-habCenter)^2) / habRadius

I don't know if it works, but it fixes it for the (1,0,0) point which should be 14%
((sqrt(3333) + 0.9) / 4)

Report message to a moderator

Re: some questions about coding Fri, 11 March 2005 05:58 Go to previous messageGo to next message
mazda is currently offline 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 07:49 Go to previous messageGo to next message
m.a@stars is currently offline m.a@stars

 
Commander

Messages: 2765
Registered: October 2004
Location: Third star to the left
mazda wrote on Fri, 11 March 2005 11:58


Be interesting to see whether it fits all available data.



I'll be doing some of that this weekend, as I was already going to use the "old" best-fit against a bunch of planet data I'm trying to analyze.

I'll have to first "translate" what this function needs as input. I hope it won't be hard. Smile

Out of curiosity: How in Stars' name do you get such a nice source out from the EXE? Cool Looks like a lot of work to reconstruct all of that from assembly by hand!


[Updated on: Fri, 11 March 2005 08:00]




So many Stars, so few Missiles!

In space no one can hear you scheme! Deal

Report message to a moderator

Re: some questions about coding Fri, 11 March 2005 07:52 Go to previous messageGo to next message
ConstB is currently offline ConstB

 
Crewman 1st Class

Messages: 27
Registered: March 2005
Location: Tomsk, Russia
I just was too inattentive reversing that code. Now I'm dealing with inattentiveness when I was reversing race advantage points calculation code. I've already fixed three mistakes but it still doesn't work as expected... :-(

Could you recommend good debugger for win16 programs? I don't have any...

Report message to a moderator

Re: some questions about coding Fri, 11 March 2005 08:24 Go to previous messageGo to next message
ConstB is currently offline ConstB

 
Crewman 1st Class

Messages: 27
Registered: March 2005
Location: Tomsk, Russia
m.a@stars wrote on Fri, 11 March 2005 18:49

I'll have to first "translate" what this function needs as input. I hope it won't be hard. :)



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).

m.a@stars wrote on Fri, 11 March 2005 18:49

Out of curiosity: How in Stars' name do you get such a nice source out from the EXE? 8) Looks like a lot of work to reconstruct all of that from assembly by hand!


Yep. ;-)

Report message to a moderator

Re: some questions about coding Fri, 11 March 2005 08:40 Go to previous messageGo to next message
m.a@stars is currently offline 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 Smile 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 Confused



So many Stars, so few Missiles!

In space no one can hear you scheme! Deal

Report message to a moderator

Re: some questions about coding Fri, 11 March 2005 09:39 Go to previous messageGo to next message
PricklyPea is currently offline PricklyPea

 
Lieutenant

Messages: 534
Registered: February 2005
ConstB wrote on Fri, 11 March 2005 07:52

Could you recommend good debugger for win16 programs? I don't have any...


Try NuMega's SoftICE. It was the king of debuggers back in the day. I think they've been acquired by CompuWare now.

I understand quite a few people are moving to windbg now.

You can also export your IDA symbols into SICE Wink

Just found this link:

http://msmvps.com/kernelmustard/archive/2004/09/01/12640.asp x


[Updated on: Fri, 11 March 2005 10:46]

Report message to a moderator

Re: some questions about coding Fri, 11 March 2005 12:02 Go to previous messageGo to previous message
ConstB is currently offline 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

Previous Topic: Summary of Math?
Next Topic: Need web hosting for Race Wizard
Goto Forum:
  


Current Time: Fri May 03 23:29:50 EDT 2024