GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/actormanager.h Lines: 0 11 0.0 %
Date: 2021-03-17 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-2019  The ManaPlus Developers
6
 *  Copyright (C) 2019-2021  Andrei Karas
7
 *
8
 *  This file is part of The ManaPlus Client.
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
#ifndef ACTORMANAGER_H
25
#define ACTORMANAGER_H
26
27
#include "enums/being/actortype.h"
28
29
#include "enums/resources/item/itemtype.h"
30
31
#include "enums/simpletypes/allowsort.h"
32
#include "enums/simpletypes/allplayers.h"
33
#include "enums/simpletypes/beingid.h"
34
#include "enums/simpletypes/beingtypeid.h"
35
#include "enums/simpletypes/damaged.h"
36
#include "enums/simpletypes/identified.h"
37
#include "enums/simpletypes/itemcolor.h"
38
#include "enums/simpletypes/npcnames.h"
39
40
#include "listeners/configlistener.h"
41
42
#include "utils/cast.h"
43
#include "utils/stringmap.h"
44
#include "utils/stringvector.h"
45
46
#include <list>
47
#include <set>
48
49
#include "localconsts.h"
50
51
class ActorSprite;
52
class Being;
53
class FloorItem;
54
class LocalPlayer;
55
class Map;
56
57
struct ChatObject;
58
59
typedef std::set<ActorSprite*> ActorSprites;
60
typedef ActorSprites::iterator ActorSpritesIterator;
61
typedef ActorSprites::const_iterator ActorSpritesConstIterator;
62
typedef std::map<BeingId, ActorSprite*> ActorSpritesMap;
63
typedef ActorSpritesMap::iterator ActorSpritesMapIterator;
64
typedef ActorSpritesMap::const_iterator ActorSpritesMapConstIterator;
65
66
typedef std::map<BeingId, std::set<std::string> > IdNameMapping;
67
typedef IdNameMapping::const_iterator IdNameMappingCIter;
68
69
class ActorManager final : public ConfigListener
70
{
71
    public:
72
        ActorManager();
73
74
        A_DELETE_COPY(ActorManager)
75
76
        ~ActorManager() override final;
77
78
        /**
79
         * Sets the map on which ActorSprites are created.
80
         */
81
        void setMap(Map *const map);
82
83
        /**
84
         * Sets the current player.
85
         */
86
        void setPlayer(LocalPlayer *const player) A_NONNULL(2);
87
88
        /**
89
         * Create a Being and add it to the list of ActorSprites.
90
         */
91
        Being *createBeing(const BeingId id,
92
                           const ActorTypeT type,
93
                           const BeingTypeId subtype) A_WARN_UNUSED;
94
95
        static Being *cloneBeing(const Being *const srcBeing,
96
                                 const int dx, const int dy,
97
                                 const int id);
98
99
        /**
100
         * Create a FloorItem and add it to the list of ActorSprites.
101
         */
102
        FloorItem *createItem(const BeingId id,
103
                              const int itemId,
104
                              const int x, const int y,
105
                              const ItemTypeT itemType,
106
                              const int amount,
107
                              const int refine,
108
                              const ItemColor color,
109
                              const Identified identified,
110
                              const Damaged damaged,
111
                              const int subX, const int subY,
112
                              const int *const cards);
113
114
        /**
115
         * Destroys the given ActorSprite at the end of
116
         * ActorManager::logic.
117
         */
118
        void destroy(ActorSprite *const actor);
119
120
        void erase(ActorSprite *const actor);
121
122
        void undelete(const ActorSprite *const actor);
123
124
        /**
125
         * Returns a specific Being, by id;
126
         */
127
        Being *findBeing(const BeingId id) const A_WARN_UNUSED;
128
129
        ActorSprite *findActor(const BeingId id) const A_WARN_UNUSED;
130
131
        /**
132
         * Returns a being at specific coordinates.
133
         */
134
        Being *findBeing(const int x,
135
                         const int y,
136
                         const ActorTypeT type) const A_WARN_UNUSED;
137
138
        /**
139
         * Returns a being at the specific pixel.
140
         */
141
        Being *findBeingByPixel(const int x, const int y,
142
                                const AllPlayers allPlayers)
143
                                const A_WARN_UNUSED;
144
145
        /**
146
         * Returns a beings at the specific pixel.
147
         */
148
        void findBeingsByPixel(STD_VECTOR<ActorSprite*> &beings,
149
                               const int x, const int y,
150
                               const AllPlayers allPlayers) const;
151
152
        /**
153
         * Returns a portal at the specific tile.
154
         */
155
        Being *findPortalByTile(const int x, const int y) const A_WARN_UNUSED;
156
157
        /**
158
         * Returns a specific FloorItem, by id.
159
         */
160
        FloorItem *findItem(const BeingId id) const A_WARN_UNUSED;
161
162
        /**
163
         * Returns a FloorItem at specific coordinates.
164
         */
165
        FloorItem *findItem(const int x, const int y) const A_WARN_UNUSED;
166
167
        /**
168
         * Returns a being nearest to specific coordinates.
169
         *
170
         * @param x           X coordinate in pixels.
171
         * @param y           Y coordinate in pixels.
172
         * @param maxTileDist Maximal distance in tiles. If minimal distance is
173
         *                    larger, no being is returned.
174
         * @param type        The type of being to look for.
175
         */
176
        Being *findNearestLivingBeing(const int x, const int y,
177
                                      int maxTileDist,
178
                                      const ActorTypeT type,
179
                                      const Being *const excluded)
180
                                      const A_WARN_UNUSED;
181
182
        /**
183
         * Returns a being nearest to another being.
184
         *
185
         * @param aroundBeing The being to search around.
186
         * @param maxTileDist Maximal distance in tiles. If minimal distance is
187
         *                    larger, no being is returned.
188
         * @param type        The type of being to look for.
189
         */
190
        Being *findNearestLivingBeing(const Being *const aroundBeing,
191
                                      const int maxTileDist,
192
                                      const ActorTypeT type,
193
                                      const AllowSort allowSort)
194
                                      const A_WARN_UNUSED;
195
196
        /**
197
         * Finds a being by name and (optionally) by type.
198
         */
199
        Being *findBeingByName(const std::string &name,
200
                               const ActorTypeT type)
201
                               const A_WARN_UNUSED;
202
203
       /**
204
        * Finds a nearest being by name and (optionally) by type.
205
        */
206
        Being *findNearestByName(const std::string &name,
207
                                 const ActorTypeT &type) const A_WARN_UNUSED;
208
209
       /**
210
        * Finds most damaged player, non-enemy and alive
211
        * \param maxTileDist maximal distance.
212
        */
213
        Being *findMostDamagedPlayer(const int maxTileDist)
214
                                     const A_WARN_UNUSED;
215
216
       /**
217
        * Finds the nearest player who has PVP on,
218
        * or just the nearest player if map pvp is on
219
        */
220
        Being *findNearestPvpPlayer() const A_WARN_UNUSED;
221
222
#ifdef TMWA_SUPPORT
223
       /**
224
        * Heal all players in distance.
225
        *
226
        * \param maxdist maximal distance. If minimal distance is larger,
227
        *                no being is returned
228
        */
229
//        void HealAllTargets(Being *aroundBeing, int maxdist,
230
//                                            ActorTypeT type) const;
231
232
        void healTarget() const;
233
234
        void heal(const Being *const target) const;
235
236
        void itenplz() const;
237
#endif  // TMWA_SUPPORT
238
239
        /**
240
         * Returns the whole list of beings.
241
         */
242
        const ActorSprites &getAll() const A_CONST;
243
244
        /**
245
         * Returns true if the given ActorSprite is in the manager's list,
246
         * false otherwise.
247
         *
248
         * \param actor the ActorSprite to search for
249
         */
250
        bool hasActorSprite(const ActorSprite *const actor)
251
                            const A_WARN_UNUSED;
252
253
        /**
254
         * Performs ActorSprite logic and deletes ActorSprite scheduled to be
255
         * deleted.
256
         */
257
        void logic();
258
259
        /**
260
         * Destroys all ActorSprites except the local player
261
         */
262
        void clear();
263
264
        void addBlock(const BeingId id);
265
266
        void deleteBlock(const BeingId id);
267
268
        bool isBlocked(const BeingId id) const;
269
270
        void printAllToChat();
271
272
        void printBeingsToChat(const std::string &header) const;
273
274
        static void printBeingsToChat(const STD_VECTOR<Being*> &beings,
275
                                      const std::string &header);
276
277
        void getPlayerNames(StringVect &names,
278
                            const NpcNames npcNames) const;
279
280
        void getMobNames(StringVect &names) const;
281
282
        void updatePlayerNames() const;
283
284
        void updatePlayerColors() const;
285
286
        void updatePlayerGuild() const;
287
288
#ifdef TMWA_SUPPORT
289
        void parseLevels(std::string levels) const;
290
#endif  // TMWA_SUPPORT
291
292
        bool pickUpAll(const int x1,
293
                       const int y1,
294
                       const int x2,
295
                       const int y2,
296
                       const bool serverBuggy) const;
297
298
        bool pickUpNearest(const int x, const int y, int maxdist) const;
299
300
        void optionChanged(const std::string &name) override final;
301
302
        void removeAttackMob(const std::string &name);
303
304
        void removePickupItem(const std::string &name);
305
306
        void addPriorityAttackMob(const std::string &name);
307
308
        void addAttackMob(const std::string &name);
309
310
        void addIgnoreAttackMob(const std::string &name);
311
312
        void addPickupItem(const std::string &name);
313
314
        void addIgnorePickupItem(const std::string &name);
315
316
        void setPriorityAttackMobs(const std::list<std::string> &mobs)
317
        { mPriorityAttackMobs = mobs; }
318
319
        void setAttackMobs(const std::list<std::string> &mobs)
320
        { mAttackMobs = mobs; }
321
322
        int getPriorityAttackMobsSize() const noexcept2 A_WARN_UNUSED
323
        { return CAST_S32(mPriorityAttackMobs.size()); }
324
325
        int getAttackMobsSize() const noexcept2 A_WARN_UNUSED
326
        { return CAST_S32(mAttackMobs.size()); }
327
328
        int getPickupItemsSize() const noexcept2 A_WARN_UNUSED
329
        { return CAST_S32(mPickupItems.size()); }
330
331
#define defList(list1, mob) \
332
        bool isIn##list1##List(const std::string &name) const A_WARN_UNUSED\
333
        { return m##list1##mob##Set.find(name) != m##list1##mob##Set.end(); }\
334
        void rebuild##list1##mob();\
335
        std::set<std::string> get##list1##mob##Set() const noexcept2\
336
                                                   A_WARN_UNUSED\
337
        { return m##list1##mob##Set; }\
338
        std::list<std::string> get##list1##mob() const noexcept2 A_WARN_UNUSED\
339
        { return m##list1##mob; }
340
341
        defList(Attack, Mobs)
342
        defList(PriorityAttack, Mobs)
343
        defList(IgnoreAttack, Mobs)
344
        defList(Pickup, Items)
345
        defList(IgnorePickup, Items)
346
347
        const StringIntMap &getAttackMobsMap() const noexcept2 A_WARN_UNUSED
348
        { return mAttackMobsMap; }
349
350
        const StringIntMap &getPriorityAttackMobsMap() const noexcept2
351
                                                     A_WARN_UNUSED
352
        { return mPriorityAttackMobsMap; }
353
354
        int getAttackMobIndex(const std::string &name) const A_WARN_UNUSED;
355
356
        int getPriorityAttackMobIndex(const std::string &name)
357
                                      const A_WARN_UNUSED;
358
359
        int getPickupItemIndex(const std::string &name) const A_WARN_UNUSED;
360
361
        static int getIndexByName(const std::string &name,
362
                                  const StringIntMap &map)
363
                                  A_WARN_UNUSED;
364
365
        bool checkForPickup(const FloorItem *const item) const A_WARN_UNUSED;
366
367
        bool checkDefaultPickup() const A_WARN_UNUSED;
368
369
        void updateEffects(const std::map<BeingTypeId, int> &addEffects,
370
                           const std::set<BeingTypeId> &removeEffects) const;
371
372
        void updateBadges() const;
373
374
        void updateNameId(const std::string &name,
375
                          const BeingId beingId);
376
377
        void updateSeenPlayers(const std::set<std::string> &onlinePlayers);
378
379
        std::string getSeenPlayerById(const BeingId id) const;
380
381
        size_t size() const
382
        { return mActors.size(); }
383
384
        void removeRoom(const int chatId);
385
386
        void updateRoom(const ChatObject *const newChat);
387
388
        std::string findCharById(const int32_t id);
389
390
        void addChar(const int32_t id,
391
                     const std::string &name);
392
393
#ifndef UNITTESTS
394
    protected:
395
#endif  // UNITTESTS
396
        bool validateBeing(const Being *const aroundBeing,
397
                           Being *const being,
398
                           const ActorTypeT &type,
399
                           const Being *const excluded,
400
                           const int maxCost) const A_WARN_UNUSED;
401
402
        Being *findNearestLivingBeing(const Being *const aroundBeing,
403
                                      const int maxdist,
404
                                      const ActorTypeT &type,
405
                                      const int x, const int y,
406
                                      const Being *const excluded,
407
                                      const AllowSort allowSort)
408
                                      const A_WARN_UNUSED;
409
410
        void loadAttackList();
411
412
        void storeAttackList() const;
413
414
        ActorSprites mActors;
415
        ActorSprites mDeleteActors;
416
        ActorSpritesMap mActorsIdMap;
417
        IdNameMapping mIdName;
418
        std::set<BeingId> mBlockedBeings;
419
        std::map<int32_t, std::string> mChars;
420
        Map *mMap;
421
#ifdef TMWA_SUPPORT
422
        std::string mSpellHeal1;
423
        std::string mSpellHeal2;
424
        std::string mSpellItenplz;
425
#endif  // TMWA_SUPPORT
426
427
        bool mTargetDeadPlayers;
428
        bool mTargetOnlyReachable;
429
        bool mCyclePlayers;
430
        bool mCycleMonsters;
431
        bool mCycleNPC;
432
        bool mExtMouseTargeting;
433
        bool mEnableIdCollecting;
434
435
#define defVarsP(mob) \
436
        std::list<std::string> mPriority##mob;\
437
        std::set<std::string> mPriority##mob##Set;\
438
        StringIntMap mPriority##mob##Map;
439
440
#define defVars(mob) \
441
        std::list<std::string> m##mob;\
442
        std::set<std::string> m##mob##Set;\
443
        StringIntMap m##mob##Map;\
444
        std::list<std::string> mIgnore##mob;\
445
        std::set<std::string> mIgnore##mob##Set;
446
447
        defVarsP(AttackMobs)
448
        defVars(AttackMobs)
449
        defVars(PickupItems)
450
};
451
452
extern ActorManager *actorManager;
453
454
#endif  // ACTORMANAGER_H