Skip to content

Bug fix: Keep generated resource tile state valid across recompute redraws#44

Open
instafluff0 wants to merge 2 commits into
maxpetul:masterfrom
instafluff0:fix-saved-tile-null
Open

Bug fix: Keep generated resource tile state valid across recompute redraws#44
instafluff0 wants to merge 2 commits into
maxpetul:masterfrom
instafluff0:fix-saved-tile-null

Conversation

@instafluff0
Copy link
Copy Markdown
Contributor

@instafluff0 instafluff0 commented May 25, 2026

This is a fix for som_rybov's bug report, here, save attached.

In a nutshell, I traced the crash back to Tile_has_colony getting a NULL tile. However patching that (bailing if NULL) fixed the symptom but not the underlying problem.

As best I can tell, the crash is from Trade_Net::recompute_resources receiving a bad tile pointer while we have the temporarily expanded Map.TileCount to include generated resource tiles.

  1. patch_Trade_Net_recompute_resources builds is->resource_tiles.
  2. It saves the real tile count:
is->saved_tile_count = p_bic_data->Map.TileCount;
p_bic_data->Map.TileCount += is->count_resource_tiles;
  1. Trade_Net::recompute_resources loops over 0 .. Map.TileCount - 1.
  2. The patched Map_get_tile_when_recomputing_resources_* functions are supposed to return:
  • real map tile if index < saved_tile_count
  • generated resource tile otherwise
  1. But Tile::has_colony(NULL) means (I think) the first tile lookup returned NULL.

So if a resource recompute happens while saved_tile_count is already active, then saved_tile_count can be overwritten with an already-inflated tile count. Then an artificial tile index is misclassified as a real map index, so the wrapper calls real Map_get_tile with an out-of-range index. That returns NULL.

The saved game moves to the next turn after these changes, and calculated resources appear correct afterward.

Also: I noticed in the course of debugging this we had duplicate define Tile_has_colony entries in civ_prog_objects.csv and removed the 2nd one.

PS - holy moly, the repo is really heating up with PRs. I hope all is well and this isn't all too much too quickly.

bug.zip

@instafluff0 instafluff0 changed the title Keep generated resource tile state valid across recompute redraws Bug fix: Keep generated resource tile state valid across recompute redraws May 25, 2026
@maxpetul
Copy link
Copy Markdown
Owner

Thanks for the fix. This all looks fine except for the deletion of line 24930, why'd you do that? Also seeing this reminds me I wanted to combine all those patch_Map_get_tile... functions. There's no reason to have separate patch functions if they're all the same.

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.

2 participants