Skip to content

Instantly share code, notes, and snippets.

@dbjorkholm
Last active September 7, 2016 10:31
Show Gist options
  • Save dbjorkholm/14808869146f7fb5a2129d1f777892dc to your computer and use it in GitHub Desktop.
Save dbjorkholm/14808869146f7fb5a2129d1f777892dc to your computer and use it in GitHub Desktop.
Monster.set/getExperience, Monster.set/getStorageValue, Creature.setBaseSpeed patch
From fe7907f85fcbbab3957213cf68e5f38ee0b2ebf4 Sun, 8 May 2016 19:10:38 +0200
From: Daniel Björkholm <danielbjorkholm@gmail.com>
Date: Sun, 8 May 2016 19:10:38 +0200
Subject: [PATCH] Test
---
creature.cpp | 4 ++-
creature.h | 3 +-
luascript.cpp | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
luascript.h | 9 +++++-
monster.cpp | 32 ++++++++++++++++++++-
monster.h | 16 +++++++++--
6 files changed, 149 insertions(+), 7 deletions(-)
diff --git a/creature.cpp b/creature.cpp
index 1325c6c..8fa6f51 100644
--- a/creature.cpp
+++ b/creature.cpp
@@ -49,6 +49,8 @@ Creature::Creature() :
healthMax = 1000;
mana = 0;
+ experience = 0;
+
lastStep = 0;
lastStepCost = 1;
baseSpeed = 220;
@@ -1642,4 +1644,4 @@ bool Creature::getPathTo(const Position& targetPos, std::forward_list<Direction>
fpp.minTargetDist = minTargetDist;
fpp.maxTargetDist = maxTargetDist;
return getPathTo(targetPos, dirList, fpp);
-}
\ No newline at end of file
+}
diff --git a/creature.h b/creature.h
index 9ff4259..95d669a 100644
--- a/creature.h
+++ b/creature.h
@@ -499,6 +499,7 @@ class Creature : virtual public Thing
Creature* followCreature;
uint64_t lastStep;
+ uint64_t experience;
uint32_t referenceCounter;
uint32_t id;
uint32_t scriptEventsBitField;
@@ -567,4 +568,4 @@ class Creature : virtual public Thing
friend class LuaScriptInterface;
};
-#endif
\ No newline at end of file
+#endif
diff --git a/luascript.cpp b/luascript.cpp
index 1809b85..03578c1 100644
--- a/luascript.cpp
+++ b/luascript.cpp
@@ -2046,6 +2046,7 @@ void LuaScriptInterface::registerFunctions()
registerMethod("Creature", "getSpeed", LuaScriptInterface::luaCreatureGetSpeed);
registerMethod("Creature", "getBaseSpeed", LuaScriptInterface::luaCreatureGetBaseSpeed);
+ registerMethod("Creature", "setBaseSpeed", LuaScriptInterface::luaCreatureSetBaseSpeed);
registerMethod("Creature", "changeSpeed", LuaScriptInterface::luaCreatureChangeSpeed);
registerMethod("Creature", "setDropLoot", LuaScriptInterface::luaCreatureSetDropLoot);
@@ -2269,6 +2270,13 @@ void LuaScriptInterface::registerFunctions()
registerMethod("Monster", "selectTarget", LuaScriptInterface::luaMonsterSelectTarget);
registerMethod("Monster", "searchTarget", LuaScriptInterface::luaMonsterSearchTarget);
+ registerMethod("Monster", "getExperience", LuaScriptInterface::luaMonsterGetExperience);
+ registerMethod("Monster", "setExperience", LuaScriptInterface::luaMonsterSetExperience);
+
+ registerMethod("Monster", "getStorageValue", LuaScriptInterface::luaMonsterGetStorageValue);
+ registerMethod("Monster", "setStorageValue", LuaScriptInterface::luaMonsterSetStorageValue);
+
+
// Npc
registerClass("Npc", "Creature", LuaScriptInterface::luaNpcCreate);
registerMetaMethod("Npc", "__eq", LuaScriptInterface::luaUserdataCompare);
@@ -6904,6 +6912,21 @@ int LuaScriptInterface::luaCreatureGetBaseSpeed(lua_State* L)
return 1;
}
+int LuaScriptInterface::luaCreatureSetBaseSpeed(lua_State* L)
+{
+ // creature:setBaseSpeed(newBaseSpeed)
+ Creature* creature = getUserdata<Creature>(L, 1);
+ if (!creature) {
+ pushBoolean(L, false);
+ return 1;
+ }
+
+ uint32_t newBaseSpeed = getNumber<uint32_t>(L, 2);
+ creature->setBaseSpeed(newBaseSpeed);
+ pushBoolean(L, true);
+ return 1;
+}
+
int LuaScriptInterface::luaCreatureChangeSpeed(lua_State* L)
{
// creature:changeSpeed(delta)
@@ -9518,6 +9541,73 @@ int LuaScriptInterface::luaMonsterSearchTarget(lua_State* L)
return 1;
}
+int LuaScriptInterface::luaMonsterGetExperience(lua_State* L)
+{
+ // monster:getExperience()
+ Monster* monster = getUserdata<Monster>(L, 1);
+ if (monster) {
+ lua_pushnumber(L, monster->getExperience());
+ } else {
+ lua_pushnil(L);
+ }
+ return 1;
+}
+
+int LuaScriptInterface::luaMonsterSetExperience(lua_State* L)
+{
+ // monster:setExperience(experience)
+ Monster* monster = getUserdata<Monster>(L, 1);
+ if (monster) {
+ monster->setExperience(getNumber<uint64_t>(L, 2));
+ pushBoolean(L, true);
+ } else {
+ lua_pushnil(L);
+ }
+ return 1;
+}
+
+int LuaScriptInterface::luaMonsterGetStorageValue(lua_State* L)
+{
+ // monster:getStorageValue(key)
+ Monster* monster = getUserdata<Monster>(L, 1);
+ if (!monster) {
+ lua_pushnil(L);
+ return 1;
+ }
+
+ uint32_t key = getNumber<uint32_t>(L, 2);
+ int32_t value;
+ if (monster->getStorageValue(key, value)) {
+ lua_pushnumber(L, value);
+ } else {
+ lua_pushnumber(L, -1);
+ }
+ return 1;
+}
+
+int LuaScriptInterface::luaMonsterSetStorageValue(lua_State* L)
+{
+ // monster:setStorageValue(key, value)
+ int32_t value = getNumber<int32_t>(L, 3);
+ uint32_t key = getNumber<uint32_t>(L, 2);
+ Monster* monster = getUserdata<Monster>(L, 1);
+ if (IS_IN_KEYRANGE(key, RESERVED_RANGE)) {
+ std::ostringstream ss;
+ ss << "Accessing reserved range: " << key;
+ reportErrorFunc(ss.str());
+ pushBoolean(L, false);
+ return 1;
+ }
+
+ if (monster) {
+ monster->addStorageValue(key, value);
+ pushBoolean(L, true);
+ } else {
+ lua_pushnil(L);
+ }
+ return 1;
+}
+
// Npc
int LuaScriptInterface::luaNpcCreate(lua_State* L)
{
@@ -12237,4 +12327,4 @@ void LuaEnvironment::executeTimerEvent(uint32_t eventIndex)
for (auto parameter : timerEventDesc.parameters) {
luaL_unref(luaState, LUA_REGISTRYINDEX, parameter);
}
-}
\ No newline at end of file
+}
diff --git a/luascript.h b/luascript.h
index 517750c..c7d811b 100644
--- a/luascript.h
+++ b/luascript.h
@@ -767,6 +767,7 @@ class LuaScriptInterface
static int luaCreatureGetSpeed(lua_State* L);
static int luaCreatureGetBaseSpeed(lua_State* L);
+ static int luaCreatureSetBaseSpeed(lua_State* L);
static int luaCreatureChangeSpeed(lua_State* L);
static int luaCreatureSetDropLoot(lua_State* L);
@@ -989,6 +990,12 @@ class LuaScriptInterface
static int luaMonsterSelectTarget(lua_State* L);
static int luaMonsterSearchTarget(lua_State* L);
+ static int luaMonsterGetExperience(lua_State* L);
+ static int luaMonsterSetExperience(lua_State* L);
+
+ static int luaMonsterGetStorageValue(lua_State* L);
+ static int luaMonsterSetStorageValue(lua_State* L);
+
// Npc
static int luaNpcCreate(lua_State* L);
@@ -1302,4 +1309,4 @@ class LuaEnvironment : public LuaScriptInterface
friend class CombatSpell;
};
-#endif
\ No newline at end of file
+#endif
diff --git a/monster.cpp b/monster.cpp
index 917348f..5efd4ab 100644
--- a/monster.cpp
+++ b/monster.cpp
@@ -54,6 +54,7 @@ Monster::Monster(MonsterType* mtype) :
health = mType->health;
healthMax = mType->healthMax;
+ experience = mType->experience;
baseSpeed = mType->baseSpeed;
internalLight.level = mType->lightLevel;
internalLight.color = mType->lightColor;
@@ -2018,4 +2019,33 @@ void Monster::getPathSearchParams(const Creature* creature, FindPathParams& fpp)
} else {
fpp.fullPathSearch = !canUseAttack(getPosition(), creature);
}
-}
\ No newline at end of file
+}
+
+void Monster::addStorageValue(const uint32_t key, const int32_t value)
+{
+ if (IS_IN_KEYRANGE(key, RESERVED_RANGE)) {
+ std::cout << "Warning: unknown reserved key: " << key << " monster: " << getName() << std::endl;
+ return;
+ }
+
+ if (value != -1) {
+ int32_t oldValue;
+ getStorageValue(key, oldValue);
+
+ storageMap[key] = value;
+ } else {
+ storageMap.erase(key);
+ }
+}
+
+bool Monster::getStorageValue(const uint32_t key, int32_t& value) const
+{
+ auto it = storageMap.find(key);
+ if (it == storageMap.end()) {
+ value = -1;
+ return false;
+ }
+
+ value = it->second;
+ return true;
+}
diff --git a/monster.h b/monster.h
index b4da31b..beb1ac6 100644
--- a/monster.h
+++ b/monster.h
@@ -139,6 +139,9 @@ class Monster final : public Creature
void onThink(uint32_t interval) final;
+ void addStorageValue(const uint32_t key, const int32_t value);
+ bool getStorageValue(const uint32_t key, int32_t& value) const;
+
bool challengeCreature(Creature* creature) final;
bool convinceCreature(Creature* creature) final;
@@ -153,6 +156,13 @@ class Monster final : public Creature
bool searchTarget(TargetSearchType_t searchType = TARGETSEARCH_DEFAULT);
bool selectTarget(Creature* creature);
+ uint64_t getExperience() const {
+ return experience;
+ }
+ void setExperience(uint64_t value) {
+ experience = value;
+ }
+
const CreatureList& getTargetList() const {
return targetList;
}
@@ -184,6 +194,8 @@ class Monster final : public Creature
MonsterType* mType;
Spawn* spawn;
+ std::map<uint32_t, int32_t> storageMap;
+
int64_t lastMeleeAttack;
uint32_t attackTicks;
@@ -252,7 +264,7 @@ class Monster final : public Creature
bool isOpponent(const Creature* creature) const;
uint64_t getLostExperience() const final {
- return skillLoss ? mType->experience : 0;
+ return skillLoss ? experience : 0;
}
uint16_t getLookCorpse() const final {
return mType->lookcorpse;
@@ -272,4 +284,4 @@ class Monster final : public Creature
friend class LuaScriptInterface;
};
-#endif
\ No newline at end of file
+#endif
--
2.6.3.windows.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment