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

Codechange: Use vector for airport tile layouts. #12607

Merged
merged 2 commits into from May 2, 2024

Conversation

PeterN
Copy link
Member

@PeterN PeterN commented May 1, 2024

Motivation / Problem

AirportSpec contains some pointers, and pointers to pointers, along with counts of the number of items that they point to.

These pointers point to either the original airport layouts or custom airport layouts, which may or may not need to be freed afterwards.

The layout of the data is awkward to understand as well, being stored in separate arrays instead of an array of structs.

Description

Simplify AirportSpec data by storing layout information together in a vector, instead of separate arrays.

Adds the struct AirportTileLayout which holds both the AirportTileTable data and the rotation, keeping this data together.

The depot data is actually not changeable as it depends on the FSM, so this pointer is removed and replaced with a span as it never needs to be copied or changed.

This removes manual memory management and separate count members.

The default layouts will be copied instead of always referring to the originals.

Limitations

Checklist for review

Some things are not automated, and forgotten often. This list is a reminder for the reviewers.

  • The bug fix is important enough to be backported? (label: 'backport requested')
  • This PR touches english.txt or translations? Check the guidelines
  • This PR affects the save game format? (label 'savegame upgrade')
  • This PR affects the GS/AI API? (label 'needs review: Script API')
    • ai_changelog.hpp, game_changelog.hpp need updating.
    • The compatibility wrappers (compat_*.nut) need updating.
  • This PR affects the NewGRF API? (label 'needs review: NewGRF')

Simplify AirportSpec data by storing layout information together in a vector, instead of separate arrays.

This removes manual memory management and separate count members.

The default layouts will be copied instead of always referring to the originals.
src/newgrf.cpp Outdated
Comment on lines 3964 to 3965
tile.ti.x = (int8_t)GB(tile.ti.x, 0, 8);
tile.ti.y = (int8_t)GB(tile.ti.y, 0, 8);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
tile.ti.x = (int8_t)GB(tile.ti.x, 0, 8);
tile.ti.y = (int8_t)GB(tile.ti.y, 0, 8);
tile.ti.x = static_cast<int8_t>(GB(tile.ti.x, 0, 8));
tile.ti.y = static_cast<int8_t>(GB(tile.ti.y, 0, 8));

Copy link
Member

Choose a reason for hiding this comment

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

I'm confused by what this line is actually doing. Seems like just doing tile.ti.x & 0xFF ?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's converting an unsigned 8 bit value to a signed 8 bit value.. This allows the airport layout to "check" for clear tiles outside of the north edges.

Comment on lines +3941 to +3944
/* Convert terminator to our own. */
tile.ti.x = -0x80;
tile.ti.y = 0;
tile.gfx = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

Not for this PR, but if the tile table is now a vector or span then the termination sentinel does not need to be saved in OpenTTD's state.

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed, I figured this already touches enough :)

src/newgrf.cpp Outdated
Comment on lines 3964 to 3965
tile.ti.x = (int8_t)GB(tile.ti.x, 0, 8);
tile.ti.y = (int8_t)GB(tile.ti.y, 0, 8);
Copy link
Member

Choose a reason for hiding this comment

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

I'm confused by what this line is actually doing. Seems like just doing tile.ti.x & 0xFF ?

@@ -348,7 +348,7 @@ class BuildAirportWindow : public PickerWindowBase {
for (int i = 0; i < NUM_AIRPORTS; i++) {
const AirportSpec *as = AirportSpec::Get(i);
if (!as->enabled) continue;
for (uint8_t layout = 0; layout < as->num_table; layout++) {
for (uint8_t layout = 0; layout < static_cast<uint8_t>(as->layouts.size()); layout++) {
Copy link
Member

Choose a reason for hiding this comment

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

would it be nicer to change layout to be a size_t, rather than have the cast? Might need some signature changes as well I suppose

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 lead to a bit of a rabbit hole, when there's no need for for a size_t field further down.

@PeterN PeterN enabled auto-merge (squash) May 2, 2024 11:23
@PeterN PeterN merged commit cf96d49 into OpenTTD:master May 2, 2024
15 checks passed
@PeterN PeterN deleted the airport-layouts branch May 2, 2024 11:40
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.

None yet

3 participants