1
/*
2
 *  The Mana World
3
 *  Copyright (C) 2004  The Mana World Development Team
4
 *
5
 *  This file is part of The Mana World.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program; if not, write to the Free Software
19
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
 */
21
22
#ifndef MAP_H
23
#define MAP_H
24
25
#include <list>
26
#include <vector>
27
28
#include "position.h"
29
#include "properties.h"
30
31
class Animation;
32
class AmbientOverlay;
33
class Graphics;
34
class Image;
35
class MapLayer;
36
class Particle;
37
class SimpleAnimation;
38
class Sprite;
39
class Tileset;
40
41
typedef std::vector<Tileset*> Tilesets;
42
typedef std::list<Sprite*> MapSprites;
43
typedef MapSprites::iterator MapSprite;
44
typedef std::vector<MapLayer*> Layers;
45
46
extern const int DEFAULT_TILE_SIDE_LENGTH;
47
48
/**
49
 * A meta tile stores additional information about a location on a tile map.
50
 * This is information that doesn't need to be repeated for each tile in each
51
 * layer of the map.
52
 */
53
struct MetaTile
54
{
55
    /**
56
     * Constructor.
57
     */
58
    MetaTile() : whichList(0), blockmask(0) {}
59
60
    // Pathfinding members
61
    int Fcost;               /**< Estimation of total path cost */
62
    int Gcost;               /**< Cost from start to this location */
63
    int Hcost;               /**< Estimated cost to goal */
64
    int whichList;           /**< No list, open list or closed list */
65
    int parentX;             /**< X coordinate of parent tile */
66
    int parentY;             /**< Y coordinate of parent tile */
67
    unsigned char blockmask; /**< Blocking properties of this tile */
68
};
69
70
/**
71
 * Animation cycle of a tile image which changes the map accordingly.
72
 */
73
class TileAnimation
74
{
75
    public:
76
        TileAnimation(Animation *ani);
77
        ~TileAnimation();
78
        void update(int ticks = 1);
79
        void addAffectedTile(MapLayer *layer, int index)
80
        { mAffected.push_back(std::make_pair(layer, index)); }
81
    private:
82
        std::list<std::pair<MapLayer*, int> > mAffected;
83
        SimpleAnimation *mAnimation;
84
        Image *mLastImage;
85
};
86
87
/**
88
 * A map layer. Stores a grid of tiles and their offset, and implements layer
89
 * rendering.
90
 */
91
class MapLayer
92
{
93
    public:
94
        /**
95
         * Constructor, taking layer origin, size and whether this layer is the
96
         * fringe layer. The fringe layer is the layer that draws the sprites.
97
         * There can be only one fringe layer per map.
98
         */
99
        MapLayer(int x, int y, int width, int height, bool isFringeLayer);
100
101
        /**
102
         * Destructor.
103
         */
104
        ~MapLayer();
105
106
        /**
107
         * Set tile image, with x and y in layer coordinates.
108
         */
109
        void setTile(int x, int y, Image *img);
110
111
        /**
112
         * Set tile image with x + y * width already known.
113
         */
114
        void setTile(int index, Image *img) { mTiles[index] = img; }
115
116
        /**
117
         * Get tile image, with x and y in layer coordinates.
118
         */
119
        Image *getTile(int x, int y) const;
120
121
        /**
122
         * Draws this layer to the given graphics context. The coordinates are
123
         * expected to be in map range and will be translated to local layer
124
         * coordinates and clipped to the layer's dimensions.
125
         *
126
         * The given sprites are only drawn when this layer is the fringe
127
         * layer.
128
         */
129
        void draw(Graphics *graphics,
130
                  int startX, int startY,
131
                  int endX, int endY,
132
                  int scrollX, int scrollY,
133
                  const MapSprites &sprites) const;
134
135
    private:
136
        int mX, mY;
137
        int mWidth, mHeight;
138
        bool mIsFringeLayer;    /**< Whether the sprites are drawn. */
139
        Image **mTiles;
140
};
141
142
/**
143
 * A tile map.
144
 */
