The Battle for Wesnoth  1.19.7+dev
fake_unit_manager.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2024
3  by Chris Beck <render787@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #include "fake_unit_manager.hpp"
17 
18 #include "display.hpp"
19 #include "log.hpp"
20 #include "units/unit.hpp"
22 #include "utils/general.hpp"
23 
24 static lg::log_domain log_engine("engine");
25 #define ERR_NG LOG_STREAM(err, log_engine)
26 
27 /** Temporarily register a unit to be drawn on the map (moving: can overlap others).
28  * The temp unit is added at the end of the temporary unit dequeue,
29  * and therefore gets drawn last, over other units and temp units.
30  * Adding the same unit twice isn't allowed.
31  */
33 {
34  if(std::find(fake_units_.begin(),fake_units_.end(), u) != fake_units_.end()) {
35  ERR_NG << "In fake_unit_manager::place_temporary_unit: attempt to add duplicate fake unit.";
36  } else {
37  fake_units_.push_back(u);
38  my_display_.invalidate(u->get_location());
39  }
40 }
41 
42 /** Removes any instances of this unit from the temporary unit database. */
44 {
45  int removed = 0;
46  if (fake_units_.empty())
47  return removed;
48  removed = utils::erase(fake_units_, u);
49  if (removed > 0) {
50  my_display_.invalidate(u->get_location());
51  // Redraw with no location to get rid of haloes
52  u->anim_comp().clear_haloes();
53  }
54  if (removed > 1) {
55  ERR_NG << "Error: duplicate temp unit found in fake_unit_manager::remove_temporary_unit";
56  }
57  return removed;
58 }
bool invalidate(const map_location &loc)
Function to invalidate a specific tile for redrawing.
Definition: display.cpp:3087
display & my_display_
Reference to my display.
std::deque< internal_ptr_type > fake_units_
collection of units destined to be drawn but not put into the unit map
void place_temporary_unit(internal_ptr_type)
Register a unit with this manager.
int remove_temporary_unit(internal_ptr_type)
Deregister a unit from this manager.
unit const * internal_ptr_type
map_display and display: classes which take care of displaying the map and game-data on the screen.
static lg::log_domain log_engine("engine")
#define ERR_NG
Standard logging facilities (interface).
std::size_t erase(Container &container, const Value &value)
Convenience wrapper for using std::remove on a container.
Definition: general.hpp:117
auto * find(Container &container, const Value &value)
Convenience wrapper for using find on a container without needing to comare to end()
Definition: general.hpp:140