The Battle for Wesnoth  1.15.12+dev
action_village.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by Fabian Mueller <fabianmueller5@gmx.de>
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 
15 /**
16  * @file
17  * Editor label action classes
18  */
19 #define GETTEXT_DOMAIN "wesnoth-editor"
20 
22 
24 #include "team.hpp"
25 
26 namespace editor
27 {
28 IMPLEMENT_ACTION(village)
29 
30 std::unique_ptr<editor_action> editor_action_village::perform(map_context& mc) const
31 {
32  if(!mc.map().is_village(loc_)) {
33  return nullptr;
34  }
35 
36  std::vector<team>& teams = mc.teams();
37 
38  try {
39  if(teams.at(side_number_).owns_village(loc_)) {
40  return nullptr;
41  }
42  } catch(const std::out_of_range&) {
43  // side_number_ was an invalid team index.
44  }
45 
46  auto undo = static_cast<std::unique_ptr<editor_action>>(std::make_unique<editor_action_village_delete>(loc_));
47 
48  for(const team& t : teams) {
49  if(t.owns_village(loc_)) {
50  undo = std::make_unique<editor_action_village>(loc_, t.side() - 1);
51  }
52  }
53 
54  perform_without_undo(mc);
55  return undo;
56 }
57 
59 {
60  std::vector<team>& teams = mc.teams();
61 
62  for(team& t : teams) {
63  if(t.owns_village(loc_)) {
64  t.lose_village(loc_);
65  }
66  }
67 
68  // TODO 0 is a bad argument
69  teams[side_number_].get_village(loc_, 0, nullptr);
70 }
71 
72 IMPLEMENT_ACTION(village_delete)
73 
75 {
76  // \todo: can this delete more than one village? If it can't, why isn't the return statement
77  // inside the loop? If it can, why doesn't it return an editor_action_chain of undo actions?
78  std::unique_ptr<editor_action> undo;
79 
80  for(const team& t : mc.teams()) {
81  if(t.owns_village(loc_)) {
83  undo = std::make_unique<editor_action_village>(loc_, t.side() - 1);
84  }
85  }
86 
87  return undo;
88 }
89 
91 {
92  for(team& t : mc.teams()) {
93  if(t.owns_village(loc_)) {
94  t.lose_village(loc_);
96  }
97  }
98 }
99 
100 } // end namespace editor
void add_changed_location(const map_location &loc)
void perform_without_undo(map_context &mc) const override
Perform the action without creating an undo action.
STL namespace.
Sets the ownership of a village to the current side.
map_location loc_
void perform_without_undo(map_context &mc) const override
Perform the action without creating an undo action.
This class stores all the data for a single &#39;side&#39; (in game nomenclature).
Definition: team.hpp:44
Manage the empty-palette in the editor.
Definition: action.cpp:29
Base class for all editor actions.
Definition: action_base.hpp:40
This class wraps around a map to provide a concise interface for the editor to work with...
Definition: map_context.hpp:59
Editor action classes.
double t
Definition: astarsearch.cpp:64
#define IMPLEMENT_ACTION(id)
Helper macro to implement common action methods.
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 ...
virtual const std::vector< team > & teams() const override
Const teams accessor.
Clears the ownership of a village.