From b630ca4cd2dc7d15f35efb4c5004c5ca88c43443 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Tue, 18 Jul 2023 06:39:10 +0300 Subject: [PATCH 12/12] Make action_by_activity_iterate() to go over cached list No point in iterating over all the actions, and then to pick just one or two that match the result. See osdn #48411 Signed-off-by: Marko Lindqvist --- common/actions.c | 26 +++++++++++++++++++++++++- common/actions.h | 11 ++++------- server/advisors/autosettlers.c | 3 ++- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/common/actions.c b/common/actions.c index be1532b8db..52ea9344bd 100644 --- a/common/actions.c +++ b/common/actions.c @@ -144,6 +144,7 @@ FC_STATIC_ASSERT(MAP_DISTANCE_MAX <= ACTION_DISTANCE_LAST_NON_SIGNAL, action_range_can_not_cover_the_whole_map); static struct action_list *actlist_by_result[ACTRES_LAST]; +static struct action_list *actlist_by_activity[ACTIVITY_LAST]; /**********************************************************************//** Returns a new array of alternative action enabler contradictions. Only @@ -1463,6 +1464,9 @@ void actions_init(void) for (i = 0; i < ACTRES_LAST; i++) { actlist_by_result[i] = action_list_new(); } + for (i = 0; i < ACTIVITY_LAST; i++) { + actlist_by_activity[i] = action_list_new(); + } /* Hard code the actions */ hard_code_actions(); @@ -1565,6 +1569,10 @@ void actions_free(void) action_list_destroy(actlist_by_result[i]); actlist_by_result[i] = NULL; } + for (i = 0; i < ACTIVITY_LAST; i++) { + action_list_destroy(actlist_by_activity[i]); + actlist_by_activity[i] = NULL; + } } /**********************************************************************//** @@ -1611,7 +1619,13 @@ static struct action *action_new(action_id id, action->result = result; if (result != ACTRES_LAST) { + enum unit_activity act = action_get_activity(action); + action_list_append(actlist_by_result[result], action); + + if (act != ACTIVITY_LAST) { + action_list_append(actlist_by_activity[act], action); + } } /* Not set here */ @@ -9294,7 +9308,7 @@ const char *action_target_kind_help(enum action_target_kind kind) } /************************************************************************//** - Returns action id list by result. + Returns action list by result. ****************************************************************************/ struct action_list *action_list_by_result(enum action_result result) { @@ -9302,3 +9316,13 @@ struct action_list *action_list_by_result(enum action_result result) return actlist_by_result[result]; } + +/************************************************************************//** + Returns action list by activity. +****************************************************************************/ +struct action_list *action_list_by_activity(enum unit_activity activity) +{ + fc_assert(activity < ACTIVITY_LAST); + + return actlist_by_activity[activity]; +} diff --git a/common/actions.h b/common/actions.h index 6c2a5b3f27..d97e5e962f 100644 --- a/common/actions.h +++ b/common/actions.h @@ -482,6 +482,7 @@ struct action_enabler #define action_list_iterate_end LIST_ITERATE_END struct action_list *action_list_by_result(enum action_result result); +struct action_list *action_list_by_activity(enum unit_activity activity); #define action_by_result_iterate(_paction_, _result_) \ { \ @@ -491,16 +492,12 @@ struct action_list *action_list_by_result(enum action_result result); } action_list_iterate_end; \ } -#define action_by_activity_iterate(_paction_, _act_id_, _activity_) \ +#define action_by_activity_iterate(_paction_, _activity_) \ { \ - action_iterate(_act_id_) { \ - struct action *_paction_ = action_by_number(_act_id_); \ - if (action_get_activity(_paction_) != _activity_) { \ - continue; \ - } + action_list_iterate(action_list_by_activity(_activity_), _paction_) { #define action_by_activity_iterate_end \ - } action_iterate_end; \ + } action_list_iterate_end; \ } #define action_array_iterate(_act_list_, _act_id_) \ diff --git a/server/advisors/autosettlers.c b/server/advisors/autosettlers.c index 4f986fa3a0..36a6604af0 100644 --- a/server/advisors/autosettlers.c +++ b/server/advisors/autosettlers.c @@ -1282,7 +1282,7 @@ bool auto_settlers_speculate_can_act_at(const struct unit *punit, struct extra_type *target, const struct tile *ptile) { - action_by_activity_iterate(paction, act_id, activity) { + action_by_activity_iterate(paction, activity) { if (action_get_actor_kind(paction) != AAK_UNIT) { /* Not relevant. */ continue; @@ -1330,5 +1330,6 @@ bool auto_settlers_speculate_can_act_at(const struct unit *punit, } action_by_activity_iterate_end; log_debug("No action found for activity %d", activity); + return FALSE; } -- 2.40.1