dymanoid / MoreVehicles

A mod for the Cities: Skylines game. Increases the maximum allowed number of spawned vehicles.
MIT License
3 stars 2 forks source link

Useful feature #8

Open manusoftar opened 3 years ago

manusoftar commented 3 years ago

It would be really useful if there was an option (perhaps on the options menu) of this mod that let you export your city with the Vanilla gamesave format in case that you decided to remove this mod but not loose your city.

I reviewed the code and honestly I don't see any method regarding saving y/o loading of game saves or I would have tried to do this on my own.

You say on the Workshop that the mode changes the format of the gamesave, if you can change it from the original format to whatever you changed it into, I'm pretty sure there should be a way back.

And there seems to be many people asking for this kind of feature or at leas a standalone app that does the job of reformatting the gamesave back to vanilla.

dymanoid commented 3 years ago

@algernon-A This might be possible but requires investigation.

The technical part is 'piece of cake'. The game's vehicle serializing method has to be patched. The mod doesn't patch it now, so it just writes the whole content of the vehicles arrays. When the mod is active, those arrays are enlarged. To ensure vanilla save compatibility, the serializing method must write exactly that number of vehicle records as expected by the vanilla game (the original array sizes).

To investigate: What to do with all the vehicle ID references which will become unavailable? E.g. if a citizen happens to have a vehicle with ID 65534 (got that while the mod was active) which will become invalid - how would the game react on this? Will it crash? Will it just wipe out the invalid vehicle? If the game gracefully handles invalid IDs - it's all done, easy to implement. If the game cannot cope with them - well, additional clean-up logic is required.

manusoftar commented 3 years ago

I understand what you say, and as far as I see it, there is only one way to know what will happen with those vehicle's id... just trying, making a new city, implementing somehow a debug mode on the mod as to be able to know when the game starts to spawn vehicles beyond the vanilla limits or with id's that exceed the vanilla id limit, so there's when you can try to patch the vehicle serializing and see what happens.

Other thing I can think off is to manage all those vehicles that exceed the vanilla limits outside the gamesave, in a mod's specific savefile that would store that information so you don't have to actually modify the format of the savegame file and thus get it unloadable without the mod being activated.

algernon-A commented 3 years ago

Not keen on a separate data savefile, as that would only introduce risk and complexity for no benefit. For example, the separate file would be completely useless without the original save, and then you introduce issues - how do you properly track state? What happens when the savefile is moved, renamed or deleted? What happens when a data file for one save gets loaded with another? And so on.

A better way would be to preserve the vanilla data savegame format, but then append the additional data within the savegame as 'normal' mod savegame data. The 81 tiles mod uses a similar technique, preserving vanilla data structures and sizes in the savegame and then effectively adding the additional data (from savegame mod data) at a specific time in the loading process.

I'll do some investigation to see what seems the most feasible approach.

GeniusYe commented 2 years ago

@dymanoid dymanoid thank you for explaining how it worked. so if the ID is the issue, will it be safe to, just wipe all the vehicles (or all the vehicles that is > vanilla limit) and start the game?

algernon-A commented 2 years ago

Investigation shows that the game does not always handle valid IDs gracefully - and why should it? Vehicle IDs out of range should never happen, and introducing checks just slows down performance-critical code.

Removing vehicles requires finding and replacing all references to those vehicles in various in-game data structures. Any associated PathUnits will also need to be identified and released, as that doesn't happen automatically.

There's also an issue with trailer units where there will be cases where some trailers are part of the 'cleared' group and others not. Presumably the entire set will need to be deleted in those cases.

So, this is far more complex than just wiping the individual vehicles.

EDIT: Still ultimately achieveable, but a lot of work is required; this isn't a quick fix.

GeniusYe commented 2 years ago

Probably I misunderstood again, I thought the limit is simply the car instances, the ones spawn on the street, and won't have any if all cars are wiped out. Or is it actually mean, say a home has a car, that car is already pre-allocated an ID even if it is not spawn. And in which case wiping all the cars might interfere with game's logic determining which family owns cars.

Am I understanding correctly or it is totally off course.

algernon-A commented 2 years ago

It is car instances, yes (not just cars, though - all active vehicles, including cargo vehicles, public transport, aircraft, ships, etc. Fun fact: the Natural Disasters meteor is ALSO a 'vehicle'!). But those active instances are referred to elsewhere in the code, and simply removing the vehicles without clearing up those references causes the game to break (with 'index out of bounds' errors every time a reference to the expanded array is encountered.

The primary references are in CitizenUnits, Citizens, and in pathfinding data. Apart from clearing up the vehicle reference itself, there's also the question of what to do with the referring data strucure as well - if a vehicle is removed, what should happen to the CitizenUnits assigned to that vehicle? What about the occupants of that vehicle? Where should they be 'transported' to (simply returning them to their home building won't work in all cases due to transience)? And so on - all questions that need to be answered in design before a single line of code is written.

I'm still investigating this, as it's going far deeper than I thought it would be, so I don't have any concrete answers yet, and I'm not even at a place (yet) where I feel confident that I've got all the questions.