1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
use std::collections::HashMap;
use xml::attribute::OwnedAttribute;
use crate::{
util::{floor_div, get_attrs, map_wrapper, parse_tag, XmlEventResult},
Error, LayerTile, LayerTileData, MapTilesetGid, Result,
};
use super::util::parse_data_line;
/// The raw data of a [`InfiniteTileLayer`]. Does not include a reference to its parent [`Map`](crate::Map).
#[derive(PartialEq, Clone)]
pub struct InfiniteTileLayerData {
chunks: HashMap<(i32, i32), ChunkData>,
}
impl std::fmt::Debug for InfiniteTileLayerData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("InfiniteTileLayerData").finish()
}
}
impl InfiniteTileLayerData {
pub(crate) fn new(
parser: &mut impl Iterator<Item = XmlEventResult>,
attrs: Vec<OwnedAttribute>,
tilesets: &[MapTilesetGid],
) -> Result<Self> {
let (e, c) = get_attrs!(
for v in attrs {
Some("encoding") => encoding = v,
Some("compression") => compression = v,
}
(encoding, compression)
);
let mut chunks = HashMap::<(i32, i32), ChunkData>::new();
parse_tag!(parser, "data", {
"chunk" => |attrs| {
let chunk = InternalChunk::new(parser, attrs, e.clone(), c.clone(), tilesets)?;
for x in chunk.x..chunk.x + chunk.width as i32 {
for y in chunk.y..chunk.y + chunk.height as i32 {
let chunk_pos = ChunkData::tile_to_chunk_pos(x, y);
let relative_pos = (x - chunk_pos.0 * ChunkData::WIDTH as i32, y - chunk_pos.1 * ChunkData::HEIGHT as i32);
let chunk_index = (relative_pos.0 + relative_pos.1 * ChunkData::WIDTH as i32) as usize;
let internal_pos = (x - chunk.x, y - chunk.y);
let internal_index = (internal_pos.0 + internal_pos.1 * chunk.width as i32) as usize;
if internal_index >= chunk.tiles.len() {
return Err(Error::InvalidTileFound);
}
chunks.entry(chunk_pos).or_insert_with(ChunkData::new).tiles[chunk_index] = chunk.tiles[internal_index];
}
}
Ok(())
}
});
Ok(Self { chunks })
}
/// Obtains the tile data present at the position given.
///
/// If the position given is invalid or the position is empty, this function will return [`None`].
///
/// If you want to get a [`Tile`](`crate::Tile`) instead, use [`InfiniteTileLayer::get_tile()`].
pub fn get_tile_data(&self, x: i32, y: i32) -> Option<&LayerTileData> {
let chunk_pos = ChunkData::tile_to_chunk_pos(x, y);
self.chunks
.get(&chunk_pos)
.and_then(|chunk| {
let relative_pos = (
x - chunk_pos.0 * ChunkData::WIDTH as i32,
y - chunk_pos.1 * ChunkData::HEIGHT as i32,
);
let chunk_index =
(relative_pos.0 + relative_pos.1 * ChunkData::WIDTH as i32) as usize;
chunk.tiles.get(chunk_index).map(Option::as_ref)
})
.flatten()
}
/// Returns an iterator over only the data part of the chunks of this tile layer.
///
/// In 99.99% of cases you'll want to use [`InfiniteTileLayer::chunks()`] instead; Using this method is only
/// needed if you *only* require the tile data of the chunks (and no other utilities provided by
/// the map-wrapped [`LayerTile`]), and you are in dire need for that extra bit of performance.
///
/// This iterator doesn't have any particular order.
#[inline]
pub fn chunk_data(&self) -> impl ExactSizeIterator<Item = ((i32, i32), &ChunkData)> {
self.chunks.iter().map(|(pos, chunk)| (*pos, chunk))
}
/// Obtains a chunk's data by its position. To obtain the position of the chunk that contains a
/// tile, use [`ChunkData::tile_to_chunk_pos()`].
///
/// In 99.99% of cases you'll want to use [`InfiniteTileLayer::get_chunk()`] instead; Using this method is only
/// needed if you *only* require the tile data of the chunk (and no other utilities provided by
/// the map-wrapped [`LayerTile`]), and you are in dire need for that extra bit of performance.
#[inline]
pub fn get_chunk_data(&self, x: i32, y: i32) -> Option<&ChunkData> {
self.chunks.get(&(x, y))
}
}
/// Part of an infinite tile layer's data.
///
/// Has only the tile data contained within and not a reference to the map it is part of.
/// In 99.99% of cases you'll actually want to use [`Chunk`].
#[derive(Debug, PartialEq, Clone)]
pub struct ChunkData {
tiles: Box<[Option<LayerTileData>; Self::TILE_COUNT]>,
}
impl ChunkData {
/// Infinite layer chunk width. This constant might change between versions, not counting as a
/// breaking change.
pub const WIDTH: u32 = 16;
/// Infinite layer chunk height. This constant might change between versions, not counting as a
/// breaking change.
pub const HEIGHT: u32 = 16;
/// Infinite layer chunk tile count. This constant might change between versions, not counting
/// as a breaking change.
pub const TILE_COUNT: usize = Self::WIDTH as usize * Self::HEIGHT as usize;
pub(crate) fn new() -> Self {
Self {
tiles: Box::new([None; Self::TILE_COUNT]),
}
}
/// Obtains the tile data present at the position given relative to the chunk's top-left-most tile.
///
/// If the position given is invalid or the position is empty, this function will return [`None`].
///
/// If you want to get a [`LayerTile`](`crate::LayerTile`) instead, use [`Chunk::get_tile()`].
pub fn get_tile_data(&self, x: i32, y: i32) -> Option<&LayerTileData> {
if x < Self::WIDTH as i32 && y < Self::HEIGHT as i32 && x >= 0 && y >= 0 {
self.tiles[x as usize + y as usize * Self::WIDTH as usize].as_ref()
} else {
None
}
}
/// Returns the position of the chunk that contains the given tile position.
pub fn tile_to_chunk_pos(x: i32, y: i32) -> (i32, i32) {
(
floor_div(x, ChunkData::WIDTH as i32),
floor_div(y, ChunkData::HEIGHT as i32),
)
}
}
map_wrapper!(
#[doc = "Part of an [`InfiniteTileLayer`]."]
Chunk => ChunkData
);
impl<'map> Chunk<'map> {
/// Obtains the tile present at the position given relative to the chunk's top-left-most tile.
///
/// If the position given is invalid or the position is empty, this function will return [`None`].
pub fn get_tile(&self, x: i32, y: i32) -> Option<LayerTile<'map>> {
self.data
.get_tile_data(x, y)
.map(|data| LayerTile::new(self.map(), data))
}
}
#[derive(Debug, PartialEq, Clone)]
struct InternalChunk {
/// The X coordinate of the top-left-most tile in the chunk.
/// Corresponds to the `x` attribute in the TMX format.
x: i32,
/// The Y coordinate of the top-left-most tile in the chunk.
/// Corresponds to the `y` attribute in the TMX format.
y: i32,
width: u32,
height: u32,
tiles: Vec<Option<LayerTileData>>,
}
impl InternalChunk {
pub(crate) fn new(
parser: &mut impl Iterator<Item = XmlEventResult>,
attrs: Vec<OwnedAttribute>,
encoding: Option<String>,
compression: Option<String>,
tilesets: &[MapTilesetGid],
) -> Result<Self> {
let (x, y, width, height) = get_attrs!(
for v in attrs {
"x" => x ?= v.parse::<i32>(),
"y" => y ?= v.parse::<i32>(),
"width" => width ?= v.parse::<u32>(),
"height" => height ?= v.parse::<u32>(),
}
(x, y, width, height)
);
let tiles = parse_data_line(encoding, compression, parser, tilesets)?;
Ok(InternalChunk {
x,
y,
width,
height,
tiles,
})
}
}
map_wrapper!(
#[doc = "A [`TileLayer`](super::TileLayer) with no bounds, internally stored using [`Chunk`]s."]
InfiniteTileLayer => InfiniteTileLayerData
);
impl<'map> InfiniteTileLayer<'map> {
/// Obtains the tile present at the position given.
///
/// If the position is empty, this function will return [`None`].
pub fn get_tile(&self, x: i32, y: i32) -> Option<LayerTile<'map>> {
self.data
.get_tile_data(x, y)
.map(|data| LayerTile::new(self.map, data))
}
/// Returns an iterator over different parts of this map called [`Chunk`]s.
///
/// These **may not** correspond with the chunks in the TMX file, as the chunk size is
/// implementation defined (see [`ChunkData::WIDTH`], [`ChunkData::HEIGHT`]).
///
/// The iterator item contains the position of the chunk in chunk coordinates along with a
/// reference to the actual chunk at that position.
///
/// This iterator doesn't have any particular order.
///
/// ## Example
/// ```
/// # use tiled::{Loader, LayerType, TileLayer};
/// use tiled::ChunkData;
///
/// # let map = Loader::new()
/// # .load_tmx_map("assets/tiled_base64_zlib_infinite.tmx")
/// # .unwrap();
/// # if let LayerType::Tiles(TileLayer::Infinite(infinite_layer)) =
/// # &map.get_layer(0).unwrap().layer_type()
/// # {
/// for (chunk_pos, chunk) in infinite_layer.chunks() {
/// for x in 0..ChunkData::WIDTH as i32 {
/// for y in 0..ChunkData::HEIGHT as i32 {
/// if let Some(tile) = chunk.get_tile(x, y) {
/// let tile_pos = (
/// chunk_pos.0 * ChunkData::WIDTH as i32 + x,
/// chunk_pos.1 * ChunkData::HEIGHT as i32 + y,
/// );
/// println!("At ({}, {}): {:?}", tile_pos.0, tile_pos.1, tile);
/// }
/// }
/// }
/// }
/// # } else {
/// # panic!("It is wrongly recognised as a finite map");
/// # }
/// ```
#[inline]
pub fn chunks(&self) -> impl ExactSizeIterator<Item = ((i32, i32), Chunk<'map>)> + 'map {
let map: &'map crate::Map = self.map;
self.data
.chunks
.iter()
.map(move |(pos, chunk)| (*pos, Chunk::new(map, chunk)))
}
/// Obtains a chunk by its position. To obtain the position of the chunk that contains a tile,
/// use [`ChunkData::tile_to_chunk_pos()`].
#[inline]
pub fn get_chunk(&self, x: i32, y: i32) -> Option<Chunk<'map>> {
let map: &'map crate::Map = self.map;
self.data
.get_chunk_data(x, y)
.map(move |data| Chunk::new(map, data))
}
}