From c3bb9bbd4f15db7f8399bc9900d42f80c3bb447a Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sat, 13 May 2023 09:59:15 +0300 Subject: [PATCH 28/28] Introduce actres_activity_result() Turn action_get_activity() to wrapper macro that uses it. actres_activity_result() is quick table lookup whereas action_get_activity() was "if ... else if ... else if .." See osdn #48029 Signed-off-by: Marko Lindqvist --- common/actions.c | 43 +------------- common/actions.h | 8 ++- common/actres.c | 149 ++++++++++++++++++++++++++--------------------- common/actres.h | 2 + 4 files changed, 90 insertions(+), 112 deletions(-) diff --git a/common/actions.c b/common/actions.c index 980705e90d..75f2753b35 100644 --- a/common/actions.c +++ b/common/actions.c @@ -2112,47 +2112,6 @@ int action_get_role(const struct action *paction) return paction->id + L_LAST; } -/**********************************************************************//** - Returns the unit activity this action may cause or ACTIVITY_LAST if the - action doesn't result in a unit activity. -**************************************************************************/ -enum unit_activity action_get_activity(const struct action *paction) -{ - fc_assert_msg(AAK_UNIT == action_get_actor_kind(paction), - "Action %s isn't performed by a unit", - action_rule_name(paction)); - - if (action_has_result(paction, ACTRES_FORTIFY)) { - return ACTIVITY_FORTIFYING; - } else if (action_has_result(paction, ACTRES_BASE)) { - return ACTIVITY_BASE; - } else if (action_has_result(paction, ACTRES_ROAD)) { - return ACTIVITY_GEN_ROAD; - } else if (action_has_result(paction, ACTRES_PILLAGE)) { - return ACTIVITY_PILLAGE; - } else if (action_has_result(paction, ACTRES_CLEAN)) { - return ACTIVITY_CLEAN; - } else if (action_has_result(paction, ACTRES_CLEAN_POLLUTION)) { - return ACTIVITY_POLLUTION; - } else if (action_has_result(paction, ACTRES_CLEAN_FALLOUT)) { - return ACTIVITY_FALLOUT; - } else if (action_has_result(paction, ACTRES_TRANSFORM_TERRAIN)) { - return ACTIVITY_TRANSFORM; - } else if (action_has_result(paction, ACTRES_CONVERT)) { - return ACTIVITY_CONVERT; - } else if (action_has_result(paction, ACTRES_PLANT)) { - return ACTIVITY_PLANT; - } else if (action_has_result(paction, ACTRES_MINE)) { - return ACTIVITY_MINE; - } else if (action_has_result(paction, ACTRES_CULTIVATE)) { - return ACTIVITY_CULTIVATE; - } else if (action_has_result(paction, ACTRES_IRRIGATE)) { - return ACTIVITY_IRRIGATE; - } else { - return ACTIVITY_LAST; - } -} - /**********************************************************************//** Returns the unit activity time (work) this action takes (requires) or ACT_TIME_INSTANTANEOUS if the action happens at once. @@ -2164,7 +2123,7 @@ int action_get_act_time(const struct action *paction, const struct tile *tgt_tile, const struct extra_type *tgt_extra) { - enum unit_activity pactivity = action_get_activity(paction); + enum unit_activity pactivity = actres_activity_result(paction->result); if (pactivity == ACTIVITY_LAST) { /* Happens instantaneously, not at turn change. */ diff --git a/common/actions.h b/common/actions.h index 1b1821faef..8f980cb95d 100644 --- a/common/actions.h +++ b/common/actions.h @@ -460,7 +460,7 @@ struct action_enabler { \ action_iterate(_act_id_) { \ struct action *_paction_ = action_by_number(_act_id_); \ - if (action_get_activity(_paction_) != _activity_) { \ + if (actres_activity_result(_paction_->result) != _activity_) { \ continue; \ } @@ -647,7 +647,8 @@ bool action_requires_details(const struct action *paction); int action_get_act_time(const struct action *paction, const struct unit *actor_unit, const struct tile *tgt_tile, - const struct extra_type *tgt_extra); + const struct extra_type *tgt_extra) \ + fc__attribute((nonnull (1))); #define action_id_get_act_time(act_id, actor_unit, tgt_tile, tgt_extra) \ action_get_act_time(action_by_number(act_id), \ actor_unit, tgt_tile, tgt_extra) @@ -676,7 +677,8 @@ int action_get_role(const struct action *paction); #define action_id_get_role(act_id) \ action_get_role(action_by_number(act_id)) -enum unit_activity action_get_activity(const struct action *paction); +#define action_get_activity(_pact_) \ + actres_activity_result(_pact_->result) #define action_id_get_activity(act_id) \ action_get_activity(action_by_number(act_id)) diff --git a/common/actres.c b/common/actres.c index e4e625edda..cab162e4a8 100644 --- a/common/actres.c +++ b/common/actres.c @@ -32,139 +32,139 @@ static struct actres act_results[ACTRES_LAST] = { { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_ESTABLISH_EMBASSY */ - FALSE}, + FALSE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_SPY_INVESTIGATE_CITY */ - TRUE }, + TRUE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_DIPLOMATIC, /* ACTRES_SPY_POISON */ - TRUE }, + TRUE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_DIPLOMATIC, /* ACTRES_SPY_STEAL_GOLD */ - TRUE}, + TRUE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_DIPLOMATIC, /* ACTRES_SPY_SABOTAGE_CITY */ - TRUE }, + TRUE, ACTIVITY_LAST }, { ACT_TGT_COMPL_MANDATORY, ABK_DIPLOMATIC, /* ACTRES_SPY_TARGETED_SABOTAGE_CITY */ - TRUE }, + TRUE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_DIPLOMATIC, /* ACTRES_SPY_SABOTAGE_CITY_PRODUCTION */ - TRUE }, + TRUE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_DIPLOMATIC, /* ACTRES_SPY_STEAL_TECH */ - TRUE }, + TRUE, ACTIVITY_LAST }, { ACT_TGT_COMPL_MANDATORY, ABK_DIPLOMATIC, /* ACTRES_SPY_TARGETED_STEAL_TECH */ - TRUE }, + TRUE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_DIPLOMATIC, /* ACTRES_SPY_INCITE_CITY */ - TRUE }, + TRUE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_TRADE_ROUTE */ - FALSE }, + FALSE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_MARKETPLACE */ - FALSE }, + FALSE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_HELP_WONDER */ - FALSE }, + FALSE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_DIPLOMATIC, /* ACTRES_SPY_BRIBE_UNIT */ - TRUE }, + TRUE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_DIPLOMATIC, /* ACTRES_SPY_SABOTAGE_UNIT */ - TRUE }, + TRUE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_CAPTURE_UNITS */ - TRUE }, + TRUE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_FOUND_CITY */ - FALSE }, + FALSE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_JOIN_CITY */ - FALSE }, + FALSE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_DIPLOMATIC, /* ACTRES_STEAL_MAPS */ - TRUE }, + TRUE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_BOMBARD */ - TRUE }, + TRUE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_DIPLOMATIC, /* ACTRES_SPY_NUKE */ - TRUE }, + TRUE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_NUKE */ - TRUE }, + TRUE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_NUKE_UNITS */ - TRUE }, + TRUE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_DESTROY_CITY */ - TRUE }, + TRUE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_EXPEL_UNIT */ - TRUE }, + TRUE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_DISBAND_UNIT_RECOVER */ - FALSE }, + FALSE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_DISBAND_UNIT */ - FALSE }, + FALSE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_HOME_CITY */ - FALSE }, + FALSE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_UPGRADE_UNIT */ - FALSE }, + FALSE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_PARADROP */ - FALSE }, + FALSE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_AIRLIFT */ - FALSE }, + FALSE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_STANDARD, /* ACTRES_ATTACK */ - TRUE }, + TRUE, ACTIVITY_LAST }, { ACT_TGT_COMPL_MANDATORY, ABK_NONE, /* ACTRES_STRIKE_BUILDING */ - TRUE }, + TRUE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_STRIKE_PRODUCTION */ - TRUE }, + TRUE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_CONQUER_CITY */ - TRUE }, + TRUE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_HEAL_UNIT */ - FALSE }, + FALSE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_TRANSFORM_TERRAIN */ - FALSE }, + FALSE, ACTIVITY_TRANSFORM }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_CULTIVATE */ - FALSE }, + FALSE, ACTIVITY_CULTIVATE }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_PLANT */ - FALSE }, + FALSE, ACTIVITY_PLANT }, { ACT_TGT_COMPL_FLEXIBLE, ABK_NONE, /* ACTRES_PILLAGE */ - TRUE }, + TRUE, ACTIVITY_PILLAGE }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_FORTIFY */ - FALSE }, + FALSE, ACTIVITY_FORTIFYING }, { ACT_TGT_COMPL_MANDATORY, ABK_NONE, /* ACTRES_ROAD */ - FALSE }, + FALSE, ACTIVITY_GEN_ROAD }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_CONVERT */ - FALSE }, + FALSE, ACTIVITY_CONVERT }, { ACT_TGT_COMPL_MANDATORY, ABK_NONE, /* ACTRES_BASE */ - FALSE }, + FALSE, ACTIVITY_BASE }, { ACT_TGT_COMPL_MANDATORY, ABK_NONE, /* ACTRES_MINE */ - FALSE }, + FALSE, ACTIVITY_MINE }, { ACT_TGT_COMPL_MANDATORY, ABK_NONE, /* ACTRES_IRRIGATE */ - FALSE }, + FALSE, ACTIVITY_IRRIGATE }, { ACT_TGT_COMPL_FLEXIBLE, ABK_NONE, /* ACTRES_CLEAN_POLLUTION */ - FALSE }, + FALSE, ACTIVITY_POLLUTION }, { ACT_TGT_COMPL_FLEXIBLE, ABK_NONE, /* ACTRES_CLEAN_FALLOUT */ - FALSE }, + FALSE, ACTIVITY_FALLOUT }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_TRANSPORT_DEBOARD */ - FALSE }, + FALSE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_TRANSPORT_UNLOAD */ - FALSE }, + FALSE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_TRANSPORT_DISEMBARK */ - FALSE }, + FALSE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_TRANSPORT_BOARD */ - FALSE }, + FALSE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_TRANSPORT_EMBARK */ - FALSE }, + FALSE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_DIPLOMATIC, /* ACTRES_SPY_SPREAD_PLAGUE */ - TRUE }, + TRUE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_DIPLOMATIC, /* ACTRES_SPY_ATTACK */ - TRUE }, + TRUE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_CONQUER_EXTRAS */ - TRUE }, + TRUE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_HUT_ENTER */ - FALSE }, + FALSE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_HUT_FRIGHTEN */ - FALSE }, + FALSE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_UNIT_MOVE */ - FALSE }, + FALSE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_PARADROP_CONQUER */ - FALSE }, /* TODO: should this be hostile? */ + FALSE, ACTIVITY_LAST }, /* TODO: Should this be hostile? */ { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_HOMELESS */ - FALSE }, + FALSE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_STANDARD, /* ACTRES_WIPE_UNITS */ - TRUE }, + TRUE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_SPY_ESCAPE */ - FALSE }, + FALSE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_TRANSPORT_LOAD */ - FALSE }, + FALSE, ACTIVITY_LAST }, { ACT_TGT_COMPL_FLEXIBLE, ABK_NONE, /* ACTRES_CLEAN */ - FALSE }, + FALSE, ACTIVITY_CLEAN }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_TELEPORT */ - FALSE }, + FALSE, ACTIVITY_LAST }, { ACT_TGT_COMPL_SIMPLE, ABK_NONE, /* ACTRES_ENABLER_CHECK */ - FALSE } + FALSE, ACTIVITY_LAST } }; /*********************************************************************//** @@ -222,6 +222,21 @@ bool actres_is_hostile(enum action_result result) return act_results[result].hostile; } +/**********************************************************************//** + Map actres to an activity that gets started. + + @param result Action result to map to activity + @return Unit activity that the action starts +**************************************************************************/ +enum unit_activity actres_activity_result(enum action_result result) +{ + if (result == ACTRES_NONE) { + return ACTIVITY_LAST; + } + + return act_results[result].activity; +} + /**********************************************************************//** Returns TRUE iff the specified player knows (has seen) the specified tile. diff --git a/common/actres.h b/common/actres.h index 20ca81a124..bf619842ef 100644 --- a/common/actres.h +++ b/common/actres.h @@ -95,6 +95,7 @@ struct actres { enum act_tgt_compl sub_tgt_compl; enum action_battle_kind battle_kind; bool hostile; + enum unit_activity activity; }; void actres_init(void); @@ -103,6 +104,7 @@ void actres_free(void); enum act_tgt_compl actres_target_compl_calc(enum action_result result); enum action_battle_kind actres_get_battle_kind(enum action_result result); bool actres_is_hostile(enum action_result result); +enum unit_activity actres_activity_result(enum action_result result); enum fc_tristate actres_possible(enum action_result result, const struct req_context *actor, -- 2.39.2