spawn.h

main 104 lines · 2.6 KB Raw
Alex Alex Commit Server upload 18/10/2026 23:38
1/**
2 * The Forgotten Server - a free and open-source MMORPG server emulator
3 * Copyright (C) 2019 Mark Samman <mark.samman@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#ifndef FS_SPAWN_H_1A86089E080846A9AE53ED12E7AE863B
21#define FS_SPAWN_H_1A86089E080846A9AE53ED12E7AE863B
22
23#include "tile.h"
24#include "position.h"
25
26class Monster;
27class MonsterType;
28class Npc;
29
30struct spawnBlock_t {
31 Position pos;
32 MonsterType* mType;
33 int64_t lastSpawn;
34 uint32_t interval;
35 Direction direction;
36};
37
38class Spawn
39{
40 public:
41 Spawn(Position pos, int32_t radius) : centerPos(std::move(pos)), radius(radius) {}
42 ~Spawn();
43
44 // non-copyable
45 Spawn(const Spawn&) = delete;
46 Spawn& operator=(const Spawn&) = delete;
47
48 bool addMonster(const std::string& name, const Position& pos, Direction dir, uint32_t interval);
49 void removeMonster(Monster* monster);
50
51 uint32_t getInterval() const {
52 return interval;
53 }
54 void startup();
55
56 void startSpawnCheck();
57 void stopEvent();
58
59 bool isInSpawnZone(const Position& pos);
60 void cleanup();
61
62 private:
63 //map of the spawned creatures
64 using SpawnedMap = std::multimap<uint32_t, Monster*>;
65 using spawned_pair = SpawnedMap::value_type;
66 SpawnedMap spawnedMap;
67
68 //map of creatures in the spawn
69 std::map<uint32_t, spawnBlock_t> spawnMap;
70
71 Position centerPos;
72 int32_t radius;
73
74 uint32_t interval = 60000;
75 uint32_t checkSpawnEvent = 0;
76
77 static bool findPlayer(const Position& pos);
78 bool spawnMonster(uint32_t spawnId, MonsterType* mType, const Position& pos, Direction dir, bool startup = false);
79 void checkSpawn();
80};
81
82class Spawns
83{
84 public:
85 static bool isInZone(const Position& centerPos, int32_t radius, const Position& pos);
86
87 bool loadFromXml(const std::string& filename);
88 void startup();
89 void clear();
90
91 bool isStarted() const {
92 return started;
93 }
94
95 private:
96 std::forward_list<Npc*> npcList;
97 std::forward_list<Spawn> spawnList;
98 std::string filename;
99 bool loaded = false;
100 bool started = false;
101};
102
103#endif
104
Top