From 1ebef4d47829ab6759a079ef960bfa73b2436c30 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Wed, 12 Apr 2023 00:15:13 +0300 Subject: [PATCH 22/22] Add unit_class cache of hiding extras See osdn #47838 Signed-off-by: Marko Lindqvist --- common/player.c | 26 +++++++++++++++----------- common/unittype.c | 9 +++++++++ common/unittype.h | 1 + 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/common/player.c b/common/player.c index 43a85c73b7..3c0c155e50 100644 --- a/common/player.c +++ b/common/player.c @@ -1008,6 +1008,7 @@ bool can_player_see_unit_at(const struct player *pplayer, { struct city *pcity; bool allied; + struct unit_class *pclass; /* If the player can't even see the tile... */ if (TILE_KNOWN_SEEN != tile_get_known(ptile, pplayer)) { @@ -1028,20 +1029,23 @@ bool can_player_see_unit_at(const struct player *pplayer, return FALSE; } - /* Units within some extras may be hidden. */ - if (!allied) { - struct unit_type *ptype = unit_type_get(punit); - - extra_type_list_iterate(extra_type_list_of_unit_hiders(), pextra) { - if (tile_has_extra(ptile, pextra) && is_native_extra_to_utype(pextra, ptype)) { - return FALSE; - } - } extra_type_list_iterate_end; + /* Allied units are always seen. + * See also stealth unit hiding part in map_change_seen() */ + if (allied) { + return TRUE; } - /* Allied or non-hiding units are always seen. + /* Units within some extras may be hidden. */ + pclass = unit_class_get(punit); + extra_type_list_iterate(pclass->cache.hiding_extras, pextra) { + if (tile_has_extra(ptile, pextra)) { + return FALSE; + } + } extra_type_list_iterate_end; + + /* Non-hiding units are always seen. * See also stealth unit hiding part in map_change_seen() */ - if (allied || !is_hiding_unit(punit)) { + if (!is_hiding_unit(punit)) { return TRUE; } diff --git a/common/unittype.c b/common/unittype.c index 65bf509455..7343dbc103 100644 --- a/common/unittype.c +++ b/common/unittype.c @@ -1873,6 +1873,7 @@ void unit_classes_init(void) 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.hiding_extras = NULL; unit_classes[i].cache.subset_movers = NULL; unit_classes[i].helptext = NULL; unit_classes[i].disabled = FALSE; @@ -1903,6 +1904,10 @@ void unit_classes_free(void) extra_type_list_destroy(unit_classes[i].cache.bonus_roads); unit_classes[i].cache.bonus_roads = NULL; } + if (unit_classes[i].cache.hiding_extras != NULL) { + extra_type_list_destroy(unit_classes[i].cache.hiding_extras); + unit_classes[i].cache.hiding_extras = NULL; + } if (unit_classes[i].cache.subset_movers != NULL) { unit_class_list_destroy(unit_classes[i].cache.subset_movers); } @@ -2066,6 +2071,7 @@ void set_unit_class_caches(struct unit_class *pclass) 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.hiding_extras = extra_type_list_new(); pclass->cache.subset_movers = unit_class_list_new(); extra_type_iterate(pextra) { @@ -2084,6 +2090,9 @@ void set_unit_class_caches(struct unit_class *pclass) if (proad != NULL && road_provides_move_bonus(proad)) { extra_type_list_append(pclass->cache.bonus_roads, pextra); } + if (pextra->eus == EUS_HIDDEN) { + extra_type_list_append(pclass->cache.hiding_extras, pextra); + } } } extra_type_iterate_end; diff --git a/common/unittype.h b/common/unittype.h index 234572e77b..fc79a24779 100644 --- a/common/unittype.h +++ b/common/unittype.h @@ -152,6 +152,7 @@ struct unit_class { struct extra_type_list *native_tile_extras; struct extra_type_list *native_bases; struct extra_type_list *bonus_roads; + struct extra_type_list *hiding_extras; struct unit_class_list *subset_movers; } cache; }; -- 2.39.2