Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ranisalt/a075bdc4d87ed8572f869da15e200dee to your computer and use it in GitHub Desktop.
Save ranisalt/a075bdc4d87ed8572f869da15e200dee to your computer and use it in GitHub Desktop.
Discover places on walking
From c0fe27dd63b02af2413ad112150652328a98eb75 Mon Sep 17 00:00:00 2001
From: Ranieri Althoff <ranisalt@gmail.com>
Date: Fri, 10 Jul 2015 03:43:51 -0300
Subject: [PATCH] Add callback for places discovery on walking
---
data/creaturescripts/creaturescripts.xml | 1 +
data/creaturescripts/scripts/walk.lua | 25 ++++++++++++++++++++
data/global.lua | 8 +++++++
src/creatureevent.cpp | 39 ++++++++++++++++++++++++++++++++
src/creatureevent.h | 3 +++
src/player.cpp | 3 +++
6 files changed, 79 insertions(+)
create mode 100644 data/creaturescripts/scripts/walk.lua
diff --git a/data/creaturescripts/creaturescripts.xml b/data/creaturescripts/creaturescripts.xml
index 86e2c8c..c1f4278 100644
--- a/data/creaturescripts/creaturescripts.xml
+++ b/data/creaturescripts/creaturescripts.xml
@@ -7,5 +7,6 @@
<event type="login" name="RegenerateStamina" script="regeneratestamina.lua" />
<event type="death" name="PlayerDeath" script="playerdeath.lua" />
<event type="death" name="DropLoot" script="droploot.lua" />
+ <event type="walk" name="PlayerWalk" script="walk.lua" />
<event type="extendedopcode" name="ExtendedOpcode" script="extendedopcode.lua" />
</creaturescripts>
diff --git a/data/creaturescripts/scripts/walk.lua b/data/creaturescripts/scripts/walk.lua
new file mode 100644
index 0000000..006697c
--- /dev/null
+++ b/data/creaturescripts/scripts/walk.lua
@@ -0,0 +1,25 @@
+local message = "You have discovered %s."
+
+local function positionInRange(this, that)
+ local xdelta, ydelta = math.abs(this.x - that.x), math.abs(this.y - that.y)
+
+ -- viewport radius is 7 tiles wide, 5 tiles tall
+ return xdelta < 7 and ydelta < 5 and this.z == that.z
+end
+
+function onWalk(player, position)
+ for storage, place in pairs(locations) do
+ if player:getStorageValue(storage) == -1 and positionInRange(position, place.pos) then
+
+ player:sendTextMessage(MESSAGE_EVENT_ADVANCE, string.format(message, place.name))
+ player:setStorageValue(storage, 1)
+
+ if place.mapmark then
+ player:addMapMark(place.pos, place.mapmark, place.name)
+ end
+
+ break
+ end
+ end
+ return true
+end
diff --git a/data/global.lua b/data/global.lua
index 9f82b98..a8fd1db 100644
--- a/data/global.lua
+++ b/data/global.lua
@@ -1,5 +1,13 @@
dofile('data/lib/lib.lua')
+locations = {
+ [15000] = {
+ name = "Initial Temple",
+ pos = { x = 95, y = 122, z = 7 },
+ mapmark = MAPMARK_TEMPLE
+ }
+}
+
STORAGEVALUE_PROMOTION = 30018
ropeSpots = {384, 418, 8278, 8592, 13189, 14435, 14436, 15635, 19518}
diff --git a/src/creatureevent.cpp b/src/creatureevent.cpp
index 8c95b40..2e71486 100644
--- a/src/creatureevent.cpp
+++ b/src/creatureevent.cpp
@@ -126,6 +126,19 @@ bool CreatureEvents::playerLogout(Player* player) const
return true;
}
+bool CreatureEvents::playerWalk(Player* player, const Position& position) const
+{
+ //fire global event if is registered
+ for (const auto& it : creatureEvents) {
+ if (it.second->getEventType() == CREATURE_EVENT_WALK) {
+ if (!it.second->executeOnWalk(player, position)) {
+ return false;
+ }
+ }
+ }
+ return true;
+}
+
bool CreatureEvents::playerAdvance(Player* player, skills_t skill, uint32_t oldLevel,
uint32_t newLevel)
{
@@ -169,6 +182,8 @@ bool CreatureEvent::configureEvent(const pugi::xml_node& node)
type = CREATURE_EVENT_LOGOUT;
} else if (tmpStr == "think") {
type = CREATURE_EVENT_THINK;
+ } else if (tmpStr == "walk") {
+ type = CREATURE_EVENT_WALK;
} else if (tmpStr == "preparedeath") {
type = CREATURE_EVENT_PREPAREDEATH;
} else if (tmpStr == "death") {
@@ -209,6 +224,9 @@ std::string CreatureEvent::getScriptEventName() const
case CREATURE_EVENT_THINK:
return "onThink";
+ case CREATURE_EVENT_WALK:
+ return "onWalk";
+
case CREATURE_EVENT_PREPAREDEATH:
return "onPrepareDeath";
@@ -317,6 +335,27 @@ bool CreatureEvent::executeOnThink(Creature* creature, uint32_t interval)
return scriptInterface->callFunction(2);
}
+bool CreatureEvent::executeOnWalk(Creature* creature, const Position& position)
+{
+ //onWalk(creature, position)
+ if (!scriptInterface->reserveScriptEnv()) {
+ std::cout << "[Error - CreatureEvent::executeOnWalk] 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<Creature>(L, creature);
+ LuaScriptInterface::setCreatureMetatable(L, -1, creature);
+ LuaScriptInterface::pushPosition(L, position);
+
+ return scriptInterface->callFunction(2);
+}
+
bool CreatureEvent::executeOnPrepareDeath(Creature* creature, Creature* killer)
{
//onPrepareDeath(creature, killer)
diff --git a/src/creatureevent.h b/src/creatureevent.h
index e259634..44c3b76 100644
--- a/src/creatureevent.h
+++ b/src/creatureevent.h
@@ -29,6 +29,7 @@ enum CreatureEventType_t {
CREATURE_EVENT_LOGIN,
CREATURE_EVENT_LOGOUT,
CREATURE_EVENT_THINK,
+ CREATURE_EVENT_WALK,
CREATURE_EVENT_PREPAREDEATH,
CREATURE_EVENT_DEATH,
CREATURE_EVENT_KILL,
@@ -55,6 +56,7 @@ class CreatureEvents final : public BaseEvents
// global events
bool playerLogin(Player* player) const;
bool playerLogout(Player* player) const;
+ bool playerWalk(Player* player, const Position& position) const;
bool playerAdvance(Player* player, skills_t, uint32_t, uint32_t);
CreatureEvent* getEventByName(const std::string& name, bool forceLoaded = true);
@@ -97,6 +99,7 @@ class CreatureEvent final : public Event
bool executeOnLogin(Player* player);
bool executeOnLogout(Player* player);
bool executeOnThink(Creature* creature, uint32_t interval);
+ bool executeOnWalk(Creature* creature, const Position& position);
bool executeOnPrepareDeath(Creature* creature, Creature* killer);
bool executeOnDeath(Creature* creature, Item* corpse, Creature* killer, Creature* mostDamageKiller, bool lastHitUnjustified, bool mostDamageUnjustified);
void executeOnKill(Creature* creature, Creature* target);
diff --git a/src/player.cpp b/src/player.cpp
index 1cacf24..164bdd3 100644
--- a/src/player.cpp
+++ b/src/player.cpp
@@ -1225,6 +1225,9 @@ void Player::onWalk(Direction& dir)
Creature::onWalk(dir);
setNextActionTask(nullptr);
setNextAction(OTSYS_TIME() + getStepDuration(dir));
+
+ //scripting event - onWalk
+ g_creatureEvents->playerWalk(this, getNextPosition(dir, _position));
}
void Player::onCreatureMove(Creature* creature, const Tile* newTile, const Position& newPos,
--
2.10.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment