-
Notifications
You must be signed in to change notification settings - Fork 21
/
creatureevent.cpp
501 lines (414 loc) · 12.6 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
//////////////////////////////////////////////////////////////////////
// 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 "creatureevent.h"
#include "tools.h"
#include "player.h"
#ifdef __DEBUG_LUASCRIPTS__
#include <sstream>
#endif
CreatureEvents::CreatureEvents() :
m_scriptInterface("CreatureScript Interface")
{
m_scriptInterface.initState();
}
CreatureEvents::~CreatureEvents()
{
CreatureEventList::iterator it;
for(it = m_creatureEvents.begin(); it != m_creatureEvents.end(); ++it)
delete it->second;
m_creatureEvents.clear();
}
void CreatureEvents::clear()
{
//clear creature events
for(CreatureEventList::iterator it = m_creatureEvents.begin(); it != m_creatureEvents.end(); ++it)
it->second->clearEvent();
//clear lua state
m_scriptInterface.reInitState();
}
LuaScriptInterface& CreatureEvents::getScriptInterface()
{
return m_scriptInterface;
}
std::string CreatureEvents::getScriptBaseName()
{
return "creaturescripts";
}
Event* CreatureEvents::getEvent(const std::string& nodeName)
{
if(asLowerCaseString(nodeName) == "event")
return new CreatureEvent(&m_scriptInterface);
return NULL;
}
bool CreatureEvents::registerEvent(Event* event, xmlNodePtr p)
{
CreatureEvent* creatureEvent = dynamic_cast<CreatureEvent*>(event);
if(!creatureEvent)
return false;
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 realoading), it is reused
if(!oldEvent->isLoaded() && oldEvent->getEventType() == creatureEvent->getEventType())
oldEvent->copyEvent(creatureEvent);
return false;
}
else
{
//if not, register it normally
m_creatureEvents[creatureEvent->getName()] = creatureEvent;
return true;
}
}
CreatureEvent* CreatureEvents::getEventByName(const std::string& name, bool forceLoaded /*= true*/)
{
CreatureEventList::iterator it = m_creatureEvents.find(name);
if(it != m_creatureEvents.end())
{
if(!forceLoaded || it->second->isLoaded())
return it->second;
}
return NULL;
}
uint32_t CreatureEvents::playerLogin(Player* player)
{
//fire global event if is registered
for(CreatureEventList::iterator it = m_creatureEvents.begin(); it != m_creatureEvents.end(); ++it)
{
if(it->second->getEventType() == CREATURE_EVENT_LOGIN)
{
if(!it->second->executeOnLogin(player))
return 0;
}
}
return 1;
}
uint32_t CreatureEvents::playerLogout(Player* player)
{
//fire global event if is registered
for(CreatureEventList::iterator it = m_creatureEvents.begin(); it != m_creatureEvents.end(); ++it)
{
if(it->second->getEventType() == CREATURE_EVENT_LOGOUT)
{
if(!it->second->executeOnLogout(player))
return 0;
}
}
return 1;
}
uint32_t CreatureEvents::playerAdvance(Player* player, skills_t skill, uint32_t oldLevel,
uint32_t newLevel)
{
for(CreatureEventList::iterator it = m_creatureEvents.begin(); it != m_creatureEvents.end(); ++it)
{
if(it->second->getEventType() == CREATURE_EVENT_ADVANCE)
{
if(!it->second->executeAdvance(dynamic_cast<Creature*>(player), skill, oldLevel, newLevel))
return 0;
}
}
return 1;
}
/////////////////////////////////////
CreatureEvent::CreatureEvent(LuaScriptInterface* _interface) :
Event(_interface)
{
m_type = CREATURE_EVENT_NONE;
m_isLoaded = false;
}
bool CreatureEvent::configureEvent(xmlNodePtr p)
{
std::string str;
//Name that will be used in monster xml files and
// lua function to register events to reference this event
if(readXMLString(p, "name", str))
m_eventName = str;
else
{
std::cout << "Error: [CreatureEvent::configureEvent] No name for creature event." << std::endl;
return false;
}
if(readXMLString(p, "type", str))
{
std::string tmpStr = asLowerCaseString(str);
if(tmpStr == "login")
m_type = CREATURE_EVENT_LOGIN;
else if(tmpStr == "logout")
m_type = CREATURE_EVENT_LOGOUT;
else if(tmpStr == "think")
m_type = CREATURE_EVENT_THINK;
else if(tmpStr == "preparedeath")
m_type = CREATURE_EVENT_PREPAREDEATH;
else if(tmpStr == "death")
m_type = CREATURE_EVENT_DEATH;
else if(tmpStr == "kill")
m_type = CREATURE_EVENT_KILL;
else if(tmpStr == "advance")
m_type = CREATURE_EVENT_ADVANCE;
else
{
std::cout << "[Error - CreatureEvent::configureEvent] No valid type for creature event." << str << std::endl;
return false;
}
}
else
{
std::cout << "[Error - CreatureEvent::configureEvent] No type for creature event." << std::endl;
return false;
}
m_isLoaded = true;
return true;
}
std::string CreatureEvent::getScriptEventName()
{
//Depending on the type script event name is different
switch(m_type)
{
case CREATURE_EVENT_LOGIN:
return "onLogin";
case CREATURE_EVENT_LOGOUT:
return "onLogout";
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_NONE:
default:
return "";
}
}
void CreatureEvent::copyEvent(CreatureEvent* creatureEvent)
{
m_scriptId = creatureEvent->m_scriptId;
m_scriptInterface = creatureEvent->m_scriptInterface;
m_scripted = creatureEvent->m_scripted;
m_isLoaded = creatureEvent->m_isLoaded;
}
void CreatureEvent::clearEvent()
{
m_scriptId = 0;
m_scriptInterface = NULL;
m_scripted = false;
m_isLoaded = false;
}
uint32_t CreatureEvent::executeOnLogin(Player* player)
{
//onLogin(cid)
if(m_scriptInterface->reserveScriptEnv())
{
ScriptEnvironment* env = m_scriptInterface->getScriptEnv();
#ifdef __DEBUG_LUASCRIPTS__
env->setEventDesc(player->getName());
#endif
env->setScriptId(m_scriptId, m_scriptInterface);
env->setRealPos(player->getPosition());
uint32_t cid = env->addThing(player);
lua_State* L = m_scriptInterface->getLuaState();
m_scriptInterface->pushFunction(m_scriptId);
lua_pushnumber(L, cid);
bool result = m_scriptInterface->callFunction(1);
m_scriptInterface->releaseScriptEnv();
return result;
}
else
{
std::cout << "[Error - CreatureEvent::executeOnLogin] Call stack overflow." << std::endl;
return 0;
}
}
uint32_t CreatureEvent::executeOnLogout(Player* player)
{
//onLogout(cid)
if(m_scriptInterface->reserveScriptEnv())
{
ScriptEnvironment* env = m_scriptInterface->getScriptEnv();
#ifdef __DEBUG_LUASCRIPTS__
env->setEventDesc(player->getName());
#endif
env->setScriptId(m_scriptId, m_scriptInterface);
env->setRealPos(player->getPosition());
uint32_t cid = env->addThing(player);
lua_State* L = m_scriptInterface->getLuaState();
m_scriptInterface->pushFunction(m_scriptId);
lua_pushnumber(L, cid);
bool result = m_scriptInterface->callFunction(1);
m_scriptInterface->releaseScriptEnv();
return result;
}
else
{
std::cout << "[Error - CreatureEvent::executeOnLogout] Call stack overflow." << std::endl;
return 0;
}
}
uint32_t CreatureEvent::executeOnThink(Creature* creature, uint32_t interval)
{
//onThink(cid, interval)
if(m_scriptInterface->reserveScriptEnv())
{
ScriptEnvironment* env = m_scriptInterface->getScriptEnv();
#ifdef __DEBUG_LUASCRIPTS__
env->setEventDesc(creature->getName());
#endif
env->setScriptId(m_scriptId, m_scriptInterface);
env->setRealPos(creature->getPosition());
uint32_t cid = env->addThing(creature);
lua_State* L = m_scriptInterface->getLuaState();
m_scriptInterface->pushFunction(m_scriptId);
lua_pushnumber(L, cid);
lua_pushnumber(L, interval);
bool result = m_scriptInterface->callFunction(2);
m_scriptInterface->releaseScriptEnv();
return result;
}
else
{
std::cout << "[Error - CreatureEvent::executeOnThink] Call stack overflow." << std::endl;
return 0;
}
}
uint32_t CreatureEvent::executeOnPrepareDeath(Player* player, Creature* killer)
{
//onPrepareDeath(cid, killer)
if(m_scriptInterface->reserveScriptEnv())
{
ScriptEnvironment* env = m_scriptInterface->getScriptEnv();
#ifdef __DEBUG_LUASCRIPTS__
env->setEventDesc(player->getName());
#endif
env->setScriptId(m_scriptId, m_scriptInterface);
env->setRealPos(player->getPosition());
uint32_t cid = env->addThing(player);
uint32_t killercid = env->addThing(killer);
lua_State* L = m_scriptInterface->getLuaState();
m_scriptInterface->pushFunction(m_scriptId);
lua_pushnumber(L, cid);
lua_pushnumber(L, killercid);
bool result = m_scriptInterface->callFunction(2);
m_scriptInterface->releaseScriptEnv();
return result;
}
else
{
std::cout << "[Error - CreatureEvent::executeOnPrepareDeath] Call stack overflow." << std::endl;
return 0;
}
}
uint32_t CreatureEvent::executeOnDeath(Creature* creature, Item* corpse, Creature* killer, Creature* mostDamageKiller, bool lastHitUnjustified, bool mostDamageUnjustified)
{
//onDeath(cid, corpse, lasthitkiller, mostdamagekiller, lasthitunjustified, mostdamageunjustified)
if(m_scriptInterface->reserveScriptEnv())
{
ScriptEnvironment* env = m_scriptInterface->getScriptEnv();
#ifdef __DEBUG_LUASCRIPTS__
env->setEventDesc(creature->getName());
#endif
env->setScriptId(m_scriptId, m_scriptInterface);
env->setRealPos(creature->getPosition());
uint32_t cid = env->addThing(creature);
uint32_t corpseid = env->addThing(corpse);
uint32_t killercid = env->addThing(killer);
uint32_t mostdamagekillercid = env->addThing(mostDamageKiller);
lua_State* L = m_scriptInterface->getLuaState();
m_scriptInterface->pushFunction(m_scriptId);
lua_pushnumber(L, cid);
lua_pushnumber(L, corpseid);
lua_pushnumber(L, killercid);
lua_pushnumber(L, mostdamagekillercid);
lua_pushnumber(L, lastHitUnjustified);
lua_pushnumber(L, mostDamageUnjustified);
bool result = m_scriptInterface->callFunction(6);
m_scriptInterface->releaseScriptEnv();
return result;
}
else
{
std::cout << "[Error - CreatureEvent::executeOnDeath] Call stack overflow." << std::endl;
return 0;
}
}
uint32_t CreatureEvent::executeAdvance(Creature* creature, skills_t skill, uint32_t oldLevel,
uint32_t newLevel)
{
if(m_scriptInterface->reserveScriptEnv())
{
ScriptEnvironment* env = m_scriptInterface->getScriptEnv();
#ifdef __DEBUG_LUASCRIPTS__
env->setEventDesc(creature->getName());
#endif
env->setScriptId(m_scriptId, m_scriptInterface);
env->setRealPos(creature->getPosition());
uint32_t cid = env->addThing(creature);
lua_State* L = m_scriptInterface->getLuaState();
m_scriptInterface->pushFunction(m_scriptId);
lua_pushnumber(L, cid);
lua_pushnumber(L, static_cast<uint32_t>(skill));
lua_pushnumber(L, oldLevel);
lua_pushnumber(L, newLevel);
bool result = m_scriptInterface->callFunction(4);
m_scriptInterface->releaseScriptEnv();
return result;
}
else
{
std::cout << "[Error - CreatureEvent::executeAdvance] Call stack overflow." << std::endl;
return 0;
}
}
uint32_t CreatureEvent::executeOnKill(Creature* creature, Creature* target)
{
//onKill(cid, target)
if(m_scriptInterface->reserveScriptEnv())
{
ScriptEnvironment* env = m_scriptInterface->getScriptEnv();
#ifdef __DEBUG_LUASCRIPTS__
env->setEventDesc(creature->getName());
#endif
env->setScriptId(m_scriptId, m_scriptInterface);
env->setRealPos(creature->getPosition());
uint32_t cid = env->addThing(creature);
uint32_t targetId = env->addThing(target);
lua_State* L = m_scriptInterface->getLuaState();
m_scriptInterface->pushFunction(m_scriptId);
lua_pushnumber(L, cid);
lua_pushnumber(L, targetId);
bool result = m_scriptInterface->callFunction(2);
m_scriptInterface->releaseScriptEnv();
return result;
}
else
{
std::cout << "[Error - CreatureEvent::executeOnKill] Call stack overflow." << std::endl;
return 0;
}
}