summaryrefslogtreecommitdiff
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
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.
-rw-r--r--ChangeLog10
-rw-r--r--data/scripts/libs/libtmw.lua11
-rw-r--r--data/scripts/test.lua19
-rw-r--r--src/game-server/character.cpp1
-rw-r--r--src/game-server/character.hpp10
-rw-r--r--src/scripting/lua.cpp74
6 files changed, 120 insertions, 5 deletions
diff --git a/ChangeLog b/ChangeLog
index 002010fd..0584405a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2008-11-01 Philipp Sehmisch <tmw@crushnet.org>
+
+ * src/game-server/character.hpp, src/game-server/scripting/lua.cpp: Added
+ script bindings for manipulating the characters experience.
+ * src/game-server/character.cpp: Catched a potential integer underflow in
+ experience calculation.
+ * data/scripts/lib/test.lua: added constants for character skills.
+ * data/scripts/test.lua: Added example script for manipulating character
+ experience.
+
2008-10-31 David Athay <ko2fan@gmail.com>
* src/utils/string.cpp, src/utils/string.hpp, src/Makefile.am,
diff --git a/data/scripts/libs/libtmw.lua b/data/scripts/libs/libtmw.lua
index 251e20a0..d1e397e4 100644
--- a/data/scripts/libs/libtmw.lua
+++ b/data/scripts/libs/libtmw.lua
@@ -52,6 +52,17 @@ ATTR_DEXTERITY = 18
ATTR_VITALITY = 19
ATTR_INTELLIGENCE = 20
ATTR_WILLPOWER = 21
+SKILL_WEAPON_NONE = 22
+SKILL_WEAPON_KNIFE = 23
+SKILL_WEAPON_SWORD = 24
+SKILL_WEAPON_POLEARM = 25
+SKILL_WEAPON_STAFF = 26
+SKILL_WEAPON_WHIP = 27
+SKILL_WEAPON_BOW = 28
+SKILL_WEAPON_SHOOTING = 29
+SKILL_WEAPON_MACE = 30
+SKILL_WEAPON_AXE = 31
+SKILL_WEAPON_THROWN = 32
TILESIZE = 32
HOURS = 3600
diff --git a/data/scripts/test.lua b/data/scripts/test.lua
index 7f96a61e..98892951 100644
--- a/data/scripts/test.lua
+++ b/data/scripts/test.lua
@@ -25,6 +25,7 @@ atinit(function()
create_npc("Fire Demon", 202, 58 * TILESIZE + 16, 35 * TILESIZE + 16, firedemon_talk, firedemon_update)
create_npc("Post Box", 158, 45 * TILESIZE + 16, 22 * TILESIZE + 16, post_talk)
create_npc("Fireworker", 158, 43 * TILESIZE, 23 * TILESIZE, fireworker_talk, npclib.walkaround_small)
+ create_npc("Axe Trainer", 126, 65 * TILESIZE, 18 * TILESIZE, axetrainer_talk, nil)
tmw.trigger_create(56 * TILESIZE, 32 * TILESIZE, 64, 64, "patrol_waypoint", 1, true)
tmw.trigger_create(63 * TILESIZE, 32 * TILESIZE, 64, 64, "patrol_waypoint", 2, true)
@@ -213,3 +214,21 @@ function fireworker_talk(npc, ch)
end
end
end
+
+function axetrainer_talk(npc, ch)
+ do_message(npc, ch, "I am the axe trainer. Do you want to get better at using axes?")
+ local answer = do_choice(npc, ch, "Please train me, master.", "I am good enough with axes.")
+ if answer == 1 then
+ local newexp = tmw.chr_get_exp(ch, SKILL_WEAPON_AXE) + 100
+ local nextlevel = tmw.exp_for_level(tmw.being_get_attribute(ch, SKILL_WEAPON_AXE) + 1)
+ tmw.chr_give_exp(ch, SKILL_WEAPON_AXE, 100)
+ local message = "I gave you 100 axe exp."
+ if newexp > nextlevel then
+ message = message.." This should be enough to reach the next level."
+ else
+ message = message.." You will still need "..tostring(nextlevel - newexp).." exp to reach the next level."
+ end
+ message = message.." I should really stop doing this when the server goes live."
+ do_message(npc, ch, message);
+ end
+end
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 },