-
Notifications
You must be signed in to change notification settings - Fork 21
/
actions.cpp
591 lines (515 loc) · 15 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
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
//////////////////////////////////////////////////////////////////////
// OpenTibia - an opensource roleplaying game
//////////////////////////////////////////////////////////////////////
// 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//////////////////////////////////////////////////////////////////////
#include "otpch.h"
#include "const.h"
#include "player.h"
#include "monster.h"
#include "npc.h"
#include "game.h"
#include "item.h"
#include "container.h"
#include "combat.h"
#include "house.h"
#include "tasks.h"
#include "tools.h"
#include "spells.h"
#include "configmanager.h"
#include "beds.h"
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include "actions.h"
extern Game g_game;
extern Spells* g_spells;
extern Actions* g_actions;
extern ConfigManager g_config;
Actions::Actions() :
m_scriptInterface("Action Interface")
{
m_scriptInterface.initState();
}
Actions::~Actions()
{
clear();
}
inline void Actions::clearMap(ActionUseMap& map)
{
ActionUseMap::iterator it = map.begin();
while(it != map.end())
{
delete it->second;
map.erase(it);
it = map.begin();
}
}
void Actions::clear()
{
clearMap(useItemMap);
clearMap(uniqueItemMap);
clearMap(actionItemMap);
m_scriptInterface.reInitState();
}
LuaScriptInterface& Actions::getScriptInterface()
{
return m_scriptInterface;
}
std::string Actions::getScriptBaseName()
{
return "actions";
}
Event* Actions::getEvent(const std::string& nodeName)
{
if(asLowerCaseString(nodeName) == "action")
return new Action(&m_scriptInterface);
else
return NULL;
}
bool Actions::registerEvent(Event* event, xmlNodePtr p)
{
Action* action = dynamic_cast<Action*>(event);
if(!action)
return false;
int32_t id, id2, from;
bool success = true;
if(readXMLInteger(p, "itemid", id))
{
if(useItemMap.find(id) != useItemMap.end())
{
std::cout << "[Warning - Actions::registerEvent] Duplicate registered item with id: " << id << std::endl;
return false;
}
useItemMap[id] = action;
}
else if(readXMLInteger(p, "fromid", id) && readXMLInteger(p, "toid", id2))
{
from = id;
if(useItemMap.find(id) != useItemMap.end())
{
std::cout << "[Warning - Actions::registerEvent] Duplicate registered item with id: " << id << " in fromid: " << from << ", toid: " << id2 << std::endl;
success = false;
}
else
useItemMap[id] = action;
while(id < id2)
{
id++;
if(useItemMap.find(id) != useItemMap.end())
{
std::cout << "[Warning - Actions::registerEvent] Duplicate registered item with id: " << id << " in fromid: " << from << ", toid: " << id2 << std::endl;
continue;
}
useItemMap[id] = new Action(action);
}
}
else if(readXMLInteger(p, "uniqueid", id))
{
if(uniqueItemMap.find(id) != uniqueItemMap.end())
{
std::cout << "[Warning - Actions::registerEvent] Duplicate registered item with uniqueid: " << id << std::endl;
return false;
}
uniqueItemMap[id] = action;
}
else if(readXMLInteger(p, "fromuid", id) && readXMLInteger(p, "touid", id2))
{
from = id;
if(uniqueItemMap.find(id) != uniqueItemMap.end())
{
std::cout << "[Warning - Actions::registerEvent] Duplicate registered item with uniqueid: " << id << " in fromuid: " << from << ", touid: " << id2 << std::endl;
success = false;
}
else
uniqueItemMap[id] = action;
while(id < id2)
{
id++;
if(uniqueItemMap.find(id) != uniqueItemMap.end())
{
std::cout << "[Warning - Actions::registerEvent] Duplicate registered item with uniqueid: " << id << " in fromuid: " << from << ", touid: " << id2 << std::endl;
continue;
}
uniqueItemMap[id] = new Action(action);
}
}
else if(readXMLInteger(p, "actionid", id) || readXMLInteger(p, "aid", id))
{
if(actionItemMap.find(id) != actionItemMap.end())
{
std::cout << "[Warning - Actions::registerEvent] Duplicate registered item with actionid: " << id << std::endl;
return false;
}
actionItemMap[id] = action;
}
else if(readXMLInteger(p, "fromaid", id) && readXMLInteger(p, "toaid", id2))
{
from = id;
if(actionItemMap.find(id) != actionItemMap.end())
{
std::cout << "[Warning - Actions::registerEvent] Duplicate registered item with actionid: " << id << " in fromaid: " << from << ", toaid: " << id2 << std::endl;
success = false;
}
else
actionItemMap[id] = action;
while(id < id2)
{
id++;
if(actionItemMap.find(id) != actionItemMap.end())
{
std::cout << "[Warning - Actions::registerEvent] Duplicate registered item with actionid: " << id << " in fromaid: " << from << ", toaid: " << id2 << std::endl;
continue;
}
actionItemMap[id] = new Action(action);
}
}
else
success = false;
return success;
}
ReturnValue Actions::canUse(const Player* player, const Position& pos)
{
const Position& playerPos = player->getPosition();
if(pos.x != 0xFFFF)
{
if(playerPos.z > pos.z)
return RET_FIRSTGOUPSTAIRS;
else if(playerPos.z < pos.z)
return RET_FIRSTGODOWNSTAIRS;
else if(!Position::areInRange<1,1,0>(playerPos, pos))
return RET_TOOFARAWAY;
}
return RET_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 RET_NOERROR;
}
ReturnValue Actions::canUseFar(const Creature* creature, const Position& toPos, bool checkLineOfSight)
{
if(toPos.x == 0xFFFF)
return RET_NOERROR;
const Position& creaturePos = creature->getPosition();
if(creaturePos.z > toPos.z)
return RET_FIRSTGOUPSTAIRS;
else if(creaturePos.z < toPos.z)
return RET_FIRSTGODOWNSTAIRS;
else if(!Position::areInRange<7,5,0>(toPos, creaturePos))
return RET_TOOFARAWAY;
if(checkLineOfSight && !g_game.canThrowObjectTo(creaturePos, toPos))
return RET_CANNOTTHROW;
return RET_NOERROR;
}
Action* Actions::getAction(const Item* item)
{
if(item->getUniqueId() != 0)
{
ActionUseMap::iterator it = uniqueItemMap.find(item->getUniqueId());
if(it != uniqueItemMap.end())
return it->second;
}
if(item->getActionId() != 0)
{
ActionUseMap::iterator it = actionItemMap.find(item->getActionId());
if(it != actionItemMap.end())
return it->second;
}
ActionUseMap::iterator it = useItemMap.find(item->getID());
if(it != useItemMap.end())
return it->second;
//rune items
Action* runeSpell = g_spells->getRuneSpell(item->getID());
if(runeSpell)
return runeSpell;
return NULL;
}
ReturnValue Actions::internalUseItem(Player* player, const Position& pos,
uint8_t index, Item* item, uint32_t creatureId)
{
if(Door* door = item->getDoor())
{
if(!door->canUse(player))
return RET_CANNOTUSETHISOBJECT;
}
Action* action = getAction(item);
if(action)
{
int32_t stack = item->getParent()->__getIndexOfThing(item);
PositionEx posEx(pos, stack);
if(action->isScripted())
{
if(action->executeUse(player, item, posEx, posEx, false, creatureId))
return RET_NOERROR;
}
else if(action->function)
{
if(action->function(player, item, posEx, posEx, false, creatureId))
return RET_NOERROR;
}
}
if(BedItem* bed = item->getBed())
{
if(!bed->canUse(player))
return RET_CANNOTUSETHISOBJECT;
if(bed->trySleep(player))
{
player->setBedItem(bed);
g_game.sendOfflineTrainingDialog(player);
}
return RET_NOERROR;
}
if(Container* container = item->getContainer())
{
Container* openContainer = NULL;
//depot container
if(DepotLocker* depot = container->getDepotLocker())
{
DepotLocker* myDepotLocker = player->getDepotLocker(depot->getDepotId());
myDepotLocker->setParent(depot->getParent());
openContainer = myDepotLocker;
player->setDepotChange(true);
player->setLastDepotId(depot->getDepotId());
}
else
openContainer = container;
if(container->getCorpseOwner() != 0 && !player->canOpenCorpse(container->getCorpseOwner()))
return RET_YOUARENOTTHEOWNER;
//open/close container
int32_t oldcid = player->getContainerID(openContainer);
if(oldcid != -1)
{
player->onCloseContainer(openContainer);
player->closeContainer(oldcid);
}
else
{
player->addContainer(index, openContainer);
player->onSendContainer(openContainer);
}
return RET_NOERROR;
}
if(item->isReadable())
{
if(item->canWriteText())
{
player->setWriteItem(item, item->getMaxWriteLength());
player->sendTextWindow(item, item->getMaxWriteLength(), true);
}
else
{
player->setWriteItem(NULL);
player->sendTextWindow(item, 0, false);
}
return RET_NOERROR;
}
return RET_CANNOTUSETHISOBJECT;
}
bool Actions::useItem(Player* player, const Position& pos, uint8_t index, Item* item, bool isHotkey)
{
if(!player->canDoAction())
return false;
player->setNextActionTask(NULL);
player->setNextAction(OTSYS_TIME() + g_config.getNumber(ConfigManager::ACTIONS_DELAY_INTERVAL));
player->stopWalk();
if(isHotkey)
showUseHotkeyMessage(player, item->getID(), player->__getItemTypeCount(item->getID(), -1));
ReturnValue ret = internalUseItem(player, pos, index, item, 0);
if(ret != RET_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, uint32_t creatureId /* = 0*/)
{
if(!player->canDoAction())
return false;
player->setNextActionTask(NULL);
player->setNextAction(OTSYS_TIME() + g_config.getNumber(ConfigManager::EX_ACTIONS_DELAY_INTERVAL));
player->stopWalk();
Action* action = getAction(item);
if(!action)
{
player->sendCancelMessage(RET_CANNOTUSETHISOBJECT);
return false;
}
ReturnValue ret = action->canExecuteAction(player, toPos);
if(ret != RET_NOERROR)
{
player->sendCancelMessage(ret);
return false;
}
if(isHotkey)
showUseHotkeyMessage(player, item->getID(), player->__getItemTypeCount(item->getID(), -1));
int32_t fromStackPos = item->getParent()->__getIndexOfThing(item);
PositionEx fromPosEx(fromPos, fromStackPos);
PositionEx toPosEx(toPos, toStackPos);
if(!action->executeUse(player, item, fromPosEx, toPosEx, true, creatureId))
{
if(!action->hasOwnErrorHandler())
player->sendCancelMessage(RET_CANNOTUSETHISOBJECT);
return false;
}
return true;
}
void Actions::showUseHotkeyMessage(Player* player, int32_t id, uint32_t count)
{
const ItemType& it = Item::items[id];
std::ostringstream ss;
if(!it.showCount)
ss << "Using one of " << it.name << "...";
else if(count == 1)
ss << "Using the last " << it.name << "...";
else
ss << "Using one of " << count << " " << it.getPluralName() << "...";
player->sendTextMessage(MSG_INFO_DESCR, ss.str());
}
bool Actions::hasAction(const Item* item)
{
return getAction(item) != NULL;
}
Action::Action(LuaScriptInterface* _interface) :
Event(_interface)
{
allowFarUse = false;
checkLineOfSight = true;
function = NULL;
}
Action::Action(const Action *copy) :
Event(copy)
{
allowFarUse = copy->allowFarUse;
checkLineOfSight = copy->checkLineOfSight;
function = copy->function;
}
Action::~Action()
{
//
}
bool Action::configureEvent(xmlNodePtr p)
{
int32_t intValue;
if(readXMLInteger(p, "allowfaruse", intValue))
{
if(intValue != 0)
setAllowFarUse(true);
}
if(readXMLInteger(p, "blockwalls", intValue))
{
if(intValue == 0)
setCheckLineOfSight(false);
}
return true;
}
bool Action::loadFunction(const std::string& functionName)
{
std::string tmpFunctionName = asLowerCaseString(functionName);
if(tmpFunctionName == "increaseitemid")
function = increaseItemId;
else if(tmpFunctionName == "decreaseitemid")
function = decreaseItemId;
else if(tmpFunctionName == "market")
function = enterMarket;
else
{
std::cout << "[Warning - Action::loadFunction] Function \"" << functionName << "\" does not exist." << std::endl;
return false;
}
m_scripted = false;
return true;
}
bool Action::increaseItemId(Player* player, Item* item, const PositionEx& posFrom, const PositionEx& posTo, bool extendedUse, uint32_t creatureId)
{
Item* newItem = g_game.transformItem(item, item->getID() + 1);
g_game.startDecay(newItem);
return true;
}
bool Action::decreaseItemId(Player* player, Item* item, const PositionEx& posFrom, const PositionEx& posTo, bool extendedUse, uint32_t creatureId)
{
Item* newItem = g_game.transformItem(item, item->getID() - 1);
g_game.startDecay(newItem);
return true;
}
bool Action::enterMarket(Player* player, Item* item, const PositionEx& posFrom, const PositionEx& posTo, bool extendedUse, uint32_t creatureId)
{
if(!g_config.getBoolean(ConfigManager::MARKET_ENABLED))
{
player->sendTextMessage(MSG_INFO_DESCR, "The market is disabled.");
return false;
}
if(player->getLastDepotId() == -1)
return false;
player->sendMarketEnter(player->getLastDepotId());
return true;
}
std::string Action::getScriptEventName()
{
return "onUse";
}
ReturnValue Action::canExecuteAction(const Player* player, const Position& toPos)
{
if(!getAllowFarUse())
return g_actions->canUse(player, toPos);
return g_actions->canUseFar(player, toPos, getCheckLineOfSight());
}
bool Action::executeUse(Player* player, Item* item, const PositionEx& fromPos, const PositionEx& toPos, bool extendedUse, uint32_t creatureId)
{
//onUse(cid, item, fromPosition, itemEx, toPosition)
if(m_scriptInterface->reserveScriptEnv())
{
ScriptEnvironment* env = m_scriptInterface->getScriptEnv();
#ifdef __DEBUG_LUASCRIPTS__
std::ostringstream desc;
desc << player->getName() << " - " << item->getID() << " " << fromPos << "|" << toPos;
env->setEventDesc(desc.str());
#endif
env->setScriptId(m_scriptId, m_scriptInterface);
env->setRealPos(player->getPosition());
uint32_t cid = env->addThing(player);
uint32_t itemid1 = env->addThing(item);
lua_State* L = m_scriptInterface->getLuaState();
m_scriptInterface->pushFunction(m_scriptId);
lua_pushnumber(L, cid);
LuaScriptInterface::pushThing(L, item, itemid1);
LuaScriptInterface::pushPosition(L, fromPos, fromPos.stackpos);
//std::cout << "posTo" << (Position)posTo << " stack" << (int32_t)posTo.stackpos <<std::endl;
Thing* thing = g_game.internalGetThing(player, toPos, toPos.stackpos);
if(thing && (!extendedUse || thing != item))
{
uint32_t thingId2 = env->addThing(thing);
LuaScriptInterface::pushThing(L, thing, thingId2);
LuaScriptInterface::pushPosition(L, toPos, toPos.stackpos);
}
else
{
LuaScriptInterface::pushThing(L, NULL, 0);
Position posEx;
LuaScriptInterface::pushPosition(L, posEx, 0);
}
bool result = m_scriptInterface->callFunction(5);
m_scriptInterface->releaseScriptEnv();
return result;
}
else
{
std::cout << "[Error] Call stack overflow. Action::executeUse" << std::endl;
return false;
}
}