GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/map/map.h Lines: 2 25 8.0 %
Date: 2017-11-29 Branches: 0 0 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2017  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#ifndef RESOURCES_MAP_MAP_H
24
#define RESOURCES_MAP_MAP_H
25
26
#include "position.h"
27
28
#include "being/actor.h"
29
30
#include "enums/render/rendertype.h"
31
32
#include "enums/resources/map/blocktype.h"
33
#include "enums/resources/map/maplayerposition.h"
34
#include "enums/resources/map/maptype.h"
35
36
#include "listeners/configlistener.h"
37
38
#include "utils/vector.h"
39
40
#include "resources/memorycounter.h"
41
42
#include "resources/map/properties.h"
43
44
class AmbientLayer;
45
class MapHeights;
46
class MapItem;
47
class MapLayer;
48
class ObjectsLayer;
49
class Resource;
50
class SpecialLayer;
51
class Tileset;
52
class TileAnimation;
53
class WalkLayer;
54
55
struct MetaTile;
56
57
typedef STD_VECTOR<Tileset*> Tilesets;
58
typedef STD_VECTOR<MapLayer*> Layers;
59
typedef Layers::const_iterator LayersCIter;
60
61
typedef STD_VECTOR<AmbientLayer*> AmbientLayerVector;
62
typedef AmbientLayerVector::const_iterator AmbientLayerVectorCIter;
63
typedef AmbientLayerVector::iterator AmbientLayerVectorIter;
64
65
/**
66
 * A tile map.
67
 */
68
class Map final : public Properties,
69
                  public ConfigListener,
70
                  public MemoryCounter
