Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport #70660 and #70687 #73876

Merged
merged 2 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions src/creature_tracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "cata_assert.h"
#include "debug.h"
#include "flood_fill.h"
#include "game.h"
#include "map.h"
#include "mongroup.h"
#include "monster.h"
Expand Down Expand Up @@ -170,7 +171,6 @@ void creature_tracker::remove( const monster &critter )
}

remove_from_location_map( critter );
removed_.emplace( iter->get() );
removed_this_turn_.emplace( *iter );
monsters_list.erase( iter );
}
Expand All @@ -179,7 +179,6 @@ void creature_tracker::clear()
{
monsters_list.clear();
monsters_by_location.clear();
removed_.clear();
removed_this_turn_.clear();
creatures_by_zone_and_faction_.clear();
invalidate_reachability_cache();
Expand All @@ -193,6 +192,27 @@ void creature_tracker::rebuild_cache()
}
}

bool creature_tracker::is_present( Creature *creature ) const
{
if( creature->is_monster() ) {
if( const auto iter = monsters_by_location.find( creature->get_location() );
iter != monsters_by_location.end() ) {
if( static_cast<const Creature *>( iter->second.get() ) == creature ) {
return !iter->second->is_dead();
}
}
} else if( creature->is_avatar() ) {
return true;
} else if( creature->is_npc() ) {
for( const shared_ptr_fast<npc> &cur_npc : active_npc ) {
if( static_cast<const Creature *>( cur_npc.get() ) == creature ) {
return !cur_npc->is_dead();
}
}
}
return false;
}

void creature_tracker::swap_positions( monster &first, monster &second )
{
if( first.get_location() == second.get_location() ) {
Expand Down Expand Up @@ -265,7 +285,6 @@ void creature_tracker::remove_dead()
monster *const critter = iter->get();
if( critter->is_dead() ) {
remove_from_location_map( *critter );
removed_.insert( critter );
iter = monsters_list.erase( iter );
} else {
++iter;
Expand Down Expand Up @@ -329,7 +348,6 @@ void creature_tracker::flood_fill_zone( const Creature &origin )
{
if( dirty_ ) {
creatures_by_zone_and_faction_.clear();
removed_.clear();
zone_tick_ = zone_tick_ > 0 ? -1 : 1;
zone_number_ = 1;
dirty_ = false;
Expand Down Expand Up @@ -386,11 +404,12 @@ void creature_tracker::flood_fill_zone( const Creature &origin )
return false;
},
[this]( const tripoint_bub_ms & loc ) {
Creature *creature = this->creature_at<Creature>( loc );
if( creature ) {
const int n = zone_number_ * zone_tick_;
creatures_by_zone_and_faction_[n][creature->get_monster_faction()].push_back( creature );
creature->set_reachable_zone( n );
if( Creature *creature = this->creature_at<Creature>( loc, true ) ) {
if( shared_ptr_fast<Creature> ptr = g->shared_from( *creature ) ) {
const int n = zone_number_ * zone_tick_;
creatures_by_zone_and_faction_[n][creature->get_monster_faction()].emplace_back( std::move( ptr ) );
creature->set_reachable_zone( n );
}
}
} );
if( zone_number_ == std::numeric_limits<int>::max() ) {
Expand Down
17 changes: 12 additions & 5 deletions src/creature_tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ class creature_tracker

void rebuild_cache();

// If the creature is in the tracker.
bool is_present( Creature *creature ) const;

std::list<shared_ptr_fast<npc>> active_npc; // NOLINT(cata-serialize)
std::vector<shared_ptr_fast<monster>> monsters_list;
// NOLINTNEXTLINE(cata-serialize)
Expand All @@ -165,9 +168,8 @@ class creature_tracker
bool dirty_ = true; // NOLINT(cata-serialize)
int zone_tick_ = 1; // NOLINT(cata-serialize)
int zone_number_ = 0; // NOLINT(cata-serialize)
std::unordered_map<int, std::unordered_map<mfaction_id, std::vector<Creature *>>>
std::unordered_map<int, std::unordered_map<mfaction_id, std::vector<shared_ptr_fast<Creature>>>>
creatures_by_zone_and_faction_; // NOLINT(cata-serialize)
std::unordered_set<Creature *> removed_; // NOLINT(cata-serialize)

friend game;
};
Expand All @@ -192,15 +194,20 @@ Creature *creature_tracker::find_reachable( const Creature &origin, FactionPredi

const auto map_iter = creatures_by_zone_and_faction_.find( origin.get_reachable_zone() );
if( map_iter != creatures_by_zone_and_faction_.end() ) {
for( const auto& [faction, creatures] : map_iter->second ) {
for( auto& [faction, creatures] : map_iter->second ) {
if( !faction_fn( faction ) ) {
continue;
}
for( Creature *other : creatures ) {
if( removed_.count( other ) == 0 ) {
for( std::size_t i = 0; i < creatures.size(); ) {
if( Creature *other = creatures[i].get(); is_present( other ) ) {
if( creature_fn( other ) ) {
return other;
}
++i;
} else {
using std::swap;
swap( creatures[i], creatures.back() );
creatures.pop_back();
}
}
}
Expand Down
Loading