From 5b312b0634211515bca34d9a4e2c203ef376cf48 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Wed, 12 Apr 2023 00:13:08 +0300 Subject: [PATCH 41/41] Add unit_class cache of hiding extras See osdn #47838 Signed-off-by: Marko Lindqvist --- common/player.c | 25 +++++++++++++++---------- common/unittype.c | 9 +++++++++ common/unittype.h | 1 + 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/common/player.c b/common/player.c index 90da00acef..c4f68f4b9e 100644 --- a/common/player.c +++ b/common/player.c @@ -1019,6 +1019,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)) { @@ -1039,20 +1040,24 @@ bool can_player_see_unit_at(const struct player *pplayer, return FALSE; } + /* Allied units are always seen. + * See also stealth unit hiding part in map_change_seen() */ + if (allied) { + return TRUE; + } + /* Units within some extras may be hidden. */ - if (!allied) { - const struct unit_type *ptype = unit_type_get(punit); + pclass = unit_class_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; - } + extra_type_list_iterate(pclass->cache.hiding_extras, pextra) { + if (tile_has_extra(ptile, pextra)) { + return FALSE; + } + } extra_type_list_iterate_end; - /* Allied or non-hiding units are always seen. + /* 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 474e7059a5..d50aab9be0 100644 --- a/common/unittype.c +++ b/common/unittype.c @@ -2514,6 +2514,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].ruledit_disabled = FALSE; @@ -2544,6 +2545,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); } @@ -2739,6 +2744,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) { @@ -2757,6 +2763,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 0ee9cebcc7..eba74cb90f 100644 --- a/common/unittype.h +++ b/common/unittype.h @@ -168,6 +168,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