Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace point constants with static class members #77730

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sparr
Copy link
Member

@sparr sparr commented Nov 10, 2024

Summary

None

Purpose of change

Currently whenever someone needs one of the point constants (zero, min, max, invalid, north, north_east, etc) for a coordinate type that hasn't been needed before, they add it to coordinate_constants.h. This file has accumulated over 100 such constants, in a hodge podge with no clear pattern to which constants exist and which constants don't and when it's appropriate to add a new one. This can make the codebase confusing to someone who sees tripoint_abs_omt_foo and wonders why tripoint_rel_ms_foo doesn't exist.

Describe the solution

I have moved the constants into static members of the point and tripoint classes, then exposed them in coord_point_ob. Now every tripoint_foo_bar type has ::zero, ::min, ::max, ::north, etc.

I also added a helper function to check whether a [tri]point equals the ::invalid constant, which was the original goal of this effort and led me down the rabbit hole of making point and tripoint behave more compatibly.

I've attempted to update every usage, including various places that were using _min but should have been using _invalid (which didn't exist in some cases) and are now using ::invalid.

I described these constants in POINTS_COORDINATES.md.

Describe alternatives you've considered

Exposing the constants on the coordinate types could be done in coord_point_base, but that felt too bloated.

Testing

Automated tests pass. I am playing with this change and will un-draft this PR when I feel I've tested thoroughly.

Additional context

I'm not sure how best to break this up into multiple PRs. I could put the old constants back, change just a few files to the new constants, PR that, then continue until they are all changed?

@github-actions github-actions bot added NPC / Factions NPCs, AI, Speech, Factions, Ownership Info / User Interface Game - player communication, menus, etc. Missions Quests and missions Bionics CBM (Compact Bionic Modules) Map / Mapgen Overmap, Mapgen, Map extras, Map display Vehicles Vehicles, parts, mechanics & interactions Crafting / Construction / Recipes Includes: Uncrafting / Disassembling Code: Tests Measurement, self-control, statistics, balancing. [C++] Changes (can be) made in C++. Previously named `Code` Monsters Monsters both friendly and unfriendly. Fields / Furniture / Terrain / Traps Objects that are part of the map or its features. Items: Ammo / Guns Ammunition for all kinds of weapons and these weapons themselves Melee Melee weapons, tactics, techniques, reach attack Scenarios New Scenarios, balancing, bugs with scenarios Character / World Generation Issues and enhancements concerning stages of creating a character or a world Player Faction Base / Camp All about the player faction base/camp/site Mechanics: Weather Rain, snow, portal storms and non-temperature environment Items: Archery Bows, crossbows, arrows, bolts Items: Containers Things that hold other things Code: Tooling Tooling that is not part of the main game but is part of the repo. Mechanics: Enchantments / Spells Enchantments and spells Items: Armor / Clothing Armor and clothing Appliance/Power Grid Anything to do with appliances and power grid EOC: Effects On Condition Anything concerning Effects On Condition astyled astyled PR, label is assigned by github actions json-styled JSON lint passed, label assigned by github actions labels Nov 10, 2024
@Procyonae
Copy link
Contributor

Adding these to POINTS_COORDINATES.md wouldn't hurt

@sparr sparr force-pushed the point_constant_static_members branch 2 times, most recently from f0022f7 to e508928 Compare November 10, 2024 16:48
@github-actions github-actions bot added the <Documentation> Design documents, internal info, guides and help. label Nov 10, 2024
@github-actions github-actions bot added the [Markdown] Markdown issues and PRs label Nov 10, 2024
@sparr sparr force-pushed the point_constant_static_members branch from e508928 to 589b9fe Compare November 10, 2024 21:14
@github-actions github-actions bot added BasicBuildPassed This PR builds correctly, label assigned by github actions and removed astyled astyled PR, label is assigned by github actions BasicBuildPassed This PR builds correctly, label assigned by github actions labels Nov 10, 2024
Copy link
Contributor

@CLIDragon CLIDragon left a comment

Choose a reason for hiding this comment

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

Given the changes are trivial and easily verified I feel like it should be okay even as a massive line diff.

src/point.h Outdated Show resolved Hide resolved
src/point.h Outdated Show resolved Hide resolved
@@ -6570,8 +6569,8 @@ void chop_tree_activity_actor::finish( player_activity &act, Character &who )
const point_rel_ms main_dir = pos.xy() - who.pos_bub().xy();
const int circle_size = 8;
static constexpr std::array<point, circle_size> circle = {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be const?

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 should really be implementing point==tripoint and using the existing eight_horizontal_neighbors instead of defining a new static array, but I'm not going to throw a functional change like that into a PR that's already this big. I'll add const here and come back to removing this on the next pass.

@PatrikLundell
Copy link
Contributor

This looks like a good change.

There was actually a sort of intention behind the set of constants that are defined and the ones that weren't, and that was that only it only made sense to have offsets for the relative types, with zero being used fairly frequently for absolute ones as well. However, it's only mildly detrimental to define everything for all types (detrimental, because it makes it easier to screw up and use an absolute offset constant when you really should have used a relative type, and similar errors).

@sparr sparr force-pushed the point_constant_static_members branch from 589b9fe to 85d7a8b Compare November 11, 2024 09:55
@github-actions github-actions bot added the astyled astyled PR, label is assigned by github actions label Nov 11, 2024
@sparr sparr force-pushed the point_constant_static_members branch from 85d7a8b to 12140a0 Compare November 11, 2024 17:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Appliance/Power Grid Anything to do with appliances and power grid astyled astyled PR, label is assigned by github actions Bionics CBM (Compact Bionic Modules) [C++] Changes (can be) made in C++. Previously named `Code` Character / World Generation Issues and enhancements concerning stages of creating a character or a world Code: Tests Measurement, self-control, statistics, balancing. Code: Tooling Tooling that is not part of the main game but is part of the repo. Crafting / Construction / Recipes Includes: Uncrafting / Disassembling <Documentation> Design documents, internal info, guides and help. EOC: Effects On Condition Anything concerning Effects On Condition Fields / Furniture / Terrain / Traps Objects that are part of the map or its features. Info / User Interface Game - player communication, menus, etc. Items: Ammo / Guns Ammunition for all kinds of weapons and these weapons themselves Items: Archery Bows, crossbows, arrows, bolts Items: Armor / Clothing Armor and clothing Items: Containers Things that hold other things json-styled JSON lint passed, label assigned by github actions Map / Mapgen Overmap, Mapgen, Map extras, Map display [Markdown] Markdown issues and PRs Mechanics: Enchantments / Spells Enchantments and spells Mechanics: Weather Rain, snow, portal storms and non-temperature environment Melee Melee weapons, tactics, techniques, reach attack Missions Quests and missions Monsters Monsters both friendly and unfriendly. NPC / Factions NPCs, AI, Speech, Factions, Ownership Player Faction Base / Camp All about the player faction base/camp/site Scenarios New Scenarios, balancing, bugs with scenarios Vehicles Vehicles, parts, mechanics & interactions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants