From 17559c4aec486120e1d38dd8309816a8e05c68cc Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Mon, 13 Feb 2023 02:18:43 +0200 Subject: [PATCH 32/33] Add unit class native_bases cache See osdn #46417 Signed-off-by: Marko Lindqvist --- client/helpdata.c | 16 ++++++---------- common/actions.c | 12 ++++-------- common/tile.c | 18 ++++++++---------- common/unittype.c | 9 +++++++++ common/unittype.h | 1 + 5 files changed, 28 insertions(+), 28 deletions(-) diff --git a/client/helpdata.c b/client/helpdata.c index 45931f34ad..02b4364862 100644 --- a/client/helpdata.c +++ b/client/helpdata.c @@ -2940,21 +2940,16 @@ char *helptext_unit(char *buf, size_t bufsz, struct player *pplayer, const char *targets[extra_count()]; int j = 0; - extra_type_by_cause_iterate(EC_BASE, pextra) { - fc_assert_action(pextra->data.base, continue); + /* Extra being native one is a hard requirement */ + extra_type_list_iterate(utype_class(utype)->cache.native_bases, + pextra) { if (!territory_claiming_base(pextra->data.base)) { /* Hard requirement */ continue; } - if (!is_native_extra_to_uclass(pextra, - utype_class(utype))) { - /* Hard requirement */ - continue; - } - targets[j++] = extra_name_translation(pextra); - } extra_type_by_cause_iterate_end; + } extra_type_list_iterate_end; if (j > 0) { struct astring list = ASTRING_INIT; @@ -3713,7 +3708,8 @@ void helptext_extra(char *buf, size_t bufsz, struct player *pplayer, _("Build by issuing a \"road\" order.\n")); } if (is_extra_caused_by(pextra, EC_BASE)) { - fc_assert(pbase); + fc_assert(pbase != NULL); + if (pbase->gui_type == BASE_GUI_OTHER) { cat_snprintf(buf, bufsz, _("Build by issuing a \"build base\" order.\n")); diff --git a/common/actions.c b/common/actions.c index f2c50c0999..9b35a54194 100644 --- a/common/actions.c +++ b/common/actions.c @@ -3390,21 +3390,17 @@ action_actor_utype_hard_reqs_ok_full(const struct action *paction, if (!ignore_third_party) { bool has_target = FALSE; - extra_type_by_cause_iterate(EC_BASE, pextra) { + /* Extra being native one is a hard requirement */ + extra_type_list_iterate(utype_class(actor_unittype)->cache.native_bases, + pextra) { if (!territory_claiming_base(pextra->data.base)) { /* Hard requirement */ continue; } - if (!is_native_extra_to_uclass(pextra, - utype_class(actor_unittype))) { - /* Hard requirement */ - continue; - } - has_target = TRUE; break; - } extra_type_by_cause_iterate_end; + } extra_type_list_iterate_end; if (!has_target) { /* Reason: no extras can be conquered by this unit. */ diff --git a/common/tile.c b/common/tile.c index 82e2d7e01d..7225e36dd5 100644 --- a/common/tile.c +++ b/common/tile.c @@ -205,20 +205,19 @@ int tile_has_not_aggressive_extra_for_unit(const struct tile *ptile, } /************************************************************************//** - Check if tile contains base providing effect for unit + Check if tile contains base providing effect for unit. ****************************************************************************/ bool tile_has_claimable_base(const struct tile *ptile, const struct unit_type *punittype) { - extra_type_by_cause_iterate(EC_BASE, pextra) { + extra_type_list_iterate(utype_class(punittype)->cache.native_bases, pextra) { struct base_type *pbase = extra_base_get(pextra); if (tile_has_extra(ptile, pextra) - && territory_claiming_base(pbase) - && is_native_extra_to_uclass(pextra, utype_class(punittype))) { + && territory_claiming_base(pbase)) { return TRUE; } - } extra_type_by_cause_iterate_end; + } extra_type_list_iterate_end; return FALSE; } @@ -315,17 +314,16 @@ bool tile_has_refuel_extra(const struct tile *ptile, } /************************************************************************//** - Check if tile contains base native for unit + Check if tile contains base native for unit. ****************************************************************************/ bool tile_has_native_base(const struct tile *ptile, const struct unit_type *punittype) { - extra_type_by_cause_iterate(EC_BASE, pextra) { - if (tile_has_extra(ptile, pextra) - && is_native_extra_to_utype(pextra, punittype)) { + extra_type_list_iterate(utype_class(punittype)->cache.native_bases, pextra) { + if (tile_has_extra(ptile, pextra)) { return TRUE; } - } extra_type_by_cause_iterate_end; + } extra_type_list_iterate_end; return FALSE; } diff --git a/common/unittype.c b/common/unittype.c index d91b6aba70..bad8d580aa 100644 --- a/common/unittype.c +++ b/common/unittype.c @@ -2574,6 +2574,7 @@ void unit_classes_init(void) unit_classes[i].item_number = i; unit_classes[i].cache.refuel_extras = NULL; unit_classes[i].cache.native_tile_extras = NULL; + unit_classes[i].cache.native_bases = NULL; unit_classes[i].cache.bonus_roads = NULL; unit_classes[i].cache.subset_movers = NULL; unit_classes[i].helptext = NULL; @@ -2597,6 +2598,10 @@ void unit_classes_free(void) extra_type_list_destroy(unit_classes[i].cache.native_tile_extras); unit_classes[i].cache.native_tile_extras = NULL; } + if (unit_classes[i].cache.native_bases != NULL) { + extra_type_list_destroy(unit_classes[i].cache.native_bases); + unit_classes[i].cache.native_bases = NULL; + } if (unit_classes[i].cache.bonus_roads != NULL) { extra_type_list_destroy(unit_classes[i].cache.bonus_roads); unit_classes[i].cache.bonus_roads = NULL; @@ -2794,6 +2799,7 @@ void set_unit_class_caches(struct unit_class *pclass) { pclass->cache.refuel_extras = extra_type_list_new(); pclass->cache.native_tile_extras = extra_type_list_new(); + pclass->cache.native_bases = extra_type_list_new(); pclass->cache.bonus_roads = extra_type_list_new(); pclass->cache.subset_movers = unit_class_list_new(); @@ -2807,6 +2813,9 @@ void set_unit_class_caches(struct unit_class *pclass) if (extra_has_flag(pextra, EF_NATIVE_TILE)) { extra_type_list_append(pclass->cache.native_tile_extras, pextra); } + if (is_extra_caused_by(pextra, EC_BASE)) { + extra_type_list_append(pclass->cache.native_bases, pextra); + } if (proad != NULL && road_provides_move_bonus(proad)) { extra_type_list_append(pclass->cache.bonus_roads, pextra); } diff --git a/common/unittype.h b/common/unittype.h index 493eea3d35..30cc4a08c1 100644 --- a/common/unittype.h +++ b/common/unittype.h @@ -156,6 +156,7 @@ struct unit_class { struct { struct extra_type_list *refuel_extras; struct extra_type_list *native_tile_extras; + struct extra_type_list *native_bases; struct extra_type_list *bonus_roads; struct unit_class_list *subset_movers; } cache; -- 2.39.1