summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--source/Entities/Player.cpp48
-rw-r--r--source/Entities/Player.h23
2 files changed, 45 insertions, 26 deletions
diff --git a/source/Entities/Player.cpp b/source/Entities/Player.cpp
index f25483375..891506802 100644
--- a/source/Entities/Player.cpp
+++ b/source/Entities/Player.cpp
@@ -67,6 +67,7 @@ cPlayer::cPlayer(cClientHandle* a_Client, const AString & a_PlayerName)
, m_IsChargingBow(false)
, m_BowCharge(0)
, m_XpTotal(0)
+ , m_IsExperienceDirty(false)
{
LOGD("Created a player object for \"%s\" @ \"%s\" at %p, ID %d",
a_PlayerName.c_str(), a_Client->GetIPString().c_str(),
@@ -222,6 +223,12 @@ void cPlayer::Tick(float a_Dt, cChunk & a_Chunk)
{
m_BowCharge += 1;
}
+
+ //handle updating experience
+ if (m_bDirtyExperience)
+ {
+ SendExperience();
+ }
if (m_bDirtyPosition)
{
@@ -306,21 +313,21 @@ short cPlayer::XpForLevel(short a_Level)
-short cPlayer::XpGetLevel()
+short cPlayer::GetXpLevel()
{
- return CalcLevelFromXp(m_XpTotal);
+ return CalcLevelFromXp(m_CurrentXp);
}
-float cPlayer::XpGetPercentage()
+float cPlayer::GetXpPercentage()
{
- short int currentLevel = CalcLevelFromXp(m_XpTotal);
+ short int currentLevel = CalcLevelFromXp(m_CurrentXp);
short int currentLevel_XpBase = XpForLevel(currentLevel);
- return (float)(m_XpTotal - currentLevel_XpBase) /
+ return (float)(m_CurrentXp - currentLevel_XpBase) /
(float)(XpForLevel(1+currentLevel) - currentLevel_XpBase);
}
@@ -328,18 +335,18 @@ float cPlayer::XpGetPercentage()
-bool cPlayer::SetExperience(short int a_XpTotal)
+bool cPlayer::SetCurrentExperience(short int a_XpTotal)
{
- if(!(a_XpTotal >= 0) || (a_XpTotal > (SHRT_MAX - m_XpTotal)))
+ if(!(a_XpTotal >= 0) || (a_XpTotal > (SHRT_MAX - m_CurrentXp)))
{
LOGWARNING("Tried to update experiece with an invalid Xp value: %d", a_XpTotal);
return false; //oops, they gave us a dodgey number
}
- m_XpTotal = a_XpTotal;
+ m_CurrentXp = a_XpTotal;
- //send details to client
- SendExperience();
+ // Set experience to be updated
+ m_bDirtyExperience = true;
return true;
}
@@ -352,20 +359,24 @@ short cPlayer::AddExperience(short a_Xp_delta)
{
if(a_Xp_delta < 0)
{
- //value was negative, abort and report
+ // Value was negative, abort and report
LOGWARNING("Attempt was made to increment Xp by %d, must be positive",
a_Xp_delta);
- return -1; //should we instead just return the current Xp?
+ return -1; // Should we instead just return the current Xp?
}
-
- LOGD("Player \"%s\" earnt %d experience", m_PlayerName.c_str(), a_Xp_delta);
- m_XpTotal += a_Xp_delta;
+ m_CurrentXp += a_Xp_delta;
+
+ // Update total for score calculation
+ m_LifetimeTotalXp += a_Xp_delta;
+
+ LOGD("Player \"%s\" earnt %d experience, total is now: %d",
+ m_PlayerName.c_str(), a_Xp_delta, m_XpTotal);
- //send details to client
- SendExperience();
+ // Set experience to be updated
+ m_bDirtyExperience = true;
- return m_XpTotal;
+ return m_CurrentXp;
}
@@ -620,6 +631,7 @@ void cPlayer::SendExperience(void)
if (m_ClientHandle != NULL)
{
m_ClientHandle->SendExperience();
+ m_bDirtyExperience = false;
}
}
diff --git a/source/Entities/Player.h b/source/Entities/Player.h
index aeec9f361..2fc0d5ac9 100644
--- a/source/Entities/Player.h
+++ b/source/Entities/Player.h
@@ -71,21 +71,24 @@ public:
Returns true on success
"should" really only be called at init or player death, plugins excepted
*/
- bool SetExperience(short a_XpTotal);
+ bool SetCurrentExperience(short a_XpTotal);
/* Adds Xp, "should" not inc more than MAX_EXPERIENCE_ORB_SIZE unless you're a plugin being funny, *cough* cheating
- Returns the new total experience, -1 on error
+ Returns the new current experience, -1 on error
*/
short AddExperience(short a_Xp_delta);
- /// Gets the experience total - XpTotal
- inline short XpGetTotal(void) { return m_XpTotal; }
+ /// Gets the experience total - XpTotal for score on death
+ inline short GetXpLifetimeTotal(void) { return m_LifetimeTotalXp; }
+
+ /// Gets the currrent experience
+ inline short GetCurrentXp(void) { return m_CurrentXp; }
/// Gets the current level - XpLevel
- short XpGetLevel(void);
+ short GetXpLevel(void);
/// Gets the experience bar percentage - XpP
- float XpGetPercentage(void);
+ float GetXpPercentage(void);
// tolua_end
@@ -415,13 +418,17 @@ protected:
Int64 m_EatingFinishTick;
/// Player Xp level
- short int m_XpTotal;
+ short int m_LifetimeTotalXp;
+ short int m_CurrentXp;
+
+ // flag saying we need to send a xp update to client
+ bool m_bDirtyExperience;
/// Caculates the Xp needed for a given level, ref: http://minecraft.gamepedia.com/XP
static short XpForLevel(short int a_Level);
/// inverse of XpAtLevel, ref: http://minecraft.gamepedia.com/XP values are as per this with pre-calculations
- static short CalcLevelFromXp(short int a_XpTotal);
+ static short CalcLevelFromXp(short int a_XpCurrent);
bool m_IsChargingBow;
int m_BowCharge;