forked from otland/forgottenserver
-
Notifications
You must be signed in to change notification settings - Fork 1
/
creatureevent.cpp
632 lines (509 loc) · 17.8 KB
/
creatureevent.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
// Copyright 2023 The Forgotten Server Authors. All rights reserved.
// Use of this source code is governed by the GPL-2.0 License that can be found in the LICENSE file.
#include "otpch.h"
#include "creatureevent.h"
#include "item.h"
#include "tools.h"
CreatureEvents::CreatureEvents() : scriptInterface("CreatureScript Interface") { scriptInterface.initState(); }
void CreatureEvents::clear(bool fromLua)
{
for (auto it = creatureEvents.begin(); it != creatureEvents.end(); ++it) {
if (fromLua == it->second.fromLua) {
it->second.clearEvent();
}
}
reInitState(fromLua);
}
void CreatureEvents::removeInvalidEvents()
{
for (auto it = creatureEvents.begin(); it != creatureEvents.end();) {
if (it->second.getScriptId() == 0) {
it = creatureEvents.erase(it);
} else {
++it;
}
}
}
LuaScriptInterface& CreatureEvents::getScriptInterface() { return scriptInterface; }
Event_ptr CreatureEvents::getEvent(const std::string& nodeName)
{
if (!caseInsensitiveEqual(nodeName, "event")) {
return nullptr;
}
return Event_ptr(new CreatureEvent(&scriptInterface));
}
bool CreatureEvents::registerEvent(Event_ptr event, const pugi::xml_node&)
{
CreatureEvent_ptr creatureEvent{
static_cast<CreatureEvent*>(event.release())}; // event is guaranteed to be a CreatureEvent
if (creatureEvent->getEventType() == CREATURE_EVENT_NONE) {
std::cout << "Error: [CreatureEvents::registerEvent] Trying to register event without type!" << std::endl;
return false;
}
CreatureEvent* oldEvent = getEventByName(creatureEvent->getName(), false);
if (oldEvent) {
// if there was an event with the same that is not loaded
//(happens when reloading), it is reused
if (!oldEvent->isLoaded() && oldEvent->getEventType() == creatureEvent->getEventType()) {
oldEvent->copyEvent(creatureEvent.get());
}
return false;
}
// if not, register it normally
creatureEvents.emplace(boost::algorithm::to_lower_copy(creatureEvent->getName()), std::move(*creatureEvent));
return true;
}
bool CreatureEvents::registerLuaEvent(CreatureEvent* event)
{
CreatureEvent_ptr creatureEvent{event};
if (creatureEvent->getEventType() == CREATURE_EVENT_NONE) {
std::cout << "Error: [CreatureEvents::registerLuaEvent] Trying to register event without type!" << std::endl;
return false;
}
CreatureEvent* oldEvent = getEventByName(creatureEvent->getName(), false);
if (oldEvent) {
// if there was an event with the same that is not loaded
//(happens when reloading), it is reused
if (!oldEvent->isLoaded() && oldEvent->getEventType() == creatureEvent->getEventType()) {
oldEvent->copyEvent(creatureEvent.get());
}
return false;
}
// if not, register it normally
creatureEvents.emplace(boost::algorithm::to_lower_copy(creatureEvent->getName()), std::move(*creatureEvent));
return true;
}
CreatureEvent* CreatureEvents::getEventByName(const std::string& name, bool forceLoaded /*= true*/)
{
auto it = creatureEvents.find(boost::algorithm::to_lower_copy(name));
if (it != creatureEvents.end()) {
if (!forceLoaded || it->second.isLoaded()) {
return &it->second;
}
}
return nullptr;
}
bool CreatureEvents::playerLogin(Player* player) const
{
// fire global event if is registered
for (const auto& it : creatureEvents) {
if (it.second.getEventType() == CREATURE_EVENT_LOGIN) {
if (!it.second.executeOnLogin(player)) {
return false;
}
}
}
return true;
}
bool CreatureEvents::playerLogout(Player* player) const
{
// fire global event if is registered
for (const auto& it : creatureEvents) {
if (it.second.getEventType() == CREATURE_EVENT_LOGOUT) {
if (!it.second.executeOnLogout(player)) {
return false;
}
}
}
return true;
}
void CreatureEvents::playerReconnect(Player* player) const
{
// fire global event if is registered
for (const auto& it : creatureEvents) {
if (it.second.getEventType() == CREATURE_EVENT_RECONNECT) {
it.second.executeOnReconnect(player);
}
}
}
bool CreatureEvents::playerAdvance(Player* player, skills_t skill, uint32_t oldLevel, uint32_t newLevel)
{
// fire global event if is registered
for (auto& it : creatureEvents) {
if (it.second.getEventType() == CREATURE_EVENT_ADVANCE) {
if (!it.second.executeAdvance(player, skill, oldLevel, newLevel)) {
return false;
}
}
}
return true;
}
/////////////////////////////////////
CreatureEvent::CreatureEvent(LuaScriptInterface* interface) : Event(interface), type(CREATURE_EVENT_NONE), loaded(false)
{}
bool CreatureEvent::configureEvent(const pugi::xml_node& node)
{
// Name that will be used in monster xml files and lua function to register events to reference this event
pugi::xml_attribute nameAttribute = node.attribute("name");
if (!nameAttribute) {
std::cout << "[Error - CreatureEvent::configureEvent] Missing name for creature event" << std::endl;
return false;
}
eventName = nameAttribute.as_string();
pugi::xml_attribute typeAttribute = node.attribute("type");
if (!typeAttribute) {
std::cout << "[Error - CreatureEvent::configureEvent] Missing type for creature event: " << eventName
<< std::endl;
return false;
}
std::string tmpStr = boost::algorithm::to_lower_copy<std::string>(typeAttribute.as_string());
if (tmpStr == "login") {
type = CREATURE_EVENT_LOGIN;
} else if (tmpStr == "logout") {
type = CREATURE_EVENT_LOGOUT;
} else if (tmpStr == "reconnect") {
type = CREATURE_EVENT_RECONNECT;
} else if (tmpStr == "think") {
type = CREATURE_EVENT_THINK;
} else if (tmpStr == "preparedeath") {
type = CREATURE_EVENT_PREPAREDEATH;
} else if (tmpStr == "death") {
type = CREATURE_EVENT_DEATH;
} else if (tmpStr == "kill") {
type = CREATURE_EVENT_KILL;
} else if (tmpStr == "advance") {
type = CREATURE_EVENT_ADVANCE;
} else if (tmpStr == "modalwindow") {
type = CREATURE_EVENT_MODALWINDOW;
} else if (tmpStr == "textedit") {
type = CREATURE_EVENT_TEXTEDIT;
} else if (tmpStr == "healthchange") {
type = CREATURE_EVENT_HEALTHCHANGE;
} else if (tmpStr == "manachange") {
type = CREATURE_EVENT_MANACHANGE;
} else if (tmpStr == "extendedopcode") {
type = CREATURE_EVENT_EXTENDED_OPCODE;
} else {
std::cout << "[Error - CreatureEvent::configureEvent] Invalid type for creature event: " << eventName
<< std::endl;
return false;
}
loaded = true;
return true;
}
std::string_view CreatureEvent::getScriptEventName() const
{
// Depending on the type script event name is different
switch (type) {
case CREATURE_EVENT_LOGIN:
return "onLogin";
case CREATURE_EVENT_LOGOUT:
return "onLogout";
case CREATURE_EVENT_RECONNECT:
return "onReconnect";
case CREATURE_EVENT_THINK:
return "onThink";
case CREATURE_EVENT_PREPAREDEATH:
return "onPrepareDeath";
case CREATURE_EVENT_DEATH:
return "onDeath";
case CREATURE_EVENT_KILL:
return "onKill";
case CREATURE_EVENT_ADVANCE:
return "onAdvance";
case CREATURE_EVENT_MODALWINDOW:
return "onModalWindow";
case CREATURE_EVENT_TEXTEDIT:
return "onTextEdit";
case CREATURE_EVENT_HEALTHCHANGE:
return "onHealthChange";
case CREATURE_EVENT_MANACHANGE:
return "onManaChange";
case CREATURE_EVENT_EXTENDED_OPCODE:
return "onExtendedOpcode";
case CREATURE_EVENT_NONE:
default:
return "";
}
}
void CreatureEvent::copyEvent(CreatureEvent* creatureEvent)
{
scriptId = creatureEvent->scriptId;
scriptInterface = creatureEvent->scriptInterface;
scripted = creatureEvent->scripted;
loaded = creatureEvent->loaded;
}
void CreatureEvent::clearEvent()
{
scriptId = 0;
scriptInterface = nullptr;
scripted = false;
loaded = false;
}
bool CreatureEvent::executeOnLogin(Player* player) const
{
// onLogin(player)
if (!tfs::lua::reserveScriptEnv()) {
std::cout << "[Error - CreatureEvent::executeOnLogin] Call stack overflow" << std::endl;
return false;
}
ScriptEnvironment* env = tfs::lua::getScriptEnv();
env->setScriptId(scriptId, scriptInterface);
lua_State* L = scriptInterface->getLuaState();
scriptInterface->pushFunction(scriptId);
tfs::lua::pushUserdata(L, player);
tfs::lua::setMetatable(L, -1, "Player");
return scriptInterface->callFunction(1);
}
bool CreatureEvent::executeOnLogout(Player* player) const
{
// onLogout(player)
if (!tfs::lua::reserveScriptEnv()) {
std::cout << "[Error - CreatureEvent::executeOnLogout] Call stack overflow" << std::endl;
return false;
}
ScriptEnvironment* env = tfs::lua::getScriptEnv();
env->setScriptId(scriptId, scriptInterface);
lua_State* L = scriptInterface->getLuaState();
scriptInterface->pushFunction(scriptId);
tfs::lua::pushUserdata(L, player);
tfs::lua::setMetatable(L, -1, "Player");
return scriptInterface->callFunction(1);
}
void CreatureEvent::executeOnReconnect(Player* player) const
{
// onReconnect(player)
if (!tfs::lua::reserveScriptEnv()) {
std::cout << "[Error - CreatureEvent::executeOnReconnect] Call stack overflow" << std::endl;
return;
}
ScriptEnvironment* env = tfs::lua::getScriptEnv();
env->setScriptId(scriptId, scriptInterface);
lua_State* L = scriptInterface->getLuaState();
scriptInterface->pushFunction(scriptId);
tfs::lua::pushUserdata(L, player);
tfs::lua::setMetatable(L, -1, "Player");
scriptInterface->callFunction(1);
}
bool CreatureEvent::executeOnThink(Creature* creature, uint32_t interval)
{
// onThink(creature, interval)
if (!tfs::lua::reserveScriptEnv()) {
std::cout << "[Error - CreatureEvent::executeOnThink] Call stack overflow" << std::endl;
return false;
}
ScriptEnvironment* env = tfs::lua::getScriptEnv();
env->setScriptId(scriptId, scriptInterface);
lua_State* L = scriptInterface->getLuaState();
scriptInterface->pushFunction(scriptId);
tfs::lua::pushUserdata(L, creature);
tfs::lua::setCreatureMetatable(L, -1, creature);
lua_pushnumber(L, interval);
return scriptInterface->callFunction(2);
}
bool CreatureEvent::executeOnPrepareDeath(Creature* creature, Creature* killer)
{
// onPrepareDeath(creature, killer)
if (!tfs::lua::reserveScriptEnv()) {
std::cout << "[Error - CreatureEvent::executeOnPrepareDeath] Call stack overflow" << std::endl;
return false;
}
ScriptEnvironment* env = tfs::lua::getScriptEnv();
env->setScriptId(scriptId, scriptInterface);
lua_State* L = scriptInterface->getLuaState();
scriptInterface->pushFunction(scriptId);
tfs::lua::pushUserdata(L, creature);
tfs::lua::setCreatureMetatable(L, -1, creature);
if (killer) {
tfs::lua::pushUserdata(L, killer);
tfs::lua::setCreatureMetatable(L, -1, killer);
} else {
lua_pushnil(L);
}
return scriptInterface->callFunction(2);
}
bool CreatureEvent::executeOnDeath(Creature* creature, Item* corpse, Creature* killer, Creature* mostDamageKiller,
bool lastHitUnjustified, bool mostDamageUnjustified)
{
// onDeath(creature, corpse, killer, mostDamageKiller, lastHitUnjustified, mostDamageUnjustified)
if (!tfs::lua::reserveScriptEnv()) {
std::cout << "[Error - CreatureEvent::executeOnDeath] Call stack overflow" << std::endl;
return false;
}
ScriptEnvironment* env = tfs::lua::getScriptEnv();
env->setScriptId(scriptId, scriptInterface);
lua_State* L = scriptInterface->getLuaState();
scriptInterface->pushFunction(scriptId);
tfs::lua::pushUserdata(L, creature);
tfs::lua::setCreatureMetatable(L, -1, creature);
tfs::lua::pushThing(L, corpse);
if (killer) {
tfs::lua::pushUserdata(L, killer);
tfs::lua::setCreatureMetatable(L, -1, killer);
} else {
lua_pushnil(L);
}
if (mostDamageKiller) {
tfs::lua::pushUserdata(L, mostDamageKiller);
tfs::lua::setCreatureMetatable(L, -1, mostDamageKiller);
} else {
lua_pushnil(L);
}
tfs::lua::pushBoolean(L, lastHitUnjustified);
tfs::lua::pushBoolean(L, mostDamageUnjustified);
return scriptInterface->callFunction(6);
}
bool CreatureEvent::executeAdvance(Player* player, skills_t skill, uint32_t oldLevel, uint32_t newLevel)
{
// onAdvance(player, skill, oldLevel, newLevel)
if (!tfs::lua::reserveScriptEnv()) {
std::cout << "[Error - CreatureEvent::executeAdvance] Call stack overflow" << std::endl;
return false;
}
ScriptEnvironment* env = tfs::lua::getScriptEnv();
env->setScriptId(scriptId, scriptInterface);
lua_State* L = scriptInterface->getLuaState();
scriptInterface->pushFunction(scriptId);
tfs::lua::pushUserdata(L, player);
tfs::lua::setMetatable(L, -1, "Player");
lua_pushnumber(L, static_cast<uint32_t>(skill));
lua_pushnumber(L, oldLevel);
lua_pushnumber(L, newLevel);
return scriptInterface->callFunction(4);
}
void CreatureEvent::executeOnKill(Creature* creature, Creature* target)
{
// onKill(creature, target)
if (!tfs::lua::reserveScriptEnv()) {
std::cout << "[Error - CreatureEvent::executeOnKill] Call stack overflow" << std::endl;
return;
}
ScriptEnvironment* env = tfs::lua::getScriptEnv();
env->setScriptId(scriptId, scriptInterface);
lua_State* L = scriptInterface->getLuaState();
scriptInterface->pushFunction(scriptId);
tfs::lua::pushUserdata(L, creature);
tfs::lua::setCreatureMetatable(L, -1, creature);
tfs::lua::pushUserdata(L, target);
tfs::lua::setCreatureMetatable(L, -1, target);
scriptInterface->callVoidFunction(2);
}
void CreatureEvent::executeModalWindow(Player* player, uint32_t modalWindowId, uint8_t buttonId, uint8_t choiceId)
{
// onModalWindow(player, modalWindowId, buttonId, choiceId)
if (!tfs::lua::reserveScriptEnv()) {
std::cout << "[Error - CreatureEvent::executeModalWindow] Call stack overflow" << std::endl;
return;
}
ScriptEnvironment* env = tfs::lua::getScriptEnv();
env->setScriptId(scriptId, scriptInterface);
lua_State* L = scriptInterface->getLuaState();
scriptInterface->pushFunction(scriptId);
tfs::lua::pushUserdata(L, player);
tfs::lua::setMetatable(L, -1, "Player");
lua_pushnumber(L, modalWindowId);
lua_pushnumber(L, buttonId);
lua_pushnumber(L, choiceId);
scriptInterface->callVoidFunction(4);
}
bool CreatureEvent::executeTextEdit(Player* player, Item* item, std::string_view text, const uint32_t windowTextId)
{
// onTextEdit(player, item, text, windowTextId)
if (!tfs::lua::reserveScriptEnv()) {
std::cout << "[Error - CreatureEvent::executeTextEdit] Call stack overflow" << std::endl;
return false;
}
ScriptEnvironment* env = tfs::lua::getScriptEnv();
env->setScriptId(scriptId, scriptInterface);
lua_State* L = scriptInterface->getLuaState();
scriptInterface->pushFunction(scriptId);
tfs::lua::pushUserdata(L, player);
tfs::lua::setMetatable(L, -1, "Player");
tfs::lua::pushThing(L, item);
tfs::lua::pushString(L, text);
lua_pushinteger(L, windowTextId);
return scriptInterface->callFunction(4);
}
namespace {
void pushCombatDamage(lua_State* L, const CombatDamage& damage)
{
lua_pushnumber(L, damage.primary.value);
lua_pushnumber(L, damage.primary.type);
lua_pushnumber(L, damage.secondary.value);
lua_pushnumber(L, damage.secondary.type);
lua_pushnumber(L, damage.origin);
}
} // namespace
void CreatureEvent::executeHealthChange(Creature* creature, Creature* attacker, CombatDamage& damage)
{
// onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
if (!tfs::lua::reserveScriptEnv()) {
std::cout << "[Error - CreatureEvent::executeHealthChange] Call stack overflow" << std::endl;
return;
}
ScriptEnvironment* env = tfs::lua::getScriptEnv();
env->setScriptId(scriptId, scriptInterface);
lua_State* L = scriptInterface->getLuaState();
scriptInterface->pushFunction(scriptId);
tfs::lua::pushUserdata(L, creature);
tfs::lua::setCreatureMetatable(L, -1, creature);
if (attacker) {
tfs::lua::pushUserdata(L, attacker);
tfs::lua::setCreatureMetatable(L, -1, attacker);
} else {
lua_pushnil(L);
}
pushCombatDamage(L, damage);
if (tfs::lua::protectedCall(L, 7, 4) != 0) {
reportErrorFunc(nullptr, tfs::lua::popString(L));
} else {
damage.primary.value = std::abs(tfs::lua::getNumber<int32_t>(L, -4));
damage.primary.type = tfs::lua::getNumber<CombatType_t>(L, -3);
damage.secondary.value = std::abs(tfs::lua::getNumber<int32_t>(L, -2));
damage.secondary.type = tfs::lua::getNumber<CombatType_t>(L, -1);
lua_pop(L, 4);
if (damage.primary.type != COMBAT_HEALING) {
damage.primary.value = -damage.primary.value;
damage.secondary.value = -damage.secondary.value;
}
}
tfs::lua::resetScriptEnv();
}
void CreatureEvent::executeManaChange(Creature* creature, Creature* attacker, CombatDamage& damage)
{
// onManaChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
if (!tfs::lua::reserveScriptEnv()) {
std::cout << "[Error - CreatureEvent::executeManaChange] Call stack overflow" << std::endl;
return;
}
ScriptEnvironment* env = tfs::lua::getScriptEnv();
env->setScriptId(scriptId, scriptInterface);
lua_State* L = scriptInterface->getLuaState();
scriptInterface->pushFunction(scriptId);
tfs::lua::pushUserdata(L, creature);
tfs::lua::setCreatureMetatable(L, -1, creature);
if (attacker) {
tfs::lua::pushUserdata(L, attacker);
tfs::lua::setCreatureMetatable(L, -1, attacker);
} else {
lua_pushnil(L);
}
pushCombatDamage(L, damage);
if (tfs::lua::protectedCall(L, 7, 4) != 0) {
reportErrorFunc(nullptr, tfs::lua::popString(L));
} else {
damage.primary.value = tfs::lua::getNumber<int32_t>(L, -4);
damage.primary.type = tfs::lua::getNumber<CombatType_t>(L, -3);
damage.secondary.value = tfs::lua::getNumber<int32_t>(L, -2);
damage.secondary.type = tfs::lua::getNumber<CombatType_t>(L, -1);
lua_pop(L, 4);
}
tfs::lua::resetScriptEnv();
}
void CreatureEvent::executeExtendedOpcode(Player* player, uint8_t opcode, const std::string& buffer)
{
// onExtendedOpcode(player, opcode, buffer)
if (!tfs::lua::reserveScriptEnv()) {
std::cout << "[Error - CreatureEvent::executeExtendedOpcode] Call stack overflow" << std::endl;
return;
}
ScriptEnvironment* env = tfs::lua::getScriptEnv();
env->setScriptId(scriptId, scriptInterface);
lua_State* L = scriptInterface->getLuaState();
scriptInterface->pushFunction(scriptId);
tfs::lua::pushUserdata(L, player);
tfs::lua::setMetatable(L, -1, "Player");
lua_pushnumber(L, opcode);
tfs::lua::pushString(L, buffer);
scriptInterface->callVoidFunction(3);
}