Proper encounter handling is now complete.
Of course, if I didn't have a computer job and dry eye syndrome, this would've been finished 3 weeks ago. I always feel my eyes "battery" and how much "text reading tolerance" I have left at any given moment. I wish I could get an e-paper screen just for coding.
Anyway...
The new encounter system was lacking two things:
1) encounter "big map" radius management/selection (completed a week earlier)
2) deleting assets after you exit the encounter
2) proved to be more of a problem than initially anticipated. Deleting "encounter people" and "encounter objects (like shelves)" was easy - just cutting off the tail of the arrays containing them, because the encounter always added them at the end. Voila. That took about 7% of my time.
The other 93% was taken by ITEMS. I couldn't just do the same with items. Picture this:
Player enters encounter.
Two NPCs are spawned.
Player kills them, and takes ONE pistol from their corpses.
Player exits encounter.
Item array is shortened to previous size.
Player discovers that his pistol has vanished, because it was part of "encounter items", and the inventory slot references NOTHING.
So I put a tracker on each item, to be able to see which person it belongs to. With this knowledge, I could go through "encounter items" when encounter is exited, and delete those that DO NOT BELONG TO A PERSON (or corpse) THAT EXISTS.
This presented challenges, as the item equipment&generation routines couldn't discern between inventory of objects(shelves,etc) and NPCs, and also they didn't return anything if the equip action failed (as it was previously considered a non-critical, quit-and-whatever, error), so a new "datapath" had to be laid through these routines.
Then I realized that item deletion wasn't actually implemented. To do it the traditional way, by shifting the array by one element onto their former space, would require me to go through each container and person and reindex references in their entire inventory for each single item deletion.
This broke my brain for a while, until I realized that there is a simpler approach - just "zero out" the item, and modify the AddItemToGame() function so that it doesn't append new items at the end of array. First, it scans the entire item list for a "zeroed out" item, and sees if the new item can take that spot.
And so, finally, it works.
Goodbye, encounter system. Hello, random new task which is not yet chosen out of many.
