- Joined
- Feb 14, 2015
- Messages
- 5,638
- Solutions
- 559
- Reaction score
- 3,935
Arguments:
position:
Return value:
position:
- the position the monster is placed at
- true: the monster is spawned on server startup
- false: the monster is spawned after server startup
- true: the monster is spawned from a spawn on the map
- false: the monster is spawned artificially via Game.createMonster(name, pos)
Return value:
- true: allows the monster to be spawned and placed on the map
- false: disallows the monster to be spawned and placed on the map
events.h
under
put
under
put
events.cpp
replace
with
above
add
luascript.cpp
under
add
under
add
replace the entire function
with
spawn.cpp
under
add
under
add
replace the entire function
with
under
C++:
struct EventsInfo {
C++:
// Monster
int32_t monsterOnSpawn = -1;
under
C++:
bool load();
C++:
// Monster
bool eventMonsterOnSpawn(Monster* monster, const Position& position, bool startup, bool artificial);
events.cpp
replace
C++:
if (className == "Creature")
C++:
if (className == "Monster") {
if (methodName == "onSpawn") {
info.monsterOnSpawn = event;
}
} else if (className == "Creature") {
above
C++:
// Creature
bool Events::eventCreatureOnChangeOutfit(Creature* creature, const Outfit_t& outfit)
C++:
// Monster
bool Events::eventMonsterOnSpawn(Monster* monster, const Position& position, bool startup, bool artificial)
{
// Monster:onSpawn(position)
if (info.monsterOnSpawn == -1) {
return true;
}
if (!scriptInterface.reserveScriptEnv()) {
std::cout << "[Error - Events::monsterOnSpawn] Call stack overflow" << std::endl;
return false;
}
ScriptEnvironment* env = scriptInterface.getScriptEnv();
env->setScriptId(info.monsterOnSpawn, &scriptInterface);
lua_State* L = scriptInterface.getLuaState();
scriptInterface.pushFunction(info.monsterOnSpawn);
LuaScriptInterface::pushUserdata<Monster>(L, monster);
LuaScriptInterface::setMetatable(L, -1, "Monster");
LuaScriptInterface::pushPosition(L, position);
LuaScriptInterface::pushBoolean(L, startup);
LuaScriptInterface::pushBoolean(L, artificial);
return scriptInterface.callFunction(4);
}
luascript.cpp
under
C++:
#include "databasetasks.h"
C++:
#include "events.h"
C++:
extern Spells* g_spells;
C++:
extern Events* g_events;
replace the entire function
C++:
int LuaScriptInterface::luaGameCreateMonster(lua_State* L)
C++:
int LuaScriptInterface::luaGameCreateMonster(lua_State* L)
{
// Game.createMonster(monsterName, position[, extended = false[, force = false]])
Monster* monster = Monster::createMonster(getString(L, 1));
if (!monster) {
lua_pushnil(L);
return 1;
}
const Position& position = getPosition(L, 2);
bool extended = getBoolean(L, 3, false);
bool force = getBoolean(L, 4, false);
bool result = g_events->eventMonsterOnSpawn(monster, position, false, true);
if (result) {
if (g_game.placeCreature(monster, position, extended, force)) {
pushUserdata<Monster>(L, monster);
setMetatable(L, -1, "Monster");
} else {
delete monster;
lua_pushnil(L);
}
} else {
delete monster;
lua_pushnil(L);
}
return 1;
}
spawn.cpp
under
C++:
#include "pugicast.h"
C++:
#include "events.h"
C++:
extern Game g_game;
C++:
extern Events* g_events;
replace the entire function
C++:
bool Spawn::spawnMonster(uint32_t spawnId, MonsterType* mType, const Position& pos, Direction dir, bool startup /*= false*/)
C++:
bool Spawn::spawnMonster(uint32_t spawnId, MonsterType* mType, const Position& pos, Direction dir, bool startup /*= false*/)
{
std::unique_ptr<Monster> monster_ptr(new Monster(mType));
bool result = g_events->eventMonsterOnSpawn(monster_ptr.get(), pos, startup, false);
if (result) {
if (startup) {
//No need to send out events to the surrounding since there is no one out there to listen!
if (!g_game.internalPlaceCreature(monster_ptr.get(), pos, true)) {
return false;
}
} else {
if (!g_game.placeCreature(monster_ptr.get(), pos, false, true)) {
return false;
}
}
}
Monster* monster = monster_ptr.release();
monster->setDirection(dir);
monster->setSpawn(this);
monster->setMasterPos(pos);
monster->incrementReferenceCounter();
if (result) {
spawnedMap.insert(spawned_pair(spawnId, monster));
}
spawnMap[spawnId].lastSpawn = OTSYS_TIME();
return result;
}
data/events/scripts/monster.lua
data/events/events.xml
Lua:
function Monster:onSpawn(position, startup, artificial)
return true
end
data/events/events.xml
XML:
<!-- Monster methods -->
<event class="Monster" method="onSpawn" enabled="1" />
Lua:
function Monster:onSpawn(position, startup, artificial)
-- register a creature event on all artificial monsters
if artificial then
self:registerEvent("some creature event")
end
return true
end
Last edited by a moderator: