Skip to content

Conversation

@voidanix
Copy link
Member

  • Initialize x, y, z inside vec
  • Remove volatile keyword from errors counter
  • Clear objects of now trivial type
  • Fix possible uninitialization of height
  • Convert weapon selection system to std::vector
  • Fix -Wnon-pod-varargs error on clang 14
  • Fix -Wint-in-bool-context
  • Document what the sound enums actually are
  • Fix unneeded parenthesis
  • Fix uninitialized values in md3 structs
  • Fix comparison between different signedness
  • Fix address of array always returning true
  • Remove set but unused variables
  • Fix implicit conversion between float and int
  • Fix shift with negative value
  • Attempt to fix misleading indentation warning

Copy link
Member

@TheAssassin TheAssassin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had a first look. Mostly great improvements of the code. I have a few remarks I'd like to discuss, though.

{
const bvec &watercol = getwatercol((curmat == MAT_WATER ? matid : pl->inmaterial) & MATF_INDEX);
mattrig(bottom, watercol, 0.5f, int(radius), PHYSMILLIS, 0.25f, PART_SPARK, curmat != MAT_WATER ? S_SPLASH2 : S_SPLASH1);
mattrig(bottom, watercol, 0.5f, int(radius), PHYSMILLIS, 0.25f, PART_SPARK, (curmat != MAT_WATER ? S_SPLASH2 : S_SPLASH1));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a readability change, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kind of: the compiler is probably interpreting this as MAT_WATER ? S_SPLASH2 : S_SPLASH1 and then comparing against curmat

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!= is computed before ?:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@robalni is right here. If you want to make this more explicit (and improve readability), you have to put brackets around the != comparison.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That does not work I'm afraid:

bn/src/game/physics.cpp: In function ‘void physics::updatematerial(physent*, const vec&, const vec&, bool)’:
bn/src/game/physics.cpp:1058:115: warning: ‘?:’ using integer constants in boolean context, the expression will always evaluate to ‘true’ [-Wint-in-bool-context]
 1058 |                 mattrig(bottom, watercol, 0.5f, int(radius), PHYSMILLIS, 0.25f, PART_SPARK, (curmat != MAT_WATER) ? S_SPLASH2 : S_SPLASH1);
      |                                                                                                                   ^

Copy link
Contributor

@robalni robalni Jun 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the problem here is that in the definition of the macro mattrig, the last argument mw is used like:

if(mw >= 0)

and this expands to:

if(curmat != MAT_WATER ? S_SPLASH2 : S_SPLASH1 >= 0)

which is the same as:

if( (curmat != MAT_WATER) ? S_SPLASH2 : (S_SPLASH1 >= 0) )

so I think this macro needs to be fixed by adding parentheses around the arguments:

if((mw) >= 0)

and fun fact, when curmat is MAT_WATER then (S_SPLASH1 >= 0) is used and this equals 1 and is the same as S_GUIBACK which means that S_GUIBACK has been played where S_SPLASH1 should have been played.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome catch! Looks a bit ugly now though 👀

Not even worth converting into a function because this macro is used twice in the codebase...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case it might just be removed entirely. That tiny bit of code duplication is acceptable.

@voidanix
Copy link
Member Author

Thanks for the reviews @TheAssassin and @robalni ! Updated the branch

@voidanix voidanix force-pushed the warnings branch 2 times, most recently from b2151fe to 10f8631 Compare June 24, 2022 08:46
@voidanix
Copy link
Member Author

voidanix commented Jul 9, 2022

FYI the inrange function now uses constexpr (it will be reused in a later refactor) and the custom NULL macro has been removed

@voidanix voidanix requested a review from TheAssassin July 10, 2022 22:59
Copy link
Member

@TheAssassin TheAssassin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a few more things.

Please also clean up the branch history when you're done.

game::player1.loadweap.end(), i) == game::player1.loadweap.end()) // NOT found
{
game::player1.loadweap.emplace_back(i);
if (game::player1.loadweap.size() >= W_LOADOUT)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please wrap all conditional code (e.g., branches of an if) in braces to create a proper scope. It is a common issue in refactorings to add lines to existing code without realizing that the scope had ended at the semicolon. This can be avoided by always using a scope.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this work?:

if (std::none_of(game::player1.loadweap.begin(),
                game::player1.loadweap.end(), [=](auto d) { return d == i; }))

I hope there isn't too much overhead if done like this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be avoided by always using a scope.

That is silly and adds unnecessary LoC; even the Kernel's coding style agrees:

Do not unnecessarily use braces where a single statement will do.

Copy link
Member

@TheAssassin TheAssassin Aug 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just because the Linux kernel uses it doesn't mean it's beginner friendly. Quite the opposite, really. I've seen this kind of problem too often, so I always use a scope now. It's a pre-emptive measure used to prevent branching problems.

"Saving lines of code" is an argument which I often heard before, but it is not a good one. It might have been relevant, back in the days when there were just 12 lines on one screen. Compacting the code too much like this does not aid readability, quite the opposite, and is often used to compact and therefore kind of "hide" complexity. Look at this line, it's probably too long already.

Python for instance, while not everyone likes the lack of braces, does this very well in general. Whenever the code branches, moving the conditional code on a new line (and therefore adding some indentation as well) is very strongly recommended. You could use the same line, but this is ugly and cumbersome. See https://docs.python-guide.org/writing/style/#one-statement-per-line.

Regarding braces, many popular code styles demand them, there are just few exceptions, mostly for old projects who had established such a style long ago. Not using braces "saves" 1-2 lines depending on the code style. I personally prefer to move the opening one on the same line as the conditional/loop statement. As stated in #248 (comment), this ensures that on future refactorings, new code won't accidentally be added in the hope that it will be conditional as well. Furthermore, this also aids the readability of the code. You can quickly see where branching happens. New variables have a clear scope. You can't be surprised when commenting out lines. You do not have to be careful about the semicolon being the end of the conditional code block.

This is not an obfuscated code competition. The code should be easy to read. Branches should be easily visible. Code should be written with the need for future changes in mind.

Edit: kernel code is among the worst code I've ever seen. This is only partly due to code style problems. It's largely C89-style, even though it's been over 30 years since that standard has been released, and there have been multiple new standards since (even C90 introduced so many positive changes, yet kernel code does not use any, e.g., comments). It uses tabs, which the majority of coders rejects for spaces. It's largely undocumented (i.e., the "what the heck were they thinking when writing that" bit is missing). And these are just a few examples...

prev.clear();
}
if(!buf.empty()) setmods(sv_previousmaps, buf.c_str());
if(!buf.empty())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While you're at it, please add spaces in between statements such as if and for and the corresponding conditions.

We really need clang-format to auto check changes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current style is to not have a space there. I'm not a big fan of mixing styles because the point of a style is the consistency that it gives.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current style is to not have a space there. I'm not a big fan of mixing styles because the point of a style is the consistency that it gives.

That is the codebase's old style: we aren't fans of mixing stuff either but every new piece of code should be adopting the new style.

@voidanix
Copy link
Member Author

voidanix commented May 4, 2023

Ping, any updates on this?

@voidanix voidanix mentioned this pull request May 11, 2023
voidanix and others added 13 commits December 8, 2025 02:34
Also introduces a new "inrange" function, useful for future refactorings
to std::vector
The mattrig macro expands the mw argument at one point into:

if (curmat != MAT_WATER ? S_SPLASH2 : S_SPLASH1 >= 0)

Co-authored-by: Robert Alm Nilsson <rorialni@gmail.com>
There should be no reason to overwrite this as the C++ spec already
specifies it. Compilers nowadays also gift us a dedicated warning for this
kind of situation (-Wzero-as-null-pointer-constant).

Remove it and specify <cstddef> to still make NULL accessible.
This usage of volatile has been deprecated since C++20: use std::atomic
in its place.
Resolves the ambiguity of implementation defined NULLs

Fixes: 8844f42 ("tools: Remove custom NULL")
@voidanix voidanix requested a review from TheAssassin December 9, 2025 00:56
@voidanix voidanix added this to the 1.7.0 milestone Dec 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants