Focussed Unit Lists

LPlate2

Warlord
Joined
Dec 27, 2018
Messages
299
Hi,

I want to avoid cycling thru all of a player’s units in python when checking for some of my triggers, where I know there’s only a very limited number of units that will ever trigger the effect, e.g. Great People.
It seems to me that this would involve creating an array in the dll, which is a trimmed version of the full unit list based on a certain criteria. This trimmed list would then be used in the python calls.

Is anyone aware of a mod which has created such trimmed unit lists (or alternatively a trimmed plot list to avoid cycling thru all of the plots on a map)?
Are there examples of what I’m talking about already in the basic vanilla/FFH dll?
Anyone have any suggestions about how I’d go about coding this?
 
In the DLL, such loops are usually unproblematic. So perhaps it would suffice to add e.g. a function CvPlayer::getGPUnits that computes a list of GP units on the fly, and expose it to Python. Returning a list to Python seems a little complicated; I think CyPlayer::getCivicUpkeep is an example. Edit: Nope, that only takes a list from Python. :(

For a cache of GP units, I'd look for and mostly copy all uses of CvPlayer::m_units. That's a FFreeListTrashArray though, which isn't suitable for storing redundant data. Since there will only be a few GP per player, an std::vector should be fine. (Not sure if a vector<IDInfo> would be easier to work with or a vector<CvUnit*>.) Or an actual array, but a vector saves you the trouble of writing initialization and cleanup code. For example, CvTeam::m_aeRevealedBonuses is a vector of force-revealed resource types. Edit: Or a CLinkList like CvPlot::m_units.

"Rise of Mankind - A New Dawn" has added a bunch of caches to CvPlayer, e.g. a can-construct cache, which they've implemented as a boolean array (CvPlayer::m_bCanConstruct) that says for each type of building whether the player can currently construct that building.
 
Last edited:
Are you experiencing concrete performance issues? Premature optimisation is a common pitfall, like f1rpo mentions cycling through all units is very, very cheap and it's almost never worth it to overthink it. Incidentally, I have run a lot of automated testing on assembling and filtering lists of all sorts of entities including units and even all of them run crazy fast. My rule of thumb is to only think about this when you observe a problem, or when you write code that could go into quadratic complexity.

If you try to solve a possibly non-existant problem like this you only create lots of work for yourself that is itself error prone and needs to be maintained. So I would err on the side of getting all units and filtering on the fly. On that end, I don't think the performance benefit of doing it in C++ instead of Python is worth it either. Really before you have amounts of units where this would be a concern, your game is probably already unplayable for memory and other engine reasons.
 
Thanks for the advice. No performance issues yet, so it probably is a case of premature optimisation.
I’ll get on with some other tasks on the to do list.
 
Top Bottom