The Battle for Wesnoth  1.17.0-dev
action_unit.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2021
3  by Fabian Mueller <fabianmueller5@gmx.de>
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 /**
17  * @file
18  * Editor unit action class
19  */
20 
21 // TODO is a textdomain needed?
22 #define GETTEXT_DOMAIN "wesnoth-editor"
23 
25 
27 
29 #include "units/unit.hpp"
30 
31 namespace editor
32 {
34 
35 std::unique_ptr<editor_action> editor_action_unit::perform(map_context& mc) const
36 {
37  auto undo = std::make_unique<editor_action_unit_delete>(loc_);
38  perform_without_undo(mc);
39  return undo;
40 }
41 
43 {
44  mc.units().add(loc_, *u_);
45  mc.units().find(loc_)->set_location(loc_);
47 }
48 
49 IMPLEMENT_ACTION(unit_delete)
50 
52 {
53  unit_map& units = mc.units();
54  unit_map::const_unit_iterator unit_it = units.find(loc_);
55 
56  if(unit_it != units.end()) {
57  auto undo = std::make_unique<editor_action_unit>(loc_, *unit_it);
59  return undo;
60  }
61 
62  return nullptr;
63 }
64 
66 {
67  unit_map& units = mc.units();
68  if(!units.erase(loc_)) {
69  ERR_ED << "Could not delete unit on " << loc_ << std::endl;
70  } else {
72  }
73 }
74 
75 IMPLEMENT_ACTION(unit_replace)
76 
78 {
79  auto undo = std::make_unique<editor_action_unit_replace>(new_loc_, loc_);
81  return undo;
82 }
83 
85 {
86  unit_map& units = mc.units();
87  units.move(loc_, new_loc_);
89 
90  unit& u = *units.find(new_loc_);
91 
92  // TODO do we still need set_standing?
93  u.anim_comp().set_standing();
94 
96  mc.add_changed_location(new_loc_);
97 
98  /* @todo
99  if (mc.map().is_village(new_loc_)) {
100  (*(resources::gameboard->teams()))[u.side()].get_village(new_loc_);
101  }
102  */
103 
104  // TODO check if that is useful
105  // game_display::get_singleton()->invalidate_unit_after_move(loc_, new_loc_);
106  // display::get_singleton()->draw();
107 }
108 
109 IMPLEMENT_ACTION(unit_facing)
110 
112 {
113  auto undo = std::make_unique<editor_action_unit_facing>(loc_, old_direction_, new_direction_);
115  return undo;
116 }
117 
119 {
120  unit_map& units = mc.units();
121  unit_map::unit_iterator unit_it = units.find(loc_);
122 
123  if(unit_it != units.end()) {
124  unit_it->set_facing(new_direction_);
125  unit_it->anim_comp().set_standing();
126  }
127 }
128 
129 } // end namespace editor
void add_changed_location(const map_location &loc)
unit_iterator end()
Definition: map.hpp:429
void perform_without_undo(map_context &mc) const override
Perform the action without creating an undo action.
Definition: action_unit.cpp:84
This class represents a single unit of a specific type.
Definition: unit.hpp:121
void perform_without_undo(map_context &mc) const override
Perform the action without creating an undo action.
Definition: action_unit.cpp:42
Editor action classes.
std::unique_ptr< editor_action > perform(map_context &mc) const override
Perform the action, returning an undo action that, when performed, shall reverse any effects of this ...
Definition: action_unit.cpp:35
STL namespace.
std::size_t erase(const map_location &l)
Erases the unit at location l, if any.
Definition: map.cpp:297
map_location loc_
void perform_without_undo(map_context &mc) const override
Perform the action without creating an undo action.
Definition: action_unit.cpp:65
static void clear_status_caches()
Clear this unit status cache for all units.
Definition: unit.cpp:673
umap_retval_pair_t add(const map_location &l, const unit &u)
Adds a copy of unit u at location l of the map.
Definition: map.cpp:77
umap_retval_pair_t move(const map_location &src, const map_location &dst)
Moves a unit from location src to location dst.
Definition: map.cpp:93
Manage the empty-palette in the editor.
Definition: action.cpp:30
unit_iterator find(std::size_t id)
Definition: map.cpp:310
unit_animation_component & anim_comp() const
Definition: unit.hpp:1533
place a new unit on the map
Definition: action_unit.hpp:38
Base class for all editor actions.
Definition: action_base.hpp:41
This class wraps around a map to provide a concise interface for the editor to work with...
Definition: map_context.hpp:60
virtual const unit_map & units() const override
Const units accessor.
#define ERR_ED
Remove a unit from the map.
Definition: action_unit.hpp:59
#define IMPLEMENT_ACTION(id)
Helper macro to implement common action methods.
Container associating units to locations.
Definition: map.hpp:98
void set_standing(bool with_bars=true)
Sets the animation state to standing.
void perform_without_undo(map_context &mc) const override
Perform the action without creating an undo action.