diff options
Diffstat (limited to '')
-rw-r--r-- | source/Entities/Player.cpp | 164 | ||||
-rw-r--r-- | source/Entities/Player.h | 55 |
2 files changed, 215 insertions, 4 deletions
diff --git a/source/Entities/Player.cpp b/source/Entities/Player.cpp index 2e4199629..436ae0cfc 100644 --- a/source/Entities/Player.cpp +++ b/source/Entities/Player.cpp @@ -1,4 +1,4 @@ - + #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules #include "Player.h" @@ -33,6 +33,7 @@ + cPlayer::cPlayer(cClientHandle* a_Client, const AString & a_PlayerName) : super(etPlayer, 0.6, 1.8) , m_GameMode(eGameMode_NotSet) @@ -65,6 +66,9 @@ cPlayer::cPlayer(cClientHandle* a_Client, const AString & a_PlayerName) , m_EatingFinishTick(-1) , m_IsChargingBow(false) , m_BowCharge(0) + , 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(), @@ -220,6 +224,12 @@ void cPlayer::Tick(float a_Dt, cChunk & a_Chunk) { m_BowCharge += 1; } + + //handle updating experience + if (m_bDirtyExperience) + { + SendExperience(); + } if (m_bDirtyPosition) { @@ -260,6 +270,130 @@ void cPlayer::Tick(float a_Dt, cChunk & a_Chunk) +short cPlayer::CalcLevelFromXp(short a_XpTotal) +{ + //level 0 to 15 + if(a_XpTotal <= XP_TO_LEVEL15) + { + return a_XpTotal / XP_PER_LEVEL_TO15; + } + + //level 30+ + if(a_XpTotal > XP_TO_LEVEL30) + { + return (short) (151.5 + sqrt( 22952.25 - (14 * (2220 - a_XpTotal)))) / 7; + } + + //level 16 to 30 + return (short) ( 29.5 + sqrt( 870.25 - (6 * ( 360 - a_XpTotal )))) / 3; +} + + + + + +short cPlayer::XpForLevel(short a_Level) +{ + //level 0 to 15 + if(a_Level <= 15) + { + return a_Level * XP_PER_LEVEL_TO15; + } + + //level 30+ + if(a_Level >= 31) + { + return (short) ( (3.5 * a_Level * a_Level) - (151.5 * a_Level) + 2220 ); + } + + //level 16 to 30 + return (short) ( (1.5 * a_Level * a_Level) - (29.5 * a_Level) + 360 ); +} + + + + + +short cPlayer::GetXpLevel() +{ + return CalcLevelFromXp(m_CurrentXp); +} + + + + + +float cPlayer::GetXpPercentage() +{ + short int currentLevel = CalcLevelFromXp(m_CurrentXp); + short int currentLevel_XpBase = XpForLevel(currentLevel); + + return (float)(m_CurrentXp - currentLevel_XpBase) / + (float)(XpForLevel(1+currentLevel) - currentLevel_XpBase); +} + + + + + +bool cPlayer::SetCurrentExperience(short int a_CurrentXp) +{ + if(!(a_CurrentXp >= 0) || (a_CurrentXp > (SHRT_MAX - m_LifetimeTotalXp))) + { + LOGWARNING("Tried to update experiece with an invalid Xp value: %d", a_CurrentXp); + return false; //oops, they gave us a dodgey number + } + + m_CurrentXp = a_CurrentXp; + + // Set experience to be updated + m_bDirtyExperience = true; + + return true; +} + + + + + +short cPlayer::DeltaExperience(short a_Xp_delta) +{ + //ToDo: figure out a better name?... + if(a_Xp_delta > (SHRT_MAX - m_LifetimeTotalXp)) + { + // Value was bad, abort and report + LOGWARNING("Attempt was made to increment Xp by %d, which was bad", + a_Xp_delta); + return -1; // Should we instead just return the current Xp? + } + + m_CurrentXp += a_Xp_delta; + + // Make sure they didn't subtract too much + if(m_CurrentXp < MIN_EXPERIENCE) + { + m_CurrentXp = MIN_EXPERIENCE; + } + + // Update total for score calculation + if(a_Xp_delta > 0) + { + m_LifetimeTotalXp += a_Xp_delta; + } + + LOGD("Player \"%s\" gained/lost %d experience, total is now: %d", + m_PlayerName.c_str(), a_Xp_delta, m_CurrentXp); + + // 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()); @@ -503,6 +637,19 @@ void cPlayer::SendHealth(void) +void cPlayer::SendExperience(void) +{ + if (m_ClientHandle != NULL) + { + m_ClientHandle->SendExperience(); + m_bDirtyExperience = false; + } +} + + + + + void cPlayer::ClearInventoryPaintSlots(void) { // Clear the list of slots that are being inventory-painted. Used by cWindow only @@ -655,6 +802,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: @@ -1278,7 +1430,7 @@ bool cPlayer::LoadFromDisk() LOGWARNING("Cannot read player data from file \"%s\"", SourceFile.c_str()); return false; } - f.Close(); + f.Close(); //cool kids play nice Json::Value root; Json::Reader reader; @@ -1307,12 +1459,16 @@ 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_LifetimeTotalXp = (short) root.get("xpTotal", 0).asInt(); + m_CurrentXp = (short) root.get("xpCurrent", 0).asInt(); + + //SetExperience(root.get("experience", 0).asInt()); m_GameMode = (eGameMode) root.get("gamemode", eGameMode_NotSet).asInt(); @@ -1354,6 +1510,8 @@ bool cPlayer::SaveToDisk() root["rotation"] = JSON_PlayerRotation; root["inventory"] = JSON_Inventory; root["health"] = m_Health; + 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 01efa3681..bda25715d 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 @@ -63,6 +64,35 @@ public: /// Returns the currently equipped boots; empty item if none virtual cItem GetEquippedBoots(void) const override { return m_Inventory.GetEquippedBoots(); } + + + // tolua_begin + + /** Sets the experience total + Returns true on success + "should" really only be called at init or player death, plugins excepted + */ + bool SetCurrentExperience(short a_XpTotal); + + /* changes Xp by Xp_delta, you "shouldn't" inc more than MAX_EXPERIENCE_ORB_SIZE + Wont't allow xp to go negative + Returns the new current experience, -1 on error + */ + short DeltaExperience(short a_Xp_delta); + + /// 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 GetXpLevel(void); + + /// Gets the experience bar percentage - XpP + float GetXpPercentage(void); + + // tolua_end /// Starts charging the equipped bow void StartChargingBow(void); @@ -244,6 +274,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(); } @@ -289,7 +321,7 @@ public: virtual bool IsSubmerged(void) const{ return m_IsSubmerged; } // tolua_end - + // cEntity overrides: virtual bool IsCrouched (void) const { return m_IsCrouched; } virtual bool IsSprinting(void) const { return m_IsSprinting; } @@ -308,6 +340,14 @@ protected: std::string m_PlayerName; std::string m_LoadedWorldName; + /// Xp Level stuff + enum + { + XP_TO_LEVEL15 = 255, + XP_PER_LEVEL_TO15 = 17, + XP_TO_LEVEL30 = 825 + } ; + /// Player's air level (for swimming) int m_AirLevel; @@ -378,6 +418,19 @@ protected: /// The world tick in which eating will be finished. -1 if not eating Int64 m_EatingFinishTick; + + /// Player Xp level + 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_CurrentXp); bool m_IsChargingBow; int m_BowCharge; |