The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
action_unit.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2016 by Fabian Mueller <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * Editor unit action class
18  */
19 
20 //TODO is a textdomain needed?
21 #define GETTEXT_DOMAIN "wesnoth-editor"
22 
24 
26 
27 #include "units/unit.hpp"
29 
30 #include <boost/scoped_ptr.hpp>
31 
32 namespace editor {
33 
35 {
36  return new editor_action_unit(*this);
37 }
38 
40 {
41  std::unique_ptr<editor_action> undo(new editor_action_unit_delete(loc_));
43  return undo.release();
44 }
45 
47 {
48  mc.get_units().add(loc_,u_);
49  mc.get_units().find(loc_)->set_location(loc_);
51 }
52 
53 
55 {
56  return new editor_action_unit_delete(*this);
57 }
58 
60 {
61  unit_map& units = mc.get_units();
62  unit_map::const_unit_iterator unit_it = units.find(loc_);
63 
64  std::unique_ptr<editor_action> undo;
65  if (unit_it != units.end()) {
66  undo.reset(new editor_action_unit(loc_, *unit_it));
68  return undo.release();
69  }
70  return nullptr;
71 }
72 
74 {
75  unit_map& units = mc.get_units();
76  if (!units.erase(loc_)) {
77  ERR_ED << "Could not delete unit on " << loc_.x << "/" << loc_.y << std::endl;
78  } else {
80  }
81 }
82 
83 
85 {
86  return new editor_action_unit_replace(*this);
87 }
88 
90 {
91  std::unique_ptr<editor_action> undo(new editor_action_unit_replace(new_loc_, loc_));
92 
94  return undo.release();
95 }
96 
98 {
99  unit_map& units = mc.get_units();
100  units.move(loc_, new_loc_);
102 
103  unit& u = *units.find(new_loc_);
104  //TODO do we still need set_standing?
105  u.anim_comp().set_standing();
106 
109 
110  /* @todo
111  if (mc.get_map().is_village(new_loc_)) {
112  (*(resources::teams))[u.side()].get_village(new_loc_);
113  }
114  */
115 
116 //TODO check if that is useful
117 // resources::screen->invalidate_unit_after_move(loc_, new_loc_);
118 // resources::screen->draw();
119 }
120 
121 
123 {
124  return new editor_action_unit_facing(*this);
125 }
126 
128 {
129  std::unique_ptr<editor_action> undo(new editor_action_unit_facing(loc_, old_direction_, new_direction_));
131  return undo.release();
132 }
133 
135 {
136  unit_map& units = mc.get_units();
137  unit_map::unit_iterator unit_it = units.find(loc_);
138 
139  if (unit_it != units.end()) {
140  unit_it->set_facing(new_direction_);
141  unit_it->anim_comp().set_standing();
142  }
143 }
144 
145 
146 } //end namespace editor
void add_changed_location(const map_location &loc)
editor_action * perform(map_context &mc) const
Perform the action, returning an undo action that, when performed, shall reverse any effects of this ...
editor_action_unit_facing * clone() const
Action cloning.
unit_iterator end()
Definition: map.hpp:311
editor_action_unit_delete(map_location loc)
Definition: action_unit.hpp:61
editor_action_unit_replace * clone() const
Action cloning.
Definition: action_unit.cpp:84
Definition: unit.hpp:95
void perform_without_undo(map_context &mc) const
Perform the action without creating an undo action.
Definition: action_unit.cpp:46
Editor action classes.
editor_action * perform(map_context &mc) const
Perform the action, returning an undo action that, when performed, shall reverse any effects of this ...
Definition: action_unit.cpp:89
editor_action * perform(map_context &mc) const
Perform the action, returning an undo action that, when performed, shall reverse any effects of this ...
Definition: action_unit.cpp:59
void perform_without_undo(map_context &mc) const
Perform the action without creating an undo action.
editor_action_unit * clone() const
Action cloning.
Definition: action_unit.cpp:34
void perform_without_undo(map_context &mc) const
Perform the action without creating an undo action.
Definition: action_unit.cpp:97
static void clear_status_caches()
Clear the unit status cache for all units.
Definition: unit.cpp:610
editor_action_unit_replace(map_location loc, map_location new_loc)
Definition: action_unit.hpp:74
editor_action_unit_delete * clone() const
Action cloning.
Definition: action_unit.cpp:54
editor_action * perform(map_context &mc) const
Perform the action, returning an undo action that, when performed, shall reverse any effects of this ...
Definition: action_unit.cpp:39
Manage the empty-palette in the editor.
Definition: action.cpp:28
unit_animation_component & anim_comp() const
Definition: unit.hpp:276
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.
Definition: map.cpp:70
place a new unit on the map
Definition: action_unit.hpp:39
editor_action_unit(map_location loc, const unit &u)
Definition: action_unit.hpp:42
unit_map & get_units()
Get the unit map from the current map context object.
Base class for all editor actions.
Definition: action_base.hpp:41
editor_action_unit_facing(map_location loc, map_location::DIRECTION new_direction, map_location::DIRECTION old_direction)
Definition: action_unit.hpp:89
void perform_without_undo(map_context &mc) const
Perform the action without creating an undo action.
Definition: action_unit.cpp:73
This class wraps around a map to provide a concise interface for the editor to work with...
Definition: map_context.hpp:41
std::pair< unit_iterator, bool > move(const map_location &src, const map_location &dst)
Moves a unit from location src to location dst.
Definition: map.cpp:79
size_t erase(const map_location &l)
Erases the unit at location l, if any.
Definition: map.cpp:277
#define ERR_ED
Remove a unit from the map.
Definition: action_unit.hpp:58
map_location::DIRECTION old_direction_
Definition: action_unit.hpp:99
Container associating units to locations.
Definition: map.hpp:90
map_location::DIRECTION new_direction_
Definition: action_unit.hpp:98
void set_standing(bool with_bars=true)
Sets the animation state to standing.
unit_iterator find(size_t id)
Definition: map.cpp:285
unit_map * units
Definition: resources.cpp:35