Skip to content

Instantly share code, notes, and snippets.

@Mkalo Mkalo/flymount.patch Secret
Last active Jan 18, 2018

Embed
What would you like to do?
diff --git a/a/creatureevent.cpp b/b/creatureevent.cpp
index cd9b81a..6cad3cd 100644
--- a/a/creatureevent.cpp
+++ b/b/creatureevent.cpp
@@ -191,6 +191,8 @@ bool CreatureEvent::configureEvent(const pugi::xml_node& node)
type = CREATURE_EVENT_MANACHANGE;
} else if (tmpStr == "extendedopcode") {
type = CREATURE_EVENT_EXTENDED_OPCODE;
+ } else if (tmpStr == "move") {
+ type = CREATURE_EVENT_MOVE;
} else {
std::cout << "[Error - CreatureEvent::configureEvent] Invalid type for creature event: " << eventName << std::endl;
return false;
@@ -240,6 +242,9 @@ std::string CreatureEvent::getScriptEventName() const
case CREATURE_EVENT_EXTENDED_OPCODE:
return "onExtendedOpcode";
+ case CREATURE_EVENT_MOVE:
+ return "onMove";
+
case CREATURE_EVENT_NONE:
default:
return std::string();
@@ -586,3 +591,25 @@ void CreatureEvent::executeExtendedOpcode(Player* player, uint8_t opcode, const
scriptInterface->callVoidFunction(3);
}
+
+bool CreatureEvent::executeOnMove(Player* player, const Position& fromPosition, const Position& toPosition)
+{
+ //onMove(player, frompos, topos)
+ if (!scriptInterface->reserveScriptEnv()) {
+ std::cout << "[Error - CreatureEvent::executeOnMove] 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(L, player);
+ LuaScriptInterface::setMetatable(L, -1, "Player");
+ LuaScriptInterface::pushPosition(L, fromPosition);
+ LuaScriptInterface::pushPosition(L, toPosition);
+
+ return scriptInterface->callFunction(3);
+}
\ No newline at end of file
diff --git a/a/creatureevent.h b/b/creatureevent.h
index 9097e2f..2cac9a3 100644
--- a/a/creatureevent.h
+++ b/b/creatureevent.h
@@ -38,6 +38,7 @@ enum CreatureEventType_t {
CREATURE_EVENT_HEALTHCHANGE,
CREATURE_EVENT_MANACHANGE,
CREATURE_EVENT_EXTENDED_OPCODE, // otclient additional network opcodes
+ CREATURE_EVENT_MOVE,
};
class CreatureEvent;
@@ -106,6 +107,7 @@ class CreatureEvent final : public Event
void executeHealthChange(Creature* creature, Creature* attacker, CombatDamage& damage);
void executeManaChange(Creature* creature, Creature* attacker, int32_t& manaChange, CombatOrigin origin);
void executeExtendedOpcode(Player* player, uint8_t opcode, const std::string& buffer);
+ bool executeOnMove(Player* player, const Position& fromPosition, const Position& toPosition);
//
protected:
diff --git a/a/events.cpp b/b/events.cpp
index f90500b..e634110 100644
--- a/a/events.cpp
+++ b/b/events.cpp
@@ -59,6 +59,7 @@ void Events::clear()
playerOnGainExperience = -1;
playerOnLoseExperience = -1;
playerOnGainSkillTries = -1;
+ playerOnToggleMount = -1;
}
bool Events::load()
@@ -137,6 +138,8 @@ bool Events::load()
playerOnLoseExperience = event;
} else if (methodName == "onGainSkillTries") {
playerOnGainSkillTries = event;
+ } else if (methodName == "onToggleMount") {
+ playerOnToggleMount = event;
} else {
std::cout << "[Warning - Events::load] Unknown player method: " << methodName << std::endl;
}
@@ -749,3 +752,29 @@ void Events::eventPlayerOnGainSkillTries(Player* player, skills_t skill, uint64_
scriptInterface.resetScriptEnv();
}
+
+bool Events::eventPlayerOnToggleMount(Player* player, uint8_t mountid, bool mounting)
+{
+ // Player:onToggleMount(mountid, mounting)
+ if (playerOnToggleMount == -1) {
+ return true;
+ }
+
+ if (!scriptInterface.reserveScriptEnv()) {
+ std::cout << "[Error - Events::eventPlayerOnToggleMount] Call stack overflow" << std::endl;
+ return false;
+ }
+
+ ScriptEnvironment* env = scriptInterface.getScriptEnv();
+ env->setScriptId(playerOnToggleMount, &scriptInterface);
+
+ lua_State* L = scriptInterface.getLuaState();
+ scriptInterface.pushFunction(playerOnToggleMount);
+
+ LuaScriptInterface::pushUserdata<Player>(L, player);
+ LuaScriptInterface::setMetatable(L, -1, "Player");
+ lua_pushnumber(L, mountid);
+ LuaScriptInterface::pushBoolean(L, mounting);
+
+ return scriptInterface.callFunction(3);
+}
diff --git a/a/events.h b/b/events.h
index 99b8a57..4b20a42 100644
--- a/a/events.h
+++ b/b/events.h
@@ -58,6 +58,7 @@ class Events
void eventPlayerOnGainExperience(Player* player, Creature* source, uint64_t& exp, uint64_t rawExp);
void eventPlayerOnLoseExperience(Player* player, uint64_t& exp);
void eventPlayerOnGainSkillTries(Player* player, skills_t skill, uint64_t& tries);
+ bool eventPlayerOnToggleMount(Player* player, uint8_t mountid, bool mounting);
private:
LuaScriptInterface scriptInterface;
@@ -86,6 +87,7 @@ class Events
int32_t playerOnGainExperience;
int32_t playerOnLoseExperience;
int32_t playerOnGainSkillTries;
+ int32_t playerOnToggleMount;
};
#endif
diff --git a/a/game.cpp b/b/game.cpp
index 20d100d..75e8d05 100644
--- a/a/game.cpp
+++ b/b/game.cpp
@@ -48,6 +48,7 @@ extern Vocations g_vocations;
extern GlobalEvents* g_globalEvents;
extern Events* g_events;
+
Game::Game() :
wildcardTree(false),
offlineTrainingWindow(std::numeric_limits<uint32_t>::max(), "Choose a Skill", "Please choose a skill:")
@@ -1709,6 +1710,17 @@ void Game::playerMove(uint32_t playerId, Direction direction)
}
player->resetIdleTime();
+
+ const Position& currentPos = player->getPosition();
+ Position destPos = getNextPosition(direction, currentPos);
+ const CreatureEventList& moveEvents = player->getCreatureEvents(CREATURE_EVENT_MOVE);
+ for (CreatureEvent* moveEvent : moveEvents) {
+ if (!moveEvent->executeOnMove(player, currentPos, destPos)) {
+ player->sendCancelWalk();
+ return;
+ }
+ }
+
player->setNextWalkActionTask(nullptr);
player->startAutoWalk(std::forward_list<Direction> { direction });
diff --git a/a/player.cpp b/b/player.cpp
index 62fceac..69ac253 100644
--- a/a/player.cpp
+++ b/b/player.cpp
@@ -4224,6 +4224,10 @@ bool Player::toggleMount(bool mount)
return false;
}
+ if (!g_events->eventPlayerOnToggleMount(this, currentMountId, mount)) {
+ return false;
+ }
+
defaultOutfit.lookMount = currentMount->clientId;
if (currentMount->speed != 0) {
@@ -4234,6 +4238,11 @@ bool Player::toggleMount(bool mount)
return false;
}
+ uint8_t currentMountId = getCurrentMount();
+ if (!g_events->eventPlayerOnToggleMount(this, currentMountId, mount)) {
+ return false;
+ }
+
dismount();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.