From 359539293782713d47e51775b65ee91fc89994e4 Mon Sep 17 00:00:00 2001 From: Daniel O'Brien Date: Sat, 16 Nov 2013 21:38:57 +1100 Subject: fixed bug and added SpendExperience() --- source/Entities/Player.cpp | 41 +++++++++++++++++++++++++++++++++++++---- source/Entities/Player.h | 4 ++++ 2 files changed, 41 insertions(+), 4 deletions(-) (limited to 'source/Entities') 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; } -- cgit v1.2.3