The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
map_fragment.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2016 by Tomasz Sniatowski <[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 #define GETTEXT_DOMAIN "wesnoth-editor"
15 
16 #include "map_fragment.hpp"
17 
18 #include "util.hpp"
19 
20 namespace editor {
21 
23  : items_()
24  , area_()
25 {
26 }
27 
28 map_fragment::map_fragment(const gamemap& map, const std::set<map_location>& area)
29  : items_()
30  , area_()
31 {
32  add_tiles(map, area);
33 }
34 
35 void map_fragment::add_tile(const gamemap& map, const map_location& loc)
36 {
37  if (area_.find(loc) == area_.end()) {
38  items_.push_back(tile_info(map, loc));
39  area_.insert(loc);
40  }
41 }
42 
43 void map_fragment::add_tiles(const gamemap& map, const std::set<map_location>& locs)
44 {
45  for (const map_location& loc : locs) {
46  add_tile(map, loc);
47  }
48 }
49 
50 std::set<map_location> map_fragment::get_area() const
51 {
52  return area_;
53 }
54 
55 std::set<map_location> map_fragment::get_offset_area(const map_location& loc) const
56 {
57  std::set<map_location> result;
58  for (const tile_info& i : items_) {
59  result.insert(i.offset.vector_sum(loc));
60  }
61  return result;
62 }
63 
64 void map_fragment::paste_into(gamemap& map, const map_location& loc) const
65 {
66  for (const tile_info& i : items_) {
67  map.set_terrain(i.offset.vector_sum(loc), i.terrain);
68  }
69 }
70 
72 {
73  for (tile_info& ti : items_) {
74  ti.offset.vector_sum_assign(offset);
75  }
76 }
77 
79 {
80  map_location sum(0, 0);
81  for (const tile_info& ti : items_) {
82  sum.vector_sum_assign(ti.offset);
83  }
84  if (items_.size() > 0) {
85  sum.x /= static_cast<int>(items_.size());
86  sum.y /= static_cast<int>(items_.size());
87  }
88  return sum;
89 }
90 
92 {
93  shift(center_of_mass().vector_negation());
94  area_.clear();
95  for (tile_info& ti : items_) {
96  area_.insert(ti.offset);
97  }
98 }
99 
101 {
102  area_.clear();
103  for (tile_info& ti : items_) {
105  int x = ti.offset.x;
106  int y = ti.offset.y;
107  // rotate the X-Y axes to SOUTH/SOUTH_EAST - SOUTH_WEST axes
108  // but if x is odd, simply using x/2 + x/2 will lack a step
109  l = l.get_direction(map_location::SOUTH, (x+is_odd(x))/2);
112  ti.offset = l;
113  area_.insert(l);
114  }
115  if (get_area().size() != items_.size()) {
116  throw editor_exception("Map fragment rotation resulted in duplicate entries");
117  }
118 }
119 
121 {
122  area_.clear();
123  for (tile_info& ti : items_) {
125  int x = ti.offset.x;
126  int y = ti.offset.y;
127  // rotate the X-Y axes to NORTH/NORTH_EAST - SOUTH_EAST axes'
128  // reverse of what the cw rotation does
129  l = l.get_direction(map_location::NORTH, (x-is_odd(x))/2);
132  ti.offset = l;
133  area_.insert(l);
134  }
135  if (get_area().size() != items_.size()) {
136  throw editor_exception("Map fragment rotation resulted in duplicate entries");
137  }
138 }
139 
141 {
142  for (tile_info& ti : items_) {
143  ti.offset.x = -ti.offset.x;
144  }
145  center_by_mass();
146 }
147 
149 {
150  for (tile_info& ti : items_) {
151  ti.offset.y = -ti.offset.y;
152  if (ti.offset.x % 2) {
153  ti.offset.y--;
154  }
155  }
156  center_by_mass();
157 }
158 
159 
161 {
162  return items_.empty();
163 }
164 
166 {
167  std::stringstream ss;
168  ss << "MF: ";
169  for (const tile_info& ti : items_) {
170  ss << "(" << ti.offset << ")";
171  }
172  ss << " -- ";
173  for (const map_location& loc : area_) {
174  ss << "(" << loc << ")";
175  }
176  return ss.str();
177 }
178 
179 } //end namespace editor
static const map_location & ZERO()
Old implementation:
Definition: location.hpp:190
bool is_odd(T num)
Definition: util.hpp:37
void flip_horizontal()
Flip the map fragment horizontally.
void paste_into(gamemap &map, const map_location &loc) const
Paste the map fragment into the map, treating loc as the (0,0) point (offset).
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1220
std::string dump() const
Debug dump to a string.
void rotate_60_ccw()
Rotate the map fragment 60 degrees counter-clockwise around (0,0)
GLdouble l
Definition: glew.h:6966
GLintptr offset
Definition: glew.h:1650
GLuint64EXT * result
Definition: glew.h:10727
std::set< map_location > get_offset_area(const map_location &offset) const
Get the area covered by this map fragment, shifted by an offset.
Encapsulates the map of the game.
Definition: map.hpp:37
Manage the empty-palette in the editor.
Definition: action.cpp:28
Templates and utility-routines for strings and numbers.
Encapsulates the map of the game.
Definition: location.hpp:38
void flip_vertical()
Flip the map fragment vertically.
void shift(const map_location &offset)
Shift all tiles in the map fragment by the specified offset.
map_location center_of_mass() const
Get the center of the map fragment, mass-wise.
size_t i
Definition: function.cpp:1057
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
This represents a tile along with information about it, namely the terrain, possibly other informatio...
void set_terrain(const map_location &loc, const t_translation::t_terrain &terrain, const terrain_type_data::tmerge_mode mode=terrain_type_data::BOTH, bool replace_if_failed=false)
Clobbers over the terrain at location 'loc', with the given terrain.
Definition: map.cpp:479
void add_tiles(const gamemap &map, const std::set< map_location > &loc)
Add many locations and pull their info from the map.
std::vector< expression_ptr > items_
Definition: formula.cpp:119
map_fragment()
Create an empty map fragment.
map_location & vector_sum_assign(const map_location &a)
Definition: location.hpp:211
GLsizeiptr size
Definition: glew.h:1649
map_location get_direction(DIRECTION d, unsigned int n=1u) const
Definition: location.hpp:232
std::set< map_location > area_
std::set< map_location > get_area() const
Get the area covered by this map fragment.
std::vector< tile_info > items_
The data of this map_fragment.
void center_by_mass()
Shift the map fragment so it is roughly centered around the (0,0) point, mass-wise.
GLsizei const GLcharARB ** string
Definition: glew.h:4503
void add_tile(const gamemap &map, const map_location &loc)
Add a single location and pull its info from the map.
void rotate_60_cw()
Rotate the map fragment 60 degrees clockwise around (0,0)