forked from opentibiabr/canary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_environment.cpp
178 lines (150 loc) · 4.2 KB
/
script_environment.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
/**
* 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 "game/game.hpp"
#include "lua/scripts/luascript.hpp"
#include "lua/scripts/script_environment.hpp"
ScriptEnvironment::ScriptEnvironment() {
resetEnv();
}
ScriptEnvironment::~ScriptEnvironment() {
resetEnv();
}
void ScriptEnvironment::resetEnv() {
scriptId = 0;
callbackId = 0;
timerEvent = false;
interface = nullptr;
localMap.clear();
tempResults.clear();
auto pair = tempItems.equal_range(this);
auto it = pair.first;
while (it != pair.second) {
std::shared_ptr<Item> item = it->second;
it = tempItems.erase(it);
}
}
bool ScriptEnvironment::setCallbackId(int32_t newCallbackId, LuaScriptInterface* scriptInterface) {
if (this->callbackId != 0) {
// nested callbacks are not allowed
if (interface) {
interface->reportErrorFunc("Nested callbacks!");
}
return false;
}
this->callbackId = newCallbackId;
interface = scriptInterface;
return true;
}
void ScriptEnvironment::getEventInfo(int32_t &retScriptId, LuaScriptInterface*&retScriptInterface, int32_t &retCallbackId, bool &retTimerEvent) const {
retScriptId = this->scriptId;
retScriptInterface = interface;
retCallbackId = this->callbackId;
retTimerEvent = this->timerEvent;
}
uint32_t ScriptEnvironment::addThing(std::shared_ptr<Thing> thing) {
if (!thing || thing->isRemoved()) {
return 0;
}
std::shared_ptr<Creature> creature = thing->getCreature();
if (creature) {
return creature->getID();
}
std::shared_ptr<Item> item = thing->getItem();
if (item && item->hasAttribute(ItemAttribute_t::UNIQUEID)) {
return item->getAttribute<uint32_t>(ItemAttribute_t::UNIQUEID);
}
for (const auto &it : localMap) {
if (it.second == item) {
return it.first;
}
}
localMap[++lastUID] = item;
return lastUID;
}
void ScriptEnvironment::insertItem(uint32_t uid, std::shared_ptr<Item> item) {
auto result = localMap.emplace(uid, item);
if (!result.second) {
g_logger().error("Thing uid already taken: {}", uid);
}
}
std::shared_ptr<Thing> ScriptEnvironment::getThingByUID(uint32_t uid) {
if (uid >= 0x10000000) {
return g_game().getCreatureByID(uid);
}
if (uid <= std::numeric_limits<uint16_t>::max()) {
std::shared_ptr<Item> item = g_game().getUniqueItem(static_cast<uint16_t>(uid));
if (item && !item->isRemoved()) {
return item;
}
return nullptr;
}
auto it = localMap.find(uid);
if (it != localMap.end()) {
std::shared_ptr<Item> item = it->second;
if (!item->isRemoved()) {
return item;
}
}
return nullptr;
}
std::shared_ptr<Item> ScriptEnvironment::getItemByUID(uint32_t uid) {
std::shared_ptr<Thing> thing = getThingByUID(uid);
if (!thing) {
return nullptr;
}
return thing->getItem();
}
std::shared_ptr<Container> ScriptEnvironment::getContainerByUID(uint32_t uid) {
std::shared_ptr<Item> item = getItemByUID(uid);
if (!item) {
return nullptr;
}
return item->getContainer();
}
void ScriptEnvironment::removeItemByUID(uint32_t uid) {
if (uid <= std::numeric_limits<uint16_t>::max()) {
g_game().removeUniqueItem(static_cast<uint16_t>(uid));
return;
}
auto it = localMap.find(uid);
if (it != localMap.end()) {
localMap.erase(it);
}
}
void ScriptEnvironment::addTempItem(std::shared_ptr<Item> item) {
tempItems.emplace(this, item);
}
void ScriptEnvironment::removeTempItem(std::shared_ptr<Item> item) {
for (auto it = tempItems.begin(), end = tempItems.end(); it != end; ++it) {
if (it->second == item) {
tempItems.erase(it);
break;
}
}
}
uint32_t ScriptEnvironment::addResult(DBResult_ptr res) {
tempResults[++lastResultId] = res;
return lastResultId;
}
bool ScriptEnvironment::removeResult(uint32_t id) {
auto it = tempResults.find(id);
if (it == tempResults.end()) {
return false;
}
tempResults.erase(it);
return true;
}
DBResult_ptr ScriptEnvironment::getResultByID(uint32_t id) {
auto it = tempResults.find(id);
if (it == tempResults.end()) {
return nullptr;
}
return it->second;
}