Marcelo Druida
Intermediate OT User
Hello!
This code makes it possible to include some script when the monster sets a target.
monster.cpp
find:
bool Monster::selectTarget(Creature* creature)
replace:
monster.h
find:
bool selectTarget(Creature* creature);
add:
bool executeOnSelectTarget(Creature* creature);
monsters.cpp:
find:
add:
targetEvent = -1;
find:
mType->thinkEvent = scriptInterface->getEvent("onThink");
add:
mType->targetEvent = scriptInterface->getEvent("onSelectTarget");
monsters.h:
find:
int32_t thinkEvent;
add:
int32_t targetEvent;
Improvement:
If you want to set a global target event (all monsters will follow the rule):
monsters.cpp:
find:
Monsters::loadMonster
add:
before return true
find:
add:
globalTargetEvent = -1;
monsters.h
find:
int32_t thinkEvent;
add:
int32_t globalTargetEvent;
create monster/scripts/global.lua:
function onSelectTarget(self, target)
print(self:getName())
return true
end
This code makes it possible to include some script when the monster sets a target.
monster.cpp
find:
bool Monster::selectTarget(Creature* creature)
replace:
Code:
bool Monster::selectTarget(Creature* creature)
{
if (!isTarget(creature)) {
return false;
}
auto it = std::find(targetList.begin(), targetList.end(), creature);
if (it == targetList.end()) {
//Target not found in our target list.
return false;
}
if (isHostile() || isSummon()) {
if (executeOnSelectTarget(creature) == 1){
if (setAttackedCreature(creature) && !isSummon()) {
g_dispatcher.addTask(createTask(std::bind(&Game::checkCreatureAttack, &g_game, getID())));
}
}
else
return false;
}
return setFollowCreature(creature);
}
bool Monster::executeOnSelectTarget(Creature* creature){
// onSelectTarget(self, target)
if (mType->targetEvent != -1) {
LuaScriptInterface* scriptInterface = mType->scriptInterface;
if (!scriptInterface->reserveScriptEnv()) {
std::cout << "[Error - Monster::onSelectTarget] Call stack overflow" << std::endl;
return true;
}
ScriptEnvironment* env = scriptInterface->getScriptEnv();
env->setScriptId(mType->targetEvent, scriptInterface);
lua_State* L = scriptInterface->getLuaState();
scriptInterface->pushFunction(mType->targetEvent);
LuaScriptInterface::pushUserdata<Monster>(L, this);
LuaScriptInterface::setMetatable(L, -1, "Monster");
LuaScriptInterface::pushUserdata(L, creature);
LuaScriptInterface::setCreatureMetatable(L, -1, creature);
return (scriptInterface->callFunction(2));
}
return true;
}
monster.h
find:
bool selectTarget(Creature* creature);
add:
bool executeOnSelectTarget(Creature* creature);
monsters.cpp:
find:
Code:
scriptInterface = nullptr;
creatureAppearEvent = -1;
creatureDisappearEvent = -1;
creatureMoveEvent = -1;
creatureSayEvent = -1;
thinkEvent = -1;
targetEvent = -1;
find:
mType->thinkEvent = scriptInterface->getEvent("onThink");
add:
mType->targetEvent = scriptInterface->getEvent("onSelectTarget");
monsters.h:
find:
int32_t thinkEvent;
add:
int32_t targetEvent;
Improvement:
If you want to set a global target event (all monsters will follow the rule):
monsters.cpp:
find:
Monsters::loadMonster
add:
before return true
Code:
if (!scriptInterface) {
scriptInterface.reset(new LuaScriptInterface("Monster Interface"));
scriptInterface->initState();
}
if (scriptInterface->loadFile("data/monster/scripts/global.lua") == 0) {
mType->scriptInterface = scriptInterface.get();
mType->globalTargetEvent = scriptInterface->getEvent("onSelectTarget");
}
find:
Code:
scriptInterface = nullptr;
creatureAppearEvent = -1;
creatureDisappearEvent = -1;
creatureMoveEvent = -1;
creatureSayEvent = -1;
thinkEvent = -1;
targetEvent = -1;
add:
globalTargetEvent = -1;
monsters.h
find:
int32_t thinkEvent;
add:
int32_t globalTargetEvent;
create monster/scripts/global.lua:
function onSelectTarget(self, target)
print(self:getName())
return true
end
Last edited: