CleverRaven / Cataclysm-DDA

Cataclysm - Dark Days Ahead. A turn-based survival game set in a post-apocalyptic world.
http://cataclysmdda.org
Other
10.62k stars 4.17k forks source link

map: refactor map class to get rid of optional z-levels #41758

Closed mlangsdorf closed 1 year ago

mlangsdorf commented 4 years ago

Summary

the map class and associated classes needs to be refactored. Here's my proposal:

level_cache becomes a class and gets setter and getter functions for all members which are now private. There may be a child class called fake_level_cache used by the remote_map class (see below) which ignores or simplifies some caching operations.

map and tinymap change quite a bit:

There may need to be a 4th class, map_slice that gets returned instead of a remote_map when the requested overmap tile is already in the reality bubble. Or perhaps the remote_map loader will detect that condition and use different caching functions. There needs to be a way to edit overmap tiles within the reality bubble (using map local co-ordinates) but have the reality bubble's caches get updated.

This refactoring is going to conflict a lot with #41693 and I don't intend to work on coding it until 41693 is mostly merged.

In the meantime, I invite critique of this plan.

Details

tinymap and remote_map

tinymap is currently a child class of map. It is used exclusively to load a single overmap tile's worth of submaps (on a single z-level), perform some inspection or alteration of those submaps, and possibly save the results.

tinymap benefits:

tinymap has some problems currently:

remote_map is intended to fix these problems while keeping the benefits of tinymap.

jbytheway commented 4 years ago

I agree with most of this, but I'm not convinced there's much benefit to making level_cache access go via getters and setters. I think this is coming at it from the wrong angle. Caches work well when they are an implementation detail of some class which presents a higher-level interface. Methods on that class should update the cache appropriately, and clients of that class shouldn't need to care that the cache exists. That's not the same as protecting access to the cache data itself.

To put it another way, if the proposed getters and setters are doing nothing more than getting or setting values in level_cache then they serve no purpose; it's just a more roundabout way to achieve the same end. If the getters and setters are needed to maintain class invariants, then they are useful.

Zireael07 commented 4 years ago

+100 to having an explicitly named reality_bubble class.

mlangsdorf commented 4 years ago

I haven't worked through the code yet, but I think that remote_map is going to use base_map calls that interact with the cache but that won't need to update the cache. So yes, this is about maintaining class invariance.

I also think the cache is better served by being a black box to the map classes, but I concede that's a more abstract point.

jbytheway commented 4 years ago

I think I was imagining that the base_map calls would be virtual and reality_bubble would override those functions to add the cache manipulations needed. In other words, putting the polymorphism at the map interface rather than the cache interface, as you seem to be imagining.

Of course, I haven't worked through any code either, so I don't know which approach would be cleaner.

github-actions[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. Please do not bump or comment on this issue unless you are actively working on it. Stale issues, and stale issues that are closed are still considered.