summaryrefslogtreecommitdiffstats
path: root/source/Entities
diff options
context:
space:
mode:
authorDaniel O'Brien <marmot.daniel@gmail.com>2013-11-16 11:38:57 +0100
committerDaniel O'Brien <marmot.daniel@gmail.com>2013-11-16 11:38:57 +0100
commit359539293782713d47e51775b65ee91fc89994e4 (patch)
tree7e6fd07f9e20b6568b3e461dbd55bc7542b07358 /source/Entities
parentfix possible threadlock issue, changed function names to be closer to standard (diff)
downloadcuberite-359539293782713d47e51775b65ee91fc89994e4.tar
cuberite-359539293782713d47e51775b65ee91fc89994e4.tar.gz
cuberite-359539293782713d47e51775b65ee91fc89994e4.tar.bz2
cuberite-359539293782713d47e51775b65ee91fc89994e4.tar.lz
cuberite-359539293782713d47e51775b65ee91fc89994e4.tar.xz
cuberite-359539293782713d47e51775b65ee91fc89994e4.tar.zst
cuberite-359539293782713d47e51775b65ee91fc89994e4.zip
Diffstat (limited to 'source/Entities')
-rw-r--r--source/Entities/Player.cpp41
-rw-r--r--source/Entities/Player.h4
2 files changed, 41 insertions, 4 deletions
diff --git a/source/Entities/Player.cpp b/source/Entities/Player.cpp
index 891506802..e8fc795d7 100644
--- a/source/Entities/Player.cpp
+++ b/source/Entities/Player.cpp
@@ -66,8 +66,9 @@ cPlayer::cPlayer(cClientHandle* a_Client, const AString & a_PlayerName)
, m_EatingFinishTick(-1)
, m_IsChargingBow(false)
, m_BowCharge(0)
- , m_XpTotal(0)
- , m_IsExperienceDirty(false)
+ , m_CurrentXp(0)
+ , m_LifetimeTotalXp(0)
+ , m_bDirtyExperience(false)
{
LOGD("Created a player object for \"%s\" @ \"%s\" at %p, ID %d",
a_PlayerName.c_str(), a_Client->GetIPString().c_str(),
@@ -383,6 +384,31 @@ short cPlayer::AddExperience(short a_Xp_delta)
+short cPlayer::SpendExperience(short a_Xp_delta)
+{
+ if(a_Xp_delta < 0)
+ {
+ // Value was negative, abort and report
+ LOGWARNING("Attempt was made to decrement Xp by %d, must be positive",
+ a_Xp_delta);
+ return -1; // Should we instead just return the current Xp?
+ }
+
+ m_CurrentXp -= a_Xp_delta;
+
+ LOGD("Player \"%s\" spent %d experience, total is now: %d",
+ m_PlayerName.c_str(), a_Xp_delta, m_XpTotal);
+
+ // Set experience to be updated
+ m_bDirtyExperience = true;
+
+ return m_CurrentXp;
+}
+
+
+
+
+
void cPlayer::StartChargingBow(void)
{
LOGD("Player \"%s\" started charging their bow", m_PlayerName.c_str());
@@ -791,6 +817,11 @@ void cPlayer::Respawn(void)
m_FoodLevel = MAX_FOOD_LEVEL;
m_FoodSaturationLevel = 5;
+ // Reset Experience
+ m_CurrentXp = MIN_EXPERIENCE;
+ m_LifetimeTotalXp = MIN_EXPERIENCE;
+ // ToDo: send score to client? How?
+
m_ClientHandle->SendRespawn();
// Extinguish the fire:
@@ -1449,7 +1480,8 @@ bool cPlayer::LoadFromDisk()
m_FoodSaturationLevel = root.get("foodSaturation", MAX_FOOD_LEVEL).asDouble();
m_FoodTickTimer = root.get("foodTickTimer", 0).asInt();
m_FoodExhaustionLevel = root.get("foodExhaustion", 0).asDouble();
- m_XpTotal = (short) root.get("experience", 0).asInt();
+ m_LifetimeTotalXp = (short) root.get("xpTotal", 0).asInt();
+ m_CurrentXp = (short) root.get("xpCurrent", 0).asInt();
//SetExperience(root.get("experience", 0).asInt());
@@ -1493,7 +1525,8 @@ bool cPlayer::SaveToDisk()
root["rotation"] = JSON_PlayerRotation;
root["inventory"] = JSON_Inventory;
root["health"] = m_Health;
- root["experience"] = m_XpTotal;
+ root["xpTotal"] = m_LifetimeTotalXp;
+ root["xpCurrent"] = m_CurrentXp;
root["air"] = m_AirLevel;
root["food"] = m_FoodLevel;
root["foodSaturation"] = m_FoodSaturationLevel;
diff --git a/source/Entities/Player.h b/source/Entities/Player.h
index 2fc0d5ac9..01a864149 100644
--- a/source/Entities/Player.h
+++ b/source/Entities/Player.h
@@ -32,6 +32,7 @@ public:
EATING_TICKS = 30, ///< Number of ticks it takes to eat an item
MAX_AIR_LEVEL = 300,
DROWNING_TICKS = 10, //number of ticks per heart of damage
+ MIN_EXPERIENCE = 0,
} ;
// tolua_end
@@ -78,6 +79,9 @@ public:
*/
short AddExperience(short a_Xp_delta);
+ /// "Spend" some experience - ie on enchanting, returns new currentXp
+ short SpendExperience(short a_Xp_delta);
+
/// Gets the experience total - XpTotal for score on death
inline short GetXpLifetimeTotal(void) { return m_LifetimeTotalXp; }