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 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) (limited to 'source/Entities/Player.cpp') 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; -- cgit v1.2.3