1
0

fix possible threadlock issue, changed function names to be closer to standard

This commit is contained in:
Daniel O'Brien 2013-11-16 20:29:57 +11:00
parent df4aa6c864
commit c0c8fe1bcd
2 changed files with 45 additions and 26 deletions

View File

@ -67,6 +67,7 @@ cPlayer::cPlayer(cClientHandle* a_Client, const AString & a_PlayerName)
, m_IsChargingBow(false) , m_IsChargingBow(false)
, m_BowCharge(0) , m_BowCharge(0)
, m_XpTotal(0) , m_XpTotal(0)
, m_IsExperienceDirty(false)
{ {
LOGD("Created a player object for \"%s\" @ \"%s\" at %p, ID %d", LOGD("Created a player object for \"%s\" @ \"%s\" at %p, ID %d",
a_PlayerName.c_str(), a_Client->GetIPString().c_str(), 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; m_BowCharge += 1;
} }
//handle updating experience
if (m_bDirtyExperience)
{
SendExperience();
}
if (m_bDirtyPosition) 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); short int currentLevel_XpBase = XpForLevel(currentLevel);
return (float)(m_XpTotal - currentLevel_XpBase) / return (float)(m_CurrentXp - currentLevel_XpBase) /
(float)(XpForLevel(1+currentLevel) - 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); LOGWARNING("Tried to update experiece with an invalid Xp value: %d", a_XpTotal);
return false; //oops, they gave us a dodgey number return false; //oops, they gave us a dodgey number
} }
m_XpTotal = a_XpTotal; m_CurrentXp = a_XpTotal;
//send details to client // Set experience to be updated
SendExperience(); m_bDirtyExperience = true;
return true; return true;
} }
@ -352,20 +359,24 @@ short cPlayer::AddExperience(short a_Xp_delta)
{ {
if(a_Xp_delta < 0) 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", LOGWARNING("Attempt was made to increment Xp by %d, must be positive",
a_Xp_delta); 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;
//send details to client // Update total for score calculation
SendExperience(); m_LifetimeTotalXp += a_Xp_delta;
return m_XpTotal; LOGD("Player \"%s\" earnt %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;
} }
@ -620,6 +631,7 @@ void cPlayer::SendExperience(void)
if (m_ClientHandle != NULL) if (m_ClientHandle != NULL)
{ {
m_ClientHandle->SendExperience(); m_ClientHandle->SendExperience();
m_bDirtyExperience = false;
} }
} }

View File

@ -71,21 +71,24 @@ public:
Returns true on success Returns true on success
"should" really only be called at init or player death, plugins excepted "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 /* 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); short AddExperience(short a_Xp_delta);
/// Gets the experience total - XpTotal /// Gets the experience total - XpTotal for score on death
inline short XpGetTotal(void) { return m_XpTotal; } inline short GetXpLifetimeTotal(void) { return m_LifetimeTotalXp; }
/// Gets the currrent experience
inline short GetCurrentXp(void) { return m_CurrentXp; }
/// Gets the current level - XpLevel /// Gets the current level - XpLevel
short XpGetLevel(void); short GetXpLevel(void);
/// Gets the experience bar percentage - XpP /// Gets the experience bar percentage - XpP
float XpGetPercentage(void); float GetXpPercentage(void);
// tolua_end // tolua_end
@ -415,13 +418,17 @@ protected:
Int64 m_EatingFinishTick; Int64 m_EatingFinishTick;
/// Player Xp level /// 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 /// Caculates the Xp needed for a given level, ref: http://minecraft.gamepedia.com/XP
static short XpForLevel(short int a_Level); static short XpForLevel(short int a_Level);
/// inverse of XpAtLevel, ref: http://minecraft.gamepedia.com/XP values are as per this with pre-calculations /// 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; bool m_IsChargingBow;
int m_BowCharge; int m_BowCharge;