summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPhilipp Sehmisch <tmw@crushnet.org>2008-11-01 01:41:12 +0000
committerPhilipp Sehmisch <tmw@crushnet.org>2008-11-01 01:41:12 +0000
commitad5efe104cd7639009622393f16bfd93075fcb34 (patch)
treef9618bffd5e38c9f50722d36d4d77e8bfea06168 /src
parent3ec0d0f152be4381dc843bc1ca99f8731c8fb366 (diff)
downloadmanaserv-ad5efe104cd7639009622393f16bfd93075fcb34.tar.gz
manaserv-ad5efe104cd7639009622393f16bfd93075fcb34.tar.bz2
manaserv-ad5efe104cd7639009622393f16bfd93075fcb34.tar.xz
manaserv-ad5efe104cd7639009622393f16bfd93075fcb34.zip
Added script bindings for manipulating the characters experience.
Diffstat (limited to 'src')
-rw-r--r--src/game-server/character.cpp1
-rw-r--r--src/game-server/character.hpp10
-rw-r--r--src/scripting/lua.cpp74
3 files changed, 80 insertions, 5 deletions
diff --git a/src/game-server/character.cpp b/src/game-server/character.cpp
index 8758b64e..bf8355cd 100644
--- a/src/game-server/character.cpp
+++ b/src/game-server/character.cpp
@@ -322,6 +322,7 @@ void Character::receiveExperience(size_t skill, int experience)
// add exp
long int newExp = mExperience.at(skill - CHAR_SKILL_BEGIN) + experience;
if (newExp > INT_MAX) newExp = INT_MAX; // avoid integer overflow.
+ if (newExp < 0) newExp = 0; // avoid integer underflow/negative exp
mExperience.at(skill - CHAR_SKILL_BEGIN) = newExp;
mModifiedExperience.insert(skill - CHAR_SKILL_BEGIN);
diff --git a/src/game-server/character.hpp b/src/game-server/character.hpp
index 30914344..03006712 100644
--- a/src/game-server/character.hpp
+++ b/src/game-server/character.hpp
@@ -248,6 +248,11 @@ class Character : public Being
{ mExperience[skill] = 0; receiveExperience(skill + CHAR_SKILL_BEGIN , value) ; }
/**
+ * Returns the exp needed to reach a specific skill level
+ */
+ static int expForLevel(int level);
+
+ /**
* Tries to use a character point to increase a
* basic attribute
*/
@@ -301,11 +306,6 @@ class Character : public Being
void flagAttribute(int);
/**
- * Returns the exp needed to reach a specific skill level
- */
- static int expForLevel(int level);
-
- /**
* Returns the exp needed for next skill levelup
*/
int getExpNeeded(size_t skill);
diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp
index c0c7a95c..34e66427 100644
--- a/src/scripting/lua.cpp
+++ b/src/scripting/lua.cpp
@@ -902,6 +902,77 @@ static int LuaEffect_Create(lua_State *s)
return 0;
}
+/**
+ * Gets the exp total in a skill of a specific character
+ * tmw.chr_get_exp (being, skill)
+ */
+static int LuaChr_GetExp(lua_State *s)
+{
+ Character *c = getCharacter(s, 1);
+ if (!c)
+ {
+ raiseScriptError(s, "luaChr_GetExp called for nonexistent character.");
+ return 0;
+ }
+
+ int skill = lua_tointeger(s, 2);
+ if (skill < CHAR_SKILL_BEGIN || skill >= CHAR_SKILL_END)
+ {
+ raiseScriptError(s, "luaChr_GetExp called for nonexistent skill number %d.", skill);
+ return 0;
+ }
+
+ int exp = c->getExperience(skill - CHAR_SKILL_BEGIN);
+
+ lua_pushinteger(s, exp);
+ return 1;
+}
+
+
+/**
+ * Gives the character a certain amount of experience points
+ * in a skill. Can also be used to reduce the exp amount when
+ * desired.
+ * tmw.chr_give_exp (being, skill, amount)
+ */
+static int LuaChr_GiveExp(lua_State *s)
+{
+ Character *c = getCharacter(s, 1);
+ if (!c)
+ {
+ raiseScriptError(s, "luaChr_GiveExp called for nonexistent character.");
+ return 0;
+ }
+
+ int skill = lua_tointeger(s, 2);
+ if (skill < CHAR_SKILL_BEGIN || skill >= CHAR_SKILL_END)
+ {
+ raiseScriptError(s, "luaChr_GiveExp called for nonexistent skill number %d.", skill);
+ return 0;
+ }
+
+ int exp = lua_tointeger(s, 3);
+
+ c->receiveExperience(skill, exp);
+
+ return 0;
+}
+
+
+/**
+ * Returns the exp total necessary to reach a specific skill level.
+ * tmw.exp_for_level (level)
+ */
+static int LuaExpForLevel(lua_State *s)
+{
+ int level = lua_tointeger(s, 1);
+
+ int exp = Character::expForLevel(level);
+
+ lua_pushinteger(s, exp);
+ return 1;
+}
+
LuaScript::LuaScript():
nbArgs(-1)
@@ -923,6 +994,9 @@ LuaScript::LuaScript():
{ "chr_get_quest", &LuaChr_GetQuest },
{ "chr_set_quest", &LuaChr_SetQuest },
{ "chr_get_post", &LuaChr_GetPost },
+ { "chr_get_exp", &LuaChr_GetExp },
+ { "chr_give_exp", &LuaChr_GiveExp },
+ { "exp_for_level", &LuaExpForLevel },
{ "monster_create", &LuaMonster_Create },
{ "being_walk", &LuaBeing_Walk },
{ "being_say", &LuaBeing_Say },