71
{
72
    public:
73
        /**
74
         * Constructor, taking map and tile size as parameters.
75
         */
76
        Map(const std::string &name,
77
            const int width,
78
            const int height,
79
            const int tileWidth,
80
            const int tileHeight);
81
82
        A_DELETE_COPY(Map)
83
84
        /**
85
         * Destructor.
86
         */
87
        ~Map();
88
89
        /**
90
         * Initialize ambient layers. Has to be called after all the properties
91
         * are set.
92
         */
93
        void initializeAmbientLayers() restrict2;
94
95
        /**
96
         * Updates animations. Called as needed.
97
         */
98
        void update(const int ticks = 1) restrict2;
99
100
        /**
101
         * Draws the map to the given graphics output. This method draws all
102
         * layers, actors and overlay effects.
103
         */
104
        void draw(Graphics *restrict const graphics,
105
                  int scrollX,
106
                  int scrollY) restrict2 A_NONNULL(2);
107
108
        /**
109
         * Visualizes collision layer for debugging
110
         */
111
        void drawCollision(Graphics *restrict const graphics,
112
                           const int scrollX,
113
                           const int scrollY,
114
                           const MapTypeT drawFlags) const
115
                           restrict2 A_NONNULL(2);
116
117
        /**
118
         * Adds a layer to this map. The map takes ownership of the layer.
119
         */
120
        void addLayer(MapLayer *const layer) restrict2 A_NONNULL(2);
121
122
        /**
123
         * Adds a tileset to this map. The map takes ownership of the tileset.
124
         */
125
        void addTileset(Tileset *const tileset) restrict2 A_NONNULL(2);
126
127
        /**
128
         * Finds the tile set that a tile with the given global id is part of.
129
         */
130
        const Tileset *getTilesetWithGid(const int gid) const
131
                                         restrict2 A_WARN_UNUSED;
132
133
        /**
134
         * Get tile reference.
135
         */
136
        const MetaTile *getMetaTile(const int x,
137
                                    const int y) const
138
                                    restrict2 A_WARN_UNUSED;
139
140
        void addBlockMask(const int x, const int y,
141
                          const BlockTypeT type) restrict2;
142
143
        void setBlockMask(const int x, const int y,
144
                          const BlockTypeT type) restrict2;
145
146
        /**
147
         * Gets walkability for a tile with a blocking bitmask. When called
148
         * without walkmask, only blocks against colliding tiles.
149
         */
150
        bool getWalk(const int x, const int y,
151
                     const unsigned char blockWalkMask) const
152
                     restrict2 A_WARN_UNUSED;
153
154
        void setWalk(const int x, const int y) restrict2;
155
156
        unsigned char getBlockMask(const int x,
157
                                   const int y) const restrict2;
158
159
        /**
160
         * Returns the width of this map in tiles.
161
         */
162
        int getWidth() const restrict2 noexcept2 A_WARN_UNUSED
163
        { return mWidth; }
164
165
        /**
166
         * Returns the height of this map in tiles.
167
         */
168
        int getHeight() const restrict2 noexcept2 A_WARN_UNUSED
169
        { return mHeight; }
170
171
        /**
172
         * Returns the tile width of this map.
173
         */
174
        int getTileWidth() const restrict2 noexcept2 A_WARN_UNUSED
175
        { return mTileWidth; }
176
177
        /**
178
         * Returns the tile height used by this map.
179
         */
180
        int getTileHeight() const restrict2 noexcept2 A_WARN_UNUSED
181
        { return mTileHeight; }
182
183
        const std::string getMusicFile() const restrict2 A_WARN_UNUSED;
184
185
        void setMusicFile(const std::string &restrict file) restrict2;
186
187
        const std::string getName() const restrict2 A_WARN_UNUSED;
188
189
        /**
190
         * Gives the map id based on filepath (ex: 009-1)
191
         */
192
        const std::string getFilename() const restrict2 A_WARN_UNUSED;
193
194
        const std::string getGatName() const restrict2 A_WARN_UNUSED;
195
196
        /**
197
         * Find a path from one location to the next.
198
         */
199
        Path findPath(const int startX, const int startY,
200
                      const int destX, const int destY,
201
                      const unsigned char blockWalkmask,
202
                      const int maxCost = 20) restrict2 A_WARN_UNUSED;
203
204
        /**
205
         * Adds a particle effect
206
         */
207
        void addParticleEffect(const std::string &restrict effectFile,
208
                               const int x,
209
                               const int y,
210
                               const int w = 0,
211
                               const int h = 0) restrict2;
212
213
        /**
214
         * Initializes all added particle effects
215
         */
216
        void initializeParticleEffects() const restrict2;
217
218
        /**
219
         * Adds a tile animation to the map
220
         */
221
        void addAnimation(const int gid,
222
                          TileAnimation *restrict const animation) restrict2
223
                          A_NONNULL(3);
224
225
        void setDrawLayersFlags(const MapTypeT &restrict n) restrict2;
226
227
        MapTypeT getDrawLayersFlags() const restrict2 A_WARN_UNUSED
228
        { return mDrawLayersFlags; }
229
230
        void addExtraLayer() restrict2;
231
232
        void saveExtraLayer() const restrict2;
233
234
        SpecialLayer *getTempLayer() const restrict2 noexcept2 A_WARN_UNUSED
235
60
        { return mTempLayer; }
236
237
        SpecialLayer *getSpecialLayer() const restrict2 noexcept2 A_WARN_UNUSED
238
60
        { return mSpecialLayer; }
239
240
        void setHasWarps(const bool n) restrict2 noexcept2
241
        { mHasWarps = n; }
242
243
        bool getHasWarps() const restrict2 noexcept2 A_WARN_UNUSED
244
        { return mHasWarps; }
245
246
        std::string getUserMapDirectory() const restrict2 A_WARN_UNUSED;
247
248
        void addPortal(const std::string &restrict name,
249
                       const int type,
250
                       const int x, const int y,
251
                       const int dx, const int dy) restrict2;
252
253
        void addRange(const std::string &restrict name,
254
                      const int type,
255
                      const int x, const int y,
256
                      const int dx, const int dy) restrict2;
257
258
        void addPortalTile(const std::string &restrict name,
259
                           const int type,
260
                           const int x, const int y) restrict2;
261
262
        void updatePortalTile(const std::string &restrict name,
263
                              const int type,
264
                              const int x, const int y,
265
                              const bool addNew = true) restrict2;
266
267
        const STD_VECTOR<MapItem*> &getPortals() const restrict2 noexcept2
268
                                                A_WARN_UNUSED
269
        { return mMapPortals; }
270
271
        /**
272
         * Gets the tile animation for a specific gid
273
         */
274
        const TileAnimation *getAnimationForGid(const int gid)
275
                                                const restrict2 A_WARN_UNUSED;
276
277
        void optionChanged(const std::string &restrict value)
278
                           restrict2 override final;
279
280
        MapItem *findPortalXY(const int x,
281
                              const int y) const restrict2 A_WARN_UNUSED;
282
283
        int getActorsCount() const restrict2 A_WARN_UNUSED
284
        { return CAST_S32(mActors.size()); }
285
286
        void setPvpMode(const int mode) restrict2;
287
288
        int getPvpMode() const restrict2 noexcept2 A_WARN_UNUSED
289
        { return mPvp; }
290
291
        const ObjectsLayer* getObjectsLayer() const restrict2 noexcept2
292
                                            A_WARN_UNUSED
293
        { return mObjects; }
294
295
        std::string getObjectData(const unsigned x,
296
                                  const unsigned y,
297
                                  const int type) const
298
                                  restrict2 A_WARN_UNUSED;
299
300
        void indexTilesets() restrict2;
301
302
        void clearIndexedTilesets() restrict2;
303
304
        void setActorsFix(const int x, const int y) restrict2;
305
306
        int getVersion() const restrict2 noexcept2 A_WARN_UNUSED
307
        { return mVersion; }
308
309
        void setVersion(const int n) restrict2 noexcept2
310
        { mVersion = n; }
311
312
        void reduce() restrict2;
313
314
        void redrawMap() restrict2 noexcept2
315
        { mRedrawMap = true; }
316
317
        bool empty() const restrict2 A_WARN_UNUSED
318
        { return mLayers.empty(); }
319
320
        void setCustom(const bool b) restrict2 noexcept2
321
        { mCustom = b; }
322
323
        bool isCustom() const restrict2 noexcept2 A_WARN_UNUSED
324
        { return mCustom; }
325
326
        const std::map<int, TileAnimation*> &getTileAnimations() const
327
                                            restrict2 noexcept2 A_WARN_UNUSED
328
        { return mTileAnimations; }
329
330
        void setAtlas(Resource *restrict const atlas) restrict2 noexcept2
331
        { mAtlas = atlas; }
332
333
        const MetaTile *getMetaTiles() const restrict2 noexcept2
334
        { return mMetaTiles; }
335
336
        const WalkLayer *getWalkLayer() const restrict2 noexcept2
337
        { return mWalkLayer; }
338
339
        void setWalkLayer(WalkLayer *restrict const layer) restrict2 noexcept2
340
        { mWalkLayer = layer; }
341
342
        void addHeights(const MapHeights *restrict const heights) restrict2
343
                        A_NONNULL(2);
344
345
        uint8_t getHeightOffset(const int x, const int y) const restrict2;
346
347
        void setMask(const int mask) restrict2;
348
349
        void updateDrawLayersList() restrict2;
350
351
        bool isHeightsPresent() const restrict2 noexcept2
352
        { return mHeights != nullptr; }
353
354
        void updateConditionLayers() restrict2;
355
356
        void preCacheLayers() restrict2;
357
358
        int calcMemoryLocal() const override final;
359
360
        int calcMemoryChilds(const int level) const override final;
361
362
        std::string getCounterName() const override final
363
        { return mName; }
364
365
        bool haveAtlas() const
366
        { return mAtlas != nullptr; }
367
368
    protected:
369
        friend class Actor;
370
        friend class Minimap;
371
372
        /**
373
         * Adds an actor to the map.
374
         */
375
        Actors::iterator addActor(Actor *const actor) restrict2 A_NONNULL(2);
376
377
        /**
378
         * Removes an actor from the map.
379
         */
380
        void removeActor(const Actors::iterator &restrict iterator) restrict2;
381
382
    private:
383
        /**
384
         * Updates scrolling of ambient layers. Has to be called each game tick.
385
         */
386
        void updateAmbientLayers(const float scrollX,
387
                                 const float scrollY) restrict2;
388
389
        /**
390
         * Draws the foreground or background layers to the given graphics output.
391
         */
392
        void drawAmbientLayers(Graphics *restrict const graphics,
393
                               const MapLayerPositionT type,
394
                               const int detail) const restrict2 A_NONNULL(2);
395
396
        /**
397
         * Tells whether the given coordinates fall within the map boundaries.
398
         */
399
        bool contains(const int x,
400
                      const int y) const restrict2 A_WARN_UNUSED;
401
402
        const int mWidth;
403
        const int mHeight;
404
        const int mTileWidth;
405
        const int mTileHeight;
406
        int mMaxTileHeight;
407
        MetaTile *const mMetaTiles;
408
        WalkLayer *mWalkLayer;
409
        Layers mLayers;
410
        Layers mDrawUnderLayers;
411
        Layers mDrawOverLayers;
412
        Tilesets mTilesets;
413
        Actors mActors;
414
        bool mHasWarps;
415
416
        // draw flags
417
        MapTypeT mDrawLayersFlags;
418
419
        // Pathfinding members
420
        unsigned int mOnClosedList;
421
        unsigned int mOnOpenList;
422
423
        // Overlay data
424
        AmbientLayerVector mBackgrounds;
425
        AmbientLayerVector mForegrounds;
426
        float mLastAScrollX;
427
        float mLastAScrollY;
428
429
        // Particle effect data
430
        struct ParticleEffectData final
431
        {
432
            ParticleEffectData(const std::string &restrict file0,
433
                               const int x0,
434
                               const int y0,
435
                               const int w0,
436
                               const int h0) noexcept2 :
437
                file(file0),
438
                x(x0),
439
                y(y0),
440
                w(w0),
441
                h(h0)
442
            {
443
            }
444
445
            A_DEFAULT_COPY(ParticleEffectData)
446
447
            const2 std::string file;
448
            const2 int x;
449
            const2 int y;
450
            const2 int w;
451
            const2 int h;
452
        };
453
        STD_VECTOR<ParticleEffectData> mParticleEffects;
454
455
        STD_VECTOR<MapItem*> mMapPortals;
456
457
        std::map<int, TileAnimation*> mTileAnimations;
458
459
        std::string mName;
460
        int mOverlayDetail;
461
        float mOpacity;
462
        const RenderType mOpenGL;
463
        int mPvp;
464
        bool mTilesetsIndexed;
465
        Tileset** mIndexedTilesets;
466
        int mIndexedTilesetsSize;
467
        int mActorFixX;
468
        int mActorFixY;
469
        int mVersion;
470
471
        SpecialLayer *mSpecialLayer;
472
        SpecialLayer *mTempLayer;
473
        ObjectsLayer *mObjects;
474
        MapLayer *mFringeLayer;
475
476
        int mLastX;
477
        int mLastY;
478
        int mLastScrollX;
479
        int mLastScrollY;
480
481
        int mDrawX;
482
        int mDrawY;
483
        int mDrawScrollX;
484
        int mDrawScrollY;
485
        int mMask;
486
        Resource *mAtlas;
487
        const MapHeights *mHeights;
488
        bool mRedrawMap;
489
        bool mBeingOpacity;
490
        bool mCachedDraw;
491
        bool mCustom;
492
        bool mDrawOnlyFringe;
493
};
494
495
#endif  // RESOURCES_MAP_MAP_H