I am rewriting the source code

All posts that don't fit into another category, discuss ideas and other things

I am rewriting the source code

Postby Norakam » 20 Dec 2016 13:02

I think the source code of Red Eclipse is not very beautiful and therefore I have started to rewrite it. Is this a good idea or am I just crazy?

https://github.com/Robalni/red-eclipse (the main rewrite branch is called "crazy")

The goal with the rewrite is to make the code easier to read and maintain but not change how the game works. It must be compatible with the master branch of the official version. Some changes that are being done are:

  • Move functions to new files, like video functions to video.cpp and input functions to input.cpp, and put them in namespaces or classes.
  • Give functions and variables and other things more readable and describing names.
  • Change the way code is written (like spaces and newlines) to make it more readable.
  • Use the standard c++11 and some features from it, like nullptr and override. I don't know if we should use the standard library. There is a discussion about it here: http://redeclipse.net/forum/viewtopic.php?f=4&t=944
  • Don't have function definitions in header files.
  • Function definitions should not be too long. Split long functions into shorter ones.
  • Lines should be shorter than 80 characters.
  • Don't use too many global variables. Put them in classes, structs or namespaces.
  • I'm using the file extension .hpp instead of .h for header files because otherwise my editor thinks it's C code.
  • Write comments about what functions and classes do and how they should be used (unless it's too obvious).

Some noticeable changes right now that will not persist:

  • The progress() function is disabled which causes the game not to render anything until it is done loading. This is just to make it easier and faster to test changes on the slow computer that I am currently using.
  • The main loop is being rewritten and is not done yet. It can currently only handle menus. (playing the game doesn't work)
  • It is possible that some variables and commands and other parts of the game are broken.

I don't know if everybody likes my coding style but i think it's better than what we currently have. Would you prefer another way of writing the code? Another thing is that the Makefile is gone and CMakeLists.txt is changed in some ways that works for me but I don't know about other operating systems. Is there a good reason to have a suffix (like "_linux") on the compiled binary? I removed it.

If you want to help me, that would be nice, or just tell me if I do bad things. Are there other changes you would like to see in the code?
Norakam
 
Posts: 12
Joined: 23 May 2015 22:25

Re: I am rewriting the source code

Postby FaTony » 20 Dec 2016 13:26

I was waiting for change of multiplayer guidelines to start modding a server. The 1 problem I see is that there's no specification of the network protocol, so I wanted to keep original code in place but add a C++ API on top of it.
User avatar
FaTony
 
Posts: 391
Joined: 05 Feb 2014 13:14

Re: I am rewriting the source code

Postby Iceflower » 20 Dec 2016 22:15

Yes i like it! But i dont know if it really ever will be merged into RE master...

Norakam wrote:Lines should be shorter than 80 characters.


Thats really short i would say 120!

I forgot:

Inexor is a fork of Sauerbraten, they are structuring the project really really well! Take a look, maybe its helpfull:
https://github.com/inexor-game
I know my English is cruel. :$
When I talk rubbish correct me.
My public data
User avatar
Iceflower
 
Posts: 208
Joined: 16 May 2014 21:44
Location: Germany

Re: I am rewriting the source code

Postby acerspyro » 20 Dec 2016 22:41

80 characters is actually a standard for line breaks, but if absolutely needed, you can go over.
*screams internally*
User avatar
acerspyro
 
Posts: 270
Joined: 03 Mar 2014 18:04
Location: Quebec, Canada

Re: I am rewriting the source code

Postby qreeves » 21 Dec 2016 04:29

OK so, some tips if you want to see this get into the master branch:

  • Don't use STL. The reason it isn't used is to ensure portability and control over low level functions, tracking down bugs is hard enough without it descending into libraries we can't fix ourselves.
  • Functions in header files prefixed 'static inline' are intentional.
  • Creating a gazillion functions just because you can isn't very helpful and just serves to make the code harder to mentally parse. Functions are for reusable sections of code, or for things that encompass a complete idea (or a contiguous section of an idea).
  • Don't go enforcing a line length limit below 120, you'll waste half the screen space with emptiness for those who aren't crazy enough to use a text editor in a console.
  • Globals are almost always associated with a particular subject, you need to make sure they end up in the right namespace.
  • Don't rename files. Feel free to break up things into more files, but don't change the extensions the project uses without discussion.
  • When writing comments, please avoid repeating the same text over and over. If you need to insert a million headers with exactly the same text describing the topic of the code below it, it's probably better to group it together elsewhere.
  • Don't go back to having the game code in a giant struct, it took me forever to undo that from Cube 2.
Quinton Reeves | Lead Developer, Red Eclipse
Check Out My YouTube Channel | Leave Me a Tip on PayPal
Contribute to Project Costs via PayPal or Patreon
User avatar
qreeves
Site Admin
 
Posts: 1616
Joined: 02 Feb 2014 05:04
Location: Australia

Re: I am rewriting the source code

Postby zaquest » 21 Dec 2016 06:30

qreeves wrote:[*] Don't go enforcing a line length limit below 120, you'll waste half the screen space with emptiness for those who aren't crazy enough to use a text editor in a console.


There're many people using text editors in console, nothing crazy about it. Console is a very good place to deal with textual data. However termial emulators easily expand beyond 80 symbols, so this is probably not a good reason to limit line length. There're other reasons to do it though: possibility to read several source files side-by-side; if your line is over 80 symbols you're probably doing something nasty, like having too many conditions in one if (or on one line at least); or passing to a function big arithmetic expressions or results of ternary operators instead of using additional meaningful variables/functions, basically it often means your line is overloaded, especially when you use a lot of contractions and single char variable names, like in RE code; or your function takes way to many arguments and maybe it's worth using a structure there.

qreeves wrote:[*] Creating a gazillion functions just because you can isn't very helpful and just serves to make the code harder to mentally parse. Functions are for reusable sections of code, or for things that encompass a complete idea (or a contiguous section of an idea).


Creating additional functions usually makes code more clear, but, yes, only when they encompass a complete idea. I think these ideas, however, shouldn't be big, it might be as small as factoring out simple bounds checks with a meaningful function name. Not just `checkvar(var)` or `checkcond(var)`, but something more meaningful, case specific, that will help to understand what and why is checked. One example of function that seems reasonable to have in RE is something like `bool emptystr(const char *str) { return !(str && *str); }` since this is a VERY common pattern in RE code.
zaquest
 
Posts: 59
Joined: 16 Mar 2015 21:05

Re: I am rewriting the source code

Postby Norakam » 21 Dec 2016 07:22

qreeves wrote:Don't rename files.

Why not?

And a question about some code: What is the difference between the following global variables?

Code: Select all
int curtime, totalmillis, lastmillis;
time_t clocktime, currenttime;
uint totalsecs;
Norakam
 
Posts: 12
Joined: 23 May 2015 22:25

Re: I am rewriting the source code

Postby qreeves » 21 Dec 2016 11:37

curtime is the time since the last frame in milliseconds.
totalmillis is the total number of milliseconds elapsed since the program was started.
lastmillis is the total number of milliseconds elapsed with gamespeed taking into account.
clocktime is the current time in seconds since the epoch in UTC.
currenttime is the current time in seconds since the epoch in local time.
totalsecs is the total number of seconds since the program was started, used only for maxruntime.
Quinton Reeves | Lead Developer, Red Eclipse
Check Out My YouTube Channel | Leave Me a Tip on PayPal
Contribute to Project Costs via PayPal or Patreon
User avatar
qreeves
Site Admin
 
Posts: 1616
Joined: 02 Feb 2014 05:04
Location: Australia

Re: I am rewriting the source code

Postby FaTony » 21 Dec 2016 15:21

qreeves wrote:[*] Don't go enforcing a line length limit below 120, you'll waste half the screen space with emptiness for those who aren't crazy enough to use a text editor in a console.


What? 80 char limit is extremely useful.
Attachments
80charsforever.png
User avatar
FaTony
 
Posts: 391
Joined: 05 Feb 2014 13:14

Re: I am rewriting the source code

Postby eihrul » 26 Dec 2016 20:50

In this case, I am just going to be blunt: I am not interested in seeing wholesale rewrites of the engine to whatever pet programming style some programmer thinks is better than what we use. The argument that there is some holy, ordained style that one should be using and that we are not using, it just doesn't fly with me. The style we use works perfectly well for our engine, you just refuse to learn it, is all.

Yes, the Cube 2 source code is a bit idiosyncratic for historical reasons, but at the same time, it is still consistent, powerful, and allows interchange with all the other codebases that use Cube 2 with far less hassle than the alternatives. For that reason alone, I will more or less refuse these kinds of efforts, because I want to keep all the various instances of Cube 2 that I oversee interoperable and easy to work with any of them if you've worked with one.
eihrul
 
Posts: 1
Joined: 26 Dec 2016 20:42

Re: I am rewriting the source code

Postby Norakam » 30 Dec 2016 10:17

eihrul wrote:In this case, I am just going to be blunt: I am not interested in seeing wholesale rewrites of the engine to whatever pet programming style some programmer thinks is better than what we use. The argument that there is some holy, ordained style that one should be using and that we are not using, it just doesn't fly with me. The style we use works perfectly well for our engine, you just refuse to learn it, is all.

Yes, the Cube 2 source code is a bit idiosyncratic for historical reasons, but at the same time, it is still consistent, powerful, and allows interchange with all the other codebases that use Cube 2 with far less hassle than the alternatives. For that reason alone, I will more or less refuse these kinds of efforts, because I want to keep all the various instances of Cube 2 that I oversee interoperable and easy to work with any of them if you've worked with one.


Of course there is no holy programming style that everybody should use. I am just using the style I am used to and everybody else has their own style. I am not saying that this game has to be written this way.

I would not say that I refuse to learn it. I am learning a lot about the code by rewriting it and this is actually one of the reasons I am doing it.

If there is a good enough reason to keep the code compatible with other Cube 2 games then I guess this rewrite will not and should not be merged but I will probably still do the rewrite just for fun.
Norakam
 
Posts: 12
Joined: 23 May 2015 22:25

Re: I am rewriting the source code

Postby shirepirate » 06 Jan 2017 23:07

i'd suggest rewriting the hit detection algorithm, one of the most frustrating things for me when i come back to this game once in a while after playing other online multiplayer shooters is the no regs and the high average ping of players.
having a small community makes it so you can't simply block people from entering a server when their ping is too high, but the hit detection should greatly favor the shooter as to alleviate frustration. When you're used to your shots registering and have that preset expectation, then find that they don't In Red Eclipse the game isn't fun any more.

bit of a ramble but as long as you're rewriting code there's an idea on something you could greatly improve, as it's horrid in it's current state (lets not even talk about a high ping player using sword).
For every action, there is an equal and opposite reaction. For every freedom, there is a new limit. For every joy, a new sorrow. For every act of kindness, an unspeakable cruelty. Where someone succeeds, someone else fails. A new association means a new separation. Loyalty meets treachery, honor breeds disgrace, free expression yields censorship. Each piece of knowledge forms new ignorance. Even death gives rise to new life. But the balance of existence is the beginning of wisdom and discernment.
User avatar
shirepirate
 
Posts: 702
Joined: 27 Mar 2014 07:20

Re: I am rewriting the source code

Postby Jfault » 07 Jan 2017 02:02

*cough* urv *cough*
Keep calm and eat fruit.

MFW I've been a Ghost for 2 years D:
User avatar
Jfault
 
Posts: 77
Joined: 29 Oct 2014 20:37

Re: I am rewriting the source code

Postby qreeves » 08 Jan 2017 04:25

shirepirate wrote:i'd suggest rewriting the hit detection algorithm, one of the most frustrating things for me when i come back to this game once in a while after playing other online multiplayer shooters is the no regs and the high average ping of players.
having a small community makes it so you can't simply block people from entering a server when their ping is too high, but the hit detection should greatly favor the shooter as to alleviate frustration. When you're used to your shots registering and have that preset expectation, then find that they don't In Red Eclipse the game isn't fun any more.

bit of a ramble but as long as you're rewriting code there's an idea on something you could greatly improve, as it's horrid in it's current state (lets not even talk about a high ping player using sword).

Uh, the hit detection is client side, and therefore already favours the person doing the shooting as everything is detected from their perspective. If shots aren't registering then you are probably desynced.

Aside from that, this whole statement is ill-thought and overly negative in tone, check the attitude at the door please.
Quinton Reeves | Lead Developer, Red Eclipse
Check Out My YouTube Channel | Leave Me a Tip on PayPal
Contribute to Project Costs via PayPal or Patreon
User avatar
qreeves
Site Admin
 
Posts: 1616
Joined: 02 Feb 2014 05:04
Location: Australia

Re: I am rewriting the source code

Postby Norakam » 02 Mar 2017 19:52

I am working on a string class (or actually three) that will make it easier to create strings and append other strings and numbers to them. The reason I started doing this was because I tried to make a new way of defining variables and commands but they did not work well with the printf-like functions that we have in the game. You can use the string classes like this:

Code: Select all
SStr<512> my_string = "Hello";            // String with static size 512 bytes (like a char array)
my_string << " world!";                   // Append to the string.
DStr string2 = "aaaaaaaaa";               // Allocated on the heap and can grow if it needs to.
Str &a = my_string, &b = string2;         // Both of them can be used as Str.
const char *x = my_string, *y = string2;  // And are easily converted to const char*.


And using these strings we can write to console and log like this:

Code: Select all
console::out << "Hello! I am " << 5 << " years old." << Buf::end;
logfile << "aaaaaaaaaa" << '!' << Buf::end;


Do you like this or do you prefer the printf-like format string functions?
Norakam
 
Posts: 12
Joined: 23 May 2015 22:25

Re: I am rewriting the source code

Postby Iceflower » 02 Mar 2017 21:25

Why do you not use the std::strings?
I know my English is cruel. :$
When I talk rubbish correct me.
My public data
User avatar
Iceflower
 
Posts: 208
Joined: 16 May 2014 21:44
Location: Germany

Re: I am rewriting the source code

Postby qreeves » 03 Mar 2017 02:09

We already have the things you are trying to implement.

vector<char> mystring;
concatstring(mystring, " world!");
DStr can be done with either vector<char> or newstring()
Either can be used as char* or const char* with vector::getbuf() or the proper pointer. Nothing prevents strings from being cast to const, you just can't use delete on it.
Quinton Reeves | Lead Developer, Red Eclipse
Check Out My YouTube Channel | Leave Me a Tip on PayPal
Contribute to Project Costs via PayPal or Patreon
User avatar
qreeves
Site Admin
 
Posts: 1616
Joined: 02 Feb 2014 05:04
Location: Australia

Re: I am rewriting the source code

Postby Norakam » 03 Mar 2017 06:47

Iceflower wrote:Why do you not use the std::strings?

We don't want to use the standard library.
Norakam
 
Posts: 12
Joined: 23 May 2015 22:25

Re: I am rewriting the source code

Postby Norakam » 03 Mar 2017 07:04

qreeves wrote:We already have the things you are trying to implement.

vector<char> mystring;
concatstring(mystring, " world!");
DStr can be done with either vector<char> or newstring()
Either can be used as char* or const char* with vector::getbuf() or the proper pointer. Nothing prevents strings from being cast to const, you just can't use delete on it.

The problem I am trying to solve is concatenating strings with other types, like numbers or other classes.
Code: Select all
somestring << "abc" << 8734 << ", " << variable_of_some_other_type << '.';

This would be a little bit annoying to do with printf-like functions because I would have to explicitly cast variable_of_some_other_type to something printf-friendly every time, and concatstring() isn't really useful when concatenating strings with numbers.
Norakam
 
Posts: 12
Joined: 23 May 2015 22:25

Re: I am rewriting the source code

Postby qreeves » 03 Mar 2017 08:54

We don't use that sort of code style, so I guess the point is moot anyway. If it helps you, then more power to you :P
Quinton Reeves | Lead Developer, Red Eclipse
Check Out My YouTube Channel | Leave Me a Tip on PayPal
Contribute to Project Costs via PayPal or Patreon
User avatar
qreeves
Site Admin
 
Posts: 1616
Joined: 02 Feb 2014 05:04
Location: Australia


Return to General Discussion