26 #define ERR_NG LOG_STREAM(err, log_engine)
27 #define WRN_NG LOG_STREAM(warn, log_engine)
28 #define LOG_NG LOG_STREAM(info, log_engine)
29 #define DBG_NG LOG_STREAM(debug, log_engine)
42 add(
i->get_location(), *
i);
66 while (i !=
umap_.end() && (!i->second.unit)) { ++
i; }
74 std::pair<unit_map::unit_iterator, bool>
res(
insert(p) );
75 if(res.second ==
false) { p.reset(); }
81 DBG_NG <<
"Unit map: Moving unit from " << src <<
" to " << dst <<
"\n";
99 std::pair<t_lmap::iterator,bool>
res =
lmap_.insert(std::make_pair(dst, uit));
102 if(res.second ==
false) {
103 p->set_location(src);
104 lmap_.insert(std::make_pair(src, uit));
130 size_t unit_id = p->underlying_id();
134 ERR_NG <<
"Trying to add " << p->name()
135 <<
" - " << p->id() <<
" at an invalid location; Discarding.\n";
142 DBG_NG <<
"Adding unit " << p->underlying_id() <<
" - " << p->id()
143 <<
" to location: (" << loc <<
")\n";
145 std::pair<t_umap::iterator, bool> uinsert =
umap_.insert(std::make_pair(unit_id, upod ));
147 if (! uinsert.second) {
149 if (!uinsert.first->second.unit) {
150 unit_pod &opod = uinsert.first->second;
155 ERR_NG <<
"Trying to add " << p->name()
156 <<
" - " << p->id() <<
" - " << p->underlying_id()
157 <<
" (" << loc <<
") over " << q->name()
158 <<
" - " << q->id() <<
" - " << q->underlying_id()
159 <<
" (" << q->get_location()
163 ERR_NG <<
"The new unit was assigned underlying_id="
164 << p->underlying_id()
165 <<
" to prevent duplicate id conflicts.\n";
167 uinsert =
umap_.insert(std::make_pair(p->underlying_id(), upod ));
169 while (!uinsert.second && (++guard < 1e6) ) {
171 ERR_NG <<
"\n\nPlease Report this error to https://gna.org/bugs/index.php?18591 "
172 "\nIn addition to the standard details of operating system and wesnoth version "
173 "and how it happened, please answer the following questions "
174 "\n 1. Were you playing multi-player?"
175 "\n 2. Did you start/restart/reload the game/scenario?"
176 "\nThank you for your help in fixing this bug.\n";
179 uinsert =
umap_.insert(std::make_pair(p->underlying_id(), upod )); }
180 if (!uinsert.second) {
181 throw game::error(
"One million collisions in unit_map"); }
185 std::pair<t_lmap::iterator,bool> linsert =
lmap_.insert(std::make_pair(loc, uinsert.first ));
188 if(! linsert.second) {
191 umap_.erase(uinsert.first);
194 uinsert.first->second.unit.reset();
196 DBG_NG <<
"Trying to add " << p->name()
197 <<
" - " << p->id() <<
" at location ("<<loc <<
"); Occupied by "
198 <<(linsert.first->second->second).
unit->
name()<<
" - " << linsert.first->second->second.unit->id() <<
"\n";
221 t_umap::const_iterator ui =
umap_.begin();
222 t_umap::const_iterator uend =
umap_.end();
223 for( ; ui != uend ; ++ui){
224 if(ui->second.ref_count < 0) {
226 bool a_reference_counter_overflowed(
false);
227 assert(a_reference_counter_overflowed);
229 num_iters += ui->second.ref_count;
240 DBG_NG <<
"Delete unit " <<
i->second.unit->underlying_id() <<
"\n";
241 i->second.unit.reset();
257 size_t uid( u->underlying_id() );
259 DBG_NG <<
"Extract unit " << uid <<
" - " << u->id()
260 <<
" from location: (" << loc <<
")\n";
262 assert(uit->first == uit->second.unit->underlying_id());
263 if(uit->second.ref_count == 0){
267 uit->second.unit.reset();
288 if((
i !=
umap_.end()) && !
i->second.unit){
i =
umap_.end() ;}
289 return make_unit_iterator<t_umap::iterator>(
i );
294 return make_unit_iterator<t_lmap::iterator>(
lmap_.find(loc) );
299 for (; i != i_end; ++
i) {
300 if (i->side() == side && i->can_recruit())
310 for (; i != i_end; ++
i) {
311 if (i->side() == side && i->can_recruit()){
312 if ((first_leader ==
end()) || (i->underlying_id() < first_leader->underlying_id()) )
321 std::vector<unit_map::unit_iterator> leaders;
322 for(;i != i_end; ++
i){
323 if(i->side() == side && i->can_recruit()){
324 leaders.push_back(i);
330 const std::vector<unit_map::unit_iterator> &leaders =
const_cast<unit_map*
>(
this)->
find_leaders(side);
331 std::vector<unit_map::const_unit_iterator> const_leaders (leaders.begin(), leaders.end());
332 return const_leaders;
335 #ifdef DEBUG_UNIT_MAP
340 t_umap::const_iterator uit(
umap_.begin());
341 for(; uit !=
umap_.end(); ++uit){
342 if(uit->second.ref_count < 0){
344 ERR_NG <<
"unit_map pod ref_count <0 is " << uit->second.ref_count<< std::endl;
346 if(uit->second.unit){
347 uit->second.unit->id();
351 ERR_NG <<
"unit_map umap uid <=0 is " << uit->first << std::endl;
353 if(!uit->second.unit && uit->second.ref_count == 0 ){
355 ERR_NG <<
"unit_map umap unit==nullptr when refcount == 0" << std::endl;
357 if(uit->second.unit && uit->second.unit->underlying_id() != uit->first){
359 ERR_NG <<
"unit_map umap uid("<<uit->first<<
") != underlying_id()["<< uit->second.unit->underlying_id()<<
"]" << std::endl;
362 t_lmap::const_iterator locit(
lmap_.begin());
363 for(; locit !=
lmap_.end(); ++locit){
364 if(locit->second ==
umap_.end() ){
366 ERR_NG <<
"unit_map lmap element == umap_.end() "<< std::endl;
368 if(locit->first != locit->second->second.unit->get_location()){
370 ERR_NG <<
"unit_map lmap location != unit->get_location() " << std::endl;
383 for(
const t_umap::value_type& item :
umap_) {
384 if(item.second.unit.get() == u) {
std::vector< unit_iterator > find_leaders(int side)
const t_string & name() const
The unit name for display.
unit_map & operator=(const unit_map &that)
unit_iterator find_leader(int side)
static l_noret error(LoadState *S, const char *why)
t_lmap lmap_
location -> umap::iterator.
bool self_check() const
Checks invariants. For debugging purposes only. Doesn't do anything in non-debug mode.
n_ref_counter::t_ref_counter< signed int > ref_count
GLdouble GLdouble GLdouble GLdouble q
t_umap umap_
underlying_id -> unit_pod.
std::pair< unit_iterator, bool > insert(unit_ptr p)
Adds the unit to the map.
void clear(bool force=false)
bool is_valid(const t_umap::const_iterator &i) const
std::pair< unit_iterator, bool > replace(const map_location &l, const unit &u)
Works like unit_map::add; but l is emptied first, if needed.
std::pair< unit_iterator, bool > add(const map_location &l, const unit &u)
Adds a copy of unit u at location l of the map.
Encapsulates the map of the game.
t_umap::iterator begin_core() const
unit_iterator find_first_leader(int side)
bool has_unit(const unit *const u) const
Is the unit in the map?
std::pair< unit_iterator, bool > move(const map_location &src, const map_location &dst)
Moves a unit from location src to location dst.
unit_map::unit_iterator make_unit_iterator(X const &i)
size_t erase(const map_location &l)
Erases the unit at location l, if any.
The pointer to the unit and a reference counter to record the number of extant iterators pointing to ...
boost::intrusive_ptr< unit > unit_ptr
void swap(game_board &one, game_board &other)
unit_ptr extract(const map_location &loc)
Extracts a unit from the map.
Standard logging facilities (interface).
static lg::log_domain log_engine("engine")
Container associating units to locations.
unit_iterator find(size_t id)