summaryrefslogtreecommitdiffstats
path: root/source/Entities
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--source/Entities/Player.cpp49
-rw-r--r--source/Entities/Player.h16
2 files changed, 44 insertions, 21 deletions
diff --git a/source/Entities/Player.cpp b/source/Entities/Player.cpp
index 098417dc5..f25483375 100644
--- a/source/Entities/Player.cpp
+++ b/source/Entities/Player.cpp
@@ -262,7 +262,7 @@ void cPlayer::Tick(float a_Dt, cChunk & a_Chunk)
-int cPlayer::CalcLevelFromXp(int a_XpTotal)
+short cPlayer::CalcLevelFromXp(short a_XpTotal)
{
//level 0 to 15
if(a_XpTotal <= XP_TO_LEVEL15)
@@ -273,18 +273,18 @@ int cPlayer::CalcLevelFromXp(int a_XpTotal)
//level 30+
if(a_XpTotal > XP_TO_LEVEL30)
{
- return (int) (151.5 + sqrt( 22952.25 - (14 * (2220 - a_XpTotal)))) / 7;
+ return (short) (151.5 + sqrt( 22952.25 - (14 * (2220 - a_XpTotal)))) / 7;
}
//level 16 to 30
- return (int) ( 29.5 + sqrt( 870.25 - (6 * ( 360 - a_XpTotal )))) / 3;
+ return (short) ( 29.5 + sqrt( 870.25 - (6 * ( 360 - a_XpTotal )))) / 3;
}
-int cPlayer::XpForLevel(int a_Level)
+short cPlayer::XpForLevel(short a_Level)
{
//level 0 to 15
if(a_Level <= 15)
@@ -295,18 +295,18 @@ int cPlayer::XpForLevel(int a_Level)
//level 30+
if(a_Level >= 31)
{
- return (int) ( (3.5 * a_Level * a_Level) - (151.5 * a_Level) + 2220 );
+ return (short) ( (3.5 * a_Level * a_Level) - (151.5 * a_Level) + 2220 );
}
//level 16 to 30
- return (int) ( (1.5 * a_Level * a_Level) - (29.5 * a_Level) + 360 );
+ return (short) ( (1.5 * a_Level * a_Level) - (29.5 * a_Level) + 360 );
}
-int cPlayer::XpGetLevel()
+short cPlayer::XpGetLevel()
{
return CalcLevelFromXp(m_XpTotal);
}
@@ -317,18 +317,20 @@ int cPlayer::XpGetLevel()
float cPlayer::XpGetPercentage()
{
- int currentLevel = CalcLevelFromXp(m_XpTotal);
+ short int currentLevel = CalcLevelFromXp(m_XpTotal);
+ short int currentLevel_XpBase = XpForLevel(currentLevel);
- return (float)m_XpTotal / (float)XpForLevel(1+currentLevel);
+ return (float)(m_XpTotal - currentLevel_XpBase) /
+ (float)(XpForLevel(1+currentLevel) - currentLevel_XpBase);
}
-bool cPlayer::SetExperience(int a_XpTotal)
+bool cPlayer::SetExperience(short int a_XpTotal)
{
- if(!(a_XpTotal >= 0) || (a_XpTotal > (INT_MAX - m_XpTotal)))
+ if(!(a_XpTotal >= 0) || (a_XpTotal > (SHRT_MAX - m_XpTotal)))
{
LOGWARNING("Tried to update experiece with an invalid Xp value: %d", a_XpTotal);
return false; //oops, they gave us a dodgey number
@@ -336,6 +338,9 @@ bool cPlayer::SetExperience(int a_XpTotal)
m_XpTotal = a_XpTotal;
+ //send details to client
+ SendExperience();
+
return true;
}
@@ -343,7 +348,7 @@ bool cPlayer::SetExperience(int a_XpTotal)
-int cPlayer::AddExperience(int a_Xp_delta)
+short cPlayer::AddExperience(short a_Xp_delta)
{
if(a_Xp_delta < 0)
{
@@ -357,6 +362,9 @@ int cPlayer::AddExperience(int a_Xp_delta)
m_XpTotal += a_Xp_delta;
+ //send details to client
+ SendExperience();
+
return m_XpTotal;
}
@@ -607,6 +615,18 @@ void cPlayer::SendHealth(void)
+void cPlayer::SendExperience(void)
+{
+ if (m_ClientHandle != NULL)
+ {
+ m_ClientHandle->SendExperience();
+ }
+}
+
+
+
+
+
void cPlayer::ClearInventoryPaintSlots(void)
{
// Clear the list of slots that are being inventory-painted. Used by cWindow only
@@ -1411,14 +1431,15 @@ bool cPlayer::LoadFromDisk()
SetRoll ((float)JSON_PlayerRotation[(unsigned int)2].asDouble());
}
- m_Health = root.get("health", 0).asInt();
+ m_Health = root.get("health", 0).asInt();
m_AirLevel = root.get("air", MAX_AIR_LEVEL).asInt();
m_FoodLevel = root.get("food", MAX_FOOD_LEVEL).asInt();
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();
- SetExperience(root.get("experience", 0).asInt());
+ //SetExperience(root.get("experience", 0).asInt());
m_GameMode = (eGameMode) root.get("gamemode", eGameMode_NotSet).asInt();
diff --git a/source/Entities/Player.h b/source/Entities/Player.h
index ab2f94d4c..aeec9f361 100644
--- a/source/Entities/Player.h
+++ b/source/Entities/Player.h
@@ -71,18 +71,18 @@ public:
Returns true on success
"should" really only be called at init or player death, plugins excepted
*/
- bool SetExperience(int a_XpTotal);
+ bool SetExperience(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
*/
- int AddExperience(int a_Xp_delta);
+ short AddExperience(short a_Xp_delta);
/// Gets the experience total - XpTotal
- inline int XpGetTotal(void) { return m_XpTotal; }
+ inline short XpGetTotal(void) { return m_XpTotal; }
/// Gets the current level - XpLevel
- int XpGetLevel(void);
+ short XpGetLevel(void);
/// Gets the experience bar percentage - XpP
float XpGetPercentage(void);
@@ -269,6 +269,8 @@ public:
void UseEquippedItem(void);
void SendHealth(void);
+
+ void SendExperience(void);
// In UI windows, the item that the player is dragging:
bool IsDraggingItem(void) const { return !m_DraggingItem.IsEmpty(); }
@@ -413,13 +415,13 @@ protected:
Int64 m_EatingFinishTick;
/// Player Xp level
- int m_XpTotal;
+ short int m_XpTotal;
/// Caculates the Xp needed for a given level, ref: http://minecraft.gamepedia.com/XP
- static int XpForLevel(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
- static int CalcLevelFromXp(int a_XpTotal);
+ static short CalcLevelFromXp(short int a_XpTotal);
bool m_IsChargingBow;
int m_BowCharge;