Mana
Loading...
Searching...
No Matches
map.h
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
24#include "actor.h"
25#include "position.h"
26#include "properties.h"
27#include "simpleanimation.h"
28
29#include <list>
30#include <vector>
31
32class AmbientLayer;
33class Graphics;
34class MapLayer;
35class Particle;
36class Tileset;
37
38const int DEFAULT_TILE_LENGTH = 32;
39
46{
47 // Pathfinding members
48 int Fcost;
49 int Gcost;
50 int Hcost;
51 unsigned whichList = 0;
52 int parentX;
53 int parentY;
54 unsigned char blockmask = 0;
55};
56
61{
62 public:
63 TileAnimation(Animation animation);
64
65 void update(int dt);
66
67 void addAffectedTile(MapLayer *layer, int index)
68 { mAffected.emplace_back(layer, index); }
69
70 private:
71 std::vector<std::pair<MapLayer*, int> > mAffected;
73 Image *mLastImage = nullptr;
74};
75
81{
82 public:
88 MapLayer(int x, int y, int width, int height, bool isFringeLayer,
89 Map *map);
90
91 ~MapLayer();
92
93 int getWidth() const { return mWidth; }
94
98 void setTile(int x, int y, Image *img);
99
103 void setTile(int index, Image *img) { mTiles[index] = img; }
104
108 Image *getTile(int x, int y) const
109 { return mTiles[x + y * mWidth]; }
110
119 void draw(Graphics *graphics,
120 int startX, int startY,
121 int endX, int endY,
122 int scrollX, int scrollY,
123 const Actors &actors,
124 int debugFlags) const;
125
126 bool isFringeLayer() const
127 { return mIsFringeLayer; }
128
129 int getTileDrawWidth(int x1, int y1, int endX, int &width) const;
130
131 void setMask(int mask) { mMask = mask; }
132 int getMask() const { return mMask; }
133
134 private:
135 int mX, mY;
137 int mMask = 1;
141};
142
146class Map : public Properties
147{
148 public:
157
159 {
160 BLOCKMASK_WALL = 0x80, // = bin 1000 0000
161 BLOCKMASK_CHARACTER = 0x01, // = bin 0000 0001
162 BLOCKMASK_MONSTER = 0x02, // = bin 0000 0010
163 BLOCKMASK_ALL = 0xFF // = bin 1111 1111
164 };
165
180
184 Map(int width, int height, int tileWidth, int tileHeight);
185
186 ~Map() override;
187
193
197 void update(int dt);
198
207 void draw(Graphics *graphics, int scrollX, int scrollY);
208
212 void drawCollision(Graphics *graphics, int scrollX, int scrollY,
213 int debugFlags) const;
214
218 void addLayer(MapLayer *layer);
219
223 void addTileset(Tileset *tileset);
224
228 Tileset *getTilesetWithGid(unsigned gid) const;
229
233 MetaTile *getMetaTile(int x, int y) const;
234
238 void blockTile(int x, int y, BlockType type);
239
244 bool getWalk(int x, int y,
245 unsigned char walkmask = BLOCKMASK_WALL) const;
246
250 bool occupied(int x, int y) const;
251
255 int getWidth() const { return mWidth; }
256
260 int getHeight() const { return mHeight; }
261
265 int getTileWidth() const
266 { return mTileWidth; }
267
271 int getTileHeight() const
272 { return mTileHeight; }
273
280 Vector getTileCenter(int x, int y) const;
281
282 std::string getMusicFile() const;
283 std::string getName() const;
284
288 std::string getFilename() const;
289
294 Position checkNodeOffsets(int radius, unsigned char walkMask,
295 const Position &position) const;
296 Position checkNodeOffsets(int radius, unsigned char walkMask,
297 int x, int y) const
298 { return checkNodeOffsets(radius, walkMask, Position(x, y)); }
299
304 Path findTilePath(int startPixelX, int startPixelY, int endPixelX,
305 int endPixelY, unsigned char walkMask,
306 int maxCost = 20);
307
311 Path findPixelPath(int startPixelX, int startPixelY,
312 int destPixelX, int destPixelY,
313 int radius, unsigned char walkmask, int maxCost = 20);
314
318 void addParticleEffect(const std::string &effectFile, int x, int y, int w = 0, int h = 0);
319
324
328 void addAnimation(int gid, TileAnimation animation);
329
330 void setDebugFlags(int flags) { mDebugFlags = flags; }
331
332 int getDebugFlags() const { return mDebugFlags; }
333
338
339 void setMask(int mask);
340
341 protected:
342 friend class Actor;
343
347 Actors::iterator addActor(Actor *actor);
348
352 void removeActor(Actors::iterator iterator);
353
354 private:
358 Path findPath(int startX, int startY, int destX, int destY,
359 unsigned char walkmask, int maxCost = 20);
360
366
370 void updateAmbientLayers(float scrollX, float scrollY);
371
375 void drawAmbientLayers(Graphics *graphics, LayerType type, float scrollX, float scrollY,
376 int detail);
377
381 bool contains(int x, int y) const;
382
387
392 std::vector<MapLayer *> mLayers;
393 std::vector<Tileset *> mTilesets;
395
396 // debug flags
398
399 // Pathfinding members
401
402 // Overlay data
403 std::vector<AmbientLayer> mBackgrounds;
404 std::vector<AmbientLayer> mForegrounds;
407
408 // Particle effect data
410 {
411 std::string file;
412 int x;
413 int y;
414 int w;
415 int h;
416 };
417 std::list<ParticleEffectData> particleEffects;
418
419 std::map<int, TileAnimation> mTileAnimations;
420
421 int mMask = 1;
422};
std::list< Actor * > Actors
Definition actor.h:32
Definition actor.h:35
An animation consists of several frames, each with their own delay and offset.
Definition animation.h:47
A central point of control for graphics.
Definition graphics.h:78
Defines a class for loading and storing images.
Definition image.h:45
A map layer.
Definition map.h:81
Image * getTile(int x, int y) const
Get tile image, with x and y in layer coordinates.
Definition map.h:108
int mMask
Definition map.h:137
int mHeight
Definition map.h:136
bool mIsFringeLayer
Whether the actors are drawn.
Definition map.h:138
void setTile(int x, int y, Image *img)
Set tile image, with x and y in layer coordinates.
Definition map.cpp:106
int mX
Definition map.h:135
Image ** mTiles
Definition map.h:139
int mWidth
Definition map.h:136
void setTile(int index, Image *img)
Set tile image with x + y * width already known.
Definition map.h:103
Map * mMap
Definition map.h:140
void setMask(int mask)
Definition map.h:131
~MapLayer()
Definition map.cpp:101
bool isFringeLayer() const
Definition map.h:126
int getTileDrawWidth(int x1, int y1, int endX, int &width) const
Definition map.cpp:189
int getWidth() const
Definition map.h:93
int mY
Definition map.h:135
int getMask() const
Definition map.h:132
void draw(Graphics *graphics, int startX, int startY, int endX, int endY, int scrollX, int scrollY, const Actors &actors, int debugFlags) const
Draws this layer to the given graphics context.
Definition map.cpp:111
A tile map.
Definition map.h:147
std::string getMusicFile() const
Definition map.cpp:593
int mHeight
Definition map.h:388
Path findPixelPath(int startPixelX, int startPixelY, int destPixelX, int destPixelY, int radius, unsigned char walkmask, int maxCost=20)
Find a pixel path from one location to the next using free offsets.
Definition map.cpp:700
TileAnimation * getAnimationForGid(int gid)
Gets the tile animation for a specific gid.
Definition map.cpp:991
int mMaxTileHeight
Definition map.h:390
void update(int dt)
Updates animations.
Definition map.cpp:299
MetaTile * mMetaTiles
Definition map.h:391
std::vector< AmbientLayer > mBackgrounds
Definition map.h:403
BlockMask
Definition map.h:159
@ BLOCKMASK_ALL
Definition map.h:163
@ BLOCKMASK_CHARACTER
Definition map.h:161
@ BLOCKMASK_MONSTER
Definition map.h:162
@ BLOCKMASK_WALL
Definition map.h:160
void setMask(int mask)
Definition map.cpp:997
int mDebugFlags
Definition map.h:397
std::vector< MapLayer * > mLayers
Definition map.h:392
void draw(Graphics *graphics, int scrollX, int scrollY)
Draws the map to the given graphics output.
Definition map.cpp:308
float mLastScrollY
Definition map.h:406
void initializeParticleEffects(Particle *particleEngine)
Initializes all added particle effects.
Definition map.cpp:964
int getHeight() const
Returns the height of this map in tiles.
Definition map.h:260
unsigned mOnOpenList
Definition map.h:400
int getTileHeight() const
Returns the tile height used by this map.
Definition map.h:271
void addLayer(MapLayer *layer)
Adds a layer to this map.
Definition map.cpp:284
void removeActor(Actors::iterator iterator)
Removes an actor from the map.
Definition map.cpp:588
Actors mActors
Definition map.h:394
std::string getName() const
Definition map.cpp:598
Path findPath(int startX, int startY, int destX, int destY, unsigned char walkmask, int maxCost=20)
Find a path from one location to the next in tile coordinates.
Definition map.cpp:750
void addAnimation(int gid, TileAnimation animation)
Adds a tile animation to the map.
Definition map.cpp:982
int getDebugFlags() const
Definition map.h:332
float mLastScrollX
Definition map.h:405
void updateAmbientLayers(float scrollX, float scrollY)
Updates scrolling of ambient layers.
Definition map.cpp:432
std::string getFilename() const
Gives the map id based on filepath (ex: 009-1)
Definition map.cpp:606
LayerType
Definition map.h:362
@ FOREGROUND_LAYERS
Definition map.h:363
@ BACKGROUND_LAYERS
Definition map.h:364
int mMask
Definition map.h:421
MetaTile * getMetaTile(int x, int y) const
Get tile reference.
Definition map.cpp:577
std::vector< Tileset * > mTilesets
Definition map.h:393
Position checkNodeOffsets(int radius, unsigned char walkMask, int x, int y) const
Definition map.h:296
int getWidth() const
Returns the width of this map in tiles.
Definition map.h:255
BlockType
Definition map.h:150
@ NB_BLOCKTYPES
Definition map.h:155
@ BLOCKTYPE_WALL
Definition map.h:152
@ BLOCKTYPE_CHARACTER
Definition map.h:153
@ BLOCKTYPE_MONSTER
Definition map.h:154
@ BLOCKTYPE_NONE
Definition map.h:151
int mTileHeight
Definition map.h:389
void initializeAmbientLayers()
Initialize ambient layers.
Definition map.cpp:241
void setDebugFlags(int flags)
Definition map.h:330
void drawAmbientLayers(Graphics *graphics, LayerType type, float scrollX, float scrollY, int detail)
Draws the foreground or background layers to the given graphics output.
Definition map.cpp:463
int getTileWidth() const
Returns the tile width of this map.
Definition map.h:265
void drawCollision(Graphics *graphics, int scrollX, int scrollY, int debugFlags) const
Visualizes collision layer for debugging.
Definition map.cpp:371
bool occupied(int x, int y) const
Tells whether a tile is occupied by a being.
Definition map.cpp:551
void blockTile(int x, int y, BlockType type)
Marks a tile as occupied.
Definition map.cpp:513
std::vector< AmbientLayer > mForegrounds
Definition map.h:404
unsigned mOnClosedList
Definition map.h:400
Tileset * getTilesetWithGid(unsigned gid) const
Finds the tile set that a tile with the given global id is part of.
Definition map.cpp:500
std::list< ParticleEffectData > particleEffects
Definition map.h:417
Position checkNodeOffsets(int radius, unsigned char walkMask, const Position &position) const
Check the current position against surrounding blocking tiles, and correct the position offset within...
Definition map.cpp:615
bool contains(int x, int y) const
Tells whether the given coordinates fall within the map boundaries.
Definition map.cpp:572
~Map() override
Definition map.cpp:229
Vector getTileCenter(int x, int y) const
Returns the tile center position in pixel coordinates.
Definition map.cpp:563
std::map< int, TileAnimation > mTileAnimations
Definition map.h:419
int mWidth
Definition map.h:388
unsigned * mOccupation[NB_BLOCKTYPES]
Blockmasks for different entities.
Definition map.h:386
void addTileset(Tileset *tileset)
Adds a tileset to this map.
Definition map.cpp:289
int mMaxTileWidth
Definition map.h:390
bool getWalk(int x, int y, unsigned char walkmask=BLOCKMASK_WALL) const
Gets walkability for a tile with a blocking bitmask.
Definition map.cpp:541
int mTileWidth
Definition map.h:389
Path findTilePath(int startPixelX, int startPixelY, int endPixelX, int endPixelY, unsigned char walkMask, int maxCost=20)
Find a tile-centered path in pixel coordinates from one location to the next.
Definition map.cpp:676
void addParticleEffect(const std::string &effectFile, int x, int y, int w=0, int h=0)
Adds a particle effect.
Definition map.cpp:953
DebugFlags
Definition map.h:167
@ DEBUG_BEING_IDS
Definition map.h:175
@ DEBUG_NONE
Definition map.h:168
@ DEBUG_BEING_COLLISION_RADIUS
Definition map.h:171
@ DEBUG_MOUSE_PATH
Definition map.h:174
@ DEBUG_SPECIAL3
Definition map.h:178
@ DEBUG_BEING_POSITION
Definition map.h:172
@ DEBUG_GRID
Definition map.h:169
@ DEBUG_SPECIAL2
Definition map.h:177
@ DEBUG_COLLISION_TILES
Definition map.h:170
@ DEBUG_SPECIAL1
Definition map.h:176
@ DEBUG_BEING_PATH
Definition map.h:173
Actors::iterator addActor(Actor *actor)
Adds an actor to the map.
Definition map.cpp:582
A particle spawned by a ParticleEmitter.
Definition particle.h:42
A class holding a set of properties.
Definition properties.h:32
This class is a leightweight alternative to the Sprite class.
Animation cycle of a tile image which changes the map accordingly.
Definition map.h:61
std::vector< std::pair< MapLayer *, int > > mAffected
Definition map.h:71
void update(int dt)
Definition map.cpp:73
void addAffectedTile(MapLayer *layer, int index)
Definition map.h:67
Image * mLastImage
Definition map.h:73
SimpleAnimation mAnimation
Definition map.h:72
A tileset, which is basically just an image set but it stores a firstgid.
Definition tileset.h:30
Vector class.
Definition vector.h:33
Graphics * graphics
Definition client.cpp:104
Particle * particleEngine
Definition game.cpp:113
const int DEFAULT_TILE_LENGTH
Definition map.h:38
std::list< Position > Path
Definition position.h:40
std::string file
Definition map.h:411
A meta tile stores additional information about a location on a tile map.
Definition map.h:46
int Fcost
Estimation of total path cost.
Definition map.h:48
int parentY
Y coordinate of parent tile.
Definition map.h:53
unsigned char blockmask
Blocking properties of this tile.
Definition map.h:54
int Hcost
Estimated cost to goal.
Definition map.h:50
int Gcost
Cost from start to this location.
Definition map.h:49
int parentX
X coordinate of parent tile.
Definition map.h:52
unsigned whichList
No list, open list or closed list.
Definition map.h:51
A position along a being's path.
Definition position.h:31