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