forked from dbjorkholm/forgottenserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.cpp
507 lines (424 loc) · 14.5 KB
/
actions.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
/**
* The Forgotten Server - a free and open-source MMORPG server emulator
* Copyright (C) 2018 Mark Samman <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "otpch.h"
#include "actions.h"
#include "bed.h"
#include "configmanager.h"
#include "container.h"
#include "game.h"
#include "pugicast.h"
#include "spells.h"
extern Game g_game;
extern Spells* g_spells;
extern Actions* g_actions;
extern ConfigManager g_config;
Actions::Actions() :
scriptInterface("Action Interface")
{
scriptInterface.initState();
}
Actions::~Actions()
{
clear();
}
void Actions::clearMap(ActionUseMap& map)
{
map.clear();
}
void Actions::clear()
{
clearMap(useItemMap);
clearMap(uniqueItemMap);
clearMap(actionItemMap);
scriptInterface.reInitState();
}
LuaScriptInterface& Actions::getScriptInterface()
{
return scriptInterface;
}
std::string Actions::getScriptBaseName() const
{
return "actions";
}
Event_ptr Actions::getEvent(const std::string& nodeName)
{
if (strcasecmp(nodeName.c_str(), "action") != 0) {
return nullptr;
}
return Event_ptr(new Action(&scriptInterface));
}
bool Actions::registerEvent(Event_ptr event, const pugi::xml_node& node)
{
Action_ptr action{static_cast<Action*>(event.release())}; //event is guaranteed to be an Action
pugi::xml_attribute attr;
if ((attr = node.attribute("itemid"))) {
uint16_t id = pugi::cast<uint16_t>(attr.value());
auto result = useItemMap.emplace(id, std::move(*action));
if (!result.second) {
std::cout << "[Warning - Actions::registerEvent] Duplicate registered item with id: " << id << std::endl;
}
return result.second;
} else if ((attr = node.attribute("fromid"))) {
pugi::xml_attribute toIdAttribute = node.attribute("toid");
if (!toIdAttribute) {
std::cout << "[Warning - Actions::registerEvent] Missing toid in fromid: " << attr.as_string() << std::endl;
return false;
}
uint16_t fromId = pugi::cast<uint16_t>(attr.value());
uint16_t iterId = fromId;
uint16_t toId = pugi::cast<uint16_t>(toIdAttribute.value());
auto result = useItemMap.emplace(iterId, *action);
if (!result.second) {
std::cout << "[Warning - Actions::registerEvent] Duplicate registered item with id: " << iterId << " in fromid: " << fromId << ", toid: " << toId << std::endl;
}
bool success = result.second;
while (++iterId <= toId) {
result = useItemMap.emplace(iterId, *action);
if (!result.second) {
std::cout << "[Warning - Actions::registerEvent] Duplicate registered item with id: " << iterId << " in fromid: " << fromId << ", toid: " << toId << std::endl;
continue;
}
success = true;
}
return success;
} else if ((attr = node.attribute("uniqueid"))) {
uint16_t uid = pugi::cast<uint16_t>(attr.value());
auto result = uniqueItemMap.emplace(uid, std::move(*action));
if (!result.second) {
std::cout << "[Warning - Actions::registerEvent] Duplicate registered item with uniqueid: " << uid << std::endl;
}
return result.second;
} else if ((attr = node.attribute("fromuid"))) {
pugi::xml_attribute toUidAttribute = node.attribute("touid");
if (!toUidAttribute) {
std::cout << "[Warning - Actions::registerEvent] Missing touid in fromuid: " << attr.as_string() << std::endl;
return false;
}
uint16_t fromUid = pugi::cast<uint16_t>(attr.value());
uint16_t iterUid = fromUid;
uint16_t toUid = pugi::cast<uint16_t>(toUidAttribute.value());
auto result = uniqueItemMap.emplace(iterUid, *action);
if (!result.second) {
std::cout << "[Warning - Actions::registerEvent] Duplicate registered item with unique id: " << iterUid << " in fromuid: " << fromUid << ", touid: " << toUid << std::endl;
}
bool success = result.second;
while (++iterUid <= toUid) {
result = uniqueItemMap.emplace(iterUid, *action);
if (!result.second) {
std::cout << "[Warning - Actions::registerEvent] Duplicate registered item with unique id: " << iterUid << " in fromuid: " << fromUid << ", touid: " << toUid << std::endl;
continue;
}
success = true;
}
return success;
} else if ((attr = node.attribute("actionid"))) {
uint16_t aid = pugi::cast<uint16_t>(attr.value());
auto result = actionItemMap.emplace(aid, std::move(*action));
if (!result.second) {
std::cout << "[Warning - Actions::registerEvent] Duplicate registered item with actionid: " << aid << std::endl;
}
return result.second;
} else if ((attr = node.attribute("fromaid"))) {
pugi::xml_attribute toAidAttribute = node.attribute("toaid");
if (!toAidAttribute) {
std::cout << "[Warning - Actions::registerEvent] Missing toaid in fromaid: " << attr.as_string() << std::endl;
return false;
}
uint16_t fromAid = pugi::cast<uint16_t>(attr.value());
uint16_t iterAid = fromAid;
uint16_t toAid = pugi::cast<uint16_t>(toAidAttribute.value());
auto result = actionItemMap.emplace(iterAid, *action);
if (!result.second) {
std::cout << "[Warning - Actions::registerEvent] Duplicate registered item with action id: " << iterAid << " in fromaid: " << fromAid << ", toaid: " << toAid << std::endl;
}
bool success = result.second;
while (++iterAid <= toAid) {
result = actionItemMap.emplace(iterAid, *action);
if (!result.second) {
std::cout << "[Warning - Actions::registerEvent] Duplicate registered item with action id: " << iterAid << " in fromaid: " << fromAid << ", toaid: " << toAid << std::endl;
continue;
}
success = true;
}
return success;
}
return false;
}
ReturnValue Actions::canUse(const Player* player, const Position& pos)
{
if (pos.x != 0xFFFF) {
const Position& playerPos = player->getPosition();
if (playerPos.z != pos.z) {
return playerPos.z > pos.z ? RETURNVALUE_FIRSTGOUPSTAIRS : RETURNVALUE_FIRSTGODOWNSTAIRS;
}
if (!Position::areInRange<1, 1>(playerPos, pos)) {
return RETURNVALUE_TOOFARAWAY;
}
}
return RETURNVALUE_NOERROR;
}
ReturnValue Actions::canUse(const Player* player, const Position& pos, const Item* item)
{
Action* action = getAction(item);
if (action) {
return action->canExecuteAction(player, pos);
}
return RETURNVALUE_NOERROR;
}
ReturnValue Actions::canUseFar(const Creature* creature, const Position& toPos, bool checkLineOfSight, bool checkFloor)
{
if (toPos.x == 0xFFFF) {
return RETURNVALUE_NOERROR;
}
const Position& creaturePos = creature->getPosition();
if (checkFloor && creaturePos.z != toPos.z) {
return creaturePos.z > toPos.z ? RETURNVALUE_FIRSTGOUPSTAIRS : RETURNVALUE_FIRSTGODOWNSTAIRS;
}
if (!Position::areInRange<7, 5>(toPos, creaturePos)) {
return RETURNVALUE_TOOFARAWAY;
}
if (checkLineOfSight && !g_game.canThrowObjectTo(creaturePos, toPos)) {
return RETURNVALUE_CANNOTTHROW;
}
return RETURNVALUE_NOERROR;
}
Action* Actions::getAction(const Item* item)
{
if (item->hasAttribute(ITEM_ATTRIBUTE_UNIQUEID)) {
auto it = uniqueItemMap.find(item->getUniqueId());
if (it != uniqueItemMap.end()) {
return &it->second;
}
}
if (item->hasAttribute(ITEM_ATTRIBUTE_ACTIONID)) {
auto it = actionItemMap.find(item->getActionId());
if (it != actionItemMap.end()) {
return &it->second;
}
}
auto it = useItemMap.find(item->getID());
if (it != useItemMap.end()) {
return &it->second;
}
//rune items
return g_spells->getRuneSpell(item->getID());
}
ReturnValue Actions::internalUseItem(Player* player, const Position& pos, uint8_t index, Item* item, bool isHotkey)
{
if (Door* door = item->getDoor()) {
if (!door->canUse(player)) {
return RETURNVALUE_CANNOTUSETHISOBJECT;
}
}
Action* action = getAction(item);
if (action) {
if (action->isScripted()) {
if (action->executeUse(player, item, pos, nullptr, pos, isHotkey)) {
return RETURNVALUE_NOERROR;
}
if (item->isRemoved()) {
return RETURNVALUE_CANNOTUSETHISOBJECT;
}
} else if (action->function) {
if (action->function(player, item, pos, nullptr, pos, isHotkey)) {
return RETURNVALUE_NOERROR;
}
}
}
if (BedItem* bed = item->getBed()) {
if (!bed->canUse(player)) {
return RETURNVALUE_CANNOTUSETHISOBJECT;
}
if (bed->trySleep(player)) {
player->setBedItem(bed);
g_game.sendOfflineTrainingDialog(player);
}
return RETURNVALUE_NOERROR;
}
if (Container* container = item->getContainer()) {
Container* openContainer;
//depot container
if (DepotLocker* depot = container->getDepotLocker()) {
DepotLocker* myDepotLocker = player->getDepotLocker(depot->getDepotId());
myDepotLocker->setParent(depot->getParent()->getTile());
openContainer = myDepotLocker;
player->setLastDepotId(depot->getDepotId());
} else {
openContainer = container;
}
uint32_t corpseOwner = container->getCorpseOwner();
if (corpseOwner != 0 && !player->canOpenCorpse(corpseOwner)) {
return RETURNVALUE_YOUARENOTTHEOWNER;
}
//open/close container
int32_t oldContainerId = player->getContainerID(openContainer);
if (oldContainerId != -1) {
player->onCloseContainer(openContainer);
player->closeContainer(oldContainerId);
} else {
player->addContainer(index, openContainer);
player->onSendContainer(openContainer);
}
return RETURNVALUE_NOERROR;
}
const ItemType& it = Item::items[item->getID()];
if (it.canReadText) {
if (it.canWriteText) {
player->setWriteItem(item, it.maxTextLen);
player->sendTextWindow(item, it.maxTextLen, true);
} else {
player->setWriteItem(nullptr);
player->sendTextWindow(item, 0, false);
}
return RETURNVALUE_NOERROR;
}
return RETURNVALUE_CANNOTUSETHISOBJECT;
}
bool Actions::useItem(Player* player, const Position& pos, uint8_t index, Item* item, bool isHotkey)
{
player->setNextAction(OTSYS_TIME() + g_config.getNumber(ConfigManager::ACTIONS_DELAY_INTERVAL));
player->stopWalk();
if (isHotkey) {
showUseHotkeyMessage(player, item, player->getItemTypeCount(item->getID(), -1));
}
ReturnValue ret = internalUseItem(player, pos, index, item, isHotkey);
if (ret != RETURNVALUE_NOERROR) {
player->sendCancelMessage(ret);
return false;
}
return true;
}
bool Actions::useItemEx(Player* player, const Position& fromPos, const Position& toPos,
uint8_t toStackPos, Item* item, bool isHotkey, Creature* creature/* = nullptr*/)
{
player->setNextAction(OTSYS_TIME() + g_config.getNumber(ConfigManager::EX_ACTIONS_DELAY_INTERVAL));
player->stopWalk();
Action* action = getAction(item);
if (!action) {
player->sendCancelMessage(RETURNVALUE_CANNOTUSETHISOBJECT);
return false;
}
ReturnValue ret = action->canExecuteAction(player, toPos);
if (ret != RETURNVALUE_NOERROR) {
player->sendCancelMessage(ret);
return false;
}
if (isHotkey) {
showUseHotkeyMessage(player, item, player->getItemTypeCount(item->getID(), -1));
}
if (!action->executeUse(player, item, fromPos, action->getTarget(player, creature, toPos, toStackPos), toPos, isHotkey)) {
if (!action->hasOwnErrorHandler()) {
player->sendCancelMessage(RETURNVALUE_CANNOTUSETHISOBJECT);
}
return false;
}
return true;
}
void Actions::showUseHotkeyMessage(Player* player, const Item* item, uint32_t count)
{
std::ostringstream ss;
const ItemType& it = Item::items[item->getID()];
if (!it.showCount) {
ss << "Using one of " << item->getName() << "...";
} else if (count == 1) {
ss << "Using the last " << item->getName() << "...";
} else {
ss << "Using one of " << count << ' ' << item->getPluralName() << "...";
}
player->sendTextMessage(MESSAGE_INFO_DESCR, ss.str());
}
Action::Action(LuaScriptInterface* interface) :
Event(interface), function(nullptr), allowFarUse(false), checkFloor(true), checkLineOfSight(true) {}
bool Action::configureEvent(const pugi::xml_node& node)
{
pugi::xml_attribute allowFarUseAttr = node.attribute("allowfaruse");
if (allowFarUseAttr) {
allowFarUse = allowFarUseAttr.as_bool();
}
pugi::xml_attribute blockWallsAttr = node.attribute("blockwalls");
if (blockWallsAttr) {
checkLineOfSight = blockWallsAttr.as_bool();
}
pugi::xml_attribute checkFloorAttr = node.attribute("checkfloor");
if (checkFloorAttr) {
checkFloor = checkFloorAttr.as_bool();
}
return true;
}
namespace {
bool enterMarket(Player* player, Item*, const Position&, Thing*, const Position&, bool)
{
if (player->getLastDepotId() == -1) {
return false;
}
player->sendMarketEnter(player->getLastDepotId());
return true;
}
}
bool Action::loadFunction(const pugi::xml_attribute& attr)
{
const char* functionName = attr.as_string();
if (strcasecmp(functionName, "market") == 0) {
function = enterMarket;
} else {
std::cout << "[Warning - Action::loadFunction] Function \"" << functionName << "\" does not exist." << std::endl;
return false;
}
scripted = false;
return true;
}
std::string Action::getScriptEventName() const
{
return "onUse";
}
ReturnValue Action::canExecuteAction(const Player* player, const Position& toPos)
{
if (!allowFarUse) {
return g_actions->canUse(player, toPos);
} else {
return g_actions->canUseFar(player, toPos, checkLineOfSight, checkFloor);
}
}
Thing* Action::getTarget(Player* player, Creature* targetCreature, const Position& toPosition, uint8_t toStackPos) const
{
if (targetCreature) {
return targetCreature;
}
return g_game.internalGetThing(player, toPosition, toStackPos, 0, STACKPOS_USETARGET);
}
bool Action::executeUse(Player* player, Item* item, const Position& fromPosition, Thing* target, const Position& toPosition, bool isHotkey)
{
//onUse(player, item, fromPosition, target, toPosition, isHotkey)
if (!scriptInterface->reserveScriptEnv()) {
std::cout << "[Error - Action::executeUse] Call stack overflow" << std::endl;
return false;
}
ScriptEnvironment* env = scriptInterface->getScriptEnv();
env->setScriptId(scriptId, scriptInterface);
lua_State* L = scriptInterface->getLuaState();
scriptInterface->pushFunction(scriptId);
LuaScriptInterface::pushUserdata<Player>(L, player);
LuaScriptInterface::setMetatable(L, -1, "Player");
LuaScriptInterface::pushThing(L, item);
LuaScriptInterface::pushPosition(L, fromPosition);
LuaScriptInterface::pushThing(L, target);
LuaScriptInterface::pushPosition(L, toPosition);
LuaScriptInterface::pushBoolean(L, isHotkey);
return scriptInterface->callFunction(6);
}