Code: Select all
// Declare a type for the key (very unsure about the '*', does the visibility map need also to be a pointer, or should the Conditions be passed by value or with &?)
typedef std::tuple<int, Universe::EmpireObjectVisibilityMap, Conditions::Conditions*, Conditions::Conditions*> targettingInfo;
// Probably I need to define a constructor for that tuple and overload operator<.
// The cache for targets could be a map:
std::map<targettingInfo, Condition::ObjectSet> targets_cache;
// While bout-invariant stuff is not considered, clear the targets_cache on every bout
// Instantiate targetting info from source, visibility and targetting conditions of species and weapons
auto targetting_info(attacker, combat_state.combat_info.empire_object_visibility, species_targetting_condition, weapon.combat_targets);
// Looking up the cache
Condition::ObjectSet targets, rejected_targets;
auto search = targets_cache.find(targetting_info);
if (search != targets_cache.end())
targets = targets_cache[targetting_info];
else { // Not found, so evaluate targets and cache them up
AddAllObjectsSet(combat_state.combat_info.objects, targets); // Adds to targets all objects in combat_info (i.e. planets, ships and fighters in the system)
ScriptingContext context(attacker, combat_state.combat_info.empire_object_visibility);
// apply species targeting condition and then weapon targeting condition
species_targetting_condition->Eval(context, targets, rejected_targets, Condition::MATCHES);
weapon.combat_targets->Eval(context, targets, rejected_targets, Condition::MATCHES);
// Caching up the set of targets corresponding to a given key.
targets_cache[targetting_info] = targets;
}
// Then the code for randomly selecting a target for the current weapon among the valid targets.