Prevent corrupting last loaded submap. Thanks to wito for finding it. #2381
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
May fix #426 fix #437 and fix #1341.
To reproduce previous issue:
Clear maps, make a new character, immediately save, exit program, restart, and load.
The submap approximately 48 squares SE of the shelter will be completely uniform and featureless.
Also if you backup the save/maps.txt file after saving, then load and save. Comparing the backup and new file will show that the last listed submap is a uniform terrain type, and has no features, even if the previous version of it did.
Big props to wito, he pointed out that there was something funny happening with the last submap in maps.txt, and that the value used was the previous value of tmpter, both of which were instrumental in figuring out what was going wrong.
Post-mortem:
What was happening was that after processing the last submap from maps.txt, the program would check for EOF and not find it, because there was a single newline left. It would then try to read a submap, but every single read would fail. Since none of the variables in the loop were initialized (boo), they would retain their values from the previous iteration, so the x,y,z, turns, etc would match the last submap, the terrain would be the last terrain* read (SE corner of submap), and the radiation level would be the same as the last one read. The submap would be featureless, because the last string_identifier read was "---", which would cause it to exit from feature processing, at which point it would re-write the most recently written submap with a garbage version of itself. The error would then be persisted the next time the map was written to disk.
The fix is simple, check for EOF after trying to read past the end of the current submap and exit the loop if it's encountered.
*On debug builds, it uses the last terrain read, on release builds it gets a 0, which I suspect is the value of radtmp. Other contents of the submap may have caused the LOS-blocking, crash-causing version that has also been reported.