From 766fc890f17646fb30748b18bbec727b599aabb2 Mon Sep 17 00:00:00 2001 From: Ihnatus Date: Sat, 8 Apr 2023 10:33:05 +0300 Subject: [PATCH 33/33] Add utility function get_effect_expected_value() See osdn #47671 --- common/effects.c | 28 ++++++++++++++++++++++++++++ common/effects.h | 15 +++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/common/effects.c b/common/effects.c index 502a3a5f91..71b2ac020f 100644 --- a/common/effects.c +++ b/common/effects.c @@ -783,6 +783,34 @@ int get_target_bonus_effects(struct effect_list *plist, return bonus; } +/**********************************************************************//** + Returns the expected value of the effect of given type for given context, + calculating value weighted with probability for each individual effect + with given callback using arbitrary additional data passed to it. + + The way of using multipliers is left on the callback. +**************************************************************************/ +double get_effect_expected_value(const struct req_context *context, + const struct player *other_player, + enum effect_type effect_type, + eft_value_filter_cb weighter, + void *data, int n_data) +{ + double sum = 0.; + + fc_assert_ret_val(weighter, 0.); + if (context == NULL) { + context = req_context_empty(); + } + + /* Loop over all effects of this type. */ + effect_list_iterate(get_effects(effect_type), peffect) { + sum += weighter(peffect, context, other_player, data, n_data); + } effect_list_iterate_end; + + return sum; +} + /**********************************************************************//** Returns the effect bonus for the whole world. **************************************************************************/ diff --git a/common/effects.h b/common/effects.h index 33c14f3d23..5b21626c5e 100644 --- a/common/effects.h +++ b/common/effects.h @@ -381,6 +381,15 @@ struct effect { } rulesave; }; +/* A callback type that takes an individual effect and a context for it + * and tells its weighted (by probability or something) value. + */ +typedef double + (*eft_value_filter_cb)(const struct effect *eft, + const struct req_context *context, + const struct player *other_player, + void *data, int n_data); + /* An effect_list is a list of effects. */ #define SPECLIST_TAG effect #define SPECLIST_TYPE struct effect @@ -480,6 +489,12 @@ int get_target_bonus_effects(struct effect_list *plist, const struct req_context *context, const struct player *other_player, enum effect_type effect_type); +double get_effect_expected_value(const struct req_context *context, + const struct player *other_player, + enum effect_type effect_type, + eft_value_filter_cb weighter, + void *data, int n_data) +fc__attribute((nonnull (4))); bool building_has_effect(const struct impr_type *pimprove, enum effect_type effect_type); -- 2.39.2