145
class Map : public Properties
146
{
147
    public:
148
        enum BlockType
149
        {
150
            BLOCKTYPE_NONE = -1,
151
            BLOCKTYPE_WALL,
152
            BLOCKTYPE_CHARACTER,
153
            BLOCKTYPE_MONSTER,
154
            NB_BLOCKTYPES
155
        };
156
157
        enum BlockMask
158
        {
159
            BLOCKMASK_WALL      = 0x80, // = bin 1000 0000
160
            BLOCKMASK_CHARACTER = 0x01, // = bin 0000 0001
161
            BLOCKMASK_MONSTER   = 0x02  // = bin 0000 0010
162
        };
163
164
        /**
165
         * Constructor, taking map and tile size as parameters.
166
         */
167
        Map(int width, int height, int tileWidth, int tileHeight);
168
169
        /**
170
         * Destructor.
171
         */
172
        ~Map();
173
174
        /**
175
         * Initialize map overlays. Should be called after all the properties
176
         * are set.
177
         */
178
        void initializeOverlays();
179
180
        /**
181
         * Updates animations. Called as needed.
182
         */
183
        void update(int ticks = 1);
184
185
        /**
186
         * Draws the map to the given graphics output. This method draws all
187
         * layers, sprites and overlay effects.
188
         *
189
         * TODO: For efficiency reasons, this method could take into account
190
         * the clipping rectangle set on the Graphics object. However,
191
         * currently the map is always drawn full-screen.
192
         */
193
        void draw(Graphics *graphics, int scrollX, int scrollY);
194
195
        /**
196
         * Visualizes collision layer for debugging
197
         */
198
        void drawCollision(Graphics *graphics, int scrollX, int scrollY);
199
200
        /**
201
         * Adds a layer to this map. The map takes ownership of the layer.
202
         */
203
        void addLayer(MapLayer *layer);
204
205
        /**
206
         * Adds a tileset to this map. The map takes ownership of the tileset.
207
         */
208
        void addTileset(Tileset *tileset);
209
210
        /**
211
         * Finds the tile set that a tile with the given global id is part of.
212
         */
213
        Tileset *getTilesetWithGid(int gid) const;
214
215
        /**
216
         * Get tile reference.
217
         */
218
        MetaTile *getMetaTile(int x, int y) const;
219
220
        /**
221
         * Marks a tile as occupied.
222
         */
223
        void blockTile(int x, int y, BlockType type);
224
225
        /**
226
         * Gets walkability for a tile with a blocking bitmask. When called
227
         * without walkmask, only blocks against colliding tiles.
228
         */
229
        bool getWalk(int x, int y,
230
                     unsigned char walkmask = BLOCKMASK_WALL) const;
231
232
#ifdef EATHENA_SUPPORT
233
        /**
234
         * Tells whether a tile is occupied by a being.
235
         */
236
        bool occupied(int x, int y) const;
237
#endif
238
239
        /**
240
         * Returns the width of this map in tiles.
241
         */
242
        int getWidth() const { return mWidth; }
243
244
        /**
245
         * Returns the height of this map in tiles.
246
         */
247
        int getHeight() const { return mHeight; }
248
249
        /**
250
         * Returns the tile width of this map.
251
         */
252
        int getTileWidth() const { return mTileWidth; }
253
254
        /**
255
         * Returns the tile height used by this map.
256
         */
257
        int getTileHeight() const { return mTileHeight; }
258
259
        const std::string &getMusicFile() const;
260
        const std::string &getName() const;
261
262
        /**
263
         * Find a path from one location to the next.
264
         */
265
        Path findPath(int startX, int startY, int destX, int destY,
266
                      unsigned char walkmask, int maxCost = 20);
267
268
        /**
269
         * Adds a sprite to the map.
270
         */
271
        MapSprite addSprite(Sprite *sprite);
272
273
        /**
274
         * Removes a sprite from the map.
275
         */
276
        void removeSprite(MapSprite iterator);
277
278
        /**
279
         * Adds a particle effect
280
         */
281
        void addParticleEffect(const std::string &effectFile, int x, int y);
282
283
        /**
284
         * Initializes all added particle effects
285
         */
286
        void initializeParticleEffects(Particle* particleEngine);
287
288
        /**
289
         * Adds a tile animation to the map
290
         */
291
        void addAnimation(int gid, TileAnimation *animation)
292
        { mTileAnimations[gid] = animation; }
293
294
        /**
295
         * Gets the tile animation for a specific gid
296
         */
297
        TileAnimation *getAnimationForGid(int gid) const;
298
299
    private:
300
        /**
301
         * Draws the overlay graphic to the given graphics output.
302
         */
303
        void drawOverlay(Graphics *graphics, float scrollX, float scrollY,
304
                         int detail);
305
306
        /**
307
         * Tells whether the given coordinates fall within the map boundaries.
308
         */
309
        bool contains(int x, int y) const;
310
311
        /**
312
         * Blockmasks for different entities
313
         */
314
        int *mOccupation[NB_BLOCKTYPES];
315
316
        int mWidth, mHeight;
317
        int mTileWidth, mTileHeight;
318
        int mMaxTileHeight;
319
        MetaTile *mMetaTiles;
320
        Layers mLayers;
321
        Tilesets mTilesets;
322
        MapSprites mSprites;
323
324
        // Pathfinding members
325
        int mOnClosedList, mOnOpenList;
326
327
        // Overlay data
328
        std::list<AmbientOverlay*> mOverlays;
329
        float mLastScrollX;
330
        float mLastScrollY;
331
332
        // Particle effect data
333
        struct ParticleEffectData
334
        {
335
            std::string file;
336
            int x;
337
            int y;
338
        };
339
        std::list<ParticleEffectData> particleEffects;
340
341
        std::map<int, TileAnimation*> mTileAnimations;
342
};
343
344
#endif