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