forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreature_tracker.cpp
163 lines (141 loc) · 4.65 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
#include "creature_tracker.h"
#include "monster.h"
#include "mongroup.h"
#include "debug.h"
Creature_tracker::Creature_tracker()
{
}
Creature_tracker::~Creature_tracker()
{
clear();
}
monster &Creature_tracker::find(int index)
{
static monster nullmon;
if( index < 0 || index >= (int)monsters_list.size() ) {
debugmsg( "Tried to find monster with invalid index %d. Monster num: %d",
index, monsters_list.size() );
return nullmon;
}
return *(monsters_list[index]);
}
int Creature_tracker::mon_at( const tripoint &coords ) const
{
const auto iter = monsters_by_location.find( coords );
if( iter != monsters_by_location.end() ) {
const int critter_id = iter->second;
if( !monsters_list[critter_id]->is_dead() ) {
return (int)critter_id;
}
}
return -1;
}
bool Creature_tracker::add( monster &critter )
{
if( critter.type->id == "mon_null" ) { // Don't wanna spawn null monsters o.O
return false;
}
if( -1 != mon_at( critter.pos3() ) ) {
debugmsg( "add_zombie: there's already a monster at %d,%d,%d",
critter.posx(), critter.posy(), critter.posz() );
return false;
}
if( monster_is_blacklisted(critter.type) ) {
return false;
}
monsters_by_location[critter.pos3()] = monsters_list.size();
monsters_list.push_back(new monster(critter));
return true;
}
size_t Creature_tracker::size() const
{
return monsters_list.size();
}
bool Creature_tracker::update_pos(const monster &critter, const tripoint &new_pos)
{
const auto old_pos = critter.pos3();
if( critter.is_dead() ) {
// mon_at ignores dead critters anyway, changing their position in the
// monsters_by_location map is useless.
remove_from_location_map( critter );
return true;
}
bool success = false;
const int critter_id = mon_at( old_pos );
const int new_critter_id = mon_at( new_pos );
if( new_critter_id >= 0 ) {
debugmsg("update_zombie_pos: new location %d,%d,%d already has zombie %d",
new_pos.x, new_pos.y, new_pos.z, new_critter_id);
} else if( critter_id >= 0 ) {
if( &critter == monsters_list[critter_id] ) {
monsters_by_location.erase( old_pos );
monsters_by_location[new_pos] = critter_id;
success = true;
} else {
debugmsg("update_zombie_pos: old location %d,%d had zombie %d instead",
old_pos.x, old_pos.y, critter_id);
}
} else {
// We're changing the x/y/z coordinates of a zombie that hasn't been added
// to the game yet. add_zombie() will update monsters_by_location for us.
debugmsg("update_zombie_pos: no such zombie at %d,%d,%d (moving to %d,%d,%d)",
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 success;
}
void Creature_tracker::remove_from_location_map( const monster &critter )
{
const tripoint &loc = critter.pos3();
const auto pos_iter = monsters_by_location.find( loc );
if( pos_iter != monsters_by_location.end() ) {
const auto &other = find( pos_iter->second );
if( &other == &critter ) {
monsters_by_location.erase( pos_iter );
}
}
}
void Creature_tracker::remove( const int idx )
{
if( idx < 0 || idx >= (int)monsters_list.size() ) {
debugmsg( "Tried to remove monster with invalid index %d. Monster num: %d",
idx, monsters_list.size() );
return;
}
monster &m = *monsters_list[idx];
remove_from_location_map( m );
delete monsters_list[idx];
monsters_list.erase( monsters_list.begin() + idx );
// Fix indices in monsters_by_location for any zombies that were just moved down 1 place.
for( auto &elem : monsters_by_location ) {
if( elem.second > (size_t)idx ) {
--elem.second;
}
}
}
void Creature_tracker::clear()
{
for( auto monster_ptr : monsters_list ) {
delete monster_ptr;
}
monsters_list.clear();
monsters_by_location.clear();
}
void Creature_tracker::rebuild_cache()
{
monsters_by_location.clear();
for( size_t i = 0; i < monsters_list.size(); i++ ) {
monster &critter = *monsters_list[i];
monsters_by_location[critter.pos3()] = i;
}
}
const std::vector<monster> &Creature_tracker::list() const
{
static std::vector<monster> for_now;
for_now.clear();
for( const auto monster_ptr : monsters_list ) {
for_now.push_back( *monster_ptr );
}
return for_now;
}