The Battle for Wesnoth  1.19.7+dev
undo_dismiss_action.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 2024
3  Part of the Battle for Wesnoth Project https://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 
16 #include "game_board.hpp"
17 #include "resources.hpp"
18 #include "team.hpp"
19 #include "units/unit.hpp"
20 
21 namespace actions::undo
22 {
24  : undo_action()
25  , dismissed_unit(dismissed->clone())
26 {
27 }
28 
30  : undo_action()
31  , dismissed_unit(unit::create(cfg.mandatory_child("unit")))
32 {
33 }
34 
35 /**
36  * Writes this into the provided config.
37  */
38 void dismiss_action::write(config & cfg) const
39 {
40  undo_action::write(cfg);
41  dismissed_unit->write(cfg.add_child("unit"));
42 }
43 
44 /**
45  * Undoes this action.
46  * @return true on success; false on an error.
47  */
48 bool dismiss_action::undo(int side)
49 {
50  team &current_team = resources::gameboard->get_team(side);
51 
52  current_team.recall_list().add(dismissed_unit);
53  return true;
54 }
55 
57 
58 }
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
config & add_child(config_key_type key)
Definition: config.cpp:440
team & get_team(int i)
Definition: game_board.hpp:92
void add(const unit_ptr &ptr, int pos=-1)
Add a unit to the list.
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:75
recall_list_manager & recall_list()
Definition: team.hpp:201
This class represents a single unit of a specific type.
Definition: unit.hpp:133
static auto red_undo_dismiss
game_board * gameboard
Definition: resources.cpp:20
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:27
dismiss_action(const unit_const_ptr &dismissed)
virtual void write(config &cfg) const
Writes this into the provided config.
virtual bool undo(int side)
Undoes this action.
Records information to be able to undo an action.
Definition: undo_action.hpp:80
virtual void write(config &cfg) const
Writes this into the provided config.
Definition: undo_action.hpp:91