The Battle for Wesnoth  1.19.0-dev
action_unit.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
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 
51 std::unique_ptr<editor_action> editor_action_unit_delete::perform(map_context& mc) const
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);
58  perform_without_undo(mc);
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_;
70  } else {
72  }
73 }
74 
75 IMPLEMENT_ACTION(unit_replace)
76 
77 std::unique_ptr<editor_action> editor_action_unit_replace::perform(map_context& mc) const
78 {
79  auto undo = std::make_unique<editor_action_unit_replace>(new_loc_, loc_);
80  perform_without_undo(mc);
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 
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 
111 std::unique_ptr<editor_action> editor_action_unit_facing::perform(map_context& mc) const
112 {
113  auto undo = std::make_unique<editor_action_unit_facing>(loc_, old_direction_, new_direction_);
114  perform_without_undo(mc);
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
#define IMPLEMENT_ACTION(id)
Helper macro to implement common action methods.
Editor action classes.
Remove a unit from the map.
Definition: action_unit.hpp:60
void perform_without_undo(map_context &mc) const override
Perform the action without creating an undo action.
Definition: action_unit.cpp:65
map_location::DIRECTION new_direction_
void perform_without_undo(map_context &mc) const override
Perform the action without creating an undo action.
void perform_without_undo(map_context &mc) const override
Perform the action without creating an undo action.
Definition: action_unit.cpp:84
place a new unit on the map
Definition: action_unit.hpp:39
void perform_without_undo(map_context &mc) const override
Perform the action without creating an undo action.
Definition: action_unit.cpp:42
Base class for all editor actions.
Definition: action_base.hpp:42
This class wraps around a map to provide a concise interface for the editor to work with.
Definition: map_context.hpp:63
virtual const unit_map & units() const override
Const units accessor.
void add_changed_location(const map_location &loc)
void set_standing(bool with_bars=true)
Sets the animation state to standing.
Container associating units to locations.
Definition: map.hpp:98
unit_iterator end()
Definition: map.hpp:428
unit_iterator find(std::size_t id)
Definition: map.cpp:302
std::size_t erase(const map_location &l)
Erases the unit at location l, if any.
Definition: map.cpp:289
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:76
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:92
This class represents a single unit of a specific type.
Definition: unit.hpp:133
static void clear_status_caches()
Clear this unit status cache for all units.
Definition: unit.cpp:695
#define ERR_ED
map_location loc_
unit_animation_component & anim_comp() const
Definition: unit.hpp:1544
Manage the empty-palette in the editor.
Definition: action.cpp:31