OK, I think I figured out what's causing the crash bug with generating new FleetMemberAPI objects and attaching them to CampaignFleetAPI objects. Basically, there's a discrepancy between the stored copy of the CampaignFleetAPI that's being stored as the "before state" and the "after state", and checks against the two are causing the crash.
It's therefore necessary to not work with a stored copy, but to manipulate the Object data in the battle context and then work from that.
I can see how that's going to cause issues; amongst other things, there's storage of things like getAllEverDeployed(), which is one of the areas that's going to cause the runtime error.
Not quite sure how to work forwards from that- tbh, I'm not sure that trying to hold that data for reference later is a good idea in general, as it's probably quite cheap to simply update within the battle context and then read the raw output when returning to the Dialog state- but it's apparent that if these things don't come into conflict, then we won't see crashes when new FleetMemberAPI objects are created.
In case the above seems a little confused, here's kind of what I see as the sequence problem:
1. Enter EncounterDialogAPI state.
2. Fleet copy for future reference appears to get created there.
3. Combat begins, FleetMemberAPI objects change state (disabled, destroyed, or in this case, added).
4. Check against copy is made to determine further stages of Dialog. Created FleetMemberAPI members causes difference between stored state and new state --> crash
What I think it should do:
1. Enter EncounterDialogAPI state.
2. Combat begins.
3. ShipAPI objects change state (become isHulk, get destroyed, get added).
4. FleetMemberAPI objects change state (disabled, destroyed, or in this case, added). CampaignFleetAPI object's state gets changed immediately.
5. Dialog returns, using the current CampaignFleetAPI object and relevant FleetMemberAPI objects. No reference made to out-of-date copy data ensues, therefore no crash results.
I am fairly certain that the above can work correctly, but I haven't quite gotten through all of the other wrinkles of writing up a new Fleet Encounter system yet to test this thoroughly. One of the issues I haven't explored is whether there's a way to set the Deployed state for created FleetMemberAPI objects, etc. so that when it returns it's all matching up again.