-
-
Notifications
You must be signed in to change notification settings - Fork 664
/
Copy pathzone.cpp
278 lines (241 loc) · 6.87 KB
/
zone.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/**
* Canary - A free and open-source MMORPG server emulator
* Copyright (©) 2019-2024 OpenTibiaBR <opentibiabr@outlook.com>
* Repository: https://github.com/opentibiabr/canary
* License: https://github.com/opentibiabr/canary/blob/main/LICENSE
* Contributors: https://github.com/opentibiabr/canary/graphs/contributors
* Website: https://docs.opentibiabr.com/
*/
#include "pch.hpp"
#include "zone.hpp"
#include "game/game.hpp"
#include "creatures/monsters/monster.hpp"
#include "creatures/npcs/npc.hpp"
#include "creatures/players/player.hpp"
#include "utils/pugicast.hpp"
phmap::parallel_flat_hash_map<std::string, std::shared_ptr<Zone>> Zone::zones = {};
phmap::parallel_flat_hash_map<uint32_t, std::shared_ptr<Zone>> Zone::zonesByID = {};
const static std::shared_ptr<Zone> nullZone = nullptr;
std::shared_ptr<Zone> Zone::addZone(const std::string &name, uint32_t zoneID /* = 0 */) {
if (name == "default") {
g_logger().error("Zone name {} is reserved", name);
return nullZone;
}
if (zoneID != 0 && zonesByID.contains(zoneID)) {
g_logger().trace("[Zone::addZone] Found with ID {} while adding {}, linking them together...", zoneID, name);
auto zone = zonesByID[zoneID];
zone->name = name;
zones[name] = zone;
return zone;
}
if (zones[name]) {
g_logger().error("Zone {} already exists", name);
return nullZone;
}
zones[name] = std::make_shared<Zone>(name, zoneID);
if (zoneID != 0) {
zonesByID[zoneID] = zones[name];
}
return zones[name];
}
void Zone::addArea(Area area) {
for (const auto &pos : area) {
addPosition(pos);
}
refresh();
}
void Zone::subtractArea(Area area) {
for (const auto &pos : area) {
removePosition(pos);
}
refresh();
}
bool Zone::contains(const Position &pos) const {
return positions.contains(pos);
}
Position Zone::getRemoveDestination(const std::shared_ptr<Creature> &creature /* = nullptr */) const {
if (!creature || !creature->getPlayer()) {
return Position();
}
if (removeDestination != Position()) {
return removeDestination;
}
if (creature->getPlayer()) {
return creature->getPlayer()->getTown()->getTemplePosition();
}
return Position();
}
std::shared_ptr<Zone> Zone::getZone(const std::string &name) {
return zones[name];
}
std::shared_ptr<Zone> Zone::getZone(uint32_t zoneID) {
if (zoneID == 0) {
return nullZone;
}
if (zonesByID.contains(zoneID)) {
return zonesByID[zoneID];
}
auto zone = std::make_shared<Zone>(zoneID);
zonesByID[zoneID] = zone;
return zone;
}
std::vector<Position> Zone::getPositions() const {
std::vector<Position> result;
for (const auto &pos : positions) {
result.push_back(pos);
}
return result;
}
std::vector<std::shared_ptr<Creature>> Zone::getCreatures() {
return weak::lock(creaturesCache);
}
std::vector<std::shared_ptr<Player>> Zone::getPlayers() {
return weak::lock(playersCache);
}
std::vector<std::shared_ptr<Monster>> Zone::getMonsters() {
return weak::lock(monstersCache);
}
std::vector<std::shared_ptr<Npc>> Zone::getNpcs() {
return weak::lock(npcsCache);
}
std::vector<std::shared_ptr<Item>> Zone::getItems() {
return weak::lock(itemsCache);
}
void Zone::removePlayers() {
for (const auto &player : getPlayers()) {
g_game().internalTeleport(player, getRemoveDestination(player));
}
}
void Zone::removeMonsters() {
for (const auto &monster : getMonsters()) {
g_game().removeCreature(monster->getCreature());
}
}
void Zone::removeNpcs() {
for (const auto &npc : getNpcs()) {
g_game().removeCreature(npc->getCreature());
}
}
void Zone::clearZones() {
for (const auto &[_, zone] : zones) {
// do not clear zones loaded from the map (id > 0)
if (!zone || zone->isStatic()) {
continue;
}
zone->refresh();
}
zones.clear();
for (const auto &[_, zone] : zonesByID) {
zones[zone->name] = zone;
}
}
std::vector<std::shared_ptr<Zone>> Zone::getZones(const Position position) {
Benchmark bm_getZones;
std::vector<std::shared_ptr<Zone>> result;
for (const auto &[_, zone] : zones) {
if (zone && zone->contains(position)) {
result.push_back(zone);
}
}
auto duration = bm_getZones.duration();
if (duration > 100) {
g_logger().warn("Listed {} zones for position {} in {} milliseconds", result.size(), position.toString(), duration);
}
return result;
}
std::vector<std::shared_ptr<Zone>> Zone::getZones() {
Benchmark bm_getZones;
std::vector<std::shared_ptr<Zone>> result;
for (const auto &[_, zone] : zones) {
if (zone) {
result.push_back(zone);
}
}
auto duration = bm_getZones.duration();
if (duration > 100) {
g_logger().warn("Listed {} zones in {} milliseconds", result.size(), duration);
}
return result;
}
void Zone::creatureAdded(const std::shared_ptr<Creature> &creature) {
if (!creature) {
return;
}
if (const auto &player = creature->getPlayer()) {
playersCache.insert(player);
} else if (const auto &monster = creature->getMonster()) {
monstersCache.insert(monster);
} else if (const auto &npc = creature->getNpc()) {
npcsCache.insert(npc);
}
creaturesCache.insert(creature);
}
void Zone::creatureRemoved(const std::shared_ptr<Creature> &creature) {
if (!creature) {
return;
}
creaturesCache.erase(creature);
playersCache.erase(creature->getPlayer());
monstersCache.erase(creature->getMonster());
npcsCache.erase(creature->getNpc());
}
void Zone::thingAdded(const std::shared_ptr<Thing> &thing) {
if (!thing) {
return;
}
if (const auto &item = thing->getItem()) {
itemAdded(item);
} else if (const auto &creature = thing->getCreature()) {
creatureAdded(creature);
}
}
void Zone::itemAdded(const std::shared_ptr<Item> &item) {
if (!item) {
return;
}
itemsCache.insert(item);
}
void Zone::itemRemoved(const std::shared_ptr<Item> &item) {
if (!item) {
return;
}
itemsCache.erase(item);
}
void Zone::refresh() {
Benchmark bm_refresh;
creaturesCache.clear();
monstersCache.clear();
npcsCache.clear();
playersCache.clear();
itemsCache.clear();
for (const auto &position : getPositions()) {
g_game().map.refreshZones(position);
}
g_logger().trace("Refreshed zone '{}' in {} milliseconds", name, bm_refresh.duration());
}
void Zone::setMonsterVariant(const std::string &variant) {
monsterVariant = variant;
g_logger().debug("Zone {} monster variant set to {}", name, variant);
for (auto &spawnMonster : g_game().map.spawnsMonster.getspawnMonsterList()) {
if (!contains(spawnMonster.getCenterPos())) {
continue;
}
spawnMonster.setMonsterVariant(variant);
}
removeMonsters();
}
bool Zone::loadFromXML(const std::string &fileName, uint16_t shiftID /* = 0 */) {
pugi::xml_document doc;
g_logger().debug("Loading zones from {}", fileName);
pugi::xml_parse_result result = doc.load_file(fileName.c_str());
if (!result) {
printXMLError(__FUNCTION__, fileName, result);
return false;
}
for (auto zoneNode : doc.child("zones").children()) {
auto name = zoneNode.attribute("name").value();
auto zoneId = pugi::cast<uint32_t>(zoneNode.attribute("zoneid").value()) << shiftID;
addZone(name, zoneId);
}
return true;
}