forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
creature_tracker.cpp
292 lines (255 loc) · 9.33 KB
/
creature_tracker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include "creature_tracker.h"
#include <algorithm>
#include <cassert>
#include <ostream>
#include <string>
#include <utility>
#include "debug.h"
#include "mongroup.h"
#include "monster.h"
#include "mtype.h"
#include "point.h"
#include "string_formatter.h"
#include "type_id.h"
#define dbg(x) DebugLogFL((x),DC::Game)
Creature_tracker::Creature_tracker() = default;
Creature_tracker::~Creature_tracker() = default;
shared_ptr_fast<monster> Creature_tracker::find( const tripoint &pos ) const
{
const auto iter = monsters_by_location.find( pos );
if( iter != monsters_by_location.end() ) {
const shared_ptr_fast<monster> &mon_ptr = iter->second;
if( !mon_ptr->is_dead() ) {
return mon_ptr;
}
}
return nullptr;
}
int Creature_tracker::temporary_id( const monster &critter ) const
{
const auto iter = std::find_if( monsters_list.begin(), monsters_list.end(),
[&]( const shared_ptr_fast<monster> &ptr ) {
return ptr.get() == &critter;
} );
if( iter == monsters_list.end() ) {
return -1;
}
return iter - monsters_list.begin();
}
shared_ptr_fast<monster> Creature_tracker::from_temporary_id( const int id )
{
if( static_cast<size_t>( id ) < monsters_list.size() ) {
return monsters_list[id];
} else {
return nullptr;
}
}
bool Creature_tracker::add( shared_ptr_fast<monster> critter_ptr )
{
assert( critter_ptr );
monster &critter = *critter_ptr;
if( critter.type->id.is_null() ) { // Don't want to spawn null monsters o.O
return false;
}
if( critter.type->has_flag( MF_VERMIN ) ) {
// Don't spawn vermin, they aren't implemented yet
return false;
}
if( const shared_ptr_fast<monster> existing_mon_ptr = find( critter.pos() ) ) {
// We can spawn stuff on hallucinations, but we need to kill them first
if( existing_mon_ptr->is_hallucination() ) {
existing_mon_ptr->die( nullptr );
// But don't remove - that would change the monster order and could segfault
} else if( critter.is_hallucination() ) {
return false;
} else {
debugmsg( "there's already a monster at %d,%d,%d", critter.pos().x, critter.pos().y,
critter.pos().z );
return false;
}
}
if( MonsterGroupManager::monster_is_blacklisted( critter.type->id ) ) {
return false;
}
monsters_list.emplace_back( critter_ptr );
monsters_by_location[critter.pos()] = critter_ptr;
add_to_faction_map( critter_ptr );
return true;
}
void Creature_tracker::add_to_faction_map( shared_ptr_fast<monster> critter_ptr )
{
assert( critter_ptr );
monster &critter = *critter_ptr;
// Only 1 faction per mon at the moment.
if( critter.friendly == 0 ) {
monster_faction_map_[ critter.faction ].insert( critter_ptr );
} else {
static const mfaction_str_id playerfaction( "player" );
monster_faction_map_[ playerfaction ].insert( critter_ptr );
}
}
size_t Creature_tracker::size() const
{
return monsters_list.size();
}
bool Creature_tracker::update_pos( const monster &critter, const tripoint &new_pos )
{
if( critter.is_dead() ) {
// find ignores dead critters anyway, changing their position in the
// monsters_by_location map is useless.
remove_from_location_map( critter );
return true;
}
if( const shared_ptr_fast<monster> new_critter_ptr = find( new_pos ) ) {
auto &othermon = *new_critter_ptr;
if( othermon.is_hallucination() ) {
othermon.die( nullptr );
} else {
debugmsg( "update_zombie_pos: wanted to move %s to %d,%d,%d, but new location already has %s",
critter.disp_name(),
new_pos.x, new_pos.y, new_pos.z, othermon.disp_name() );
return false;
}
}
const auto iter = std::find_if( monsters_list.begin(), monsters_list.end(),
[&]( const shared_ptr_fast<monster> &ptr ) {
return ptr.get() == &critter;
} );
if( iter != monsters_list.end() ) {
monsters_by_location.erase( critter.pos() );
monsters_by_location[new_pos] = *iter;
return true;
} else {
const tripoint &old_pos = critter.pos();
// We're changing the x/y/z coordinates of a zombie that hasn't been added
// to the game yet. `add` will update monsters_by_location for us.
debugmsg( "update_zombie_pos: no %s at %d,%d,%d (moving to %d,%d,%d)",
critter.disp_name(),
old_pos.x, old_pos.y, old_pos.z, new_pos.x, new_pos.y, new_pos.z );
// Rebuild cache in case the monster actually IS in the game, just bugged
rebuild_cache();
return false;
}
}
void Creature_tracker::remove_from_location_map( const monster &critter )
{
const auto pos_iter = monsters_by_location.find( critter.pos() );
if( pos_iter != monsters_by_location.end() && pos_iter->second.get() == &critter ) {
monsters_by_location.erase( pos_iter );
return;
}
// When it's not in the map at its current location, it might still be there under,
// another location, so look for it.
const auto iter = std::find_if( monsters_by_location.begin(), monsters_by_location.end(),
[&]( const decltype( monsters_by_location )::value_type & v ) {
return v.second.get() == &critter;
} );
if( iter != monsters_by_location.end() ) {
monsters_by_location.erase( iter );
}
}
void Creature_tracker::remove( const monster &critter )
{
const auto iter = std::find_if( monsters_list.begin(), monsters_list.end(),
[&]( const shared_ptr_fast<monster> &ptr ) {
return ptr.get() == &critter;
} );
if( iter == monsters_list.end() ) {
debugmsg( "Tried to remove invalid monster %s", critter.name() );
return;
}
for( auto &pair : monster_faction_map_ ) {
const auto fac_iter = pair.second.find( *iter );
if( fac_iter != pair.second.end() ) {
// Need to do this manually because the shared pointer containing critter is kept valid
// within removed_ and so the weak pointer in monster_faction_map_ is also valid.
pair.second.erase( fac_iter );
break;
}
}
remove_from_location_map( critter );
removed_.push_back( *iter );
monsters_list.erase( iter );
}
void Creature_tracker::clear()
{
monsters_list.clear();
monsters_by_location.clear();
monster_faction_map_.clear();
removed_.clear();
}
void Creature_tracker::rebuild_cache()
{
monsters_by_location.clear();
monster_faction_map_.clear();
for( const shared_ptr_fast<monster> &mon_ptr : monsters_list ) {
monsters_by_location[mon_ptr->pos()] = mon_ptr;
add_to_faction_map( mon_ptr );
}
}
void Creature_tracker::swap_positions( monster &first, monster &second )
{
if( first.pos() == second.pos() ) {
return;
}
// Either of them may be invalid!
const auto first_iter = monsters_by_location.find( first.pos() );
const auto second_iter = monsters_by_location.find( second.pos() );
// implied: first_iter != second_iter
shared_ptr_fast<monster> first_ptr;
if( first_iter != monsters_by_location.end() ) {
first_ptr = first_iter->second;
monsters_by_location.erase( first_iter );
}
shared_ptr_fast<monster> second_ptr;
if( second_iter != monsters_by_location.end() ) {
second_ptr = second_iter->second;
monsters_by_location.erase( second_iter );
}
// implied: (first_ptr != second_ptr) or (first_ptr == nullptr && second_ptr == nullptr)
tripoint temp = second.pos();
second.spawn( first.pos() );
first.spawn( temp );
// If the pointers have been taken out of the list, put them back in.
if( first_ptr ) {
monsters_by_location[first.pos()] = first_ptr;
}
if( second_ptr ) {
monsters_by_location[second.pos()] = second_ptr;
}
}
bool Creature_tracker::kill_marked_for_death()
{
// Important: `Creature::die` must not be called after creature objects (NPCs, monsters) have
// been removed, the dying creature could still have a pointer (the killer) to another creature.
bool monster_is_dead = false;
// Copy the list so we can iterate the copy safely *and* add new monsters from within monster::die
// This happens for example with blob monsters (they split into two smaller monsters).
const auto copy = monsters_list;
for( const shared_ptr_fast<monster> &mon_ptr : copy ) {
assert( mon_ptr );
monster &critter = *mon_ptr;
if( !critter.is_dead() ) {
continue;
}
dbg( DL::Info ) << string_format( "cleanup_dead: critter %s hp:%d %s",
critter.pos().to_string(), critter.get_hp(), critter.name() );
critter.die( nullptr );
monster_is_dead = true;
}
return monster_is_dead;
}
void Creature_tracker::remove_dead()
{
// Can't use game::all_monsters() as it would not contain *dead* monsters.
for( auto iter = monsters_list.begin(); iter != monsters_list.end(); ) {
const monster &critter = **iter;
if( critter.is_dead() ) {
remove_from_location_map( critter );
iter = monsters_list.erase( iter );
} else {
++iter;
}
}
removed_.clear();
}