From b3e3bc196dcf5c81fa5e5a1228482d48dde48a18 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sat, 28 Jan 2023 17:13:36 +0200 Subject: [PATCH 26/26] Replace action_get_battle_kind() with actres_get_battle_kind() Uses indexed table lookup instead of a switch See osdn #46609 Signed-off-by: Marko Lindqvist --- client/helpdata.c | 4 +- common/actions.c | 35 +---------- common/actions.h | 15 ----- common/actres.c | 144 +++++++++++++++++++++++++--------------------- common/actres.h | 14 +++++ 5 files changed, 98 insertions(+), 114 deletions(-) diff --git a/client/helpdata.c b/client/helpdata.c index 0582f2a44c..60fba38907 100644 --- a/client/helpdata.c +++ b/client/helpdata.c @@ -2481,14 +2481,14 @@ char *helptext_unit(char *buf, size_t bufsz, struct player *pplayer, utype_name_translation(utype)); } - if (action_get_battle_kind(paction) != ABK_NONE) { + if (actres_get_battle_kind(paction->result) != ABK_NONE) { cat_snprintf(buf, bufsz, /* TRANS: The %s is a kind of battle defined in * actions.h. Example: "diplomatic battle". */ _(" %s can lead to a %s against a defender.\n"), BULLET, action_battle_kind_translated_name( - action_get_battle_kind(paction))); + actres_get_battle_kind(paction->result))); } { diff --git a/common/actions.c b/common/actions.c index 65f3f50d8d..eca997bd65 100644 --- a/common/actions.c +++ b/common/actions.c @@ -1757,35 +1757,6 @@ enum action_sub_target_kind action_get_sub_target_kind( return paction->sub_target_kind; } -/**********************************************************************//** - Get the battle kind that can prevent an action. -**************************************************************************/ -enum action_battle_kind action_get_battle_kind(const struct action *pact) -{ - switch (pact->result) { - case ACTRES_ATTACK: - case ACTRES_WIPE_UNITS: - return ABK_STANDARD; - case ACTRES_SPY_ATTACK: - case ACTRES_SPY_POISON: - case ACTRES_SPY_STEAL_GOLD: - case ACTRES_SPY_SABOTAGE_CITY: - case ACTRES_SPY_TARGETED_SABOTAGE_CITY: - case ACTRES_SPY_SABOTAGE_CITY_PRODUCTION: - case ACTRES_SPY_STEAL_TECH: - case ACTRES_SPY_TARGETED_STEAL_TECH: - case ACTRES_SPY_INCITE_CITY: - case ACTRES_SPY_BRIBE_UNIT: - case ACTRES_SPY_SABOTAGE_UNIT: - case ACTRES_STEAL_MAPS: - case ACTRES_SPY_NUKE: - case ACTRES_SPY_SPREAD_PLAGUE: - return ABK_DIPLOMATIC; - default: - return ABK_NONE; - } -} - /**********************************************************************//** Returns TRUE iff the specified action allows the player to provide details in addition to actor and target. Returns FALSE if the action @@ -5529,7 +5500,7 @@ action_prob_battle_then_dice_roll(const struct player *act_player, struct act_prob dice_roll; battle = ACTPROB_CERTAIN; - switch (action_get_battle_kind(paction)) { + switch (actres_get_battle_kind(paction->result)) { case ABK_NONE: /* No pre action battle. */ break; @@ -5539,10 +5510,10 @@ action_prob_battle_then_dice_roll(const struct player *act_player, break; case ABK_STANDARD: /* Not supported here yet. Implement when users appear. */ - fc_assert(action_get_battle_kind(paction) != ABK_STANDARD); + fc_assert(actres_get_battle_kind(paction->result) != ABK_STANDARD); break; case ABK_COUNT: - fc_assert(action_get_battle_kind(paction) != ABK_COUNT); + fc_assert(actres_get_battle_kind(paction->result) != ABK_COUNT); break; } diff --git a/common/actions.h b/common/actions.h index 1d64437db1..3b1bf0165f 100644 --- a/common/actions.h +++ b/common/actions.h @@ -279,19 +279,6 @@ extern "C" { #define MAX_NUM_ACTIONS ACTION_COUNT #define NUM_ACTIONS MAX_NUM_ACTIONS -/* A battle is against a defender that tries to stop the action where the - * defender is in danger. A dice roll without a defender risking anything, - * like the roll controlled by EFT_ACTION_ODDS_PCT, isn't a battle. */ -#define SPECENUM_NAME action_battle_kind -#define SPECENUM_VALUE0 ABK_NONE -#define SPECENUM_VALUE0NAME N_("no battle") -#define SPECENUM_VALUE1 ABK_STANDARD -#define SPECENUM_VALUE1NAME N_("battle") -#define SPECENUM_VALUE2 ABK_DIPLOMATIC -#define SPECENUM_VALUE2NAME N_("diplomatic battle") -#define SPECENUM_COUNT ABK_COUNT -#include "specenum_gen.h" - /* Describes how a unit successfully performing an action will move it. */ #define SPECENUM_NAME moves_actor_kind #define SPECENUM_VALUE0 MAK_STAYS @@ -617,8 +604,6 @@ enum action_sub_target_kind action_get_sub_target_kind( #define action_id_get_sub_target_kind(act_id) \ action_get_sub_target_kind(action_by_number(act_id)) -enum action_battle_kind action_get_battle_kind(const struct action *pact); - int action_number(const struct action *action); #define action_has_result_safe(paction, result) \ diff --git a/common/actres.c b/common/actres.c index b1266ac2c2..dec7f59709 100644 --- a/common/actres.c +++ b/common/actres.c @@ -18,71 +18,71 @@ #include "actres.h" static struct actres act_results[ACTRES_LAST] = { - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_ESTABLISH_EMBASSY */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_SPY_INVESTIGATE_CITY */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_SPY_POISON */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_SPY_STEAL_GOLD */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_SPY_SABOTAGE_CITY */ - { ACT_TGT_COMPL_MANDATORY }, /* ACTRES_SPY_TARGETED_SABOTAGE_CITY */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_SPY_SABOTAGE_CITY_PRODUCTION */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_SPY_STEAL_TECH */ - { ACT_TGT_COMPL_MANDATORY }, /* ACTRES_SPY_TARGETED_STEAL_TECH */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_SPY_INCITE_CITY */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_TRADE_ROUTE */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_MARKETPLACE */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_HELP_WONDER */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_SPY_BRIBE_UNIT */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_SPY_SABOTAGE_UNIT */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_CAPTURE_UNITS */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_FOUND_CITY */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_JOIN_CITY */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_STEAL_MAPS */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_BOMBARD */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_SPY_NUKE */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_NUKE */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_NUKE_UNITS */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_DESTROY_CITY */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_EXPEL_UNIT */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_DISBAND_UNIT_RECOVER */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_DISBAND_UNIT */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_HOME_CITY */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_UPGRADE_UNIT */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_PARADROP */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_AIRLIFT */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_ATTACK */ - { ACT_TGT_COMPL_MANDATORY }, /* ACTRES_STRIKE_BUILDING */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_STRIKE_PRODUCTION */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_CONQUER_CITY */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_HEAL_UNIT */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_TRANSFORM_TERRAIN */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_CULTIVATE */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_PLANT */ - { ACT_TGT_COMPL_FLEXIBLE }, /* ACTRES_PILLAGE */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_FORTIFY */ - { ACT_TGT_COMPL_MANDATORY }, /* ACTRES_ROAD */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_CONVERT */ - { ACT_TGT_COMPL_MANDATORY }, /* ACTRES_BASE */ - { ACT_TGT_COMPL_MANDATORY }, /* ACTRES_MINE */ - { ACT_TGT_COMPL_MANDATORY }, /* ACTRES_IRRIGATE */ - { ACT_TGT_COMPL_FLEXIBLE }, /* ACTRES_CLEAN_POLLUTION */ - { ACT_TGT_COMPL_FLEXIBLE }, /* ACTRES_CLEAN_FALLOUT */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_TRANSPORT_ALIGHT */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_TRANSPORT_UNLOAD */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_TRANSPORT_DISEMBARK */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_TRANSPORT_BOARD */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_TRANSPORT_EMBARK */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_SPY_SPREAD_PLAGUE */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_SPY_ATTACK */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_CONQUER_EXTRAS */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_HUT_ENTER */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_HUT_FRIGHTEN */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_UNIT_MOVE */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_PARADROP_CONQUER */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_HOMELESS */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_WIPE_UNITS */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_SPY_ESCAPE */ - { ACT_TGT_COMPL_SIMPLE }, /* ACTRES_TRANSPORT_LOAD */ - { ACT_TGT_COMPL_FLEXIBLE } /* ACTRES_CLEAN */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_ESTABLISH_EMBASSY */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_SPY_INVESTIGATE_CITY */ + { ACT_TGT_COMPL_SIMPLE, ABK_DIPLOMATIC }, /* ACTRES_SPY_POISON */ + { ACT_TGT_COMPL_SIMPLE, ABK_DIPLOMATIC }, /* ACTRES_SPY_STEAL_GOLD */ + { ACT_TGT_COMPL_SIMPLE, ABK_DIPLOMATIC }, /* ACTRES_SPY_SABOTAGE_CITY */ + { ACT_TGT_COMPL_MANDATORY, ABK_DIPLOMATIC }, /* ACTRES_SPY_TARGETED_SABOTAGE_CITY */ + { ACT_TGT_COMPL_SIMPLE, ABK_DIPLOMATIC }, /* ACTRES_SPY_SABOTAGE_CITY_PRODUCTION */ + { ACT_TGT_COMPL_SIMPLE, ABK_DIPLOMATIC }, /* ACTRES_SPY_STEAL_TECH */ + { ACT_TGT_COMPL_MANDATORY, ABK_DIPLOMATIC }, /* ACTRES_SPY_TARGETED_STEAL_TECH */ + { ACT_TGT_COMPL_SIMPLE, ABK_DIPLOMATIC }, /* ACTRES_SPY_INCITE_CITY */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_TRADE_ROUTE */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_MARKETPLACE */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_HELP_WONDER */ + { ACT_TGT_COMPL_SIMPLE, ABK_DIPLOMATIC }, /* ACTRES_SPY_BRIBE_UNIT */ + { ACT_TGT_COMPL_SIMPLE, ABK_DIPLOMATIC }, /* ACTRES_SPY_SABOTAGE_UNIT */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_CAPTURE_UNITS */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_FOUND_CITY */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_JOIN_CITY */ + { ACT_TGT_COMPL_SIMPLE, ABK_DIPLOMATIC }, /* ACTRES_STEAL_MAPS */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_BOMBARD */ + { ACT_TGT_COMPL_SIMPLE, ABK_DIPLOMATIC }, /* ACTRES_SPY_NUKE */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_NUKE */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_NUKE_UNITS */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_DESTROY_CITY */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_EXPEL_UNIT */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_DISBAND_UNIT_RECOVER */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_DISBAND_UNIT */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_HOME_CITY */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_UPGRADE_UNIT */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_PARADROP */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_AIRLIFT */ + { ACT_TGT_COMPL_SIMPLE, ABK_STANDARD }, /* ACTRES_ATTACK */ + { ACT_TGT_COMPL_MANDATORY, ABK_NONE }, /* ACTRES_STRIKE_BUILDING */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_STRIKE_PRODUCTION */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_CONQUER_CITY */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_HEAL_UNIT */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_TRANSFORM_TERRAIN */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_CULTIVATE */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_PLANT */ + { ACT_TGT_COMPL_FLEXIBLE, ABK_NONE }, /* ACTRES_PILLAGE */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_FORTIFY */ + { ACT_TGT_COMPL_MANDATORY, ABK_NONE }, /* ACTRES_ROAD */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_CONVERT */ + { ACT_TGT_COMPL_MANDATORY, ABK_NONE }, /* ACTRES_BASE */ + { ACT_TGT_COMPL_MANDATORY, ABK_NONE }, /* ACTRES_MINE */ + { ACT_TGT_COMPL_MANDATORY, ABK_NONE }, /* ACTRES_IRRIGATE */ + { ACT_TGT_COMPL_FLEXIBLE, ABK_NONE }, /* ACTRES_CLEAN_POLLUTION */ + { ACT_TGT_COMPL_FLEXIBLE, ABK_NONE }, /* ACTRES_CLEAN_FALLOUT */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_TRANSPORT_ALIGHT */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_TRANSPORT_UNLOAD */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_TRANSPORT_DISEMBARK */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_TRANSPORT_BOARD */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_TRANSPORT_EMBARK */ + { ACT_TGT_COMPL_SIMPLE, ABK_DIPLOMATIC }, /* ACTRES_SPY_SPREAD_PLAGUE */ + { ACT_TGT_COMPL_SIMPLE, ABK_DIPLOMATIC }, /* ACTRES_SPY_ATTACK */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_CONQUER_EXTRAS */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_HUT_ENTER */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_HUT_FRIGHTEN */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_UNIT_MOVE */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_PARADROP_CONQUER */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_HOMELESS */ + { ACT_TGT_COMPL_SIMPLE, ABK_STANDARD }, /* ACTRES_WIPE_UNITS */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_SPY_ESCAPE */ + { ACT_TGT_COMPL_SIMPLE, ABK_NONE }, /* ACTRES_TRANSPORT_LOAD */ + { ACT_TGT_COMPL_FLEXIBLE, ABK_NONE } /* ACTRES_CLEAN */ }; /*********************************************************************//** @@ -113,3 +113,17 @@ enum act_tgt_compl actres_target_compl_calc(enum action_result result) return act_results[result].sub_tgt_compl; } + +/**********************************************************************//** + Get the battle kind that can prevent an action. +**************************************************************************/ +enum action_battle_kind actres_get_battle_kind(enum action_result result) +{ + if (result == ACTRES_NONE) { + return ABK_NONE; + } + + fc_assert_ret_val(action_result_is_valid(result), ABK_NONE); + + return act_results[result].battle_kind; +} diff --git a/common/actres.h b/common/actres.h index 4b38c36516..6c2c539be0 100644 --- a/common/actres.h +++ b/common/actres.h @@ -76,16 +76,30 @@ extern "C" { #define SPECENUM_VALUE2NAME N_("mandatory") #include "specenum_gen.h" +/* A battle is against a defender that tries to stop the action where the + * defender is in danger. A dice roll without a defender risking anything, + * like the roll controlled by EFT_ACTION_ODDS_PCT, isn't a battle. */ +#define SPECENUM_NAME action_battle_kind +#define SPECENUM_VALUE0 ABK_NONE +#define SPECENUM_VALUE0NAME N_("no battle") +#define SPECENUM_VALUE1 ABK_STANDARD +#define SPECENUM_VALUE1NAME N_("battle") +#define SPECENUM_VALUE2 ABK_DIPLOMATIC +#define SPECENUM_VALUE2NAME N_("diplomatic battle") +#define SPECENUM_COUNT ABK_COUNT +#include "specenum_gen.h" struct actres { enum act_tgt_compl sub_tgt_compl; + enum action_battle_kind battle_kind; }; void actres_init(void); 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); #ifdef __cplusplus } -- 2.39.0