Schaf-Unschaf / BountiesExpanded

2 stars 0 forks source link

Conversion to utility mod #11

Open jaghaimo opened 3 years ago

jaghaimo commented 3 years ago

Utility mods can be safely disabled mid-save. To make this utility mod, we need to remove all mod instances from save game. This will mean no progress is saved, though, so an alternative approach will be needed to track the progress. Currently, progress is tracked via memory map (HVBs) and persistent objects (in IntelManager).

Removing mod objects

Easy part, use two methods from BaseModPlugin: :beforeGameSave()to remove bounty managers added, and all intel classes, andafterGameSave()` to add back bounty manager. This will lose track of existing bounties, and will regenerate bounties, which is a bad (and unacceptable) user experience.

Keeping track of progress (persistent cross-save bounties)

Two approaches are possible here - basic and complete.

Basic

Each bounty will store the current random seed at time of its creation. Each bounty will implement a common interface that will have one method generate(seed) and will know how to create itself. Internally, each bounty will be created in the same way as currently, but will store the current random number generator seed, set it to what it was at time of generation, and set it back after it is done. We are using pseudo randomness of RNG to create exactly the same bounty as it was before. It will not keep track of partial battles (e.g. say remnants chipped the bounty a bit, regenerated bounty will be once again fresh).

We are only storing bounty type, time it was originally created, and it's seed in a memory map. All are primitives (String, Integer) and having it in memory when mod is disabled will not affect anything. Re-enabling mod at a later date will bring those bounties back, but they might have timed out by then, at which point we will just create new ones.

Before game save serialize all active bounties to memory map, remove all mod objects. After game save unserialize and recreate bounties.

Complete

Similar to the above. Make all bounties serializable (say to json) and unserializable. Store json as string in memory map. This approach retains the current state of the bounty fleet.

jaghaimo commented 3 years ago

Serializable objects that are saved via some form of repository utility class in memory map as primitive (string) representation (JSON format) of actual objects. That way, no reference to mod is in a save game. Then use repository to fetch all objects that should be present, deserialize and recreate. Because this is mod is special, we can cheat and regenerate bounties using the same RNG process they were created initially (via saving RNG seed).